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"
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
, (), ());
54 const char frr_sysconfdir
[] = SYSCONFDIR
;
57 const char frr_dbdir
[] = DAEMON_DB_DIR
;
59 const char frr_moduledir
[] = MODULE_PATH
;
60 const char frr_scriptdir
[] = SCRIPT_PATH
;
62 char frr_protoname
[256] = "NONE";
63 char frr_protonameinst
[256] = "NONE";
65 char config_default
[512];
66 char frr_zclientpath
[256];
67 static char pidfile_default
[1024];
69 static char dbfile_default
[512];
71 static char vtypath_default
[512];
73 /* cleared in frr_preinit(), then re-set after daemonizing */
74 bool frr_is_after_fork
= true;
75 bool debug_memstats_at_exit
= false;
76 static bool nodetach_term
, nodetach_daemon
;
77 static uint64_t startup_fds
;
79 static char comb_optstr
[256];
80 static struct option comb_lo
[64];
81 static struct option
*comb_next_lo
= &comb_lo
[0];
82 static char comb_helpstr
[4096];
87 const struct option
*longopts
;
90 static void opt_extend(const struct optspec
*os
)
92 const struct option
*lo
;
94 strlcat(comb_optstr
, os
->optstr
, sizeof(comb_optstr
));
95 strlcat(comb_helpstr
, os
->helpstr
, sizeof(comb_helpstr
));
96 for (lo
= os
->longopts
; lo
->name
; lo
++)
97 memcpy(comb_next_lo
++, lo
, sizeof(*lo
));
101 #define OPTION_VTYSOCK 1000
102 #define OPTION_MODULEDIR 1002
103 #define OPTION_LOG 1003
104 #define OPTION_LOGLEVEL 1004
105 #define OPTION_TCLI 1005
106 #define OPTION_DB_FILE 1006
107 #define OPTION_LOGGING 1007
108 #define OPTION_LIMIT_FDS 1008
109 #define OPTION_SCRIPTDIR 1009
111 static const struct option lo_always
[] = {
112 {"help", no_argument
, NULL
, 'h'},
113 {"version", no_argument
, NULL
, 'v'},
114 {"daemon", no_argument
, NULL
, 'd'},
115 {"module", no_argument
, NULL
, 'M'},
116 {"profile", required_argument
, NULL
, 'F'},
117 {"pathspace", required_argument
, NULL
, 'N'},
118 {"vty_socket", required_argument
, NULL
, OPTION_VTYSOCK
},
119 {"moduledir", required_argument
, NULL
, OPTION_MODULEDIR
},
120 {"scriptdir", required_argument
, NULL
, OPTION_SCRIPTDIR
},
121 {"log", required_argument
, NULL
, OPTION_LOG
},
122 {"log-level", required_argument
, NULL
, OPTION_LOGLEVEL
},
123 {"tcli", no_argument
, NULL
, OPTION_TCLI
},
124 {"command-log-always", no_argument
, NULL
, OPTION_LOGGING
},
125 {"limit-fds", required_argument
, NULL
, OPTION_LIMIT_FDS
},
127 static const struct optspec os_always
= {
129 " -h, --help Display this help and exit\n"
130 " -v, --version Print program version\n"
131 " -d, --daemon Runs in daemon mode\n"
132 " -M, --module Load specified module\n"
133 " -F, --profile Use specified configuration profile\n"
134 " -N, --pathspace Insert prefix into config & socket paths\n"
135 " --vty_socket Override vty socket path\n"
136 " --moduledir Override modules directory\n"
137 " --scriptdir Override scripts directory\n"
138 " --log Set Logging to stdout, syslog, or file:<name>\n"
139 " --log-level Set Logging Level to use, debug, info, warn, etc\n"
140 " --tcli Use transaction-based CLI\n"
141 " --limit-fds Limit number of fds supported\n",
145 static const struct option lo_cfg_pid_dry
[] = {
146 {"pid_file", required_argument
, NULL
, 'i'},
147 {"config_file", required_argument
, NULL
, 'f'},
149 {"db_file", required_argument
, NULL
, OPTION_DB_FILE
},
151 {"dryrun", no_argument
, NULL
, 'C'},
152 {"terminal", no_argument
, NULL
, 't'},
154 static const struct optspec os_cfg_pid_dry
= {
156 " -f, --config_file Set configuration file name\n"
157 " -i, --pid_file Set process identifier file name\n"
159 " --db_file Set database file name\n"
161 " -C, --dryrun Check configuration for validity and exit\n"
162 " -t, --terminal Open terminal session on stdio\n"
163 " -d -t Daemonize after terminal session ends\n",
167 static const struct option lo_zclient
[] = {
168 {"socket", required_argument
, NULL
, 'z'},
170 static const struct optspec os_zclient
= {
171 "z:", " -z, --socket Set path of zebra socket\n", lo_zclient
};
174 static const struct option lo_vty
[] = {
175 {"vty_addr", required_argument
, NULL
, 'A'},
176 {"vty_port", required_argument
, NULL
, 'P'},
178 static const struct optspec os_vty
= {
180 " -A, --vty_addr Set vty's bind address\n"
181 " -P, --vty_port Set vty's port number\n",
185 static const struct option lo_user
[] = {{"user", required_argument
, NULL
, 'u'},
186 {"group", required_argument
, NULL
, 'g'},
188 static const struct optspec os_user
= {"u:g:",
189 " -u, --user User to run as\n"
190 " -g, --group Group to run as\n",
193 bool frr_zclient_addr(struct sockaddr_storage
*sa
, socklen_t
*sa_len
,
196 memset(sa
, 0, sizeof(*sa
));
199 path
= frr_zclientpath
;
201 if (!strncmp(path
, ZAPI_TCP_PATHNAME
, strlen(ZAPI_TCP_PATHNAME
))) {
202 /* note: this functionality is disabled at bottom */
204 int port
= ZEBRA_PORT
;
206 struct sockaddr_in
*sin
= NULL
;
207 struct sockaddr_in6
*sin6
= NULL
;
209 path
+= strlen(ZAPI_TCP_PATHNAME
);
229 port
= strtoul(path
, &err
, 10);
240 sin
= (struct sockaddr_in
*)sa
;
241 sin
->sin_port
= htons(port
);
242 sin
->sin_addr
.s_addr
= htonl(INADDR_LOOPBACK
);
243 *sa_len
= sizeof(struct sockaddr_in
);
244 #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
245 sin
->sin_len
= *sa_len
;
249 sin6
= (struct sockaddr_in6
*)sa
;
250 sin6
->sin6_port
= htons(port
);
251 inet_pton(AF_INET6
, "::1", &sin6
->sin6_addr
);
252 *sa_len
= sizeof(struct sockaddr_in6
);
254 sin6
->sin6_len
= *sa_len
;
260 /* force-disable this path, because tcp-zebra is a
261 * SECURITY ISSUE. there are no checks at all against
262 * untrusted users on the local system connecting on TCP
263 * and injecting bogus routing data into the entire routing
266 * The functionality is only left here because it may be
267 * useful during development, in order to be able to get
268 * tcpdump or wireshark watching ZAPI as TCP. If you want
269 * to do that, flip the #if 1 above to #if 0. */
270 memset(sa
, 0, sizeof(*sa
));
274 /* "sun" is a #define on solaris */
275 struct sockaddr_un
*suna
= (struct sockaddr_un
*)sa
;
277 suna
->sun_family
= AF_UNIX
;
278 strlcpy(suna
->sun_path
, path
, sizeof(suna
->sun_path
));
279 #ifdef HAVE_STRUCT_SOCKADDR_UN_SUN_LEN
280 *sa_len
= suna
->sun_len
= SUN_LEN(suna
);
282 *sa_len
= sizeof(suna
->sun_family
) + strlen(suna
->sun_path
);
283 #endif /* HAVE_STRUCT_SOCKADDR_UN_SUN_LEN */
285 /* this is left here for future reference; Linux abstract
286 * socket namespace support can be enabled by replacing
287 * above #if 0 with #ifdef GNU_LINUX.
289 * THIS IS A SECURITY ISSUE, the abstract socket namespace
290 * does not have user/group permission control on sockets.
291 * we'd need to implement SCM_CREDENTIALS support first to
292 * check that only proper users can connect to abstract
293 * sockets. (same problem as tcp-zebra, except there is a
294 * fix with SCM_CREDENTIALS. tcp-zebra has no such fix.)
296 if (suna
->sun_path
[0] == '@')
297 suna
->sun_path
[0] = '\0';
303 static struct frr_daemon_info
*di
= NULL
;
305 void frr_init_vtydir(void)
307 snprintf(frr_vtydir
, sizeof(frr_vtydir
), DAEMON_VTY_DIR
, "", "");
310 void frr_preinit(struct frr_daemon_info
*daemon
, int argc
, char **argv
)
313 frr_is_after_fork
= false;
315 /* basename(), opencoded. */
316 char *p
= strrchr(argv
[0], '/');
317 di
->progname
= p
? p
+ 1 : argv
[0];
321 opt_extend(&os_always
);
322 if (!(di
->flags
& FRR_NO_CFG_PID_DRY
))
323 opt_extend(&os_cfg_pid_dry
);
324 if (!(di
->flags
& FRR_NO_PRIVSEP
))
325 opt_extend(&os_user
);
326 if (!(di
->flags
& FRR_NO_ZCLIENT
))
327 opt_extend(&os_zclient
);
328 if (!(di
->flags
& FRR_NO_TCPVTY
))
330 if (di
->flags
& FRR_DETACH_LATER
)
331 nodetach_daemon
= true;
334 snprintf(config_default
, sizeof(config_default
), "%s/%s.conf",
335 frr_sysconfdir
, di
->name
);
336 snprintf(pidfile_default
, sizeof(pidfile_default
), "%s/%s.pid",
337 frr_vtydir
, di
->name
);
338 snprintf(frr_zclientpath
, sizeof(frr_zclientpath
),
339 ZEBRA_SERV_PATH
, "", "");
341 snprintf(dbfile_default
, sizeof(dbfile_default
), "%s/%s.db",
342 frr_dbdir
, di
->name
);
345 strlcpy(frr_protoname
, di
->logname
, sizeof(frr_protoname
));
346 strlcpy(frr_protonameinst
, di
->logname
, sizeof(frr_protonameinst
));
348 di
->cli_mode
= FRR_CLI_CLASSIC
;
350 /* we may be starting with extra FDs open for whatever purpose,
351 * e.g. logging, some module, etc. Recording them here allows later
352 * checking whether an fd is valid for such extension purposes,
353 * without this we could end up e.g. logging to a BGP session fd.
356 for (int i
= 0; i
< 64; i
++) {
361 if (S_ISDIR(st
.st_mode
) || S_ISBLK(st
.st_mode
))
364 startup_fds
|= UINT64_C(0x1) << (uint64_t)i
;
368 bool frr_is_startup_fd(int fd
)
370 return !!(startup_fds
& (UINT64_C(0x1) << (uint64_t)fd
));
373 void frr_opt_add(const char *optstr
, const struct option
*longopts
,
376 const struct optspec main_opts
= {optstr
, helpstr
, longopts
};
377 opt_extend(&main_opts
);
380 void frr_help_exit(int status
)
382 FILE *target
= status
? stderr
: stdout
;
385 fprintf(stderr
, "Invalid options.\n\n");
388 di
->printhelp(target
);
390 fprintf(target
, "Usage: %s [OPTION...]\n\n%s%s%s\n\n%s",
391 di
->progname
, di
->proghelp
, di
->copyright
? "\n\n" : "",
392 di
->copyright
? di
->copyright
: "", comb_helpstr
);
393 fprintf(target
, "\nReport bugs to %s\n", FRR_BUG_ADDRESS
);
397 struct option_chain
{
398 struct option_chain
*next
;
402 static struct option_chain
*modules
= NULL
, **modnext
= &modules
;
403 static int errors
= 0;
405 static int frr_opt(int opt
)
407 static int vty_port_set
= 0;
408 static int vty_addr_set
= 0;
409 struct option_chain
*oc
;
417 print_version(di
->progname
);
421 di
->daemon_mode
= true;
424 oc
= XMALLOC(MTYPE_TMP
, sizeof(*oc
));
431 if (!frr_defaults_profile_valid(optarg
)) {
435 if (!strcmp(optarg
, "help"))
439 "The \"%s\" configuration profile is not valid for this FRR version.\n",
442 fprintf(ofd
, "Available profiles are:\n");
443 for (p
= frr_defaults_profiles
; *p
; p
++)
444 fprintf(ofd
, "%s%s\n",
445 strcmp(*p
, DFLT_NAME
) ? " " : " * ",
454 frr_defaults_profile_set(optarg
);
457 if (di
->flags
& FRR_NO_CFG_PID_DRY
)
459 di
->pid_file
= optarg
;
462 if (di
->flags
& FRR_NO_CFG_PID_DRY
)
464 di
->config_file
= optarg
;
469 "-N/--pathspace option specified more than once!\n");
475 "-N option overridden by -z for zebra named socket path\n");
477 if (strchr(optarg
, '/') || strchr(optarg
, '.')) {
479 "slashes or dots are not permitted in the --pathspace option.\n");
483 di
->pathspace
= optarg
;
486 snprintf(frr_zclientpath
, sizeof(frr_zclientpath
),
487 ZEBRA_SERV_PATH
, "/", di
->pathspace
);
488 snprintf(frr_vtydir
, sizeof(frr_vtydir
), DAEMON_VTY_DIR
, "/",
490 snprintf(pidfile_default
, sizeof(pidfile_default
), "%s/%s.pid",
491 frr_vtydir
, di
->name
);
495 if (di
->flags
& FRR_NO_CFG_PID_DRY
)
497 di
->db_file
= optarg
;
501 if (di
->flags
& FRR_NO_CFG_PID_DRY
)
506 if (di
->flags
& FRR_NO_CFG_PID_DRY
)
511 di
->zpathspace
= true;
514 "-z option overrides -N option for zebra named socket path\n");
515 if (di
->flags
& FRR_NO_ZCLIENT
)
517 strlcpy(frr_zclientpath
, optarg
, sizeof(frr_zclientpath
));
520 if (di
->flags
& FRR_NO_TCPVTY
)
524 "-A option specified more than once!\n");
529 di
->vty_addr
= optarg
;
532 if (di
->flags
& FRR_NO_TCPVTY
)
536 "-P option specified more than once!\n");
541 di
->vty_port
= strtoul(optarg
, &err
, 0);
542 if (*err
|| !*optarg
) {
544 "invalid port number \"%s\" for -P option\n",
551 if (di
->vty_sock_path
) {
553 "--vty_socket option specified more than once!\n");
557 di
->vty_sock_path
= optarg
;
559 case OPTION_MODULEDIR
:
560 if (di
->module_path
) {
562 "----moduledir option specified more than once!\n");
566 di
->module_path
= optarg
;
568 case OPTION_SCRIPTDIR
:
569 if (di
->script_path
) {
570 fprintf(stderr
, "--scriptdir option specified more than once!\n");
574 di
->script_path
= optarg
;
577 di
->cli_mode
= FRR_CLI_TRANSACTIONAL
;
580 if (di
->flags
& FRR_NO_PRIVSEP
)
582 di
->privs
->user
= optarg
;
585 if (di
->flags
& FRR_NO_PRIVSEP
)
587 di
->privs
->group
= optarg
;
590 di
->early_logging
= optarg
;
592 case OPTION_LOGLEVEL
:
593 di
->early_loglevel
= optarg
;
596 di
->log_always
= true;
598 case OPTION_LIMIT_FDS
:
599 di
->limit_fds
= strtoul(optarg
, &err
, 0);
607 int frr_getopt(int argc
, char *const argv
[], int *longindex
)
612 comb_next_lo
->name
= NULL
;
615 opt
= getopt_long(argc
, argv
, comb_optstr
, comb_lo
, &lidx
);
620 if (opt
== -1 && errors
)
627 static void frr_mkdir(const char *path
, bool strip
)
632 struct zprivs_ids_t ids
;
635 char *slash
= strrchr(path
, '/');
640 if (plen
> sizeof(buf
) - 1)
642 memcpy(buf
, path
, plen
);
647 /* o+rx (..5) is needed for the frrvty group to work properly;
648 * without it, users in the frrvty group can't access the vty sockets.
651 ret
= mkdir(path
, 0755);
655 /* if EEXIST, return without touching the permissions,
656 * so user-set custom permissions are left in place
661 flog_err(EC_LIB_SYSTEM_CALL
, "failed to mkdir \"%s\": %s", path
,
666 zprivs_get_ids(&ids
);
667 if (chown(path
, ids
.uid_normal
, ids
.gid_normal
))
668 flog_err(EC_LIB_SYSTEM_CALL
, "failed to chown \"%s\": %s", path
,
672 static struct thread_master
*master
;
673 struct thread_master
*frr_init(void)
675 struct option_chain
*oc
;
676 struct frrmod_runtime
*module
;
677 struct zprivs_ids_t ids
;
679 char p_instance
[16] = "", p_pathspace
[256] = "";
681 dir
= di
->module_path
? di
->module_path
: frr_moduledir
;
684 frr_defaults_apply();
687 snprintf(frr_protonameinst
, sizeof(frr_protonameinst
), "%s[%u]",
688 di
->logname
, di
->instance
);
689 snprintf(p_instance
, sizeof(p_instance
), "-%d", di
->instance
);
692 snprintf(p_pathspace
, sizeof(p_pathspace
), "%s/",
695 snprintf(config_default
, sizeof(config_default
), "%s%s%s%s.conf",
696 frr_sysconfdir
, p_pathspace
, di
->name
, p_instance
);
697 snprintf(pidfile_default
, sizeof(pidfile_default
), "%s/%s%s.pid",
698 frr_vtydir
, di
->name
, p_instance
);
700 snprintf(dbfile_default
, sizeof(dbfile_default
), "%s/%s%s%s.db",
701 frr_dbdir
, p_pathspace
, di
->name
, p_instance
);
704 zprivs_preinit(di
->privs
);
705 zprivs_get_ids(&ids
);
707 zlog_init(di
->progname
, di
->logname
, di
->instance
,
708 ids
.uid_normal
, ids
.gid_normal
);
710 command_setup_early_logging(di
->early_logging
, di
->early_loglevel
);
712 if (!frr_zclient_addr(&zclient_addr
, &zclient_addr_len
,
714 fprintf(stderr
, "Invalid zserv socket path: %s\n",
719 /* don't mkdir these as root... */
720 if (!(di
->flags
& FRR_NO_PRIVSEP
)) {
721 if (!di
->pid_file
|| !di
->vty_path
)
722 frr_mkdir(frr_vtydir
, false);
724 frr_mkdir(di
->pid_file
, true);
726 frr_mkdir(di
->vty_path
, true);
729 frrmod_init(di
->module
);
731 modules
= (oc
= modules
)->next
;
732 module
= frrmod_load(oc
->arg
, dir
, moderr
, sizeof(moderr
));
734 fprintf(stderr
, "%s\n", moderr
);
737 XFREE(MTYPE_TMP
, oc
);
740 zprivs_init(di
->privs
);
742 master
= thread_master_create(NULL
);
743 signal_init(master
, di
->n_signals
, di
->signals
);
747 di
->db_file
= dbfile_default
;
748 db_init(di
->db_file
);
751 if (di
->flags
& FRR_LIMITED_CLI
)
756 vty_init(master
, di
->log_always
);
760 #ifdef HAVE_SCRIPTING
761 frrscript_init(di
->script_path
? di
->script_path
: frr_scriptdir
);
772 nb_init(master
, di
->yang_modules
, di
->n_yang_modules
, true);
773 if (nb_db_init() != NB_OK
)
774 flog_warn(EC_LIB_NB_DATABASE
,
775 "%s: failed to initialize northbound database",
781 const char *frr_get_progname(void)
783 return di
? di
->progname
: NULL
;
786 enum frr_cli_mode
frr_get_cli_mode(void)
788 return di
? di
->cli_mode
: FRR_CLI_CLASSIC
;
791 uint32_t frr_get_fd_limit(void)
793 return di
? di
->limit_fds
: 0;
796 static int rcvd_signal
= 0;
798 static void rcv_signal(int signum
)
800 rcvd_signal
= signum
;
801 /* poll() is interrupted by the signal; handled below */
804 static void frr_daemon_wait(int fd
)
806 struct pollfd pfd
[1];
810 sigset_t sigs
, prevsigs
;
813 sigaddset(&sigs
, SIGTSTP
);
814 sigaddset(&sigs
, SIGQUIT
);
815 sigaddset(&sigs
, SIGINT
);
816 sigprocmask(SIG_BLOCK
, &sigs
, &prevsigs
);
818 struct sigaction sa
= {
819 .sa_handler
= rcv_signal
, .sa_flags
= SA_RESETHAND
,
821 sigemptyset(&sa
.sa_mask
);
822 sigaction(SIGTSTP
, &sa
, NULL
);
823 sigaction(SIGQUIT
, &sa
, NULL
);
824 sigaction(SIGINT
, &sa
, NULL
);
831 pfd
[0].events
= POLLIN
;
835 #if defined(HAVE_PPOLL)
836 ret
= ppoll(pfd
, 1, NULL
, &prevsigs
);
837 #elif defined(HAVE_POLLTS)
838 ret
= pollts(pfd
, 1, NULL
, &prevsigs
);
840 /* racy -- only used on FreeBSD 9 */
842 sigprocmask(SIG_SETMASK
, &prevsigs
, &tmpsigs
);
843 ret
= poll(pfd
, 1, -1);
844 sigprocmask(SIG_SETMASK
, &tmpsigs
, NULL
);
846 if (ret
< 0 && errno
!= EINTR
&& errno
!= EAGAIN
) {
850 switch (rcvd_signal
) {
854 nrecv
= recv(fd
, buf
, sizeof(buf
), 0);
856 && (errno
== EINTR
|| errno
== EAGAIN
));
859 sigaction(SIGTSTP
, &sa
, NULL
);
871 exitpid
= waitpid(-1, &exitstat
, WNOHANG
);
873 /* child successfully went to main loop & closed socket */
876 /* child failed one way or another ... */
877 if (WIFEXITED(exitstat
) && WEXITSTATUS(exitstat
) == 0)
878 /* can happen in --terminal case if exit is fast enough */
880 else if (WIFEXITED(exitstat
))
881 fprintf(stderr
, "%s failed to start, exited %d\n", di
->name
,
882 WEXITSTATUS(exitstat
));
883 else if (WIFSIGNALED(exitstat
))
884 fprintf(stderr
, "%s crashed in startup, signal %d\n", di
->name
,
887 fprintf(stderr
, "%s failed to start, unknown problem\n",
892 static int daemon_ctl_sock
= -1;
894 static void frr_daemonize(void)
899 if (socketpair(AF_UNIX
, SOCK_STREAM
, 0, fds
)) {
900 perror("socketpair() for daemon control");
919 daemon_ctl_sock
= fds
[1];
924 frr_daemon_wait(fds
[0]);
928 * Why is this a thread?
930 * The read in of config for integrated config happens *after*
931 * thread execution starts( because it is passed in via a vtysh -b -n )
932 * While if you are not using integrated config we want the ability
933 * to read the config in after thread execution starts, so that
934 * we can match this behavior.
936 static int frr_config_read_in(struct thread
*t
)
938 hook_call(frr_config_pre
, master
);
940 if (!vty_read_config(vty_shared_candidate_config
, di
->config_file
,
942 && di
->backup_config_file
) {
943 char *orig
= XSTRDUP(MTYPE_TMP
, host_config_get());
945 zlog_info("Attempting to read backup config file: %s specified",
946 di
->backup_config_file
);
947 vty_read_config(vty_shared_candidate_config
,
948 di
->backup_config_file
, config_default
);
950 host_config_set(orig
);
951 XFREE(MTYPE_TMP
, orig
);
955 * Automatically commit the candidate configuration after
956 * reading the configuration file.
958 if (frr_get_cli_mode() == FRR_CLI_TRANSACTIONAL
) {
959 struct nb_context context
= {};
960 char errmsg
[BUFSIZ
] = {0};
963 context
.client
= NB_CLIENT_CLI
;
964 ret
= nb_candidate_commit(&context
, vty_shared_candidate_config
,
965 true, "Read configuration file", NULL
,
966 errmsg
, sizeof(errmsg
));
967 if (ret
!= NB_OK
&& ret
!= NB_ERR_NO_CHANGES
)
969 "%s: failed to read configuration file: %s (%s)",
970 __func__
, nb_err_name(ret
), errmsg
);
973 hook_call(frr_config_post
, master
);
978 void frr_config_fork(void)
980 hook_call(frr_late_init
, master
);
982 if (!(di
->flags
& FRR_NO_CFG_PID_DRY
)) {
983 /* Don't start execution if we are in dry-run mode */
985 frr_config_read_in(NULL
);
989 thread_add_event(master
, frr_config_read_in
, NULL
, 0,
993 if (di
->daemon_mode
|| di
->terminal
)
996 frr_is_after_fork
= true;
999 di
->pid_file
= pidfile_default
;
1000 pid_output(di
->pid_file
);
1001 zlog_tls_buffer_init();
1004 static void frr_vty_serv(void)
1006 /* allow explicit override of vty_path in the future
1007 * (not currently set anywhere) */
1008 if (!di
->vty_path
) {
1010 char defvtydir
[256];
1012 snprintf(defvtydir
, sizeof(defvtydir
), "%s", frr_vtydir
);
1014 dir
= di
->vty_sock_path
? di
->vty_sock_path
: defvtydir
;
1017 snprintf(vtypath_default
, sizeof(vtypath_default
),
1018 "%s/%s-%d.vty", dir
, di
->name
, di
->instance
);
1020 snprintf(vtypath_default
, sizeof(vtypath_default
),
1021 "%s/%s.vty", dir
, di
->name
);
1023 di
->vty_path
= vtypath_default
;
1026 vty_serv_sock(di
->vty_addr
, di
->vty_port
, di
->vty_path
);
1029 static void frr_check_detach(void)
1031 if (nodetach_term
|| nodetach_daemon
)
1034 if (daemon_ctl_sock
!= -1)
1035 close(daemon_ctl_sock
);
1036 daemon_ctl_sock
= -1;
1039 static void frr_terminal_close(int isexit
)
1043 nodetach_term
= false;
1046 if (!di
->daemon_mode
|| isexit
) {
1047 printf("\n%s exiting\n", di
->name
);
1052 printf("\n%s daemonizing\n", di
->name
);
1056 nullfd
= open("/dev/null", O_RDONLY
| O_NOCTTY
);
1058 flog_err_sys(EC_LIB_SYSTEM_CALL
,
1059 "%s: failed to open /dev/null: %s", __func__
,
1060 safe_strerror(errno
));
1069 static struct thread
*daemon_ctl_thread
= NULL
;
1071 static int frr_daemon_ctl(struct thread
*t
)
1076 nr
= recv(daemon_ctl_sock
, buf
, sizeof(buf
), 0);
1077 if (nr
< 0 && (errno
== EINTR
|| errno
== EAGAIN
))
1083 case 'S': /* SIGTSTP */
1084 vty_stdio_suspend();
1085 if (send(daemon_ctl_sock
, "s", 1, 0) < 0)
1086 zlog_err("%s send(\"s\") error (SIGTSTP propagation)",
1087 (di
&& di
->name
? di
->name
: ""));
1089 case 'R': /* SIGTCNT [implicit] */
1092 case 'I': /* SIGINT */
1093 di
->daemon_mode
= false;
1096 case 'Q': /* SIGQUIT */
1097 di
->daemon_mode
= true;
1103 thread_add_read(master
, frr_daemon_ctl
, NULL
, daemon_ctl_sock
,
1104 &daemon_ctl_thread
);
1108 void frr_detach(void)
1110 nodetach_daemon
= false;
1114 void frr_run(struct thread_master
*master
)
1116 char instanceinfo
[64] = "";
1121 snprintf(instanceinfo
, sizeof(instanceinfo
), "instance %u ",
1124 zlog_notice("%s %s starting: %svty@%d%s", di
->name
, FRR_VERSION
,
1125 instanceinfo
, di
->vty_port
, di
->startinfo
);
1128 nodetach_term
= true;
1130 vty_stdio(frr_terminal_close
);
1131 if (daemon_ctl_sock
!= -1) {
1132 set_nonblocking(daemon_ctl_sock
);
1133 thread_add_read(master
, frr_daemon_ctl
, NULL
,
1134 daemon_ctl_sock
, &daemon_ctl_thread
);
1136 } else if (di
->daemon_mode
) {
1137 int nullfd
= open("/dev/null", O_RDONLY
| O_NOCTTY
);
1139 flog_err_sys(EC_LIB_SYSTEM_CALL
,
1140 "%s: failed to open /dev/null: %s",
1141 __func__
, safe_strerror(errno
));
1152 /* end fixed stderr startup logging */
1155 struct thread thread
;
1156 while (thread_fetch(master
, &thread
))
1157 thread_call(&thread
);
1160 void frr_early_fini(void)
1162 hook_call(frr_early_fini
);
1171 hook_call(frr_fini
);
1181 frr_pthread_finish();
1182 zprivs_terminate(di
->privs
);
1183 /* signal_init -> nothing needed */
1184 thread_master_free(master
);
1186 zlog_tls_buffer_fini();
1188 /* frrmod_init -> nothing needed / hooks */
1191 if (!debug_memstats_at_exit
)
1194 have_leftovers
= log_memstats(stderr
, di
->name
);
1196 /* in case we decide at runtime that we want exit-memstats for
1197 * a daemon, but it has no stderr because it's daemonized
1198 * (only do this if we actually have something to print though)
1200 if (!have_leftovers
)
1203 snprintf(filename
, sizeof(filename
), "/tmp/frr-memstats-%s-%llu-%llu",
1204 di
->name
, (unsigned long long)getpid(),
1205 (unsigned long long)time(NULL
));
1207 fp
= fopen(filename
, "w");
1209 log_memstats(fp
, di
->name
);
1215 static const char interp
[]
1216 __attribute__((section(".interp"), used
)) = INTERP
;
1219 * executable entry point for libfrr.so
1221 * note that libc initialization is skipped for this so the set of functions
1222 * that can be called is rather limited
1224 extern void _libfrr_version(void)
1225 __attribute__((visibility("hidden"), noreturn
));
1226 void _libfrr_version(void)
1228 const char banner
[] =
1229 FRR_FULL_NAME
" " FRR_VERSION
".\n"
1230 FRR_COPYRIGHT GIT_INFO
"\n"
1231 "configured with:\n " FRR_CONFIG_ARGS
"\n";
1232 write(1, banner
, sizeof(banner
) - 1);