File: server-conf.c

package info (click to toggle)
conman 0.3.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,600 kB
  • sloc: ansic: 11,863; sh: 2,174; exp: 2,132; makefile: 270; perl: 120
file content (1616 lines) | stat: -rw-r--r-- 50,916 bytes parent folder | download
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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
/*****************************************************************************
 *  Written by Chris Dunlap <cdunlap@llnl.gov>.
 *  Copyright (C) 2007-2022 Lawrence Livermore National Security, LLC.
 *  Copyright (C) 2001-2007 The Regents of the University of California.
 *  UCRL-CODE-2002-009.
 *
 *  This file is part of ConMan: The Console Manager.
 *  For details, see <https://dun.github.io/conman/>.
 *
 *  ConMan 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 3 of the License, or (at your option)
 *  any later version.
 *
 *  ConMan 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 ConMan.  If not, see <http://www.gnu.org/licenses/>.
 *****************************************************************************/


#if HAVE_CONFIG_H
#  include <config.h>
#endif /* HAVE_CONFIG_H */

#include <assert.h>
#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <syslog.h>
#include <unistd.h>
#include "common.h"
#include "lex.h"
#include "list.h"
#include "log.h"
#include "server.h"
#include "tpoll.h"
#include "util-file.h"
#include "util-str.h"
#include "util.h"


enum server_conf_toks {
/*
 *  Keep enums in sync w/ server_conf_strs[].
 */
    SERVER_CONF_CONSOLE = LEX_TOK_OFFSET,
    SERVER_CONF_COREDUMP,
    SERVER_CONF_COREDUMPDIR,
    SERVER_CONF_DEV,
    SERVER_CONF_EXECPATH,
    SERVER_CONF_GLOBAL,
#if WITH_FREEIPMI
    SERVER_CONF_IPMIOPTS,
#endif /* WITH_FREEIPMI */
    SERVER_CONF_KEEPALIVE,
    SERVER_CONF_LOG,
    SERVER_CONF_LOGDIR,
    SERVER_CONF_LOGFILE,
    SERVER_CONF_LOGOPTS,
    SERVER_CONF_LOOPBACK,
    SERVER_CONF_NAME,
    SERVER_CONF_NOFILE,
    SERVER_CONF_OFF,
    SERVER_CONF_ON,
    SERVER_CONF_PIDFILE,
    SERVER_CONF_PORT,
    SERVER_CONF_RESETCMD,
    SERVER_CONF_SEROPTS,
    SERVER_CONF_SERVER,
    SERVER_CONF_SYSLOG,
    SERVER_CONF_TCPWRAPPERS,
    SERVER_CONF_TESTOPTS,
    SERVER_CONF_TIMESTAMP
};

static char *server_conf_strs[] = {
/*
 *  Keep strings in sync w/ server_conf_toks enum.
 *  These must be sorted in a case-insensitive manner.
 */
    "CONSOLE",
    "COREDUMP",
    "COREDUMPDIR",
    "DEV",
    "EXECPATH",
    "GLOBAL",
#if WITH_FREEIPMI
    "IPMIOPTS",
#endif /* WITH_FREEIPMI */
    "KEEPALIVE",
    "LOG",
    "LOGDIR",
    "LOGFILE",
    "LOGOPTS",
    "LOOPBACK",
    "NAME",
    "NOFILE",
    "OFF",
    "ON",
    "PIDFILE",
    "PORT",
    "RESETCMD",
    "SEROPTS",
    "SERVER",
    "SYSLOG",
    "TCPWRAPPERS",
    "TESTOPTS",
    "TIMESTAMP",
    NULL
};


/*  The <sys/syslog.h> prioritynames[] and facilitynames[] are not portable
 *    (solaris2.8 doesn't have 'em), so they're effectively reproduced here.
 */
typedef struct tag {
    const char *key;
    int         val;
} tag_t;

static tag_t logPriorities[] = {
    { "alert",      LOG_ALERT },
    { "crit",       LOG_CRIT },
    { "critical",   LOG_CRIT },
    { "debug",      LOG_DEBUG },
    { "emerg",      LOG_EMERG },
    { "emergency",  LOG_EMERG },
    { "err",        LOG_ERR },
    { "error",      LOG_ERR },
    { "info",       LOG_INFO },
    { "notice",     LOG_NOTICE },
    { "panic",      LOG_EMERG },
    { "warn",       LOG_WARNING },
    { "warning",    LOG_WARNING },
    { NULL,         -1 }
};

static tag_t logFacilities[] = {
    { "auth",       LOG_AUTH },
#ifdef LOG_AUTHPRIV
    { "authpriv",   LOG_AUTHPRIV },
#endif /* LOG_AUTHPRIV */
    { "cron",       LOG_CRON },
    { "daemon",     LOG_DAEMON },
    { "kern",       LOG_KERN },
    { "lpr",        LOG_LPR },
    { "mail",       LOG_MAIL },
    { "news",       LOG_NEWS },
    { "user",       LOG_USER },
    { "uucp",       LOG_UUCP },
    { "local0",     LOG_LOCAL0 },
    { "local1",     LOG_LOCAL1 },
    { "local2",     LOG_LOCAL2 },
    { "local3",     LOG_LOCAL3 },
    { "local4",     LOG_LOCAL4 },
    { "local5",     LOG_LOCAL5 },
    { "local6",     LOG_LOCAL6 },
    { "local7",     LOG_LOCAL7 },
    { NULL,         -1 }
};

typedef struct console_strs {
    char *name;
    char *dev;
    char *log;
    char *lopts;
    char *sopts;
#if WITH_FREEIPMI
    char *iopts;
#endif /* WITH_FREEIPMI */
    char *topts;
} console_strs_t;


static void display_server_help(char *prog);
static void signal_daemon(server_conf_t *conf);
static void parse_console_directive(server_conf_t *conf, Lex l);
static int process_console(server_conf_t *conf, console_strs_t *con_p,
    char *errbuf, int errbuflen);
static void parse_global_directive(server_conf_t *conf, Lex l);
static void parse_server_directive(server_conf_t *conf, Lex l);
static int read_pidfile(const char *pidfile);
static int write_pidfile(const char *pidfile);
static int lookup_syslog_priority(const char *priority);
static int lookup_syslog_facility(const char *facility);


