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>
33 #include "memory_vty.h"
39 #include "lib_errors.h"
41 #include "northbound_cli.h"
42 #include "northbound_db.h"
46 DEFINE_HOOK(frr_late_init
, (struct thread_master
* tm
), (tm
))
47 DEFINE_KOOH(frr_early_fini
, (), ())
48 DEFINE_KOOH(frr_fini
, (), ())
50 const char frr_sysconfdir
[] = SYSCONFDIR
;
53 const char frr_dbdir
[] = DAEMON_DB_DIR
;
55 const char frr_moduledir
[] = MODULE_PATH
;
57 char frr_protoname
[256] = "NONE";
58 char frr_protonameinst
[256] = "NONE";
60 char config_default
[512];
61 char frr_zclientpath
[256];
62 static char pidfile_default
[1024];
64 static char dbfile_default
[512];
66 static char vtypath_default
[512];
68 bool debug_memstats_at_exit
= false;
69 static bool nodetach_term
, nodetach_daemon
;
71 static char comb_optstr
[256];
72 static struct option comb_lo
[64];
73 static struct option
*comb_next_lo
= &comb_lo
[0];
74 static char comb_helpstr
[4096];
79 const struct option
*longopts
;
82 static void opt_extend(const struct optspec
*os
)
84 const struct option
*lo
;
86 strlcat(comb_optstr
, os
->optstr
, sizeof(comb_optstr
));
87 strlcat(comb_helpstr
, os
->helpstr
, sizeof(comb_helpstr
));
88 for (lo
= os
->longopts
; lo
->name
; lo
++)
89 memcpy(comb_next_lo
++, lo
, sizeof(*lo
));
93 #define OPTION_VTYSOCK 1000
94 #define OPTION_MODULEDIR 1002
95 #define OPTION_LOG 1003
96 #define OPTION_LOGLEVEL 1004
97 #define OPTION_TCLI 1005
98 #define OPTION_DB_FILE 1006
99 #define OPTION_LOGGING 1007
101 static const struct option lo_always
[] = {
102 {"help", no_argument
, NULL
, 'h'},
103 {"version", no_argument
, NULL
, 'v'},
104 {"daemon", no_argument
, NULL
, 'd'},
105 {"module", no_argument
, NULL
, 'M'},
106 {"vty_socket", required_argument
, NULL
, OPTION_VTYSOCK
},
107 {"moduledir", required_argument
, NULL
, OPTION_MODULEDIR
},
108 {"log", required_argument
, NULL
, OPTION_LOG
},
109 {"log-level", required_argument
, NULL
, OPTION_LOGLEVEL
},
110 {"tcli", no_argument
, NULL
, OPTION_TCLI
},
111 {"command-log-always", no_argument
, NULL
, OPTION_LOGGING
},
113 static const struct optspec os_always
= {
115 " -h, --help Display this help and exit\n"
116 " -v, --version Print program version\n"
117 " -d, --daemon Runs in daemon mode\n"
118 " -M, --module Load specified module\n"
119 " --vty_socket Override vty socket path\n"
120 " --moduledir Override modules directory\n"
121 " --log Set Logging to stdout, syslog, or file:<name>\n"
122 " --log-level Set Logging Level to use, debug, info, warn, etc\n"
123 " --tcli Use transaction-based CLI\n",
127 static const struct option lo_cfg_pid_dry
[] = {
128 {"pid_file", required_argument
, NULL
, 'i'},
129 {"config_file", required_argument
, NULL
, 'f'},
131 {"db_file", required_argument
, NULL
, OPTION_DB_FILE
},
133 {"pathspace", required_argument
, NULL
, 'N'},
134 {"dryrun", no_argument
, NULL
, 'C'},
135 {"terminal", no_argument
, NULL
, 't'},
137 static const struct optspec os_cfg_pid_dry
= {
139 " -f, --config_file Set configuration file name\n"
140 " -i, --pid_file Set process identifier file name\n"
142 " --db_file Set database file name\n"
144 " -N, --pathspace Insert prefix into config & socket paths\n"
145 " -C, --dryrun Check configuration for validity and exit\n"
146 " -t, --terminal Open terminal session on stdio\n"
147 " -d -t Daemonize after terminal session ends\n",
151 static const struct option lo_zclient
[] = {
152 {"socket", required_argument
, NULL
, 'z'},
154 static const struct optspec os_zclient
= {
155 "z:", " -z, --socket Set path of zebra socket\n", lo_zclient
};
158 static const struct option lo_vty
[] = {
159 {"vty_addr", required_argument
, NULL
, 'A'},
160 {"vty_port", required_argument
, NULL
, 'P'},
162 static const struct optspec os_vty
= {
164 " -A, --vty_addr Set vty's bind address\n"
165 " -P, --vty_port Set vty's port number\n",
169 static const struct option lo_user
[] = {{"user", required_argument
, NULL
, 'u'},
170 {"group", required_argument
, NULL
, 'g'},
172 static const struct optspec os_user
= {"u:g:",
173 " -u, --user User to run as\n"
174 " -g, --group Group to run as\n",
178 bool frr_zclient_addr(struct sockaddr_storage
*sa
, socklen_t
*sa_len
,
181 memset(sa
, 0, sizeof(*sa
));
184 path
= frr_zclientpath
;
186 if (!strncmp(path
, ZAPI_TCP_PATHNAME
, strlen(ZAPI_TCP_PATHNAME
))) {
187 /* note: this functionality is disabled at bottom */
189 int port
= ZEBRA_PORT
;
191 struct sockaddr_in
*sin
= NULL
;
192 struct sockaddr_in6
*sin6
= NULL
;
194 path
+= strlen(ZAPI_TCP_PATHNAME
);
214 port
= strtoul(path
, &err
, 10);
225 sin
= (struct sockaddr_in
*)sa
;
226 sin
->sin_port
= htons(port
);
227 sin
->sin_addr
.s_addr
= htonl(INADDR_LOOPBACK
);
228 *sa_len
= sizeof(struct sockaddr_in
);
229 #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
230 sin
->sin_len
= *sa_len
;
234 sin6
= (struct sockaddr_in6
*)sa
;
235 sin6
->sin6_port
= htons(port
);
236 inet_pton(AF_INET6
, "::1", &sin6
->sin6_addr
);
237 *sa_len
= sizeof(struct sockaddr_in6
);
239 sin6
->sin6_len
= *sa_len
;
245 /* force-disable this path, because tcp-zebra is a
246 * SECURITY ISSUE. there are no checks at all against
247 * untrusted users on the local system connecting on TCP
248 * and injecting bogus routing data into the entire routing
251 * The functionality is only left here because it may be
252 * useful during development, in order to be able to get
253 * tcpdump or wireshark watching ZAPI as TCP. If you want
254 * to do that, flip the #if 1 above to #if 0. */
255 memset(sa
, 0, sizeof(*sa
));
259 /* "sun" is a #define on solaris */
260 struct sockaddr_un
*suna
= (struct sockaddr_un
*)sa
;
262 suna
->sun_family
= AF_UNIX
;
263 strlcpy(suna
->sun_path
, path
, sizeof(suna
->sun_path
));
264 #ifdef HAVE_STRUCT_SOCKADDR_UN_SUN_LEN
265 *sa_len
= suna
->sun_len
= SUN_LEN(suna
);
267 *sa_len
= sizeof(suna
->sun_family
) + strlen(suna
->sun_path
);
268 #endif /* HAVE_STRUCT_SOCKADDR_UN_SUN_LEN */
270 /* this is left here for future reference; Linux abstract
271 * socket namespace support can be enabled by replacing
272 * above #if 0 with #ifdef GNU_LINUX.
274 * THIS IS A SECURITY ISSUE, the abstract socket namespace
275 * does not have user/group permission control on sockets.
276 * we'd need to implement SCM_CREDENTIALS support first to
277 * check that only proper users can connect to abstract
278 * sockets. (same problem as tcp-zebra, except there is a
279 * fix with SCM_CREDENTIALS. tcp-zebra has no such fix.)
281 if (suna
->sun_path
[0] == '@')
282 suna
->sun_path
[0] = '\0';
288 static struct frr_daemon_info
*di
= NULL
;
290 void frr_init_vtydir(void)
292 snprintf(frr_vtydir
, sizeof(frr_vtydir
), DAEMON_VTY_DIR
, "", "");
295 void frr_preinit(struct frr_daemon_info
*daemon
, int argc
, char **argv
)
299 /* basename(), opencoded. */
300 char *p
= strrchr(argv
[0], '/');
301 di
->progname
= p
? p
+ 1 : argv
[0];
305 opt_extend(&os_always
);
306 if (!(di
->flags
& FRR_NO_CFG_PID_DRY
))
307 opt_extend(&os_cfg_pid_dry
);
308 if (!(di
->flags
& FRR_NO_PRIVSEP
))
309 opt_extend(&os_user
);
310 if (!(di
->flags
& FRR_NO_ZCLIENT
))
311 opt_extend(&os_zclient
);
312 if (!(di
->flags
& FRR_NO_TCPVTY
))
314 if (di
->flags
& FRR_DETACH_LATER
)
315 nodetach_daemon
= true;
318 snprintf(config_default
, sizeof(config_default
), "%s/%s.conf",
319 frr_sysconfdir
, di
->name
);
320 snprintf(pidfile_default
, sizeof(pidfile_default
), "%s/%s.pid",
321 frr_vtydir
, di
->name
);
322 snprintf(frr_zclientpath
, sizeof(frr_zclientpath
),
323 ZEBRA_SERV_PATH
, "", "");
325 snprintf(dbfile_default
, sizeof(dbfile_default
), "%s/%s.db",
326 frr_dbdir
, di
->name
);
329 strlcpy(frr_protoname
, di
->logname
, sizeof(frr_protoname
));
330 strlcpy(frr_protonameinst
, di
->logname
, sizeof(frr_protonameinst
));
332 di
->cli_mode
= FRR_CLI_CLASSIC
;
335 void frr_opt_add(const char *optstr
, const struct option
*longopts
,
338 const struct optspec main_opts
= {optstr
, helpstr
, longopts
};
339 opt_extend(&main_opts
);
342 void frr_help_exit(int status
)
344 FILE *target
= status
? stderr
: stdout
;
347 fprintf(stderr
, "Invalid options.\n\n");
350 di
->printhelp(target
);
352 fprintf(target
, "Usage: %s [OPTION...]\n\n%s%s%s\n\n%s",
353 di
->progname
, di
->proghelp
, di
->copyright
? "\n\n" : "",
354 di
->copyright
? di
->copyright
: "", comb_helpstr
);
355 fprintf(target
, "\nReport bugs to %s\n", FRR_BUG_ADDRESS
);
359 struct option_chain
{
360 struct option_chain
*next
;
364 static struct option_chain
*modules
= NULL
, **modnext
= &modules
;
365 static int errors
= 0;
367 static int frr_opt(int opt
)
369 static int vty_port_set
= 0;
370 static int vty_addr_set
= 0;
371 struct option_chain
*oc
;
379 print_version(di
->progname
);
386 oc
= XMALLOC(MTYPE_TMP
, sizeof(*oc
));
393 if (di
->flags
& FRR_NO_CFG_PID_DRY
)
395 di
->pid_file
= optarg
;
398 if (di
->flags
& FRR_NO_CFG_PID_DRY
)
400 di
->config_file
= optarg
;
403 if (di
->flags
& FRR_NO_CFG_PID_DRY
)
407 "-N/--pathspace option specified more than once!\n");
413 "-N option overriden by -z for zebra named socket path\n");
415 if (strchr(optarg
, '/') || strchr(optarg
, '.')) {
417 "slashes or dots are not permitted in the --pathspace option.\n");
421 di
->pathspace
= optarg
;
424 snprintf(frr_zclientpath
, sizeof(frr_zclientpath
),
425 ZEBRA_SERV_PATH
, "/", di
->pathspace
);
426 snprintf(frr_vtydir
, sizeof(frr_vtydir
), DAEMON_VTY_DIR
, "/",
428 snprintf(pidfile_default
, sizeof(pidfile_default
), "%s/%s.pid",
429 frr_vtydir
, di
->name
);
433 if (di
->flags
& FRR_NO_CFG_PID_DRY
)
435 di
->db_file
= optarg
;
439 if (di
->flags
& FRR_NO_CFG_PID_DRY
)
444 if (di
->flags
& FRR_NO_CFG_PID_DRY
)
449 di
->zpathspace
= true;
452 "-z option overrides -N option for zebra named socket path\n");
453 if (di
->flags
& FRR_NO_ZCLIENT
)
455 strlcpy(frr_zclientpath
, optarg
, sizeof(frr_zclientpath
));
458 if (di
->flags
& FRR_NO_TCPVTY
)
462 "-A option specified more than once!\n");
467 di
->vty_addr
= optarg
;
470 if (di
->flags
& FRR_NO_TCPVTY
)
474 "-P option specified more than once!\n");
479 di
->vty_port
= strtoul(optarg
, &err
, 0);
480 if (*err
|| !*optarg
) {
482 "invalid port number \"%s\" for -P option\n",
489 if (di
->vty_sock_path
) {
491 "--vty_socket option specified more than once!\n");
495 di
->vty_sock_path
= optarg
;
497 case OPTION_MODULEDIR
:
498 if (di
->module_path
) {
500 "----moduledir option specified more than once!\n");
504 di
->module_path
= optarg
;
507 di
->cli_mode
= FRR_CLI_TRANSACTIONAL
;
510 if (di
->flags
& FRR_NO_PRIVSEP
)
512 di
->privs
->user
= optarg
;
515 if (di
->flags
& FRR_NO_PRIVSEP
)
517 di
->privs
->group
= optarg
;
520 di
->early_logging
= optarg
;
522 case OPTION_LOGLEVEL
:
523 di
->early_loglevel
= optarg
;
526 di
->log_always
= true;
534 int frr_getopt(int argc
, char *const argv
[], int *longindex
)
539 comb_next_lo
->name
= NULL
;
542 opt
= getopt_long(argc
, argv
, comb_optstr
, comb_lo
, &lidx
);
547 if (opt
== -1 && errors
)
554 static void frr_mkdir(const char *path
, bool strip
)
559 struct zprivs_ids_t ids
;
562 char *slash
= strrchr(path
, '/');
567 if (plen
> sizeof(buf
) - 1)
569 memcpy(buf
, path
, plen
);
574 /* o+rx (..5) is needed for the frrvty group to work properly;
575 * without it, users in the frrvty group can't access the vty sockets.
578 ret
= mkdir(path
, 0755);
582 /* if EEXIST, return without touching the permissions,
583 * so user-set custom permissions are left in place
588 flog_err(EC_LIB_SYSTEM_CALL
, "failed to mkdir \"%s\": %s", path
,
593 zprivs_get_ids(&ids
);
594 if (chown(path
, ids
.uid_normal
, ids
.gid_normal
))
595 flog_err(EC_LIB_SYSTEM_CALL
, "failed to chown \"%s\": %s", path
,
599 static struct thread_master
*master
;
600 struct thread_master
*frr_init(void)
602 struct option_chain
*oc
;
603 struct frrmod_runtime
*module
;
605 char p_instance
[16] = "", p_pathspace
[256] = "";
607 dir
= di
->module_path
? di
->module_path
: frr_moduledir
;
612 snprintf(frr_protonameinst
, sizeof(frr_protonameinst
), "%s[%u]",
613 di
->logname
, di
->instance
);
614 snprintf(p_instance
, sizeof(p_instance
), "-%d", di
->instance
);
617 snprintf(p_pathspace
, sizeof(p_pathspace
), "%s/",
620 snprintf(config_default
, sizeof(config_default
), "%s%s%s%s.conf",
621 frr_sysconfdir
, p_pathspace
, di
->name
, p_instance
);
622 snprintf(pidfile_default
, sizeof(pidfile_default
), "%s/%s%s.pid",
623 frr_vtydir
, di
->name
, p_instance
);
625 snprintf(dbfile_default
, sizeof(dbfile_default
), "%s/%s%s%s.db",
626 frr_dbdir
, p_pathspace
, di
->name
, p_instance
);
629 zprivs_preinit(di
->privs
);
631 openzlog(di
->progname
, di
->logname
, di
->instance
,
632 LOG_CONS
| LOG_NDELAY
| LOG_PID
, LOG_DAEMON
);
634 command_setup_early_logging(di
->early_logging
, di
->early_loglevel
);
636 if (!frr_zclient_addr(&zclient_addr
, &zclient_addr_len
,
638 fprintf(stderr
, "Invalid zserv socket path: %s\n",
643 /* don't mkdir these as root... */
644 if (!(di
->flags
& FRR_NO_PRIVSEP
)) {
645 if (!di
->pid_file
|| !di
->vty_path
)
646 frr_mkdir(frr_vtydir
, false);
648 frr_mkdir(di
->pid_file
, true);
650 frr_mkdir(di
->vty_path
, true);
653 frrmod_init(di
->module
);
655 modules
= (oc
= modules
)->next
;
656 module
= frrmod_load(oc
->arg
, dir
, moderr
, sizeof(moderr
));
658 fprintf(stderr
, "%s\n", moderr
);
661 XFREE(MTYPE_TMP
, oc
);
664 zprivs_init(di
->privs
);
666 master
= thread_master_create(NULL
);
667 signal_init(master
, di
->n_signals
, di
->signals
);
671 di
->db_file
= dbfile_default
;
672 db_init(di
->db_file
);
675 if (di
->flags
& FRR_LIMITED_CLI
)
680 vty_init(master
, di
->log_always
);
682 log_filter_cmd_init();
692 nb_init(master
, di
->yang_modules
, di
->n_yang_modules
);
693 if (nb_db_init() != NB_OK
)
694 flog_warn(EC_LIB_NB_DATABASE
,
695 "%s: failed to initialize northbound database",
701 const char *frr_get_progname(void)
703 return di
? di
->progname
: NULL
;
706 enum frr_cli_mode
frr_get_cli_mode(void)
708 return di
? di
->cli_mode
: FRR_CLI_CLASSIC
;
711 static int rcvd_signal
= 0;
713 static void rcv_signal(int signum
)
715 rcvd_signal
= signum
;
716 /* poll() is interrupted by the signal; handled below */
719 static void frr_daemon_wait(int fd
)
721 struct pollfd pfd
[1];
725 sigset_t sigs
, prevsigs
;
728 sigaddset(&sigs
, SIGTSTP
);
729 sigaddset(&sigs
, SIGQUIT
);
730 sigaddset(&sigs
, SIGINT
);
731 sigprocmask(SIG_BLOCK
, &sigs
, &prevsigs
);
733 struct sigaction sa
= {
734 .sa_handler
= rcv_signal
, .sa_flags
= SA_RESETHAND
,
736 sigemptyset(&sa
.sa_mask
);
737 sigaction(SIGTSTP
, &sa
, NULL
);
738 sigaction(SIGQUIT
, &sa
, NULL
);
739 sigaction(SIGINT
, &sa
, NULL
);
746 pfd
[0].events
= POLLIN
;
750 #if defined(HAVE_PPOLL)
751 ret
= ppoll(pfd
, 1, NULL
, &prevsigs
);
752 #elif defined(HAVE_POLLTS)
753 ret
= pollts(pfd
, 1, NULL
, &prevsigs
);
755 /* racy -- only used on FreeBSD 9 */
757 sigprocmask(SIG_SETMASK
, &prevsigs
, &tmpsigs
);
758 ret
= poll(pfd
, 1, -1);
759 sigprocmask(SIG_SETMASK
, &tmpsigs
, NULL
);
761 if (ret
< 0 && errno
!= EINTR
&& errno
!= EAGAIN
) {
765 switch (rcvd_signal
) {
769 nrecv
= recv(fd
, buf
, sizeof(buf
), 0);
771 && (errno
== EINTR
|| errno
== EAGAIN
));
774 sigaction(SIGTSTP
, &sa
, NULL
);
786 exitpid
= waitpid(-1, &exitstat
, WNOHANG
);
788 /* child successfully went to main loop & closed socket */
791 /* child failed one way or another ... */
792 if (WIFEXITED(exitstat
) && WEXITSTATUS(exitstat
) == 0)
793 /* can happen in --terminal case if exit is fast enough */
795 else if (WIFEXITED(exitstat
))
796 fprintf(stderr
, "%s failed to start, exited %d\n", di
->name
,
797 WEXITSTATUS(exitstat
));
798 else if (WIFSIGNALED(exitstat
))
799 fprintf(stderr
, "%s crashed in startup, signal %d\n", di
->name
,
802 fprintf(stderr
, "%s failed to start, unknown problem\n",
807 static int daemon_ctl_sock
= -1;
809 static void frr_daemonize(void)
814 if (socketpair(AF_UNIX
, SOCK_STREAM
, 0, fds
)) {
815 perror("socketpair() for daemon control");
834 daemon_ctl_sock
= fds
[1];
839 frr_daemon_wait(fds
[0]);
843 * Why is this a thread?
845 * The read in of config for integrated config happens *after*
846 * thread execution starts( because it is passed in via a vtysh -b -n )
847 * While if you are not using integrated config we want the ability
848 * to read the config in after thread execution starts, so that
849 * we can match this behavior.
851 static int frr_config_read_in(struct thread
*t
)
853 if (!vty_read_config(NULL
, di
->config_file
, config_default
) &&
854 di
->backup_config_file
) {
855 char *orig
= XSTRDUP(MTYPE_TMP
, host_config_get());
857 zlog_info("Attempting to read backup config file: %s specified",
858 di
->backup_config_file
);
859 vty_read_config(NULL
, di
->backup_config_file
, config_default
);
861 host_config_set(orig
);
862 XFREE(MTYPE_TMP
, orig
);
866 * Update the shared candidate after reading the startup configuration.
868 pthread_rwlock_rdlock(&running_config
->lock
);
870 nb_config_replace(vty_shared_candidate_config
, running_config
,
873 pthread_rwlock_unlock(&running_config
->lock
);
878 void frr_config_fork(void)
880 hook_call(frr_late_init
, master
);
882 if (!(di
->flags
& FRR_NO_CFG_PID_DRY
)) {
883 /* Don't start execution if we are in dry-run mode */
885 frr_config_read_in(NULL
);
889 thread_add_event(master
, frr_config_read_in
, NULL
, 0,
893 if (di
->daemon_mode
|| di
->terminal
)
897 di
->pid_file
= pidfile_default
;
898 pid_output(di
->pid_file
);
901 static void frr_vty_serv(void)
903 /* allow explicit override of vty_path in the future
904 * (not currently set anywhere) */
909 snprintf(defvtydir
, sizeof(defvtydir
), "%s", frr_vtydir
);
911 dir
= di
->vty_sock_path
? di
->vty_sock_path
: defvtydir
;
914 snprintf(vtypath_default
, sizeof(vtypath_default
),
915 "%s/%s-%d.vty", dir
, di
->name
, di
->instance
);
917 snprintf(vtypath_default
, sizeof(vtypath_default
),
918 "%s/%s.vty", dir
, di
->name
);
920 di
->vty_path
= vtypath_default
;
923 vty_serv_sock(di
->vty_addr
, di
->vty_port
, di
->vty_path
);
926 static void frr_check_detach(void)
928 if (nodetach_term
|| nodetach_daemon
)
931 if (daemon_ctl_sock
!= -1)
932 close(daemon_ctl_sock
);
933 daemon_ctl_sock
= -1;
936 static void frr_terminal_close(int isexit
)
940 nodetach_term
= false;
943 if (!di
->daemon_mode
|| isexit
) {
944 printf("\n%s exiting\n", di
->name
);
949 printf("\n%s daemonizing\n", di
->name
);
953 nullfd
= open("/dev/null", O_RDONLY
| O_NOCTTY
);
955 flog_err_sys(EC_LIB_SYSTEM_CALL
,
956 "%s: failed to open /dev/null: %s", __func__
,
957 safe_strerror(errno
));
966 static struct thread
*daemon_ctl_thread
= NULL
;
968 static int frr_daemon_ctl(struct thread
*t
)
973 nr
= recv(daemon_ctl_sock
, buf
, sizeof(buf
), 0);
974 if (nr
< 0 && (errno
== EINTR
|| errno
== EAGAIN
))
980 case 'S': /* SIGTSTP */
982 if (send(daemon_ctl_sock
, "s", 1, 0) < 0)
983 zlog_err("%s send(\"s\") error (SIGTSTP propagation)",
984 (di
&& di
->name
? di
->name
: ""));
986 case 'R': /* SIGTCNT [implicit] */
989 case 'I': /* SIGINT */
990 di
->daemon_mode
= false;
993 case 'Q': /* SIGQUIT */
994 di
->daemon_mode
= true;
1000 thread_add_read(master
, frr_daemon_ctl
, NULL
, daemon_ctl_sock
,
1001 &daemon_ctl_thread
);
1005 void frr_detach(void)
1007 nodetach_daemon
= false;
1011 void frr_run(struct thread_master
*master
)
1013 char instanceinfo
[64] = "";
1018 snprintf(instanceinfo
, sizeof(instanceinfo
), "instance %u ",
1021 zlog_notice("%s %s starting: %svty@%d%s", di
->name
, FRR_VERSION
,
1022 instanceinfo
, di
->vty_port
, di
->startinfo
);
1025 nodetach_term
= true;
1027 vty_stdio(frr_terminal_close
);
1028 if (daemon_ctl_sock
!= -1) {
1029 set_nonblocking(daemon_ctl_sock
);
1030 thread_add_read(master
, frr_daemon_ctl
, NULL
,
1031 daemon_ctl_sock
, &daemon_ctl_thread
);
1033 } else if (di
->daemon_mode
) {
1034 int nullfd
= open("/dev/null", O_RDONLY
| O_NOCTTY
);
1036 flog_err_sys(EC_LIB_SYSTEM_CALL
,
1037 "%s: failed to open /dev/null: %s",
1038 __func__
, safe_strerror(errno
));
1049 /* end fixed stderr startup logging */
1050 zlog_startup_stderr
= false;
1052 struct thread thread
;
1053 while (thread_fetch(master
, &thread
))
1054 thread_call(&thread
);
1057 void frr_early_fini(void)
1059 hook_call(frr_early_fini
);
1068 hook_call(frr_fini
);
1070 /* memory_init -> nothing needed */
1079 zprivs_terminate(di
->privs
);
1080 /* signal_init -> nothing needed */
1081 thread_master_free(master
);
1084 /* frrmod_init -> nothing needed / hooks */
1087 if (!debug_memstats_at_exit
)
1090 have_leftovers
= log_memstats(stderr
, di
->name
);
1092 /* in case we decide at runtime that we want exit-memstats for
1093 * a daemon, but it has no stderr because it's daemonized
1094 * (only do this if we actually have something to print though)
1096 if (!have_leftovers
)
1099 snprintf(filename
, sizeof(filename
), "/tmp/frr-memstats-%s-%llu-%llu",
1100 di
->name
, (unsigned long long)getpid(),
1101 (unsigned long long)time(NULL
));
1103 fp
= fopen(filename
, "w");
1105 log_memstats(fp
, di
->name
);
1111 static const char interp
[]
1112 __attribute__((section(".interp"), used
)) = INTERP
;
1115 * executable entry point for libfrr.so
1117 * note that libc initialization is skipped for this so the set of functions
1118 * that can be called is rather limited
1120 extern void _libfrr_version(void)
1121 __attribute__((visibility("hidden"), noreturn
));
1122 void _libfrr_version(void)
1124 const char banner
[] =
1125 FRR_FULL_NAME
" " FRR_VERSION
".\n"
1126 FRR_COPYRIGHT GIT_INFO
"\n"
1127 "configured with:\n " FRR_CONFIG_ARGS
"\n";
1128 write(1, banner
, sizeof(banner
) - 1);