| 1 |
/* |
| 2 |
main.c - Part of sensors, a user-space program for hardware monitoring |
| 3 |
Copyright (c) 1998, 1999 Frodo Looijaard <[email protected]> |
| 4 |
Copyright (C) 2007-2012 Jean Delvare <[email protected]> |
| 5 |
|
| 6 |
This program is free software; you can redistribute it and/or modify |
| 7 |
it under the terms of the GNU General Public License as published by |
| 8 |
the Free Software Foundation; either version 2 of the License, or |
| 9 |
(at your option) any later version. |
| 10 |
|
| 11 |
This program is distributed in the hope that it will be useful, |
| 12 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
GNU General Public License for more details. |
| 15 |
|
| 16 |
You should have received a copy of the GNU General Public License |
| 17 |
along with this program; if not, write to the Free Software |
| 18 |
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
| 19 |
MA 02110-1301 USA. |
| 20 |
*/ |
| 21 |
|
| 22 |
#include <stdio.h> |
| 23 |
#include <stdlib.h> |
| 24 |
#include <getopt.h> |
| 25 |
#include <string.h> |
| 26 |
#include <errno.h> |
| 27 |
#include <locale.h> |
| 28 |
#include <langinfo.h> |
| 29 |
|
| 30 |
#ifndef __UCLIBC__ |
| 31 |
#include <iconv.h> |
| 32 |
#define HAVE_ICONV |
| 33 |
#endif |
| 34 |
|
| 35 |
#include "lib/sensors.h" |
| 36 |
#include "lib/error.h" |
| 37 |
#include "main.h" |
| 38 |
#include "chips.h" |
| 39 |
#include "version.h" |
| 40 |
|
| 41 |
#define PROGRAM "sensors" |
| 42 |
#define VERSION LM_VERSION |
| 43 |
|
| 44 |
static int do_sets, do_raw, do_json, hide_adapter; |
| 45 |
int new_json; |
| 46 |
|
| 47 |
int fahrenheit; |
| 48 |
char degstr[5]; /* store the correct string to print degrees */ |
| 49 |
|
| 50 |
static void print_short_help(void) |
| 51 |
{ |
| 52 |
printf("Try `%s -h' for more information\n", PROGRAM); |
| 53 |
} |
| 54 |
|
| 55 |
static void print_long_help(void) |
| 56 |
{ |
| 57 |
printf("Usage: %s [OPTION]... [CHIP]...\n", PROGRAM); |
| 58 |
puts(" -c, --config-file Specify a config file\n" |
| 59 |
" -h, --help Display this help text\n" |
| 60 |
" -s, --set Execute `set' statements (root only)\n" |
| 61 |
" -f, --fahrenheit Show temperatures in degrees fahrenheit\n" |
| 62 |
" -A, --no-adapter Do not show adapter for each chip\n" |
| 63 |
" --bus-list Generate bus statements for sensors.conf\n" |
| 64 |
" -u Raw output\n" |
| 65 |
" -j Json output\n" |
| 66 |
" -v, --version Display the program version\n" |
| 67 |
" -n, --allow-no-sensors Do not fail if no sensors found\n" |
| 68 |
"\n" |
| 69 |
"Use `-' after `-c' to read the config file from stdin.\n" |
| 70 |
"If no chips are specified, all chip info will be printed.\n" |
| 71 |
"Example chip names:\n" |
| 72 |
"\tlm78-i2c-0-2d\t*-i2c-0-2d\n" |
| 73 |
"\tlm78-i2c-0-*\t*-i2c-0-*\n" |
| 74 |
"\tlm78-i2c-*-2d\t*-i2c-*-2d\n" |
| 75 |
"\tlm78-i2c-*-*\t*-i2c-*-*\n" |
| 76 |
"\tlm78-isa-0290\t*-isa-0290\n" |
| 77 |
"\tlm78-isa-*\t*-isa-*\n" |
| 78 |
"\tlm78-*"); |
| 79 |
} |
| 80 |
|
| 81 |
static void print_version(void) |
| 82 |
{ |
| 83 |
printf("%s version %s with libsensors version %s\n", PROGRAM, VERSION, |
| 84 |
libsensors_version); |
| 85 |
} |
| 86 |
|
| 87 |
/* Return 0 on success, and an exit error code otherwise */ |
| 88 |
static int read_config_file(const char *config_file_name) |
| 89 |
{ |
| 90 |
FILE *config_file; |
| 91 |
int err; |
| 92 |
|
| 93 |
if (config_file_name) { |
| 94 |
if (!strcmp(config_file_name, "-")) |
| 95 |
config_file = stdin; |
| 96 |
else |
| 97 |
config_file = fopen(config_file_name, "r"); |
| 98 |
|
| 99 |
if (!config_file) { |
| 100 |
fprintf(stderr, "Could not open config file\n"); |
| 101 |
perror(config_file_name); |
| 102 |
return 1; |
| 103 |
} |
| 104 |
} else { |
| 105 |
/* Use libsensors default */ |
| 106 |
config_file = NULL; |
| 107 |
} |
| 108 |
|
| 109 |
err = sensors_init(config_file); |
| 110 |
if (err) { |
| 111 |
fprintf(stderr, "sensors_init: %s\n", sensors_strerror(err)); |
| 112 |
if (config_file) |
| 113 |
fclose(config_file); |
| 114 |
return 1; |
| 115 |
} |
| 116 |
|
| 117 |
if (config_file && fclose(config_file) == EOF) |
| 118 |
perror(config_file_name); |
| 119 |
|
| 120 |
return 0; |
| 121 |
} |
| 122 |
|
| 123 |
static void set_degstr(void) |
| 124 |
{ |
| 125 |
const char *deg_default_text[2] = { " C", " F" }; |
| 126 |
|
| 127 |
#ifdef HAVE_ICONV |
| 128 |
/* Size hardcoded for better performance. |
| 129 |
Don't forget to count the trailing \0! */ |
| 130 |
size_t deg_latin1_size = 3; |
| 131 |
char deg_latin1_text[2][3] = { "\260C", "\260F" }; |
| 132 |
char *deg_latin1_ptr = deg_latin1_text[fahrenheit]; |
| 133 |
size_t nconv; |
| 134 |
size_t degstr_size = sizeof(degstr); |
| 135 |
char *degstr_ptr = degstr; |
| 136 |
|
| 137 |
iconv_t cd = iconv_open(nl_langinfo(CODESET), "ISO-8859-1"); |
| 138 |
if (cd != (iconv_t) -1) { |
| 139 |
nconv = iconv(cd, °_latin1_ptr, °_latin1_size, |
| 140 |
°str_ptr, °str_size); |
| 141 |
iconv_close(cd); |
| 142 |
|
| 143 |
if (nconv != (size_t) -1) |
| 144 |
return; |
| 145 |
} |
| 146 |
#endif /* HAVE_ICONV */ |
| 147 |
|
| 148 |
/* There was an error during the conversion, use the default text */ |
| 149 |
strcpy(degstr, deg_default_text[fahrenheit]); |
| 150 |
} |
| 151 |
|
| 152 |
static const char *sprintf_chip_name(const sensors_chip_name *name) |
| 153 |
{ |
| 154 |
#define BUF_SIZE 200 |
| 155 |
static char buf[BUF_SIZE]; |
| 156 |
|
| 157 |
if (sensors_snprintf_chip_name(buf, BUF_SIZE, name) < 0) |
| 158 |
return NULL; |
| 159 |
return buf; |
| 160 |
} |
| 161 |
|
| 162 |
static void do_a_print(const sensors_chip_name *name) |
| 163 |
{ |
| 164 |
printf("%s\n", sprintf_chip_name(name)); |
| 165 |
if (!hide_adapter) { |
| 166 |
const char *adap = sensors_get_adapter_name(&name->bus); |
| 167 |
if (adap) |
| 168 |
printf("Adapter: %s\n", adap); |
| 169 |
else |
| 170 |
fprintf(stderr, "Can't get adapter name\n"); |
| 171 |
} |
| 172 |
if (do_raw) |
| 173 |
print_chip_raw(name); |
| 174 |
else |
| 175 |
print_chip(name); |
| 176 |
printf("\n"); |
| 177 |
} |
| 178 |
|
| 179 |
static void do_a_json_print(const sensors_chip_name *name) |
| 180 |
{ |
| 181 |
printf("\"%s\":{", sprintf_chip_name(name)); |
| 182 |
if (!hide_adapter) { |
| 183 |
int a = 0; |
| 184 |
const char *adap = sensors_get_adapter_name(&name->bus); |
| 185 |
if (adap) { |
| 186 |
printf("\"Adapter\":\"%s\"", adap); |
| 187 |
/* only print trailing ',' if there are features to list */ |
| 188 |
if (sensors_get_features(name, &a) != NULL) { |
| 189 |
printf(","); |
| 190 |
} |
| 191 |
} else { |
| 192 |
fprintf(stderr, "Can't get adapter name\n"); |
| 193 |
} |
| 194 |
} |
| 195 |
print_chip_json(name); |
| 196 |
printf("}"); |
| 197 |
} |
| 198 |
|
| 199 |
/* returns 1 on error */ |
| 200 |
static int do_a_set(const sensors_chip_name *name) |
| 201 |
{ |
| 202 |
int err; |
| 203 |
|
| 204 |
if ((err = sensors_do_chip_sets(name))) { |
| 205 |
if (err == -SENSORS_ERR_KERNEL) { |
| 206 |
fprintf(stderr, "%s: %s\n", |
| 207 |
sprintf_chip_name(name), |
| 208 |
sensors_strerror(err)); |
| 209 |
fprintf(stderr, "Run as root?\n"); |
| 210 |
return 1; |
| 211 |
} else if (err == -SENSORS_ERR_ACCESS_W) { |
| 212 |
fprintf(stderr, |
| 213 |
"%s: At least one \"set\" statement failed\n", |
| 214 |
sprintf_chip_name(name)); |
| 215 |
} else { |
| 216 |
fprintf(stderr, "%s: %s\n", sprintf_chip_name(name), |
| 217 |
sensors_strerror(err)); |
| 218 |
} |
| 219 |
} |
| 220 |
return 0; |
| 221 |
} |
| 222 |
|
| 223 |
/* returns number of chips found */ |
| 224 |
static int do_the_real_work(const sensors_chip_name *match, int *err) |
| 225 |
{ |
| 226 |
const sensors_chip_name *chip; |
| 227 |
int chip_nr; |
| 228 |
int cnt = 0; |
| 229 |
|
| 230 |
if (do_json) |
| 231 |
printf("{"); |
| 232 |
chip_nr = 0; |
| 233 |
while ((chip = sensors_get_detected_chips(match, &chip_nr))) { |
| 234 |
if (do_sets) { |
| 235 |
if (do_a_set(chip)) |
| 236 |
*err = 1; |
| 237 |
} else { |
| 238 |
if (do_json) { |
| 239 |
if (cnt > 0) |
| 240 |
printf(","); |
| 241 |
do_a_json_print(chip); |
| 242 |
} else { |
| 243 |
do_a_print(chip); |
| 244 |
} |
| 245 |
} |
| 246 |
cnt++; |
| 247 |
} |
| 248 |
if (do_json) |
| 249 |
printf("}\n"); |
| 250 |
return cnt; |
| 251 |
} |
| 252 |
|
| 253 |
/* List the buses in a format suitable for sensors.conf. We only list |
| 254 |
bus types for which bus statements are actually useful and supported. |
| 255 |
Known bug: i2c buses with number >= 32 or 64 could be listed several |
| 256 |
times. Very unlikely to ever happen, though. */ |
| 257 |
static void print_bus_list(void) |
| 258 |
{ |
| 259 |
const sensors_chip_name *chip; |
| 260 |
int chip_nr; |
| 261 |
unsigned long seen_i2c = 0; |
| 262 |
|
| 263 |
chip_nr = 0; |
| 264 |
while ((chip = sensors_get_detected_chips(NULL, &chip_nr))) { |
| 265 |
switch (chip->bus.type) { |
| 266 |
case SENSORS_BUS_TYPE_I2C: |
| 267 |
if (chip->bus.nr < (int)sizeof(unsigned long) * 8) { |
| 268 |
if (seen_i2c & (1 << chip->bus.nr)) |
| 269 |
break; |
| 270 |
seen_i2c |= 1 << chip->bus.nr; |
| 271 |
} |
| 272 |
printf("bus \"i2c-%d\" \"%s\"\n", chip->bus.nr, |
| 273 |
sensors_get_adapter_name(&chip->bus)); |
| 274 |
break; |
| 275 |
} |
| 276 |
} |
| 277 |
} |
| 278 |
|
| 279 |
int main(int argc, char *argv[]) |
| 280 |
{ |
| 281 |
int c, i, err, do_bus_list, allow_no_sensors; |
| 282 |
const char *config_file_name = NULL; |
| 283 |
|
| 284 |
struct option long_opts[] = { |
| 285 |
{ "help", no_argument, NULL, 'h' }, |
| 286 |
{ "set", no_argument, NULL, 's' }, |
| 287 |
{ "version", no_argument, NULL, 'v'}, |
| 288 |
{ "fahrenheit", no_argument, NULL, 'f' }, |
| 289 |
{ "no-adapter", no_argument, NULL, 'A' }, |
| 290 |
{ "config-file", required_argument, NULL, 'c' }, |
| 291 |
{ "bus-list", no_argument, NULL, 'B' }, |
| 292 |
{ "allow-no-sensors", no_argument, NULL, 'n' }, |
| 293 |
{ 0, 0, 0, 0 } |
| 294 |
}; |
| 295 |
|
| 296 |
setlocale(LC_CTYPE, ""); |
| 297 |
|
| 298 |
do_raw = 0; |
| 299 |
do_json = 0; |
| 300 |
new_json = 0; |
| 301 |
do_sets = 0; |
| 302 |
do_bus_list = 0; |
| 303 |
hide_adapter = 0; |
| 304 |
allow_no_sensors = 0; |
| 305 |
while (1) { |
| 306 |
c = getopt_long(argc, argv, "hsvfAc:ujJn", long_opts, NULL); |
| 307 |
if (c == EOF) |
| 308 |
break; |
| 309 |
switch(c) { |
| 310 |
case ':': |
| 311 |
case '?': |
| 312 |
print_short_help(); |
| 313 |
exit(1); |
| 314 |
case 'h': |
| 315 |
print_long_help(); |
| 316 |
exit(0); |
| 317 |
case 'v': |
| 318 |
print_version(); |
| 319 |
exit(0); |
| 320 |
case 'c': |
| 321 |
config_file_name = optarg; |
| 322 |
break; |
| 323 |
case 's': |
| 324 |
do_sets = 1; |
| 325 |
break; |
| 326 |
case 'f': |
| 327 |
fahrenheit = 1; |
| 328 |
break; |
| 329 |
case 'A': |
| 330 |
hide_adapter = 1; |
| 331 |
break; |
| 332 |
case 'u': |
| 333 |
do_raw = 1; |
| 334 |
break; |
| 335 |
case 'j': |
| 336 |
do_json = 1; |
| 337 |
break; |
| 338 |
case 'J': |
| 339 |
do_json = 1; |
| 340 |
new_json = 1; |
| 341 |
break; |
| 342 |
case 'B': |
| 343 |
do_bus_list = 1; |
| 344 |
break; |
| 345 |
case 'n': |
| 346 |
allow_no_sensors = 1; |
| 347 |
break; |
| 348 |
default: |
| 349 |
fprintf(stderr, |
| 350 |
"Internal error while parsing options!\n"); |
| 351 |
exit(1); |
| 352 |
} |
| 353 |
} |
| 354 |
|
| 355 |
err = read_config_file(config_file_name); |
| 356 |
if (err) |
| 357 |
exit(err); |
| 358 |
|
| 359 |
/* build the degrees string */ |
| 360 |
set_degstr(); |
| 361 |
|
| 362 |
if (do_bus_list) { |
| 363 |
print_bus_list(); |
| 364 |
} else if (optind == argc) { /* No chip name on command line */ |
| 365 |
if (!do_the_real_work(NULL, &err)) { |
| 366 |
fprintf(stderr, |
| 367 |
"No sensors found!\n" |
| 368 |
"Make sure you loaded all the kernel drivers you need.\n" |
| 369 |
"Try sensors-detect to find out which these are.\n"); |
| 370 |
if (!allow_no_sensors) { |
| 371 |
err = 1; |
| 372 |
} |
| 373 |
} |
| 374 |
} else { |
| 375 |
int cnt = 0; |
| 376 |
sensors_chip_name chip; |
| 377 |
|
| 378 |
for (i = optind; i < argc; i++) { |
| 379 |
if (sensors_parse_chip_name(argv[i], &chip)) { |
| 380 |
fprintf(stderr, |
| 381 |
"Parse error in chip name `%s'\n", |
| 382 |
argv[i]); |
| 383 |
print_short_help(); |
| 384 |
err = 1; |
| 385 |
goto exit; |
| 386 |
} |
| 387 |
cnt += do_the_real_work(&chip, &err); |
| 388 |
sensors_free_chip_name(&chip); |
| 389 |
} |
| 390 |
|
| 391 |
if (!cnt) { |
| 392 |
fprintf(stderr, "Specified sensor(s) not found!\n"); |
| 393 |
err = 1; |
| 394 |
} |
| 395 |
} |
| 396 |
|
| 397 |
exit: |
| 398 |
sensors_cleanup(); |
| 399 |
exit(err); |
| 400 |
} |