1998-12-21 16:49:11 +00:00
|
|
|
/*
|
|
|
|
main.c - Part of sensors, a user-space program for hardware monitoring
|
1999-02-08 22:50:29 +00:00
|
|
|
Copyright (c) 1998, 1999 Frodo Looijaard <frodol@dds.nl>
|
1998-12-21 16:49:11 +00:00
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <getopt.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
2004-01-31 11:58:06 +00:00
|
|
|
#include <locale.h>
|
|
|
|
#include <langinfo.h>
|
2005-10-14 16:51:44 +00:00
|
|
|
|
|
|
|
#ifndef __UCLIBC__
|
2004-03-21 18:57:09 +00:00
|
|
|
#include <iconv.h>
|
2005-10-14 16:51:44 +00:00
|
|
|
#define HAVE_ICONV
|
|
|
|
#endif
|
1998-12-21 16:49:11 +00:00
|
|
|
|
2007-08-26 10:57:16 +00:00
|
|
|
#include "lib/sensors.h"
|
1998-12-21 16:49:11 +00:00
|
|
|
#include "lib/error.h"
|
1998-12-22 17:41:43 +00:00
|
|
|
#include "chips.h"
|
2001-01-31 03:51:06 +00:00
|
|
|
#include "version.h"
|
1998-12-21 16:49:11 +00:00
|
|
|
|
2007-08-26 10:57:16 +00:00
|
|
|
#define PROGRAM "sensors"
|
|
|
|
#define VERSION LM_VERSION
|
2007-08-26 08:42:20 +00:00
|
|
|
#define DEFAULT_CONFIG_FILE ETCDIR "/sensors.conf"
|
1998-12-21 16:49:11 +00:00
|
|
|
|
|
|
|
FILE *config_file;
|
|
|
|
|
|
|
|
extern int main(int argc, char *arv[]);
|
|
|
|
static void print_short_help(void);
|
|
|
|
static void print_long_help(void);
|
|
|
|
static void print_version(void);
|
2007-08-13 20:19:44 +00:00
|
|
|
static void do_a_print(const sensors_chip_name *name);
|
|
|
|
static int do_a_set(const sensors_chip_name *name);
|
2007-08-26 08:28:10 +00:00
|
|
|
static int do_the_real_work(const sensors_chip_name *chip, int *error);
|
2007-08-13 20:19:44 +00:00
|
|
|
static const char *sprintf_chip_name(const sensors_chip_name *name);
|
1998-12-29 20:33:34 +00:00
|
|
|
|
2007-08-23 07:45:58 +00:00
|
|
|
int do_sets, do_raw, fahrenheit, hide_adapter;
|
1998-12-21 16:49:11 +00:00
|
|
|
|
2004-01-31 10:17:15 +00:00
|
|
|
char degstr[5]; /* store the correct string to print degrees */
|
|
|
|
|
1998-12-21 16:49:11 +00:00
|
|
|
void print_short_help(void)
|
|
|
|
{
|
2007-08-26 10:57:16 +00:00
|
|
|
printf("Try `%s -h' for more information\n", PROGRAM);
|
1998-12-21 16:49:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void print_long_help(void)
|
|
|
|
{
|
2007-08-26 10:57:16 +00:00
|
|
|
printf("Usage: %s [OPTION]... [CHIP]...\n", PROGRAM);
|
|
|
|
printf(" -c, --config-file Specify a config file (default: %s)\n",
|
|
|
|
DEFAULT_CONFIG_FILE);
|
|
|
|
puts(" -h, --help Display this help text\n"
|
|
|
|
" -s, --set Execute `set' statements (root only)\n"
|
|
|
|
" -f, --fahrenheit Show temperatures in degrees fahrenheit\n"
|
|
|
|
" -A, --no-adapter Do not show adapter for each chip\n"
|
|
|
|
" -u Raw output (debugging only)\n"
|
|
|
|
" -v, --version Display the program version\n"
|
|
|
|
"\n"
|
|
|
|
"Use `-' after `-c' to read the config file from stdin.\n"
|
|
|
|
"If no chips are specified, all chip info will be printed.\n"
|
|
|
|
"Example chip names:\n"
|
|
|
|
"\tlm78-i2c-0-2d\t*-i2c-0-2d\n"
|
|
|
|
"\tlm78-i2c-0-*\t*-i2c-0-*\n"
|
|
|
|
"\tlm78-i2c-*-2d\t*-i2c-*-2d\n"
|
|
|
|
"\tlm78-i2c-*-*\t*-i2c-*-*\n"
|
|
|
|
"\tlm78-isa-0290\t*-isa-0290\n"
|
|
|
|
"\tlm78-isa-*\t*-isa-*\n"
|
|
|
|
"\tlm78-*");
|
1998-12-21 16:49:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void print_version(void)
|
|
|
|
{
|
2007-08-26 10:57:16 +00:00
|
|
|
printf("%s version %s with libsensors version %s\n", PROGRAM, VERSION,
|
|
|
|
libsensors_version);
|
1998-12-21 16:49:11 +00:00
|
|
|
}
|
|
|
|
|
2007-08-26 10:57:16 +00:00
|
|
|
/* This examines global var config_file, and leaves the name there too.
|
1998-12-21 16:49:11 +00:00
|
|
|
It also opens config_file. */
|
2007-07-03 16:03:22 +00:00
|
|
|
static void open_config_file(const char* config_file_name)
|
1998-12-21 16:49:11 +00:00
|
|
|
{
|
2007-08-26 10:57:16 +00:00
|
|
|
if (!strcmp(config_file_name, "-")) {
|
|
|
|
config_file = stdin;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
config_file = fopen(config_file_name, "r");
|
|
|
|
if (!config_file) {
|
|
|
|
fprintf(stderr, "Could not open config file\n");
|
|
|
|
perror(config_file_name);
|
|
|
|
exit(1);
|
|
|
|
}
|
1998-12-21 16:49:11 +00:00
|
|
|
}
|
2007-08-26 10:57:16 +00:00
|
|
|
|
2007-07-03 16:03:22 +00:00
|
|
|
static void close_config_file(const char* config_file_name)
|
1998-12-21 16:49:11 +00:00
|
|
|
{
|
2007-08-26 10:57:16 +00:00
|
|
|
if (fclose(config_file) == EOF) {
|
|
|
|
fprintf(stderr, "Could not close config file\n");
|
|
|
|
perror(config_file_name);
|
|
|
|
}
|
2003-11-23 21:24:07 +00:00
|
|
|
}
|
1998-12-21 16:49:11 +00:00
|
|
|
|
2004-03-21 18:57:09 +00:00
|
|
|
static void set_degstr(void)
|
|
|
|
{
|
2007-08-26 10:57:16 +00:00
|
|
|
const char *deg_default_text[2] = { " C", " F" };
|
2005-10-14 16:51:44 +00:00
|
|
|
|
|
|
|
#ifdef HAVE_ICONV
|
2007-08-26 10:57:16 +00:00
|
|
|
/* Size hardcoded for better performance.
|
|
|
|
Don't forget to count the trailing \0! */
|
|
|
|
size_t deg_latin1_size = 3;
|
|
|
|
char *deg_latin1_text[2] = { "\260C", "\260F" };
|
|
|
|
size_t nconv;
|
|
|
|
size_t degstr_size = sizeof(degstr);
|
|
|
|
char *degstr_ptr = degstr;
|
|
|
|
|
|
|
|
iconv_t cd = iconv_open(nl_langinfo(CODESET), "ISO-8859-1");
|
|
|
|
if (cd != (iconv_t) -1) {
|
|
|
|
nconv = iconv(cd, &(deg_latin1_text[fahrenheit]),
|
|
|
|
°_latin1_size, °str_ptr, °str_size);
|
|
|
|
iconv_close(cd);
|
|
|
|
|
|
|
|
if (nconv != (size_t) -1)
|
|
|
|
return;
|
|
|
|
}
|
2005-10-14 16:51:44 +00:00
|
|
|
#endif /* HAVE_ICONV */
|
2004-03-21 18:57:09 +00:00
|
|
|
|
2007-08-26 10:57:16 +00:00
|
|
|
/* There was an error during the conversion, use the default text */
|
|
|
|
strcpy(degstr, deg_default_text[fahrenheit]);
|
2004-03-21 18:57:09 +00:00
|
|
|
}
|
|
|
|
|
1998-12-21 16:49:11 +00:00
|
|
|
int main (int argc, char *argv[])
|
|
|
|
{
|
2007-08-26 10:57:16 +00:00
|
|
|
int c, res, i, error;
|
|
|
|
const char *config_file_name = DEFAULT_CONFIG_FILE;
|
|
|
|
|
|
|
|
struct option long_opts[] = {
|
|
|
|
{ "help", no_argument, NULL, 'h' },
|
|
|
|
{ "set", no_argument, NULL, 's' },
|
|
|
|
{ "version", no_argument, NULL, 'v'},
|
|
|
|
{ "fahrenheit", no_argument, NULL, 'f' },
|
|
|
|
{ "no-adapter", no_argument, NULL, 'A' },
|
|
|
|
{ "config-file", required_argument, NULL, 'c' },
|
|
|
|
{ 0, 0, 0, 0 }
|
|
|
|
};
|
|
|
|
|
|
|
|
setlocale(LC_CTYPE, "");
|
|
|
|
|
|
|
|
do_raw = 0;
|
|
|
|
do_sets = 0;
|
|
|
|
hide_adapter = 0;
|
|
|
|
while (1) {
|
|
|
|
c = getopt_long(argc, argv, "hsvfAc:u", long_opts, NULL);
|
|
|
|
if (c == EOF)
|
|
|
|
break;
|
|
|
|
switch(c) {
|
|
|
|
case ':':
|
|
|
|
case '?':
|
|
|
|
print_short_help();
|
|
|
|
exit(1);
|
|
|
|
case 'h':
|
|
|
|
print_long_help();
|
|
|
|
exit(0);
|
|
|
|
case 'v':
|
|
|
|
print_version();
|
|
|
|
exit(0);
|
|
|
|
case 'c':
|
|
|
|
config_file_name = optarg;
|
|
|
|
break;
|
|
|
|
case 's':
|
|
|
|
do_sets = 1;
|
|
|
|
break;
|
|
|
|
case 'f':
|
|
|
|
fahrenheit = 1;
|
|
|
|
break;
|
|
|
|
case 'A':
|
|
|
|
hide_adapter = 1;
|
|
|
|
break;
|
|
|
|
case 'u':
|
|
|
|
do_raw = 1;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
fprintf(stderr,
|
|
|
|
"Internal error while parsing options!\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
open_config_file(config_file_name);
|
|
|
|
if ((res = sensors_init(config_file))) {
|
|
|
|
fprintf(stderr, "sensors_init: %s\n", sensors_strerror(res));
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
close_config_file(config_file_name);
|
|
|
|
|
|
|
|
/* build the degrees string */
|
|
|
|
set_degstr();
|
|
|
|
|
|
|
|
if (optind == argc) { /* No chip name on command line */
|
|
|
|
if (!do_the_real_work(NULL, &error)) {
|
|
|
|
fprintf(stderr,
|
|
|
|
"No sensors found!\n"
|
|
|
|
"Make sure you loaded all the kernel drivers you need.\n"
|
|
|
|
"Try sensors-detect to find out which these are.\n");
|
|
|
|
error = 1;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
int cnt = 0;
|
|
|
|
sensors_chip_name chip;
|
|
|
|
|
|
|
|
for (i = optind; i < argc; i++) {
|
|
|
|
if (sensors_parse_chip_name(argv[i], &chip)) {
|
|
|
|
fprintf(stderr,
|
|
|
|
"Parse error in chip name `%s'\n",
|
|
|
|
argv[i]);
|
|
|
|
print_short_help();
|
|
|
|
error = 1;
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
cnt += do_the_real_work(&chip, &error);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!cnt) {
|
|
|
|
fprintf(stderr, "Specified sensor(s) not found!\n");
|
|
|
|
error = 1;
|
|
|
|
}
|
|
|
|
}
|
2007-08-26 08:28:10 +00:00
|
|
|
|
|
|
|
exit:
|
2007-08-26 10:57:16 +00:00
|
|
|
sensors_cleanup();
|
|
|
|
exit(res);
|
1998-12-21 16:49:11 +00:00
|
|
|
}
|
1998-12-24 18:40:58 +00:00
|
|
|
|
2000-12-14 01:20:52 +00:00
|
|
|
/* returns number of chips found */
|
2007-08-26 08:28:10 +00:00
|
|
|
int do_the_real_work(const sensors_chip_name *match, int *error)
|
1998-12-29 20:33:34 +00:00
|
|
|
{
|
2007-08-26 10:57:16 +00:00
|
|
|
const sensors_chip_name *chip;
|
|
|
|
int chip_nr;
|
|
|
|
int cnt = 0;
|
|
|
|
|
|
|
|
chip_nr = 0;
|
|
|
|
while ((chip = sensors_get_detected_chips(match, &chip_nr))) {
|
|
|
|
if (do_sets) {
|
|
|
|
if (do_a_set(chip))
|
|
|
|
*error = 1;
|
|
|
|
} else
|
|
|
|
do_a_print(chip);
|
|
|
|
cnt++;
|
|
|
|
}
|
|
|
|
return cnt;
|
1998-12-29 20:33:34 +00:00
|
|
|
}
|
|
|
|
|
2003-08-09 08:39:44 +00:00
|
|
|
/* returns 1 on error */
|
2007-08-13 20:19:44 +00:00
|
|
|
int do_a_set(const sensors_chip_name *name)
|
1998-12-29 20:33:34 +00:00
|
|
|
{
|
2007-08-26 10:57:16 +00:00
|
|
|
int res;
|
|
|
|
|
|
|
|
if ((res = sensors_do_chip_sets(name))) {
|
|
|
|
if (res == -SENSORS_ERR_PROC) {
|
|
|
|
fprintf(stderr, "%s: %s for writing;\n",
|
|
|
|
sprintf_chip_name(name),
|
|
|
|
sensors_strerror(res));
|
|
|
|
fprintf(stderr, "Run as root?\n");
|
|
|
|
return 1;
|
|
|
|
} else if (res == -SENSORS_ERR_ACCESS_W) {
|
|
|
|
fprintf(stderr,
|
|
|
|
"%s: At least one \"set\" statement failed\n",
|
|
|
|
sprintf_chip_name(name));
|
|
|
|
} else {
|
|
|
|
fprintf(stderr, "%s: %s\n", sprintf_chip_name(name),
|
|
|
|
sensors_strerror(res));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
1998-12-29 20:33:34 +00:00
|
|
|
}
|
|
|
|
|
2007-08-13 20:19:44 +00:00
|
|
|
const char *sprintf_chip_name(const sensors_chip_name *name)
|
1998-12-29 20:33:34 +00:00
|
|
|
{
|
2007-08-26 10:57:16 +00:00
|
|
|
#define BUF_SIZE 200
|
|
|
|
static char buf[BUF_SIZE];
|
1998-12-24 18:40:58 +00:00
|
|
|
|
2007-08-26 10:57:16 +00:00
|
|
|
if (sensors_snprintf_chip_name(buf, BUF_SIZE, name) < 0)
|
|
|
|
return NULL;
|
|
|
|
return buf;
|
1998-12-29 20:33:34 +00:00
|
|
|
}
|
|
|
|
|
2007-08-13 20:19:44 +00:00
|
|
|
void do_a_print(const sensors_chip_name *name)
|
1998-12-29 20:33:34 +00:00
|
|
|
{
|
2007-08-26 10:57:16 +00:00
|
|
|
printf("%s\n", sprintf_chip_name(name));
|
|
|
|
if (!hide_adapter) {
|
|
|
|
const char *adap = sensors_get_adapter_name(&name->bus);
|
|
|
|
if (adap)
|
|
|
|
printf("Adapter: %s\n", adap);
|
|
|
|
else
|
|
|
|
fprintf(stderr, "Can't get adapter name\n");
|
|
|
|
}
|
|
|
|
if (do_raw)
|
|
|
|
print_chip_raw(name);
|
|
|
|
else
|
|
|
|
print_generic_chip(name);
|
|
|
|
printf("\n");
|
1998-12-29 20:33:34 +00:00
|
|
|
}
|