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

« back to all changes in this revision

Viewing changes to models/main.c

  • Committer: Bazaar Package Importer
  • Author(s): Arnaud Quette
  • Date: 2004-05-28 13:10:01 UTC
  • mto: (16.1.1 squeeze)
  • mto: This revision was merged to the branch mainline in revision 3.
  • Revision ID: james.westby@ubuntu.com-20040528131001-yj2m9qcez4ya2w14
Tags: upstream-1.4.2
ImportĀ upstreamĀ versionĀ 1.4.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* main.c - Network UPS Tools driver core
2
 
 
3
 
   Copyright (C) 1999  Russell Kroll <rkroll@exploits.org>
4
 
 
5
 
   This program is free software; you can redistribute it and/or modify
6
 
   it under the terms of the GNU General Public License as published by
7
 
   the Free Software Foundation; either version 2 of the License, or
8
 
   (at your option) any later version.
9
 
 
10
 
   This program is distributed in the hope that it will be useful,
11
 
   but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 
   GNU General Public License for more details.
14
 
 
15
 
   You should have received a copy of the GNU General Public License
16
 
   along with this program; if not, write to the Free Software
17
 
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18
 
*/
19
 
 
20
 
#include "main.h"
21
 
 
22
 
        unsigned int sddelay = 90;      /* wait 90 seconds for shutdown */
23
 
        const char *progname = NULL, *upsname = NULL;
24
 
        const char *device_name = NULL, *device_path = NULL;
25
 
        const char *statepath = NULL;
26
 
        int     do_forceshutdown = 0;
27
 
        vartab_t        *vartab_h = NULL;
28
 
        int     experimental_driver = 0;
29
 
 
30
 
        /* stuff from upscommon */
31
 
        extern  int     do_lock_port;
32
 
        extern  char    *pidfn;
33
 
        extern  int     use_init_fn;    /* TODO: remove in the future */
34
 
 
35
 
        /* stuff from common */
36
 
        extern  int     upslog_flags;
37
 
 
38
 
/* power down the attached load immediately */
39
 
static void forceshutdown(void)
40
 
{
41
 
        upslogx(LOG_NOTICE, "Initiating UPS shutdown");
42
 
        printf("Initiating forced UPS shutdown!\n");
43
 
 
44
 
        do_lock_port = 0;
45
 
        upsdrv_shutdown();
46
 
 
47
 
        if (sddelay > 0) {
48
 
                printf("Waiting for poweroff...\n");
49
 
                sleep(sddelay);
50
 
                printf("Hmm, did the shutdown fail?  Oh well...\n");
51
 
                exit(1);
52
 
        }
53
 
        exit(0);
54
 
}
55
 
 
56
 
void usage(void)
57
 
{
58
 
        printf ("usage: %s [-h] [-a <id>] [-d <num>] [-k] [-x <var>=<val>] [<device>]\n", 
59
 
                progname);
60
 
        printf("\n");
61
 
        printf("Examples:\n");
62
 
        printf("         %s -a myups       - new method: use ups.conf section myups\n",
63
 
                progname);
64
 
        printf("         %s /dev/ttyS0     - old method: start up on port /dev/ttyS0\n", 
65
 
                progname);
66
 
        exit(1);
67
 
}
68
 
 
69
 
void help(void)
70
 
{
71
 
        vartab_t        *tmp;
72
 
 
73
 
        printf ("usage: %s [-h] [-a <id>] [-d <num>] [-k] [-x <var>=<val>] [<device>]\n", 
74
 
                progname);
75
 
 
76
 
        printf (
77
 
                "  -a <id>        - autoconfig using ups.conf section <id>\n"
78
 
                "                 - note: -x options after -a override ups.conf settings\n"
79
 
                "  -d <num>       - wait <num> seconds after sending shutdown command\n"
80
 
                "  -k             - force shutdown\n"
81
 
                "  -h             - display this help\n"
82
 
                "  -x <var>=<val> - set driver variable <var> to <val>\n"
83
 
                "                 - example: -x cable=940-0095B\n"
84
 
                "\n"
85
 
                "  <device>       - /dev entry corresponding to UPS port\n"
86
 
                "                 - Only optional when using -a!\n"
87
 
                "\n");
88
 
 
89
 
        if (vartab_h) {
90
 
                tmp = vartab_h;
91
 
 
92
 
                printf ("Acceptable values for -x in this driver:\n\n");
93
 
 
94
 
                while (tmp) {
95
 
                        if (tmp->vartype == VAR_VALUE)
96
 
                                printf("%40s : -x %s=<value>\n", 
97
 
                                       tmp->desc, tmp->var);
98
 
                        else
99
 
                                printf("%40s : -x %s\n", tmp->desc, tmp->var);
100
 
                        tmp = tmp->next;
101
 
                }
102
 
        }
103
 
 
104
 
        upsdrv_help();
105
 
        exit(1);
106
 
}
107
 
 
108
 
