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

« back to all changes in this revision

Viewing changes to server/conf.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:
29
29
        extern  char    *statepath, *datapath, *certfile;
30
30
        extern  upstype *firstups;
31
31
        ups_t   *upstable = NULL;
32
 
        int     reload_flag;
 
32
        int     num_ups = 0;
 
33
 
 
34
/* add another UPS for monitoring from ups.conf */
 
35
static void ups_create(const char *fn, const char *name, const char *desc)
 
36
{
 
37
        upstype *temp, *last;
 
38
 
 
39
        temp = last = firstups;
 
40
 
 
41
        /* find end of linked list */
 
42
        while (temp != NULL) {
 
43
                last = temp;
 
44
 
 
45
                if (!strcasecmp(temp->name, name)) {
 
46
                        upslogx(LOG_ERR, "UPS name [%s] is already in use!", 
 
47
                                name);
 
48
                        return;
 
49
                }
 
50
 
 
51
                temp = temp->next;
 
52
        }
 
53
 
 
54
        /* grab some memory and add the info */
 
55
        temp = xmalloc(sizeof(upstype));
 
56
 
 
57
        temp->name = xstrdup(name);
 
58
        temp->fn = xstrdup(fn);
 
59
 
 
60
        if (desc)
 
61
                temp->desc = xstrdup(desc);
 
62
        else
 
63
                temp->desc = NULL;
 
64
 
 
65
        temp->stale = 1;
 
66
 
 
67
        temp->numlogins = 0;
 
68
        temp->fsd = 0;
 
69
        temp->retain = 1;
 
70
        temp->next = NULL;
 
71
 
 
72
        temp->dumpdone = 0;
 
73
        temp->data_ok = 0;
 
74
 
 
75
        /* preload this to the current time to avoid false staleness */
 
76
        time(&temp->last_heard);
 
77
 
 
78
        temp->last_ping = 0;
 
79
        temp->last_connfail = 0;
 
80
        temp->inforoot = NULL;
 
81
        temp->cmdlist = NULL;
 
82
 
 
83
        if (last == NULL)
 
84
                firstups = temp;
 
85
        else
 
86
                last->next = temp;
 
87
 
 
88
        temp->sock_fd = sstate_connect(temp);
 
89
 
 
90
        num_ups++;
 
91
}
 
92
 
 
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)
 
95
{
 
96
        upstype *temp;
 
97
 
 
98
        temp = get_ups_ptr(name);
 
99
 
 
100
        if (!temp) {
 
101
                upslogx(LOG_ERR, "UPS %s disappeared during reload", name);
 
102
                return;
 
103
        }
 
104
 
 
105
        /* paranoia */
 
106
        if (!temp->fn) {
 
107
                upslogx(LOG_ERR, "UPS %s had a NULL filename!", name);
 
108
 
 
109
                /* let's give it something quick to use later */
 
110
                temp->fn = xstrdup("");
 
111
        }
 
112
 
 
113
        /* when the filename changes, force a reconnect */
 
114
        if (strcmp(temp->fn, fn) != 0) {
 
115
 
 
116
                upslogx(LOG_NOTICE, "Redefined UPS [%s]", name);
 
117
 
 
118
                /* release all data */
 
119
                sstate_infofree(temp);
 
120
                sstate_cmdfree(temp);
 
121
                pconf_finish(&temp->sock_ctx);
 
122
 
 
123
                close(temp->sock_fd);
 
124
                temp->sock_fd = -1;
 
125
                temp->dumpdone = 0;
 
126
 
 
127
                /* now redefine the filename and wrap up */
 
128
                free(temp->fn);
 
129
                temp->fn = xstrdup(fn);
 
130
        }
 
131
 
 
132
        /* update the description */
 
133
 
 
134
        if (temp->desc)
 
135
                free(temp->desc);
 
136
 
 
137
        if (desc)
 
138
                temp->desc = xstrdup(desc);
 
139
        else
 
140
                temp->desc = NULL;
 
141
 
 
142
        /* always set this on reload */
 
143
        temp->retain = 1;
 
144
}               
33
145
 
34
146
/* return 1 if usable, 0 if not */
35
147
static int parse_upsd_conf_args(int numargs, char **arg)
71
183
                return 1;
72
184
        }
73
185
 
 
186
        /* ACCEPT <aclname> [<aclname>...] */
 
187
        if (!strcmp(arg[0], "ACCEPT")) {
 
188
                access_add(ACCESS_ACCEPT, numargs - 1, 
 
189
                        (const char **) &arg[1]);
 
190
                return 1;
 
191
        }
 
192
 
 
193
        /* REJECT <aclname> [<aclname>...] */
 
194
        if (!strcmp(arg[0], "REJECT")) {
 
195
                access_add(ACCESS_REJECT, numargs - 1, 
 
196
                        (const char **) &arg[1]);
 
197
                return 1;
 
198
        }
 