server_conf_t * create_server_conf(void)
{
    server_conf_t *conf;
    char buf[PATH_MAX];

    if (!(conf = malloc(sizeof(server_conf_t)))) {
        out_of_memory();
    }
    conf->cwd = NULL;
    conf->confFileName = create_string(CONMAN_CONF);
    conf->coreDumpDir = NULL;
    conf->execPath = NULL;
    conf->logDirName = NULL;
    conf->logFileName = NULL;
    conf->logFmtName = NULL;
    conf->logFilePtr = NULL;
    conf->logFileLevel = LOG_INFO;
    conf->numOpenFiles = 0;
    conf->pidFileName = NULL;
    conf->resetCmd = NULL;
    conf->syslogFacility = -1;
    conf->throwSignal = -1;
    conf->tStampMinutes = 0;
    conf->tStampNext = 0;
    /*
     *  The conf file's fd must be saved and kept open in order to hold an
     *    fcntl-style lock.  This lock is used to ensure only one instance
     *    of a given configuration can be running.  It is also used to
     *    obtain the pid of an active daemon in order to support the
     *    '-k' and '-r' cmdline options.
     */
    conf->fd = -1;
    conf->port = 0;
    conf->ld = -1;
    conf->objs = list_create((ListDelF) destroy_obj);
    if (!(conf->tp = tpoll_create(0))) {
        log_err(0, "Unable to create object for multiplexing I/O");
    }
    conf->globalLogName = NULL;
    conf->globalLogOpts.enableSanitize = DEFAULT_LOGOPT_SANITIZE;
    conf->globalLogOpts.enableTimestamp = DEFAULT_LOGOPT_TIMESTAMP;
    conf->globalLogOpts.enableLock = DEFAULT_LOGOPT_LOCK;
    conf->globalSerOpts.bps = DEFAULT_SEROPT_BPS;
    conf->globalSerOpts.databits = DEFAULT_SEROPT_DATABITS;
    conf->globalSerOpts.parity = DEFAULT_SEROPT_PARITY;
    conf->globalSerOpts.stopbits = DEFAULT_SEROPT_STOPBITS;

#if WITH_FREEIPMI
    if (init_ipmi_opts(&conf->globalIpmiOpts) < 0) {
        log_err(0, "Unable to initialize default IPMI options");
    }
    conf->numIpmiObjs = 0;
#endif /* WITH_FREEIPMI */

    if (init_test_opts(&conf->globalTestOpts) < 0) {
        log_err(0, "Unable to initialize default test options");
    }
    conf->enableCoreDump = 0;
    conf->enableKeepAlive = 1;
    conf->enableLoopBack = 1;
    conf->enableTCPWrap = 0;
    conf->enableVerbose = 0;
    conf->enableZeroLogs = 0;
    conf->enableForeground = 0;
    /*
     *  Copy the current working directory before we chdir() away.
     *  Since logfiles can be re-opened after the daemon has chdir()'d,
     *    we need to prepend relative paths with the cwd.
     */
    if (!getcwd(buf, sizeof(buf))) {
        log_err(errno, "Unable to determine working directory");
    }
    conf->cwd = create_string(buf);
    conf->logDirName = create_string(buf);
    return(conf);
}


void destroy_server_conf(server_conf_t *conf)
{
    if (!conf) {
        return;
    }
    if (conf->pidFileName) {
        if (unlink(conf->pidFileName) < 0) {
            log_msg(LOG_ERR, "Unable to delete pid file \"%s\": %s",
                conf->pidFileName, strerror(errno));
        }
    }
    if (conf->fd >= 0) {
        if (close(conf->fd) < 0) {
            log_msg(LOG_ERR, "Unable to close config file \"%s\": %s",
                conf->confFileName, strerror(errno));
        }
        conf->fd = -1;
    }
    if (conf->ld >= 0) {
        if (close(conf->ld) < 0) {
            log_msg(LOG_ERR, "Unable to close listening socket: %s",
                strerror(errno));
        }
        conf->ld = -1;
    }
    if (conf->objs) {
        list_destroy(conf->objs);
    }
    if (conf->tp) {
        tpoll_destroy(conf->tp);
    }
    destroy_string(conf->confFileName);
    destroy_string(conf->coreDumpDir);
    destroy_string(conf->cwd);
    destroy_string(conf->execPath);
    destroy_string(conf->globalLogName);
    destroy_string(conf->logDirName);
    destroy_string(conf->logFileName);
    destroy_string(conf->logFmtName);
    destroy_string(conf->pidFileName);
    destroy_string(conf->resetCmd);
    free(conf);
    return;
}


void process_cmdline(server_conf_t *conf, int argc, char *argv[])
{
    int c;

    opterr = 0;
    while ((c = getopt(argc, argv, "c:FhkLp:P:qrvVz")) != -1) {
        switch(c) {
        case 'c':
            destroy_string(conf->confFileName);
            conf->confFileName = create_string(optarg);
            break;
        case 'F':
            conf->enableForeground = 1;
            break;
        case 'h':
            display_server_help(argv[0]);
            exit(0);
        case 'k':
            conf->throwSignal = SIGTERM;
            break;
        case 'L':
            printf("%s", conman_license);
            exit(0);
        case 'p':
            if ((conf->port = atoi(optarg)) <= 0) {
                log_err(0, "CMDLINE: invalid port \"%s\"", optarg);
            }
            break;
        case 'P':
            destroy_string(conf->pidFileName);
            conf->pidFileName = (optarg[0] == '/') ?
                create_string(optarg) :
                create_format_string("%s/%s", conf->cwd, optarg);
            break;
        case 'q':
            conf->throwSignal = 0;      /* null signal for error checking */
            break;
        case 'r':
            conf->throwSignal = SIGHUP;
            break;
        case 'v':
            conf->enableVerbose = 1;
            break;
        case 'V':
            printf("%s-%s%s\n", PACKAGE, VERSION, SERVER_FEATURES);
            exit(0);
        case 'z':
            conf->enableZeroLogs = 1;
            break;
        case '?':                       /* invalid option */
            log_err(0, "CMDLINE: invalid option \"%c\"", optopt);
            break;
        default:
            log_err(0, "CMDLINE: option \"%c\" not implemented", c);
            break;
        }
    }
    return;
}


