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