2
0
mirror of https://github.com/lm-sensors/lm-sensors synced 2025-09-02 23:35:57 +00:00

Added -p/--pid-file option to write a PID file for the daemon and

-d/--debug option to allow unwanted information to be more easily
suppressed. Kicked version to 0.5.0. -- merlin@merlin.org


git-svn-id: http://lm-sensors.org/svn/lm-sensors/trunk@1024 7894878c-1315-0410-8ee3-d5d059ff63e0
This commit is contained in:
Merlin Hughes
2001-02-11 18:51:24 +00:00
parent 0f1417afc7
commit 1d1859040b
7 changed files with 89 additions and 41 deletions

View File

@@ -3,7 +3,7 @@
* *
* A daemon that periodically logs sensor information to syslog. * A daemon that periodically logs sensor information to syslog.
* *
* Copyright (c) 1999-2000 Merlin Hughes <merlin@merlin.org> * Copyright (c) 1999-2001 Merlin Hughes <merlin@merlin.org>
* *
* This program is free software; you can redistribute it and/or modify * 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 * it under the terms of the GNU General Public License as published by
@@ -33,11 +33,13 @@
int isDaemon = 0; int isDaemon = 0;
const char *sensorsCfgFile = "sensors.conf"; const char *sensorsCfgFile = "sensors.conf";
const char *pidFile = "/var/run/sensord.pid";
int scanTime = 60; int scanTime = 60;
int logTime = 30 * 60; int logTime = 30 * 60;
int syslogFacility = LOG_LOCAL4; int syslogFacility = LOG_LOCAL4;
int doScan = 0; int doScan = 0;
int doSet = 0; int doSet = 0;
int debug = 0;
sensors_chip_name chipNames[MAX_CHIP_NAMES]; sensors_chip_name chipNames[MAX_CHIP_NAMES];
int numChipNames = 0; int numChipNames = 0;
@@ -92,6 +94,8 @@ static const char *daemonSyntax =
" -l, --log-interval <time> -- interval between logging sensors (default 30m)\n" " -l, --log-interval <time> -- interval between logging sensors (default 30m)\n"
" -f, --syslog-facility <f> -- syslog facility to use (default local4)\n" " -f, --syslog-facility <f> -- syslog facility to use (default local4)\n"
" -c, --config-file <file> -- configuration file (default sensors.conf)\n" " -c, --config-file <file> -- configuration file (default sensors.conf)\n"
" -p, --pid-file <file> -- PID file (default /var/run/sensord.pid)\n"
" -d, --debug -- display some debug information\n"
" -v, --version -- display version and exit\n" " -v, --version -- display version and exit\n"
" -h, --help -- display help and exit\n" " -h, --help -- display help and exit\n"
"\n" "\n"
@@ -107,6 +111,7 @@ static const char *appSyntax =
" -a, --alarm-scan -- only scan for alarms\n" " -a, --alarm-scan -- only scan for alarms\n"
" -s, --set -- execute set statements too (root only)\n" " -s, --set -- execute set statements too (root only)\n"
" -c, --config-file <file> -- configuration file (default sensors.conf)\n" " -c, --config-file <file> -- configuration file (default sensors.conf)\n"
" -d, --debug -- display some debug information\n"
" -v, --version -- display version and exit\n" " -v, --version -- display version and exit\n"
" -h, --help -- display help and exit\n" " -h, --help -- display help and exit\n"
"\n" "\n"
@@ -115,24 +120,27 @@ static const char *appSyntax =
"\n" "\n"
"If no chips are specified, all chip info will be printed.\n"; "If no chips are specified, all chip info will be printed.\n";
static const char *daemonShortOptions = "i:l:f:c:vh"; static const char *daemonShortOptions = "i:l:f:c:p:dvh";
static const struct option daemonLongOptions[] = { static const struct option daemonLongOptions[] = {
{ "interval", required_argument, NULL, 'i' }, { "interval", required_argument, NULL, 'i' },
{ "log-interval", required_argument, NULL, 'l' }, { "log-interval", required_argument, NULL, 'l' },
{ "syslog-facility", required_argument, NULL, 'f' }, { "syslog-facility", required_argument, NULL, 'f' },
{ "config-file", required_argument, NULL, 'c' }, { "config-file", required_argument, NULL, 'c' },
{ "pid-file", required_argument, NULL, 'p' },
{ "debug", no_argument, NULL, 'd' },
{ "version", no_argument, NULL, 'v' }, { "version", no_argument, NULL, 'v' },
{ "help", no_argument, NULL, 'h' }, { "help", no_argument, NULL, 'h' },
{ NULL, 0, NULL, 0 } { NULL, 0, NULL, 0 }
}; };
static const char *appShortOptions = "asc:vh"; static const char *appShortOptions = "asc:dvh";
static const struct option appLongOptions[] = { static const struct option appLongOptions[] = {
{ "alarm-scan", no_argument, NULL, 'a' }, { "alarm-scan", no_argument, NULL, 'a' },
{ "set", no_argument, NULL, 's' }, { "set", no_argument, NULL, 's' },
{ "config-file", required_argument, NULL, 'c' }, { "config-file", required_argument, NULL, 'c' },
{ "debug", no_argument, NULL, 'd' },
{ "version", no_argument, NULL, 'v' }, { "version", no_argument, NULL, 'v' },
{ "help", no_argument, NULL, 'h' }, { "help", no_argument, NULL, 'h' },
{ NULL, 0, NULL, 0 } { NULL, 0, NULL, 0 }
@@ -173,6 +181,13 @@ parseArgs
if ((sensorsCfgFile = strdup (optarg)) == NULL) if ((sensorsCfgFile = strdup (optarg)) == NULL)
return -1; return -1;
break; break;
case 'p':
if ((pidFile = strdup (optarg)) == NULL)
return -1;
break;
case 'd':
debug = 1;
break;
case 'v': case 'v':
printf ("sensord version %s\n", version); printf ("sensord version %s\n", version);
exit (EXIT_SUCCESS); exit (EXIT_SUCCESS);

View File

@@ -3,7 +3,7 @@
* *
* A daemon that periodically logs sensor information to syslog. * A daemon that periodically logs sensor information to syslog.
* *
* Copyright (c) 1999-2000 Merlin Hughes <merlin@merlin.org> * Copyright (c) 1999-2001 Merlin Hughes <merlin@merlin.org>
* *
* This program is free software; you can redistribute it and/or modify * 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 * it under the terms of the GNU General Public License as published by

View File

@@ -3,7 +3,7 @@
* *
* A daemon that periodically logs sensor information to syslog. * A daemon that periodically logs sensor information to syslog.
* *
* Copyright (c) 1999-2000 Merlin Hughes <merlin@merlin.org> * Copyright (c) 1999-2001 Merlin Hughes <merlin@merlin.org>
* *
* This program is free software; you can redistribute it and/or modify * 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 * it under the terms of the GNU General Public License as published by

View File

@@ -3,7 +3,7 @@
* *
* A daemon that periodically logs sensor information to syslog. * A daemon that periodically logs sensor information to syslog.
* *
* Copyright (c) 1999-2000 Merlin Hughes <merlin@merlin.org> * Copyright (c) 1999-2001 Merlin Hughes <merlin@merlin.org>
* *
* This program is free software; you can redistribute it and/or modify * 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 * it under the terms of the GNU General Public License as published by
@@ -214,6 +214,7 @@ doChips
(int action) { (int action) {
const sensors_chip_name *chip; const sensors_chip_name *chip;
int i = 0, j, ret = 0; int i = 0, j, ret = 0;
while ((ret == 0) && ((chip = sensors_get_detected_chips (&i)) != NULL)) { while ((ret == 0) && ((chip = sensors_get_detected_chips (&i)) != NULL)) {
for (j = 0; j < numChipNames; ++ j) { for (j = 0; j < numChipNames; ++ j) {
if (sensors_match_chip (*chip, chipNames[j])) { if (sensors_match_chip (*chip, chipNames[j])) {
@@ -222,6 +223,8 @@ doChips
} }
} }
} }
return ret;
} }
int int
@@ -241,9 +244,9 @@ scanChips
(void) { (void) {
int ret = 0; int ret = 0;
sensorLog (LOG_DEBUG, "sensor scan started"); sensorLog (LOG_DEBUG, "sensor sweeep started");
ret = doChips (DO_SCAN); ret = doChips (DO_SCAN);
sensorLog (LOG_DEBUG, "sensor scan finished"); sensorLog (LOG_DEBUG, "sensor sweep finished");
return ret; return ret;
} }

View File

@@ -1,4 +1,4 @@
.\" Copyright 1999-2000 Merlin Hughes <merlin@merlin.org> .\" Copyright 1999-2001 Merlin Hughes <merlin@merlin.org>
.\" sensord is distributed under the GPL .\" sensord is distributed under the GPL
.\" .\"
.\" Permission is granted to make and distribute verbatim copies of this .\" Permission is granted to make and distribute verbatim copies of this
@@ -21,7 +21,7 @@
.\" Formatted or processed versions of this manual, if unaccompanied by .\" Formatted or processed versions of this manual, if unaccompanied by
.\" the source, must acknowledge the copyright and authors of this work. .\" the source, must acknowledge the copyright and authors of this work.
.\" .\"
.TH sensord 8 "July 7, 2000" "Version 0.4.0" "Linux System Administration" .TH sensord 8 "February 11, 2001" "Version 0.5.0" "Linux System Administration"
.SH NAME .SH NAME
sensord \- Sensor information logging daemon. sensord \- Sensor information logging daemon.
.SH SYNOPSIS .SH SYNOPSIS
@@ -42,7 +42,7 @@ fan fails, a temperature limit is exceeded, etc.
.B Sensord .B Sensord
knows about certain chips, and outputs nicely formatted readings for them; but knows about certain chips, and outputs nicely formatted readings for them; but
it can also display the information of unknown chips, as long as it can also display the information of unknown chips, as long as
.BR libsensors .BR libsensors (3)
knows about them. knows about them.
.SH OPTIONS .SH OPTIONS
.IP "-i, --interval time" .IP "-i, --interval time"
@@ -78,13 +78,19 @@ or
.IR user . .IR user .
.IP "-c, --config-file file" .IP "-c, --config-file file"
Specify a Specify a
.BR libsensors .BR libsensors (3)
configuration file. If no file is specified, the name `sensors.conf' configuration file. If no file is specified, the name `sensors.conf'
is used. is used.
If the sensors configuration name does not contain a directory separator, If the sensors configuration name does not contain a directory separator,
the following paths are searched for the file: the following paths are searched for the file:
`/etc', `/usr/lib/sensors', `/usr/local/lib/sensors', `/usr/lib', `/usr/local/lib'. `/etc', `/usr/lib/sensors', `/usr/local/lib/sensors', `/usr/lib', `/usr/local/lib'.
.IP "-p, --pid-file file"
Specify what PID file to write; the default is to write the file
`/var/run/sensord.pid'. You should always specify an absolute path
here. The file is removed when the daemon exits.
.IP "-d, --debug"
Prints a small amount of additional debugging information.
.IP "-h, --help" .IP "-h, --help"
Prints a help message and exits. Prints a help message and exits.
.IP "-v, --version" .IP "-v, --version"
@@ -116,7 +122,8 @@ Alarms are logged at the level
.IR alert . .IR alert .
Inconsequential status messages are logged at Inconsequential status messages are logged at
the minimum level, the minimum level,
.IR debug . .IR debug ,
when debugging is enabled.
You can use an appropriate `/etc/syslog.conf' You can use an appropriate `/etc/syslog.conf'
file to direct these messages in a useful manner. See file to direct these messages in a useful manner. See
@@ -126,7 +133,7 @@ for full details, however the following is a sample configuration:
.nf .nf
# Sample syslog.conf entries # Sample syslog.conf entries
*.info;...;local4.none;local4.warn /var/log/messages *.info;...;local4.none;local4.warn /var/log/messages
local4.debug -/var/log/sensors local4.info -/var/log/sensors
local4.alert /dev/console local4.alert /dev/console
local4.alert * local4.alert *
.fi .fi
@@ -173,10 +180,10 @@ Typically, an alarm will only be signaled once,
even if the critical condition persists. This means that it is very even if the critical condition persists. This means that it is very
easy to miss an alarm! easy to miss an alarm!
In other cases, however, In other cases, however, uninteresting alarms (e.g., chassis
uninteresting alarms (e.g., chassis intrusion detection) will be intrusion detection) will be repeated continuously. You can
repeated continuously. You can configure configure
.BR libsensors .BR libsensors (3)
to ignore unwanted sensor reading such as these by placing an to ignore unwanted sensor reading such as these by placing an
`ignore' entry in the appropriate chip-specific section of the `ignore' entry in the appropriate chip-specific section of the
.BR sensors.conf (5) .BR sensors.conf (5)
@@ -201,28 +208,26 @@ your system is configured to issue an audio warning from the
motherboard if an alarm is signalled on that sensor. motherboard if an alarm is signalled on that sensor.
.SH MODULES .SH MODULES
It is expected that all required sensor modules are loaded prior to It is expected that all required sensor modules are loaded prior to
this daemon being started. Typically, this is achieved with an entry this daemon being started. This can either be achieved with a system
in `/etc/rc.d/...' containing specific module loading scheme (e.g., listing the required modules
in the file `/etc/modules' under Debian) or with explicit
.BR modprobe (1) .BR modprobe (1)
commands to load the required modules before starting the daemon. commands in an init script before loading the daemon.
For example, on my box I have a `sensord' initialization script For example, a `sensord' initialization script might
containing (among others) the following commands: contain (among others) the following commands:
.IP .IP
.nf .nf
# Sample /etc/rc.d/init.d scriptlet # Sample init.d scriptlet
echo -n "Loading AMD756 module: " echo -n "Loading AMD756 module: "
modprobe i2c-amd756 || { failure ; echo ; exit 1 ; } modprobe i2c-amd756 || { echo Fail. ; exit 1 ; }
echo_success echo OK.
echo
echo -n "Loading W83781D module: " echo -n "Loading W83781D module: "
modprobe w83781d || { failure ; echo ; exit 1 ; } modprobe w83781d || { echo Fail. ; exit 1 ; }
echo_success echo OK.
echo
echo -n "Starting sensord: " echo -n "Starting sensord: "
daemon sensord daemon sensord
RETVAL=$? ...
echo
.fi .fi
.PP .PP
Ignore the platform-specific shell functions; the general idea Ignore the platform-specific shell functions; the general idea
@@ -240,7 +245,7 @@ chips.
.I /etc/sensors.conf .I /etc/sensors.conf
.RS .RS
The system-wide The system-wide
.BR libsensors .BR libsensors (3)
configuration file. See configuration file. See
.BR sensors.conf (5) .BR sensors.conf (5)
for further details. for further details.

View File

@@ -3,7 +3,7 @@
* *
* A daemon that periodically logs sensor information to syslog. * A daemon that periodically logs sensor information to syslog.
* *
* Copyright (c) 1999-2000 Merlin Hughes <merlin@merlin.org> * Copyright (c) 1999-2001 Merlin Hughes <merlin@merlin.org>
* *
* This program is free software; you can redistribute it and/or modify * 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 * it under the terms of the GNU General Public License as published by
@@ -22,9 +22,13 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <signal.h> #include <signal.h>
#include <syslog.h> #include <syslog.h>
#include <unistd.h> #include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include "sensord.h" #include "sensord.h"
@@ -45,11 +49,13 @@ sensorLog
vsnprintf (buffer, LOG_BUFFER, fmt, ap); vsnprintf (buffer, LOG_BUFFER, fmt, ap);
buffer[LOG_BUFFER] = '\0'; buffer[LOG_BUFFER] = '\0';
va_end (ap); va_end (ap);
if (logOpened) { if (debug || (priority < LOG_DEBUG)) {
syslog (priority, "%s", buffer); if (logOpened) {
} else if (priority != LOG_DEBUG) { syslog (priority, "%s", buffer);
fprintf (stderr, "%s\n", buffer); } else {
fflush (stderr); fprintf (stderr, "%s\n", buffer);
fflush (stderr);
}
} }
} }
@@ -108,6 +114,8 @@ static void
daemonize daemonize
(void) { (void) {
int pid; int pid;
struct stat fileStat;
FILE *file;
openlog ("sensord", 0, syslogFacility); openlog ("sensord", 0, syslogFacility);
@@ -117,6 +125,17 @@ daemonize
perror ("chdir()"); perror ("chdir()");
exit (EXIT_FAILURE); exit (EXIT_FAILURE);
} }
if (!(stat (pidFile, &fileStat)) &&
((!S_ISREG (fileStat.st_mode)) || (fileStat.st_size > 11))) {
fprintf (stderr, "Error: PID file `%s' already exists and looks suspicious.\n", pidFile);
exit (EXIT_FAILURE);
}
if (!(file = fopen (pidFile, "w"))) {
fprintf (stderr, "fopen(\"%s\"): %s\n", pidFile, strerror (errno));
exit (EXIT_FAILURE);
}
/* I should use sigaction but... */ /* I should use sigaction but... */
if (signal (SIGTERM, signalHandler) == SIG_ERR) { if (signal (SIGTERM, signalHandler) == SIG_ERR) {
@@ -128,6 +147,8 @@ daemonize
perror ("fork()"); perror ("fork()");
exit (EXIT_FAILURE); exit (EXIT_FAILURE);
} else if (pid != 0) { } else if (pid != 0) {
fprintf (file, "%d\n", pid);
fclose (file);
exit (EXIT_SUCCESS); exit (EXIT_SUCCESS);
} }
@@ -136,6 +157,7 @@ daemonize
exit (EXIT_FAILURE); exit (EXIT_FAILURE);
} }
fclose (file);
close (STDIN_FILENO); close (STDIN_FILENO);
close (STDOUT_FILENO); close (STDOUT_FILENO);
close (STDERR_FILENO); close (STDERR_FILENO);
@@ -144,6 +166,7 @@ daemonize
static void static void
undaemonize undaemonize
(void) { (void) {
unlink (pidFile);
closelog (); closelog ();
} }

View File

@@ -3,7 +3,7 @@
* *
* A daemon that periodically logs sensor information to syslog. * A daemon that periodically logs sensor information to syslog.
* *
* Copyright (c) 1999-2000 Merlin Hughes <merlin@merlin.org> * Copyright (c) 1999-2001 Merlin Hughes <merlin@merlin.org>
* *
* This program is free software; you can redistribute it and/or modify * 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 * it under the terms of the GNU General Public License as published by
@@ -20,7 +20,7 @@
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/ */
#define version "0.4.0" #define version "0.5.0"
#include "lib/sensors.h" #include "lib/sensors.h"
@@ -30,11 +30,13 @@ extern void sensorLog (int priority, const char *fmt, ...);
extern int isDaemon; extern int isDaemon;
extern const char *sensorsCfgFile; extern const char *sensorsCfgFile;
extern const char *pidFile;
extern int scanTime; extern int scanTime;
extern int logTime; extern int logTime;
extern int syslogFacility; extern int syslogFacility;
extern int doScan; extern int doScan;
extern int doSet; extern int doSet;
extern int debug;
extern sensors_chip_name chipNames[]; extern sensors_chip_name chipNames[];
extern int numChipNames; extern int numChipNames;