void process_config(server_conf_t *conf)
{
    pid_t pid;
    struct stat fdStat;
    int len;
    char *buf;
    int n;
    Lex l;
    int tok;

    /*  Keep conf->fd open after parsing the file in order to obtain the lock.
     */
    if ((conf->fd = open(conf->confFileName, O_RDONLY)) < 0) {
        log_err(errno, "Unable to open \"%s\"", conf->confFileName);
    }
    /*  The daemon must be signalled, if needed, after opening the file but
     *    before obtaining a lock.
     */
    if (conf->throwSignal >= 0) {
        signal_daemon(conf);
        exit(0);
    }
    /*  Must use a read lock here since the file is only open for reading.
     *    Exclusive access is ensured by testing for a write lock afterwards.
     */
    if (get_read_lock(conf->fd) < 0) {
        log_err(0, "Unable to lock configuration \"%s\"",
            conf->confFileName);
    }
    if ((pid = is_write_lock_blocked(conf->fd)) > 0) {
        log_err(0, "Configuration \"%s\" in use by pid %d",
            conf->confFileName, pid);
    }
    /*  Read config into memory for parsing.
     */
    DPRINTF((9, "Opened config \"%s\": fd=%d.\n",
        conf->confFileName, conf->fd));
    set_fd_closed_on_exec(conf->fd);
    if (fstat(conf->fd, &fdStat) < 0) {
        log_err(errno, "Unable to stat \"%s\"", conf->confFileName);
    }
    len = fdStat.st_size;
    if (!(buf = malloc(len + 1))) {
        out_of_memory();
    }
    if ((n = read_n(conf->fd, buf, len)) < 0) {
        log_err(errno, "Unable to read \"%s\"", conf->confFileName);
    }
    assert(n == len);
    buf[len] = '\0';

    l = lex_create(buf, server_conf_strs);
    while ((tok = lex_next(l)) != LEX_EOF) {
        switch(tok) {
        case SERVER_CONF_CONSOLE:
            parse_console_directive(conf, l);
            break;
        case SERVER_CONF_GLOBAL:
            parse_global_directive(conf, l);
            break;
        case SERVER_CONF_SERVER:
            parse_server_directive(conf, l);
            break;
        case LEX_EOL:
            break;
        case LEX_ERR:
            log_msg(LOG_ERR, "CONFIG[%s:%d]: unmatched quote",
                conf->confFileName, lex_line(l));
            break;
        default:
            log_msg(LOG_ERR, "CONFIG[%s:%d]: unrecognized token '%s'",
                conf->confFileName, lex_line(l), lex_text(l));
            while (tok != LEX_EOL && tok != LEX_EOF) {
                tok = lex_next(l);
            }
            break;
        }
    }
    lex_destroy(l);
    free(buf);

    if (conf->port <= 0) {              /* port not set so use default */
        conf->port = atoi(CONMAN_PORT);
    }
    if (conf->logFileName) {
        if (strchr(conf->logFileName, '%')) {
            conf->logFmtName = create_string(conf->logFileName);
        }
    }
    if (conf->pidFileName) {
        if (write_pidfile(conf->pidFileName) < 0) {
            free(conf->pidFileName);
            conf->pidFileName = NULL;   /* prevent unlink() at exit */
        }
    }
    return;
}


static void display_server_help(char *prog)
{
/*  Displays a help message for the server's command-line options.
 */
    printf("Usage: %s [OPTIONS]\n", prog);
    printf("\n");
    printf("  -c FILE   Specify configuration. [%s]\n", CONMAN_CONF);
    printf("  -F        Run daemon in foreground.\n");
    printf("  -h        Display this help.\n");
    printf("  -k        Kill daemon.\n");
    printf("  -L        Display license information.\n");
    printf("  -p PORT   Specify port number. [%d]\n", atoi(CONMAN_PORT));
    printf("  -P FILE   Specify PID file.\n");
    printf("  -q        Query daemon's pid.\n");
    printf("  -r        Re-open log files.\n");
    printf("  -v        Be verbose.\n");
    printf("  -V        Display version information.\n");
    printf("  -z        Zero log files.\n");
    printf("\n");
    return;
}


static void signal_daemon(server_conf_t *conf)
{
/*  Signals the daemon running with the configuration specified by 'conf'.
 */
    pid_t pid;
    const char *msg;

    assert(conf != NULL);
    assert(conf->confFileName != NULL);
    assert(conf->fd >= 0);
    assert(conf->throwSignal >= 0);

    /*  Attempt to get the pid from the lock held on the config file.
     *  However, that could fail if the file has been modified since the
     *  daemon was invoked; this is because some editors move the old file
     *  out of the way and write a new file in its place.  As such, fall
     *  back on reading the pid file if one is available.  Unfortunately,
     *  that could fail as well if the name of the pid file was altered in
     *  the config file.  Note that the pid file does not support conversion
     *  specifiers in order to reduce the likelihood of this happening.
     */
    if ( !(pid = is_write_lock_blocked(conf->fd))
      && !(pid = read_pidfile(conf->pidFileName)) ) {
        log_err(0, "Configuration \"%s\" does not appear to be active",
            conf->confFileName);
    }
    /*  Prevent pidfile from being unlink()d by destroy_server_conf().
     */
    if (conf->pidFileName) {
        free(conf->pidFileName);
        conf->pidFileName = NULL;
    }
    /*  "Now go do that voodoo that you do so well."
     */
    if (kill(pid, conf->throwSignal) < 0) {
        if ((conf->throwSignal == 0) && (errno == EPERM)) {
            ; /* ignore permission error for pid query */
        }
        else if (errno == ESRCH) {
            log_err(0, "Configuration \"%s\" does not appear to be active",
                conf->confFileName);
        }
        else {
            log_err(errno,
                "Configuration \"%s\" (pid %d) cannot be sent signal=%d",
                conf->confFileName, (int) pid, conf->throwSignal);
        }
    }
    /*  "We don't need no stinkin' badges."
     */
    if (conf->throwSignal == 0) {
        printf("%d\n", (int) pid);
    }
    else if (conf->enableVerbose) {
        if (conf->throwSignal == SIGHUP) {
            msg = "reconfigured on";
        }
        else if (conf->throwSignal == SIGTERM) {
            msg = "terminated on";
        }
        else {
            msg = "sent";
        }
        fprintf(stderr, "Configuration \"%s\" (pid %d) %s signal=%d\n",
            conf->confFileName, (int) pid, msg, conf->throwSignal);
    }
#if WITH_FREEIPMI
    ipmi_fini();
#endif /* WITH_FREEIPMI */

    destroy_server_conf(conf);
    exit(0);
}


