1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382
|
/* sstate.c - Network UPS Tools server-side state management
Copyright (C) 2003 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
*/
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include "common.h"
#include "timehead.h"
#include "upstype.h"
#include "sstate.h"
#include "state.h"
static int parse_args(upstype_t *ups, int numargs, char **arg)
{
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;
/* 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;
}
/* 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;
}
return 0;
}
/* nothing fancy - just make the driver say something back to us */
static void sendping(upstype_t *ups)
{
int ret;
const char *cmd = "PING\n";
upsdebugx(3, "Pinging UPS [%s]", ups->name);
ret = write(ups->sock_fd, cmd, strlen(cmd));
if ((ret < 1) || (ret != (int) strlen(cmd))) {
upslog_with_errno(LOG_NOTICE, "Send ping to UPS [%s] failed", ups->name);
sstate_infofree(ups);
sstate_cmdfree(ups);
pconf_finish(&ups->sock_ctx);
close(ups->sock_fd);
ups->sock_fd = -1;
ups->dumpdone = 0;
return;
}
time(&ups->last_ping);
}
/* interface */
int sstate_connect(upstype_t *ups)
{
int ret, fd;
const char *dumpcmd = "DUMPALL\n";
struct sockaddr_un sa;
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;
upslog_with_errno(LOG_ERR, "Can't connect to UPS [%s] (%s)",
ups->name, ups->fn);
return -1;
}
ret = fcntl(fd, F_GETFL, 0);
if (ret < 0) {
close(fd);
upslog_with_errno(LOG_ERR, "fcntl get on UPS [%s] failed", ups->name);
return -1;
}
ret = fcntl(fd, F_SETFL, ret | O_NDELAY);
if (ret < 0) {
close(fd);
upslog_with_errno(LOG_ERR, "fcntl set O_NDELAY on UPS [%s] failed", ups->name);
return -1;
}
/* get a dump started so we have a fresh set of data */
ret = write(fd, dumpcmd, strlen(dumpcmd));
if ((ret < 1) || (ret != (int) strlen(dumpcmd))) {
close(fd);
upslog_with_errno(LOG_ERR, "Initial write to UPS [%s] failed", ups->name);
return -1;
}
/* clear out any old junk from before */
sstate_infofree(ups);
sstate_cmdfree(ups);
pconf_init(&ups->sock_ctx, NULL);
ups->stale = 0;
/* 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_sock_read(upstype_t *ups)
{
int i, ret;
char ch;
for (i = 0; i < SS_MAX_READ; i++) {
ret = read(ups->sock_fd, &ch, 1);
if (ret < 1) {
/* ran out of data pre-parse */
if ((ret == -1) && (errno == EAGAIN))
return;
if (ret == 0)
upslogx(LOG_WARNING, "UPS [%s] disconnected - "
"check driver", ups->name);
else
upslog_with_errno(LOG_WARNING, "Read from UPS [%s] failed",
ups->name);
sstate_infofree(ups);
sstate_cmdfree(ups);
pconf_finish(&ups->sock_ctx);
close(ups->sock_fd);
ups->sock_fd = -1;
ups->dumpdone = 0;
return;
}
ret = pconf_char(&ups->sock_ctx, ch);
if (ret == 0)
continue; /* haven't gotten a line yet */
if (ret == 1) { /* got one - parse it */
/* set the 'last heard' time to now for later staleness checks */
if (parse_args(ups, ups->sock_ctx.numargs,
ups->sock_ctx.arglist))
time(&ups->last_heard);
/* only one command per pass */
return;
}
/* parse error */
upslogx(LOG_NOTICE, "Parse error on sock: %s",
ups->sock_ctx.errmsg);
}
}
const char *sstate_getinfo(const upstype_t *ups, const char *var)
{
/* requesting an old variable name? */
if (!strchr(var, '.'))
return NULL;
return state_getinfo(ups->inforoot, var);
}
int sstate_getflags(const upstype_t *ups, const char *var)
{
return state_getflags(ups->inforoot, var);
}
int sstate_getaux(const upstype_t *ups, const char *var)
{
return state_getaux(ups->inforoot, var);
}
const struct enum_t *sstate_getenumlist(const upstype_t *ups, const char *var)
{
return state_getenumlist(ups->inforoot, var);
}
const struct cmdlist_t *sstate_getcmdlist(const upstype_t *ups)
{
return ups->cmdlist;
}
int sstate_dead(upstype_t *ups, int maxage)
{
time_t now;
double elapsed;
/* an unconnected ups is always dead */
if (ups->sock_fd == -1) {
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 */
if ((elapsed > (maxage / 3)) && (difftime(now, ups->last_ping) > (maxage / 3)))
sendping(ups);
if (elapsed > maxage) {
upsdebugx(3, "sstate_dead: didn't hear from driver for UPS [%s] for %g seconds (max %d)",
ups->name, elapsed, maxage);
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)
{
int ret;
if (ups->sock_fd == -1)
return 0; /* failed */
ret = write(ups->sock_fd, buf, strlen(buf));
if ((ret < 1) || (ret != (int) strlen(buf))) {
upslog_with_errno(LOG_NOTICE, "Send to UPS [%s] failed", ups->name);
sstate_infofree(ups);
sstate_cmdfree(ups);
pconf_finish(&ups->sock_ctx);
close(ups->sock_fd);
ups->sock_fd = -1;
return 0; /* failed */
}
return 1;
}
const struct st_tree_t *sstate_getnode(const upstype_t *ups, const char *varname)
{
return state_tree_find(ups->inforoot, varname);
}
|