2010-03-25 23:20:59 +00:00
|
|
|
/* sstate.c - Network UPS Tools server-side state management
|
|
|
|
|
|
|
|
Copyright (C)
|
|
|
|
2003 Russell Kroll <rkroll@exploits.org>
|
|
|
|
2008 Arjen de Korte <adkorte-guest@alioth.debian.org>
|
2012-06-01 13:55:19 +00:00
|
|
|
2012 Arnaud Quette <arnaud.quette@free.fr>
|
2010-03-25 23:20:59 +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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*/
|
|
|
|
|
2011-01-26 09:35:08 +00:00
|
|
|
#include "common.h"
|
|
|
|
|
|
|
|
#include "timehead.h"
|
|
|
|
|
|
|
|
#include "sstate.h"
|
2022-06-29 10:37:36 +00:00
|
|
|
#include "upsd.h"
|
2011-01-26 09:35:08 +00:00
|
|
|
#include "upstype.h"
|
2022-06-29 10:37:36 +00:00
|
|
|
#include "nut_stdint.h"
|
2011-01-26 09:35:08 +00:00
|
|
|
|
2010-03-25 23:20:59 +00:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/socket.h>
|
2022-06-29 10:37:36 +00:00
|
|
|
#include <sys/un.h>
|
2010-03-25 23:20:59 +00:00
|
|
|
|
2022-06-29 10:37:36 +00:00
|
|
|
static int parse_args(upstype_t *ups, size_t numargs, char **arg)
|
2010-03-25 23:20:59 +00:00
|
|
|
{
|
|
|
|
if (numargs < 1)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (!strcasecmp(arg[0], "PONG")) {
|
|
|
|
upsdebugx(3, "Got PONG from UPS [%s]", ups->name);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!strcasecmp(arg[0], "DUMPDONE")) {
|
|
|
|
upsdebugx(3, "UPS [%s]: dump is done", ups->name);
|
|
|
|
ups->dumpdone = 1;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!strcasecmp(arg[0], "DATASTALE")) {
|
|
|
|
ups->data_ok = 0;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!strcasecmp(arg[0], "DATAOK")) {
|
|
|
|
ups->data_ok = 1;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (numargs < 2)
|
|
|
|
return 0;
|
|
|
|
|
2012-06-01 13:55:19 +00:00
|
|
|
/* FIXME: all these should return their state_...() value! */
|
2010-03-25 23:20:59 +00:00
|
|
|
/* ADDCMD <cmdname> */
|
|
|
|
if (!strcasecmp(arg[0], "ADDCMD")) {
|
|
|
|
state_addcmd(&ups->cmdlist, arg[1]);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* DELCMD <cmdname> */
|
|
|
|
if (!strcasecmp(arg[0], "DELCMD")) {
|
|
|
|
state_delcmd(&ups->cmdlist, arg[1]);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* DELINFO <var> */
|
|
|
|
if (!strcasecmp(arg[0], "DELINFO")) {
|
|
|
|
state_delinfo(&ups->inforoot, arg[1]);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (numargs < 3)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* SETFLAGS <varname> <flags>... */
|
|
|
|
if (!strcasecmp(arg[0], "SETFLAGS")) {
|
|
|
|
state_setflags(ups->inforoot, arg[1], numargs - 2, &arg[2]);
|
|
|
|
return 1;
|
2012-06-01 13:55:19 +00:00
|
|
|
}
|
2010-03-25 23:20:59 +00:00
|
|
|
|
|
|
|
/* SETINFO <varname> <value> */
|
|
|
|
if (!strcasecmp(arg[0], "SETINFO")) {
|
|
|
|
state_setinfo(&ups->inforoot, arg[1], arg[2]);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ADDENUM <varname> <enumval> */
|
|
|
|
if (!strcasecmp(arg[0], "ADDENUM")) {
|
|
|
|
state_addenum(ups->inforoot, arg[1], arg[2]);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* DELENUM <varname> <enumval> */
|
|
|
|
if (!strcasecmp(arg[0], "DELENUM")) {
|
|
|
|
state_delenum(ups->inforoot, arg[1], arg[2]);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* SETAUX <varname> <auxval> */
|
|
|
|
if (!strcasecmp(arg[0], "SETAUX")) {
|
|
|
|
state_setaux(ups->inforoot, arg[1], arg[2]);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2022-06-29 10:37:36 +00:00
|
|
|
/* TRACKING <id> <status> */
|
|
|
|
if (!strcasecmp(arg[0], "TRACKING")) {
|
|
|
|
tracking_set(arg[1], arg[2]);
|
|
|
|
upsdebugx(1, "TRACKING: ID %s status %s", arg[1], arg[2]);
|
|
|
|
|
|
|
|
/* log actual result of instcmd / setvar */
|
|
|
|
if (strncmp(arg[2], "PENDING", 7) != 0) {
|
|
|
|
upslogx(LOG_INFO, "tracking ID: %s\tresult: %s", arg[1], tracking_get(arg[1]));
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (numargs < 4)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* ADDRANGE <varname> <minvalue> <maxvalue> */
|
|
|
|
if (!strcasecmp(arg[0], "ADDRANGE")) {
|
|
|
|
state_addrange(ups->inforoot, arg[1], atoi(arg[2]), atoi(arg[3]));
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* DELRANGE <varname> <minvalue> <maxvalue> */
|
|
|
|
if (!strcasecmp(arg[0], "DELRANGE")) {
|
|
|
|
state_delrange(ups->inforoot, arg[1], atoi(arg[2]), atoi(arg[3]));
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2010-03-25 23:20:59 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* nothing fancy - just make the driver say something back to us */
|
|
|
|
static void sendping(upstype_t *ups)
|
|
|
|
{
|
2022-06-29 10:37:36 +00:00
|
|
|
ssize_t ret;
|
2010-03-25 23:20:59 +00:00
|
|
|
const char *cmd = "PING\n";
|
2022-06-29 10:37:36 +00:00
|
|
|
size_t cmdlen = strlen(cmd);
|
2010-03-25 23:20:59 +00:00
|
|
|
|
|
|
|
if ((!ups) || (ups->sock_fd < 0)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
upsdebugx(3, "Pinging UPS [%s]", ups->name);
|
2022-06-29 10:37:36 +00:00
|
|
|
ret = write(ups->sock_fd, cmd, cmdlen);
|
2010-03-25 23:20:59 +00:00
|
|
|
|
2022-06-29 10:37:36 +00:00
|
|
|
if ((ret < 1) || (ret != (ssize_t)cmdlen)) {
|
2010-03-25 23:20:59 +00:00
|
|
|
upslog_with_errno(LOG_NOTICE, "Send ping to UPS [%s] failed", ups->name);
|
|
|
|
sstate_disconnect(ups);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
time(&ups->last_ping);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* interface */
|
|
|
|
|
|
|
|
int sstate_connect(upstype_t *ups)
|
|
|
|
{
|
2022-06-29 10:37:36 +00:00
|
|
|
int fd;
|
2010-03-25 23:20:59 +00:00
|
|
|
const char *dumpcmd = "DUMPALL\n";
|
2022-06-29 10:37:36 +00:00
|
|
|
size_t dumpcmdlen = strlen(dumpcmd);
|
|
|
|
ssize_t ret;
|
2010-03-25 23:20:59 +00:00
|
|
|
struct sockaddr_un sa;
|
|
|
|
|
2022-06-29 10:37:36 +00:00
|
|
|
check_unix_socket_filename(ups->fn);
|
|
|
|
|
2010-03-25 23:20:59 +00:00
|
|
|
memset(&sa, '\0', sizeof(sa));
|
|
|
|
sa.sun_family = AF_UNIX;
|
|
|
|
snprintf(sa.sun_path, sizeof(sa.sun_path), "%s", ups->fn);
|
|
|
|
|
|
|
|
fd = socket(AF_UNIX, SOCK_STREAM, 0);
|
|
|
|
|
|
|
|
if (fd < 0) {
|
|
|
|
upslog_with_errno(LOG_ERR, "Can't create socket for UPS [%s]", ups->name);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = connect(fd, (struct sockaddr *) &sa, sizeof(sa));
|
|
|
|
|
|
|
|
if (ret < 0) {
|
|
|
|
time_t now;
|
|
|
|
|
|
|
|
close(fd);
|
|
|
|
|
|
|
|
/* rate-limit complaints - don't spam the syslog */
|
|
|
|
time(&now);
|
|
|
|
if (difftime(now, ups->last_connfail) < SS_CONNFAIL_INT)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
ups->last_connfail = now;
|
2022-06-29 10:37:36 +00:00
|
|
|
upslog_with_errno(LOG_ERR, "Can't connect to UPS [%s] (%s)",
|
2010-03-25 23:20:59 +00:00
|
|
|
ups->name, ups->fn);
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = fcntl(fd, F_GETFL, 0);
|
|
|
|
|
|
|
|
if (ret < 0) {
|
|
|
|
upslog_with_errno(LOG_ERR, "fcntl get on UPS [%s] failed", ups->name);
|
|
|
|
close(fd);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = fcntl(fd, F_SETFL, ret | O_NDELAY);
|
|
|
|
|
|
|
|
if (ret < 0) {
|
|
|
|
upslog_with_errno(LOG_ERR, "fcntl set O_NDELAY on UPS [%s] failed", ups->name);
|
|
|
|
close(fd);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* get a dump started so we have a fresh set of data */
|
2022-06-29 10:37:36 +00:00
|
|
|
ret = write(fd, dumpcmd, dumpcmdlen);
|
2010-03-25 23:20:59 +00:00
|
|
|
|
2022-06-29 10:37:36 +00:00
|
|
|
if ((ret < 1) || (ret != (ssize_t)dumpcmdlen)) {
|
2010-03-25 23:20:59 +00:00
|
|
|
upslog_with_errno(LOG_ERR, "Initial write to UPS [%s] failed", ups->name);
|
|
|
|
close(fd);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
pconf_init(&ups->sock_ctx, NULL);
|
|
|
|
|
|
|
|
ups->dumpdone = 0;
|
|
|
|
ups->stale = 0;
|
|
|
|
|
|
|
|
/* now is the last time we heard something from the driver */
|
|
|
|
time(&ups->last_heard);
|
|
|
|
|
|
|
|
/* set ups.status to "WAIT" while waiting for the driver response to dumpcmd */
|
|
|
|
state_setinfo(&ups->inforoot, "ups.status", "WAIT");
|
|
|
|
|
|
|
|
upslogx(LOG_INFO, "Connected to UPS [%s]: %s", ups->name, ups->fn);
|
|
|
|
|
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
|
|
|
|
void sstate_disconnect(upstype_t *ups)
|
|
|
|
{
|
|
|
|
if ((!ups) || (ups->sock_fd < 0)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
sstate_infofree(ups);
|
|
|
|
sstate_cmdfree(ups);
|
|
|
|
|
|
|
|
pconf_finish(&ups->sock_ctx);
|
|
|
|
|
|
|
|
close(ups->sock_fd);
|
|
|
|
ups->sock_fd = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void sstate_readline(upstype_t *ups)
|
|
|
|
{
|
2022-06-29 10:37:36 +00:00
|
|
|
ssize_t i, ret;
|
2010-03-25 23:20:59 +00:00
|
|
|
char buf[SMALLBUF];
|
|
|
|
|
|
|
|
if ((!ups) || (ups->sock_fd < 0)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = read(ups->sock_fd, buf, sizeof(buf));
|
|
|
|
|
|
|
|
if (ret < 0) {
|
|
|
|
switch(errno)
|
|
|
|
{
|
|
|
|
case EINTR:
|
|
|
|
case EAGAIN:
|
|
|
|
return;
|
|
|
|
|
|
|
|
default:
|
|
|
|
upslog_with_errno(LOG_WARNING, "Read from UPS [%s] failed", ups->name);
|
|
|
|
sstate_disconnect(ups);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < ret; i++) {
|
|
|
|
|
|
|
|
switch (pconf_char(&ups->sock_ctx, buf[i]))
|
|
|
|
{
|
|
|
|
case 1:
|
|
|
|
/* set the 'last heard' time to now for later staleness checks */
|
|
|
|
if (parse_args(ups, ups->sock_ctx.numargs, ups->sock_ctx.arglist)) {
|
2022-06-29 10:37:36 +00:00
|
|
|
time(&ups->last_heard);
|
2010-03-25 23:20:59 +00:00
|
|
|
}
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case 0:
|
|
|
|
continue; /* haven't gotten a line yet */
|
|
|
|
|
|
|
|
default:
|
|
|
|
/* parse error */
|
|
|
|
upslogx(LOG_NOTICE, "Parse error on sock: %s", ups->sock_ctx.errmsg);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *sstate_getinfo(const upstype_t *ups, const char *var)
|
|
|
|
{
|
|
|
|
return state_getinfo(ups->inforoot, var);
|
|
|
|
}
|
|
|
|
|
|
|
|
int sstate_getflags(const upstype_t *ups, const char *var)
|
|
|
|
{
|
|
|
|
return state_getflags(ups->inforoot, var);
|
2022-06-29 10:37:36 +00:00
|
|
|
}
|
2010-03-25 23:20:59 +00:00
|
|
|
|
2022-06-29 10:37:36 +00:00
|
|
|
long sstate_getaux(const upstype_t *ups, const char *var)
|
2010-03-25 23:20:59 +00:00
|
|
|
{
|
|
|
|
return state_getaux(ups->inforoot, var);
|
2022-06-29 10:37:36 +00:00
|
|
|
}
|
2010-03-25 23:20:59 +00:00
|
|
|
|
2011-01-26 09:35:08 +00:00
|
|
|
const enum_t *sstate_getenumlist(const upstype_t *ups, const char *var)
|
2010-03-25 23:20:59 +00:00
|
|
|
{
|
|
|
|
return state_getenumlist(ups->inforoot, var);
|
|
|
|
}
|
|
|
|
|
2012-06-01 13:55:19 +00:00
|
|
|
const range_t *sstate_getrangelist(const upstype_t *ups, const char *var)
|
|
|
|
{
|
|
|
|
return state_getrangelist(ups->inforoot, var);
|
|
|
|
}
|
|
|
|
|
2011-01-26 09:35:08 +00:00
|
|
|
const cmdlist_t *sstate_getcmdlist(const upstype_t *ups)
|
2010-03-25 23:20:59 +00:00
|
|
|
{
|
|
|
|
return ups->cmdlist;
|
|
|
|
}
|
|
|
|
|
2022-06-29 10:37:36 +00:00
|
|
|
int sstate_dead(upstype_t *ups, int arg_maxage)
|
2010-03-25 23:20:59 +00:00
|
|
|
{
|
|
|
|
time_t now;
|
|
|
|
double elapsed;
|
|
|
|
|
|
|
|
/* an unconnected ups is always dead */
|
|
|
|
if (ups->sock_fd < 0) {
|
|
|
|
upsdebugx(3, "sstate_dead: connection to driver socket for UPS [%s] lost", ups->name);
|
|
|
|
return 1; /* dead */
|
|
|
|
}
|
|
|
|
|
|
|
|
time(&now);
|
|
|
|
|
|
|
|
/* ignore DATAOK/DATASTALE unless the dump is done */
|
|
|
|
if ((ups->dumpdone) && (!ups->data_ok)) {
|
|
|
|
upsdebugx(3, "sstate_dead: driver for UPS [%s] says data is stale", ups->name);
|
|
|
|
return 1; /* dead */
|
|
|
|
}
|
|
|
|
|
|
|
|
elapsed = difftime(now, ups->last_heard);
|
|
|
|
|
|
|
|
/* somewhere beyond a third of the maximum time - prod it to make it talk */
|
2022-06-29 10:37:36 +00:00
|
|
|
if ((elapsed > (arg_maxage / 3)) && (difftime(now, ups->last_ping) > (arg_maxage / 3)))
|
2010-03-25 23:20:59 +00:00
|
|
|
sendping(ups);
|
|
|
|
|
2022-06-29 10:37:36 +00:00
|
|
|
if (elapsed > arg_maxage) {
|
2010-03-25 23:20:59 +00:00
|
|
|
upsdebugx(3, "sstate_dead: didn't hear from driver for UPS [%s] for %g seconds (max %d)",
|
2022-06-29 10:37:36 +00:00
|
|
|
ups->name, elapsed, arg_maxage);
|
2010-03-25 23:20:59 +00:00
|
|
|
return 1; /* dead */
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* release all info(tree) data used by <ups> */
|
|
|
|
void sstate_infofree(upstype_t *ups)
|
|
|
|
{
|
|
|
|
state_infofree(ups->inforoot);
|
|
|
|
|
|
|
|
ups->inforoot = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void sstate_cmdfree(upstype_t *ups)
|
|
|
|
{
|
|
|
|
state_cmdfree(ups->cmdlist);
|
|
|
|
|
|
|
|
ups->cmdlist = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
int sstate_sendline(upstype_t *ups, const char *buf)
|
|
|
|
{
|
2022-06-29 10:37:36 +00:00
|
|
|
ssize_t ret;
|
|
|
|
size_t buflen;
|
2010-03-25 23:20:59 +00:00
|
|
|
|
|
|
|
if ((!ups) ||(ups->sock_fd < 0)) {
|
|
|
|
return 0; /* failed */
|
|
|
|
}
|
|
|
|
|
2022-06-29 10:37:36 +00:00
|
|
|
buflen = strlen(buf);
|
|
|
|
if (buflen >= SSIZE_MAX) {
|
|
|
|
/* Can't compare buflen to ret... */
|
|
|
|
upslog_with_errno(LOG_NOTICE, "Send ping to UPS [%s] failed: buffered message too large", ups->name);
|
|
|
|
return 0; /* failed */
|
|
|
|
}
|
2010-03-25 23:20:59 +00:00
|
|
|
|
2022-06-29 10:37:36 +00:00
|
|
|
ret = write(ups->sock_fd, buf, buflen);
|
|
|
|
|
|
|
|
if (ret == (ssize_t)buflen) {
|
|
|
|
return 1;
|
2010-03-25 23:20:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
upslog_with_errno(LOG_NOTICE, "Send to UPS [%s] failed", ups->name);
|
|
|
|
sstate_disconnect(ups);
|
|
|
|
|
|
|
|
return 0; /* failed */
|
|
|
|
}
|
|
|
|
|
2011-01-26 09:35:08 +00:00
|
|
|
const st_tree_t *sstate_getnode(const upstype_t *ups, const char *varname)
|
2010-03-25 23:20:59 +00:00
|
|
|
{
|
|
|
|
return state_tree_find(ups->inforoot, varname);
|
|
|
|
}
|