static void parse_console_directive(server_conf_t *conf, Lex l)
{
/*  CONSOLE NAME="<str>" DEV="<file>" [LOG="<file>"]
 *    [LOGOPTS="<str>"] [SEROPTS="<str>"] [IPMIOPTS="<str>"] [TESTOPTS="<str>"]
 *  Note: IPMIOPTS is only available if WITH_FREEIPMI is defined.
 */
    const char *directive;              /* name of directive being parsed */
    int tok;
    const char *tokstr;
    int done = 0;
    char err[MAX_LINE] = "";
    console_strs_t con;

    memset(&con, 0, sizeof(con));

    directive = lex_tok_to_str(l, lex_prev(l));
    if (!directive) {
        log_err(0, "Unable to lookup string for console directive");
    }
    while (!done && !*err) {
        tok = lex_next(l);
        tokstr = lex_tok_to_str(l, tok);
        switch(tok) {

        case SERVER_CONF_NAME:
            if (lex_next(l) != '=') {
                snprintf(err, sizeof(err),
                    "expected '=' after %s keyword", tokstr);
            }
            else if ((lex_next(l) != LEX_STR)
                    || is_empty_string(lex_text(l))) {
                snprintf(err, sizeof(err),
                    "expected STRING for %s value", tokstr);
            }
            else {
                replace_string(&con.name, lex_text(l));
            }
            break;

        case SERVER_CONF_DEV:
            if (lex_next(l) != '=') {
                snprintf(err, sizeof(err),
                    "expected '=' after %s keyword", tokstr);
            }
            else if ((lex_next(l) != LEX_STR)
                    || is_empty_string(lex_text(l))) {
                snprintf(err, sizeof(err),
                    "expected STRING for %s value", tokstr);
            }
            else {
                replace_string(&con.dev, lex_text(l));
            }
            break;

        case SERVER_CONF_LOG:
            if (lex_next(l) != '=') {
                snprintf(err, sizeof(err),
                    "expected '=' after %s keyword", tokstr);
            }
            else if (lex_next(l) != LEX_STR) {
                snprintf(err, sizeof(err),
                    "expected STRING for %s value", tokstr);
            }
#ifdef DO_CONF_ESCAPE_ERROR
            else if (strchr(lex_text(l), DEPRECATED_CONF_ESCAPE)) {
                snprintf(err, sizeof(err),
                    "ignoring %s %s value with deprecated '%c' -- use '%%N'",
                    directive, tokstr, DEPRECATED_CONF_ESCAPE);
            }
#endif /* DO_CONF_ESCAPE_ERROR */
            else if (is_empty_string(lex_text(l))) {
                replace_string(&con.log, "");
            }
            else if ((lex_text(l)[0] != '/') && (conf->logDirName)) {
                destroy_string(con.log);
                con.log = create_format_string("%s/%s",
                    conf->logDirName, lex_text(l));
            }
            else {
                replace_string(&con.log, lex_text(l));
            }
            break;

        case SERVER_CONF_LOGOPTS:
            if (lex_next(l) != '=') {
                snprintf(err, sizeof(err),
                    "expected '=' after %s keyword", tokstr);
            }
            else if ((lex_next(l) != LEX_STR)
                    || is_empty_string(lex_text(l))) {
                snprintf(err, sizeof(err),
                    "expected STRING for %s value", tokstr);
            }
            else {
                replace_string(&con.lopts, lex_text(l));
            }
            break;

        case SERVER_CONF_SEROPTS:
            if (lex_next(l) != '=') {
                snprintf(err, sizeof(err),
                    "expected '=' after %s keyword", tokstr);
            }
            else if ((lex_next(l) != LEX_STR)
                    || is_empty_string(lex_text(l))) {
                snprintf(err, sizeof(err),
                    "expected STRING for %s value", tokstr);
            }
            else {
                replace_string(&con.sopts, lex_text(l));
            }
            break;

#if WITH_FREEIPMI
        case SERVER_CONF_IPMIOPTS:
            if (lex_next(l) != '=') {
                snprintf(err, sizeof(err),
                    "unexpected '=' after %s keyword", tokstr);
            }
            else if (lex_next(l) != LEX_STR) {
                snprintf(err, sizeof(err),
                    "expected STRING for %s value", tokstr);
            }
            else {
                replace_string(&con.iopts, lex_text(l));
            }
            break;
#endif /* WITH_FREEIPMI */

        case SERVER_CONF_TESTOPTS:
            if (lex_next(l) != '=') {
                snprintf(err, sizeof(err),
                    "unexpected '=' after %s keyword", tokstr);
            }
            else if (lex_next(l) != LEX_STR) {
                snprintf(err, sizeof(err),
                    "expected STRING for %s value", tokstr);
            }
            else {
                replace_string(&con.topts, lex_text(l));
            }
            break;

        case LEX_EOF:
        case LEX_EOL:
            done = 1;
            break;

        case LEX_ERR:
            snprintf(err, sizeof(err), "unmatched quote");
            break;

        default:
            snprintf(err, sizeof(err), "unrecognized token '%s'", lex_text(l));
            break;
        }
    }
    if (!*err) {
        if (!con.name || !con.dev) {
            snprintf(err, sizeof(err), "incomplete %s directive", directive);
        }
        else {
            process_console(conf, &con, err, sizeof(err));
        }
    }
    if (*err) {
        log_msg(LOG_ERR, "CONFIG[%s:%d]: %s",
            conf->confFileName, lex_line(l), err);
        while (lex_prev(l) != LEX_EOL && lex_prev(l) != LEX_EOF) {
            (void) lex_next(l);
        }
    }
    destroy_string(con.name);
    destroy_string(con.dev);
    destroy_string(con.log);
    destroy_string(con.lopts);
    destroy_string(con.sopts);
#if WITH_FREEIPMI
    destroy_string(con.iopts);
#endif /* WITH_FREEIPMI */
    destroy_string(con.topts);
    return;
}