/* cram var [= <val>] data into storage */
109
 
static void storeval(const char *var, char *val)
110
 
{
111
 
        vartab_t        *tmp, *last;
112
 
 
113
 
        tmp = last = vartab_h;
114
 
 
115
 
        while (tmp) {
116
 
                last = tmp;
117
 
 
118
 
                /* sanity check */
119
 
                if (!tmp->var) {
120
 
                        tmp = tmp->next;
121
 
                        continue;
122
 
                }
123
 
 
124
 
                /* later definitions overwrite earlier ones */
125
 
                if (!strcasecmp(tmp->var, var)) {
126
 
                        if (tmp->val)
127
 
                                free(tmp->val);
128
 
 
129
 
                        if (val)
130
 
                                tmp->val = xstrdup(val);
131
 
 
132
 
                        tmp->found = 1;
133
 
                        return;
134
 
                }
135
 
 
136
 
                tmp = tmp->next;
137
 
        }
138
 
 
139
 
        printf("Error: -x %s is not valid for this driver.\n\n", var);
140
 
        help();
141
 
}
142
 
 
143
 
/* retrieve the value of variable <var> if possible */
144
 
char *getval(const char *var)
145
 
{
146
 
        vartab_t        *tmp = vartab_h;
147
 
 
148
 
        while (tmp) {
149
 
                if (!strcasecmp(tmp->var, var))
150
 
                        return(tmp->val);
151
 
                tmp = tmp->next;
152
 
        }
153
 
 
154
 
        return NULL;
155
 
}
156
 
 
157
 
/* see if <var> has been defined, even if no value has been given to it */
158
 
int testvar(const char *var)
159
 
{
160
 
        vartab_t        *tmp = vartab_h;
161
 
 
162
 
        while (tmp) {
163
 
                if (!strcasecmp(tmp->var, var))
164
 
                        return tmp->found;
165
 
                tmp = tmp->next;
166
 
        }
167
 
 
168
 
        return 0;       /* not found */
169
 
}
170
 
 
171
 
/* callback from driver - create the table for -x/conf entries */
172
 
void addvar(int vartype, const char *name, const char *desc)
173
 
{
174
 
        vartab_t        *tmp, *last;
175
 
 
176
 
        tmp = last = vartab_h;
177
 
 
178
 
        while (tmp) {
179
 
                last = tmp;
180
 
                tmp = tmp->next;
181
 
        }
182
 
 
183
 
        tmp = xmalloc(sizeof(vartab_t));
184
 
 
185
 
        tmp->vartype = vartype;
186
 
        tmp->var = xstrdup(name);
187
 
        tmp->val = NULL;
188
 
        tmp->desc = xstrdup(desc);
189
 
        tmp->found = 0;
190
 
        tmp->next = NULL;
191
 
 
192
 
        if (last)
193
 
                last->next = tmp;
194
 
        else
195
 
                vartab_h = tmp;
196
 
}       
197
 
 
198
 
/* handle -x / ups.conf config details that are for this part of the code */
199
 
static int main_arg(char *var, char *val)
200
 
{
201
 
        /* flags for main: just 'nolock' for now */
202
 
 
203
 
        if (!strcmp(var, "nolock")) {
204
 
                do_lock_port = 0;
205
 
                return 1;       /* handled */
206
 
        }
207
 
 
208
 
        /* any other flags are for the driver code */
209
 
        if (!val)
210
 
                return 0;
211
 
 
212
 
        /* variables for main: port and sddelay */
213
 
 
214
 
        if (!strcmp(var, "port")) {
215
 
                device_path = xstrdup(val);
216
 
                device_name = xbasename(device_path);
217
 
                return 1;       /* handled */
218
 
        }
219
 
 
220
 
        if (!strcmp(var, "sddelay")) {
221
 
                sddelay = atoi(val);
222
 
                return 1;       /* handled */
223
 
        }
224
 
 
225
 
        /* only for upsdrvctl - ignored here */
226
 
        if (!strcmp(var, "sdorder"))
227
 
                return 1;       /* handled */
228
 
 
229
 
        return 0;       /* unhandled, pass it through to the driver */
230
 
}
231
 
 
232
 
void do_upsconf_args(char *confupsname, char *var, char *val)
233
 