199
 
74
200
        /* everything below here uses up through arg[2] */
75
201
        if (numargs < 3)
76
202
                return 0;
84
210
        if (numargs < 4)
85
211
                return 0;
86
212
 
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");
91
215
                return 1;
92
216
        }
93
217
 
96
220
}
97
221
 
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)
100
224
{
101
225
        upslogx(LOG_ERR, "Fatal error in parseconf (upsd.conf): %s", errmsg);
102
226
}
103
227
 
104
 
void read_upsdconf(int reload)
 
228
static void load_upsdconf(int reloading)
105
229
{
106
230
        char    fn[SMALLBUF];
107
231
        PCONF_CTX       ctx;
110
234
 
111
235
        check_perms(fn);
112
236
 
113
 
        reload_flag = reload;
114
 
 
115
237
        pconf_init(&ctx, upsd_conf_err);
116
238
 
117
239
        if (!pconf_file_begin(&ctx, fn)) {
118
240
                pconf_finish(&ctx);
119
241
 
120
 
                if (!reload)
 
242
                if (!reloading)
121
243
                        fatalx("%s", ctx.errmsg);
122
 
                else {
123
 
                        upslogx(LOG_ERR, "Reload failed: %s", ctx.errmsg);
124
 
                        return;
125
 
                }
 
244
 
 
245
                upslogx(LOG_ERR, "Reload failed: %s", ctx.errmsg);
 
246
                return;
126
247
        }
127
248
 
128
249
        while (pconf_file_next(&ctx)) {
136
257
                        continue;
137
258
 
138
259
                if (!parse_upsd_conf_args(ctx.numargs, ctx.arglist)) {
139
 
                        int     i;
 
260
                        unsigned int    i;
140
261
                        char    errmsg[SMALLBUF];
141
262
 
142
263
                        snprintf(errmsg, sizeof(errmsg), 
146
267
                                snprintfcat(errmsg, sizeof(errmsg), " %s", 
147
268
                                        ctx.arglist[i]);
148
269
 
149
 
                        upslogx(LOG_WARNING, errmsg);
 
270
                        upslogx(LOG_WARNING, "%s", errmsg);
150
271
                }
151
272
 
152
273
        }
202
323
}
203
324
 
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)
206
327
{
207
328
        ups_t   *tmp = upstable, *next;
208
329
        char    statefn[SMALLBUF];
209
330
 
210
 
        /* nonfatal for now - eventually this will be required */
211
331
        if (!tmp) {
212
332
                upslogx(LOG_WARNING, "Warning: no UPS definitions in ups.conf");
213
333
                return;
229
349
                        upslogx(LOG_WARNING, "Warning: ignoring incomplete configuration for UPS [%s]\n", 
230
350
                                tmp->upsname);
231
351
                } else {
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));
233
354
 
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);
237
358
                        else
238
 
                                addups(statefn, tmp->upsname, tmp->desc);
 
359
                                ups_create(statefn, tmp->upsname, tmp->desc);
239
360
                }
240
361
 
241
362
                /* free tmp's resources */
258
379
}
259
380
 
260
381
/* remove a UPS from the linked list */
261
 
void delete_ups(upstype *target)
 
382
static void delete_ups(upstype *target)
262
383
{
263
384
        upstype *ptr, *last;
264
385
 
308
429
}                       
309
430
 
310
431
/* see if we can open a file */
311
 
static int check_file(char *fn)
 
432
static int check_file(const char *fn)
312
433
{
313
434
        char    chkfn[SMALLBUF];
314
435
        FILE    *f;
327
448
}
328
449
 
329
450
/* called after SIGHUP */
330
 
void reload_config(void)
 
451
void conf_reload(void)
331
452
{
332
453
        upstype *upstmp, *upsnext;
333
454
 
345
466
        }
346
467
 
347
468
        /* reload from ups.conf */
348
 
        read_upsconf(1);                /* 1 = required */
 
469
        read_upsconf();
349
470
        upsconf_add(1);                 /* 1 = reloading */
350
471
 
351
472
        /* flush ACL/ACCESS definitions */
353
474
        access_free();
354
475
 
355
476
        /* now reread upsd.conf */
356
 
        read_upsdconf(1);
 
477
        load_upsdconf(1);               /* 1 = reloading */
357
478
 
358
479
        /* now delete all UPS entries that didn't get reloaded */
359
480
 
383
504
        /* and finally reread from upsd.users */
384
505
        user_load();
385
506
}
 
507
 
 
508
/* startup: load config files */
 
509
void conf_load(void)
 
510
{
 
511
        /* upsd.conf */
 
512
        load_upsdconf(0);       /* 0 = initial */
 
513
 
 
514
        /* handle ups.conf */
 
515
        read_upsconf();
 
516
        upsconf_add(0);
 
517
 
 
518
        /* upsd.users */
 
519
        user_load();
 
520
}