static int process_console(server_conf_t *conf, console_strs_t *con_p,
    char *errbuf, int errbuflen)
{
    List         args = NULL;
    char        *p;
    char        *q;
    char         quote;
    int          rc;
    char         buf[MAX_LINE];
    char        *arg0;
    char        *host = NULL;
    int          port;
    char        *path = NULL;
    obj_t       *console;
    seropt_t     seropts;
#if WITH_FREEIPMI
    ipmiopt_t    ipmiopts;
#endif /* WITH_FREEIPMI */
    logopt_t     logopts;
    test_opt_t   testopts;
    obj_t       *logfile;

    assert(conf != NULL);
    assert(con_p != NULL);
    assert(con_p->name != NULL);
    assert(con_p->dev != NULL);
    assert(errbuf != NULL);
    assert(errbuflen > 0);

    errbuf[ 0 ] = '\0';
    if (!(args = list_create((ListDelF) destroy_string))) {
        out_of_memory();
    }
    q = NULL;
    while ((rc = parse_string(con_p->dev, &p, &q, &quote)) > 0) {
        if (quote != '\'') {
            if (substitute_string(buf, sizeof(buf), p, 'N', con_p->name) < 0) {
                snprintf(errbuf, errbuflen,
                    "console [%s] dev string substitution failed",
                    con_p->name);
                goto err;
            }
            p = buf;
        }
        list_append(args, create_string(p));
    }
    if (rc < 0) {
        snprintf(errbuf, errbuflen,
            "console [%s] dev string parse error", con_p->name);
        goto err;
    }
    if (!(arg0 = list_peek(args))) {
        snprintf(errbuf, errbuflen,
            "console [%s] dev string is empty", con_p->name);
        goto err;
    }
    if (is_unixsock_dev(arg0, conf->cwd, &path)) {
        if (list_count(args) != 1) {
            snprintf(errbuf, errbuflen,
                "console [%s] dev string has too many args", con_p->name);
            goto err;
        }
        if (!(console = create_unixsock_obj(
                conf, con_p->name, path, errbuf, errbuflen))) {
            goto err;
        }
        free(path);
        path = NULL;
    }
    else if (is_telnet_dev(arg0, &host, &port)) {
        if (list_count(args) != 1) {
            snprintf(errbuf, errbuflen,
                "console [%s] dev string has too many args", con_p->name);
            goto err;
        }
        if (!(console = create_telnet_obj(
                conf, con_p->name, host, port, errbuf, errbuflen))) {
            goto err;
        }
        free(host);
        host = NULL;
    }
    else if (is_serial_dev(arg0, conf->cwd, &path)) {
        if (list_count(args) != 1) {
            snprintf(errbuf, errbuflen,
                "console [%s] dev string has too many args", con_p->name);
            goto err;
        }
        if (access(path, R_OK | W_OK) < 0) {
            snprintf(errbuf, errbuflen,
                "console [%s] device \"%s\" is not readable/writable",
                con_p->name, path);
            goto err;
        }
        seropts = conf->globalSerOpts;
        if (con_p->sopts && parse_serial_opts(
                &seropts, con_p->sopts, errbuf, errbuflen) < 0) {
            goto err;
        }
        if (!(console = create_serial_obj(
                conf, con_p->name, path, &seropts, errbuf, errbuflen))) {
            goto err;
        }
        free(path);
        path = NULL;
    }
    else if (is_process_dev(arg0, conf->cwd, conf->execPath, &path)) {
        free(list_pop(args));
        arg0 = list_push(args, path);
        path = NULL;
        if (!(console = create_process_obj(
                conf, con_p->name, args, errbuf, errbuflen))) {
            goto err;
        }
    }
#if WITH_FREEIPMI
    else if (is_ipmi_dev(arg0, &host)) {
        if (list_count(args) != 1) {
            snprintf(errbuf, errbuflen,
                "console [%s] dev string has too many args", con_p->name);
            goto err;
        }
        ipmiopts = conf->globalIpmiOpts;
        if (con_p->iopts && parse_ipmi_opts(
                &ipmiopts, con_p->iopts, errbuf, errbuflen) < 0) {
            goto err;
        }
        if (!(console = create_ipmi_obj(
                conf, con_p->name, &ipmiopts, host, errbuf, errbuflen))) {
            goto err;
        }
        free(host);
        host = NULL;
    }
#endif /* WITH_FREEIPMI */
    else if (is_test_dev(arg0)) {
        if (list_count(args) != 1) {
            snprintf(errbuf, errbuflen,
                "console [%s] dev string has too many args", con_p->name);
            goto err;
        }
        testopts = conf->globalTestOpts;
        if (con_p->topts && parse_test_opts(
                &testopts, con_p->topts, errbuf, errbuflen) < 0) {
            goto err;
        }
        if (!(console = create_test_obj(
                conf, con_p->name, &testopts, errbuf, errbuflen))) {
            goto err;
        }
    }
    else {
        snprintf(errbuf, errbuflen,
            "console [%s] device \"%s\" type unrecognized",
            con_p->name, arg0);
        goto err;
    }
    if ((con_p->log && con_p->log[ 0 ] != '\0')
            || (!con_p->log && conf->globalLogName)) {
        if (con_p->log) {
            strlcpy(buf, con_p->log, sizeof(buf));
        }
        else if ((conf->globalLogName[ 0 ] != '/') && (conf->logDirName)) {
            snprintf(buf, sizeof(buf), "%s/%s",
                conf->logDirName, conf->globalLogName);
            buf[ sizeof(buf) - 1 ] = '\0';
        }
        else {
            strlcpy(buf, conf->globalLogName, sizeof(buf));
        }
        logopts = conf->globalLogOpts;
        if (con_p->lopts && parse_logfile_opts(
                &logopts, con_p->lopts, errbuf, errbuflen) < 0) {
            goto err;
        }
        if (!(logfile = create_logfile_obj(
                conf, buf, console, &logopts, errbuf, errbuflen))) {
            goto err;
        }
        link_objs(console, logfile);
    }
    list_destroy(args);
    return(0);

err:
    errbuf[ errbuflen - 1 ] = '\0';
    list_destroy(args);
    destroy_string(host);
    destroy_string(path);
    return(-1);
}