{
234
 
        /* ignore global stuff in here */
235
 
        if (!confupsname)
236
 
                return;
237
 
 
238
 
        /* no match = not for us */
239
 
        if (strcmp(confupsname, upsname) != 0)
240
 
                return;
241
 
 
242
 
        if (main_arg(var, val))
243
 
                return;
244
 
 
245
 
        /* flags (no =) now get passed to the driver-level stuff */
246
 
        if (!val) {
247
 
                storeval(var, NULL);
248
 
                return;
249
 
        }
250
 
 
251
 
        /* don't let the user shoot themselves in the foot */
252
 
        if (!strcmp(var, "driver")) {
253
 
                if (strcmp(val, progname) != 0)
254
 
                        fatalx("Error: UPS [%s] is for driver %s, but I'm %s!\n",
255
 
                                confupsname, val, progname);
256
 
                return;
257
 
        }
258
 
 
259
 
        /* everything else must be for the driver */
260
 
 
261
 
        storeval(var, val);
262
 
}
263
 
 
264
 
/* split -x foo=bar into 'foo' and 'bar' */
265
 
static void splitxarg(char *inbuf)
266
 
{
267
 
        char    *eqptr, *val, *buf;
268
 
 
269
 
        /* make our own copy - avoid changing argv */
270
 
        buf = xstrdup(inbuf);
271
 
 
272
 
        eqptr = strchr(buf, '=');
273
 
 
274
 
        if (!eqptr)
275
 
                val = NULL;
276
 
        else {
277
 
                *eqptr++ = '\0';
278
 
                val = eqptr;
279
 
        }
280
 
 
281
 
        /* see if main handles this first */
282
 
        if (main_arg(buf, val))
283
 
                return;
284
 
 
285
 
        /* otherwise store it for later */
286
 
        storeval(buf, val);
287
 
}
288
 
 
289
 
/* two pipes for two different sync points during startup */
290
 
 
291
 
#define SERIALIZE_INIT 1
292
 
#define SERIALIZE_SETM1 2
293
 
#define SERIALIZE_SETM2 3
294
 
#define SERIALIZE_WAITM1 4
295
 
#define SERIALIZE_WAITM2 5
296
 
 
297
 
void serialize(int op)
298
 
{
299
 
        static  int     pipe1[2], pipe2[2];
300
 
        int     ret;
301
 
        char    ch;
302
 
 
303
 
        switch (op) {
304
 
 
305
 
                case SERIALIZE_INIT:
306
 
                        ret = pipe(pipe1);
307
 
 
308
 
                        if (ret != 0) {
309
 
                                fatal("serialize: pipe");
310
 
                                exit(1);
311
 
                        }
312
 
 
313
 
                        ret = pipe(pipe2);
314
 
 
315
 
                        if (ret != 0) {
316
 
                                fatal("serialize: pipe");
317
 
                                exit(1);
318
 
                        }
319
 
 
320
 
                        break;
321
 
 
322
 
                case SERIALIZE_SETM1:
323
 
                        close(pipe1[0]);
324
 
                        close(pipe1[1]);
325
 
                        break;
326
 
 
327
 
                case SERIALIZE_WAITM1:
328
 
                        close(pipe1[1]);
329
 
                        ret = read(pipe1[0], &ch, 1);
330
 
                        close(pipe1[0]);
331
 
                        break;
332
 
 
333
 
                case SERIALIZE_SETM2:
334
 
                        close(pipe2[0]);
335
 
                        close(pipe2[1]);
336
 
                        break;
337
 
 
338
 
                case SERIALIZE_WAITM2:
339
 
                        close(pipe2[1]);
340
 
                        ret = read(pipe2[0], &ch, 1);
341
 
                        close(pipe2[0]);
342
 
                        break;
343
 
        }
344
 
}
345
 
 
346
 
static void runparent(void)
347
 
{
348
 
        close_statefd();
349
 
 
350
 
        /* allow the client to exit from its WAITM1 call */
351
 
        serialize(SERIALIZE_SETM1);
352
 
 
353
 
        /* wait for the client to call info_ready() and set M2 */
354
 
        serialize(SERIALIZE_WAITM2);
355
 
 
356
 
        _exit(0);
357
 
}
358
 
 
359
 
 
360
 
/* special version to deal with the nasty sync issues in here */
361
 
static void main_background(void)
362
 
{
363
 
        int     pid;
364
 
 
365
 
        if ((pid = fork()) < 0)
366
 
                fatal("Unable to enter background");
367
 
 
368
 
        xbit_set(&upslog_flags, UPSLOG_SYSLOG);
369
 
        xbit_clear(&upslog_flags, UPSLOG_STDERR);
370
 
 
371
 
        close(0);
372
 
        close(1);
373
 
        close(2);
374
 
 
375
 
        if (pid != 0)
376
 
                runparent();
377
 
 
378
 
        /* child */
379
 
 
380
 
        /* make fds 0-2 point somewhere defined */
381
 
        if (open("/dev/null", O_RDWR) != 0)
382
 
                fatal("open /dev/null");
383
 
        dup(0);
384
 
        dup(0);
385
 
 
386
 
#ifdef HAVE_SETSID
387
 
        setsid();               /* make a new session to dodge signals */
388
 
#endif
389
 
 
390
 
        /* wait until after parent closes the statefd */
391
 
        serialize(SERIALIZE_WAITM1);
392
 
 
393
 
        /* load up info with a full set of data */
394
 
        upsdrv_updateinfo();
395
 
 
396
 
        /* put the state file where it's supposed to be */
397
 
        info_ready();
398
 
 
399
 
        /* let the parent exit now that we're ready to roll */
400
 
        serialize(SERIALIZE_SETM2);
401
 
 
402
 
        upslogx(LOG_INFO, "Startup successful");
403
 
}
404
 
 
405
 
