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