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