int main(int argc, char **argv)
406
 
{
407
 
        int     i;
408
 
#ifdef HAVE_MMAP
409
 
        int     shmok = 0;
410
 
#else
411
 
        int     shmok = 1;
412
 
#endif
413
 
 
414
 
        droproot();
415
 
 
416
 
        upsdrv_banner();
417
 
 
418
 
        if (experimental_driver) {
419
 
                printf("Warning: This is an experimental driver.\n");
420
 
                printf("Some features may not function correctly.\n\n");
421
 
        }
422
 
 
423
 
        progname = xbasename(argv[0]);
424
 
        openlog(progname, LOG_PID, LOG_FACILITY);
425
 
 
426
 
        if ((statepath = getenv("NUT_STATEPATH")) == NULL)
427
 
                statepath = STATEPATH;
428
 
 
429
 
        /* build the driver's extra (-x) variable table */
430
 
        upsdrv_makevartable();
431
 
 
432
 
        while ((i = getopt(argc, argv, "+a:d:kDhx:")) != EOF) {
433
 
                switch (i) {
434
 
                        case 'a':
435
 
                                upsname = optarg;
436
 
                                read_upsconf(1);
437
 
                                break;
438
 
                        case 'd':
439
 
                                sddelay = atoi(optarg);
440
 
                                break;
441
 
                        case 'k':
442
 
                                do_forceshutdown = 1;
443
 
                                break;
444
 
                        case 'D':
445
 
                                nut_debug_level++;
446
 
                                break;
447
 
                        case 'V':
448
 
                                printf("Network UPS Tools (%s)\n", UPS_VERSION);
449
 
                                exit(0);
450
 
                        case 'x':
451
 
                                splitxarg(optarg);
452
 
                                break;
453
 
                        case 'h':
454
 
                                help();
455
 
                                break;
456
 
                        default:
457
 
                                usage();
458
 
                                break;
459
 
                }
460
 
        }
461
 
 
462
 
        argc -= optind;
463
 
        argv += optind;
464
 
        optind = 1;
465
 
 
466
 
        /* we need to get the port from somewhere */
467
 
        if (argc < 1) {
468
 
                if (!device_path) {
469
 
                        fprintf(stderr, "Error: You must specify a port name in ups.conf or on the command line.\n");
470
 
                        usage();
471
 
                }
472
 
        }
473
 
 
474
 
        /* allow argv to override the ups.conf entry if specified */
475
 
        else {
476
 
                device_path = argv[0];
477
 
                device_name = xbasename(device_path);
478
 
        }
479
 
 
480
 
        snprintf(statefn, sizeof(statefn), "%s/%s-%s", 
481
 
                statepath, progname, device_name);
482
 
 
483
 
        pidfn = xmalloc(SMALLBUF);
484
 
        snprintf(pidfn, SMALLBUF, "%s/%s-%s.pid", 
485
 
                ALTPIDPATH, progname, device_name);
486
 
 
487
 
        upsdebugx(1, "debug level is '%d'", nut_debug_level);
488
 
 
489
 
        upsdrv_initups();
490
 
 
491
 
        if (do_forceshutdown)
492
 
                forceshutdown();
493
 
 
494
 
        if (chdir(statepath))
495
 
                fatal("Can't chdir to %s", statepath);
496
 
 
497
 
        /* TODO: remove when all old drivers are dead */
498
 
        use_init_fn = 1;
499
 
 
500
 
        create_info(upsdrv_infomax(), shmok);
501
 
        createmsgq();
502
 
 
503
 
        upsdrv_initinfo();
504
 
 
505
 
        if (nut_debug_level == 0) {
506
 
                serialize(SERIALIZE_INIT);
507
 
                main_background();
508
 
                writepid(pidfn);
509
 
        } else {
510
 
 
511
 
                /* no sync to worry about, so rename it and get going */
512
 
                info_ready();
513
 
 
514
 
        }
515
 
 
516
 
        for (;;) {
517
 
                upsdrv_updateinfo();
518
 
 
519
 
                if (getupsmsg(2))       
520
 
                        upslogx(LOG_DEBUG, "Received a message from upsd");
521
 
        }
522
 
 
523
 
        return 0;
524
 
}