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