summaryrefslogtreecommitdiffstats
path: root/watchfrr/watchfrr.c
blob: 228b7ff363a69eb3d97ea365a1f22fb646da963b (plain)
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
// SPDX-License-Identifier: GPL-2.0-or-later
/*
 * Monitor status of frr daemons and restart if necessary.
 *
 * Copyright (C) 2004  Andrew J. Schorr
 */

#include <zebra.h>
#include "frrevent.h"
#include <log.h>
#include <network.h>
#include <sigevent.h>
#include <lib/version.h>
#include "command.h"
#include "libfrr.h"
#include "lib_errors.h"
#include "zlog_targets.h"
#include "network.h"
#include "printfrr.h"

#include <getopt.h>
#include <sys/un.h>
#include <sys/wait.h>
#include <memory.h>
#include <systemd.h>

#include "watchfrr.h"
#include "watchfrr_errors.h"

#ifndef MIN
#define MIN(X,Y) (((X) <= (Y)) ? (X) : (Y))
#endif

/* Macros to help randomize timers. */
#define JITTER(X) ((frr_weak_random() % ((X)+1))-((X)/2))
#define FUZZY(X) ((X)+JITTER((X)/20))

#define DEFAULT_PERIOD		5
#define DEFAULT_TIMEOUT		90
#define DEFAULT_RESTART_TIMEOUT	20
#define DEFAULT_LOGLEVEL	LOG_INFO
#define DEFAULT_MIN_RESTART	60
#define DEFAULT_MAX_RESTART	600
#define DEFAULT_OPERATIONAL_TIMEOUT 60

#define DEFAULT_RESTART_CMD	WATCHFRR_SH_PATH " restart %s"
#define DEFAULT_START_CMD	WATCHFRR_SH_PATH " start %s"
#define DEFAULT_STOP_CMD	WATCHFRR_SH_PATH " stop %s"

#define PING_TOKEN	"PING"

DEFINE_MGROUP(WATCHFRR, "watchfrr");
DEFINE_MTYPE_STATIC(WATCHFRR, WATCHFRR_DAEMON, "watchfrr daemon entry");

/* Needs to be global, referenced somewhere inside libfrr. */
struct event_loop *master;

static bool watch_only = false;
const char *pathspace;

enum restart_phase {
	PHASE_NONE = 0,
	PHASE_INIT,
	PHASE_STOPS_PENDING,
	PHASE_WAITING_DOWN,
	PHASE_ZEBRA_RESTART_PENDING,
	PHASE_WAITING_ZEBRA_UP
};

static const char *const phase_str[] = {
	"Idle",
	"Startup",
	"Stop jobs running",
	"Waiting for other daemons to come down",
	"Zebra restart job running",
	"Waiting for zebra to come up",
	"Start jobs running",
};

#define PHASE_TIMEOUT (3*gs.restart_timeout)
#define STARTUP_TIMEOUT	55 * 1000

struct restart_info {
	const char *name;
	const char *what;
	pid_t pid;
	struct timeval time;
	long interval;
	struct event *t_kill;
	int kills;
};

static struct global_state {
	enum restart_phase phase;
	struct event *t_phase_hanging;
	struct event *t_startup_timeout;
	struct event *t_operational;
	const char *vtydir;
	long period;
	long timeout;
	long restart_timeout;
	bool reading_configuration;
	long min_restart_interval;
	long max_restart_interval;
	long operational_timeout;
	struct daemon *daemons;
	const char *restart_command;
	const char *start_command;
	const char *stop_command;
	struct restart_info restart;
	int loglevel;
	struct daemon *special; /* points to zebra when doing phased restart */
	int numdaemons;
	int numpids;
	int numdown; /* # of daemons that are not UP or UNRESPONSIVE */
} gs = {
	.phase = PHASE_INIT,
	.vtydir = frr_vtydir,
	.period = 1000 * DEFAULT_PERIOD,
	.timeout = DEFAULT_TIMEOUT,
	.restart_timeout = DEFAULT_RESTART_TIMEOUT,
	.loglevel = DEFAULT_LOGLEVEL,
	.min_restart_interval = DEFAULT_MIN_RESTART,
	.max_restart_interval = DEFAULT_MAX_RESTART,
	.operational_timeout = DEFAULT_OPERATIONAL_TIMEOUT,
	.restart_command = DEFAULT_RESTART_CMD,
	.start_command = DEFAULT_START_CMD,
	.stop_command = DEFAULT_STOP_CMD,
};

enum daemon_state {
	DAEMON_INIT,
	DAEMON_DOWN,
	DAEMON_CONNECTING,
	DAEMON_UP,
	DAEMON_UNRESPONSIVE
};

#define IS_UP(DMN)                                                             \
	(((DMN)->state == DAEMON_UP) || ((DMN)->state == DAEMON_UNRESPONSIVE))

static const char *const state_str[] = {
	"Init", "Down", "Connecting", "Up", "Unresponsive",
};

struct daemon {
	const char *name;
	enum daemon_state state;
	int fd;
	struct timeval echo_sent;
	unsigned int connect_tries;
	struct event *t_wakeup;
	struct event *t_read;
	struct event *t_write;
	struct daemon *next;
	struct restart_info restart;

	/*
	 * For a given daemon, if we've turned on ignore timeouts
	 * ignore the timeout value and assume everything is ok
	 * This is for daemon debugging w/ gdb after we have started
	 * FRR and realize we have something that needs to be looked
	 * at
	 */
	bool ignore_timeout;
};

#define OPTION_MINRESTART 2000
#define OPTION_MAXRESTART 2001
#define OPTION_DRY        2002
#define OPTION_NETNS      2003
#define OPTION_MAXOPERATIONAL 2004

static const struct option longopts[] = {
	{"daemon", no_argument, NULL, 'd'},
	{"statedir", required_argument, NULL, 'S'},
	{"loglevel", required_argument, NULL, 'l'},
	{"interval", required_argument, NULL, 'i'},
	{"timeout", required_argument, NULL, 't'},
	{"restart-timeout", required_argument, NULL, 'T'},
	{"restart", required_argument, NULL, 'r'},
	{"start-command", required_argument, NULL, 's'},
	{"kill-command", required_argument, NULL, 'k'},
	{"dry", no_argument, NULL, OPTION_DRY},
	{"min-restart-interval", required_argument, NULL, OPTION_MINRESTART},
	{"max-restart-interval", required_argument, NULL, OPTION_MAXRESTART},
	{"operational-timeout", required_argument, NULL, OPTION_MAXOPERATIONAL},
	{"pid-file", required_argument, NULL, 'p'},
	{"blank-string", required_argument, NULL, 'b'},
#ifdef GNU_LINUX
	{"netns", optional_argument, NULL, OPTION_NETNS},
#endif
	{"help", no_argument, NULL, 'h'},
	{"version", no_argument, NULL, 'v'},
	{NULL, 0, NULL, 0}};

