~ubuntu-branches/ubuntu/saucy/nut/saucy

« back to all changes in this revision

Viewing changes to server/sstate.c

  • Committer: Bazaar Package Importer
  • Author(s): Reinhard Tartler
  • Date: 2005-07-20 19:48:50 UTC
  • mto: (16.1.1 squeeze)
  • mto: This revision was merged to the branch mainline in revision 4.
  • Revision ID: james.westby@ubuntu.com-20050720194850-oo61wjr33rrx2mre
Tags: upstream-2.0.2
ImportĀ upstreamĀ versionĀ 2.0.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
33
33
#include "sstate.h"
34
34
#include "state.h"
35
35
 
36
 
        /* until old-style vars are gone */
37
 
#include "shared-tables.h"
38
 
#include "var-map.h"
39
 
#include "cmd-map.h"
40
 
 
41
 
        /* upsd.c pulls in the initializers, so just reference those */
42
 
        extern  struct  netvars_t       netvars[];
43
 
        extern  struct  instcmds_t      instcmds[];
44
 
        extern  struct  var_map_t       var_map[];
45
 
        extern  struct  cmd_map_t       cmd_map[];
46
 
 
47
 
static const char *newcmd_to_old(const char *new)
48
 
{
49
 
        int     i;
50
 
 
51
 
        for (i = 0; cmd_map[i].name != NULL; i++)
52
 
                if (!strcasecmp(cmd_map[i].name, new))
53
 
                        return cmd_map[i].old;
54
 
 
55
 
        return NULL;
56
 
}
57
 
 
58
 
/* convert temporary hex numerics to the old-style cmdnames */
59
 
static void append_oldcmdname(char *buf, size_t bufsize, const char *cmdname)
60
 
{
61
 
        int     i, cmd;
62
 
 
63
 
        /* only handle old-style stuff (no periods, and 4 chars long) */
64
 
        if ((strchr(cmdname, '.')) || (strlen(cmdname) != 4)) {
65
 
                const   char    *old;
66
 
 
67
 
                old = newcmd_to_old(cmdname);
68
 
 
69
 
                if (!old) {
70
 
                        upsdebugx(2, "Unhandled new command name [%s]",
71
 
                                cmdname);
72
 
                        return;
73
 
                }
74
 
 
75
 
                snprintfcat(buf, bufsize, " %s", old);
76
 
                return;
77
 
        }
78
 
 
79
 
        cmd = strtol(cmdname, (char **) NULL, 16);
80
 
 
81
 
        if (cmd < 0)
82
 
                return;
83
 
 
84
 
        /* yeah, it's ugly, but it's going away, so ignore it for now */
85
 
        for (i = 0; instcmds[i].name != NULL; i++) {
86
 
                if (instcmds[i].cmd == cmd) {
87
 
                        snprintfcat(buf, bufsize, " %s", instcmds[i].name);
88
 
                        return;
89
 
                }
90
 
        }
91
 
 
92
 
        /* odd ... */
93
 
 
94
 
        upslogx(LOG_NOTICE, "Failed to find %04X in instcmds (%d)\n", cmd, i);
95
 
}
96
 
 
97
 
static const char *newvar_to_old(const char *new)
98
 
{
99
 
        int     i;
100
 
 
101
 
        for (i = 0; var_map[i].name != NULL; i++)
102
 
                if (!strcasecmp(var_map[i].name, new))
103
 
                        return var_map[i].old;
104
 
 
105
 
        return NULL;
106
 
}
107
 
 
108
 
/* convert temporary hex numerics to the old-style varnames */
109
 
static void append_oldinfoname(char *buf, size_t bufsize, const char *varname)
110
 
