Debian Codegraph @

madrisan-nagios-plugins-linux/nagios-plugins-linux/lib/cpudesc.c

raw symbols SCIP
1 // SPDX-License-Identifier: GPL-3.0-or-later
2 /*
3 * License: GPLv3+
4 * Copyright (c) 2014-2015,2022 Davide Madrisan <[email protected]>
5 *
6 * A library for checking the CPU features
7 *
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>. */
20
21 #ifndef _GNU_SOURCE
22 # define _GNU_SOURCE /* activate extra prototypes for glibc */
23 #endif
24
25 #include <sys/utsname.h>
26 #include <ctype.h>
27 #include <errno.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31
32 #include "common.h"
33 #include "cputopology.h"
34 #include "string-macros.h"
35 #include "messages.h"
36 #include "procparser.h"
37 #include "sysfsparser.h"
38 #include "system.h"
39 #include "xalloc.h"
40
41 #define PATH_PROC_CPUINFO "/proc/cpuinfo"
42
43 #define PATH_SYS_SYSTEM PATH_SYS "/devices/system"
44 #define PATH_SYS_CPU PATH_SYS_SYSTEM "/cpu"
45
46 bool
47 get_processor_is_hot_pluggable (unsigned int cpu)
48 {
49 return sysfsparser_path_exist (PATH_SYS_CPU "/cpu%u/online", cpu);
50 }
51
52 int
53 get_processor_is_online (unsigned int cpu)
54 {
55 bool path_exist =
56 sysfsparser_path_exist (PATH_SYS_CPU "/cpu%u/online", cpu);
57
58 if (false == path_exist)
59 return -1;
60
61 unsigned long long value;
62
63 sysfsparser_getvalue (&value, PATH_SYS_CPU "/cpu%u/online", cpu);
64 return value;
65 }
66
67 enum /* CPU modes */
68 {
69 MODE_32BIT = (1 << 1),
70 MODE_64BIT = (1 << 2)
71 };
72
73 struct cpu_desc
74 {
75 int refcount;
76
77 char *arch;
78 char *vendor;
79 char *family;
80 char *model;
81 char *modelname;
82 char *virtflag; /* virtualization flag (vmx, svm) */
83 char *mhz;
84 char *flags; /* x86 */
85 int mode;
86 int ncpus; /* number of present CPUs */
87 int ncpuspos; /* maximal possible CPUs */
88 };
89
90 /* Allocates space for a new cpu_desc object.
91 * Returns 0 if all went ok. Errors are returned as negative values. */
92 int
93 cpu_desc_new (struct cpu_desc **cpudesc)
94 {
95 struct cpu_desc *d;
96
97 d = calloc (1, sizeof (struct cpu_desc));
98 if (!d)
99 return -ENOMEM;
100
101 d->refcount = 1;
102
103 *cpudesc = d;
104 return 0;
105 }
106
107 /* Fill the cpu_desc structure pointed with the values found in the
108 * proc and sysfs filesystems */
109
110 void
111 cpu_desc_read (struct cpu_desc *cpudesc)
112 {
113 char *line = NULL;
114 FILE *fp;
115 size_t len = 0;
116 ssize_t chread;
117 struct utsname utsbuf;
118
119 if (cpudesc == NULL)
120 return;
121
122 if ((fp = fopen (PATH_PROC_CPUINFO, "r")) == NULL)
123 plugin_error (STATE_UNKNOWN, errno, "error opening %s", PATH_PROC_CPUINFO);
124
125 if (uname (&utsbuf) == -1)
126 plugin_error (STATE_UNKNOWN, errno, "uname() failed");
127 cpudesc->arch = xstrdup (utsbuf.machine);
128
129 cpudesc->ncpus = get_processor_number_total ();
130 cpudesc->ncpuspos = get_processor_number_kernel_max ();
131
132 cpudesc->mode = 0;
133 #if defined(__alpha__) || defined(__ia64__)
134 cpudesc->mode |= MODE_64BIT; /* 64bit platforms only */
135 #endif
136 /* platforms with 64bit flag in /proc/cpuinfo, define
137 * 32bit default here */
138 #if defined(__i386__) || defined(__x86_64__) || \
139 defined(__s390x__) || defined(__s390__) || defined(__sparc_v9__)
140 cpudesc->mode |= MODE_32BIT;
141 #endif
142
143 while ((chread = getline (&line, &len, fp)) != -1)
144 {
145 if (linelookup (line, "vendor", &cpudesc->vendor));
146 else if (linelookup (line, "vendor_id", &cpudesc->vendor));
147 else if (linelookup (line, "family", &cpudesc->family));
148 else if (linelookup (line, "cpu family", &cpudesc->family));
149 else if (linelookup (line, "model", &cpudesc->model));
150 else if (linelookup (line, "model name", &cpudesc->modelname)) ;
151 else if (linelookup (line, "cpu MHz", &cpudesc->mhz)) ;
152 else if (linelookup (line, "flags", &cpudesc->flags)) ; /* x86 */
153 else
154 continue;
155 }
156
157 if (cpudesc->flags)
158 {
159 size_t buflen = strlen (cpudesc->flags) + 2;
160 char *buf = xmalloc (buflen);
161
162 snprintf (buf, buflen, " %s ", cpudesc->flags);
163 if (strstr (buf, " svm "))
164 cpudesc->virtflag = xstrdup ("svm");
165 else if (strstr (buf, " vmx "))
166 cpudesc->virtflag = xstrdup ("vmx");
167 if (strstr (buf, " lm "))
168 cpudesc->mode |= MODE_32BIT | MODE_64BIT; /* x86_64 */
169 if (strstr (buf, " zarch "))
170 cpudesc->mode |= MODE_32BIT | MODE_64BIT; /* s390x */
171 if (strstr (buf, " sun4v ") || strstr (buf, " sun4u "))
172 cpudesc->mode |= MODE_32BIT | MODE_64BIT; /* sparc64 */
173
174 free (buf);
175 }
176
177 free (line);
178 }
179
180 /* Drop a reference of the cpu_desc library context. If the refcount of
181 * reaches zero, the resources of the context will be released. */
182 struct cpu_desc *
183 cpu_desc_unref (struct cpu_desc *cpudesc)
184 {
185 if (cpudesc == NULL)
186 return NULL;
187
188 cpudesc->refcount--;
189 if (cpudesc->refcount > 0)
190 return cpudesc;
191
192 free (cpudesc);
193 return NULL;
194 }
195
196 char *
197 cpu_desc_get_architecture (struct cpu_desc *cpudesc)
198 {
199 return cpudesc->arch;
200 }
201
202 char *
203 cpu_desc_get_vendor (struct cpu_desc *cpudesc)
204 {
205 return cpudesc->vendor;
206 }
207
208 char *
209 cpu_desc_get_family (struct cpu_desc *cpudesc)
210 {
211 return cpudesc->family;
212 }
213
214 char *
215 cpu_desc_get_model (struct cpu_desc *cpudesc)
216 {
217 return cpudesc->model;
218 }
219
220 char *
221 cpu_desc_get_model_name (struct cpu_desc *cpudesc)
222 {
223 return cpudesc->modelname;
224 }
225
226 char *
227 cpu_desc_get_virtualization_flag (struct cpu_desc *cpudesc)
228 {
229 if (cpudesc->virtflag == NULL)
230 return NULL;
231
232 if (STREQ (cpudesc->virtflag, "svm"))
233 return "AMD-V";
234 else if (STREQ (cpudesc->virtflag, "vmx"))
235 return "VT-x";
236 /* should never been reached */
237 else
238 return cpudesc->virtflag;
239 }
240
241 char *
242 cpu_desc_get_mhz (struct cpu_desc *cpudesc)
243 {
244 return cpudesc->mhz;
245 }
246
247 char *
248 cpu_desc_get_flags (struct cpu_desc *cpudesc)
249 {
250 return cpudesc->flags;
251 }
252
253 int
254 cpu_desc_get_mode (struct cpu_desc *cpudesc)
255 {
256 return cpudesc->mode;
257 }
258
259 int
260 cpu_desc_get_ncpus (struct cpu_desc *cpudesc)
261 {
262 return cpudesc->ncpus;
263 }
264
265 int
266 cpu_desc_get_ncpuspos (struct cpu_desc *cpudesc)
267 {
268 return cpudesc->ncpuspos;
269 }