2 * libfrr overall management functions
4 * Copyright (C) 2016 David Lamparter for NetDEF, Inc.
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)
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
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
24 #include <sys/types.h>
32 #include "lib/version.h"
38 #include "lib_errors.h"
40 #include "northbound_cli.h"
41 #include "northbound_db.h"
44 #include "frr_pthread.h"
46 #include "frrscript.h"
49 DEFINE_HOOK(frr_early_init
, (struct thread_master
* tm
), (tm
));
50 DEFINE_HOOK(frr_late_init
, (struct thread_master
* tm
), (tm
));
51 DEFINE_HOOK(frr_config_pre
, (struct thread_master
* tm
), (tm
));
52 DEFINE_HOOK(frr_config_post
, (struct thread_master
* tm
), (tm
));
53 DEFINE_KOOH(frr_early_fini
, (), ());
54 DEFINE_KOOH(frr_fini
, (), ());
56 const char frr_sysconfdir
[] = SYSCONFDIR
;
59 const char frr_dbdir
[] = DAEMON_DB_DIR
;
61 const char frr_moduledir
[] = MODULE_PATH
;
62 const char frr_scriptdir
[] = SCRIPT_PATH
;
64 char frr_protoname
[256] = "NONE";
65 char frr_protonameinst
[256] = "NONE";
67 char config_default
[512];
68 char frr_zclientpath
[256];
69 static char pidfile_default
[1024];
71 static char dbfile_default
[512];
73 static char vtypath_default
[512];
75 /* cleared in frr_preinit(), then re-set after daemonizing */
76 bool frr_is_after_fork
= true;
77 bool debug_memstats_at_exit
= false;
78 static bool nodetach_term
, nodetach_daemon
;
79 static uint64_t startup_fds
;
81 static char comb_optstr
[256];
82 static struct option comb_lo
[64];
83 static struct option
*comb_next_lo
= &comb_lo
[0];
84 static char comb_helpstr
[4096];
89 const struct option
*longopts
;
92 static void opt_extend(const struct optspec
*os
)
94 const struct option
*lo
;
96 strlcat(comb_optstr
, os
->optstr
, sizeof(comb_optstr
));
97 strlcat(comb_helpstr
, os
->helpstr
, sizeof(comb_helpstr
));
98 for (lo
= os
->longopts
; lo
->name
; lo
++)
99 memcpy(comb_next_lo
++, lo
, sizeof(*lo
));
103 #define OPTION_VTYSOCK 1000
104 #define OPTION_MODULEDIR 1002
105 #define OPTION_LOG 1003
106 #define OPTION_LOGLEVEL 1004
107 #define OPTION_TCLI 1005
108 #define OPTION_DB_FILE 1006
109 #define OPTION_LOGGING 1007
110 #define OPTION_LIMIT_FDS 1008
111 #define OPTION_SCRIPTDIR 1009
113 static const struct option lo_always
[] = {
114 {"help", no_argument
, NULL
, 'h'},
115 {"version", no_argument
, NULL
, 'v'},
116 {"daemon", no_argument
, NULL
, 'd'},
117 {"module", no_argument
, NULL
, 'M'},
118 {"profile", required_argument
, NULL
, 'F'},
119 {"pathspace", required_argument
, NULL
, 'N'},
120 {"vrfdefaultname", required_argument
, NULL
, 'o'},
121 {"vty_socket", required_argument
, NULL
, OPTION_VTYSOCK
},
122 {"moduledir", required_argument
, NULL
, OPTION_MODULEDIR
},
123 {"scriptdir", required_argument
, NULL
, OPTION_SCRIPTDIR
},
124 {"log", required_argument
, NULL
, OPTION_LOG
},
125 {"log-level", required_argument
, NULL
, OPTION_LOGLEVEL
},
126 {"command-log-always", no_argument
, NULL
, OPTION_LOGGING
},
127 {"limit-fds", required_argument
, NULL
, OPTION_LIMIT_FDS
},
129 static const struct optspec os_always
= {
131 " -h, --help Display this help and exit\n"
132 " -v, --version Print program version\n"
133 " -d, --daemon Runs in daemon mode\n"
134 " -M, --module Load specified module\n"
135 " -F, --profile Use specified configuration profile\n"
136 " -N, --pathspace Insert prefix into config & socket paths\n"
137 " -o, --vrfdefaultname Set default VRF name.\n"
138 " --vty_socket Override vty socket path\n"
139 " --moduledir Override modules directory\n"
140 " --scriptdir Override scripts directory\n"
141 " --log Set Logging to stdout, syslog, or file:<name>\n"
142 " --log-level Set Logging Level to use, debug, info, warn, etc\n"
143 " --limit-fds Limit number of fds supported\n",
147 static const struct option lo_cfg
[] = {
148 {"config_file", required_argument
, NULL
, 'f'},
149 {"dryrun", no_argument
, NULL
, 'C'},
151 static const struct optspec os_cfg
= {
153 " -f, --config_file Set configuration file name\n"
154 " -C, --dryrun Check configuration for validity and exit\n",
158 static const struct option lo_fullcli
[] = {
159 {"terminal", no_argument
, NULL
, 't'},
160 {"tcli", no_argument
, NULL
, OPTION_TCLI
},
162 {"db_file", required_argument
, NULL
, OPTION_DB_FILE
},
165 static const struct optspec os_fullcli
= {
167 " --tcli Use transaction-based CLI\n"
168 " -t, --terminal Open terminal session on stdio\n"
169 " -d -t Daemonize after terminal session ends\n",
173 static const struct option lo_pid
[] = {
174 {"pid_file", required_argument
, NULL
, 'i'},
176 static const struct optspec os_pid
= {
178 " -i, --pid_file Set process identifier file name\n",
182 static const struct option lo_zclient
[] = {
183 {"socket", required_argument
, NULL
, 'z'},
185 static const struct optspec os_zclient
= {
186 "z:", " -z, --socket Set path of zebra socket\n", lo_zclient
};
189 static const struct option lo_vty
[] = {
190 {"vty_addr", required_argument
, NULL
, 'A'},
191 {"vty_port", required_argument
, NULL
, 'P'},
193 static const struct optspec os_vty
= {
195 " -A, --vty_addr Set vty's bind address\n"
196 " -P, --vty_port Set vty's port number\n",
200 static const struct option lo_user
[] = {{"user", required_argument
, NULL
, 'u'},
201 {"group", required_argument
, NULL
, 'g'},
203 static const struct optspec os_user
= {"u:g:",
204 " -u, --user User to run as\n"
205 " -g, --group Group to run as\n",
208 bool frr_zclient_addr(struct sockaddr_storage
*sa
, socklen_t
*sa_len
,
211 memset(sa
, 0, sizeof(*sa
));
214 path
= frr_zclientpath
;
216 if (!strncmp(path
, ZAPI_TCP_PATHNAME
, strlen(ZAPI_TCP_PATHNAME
))) {
217 /* note: this functionality is disabled at bottom */
219 int port
= ZEBRA_PORT
;
221 struct sockaddr_in
*sin
= NULL
;
222 struct sockaddr_in6
*sin6
= NULL
;
224 path
+= strlen(ZAPI_TCP_PATHNAME
);
244 port
= strtoul(path
, &err
, 10);
255 sin
= (struct sockaddr_in
*)sa
;
256 sin
->sin_port
= htons(port
);
257 sin
->sin_addr
.s_addr
= htonl(INADDR_LOOPBACK
);
258 *sa_len
= sizeof(struct sockaddr_in
);
259 #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
260 sin
->sin_len
= *sa_len
;
264 sin6
= (struct sockaddr_in6
*)sa
;
265 sin6
->sin6_port
= htons(port
);
266 inet_pton(AF_INET6
, "::1", &sin6
->sin6_addr
);
267 *sa_len
= sizeof(struct sockaddr_in6
);
269 sin6
->sin6_len
= *sa_len
;
275 /* force-disable this path, because tcp-zebra is a
276 * SECURITY ISSUE. there are no checks at all against
277 * untrusted users on the local system connecting on TCP
278 * and injecting bogus routing data into the entire routing
281 * The functionality is only left here because it may be
282 * useful during development, in order to be able to get
283 * tcpdump or wireshark watching ZAPI as TCP. If you want
284 * to do that, flip the #if 1 above to #if 0. */
285 memset(sa
, 0, sizeof(*sa
));
289 /* "sun" is a #define on solaris */
290 struct sockaddr_un
*suna
= (struct sockaddr_un
*)sa
;
292 suna
->sun_family
= AF_UNIX
;
293 strlcpy(suna
->sun_path
, path
, sizeof(suna
->sun_path
));
294 #ifdef HAVE_STRUCT_SOCKADDR_UN_SUN_LEN
295 *sa_len
= suna
->sun_len
= SUN_LEN(suna
);
297 *sa_len
= sizeof(suna
->sun_family
) + strlen(suna
->sun_path
);
298 #endif /* HAVE_STRUCT_SOCKADDR_UN_SUN_LEN */
300 /* this is left here for future reference; Linux abstract
301 * socket namespace support can be enabled by replacing
302 * above #if 0 with #ifdef GNU_LINUX.
304 * THIS IS A SECURITY ISSUE, the abstract socket namespace
305 * does not have user/group permission control on sockets.
306 * we'd need to implement SCM_CREDENTIALS support first to
307 * check that only proper users can connect to abstract
308 * sockets. (same problem as tcp-zebra, except there is a
309 * fix with SCM_CREDENTIALS. tcp-zebra has no such fix.)
311 if (suna
->sun_path
[0] == '@')
312 suna
->sun_path
[0] = '\0';
318 static struct frr_daemon_info
*di
= NULL
;
320 void frr_init_vtydir(void)
322 snprintf(frr_vtydir
, sizeof(frr_vtydir
), DAEMON_VTY_DIR
, "", "");
325 void frr_preinit(struct frr_daemon_info
*daemon
, int argc
, char **argv
)
328 frr_is_after_fork
= false;
330 /* basename(), opencoded. */
331 char *p
= strrchr(argv
[0], '/');
332 di
->progname
= p
? p
+ 1 : argv
[0];
336 log_args_init(daemon
->early_logging
);
338 opt_extend(&os_always
);
339 if (!(di
->flags
& FRR_NO_SPLIT_CONFIG
))
341 if (!(di
->flags
& FRR_LIMITED_CLI
))
342 opt_extend(&os_fullcli
);
343 if (!(di
->flags
& FRR_NO_PID
))
345 if (!(di
->flags
& FRR_NO_PRIVSEP
))
346 opt_extend(&os_user
);
347 if (!(di
->flags
& FRR_NO_ZCLIENT
))
348 opt_extend(&os_zclient
);
349 if (!(di
->flags
& FRR_NO_TCPVTY
))
351 if (di
->flags
& FRR_DETACH_LATER
)
352 nodetach_daemon
= true;
355 snprintf(config_default
, sizeof(config_default
), "%s/%s.conf",
356 frr_sysconfdir
, di
->name
);
357 snprintf(pidfile_default
, sizeof(pidfile_default
), "%s/%s.pid",
358 frr_vtydir
, di
->name
);
359 snprintf(frr_zclientpath
, sizeof(frr_zclientpath
),
360 ZEBRA_SERV_PATH
, "", "");
362 snprintf(dbfile_default
, sizeof(dbfile_default
), "%s/%s.db",
363 frr_dbdir
, di
->name
);
366 strlcpy(frr_protoname
, di
->logname
, sizeof(frr_protoname
));
367 strlcpy(frr_protonameinst
, di
->logname
, sizeof(frr_protonameinst
));
369 di
->cli_mode
= FRR_CLI_CLASSIC
;
371 /* we may be starting with extra FDs open for whatever purpose,
372 * e.g. logging, some module, etc. Recording them here allows later
373 * checking whether an fd is valid for such extension purposes,
374 * without this we could end up e.g. logging to a BGP session fd.
377 for (int i
= 0; i
< 64; i
++) {
382 if (S_ISDIR(st
.st_mode
) || S_ISBLK(st
.st_mode
))
385 startup_fds
|= UINT64_C(0x1) << (uint64_t)i
;
388 /* note this doesn't do anything, it just grabs state, so doing it
389 * early in _preinit is perfect.
394 bool frr_is_startup_fd(int fd
)
396 return !!(startup_fds
& (UINT64_C(0x1) << (uint64_t)fd
));
399 void frr_opt_add(const char *optstr
, const struct option
*longopts
,
402 const struct optspec main_opts
= {optstr
, helpstr
, longopts
};
403 opt_extend(&main_opts
);
406 void frr_help_exit(int status
)
408 FILE *target
= status
? stderr
: stdout
;
411 fprintf(stderr
, "Invalid options.\n\n");
414 di
->printhelp(target
);
416 fprintf(target
, "Usage: %s [OPTION...]\n\n%s%s%s\n\n%s",
417 di
->progname
, di
->proghelp
, di
->copyright
? "\n\n" : "",
418 di
->copyright
? di
->copyright
: "", comb_helpstr
);
419 fprintf(target
, "\nReport bugs to %s\n", FRR_BUG_ADDRESS
);
423 struct option_chain
{
424 struct option_chain
*next
;
428 static struct option_chain
*modules
= NULL
, **modnext
= &modules
;
429 static int errors
= 0;
431 static int frr_opt(int opt
)
433 static int vty_port_set
= 0;
434 static int vty_addr_set
= 0;
435 struct option_chain
*oc
;
436 struct log_arg
*log_arg
;
444 print_version(di
->progname
);
448 di
->daemon_mode
= true;
451 oc
= XMALLOC(MTYPE_TMP
, sizeof(*oc
));
458 if (!frr_defaults_profile_valid(optarg
)) {
462 if (!strcmp(optarg
, "help"))
466 "The \"%s\" configuration profile is not valid for this FRR version.\n",
469 fprintf(ofd
, "Available profiles are:\n");
470 for (p
= frr_defaults_profiles
; *p
; p
++)
471 fprintf(ofd
, "%s%s\n",
472 strcmp(*p
, DFLT_NAME
) ? " " : " * ",
481 frr_defaults_profile_set(optarg
);
484 if (di
->flags
& FRR_NO_PID
)
486 di
->pid_file
= optarg
;
489 if (di
->flags
& FRR_NO_SPLIT_CONFIG
)
491 di
->config_file
= optarg
;
496 "-N/--pathspace option specified more than once!\n");
502 "-N option overridden by -z for zebra named socket path\n");
504 if (strchr(optarg
, '/') || strchr(optarg
, '.')) {
506 "slashes or dots are not permitted in the --pathspace option.\n");
510 di
->pathspace
= optarg
;
513 snprintf(frr_zclientpath
, sizeof(frr_zclientpath
),
514 ZEBRA_SERV_PATH
, "/", di
->pathspace
);
515 snprintf(frr_vtydir
, sizeof(frr_vtydir
), DAEMON_VTY_DIR
, "/",
517 snprintf(pidfile_default
, sizeof(pidfile_default
), "%s/%s.pid",
518 frr_vtydir
, di
->name
);
521 vrf_set_default_name(optarg
);
525 if (di
->flags
& FRR_NO_PID
)
527 di
->db_file
= optarg
;
531 if (di
->flags
& FRR_NO_SPLIT_CONFIG
)
536 if (di
->flags
& FRR_LIMITED_CLI
)
541 di
->zpathspace
= true;
544 "-z option overrides -N option for zebra named socket path\n");
545 if (di
->flags
& FRR_NO_ZCLIENT
)
547 strlcpy(frr_zclientpath
, optarg
, sizeof(frr_zclientpath
));
550 if (di
->flags
& FRR_NO_TCPVTY
)
554 "-A option specified more than once!\n");
559 di
->vty_addr
= optarg
;
562 if (di
->flags
& FRR_NO_TCPVTY
)
566 "-P option specified more than once!\n");
571 di
->vty_port
= strtoul(optarg
, &err
, 0);
572 if (*err
|| !*optarg
) {
574 "invalid port number \"%s\" for -P option\n",
581 if (di
->vty_sock_path
) {
583 "--vty_socket option specified more than once!\n");
587 di
->vty_sock_path
= optarg
;
589 case OPTION_MODULEDIR
:
590 if (di
->module_path
) {
592 "----moduledir option specified more than once!\n");
596 di
->module_path
= optarg
;
598 case OPTION_SCRIPTDIR
:
599 if (di
->script_path
) {
600 fprintf(stderr
, "--scriptdir option specified more than once!\n");
604 di
->script_path
= optarg
;
607 di
->cli_mode
= FRR_CLI_TRANSACTIONAL
;
610 if (di
->flags
& FRR_NO_PRIVSEP
)
612 di
->privs
->user
= optarg
;
615 if (di
->flags
& FRR_NO_PRIVSEP
)
617 di
->privs
->group
= optarg
;
620 arg_len
= strlen(optarg
) + 1;
621 log_arg
= XCALLOC(MTYPE_TMP
, sizeof(*log_arg
) + arg_len
);
622 memcpy(log_arg
->target
, optarg
, arg_len
);
623 log_args_add_tail(di
->early_logging
, log_arg
);
625 case OPTION_LOGLEVEL
:
626 di
->early_loglevel
= optarg
;
629 di
->log_always
= true;
631 case OPTION_LIMIT_FDS
:
632 di
->limit_fds
= strtoul(optarg
, &err
, 0);
640 int frr_getopt(int argc
, char *const argv
[], int *longindex
)
645 comb_next_lo
->name
= NULL
;
648 opt
= getopt_long(argc
, argv
, comb_optstr
, comb_lo
, &lidx
);
653 if (opt
== -1 && errors
)
660 static void frr_mkdir(const char *path
, bool strip
)
665 struct zprivs_ids_t ids
;
668 char *slash
= strrchr(path
, '/');
673 if (plen
> sizeof(buf
) - 1)
675 memcpy(buf
, path
, plen
);
680 /* o+rx (..5) is needed for the frrvty group to work properly;
681 * without it, users in the frrvty group can't access the vty sockets.
684 ret
= mkdir(path
, 0755);
688 /* if EEXIST, return without touching the permissions,
689 * so user-set custom permissions are left in place
694 flog_err(EC_LIB_SYSTEM_CALL
, "failed to mkdir \"%s\": %s", path
,
699 zprivs_get_ids(&ids
);
700 if (chown(path
, ids
.uid_normal
, ids
.gid_normal
))
701 flog_err(EC_LIB_SYSTEM_CALL
, "failed to chown \"%s\": %s", path
,
705 static void _err_print(const void *cookie
, const char *errstr
)
707 const char *prefix
= (const char *)cookie
;
709 fprintf(stderr
, "%s: %s\n", prefix
, errstr
);
712 static struct thread_master
*master
;
713 struct thread_master
*frr_init(void)
715 struct option_chain
*oc
;
716 struct log_arg
*log_arg
;
717 struct frrmod_runtime
*module
;
718 struct zprivs_ids_t ids
;
719 char p_instance
[16] = "", p_pathspace
[256] = "";
722 dir
= di
->module_path
? di
->module_path
: frr_moduledir
;
725 frr_defaults_apply();
728 snprintf(frr_protonameinst
, sizeof(frr_protonameinst
), "%s[%u]",
729 di
->logname
, di
->instance
);
730 snprintf(p_instance
, sizeof(p_instance
), "-%d", di
->instance
);
733 snprintf(p_pathspace
, sizeof(p_pathspace
), "%s/",
736 snprintf(config_default
, sizeof(config_default
), "%s%s%s%s.conf",
737 frr_sysconfdir
, p_pathspace
, di
->name
, p_instance
);
738 snprintf(pidfile_default
, sizeof(pidfile_default
), "%s/%s%s.pid",
739 frr_vtydir
, di
->name
, p_instance
);
741 snprintf(dbfile_default
, sizeof(dbfile_default
), "%s/%s%s%s.db",
742 frr_dbdir
, p_pathspace
, di
->name
, p_instance
);
745 zprivs_preinit(di
->privs
);
746 zprivs_get_ids(&ids
);
748 zlog_init(di
->progname
, di
->logname
, di
->instance
,
749 ids
.uid_normal
, ids
.gid_normal
);
751 while ((log_arg
= log_args_pop(di
->early_logging
))) {
752 command_setup_early_logging(log_arg
->target
,
754 XFREE(MTYPE_TMP
, log_arg
);
757 if (!frr_zclient_addr(&zclient_addr
, &zclient_addr_len
,
759 fprintf(stderr
, "Invalid zserv socket path: %s\n",
764 /* don't mkdir these as root... */
765 if (!(di
->flags
& FRR_NO_PRIVSEP
)) {
766 if (!di
->pid_file
|| !di
->vty_path
)
767 frr_mkdir(frr_vtydir
, false);
769 frr_mkdir(di
->pid_file
, true);
771 frr_mkdir(di
->vty_path
, true);
774 frrmod_init(di
->module
);
776 modules
= (oc
= modules
)->next
;
777 module
= frrmod_load(oc
->arg
, dir
, _err_print
, __func__
);
780 XFREE(MTYPE_TMP
, oc
);
783 zprivs_init(di
->privs
);
785 master
= thread_master_create(NULL
);
786 signal_init(master
, di
->n_signals
, di
->signals
);
787 hook_call(frr_early_init
, master
);
791 di
->db_file
= dbfile_default
;
792 db_init("%s", di
->db_file
);
795 if (di
->flags
& FRR_LIMITED_CLI
)
800 vty_init(master
, di
->log_always
);
804 #ifdef HAVE_SCRIPTING
805 frrscript_init(di
->script_path
? di
->script_path
: frr_scriptdir
);
812 nb_init(master
, di
->yang_modules
, di
->n_yang_modules
, true);
813 if (nb_db_init() != NB_OK
)
814 flog_warn(EC_LIB_NB_DATABASE
,
815 "%s: failed to initialize northbound database",
823 const char *frr_get_progname(void)
825 return di
? di
->progname
: NULL
;
828 enum frr_cli_mode
frr_get_cli_mode(void)
830 return di
? di
->cli_mode
: FRR_CLI_CLASSIC
;
833 uint32_t frr_get_fd_limit(void)
835 return di
? di
->limit_fds
: 0;
838 static int rcvd_signal
= 0;
840 static void rcv_signal(int signum
)
842 rcvd_signal
= signum
;
843 /* poll() is interrupted by the signal; handled below */
846 static void frr_daemon_wait(int fd
)
848 struct pollfd pfd
[1];
852 sigset_t sigs
, prevsigs
;
855 sigaddset(&sigs
, SIGTSTP
);
856 sigaddset(&sigs
, SIGQUIT
);
857 sigaddset(&sigs
, SIGINT
);
858 sigprocmask(SIG_BLOCK
, &sigs
, &prevsigs
);
860 struct sigaction sa
= {
861 .sa_handler
= rcv_signal
, .sa_flags
= SA_RESETHAND
,
863 sigemptyset(&sa
.sa_mask
);
864 sigaction(SIGTSTP
, &sa
, NULL
);
865 sigaction(SIGQUIT
, &sa
, NULL
);
866 sigaction(SIGINT
, &sa
, NULL
);
873 pfd
[0].events
= POLLIN
;
877 #if defined(HAVE_PPOLL)
878 ret
= ppoll(pfd
, 1, NULL
, &prevsigs
);
879 #elif defined(HAVE_POLLTS)
880 ret
= pollts(pfd
, 1, NULL
, &prevsigs
);
882 /* racy -- only used on FreeBSD 9 */
884 sigprocmask(SIG_SETMASK
, &prevsigs
, &tmpsigs
);
885 ret
= poll(pfd
, 1, -1);
886 sigprocmask(SIG_SETMASK
, &tmpsigs
, NULL
);
888 if (ret
< 0 && errno
!= EINTR
&& errno
!= EAGAIN
) {
892 switch (rcvd_signal
) {
896 nrecv
= recv(fd
, buf
, sizeof(buf
), 0);
898 && (errno
== EINTR
|| errno
== EAGAIN
));
901 sigaction(SIGTSTP
, &sa
, NULL
);
913 exitpid
= waitpid(-1, &exitstat
, WNOHANG
);
915 /* child successfully went to main loop & closed socket */
918 /* child failed one way or another ... */
919 if (WIFEXITED(exitstat
) && WEXITSTATUS(exitstat
) == 0)
920 /* can happen in --terminal case if exit is fast enough */
922 else if (WIFEXITED(exitstat
))
923 fprintf(stderr
, "%s failed to start, exited %d\n", di
->name
,
924 WEXITSTATUS(exitstat
));
925 else if (WIFSIGNALED(exitstat
))
926 fprintf(stderr
, "%s crashed in startup, signal %d\n", di
->name
,
929 fprintf(stderr
, "%s failed to start, unknown problem\n",
934 static int daemon_ctl_sock
= -1;
936 static void frr_daemonize(void)
941 if (socketpair(AF_UNIX
, SOCK_STREAM
, 0, fds
)) {
942 perror("socketpair() for daemon control");
961 daemon_ctl_sock
= fds
[1];
968 frr_daemon_wait(fds
[0]);
972 * Why is this a thread?
974 * The read in of config for integrated config happens *after*
975 * thread execution starts( because it is passed in via a vtysh -b -n )
976 * While if you are not using integrated config we want the ability
977 * to read the config in after thread execution starts, so that
978 * we can match this behavior.
980 static void frr_config_read_in(struct thread
*t
)
982 hook_call(frr_config_pre
, master
);
984 if (!vty_read_config(vty_shared_candidate_config
, di
->config_file
,
986 && di
->backup_config_file
) {
987 char *orig
= XSTRDUP(MTYPE_TMP
, host_config_get());
989 zlog_info("Attempting to read backup config file: %s specified",
990 di
->backup_config_file
);
991 vty_read_config(vty_shared_candidate_config
,
992 di
->backup_config_file
, config_default
);
994 host_config_set(orig
);
995 XFREE(MTYPE_TMP
, orig
);
999 * Automatically commit the candidate configuration after
1000 * reading the configuration file.
1002 if (frr_get_cli_mode() == FRR_CLI_TRANSACTIONAL
) {
1003 struct nb_context context
= {};
1004 char errmsg
[BUFSIZ
] = {0};
1007 context
.client
= NB_CLIENT_CLI
;
1008 ret
= nb_candidate_commit(&context
, vty_shared_candidate_config
,
1009 true, "Read configuration file", NULL
,
1010 errmsg
, sizeof(errmsg
));
1011 if (ret
!= NB_OK
&& ret
!= NB_ERR_NO_CHANGES
)
1013 "%s: failed to read configuration file: %s (%s)",
1014 __func__
, nb_err_name(ret
), errmsg
);
1017 hook_call(frr_config_post
, master
);
1020 void frr_config_fork(void)
1022 hook_call(frr_late_init
, master
);
1024 if (!(di
->flags
& FRR_NO_SPLIT_CONFIG
)) {
1025 /* Don't start execution if we are in dry-run mode */
1027 frr_config_read_in(NULL
);
1031 thread_add_event(master
, frr_config_read_in
, NULL
, 0,
1035 if (di
->daemon_mode
|| di
->terminal
)
1038 frr_is_after_fork
= true;
1041 di
->pid_file
= pidfile_default
;
1042 pid_output(di
->pid_file
);
1043 zlog_tls_buffer_init();
1046 static void frr_vty_serv(void)
1048 /* allow explicit override of vty_path in the future
1049 * (not currently set anywhere) */
1050 if (!di
->vty_path
) {
1052 char defvtydir
[256];
1054 snprintf(defvtydir
, sizeof(defvtydir
), "%s", frr_vtydir
);
1056 dir
= di
->vty_sock_path
? di
->vty_sock_path
: defvtydir
;
1059 snprintf(vtypath_default
, sizeof(vtypath_default
),
1060 "%s/%s-%d.vty", dir
, di
->name
, di
->instance
);
1062 snprintf(vtypath_default
, sizeof(vtypath_default
),
1063 "%s/%s.vty", dir
, di
->name
);
1065 di
->vty_path
= vtypath_default
;
1068 vty_serv_sock(di
->vty_addr
, di
->vty_port
, di
->vty_path
);
1071 static void frr_check_detach(void)
1073 if (nodetach_term
|| nodetach_daemon
)
1076 if (daemon_ctl_sock
!= -1)
1077 close(daemon_ctl_sock
);
1078 daemon_ctl_sock
= -1;
1081 static void frr_terminal_close(int isexit
)
1085 nodetach_term
= false;
1088 if (!di
->daemon_mode
|| isexit
) {
1089 printf("\n%s exiting\n", di
->name
);
1094 printf("\n%s daemonizing\n", di
->name
);
1098 nullfd
= open("/dev/null", O_RDONLY
| O_NOCTTY
);
1100 flog_err_sys(EC_LIB_SYSTEM_CALL
,
1101 "%s: failed to open /dev/null: %s", __func__
,
1102 safe_strerror(errno
));
1111 static struct thread
*daemon_ctl_thread
= NULL
;
1113 static void frr_daemon_ctl(struct thread
*t
)
1118 nr
= recv(daemon_ctl_sock
, buf
, sizeof(buf
), 0);
1119 if (nr
< 0 && (errno
== EINTR
|| errno
== EAGAIN
))
1125 case 'S': /* SIGTSTP */
1126 vty_stdio_suspend();
1127 if (send(daemon_ctl_sock
, "s", 1, 0) < 0)
1128 zlog_err("%s send(\"s\") error (SIGTSTP propagation)",
1129 (di
&& di
->name
? di
->name
: ""));
1131 case 'R': /* SIGTCNT [implicit] */
1134 case 'I': /* SIGINT */
1135 di
->daemon_mode
= false;
1138 case 'Q': /* SIGQUIT */
1139 di
->daemon_mode
= true;
1145 thread_add_read(master
, frr_daemon_ctl
, NULL
, daemon_ctl_sock
,
1146 &daemon_ctl_thread
);
1149 void frr_detach(void)
1151 nodetach_daemon
= false;
1155 void frr_run(struct thread_master
*master
)
1157 char instanceinfo
[64] = "";
1162 snprintf(instanceinfo
, sizeof(instanceinfo
), "instance %u ",
1165 zlog_notice("%s %s starting: %svty@%d%s", di
->name
, FRR_VERSION
,
1166 instanceinfo
, di
->vty_port
, di
->startinfo
);
1169 nodetach_term
= true;
1171 vty_stdio(frr_terminal_close
);
1172 if (daemon_ctl_sock
!= -1) {
1173 set_nonblocking(daemon_ctl_sock
);
1174 thread_add_read(master
, frr_daemon_ctl
, NULL
,
1175 daemon_ctl_sock
, &daemon_ctl_thread
);
1177 } else if (di
->daemon_mode
) {
1178 int nullfd
= open("/dev/null", O_RDONLY
| O_NOCTTY
);
1180 flog_err_sys(EC_LIB_SYSTEM_CALL
,
1181 "%s: failed to open /dev/null: %s",
1182 __func__
, safe_strerror(errno
));
1193 /* end fixed stderr startup logging */
1196 struct thread thread
;
1197 while (thread_fetch(master
, &thread
))
1198 thread_call(&thread
);
1201 void frr_early_fini(void)
1203 hook_call(frr_early_fini
);
1212 hook_call(frr_fini
);
1223 #ifdef HAVE_SCRIPTING
1226 frr_pthread_finish();
1227 zprivs_terminate(di
->privs
);
1228 /* signal_init -> nothing needed */
1229 thread_master_free(master
);
1231 zlog_tls_buffer_fini();
1233 /* frrmod_init -> nothing needed / hooks */
1236 if (!debug_memstats_at_exit
)
1239 have_leftovers
= log_memstats(stderr
, di
->name
);
1241 /* in case we decide at runtime that we want exit-memstats for
1242 * a daemon, but it has no stderr because it's daemonized
1243 * (only do this if we actually have something to print though)
1245 if (!have_leftovers
)
1248 snprintf(filename
, sizeof(filename
), "/tmp/frr-memstats-%s-%llu-%llu",
1249 di
->name
, (unsigned long long)getpid(),
1250 (unsigned long long)time(NULL
));
1252 fp
= fopen(filename
, "w");
1254 log_memstats(fp
, di
->name
);
1260 static const char interp
[]
1261 __attribute__((section(".interp"), used
)) = INTERP
;
1264 * executable entry point for libfrr.so
1266 * note that libc initialization is skipped for this so the set of functions
1267 * that can be called is rather limited
1269 extern void _libfrr_version(void)
1270 __attribute__((visibility("hidden"), noreturn
));
1271 void _libfrr_version(void)
1273 const char banner
[] =
1274 FRR_FULL_NAME
" " FRR_VERSION
".\n"
1275 FRR_COPYRIGHT GIT_INFO
"\n"
1276 "configured with:\n " FRR_CONFIG_ARGS
"\n";
1277 write(1, banner
, sizeof(banner
) - 1);