{
111
 
        int     i, var;
112
 
 
113
 
        /* only handle old-style stuff (no periods, and 4 chars long) */
114
 
        if ((strchr(varname, '.')) || (strlen(varname) != 4)) {
115
 
                const   char    *old;
116
 
 
117
 
                old = newvar_to_old(varname);
118
 
 
119
 
                if (!old) {
120
 
                        upsdebugx(2, "Unhandled new variable name [%s]",
121
 
                                varname);
122
 
                        return;
123
 
                }
124
 
 
125
 
                snprintfcat(buf, bufsize, " %s", old);
126
 
                return;
127
 
        }
128
 
 
129
 
        var = strtol(varname, (char **) NULL, 16);
130
 
 
131
 
        if (var < 0)
132
 
                return;
133
 
 
134
 
        /* yeah, it's ugly, but it's going away, so ignore it for now */
135
 
        for (i = 0; netvars[i].name != NULL; i++) {
136
 
                if (netvars[i].type == var) {
137
 
                        snprintfcat(buf, bufsize, " %s", netvars[i].name);
138
 
                        return;
139
 
                }
140
 
        }
141
 
 
142
 
        /* odd ... */
143
 
 
144
 
        upslogx(LOG_NOTICE, "Failed to find %04X in netvars (%d)\n", var, i);
145
 
}
146
 
 
147
 
/* walk the tree and build an old-style variable list */
148
 
static void geninfolist(struct st_tree_t *node, char *buf, size_t bufsize, 
149
 
        int rw)
150
 
