nut-debian/drivers/upsdrvctl.c

626 lines
14 KiB
C
Raw Normal View History

2010-03-26 01:20:59 +02:00
/* upsdrvctl.c - UPS driver controller
Copyright (C) 2001 Russell Kroll <rkroll@exploits.org>
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
2022-07-10 10:23:45 +03:00
#include "config.h" /* must be the first header */
2010-03-26 01:20:59 +02:00
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include "proto.h"
#include "common.h"
#include "upsconf.h"
2022-07-10 10:23:45 +03:00
#include "attribute.h"
#include "nut_stdint.h"
2010-03-26 01:20:59 +02:00
typedef struct {
char *upsname;
char *driver;
char *port;
int sdorder;
int maxstartdelay;
void *next;
} ups_t;
2011-01-26 11:35:08 +02:00
static ups_t *upstable = NULL;
2010-03-26 01:20:59 +02:00
2011-01-26 11:35:08 +02:00
static int maxsdorder = 0, testmode = 0, exec_error = 0;
2010-03-26 01:20:59 +02:00
2022-07-10 10:23:45 +03:00
/* Should we wait for driver (1) or "parallelize" drivers start (0) */
static int waitfordrivers = 1;
2010-03-26 01:20:59 +02:00
/* timer - keeps us from getting stuck if a driver hangs */
2011-01-26 11:35:08 +02:00
static int maxstartdelay = 45;
2010-03-26 01:20:59 +02:00
2014-04-22 21:39:47 +03:00
/* counter - retry that many time(s) to start the driver if it fails to */
static int maxretry = 1;
/* timer - delay between each restart attempt of the driver(s) */
static int retrydelay = 5;
2010-03-26 01:20:59 +02:00
/* Directory where driver executables live */
2011-01-26 11:35:08 +02:00
static char *driverpath = NULL;
2010-03-26 01:20:59 +02:00
/* passthrough to the drivers: chroot path and new user name */
2011-01-26 11:35:08 +02:00
static char *pt_root = NULL, *pt_user = NULL;
2010-03-26 01:20:59 +02:00
2022-07-10 10:23:45 +03:00
/* flag to pass nut_debug_level to launched drivers (as their -D... args) */
static int nut_debug_level_passthrough = 0;
2010-03-26 01:20:59 +02:00
void do_upsconf_args(char *upsname, char *var, char *val)
{
ups_t *tmp, *last;
/* handle global declarations */
if (!upsname) {
if (!strcmp(var, "maxstartdelay"))
maxstartdelay = atoi(val);
if (!strcmp(var, "driverpath")) {
free(driverpath);
driverpath = xstrdup(val);
}
2014-04-22 21:39:47 +03:00
if (!strcmp(var, "maxretry"))
maxretry = atoi(val);
if (!strcmp(var, "retrydelay"))
retrydelay = atoi(val);
2022-07-10 10:23:45 +03:00
if (!strcmp(var, "nowait"))
waitfordrivers = 0;
2010-03-26 01:20:59 +02:00
/* ignore anything else - it's probably for main */
return;
}
last = tmp = upstable;
while (tmp) {
last = tmp;
if (!strcmp(tmp->upsname, upsname)) {
if (!strcmp(var, "driver"))
tmp->driver = xstrdup(val);
if (!strcmp(var, "port"))
tmp->port = xstrdup(val);
if (!strcmp(var, "maxstartdelay"))
tmp->maxstartdelay = atoi(val);
if (!strcmp(var, "sdorder")) {
tmp->sdorder = atoi(val);
if (tmp->sdorder > maxsdorder)
maxsdorder = tmp->sdorder;
}
return;
}
tmp = tmp->next;
}
tmp = xmalloc(sizeof(ups_t));
tmp->upsname = xstrdup(upsname);
tmp->driver = NULL;
tmp->port = NULL;
tmp->next = NULL;
tmp->sdorder = 0;
tmp->maxstartdelay = -1; /* use global value by default */
if (!strcmp(var, "driver"))
tmp->driver = xstrdup(val);
if (!strcmp(var, "port"))
tmp->port = xstrdup(val);
if (last)
last->next = tmp;
else
upstable = tmp;
}
/* handle sending the signal */
static void stop_driver(const ups_t *ups)
{
char pidfn[SMALLBUF];
int ret;
2011-01-26 11:35:08 +02:00
struct stat fs;
2010-03-26 01:20:59 +02:00
upsdebugx(1, "Stopping UPS: %s", ups->upsname);
snprintf(pidfn, sizeof(pidfn), "%s/%s-%s.pid", altpidpath(),
ups->driver, ups->upsname);
ret = stat(pidfn, &fs);
2011-01-26 11:35:08 +02:00
if ((ret != 0) && (ups->port != NULL)) {
2022-07-10 10:23:45 +03:00
upslog_with_errno(LOG_ERR, "Can't open %s", pidfn);
2010-03-26 01:20:59 +02:00
snprintf(pidfn, sizeof(pidfn), "%s/%s-%s.pid", altpidpath(),
ups->driver, xbasename(ups->port));
ret = stat(pidfn, &fs);
}
if (ret != 0) {
2022-07-10 10:23:45 +03:00
upslog_with_errno(LOG_ERR, "Can't open %s either", pidfn);
2010-03-26 01:20:59 +02:00
exec_error++;
return;
}
upsdebugx(2, "Sending signal to %s", pidfn);
if (testmode)
return;
ret = sendsignalfn(pidfn, SIGTERM);
if (ret < 0) {
upslog_with_errno(LOG_ERR, "Stopping %s failed", pidfn);
exec_error++;
return;
}
}
static void waitpid_timeout(const int sig)
{
2022-07-10 10:23:45 +03:00
NUT_UNUSED_VARIABLE(sig);
2010-03-26 01:20:59 +02:00
/* do nothing */
return;
}
/* print out a command line at the given debug level. */
2011-01-26 11:35:08 +02:00
static void debugcmdline(int level, const char *msg, char *const argv[])
{
char cmdline[LARGEBUF];
snprintf(cmdline, sizeof(cmdline), "%s", msg);
2010-03-26 01:20:59 +02:00
while (*argv) {
2011-01-26 11:35:08 +02:00
snprintfcat(cmdline, sizeof(cmdline), " %s", *argv++);
2010-03-26 01:20:59 +02:00
}
2011-01-26 11:35:08 +02:00
2010-03-26 01:20:59 +02:00
upsdebugx(level, "%s", cmdline);
}
2011-01-26 11:35:08 +02:00
static void forkexec(char *const argv[], const ups_t *ups)
2010-03-26 01:20:59 +02:00
{
int ret;
pid_t pid;
pid = fork();
if (pid < 0)
fatal_with_errno(EXIT_FAILURE, "fork");
if (pid != 0) { /* parent */
int wstat;
struct sigaction sa;
2022-07-10 10:23:45 +03:00
/* Handle "parallel" drivers startup */
if (waitfordrivers == 0) {
upsdebugx(2, "'nowait' set, continuing...");
return;
}
2010-03-26 01:20:59 +02:00
sigemptyset(&sa.sa_mask);
sa.sa_flags = 0;
sa.sa_handler = waitpid_timeout;
sigaction(SIGALRM, &sa, NULL);
2022-07-10 10:23:45 +03:00
/* Use the local maxstartdelay, if available */
if (ups->maxstartdelay != -1) {
if (ups->maxstartdelay >= 0)
alarm((unsigned int)ups->maxstartdelay);
} else { /* Otherwise, use the global (or default) value */
if (maxstartdelay >= 0)
alarm((unsigned int)maxstartdelay);
}
2010-03-26 01:20:59 +02:00
ret = waitpid(pid, &wstat, 0);
alarm(0);
if (ret == -1) {
upslogx(LOG_WARNING, "Startup timer elapsed, continuing...");
exec_error++;
return;
}
if (WIFEXITED(wstat) == 0) {
upslogx(LOG_WARNING, "Driver exited abnormally");
exec_error++;
return;
}
if (WEXITSTATUS(wstat) != 0) {
upslogx(LOG_WARNING, "Driver failed to start"
" (exit status=%d)", WEXITSTATUS(wstat));
exec_error++;
return;
}
/* the rest only work when WIFEXITED is nonzero */
if (WIFSIGNALED(wstat)) {
upslog_with_errno(LOG_WARNING, "Driver died after signal %d",
WTERMSIG(wstat));
exec_error++;
}
return;
}
/* child */
2011-01-26 11:35:08 +02:00
ret = execv(argv[0], argv);
2010-03-26 01:20:59 +02:00
/* shouldn't get here */
fatal_with_errno(EXIT_FAILURE, "execv");
}
static void start_driver(const ups_t *ups)
{
2022-07-10 10:23:45 +03:00
char *argv[9];
char dfn[SMALLBUF], dbg[SMALLBUF];
2010-03-26 01:20:59 +02:00
int ret, arg = 0;
2014-04-22 21:39:47 +03:00
int initial_exec_error = exec_error, drv_maxretry = maxretry;
2011-01-26 11:35:08 +02:00
struct stat fs;
2010-03-26 01:20:59 +02:00
upsdebugx(1, "Starting UPS: %s", ups->upsname);
snprintf(dfn, sizeof(dfn), "%s/%s", driverpath, ups->driver);
ret = stat(dfn, &fs);
if (ret < 0)
fatal_with_errno(EXIT_FAILURE, "Can't start %s", dfn);
argv[arg++] = dfn;
2022-07-10 10:23:45 +03:00
if (nut_debug_level_passthrough > 0
&& nut_debug_level > 0
&& sizeof(dbg) > 3
) {
size_t d, m;
/* cut-off point: buffer size or requested debug level */
m = sizeof(dbg) - 1; /* leave a place for '\0' */
#if (defined HAVE_PRAGMA_GCC_DIAGNOSTIC_PUSH_POP) && ( (defined HAVE_PRAGMA_GCC_DIAGNOSTIC_IGNORED_TYPE_LIMITS) || (defined HAVE_PRAGMA_GCC_DIAGNOSTIC_IGNORED_TAUTOLOGICAL_CONSTANT_OUT_OF_RANGE_COMPARE) || (defined HAVE_PRAGMA_GCC_DIAGNOSTIC_IGNORED_UNREACHABLE_CODE) )
# pragma GCC diagnostic push
#endif
#ifdef HAVE_PRAGMA_GCC_DIAGNOSTIC_IGNORED_UNREACHABLE_CODE
# pragma GCC diagnostic ignored "-Wunreachable-code"
#endif
#ifdef HAVE_PRAGMA_GCC_DIAGNOSTIC_IGNORED_TYPE_LIMITS
# pragma GCC diagnostic ignored "-Wtype-limits"
#endif
#ifdef HAVE_PRAGMA_GCC_DIAGNOSTIC_IGNORED_TAUTOLOGICAL_CONSTANT_OUT_OF_RANGE_COMPARE
# pragma GCC diagnostic ignored "-Wtautological-constant-out-of-range-compare"
#endif
#ifdef __clang__
# pragma clang diagnostic push
# pragma clang diagnostic ignored "-Wunreachable-code"
# pragma clang diagnostic ignored "-Wtautological-compare"
# pragma clang diagnostic ignored "-Wtautological-constant-out-of-range-compare"
#endif
/* Different platforms, different sizes, none fits all... */
/* can we fit this many 'D's? */
if ((uintmax_t)SIZE_MAX > (uintmax_t)nut_debug_level /* else can't assign, requested debug level is huge */
&& (size_t)nut_debug_level + 1 < m
) {
#ifdef __clang__
# pragma clang diagnostic pop
#endif
#if (defined HAVE_PRAGMA_GCC_DIAGNOSTIC_PUSH_POP) && ( (defined HAVE_PRAGMA_GCC_DIAGNOSTIC_IGNORED_TYPE_LIMITS) || (defined HAVE_PRAGMA_GCC_DIAGNOSTIC_IGNORED_TAUTOLOGICAL_CONSTANT_OUT_OF_RANGE_COMPARE) || (defined HAVE_PRAGMA_GCC_DIAGNOSTIC_IGNORED_UNREACHABLE_CODE) )
# pragma GCC diagnostic pop
#endif
/* need even fewer (leave a place for '-'): */
m = (size_t)nut_debug_level + 1;
} else {
upsdebugx(1, "Requested debugging level %d is too "
"high for pass-through args, truncated to %zu",
nut_debug_level,
(m - 1) /* count off '-' (and '\0' already) chars */
);
}
dbg[0] = '-';
for (d = 1; d < m ; d++) {
dbg[d] = 'D';
}
dbg[d] = '\0';
argv[arg++] = dbg;
}
2011-01-26 11:35:08 +02:00
argv[arg++] = (char *)"-a"; /* FIXME: cast away const */
2010-03-26 01:20:59 +02:00
argv[arg++] = ups->upsname;
/* stick on the chroot / user args if given to us */
if (pt_root) {
2011-01-26 11:35:08 +02:00
argv[arg++] = (char *)"-r"; /* FIXME: cast away const */
2010-03-26 01:20:59 +02:00
argv[arg++] = pt_root;
}
if (pt_user) {
2011-01-26 11:35:08 +02:00
argv[arg++] = (char *)"-u"; /* FIXME: cast away const */
2010-03-26 01:20:59 +02:00
argv[arg++] = pt_user;
}
/* tie it off */
argv[arg++] = NULL;
2014-04-22 21:39:47 +03:00
while (drv_maxretry > 0) {
int cur_exec_error = exec_error;
upsdebugx(2, "%i remaining attempts", drv_maxretry);
debugcmdline(2, "exec: ", argv);
drv_maxretry--;
if (!testmode) {
forkexec(argv, ups);
}
/* driver command succeeded */
if (cur_exec_error == exec_error) {
drv_maxretry = 0;
exec_error = initial_exec_error;
}
else {
/* otherwise, retry if still needed */
if (drv_maxretry > 0)
2022-07-10 10:23:45 +03:00
if (retrydelay >= 0)
sleep ((unsigned int)retrydelay);
2014-04-22 21:39:47 +03:00
}
2011-01-26 11:35:08 +02:00
}
2010-03-26 01:20:59 +02:00
}
2022-07-10 10:23:45 +03:00
static void help(const char *progname)
__attribute__((noreturn));
2010-03-26 01:20:59 +02:00
static void help(const char *progname)
{
printf("Starts and stops UPS drivers via ups.conf.\n\n");
printf("usage: %s [OPTIONS] (start | stop | shutdown) [<ups>]\n\n", progname);
printf(" -h display this help\n");
printf(" -r <path> drivers will chroot to <path>\n");
printf(" -t testing mode - prints actions without doing them\n");
printf(" -u <user> drivers started will switch from root to <user>\n");
printf(" -D raise debugging level\n");
2022-07-10 10:23:45 +03:00
printf(" -d pass debugging level from upsdrvctl to driver\n");
2010-03-26 01:20:59 +02:00
printf(" start start all UPS drivers in ups.conf\n");
printf(" start <ups> only start driver for UPS <ups>\n");
printf(" stop stop all UPS drivers in ups.conf\n");
printf(" stop <ups> only stop driver for UPS <ups>\n");
printf(" shutdown shutdown all UPS drivers in ups.conf\n");
printf(" shutdown <ups> only shutdown UPS <ups>\n");
exit(EXIT_SUCCESS);
}
static void shutdown_driver(const ups_t *ups)
{
2011-01-26 11:35:08 +02:00
char *argv[9];
char dfn[SMALLBUF];
2010-03-26 01:20:59 +02:00
int arg = 0;
upsdebugx(1, "Shutdown UPS: %s", ups->upsname);
snprintf(dfn, sizeof(dfn), "%s/%s", driverpath, ups->driver);
argv[arg++] = dfn;
2011-01-26 11:35:08 +02:00
argv[arg++] = (char *)"-a"; /* FIXME: cast away const */
2010-03-26 01:20:59 +02:00
argv[arg++] = ups->upsname;
2011-01-26 11:35:08 +02:00
argv[arg++] = (char *)"-k"; /* FIXME: cast away const */
2010-03-26 01:20:59 +02:00
/* stick on the chroot / user args if given to us */
if (pt_root) {
2011-01-26 11:35:08 +02:00
argv[arg++] = (char *)"-r"; /* FIXME: cast away const */
2010-03-26 01:20:59 +02:00
argv[arg++] = pt_root;
}
if (pt_user) {
2011-01-26 11:35:08 +02:00
argv[arg++] = (char *)"-u"; /* FIXME: cast away const */
2010-03-26 01:20:59 +02:00
argv[arg++] = pt_user;
}
argv[arg++] = NULL;
2011-01-26 11:35:08 +02:00
debugcmdline(2, "exec: ", argv);
2010-03-26 01:20:59 +02:00
2011-01-26 11:35:08 +02:00
if (!testmode) {
forkexec(argv, ups);
}
2010-03-26 01:20:59 +02:00
}
static void send_one_driver(void (*command)(const ups_t *), const char *upsname)
{
ups_t *ups = upstable;
if (!ups)
fatalx(EXIT_FAILURE, "Error: no UPS definitions found in ups.conf!\n");
while (ups) {
if (!strcmp(ups->upsname, upsname)) {
command(ups);
return;
}
ups = ups->next;
}
fatalx(EXIT_FAILURE, "UPS %s not found in ups.conf", upsname);
}
/* walk UPS table and send command to all UPSes according to sdorder */
static void send_all_drivers(void (*command)(const ups_t *))
{
ups_t *ups;
int i;
if (!upstable)
fatalx(EXIT_FAILURE, "Error: no UPS definitions found in ups.conf");
if (command != &shutdown_driver) {
ups = upstable;
while (ups) {
command(ups);
ups = ups->next;
}
return;
}
for (i = 0; i <= maxsdorder; i++) {
ups = upstable;
while (ups) {
if (ups->sdorder == i)
command(ups);
2022-07-10 10:23:45 +03:00
2010-03-26 01:20:59 +02:00
ups = ups->next;
}
}
}
static void exit_cleanup(void)
{
ups_t *tmp, *next;
tmp = upstable;
while (tmp) {
next = tmp->next;
free(tmp->driver);
free(tmp->port);
free(tmp->upsname);
free(tmp);
tmp = next;
}
free(driverpath);
}
int main(int argc, char **argv)
{
int i;
char *prog;
void (*command)(const ups_t *) = NULL;
printf("Network UPS Tools - UPS driver controller %s\n",
UPS_VERSION);
prog = argv[0];
2022-07-10 10:23:45 +03:00
while ((i = getopt(argc, argv, "+htu:r:DdV")) != -1) {
2010-03-26 01:20:59 +02:00
switch(i) {
case 'r':
pt_root = optarg;
break;
case 't':
testmode = 1;
break;
case 'u':
pt_user = optarg;
break;
case 'V':
exit(EXIT_SUCCESS);
case 'D':
nut_debug_level++;
break;
2022-07-10 10:23:45 +03:00
case 'd':
nut_debug_level_passthrough = 1;
break;
2010-03-26 01:20:59 +02:00
case 'h':
default:
help(prog);
}
}
argc -= optind;
argv += optind;
if (argc < 1)
help(prog);
if (testmode) {
printf("*** Testing mode: not calling exec/kill\n");
if (nut_debug_level < 2)
nut_debug_level = 2;
}
2022-07-10 10:23:45 +03:00
if (nut_debug_level_passthrough == 0) {
upsdebugx(2, "\n"
"If you're not a NUT core developer, chances are that you're told to enable debugging\n"
"to see why a driver isn't working for you. We're sorry for the confusion, but this is\n"
"the 'upsdrvctl' wrapper, not the driver you're interested in.\n\n"
"Below you'll find one or more lines starting with 'exec:' followed by an absolute\n"
"path to the driver binary and some command line option. This is what the driver\n"
"starts and you need to copy and paste that line and append the debug flags to that\n"
"line (less the 'exec:' prefix).\n\n"
"Alternately, provide an additional '-d' (lower-case) parameter to 'upsdrvctl' to\n"
"pass its current debug level to the launched driver.\n");
}
2010-03-26 01:20:59 +02:00
if (!strcmp(argv[0], "start"))
command = &start_driver;
if (!strcmp(argv[0], "stop"))
command = &stop_driver;
if (!strcmp(argv[0], "shutdown"))
command = &shutdown_driver;
if (!command)
fatalx(EXIT_FAILURE, "Error: unrecognized command [%s]", argv[0]);
driverpath = xstrdup(DRVPATH); /* set default */
atexit(exit_cleanup);
read_upsconf();
if (argc == 1)
send_all_drivers(command);
else
send_one_driver(command, argv[1]);
if (exec_error)
exit(EXIT_FAILURE);
exit(EXIT_SUCCESS);
}