static int try_connect(struct daemon *dmn);
static void wakeup_send_echo(struct event *t_wakeup);
static void try_restart(struct daemon *dmn);
static void phase_check(void);
static void restart_done(struct daemon *dmn);

static const char *progname;

void watchfrr_set_ignore_daemon(struct vty *vty, const char *dname, bool ignore)
{
	struct daemon *dmn;

	for (dmn = gs.daemons; dmn; dmn = dmn->next) {
		if (strncmp(dmn->name, dname, strlen(dmn->name)) == 0)
			break;
	}

	if (dmn) {
		dmn->ignore_timeout = ignore;
		vty_out(vty, "%s switching to %s\n", dmn->name,
			ignore ? "ignore" : "watch");
	} else
		vty_out(vty, "%s is not configured for running at the moment",
			dname);
}

static void printhelp(FILE *target)
{
	fprintf(target,
		"Usage : %s [OPTION...] <daemon name> ...\n\n\
Watchdog program to monitor status of frr daemons and try to restart\n\
them if they are down or unresponsive.  It determines whether a daemon is\n\
up based on whether it can connect to the daemon's vty unix stream socket.\n\
It then repeatedly sends echo commands over that socket to determine whether\n\
the daemon is responsive.  If the daemon crashes, we will receive an EOF\n\
on the socket connection and know immediately that the daemon is down.\n\n\
The daemons to be monitored should be listed on the command line.\n\n\
In order to avoid attempting to restart the daemons in a fast loop,\n\
the -m and -M options allow you to control the minimum delay between\n\
restart commands.  The minimum restart delay is recalculated each time\n\
a restart is attempted: if the time since the last restart attempt exceeds\n\
twice the -M value, then the restart delay is set to the -m value.\n\
Otherwise, the interval is doubled (but capped at the -M value).\n\n",
		progname);

	fprintf(target,
		"Options:\n\
-d, --daemon	Run in daemon mode.  In this mode, error messages are sent\n\
		to syslog instead of stdout.\n\
-S, --statedir	Set the vty socket directory (default is %s)\n\
-N, --pathspace	Insert prefix into config & socket paths\n"
#ifdef GNU_LINUX
"    --netns	Create and/or use Linux network namespace.  If no name is\n"
"		given, uses the value from `-N`.\n"
#endif
"-l, --loglevel	Set the logging level (default is %d).\n\
		The value should range from %d (LOG_EMERG) to %d (LOG_DEBUG),\n\
		but it can be set higher than %d if extra-verbose debugging\n\
		messages are desired.\n\
    --min-restart-interval\n\
		Set the minimum seconds to wait between invocations of daemon\n\
		restart commands (default is %d).\n\
    --max-restart-interval\n\
		Set the maximum seconds to wait between invocations of daemon\n\
		restart commands (default is %d).\n\
    --operational-timeout\n\
                Set the time before systemd is notified that we are considered\n\
                operational again after a daemon restart (default is %d).\n\
-i, --interval	Set the status polling interval in seconds (default is %d)\n\
-t, --timeout	Set the unresponsiveness timeout in seconds (default is %d)\n\
-T, --restart-timeout\n\
		Set the restart (kill) timeout in seconds (default is %d).\n\
		If any background jobs are still running after this much\n\
		time has elapsed, they will be killed.\n\
-r, --restart	Supply a Bourne shell command to use to restart a single\n\
		daemon.  The command string should include '%%s' where the\n\
		name of the daemon should be substituted.\n\
		(default: '%s')\n\
-s, --start-command\n\
		Supply a Bourne shell to command to use to start a single\n\
		daemon.  The command string should include '%%s' where the\n\
		name of the daemon should be substituted.\n\
		(default: '%s')\n\
-k, --kill-command\n\
		Supply a Bourne shell to command to use to stop a single\n\
		daemon.  The command string should include '%%s' where the\n\
		name of the daemon should be substituted.\n\
		(default: '%s')\n\
    --dry	Do not start or restart anything, just log.\n\
-p, --pid-file	Set process identifier file name\n\
		(default is %s/watchfrr.pid).\n\
-b, --blank-string\n\
		When the supplied argument string is found in any of the\n\
		various shell command arguments (-r, -s, or -k), replace\n\
		it with a space.  This is an ugly hack to circumvent problems\n\
		passing command-line arguments with embedded spaces.\n\
-v, --version	Print program version\n\
-h, --help	Display this help and exit\n",
		frr_vtydir, DEFAULT_LOGLEVEL, LOG_EMERG, LOG_DEBUG, LOG_DEBUG,
		DEFAULT_MIN_RESTART, DEFAULT_MAX_RESTART,
		DEFAULT_OPERATIONAL_TIMEOUT, DEFAULT_PERIOD, DEFAULT_TIMEOUT,
		DEFAULT_RESTART_TIMEOUT, DEFAULT_RESTART_CMD, DEFAULT_START_CMD,
		DEFAULT_STOP_CMD, frr_vtydir);
}

static pid_t run_background(char *shell_cmd)
{
	pid_t child;

	switch (child = fork()) {
	case -1:
		flog_err_sys(EC_LIB_SYSTEM_CALL,
			     "fork failed, cannot run command [%s]: %s",
			     shell_cmd, safe_strerror(errno));
		return -1;
	case 0:
		/* Child process. */
		/* Use separate process group so child processes can be killed
		 * easily. */
		if (setpgid(0, 0) < 0)
			zlog_warn("setpgid(0,0) failed: %s",
				  safe_strerror(errno));
		{
			char shell[] = "sh";
			char dashc[] = "-c";
			char *const argv[4] = {shell, dashc, shell_cmd, NULL};
			execv("/bin/sh", argv);
			flog_err_sys(EC_LIB_SYSTEM_CALL,
				     "execv(/bin/sh -c '%s') failed: %s",
				     shell_cmd, safe_strerror(errno));
			_exit(127);
		}
	default:
		/* Parent process: we will reap the child later. */
		zlog_info("Forked background command [pid %d]: %s", (int)child,
			  shell_cmd);
		return child;
	}
}

static struct timeval *time_elapsed(struct timeval *result,
				    const struct timeval *start_time)
{
	gettimeofday(result, NULL);
	result->tv_sec -= start_time->tv_sec;
	result->tv_usec -= start_time->tv_usec;
	while (result->tv_usec < 0) {
		result->tv_usec += 1000000L;
		result->tv_sec--;
	}
	return result;
}

