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

Revision 2243, 12.4 KB (checked in by khali, 9 years ago)

Separate prefix for NE1619.

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