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 bool debug_memstats_at_exit
= false;
74 static bool nodetach_term
, nodetach_daemon
;
75 static uint64_t startup_fds
;
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];
85 const struct option
*longopts
;
88 static void opt_extend(const struct optspec
*os
)
90 const struct option
*lo
;
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
));
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
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
},
125 static const struct optspec os_always
= {
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",
143 static const struct option lo_cfg_pid_dry
[] = {
144 {"pid_file", required_argument
, NULL
, 'i'},
145 {"config_file", required_argument
, NULL
, 'f'},
147 {"db_file", required_argument
, NULL
, OPTION_DB_FILE
},
149 {"dryrun", no_argument
, NULL
, 'C'},
150 {"terminal", no_argument
, NULL
, 't'},
152 static const struct optspec os_cfg_pid_dry
= {
154 " -f, --config_file Set configuration file name\n"
155 " -i, --pid_file Set process identifier file name\n"
157 " --db_file Set database file name\n"
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",
165 static const struct option lo_zclient
[] = {
166 {"socket", required_argument
, NULL
, 'z'},
168 static const struct optspec os_zclient
= {
169 "z:", " -z, --socket Set path of zebra socket\n", lo_zclient
};
172 static const struct option lo_vty
[] = {
173 {"vty_addr", required_argument
, NULL
, 'A'},
174 {"vty_port", required_argument
, NULL
, 'P'},
176 static const struct optspec os_vty
= {
178 " -A, --vty_addr Set vty's bind address\n"
179 " -P, --vty_port Set vty's port number\n",
183 static const struct option lo_user
[] = {{"user", required_argument
, NULL
, 'u'},
184 {"group", required_argument
, NULL
, 'g'},
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",
191 bool frr_zclient_addr(struct sockaddr_storage
*sa
, socklen_t
*sa_len
,
194 memset(sa
, 0, sizeof(*sa
));
197 path
= frr_zclientpath
;
199 if (!strncmp(path
, ZAPI_TCP_PATHNAME
, strlen(ZAPI_TCP_PATHNAME
))) {
200 /* note: this functionality is disabled at bottom */
202 int port
= ZEBRA_PORT
;
204 struct sockaddr_in
*sin
= NULL
;
205 struct sockaddr_in6
*sin6
= NULL
;
207 path
+= strlen(ZAPI_TCP_PATHNAME
);
227 port
= strtoul(path
, &err
, 10);
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
;
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
);
252 sin6
->sin6_len
= *sa_len
;
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
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
));
272 /* "sun" is a #define on solaris */
273 struct sockaddr_un
*suna
= (struct sockaddr_un
*)sa
;
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
);
280 *sa_len
= sizeof(suna
->sun_family
) + strlen(suna
->sun_path
);
281 #endif /* HAVE_STRUCT_SOCKADDR_UN_SUN_LEN */
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.
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.)
294 if (suna
->sun_path
[0] == '@')
295 suna
->sun_path
[0] = '\0';
301 static struct frr_daemon_info
*di
= NULL
;
303 void frr_init_vtydir(void)
305 snprintf(frr_vtydir
, sizeof(frr_vtydir
), DAEMON_VTY_DIR
, "", "");
308 void frr_preinit(struct frr_daemon_info
*daemon
, int argc
, char **argv
)
312 /* basename(), opencoded. */
313 char *p
= strrchr(argv
[0], '/');
314 di
->progname
= p
? p
+ 1 : argv
[0];
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
))
327 if (di
->flags
& FRR_DETACH_LATER
)
328 nodetach_daemon
= true;
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
, "", "");
338 snprintf(dbfile_default
, sizeof(dbfile_default
), "%s/%s.db",
339 frr_dbdir
, di
->name
);
342 strlcpy(frr_protoname
, di
->logname
, sizeof(frr_protoname
));
343 strlcpy(frr_protonameinst
, di
->logname
, sizeof(frr_protonameinst
));
345 di
->cli_mode
= FRR_CLI_CLASSIC
;
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.
353 for (int i
= 0; i
< 64; i
++) {
358 if (S_ISDIR(st
.st_mode
) || S_ISBLK(st
.st_mode
))
361 startup_fds
|= UINT64_C(0x1) << (uint64_t)i
;
365 bool frr_is_startup_fd(int fd
)
367 return !!(startup_fds
& (UINT64_C(0x1) << (uint64_t)fd
));
370 void frr_opt_add(const char *optstr
, const struct option
*longopts
,
373 const struct optspec main_opts
= {optstr
, helpstr
, longopts
};
374 opt_extend(&main_opts
);
377 void frr_help_exit(int status
)
379 FILE *target
= status
? stderr
: stdout
;
382 fprintf(stderr
, "Invalid options.\n\n");
385 di
->printhelp(target
);
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
);
394 struct option_chain
{
395 struct option_chain
*next
;
399 static struct option_chain
*modules
= NULL
, **modnext
= &modules
;
400 static int errors
= 0;
402 static int frr_opt(int opt
)
404 static int vty_port_set
= 0;
405 static int vty_addr_set
= 0;
406 struct option_chain
*oc
;
414 print_version(di
->progname
);
418 di
->daemon_mode
= true;
421 oc
= XMALLOC(MTYPE_TMP
, sizeof(*oc
));
428 if (!frr_defaults_profile_valid(optarg
)) {
432 if (!strcmp(optarg
, "help"))
436 "The \"%s\" configuration profile is not valid for this FRR version.\n",
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
) ? " " : " * ",
451 frr_defaults_profile_set(optarg
);
454 if (di
->flags
& FRR_NO_CFG_PID_DRY
)
456 di
->pid_file
= optarg
;
459 if (di
->flags
& FRR_NO_CFG_PID_DRY
)
461 di
->config_file
= optarg
;
466 "-N/--pathspace option specified more than once!\n");
472 "-N option overridden by -z for zebra named socket path\n");
474 if (strchr(optarg
, '/') || strchr(optarg
, '.')) {
476 "slashes or dots are not permitted in the --pathspace option.\n");
480 di
->pathspace
= optarg
;
483 snprintf(frr_zclientpath
, sizeof(frr_zclientpath
),
484 ZEBRA_SERV_PATH
, "/", di
->pathspace
);
485 snprintf(frr_vtydir
, sizeof(frr_vtydir
), DAEMON_VTY_DIR
, "/",
487 snprintf(pidfile_default
, sizeof(pidfile_default
), "%s/%s.pid",
488 frr_vtydir
, di
->name
);
492 if (di
->flags
& FRR_NO_CFG_PID_DRY
)
494 di
->db_file
= optarg
;
498 if (di
->flags
& FRR_NO_CFG_PID_DRY
)
503 if (di
->flags
& FRR_NO_CFG_PID_DRY
)
508 di
->zpathspace
= true;
511 "-z option overrides -N option for zebra named socket path\n");
512 if (di
->flags
& FRR_NO_ZCLIENT
)
514 strlcpy(frr_zclientpath
, optarg
, sizeof(frr_zclientpath
));
517 if (di
->flags
& FRR_NO_TCPVTY
)
521 "-A option specified more than once!\n");
526 di
->vty_addr
= optarg
;
529 if (di
->flags
& FRR_NO_TCPVTY
)
533 "-P option specified more than once!\n");
538 di
->vty_port
= strtoul(optarg
, &err
, 0);
539 if (*err
|| !*optarg
) {
541 "invalid port number \"%s\" for -P option\n",
548 if (di
->vty_sock_path
) {
550 "--vty_socket option specified more than once!\n");
554 di
->vty_sock_path
= optarg
;
556 case OPTION_MODULEDIR
:
557 if (di
->module_path
) {
559 "----moduledir option specified more than once!\n");
563 di
->module_path
= optarg
;
565 case OPTION_SCRIPTDIR
:
566 if (di
->script_path
) {
567 fprintf(stderr
, "--scriptdir option specified more than once!\n");
571 di
->script_path
= optarg
;
574 di
->cli_mode
= FRR_CLI_TRANSACTIONAL
;
577 if (di
->flags
& FRR_NO_PRIVSEP
)
579 di
->privs
->user
= optarg
;
582 if (di
->flags
& FRR_NO_PRIVSEP
)
584 di
->privs
->group
= optarg
;
587 di
->early_logging
= optarg
;
589 case OPTION_LOGLEVEL
:
590 di
->early_loglevel
= optarg
;
593 di
->log_always
= true;
595 case OPTION_LIMIT_FDS
:
596 di
->limit_fds
= strtoul(optarg
, &err
, 0);
604 int frr_getopt(int argc
, char *const argv
[], int *longindex
)
609 comb_next_lo
->name
= NULL
;
612 opt
= getopt_long(argc
, argv
, comb_optstr
, comb_lo
, &lidx
);
617 if (opt
== -1 && errors
)
624 static void frr_mkdir(const char *path
, bool strip
)
629 struct zprivs_ids_t ids
;
632 char *slash
= strrchr(path
, '/');
637 if (plen
> sizeof(buf
) - 1)
639 memcpy(buf
, path
, plen
);
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.
648 ret
= mkdir(path
, 0755);
652 /* if EEXIST, return without touching the permissions,
653 * so user-set custom permissions are left in place
658 flog_err(EC_LIB_SYSTEM_CALL
, "failed to mkdir \"%s\": %s", path
,
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
,
669 static struct thread_master
*master
;
670 struct thread_master
*frr_init(void)
672 struct option_chain
*oc
;
673 struct frrmod_runtime
*module
;
674 struct zprivs_ids_t ids
;
676 char p_instance
[16] = "", p_pathspace
[256] = "";
678 dir
= di
->module_path
? di
->module_path
: frr_moduledir
;
681 frr_defaults_apply();
684 snprintf(frr_protonameinst
, sizeof(frr_protonameinst
), "%s[%u]",
685 di
->logname
, di
->instance
);
686 snprintf(p_instance
, sizeof(p_instance
), "-%d", di
->instance
);
689 snprintf(p_pathspace
, sizeof(p_pathspace
), "%s/",
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
);
697 snprintf(dbfile_default
, sizeof(dbfile_default
), "%s/%s%s%s.db",
698 frr_dbdir
, p_pathspace
, di
->name
, p_instance
);
701 zprivs_preinit(di
->privs
);
702 zprivs_get_ids(&ids
);
704 zlog_init(di
->progname
, di
->logname
, di
->instance
,
705 ids
.uid_normal
, ids
.gid_normal
);
707 command_setup_early_logging(di
->early_logging
, di
->early_loglevel
);
709 if (!frr_zclient_addr(&zclient_addr
, &zclient_addr_len
,
711 fprintf(stderr
, "Invalid zserv socket path: %s\n",
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);
721 frr_mkdir(di
->pid_file
, true);
723 frr_mkdir(di
->vty_path
, true);
726 frrmod_init(di
->module
);
728 modules
= (oc
= modules
)->next
;
729 module
= frrmod_load(oc
->arg
, dir
, moderr
, sizeof(moderr
));
731 fprintf(stderr
, "%s\n", moderr
);
734 XFREE(MTYPE_TMP
, oc
);
737 zprivs_init(di
->privs
);
739 master
= thread_master_create(NULL
);
740 signal_init(master
, di
->n_signals
, di
->signals
);
744 di
->db_file
= dbfile_default
;
745 db_init(di
->db_file
);
748 if (di
->flags
& FRR_LIMITED_CLI
)
753 vty_init(master
, di
->log_always
);
757 #ifdef HAVE_SCRIPTING
758 frrscript_init(di
->script_path
? di
->script_path
: frr_scriptdir
);
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",
778 const char *frr_get_progname(void)
780 return di
? di
->progname
: NULL
;
783 enum frr_cli_mode
frr_get_cli_mode(void)
785 return di
? di
->cli_mode
: FRR_CLI_CLASSIC
;
788 uint32_t frr_get_fd_limit(void)
790 return di
? di
->limit_fds
: 0;
793 static int rcvd_signal
= 0;
795 static void rcv_signal(int signum
)
797 rcvd_signal
= signum
;
798 /* poll() is interrupted by the signal; handled below */
801 static void frr_daemon_wait(int fd
)
803 struct pollfd pfd
[1];
807 sigset_t sigs
, prevsigs
;
810 sigaddset(&sigs
, SIGTSTP
);
811 sigaddset(&sigs
, SIGQUIT
);
812 sigaddset(&sigs
, SIGINT
);
813 sigprocmask(SIG_BLOCK
, &sigs
, &prevsigs
);
815 struct sigaction sa
= {
816 .sa_handler
= rcv_signal
, .sa_flags
= SA_RESETHAND
,
818 sigemptyset(&sa
.sa_mask
);
819 sigaction(SIGTSTP
, &sa
, NULL
);
820 sigaction(SIGQUIT
, &sa
, NULL
);
821 sigaction(SIGINT
, &sa
, NULL
);
828 pfd
[0].events
= POLLIN
;
832 #if defined(HAVE_PPOLL)
833 ret
= ppoll(pfd
, 1, NULL
, &prevsigs
);
834 #elif defined(HAVE_POLLTS)
835 ret
= pollts(pfd
, 1, NULL
, &prevsigs
);
837 /* racy -- only used on FreeBSD 9 */
839 sigprocmask(SIG_SETMASK
, &prevsigs
, &tmpsigs
);
840 ret
= poll(pfd
, 1, -1);
841 sigprocmask(SIG_SETMASK
, &tmpsigs
, NULL
);
843 if (ret
< 0 && errno
!= EINTR
&& errno
!= EAGAIN
) {
847 switch (rcvd_signal
) {
851 nrecv
= recv(fd
, buf
, sizeof(buf
), 0);
853 && (errno
== EINTR
|| errno
== EAGAIN
));
856 sigaction(SIGTSTP
, &sa
, NULL
);
868 exitpid
= waitpid(-1, &exitstat
, WNOHANG
);
870 /* child successfully went to main loop & closed socket */
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 */
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
,
884 fprintf(stderr
, "%s failed to start, unknown problem\n",
889 static int daemon_ctl_sock
= -1;
891 static void frr_daemonize(void)
896 if (socketpair(AF_UNIX
, SOCK_STREAM
, 0, fds
)) {
897 perror("socketpair() for daemon control");
916 daemon_ctl_sock
= fds
[1];
921 frr_daemon_wait(fds
[0]);
925 * Why is this a thread?
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.
933 static int frr_config_read_in(struct thread
*t
)
935 hook_call(frr_config_pre
, master
);
937 if (!vty_read_config(vty_shared_candidate_config
, di
->config_file
,
939 && di
->backup_config_file
) {
940 char *orig
= XSTRDUP(MTYPE_TMP
, host_config_get());
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
);
947 host_config_set(orig
);
948 XFREE(MTYPE_TMP
, orig
);
952 * Automatically commit the candidate configuration after
953 * reading the configuration file.
955 if (frr_get_cli_mode() == FRR_CLI_TRANSACTIONAL
) {
956 struct nb_context context
= {};
957 char errmsg
[BUFSIZ
] = {0};
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
)
966 "%s: failed to read configuration file: %s (%s)",
967 __func__
, nb_err_name(ret
), errmsg
);
970 hook_call(frr_config_post
, master
);
975 void frr_config_fork(void)
977 hook_call(frr_late_init
, master
);
979 if (!(di
->flags
& FRR_NO_CFG_PID_DRY
)) {
980 /* Don't start execution if we are in dry-run mode */
982 frr_config_read_in(NULL
);
986 thread_add_event(master
, frr_config_read_in
, NULL
, 0,
990 if (di
->daemon_mode
|| di
->terminal
)
994 di
->pid_file
= pidfile_default
;
995 pid_output(di
->pid_file
);
996 zlog_tls_buffer_init();
999 static void frr_vty_serv(void)
1001 /* allow explicit override of vty_path in the future
1002 * (not currently set anywhere) */
1003 if (!di
->vty_path
) {
1005 char defvtydir
[256];
1007 snprintf(defvtydir
, sizeof(defvtydir
), "%s", frr_vtydir
);
1009 dir
= di
->vty_sock_path
? di
->vty_sock_path
: defvtydir
;
1012 snprintf(vtypath_default
, sizeof(vtypath_default
),
1013 "%s/%s-%d.vty", dir
, di
->name
, di
->instance
);
1015 snprintf(vtypath_default
, sizeof(vtypath_default
),
1016 "%s/%s.vty", dir
, di
->name
);
1018 di
->vty_path
= vtypath_default
;
1021 vty_serv_sock(di
->vty_addr
, di
->vty_port
, di
->vty_path
);
1024 static void frr_check_detach(void)
1026 if (nodetach_term
|| nodetach_daemon
)
1029 if (daemon_ctl_sock
!= -1)
1030 close(daemon_ctl_sock
);
1031 daemon_ctl_sock
= -1;
1034 static void frr_terminal_close(int isexit
)
1038 nodetach_term
= false;
1041 if (!di
->daemon_mode
|| isexit
) {
1042 printf("\n%s exiting\n", di
->name
);
1047 printf("\n%s daemonizing\n", di
->name
);
1051 nullfd
= open("/dev/null", O_RDONLY
| O_NOCTTY
);
1053 flog_err_sys(EC_LIB_SYSTEM_CALL
,
1054 "%s: failed to open /dev/null: %s", __func__
,
1055 safe_strerror(errno
));
1064 static struct thread
*daemon_ctl_thread
= NULL
;
1066 static int frr_daemon_ctl(struct thread
*t
)
1071 nr
= recv(daemon_ctl_sock
, buf
, sizeof(buf
), 0);
1072 if (nr
< 0 && (errno
== EINTR
|| errno
== EAGAIN
))
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
: ""));
1084 case 'R': /* SIGTCNT [implicit] */
1087 case 'I': /* SIGINT */
1088 di
->daemon_mode
= false;
1091 case 'Q': /* SIGQUIT */
1092 di
->daemon_mode
= true;
1098 thread_add_read(master
, frr_daemon_ctl
, NULL
, daemon_ctl_sock
,
1099 &daemon_ctl_thread
);
1103 void frr_detach(void)
1105 nodetach_daemon
= false;
1109 void frr_run(struct thread_master
*master
)
1111 char instanceinfo
[64] = "";
1116 snprintf(instanceinfo
, sizeof(instanceinfo
), "instance %u ",
1119 zlog_notice("%s %s starting: %svty@%d%s", di
->name
, FRR_VERSION
,
1120 instanceinfo
, di
->vty_port
, di
->startinfo
);
1123 nodetach_term
= true;
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
);
1131 } else if (di
->daemon_mode
) {
1132 int nullfd
= open("/dev/null", O_RDONLY
| O_NOCTTY
);
1134 flog_err_sys(EC_LIB_SYSTEM_CALL
,
1135 "%s: failed to open /dev/null: %s",
1136 __func__
, safe_strerror(errno
));
1147 /* end fixed stderr startup logging */
1150 struct thread thread
;
1151 while (thread_fetch(master
, &thread
))
1152 thread_call(&thread
);
1155 void frr_early_fini(void)
1157 hook_call(frr_early_fini
);
1166 hook_call(frr_fini
);
1176 frr_pthread_finish();
1177 zprivs_terminate(di
->privs
);
1178 /* signal_init -> nothing needed */
1179 thread_master_free(master
);
1181 zlog_tls_buffer_fini();
1183 /* frrmod_init -> nothing needed / hooks */
1186 if (!debug_memstats_at_exit
)
1189 have_leftovers
= log_memstats(stderr
, di
->name
);
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)
1195 if (!have_leftovers
)
1198 snprintf(filename
, sizeof(filename
), "/tmp/frr-memstats-%s-%llu-%llu",
1199 di
->name
, (unsigned long long)getpid(),
1200 (unsigned long long)time(NULL
));
1202 fp
= fopen(filename
, "w");
1204 log_memstats(fp
, di
->name
);
1210 static const char interp
[]
1211 __attribute__((section(".interp"), used
)) = INTERP
;
1214 * executable entry point for libfrr.so
1216 * note that libc initialization is skipped for this so the set of functions
1217 * that can be called is rather limited
1219 extern void _libfrr_version(void)
1220 __attribute__((visibility("hidden"), noreturn
));
1221 void _libfrr_version(void)
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);