| 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 | |
|---|
| 30 | #define PROGRAM "sensors" |
|---|
| 31 | #define VERSION "1.0" |
|---|
| 32 | #define DEFAULT_CONFIG_FILE_NAME "sensors.conf" |
|---|
| 33 | |
|---|
| 34 | static char *config_file_name; |
|---|
| 35 | FILE *config_file; |
|---|
| 36 | static const char *config_file_path[] = |
|---|
| 37 | { "/etc", "/usr/lib/sensors", "/usr/local/lib/sensors", "/usr/lib", |
|---|
| 38 | "/usr/local/lib", ".", 0 }; |
|---|
| 39 | |
|---|
| 40 | extern int main(int argc, char *arv[]); |
|---|
| 41 | static void print_short_help(void); |
|---|
| 42 | static void print_long_help(void); |
|---|
| 43 | static void print_version(void); |
|---|
| 44 | static void open_config_file(void); |
|---|
| 45 | static int open_this_config_file(char *filename); |
|---|
| 46 | static void do_a_print(sensors_chip_name name); |
|---|
| 47 | static void do_a_set(sensors_chip_name name); |
|---|
| 48 | static void do_the_real_work(void); |
|---|
| 49 | static const char *sprintf_chip_name(sensors_chip_name name); |
|---|
| 50 | |
|---|
| 51 | #define CHIPS_MAX 20 |
|---|
| 52 | sensors_chip_name chips[CHIPS_MAX]; |
|---|
| 53 | int chips_count=0; |
|---|
| 54 | int do_sets; |
|---|
| 55 | |
|---|
| 56 | void print_short_help(void) |
|---|
| 57 | { |
|---|
| 58 | printf("Try `%s -h' for more information\n",PROGRAM); |
|---|
| 59 | } |
|---|
| 60 | |
|---|
| 61 | void print_long_help(void) |
|---|
| 62 | { |
|---|
| 63 | printf("Usage: %s [OPTION]... [CHIP]...\n",PROGRAM); |
|---|
| 64 | printf(" -c, --config-file Specify a config file\n"); |
|---|
| 65 | printf(" -h, --help Display this help text\n"); |
|---|
| 66 | printf(" -s, --set Execute `set' statements too (root only)\n"); |
|---|
| 67 | printf(" -v, --version Display the program version\n"); |
|---|
| 68 | printf("\n"); |
|---|
| 69 | printf("By default, a list of directories is examined for the config file `sensors.conf'\n"); |
|---|
| 70 | printf("Use `-' after `-c' to read the config file from stdin\n"); |
|---|
| 71 | printf("If no chips are specified, all chip info will be printed\n"); |
|---|
| 72 | } |
|---|
| 73 | |
|---|
| 74 | void print_version(void) |
|---|
| 75 | { |
|---|
| 76 | printf("%s version %s\n", PROGRAM, VERSION); |
|---|
| 77 | } |
|---|
| 78 | |
|---|
| 79 | /* This examines global var config_file, and leaves the name there too. |
|---|
| 80 | It also opens config_file. */ |
|---|
| 81 | void open_config_file(void) |
|---|
| 82 | { |
|---|
| 83 | #define MAX_FILENAME_LEN 1024 |
|---|
| 84 | char *filename; |
|---|
| 85 | char buffer[MAX_FILENAME_LEN]; |
|---|
| 86 | int res,i; |
|---|
| 87 | |
|---|
| 88 | if (config_file_name && !strcmp(config_file_name,"-")) { |
|---|
| 89 | config_file = stdin; |
|---|
| 90 | return; |
|---|
| 91 | } else if (config_file_name && index(config_file_name,'/')) { |
|---|
| 92 | if ((res = open_this_config_file(config_file_name))) { |
|---|
| 93 | fprintf(stderr,"Could not locate or open config file\n"); |
|---|
| 94 | fprintf(stderr,"%s: %s\n",config_file_name,strerror(res)); |
|---|
| 95 | exit(1); |
|---|
| 96 | } |
|---|
| 97 | } |
|---|
| 98 | else { |
|---|
| 99 | if (config_file_name) |
|---|
| 100 | filename = config_file_name; |
|---|
| 101 | else |
|---|
| 102 | filename = strdup(DEFAULT_CONFIG_FILE_NAME); |
|---|
| 103 | for (i = 0; config_file_path[i]; i++) { |
|---|
| 104 | if ((snprintf(buffer,MAX_FILENAME_LEN, |
|---|
| 105 | "%s/%s",config_file_path[i],filename)) < 1) { |
|---|
| 106 | fprintf(stderr, |
|---|
| 107 | "open_config_file: ridiculous long config file name!\n"); |
|---|
| 108 | exit(1); |
|---|
| 109 | } |
|---|
| 110 | if (!open_this_config_file(buffer)) { |
|---|
| 111 | free(config_file_name); |
|---|
| 112 | config_file_name = strdup(buffer); |
|---|
| 113 | return; |
|---|
| 114 | } |
|---|
| 115 | } |
|---|
| 116 | fprintf(stderr,"Could not locate or open config file!\n"); |
|---|
| 117 | exit(1); |
|---|
| 118 | } |
|---|
| 119 | } |
|---|
| 120 | |
|---|
| 121 | int open_this_config_file(char *filename) |
|---|
| 122 | { |
|---|
| 123 | config_file = fopen(filename,"r"); |
|---|
| 124 | if (! config_file) |
|---|
| 125 | return -errno; |
|---|
| 126 | return 0; |
|---|
| 127 | } |
|---|
| 128 | |
|---|
| 129 | |
|---|
| 130 | int main (int argc, char *argv[]) |
|---|
| 131 | { |
|---|
| 132 | int c,res,i; |
|---|
| 133 | |
|---|
| 134 | struct option long_opts[] = { |
|---|
| 135 | { "help", no_argument, NULL, 'h' }, |
|---|
| 136 | { "set", no_argument, NULL, 's' }, |
|---|
| 137 | { "version", no_argument, NULL, 'v'}, |
|---|
| 138 | { "config-file", required_argument, NULL, 'c' }, |
|---|
| 139 | { 0,0,0,0 } |
|---|
| 140 | }; |
|---|
| 141 | |
|---|
| 142 | do_sets = 0; |
|---|
| 143 | while (1) { |
|---|
| 144 | c = getopt_long(argc,argv,"hvsc:",long_opts,NULL); |
|---|
| 145 | if (c == EOF) |
|---|
| 146 | break; |
|---|
| 147 | switch(c) { |
|---|
| 148 | case ':': |
|---|
| 149 | case '?': |
|---|
| 150 | print_short_help(); |
|---|
| 151 | exit(1); |
|---|
| 152 | case 'h': |
|---|
| 153 | print_long_help(); |
|---|
| 154 | exit(0); |
|---|
| 155 | case 'v': |
|---|
| 156 | print_version(); |
|---|
| 157 | exit(0); |
|---|
| 158 | case 'c': |
|---|
| 159 | config_file_name = strdup(optarg); |
|---|
| 160 | break; |
|---|
| 161 | case 's': |
|---|
| 162 | do_sets = 1; |
|---|
| 163 | break; |
|---|
| 164 | default: |
|---|
| 165 | fprintf(stderr,"Internal error while parsing options!\n"); |
|---|
| 166 | exit(1); |
|---|
| 167 | } |
|---|
| 168 | } |
|---|
| 169 | |
|---|
| 170 | if (optind == argc) { |
|---|
| 171 | chips[0].prefix = SENSORS_CHIP_NAME_PREFIX_ANY; |
|---|
| 172 | chips[0].bus = SENSORS_CHIP_NAME_BUS_ANY; |
|---|
| 173 | chips[0].addr = SENSORS_CHIP_NAME_ADDR_ANY; |
|---|
| 174 | chips_count = 1; |
|---|
| 175 | } else |
|---|
| 176 | for(i = optind; i < argc; i++) |
|---|
| 177 | if ((res = sensors_parse_chip_name(argv[i],chips+chips_count))) { |
|---|
| 178 | fprintf(stderr,"Parse error in chip name `%s'\n",argv[i]); |
|---|
| 179 | exit(1); |
|---|
| 180 | } else if (++chips_count == CHIPS_MAX) { |
|---|
| 181 | fprintf(stderr,"Too many chips on command line!\n"); |
|---|
| 182 | exit(1); |
|---|
| 183 | } |
|---|
| 184 | |
|---|
| 185 | |
|---|
| 186 | open_config_file(); |
|---|
| 187 | |
|---|
| 188 | if ((res = sensors_init(config_file))) { |
|---|
| 189 | if (res == SENSORS_ERR_PROC) |
|---|
| 190 | fprintf(stderr, |
|---|
| 191 | "/proc/sys/dev/sensors/chips or /proc/bus/i2c unreadable:\n" |
|---|
| 192 | "Make sure you have inserted modules sensors.o and i2c-proc.o!"); |
|---|
| 193 | else |
|---|
| 194 | fprintf(stderr,"%s\n",sensors_strerror(res)); |
|---|
| 195 | exit(1); |
|---|
| 196 | } |
|---|
| 197 | |
|---|
| 198 | do_the_real_work(); |
|---|
| 199 | exit(0); |
|---|
| 200 | } |
|---|
| 201 | |
|---|
| 202 | void do_the_real_work(void) |
|---|
| 203 | { |
|---|
| 204 | const sensors_chip_name *chip; |
|---|
| 205 | int chip_nr,i; |
|---|
| 206 | |
|---|
| 207 | for (chip_nr = 0; (chip = sensors_get_detected_chips(&chip_nr));) |
|---|
| 208 | for(i = 0; i < chips_count; i++) |
|---|
| 209 | if (sensors_match_chip(*chip,chips[i])) { |
|---|
| 210 | if(do_sets) |
|---|
| 211 | do_a_set(*chip); |
|---|
| 212 | else |
|---|
| 213 | do_a_print(*chip); |
|---|
| 214 | i = chips_count; |
|---|
| 215 | } |
|---|
| 216 | } |
|---|
| 217 | |
|---|
| 218 | void do_a_set(sensors_chip_name name) |
|---|
| 219 | { |
|---|
| 220 | int res; |
|---|
| 221 | if ((res = sensors_do_chip_sets(name))) |
|---|
| 222 | fprintf(stderr,"%s: %s\n",sprintf_chip_name(name),sensors_strerror(res)); |
|---|
| 223 | } |
|---|
| 224 | |
|---|
| 225 | const char *sprintf_chip_name(sensors_chip_name name) |
|---|
| 226 | { |
|---|
| 227 | #define BUF_SIZE 200 |
|---|
| 228 | static char buf[BUF_SIZE]; |
|---|
| 229 | |
|---|
| 230 | if (name.bus == SENSORS_CHIP_NAME_BUS_ISA) |
|---|
| 231 | snprintf(buf,BUF_SIZE,"%s-isa-%04x",name.prefix,name.addr); |
|---|
| 232 | else |
|---|
| 233 | snprintf(buf,BUF_SIZE,"%s-i2c-%d-%02x",name.prefix,name.bus,name.addr); |
|---|
| 234 | return buf; |
|---|
| 235 | } |
|---|
| 236 | |
|---|
| 237 | void do_a_print(sensors_chip_name name) |
|---|
| 238 | { |
|---|
| 239 | const char *algo,*adap; |
|---|
| 240 | |
|---|
| 241 | printf("%s\n",sprintf_chip_name(name)); |
|---|
| 242 | adap = sensors_get_adapter_name(name.bus); |
|---|
| 243 | if (adap) |
|---|
| 244 | printf("Adapter: %s\n",adap); |
|---|
| 245 | algo = sensors_get_algorithm_name(name.bus); |
|---|
| 246 | if (algo) |
|---|
| 247 | printf("Algorithm: %s\n",algo); |
|---|
| 248 | if (!algo || !adap) |
|---|
| 249 | printf(" ERROR: Can't get adapter or algorithm?!?\n"); |
|---|
| 250 | if (!strcmp(name.prefix,"lm75")) |
|---|
| 251 | print_lm75(&name); |
|---|
| 252 | else if (!strcmp(name.prefix,"adm1021") || !strcmp(name.prefix,"max1617") || |
|---|
| 253 | !strcmp(name.prefix,"max1617a") || !strcmp(name.prefix, "thmc10") || |
|---|
| 254 | !strcmp(name.prefix,"lm84") || !strcmp(name.prefix, "gl523")) |
|---|
| 255 | print_adm1021(&name); |
|---|
| 256 | else if (!strcmp(name.prefix,"adm9240") || |
|---|
| 257 | !strcmp(name.prefix,"ds1780") || |
|---|
| 258 | !strcmp(name.prefix,"lm81")) |
|---|
| 259 | print_adm9240(&name); |
|---|
| 260 | else if (!strcmp(name.prefix,"lm78") || !strcmp(name.prefix,"lm78-j") || |
|---|
| 261 | !strcmp(name.prefix,"lm79")) |
|---|
| 262 | print_lm78(&name); |
|---|
| 263 | else if (!strcmp(name.prefix,"sis5595")) |
|---|
| 264 | print_sis5595(&name); |
|---|
| 265 | else if (!strcmp(name.prefix,"lm80")) |
|---|
| 266 | print_lm80(&name); |
|---|
| 267 | else if (!strcmp(name.prefix,"gl518sm")) |
|---|
| 268 | print_gl518(&name); |
|---|
| 269 | else if ((!strcmp(name.prefix,"w83781d")) || |
|---|
| 270 | (!strcmp(name.prefix,"w83782d")) || |
|---|
| 271 | (!strcmp(name.prefix,"w83783s")) || |
|---|
| 272 | (!strcmp(name.prefix,"w83627hf")) || |
|---|
| 273 | (!strcmp(name.prefix,"as99127f"))) |
|---|
| 274 | print_w83781d(&name); |
|---|
| 275 | else if (!strcmp(name.prefix,"maxilife-cg") || |
|---|
| 276 | !strcmp(name.prefix,"maxilife-co") || |
|---|
| 277 | !strcmp(name.prefix,"maxilife-as")) |
|---|
| 278 | print_maxilife(&name); |
|---|
| 279 | else if (!strcmp(name.prefix,"ddcmon")) |
|---|
| 280 | print_ddcmon(&name); |
|---|
| 281 | else if (!strcmp(name.prefix,"eeprom")) |
|---|
| 282 | print_eeprom(&name); |
|---|
| 283 | else |
|---|
| 284 | print_unknown_chip(&name); |
|---|
| 285 | printf("\n"); |
|---|
| 286 | } |
|---|