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

Revision 2729, 13.1 KB (checked in by khali, 9 years ago)

Support the new lm63 driver.

  • 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
5    This program is free software; you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation; either version 2 of the License, or
8    (at your option) any later version.
9
10    This program is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13    GNU General Public License for more details.
14
15    You should have received a copy of the GNU General Public License
16    along with this program; if not, write to the Free Software
17    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18*/
19
20#include <stdio.h>
21#include <stdlib.h>
22#include <getopt.h>
23#include <string.h>
24#include <errno.h>
25#include <locale.h>
26#include <langinfo.h>
27#include <iconv.h>
28
29#include "lib/sensors.h"
30#include "lib/error.h"
31#include "chips.h"
32#include "version.h"
33
34#define PROGRAM "sensors"
35#define VERSION LM_VERSION
36#define DEFAULT_CONFIG_FILE_NAME "sensors.conf"
37
38static char *config_file_name = NULL;
39FILE *config_file;
40static const char *config_file_path[] = 
41  { "/etc", "/usr/local/etc", "/usr/lib/sensors", "/usr/local/lib/sensors",
42    "/usr/lib", "/usr/local/lib", ".", 0 };
43extern const char *libsensors_version;
44
45extern int main(int argc, char *arv[]);
46static void print_short_help(void);
47static void print_long_help(void);
48static void print_version(void);
49static void open_config_file(void);
50static int open_this_config_file(char *filename);
51static void do_a_print(sensors_chip_name name);
52static int do_a_set(sensors_chip_name name);
53static int do_the_real_work(int *error);
54static const char *sprintf_chip_name(sensors_chip_name name);
55
56#define CHIPS_MAX 20
57sensors_chip_name chips[CHIPS_MAX];
58int chips_count=0;
59int do_sets, do_unknown, fahrenheit, show_algorithm, hide_adapter, hide_unknown;
60
61char degstr[5]; /* store the correct string to print degrees */
62
63void print_short_help(void)
64{
65  printf("Try `%s -h' for more information\n",PROGRAM);
66}
67
68void print_long_help(void)
69{
70  printf("Usage: %s [OPTION]... [CHIP]...\n",PROGRAM);
71  printf("  -c, --config-file     Specify a config file\n");
72  printf("  -h, --help            Display this help text\n");
73  printf("  -s, --set             Execute `set' statements too (root only)\n");
74  printf("  -f, --fahrenheit      Show temperatures in degrees fahrenheit\n");
75  printf("  -a, --algorithm       Show algorithm for each chip\n");
76  printf("  -A, --no-adapter      Do not show adapter for each chip\n");
77  printf("  -U, --no-unknown      Do not show unknown chips\n");
78  printf("  -u, --unknown         Treat chips as unknown ones (testing only)\n");
79  printf("  -v, --version         Display the program version\n");
80  printf("\n");
81  printf("By default, a list of directories is examined for config file `sensors.conf'.\n");
82  printf("Use `-' after `-c' to read the config file from stdin.\n");
83  printf("If no chips are specified, all chip info will be printed.\n");
84  printf("Example chip names:\n");
85  printf("\tlm78-i2c-0-2d\t*-i2c-0-2d\n");
86  printf("\tlm78-i2c-0-*\t*-i2c-0-*\n");
87  printf("\tlm78-i2c-*-2d\t*-i2c-*-2d\n");
88  printf("\tlm78-i2c-*-*\t*-i2c-*-*\n");
89  printf("\tlm78-isa-0290\t*-isa-0290\n");
90  printf("\tlm78-isa-*\t*-isa-*\n");
91  printf("\tlm78-*\n");
92}
93
94void print_version(void)
95{
96  printf("%s version %s with libsensors version %s\n", PROGRAM, VERSION, libsensors_version);
97}
98
99/* This examines global var config_file, and leaves the name there too.
100   It also opens config_file. */
101void open_config_file(void)
102{
103#define MAX_FILENAME_LEN 1024
104  char *filename;
105  char buffer[MAX_FILENAME_LEN];
106  int res,i;
107
108  if (config_file_name && !strcmp(config_file_name,"-")) {
109    config_file = stdin;
110    return;
111  } else if (config_file_name && index(config_file_name,'/')) {
112    if ((res = open_this_config_file(config_file_name))) {
113      fprintf(stderr,"Could not locate or open config file\n");
114      fprintf(stderr,"%s: %s\n",config_file_name,strerror(-res));
115      exit(1);
116    }
117  }
118  else {
119    if (config_file_name)
120      filename = config_file_name;
121    else
122      filename = DEFAULT_CONFIG_FILE_NAME;
123    for (i = 0; config_file_path[i]; i++) {
124      if ((snprintf(buffer,MAX_FILENAME_LEN,
125                   "%s/%s",config_file_path[i],filename)) < 1) {
126        fprintf(stderr,
127                "open_config_file: ridiculous long config file name!\n");
128        exit(1);
129      }
130      if (!open_this_config_file(buffer)) {
131        free(config_file_name);
132        config_file_name = strdup(buffer);
133        return;
134      }
135    }
136    fprintf(stderr,"Could not locate or open config file!\n");
137    exit(1);
138  }
139}
140   
141int open_this_config_file(char *filename)
142{
143  config_file = fopen(filename,"r");
144  if (! config_file)
145    return -errno;
146  return 0;
147}
148
149void close_config_file(void)
150{
151  if (fclose(config_file)) {
152    fprintf(stderr,"Could not close config file\n");
153    fprintf(stderr,"%s: %s\n",config_file_name,strerror(errno));
154  }
155 
156  free(config_file_name);
157}
158
159static void set_degstr(void)
160{
161  /* Size hardcoded for better performance.
162     Don't forget to count the trailing \0! */
163  size_t deg_latin1_size = 3;
164  char *deg_latin1_text[2] = {"\260C", "\260F"};
165  const char *deg_default_text[2] = {" C", " F"};
166  size_t nconv;
167  size_t degstr_size = sizeof(degstr);
168  char *degstr_ptr = degstr;
169
170  iconv_t cd = iconv_open(nl_langinfo(CODESET), "ISO-8859-1");
171  if (cd != (iconv_t) -1) {
172    nconv = iconv(cd, &(deg_latin1_text[fahrenheit]), &deg_latin1_size,
173                  &degstr_ptr, &degstr_size);
174    iconv_close(cd);
175   
176    if (nconv != (size_t) -1)
177      return;     
178  }
179
180  /* There was an error during the conversion, use the default text */
181  strcpy(degstr, deg_default_text[fahrenheit]);
182}
183
184int main (int argc, char *argv[])
185{
186  int c,res,i,error;
187
188  struct option long_opts[] =  {
189    { "help", no_argument, NULL, 'h' },
190    { "set", no_argument, NULL, 's' },
191    { "version", no_argument, NULL, 'v'},
192    { "fahrenheit", no_argument, NULL, 'f' },
193    { "algorithm", no_argument, NULL, 'a' },
194    { "no-adapter", no_argument, NULL, 'A' },
195    { "no-unknown", no_argument, NULL, 'U' },
196    { "config-file", required_argument, NULL, 'c' },
197    { "unknown", no_argument, NULL, 'u' },
198    { 0,0,0,0 }
199  };
200
201  setlocale(LC_CTYPE, "");
202
203  do_unknown = 0;
204  do_sets = 0;
205  show_algorithm = 0;
206  hide_adapter = 0;
207  hide_unknown = 0;
208  while (1) {
209    c = getopt_long(argc,argv,"hsvfaAUc:u",long_opts,NULL);
210    if (c == EOF)
211      break;
212    switch(c) {
213    case ':':
214    case '?':
215      print_short_help();
216      exit(1);
217    case 'h':
218      print_long_help();
219      exit(0);
220    case 'v':
221      print_version();
222      exit(0);
223    case 'c':
224      config_file_name = strdup(optarg);
225      break;
226    case 's':
227      do_sets = 1;
228      break;
229    case 'f':
230      fahrenheit = 1;
231      break;
232    case 'a':
233      show_algorithm = 1;
234      break;
235    case 'A':
236      hide_adapter = 1;
237      break;
238    case 'U':
239      hide_unknown = 1;
240      break;
241    case 'u':
242      do_unknown = 1;
243      break;
244    default:
245      fprintf(stderr,"Internal error while parsing options!\n");
246      exit(1);
247    }
248  }
249
250  if (optind == argc) {
251    chips[0].prefix = SENSORS_CHIP_NAME_PREFIX_ANY;
252    chips[0].bus = SENSORS_CHIP_NAME_BUS_ANY;
253    chips[0].addr = SENSORS_CHIP_NAME_ADDR_ANY;
254    chips_count = 1;
255  } else 
256    for(i = optind; i < argc; i++) 
257      if ((res = sensors_parse_chip_name(argv[i],chips+chips_count))) {
258        fprintf(stderr,"Parse error in chip name `%s'\n",argv[i]);
259        print_short_help();
260        exit(1);
261      } else if (++chips_count == CHIPS_MAX) {
262        fprintf(stderr,"Too many chips on command line!\n");
263        exit(1);
264      }
265
266
267  open_config_file();
268
269  if ((res = sensors_init(config_file))) {
270    fprintf(stderr,"%s\n",sensors_strerror(res));
271    if (res == -SENSORS_ERR_PROC)
272      fprintf(stderr,
273              "Unable to find i2c bus information;\n"
274              "For 2.6 kernels, make sure you have mounted sysfs and done\n"
275              "'modprobe i2c_sensor'!\n"
276              "For older kernels, make sure you have done 'modprobe i2c-proc'!\n");
277    exit(1);
278  }
279
280  close_config_file();
281
282  /* build the degrees string */
283  set_degstr();
284
285  if(do_the_real_work(&error)) {
286    sensors_cleanup();
287    exit(error);
288  } else {
289    if(chips[0].prefix == SENSORS_CHIP_NAME_PREFIX_ANY)
290            fprintf(stderr,"No sensors found!\n");
291    else
292            fprintf(stderr,"Specified sensor(s) not found!\n");
293    sensors_cleanup();
294    exit(1);
295  }
296}
297
298/* returns number of chips found */
299int do_the_real_work(int *error)
300{
301  const sensors_chip_name *chip;
302  int chip_nr,i;
303  int cnt = 0;
304
305  *error = 0;
306  for (chip_nr = 0; (chip = sensors_get_detected_chips(&chip_nr));)
307    for(i = 0; i < chips_count; i++)
308      if (sensors_match_chip(*chip,chips[i])) {
309        if(do_sets) {
310          if (do_a_set(*chip))
311            *error = 1;
312        } else
313          do_a_print(*chip);
314        i = chips_count;
315        cnt++;
316      }
317   return(cnt);
318}
319
320/* returns 1 on error */
321int do_a_set(sensors_chip_name name)
322{
323  int res;
324
325  if ((res = sensors_do_chip_sets(name))) {
326    if (res == -SENSORS_ERR_PROC) {
327      fprintf(stderr,"%s: %s for writing;\n",sprintf_chip_name(name),
328              sensors_strerror(res));
329      fprintf(stderr,"Run as root?\n");
330      return 1;
331    } else if (res == -SENSORS_ERR_ACCESS_W) {
332      fprintf(stderr, "%s: At least one \"set\" statement failed\n",
333              sprintf_chip_name(name));
334    } else {
335      fprintf(stderr,"%s: %s\n",sprintf_chip_name(name),
336              sensors_strerror(res));
337    }
338  }
339  return 0;
340}
341
342const char *sprintf_chip_name(sensors_chip_name name)
343{
344  #define BUF_SIZE 200
345  static char buf[BUF_SIZE];
346
347  if (name.bus == SENSORS_CHIP_NAME_BUS_ISA)
348    snprintf(buf,BUF_SIZE,"%s-isa-%04x",name.prefix,name.addr);
349  else if (name.bus == SENSORS_CHIP_NAME_BUS_DUMMY)
350    snprintf(buf,BUF_SIZE,"%s-%s-%04x",name.prefix,name.busname,name.addr);
351  else
352    snprintf(buf,BUF_SIZE,"%s-i2c-%d-%02x",name.prefix,name.bus,name.addr);
353  return buf;
354}
355
356struct match {
357        const char * prefix;
358        void (*fn) (const sensors_chip_name *name);
359};
360
361struct match matches[] = {
362        { "ds1621", print_ds1621 },
363        { "lm75", print_lm75 },
364        { "adm1021", print_adm1021 },
365        { "max1617", print_adm1021 },
366        { "max1617a", print_adm1021 },
367        { "thmc10", print_adm1021 },
368        { "lm84", print_adm1021 },
369        { "gl523", print_adm1021 },
370        { "adm1023", print_adm1021 },
371        { "mc1066", print_adm1021 },
372        { "adm9240", print_adm9240 },
373        { "ds1780", print_adm9240 },
374        { "lm81", print_adm9240 },
375        { "lm78", print_lm78 },
376        { "lm78-j", print_lm78 },
377        { "lm79", print_lm78 },
378        { "mtp008", print_mtp008 },
379        { "sis5595", print_sis5595 },
380        { "via686a", print_via686a },
381        { "lm80", print_lm80 },
382        { "lm85", print_lm85 },
383        { "lm85b", print_lm85 },
384        { "lm85c", print_lm85 },
385        { "adm1027", print_lm85 },
386        { "adt7463", print_lm85 },
387        { "emc6d100", print_lm85 },
388        { "lm87", print_lm87 },
389        { "gl518sm", print_gl518 },
390        { "adm1025", print_adm1025 },
391        { "ne1619", print_adm1025 },
392        { "adm1024", print_adm1024 },
393        { "w83781d", print_w83781d },
394        { "w83782d", print_w83781d },
395        { "w83783d", print_w83781d },
396        { "w83627hf", print_w83781d },
397        { "w83627thf", print_w83781d },
398        { "w83637hf", print_w83781d },
399        { "w83697hf", print_w83781d },
400        { "w83791d", print_w83781d },
401        { "w83l785ts", print_w83l785ts },
402        { "as99127f", print_w83781d },
403        { "maxilife", print_maxilife },
404        { "maxilife-cg", print_maxilife },
405        { "maxilife-co", print_maxilife },
406        { "maxilife-as", print_maxilife },
407        { "maxilife-nba", print_maxilife },
408        { "it87", print_it87 },
409        { "it8712", print_it87 },
410        { "ddcmon", print_ddcmon },
411        { "eeprom", print_eeprom },
412        { "fscpos", print_fscpos },
413        { "fscscy", print_fscscy },
414        { "fscher", print_fscher },
415        { "pcf8591", print_pcf8591 },
416        { "vt1211", print_vt1211 },
417        { "smsc47m1", print_smsc47m1 },
418        { "pc87360", print_pc87360 },
419        { "pc87363", print_pc87360 },
420        { "pc87364", print_pc87364 },
421        { "pc87365", print_pc87366 },
422        { "pc87366", print_pc87366 },
423        { "lm92", print_lm92 },
424        { "vt8231", print_vt8231 },
425        { "bmc", print_bmc },
426        { "adm1026", print_adm1026 },
427        { "lm83", print_lm83 },
428        { "lm90", print_lm90 },
429        { "adm1032", print_lm90 },
430        { "lm99", print_lm90 },
431        { "lm86", print_lm90 },
432        { "max6657", print_lm90 },
433        { "lm63", print_lm63 },
434        { "xeontemp", print_xeontemp },
435        { "max6650", print_max6650 },
436        { "asb100", print_asb100 },
437        { "adm1030", print_adm1031 },
438        { "adm1031", print_adm1031 },
439        { "lm93", print_lm93 },
440        { NULL, NULL }
441};
442
443void do_a_print(sensors_chip_name name)
444{
445  const char *algo,*adap;
446  struct match *m;
447
448  /* do we know how to display it? */
449  for(m = matches; m->prefix != NULL; m++) {
450    if(!strcmp(name.prefix, m->prefix)) break;
451  }
452
453  if(m->prefix==NULL && hide_unknown)
454    return;
455
456  printf("%s\n",sprintf_chip_name(name));
457  adap = sensors_get_adapter_name(name.bus);
458  if (adap && !hide_adapter)
459    printf("Adapter: %s\n",adap);
460  algo = sensors_get_algorithm_name(name.bus);
461  if (algo && show_algorithm)
462    printf("Algorithm: %s\n",algo);
463  if (!algo || !adap)
464    printf(" ERROR: Can't get adapter or algorithm?!?\n");
465  if (do_unknown)
466    print_unknown_chip(&name);
467  else {
468    if(m->prefix == NULL)
469        print_unknown_chip(&name);
470    else
471        m->fn(&name);
472  }
473  printf("\n");
474}
Note: See TracBrowser for help on using the browser.