static void restart_kill(struct event *t_kill)
{
	struct restart_info *restart = EVENT_ARG(t_kill);
	struct timeval delay;

	time_elapsed(&delay, &restart->time);

	if (gs.reading_configuration) {
		zlog_err(
			"%s %s child process appears to still be reading configuration, delaying for another %lu time",
			restart->what, restart->name, gs.restart_timeout);
		event_add_timer(master, restart_kill, restart,
				gs.restart_timeout, &restart->t_kill);
		return;
	}

	zlog_warn(
		"%s %s child process %d still running after %ld seconds, sending signal %d",
		restart->what, restart->name, (int)restart->pid,
		(long)delay.tv_sec, (restart->kills ? SIGKILL : SIGTERM));
	kill(-restart->pid, (restart->kills ? SIGKILL : SIGTERM));
	restart->kills++;
	event_add_timer(master, restart_kill, restart, gs.restart_timeout,
			&restart->t_kill);
}

static struct restart_info *find_child(pid_t child)
{
	struct daemon *dmn;
	if (gs.restart.pid == child)
		return &gs.restart;

	for (dmn = gs.daemons; dmn; dmn = dmn->next) {
		if (dmn->restart.pid == child)
			return &dmn->restart;
	}
	return NULL;
}

static void sigchild(void)
{
	pid_t child;
	int status;
	const char *name;
	const char *what;
	struct restart_info *restart;
	struct daemon *dmn;

	switch (child = waitpid(-1, &status, WNOHANG)) {
	case -1:
		flog_err_sys(EC_LIB_SYSTEM_CALL, "waitpid failed: %s",
			     safe_strerror(errno));
		return;
	case 0:
		zlog_warn("SIGCHLD received, but waitpid did not reap a child");
		return;
	}

	if (child == integrated_write_pid) {
		integrated_write_sigchld(status);
		return;
	}

	if ((restart = find_child(child)) != NULL) {
		name = restart->name;
		what = restart->what;
		restart->pid = 0;
		gs.numpids--;
		event_cancel(&restart->t_kill);

		/* Update restart time to reflect the time the command
		 * completed. */
		gettimeofday(&restart->time, NULL);
	} else {
		flog_err_sys(
			EC_LIB_SYSTEM_CALL,
			"waitpid returned status for an unknown child process %d",
			(int)child);
		name = "(unknown)";
		what = "background";
	}
	if (WIFSTOPPED(status))
		zlog_warn("%s %s process %d is stopped", what, name,
			  (int)child);
	else if (WIFSIGNALED(status))
		zlog_warn("%s %s process %d terminated due to signal %d", what,
			  name, (int)child, WTERMSIG(status));
	else if (WIFEXITED(status)) {
		if (WEXITSTATUS(status) != 0)
			zlog_warn(
				"%s %s process %d exited with non-zero status %d",
				what, name, (int)child, WEXITSTATUS(status));
		else {
			zlog_debug("%s %s process %d exited normally", what,
				   name, (int)child);

			if (restart && restart != &gs.restart) {
				dmn = container_of(restart, struct daemon,
						   restart);
				restart_done(dmn);
			} else if (restart)
				for (dmn = gs.daemons; dmn; dmn = dmn->next)
					restart_done(dmn);
		}
	} else
		flog_err_sys(
			EC_LIB_SYSTEM_CALL,
			"cannot interpret %s %s process %d wait status 0x%x",
			what, name, (int)child, status);
	phase_check();
}

static int run_job(struct restart_info *restart, const char *cmdtype,
		   const char *command, int force, int update_interval)
{
	struct timeval delay;

	if (gs.loglevel > LOG_DEBUG + 1)
		zlog_debug("attempting to %s %s", cmdtype, restart->name);

	if (restart->pid) {
		if (gs.loglevel > LOG_DEBUG + 1)
			zlog_debug(
				"cannot %s %s, previous pid %d still running",
				cmdtype, restart->name, (int)restart->pid);
		return -1;
	}

	char buffer[512];

	snprintf(buffer, sizeof(buffer), "restarting %s", restart->name);
	systemd_send_status(buffer);

	/* Note: time_elapsed test must come before the force test, since we
	   need
	   to make sure that delay is initialized for use below in updating the
	   restart interval. */
	if ((time_elapsed(&delay, &restart->time)->tv_sec < restart->interval)
	    && !force) {

		if (gs.loglevel > LOG_DEBUG + 1)
			zlog_debug(
				"postponing %s %s: elapsed time %ld < retry interval %ld",
				cmdtype, restart->name, (long)delay.tv_sec,
				restart->interval);
		return -1;
	}

	gettimeofday(&restart->time, NULL);
	restart->kills = 0;
	{
		char cmd[strlen(command) + strlen(restart->name) + 1];
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wformat-nonliteral"
		/* user supplied command string has a %s for the daemon name */
		snprintf(cmd, sizeof(cmd), command, restart->name);
#pragma GCC diagnostic pop
		if ((restart->pid = run_background(cmd)) > 0) {
			event_add_timer(master, restart_kill, restart,
					gs.restart_timeout, &restart->t_kill);
			restart->what = cmdtype;
			gs.numpids++;
		} else
			restart->pid = 0;
	}

	/* Calculate the new restart interval. */
	if (update_interval) {
		if (delay.tv_sec > 2 * gs.max_restart_interval)
			restart->interval = gs.min_restart_interval;
		else if ((restart->interval *= 2) > gs.max_restart_interval)
			restart->interval = gs.max_restart_interval;
		if (gs.loglevel > LOG_DEBUG + 1)
			zlog_debug("restart %s interval is now %ld",
				   restart->name, restart->interval);
	}
	return restart->pid;
}

#define SET_READ_HANDLER(DMN)                                                  \
	do {                                                                   \
		(DMN)->t_read = NULL;                                          \
		event_add_read(master, handle_read, (DMN), (DMN)->fd,          \
			       &(DMN)->t_read);                                \
	} while (0);

#define SET_WAKEUP_DOWN(DMN)                                                   \
	do {                                                                   \
		(DMN)->t_wakeup = NULL;                                        \
		event_add_timer_msec(master, wakeup_down, (DMN),               \
				     FUZZY(gs.period), &(DMN)->t_wakeup);      \
	} while (0);

#define SET_WAKEUP_UNRESPONSIVE(DMN)                                           \
	do {                                                                   \
		(DMN)->t_wakeup = NULL;                                        \
		event_add_timer_msec(master, wakeup_unresponsive, (DMN),       \
				     FUZZY(gs.period), &(DMN)->t_wakeup);      \
	} while (0);

#define SET_WAKEUP_ECHO(DMN)                                                   \
	do {                                                                   \
		(DMN)->t_wakeup = NULL;                                        \
		event_add_timer_msec(master, wakeup_send_echo, (DMN),          \
				     FUZZY(gs.period), &(DMN)->t_wakeup);      \
	} while (0);

