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