29
29
extern char *statepath, *datapath, *certfile;
30
30
extern upstype *firstups;
31
31
ups_t *upstable = NULL;
34
/* add another UPS for monitoring from ups.conf */
35
static void ups_create(const char *fn, const char *name, const char *desc)
39
temp = last = firstups;
41
/* find end of linked list */
42
while (temp != NULL) {
45
if (!strcasecmp(temp->name, name)) {
46
upslogx(LOG_ERR, "UPS name [%s] is already in use!",
54
/* grab some memory and add the info */
55
temp = xmalloc(sizeof(upstype));
57
temp->name = xstrdup(name);
58
temp->fn = xstrdup(fn);
61
temp->desc = xstrdup(desc);
75
/* preload this to the current time to avoid false staleness */
76
time(&temp->last_heard);
79
temp->last_connfail = 0;
80
temp->inforoot = NULL;
88
temp->sock_fd = sstate_connect(temp);
93
/* change the configuration of an existing UPS (used during reloads) */
94
static void ups_update(const char *fn, const char *name, const char *desc)
98
temp = get_ups_ptr(name);
101
upslogx(LOG_ERR, "UPS %s disappeared during reload", name);
107
upslogx(LOG_ERR, "UPS %s had a NULL filename!", name);
109
/* let's give it something quick to use later */
110
temp->fn = xstrdup("");
113
/* when the filename changes, force a reconnect */
114
if (strcmp(temp->fn, fn) != 0) {
116
upslogx(LOG_NOTICE, "Redefined UPS [%s]", name);
118
/* release all data */
119
sstate_infofree(temp);
120
sstate_cmdfree(temp);
121
pconf_finish(&temp->sock_ctx);
123
close(temp->sock_fd);
127
/* now redefine the filename and wrap up */
129
temp->fn = xstrdup(fn);
132
/* update the description */
138
temp->desc = xstrdup(desc);
142
/* always set this on reload */
34
146
/* return 1 if usable, 0 if not */
35
147
static int parse_upsd_conf_args(int numargs, char **arg)
186
/* ACCEPT <aclname> [<aclname>...] */
187
if (!strcmp(arg[0], "ACCEPT")) {
188
access_add(ACCESS_ACCEPT, numargs - 1,
189
(const char **) &arg[1]);
193
/* REJECT <aclname> [<aclname>...] */
194
if (!strcmp(arg[0], "REJECT")) {
195
access_add(ACCESS_REJECT, numargs - 1,
196
(const char **) &arg[1]);
74
200
/* everything below here uses up through arg[2] */
87
/* ACCESS <action> <level> <aclname> */
88
/* any password on the end is now ignored - no more host-based auth */
89
213
if (!strcmp(arg[0], "ACCESS")) {
90
access_add(arg[1], arg[2], arg[3]);
214
upslogx(LOG_WARNING, "ACCESS in upsd.conf is no longer supported - switch to ACCEPT/REJECT");
98
222
/* called for fatal errors in parseconf like malloc failures */
99
void upsd_conf_err(const char *errmsg)
223
static void upsd_conf_err(const char *errmsg)
101
225
upslogx(LOG_ERR, "Fatal error in parseconf (upsd.conf): %s", errmsg);
104
void read_upsdconf(int reload)
228
static void load_upsdconf(int reloading)
106
230
char fn[SMALLBUF];
113
reload_flag = reload;
115
237
pconf_init(&ctx, upsd_conf_err);
117
239
if (!pconf_file_begin(&ctx, fn)) {
118
240
pconf_finish(&ctx);
121
243
fatalx("%s", ctx.errmsg);
123
upslogx(LOG_ERR, "Reload failed: %s", ctx.errmsg);
245
upslogx(LOG_ERR, "Reload failed: %s", ctx.errmsg);
128
249
while (pconf_file_next(&ctx)) {
146
267
snprintfcat(errmsg, sizeof(errmsg), " %s",
149
upslogx(LOG_WARNING, errmsg);
270
upslogx(LOG_WARNING, "%s", errmsg);
204
325
/* add valid UPSes from ups.conf to the internal structures */
205
void upsconf_add(int reloading)
326
static void upsconf_add(int reloading)
207
328
ups_t *tmp = upstable, *next;
208
329
char statefn[SMALLBUF];
210
/* nonfatal for now - eventually this will be required */
212
332
upslogx(LOG_WARNING, "Warning: no UPS definitions in ups.conf");
229
349
upslogx(LOG_WARNING, "Warning: ignoring incomplete configuration for UPS [%s]\n",
232
snprintf(statefn, sizeof(statefn), "%s-%s", tmp->driver, xbasename(tmp->port));
352
snprintf(statefn, sizeof(statefn), "%s-%s",
353
tmp->driver, xbasename(tmp->port));
234
355
/* if a UPS exists, update it, else add it as new */
235
if ((reloading) && (findups(tmp->upsname) != NULL))
236
redefine_ups(statefn, tmp->upsname, tmp->desc);
356
if ((reloading) && (get_ups_ptr(tmp->upsname) != NULL))
357
ups_update(statefn, tmp->upsname, tmp->desc);
238
addups(statefn, tmp->upsname, tmp->desc);
359
ups_create(statefn, tmp->upsname, tmp->desc);
241
362
/* free tmp's resources */