root/lm-sensors/trunk/prog/sensors/main.c

Revision 6090, 8.4 KB (checked in by khali, 6 months ago)

sensors: Clarify what option -u is good for.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1/*
2    main.c - Part of sensors, a user-space program for hardware monitoring
3    Copyright (c) 1998, 1999  Frodo Looijaard <frodol@dds.nl>
4    Copyright (C) 2007        Jean Delvare <khali@linux-fr.org>
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
44static int do_sets, do_raw, hide_adapter;
45
46int fahrenheit;
47char degstr[5]; /* store the correct string to print degrees */
48
49static void print_short_help(void)
50{
51        printf("Try `%s -h' for more information\n", PROGRAM);
52}
53
54static void print_long_help(void)
55{
56        printf("Usage: %s [OPTION]... [CHIP]...\n", PROGRAM);
57        puts("  -c, --config-file     Specify a config file\n"
58             "  -h, --help            Display this help text\n"
59             "  -s, --set             Execute `set' statements (root only)\n"
60             "  -f, --fahrenheit      Show temperatures in degrees fahrenheit\n"
61             "  -A, --no-adapter      Do not show adapter for each chip\n"
62             "      --bus-list        Generate bus statements for sensors.conf\n"
63             "  -u                    Raw output\n"
64             "  -v, --version         Display the program version\n"
65             "\n"
66             "Use `-' after `-c' to read the config file from stdin.\n"
67             "If no chips are specified, all chip info will be printed.\n"
68             "Example chip names:\n"
69             "\tlm78-i2c-0-2d\t*-i2c-0-2d\n"
70             "\tlm78-i2c-0-*\t*-i2c-0-*\n"
71             "\tlm78-i2c-*-2d\t*-i2c-*-2d\n"
72             "\tlm78-i2c-*-*\t*-i2c-*-*\n"
73             "\tlm78-isa-0290\t*-isa-0290\n"
74             "\tlm78-isa-*\t*-isa-*\n"
75             "\tlm78-*");
76}
77
78static void print_version(void)
79{
80        printf("%s version %s with libsensors version %s\n", PROGRAM, VERSION,
81               libsensors_version);
82}
83
84/* Return 0 on success, and an exit error code otherwise */
85static int read_config_file(const char *config_file_name)
86{
87        FILE *config_file;
88        int err;
89
90        if (config_file_name) {
91                if (!strcmp(config_file_name, "-"))
92                        config_file = stdin;
93                else
94                        config_file = fopen(config_file_name, "r");
95
96                if (!config_file) {
97                        fprintf(stderr, "Could not open config file\n");
98                        perror(config_file_name);
99                        return 1;
100                }
101        } else {
102                /* Use libsensors default */
103                config_file = NULL;
104        }
105
106        err = sensors_init(config_file);
107        if (err) {
108                fprintf(stderr, "sensors_init: %s\n", sensors_strerror(err));
109                if (config_file)
110                        fclose(config_file);
111                return 1;
112        }
113
114        if (config_file && fclose(config_file) == EOF)
115                perror(config_file_name);
116
117        return 0;
118}
119
120static void set_degstr(void)
121{
122        const char *deg_default_text[2] = { " C", " F" };
123
124#ifdef HAVE_ICONV
125        /* Size hardcoded for better performance.
126           Don't forget to count the trailing \0! */
127        size_t deg_latin1_size = 3;
128        char deg_latin1_text[2][3] = { "\260C", "\260F" };
129        char *deg_latin1_ptr = deg_latin1_text[fahrenheit];
130        size_t nconv;
131        size_t degstr_size = sizeof(degstr);
132        char *degstr_ptr = degstr;
133
134        iconv_t cd = iconv_open(nl_langinfo(CODESET), "ISO-8859-1");
135        if (cd != (iconv_t) -1) {
136                nconv = iconv(cd, &deg_latin1_ptr, &deg_latin1_size,
137                              &degstr_ptr, &degstr_size);
138                iconv_close(cd);
139
140                if (nconv != (size_t) -1)
141                        return;
142        }
143#endif /* HAVE_ICONV */
144
145        /* There was an error during the conversion, use the default text */
146        strcpy(degstr, deg_default_text[fahrenheit]);
147}
148
149static const char *sprintf_chip_name(const sensors_chip_name *name)
150{
151#define BUF_SIZE 200
152        static char buf[BUF_SIZE];
153
154        if (sensors_snprintf_chip_name(buf, BUF_SIZE, name) < 0)
155                return NULL;
156        return buf;
157}
158
159static void do_a_print(const sensors_chip_name *name)
160{
161        printf("%s\n", sprintf_chip_name(name));
162        if (!hide_adapter) {
163                const char *adap = sensors_get_adapter_name(&name->bus);
164                if (adap)
165                        printf("Adapter: %s\n", adap);
166                else
167                        fprintf(stderr, "Can't get adapter name\n");
168        }
169        if (do_raw)
170                print_chip_raw(name);
171        else
172                print_chip(name);
173        printf("\n");
174}
175
176/* returns 1 on error */
177static int do_a_set(const sensors_chip_name *name)
178{
179        int err;
180
181        if ((err = sensors_do_chip_sets(name))) {
182                if (err == -SENSORS_ERR_KERNEL) {
183                        fprintf(stderr, "%s: %s\n",
184                                sprintf_chip_name(name),
185                                sensors_strerror(err));
186                        fprintf(stderr, "Run as root?\n");
187                        return 1;
188                } else if (err == -SENSORS_ERR_ACCESS_W) {
189                        fprintf(stderr,
190                                "%s: At least one \"set\" statement failed\n",
191                                sprintf_chip_name(name));
192                } else {
193                        fprintf(stderr, "%s: %s\n", sprintf_chip_name(name),
194                                sensors_strerror(err));
195                }
196        }
197        return 0;
198}
199
200/* returns number of chips found */
201static int do_the_real_work(const sensors_chip_name *match, int *err)
202{
203        const sensors_chip_name *chip;
204        int chip_nr;
205        int cnt = 0;
206
207        chip_nr = 0;
208        while ((chip = sensors_get_detected_chips(match, &chip_nr))) {
209                if (do_sets) {
210                        if (do_a_set(chip))
211                                *err = 1;
212                } else
213                        do_a_print(chip);
214                cnt++;
215        }
216        return cnt;
217}
218
219/* List the buses in a format suitable for sensors.conf. We only list
220   bus types for which bus statements are actually useful and supported.
221   Known bug: i2c buses with number >= 32 or 64 could be listed several
222   times. Very unlikely to ever happen, though. */
223static void print_bus_list(void)
224{
225        const sensors_chip_name *chip;
226        int chip_nr;
227        unsigned long seen_i2c = 0;
228
229        chip_nr = 0;
230        while ((chip = sensors_get_detected_chips(NULL, &chip_nr))) {
231                switch (chip->bus.type) {
232                case SENSORS_BUS_TYPE_I2C:
233                        if (chip->bus.nr < (int)sizeof(unsigned long) * 8) {
234                                if (seen_i2c & (1 << chip->bus.nr))
235                                        break;
236                                seen_i2c |= 1 << chip->bus.nr;
237                        }
238                        printf("bus \"i2c-%d\" \"%s\"\n", chip->bus.nr,
239                               sensors_get_adapter_name(&chip->bus));
240                        break;
241                }
242        }
243}
244
245int main(int argc, char *argv[])
246{
247        int c, i, err, do_bus_list;
248        const char *config_file_name = NULL;
249
250        struct option long_opts[] =  {
251                { "help", no_argument, NULL, 'h' },
252                { "set", no_argument, NULL, 's' },
253                { "version", no_argument, NULL, 'v'},
254                { "fahrenheit", no_argument, NULL, 'f' },
255                { "no-adapter", no_argument, NULL, 'A' },
256                { "config-file", required_argument, NULL, 'c' },
257                { "bus-list", no_argument, NULL, 'B' },
258                { 0, 0, 0, 0 }
259        };
260
261        setlocale(LC_CTYPE, "");
262
263        do_raw = 0;
264        do_sets = 0;
265        do_bus_list = 0;
266        hide_adapter = 0;
267        while (1) {
268                c = getopt_long(argc, argv, "hsvfAc:u", long_opts, NULL);
269                if (c == EOF)
270                        break;
271                switch(c) {
272                case ':':
273                case '?':
274                        print_short_help();
275                        exit(1);
276                case 'h':
277                        print_long_help();
278                        exit(0);
279                case 'v':
280                        print_version();
281                        exit(0);
282                case 'c':
283                        config_file_name = optarg;
284                        break;
285                case 's':
286                        do_sets = 1;
287                        break;
288                case 'f':
289                        fahrenheit = 1;
290                        break;
291                case 'A':
292                        hide_adapter = 1;
293                        break;
294                case 'u':
295                        do_raw = 1;
296                        break;
297                case 'B':
298                        do_bus_list = 1;
299                        break;
300                default:
301                        fprintf(stderr,
302                                "Internal error while parsing options!\n");
303                        exit(1);
304                }
305        }
306
307        err = read_config_file(config_file_name);
308        if (err)
309                exit(err);
310
311        /* build the degrees string */
312        set_degstr();
313
314        if (do_bus_list) {
315                print_bus_list();
316        } else if (optind == argc) { /* No chip name on command line */
317                if (!do_the_real_work(NULL, &err)) {
318                        fprintf(stderr,
319                                "No sensors found!\n"
320                                "Make sure you loaded all the kernel drivers you need.\n"
321                                "Try sensors-detect to find out which these are.\n");
322                        err = 1;
323                }
324        } else {
325                int cnt = 0;
326                sensors_chip_name chip;
327
328                for (i = optind; i < argc; i++) {
329                        if (sensors_parse_chip_name(argv[i], &chip)) {
330                                fprintf(stderr,
331                                        "Parse error in chip name `%s'\n",
332                                        argv[i]);
333                                print_short_help();
334                                err = 1;
335                                goto exit;
336                        }
337                        cnt += do_the_real_work(&chip, &err);
338                        sensors_free_chip_name(&chip);
339                }
340
341                if (!cnt) {
342                        fprintf(stderr, "Specified sensor(s) not found!\n");
343                        err = 1;
344                }
345        }
346
347exit:
348        sensors_cleanup();
349        exit(err);
350}
Note: See TracBrowser for help on using the browser.