{
151
 
        if (!node)
152
 
                return;
153
 
 
154
 
        if (node->left)
155
 
                geninfolist(node->left, buf, bufsize, rw);
156
 
 
157
 
        if (rw) {
158
 
 
159
 
                /* only list rw entries */
160
 
                if (node->flags & ST_FLAG_RW)
161
 
                        append_oldinfoname(buf, bufsize, node->var);
162
 
        } else {
163
 
 
164
 
                /* list all entries */
165
 
                append_oldinfoname(buf, bufsize, node->var);
166
 
        }
167
 
 
168
 
        if (node->right)
169
 
                geninfolist(node->right, buf, bufsize, rw);
170
 
}
171
 
 
172
36
/* set the 'last heard' time to now for later staleness checks */
173
37
static void update_time(upstype *ups)
174
38
{
262
126
static void sendping(upstype *ups, time_t now, int maxage)
263
127
{
264
128
        int     ret;
265
 
        char    *cmd = "PING\n";
 
129
        const   char    *cmd = "PING\n";
266
130
 
267
131
        /* don't beat the driver to death with pings */
268
132
        if (difftime(now, ups->last_ping) < (maxage / 2))
272
136
 
273
137
        ret = write(ups->sock_fd, cmd, strlen(cmd));
274
138
 
275
 
        if (ret != strlen(cmd)) {
 
139
        if ((ret < 1) || (ret != (int) strlen(cmd))) {
276
140
                upslog(LOG_NOTICE, "Send ping to UPS [%s] failed", ups->name);
277
141
 
278
 
                sstate_infofree(ups);
279
 
                sstate_cmdfree(ups);
280
 
                pconf_finish(&ups->sock_ctx);
 
142
                sstate_infofree(ups);
 
143
                sstate_cmdfree(ups);
 
144
                pconf_finish(&ups->sock_ctx);
281
145
 
282
146
                close(ups->sock_fd);
283
147
                ups->sock_fd = -1;
293
157
int sstate_connect(upstype *ups)
294
158
{
295
159
        int     ret, fd;
296
 
        char    *dumpcmd = "DUMPALL\n";
 
160
        const   char    *dumpcmd = "DUMPALL\n";
297
161
        struct  sockaddr_un sa;
298
162
 
299
163
        memset(&sa, '\0', sizeof(sa));
345
209
        /* get a dump started so we have a fresh set of data */
346
210
        ret = write(fd, dumpcmd, strlen(dumpcmd));
347
211
 
348
 
        if (ret != strlen(dumpcmd)) {
 
212
        if ((ret < 1) || (ret != (int) strlen(dumpcmd))) {
349
213
                close(fd);
350
214
                upslog(LOG_ERR, "Initial write to UPS [%s] failed", ups->name);
351
215
                return -1;
377
241
                        if ((ret == -1) && (errno == EAGAIN))
378
242
                                return;
379
243
 
380
 
                        upslog(LOG_NOTICE, "Read from UPS [%s] failed", 
381
 
                                ups->name);
 
244
                        if (ret == 0)
 
245
                                upslogx(LOG_WARNING, "UPS [%s] disconnected - "
 
246
                                        "check driver", ups->name);
 
247
                        else
 
248
                                upslog(LOG_WARNING, "Read from UPS [%s] failed",
 
249
                                        ups->name);
382
250
 
383
 
                        sstate_infofree(ups);
384
 
                        sstate_cmdfree(ups);
385
 
                        pconf_finish(&ups->sock_ctx);
 
251
                        sstate_infofree(ups);
 
252
                        sstate_cmdfree(ups);
 
253
                        pconf_finish(&ups->sock_ctx);
386
254
 
387
255
                        close(ups->sock_fd);
388
256
                        ups->sock_fd = -1;
411
279
        }
412
280
}
413
281
 
414
 
/* strictly a name-based transform - no netcmds numerics */
415
 
static const char *oldvar_to_new_noconv(const char *old)
416
 
{
417
 
        int     i;
418
 
 
419
 
        for (i = 0; var_map[i].name != NULL; i++)
420
 
                if (!strcasecmp(var_map[i].old, old))
421
 
                        return var_map[i].name;
422
 
 
423
 
        return NULL;
424
 
}
425
 
 
426
 
const char *sstate_getinfo(const upstype *ups, const char *var, int tryold)
427
 
{
428
 
        const   char    *new;
429
 
 
 
282
const char *sstate_getinfo(const upstype *ups, const char *var)
 
283
{
430
284
        /* requesting an old variable name? */
431
 
        if (!strchr(var, '.')) {
432
 
 
433
 
                if (!tryold) 
434
 
                        return NULL;
435
 
 
436
 
                new = oldvar_to_new_noconv(var);
437
 
 
438
 
                /* if it's been mapped, look up the new name */
439
 
                if (new)
440
 
                        return state_getinfo(ups->inforoot, new);
441
 
 
442
 
                /* otherwise fall through and try it directly */
443
 
        }
 
285
        if (!strchr(var, '.'))
 
286
                return NULL;
444
287
 
445
288
        return state_getinfo(ups->inforoot, var);
446
289
}
465
308
        return ups->cmdlist;
466
309
}
467
310
 
468
 
void sstate_makeinfolist(const upstype *ups, char *buf, size_t bufsize)
469
 
{
470
 
        geninfolist(ups->inforoot, buf, bufsize, 0);
471
 
}
472
 
 
473
 
void sstate_makerwlist(const upstype *ups, char *buf, size_t bufsize)
474
 
{
475
 
        geninfolist(ups->inforoot, buf, bufsize, 1);
476
 
}
477
 
 
478
 
void sstate_makeinstcmdlist(const upstype *ups, char *buf, size_t bufsize)
479
 
{
480
 
        struct  cmdlist_t       *tmp;
481
 
 
482
 
        tmp = ups->cmdlist;
483
 
 
484
 
        while (tmp) {
485
 
                append_oldcmdname(buf, bufsize, tmp->name);
486
 
                tmp = tmp->next;
487
 
        }
488
 
}
489
 
 
490
311
int sstate_dead(upstype *ups, int maxage)
491
312
{
492
313
        time_t  now;
550
371
 
551
372
        ret = write(ups->sock_fd, buf, strlen(buf));
552
373
 
553
 
        if (ret != strlen(buf)) {
 
374
        if ((ret < 1) || (ret != (int) strlen(buf))) {
554
375
                upslog(LOG_NOTICE, "Send to UPS [%s] failed", ups->name);
555
376
 
556
 
                sstate_infofree(ups);
557
 
                sstate_cmdfree(ups);
558
 
                pconf_finish(&ups->sock_ctx);
 
377
                sstate_infofree(ups);
 
378
                sstate_cmdfree(ups);
 
379
                pconf_finish(&ups->sock_ctx);
559
380
 
560
381
                close(ups->sock_fd);
561
382
                ups->sock_fd = -1;