static void wakeup_down(struct event *t_wakeup)
{
	struct daemon *dmn = EVENT_ARG(t_wakeup);

	dmn->t_wakeup = NULL;
	if (try_connect(dmn) < 0)
		SET_WAKEUP_DOWN(dmn);
	if ((dmn->connect_tries > 1) && (dmn->state != DAEMON_UP))
		try_restart(dmn);
}

static void wakeup_init(struct event *t_wakeup)
{
	struct daemon *dmn = EVENT_ARG(t_wakeup);

	dmn->t_wakeup = NULL;
	if (try_connect(dmn) < 0) {
		zlog_info(
			"%s state -> down : initial connection attempt failed",
			dmn->name);
		dmn->state = DAEMON_DOWN;
	}
	phase_check();
}

static void restart_done(struct daemon *dmn)
{
	if (dmn->state != DAEMON_DOWN) {
		zlog_warn(
			"Daemon: %s: is in %s state but expected it to be in DAEMON_DOWN state",
			dmn->name, state_str[dmn->state]);
		return;
	}
	EVENT_OFF(dmn->t_wakeup);

	if (try_connect(dmn) < 0)
		SET_WAKEUP_DOWN(dmn);
}

static void daemon_restarting_operational(struct event *thread)
{
	systemd_send_status("FRR Operational");
}

static void daemon_down(struct daemon *dmn, const char *why)
{
	if (dmn->ignore_timeout)
		return;

	if (IS_UP(dmn) || (dmn->state == DAEMON_INIT))
		flog_err(EC_WATCHFRR_CONNECTION, "%s state -> down : %s",
			 dmn->name, why);
	else if (gs.loglevel > LOG_DEBUG)
		zlog_debug("%s still down : %s", dmn->name, why);
	if (IS_UP(dmn))
		gs.numdown++;
	dmn->state = DAEMON_DOWN;
	if (dmn->fd >= 0) {
		close(dmn->fd);
		dmn->fd = -1;
	}
	EVENT_OFF(dmn->t_read);
	EVENT_OFF(dmn->t_write);
	EVENT_OFF(dmn->t_wakeup);
	if (try_connect(dmn) < 0)
		SET_WAKEUP_DOWN(dmn);

	systemd_send_status("FRR partially operational");
	phase_check();
}

static void handle_read(struct event *t_read)
{
	struct daemon *dmn = EVENT_ARG(t_read);
	static const char resp[sizeof(PING_TOKEN) + 4] = PING_TOKEN "\n";
	char buf[sizeof(resp) + 100];
	ssize_t rc;
	struct timeval delay;

	dmn->t_read = NULL;
	if ((rc = read(dmn->fd, buf, sizeof(buf))) < 0) {
		char why[100];

		if (ERRNO_IO_RETRY(errno)) {
			/* Pretend it never happened. */
			SET_READ_HANDLER(dmn);
			return;
		}
		snprintf(why, sizeof(why), "unexpected read error: %s",
			 safe_strerror(errno));
		daemon_down(dmn, why);
		return;
	}
	if (rc == 0) {
		daemon_down(dmn, "read returned EOF");
		return;
	}
	if (!dmn->echo_sent.tv_sec) {
		char why[sizeof(buf) + 100];
		snprintf(why, sizeof(why),
			 "unexpected read returns %d bytes: %.*s", (int)rc,
			 (int)rc, buf);
		daemon_down(dmn, why);
		return;
	}

	/* We are expecting an echo response: is there any chance that the
	   response would not be returned entirely in the first read?  That
	   seems inconceivable... */
	if ((rc != sizeof(resp)) || memcmp(buf, resp, sizeof(resp))) {
		char why[100 + sizeof(buf)];
		snprintf(why, sizeof(why),
			 "read returned bad echo response of %d bytes (expecting %u): %.*s",
			 (int)rc, (unsigned int)sizeof(resp), (int)rc, buf);
		daemon_down(dmn, why);
		return;
	}

	time_elapsed(&delay, &dmn->echo_sent);
	dmn->echo_sent.tv_sec = 0;
	if (dmn->state == DAEMON_UNRESPONSIVE) {
		if (delay.tv_sec < gs.timeout) {
			dmn->state = DAEMON_UP;
			zlog_warn(
				"%s state -> up : echo response received after %ld.%06ld seconds",
				dmn->name, (long)delay.tv_sec,
				(long)delay.tv_usec);
		} else
			zlog_warn(
				"%s: slow echo response finally received after %ld.%06ld seconds",
				dmn->name, (long)delay.tv_sec,
				(long)delay.tv_usec);
	} else if (gs.loglevel > LOG_DEBUG + 1)
		zlog_debug("%s: echo response received after %ld.%06ld seconds",
			   dmn->name, (long)delay.tv_sec, (long)delay.tv_usec);

	SET_READ_HANDLER(dmn);
	event_cancel(&dmn->t_wakeup);
	SET_WAKEUP_ECHO(dmn);
}

/*
 * Wait till we notice that all daemons are ready before
 * we send we are ready to systemd
 */
static void daemon_send_ready(int exitcode)
{
	FILE *fp;
	static int sent = 0;
	char started[1024];

	if (sent)
		return;

	if (exitcode == 0)
		zlog_notice("all daemons up, doing startup-complete notify");
	else if (gs.numdown < gs.numdaemons)
		flog_err(EC_WATCHFRR_CONNECTION,
			 "startup did not complete within timeout (%d/%d daemons running)",
			 gs.numdaemons - gs.numdown, gs.numdaemons);
	else {
		flog_err(EC_WATCHFRR_CONNECTION,
			 "all configured daemons failed to start -- exiting watchfrr");
		exit(exitcode);

	}

	frr_detach();

	snprintf(started, sizeof(started), "%s/%s", frr_vtydir,
		 "watchfrr.started");
	fp = fopen(started, "w");
	if (fp)
		fclose(fp);

	systemd_send_started(master);
	systemd_send_status("FRR Operational");
	sent = 1;
}

static void daemon_up(struct daemon *dmn, const char *why)
{
	dmn->state = DAEMON_UP;
	gs.numdown--;
	dmn->connect_tries = 0;
	zlog_notice("%s state -> up : %s", dmn->name, why);
	if (gs.numdown == 0) {
		daemon_send_ready(0);

		EVENT_OFF(gs.t_operational);

		event_add_timer(master, daemon_restarting_operational, NULL,
				gs.operational_timeout, &gs.t_operational);
	}

	SET_WAKEUP_ECHO(dmn);
	phase_check();
}

