File: | plugins/libsensors/libsensors-plugin.c |
Warning: | line 283, column 5 Value stored to 'i' is never read |
Press '?' to see keyboard shortcuts
Keyboard shortcuts:
1 | /* |
2 | * Copyright (C) 2005-2009 Alex Murray <murray.alex@gmail.com> |
3 | * |
4 | * This program is free software; you can redistribute it and/or modify |
5 | * it under the terms of the GNU General Public License as published by |
6 | * the Free Software Foundation; either version 2 of the License, or |
7 | * (at your option) any later version. |
8 | * |
9 | * This program is distributed in the hope that it will be useful, |
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 | * GNU General Public License for more details. |
13 | * |
14 | * You should have received a copy of the GNU General Public License |
15 | * along with this program; if not, write to the Free Software |
16 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
17 | */ |
18 | |
19 | #ifdef HAVE_CONFIG_H1 |
20 | #include "config.h" |
21 | #endif /* HAVE_CONFIG_H */ |
22 | |
23 | #ifdef HAVE_STDLIB_H1 |
24 | #include <stdlib.h> |
25 | #endif |
26 | |
27 | #ifdef HAVE_STRING_H1 |
28 | #include <string.h> |
29 | #endif |
30 | |
31 | #ifdef HAVE_SYS_TYPES_H1 |
32 | #include <sys/types.h> |
33 | #endif |
34 | |
35 | #ifdef HAVE_SENSORS_SENSORS_H1 |
36 | #include <sensors/sensors.h> |
37 | #endif |
38 | |
39 | #include "libsensors-plugin.h" |
40 | |
41 | const gchar *plugin_name = "libsensors"; |
42 | |
43 | GHashTable *hash_table = NULL((void*)0); |
44 | |
45 | enum { |
46 | LIBSENSORS_CHIP_PARSE_ERROR, |
47 | LIBSENSORS_MISSING_FEATURE_ERROR, |
48 | LIBSENSORS_REGEX_URL_COMPILE_ERROR, |
49 | LIBSENSORS_CHIP_NOT_FOUND_ERROR |
50 | }; |
51 | |
52 | #if SENSORS_API_VERSION0x500 < 0x400 |
53 | #define LIBSENSORS_CONFIG_FILE "/etc/sensors.conf" |
54 | #define LIBSENSORS_ALTERNATIVE_CONFIG_FILE "/usr/local/etc/sensors.conf" |
55 | #endif |
56 | |
57 | GRegex *uri_re; |
58 | |
59 | static char *get_chip_name_string(const sensors_chip_name *chip) { |
60 | char *name; |
61 | |
62 | #if SENSORS_API_VERSION0x500 < 0x400 |
63 | /* taken from lm-sensors:prog/sensors/main.c:sprintf_chip_name */ |
64 | switch (chip->bus) { |
65 | case SENSORS_CHIP_NAME_BUS_ISA: |
66 | name = g_strdup_printf ("%s-isa-%04x", chip->prefix, chip->addr); |
67 | break; |
68 | case SENSORS_CHIP_NAME_BUS_DUMMY: |
69 | name = g_strdup_printf ("%s-%s-%04x", chip->prefix, chip->busname, chip->addr); |
70 | break; |
71 | case SENSORS_CHIP_NAME_BUS_PCI: |
72 | name = g_strdup_printf ("%s-pci-%04x", chip->prefix, chip->addr); |
73 | break; |
74 | default: |
75 | name = g_strdup_printf ("%s-i2c-%d-%02x", chip->prefix, chip->bus, chip->addr); |
76 | break; |
77 | } |
78 | |
79 | #else |
80 | /* adapted from lm-sensors:prog/sensors/main.c:sprintf_chip_name in lm-sensors-3.0 */ |
81 | #define BUF_SIZE200 200 |
82 | static char buf[BUF_SIZE200]; |
83 | if (sensors_snprintf_chip_name(buf, BUF_SIZE200, chip) < 0) { |
84 | name = NULL((void*)0); |
85 | } else { |
86 | name = g_strdup (buf)g_strdup_inline (buf); |
87 | } |
88 | #endif |
89 | |
90 | return name; |
91 | } |
92 | |
93 | #if SENSORS_API_VERSION0x500 < 0x400 |
94 | static SensorType get_sensor_type (const char *name) { |
95 | SensorType type = CURRENT_SENSOR; |
96 | |
97 | if (g_strrstr(name, "in")) { |
98 | type = VOLTAGE_SENSOR; |
99 | } |
100 | else if (g_strrstr(name, "fan")) { |
101 | type = FAN_SENSOR; |
102 | } |
103 | else if (g_strrstr(name, "temp")) { |
104 | type = TEMP_SENSOR; |
105 | } else { |
106 | g_debug("sensor %s not recognised as either voltage, fan or temp sensor - assuming is a current sensor", name); |
107 | type = CURRENT_SENSOR; |
108 | } |
109 | return type; |
110 | } |
111 | |
112 | /* If a sensor is 'interesting' to us then return its label, otherwise NULL. */ |
113 | static char *get_sensor_interesting_label (sensors_chip_name chip, int feature) { |
114 | char *label; |
115 | |
116 | if (sensors_get_ignored(chip, feature) && (sensors_get_label(chip, feature, &label) == 0)) { |
117 | if (! (strcmp ("alarms", label) == 0 || strncmp ("sensor", label, 6) == 0)) { |
118 | return label; |
119 | } else { |
120 | free (label); |
121 | } |
122 | } |
123 | |
124 | return NULL((void*)0); |
125 | } |
126 | |
127 | static const sensors_feature_data * get_sensor_min_max(const sensors_chip_name *chip, |
128 | int *n1, int *n2, |
129 | int number, |
130 | gdouble *low_value, |
131 | gdouble *high_value) { |
132 | |
133 | const sensors_feature_data *data; |
134 | double value; |
135 | |
136 | /* The sub features are returned directly after the main feature by |
137 | sensors_get_all_features(), so no need to iterate over all features */ |
138 | while ((data = sensors_get_all_features (*chip, n1, n2)) != NULL((void*)0) && data->mapping == number) { |
139 | if ((data->mode & SENSORS_MODE_R1) && (sensors_get_feature(*chip, data->number, &value) == 0) && data->name != NULL((void*)0)) { |
140 | |
141 | if (g_str_has_suffix(data->name, "_min")(__builtin_constant_p ("_min")? __extension__ ({ const char * const __str = (data->name); const char * const __suffix = ("_min"); gboolean __result = (0); if (__str == ((void*)0) || __suffix == ((void*)0)) __result = (g_str_has_suffix) (__str , __suffix); else { const size_t __str_len = strlen (((__str) + !(__str))); const size_t __suffix_len = strlen (((__suffix ) + !(__suffix))); if (__str_len >= __suffix_len) __result = memcmp (__str + __str_len - __suffix_len, ((__suffix) + !( __suffix)), __suffix_len) == 0; } __result; }) : (g_str_has_suffix ) (data->name, "_min") )) { |
142 | *low_value = value; |
143 | g_debug("overriding low value of sensor %s with value %f", data->name, value); |
144 | } else if (g_str_has_suffix(data->name, "_max")(__builtin_constant_p ("_max")? __extension__ ({ const char * const __str = (data->name); const char * const __suffix = ("_max"); gboolean __result = (0); if (__str == ((void*)0) || __suffix == ((void*)0)) __result = (g_str_has_suffix) (__str , __suffix); else { const size_t __str_len = strlen (((__str) + !(__str))); const size_t __suffix_len = strlen (((__suffix ) + !(__suffix))); if (__str_len >= __suffix_len) __result = memcmp (__str + __str_len - __suffix_len, ((__suffix) + !( __suffix)), __suffix_len) == 0; } __result; }) : (g_str_has_suffix ) (data->name, "_max") )) { |
145 | *high_value = value; |
146 | g_debug("overriding high value of sensor %s with value %f", data->name, value); |
147 | } |
148 | } |
149 | } |
150 | return data; |
151 | } |
152 | |
153 | #endif |
154 | |
155 | static IconType get_sensor_icon (SensorType type) { |
156 | switch (type) { |
157 | case TEMP_SENSOR: |
158 | return CPU_ICON; |
159 | case FAN_SENSOR: |
160 | return FAN_ICON; |
161 | default: |
162 | return GENERIC_ICON; |
163 | } |
164 | } |
165 | |
166 | #if SENSORS_API_VERSION0x500 < 0x400 |
167 | static void check_sensor_with_data(GList **sensors, |
168 | const char * const chip_name, |
169 | const sensors_chip_name *chip, |
170 | int *n1, int *n2, |
171 | const sensors_feature_data *data) { |
172 | |
173 | char *label; |
174 | double value; |
175 | SensorsAppletSensorInfo *sensor_info = NULL((void*)0); |
176 | |
177 | /* ... some of which are boring ... */ |
178 | if ((label = get_sensor_interesting_label (*chip, data->number))) { |
179 | /* ... and some of which are actually sensors */ |
180 | if ((data->mode & SENSORS_MODE_R1) && |
181 | (data->mapping == SENSORS_NO_MAPPING) && |
182 | (sensors_get_feature(*chip, data->number, &value) == 0)) { /* make sure we can actually get a value for it */ |
183 | |
184 | SensorType type; |
185 | gboolean visible; |
186 | IconType icon; |
187 | gdouble low_value, high_value; |
188 | |
189 | gchar *url; |
190 | |
191 | type = get_sensor_type (data->name); |
192 | visible = (type == TEMP_SENSOR ? TRUE(!(0)) : FALSE(0)); |
193 | icon = get_sensor_icon(type); |
194 | |
195 | /* the 'path' contains all the information we need to identify this sensor later */ |
196 | url = g_strdup_printf ("sensor://%s/%d", chip_name, data->number); |
197 | |
198 | /* get low and high values */ |
199 | sensors_applet_plugin_default_sensor_limits(type, &low_value, &high_value); |
200 | |
201 | data = get_sensor_min_max(chip, n1, n2, data->number, &low_value, &high_value); |
202 | if (data != NULL((void*)0)) { |
203 | /* try adding this one */ |
204 | /* at this point we have called sensors_get_all_features() and stopped when we have a potential new sensor, so make sure we try this as well - do a recursive call */ |
205 | check_sensor_with_data(sensors, chip_name, chip, n1, n2, data); |
206 | } |
207 | |
208 | g_hash_table_insert(hash_table, g_strdup(url)g_strdup_inline (url), (void *)chip); |
209 | /* the id identifies a particular sensor for the user; */ |
210 | /* we default to the label returned by libsensors */ |
211 | sensors_applet_plugin_add_sensor_with_limits(sensors, |
212 | url, |
213 | label, |
214 | label, |
215 | type, |
216 | visible, |
217 | low_value, |
218 | high_value, |
219 | icon, |
220 | DEFAULT_GRAPH_COLOR"#ff0000"); |
221 | g_free(url); |
222 | } |
223 | free (label); |
224 | } |
225 | |
226 | } |
227 | |
228 | #endif |
229 | |
230 | static GList *libsensors_plugin_get_sensors(void) { |
231 | const sensors_chip_name *chip_name; |
232 | int i; |
233 | GList *sensors = NULL((void*)0); |
234 | |
235 | #if SENSORS_API_VERSION0x500 < 0x400 |
236 | FILE *file; |
237 | g_debug("%s: using libsensors version < 4", __FUNCTION__); |
238 | |
239 | /* try to open config file, otherwise try alternate config |
240 | * file - if neither succeed, exit */ |
241 | if ((file = fopen (LIBSENSORS_CONFIG_FILE, "r")) == NULL((void*)0)) { |
242 | if ((file = fopen (LIBSENSORS_ALTERNATIVE_CONFIG_FILE, "r")) == NULL((void*)0)) { |
243 | g_debug("%s: error opening libsensors config file... ", __FUNCTION__); |
244 | return sensors; |
245 | } |
246 | } |
247 | |
248 | /* at this point should have an open config file, if is not |
249 | * valid, close file and return */ |
250 | if (sensors_init(file) != 0) { |
251 | fclose(file); |
252 | g_debug("%s: error initing libsensors from config file...", __FUNCTION__); |
253 | return sensors; |
254 | } |
255 | fclose(file); |
256 | |
257 | /* libsensors exposes a number of chips - ... */ |
258 | i = 0; |
259 | while ((chip_name = sensors_get_detected_chips (&i)) != NULL((void*)0)) { |
260 | char *chip_name_string; |
261 | const sensors_feature_data *data; |
262 | int n1 = 0, n2 = 0; |
263 | |
264 | chip_name_string = get_chip_name_string(chip_name); |
265 | |
266 | /* ... each of which has one or more 'features' ... */ |
267 | while ((data = sensors_get_all_features (*chip_name, &n1, &n2)) != NULL((void*)0)) { /* error */ |
268 | /* fill in list for us */ |
269 | check_sensor_with_data(&sensors, chip_name_string, chip_name, &n1, &n2, data); |
270 | } |
271 | g_free (chip_name_string); |
272 | } |
273 | |
274 | #else |
275 | g_debug("%s: using libsensors version >= 4", __FUNCTION__); |
276 | |
277 | int nr = 0; |
278 | if (sensors_init(NULL((void*)0)) != 0) { |
279 | g_debug("%s: error initing libsensors", __FUNCTION__); |
280 | return sensors; |
281 | } |
282 | |
283 | i = 0; |
Value stored to 'i' is never read | |
284 | while ((chip_name = sensors_get_detected_chips(NULL((void*)0), &nr))) { |
285 | |
286 | char *chip_name_string, *label; |
287 | const sensors_subfeature *input_feature; |
288 | const sensors_subfeature *low_feature; |
289 | const sensors_subfeature *high_feature; |
290 | const sensors_feature *main_feature; |
291 | SensorType type; |
292 | gint nr1 = 0; |
293 | gdouble value, low, high; |
294 | gchar *path; |
295 | gboolean visible; |
296 | IconType icon; |
297 | |
298 | chip_name_string = get_chip_name_string(chip_name); |
299 | if (chip_name_string == NULL((void*)0)) { |
300 | g_debug("%s: %d: error getting name string for sensor: %s\n", __FILE__"libsensors-plugin.c", __LINE__300, chip_name->path); |
301 | continue; |
302 | } |
303 | |
304 | while ((main_feature = sensors_get_features(chip_name, &nr1))) { |
305 | switch (main_feature->type) { |
306 | case SENSORS_FEATURE_IN: |
307 | type = VOLTAGE_SENSOR; |
308 | input_feature = sensors_get_subfeature(chip_name, main_feature, SENSORS_SUBFEATURE_IN_INPUT); |
309 | low_feature = sensors_get_subfeature(chip_name, main_feature, SENSORS_SUBFEATURE_IN_MIN); |
310 | high_feature = sensors_get_subfeature(chip_name, main_feature, SENSORS_SUBFEATURE_IN_MAX); |
311 | break; |
312 | |
313 | case SENSORS_FEATURE_FAN: |
314 | type = FAN_SENSOR; |
315 | input_feature = sensors_get_subfeature(chip_name, main_feature, SENSORS_SUBFEATURE_FAN_INPUT); |
316 | low_feature = sensors_get_subfeature(chip_name, main_feature, SENSORS_SUBFEATURE_FAN_MIN); |
317 | /* no fan max feature */ |
318 | high_feature = NULL((void*)0); |
319 | break; |
320 | |
321 | case SENSORS_FEATURE_TEMP: |
322 | type = TEMP_SENSOR; |
323 | input_feature = sensors_get_subfeature(chip_name, main_feature, SENSORS_SUBFEATURE_TEMP_INPUT); |
324 | low_feature = sensors_get_subfeature(chip_name, main_feature, SENSORS_SUBFEATURE_TEMP_MIN); |
325 | high_feature = sensors_get_subfeature(chip_name, main_feature, SENSORS_SUBFEATURE_TEMP_MAX); |
326 | if (!high_feature) |
327 | high_feature = sensors_get_subfeature(chip_name, main_feature, SENSORS_SUBFEATURE_TEMP_CRIT); |
328 | break; |
329 | |
330 | default: |
331 | g_debug("%s: %d: error determining type for: %s\n", __FILE__"libsensors-plugin.c", __LINE__331, chip_name_string); |
332 | continue; |
333 | } |
334 | |
335 | if (!input_feature) { |
336 | g_debug("%s: %d: could not get input subfeature for: %s\n", __FILE__"libsensors-plugin.c", __LINE__336, chip_name_string); |
337 | continue; |
338 | } |
339 | |
340 | /* if still here we got input feature so get label */ |
341 | label = sensors_get_label(chip_name, main_feature); |
342 | if (!label) { |
343 | g_debug("%s: %d: error: could not get label for: %s\n", __FILE__"libsensors-plugin.c", __LINE__343, chip_name_string); |
344 | continue; |
345 | } |
346 | |
347 | g_assert(chip_name_string && label)do { if (chip_name_string && label) ; else g_assertion_message_expr ("sensors-applet", "libsensors-plugin.c", 347, ((const char* ) (__func__)), "chip_name_string && label"); } while ( 0); |
348 | |
349 | icon = get_sensor_icon(type); |
350 | visible = (type == TEMP_SENSOR ? TRUE(!(0)) : FALSE(0)); |
351 | sensors_applet_plugin_default_sensor_limits(type, &low, &high); |
352 | if (low_feature) { |
353 | sensors_get_value(chip_name, low_feature->number, &low); |
354 | } |
355 | |
356 | if (high_feature) { |
357 | sensors_get_value(chip_name, high_feature->number, &high); |
358 | } |
359 | |
360 | if (sensors_get_value(chip_name, input_feature->number, &value) < 0) { |
361 | g_debug("%s: %d: error: could not get value for input feature of sensor: %s\n", __FILE__"libsensors-plugin.c", __LINE__361, chip_name_string); |
362 | free(label); |
363 | continue; |
364 | } |
365 | |
366 | g_debug("for chip %s (type %s) got label %s and value %f", chip_name_string, |
367 | (type == TEMP_SENSOR ? "temp" : (type == FAN_SENSOR ? "fan" : (type == VOLTAGE_SENSOR ? "voltage" : "error"))), label, value); |
368 | |
369 | path = g_strdup_printf ("sensor://%s/%d", chip_name_string, input_feature->number); |
370 | g_hash_table_insert(hash_table, g_strdup(path)g_strdup_inline (path), (void *)chip_name); |
371 | |
372 | sensors_applet_plugin_add_sensor_with_limits(&sensors, |
373 | path, |
374 | label, |
375 | label, |
376 | type, |
377 | visible, |
378 | low, |
379 | high, |
380 | icon, |
381 | DEFAULT_GRAPH_COLOR"#ff0000"); |
382 | } |
383 | g_free(chip_name_string); |
384 | |
385 | } |
386 | #endif |
387 | |
388 | return sensors; |
389 | } |
390 | |
391 | static gdouble libsensors_plugin_get_sensor_value(const gchar *path, |
392 | const gchar *id, |
393 | SensorType type, |
394 | GError **error) { |
395 | gdouble result = 0; |
396 | GMatchInfo *m; |
397 | |
398 | /* parse the uri into a (chip, feature) tuplet */ |
399 | g_regex_match (uri_re, path, 0, &m); |
400 | if (g_match_info_matches (m)) { |
401 | const sensors_chip_name *found_chip; |
402 | int feature; |
403 | int i; |
404 | |
405 | if ((found_chip = g_hash_table_lookup(hash_table, path)) != NULL((void*)0)) { |
406 | gdouble value; |
407 | gchar *feature_str; |
408 | |
409 | feature_str = g_match_info_fetch (m, 1); |
410 | feature = atoi(feature_str); |
411 | g_free (feature_str); |
412 | |
413 | #if SENSORS_API_VERSION0x500 < 0x400 |
414 | /* retrieve the value of the feature */ |
415 | if (sensors_get_feature (*found_chip, feature, &value) == 0) { |
416 | result = value; |
417 | } else { |
418 | g_set_error (error, SENSORS_APPLET_PLUGIN_ERROR(sensors_applet_plugin_error_quark()), LIBSENSORS_MISSING_FEATURE_ERROR, "Error retrieving sensor value"); |
419 | } |
420 | #else |
421 | if (sensors_get_value(found_chip, feature, &value) >= 0) { |
422 | result = value; |
423 | } else { |
424 | g_set_error (error, SENSORS_APPLET_PLUGIN_ERROR(sensors_applet_plugin_error_quark()), LIBSENSORS_MISSING_FEATURE_ERROR, "Error retrieving sensor value"); |
425 | } |
426 | #endif |
427 | |
428 | } else { |
429 | g_set_error (error, SENSORS_APPLET_PLUGIN_ERROR(sensors_applet_plugin_error_quark()), LIBSENSORS_CHIP_NOT_FOUND_ERROR, "Chip not found"); |
430 | } |
431 | } else { |
432 | g_set_error (error, SENSORS_APPLET_PLUGIN_ERROR(sensors_applet_plugin_error_quark()), LIBSENSORS_REGEX_URL_COMPILE_ERROR, "Error compiling URL regex: Not match"); |
433 | } |
434 | g_match_info_free (m); |
435 | return result; |
436 | } |
437 | |
438 | |
439 | static GList *libsensors_plugin_init() { |
440 | GError *err = NULL((void*)0); |
441 | |
442 | /* compile the regular expressions */ |
443 | uri_re = g_regex_new ("^sensor://[a-z0-9_-]+/([0-9]+)$", G_REGEX_CASELESS | G_REGEX_OPTIMIZE, G_REGEX_MATCH_ANCHORED, &err); |
444 | if (err) { |
445 | g_debug("Error compiling regexp: %s\nnot initing libsensors sensors interface", err->message); |
446 | g_error_free (err); |
447 | return NULL((void*)0); |
448 | } |
449 | |
450 | /* create hash table to associate path strings with sensors_chip_name |
451 | * pointers - make sure it free's the keys strings on destroy */ |
452 | hash_table = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, NULL((void*)0)); |
453 | |
454 | return libsensors_plugin_get_sensors(); |
455 | } |
456 | |
457 | const gchar *sensors_applet_plugin_name(void) |
458 | { |
459 | return plugin_name; |
460 | } |
461 | |
462 | GList *sensors_applet_plugin_init(void) |
463 | { |
464 | return libsensors_plugin_init(); |
465 | } |
466 | |
467 | gdouble sensors_applet_plugin_get_sensor_value(const gchar *path, |
468 | const gchar *id, |
469 | SensorType type, |
470 | GError **error) { |
471 | |
472 | return libsensors_plugin_get_sensor_value(path, id, type, error); |
473 | } |