static void parse_global_directive(server_conf_t *conf, Lex l)
{
    const char *directive;              /* name of directive being parsed */
    int tok;
    const char *tokstr;
    int done = 0;
    char err[MAX_LINE] = "";

    directive = lex_tok_to_str(l, lex_prev(l));
    if (!directive) {
        log_err(0, "Unable to lookup string for global directive");
    }
    while (!done && !*err) {
        tok = lex_next(l);
        tokstr = lex_tok_to_str(l, tok);
        switch(tok) {

        case SERVER_CONF_LOG:
            if (lex_next(l) != '=') {
                snprintf(err, sizeof(err),
                    "expected '=' after %s keyword", tokstr);
            }
            else if (lex_next(l) != LEX_STR) {
                snprintf(err, sizeof(err),
                    "expected STRING for %s value", tokstr);
            }
            else if (is_empty_string(lex_text(l))) {
                /*
                 *  Unset global log name if string is empty.
                 */
                destroy_string(conf->globalLogName);
                conf->globalLogName = NULL;
            }
#ifdef DO_CONF_ESCAPE_ERROR
            else if (strchr(lex_text(l), DEPRECATED_CONF_ESCAPE)) {
                snprintf(err, sizeof(err),
                    "ignoring %s %s value with deprecated '%c' -- use '%%N'",
                    directive, tokstr, DEPRECATED_CONF_ESCAPE);
            }
#endif /* DO_CONF_ESCAPE_ERROR */
            else if (!strstr(lex_text(l), "%N")
                    && !strstr(lex_text(l), "%D")) {
                snprintf(err, sizeof(err),
                    "ignoring %s %s value without '%%N' or '%%D'",
                    directive, tokstr);
            }
            else {
                destroy_string(conf->globalLogName);
                conf->globalLogName = create_string(lex_text(l));
            }
            break;

        case SERVER_CONF_LOGOPTS:
            if (lex_next(l) != '=') {
                snprintf(err, sizeof(err),
                    "expected '=' after %s keyword", tokstr);
            }
            else if ((lex_next(l) != LEX_STR)
                    || is_empty_string(lex_text(l))) {
                snprintf(err, sizeof(err),
                    "expected STRING for %s value", tokstr);
            }
            else {
                parse_logfile_opts(&conf->globalLogOpts, lex_text(l),
                    err, sizeof(err));
            }
            break;

        case SERVER_CONF_SEROPTS:
            if (lex_next(l) != '=') {
                snprintf(err, sizeof(err),
                    "expected '=' after %s keyword", tokstr);
            }
            else if ((lex_next(l) != LEX_STR)
                    || is_empty_string(lex_text(l))) {
                snprintf(err, sizeof(err),
                    "expected STRING for %s value", tokstr);
            }
            else {
                parse_serial_opts(&conf->globalSerOpts, lex_text(l),
                    err, sizeof(err));
            }
            break;

#if WITH_FREEIPMI
        case SERVER_CONF_IPMIOPTS:
            if (lex_next(l) != '=') {
                snprintf(err, sizeof(err),
                    "expected '=' after %s keyword", tokstr);
            }
            else if ((lex_next(l) != LEX_STR)
                    || is_empty_string(lex_text(l))) {
                snprintf(err, sizeof(err),
                    "expected STRING for %s value", tokstr);
            }
            else {
                parse_ipmi_opts(&conf->globalIpmiOpts, lex_text(l),
                    err, sizeof(err));
            }
            break;
#endif /* WITH_FREEIPMI */

        case SERVER_CONF_TESTOPTS:
            if (lex_next(l) != '=') {
                snprintf(err, sizeof(err),
                    "expected '=' after %s keyword", tokstr);
            }
            else if ((lex_next(l) != LEX_STR)
                    || is_empty_string(lex_text(l))) {
                snprintf(err, sizeof(err),
                    "expected STRING for %s value", tokstr);
            }
            else {
                parse_test_opts(&conf->globalTestOpts, lex_text(l),
                    err, sizeof(err));
            }
            break;

        case LEX_EOF:
        case LEX_EOL:
            done = 1;
            break;

        case LEX_ERR:
            snprintf(err, sizeof(err), "unmatched quote");
            break;

        default:
            snprintf(err, sizeof(err), "unrecognized token '%s'", lex_text(l));
            break;
        }
    }

    if (*err) {
        log_msg(LOG_ERR, "CONFIG[%s:%d]: %s",
            conf->confFileName, lex_line(l), err);
        while (lex_prev(l) != LEX_EOL && lex_prev(l) != LEX_EOF) {
            (void) lex_next(l);
        }
    }
    return;
}


