]>
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 | ||
336 | opt_extend(&os_always); | |
b1dc60f4 DL |
337 | if (!(di->flags & FRR_NO_SPLIT_CONFIG)) |
338 | opt_extend(&os_cfg); | |
339 | if (!(di->flags & FRR_LIMITED_CLI)) | |
340 | opt_extend(&os_fullcli); | |
341 | if (!(di->flags & FRR_NO_PID)) | |
342 | opt_extend(&os_pid); | |
4f04a76b DL |
343 | if (!(di->flags & FRR_NO_PRIVSEP)) |
344 | opt_extend(&os_user); | |
eb05883f DL |
345 | if (!(di->flags & FRR_NO_ZCLIENT)) |
346 | opt_extend(&os_zclient); | |
4f04a76b DL |
347 | if (!(di->flags & FRR_NO_TCPVTY)) |
348 | opt_extend(&os_vty); | |
0a7c7856 DL |
349 | if (di->flags & FRR_DETACH_LATER) |
350 | nodetach_daemon = true; | |
eb05883f | 351 | |
43e587c1 | 352 | frr_init_vtydir(); |
eb05883f | 353 | snprintf(config_default, sizeof(config_default), "%s/%s.conf", |
d62a17ae | 354 | frr_sysconfdir, di->name); |
eb05883f | 355 | snprintf(pidfile_default, sizeof(pidfile_default), "%s/%s.pid", |
d62a17ae | 356 | frr_vtydir, di->name); |
43e587c1 DS |
357 | snprintf(frr_zclientpath, sizeof(frr_zclientpath), |
358 | ZEBRA_SERV_PATH, "", ""); | |
1c2facd1 RW |
359 | #ifdef HAVE_SQLITE3 |
360 | snprintf(dbfile_default, sizeof(dbfile_default), "%s/%s.db", | |
361 | frr_dbdir, di->name); | |
362 | #endif | |
b85120bc DL |
363 | |
364 | strlcpy(frr_protoname, di->logname, sizeof(frr_protoname)); | |
365 | strlcpy(frr_protonameinst, di->logname, sizeof(frr_protonameinst)); | |
689f5a8c | 366 | |
1c2facd1 | 367 | di->cli_mode = FRR_CLI_CLASSIC; |
6e3253b9 DL |
368 | |
369 | /* we may be starting with extra FDs open for whatever purpose, | |
370 | * e.g. logging, some module, etc. Recording them here allows later | |
371 | * checking whether an fd is valid for such extension purposes, | |
372 | * without this we could end up e.g. logging to a BGP session fd. | |
373 | */ | |
374 | startup_fds = 0; | |
375 | for (int i = 0; i < 64; i++) { | |
376 | struct stat st; | |
377 | ||
378 | if (fstat(i, &st)) | |
379 | continue; | |
380 | if (S_ISDIR(st.st_mode) || S_ISBLK(st.st_mode)) | |
381 | continue; | |
382 | ||
383 | startup_fds |= UINT64_C(0x1) << (uint64_t)i; | |
384 | } | |
247898d5 DL |
385 | |
386 | /* note this doesn't do anything, it just grabs state, so doing it | |
387 | * early in _preinit is perfect. | |
388 | */ | |
389 | systemd_init_env(); | |
6e3253b9 DL |
390 | } |
391 | ||
392 | bool frr_is_startup_fd(int fd) | |
393 | { | |
394 | return !!(startup_fds & (UINT64_C(0x1) << (uint64_t)fd)); | |
4f04a76b DL |
395 | } |
396 | ||
397 | void frr_opt_add(const char *optstr, const struct option *longopts, | |
d62a17ae | 398 | const char *helpstr) |
4f04a76b | 399 | { |
d62a17ae | 400 | const struct optspec main_opts = {optstr, helpstr, longopts}; |
4f04a76b DL |
401 | opt_extend(&main_opts); |
402 | } | |
403 | ||
404 | void frr_help_exit(int status) | |
405 | { | |
406 | FILE *target = status ? stderr : stdout; | |
407 | ||
408 | if (status != 0) | |
409 | fprintf(stderr, "Invalid options.\n\n"); | |
410 | ||
411 | if (di->printhelp) | |
412 | di->printhelp(target); | |
413 | else | |
414 | fprintf(target, "Usage: %s [OPTION...]\n\n%s%s%s\n\n%s", | |
d62a17ae | 415 | di->progname, di->proghelp, di->copyright ? "\n\n" : "", |
416 | di->copyright ? di->copyright : "", comb_helpstr); | |
4f04a76b DL |
417 | fprintf(target, "\nReport bugs to %s\n", FRR_BUG_ADDRESS); |
418 | exit(status); | |
419 | } | |
420 | ||
30771d65 DL |
421 | struct option_chain { |
422 | struct option_chain *next; | |
423 | const char *arg; | |
424 | }; | |
80b4df3b | 425 | |
30771d65 | 426 | static struct option_chain *modules = NULL, **modnext = &modules; |
4f04a76b DL |
427 | static int errors = 0; |
428 | ||
429 | static int frr_opt(int opt) | |
430 | { | |
431 | static int vty_port_set = 0; | |
432 | static int vty_addr_set = 0; | |
30771d65 | 433 | struct option_chain *oc; |
4f04a76b DL |
434 | char *err; |
435 | ||
436 | switch (opt) { | |
437 | case 'h': | |
438 | frr_help_exit(0); | |
4f04a76b DL |
439 | case 'v': |
440 | print_version(di->progname); | |
441 | exit(0); | |
442 | break; | |
eb05883f | 443 | case 'd': |
08c2d52a | 444 | di->daemon_mode = true; |
eb05883f | 445 | break; |
30771d65 DL |
446 | case 'M': |
447 | oc = XMALLOC(MTYPE_TMP, sizeof(*oc)); | |
448 | oc->arg = optarg; | |
449 | oc->next = NULL; | |
450 | *modnext = oc; | |
451 | modnext = &oc->next; | |
452 | break; | |
ac4adef4 DL |
453 | case 'F': |
454 | if (!frr_defaults_profile_valid(optarg)) { | |
455 | const char **p; | |
456 | FILE *ofd = stderr; | |
457 | ||
458 | if (!strcmp(optarg, "help")) | |
459 | ofd = stdout; | |
460 | else | |
461 | fprintf(stderr, | |
462 | "The \"%s\" configuration profile is not valid for this FRR version.\n", | |
463 | optarg); | |
464 | ||
465 | fprintf(ofd, "Available profiles are:\n"); | |
466 | for (p = frr_defaults_profiles; *p; p++) | |
467 | fprintf(ofd, "%s%s\n", | |
468 | strcmp(*p, DFLT_NAME) ? " " : " * ", | |
469 | *p); | |
470 | ||
471 | if (ofd == stdout) | |
472 | exit(0); | |
473 | fprintf(ofd, "\n"); | |
474 | errors++; | |
475 | break; | |
476 | } | |
477 | frr_defaults_profile_set(optarg); | |
478 | break; | |
eb05883f | 479 | case 'i': |
b1dc60f4 | 480 | if (di->flags & FRR_NO_PID) |
eb05883f DL |
481 | return 1; |
482 | di->pid_file = optarg; | |
483 | break; | |
484 | case 'f': | |
b1dc60f4 | 485 | if (di->flags & FRR_NO_SPLIT_CONFIG) |
eb05883f DL |
486 | return 1; |
487 | di->config_file = optarg; | |
488 | break; | |
d1b4fc1f | 489 | case 'N': |
d1b4fc1f DL |
490 | if (di->pathspace) { |
491 | fprintf(stderr, | |
492 | "-N/--pathspace option specified more than once!\n"); | |
493 | errors++; | |
494 | break; | |
495 | } | |
43e587c1 DS |
496 | if (di->zpathspace) |
497 | fprintf(stderr, | |
f79f7a7b | 498 | "-N option overridden by -z for zebra named socket path\n"); |
43e587c1 | 499 | |
d1b4fc1f DL |
500 | if (strchr(optarg, '/') || strchr(optarg, '.')) { |
501 | fprintf(stderr, | |
502 | "slashes or dots are not permitted in the --pathspace option.\n"); | |
503 | errors++; | |
504 | break; | |
505 | } | |
506 | di->pathspace = optarg; | |
4e99f309 | 507 | |
43e587c1 DS |
508 | if (!di->zpathspace) |
509 | snprintf(frr_zclientpath, sizeof(frr_zclientpath), | |
510 | ZEBRA_SERV_PATH, "/", di->pathspace); | |
511 | snprintf(frr_vtydir, sizeof(frr_vtydir), DAEMON_VTY_DIR, "/", | |
512 | di->pathspace); | |
513 | snprintf(pidfile_default, sizeof(pidfile_default), "%s/%s.pid", | |
514 | frr_vtydir, di->name); | |
d1b4fc1f | 515 | break; |
ac2cb9bf IR |
516 | case 'o': |
517 | vrf_set_default_name(optarg); | |
518 | break; | |
1c2facd1 RW |
519 | #ifdef HAVE_SQLITE3 |
520 | case OPTION_DB_FILE: | |
b1dc60f4 | 521 | if (di->flags & FRR_NO_PID) |
1c2facd1 RW |
522 | return 1; |
523 | di->db_file = optarg; | |
524 | break; | |
525 | #endif | |
eb05883f | 526 | case 'C': |
b1dc60f4 | 527 | if (di->flags & FRR_NO_SPLIT_CONFIG) |
eb05883f | 528 | return 1; |
08c2d52a | 529 | di->dryrun = true; |
eb05883f | 530 | break; |
cff2b211 | 531 | case 't': |
b1dc60f4 | 532 | if (di->flags & FRR_LIMITED_CLI) |
cff2b211 | 533 | return 1; |
08c2d52a | 534 | di->terminal = true; |
cff2b211 | 535 | break; |
eb05883f | 536 | case 'z': |
43e587c1 DS |
537 | di->zpathspace = true; |
538 | if (di->pathspace) | |
539 | fprintf(stderr, | |
540 | "-z option overrides -N option for zebra named socket path\n"); | |
eb05883f DL |
541 | if (di->flags & FRR_NO_ZCLIENT) |
542 | return 1; | |
689f5a8c | 543 | strlcpy(frr_zclientpath, optarg, sizeof(frr_zclientpath)); |
eb05883f | 544 | break; |
4f04a76b DL |
545 | case 'A': |
546 | if (di->flags & FRR_NO_TCPVTY) | |
547 | return 1; | |
548 | if (vty_addr_set) { | |
d62a17ae | 549 | fprintf(stderr, |
550 | "-A option specified more than once!\n"); | |
4f04a76b DL |
551 | errors++; |
552 | break; | |
553 | } | |
554 | vty_addr_set = 1; | |
555 | di->vty_addr = optarg; | |
556 | break; | |
557 | case 'P': | |
558 | if (di->flags & FRR_NO_TCPVTY) | |
559 | return 1; | |
560 | if (vty_port_set) { | |
d62a17ae | 561 | fprintf(stderr, |
562 | "-P option specified more than once!\n"); | |
4f04a76b DL |
563 | errors++; |
564 | break; | |
565 | } | |
566 | vty_port_set = 1; | |
567 | di->vty_port = strtoul(optarg, &err, 0); | |
568 | if (*err || !*optarg) { | |
d62a17ae | 569 | fprintf(stderr, |
570 | "invalid port number \"%s\" for -P option\n", | |
571 | optarg); | |
4f04a76b DL |
572 | errors++; |
573 | break; | |
574 | } | |
575 | break; | |
576 | case OPTION_VTYSOCK: | |
577 | if (di->vty_sock_path) { | |
d62a17ae | 578 | fprintf(stderr, |
579 | "--vty_socket option specified more than once!\n"); | |
4f04a76b DL |
580 | errors++; |
581 | break; | |
582 | } | |
583 | di->vty_sock_path = optarg; | |
584 | break; | |
80b4df3b MW |
585 | case OPTION_MODULEDIR: |
586 | if (di->module_path) { | |
d62a17ae | 587 | fprintf(stderr, |
588 | "----moduledir option specified more than once!\n"); | |
80b4df3b MW |
589 | errors++; |
590 | break; | |
591 | } | |
592 | di->module_path = optarg; | |
593 | break; | |
e4e0229a QY |
594 | case OPTION_SCRIPTDIR: |
595 | if (di->script_path) { | |
596 | fprintf(stderr, "--scriptdir option specified more than once!\n"); | |
597 | errors++; | |
598 | break; | |
599 | } | |
600 | di->script_path = optarg; | |
601 | break; | |
1c2facd1 RW |
602 | case OPTION_TCLI: |
603 | di->cli_mode = FRR_CLI_TRANSACTIONAL; | |
604 | break; | |
4f04a76b DL |
605 | case 'u': |
606 | if (di->flags & FRR_NO_PRIVSEP) | |
607 | return 1; | |
608 | di->privs->user = optarg; | |
609 | break; | |
610 | case 'g': | |
611 | if (di->flags & FRR_NO_PRIVSEP) | |
612 | return 1; | |
613 | di->privs->group = optarg; | |
614 | break; | |
f8507817 DS |
615 | case OPTION_LOG: |
616 | di->early_logging = optarg; | |
617 | break; | |
e9b4e74a DS |
618 | case OPTION_LOGLEVEL: |
619 | di->early_loglevel = optarg; | |
620 | break; | |
2950f5da DS |
621 | case OPTION_LOGGING: |
622 | di->log_always = true; | |
623 | break; | |
1a9f340b MS |
624 | case OPTION_LIMIT_FDS: |
625 | di->limit_fds = strtoul(optarg, &err, 0); | |
626 | break; | |
4f04a76b DL |
627 | default: |
628 | return 1; | |
629 | } | |
630 | return 0; | |
631 | } | |
632 | ||
d62a17ae | 633 | int frr_getopt(int argc, char *const argv[], int *longindex) |
4f04a76b DL |
634 | { |
635 | int opt; | |
636 | int lidx; | |
637 | ||
638 | comb_next_lo->name = NULL; | |
639 | ||
640 | do { | |
641 | opt = getopt_long(argc, argv, comb_optstr, comb_lo, &lidx); | |
642 | if (frr_opt(opt)) | |
643 | break; | |
644 | } while (opt != -1); | |
645 | ||
646 | if (opt == -1 && errors) | |
647 | frr_help_exit(1); | |
648 | if (longindex) | |
649 | *longindex = lidx; | |
650 | return opt; | |
651 | } | |
652 | ||
beaa5470 DL |
653 | static void frr_mkdir(const char *path, bool strip) |
654 | { | |
655 | char buf[256]; | |
656 | mode_t prev; | |
657 | int ret; | |
658 | struct zprivs_ids_t ids; | |
659 | ||
660 | if (strip) { | |
661 | char *slash = strrchr(path, '/'); | |
662 | size_t plen; | |
663 | if (!slash) | |
664 | return; | |
665 | plen = slash - path; | |
666 | if (plen > sizeof(buf) - 1) | |
667 | return; | |
668 | memcpy(buf, path, plen); | |
669 | buf[plen] = '\0'; | |
670 | path = buf; | |
671 | } | |
672 | ||
673 | /* o+rx (..5) is needed for the frrvty group to work properly; | |
674 | * without it, users in the frrvty group can't access the vty sockets. | |
675 | */ | |
676 | prev = umask(0022); | |
677 | ret = mkdir(path, 0755); | |
678 | umask(prev); | |
679 | ||
680 | if (ret != 0) { | |
681 | /* if EEXIST, return without touching the permissions, | |
682 | * so user-set custom permissions are left in place | |
683 | */ | |
684 | if (errno == EEXIST) | |
685 | return; | |
686 | ||
1c50c1c0 QY |
687 | flog_err(EC_LIB_SYSTEM_CALL, "failed to mkdir \"%s\": %s", path, |
688 | strerror(errno)); | |
beaa5470 DL |
689 | return; |
690 | } | |
691 | ||
692 | zprivs_get_ids(&ids); | |
693 | if (chown(path, ids.uid_normal, ids.gid_normal)) | |
1c50c1c0 QY |
694 | flog_err(EC_LIB_SYSTEM_CALL, "failed to chown \"%s\": %s", path, |
695 | strerror(errno)); | |
beaa5470 DL |
696 | } |
697 | ||
52fad8f6 PZ |
698 | static void _err_print(const void *cookie, const char *errstr) |
699 | { | |
700 | const char *prefix = (const char *)cookie; | |
701 | ||
702 | fprintf(stderr, "%s: %s\n", prefix, errstr); | |
703 | } | |
704 | ||
a5b38c5b | 705 | static struct thread_master *master; |
4f04a76b DL |
706 | struct thread_master *frr_init(void) |
707 | { | |
30771d65 DL |
708 | struct option_chain *oc; |
709 | struct frrmod_runtime *module; | |
0bdeb5e5 | 710 | struct zprivs_ids_t ids; |
d1b4fc1f | 711 | char p_instance[16] = "", p_pathspace[256] = ""; |
80b4df3b MW |
712 | const char *dir; |
713 | dir = di->module_path ? di->module_path : frr_moduledir; | |
4f04a76b DL |
714 | |
715 | srandom(time(NULL)); | |
ac4adef4 | 716 | frr_defaults_apply(); |
4f04a76b | 717 | |
d1b4fc1f | 718 | if (di->instance) { |
d62a17ae | 719 | snprintf(frr_protonameinst, sizeof(frr_protonameinst), "%s[%u]", |
720 | di->logname, di->instance); | |
d1b4fc1f DL |
721 | snprintf(p_instance, sizeof(p_instance), "-%d", di->instance); |
722 | } | |
723 | if (di->pathspace) | |
b39404c1 | 724 | snprintf(p_pathspace, sizeof(p_pathspace), "%s/", |
d1b4fc1f DL |
725 | di->pathspace); |
726 | ||
36077833 | 727 | snprintf(config_default, sizeof(config_default), "%s%s%s%s.conf", |
d1b4fc1f | 728 | frr_sysconfdir, p_pathspace, di->name, p_instance); |
43e587c1 DS |
729 | snprintf(pidfile_default, sizeof(pidfile_default), "%s/%s%s.pid", |
730 | frr_vtydir, di->name, p_instance); | |
1c2facd1 RW |
731 | #ifdef HAVE_SQLITE3 |
732 | snprintf(dbfile_default, sizeof(dbfile_default), "%s/%s%s%s.db", | |
733 | frr_dbdir, p_pathspace, di->name, p_instance); | |
734 | #endif | |
b85120bc | 735 | |
37a1f2fb | 736 | zprivs_preinit(di->privs); |
0bdeb5e5 | 737 | zprivs_get_ids(&ids); |
37a1f2fb | 738 | |
0bdeb5e5 DL |
739 | zlog_init(di->progname, di->logname, di->instance, |
740 | ids.uid_normal, ids.gid_normal); | |
f8507817 | 741 | |
e9b4e74a | 742 | command_setup_early_logging(di->early_logging, di->early_loglevel); |
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; | |
779 | db_init(di->db_file); | |
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]); | |
953 | frr_daemon_wait(fds[0]); | |
954 | } | |
955 | ||
9e224e60 DS |
956 | /* |
957 | * Why is this a thread? | |
958 | * | |
959 | * The read in of config for integrated config happens *after* | |
960 | * thread execution starts( because it is passed in via a vtysh -b -n ) | |
961 | * While if you are not using integrated config we want the ability | |
962 | * to read the config in after thread execution starts, so that | |
963 | * we can match this behavior. | |
964 | */ | |
cc9f21da | 965 | static void frr_config_read_in(struct thread *t) |
4f04a76b | 966 | { |
bf645e31 DL |
967 | hook_call(frr_config_pre, master); |
968 | ||
91f9fd78 RW |
969 | if (!vty_read_config(vty_shared_candidate_config, di->config_file, |
970 | config_default) | |
971 | && di->backup_config_file) { | |
fe64533a DS |
972 | char *orig = XSTRDUP(MTYPE_TMP, host_config_get()); |
973 | ||
573de11f DS |
974 | zlog_info("Attempting to read backup config file: %s specified", |
975 | di->backup_config_file); | |
91f9fd78 RW |
976 | vty_read_config(vty_shared_candidate_config, |
977 | di->backup_config_file, config_default); | |
fe64533a DS |
978 | |
979 | host_config_set(orig); | |
980 | XFREE(MTYPE_TMP, orig); | |
573de11f | 981 | } |
1c2facd1 RW |
982 | |
983 | /* | |
91f9fd78 RW |
984 | * Automatically commit the candidate configuration after |
985 | * reading the configuration file. | |
1c2facd1 | 986 | */ |
91f9fd78 | 987 | if (frr_get_cli_mode() == FRR_CLI_TRANSACTIONAL) { |
13d6b9c1 | 988 | struct nb_context context = {}; |
df5eda3d | 989 | char errmsg[BUFSIZ] = {0}; |
91f9fd78 RW |
990 | int ret; |
991 | ||
13d6b9c1 RW |
992 | context.client = NB_CLIENT_CLI; |
993 | ret = nb_candidate_commit(&context, vty_shared_candidate_config, | |
df5eda3d RW |
994 | true, "Read configuration file", NULL, |
995 | errmsg, sizeof(errmsg)); | |
91f9fd78 | 996 | if (ret != NB_OK && ret != NB_ERR_NO_CHANGES) |
df5eda3d RW |
997 | zlog_err( |
998 | "%s: failed to read configuration file: %s (%s)", | |
999 | __func__, nb_err_name(ret), errmsg); | |
91f9fd78 | 1000 | } |
1c2facd1 | 1001 | |
2bafda27 | 1002 | hook_call(frr_config_post, master); |
9e224e60 DS |
1003 | } |
1004 | ||
1005 | void frr_config_fork(void) | |
1006 | { | |
1007 | hook_call(frr_late_init, master); | |
eb05883f | 1008 | |
b1dc60f4 | 1009 | if (!(di->flags & FRR_NO_SPLIT_CONFIG)) { |
0a7c7856 DL |
1010 | /* Don't start execution if we are in dry-run mode */ |
1011 | if (di->dryrun) { | |
1012 | frr_config_read_in(NULL); | |
1013 | exit(0); | |
1014 | } | |
9e224e60 | 1015 | |
0a7c7856 DL |
1016 | thread_add_event(master, frr_config_read_in, NULL, 0, |
1017 | &di->read_in); | |
1018 | } | |
eb05883f | 1019 | |
154b9e8f | 1020 | if (di->daemon_mode || di->terminal) |
f43fbf83 | 1021 | frr_daemonize(); |
eb05883f | 1022 | |
38554d3a DL |
1023 | frr_is_after_fork = true; |
1024 | ||
eb05883f DL |
1025 | if (!di->pid_file) |
1026 | di->pid_file = pidfile_default; | |
d62a17ae | 1027 | pid_output(di->pid_file); |
e2be2643 | 1028 | zlog_tls_buffer_init(); |
eb05883f DL |
1029 | } |
1030 | ||
0a7c7856 | 1031 | static void frr_vty_serv(void) |
eb05883f | 1032 | { |
d62a17ae | 1033 | /* allow explicit override of vty_path in the future |
eb05883f DL |
1034 | * (not currently set anywhere) */ |
1035 | if (!di->vty_path) { | |
1036 | const char *dir; | |
d1b4fc1f DL |
1037 | char defvtydir[256]; |
1038 | ||
43e587c1 | 1039 | snprintf(defvtydir, sizeof(defvtydir), "%s", frr_vtydir); |
d1b4fc1f DL |
1040 | |
1041 | dir = di->vty_sock_path ? di->vty_sock_path : defvtydir; | |
eb05883f DL |
1042 | |
1043 | if (di->instance) | |
1044 | snprintf(vtypath_default, sizeof(vtypath_default), | |
d62a17ae | 1045 | "%s/%s-%d.vty", dir, di->name, di->instance); |
eb05883f DL |
1046 | else |
1047 | snprintf(vtypath_default, sizeof(vtypath_default), | |
d62a17ae | 1048 | "%s/%s.vty", dir, di->name); |
eb05883f DL |
1049 | |
1050 | di->vty_path = vtypath_default; | |
1051 | } | |
1052 | ||
1053 | vty_serv_sock(di->vty_addr, di->vty_port, di->vty_path); | |
4f04a76b DL |
1054 | } |
1055 | ||
0a7c7856 DL |
1056 | static void frr_check_detach(void) |
1057 | { | |
1058 | if (nodetach_term || nodetach_daemon) | |
1059 | return; | |
1060 | ||
1061 | if (daemon_ctl_sock != -1) | |
1062 | close(daemon_ctl_sock); | |
1063 | daemon_ctl_sock = -1; | |
1064 | } | |
1065 | ||
154b9e8f | 1066 | static void frr_terminal_close(int isexit) |
cff2b211 | 1067 | { |
993bab89 RW |
1068 | int nullfd; |
1069 | ||
0a7c7856 DL |
1070 | nodetach_term = false; |
1071 | frr_check_detach(); | |
154b9e8f DL |
1072 | |
1073 | if (!di->daemon_mode || isexit) { | |
cff2b211 | 1074 | printf("\n%s exiting\n", di->name); |
154b9e8f DL |
1075 | if (!isexit) |
1076 | raise(SIGINT); | |
1077 | return; | |
cff2b211 DL |
1078 | } else { |
1079 | printf("\n%s daemonizing\n", di->name); | |
1080 | fflush(stdout); | |
1081 | } | |
1082 | ||
993bab89 RW |
1083 | nullfd = open("/dev/null", O_RDONLY | O_NOCTTY); |
1084 | if (nullfd == -1) { | |
450971aa | 1085 | flog_err_sys(EC_LIB_SYSTEM_CALL, |
09c866e3 QY |
1086 | "%s: failed to open /dev/null: %s", __func__, |
1087 | safe_strerror(errno)); | |
993bab89 RW |
1088 | } else { |
1089 | dup2(nullfd, 0); | |
1090 | dup2(nullfd, 1); | |
1091 | dup2(nullfd, 2); | |
1092 | close(nullfd); | |
1093 | } | |
154b9e8f | 1094 | } |
cff2b211 | 1095 | |
154b9e8f DL |
1096 | static struct thread *daemon_ctl_thread = NULL; |
1097 | ||
cc9f21da | 1098 | static void frr_daemon_ctl(struct thread *t) |
154b9e8f DL |
1099 | { |
1100 | char buf[1]; | |
1101 | ssize_t nr; | |
1102 | ||
1103 | nr = recv(daemon_ctl_sock, buf, sizeof(buf), 0); | |
1104 | if (nr < 0 && (errno == EINTR || errno == EAGAIN)) | |
1105 | goto out; | |
1106 | if (nr <= 0) | |
cc9f21da | 1107 | return; |
154b9e8f DL |
1108 | |
1109 | switch (buf[0]) { | |
996c9314 | 1110 | case 'S': /* SIGTSTP */ |
154b9e8f | 1111 | vty_stdio_suspend(); |
e339d7c0 | 1112 | if (send(daemon_ctl_sock, "s", 1, 0) < 0) |
1113 | zlog_err("%s send(\"s\") error (SIGTSTP propagation)", | |
1114 | (di && di->name ? di->name : "")); | |
154b9e8f | 1115 | break; |
996c9314 | 1116 | case 'R': /* SIGTCNT [implicit] */ |
154b9e8f DL |
1117 | vty_stdio_resume(); |
1118 | break; | |
996c9314 | 1119 | case 'I': /* SIGINT */ |
154b9e8f DL |
1120 | di->daemon_mode = false; |
1121 | raise(SIGINT); | |
1122 | break; | |
996c9314 | 1123 | case 'Q': /* SIGQUIT */ |
154b9e8f DL |
1124 | di->daemon_mode = true; |
1125 | vty_stdio_close(); | |
1126 | break; | |
cff2b211 | 1127 | } |
154b9e8f DL |
1128 | |
1129 | out: | |
1130 | thread_add_read(master, frr_daemon_ctl, NULL, daemon_ctl_sock, | |
1131 | &daemon_ctl_thread); | |
cff2b211 DL |
1132 | } |
1133 | ||
0a7c7856 DL |
1134 | void frr_detach(void) |
1135 | { | |
1136 | nodetach_daemon = false; | |
1137 | frr_check_detach(); | |
1138 | } | |
1139 | ||
16077f2f DL |
1140 | void frr_run(struct thread_master *master) |
1141 | { | |
1142 | char instanceinfo[64] = ""; | |
1143 | ||
1144 | frr_vty_serv(); | |
1145 | ||
1146 | if (di->instance) | |
1147 | snprintf(instanceinfo, sizeof(instanceinfo), "instance %u ", | |
d62a17ae | 1148 | di->instance); |
1149 | ||
1150 | zlog_notice("%s %s starting: %svty@%d%s", di->name, FRR_VERSION, | |
1151 | instanceinfo, di->vty_port, di->startinfo); | |
16077f2f | 1152 | |
cff2b211 | 1153 | if (di->terminal) { |
0a7c7856 DL |
1154 | nodetach_term = true; |
1155 | ||
cff2b211 | 1156 | vty_stdio(frr_terminal_close); |
154b9e8f DL |
1157 | if (daemon_ctl_sock != -1) { |
1158 | set_nonblocking(daemon_ctl_sock); | |
1159 | thread_add_read(master, frr_daemon_ctl, NULL, | |
1160 | daemon_ctl_sock, &daemon_ctl_thread); | |
1161 | } | |
eef3d030 | 1162 | } else if (di->daemon_mode) { |
c9c8d0d1 | 1163 | int nullfd = open("/dev/null", O_RDONLY | O_NOCTTY); |
993bab89 | 1164 | if (nullfd == -1) { |
450971aa | 1165 | flog_err_sys(EC_LIB_SYSTEM_CALL, |
09c866e3 QY |
1166 | "%s: failed to open /dev/null: %s", |
1167 | __func__, safe_strerror(errno)); | |
993bab89 RW |
1168 | } else { |
1169 | dup2(nullfd, 0); | |
1170 | dup2(nullfd, 1); | |
1171 | dup2(nullfd, 2); | |
1172 | close(nullfd); | |
1173 | } | |
c9c8d0d1 | 1174 | |
0a7c7856 | 1175 | frr_check_detach(); |
f43fbf83 DL |
1176 | } |
1177 | ||
d34cb7f0 | 1178 | /* end fixed stderr startup logging */ |
0bdeb5e5 | 1179 | zlog_startup_end(); |
d34cb7f0 | 1180 | |
16077f2f DL |
1181 | struct thread thread; |
1182 | while (thread_fetch(master, &thread)) | |
1183 | thread_call(&thread); | |
1184 | } | |
03951374 DL |
1185 | |
1186 | void frr_early_fini(void) | |
1187 | { | |
1188 | hook_call(frr_early_fini); | |
1189 | } | |
1190 | ||
1191 | void frr_fini(void) | |
1192 | { | |
9eed278b DL |
1193 | FILE *fp; |
1194 | char filename[128]; | |
1195 | int have_leftovers; | |
1196 | ||
03951374 DL |
1197 | hook_call(frr_fini); |
1198 | ||
03951374 DL |
1199 | vty_terminate(); |
1200 | cmd_terminate(); | |
1c2facd1 RW |
1201 | nb_terminate(); |
1202 | yang_terminate(); | |
1203 | #ifdef HAVE_SQLITE3 | |
1204 | db_close(); | |
1205 | #endif | |
85cd2f9f | 1206 | log_ref_fini(); |
b9b4c061 | 1207 | frr_pthread_finish(); |
03951374 DL |
1208 | zprivs_terminate(di->privs); |
1209 | /* signal_init -> nothing needed */ | |
1210 | thread_master_free(master); | |
e5716b16 | 1211 | master = NULL; |
0bdeb5e5 DL |
1212 | zlog_tls_buffer_fini(); |
1213 | zlog_fini(); | |
03951374 | 1214 | /* frrmod_init -> nothing needed / hooks */ |
3e41733f | 1215 | rcu_shutdown(); |
9eed278b DL |
1216 | |
1217 | if (!debug_memstats_at_exit) | |
1218 | return; | |
1219 | ||
1220 | have_leftovers = log_memstats(stderr, di->name); | |
1221 | ||
1222 | /* in case we decide at runtime that we want exit-memstats for | |
1223 | * a daemon, but it has no stderr because it's daemonized | |
1224 | * (only do this if we actually have something to print though) | |
1225 | */ | |
1226 | if (!have_leftovers) | |
1227 | return; | |
1228 | ||
996c9314 LB |
1229 | snprintf(filename, sizeof(filename), "/tmp/frr-memstats-%s-%llu-%llu", |
1230 | di->name, (unsigned long long)getpid(), | |
9eed278b DL |
1231 | (unsigned long long)time(NULL)); |
1232 | ||
1233 | fp = fopen(filename, "w"); | |
1234 | if (fp) { | |
1235 | log_memstats(fp, di->name); | |
1236 | fclose(fp); | |
1237 | } | |
03951374 | 1238 | } |
42efb0d4 DL |
1239 | |
1240 | #ifdef INTERP | |
1241 | static const char interp[] | |
1242 | __attribute__((section(".interp"), used)) = INTERP; | |
1243 | #endif | |
1244 | /* | |
1245 | * executable entry point for libfrr.so | |
1246 | * | |
1247 | * note that libc initialization is skipped for this so the set of functions | |
1248 | * that can be called is rather limited | |
1249 | */ | |
1250 | extern void _libfrr_version(void) | |
1251 | __attribute__((visibility("hidden"), noreturn)); | |
1252 | void _libfrr_version(void) | |
1253 | { | |
1254 | const char banner[] = | |
1255 | FRR_FULL_NAME " " FRR_VERSION ".\n" | |
1256 | FRR_COPYRIGHT GIT_INFO "\n" | |
1257 | "configured with:\n " FRR_CONFIG_ARGS "\n"; | |
1258 | write(1, banner, sizeof(banner) - 1); | |
1259 | _exit(0); | |
1260 | } |