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