static void check_connect(struct event *t_write)
{
	struct daemon *dmn = EVENT_ARG(t_write);
	int sockerr;
	socklen_t reslen = sizeof(sockerr);

	dmn->t_write = NULL;
	if (getsockopt(dmn->fd, SOL_SOCKET, SO_ERROR, (char *)&sockerr, &reslen)
	    < 0) {
		zlog_warn("%s: check_connect: getsockopt failed: %s", dmn->name,
			  safe_strerror(errno));
		daemon_down(dmn,
			    "getsockopt failed checking connection success");
		return;
	}
	if ((reslen == sizeof(sockerr)) && sockerr) {
		char why[100];
		snprintf(
			why, sizeof(why),
			"getsockopt reports that connection attempt failed: %s",
			safe_strerror(sockerr));
		daemon_down(dmn, why);
		return;
	}

	daemon_up(dmn, "delayed connect succeeded");
}

static void wakeup_connect_hanging(struct event *t_wakeup)
{
	struct daemon *dmn = EVENT_ARG(t_wakeup);
	char why[100];

	dmn->t_wakeup = NULL;
	snprintf(why, sizeof(why),
		 "connection attempt timed out after %ld seconds", gs.timeout);
	daemon_down(dmn, why);
}

/* Making connection to protocol daemon. */
static int try_connect(struct daemon *dmn)
{
	int sock;
	struct sockaddr_un addr;
	socklen_t len;

	if (gs.loglevel > LOG_DEBUG + 1)
		zlog_debug("%s: attempting to connect", dmn->name);
	dmn->connect_tries++;

	memset(&addr, 0, sizeof(addr));
	addr.sun_family = AF_UNIX;
	snprintf(addr.sun_path, sizeof(addr.sun_path), "%s/%s.vty", gs.vtydir,
		 dmn->name);
#ifdef HAVE_STRUCT_SOCKADDR_UN_SUN_LEN
	len = addr.sun_len = SUN_LEN(&addr);
#else
	len = sizeof(addr.sun_family) + strlen(addr.sun_path);
#endif /* HAVE_STRUCT_SOCKADDR_UN_SUN_LEN */

	/* Quick check to see if we might succeed before we go to the trouble
	   of creating a socket. */
	if (access(addr.sun_path, W_OK) < 0) {
		if (errno != ENOENT)
			flog_err_sys(EC_LIB_SYSTEM_CALL,
				     "%s: access to socket %s denied: %s",
				     dmn->name, addr.sun_path,
				     safe_strerror(errno));
		return -1;
	}

	if ((sock = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
		flog_err_sys(EC_LIB_SOCKET, "%s(%s): cannot make socket: %s",
			     __func__, addr.sun_path, safe_strerror(errno));
		return -1;
	}

	if (set_nonblocking(sock) < 0 || set_cloexec(sock) < 0) {
		flog_err_sys(EC_LIB_SYSTEM_CALL,
			     "%s(%s): set_nonblocking/cloexec(%d) failed",
			     __func__, addr.sun_path, sock);
		close(sock);
		return -1;
	}

	if (connect(sock, (struct sockaddr *)&addr, len) < 0) {
		if ((errno != EINPROGRESS) && (errno != EWOULDBLOCK)) {
			if (gs.loglevel > LOG_DEBUG)
				zlog_debug("%s(%s): connect failed: %s",
					   __func__, addr.sun_path,
					   safe_strerror(errno));
			close(sock);
			return -1;
		}
		if (gs.loglevel > LOG_DEBUG)
			zlog_debug("%s: connection in progress", dmn->name);
		dmn->state = DAEMON_CONNECTING;
		dmn->fd = sock;
		event_add_write(master, check_connect, dmn, dmn->fd,
				&dmn->t_write);
		event_add_timer(master, wakeup_connect_hanging, dmn, gs.timeout,
				&dmn->t_wakeup);
		SET_READ_HANDLER(dmn);
		return 0;
	}

	dmn->fd = sock;
	SET_READ_HANDLER(dmn);
	daemon_up(dmn, "connect succeeded");
	return 1;
}

static void phase_hanging(struct event *t_hanging)
{
	gs.t_phase_hanging = NULL;
	flog_err(EC_WATCHFRR_CONNECTION,
		 "Phase [%s] hanging for %ld seconds, aborting phased restart",
		 phase_str[gs.phase], PHASE_TIMEOUT);
	gs.phase = PHASE_NONE;
}

static void set_phase(enum restart_phase new_phase)
{
	gs.phase = new_phase;
	event_cancel(&gs.t_phase_hanging);

	event_add_timer(master, phase_hanging, NULL, PHASE_TIMEOUT,
			&gs.t_phase_hanging);
}

static void phase_check(void)
{
	struct daemon *dmn;

	switch (gs.phase) {
	case PHASE_NONE:
		break;

	case PHASE_INIT:
		for (dmn = gs.daemons; dmn; dmn = dmn->next)
			if (dmn->state == DAEMON_INIT)
				return;

		/* startup complete, everything out of INIT */
		gs.phase = PHASE_NONE;
		for (dmn = gs.daemons; dmn; dmn = dmn->next)
			if (dmn->state == DAEMON_DOWN) {
				SET_WAKEUP_DOWN(dmn);
				try_restart(dmn);
			}
		break;
	case PHASE_STOPS_PENDING:
		if (gs.numpids)
			break;
		zlog_info(
			"Phased restart: all routing daemon stop jobs have completed.");
		set_phase(PHASE_WAITING_DOWN);

	/*FALLTHRU*/
	case PHASE_WAITING_DOWN:
		if (gs.numdown + IS_UP(gs.special) < gs.numdaemons)
			break;
		systemd_send_status("Phased Restart");
		zlog_info("Phased restart: all routing daemons now down.");
		run_job(&gs.special->restart, "restart", gs.restart_command, 1,
			1);
		set_phase(PHASE_ZEBRA_RESTART_PENDING);

	/*FALLTHRU*/
	case PHASE_ZEBRA_RESTART_PENDING:
		if (gs.special->restart.pid)
			break;
		systemd_send_status("Zebra Restarting");
		zlog_info("Phased restart: %s restart job completed.",
			  gs.special->name);
		set_phase(PHASE_WAITING_ZEBRA_UP);

	/*FALLTHRU*/
	case PHASE_WAITING_ZEBRA_UP:
		if (!IS_UP(gs.special))
			break;
		zlog_info("Phased restart: %s is now up.", gs.special->name);
		for (dmn = gs.daemons; dmn; dmn = dmn->next) {
			if (dmn != gs.special)
				run_job(&dmn->restart, "start",
					gs.start_command, 1, 0);
		}
		gs.phase = PHASE_NONE;
		EVENT_OFF(gs.t_phase_hanging);
		zlog_notice("Phased global restart has completed.");
		break;
	}
}

static void try_restart(struct daemon *dmn)
{
	if (watch_only)
		return;

	if (dmn != gs.special) {
		if ((gs.special->state == DAEMON_UP)
		    && (gs.phase == PHASE_NONE))
			run_job(&dmn->restart, "restart", gs.restart_command, 0,
				1);
		else
			zlog_debug(
				"%s: postponing restart attempt because master %s daemon not up [%s], or phased restart in progress",
				dmn->name, gs.special->name,
				state_str[gs.special->state]);
		return;
	}

	if ((gs.phase != PHASE_NONE) || gs.numpids) {
		if (gs.loglevel > LOG_DEBUG + 1)
			zlog_debug(
				"postponing phased global restart: restart already in progress [%s], or outstanding child processes [%d]",
				phase_str[gs.phase], gs.numpids);
		return;
	}
	/* Is it too soon for a restart? */
	{
		struct timeval delay;
		if (time_elapsed(&delay, &gs.special->restart.time)->tv_sec
		    < gs.special->restart.interval) {
			if (gs.loglevel > LOG_DEBUG + 1)
				zlog_debug(
					"postponing phased global restart: elapsed time %ld < retry interval %ld",
					(long)delay.tv_sec,
					gs.special->restart.interval);
			return;
		}
	}
	run_job(&gs.restart, "restart", gs.restart_command, 0, 1);
}

static void wakeup_unresponsive(struct event *t_wakeup)
{
	struct daemon *dmn = EVENT_ARG(t_wakeup);

	dmn->t_wakeup = NULL;
	if (dmn->state != DAEMON_UNRESPONSIVE)
		flog_err(EC_WATCHFRR_CONNECTION,
			 "%s: no longer unresponsive (now %s), wakeup should have been cancelled!",
			 dmn->name, state_str[dmn->state]);
	else {
		SET_WAKEUP_UNRESPONSIVE(dmn);
		try_restart(dmn);
	}
}

static void wakeup_no_answer(struct event *t_wakeup)
{
	struct daemon *dmn = EVENT_ARG(t_wakeup);

	dmn->t_wakeup = NULL;
	dmn->state = DAEMON_UNRESPONSIVE;
	if (dmn->ignore_timeout)
		return;
	flog_err(EC_WATCHFRR_CONNECTION,
		 "%s state -> unresponsive : no response yet to ping sent %ld seconds ago",
		 dmn->name, gs.timeout);
	SET_WAKEUP_UNRESPONSIVE(dmn);
	try_restart(dmn);
}

static void wakeup_send_echo(struct event *t_wakeup)
{
	static const char echocmd[] = "echo " PING_TOKEN;
	ssize_t rc;
	struct daemon *dmn = EVENT_ARG(t_wakeup);

	dmn->t_wakeup = NULL;
	if (((rc = write(dmn->fd, echocmd, sizeof(echocmd))) < 0)
	    || ((size_t)rc != sizeof(echocmd))) {
		char why[100 + sizeof(echocmd)];
		snprintf(why, sizeof(why),
			 "write '%s' returned %d instead of %u", echocmd,
			 (int)rc, (unsigned int)sizeof(echocmd));
		daemon_down(dmn, why);
	} else {
		gettimeofday(&dmn->echo_sent, NULL);
		event_add_timer(master, wakeup_no_answer, dmn, gs.timeout,
				&dmn->t_wakeup);
	}
}

bool check_all_up(void)
{
	struct daemon *dmn;

	for (dmn = gs.daemons; dmn; dmn = dmn->next)
		if (dmn->state != DAEMON_UP)
			return false;
	return true;
}

void watchfrr_status(struct vty *vty)
{
	struct daemon *dmn;
	struct timeval delay;

	vty_out(vty, "watchfrr global phase: %s\n", phase_str[gs.phase]);
	vty_out(vty, " Restart Command: %pSQq\n", gs.restart_command);
	vty_out(vty, " Start Command: %pSQq\n", gs.start_command);
	vty_out(vty, " Stop Command: %pSQq\n", gs.stop_command);
	vty_out(vty, " Min Restart Interval: %ld\n", gs.min_restart_interval);
	vty_out(vty, " Max Restart Interval: %ld\n", gs.max_restart_interval);
	vty_out(vty, " Restart Timeout: %ld\n", gs.restart_timeout);
	vty_out(vty, " Reading Configuration: %s\n",
		gs.reading_configuration ? "yes" : "no");
	if (gs.restart.pid)
		vty_out(vty, "    global restart running, pid %ld\n",
			(long)gs.restart.pid);

	for (dmn = gs.daemons; dmn; dmn = dmn->next) {
		vty_out(vty, "  %-20s %s%s", dmn->name, state_str[dmn->state],
			dmn->ignore_timeout ? "/Ignoring Timeout\n" : "\n");
		if (dmn->restart.pid)
			vty_out(vty, "      restart running, pid %ld\n",
				(long)dmn->restart.pid);
		else if (dmn->state == DAEMON_DOWN &&
			time_elapsed(&delay, &dmn->restart.time)->tv_sec
				< dmn->restart.interval)
			vty_out(vty, "      restarting in %jd seconds (%jds backoff interval)\n",
				(intmax_t)dmn->restart.interval
					- (intmax_t)delay.tv_sec,
				(intmax_t)dmn->restart.interval);
	}
}

static void sigint(void)
{
	zlog_notice("Terminating on signal");
	systemd_send_stopping();
	exit(0);
}

static int valid_command(const char *cmd)
{
	char *p;

	if (cmd == NULL)
		return 0;

	return ((p = strchr(cmd, '%')) != NULL) && (*(p + 1) == 's')
	       && !strchr(p + 1, '%');
}

/* This is an ugly hack to circumvent problems with passing command-line
   arguments that contain spaces.  The fix is to use a configuration file. */
static char *translate_blanks(const char *cmd, const char *blankstr)
{
	char *res;
	char *p;
	size_t bslen = strlen(blankstr);

	if (!(res = strdup(cmd))) {
		perror("strdup");
		exit(1);
	}
	while ((p = strstr(res, blankstr)) != NULL) {
		*p = ' ';
		if (bslen != 1)
			memmove(p + 1, p + bslen, strlen(p + bslen) + 1);
	}
	return res;
}

static void startup_timeout(struct event *t_wakeup)
{
	daemon_send_ready(1);
}

#ifdef GNU_LINUX

#include <sys/mount.h>
#include <sched.h>

#define NETNS_RUN_DIR "/var/run/netns"

static void netns_create(int dirfd, const char *nsname)
{
	/* make /var/run/netns shared between mount namespaces
	 * just like iproute2 sets it up
	 */
	if (mount("", NETNS_RUN_DIR, "none", MS_SHARED | MS_REC, NULL)) {
		if (errno != EINVAL) {
			perror("mount");
			exit(1);
		}

		if (mount(NETNS_RUN_DIR, NETNS_RUN_DIR, "none",
			  MS_BIND | MS_REC, NULL)) {
			perror("mount");
			exit(1);
		}

		if (mount("", NETNS_RUN_DIR, "none", MS_SHARED | MS_REC,
			  NULL)) {
			perror("mount");
			exit(1);
		}
	}

	/* need an empty file to mount on top of */
	int nsfd = openat(dirfd, nsname, O_CREAT | O_RDONLY | O_EXCL, 0);

	if (nsfd < 0) {
		fprintf(stderr, "failed to create \"%s/%s\": %s\n",
			NETNS_RUN_DIR, nsname, strerror(errno));
		exit(1);
	}
	close(nsfd);

	if (unshare(CLONE_NEWNET)) {
		perror("unshare");
		unlinkat(dirfd, nsname, 0);
		exit(1);
	}

	char *dstpath = asprintfrr(MTYPE_TMP, "%s/%s", NETNS_RUN_DIR, nsname);

	/* bind-mount so the namespace has a name and is persistent */
	if (mount("/proc/self/ns/net", dstpath, "none", MS_BIND, NULL) < 0) {
		fprintf(stderr, "failed to bind-mount netns to \"%s\": %s\n",
			dstpath, strerror(errno));
		unlinkat(dirfd, nsname, 0);
		exit(1);
	}

	XFREE(MTYPE_TMP, dstpath);
}

static void netns_setup(const char *nsname)
{
	int dirfd, nsfd;

	dirfd = open(NETNS_RUN_DIR, O_DIRECTORY | O_RDONLY);
	if (dirfd < 0) {
		if (errno == ENOTDIR) {
			fprintf(stderr, "error: \"%s\" is not a directory!\n",
				NETNS_RUN_DIR);
			exit(1);
		} else if (errno == ENOENT) {
			if (mkdir(NETNS_RUN_DIR, 0755)) {
				fprintf(stderr, "error: \"%s\": mkdir: %s\n",
					NETNS_RUN_DIR, strerror(errno));
				exit(1);
			}
			dirfd = open(NETNS_RUN_DIR, O_DIRECTORY | O_RDONLY);
			if (dirfd < 0) {
				fprintf(stderr, "error: \"%s\": opendir: %s\n",
					NETNS_RUN_DIR, strerror(errno));
				exit(1);
			}
		} else {
			fprintf(stderr, "error: \"%s\": %s\n",
				NETNS_RUN_DIR, strerror(errno));
			exit(1);
		}
	}

	nsfd = openat(dirfd, nsname, O_RDONLY);
	if (nsfd < 0 && errno != ENOENT) {
		fprintf(stderr, "error: \"%s/%s\": %s\n",
			NETNS_RUN_DIR, nsname, strerror(errno));
		exit(1);
	}
	if (nsfd < 0)
		netns_create(dirfd, nsname);
	else {
		if (setns(nsfd, CLONE_NEWNET)) {
			perror("setns");
			exit(1);
		}
		close(nsfd);
	}
	close(dirfd);

	/* make sure loopback is up... weird things happen otherwise.
	 * ioctl is perfectly fine for this, don't need netlink...
	 */
	int sockfd;
	struct ifreq ifr = { };

	strlcpy(ifr.ifr_name, "lo", sizeof(ifr.ifr_name));

	sockfd = socket(AF_INET, SOCK_DGRAM, 0);
	if (sockfd < 0) {
		perror("socket");
		exit(1);
	}
	if (ioctl(sockfd, SIOCGIFFLAGS, &ifr)) {
		perror("ioctl(SIOCGIFFLAGS, \"lo\")");
		exit(1);
	}
	if (!(ifr.ifr_flags & IFF_UP)) {
		ifr.ifr_flags |= IFF_UP;
		if (ioctl(sockfd, SIOCSIFFLAGS, &ifr)) {
			perror("ioctl(SIOCSIFFLAGS, \"lo\")");
			exit(1);
		}
	}
	close(sockfd);
}

#else /* !GNU_LINUX */

static void netns_setup(const char *nsname)
{
	fprintf(stderr, "network namespaces are only available on Linux\n");
	exit(1);
}
#endif

static void watchfrr_start_config(void)
{
	gs.reading_configuration = true;
}

static void watchfrr_end_config(void)
{
	gs.reading_configuration = false;
}

static void watchfrr_init(int argc, char **argv)
{
	const char *special = "zebra";
	int i;
	struct daemon *dmn, **add = &gs.daemons;
	char alldaemons[512] = "", *p = alldaemons;

	event_add_timer_msec(master, startup_timeout, NULL, STARTUP_TIMEOUT,
			     &gs.t_startup_timeout);

	for (i = optind; i < argc; i++) {
		dmn = XCALLOC(MTYPE_WATCHFRR_DAEMON, sizeof(*dmn));

		dmn->name = dmn->restart.name = argv[i];
		dmn->state = DAEMON_INIT;
		gs.numdaemons++;
		gs.numdown++;
		dmn->fd = -1;
		event_add_timer_msec(master, wakeup_init, dmn, 0,
				     &dmn->t_wakeup);
		dmn->restart.interval = gs.min_restart_interval;
		*add = dmn;
		add = &dmn->next;

		if (!strcmp(dmn->name, special))
			gs.special = dmn;
	}

	if (!gs.daemons) {
		fprintf(stderr,
			"Must specify one or more daemons to monitor.\n\n");
		frr_help_exit(1);
	}
	if (!watch_only && !gs.special) {
		fprintf(stderr, "\"%s\" daemon must be in daemon lists\n\n",
			special);
		frr_help_exit(1);
	}

	for (dmn = gs.daemons; dmn; dmn = dmn->next) {
		snprintf(p, alldaemons + sizeof(alldaemons) - p, "%s%s",
			 (p == alldaemons) ? "" : " ", dmn->name);
		p += strlen(p);
	}
	zlog_notice("%s %s watching [%s]%s", progname, FRR_VERSION, alldaemons,
		    watch_only ? ", monitor mode" : "");
}

struct zebra_privs_t watchfrr_privs = {
#ifdef VTY_GROUP
	.vty_group = VTY_GROUP,
#endif
};

static struct frr_signal_t watchfrr_signals[] = {
	{
		.signal = SIGINT,
		.handler = sigint,
	},
	{
		.signal = SIGTERM,
		.handler = sigint,
	},
	{
		.signal = SIGCHLD,
		.handler = sigchild,
	},
};

FRR_DAEMON_INFO(watchfrr, WATCHFRR,
		.flags = FRR_NO_PRIVSEP | FRR_NO_TCPVTY | FRR_LIMITED_CLI
			 | FRR_NO_CFG_PID_DRY | FRR_NO_ZCLIENT
			 | FRR_DETACH_LATER,

		.printhelp = printhelp,
		.copyright = "Copyright 2004 Andrew J. Schorr",

		.signals = watchfrr_signals,
		.n_signals = array_size(watchfrr_signals),

		.privs = &watchfrr_privs,
);

#define DEPRECATED_OPTIONS "aAezR:"

int main(int argc, char **argv)
{
	int opt;
	const char *blankstr = NULL;
	const char *netns = NULL;
	bool netns_en = false;

	frr_preinit(&watchfrr_di, argc, argv);
	progname = watchfrr_di.progname;

	frr_opt_add("b:di:k:l:N:p:r:S:s:t:T:" DEPRECATED_OPTIONS, longopts, "");

	gs.restart.name = "all";
	while ((opt = frr_getopt(argc, argv, NULL)) != EOF) {
		if (opt && opt < 128 && strchr(DEPRECATED_OPTIONS, opt)) {
			fprintf(stderr,
				"The -%c option no longer exists.\n"
				"Please refer to the watchfrr(8) man page.\n",
				opt);
			exit(1);
		}

		switch (opt) {
		case 0:
			break;
		case 'b':
			blankstr = optarg;
			break;
		case OPTION_DRY:
			watch_only = true;
			break;
		case 'k':
			if (!valid_command(optarg)) {
				fprintf(stderr,
					"Invalid kill command, must contain '%%s': %s\n",
					optarg);
				frr_help_exit(1);
			}
			gs.stop_command = optarg;
			break;
		case 'l': {
			char garbage[3];
			if ((sscanf(optarg, "%d%1s", &gs.loglevel, garbage)
			     != 1)
			    || (gs.loglevel < LOG_EMERG)) {
				fprintf(stderr,
					"Invalid loglevel argument: %s\n",
					optarg);
				frr_help_exit(1);
			}
		} break;
		case OPTION_MINRESTART: {
			char garbage[3];
			if ((sscanf(optarg, "%ld%1s", &gs.min_restart_interval,
				    garbage)
			     != 1)
			    || (gs.min_restart_interval < 0)) {
				fprintf(stderr,
					"Invalid min_restart_interval argument: %s\n",
					optarg);
				frr_help_exit(1);
			}
		} break;
		case OPTION_MAXRESTART: {
			char garbage[3];
			if ((sscanf(optarg, "%ld%1s", &gs.max_restart_interval,
				    garbage)
			     != 1)
			    || (gs.max_restart_interval < 0)) {
				fprintf(stderr,
					"Invalid max_restart_interval argument: %s\n",
					optarg);
				frr_help_exit(1);
			}
		} break;
		case OPTION_MAXOPERATIONAL: {
			char garbage[3];

			if ((sscanf(optarg, "%ld%1s", &gs.operational_timeout,
				    garbage) != 1) ||
			    (gs.operational_timeout < 0)) {
				fprintf(stderr,
					"Invalid Operational_timeout argument: %s\n",
					optarg);
				frr_help_exit(1);
			}
		} break;
		case OPTION_NETNS:
			netns_en = true;
			if (optarg && strchr(optarg, '/')) {
				fprintf(stderr,
					"invalid network namespace name \"%s\" (may not contain slashes)\n",
					optarg);
				frr_help_exit(1);
			}
			netns = optarg;
			break;
		case 'i': {
			char garbage[3];
			int period;
			if ((sscanf(optarg, "%d%1s", &period, garbage) != 1)
			    || (gs.period < 1)) {
				fprintf(stderr,
					"Invalid interval argument: %s\n",
					optarg);
				frr_help_exit(1);
			}
			gs.period = 1000 * period;
		} break;
		case 'p':
			watchfrr_di.pid_file = optarg;
			break;
		case 'r':
			if (!valid_command(optarg)) {
				fprintf(stderr,
					"Invalid restart command, must contain '%%s': %s\n",
					optarg);
				frr_help_exit(1);
			}
			gs.restart_command = optarg;
			break;
		case 's':
			if (!valid_command(optarg)) {
				fprintf(stderr,
					"Invalid start command, must contain '%%s': %s\n",
					optarg);
				frr_help_exit(1);
			}
			gs.start_command = optarg;
			break;
		case 'S':
			gs.vtydir = optarg;
			break;
		case 't': {
			char garbage[3];
			if ((sscanf(optarg, "%ld%1s", &gs.timeout, garbage)
			     != 1)
			    || (gs.timeout < 1)) {
				fprintf(stderr,
					"Invalid timeout argument: %s\n",
					optarg);
				frr_help_exit(1);
			}
		} break;
		case 'T': {
			char garbage[3];
			if ((sscanf(optarg, "%ld%1s", &gs.restart_timeout,
				    garbage)
			     != 1)
			    || (gs.restart_timeout < 1)) {
				fprintf(stderr,
					"Invalid restart timeout argument: %s\n",
					optarg);
				frr_help_exit(1);
			}
		} break;
		default:
			fputs("Invalid option.\n", stderr);
			frr_help_exit(1);
		}
	}

	if (watch_only
	    && (gs.start_command || gs.stop_command || gs.restart_command)) {
		fputs("Options -r/-s/-k are not used when --dry is active.\n",
		      stderr);
	}
	if (!watch_only
	    && (!gs.restart_command || !gs.start_command || !gs.stop_command)) {
		fprintf(stderr,
			"Options -s (start), -k (kill), and -r (restart) are required.\n");
		frr_help_exit(1);
	}

	if (blankstr) {
		if (gs.restart_command)
			gs.restart_command =
				translate_blanks(gs.restart_command, blankstr);
		if (gs.start_command)
			gs.start_command =
				translate_blanks(gs.start_command, blankstr);
		if (gs.stop_command)
			gs.stop_command =
				translate_blanks(gs.stop_command, blankstr);
	}

	gs.restart.interval = gs.min_restart_interval;

	/* env variable for the processes that we start */
	if (watchfrr_di.pathspace)
		setenv("FRR_PATHSPACE", watchfrr_di.pathspace, 1);
	else
		unsetenv("FRR_PATHSPACE");

	/*
	 * when watchfrr_di.pathspace is read, if it is not specified
	 * pathspace is NULL as expected
	 */
	pathspace = watchfrr_di.pathspace;

	if (netns_en && !netns)
		netns = watchfrr_di.pathspace;

	if (netns_en && netns && netns[0])
		netns_setup(netns);

	master = frr_init();
	watchfrr_error_init();
	watchfrr_init(argc, argv);
	cmd_init_config_callbacks(watchfrr_start_config, watchfrr_end_config);
	watchfrr_vty_init();

	frr_config_fork();

	if (watchfrr_di.daemon_mode)
		zlog_syslog_set_prio_min(MIN(gs.loglevel, LOG_DEBUG));
	else
		zlog_aux_init(NULL, MIN(gs.loglevel, LOG_DEBUG));

	frr_run(master);

	systemd_send_stopping();
	/* Not reached. */
	return 0;
}