static void parse_server_directive(server_conf_t *conf, Lex l)
{
    const char *directive;              /* name of directive being parsed */
    int tok;
    const char *tokstr;
    int done = 0;
    char err[MAX_LINE] = "";
    char *p;
    int n;
    struct stat st;

    /* Prevent command-line options from being overridden by the config file.
     */
    const int isPidFileNameSet = (conf->pidFileName != NULL);
    const int isPortSet = (conf->port > 0);

    directive = lex_tok_to_str(l, lex_prev(l));
    if (!directive) {
        log_err(0, "Unable to lookup string for server directive");
    }
    while (!done && !*err) {
        tok = lex_next(l);
        tokstr = lex_tok_to_str(l, tok);
        switch(tok) {

        case SERVER_CONF_COREDUMP:
            if (lex_next(l) != '=') {
                snprintf(err, sizeof(err),
                    "expected '=' after %s keyword", tokstr);
            }
            else if (lex_next(l) == SERVER_CONF_ON) {
                conf->enableCoreDump = 1;
            }
            else if (lex_prev(l) == SERVER_CONF_OFF) {
                conf->enableCoreDump = 0;
            }
            else {
                snprintf(err, sizeof(err),
                    "expected ON or OFF for %s value", tokstr);
            }
            break;

        case SERVER_CONF_COREDUMPDIR:
            if (lex_next(l) != '=') {
                snprintf(err, sizeof(err),
                    "expected '=' after %s keyword", tokstr);
            }
            else if ((lex_next(l) != LEX_STR)) {
                snprintf(err, sizeof(err),
                    "expected STRING for %s value", tokstr);
            }
            else if (is_empty_string(lex_text(l))) {
                destroy_string(conf->coreDumpDir);
                conf->coreDumpDir = NULL;
            }
            else if (stat(lex_text(l), &st) < 0) {
                snprintf(err, sizeof(err),
                    "cannot stat %s \"%s\"", tokstr, lex_text(l));
            }
            else if (!S_ISDIR(st.st_mode)) {
                snprintf(err, sizeof(err),
                    "invalid %s \"%s\" not a directory", tokstr, lex_text(l));
            }
            else {
                p = (lex_text(l)[0] != '/')
                    ? create_format_string("%s/%s", conf->cwd, lex_text(l))
                    : create_string(lex_text(l));
                destroy_string(conf->coreDumpDir);
                conf->coreDumpDir = p;
                /*
                 *  Remove trailing slashes from dir name.
                 */
                p += strlen(p) - 1;
                while ((p > conf->coreDumpDir) && (*p == '/')) {
                    *p-- = '\0';
                }
            }
            break;

        case SERVER_CONF_EXECPATH:
            if (lex_next(l) != '=') {
                snprintf(err, sizeof(err),
                    "expected '=' after %s keyword", tokstr);
            }
            else if ((lex_next(l) != LEX_STR)) {
                snprintf(err, sizeof(err),
                    "expected STRING for %s value", tokstr);
            }
            else if (strlen(lex_text(l)) >= PATH_MAX) {
                snprintf(err, sizeof(err),
                    "exceeded max length for %s value", tokstr);
            }
            else if (is_empty_string(lex_text(l))) {
                destroy_string(conf->execPath);
                conf->execPath = NULL;
            }
            else {
                destroy_string(conf->execPath);
                conf->execPath = create_string(lex_text(l));
            }
            break;

        case SERVER_CONF_KEEPALIVE:
            if (lex_next(l) != '=') {
                snprintf(err, sizeof(err),
                    "expected '=' after %s keyword", tokstr);
            }
            else if (lex_next(l) == SERVER_CONF_ON) {
                conf->enableKeepAlive = 1;
            }
            else if (lex_prev(l) == SERVER_CONF_OFF) {
                conf->enableKeepAlive = 0;
            }
            else {
                snprintf(err, sizeof(err),
                    "expected ON or OFF for %s value", tokstr);
            }
            break;

        case SERVER_CONF_LOGDIR:
            if (lex_next(l) != '=') {
                snprintf(err, sizeof(err),
                    "expected '=' after %s keyword", tokstr);
            }
            else if ((lex_next(l) != LEX_STR)) {
                snprintf(err, sizeof(err),
                    "expected STRING for %s value", tokstr);
            }
            else if (is_empty_string(lex_text(l))) {
                destroy_string(conf->logDirName);
                conf->logDirName = create_string(conf->cwd);
            }
            else {
                p = (lex_text(l)[0] != '/')
                    ? create_format_string("%s/%s", conf->cwd, lex_text(l))
                    : create_string(lex_text(l));
                destroy_string(conf->logDirName);
                conf->logDirName = p;
            }
            break;

        case SERVER_CONF_LOGFILE:
            if (lex_next(l) != '=') {
                snprintf(err, sizeof(err),
                    "expected '=' after %s keyword", tokstr);
            }
            else if ((lex_next(l) != LEX_STR)
                    || is_empty_string(lex_text(l))) {
                snprintf(err, sizeof(err),
                    "expected STRING for %s value", tokstr);
            }
            else {
                destroy_string(conf->logFileName);
                if ((lex_text(l)[0] != '/') && (conf->logDirName)) {
                    conf->logFileName = create_format_string("%s/%s",
                        conf->logDirName, lex_text(l));
                }
                else {
                    conf->logFileName = create_string(lex_text(l));
                }
                if ((p = strrchr(conf->logFileName, ','))) {
                    *p++ = '\0';
                    if ((n = lookup_syslog_priority(p)) < 0) {
                        snprintf(err, sizeof(err),
                            "invalid %s priority \"%s\"", tokstr, p);
                    }
                    else {
                        conf->logFileLevel = n;
                    }
                }
            }
            break;

        case SERVER_CONF_LOOPBACK:
            if (lex_next(l) != '=') {
                snprintf(err, sizeof(err),
                    "expected '=' after %s keyword", tokstr);
            }
            else if (lex_next(l) == SERVER_CONF_ON) {
                conf->enableLoopBack = 1;
            }
            else if (lex_prev(l) == SERVER_CONF_OFF) {
                conf->enableLoopBack = 0;
            }
            else {
                snprintf(err, sizeof(err),
                    "expected ON or OFF for %s value", tokstr);
            }
            break;

        case SERVER_CONF_NOFILE:
            if (lex_next(l) != '=') {
                snprintf(err, sizeof(err),
                    "expected '=' after %s keyword", tokstr);
            }
            else if (lex_next(l) != LEX_INT) {
                snprintf(err, sizeof(err),
                    "expected INTEGER for %s value", tokstr);
            }
            else {
                n = atoi(lex_text(l));
                conf->numOpenFiles = n;
            }
            break;

        case SERVER_CONF_PIDFILE:
            if (lex_next(l) != '=') {
                snprintf(err, sizeof(err),
                    "expected '=' after %s keyword", tokstr);
            }
            else if ((lex_next(l) != LEX_STR)
                    || is_empty_string(lex_text(l))) {
                snprintf(err, sizeof(err),
                    "expected STRING for %s value", tokstr);
            }
            else if (!isPidFileNameSet) {
                destroy_string(conf->pidFileName);
                if (lex_text(l)[0] != '/') {
                    conf->pidFileName = create_format_string("%s/%s",
                        conf->cwd, lex_text(l));
                }
                else {
                    conf->pidFileName = create_string(lex_text(l));
                }
            }
            break;

        case SERVER_CONF_PORT:
            if (lex_next(l) != '=') {
                snprintf(err, sizeof(err),
                    "expected '=' after %s keyword", tokstr);
            }
            else if (lex_next(l) != LEX_INT) {
                snprintf(err, sizeof(err),
                    "expected INTEGER for %s value", tokstr);
            }
            else if ((n = atoi(lex_text(l))) <= 0) {
                snprintf(err, sizeof(err),
                    "invalid %s value %d", tokstr, n);
            }
            else if (!isPortSet) {
                conf->port = n;
            }
            break;

        case SERVER_CONF_RESETCMD:
            if (lex_next(l) != '=') {
                snprintf(err, sizeof(err),
                    "expected '=' after %s keyword", tokstr);
            }
            else if ((lex_next(l) != LEX_STR)
                    || is_empty_string(lex_text(l))) {
                snprintf(err, sizeof(err),
                    "expected STRING for %s value", tokstr);
            }
#ifdef DO_CONF_ESCAPE_ERROR
            else if (strchr(lex_text(l), DEPRECATED_CONF_ESCAPE)) {
                snprintf(err, sizeof(err),
                    "ignoring %s %s value with deprecated '%c' -- use '%%N'",
                    directive, tokstr, DEPRECATED_CONF_ESCAPE);
            }
#endif /* DO_CONF_ESCAPE_ERROR */
            else {
                destroy_string(conf->resetCmd);
                conf->resetCmd = create_string(lex_text(l));
            }
            break;

        case SERVER_CONF_SYSLOG:
            if (lex_next(l) != '=') {
                snprintf(err, sizeof(err),
                    "expected '=' after %s keyword", tokstr);
            }
            else if ((lex_next(l) != LEX_STR)
                    || is_empty_string(lex_text(l))) {
                snprintf(err, sizeof(err),
                    "expected STRING for %s value", tokstr);
            }
            else if ((n = lookup_syslog_facility(lex_text(l))) < 0) {
                snprintf(err, sizeof(err),
                    "invalid %s facility \"%s\"", tokstr, lex_text(l));
            }
            else {
                conf->syslogFacility = n;
            }
            break;

        case SERVER_CONF_TCPWRAPPERS:
#if ! WITH_TCP_WRAPPERS
            snprintf(err, sizeof(err),
                "%s keyword requires compile-time support", tokstr);
#else /* WITH_TCP_WRAPPERS */
            if (lex_next(l) != '=') {
                snprintf(err, sizeof(err),
                    "expected '=' after %s keyword", tokstr);
            }
            else if (lex_next(l) == SERVER_CONF_ON) {
                conf->enableTCPWrap = 1;
            }
            else if (lex_prev(l) == SERVER_CONF_OFF) {
                conf->enableTCPWrap = 0;
            }
            else {
                snprintf(err, sizeof(err),
                    "expected ON or OFF for %s value", tokstr);
            }
#endif /* WITH_TCP_WRAPPERS */
            break;

        case SERVER_CONF_TIMESTAMP:
            if (lex_next(l) != '=') {
                snprintf(err, sizeof(err),
                    "expected '=' after %s keyword", tokstr);
            }
            else if (lex_next(l) != LEX_INT) {
                snprintf(err, sizeof(err),
                    "expected INTEGER for %s value", tokstr);
            }
            else if ((n = atoi(lex_text(l))) < 0) {
                snprintf(err, sizeof(err),
                    "invalid %s value %d", tokstr, n);
            }
            else {
                conf->tStampMinutes = n;
                if ((lex_next(l) == LEX_EOF) || (lex_prev(l) == LEX_EOL)) {
                    ; /* no-op */
                }
                else if (lex_prev(l) == LEX_STR) {
                    if (lex_text(l)[1] != '\0') {
                        conf->tStampMinutes = -1;
                    }
                    else if (lex_text(l)[0] == 'm' || lex_text(l)[0] == 'M') {
                        ; /* no-op */
                    }
                    else if (lex_text(l)[0] == 'h' || lex_text(l)[0] == 'H') {
                        conf->tStampMinutes *= 60;
                    }
                    else if (lex_text(l)[0] == 'd' || lex_text(l)[0] == 'D') {
                        conf->tStampMinutes *= 60 * 24;
                    }
                    else {
                        conf->tStampMinutes = -1;
                    }
                }
                else {
                    conf->tStampMinutes = -1;
                }

                if (conf->tStampMinutes < 0) {
                    conf->tStampMinutes = 0;
                    snprintf(err, sizeof(err),
                        "expected (m|d|h) qualifier for %s value", tokstr);
                }
            }
            break;

        case LEX_EOF:
        case LEX_EOL:
            done = 1;
            break;

        case LEX_ERR:
            snprintf(err, sizeof(err), "unmatched quote");
            break;

        default:
            snprintf(err, sizeof(err), "unrecognized token '%s'", lex_text(l));
            break;
        }
    }

    if (*err) {
        log_msg(LOG_ERR, "CONFIG[%s:%d]: %s",
            conf->confFileName, lex_line(l), err);
        while (lex_prev(l) != LEX_EOL && lex_prev(l) != LEX_EOF) {
            (void) lex_next(l);
        }
    }
    return;
}


