]>
Commit | Line | Data |
---|---|---|
acddc0ed | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
4f04a76b DL |
2 | /* |
3 | * libfrr overall management functions | |
4 | * | |
5 | * Copyright (C) 2016 David Lamparter for NetDEF, Inc. | |
4f04a76b DL |
6 | */ |
7 | ||
8 | #include <zebra.h> | |
689f5a8c | 9 | #include <sys/un.h> |
4f04a76b | 10 | |
f43fbf83 DL |
11 | #include <sys/types.h> |
12 | #include <sys/wait.h> | |
13 | ||
4f04a76b DL |
14 | #include "libfrr.h" |
15 | #include "getopt.h" | |
beaa5470 | 16 | #include "privs.h" |
4f04a76b DL |
17 | #include "vty.h" |
18 | #include "command.h" | |
09781197 | 19 | #include "lib/version.h" |
1c0d8808 | 20 | #include "lib_vty.h" |
f73126c3 | 21 | #include "log_vty.h" |
eb05883f | 22 | #include "zclient.h" |
30771d65 | 23 | #include "module.h" |
f43fbf83 | 24 | #include "network.h" |
b66d022e | 25 | #include "lib_errors.h" |
1c2facd1 RW |
26 | #include "db.h" |
27 | #include "northbound_cli.h" | |
1ae9686c | 28 | #include "northbound_db.h" |
ae0994f6 | 29 | #include "debug.h" |
3e41733f | 30 | #include "frrcu.h" |
b9b4c061 | 31 | #include "frr_pthread.h" |
ac4adef4 | 32 | #include "defaults.h" |
e613a6f7 | 33 | #include "frrscript.h" |
247898d5 | 34 | #include "systemd.h" |
eb05883f | 35 | |
cd9d0537 DS |
36 | DEFINE_HOOK(frr_early_init, (struct event_loop * tm), (tm)); |
37 | DEFINE_HOOK(frr_late_init, (struct event_loop * tm), (tm)); | |
38 | DEFINE_HOOK(frr_config_pre, (struct event_loop * tm), (tm)); | |
39 | DEFINE_HOOK(frr_config_post, (struct event_loop * tm), (tm)); | |
8451921b DL |
40 | DEFINE_KOOH(frr_early_fini, (), ()); |
41 | DEFINE_KOOH(frr_fini, (), ()); | |
a5b38c5b | 42 | |
eb05883f | 43 | const char frr_sysconfdir[] = SYSCONFDIR; |
43e587c1 | 44 | char frr_vtydir[256]; |
1c2facd1 RW |
45 | #ifdef HAVE_SQLITE3 |
46 | const char frr_dbdir[] = DAEMON_DB_DIR; | |
47 | #endif | |
80b4df3b | 48 | const char frr_moduledir[] = MODULE_PATH; |
e4e0229a | 49 | const char frr_scriptdir[] = SCRIPT_PATH; |
eb05883f | 50 | |
4f138a3e DL |
51 | char frr_protoname[256] = "NONE"; |
52 | char frr_protonameinst[256] = "NONE"; | |
b85120bc | 53 | |
ff44f570 | 54 | char config_default[512]; |
689f5a8c | 55 | char frr_zclientpath[256]; |
43e587c1 | 56 | static char pidfile_default[1024]; |
1c2facd1 RW |
57 | #ifdef HAVE_SQLITE3 |
58 | static char dbfile_default[512]; | |
59 | #endif | |
918537e2 | 60 | static char vtypath_default[512]; |
4f04a76b | 61 | |
38554d3a DL |
62 | /* cleared in frr_preinit(), then re-set after daemonizing */ |
63 | bool frr_is_after_fork = true; | |
d8729f8c | 64 | bool debug_memstats_at_exit = false; |
0a7c7856 | 65 | static bool nodetach_term, nodetach_daemon; |
6e3253b9 | 66 | static uint64_t startup_fds; |
9eed278b | 67 | |
4f04a76b DL |
68 | static char comb_optstr[256]; |
69 | static struct option comb_lo[64]; | |
70 | static struct option *comb_next_lo = &comb_lo[0]; | |
71 | static char comb_helpstr[4096]; | |
72 | ||
73 | struct optspec { | |
74 | const char *optstr; | |
75 | const char *helpstr; | |
76 | const struct option *longopts; | |
77 | }; | |
78 | ||
79 | static void opt_extend(const struct optspec *os) | |
80 | { | |
81 | const struct option *lo; | |
82 | ||
9f73d2c9 | 83 | strlcat(comb_optstr, os->optstr, sizeof(comb_optstr)); |
67c726a1 | 84 | strlcat(comb_helpstr, os->helpstr, sizeof(comb_helpstr)); |
4f04a76b DL |
85 | for (lo = os->longopts; lo->name; lo++) |
86 | memcpy(comb_next_lo++, lo, sizeof(*lo)); | |
87 | } | |
88 | ||
89 | ||
80b4df3b MW |
90 | #define OPTION_VTYSOCK 1000 |
91 | #define OPTION_MODULEDIR 1002 | |
e9b4e74a DS |
92 | #define OPTION_LOG 1003 |
93 | #define OPTION_LOGLEVEL 1004 | |
1c2facd1 RW |
94 | #define OPTION_TCLI 1005 |
95 | #define OPTION_DB_FILE 1006 | |
2950f5da | 96 | #define OPTION_LOGGING 1007 |
1a9f340b | 97 | #define OPTION_LIMIT_FDS 1008 |
e4e0229a | 98 | #define OPTION_SCRIPTDIR 1009 |
4f04a76b DL |
99 | |
100 | static const struct option lo_always[] = { | |
d62a17ae | 101 | {"help", no_argument, NULL, 'h'}, |
102 | {"version", no_argument, NULL, 'v'}, | |
103 | {"daemon", no_argument, NULL, 'd'}, | |
104 | {"module", no_argument, NULL, 'M'}, | |
ac4adef4 | 105 | {"profile", required_argument, NULL, 'F'}, |
33606a15 | 106 | {"pathspace", required_argument, NULL, 'N'}, |
ac2cb9bf | 107 | {"vrfdefaultname", required_argument, NULL, 'o'}, |
d62a17ae | 108 | {"vty_socket", required_argument, NULL, OPTION_VTYSOCK}, |
109 | {"moduledir", required_argument, NULL, OPTION_MODULEDIR}, | |
e4e0229a | 110 | {"scriptdir", required_argument, NULL, OPTION_SCRIPTDIR}, |
f8507817 | 111 | {"log", required_argument, NULL, OPTION_LOG}, |
e9b4e74a | 112 | {"log-level", required_argument, NULL, OPTION_LOGLEVEL}, |
2950f5da | 113 | {"command-log-always", no_argument, NULL, OPTION_LOGGING}, |
1a9f340b | 114 | {"limit-fds", required_argument, NULL, OPTION_LIMIT_FDS}, |
d62a17ae | 115 | {NULL}}; |
4f04a76b | 116 | static const struct optspec os_always = { |
ac2cb9bf | 117 | "hvdM:F:N:o:", |
4f04a76b DL |
118 | " -h, --help Display this help and exit\n" |
119 | " -v, --version Print program version\n" | |
eb05883f | 120 | " -d, --daemon Runs in daemon mode\n" |
30771d65 | 121 | " -M, --module Load specified module\n" |
ac4adef4 | 122 | " -F, --profile Use specified configuration profile\n" |
33606a15 | 123 | " -N, --pathspace Insert prefix into config & socket paths\n" |
ac2cb9bf | 124 | " -o, --vrfdefaultname Set default VRF name.\n" |
80b4df3b | 125 | " --vty_socket Override vty socket path\n" |
f8507817 | 126 | " --moduledir Override modules directory\n" |
e4e0229a | 127 | " --scriptdir Override scripts directory\n" |
e9b4e74a | 128 | " --log Set Logging to stdout, syslog, or file:<name>\n" |
1c2facd1 | 129 | " --log-level Set Logging Level to use, debug, info, warn, etc\n" |
1a9f340b | 130 | " --limit-fds Limit number of fds supported\n", |
d62a17ae | 131 | lo_always}; |
4f04a76b | 132 | |
4d28aea9 | 133 | static bool logging_to_stdout = false; /* set when --log stdout specified */ |
4f04a76b | 134 | |
b1dc60f4 | 135 | static const struct option lo_cfg[] = { |
d62a17ae | 136 | {"config_file", required_argument, NULL, 'f'}, |
137 | {"dryrun", no_argument, NULL, 'C'}, | |
138 | {NULL}}; | |
b1dc60f4 DL |
139 | static const struct optspec os_cfg = { |
140 | "f:C", | |
eb05883f | 141 | " -f, --config_file Set configuration file name\n" |
b1dc60f4 DL |
142 | " -C, --dryrun Check configuration for validity and exit\n", |
143 | lo_cfg}; | |
144 | ||
145 | ||
146 | static const struct option lo_fullcli[] = { | |
147 | {"terminal", no_argument, NULL, 't'}, | |
148 | {"tcli", no_argument, NULL, OPTION_TCLI}, | |
1c2facd1 | 149 | #ifdef HAVE_SQLITE3 |
b1dc60f4 | 150 | {"db_file", required_argument, NULL, OPTION_DB_FILE}, |
1c2facd1 | 151 | #endif |
b1dc60f4 DL |
152 | {NULL}}; |
153 | static const struct optspec os_fullcli = { | |
154 | "t", | |
155 | " --tcli Use transaction-based CLI\n" | |
cff2b211 DL |
156 | " -t, --terminal Open terminal session on stdio\n" |
157 | " -d -t Daemonize after terminal session ends\n", | |
b1dc60f4 DL |
158 | lo_fullcli}; |
159 | ||
160 | ||
161 | static const struct option lo_pid[] = { | |
162 | {"pid_file", required_argument, NULL, 'i'}, | |
163 | {NULL}}; | |
164 | static const struct optspec os_pid = { | |
165 | "i:", | |
166 | " -i, --pid_file Set process identifier file name\n", | |
167 | lo_pid}; | |
eb05883f DL |
168 | |
169 | ||
170 | static const struct option lo_zclient[] = { | |
d62a17ae | 171 | {"socket", required_argument, NULL, 'z'}, |
172 | {NULL}}; | |
eb05883f | 173 | static const struct optspec os_zclient = { |
d62a17ae | 174 | "z:", " -z, --socket Set path of zebra socket\n", lo_zclient}; |
eb05883f DL |
175 | |
176 | ||
4f04a76b | 177 | static const struct option lo_vty[] = { |
d62a17ae | 178 | {"vty_addr", required_argument, NULL, 'A'}, |
179 | {"vty_port", required_argument, NULL, 'P'}, | |
180 | {NULL}}; | |
4f04a76b DL |
181 | static const struct optspec os_vty = { |
182 | "A:P:", | |
183 | " -A, --vty_addr Set vty's bind address\n" | |
184 | " -P, --vty_port Set vty's port number\n", | |
d62a17ae | 185 | lo_vty}; |
4f04a76b DL |
186 | |
187 | ||
d62a17ae | 188 | static const struct option lo_user[] = {{"user", required_argument, NULL, 'u'}, |
189 | {"group", required_argument, NULL, 'g'}, | |
190 | {NULL}}; | |
191 | static const struct optspec os_user = {"u:g:", | |
192 | " -u, --user User to run as\n" | |
193 | " -g, --group Group to run as\n", | |
194 | lo_user}; | |
4f04a76b | 195 | |
689f5a8c DL |
196 | bool frr_zclient_addr(struct sockaddr_storage *sa, socklen_t *sa_len, |
197 | const char *path) | |
198 | { | |
199 | memset(sa, 0, sizeof(*sa)); | |
200 | ||
201 | if (!path) | |
4e99f309 | 202 | path = frr_zclientpath; |
689f5a8c DL |
203 | |
204 | if (!strncmp(path, ZAPI_TCP_PATHNAME, strlen(ZAPI_TCP_PATHNAME))) { | |
5d13cd09 | 205 | /* note: this functionality is disabled at bottom */ |
689f5a8c DL |
206 | int af; |
207 | int port = ZEBRA_PORT; | |
208 | char *err = NULL; | |
209 | struct sockaddr_in *sin = NULL; | |
210 | struct sockaddr_in6 *sin6 = NULL; | |
211 | ||
212 | path += strlen(ZAPI_TCP_PATHNAME); | |
213 | ||
214 | switch (path[0]) { | |
215 | case '4': | |
216 | path++; | |
217 | af = AF_INET; | |
218 | break; | |
219 | case '6': | |
220 | path++; | |
996c9314 | 221 | /* fallthrough */ |
689f5a8c DL |
222 | default: |
223 | af = AF_INET6; | |
224 | break; | |
225 | } | |
226 | ||
227 | switch (path[0]) { | |
228 | case '\0': | |
229 | break; | |
230 | case ':': | |
231 | path++; | |
232 | port = strtoul(path, &err, 10); | |
233 | if (*err || !*path) | |
234 | return false; | |
235 | break; | |
236 | default: | |
237 | return false; | |
238 | } | |
239 | ||
240 | sa->ss_family = af; | |
241 | switch (af) { | |
242 | case AF_INET: | |
243 | sin = (struct sockaddr_in *)sa; | |
244 | sin->sin_port = htons(port); | |
245 | sin->sin_addr.s_addr = htonl(INADDR_LOOPBACK); | |
246 | *sa_len = sizeof(struct sockaddr_in); | |
247 | #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN | |
248 | sin->sin_len = *sa_len; | |
249 | #endif | |
250 | break; | |
251 | case AF_INET6: | |
252 | sin6 = (struct sockaddr_in6 *)sa; | |
253 | sin6->sin6_port = htons(port); | |
254 | inet_pton(AF_INET6, "::1", &sin6->sin6_addr); | |
255 | *sa_len = sizeof(struct sockaddr_in6); | |
256 | #ifdef SIN6_LEN | |
257 | sin6->sin6_len = *sa_len; | |
258 | #endif | |
259 | break; | |
260 | } | |
5d13cd09 DL |
261 | |
262 | #if 1 | |
263 | /* force-disable this path, because tcp-zebra is a | |
264 | * SECURITY ISSUE. there are no checks at all against | |
265 | * untrusted users on the local system connecting on TCP | |
266 | * and injecting bogus routing data into the entire routing | |
267 | * domain. | |
268 | * | |
269 | * The functionality is only left here because it may be | |
270 | * useful during development, in order to be able to get | |
271 | * tcpdump or wireshark watching ZAPI as TCP. If you want | |
272 | * to do that, flip the #if 1 above to #if 0. */ | |
273 | memset(sa, 0, sizeof(*sa)); | |
274 | return false; | |
275 | #endif | |
689f5a8c DL |
276 | } else { |
277 | /* "sun" is a #define on solaris */ | |
278 | struct sockaddr_un *suna = (struct sockaddr_un *)sa; | |
279 | ||
280 | suna->sun_family = AF_UNIX; | |
281 | strlcpy(suna->sun_path, path, sizeof(suna->sun_path)); | |
282 | #ifdef HAVE_STRUCT_SOCKADDR_UN_SUN_LEN | |
283 | *sa_len = suna->sun_len = SUN_LEN(suna); | |
284 | #else | |
285 | *sa_len = sizeof(suna->sun_family) + strlen(suna->sun_path); | |
286 | #endif /* HAVE_STRUCT_SOCKADDR_UN_SUN_LEN */ | |
287 | #if 0 | |
288 | /* this is left here for future reference; Linux abstract | |
289 | * socket namespace support can be enabled by replacing | |
290 | * above #if 0 with #ifdef GNU_LINUX. | |
291 | * | |
292 | * THIS IS A SECURITY ISSUE, the abstract socket namespace | |
293 | * does not have user/group permission control on sockets. | |
294 | * we'd need to implement SCM_CREDENTIALS support first to | |
295 | * check that only proper users can connect to abstract | |
296 | * sockets. (same problem as tcp-zebra, except there is a | |
297 | * fix with SCM_CREDENTIALS. tcp-zebra has no such fix.) | |
298 | */ | |
299 | if (suna->sun_path[0] == '@') | |
300 | suna->sun_path[0] = '\0'; | |
301 | #endif | |
302 | } | |
303 | return true; | |
304 | } | |
305 | ||
4f04a76b DL |
306 | static struct frr_daemon_info *di = NULL; |
307 | ||
43e587c1 DS |
308 | void frr_init_vtydir(void) |
309 | { | |
310 | snprintf(frr_vtydir, sizeof(frr_vtydir), DAEMON_VTY_DIR, "", ""); | |
311 | } | |
312 | ||
4f04a76b DL |
313 | void frr_preinit(struct frr_daemon_info *daemon, int argc, char **argv) |
314 | { | |
315 | di = daemon; | |
38554d3a | 316 | frr_is_after_fork = false; |
4f04a76b DL |
317 | |
318 | /* basename(), opencoded. */ | |
319 | char *p = strrchr(argv[0], '/'); | |
320 | di->progname = p ? p + 1 : argv[0]; | |
321 | ||
322 | umask(0027); | |
323 | ||
da2fc191 DL |
324 | log_args_init(daemon->early_logging); |
325 | ||
4f04a76b | 326 | opt_extend(&os_always); |
b1dc60f4 DL |
327 | if (!(di->flags & FRR_NO_SPLIT_CONFIG)) |
328 | opt_extend(&os_cfg); | |
329 | if (!(di->flags & FRR_LIMITED_CLI)) | |
330 | opt_extend(&os_fullcli); | |
331 | if (!(di->flags & FRR_NO_PID)) | |
332 | opt_extend(&os_pid); | |
4f04a76b DL |
333 | if (!(di->flags & FRR_NO_PRIVSEP)) |
334 | opt_extend(&os_user); | |
eb05883f DL |
335 | if (!(di->flags & FRR_NO_ZCLIENT)) |
336 | opt_extend(&os_zclient); | |
4f04a76b DL |
337 | if (!(di->flags & FRR_NO_TCPVTY)) |
338 | opt_extend(&os_vty); | |
0a7c7856 DL |
339 | if (di->flags & FRR_DETACH_LATER) |
340 | nodetach_daemon = true; | |
eb05883f | 341 | |
43e587c1 | 342 | frr_init_vtydir(); |
eb05883f | 343 | snprintf(config_default, sizeof(config_default), "%s/%s.conf", |
d62a17ae | 344 | frr_sysconfdir, di->name); |
eb05883f | 345 | snprintf(pidfile_default, sizeof(pidfile_default), "%s/%s.pid", |
d62a17ae | 346 | frr_vtydir, di->name); |
43e587c1 DS |
347 | snprintf(frr_zclientpath, sizeof(frr_zclientpath), |
348 | ZEBRA_SERV_PATH, "", ""); | |
1c2facd1 RW |
349 | #ifdef HAVE_SQLITE3 |
350 | snprintf(dbfile_default, sizeof(dbfile_default), "%s/%s.db", | |
351 | frr_dbdir, di->name); | |
352 | #endif | |
b85120bc DL |
353 | |
354 | strlcpy(frr_protoname, di->logname, sizeof(frr_protoname)); | |
355 | strlcpy(frr_protonameinst, di->logname, sizeof(frr_protonameinst)); | |
689f5a8c | 356 | |
1c2facd1 | 357 | di->cli_mode = FRR_CLI_CLASSIC; |
6e3253b9 DL |
358 | |
359 | /* we may be starting with extra FDs open for whatever purpose, | |
360 | * e.g. logging, some module, etc. Recording them here allows later | |
361 | * checking whether an fd is valid for such extension purposes, | |
362 | * without this we could end up e.g. logging to a BGP session fd. | |
363 | */ | |
364 | startup_fds = 0; | |
365 | for (int i = 0; i < 64; i++) { | |
366 | struct stat st; | |
367 | ||
368 | if (fstat(i, &st)) | |
369 | continue; | |
370 | if (S_ISDIR(st.st_mode) || S_ISBLK(st.st_mode)) | |
371 | continue; | |
372 | ||
373 | startup_fds |= UINT64_C(0x1) << (uint64_t)i; | |
374 | } | |
247898d5 DL |
375 | |
376 | /* note this doesn't do anything, it just grabs state, so doing it | |
377 | * early in _preinit is perfect. | |
378 | */ | |
379 | systemd_init_env(); | |
6e3253b9 DL |
380 | } |
381 | ||
382 | bool frr_is_startup_fd(int fd) | |
383 | { | |
384 | return !!(startup_fds & (UINT64_C(0x1) << (uint64_t)fd)); | |
4f04a76b DL |
385 | } |
386 | ||
387 | void frr_opt_add(const char *optstr, const struct option *longopts, | |
d62a17ae | 388 | const char *helpstr) |
4f04a76b | 389 | { |
d62a17ae | 390 | const struct optspec main_opts = {optstr, helpstr, longopts}; |
4f04a76b DL |
391 | opt_extend(&main_opts); |
392 | } | |
393 | ||
394 | void frr_help_exit(int status) | |
395 | { | |
396 | FILE *target = status ? stderr : stdout; | |
397 | ||
398 | if (status != 0) | |
399 | fprintf(stderr, "Invalid options.\n\n"); | |
400 | ||
401 | if (di->printhelp) | |
402 | di->printhelp(target); | |
403 | else | |
404 | fprintf(target, "Usage: %s [OPTION...]\n\n%s%s%s\n\n%s", | |
d62a17ae | 405 | di->progname, di->proghelp, di->copyright ? "\n\n" : "", |
406 | di->copyright ? di->copyright : "", comb_helpstr); | |
4f04a76b DL |
407 | fprintf(target, "\nReport bugs to %s\n", FRR_BUG_ADDRESS); |
408 | exit(status); | |
409 | } | |
410 | ||
30771d65 DL |
411 | struct option_chain { |
412 | struct option_chain *next; | |
413 | const char *arg; | |
414 | }; | |
80b4df3b | 415 | |
30771d65 | 416 | static struct option_chain *modules = NULL, **modnext = &modules; |
4f04a76b DL |
417 | static int errors = 0; |
418 | ||
419 | static int frr_opt(int opt) | |
420 | { | |
421 | static int vty_port_set = 0; | |
422 | static int vty_addr_set = 0; | |
30771d65 | 423 | struct option_chain *oc; |
da2fc191 DL |
424 | struct log_arg *log_arg; |
425 | size_t arg_len; | |
4f04a76b DL |
426 | char *err; |
427 | ||
428 | switch (opt) { | |
429 | case 'h': | |
430 | frr_help_exit(0); | |
4f04a76b DL |
431 | case 'v': |
432 | print_version(di->progname); | |
433 | exit(0); | |
434 | break; | |
eb05883f | 435 | case 'd': |
08c2d52a | 436 | di->daemon_mode = true; |
eb05883f | 437 | break; |
30771d65 DL |
438 | case 'M': |
439 | oc = XMALLOC(MTYPE_TMP, sizeof(*oc)); | |
440 | oc->arg = optarg; | |
441 | oc->next = NULL; | |
442 | *modnext = oc; | |
443 | modnext = &oc->next; | |
444 | break; | |
ac4adef4 DL |
445 | case 'F': |
446 | if (!frr_defaults_profile_valid(optarg)) { | |
447 | const char **p; | |
448 | FILE *ofd = stderr; | |
449 | ||
450 | if (!strcmp(optarg, "help")) | |
451 | ofd = stdout; | |
452 | else | |
453 | fprintf(stderr, | |
454 | "The \"%s\" configuration profile is not valid for this FRR version.\n", | |
455 | optarg); | |
456 | ||
457 | fprintf(ofd, "Available profiles are:\n"); | |
458 | for (p = frr_defaults_profiles; *p; p++) | |
459 | fprintf(ofd, "%s%s\n", | |
460 | strcmp(*p, DFLT_NAME) ? " " : " * ", | |
461 | *p); | |
462 | ||
463 | if (ofd == stdout) | |
464 | exit(0); | |
465 | fprintf(ofd, "\n"); | |
466 | errors++; | |
467 | break; | |
468 | } | |
469 | frr_defaults_profile_set(optarg); | |
470 | break; | |
eb05883f | 471 | case 'i': |
b1dc60f4 | 472 | if (di->flags & FRR_NO_PID) |
eb05883f DL |
473 | return 1; |
474 | di->pid_file = optarg; | |
475 | break; | |
476 | case 'f': | |
b1dc60f4 | 477 | if (di->flags & FRR_NO_SPLIT_CONFIG) |
eb05883f DL |
478 | return 1; |
479 | di->config_file = optarg; | |
480 | break; | |
d1b4fc1f | 481 | case 'N': |
d1b4fc1f DL |
482 | if (di->pathspace) { |
483 | fprintf(stderr, | |
484 | "-N/--pathspace option specified more than once!\n"); | |
485 | errors++; | |
486 | break; | |
487 | } | |
43e587c1 DS |
488 | if (di->zpathspace) |
489 | fprintf(stderr, | |
f79f7a7b | 490 | "-N option overridden by -z for zebra named socket path\n"); |
43e587c1 | 491 | |
d1b4fc1f DL |
492 | if (strchr(optarg, '/') || strchr(optarg, '.')) { |
493 | fprintf(stderr, | |
494 | "slashes or dots are not permitted in the --pathspace option.\n"); | |
495 | errors++; | |
496 | break; | |
497 | } | |
498 | di->pathspace = optarg; | |
4e99f309 | 499 | |
43e587c1 DS |
500 | if (!di->zpathspace) |
501 | snprintf(frr_zclientpath, sizeof(frr_zclientpath), | |
502 | ZEBRA_SERV_PATH, "/", di->pathspace); | |
503 | snprintf(frr_vtydir, sizeof(frr_vtydir), DAEMON_VTY_DIR, "/", | |
504 | di->pathspace); | |
505 | snprintf(pidfile_default, sizeof(pidfile_default), "%s/%s.pid", | |
506 | frr_vtydir, di->name); | |
d1b4fc1f | 507 | break; |
ac2cb9bf IR |
508 | case 'o': |
509 | vrf_set_default_name(optarg); | |
510 | break; | |
1c2facd1 RW |
511 | #ifdef HAVE_SQLITE3 |
512 | case OPTION_DB_FILE: | |
b1dc60f4 | 513 | if (di->flags & FRR_NO_PID) |
1c2facd1 RW |
514 | return 1; |
515 | di->db_file = optarg; | |
516 | break; | |
517 | #endif | |
eb05883f | 518 | case 'C': |
b1dc60f4 | 519 | if (di->flags & FRR_NO_SPLIT_CONFIG) |
eb05883f | 520 | return 1; |
08c2d52a | 521 | di->dryrun = true; |
eb05883f | 522 | break; |
cff2b211 | 523 | case 't': |
b1dc60f4 | 524 | if (di->flags & FRR_LIMITED_CLI) |
cff2b211 | 525 | return 1; |
08c2d52a | 526 | di->terminal = true; |
cff2b211 | 527 | break; |
eb05883f | 528 | case 'z': |
43e587c1 DS |
529 | di->zpathspace = true; |
530 | if (di->pathspace) | |
531 | fprintf(stderr, | |
532 | "-z option overrides -N option for zebra named socket path\n"); | |
eb05883f DL |
533 | if (di->flags & FRR_NO_ZCLIENT) |
534 | return 1; | |
689f5a8c | 535 | strlcpy(frr_zclientpath, optarg, sizeof(frr_zclientpath)); |
eb05883f | 536 | break; |
4f04a76b DL |
537 | case 'A': |
538 | if (di->flags & FRR_NO_TCPVTY) | |
539 | return 1; | |
540 | if (vty_addr_set) { | |
d62a17ae | 541 | fprintf(stderr, |
542 | "-A option specified more than once!\n"); | |
4f04a76b DL |
543 | errors++; |
544 | break; | |
545 | } | |
546 | vty_addr_set = 1; | |
547 | di->vty_addr = optarg; | |
548 | break; | |
549 | case 'P': | |
550 | if (di->flags & FRR_NO_TCPVTY) | |
551 | return 1; | |
552 | if (vty_port_set) { | |
d62a17ae | 553 | fprintf(stderr, |
554 | "-P option specified more than once!\n"); | |
4f04a76b DL |
555 | errors++; |
556 | break; | |
557 | } | |
558 | vty_port_set = 1; | |
559 | di->vty_port = strtoul(optarg, &err, 0); | |
560 | if (*err || !*optarg) { | |
d62a17ae | 561 | fprintf(stderr, |
562 | "invalid port number \"%s\" for -P option\n", | |
563 | optarg); | |
4f04a76b DL |
564 | errors++; |
565 | break; | |
566 | } | |
567 | break; | |
568 | case OPTION_VTYSOCK: | |
569 | if (di->vty_sock_path) { | |
d62a17ae | 570 | fprintf(stderr, |
571 | "--vty_socket option specified more than once!\n"); | |
4f04a76b DL |
572 | errors++; |
573 | break; | |
574 | } | |
575 | di->vty_sock_path = optarg; | |
576 | break; | |
80b4df3b MW |
577 | case OPTION_MODULEDIR: |
578 | if (di->module_path) { | |
d62a17ae | 579 | fprintf(stderr, |
580 | "----moduledir option specified more than once!\n"); | |
80b4df3b MW |
581 | errors++; |
582 | break; | |
583 | } | |
584 | di->module_path = optarg; | |
585 | break; | |
e4e0229a QY |
586 | case OPTION_SCRIPTDIR: |
587 | if (di->script_path) { | |
588 | fprintf(stderr, "--scriptdir option specified more than once!\n"); | |
589 | errors++; | |
590 | break; | |
591 | } | |
592 | di->script_path = optarg; | |
593 | break; | |
1c2facd1 RW |
594 | case OPTION_TCLI: |
595 | di->cli_mode = FRR_CLI_TRANSACTIONAL; | |
596 | break; | |
4f04a76b DL |
597 | case 'u': |
598 | if (di->flags & FRR_NO_PRIVSEP) | |
599 | return 1; | |
600 | di->privs->user = optarg; | |
601 | break; | |
602 | case 'g': | |
603 | if (di->flags & FRR_NO_PRIVSEP) | |
604 | return 1; | |
605 | di->privs->group = optarg; | |
606 | break; | |
f8507817 | 607 | case OPTION_LOG: |
da2fc191 DL |
608 | arg_len = strlen(optarg) + 1; |
609 | log_arg = XCALLOC(MTYPE_TMP, sizeof(*log_arg) + arg_len); | |
610 | memcpy(log_arg->target, optarg, arg_len); | |
611 | log_args_add_tail(di->early_logging, log_arg); | |
f8507817 | 612 | break; |
e9b4e74a DS |
613 | case OPTION_LOGLEVEL: |
614 | di->early_loglevel = optarg; | |
615 | break; | |
2950f5da DS |
616 | case OPTION_LOGGING: |
617 | di->log_always = true; | |
618 | break; | |
1a9f340b MS |
619 | case OPTION_LIMIT_FDS: |
620 | di->limit_fds = strtoul(optarg, &err, 0); | |
621 | break; | |
4f04a76b DL |
622 | default: |
623 | return 1; | |
624 | } | |
625 | return 0; | |
626 | } | |
627 | ||
d62a17ae | 628 | int frr_getopt(int argc, char *const argv[], int *longindex) |
4f04a76b DL |
629 | { |
630 | int opt; | |
631 | int lidx; | |
632 | ||
633 | comb_next_lo->name = NULL; | |
634 | ||
635 | do { | |
636 | opt = getopt_long(argc, argv, comb_optstr, comb_lo, &lidx); | |
637 | if (frr_opt(opt)) | |
638 | break; | |
639 | } while (opt != -1); | |
640 | ||
641 | if (opt == -1 && errors) | |
642 | frr_help_exit(1); | |
643 | if (longindex) | |
644 | *longindex = lidx; | |
645 | return opt; | |
646 | } | |
647 | ||
beaa5470 DL |
648 | static void frr_mkdir(const char *path, bool strip) |
649 | { | |
650 | char buf[256]; | |
651 | mode_t prev; | |
652 | int ret; | |
653 | struct zprivs_ids_t ids; | |
654 | ||
655 | if (strip) { | |
656 | char *slash = strrchr(path, '/'); | |
657 | size_t plen; | |
658 | if (!slash) | |
659 | return; | |
660 | plen = slash - path; | |
661 | if (plen > sizeof(buf) - 1) | |
662 | return; | |
663 | memcpy(buf, path, plen); | |
664 | buf[plen] = '\0'; | |
665 | path = buf; | |
666 | } | |
667 | ||
668 | /* o+rx (..5) is needed for the frrvty group to work properly; | |
669 | * without it, users in the frrvty group can't access the vty sockets. | |
670 | */ | |
671 | prev = umask(0022); | |
672 | ret = mkdir(path, 0755); | |
673 | umask(prev); | |
674 | ||
675 | if (ret != 0) { | |
676 | /* if EEXIST, return without touching the permissions, | |
677 | * so user-set custom permissions are left in place | |
678 | */ | |
679 | if (errno == EEXIST) | |
680 | return; | |
681 | ||
1c50c1c0 QY |
682 | flog_err(EC_LIB_SYSTEM_CALL, "failed to mkdir \"%s\": %s", path, |
683 | strerror(errno)); | |
beaa5470 DL |
684 | return; |
685 | } | |
686 | ||
687 | zprivs_get_ids(&ids); | |
688 | if (chown(path, ids.uid_normal, ids.gid_normal)) | |
1c50c1c0 QY |
689 | flog_err(EC_LIB_SYSTEM_CALL, "failed to chown \"%s\": %s", path, |
690 | strerror(errno)); | |
beaa5470 DL |
691 | } |
692 | ||
52fad8f6 PZ |
693 | static void _err_print(const void *cookie, const char *errstr) |
694 | { | |
695 | const char *prefix = (const char *)cookie; | |
696 | ||
697 | fprintf(stderr, "%s: %s\n", prefix, errstr); | |
698 | } | |
699 | ||
cd9d0537 DS |
700 | static struct event_loop *master; |
701 | struct event_loop *frr_init(void) | |
4f04a76b | 702 | { |
30771d65 | 703 | struct option_chain *oc; |
da2fc191 | 704 | struct log_arg *log_arg; |
30771d65 | 705 | struct frrmod_runtime *module; |
0bdeb5e5 | 706 | struct zprivs_ids_t ids; |
d1b4fc1f | 707 | char p_instance[16] = "", p_pathspace[256] = ""; |
80b4df3b | 708 | const char *dir; |
da2fc191 | 709 | |
80b4df3b | 710 | dir = di->module_path ? di->module_path : frr_moduledir; |
4f04a76b DL |
711 | |
712 | srandom(time(NULL)); | |
ac4adef4 | 713 | frr_defaults_apply(); |
4f04a76b | 714 | |
d1b4fc1f | 715 | if (di->instance) { |
d62a17ae | 716 | snprintf(frr_protonameinst, sizeof(frr_protonameinst), "%s[%u]", |
717 | di->logname, di->instance); | |
d1b4fc1f DL |
718 | snprintf(p_instance, sizeof(p_instance), "-%d", di->instance); |
719 | } | |
720 | if (di->pathspace) | |
b39404c1 | 721 | snprintf(p_pathspace, sizeof(p_pathspace), "%s/", |
d1b4fc1f DL |
722 | di->pathspace); |
723 | ||
36077833 | 724 | snprintf(config_default, sizeof(config_default), "%s%s%s%s.conf", |
d1b4fc1f | 725 | frr_sysconfdir, p_pathspace, di->name, p_instance); |
43e587c1 DS |
726 | snprintf(pidfile_default, sizeof(pidfile_default), "%s/%s%s.pid", |
727 | frr_vtydir, di->name, p_instance); | |
1c2facd1 RW |
728 | #ifdef HAVE_SQLITE3 |
729 | snprintf(dbfile_default, sizeof(dbfile_default), "%s/%s%s%s.db", | |
730 | frr_dbdir, p_pathspace, di->name, p_instance); | |
731 | #endif | |
b85120bc | 732 | |
37a1f2fb | 733 | zprivs_preinit(di->privs); |
0bdeb5e5 | 734 | zprivs_get_ids(&ids); |
37a1f2fb | 735 | |
0bdeb5e5 DL |
736 | zlog_init(di->progname, di->logname, di->instance, |
737 | ids.uid_normal, ids.gid_normal); | |
f8507817 | 738 | |
da2fc191 DL |
739 | while ((log_arg = log_args_pop(di->early_logging))) { |
740 | command_setup_early_logging(log_arg->target, | |
741 | di->early_loglevel); | |
4d28aea9 LB |
742 | /* this is a bit of a hack, |
743 | but need to notice when | |
744 | the target is stdout */ | |
745 | if (strcmp(log_arg->target, "stdout") == 0) | |
746 | logging_to_stdout = true; | |
da2fc191 DL |
747 | XFREE(MTYPE_TMP, log_arg); |
748 | } | |
4f04a76b | 749 | |
689f5a8c DL |
750 | if (!frr_zclient_addr(&zclient_addr, &zclient_addr_len, |
751 | frr_zclientpath)) { | |
752 | fprintf(stderr, "Invalid zserv socket path: %s\n", | |
753 | frr_zclientpath); | |
754 | exit(1); | |
755 | } | |
756 | ||
b8c1fde3 DL |
757 | /* don't mkdir these as root... */ |
758 | if (!(di->flags & FRR_NO_PRIVSEP)) { | |
759 | if (!di->pid_file || !di->vty_path) | |
760 | frr_mkdir(frr_vtydir, false); | |
761 | if (di->pid_file) | |
762 | frr_mkdir(di->pid_file, true); | |
763 | if (di->vty_path) | |
764 | frr_mkdir(di->vty_path, true); | |
765 | } | |
beaa5470 | 766 | |
30771d65 DL |
767 | frrmod_init(di->module); |
768 | while (modules) { | |
769 | modules = (oc = modules)->next; | |
52fad8f6 PZ |
770 | module = frrmod_load(oc->arg, dir, _err_print, __func__); |
771 | if (!module) | |
30771d65 | 772 | exit(1); |
30771d65 DL |
773 | XFREE(MTYPE_TMP, oc); |
774 | } | |
775 | ||
4f04a76b DL |
776 | zprivs_init(di->privs); |
777 | ||
ce50d11c | 778 | master = event_master_create(NULL); |
4f04a76b | 779 | signal_init(master, di->n_signals, di->signals); |
603c6165 | 780 | hook_call(frr_early_init, master); |
4f04a76b | 781 | |
1c2facd1 RW |
782 | #ifdef HAVE_SQLITE3 |
783 | if (!di->db_file) | |
784 | di->db_file = dbfile_default; | |
bd90faea | 785 | db_init("%s", di->db_file); |
1c2facd1 RW |
786 | #endif |
787 | ||
857b5446 DL |
788 | if (di->flags & FRR_LIMITED_CLI) |
789 | cmd_init(-1); | |
790 | else | |
791 | cmd_init(1); | |
1c2facd1 | 792 | |
2950f5da | 793 | vty_init(master, di->log_always); |
1c0d8808 | 794 | lib_cmd_init(); |
857b5446 | 795 | |
b9b4c061 | 796 | frr_pthread_init(); |
fa22080d | 797 | #ifdef HAVE_SCRIPTING |
e4e0229a | 798 | frrscript_init(di->script_path ? di->script_path : frr_scriptdir); |
fa22080d | 799 | #endif |
b9b4c061 | 800 | |
85cd2f9f | 801 | log_ref_init(); |
1f9128d6 | 802 | log_ref_vty_init(); |
b66d022e DS |
803 | lib_error_init(); |
804 | ||
390a8862 | 805 | nb_init(master, di->yang_modules, di->n_yang_modules, true); |
1ae9686c RW |
806 | if (nb_db_init() != NB_OK) |
807 | flog_warn(EC_LIB_NB_DATABASE, | |
808 | "%s: failed to initialize northbound database", | |
809 | __func__); | |
1c2facd1 | 810 | |
3bb513c3 CH |
811 | debug_init_cli(); |
812 | ||
4f04a76b DL |
813 | return master; |
814 | } | |
815 | ||
1c2facd1 RW |
816 | const char *frr_get_progname(void) |
817 | { | |
818 | return di ? di->progname : NULL; | |
819 | } | |
820 | ||
821 | enum frr_cli_mode frr_get_cli_mode(void) | |
822 | { | |
823 | return di ? di->cli_mode : FRR_CLI_CLASSIC; | |
824 | } | |
825 | ||
1a9f340b MS |
826 | uint32_t frr_get_fd_limit(void) |
827 | { | |
828 | return di ? di->limit_fds : 0; | |
829 | } | |
830 | ||
154b9e8f DL |
831 | static int rcvd_signal = 0; |
832 | ||
833 | static void rcv_signal(int signum) | |
834 | { | |
835 | rcvd_signal = signum; | |
836 | /* poll() is interrupted by the signal; handled below */ | |
837 | } | |
838 | ||
f43fbf83 DL |
839 | static void frr_daemon_wait(int fd) |
840 | { | |
841 | struct pollfd pfd[1]; | |
842 | int ret; | |
843 | pid_t exitpid; | |
844 | int exitstat; | |
154b9e8f DL |
845 | sigset_t sigs, prevsigs; |
846 | ||
847 | sigemptyset(&sigs); | |
848 | sigaddset(&sigs, SIGTSTP); | |
849 | sigaddset(&sigs, SIGQUIT); | |
850 | sigaddset(&sigs, SIGINT); | |
851 | sigprocmask(SIG_BLOCK, &sigs, &prevsigs); | |
852 | ||
853 | struct sigaction sa = { | |
854 | .sa_handler = rcv_signal, .sa_flags = SA_RESETHAND, | |
855 | }; | |
856 | sigemptyset(&sa.sa_mask); | |
857 | sigaction(SIGTSTP, &sa, NULL); | |
858 | sigaction(SIGQUIT, &sa, NULL); | |
859 | sigaction(SIGINT, &sa, NULL); | |
f43fbf83 DL |
860 | |
861 | do { | |
154b9e8f DL |
862 | char buf[1]; |
863 | ssize_t nrecv; | |
864 | ||
f43fbf83 DL |
865 | pfd[0].fd = fd; |
866 | pfd[0].events = POLLIN; | |
867 | ||
154b9e8f DL |
868 | rcvd_signal = 0; |
869 | ||
996c9314 | 870 | #if defined(HAVE_PPOLL) |
154b9e8f DL |
871 | ret = ppoll(pfd, 1, NULL, &prevsigs); |
872 | #elif defined(HAVE_POLLTS) | |
873 | ret = pollts(pfd, 1, NULL, &prevsigs); | |
874 | #else | |
875 | /* racy -- only used on FreeBSD 9 */ | |
876 | sigset_t tmpsigs; | |
877 | sigprocmask(SIG_SETMASK, &prevsigs, &tmpsigs); | |
f43fbf83 | 878 | ret = poll(pfd, 1, -1); |
154b9e8f DL |
879 | sigprocmask(SIG_SETMASK, &tmpsigs, NULL); |
880 | #endif | |
f43fbf83 DL |
881 | if (ret < 0 && errno != EINTR && errno != EAGAIN) { |
882 | perror("poll()"); | |
883 | exit(1); | |
884 | } | |
154b9e8f DL |
885 | switch (rcvd_signal) { |
886 | case SIGTSTP: | |
887 | send(fd, "S", 1, 0); | |
888 | do { | |
889 | nrecv = recv(fd, buf, sizeof(buf), 0); | |
890 | } while (nrecv == -1 | |
891 | && (errno == EINTR || errno == EAGAIN)); | |
892 | ||
893 | raise(SIGTSTP); | |
894 | sigaction(SIGTSTP, &sa, NULL); | |
895 | send(fd, "R", 1, 0); | |
896 | break; | |
897 | case SIGINT: | |
898 | send(fd, "I", 1, 0); | |
899 | break; | |
900 | case SIGQUIT: | |
901 | send(fd, "Q", 1, 0); | |
902 | break; | |
903 | } | |
f43fbf83 DL |
904 | } while (ret <= 0); |
905 | ||
906 | exitpid = waitpid(-1, &exitstat, WNOHANG); | |
907 | if (exitpid == 0) | |
908 | /* child successfully went to main loop & closed socket */ | |
909 | exit(0); | |
910 | ||
911 | /* child failed one way or another ... */ | |
6bd2b360 DL |
912 | if (WIFEXITED(exitstat) && WEXITSTATUS(exitstat) == 0) |
913 | /* can happen in --terminal case if exit is fast enough */ | |
914 | (void)0; | |
915 | else if (WIFEXITED(exitstat)) | |
f43fbf83 DL |
916 | fprintf(stderr, "%s failed to start, exited %d\n", di->name, |
917 | WEXITSTATUS(exitstat)); | |
918 | else if (WIFSIGNALED(exitstat)) | |
919 | fprintf(stderr, "%s crashed in startup, signal %d\n", di->name, | |
920 | WTERMSIG(exitstat)); | |
921 | else | |
922 | fprintf(stderr, "%s failed to start, unknown problem\n", | |
923 | di->name); | |
924 | exit(1); | |
925 | } | |
926 | ||
927 | static int daemon_ctl_sock = -1; | |
928 | ||
929 | static void frr_daemonize(void) | |
930 | { | |
931 | int fds[2]; | |
932 | pid_t pid; | |
933 | ||
934 | if (socketpair(AF_UNIX, SOCK_STREAM, 0, fds)) { | |
935 | perror("socketpair() for daemon control"); | |
936 | exit(1); | |
937 | } | |
938 | set_cloexec(fds[0]); | |
939 | set_cloexec(fds[1]); | |
940 | ||
941 | pid = fork(); | |
942 | if (pid < 0) { | |
943 | perror("fork()"); | |
944 | exit(1); | |
945 | } | |
946 | if (pid == 0) { | |
947 | /* child */ | |
948 | close(fds[0]); | |
949 | if (setsid() < 0) { | |
950 | perror("setsid()"); | |
951 | exit(1); | |
952 | } | |
953 | ||
954 | daemon_ctl_sock = fds[1]; | |
955 | return; | |
956 | } | |
957 | ||
958 | close(fds[1]); | |
1213e46b CH |
959 | nb_terminate(); |
960 | yang_terminate(); | |
f43fbf83 DL |
961 | frr_daemon_wait(fds[0]); |
962 | } | |
963 | ||
9e224e60 DS |
964 | /* |
965 | * Why is this a thread? | |
966 | * | |
967 | * The read in of config for integrated config happens *after* | |
968 | * thread execution starts( because it is passed in via a vtysh -b -n ) | |
969 | * While if you are not using integrated config we want the ability | |
970 | * to read the config in after thread execution starts, so that | |
971 | * we can match this behavior. | |
972 | */ | |
e6685141 | 973 | static void frr_config_read_in(struct event *t) |
4f04a76b | 974 | { |
bf645e31 DL |
975 | hook_call(frr_config_pre, master); |
976 | ||
91f9fd78 RW |
977 | if (!vty_read_config(vty_shared_candidate_config, di->config_file, |
978 | config_default) | |
979 | && di->backup_config_file) { | |
fe64533a DS |
980 | char *orig = XSTRDUP(MTYPE_TMP, host_config_get()); |
981 | ||
573de11f DS |
982 | zlog_info("Attempting to read backup config file: %s specified", |
983 | di->backup_config_file); | |
91f9fd78 RW |
984 | vty_read_config(vty_shared_candidate_config, |
985 | di->backup_config_file, config_default); | |
fe64533a DS |
986 | |
987 | host_config_set(orig); | |
988 | XFREE(MTYPE_TMP, orig); | |
573de11f | 989 | } |
1c2facd1 RW |
990 | |
991 | /* | |
91f9fd78 RW |
992 | * Automatically commit the candidate configuration after |
993 | * reading the configuration file. | |
1c2facd1 | 994 | */ |
91f9fd78 | 995 | if (frr_get_cli_mode() == FRR_CLI_TRANSACTIONAL) { |
13d6b9c1 | 996 | struct nb_context context = {}; |
df5eda3d | 997 | char errmsg[BUFSIZ] = {0}; |
91f9fd78 RW |
998 | int ret; |
999 | ||
13d6b9c1 | 1000 | context.client = NB_CLIENT_CLI; |
41ef7327 | 1001 | ret = nb_candidate_commit(context, vty_shared_candidate_config, |
df5eda3d RW |
1002 | true, "Read configuration file", NULL, |
1003 | errmsg, sizeof(errmsg)); | |
91f9fd78 | 1004 | if (ret != NB_OK && ret != NB_ERR_NO_CHANGES) |
df5eda3d RW |
1005 | zlog_err( |
1006 | "%s: failed to read configuration file: %s (%s)", | |
1007 | __func__, nb_err_name(ret), errmsg); | |
91f9fd78 | 1008 | } |
1c2facd1 | 1009 | |
2bafda27 | 1010 | hook_call(frr_config_post, master); |
9e224e60 DS |
1011 | } |
1012 | ||
1013 | void frr_config_fork(void) | |
1014 | { | |
1015 | hook_call(frr_late_init, master); | |
eb05883f | 1016 | |
b1dc60f4 | 1017 | if (!(di->flags & FRR_NO_SPLIT_CONFIG)) { |
0a7c7856 DL |
1018 | /* Don't start execution if we are in dry-run mode */ |
1019 | if (di->dryrun) { | |
1020 | frr_config_read_in(NULL); | |
1021 | exit(0); | |
1022 | } | |
9e224e60 | 1023 | |
907a2395 DS |
1024 | event_add_event(master, frr_config_read_in, NULL, 0, |
1025 | &di->read_in); | |
0a7c7856 | 1026 | } |
eb05883f | 1027 | |
154b9e8f | 1028 | if (di->daemon_mode || di->terminal) |
f43fbf83 | 1029 | frr_daemonize(); |
eb05883f | 1030 | |
38554d3a DL |
1031 | frr_is_after_fork = true; |
1032 | ||
eb05883f DL |
1033 | if (!di->pid_file) |
1034 | di->pid_file = pidfile_default; | |
d62a17ae | 1035 | pid_output(di->pid_file); |
e2be2643 | 1036 | zlog_tls_buffer_init(); |
eb05883f DL |
1037 | } |
1038 | ||
5ba56130 | 1039 | void frr_vty_serv_start(void) |
eb05883f | 1040 | { |
d62a17ae | 1041 | /* allow explicit override of vty_path in the future |
eb05883f DL |
1042 | * (not currently set anywhere) */ |
1043 | if (!di->vty_path) { | |
1044 | const char *dir; | |
d1b4fc1f DL |
1045 | char defvtydir[256]; |
1046 | ||
43e587c1 | 1047 | snprintf(defvtydir, sizeof(defvtydir), "%s", frr_vtydir); |
d1b4fc1f DL |
1048 | |
1049 | dir = di->vty_sock_path ? di->vty_sock_path : defvtydir; | |
eb05883f DL |
1050 | |
1051 | if (di->instance) | |
1052 | snprintf(vtypath_default, sizeof(vtypath_default), | |
d62a17ae | 1053 | "%s/%s-%d.vty", dir, di->name, di->instance); |
eb05883f DL |
1054 | else |
1055 | snprintf(vtypath_default, sizeof(vtypath_default), | |
d62a17ae | 1056 | "%s/%s.vty", dir, di->name); |
eb05883f DL |
1057 | |
1058 | di->vty_path = vtypath_default; | |
1059 | } | |
1060 | ||
5ba56130 CH |
1061 | vty_serv_start(di->vty_addr, di->vty_port, di->vty_path); |
1062 | } | |
1063 | ||
1064 | void frr_vty_serv_stop(void) | |
1065 | { | |
1066 | vty_serv_stop(); | |
1067 | ||
1068 | if (di->vty_path) | |
1069 | unlink(di->vty_path); | |
4f04a76b DL |
1070 | } |
1071 | ||
0a7c7856 DL |
1072 | static void frr_check_detach(void) |
1073 | { | |
1074 | if (nodetach_term || nodetach_daemon) | |
1075 | return; | |
1076 | ||
1077 | if (daemon_ctl_sock != -1) | |
1078 | close(daemon_ctl_sock); | |
1079 | daemon_ctl_sock = -1; | |
1080 | } | |
1081 | ||
154b9e8f | 1082 | static void frr_terminal_close(int isexit) |
cff2b211 | 1083 | { |
993bab89 RW |
1084 | int nullfd; |
1085 | ||
0a7c7856 DL |
1086 | nodetach_term = false; |
1087 | frr_check_detach(); | |
154b9e8f DL |
1088 | |
1089 | if (!di->daemon_mode || isexit) { | |
cff2b211 | 1090 | printf("\n%s exiting\n", di->name); |
154b9e8f DL |
1091 | if (!isexit) |
1092 | raise(SIGINT); | |
1093 | return; | |
cff2b211 DL |
1094 | } else { |
1095 | printf("\n%s daemonizing\n", di->name); | |
1096 | fflush(stdout); | |
1097 | } | |
1098 | ||
993bab89 RW |
1099 | nullfd = open("/dev/null", O_RDONLY | O_NOCTTY); |
1100 | if (nullfd == -1) { | |
450971aa | 1101 | flog_err_sys(EC_LIB_SYSTEM_CALL, |
09c866e3 QY |
1102 | "%s: failed to open /dev/null: %s", __func__, |
1103 | safe_strerror(errno)); | |
993bab89 | 1104 | } else { |
4d28aea9 LB |
1105 | int fd; |
1106 | /* | |
1107 | * only redirect stdin, stdout, stderr to null when a tty also | |
1108 | * don't redirect when stdout is set with --log stdout | |
1109 | */ | |
1110 | for (fd = 2; fd >= 0; fd--) | |
1111 | if (isatty(fd) && | |
1112 | (fd != STDOUT_FILENO || !logging_to_stdout)) | |
1113 | dup2(nullfd, fd); | |
993bab89 RW |
1114 | close(nullfd); |
1115 | } | |
154b9e8f | 1116 | } |
cff2b211 | 1117 | |
e6685141 | 1118 | static struct event *daemon_ctl_thread = NULL; |
154b9e8f | 1119 | |
e6685141 | 1120 | static void frr_daemon_ctl(struct event *t) |
154b9e8f DL |
1121 | { |
1122 | char buf[1]; | |
1123 | ssize_t nr; | |
1124 | ||
1125 | nr = recv(daemon_ctl_sock, buf, sizeof(buf), 0); | |
1126 | if (nr < 0 && (errno == EINTR || errno == EAGAIN)) | |
1127 | goto out; | |
1128 | if (nr <= 0) | |
cc9f21da | 1129 | return; |
154b9e8f DL |
1130 | |
1131 | switch (buf[0]) { | |
996c9314 | 1132 | case 'S': /* SIGTSTP */ |
154b9e8f | 1133 | vty_stdio_suspend(); |
e339d7c0 | 1134 | if (send(daemon_ctl_sock, "s", 1, 0) < 0) |
1135 | zlog_err("%s send(\"s\") error (SIGTSTP propagation)", | |
1136 | (di && di->name ? di->name : "")); | |
154b9e8f | 1137 | break; |
996c9314 | 1138 | case 'R': /* SIGTCNT [implicit] */ |
154b9e8f DL |
1139 | vty_stdio_resume(); |
1140 | break; | |
996c9314 | 1141 | case 'I': /* SIGINT */ |
154b9e8f DL |
1142 | di->daemon_mode = false; |
1143 | raise(SIGINT); | |
1144 | break; | |
996c9314 | 1145 | case 'Q': /* SIGQUIT */ |
154b9e8f DL |
1146 | di->daemon_mode = true; |
1147 | vty_stdio_close(); | |
1148 | break; | |
cff2b211 | 1149 | } |
154b9e8f DL |
1150 | |
1151 | out: | |
907a2395 DS |
1152 | event_add_read(master, frr_daemon_ctl, NULL, daemon_ctl_sock, |
1153 | &daemon_ctl_thread); | |
cff2b211 DL |
1154 | } |
1155 | ||
0a7c7856 DL |
1156 | void frr_detach(void) |
1157 | { | |
1158 | nodetach_daemon = false; | |
1159 | frr_check_detach(); | |
1160 | } | |
1161 | ||
cd9d0537 | 1162 | void frr_run(struct event_loop *master) |
16077f2f DL |
1163 | { |
1164 | char instanceinfo[64] = ""; | |
1165 | ||
5ba56130 CH |
1166 | if (!(di->flags & FRR_MANUAL_VTY_START)) |
1167 | frr_vty_serv_start(); | |
16077f2f DL |
1168 | |
1169 | if (di->instance) | |
1170 | snprintf(instanceinfo, sizeof(instanceinfo), "instance %u ", | |
d62a17ae | 1171 | di->instance); |
1172 | ||
1173 | zlog_notice("%s %s starting: %svty@%d%s", di->name, FRR_VERSION, | |
1174 | instanceinfo, di->vty_port, di->startinfo); | |
16077f2f | 1175 | |
cff2b211 | 1176 | if (di->terminal) { |
0a7c7856 DL |
1177 | nodetach_term = true; |
1178 | ||
cff2b211 | 1179 | vty_stdio(frr_terminal_close); |
154b9e8f DL |
1180 | if (daemon_ctl_sock != -1) { |
1181 | set_nonblocking(daemon_ctl_sock); | |
907a2395 DS |
1182 | event_add_read(master, frr_daemon_ctl, NULL, |
1183 | daemon_ctl_sock, &daemon_ctl_thread); | |
154b9e8f | 1184 | } |
eef3d030 | 1185 | } else if (di->daemon_mode) { |
c9c8d0d1 | 1186 | int nullfd = open("/dev/null", O_RDONLY | O_NOCTTY); |
993bab89 | 1187 | if (nullfd == -1) { |
450971aa | 1188 | flog_err_sys(EC_LIB_SYSTEM_CALL, |
09c866e3 QY |
1189 | "%s: failed to open /dev/null: %s", |
1190 | __func__, safe_strerror(errno)); | |
993bab89 | 1191 | } else { |
4d28aea9 LB |
1192 | int fd; |
1193 | /* | |
1194 | * only redirect stdin, stdout, stderr to null when a | |
1195 | * tty also don't redirect when stdout is set with --log | |
1196 | * stdout | |
1197 | */ | |
1198 | for (fd = 2; fd >= 0; fd--) | |
1199 | if (isatty(fd) && | |
1200 | (fd != STDOUT_FILENO || !logging_to_stdout)) | |
1201 | dup2(nullfd, fd); | |
993bab89 RW |
1202 | close(nullfd); |
1203 | } | |
c9c8d0d1 | 1204 | |
0a7c7856 | 1205 | frr_check_detach(); |
f43fbf83 DL |
1206 | } |
1207 | ||
d34cb7f0 | 1208 | /* end fixed stderr startup logging */ |
0bdeb5e5 | 1209 | zlog_startup_end(); |
d34cb7f0 | 1210 | |
e6685141 | 1211 | struct event thread; |
de2754be DS |
1212 | while (event_fetch(master, &thread)) |
1213 | event_call(&thread); | |
16077f2f | 1214 | } |
03951374 DL |
1215 | |
1216 | void frr_early_fini(void) | |
1217 | { | |
1218 | hook_call(frr_early_fini); | |
1219 | } | |
1220 | ||
1221 | void frr_fini(void) | |
1222 | { | |
9eed278b DL |
1223 | FILE *fp; |
1224 | char filename[128]; | |
4d28aea9 | 1225 | int have_leftovers = 0; |
9eed278b | 1226 | |
03951374 DL |
1227 | hook_call(frr_fini); |
1228 | ||
03951374 DL |
1229 | vty_terminate(); |
1230 | cmd_terminate(); | |
1c2facd1 RW |
1231 | nb_terminate(); |
1232 | yang_terminate(); | |
1233 | #ifdef HAVE_SQLITE3 | |
1234 | db_close(); | |
1235 | #endif | |
85cd2f9f | 1236 | log_ref_fini(); |
ca28a0f6 DS |
1237 | |
1238 | #ifdef HAVE_SCRIPTING | |
1239 | frrscript_fini(); | |
1240 | #endif | |
b9b4c061 | 1241 | frr_pthread_finish(); |
03951374 DL |
1242 | zprivs_terminate(di->privs); |
1243 | /* signal_init -> nothing needed */ | |
ce50d11c | 1244 | event_master_free(master); |
e5716b16 | 1245 | master = NULL; |
0bdeb5e5 DL |
1246 | zlog_tls_buffer_fini(); |
1247 | zlog_fini(); | |
03951374 | 1248 | /* frrmod_init -> nothing needed / hooks */ |
3e41733f | 1249 | rcu_shutdown(); |
9eed278b | 1250 | |
4d28aea9 LB |
1251 | /* also log memstats to stderr when stderr goes to a file*/ |
1252 | if (debug_memstats_at_exit || !isatty(STDERR_FILENO)) | |
1253 | have_leftovers = log_memstats(stderr, di->name); | |
9eed278b DL |
1254 | |
1255 | /* in case we decide at runtime that we want exit-memstats for | |
4d28aea9 | 1256 | * a daemon |
9eed278b DL |
1257 | * (only do this if we actually have something to print though) |
1258 | */ | |
4d28aea9 | 1259 | if (!debug_memstats_at_exit || !have_leftovers) |
9eed278b DL |
1260 | return; |
1261 | ||
996c9314 LB |
1262 | snprintf(filename, sizeof(filename), "/tmp/frr-memstats-%s-%llu-%llu", |
1263 | di->name, (unsigned long long)getpid(), | |
9eed278b DL |
1264 | (unsigned long long)time(NULL)); |
1265 | ||
1266 | fp = fopen(filename, "w"); | |
1267 | if (fp) { | |
1268 | log_memstats(fp, di->name); | |
1269 | fclose(fp); | |
1270 | } | |
03951374 | 1271 | } |
42efb0d4 DL |
1272 | |
1273 | #ifdef INTERP | |
1274 | static const char interp[] | |
1275 | __attribute__((section(".interp"), used)) = INTERP; | |
1276 | #endif | |
1277 | /* | |
1278 | * executable entry point for libfrr.so | |
1279 | * | |
1280 | * note that libc initialization is skipped for this so the set of functions | |
1281 | * that can be called is rather limited | |
1282 | */ | |
1283 | extern void _libfrr_version(void) | |
1284 | __attribute__((visibility("hidden"), noreturn)); | |
1285 | void _libfrr_version(void) | |
1286 | { | |
1287 | const char banner[] = | |
1288 | FRR_FULL_NAME " " FRR_VERSION ".\n" | |
1289 | FRR_COPYRIGHT GIT_INFO "\n" | |
1290 | "configured with:\n " FRR_CONFIG_ARGS "\n"; | |
1291 | write(1, banner, sizeof(banner) - 1); | |
1292 | _exit(0); | |
1293 | } |