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>
39 #include "lib_errors.h"
41 #include "northbound_cli.h"
42 #include "northbound_db.h"
45 #include "frr_pthread.h"
48 DEFINE_HOOK(frr_late_init
, (struct thread_master
* tm
), (tm
))
49 DEFINE_KOOH(frr_early_fini
, (), ())
50 DEFINE_KOOH(frr_fini
, (), ())
52 const char frr_sysconfdir
[] = SYSCONFDIR
;
55 const char frr_dbdir
[] = DAEMON_DB_DIR
;
57 const char frr_moduledir
[] = MODULE_PATH
;
59 char frr_protoname
[256] = "NONE";
60 char frr_protonameinst
[256] = "NONE";
62 char config_default
[512];
63 char frr_zclientpath
[256];
64 static char pidfile_default
[1024];
66 static char dbfile_default
[512];
68 static char vtypath_default
[512];
70 bool debug_memstats_at_exit
= false;
71 static bool nodetach_term
, nodetach_daemon
;
73 static char comb_optstr
[256];
74 static struct option comb_lo
[64];
75 static struct option
*comb_next_lo
= &comb_lo
[0];
76 static char comb_helpstr
[4096];
81 const struct option
*longopts
;
84 static void opt_extend(const struct optspec
*os
)
86 const struct option
*lo
;
88 strlcat(comb_optstr
, os
->optstr
, sizeof(comb_optstr
));
89 strlcat(comb_helpstr
, os
->helpstr
, sizeof(comb_helpstr
));
90 for (lo
= os
->longopts
; lo
->name
; lo
++)
91 memcpy(comb_next_lo
++, lo
, sizeof(*lo
));
95 #define OPTION_VTYSOCK 1000
96 #define OPTION_MODULEDIR 1002
97 #define OPTION_LOG 1003
98 #define OPTION_LOGLEVEL 1004
99 #define OPTION_TCLI 1005
100 #define OPTION_DB_FILE 1006
101 #define OPTION_LOGGING 1007
103 static const struct option lo_always
[] = {
104 {"help", no_argument
, NULL
, 'h'},
105 {"version", no_argument
, NULL
, 'v'},
106 {"daemon", no_argument
, NULL
, 'd'},
107 {"module", no_argument
, NULL
, 'M'},
108 {"profile", required_argument
, NULL
, 'F'},
109 {"vty_socket", required_argument
, NULL
, OPTION_VTYSOCK
},
110 {"moduledir", required_argument
, NULL
, OPTION_MODULEDIR
},
111 {"log", required_argument
, NULL
, OPTION_LOG
},
112 {"log-level", required_argument
, NULL
, OPTION_LOGLEVEL
},
113 {"tcli", no_argument
, NULL
, OPTION_TCLI
},
114 {"command-log-always", no_argument
, NULL
, OPTION_LOGGING
},
116 static const struct optspec os_always
= {
118 " -h, --help Display this help and exit\n"
119 " -v, --version Print program version\n"
120 " -d, --daemon Runs in daemon mode\n"
121 " -M, --module Load specified module\n"
122 " -F, --profile Use specified configuration profile\n"
123 " --vty_socket Override vty socket path\n"
124 " --moduledir Override modules directory\n"
125 " --log Set Logging to stdout, syslog, or file:<name>\n"
126 " --log-level Set Logging Level to use, debug, info, warn, etc\n"
127 " --tcli Use transaction-based CLI\n",
131 static const struct option lo_cfg_pid_dry
[] = {
132 {"pid_file", required_argument
, NULL
, 'i'},
133 {"config_file", required_argument
, NULL
, 'f'},
135 {"db_file", required_argument
, NULL
, OPTION_DB_FILE
},
137 {"pathspace", required_argument
, NULL
, 'N'},
138 {"dryrun", no_argument
, NULL
, 'C'},
139 {"terminal", no_argument
, NULL
, 't'},
141 static const struct optspec os_cfg_pid_dry
= {
143 " -f, --config_file Set configuration file name\n"
144 " -i, --pid_file Set process identifier file name\n"
146 " --db_file Set database file name\n"
148 " -N, --pathspace Insert prefix into config & socket paths\n"
149 " -C, --dryrun Check configuration for validity and exit\n"
150 " -t, --terminal Open terminal session on stdio\n"
151 " -d -t Daemonize after terminal session ends\n",
155 static const struct option lo_zclient
[] = {
156 {"socket", required_argument
, NULL
, 'z'},
158 static const struct optspec os_zclient
= {
159 "z:", " -z, --socket Set path of zebra socket\n", lo_zclient
};
162 static const struct option lo_vty
[] = {
163 {"vty_addr", required_argument
, NULL
, 'A'},
164 {"vty_port", required_argument
, NULL
, 'P'},
166 static const struct optspec os_vty
= {
168 " -A, --vty_addr Set vty's bind address\n"
169 " -P, --vty_port Set vty's port number\n",
173 static const struct option lo_user
[] = {{"user", required_argument
, NULL
, 'u'},
174 {"group", required_argument
, NULL
, 'g'},
176 static const struct optspec os_user
= {"u:g:",
177 " -u, --user User to run as\n"
178 " -g, --group Group to run as\n",
181 bool frr_zclient_addr(struct sockaddr_storage
*sa
, socklen_t
*sa_len
,
184 memset(sa
, 0, sizeof(*sa
));
187 path
= frr_zclientpath
;
189 if (!strncmp(path
, ZAPI_TCP_PATHNAME
, strlen(ZAPI_TCP_PATHNAME
))) {
190 /* note: this functionality is disabled at bottom */
192 int port
= ZEBRA_PORT
;
194 struct sockaddr_in
*sin
= NULL
;
195 struct sockaddr_in6
*sin6
= NULL
;
197 path
+= strlen(ZAPI_TCP_PATHNAME
);
217 port
= strtoul(path
, &err
, 10);
228 sin
= (struct sockaddr_in
*)sa
;
229 sin
->sin_port
= htons(port
);
230 sin
->sin_addr
.s_addr
= htonl(INADDR_LOOPBACK
);
231 *sa_len
= sizeof(struct sockaddr_in
);
232 #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
233 sin
->sin_len
= *sa_len
;
237 sin6
= (struct sockaddr_in6
*)sa
;
238 sin6
->sin6_port
= htons(port
);
239 inet_pton(AF_INET6
, "::1", &sin6
->sin6_addr
);
240 *sa_len
= sizeof(struct sockaddr_in6
);
242 sin6
->sin6_len
= *sa_len
;
248 /* force-disable this path, because tcp-zebra is a
249 * SECURITY ISSUE. there are no checks at all against
250 * untrusted users on the local system connecting on TCP
251 * and injecting bogus routing data into the entire routing
254 * The functionality is only left here because it may be
255 * useful during development, in order to be able to get
256 * tcpdump or wireshark watching ZAPI as TCP. If you want
257 * to do that, flip the #if 1 above to #if 0. */
258 memset(sa
, 0, sizeof(*sa
));
262 /* "sun" is a #define on solaris */
263 struct sockaddr_un
*suna
= (struct sockaddr_un
*)sa
;
265 suna
->sun_family
= AF_UNIX
;
266 strlcpy(suna
->sun_path
, path
, sizeof(suna
->sun_path
));
267 #ifdef HAVE_STRUCT_SOCKADDR_UN_SUN_LEN
268 *sa_len
= suna
->sun_len
= SUN_LEN(suna
);
270 *sa_len
= sizeof(suna
->sun_family
) + strlen(suna
->sun_path
);
271 #endif /* HAVE_STRUCT_SOCKADDR_UN_SUN_LEN */
273 /* this is left here for future reference; Linux abstract
274 * socket namespace support can be enabled by replacing
275 * above #if 0 with #ifdef GNU_LINUX.
277 * THIS IS A SECURITY ISSUE, the abstract socket namespace
278 * does not have user/group permission control on sockets.
279 * we'd need to implement SCM_CREDENTIALS support first to
280 * check that only proper users can connect to abstract
281 * sockets. (same problem as tcp-zebra, except there is a
282 * fix with SCM_CREDENTIALS. tcp-zebra has no such fix.)
284 if (suna
->sun_path
[0] == '@')
285 suna
->sun_path
[0] = '\0';
291 static struct frr_daemon_info
*di
= NULL
;
293 void frr_init_vtydir(void)
295 snprintf(frr_vtydir
, sizeof(frr_vtydir
), DAEMON_VTY_DIR
, "", "");
298 void frr_preinit(struct frr_daemon_info
*daemon
, int argc
, char **argv
)
302 /* basename(), opencoded. */
303 char *p
= strrchr(argv
[0], '/');
304 di
->progname
= p
? p
+ 1 : argv
[0];
308 opt_extend(&os_always
);
309 if (!(di
->flags
& FRR_NO_CFG_PID_DRY
))
310 opt_extend(&os_cfg_pid_dry
);
311 if (!(di
->flags
& FRR_NO_PRIVSEP
))
312 opt_extend(&os_user
);
313 if (!(di
->flags
& FRR_NO_ZCLIENT
))
314 opt_extend(&os_zclient
);
315 if (!(di
->flags
& FRR_NO_TCPVTY
))
317 if (di
->flags
& FRR_DETACH_LATER
)
318 nodetach_daemon
= true;
321 snprintf(config_default
, sizeof(config_default
), "%s/%s.conf",
322 frr_sysconfdir
, di
->name
);
323 snprintf(pidfile_default
, sizeof(pidfile_default
), "%s/%s.pid",
324 frr_vtydir
, di
->name
);
325 snprintf(frr_zclientpath
, sizeof(frr_zclientpath
),
326 ZEBRA_SERV_PATH
, "", "");
328 snprintf(dbfile_default
, sizeof(dbfile_default
), "%s/%s.db",
329 frr_dbdir
, di
->name
);
332 strlcpy(frr_protoname
, di
->logname
, sizeof(frr_protoname
));
333 strlcpy(frr_protonameinst
, di
->logname
, sizeof(frr_protonameinst
));
335 di
->cli_mode
= FRR_CLI_CLASSIC
;
338 void frr_opt_add(const char *optstr
, const struct option
*longopts
,
341 const struct optspec main_opts
= {optstr
, helpstr
, longopts
};
342 opt_extend(&main_opts
);
345 void frr_help_exit(int status
)
347 FILE *target
= status
? stderr
: stdout
;
350 fprintf(stderr
, "Invalid options.\n\n");
353 di
->printhelp(target
);
355 fprintf(target
, "Usage: %s [OPTION...]\n\n%s%s%s\n\n%s",
356 di
->progname
, di
->proghelp
, di
->copyright
? "\n\n" : "",
357 di
->copyright
? di
->copyright
: "", comb_helpstr
);
358 fprintf(target
, "\nReport bugs to %s\n", FRR_BUG_ADDRESS
);
362 struct option_chain
{
363 struct option_chain
*next
;
367 static struct option_chain
*modules
= NULL
, **modnext
= &modules
;
368 static int errors
= 0;
370 static int frr_opt(int opt
)
372 static int vty_port_set
= 0;
373 static int vty_addr_set
= 0;
374 struct option_chain
*oc
;
382 print_version(di
->progname
);
389 oc
= XMALLOC(MTYPE_TMP
, sizeof(*oc
));
396 if (!frr_defaults_profile_valid(optarg
)) {
400 if (!strcmp(optarg
, "help"))
404 "The \"%s\" configuration profile is not valid for this FRR version.\n",
407 fprintf(ofd
, "Available profiles are:\n");
408 for (p
= frr_defaults_profiles
; *p
; p
++)
409 fprintf(ofd
, "%s%s\n",
410 strcmp(*p
, DFLT_NAME
) ? " " : " * ",
419 frr_defaults_profile_set(optarg
);
422 if (di
->flags
& FRR_NO_CFG_PID_DRY
)
424 di
->pid_file
= optarg
;
427 if (di
->flags
& FRR_NO_CFG_PID_DRY
)
429 di
->config_file
= optarg
;
432 if (di
->flags
& FRR_NO_CFG_PID_DRY
)
436 "-N/--pathspace option specified more than once!\n");
442 "-N option overridden by -z for zebra named socket path\n");
444 if (strchr(optarg
, '/') || strchr(optarg
, '.')) {
446 "slashes or dots are not permitted in the --pathspace option.\n");
450 di
->pathspace
= optarg
;
453 snprintf(frr_zclientpath
, sizeof(frr_zclientpath
),
454 ZEBRA_SERV_PATH
, "/", di
->pathspace
);
455 snprintf(frr_vtydir
, sizeof(frr_vtydir
), DAEMON_VTY_DIR
, "/",
457 snprintf(pidfile_default
, sizeof(pidfile_default
), "%s/%s.pid",
458 frr_vtydir
, di
->name
);
462 if (di
->flags
& FRR_NO_CFG_PID_DRY
)
464 di
->db_file
= optarg
;
468 if (di
->flags
& FRR_NO_CFG_PID_DRY
)
473 if (di
->flags
& FRR_NO_CFG_PID_DRY
)
478 di
->zpathspace
= true;
481 "-z option overrides -N option for zebra named socket path\n");
482 if (di
->flags
& FRR_NO_ZCLIENT
)
484 strlcpy(frr_zclientpath
, optarg
, sizeof(frr_zclientpath
));
487 if (di
->flags
& FRR_NO_TCPVTY
)
491 "-A option specified more than once!\n");
496 di
->vty_addr
= optarg
;
499 if (di
->flags
& FRR_NO_TCPVTY
)
503 "-P option specified more than once!\n");
508 di
->vty_port
= strtoul(optarg
, &err
, 0);
509 if (*err
|| !*optarg
) {
511 "invalid port number \"%s\" for -P option\n",
518 if (di
->vty_sock_path
) {
520 "--vty_socket option specified more than once!\n");
524 di
->vty_sock_path
= optarg
;
526 case OPTION_MODULEDIR
:
527 if (di
->module_path
) {
529 "----moduledir option specified more than once!\n");
533 di
->module_path
= optarg
;
536 di
->cli_mode
= FRR_CLI_TRANSACTIONAL
;
539 if (di
->flags
& FRR_NO_PRIVSEP
)
541 di
->privs
->user
= optarg
;
544 if (di
->flags
& FRR_NO_PRIVSEP
)
546 di
->privs
->group
= optarg
;
549 di
->early_logging
= optarg
;
551 case OPTION_LOGLEVEL
:
552 di
->early_loglevel
= optarg
;
555 di
->log_always
= true;
563 int frr_getopt(int argc
, char *const argv
[], int *longindex
)
568 comb_next_lo
->name
= NULL
;
571 opt
= getopt_long(argc
, argv
, comb_optstr
, comb_lo
, &lidx
);
576 if (opt
== -1 && errors
)
583 static void frr_mkdir(const char *path
, bool strip
)
588 struct zprivs_ids_t ids
;
591 char *slash
= strrchr(path
, '/');
596 if (plen
> sizeof(buf
) - 1)
598 memcpy(buf
, path
, plen
);
603 /* o+rx (..5) is needed for the frrvty group to work properly;
604 * without it, users in the frrvty group can't access the vty sockets.
607 ret
= mkdir(path
, 0755);
611 /* if EEXIST, return without touching the permissions,
612 * so user-set custom permissions are left in place
617 flog_err(EC_LIB_SYSTEM_CALL
, "failed to mkdir \"%s\": %s", path
,
622 zprivs_get_ids(&ids
);
623 if (chown(path
, ids
.uid_normal
, ids
.gid_normal
))
624 flog_err(EC_LIB_SYSTEM_CALL
, "failed to chown \"%s\": %s", path
,
628 static struct thread_master
*master
;
629 struct thread_master
*frr_init(void)
631 struct option_chain
*oc
;
632 struct frrmod_runtime
*module
;
634 char p_instance
[16] = "", p_pathspace
[256] = "";
636 dir
= di
->module_path
? di
->module_path
: frr_moduledir
;
639 frr_defaults_apply();
642 snprintf(frr_protonameinst
, sizeof(frr_protonameinst
), "%s[%u]",
643 di
->logname
, di
->instance
);
644 snprintf(p_instance
, sizeof(p_instance
), "-%d", di
->instance
);
647 snprintf(p_pathspace
, sizeof(p_pathspace
), "%s/",
650 snprintf(config_default
, sizeof(config_default
), "%s%s%s%s.conf",
651 frr_sysconfdir
, p_pathspace
, di
->name
, p_instance
);
652 snprintf(pidfile_default
, sizeof(pidfile_default
), "%s/%s%s.pid",
653 frr_vtydir
, di
->name
, p_instance
);
655 snprintf(dbfile_default
, sizeof(dbfile_default
), "%s/%s%s%s.db",
656 frr_dbdir
, p_pathspace
, di
->name
, p_instance
);
659 zprivs_preinit(di
->privs
);
661 openzlog(di
->progname
, di
->logname
, di
->instance
,
662 LOG_CONS
| LOG_NDELAY
| LOG_PID
, LOG_DAEMON
);
664 command_setup_early_logging(di
->early_logging
, di
->early_loglevel
);
666 if (!frr_zclient_addr(&zclient_addr
, &zclient_addr_len
,
668 fprintf(stderr
, "Invalid zserv socket path: %s\n",
673 /* don't mkdir these as root... */
674 if (!(di
->flags
& FRR_NO_PRIVSEP
)) {
675 if (!di
->pid_file
|| !di
->vty_path
)
676 frr_mkdir(frr_vtydir
, false);
678 frr_mkdir(di
->pid_file
, true);
680 frr_mkdir(di
->vty_path
, true);
683 frrmod_init(di
->module
);
685 modules
= (oc
= modules
)->next
;
686 module
= frrmod_load(oc
->arg
, dir
, moderr
, sizeof(moderr
));
688 fprintf(stderr
, "%s\n", moderr
);
691 XFREE(MTYPE_TMP
, oc
);
694 zprivs_init(di
->privs
);
696 master
= thread_master_create(NULL
);
697 signal_init(master
, di
->n_signals
, di
->signals
);
701 di
->db_file
= dbfile_default
;
702 db_init(di
->db_file
);
705 if (di
->flags
& FRR_LIMITED_CLI
)
710 vty_init(master
, di
->log_always
);
712 log_filter_cmd_init();
724 nb_init(master
, di
->yang_modules
, di
->n_yang_modules
);
725 if (nb_db_init() != NB_OK
)
726 flog_warn(EC_LIB_NB_DATABASE
,
727 "%s: failed to initialize northbound database",
733 const char *frr_get_progname(void)
735 return di
? di
->progname
: NULL
;
738 enum frr_cli_mode
frr_get_cli_mode(void)
740 return di
? di
->cli_mode
: FRR_CLI_CLASSIC
;
743 static int rcvd_signal
= 0;
745 static void rcv_signal(int signum
)
747 rcvd_signal
= signum
;
748 /* poll() is interrupted by the signal; handled below */
751 static void frr_daemon_wait(int fd
)
753 struct pollfd pfd
[1];
757 sigset_t sigs
, prevsigs
;
760 sigaddset(&sigs
, SIGTSTP
);
761 sigaddset(&sigs
, SIGQUIT
);
762 sigaddset(&sigs
, SIGINT
);
763 sigprocmask(SIG_BLOCK
, &sigs
, &prevsigs
);
765 struct sigaction sa
= {
766 .sa_handler
= rcv_signal
, .sa_flags
= SA_RESETHAND
,
768 sigemptyset(&sa
.sa_mask
);
769 sigaction(SIGTSTP
, &sa
, NULL
);
770 sigaction(SIGQUIT
, &sa
, NULL
);
771 sigaction(SIGINT
, &sa
, NULL
);
778 pfd
[0].events
= POLLIN
;
782 #if defined(HAVE_PPOLL)
783 ret
= ppoll(pfd
, 1, NULL
, &prevsigs
);
784 #elif defined(HAVE_POLLTS)
785 ret
= pollts(pfd
, 1, NULL
, &prevsigs
);
787 /* racy -- only used on FreeBSD 9 */
789 sigprocmask(SIG_SETMASK
, &prevsigs
, &tmpsigs
);
790 ret
= poll(pfd
, 1, -1);
791 sigprocmask(SIG_SETMASK
, &tmpsigs
, NULL
);
793 if (ret
< 0 && errno
!= EINTR
&& errno
!= EAGAIN
) {
797 switch (rcvd_signal
) {
801 nrecv
= recv(fd
, buf
, sizeof(buf
), 0);
803 && (errno
== EINTR
|| errno
== EAGAIN
));
806 sigaction(SIGTSTP
, &sa
, NULL
);
818 exitpid
= waitpid(-1, &exitstat
, WNOHANG
);
820 /* child successfully went to main loop & closed socket */
823 /* child failed one way or another ... */
824 if (WIFEXITED(exitstat
) && WEXITSTATUS(exitstat
) == 0)
825 /* can happen in --terminal case if exit is fast enough */
827 else if (WIFEXITED(exitstat
))
828 fprintf(stderr
, "%s failed to start, exited %d\n", di
->name
,
829 WEXITSTATUS(exitstat
));
830 else if (WIFSIGNALED(exitstat
))
831 fprintf(stderr
, "%s crashed in startup, signal %d\n", di
->name
,
834 fprintf(stderr
, "%s failed to start, unknown problem\n",
839 static int daemon_ctl_sock
= -1;
841 static void frr_daemonize(void)
846 if (socketpair(AF_UNIX
, SOCK_STREAM
, 0, fds
)) {
847 perror("socketpair() for daemon control");
866 daemon_ctl_sock
= fds
[1];
871 frr_daemon_wait(fds
[0]);
875 * Why is this a thread?
877 * The read in of config for integrated config happens *after*
878 * thread execution starts( because it is passed in via a vtysh -b -n )
879 * While if you are not using integrated config we want the ability
880 * to read the config in after thread execution starts, so that
881 * we can match this behavior.
883 static int frr_config_read_in(struct thread
*t
)
885 if (!vty_read_config(vty_shared_candidate_config
, di
->config_file
,
887 && di
->backup_config_file
) {
888 char *orig
= XSTRDUP(MTYPE_TMP
, host_config_get());
890 zlog_info("Attempting to read backup config file: %s specified",
891 di
->backup_config_file
);
892 vty_read_config(vty_shared_candidate_config
,
893 di
->backup_config_file
, config_default
);
895 host_config_set(orig
);
896 XFREE(MTYPE_TMP
, orig
);
900 * Automatically commit the candidate configuration after
901 * reading the configuration file.
903 if (frr_get_cli_mode() == FRR_CLI_TRANSACTIONAL
) {
906 ret
= nb_candidate_commit(vty_shared_candidate_config
,
907 NB_CLIENT_CLI
, NULL
, true,
908 "Read configuration file", NULL
);
909 if (ret
!= NB_OK
&& ret
!= NB_ERR_NO_CHANGES
)
910 zlog_err("%s: failed to read configuration file.",
917 void frr_config_fork(void)
919 hook_call(frr_late_init
, master
);
921 if (!(di
->flags
& FRR_NO_CFG_PID_DRY
)) {
922 /* Don't start execution if we are in dry-run mode */
924 frr_config_read_in(NULL
);
928 thread_add_event(master
, frr_config_read_in
, NULL
, 0,
932 if (di
->daemon_mode
|| di
->terminal
)
936 di
->pid_file
= pidfile_default
;
937 pid_output(di
->pid_file
);
940 static void frr_vty_serv(void)
942 /* allow explicit override of vty_path in the future
943 * (not currently set anywhere) */
948 snprintf(defvtydir
, sizeof(defvtydir
), "%s", frr_vtydir
);
950 dir
= di
->vty_sock_path
? di
->vty_sock_path
: defvtydir
;
953 snprintf(vtypath_default
, sizeof(vtypath_default
),
954 "%s/%s-%d.vty", dir
, di
->name
, di
->instance
);
956 snprintf(vtypath_default
, sizeof(vtypath_default
),
957 "%s/%s.vty", dir
, di
->name
);
959 di
->vty_path
= vtypath_default
;
962 vty_serv_sock(di
->vty_addr
, di
->vty_port
, di
->vty_path
);
965 static void frr_check_detach(void)
967 if (nodetach_term
|| nodetach_daemon
)
970 if (daemon_ctl_sock
!= -1)
971 close(daemon_ctl_sock
);
972 daemon_ctl_sock
= -1;
975 static void frr_terminal_close(int isexit
)
979 nodetach_term
= false;
982 if (!di
->daemon_mode
|| isexit
) {
983 printf("\n%s exiting\n", di
->name
);
988 printf("\n%s daemonizing\n", di
->name
);
992 nullfd
= open("/dev/null", O_RDONLY
| O_NOCTTY
);
994 flog_err_sys(EC_LIB_SYSTEM_CALL
,
995 "%s: failed to open /dev/null: %s", __func__
,
996 safe_strerror(errno
));
1005 static struct thread
*daemon_ctl_thread
= NULL
;
1007 static int frr_daemon_ctl(struct thread
*t
)
1012 nr
= recv(daemon_ctl_sock
, buf
, sizeof(buf
), 0);
1013 if (nr
< 0 && (errno
== EINTR
|| errno
== EAGAIN
))
1019 case 'S': /* SIGTSTP */
1020 vty_stdio_suspend();
1021 if (send(daemon_ctl_sock
, "s", 1, 0) < 0)
1022 zlog_err("%s send(\"s\") error (SIGTSTP propagation)",
1023 (di
&& di
->name
? di
->name
: ""));
1025 case 'R': /* SIGTCNT [implicit] */
1028 case 'I': /* SIGINT */
1029 di
->daemon_mode
= false;
1032 case 'Q': /* SIGQUIT */
1033 di
->daemon_mode
= true;
1039 thread_add_read(master
, frr_daemon_ctl
, NULL
, daemon_ctl_sock
,
1040 &daemon_ctl_thread
);
1044 void frr_detach(void)
1046 nodetach_daemon
= false;
1050 void frr_run(struct thread_master
*master
)
1052 char instanceinfo
[64] = "";
1057 snprintf(instanceinfo
, sizeof(instanceinfo
), "instance %u ",
1060 zlog_notice("%s %s starting: %svty@%d%s", di
->name
, FRR_VERSION
,
1061 instanceinfo
, di
->vty_port
, di
->startinfo
);
1064 nodetach_term
= true;
1066 vty_stdio(frr_terminal_close
);
1067 if (daemon_ctl_sock
!= -1) {
1068 set_nonblocking(daemon_ctl_sock
);
1069 thread_add_read(master
, frr_daemon_ctl
, NULL
,
1070 daemon_ctl_sock
, &daemon_ctl_thread
);
1072 } else if (di
->daemon_mode
) {
1073 int nullfd
= open("/dev/null", O_RDONLY
| O_NOCTTY
);
1075 flog_err_sys(EC_LIB_SYSTEM_CALL
,
1076 "%s: failed to open /dev/null: %s",
1077 __func__
, safe_strerror(errno
));
1088 /* end fixed stderr startup logging */
1089 zlog_startup_stderr
= false;
1091 struct thread thread
;
1092 while (thread_fetch(master
, &thread
))
1093 thread_call(&thread
);
1096 void frr_early_fini(void)
1098 hook_call(frr_early_fini
);
1107 hook_call(frr_fini
);
1117 frr_pthread_finish();
1118 zprivs_terminate(di
->privs
);
1119 /* signal_init -> nothing needed */
1120 thread_master_free(master
);
1123 /* frrmod_init -> nothing needed / hooks */
1126 if (!debug_memstats_at_exit
)
1129 have_leftovers
= log_memstats(stderr
, di
->name
);
1131 /* in case we decide at runtime that we want exit-memstats for
1132 * a daemon, but it has no stderr because it's daemonized
1133 * (only do this if we actually have something to print though)
1135 if (!have_leftovers
)
1138 snprintf(filename
, sizeof(filename
), "/tmp/frr-memstats-%s-%llu-%llu",
1139 di
->name
, (unsigned long long)getpid(),
1140 (unsigned long long)time(NULL
));
1142 fp
= fopen(filename
, "w");
1144 log_memstats(fp
, di
->name
);
1150 static const char interp
[]
1151 __attribute__((section(".interp"), used
)) = INTERP
;
1154 * executable entry point for libfrr.so
1156 * note that libc initialization is skipped for this so the set of functions
1157 * that can be called is rather limited
1159 extern void _libfrr_version(void)
1160 __attribute__((visibility("hidden"), noreturn
));
1161 void _libfrr_version(void)
1163 const char banner
[] =
1164 FRR_FULL_NAME
" " FRR_VERSION
".\n"
1165 FRR_COPYRIGHT GIT_INFO
"\n"
1166 "configured with:\n " FRR_CONFIG_ARGS
"\n";
1167 write(1, banner
, sizeof(banner
) - 1);