static int read_pidfile(const char *pidfile)
{
/*  Reads the PID from the specified pidfile.
 *  Returns the PID on success, or 0 if no pidfile is found.
 */
    FILE *fp;
    int n;
    int pid = 0;

    if (!pidfile) {
        return (0);
    }
    assert(pidfile[0] == '/');

    /*  FIXME: Ensure pathname is secure before trusting pid from pidfile.
     */
    if (!(fp = fopen(pidfile, "r"))) {
        return(0);
    }
    if ((n = fscanf(fp, "%d", &pid)) == EOF) {
        log_msg(LOG_WARNING, "Unable to read from pidfile \"%s\"", pidfile);
    }
    else if (n != 1) {
        log_msg(LOG_WARNING, "Unable to obtain pid from pidfile \"%s\"",
            pidfile);
    }
    if (fclose(fp) == EOF) {
        log_msg(LOG_WARNING, "Unable to close pidfile \"%s\"", pidfile);
    }
    return(((n == 1) && (pid > 1)) ? pid : 0);
}


static int write_pidfile(const char *pidfile)
{
/*  Creates the specified pidfile.
 *  Returns 0 on success, or -1 on error.
 *
 *  The pidfile must be created after daemonize has finished forking.
 *  The pidfile must be specified with an absolute pathname; o/w, the
 *    unlink() call at exit will fail because the daemon has chdir()'d.
 */
    mode_t  mask;
    char    dirname[PATH_MAX];
    FILE   *fp;

    if (pidfile == NULL) {
        return(0);
    }
    if (pidfile[0] != '/') {
        log_msg(0, "Unable to create relative-path pidfile \"%s\"", pidfile);
        return(-1);
    }
    (void) unlink(pidfile);
    /*
     *  Protect pidfile against unauthorized writes by removing
     *    group+other write-access from current mask.
     */
    mask = umask(0);
    umask(mask | 022);
    /*
     *  Create intermediate directories.
     */
    if (get_dir_name(pidfile, dirname, sizeof(dirname))) {
        (void) create_dirs(dirname);
    }
    /*  Open the pidfile.
     */
    fp = fopen(pidfile, "w");
    umask(mask);

    if (!fp) {
        log_msg(LOG_ERR, "Unable to open pidfile \"%s\": %s",
            pidfile, strerror(errno));
    }
    else if (fprintf(fp, "%d\n", (int) getpid()) == EOF) {
        log_msg(LOG_ERR, "Unable to write to pidfile \"%s\": %s",
            pidfile, strerror(errno));
        (void) fclose(fp);
    }
    else if (fclose(fp) == EOF) {
        log_msg(LOG_ERR, "Unable to close pidfile \"%s\": %s",
            pidfile, strerror(errno));
    }
    else {
        return(0);
    }
    (void) unlink(pidfile);
    return(-1);
}


static int lookup_syslog_priority(const char *priority)
{
/*  Returns the numeric id associated with the specified syslog priority,
 *    or -1 if no match is found.
 */
    tag_t *t;

    assert(priority != NULL);

    while (*priority && isspace((int) *priority)) {
        priority++;
    }
    for (t=logPriorities; t->key; t++) {
        if (!strcasecmp(t->key, priority)) {
            return(t->val);
        }
    }
    return(-1);
}


static int lookup_syslog_facility(const char *facility)
{
/*  Returns the numeric id associated with the specified syslog facility,
 *    or -1 if no match is found.
 */
    tag_t *t;

    assert(facility != NULL);

    while (*facility && isspace((int) *facility)) {
        facility++;
    }
    for (t=logFacilities; t->key; t++) {
        if (!strcasecmp(t->key, facility)) {
            return(t->val);
        }
    }
    return(-1);
}