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"
45 DEFINE_HOOK(frr_late_init
, (struct thread_master
* tm
), (tm
))
46 DEFINE_KOOH(frr_early_fini
, (), ())
47 DEFINE_KOOH(frr_fini
, (), ())
49 const char frr_sysconfdir
[] = SYSCONFDIR
;
52 const char frr_dbdir
[] = DAEMON_DB_DIR
;
54 const char frr_moduledir
[] = MODULE_PATH
;
56 char frr_protoname
[256] = "NONE";
57 char frr_protonameinst
[256] = "NONE";
59 char config_default
[512];
60 char frr_zclientpath
[256];
61 static char pidfile_default
[1024];
63 static char dbfile_default
[512];
65 static char vtypath_default
[512];
67 bool debug_memstats_at_exit
= false;
68 static bool nodetach_term
, nodetach_daemon
;
70 static char comb_optstr
[256];
71 static struct option comb_lo
[64];
72 static struct option
*comb_next_lo
= &comb_lo
[0];
73 static char comb_helpstr
[4096];
78 const struct option
*longopts
;
81 static void opt_extend(const struct optspec
*os
)
83 const struct option
*lo
;
85 strlcat(comb_optstr
, os
->optstr
, sizeof(comb_optstr
));
86 strlcat(comb_helpstr
, os
->helpstr
, sizeof(comb_helpstr
));
87 for (lo
= os
->longopts
; lo
->name
; lo
++)
88 memcpy(comb_next_lo
++, lo
, sizeof(*lo
));
92 #define OPTION_VTYSOCK 1000
93 #define OPTION_MODULEDIR 1002
94 #define OPTION_LOG 1003
95 #define OPTION_LOGLEVEL 1004
96 #define OPTION_TCLI 1005
97 #define OPTION_DB_FILE 1006
98 #define OPTION_LOGGING 1007
100 static const struct option lo_always
[] = {
101 {"help", no_argument
, NULL
, 'h'},
102 {"version", no_argument
, NULL
, 'v'},
103 {"daemon", no_argument
, NULL
, 'd'},
104 {"module", no_argument
, NULL
, 'M'},
105 {"vty_socket", required_argument
, NULL
, OPTION_VTYSOCK
},
106 {"moduledir", required_argument
, NULL
, OPTION_MODULEDIR
},
107 {"log", required_argument
, NULL
, OPTION_LOG
},
108 {"log-level", required_argument
, NULL
, OPTION_LOGLEVEL
},
109 {"tcli", no_argument
, NULL
, OPTION_TCLI
},
110 {"command-log-always", no_argument
, NULL
, OPTION_LOGGING
},
112 static const struct optspec os_always
= {
114 " -h, --help Display this help and exit\n"
115 " -v, --version Print program version\n"
116 " -d, --daemon Runs in daemon mode\n"
117 " -M, --module Load specified module\n"
118 " --vty_socket Override vty socket path\n"
119 " --moduledir Override modules directory\n"
120 " --log Set Logging to stdout, syslog, or file:<name>\n"
121 " --log-level Set Logging Level to use, debug, info, warn, etc\n"
122 " --tcli Use transaction-based CLI\n",
126 static const struct option lo_cfg_pid_dry
[] = {
127 {"pid_file", required_argument
, NULL
, 'i'},
128 {"config_file", required_argument
, NULL
, 'f'},
130 {"db_file", required_argument
, NULL
, OPTION_DB_FILE
},
132 {"pathspace", required_argument
, NULL
, 'N'},
133 {"dryrun", no_argument
, NULL
, 'C'},
134 {"terminal", no_argument
, NULL
, 't'},
136 static const struct optspec os_cfg_pid_dry
= {
138 " -f, --config_file Set configuration file name\n"
139 " -i, --pid_file Set process identifier file name\n"
141 " --db_file Set database file name\n"
143 " -N, --pathspace Insert prefix into config & socket paths\n"
144 " -C, --dryrun Check configuration for validity and exit\n"
145 " -t, --terminal Open terminal session on stdio\n"
146 " -d -t Daemonize after terminal session ends\n",
150 static const struct option lo_zclient
[] = {
151 {"socket", required_argument
, NULL
, 'z'},
153 static const struct optspec os_zclient
= {
154 "z:", " -z, --socket Set path of zebra socket\n", lo_zclient
};
157 static const struct option lo_vty
[] = {
158 {"vty_addr", required_argument
, NULL
, 'A'},
159 {"vty_port", required_argument
, NULL
, 'P'},
161 static const struct optspec os_vty
= {
163 " -A, --vty_addr Set vty's bind address\n"
164 " -P, --vty_port Set vty's port number\n",
168 static const struct option lo_user
[] = {{"user", required_argument
, NULL
, 'u'},
169 {"group", required_argument
, NULL
, 'g'},
171 static const struct optspec os_user
= {"u:g:",
172 " -u, --user User to run as\n"
173 " -g, --group Group to run as\n",
177 bool frr_zclient_addr(struct sockaddr_storage
*sa
, socklen_t
*sa_len
,
180 memset(sa
, 0, sizeof(*sa
));
183 path
= frr_zclientpath
;
185 if (!strncmp(path
, ZAPI_TCP_PATHNAME
, strlen(ZAPI_TCP_PATHNAME
))) {
186 /* note: this functionality is disabled at bottom */
188 int port
= ZEBRA_PORT
;
190 struct sockaddr_in
*sin
= NULL
;
191 struct sockaddr_in6
*sin6
= NULL
;
193 path
+= strlen(ZAPI_TCP_PATHNAME
);
213 port
= strtoul(path
, &err
, 10);
224 sin
= (struct sockaddr_in
*)sa
;
225 sin
->sin_port
= htons(port
);
226 sin
->sin_addr
.s_addr
= htonl(INADDR_LOOPBACK
);
227 *sa_len
= sizeof(struct sockaddr_in
);
228 #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
229 sin
->sin_len
= *sa_len
;
233 sin6
= (struct sockaddr_in6
*)sa
;
234 sin6
->sin6_port
= htons(port
);
235 inet_pton(AF_INET6
, "::1", &sin6
->sin6_addr
);
236 *sa_len
= sizeof(struct sockaddr_in6
);
238 sin6
->sin6_len
= *sa_len
;
244 /* force-disable this path, because tcp-zebra is a
245 * SECURITY ISSUE. there are no checks at all against
246 * untrusted users on the local system connecting on TCP
247 * and injecting bogus routing data into the entire routing
250 * The functionality is only left here because it may be
251 * useful during development, in order to be able to get
252 * tcpdump or wireshark watching ZAPI as TCP. If you want
253 * to do that, flip the #if 1 above to #if 0. */
254 memset(sa
, 0, sizeof(*sa
));
258 /* "sun" is a #define on solaris */
259 struct sockaddr_un
*suna
= (struct sockaddr_un
*)sa
;
261 suna
->sun_family
= AF_UNIX
;
262 strlcpy(suna
->sun_path
, path
, sizeof(suna
->sun_path
));
263 #ifdef HAVE_STRUCT_SOCKADDR_UN_SUN_LEN
264 *sa_len
= suna
->sun_len
= SUN_LEN(suna
);
266 *sa_len
= sizeof(suna
->sun_family
) + strlen(suna
->sun_path
);
267 #endif /* HAVE_STRUCT_SOCKADDR_UN_SUN_LEN */
269 /* this is left here for future reference; Linux abstract
270 * socket namespace support can be enabled by replacing
271 * above #if 0 with #ifdef GNU_LINUX.
273 * THIS IS A SECURITY ISSUE, the abstract socket namespace
274 * does not have user/group permission control on sockets.
275 * we'd need to implement SCM_CREDENTIALS support first to
276 * check that only proper users can connect to abstract
277 * sockets. (same problem as tcp-zebra, except there is a
278 * fix with SCM_CREDENTIALS. tcp-zebra has no such fix.)
280 if (suna
->sun_path
[0] == '@')
281 suna
->sun_path
[0] = '\0';
287 static struct frr_daemon_info
*di
= NULL
;
289 void frr_init_vtydir(void)
291 snprintf(frr_vtydir
, sizeof(frr_vtydir
), DAEMON_VTY_DIR
, "", "");
294 void frr_preinit(struct frr_daemon_info
*daemon
, int argc
, char **argv
)
298 /* basename(), opencoded. */
299 char *p
= strrchr(argv
[0], '/');
300 di
->progname
= p
? p
+ 1 : argv
[0];
304 opt_extend(&os_always
);
305 if (!(di
->flags
& FRR_NO_CFG_PID_DRY
))
306 opt_extend(&os_cfg_pid_dry
);
307 if (!(di
->flags
& FRR_NO_PRIVSEP
))
308 opt_extend(&os_user
);
309 if (!(di
->flags
& FRR_NO_ZCLIENT
))
310 opt_extend(&os_zclient
);
311 if (!(di
->flags
& FRR_NO_TCPVTY
))
313 if (di
->flags
& FRR_DETACH_LATER
)
314 nodetach_daemon
= true;
317 snprintf(config_default
, sizeof(config_default
), "%s/%s.conf",
318 frr_sysconfdir
, di
->name
);
319 snprintf(pidfile_default
, sizeof(pidfile_default
), "%s/%s.pid",
320 frr_vtydir
, di
->name
);
321 snprintf(frr_zclientpath
, sizeof(frr_zclientpath
),
322 ZEBRA_SERV_PATH
, "", "");
324 snprintf(dbfile_default
, sizeof(dbfile_default
), "%s/%s.db",
325 frr_dbdir
, di
->name
);
328 strlcpy(frr_protoname
, di
->logname
, sizeof(frr_protoname
));
329 strlcpy(frr_protonameinst
, di
->logname
, sizeof(frr_protonameinst
));
331 di
->cli_mode
= FRR_CLI_CLASSIC
;
334 void frr_opt_add(const char *optstr
, const struct option
*longopts
,
337 const struct optspec main_opts
= {optstr
, helpstr
, longopts
};
338 opt_extend(&main_opts
);
341 void frr_help_exit(int status
)
343 FILE *target
= status
? stderr
: stdout
;
346 fprintf(stderr
, "Invalid options.\n\n");
349 di
->printhelp(target
);
351 fprintf(target
, "Usage: %s [OPTION...]\n\n%s%s%s\n\n%s",
352 di
->progname
, di
->proghelp
, di
->copyright
? "\n\n" : "",
353 di
->copyright
? di
->copyright
: "", comb_helpstr
);
354 fprintf(target
, "\nReport bugs to %s\n", FRR_BUG_ADDRESS
);
358 struct option_chain
{
359 struct option_chain
*next
;
363 static struct option_chain
*modules
= NULL
, **modnext
= &modules
;
364 static int errors
= 0;
366 static int frr_opt(int opt
)
368 static int vty_port_set
= 0;
369 static int vty_addr_set
= 0;
370 struct option_chain
*oc
;
378 print_version(di
->progname
);
385 oc
= XMALLOC(MTYPE_TMP
, sizeof(*oc
));
392 if (di
->flags
& FRR_NO_CFG_PID_DRY
)
394 di
->pid_file
= optarg
;
397 if (di
->flags
& FRR_NO_CFG_PID_DRY
)
399 di
->config_file
= optarg
;
402 if (di
->flags
& FRR_NO_CFG_PID_DRY
)
406 "-N/--pathspace option specified more than once!\n");
412 "-N option overriden by -z for zebra named socket path\n");
414 if (strchr(optarg
, '/') || strchr(optarg
, '.')) {
416 "slashes or dots are not permitted in the --pathspace option.\n");
420 di
->pathspace
= optarg
;
423 snprintf(frr_zclientpath
, sizeof(frr_zclientpath
),
424 ZEBRA_SERV_PATH
, "/", di
->pathspace
);
425 snprintf(frr_vtydir
, sizeof(frr_vtydir
), DAEMON_VTY_DIR
, "/",
427 snprintf(pidfile_default
, sizeof(pidfile_default
), "%s/%s.pid",
428 frr_vtydir
, di
->name
);
432 if (di
->flags
& FRR_NO_CFG_PID_DRY
)
434 di
->db_file
= optarg
;
438 if (di
->flags
& FRR_NO_CFG_PID_DRY
)
443 if (di
->flags
& FRR_NO_CFG_PID_DRY
)
448 di
->zpathspace
= true;
451 "-z option overrides -N option for zebra named socket path\n");
452 if (di
->flags
& FRR_NO_ZCLIENT
)
454 strlcpy(frr_zclientpath
, optarg
, sizeof(frr_zclientpath
));
457 if (di
->flags
& FRR_NO_TCPVTY
)
461 "-A option specified more than once!\n");
466 di
->vty_addr
= optarg
;
469 if (di
->flags
& FRR_NO_TCPVTY
)
473 "-P option specified more than once!\n");
478 di
->vty_port
= strtoul(optarg
, &err
, 0);
479 if (*err
|| !*optarg
) {
481 "invalid port number \"%s\" for -P option\n",
488 if (di
->vty_sock_path
) {
490 "--vty_socket option specified more than once!\n");
494 di
->vty_sock_path
= optarg
;
496 case OPTION_MODULEDIR
:
497 if (di
->module_path
) {
499 "----moduledir option specified more than once!\n");
503 di
->module_path
= optarg
;
506 di
->cli_mode
= FRR_CLI_TRANSACTIONAL
;
509 if (di
->flags
& FRR_NO_PRIVSEP
)
511 di
->privs
->user
= optarg
;
514 if (di
->flags
& FRR_NO_PRIVSEP
)
516 di
->privs
->group
= optarg
;
519 di
->early_logging
= optarg
;
521 case OPTION_LOGLEVEL
:
522 di
->early_loglevel
= optarg
;
525 di
->log_always
= true;
533 int frr_getopt(int argc
, char *const argv
[], int *longindex
)
538 comb_next_lo
->name
= NULL
;
541 opt
= getopt_long(argc
, argv
, comb_optstr
, comb_lo
, &lidx
);
546 if (opt
== -1 && errors
)
553 static void frr_mkdir(const char *path
, bool strip
)
558 struct zprivs_ids_t ids
;
561 char *slash
= strrchr(path
, '/');
566 if (plen
> sizeof(buf
) - 1)
568 memcpy(buf
, path
, plen
);
573 /* o+rx (..5) is needed for the frrvty group to work properly;
574 * without it, users in the frrvty group can't access the vty sockets.
577 ret
= mkdir(path
, 0755);
581 /* if EEXIST, return without touching the permissions,
582 * so user-set custom permissions are left in place
587 flog_err(EC_LIB_SYSTEM_CALL
, "failed to mkdir \"%s\": %s", path
,
592 zprivs_get_ids(&ids
);
593 if (chown(path
, ids
.uid_normal
, ids
.gid_normal
))
594 flog_err(EC_LIB_SYSTEM_CALL
, "failed to chown \"%s\": %s", path
,
598 static struct thread_master
*master
;
599 struct thread_master
*frr_init(void)
601 struct option_chain
*oc
;
602 struct frrmod_runtime
*module
;
604 char p_instance
[16] = "", p_pathspace
[256] = "";
606 dir
= di
->module_path
? di
->module_path
: frr_moduledir
;
611 snprintf(frr_protonameinst
, sizeof(frr_protonameinst
), "%s[%u]",
612 di
->logname
, di
->instance
);
613 snprintf(p_instance
, sizeof(p_instance
), "-%d", di
->instance
);
616 snprintf(p_pathspace
, sizeof(p_pathspace
), "%s/",
619 snprintf(config_default
, sizeof(config_default
), "%s%s%s%s.conf",
620 frr_sysconfdir
, p_pathspace
, di
->name
, p_instance
);
621 snprintf(pidfile_default
, sizeof(pidfile_default
), "%s/%s%s.pid",
622 frr_vtydir
, di
->name
, p_instance
);
624 snprintf(dbfile_default
, sizeof(dbfile_default
), "%s/%s%s%s.db",
625 frr_dbdir
, p_pathspace
, di
->name
, p_instance
);
628 zprivs_preinit(di
->privs
);
630 openzlog(di
->progname
, di
->logname
, di
->instance
,
631 LOG_CONS
| LOG_NDELAY
| LOG_PID
, LOG_DAEMON
);
633 command_setup_early_logging(di
->early_logging
, di
->early_loglevel
);
635 if (!frr_zclient_addr(&zclient_addr
, &zclient_addr_len
,
637 fprintf(stderr
, "Invalid zserv socket path: %s\n",
642 /* don't mkdir these as root... */
643 if (!(di
->flags
& FRR_NO_PRIVSEP
)) {
644 if (!di
->pid_file
|| !di
->vty_path
)
645 frr_mkdir(frr_vtydir
, false);
647 frr_mkdir(di
->pid_file
, true);
649 frr_mkdir(di
->vty_path
, true);
652 frrmod_init(di
->module
);
654 modules
= (oc
= modules
)->next
;
655 module
= frrmod_load(oc
->arg
, dir
, moderr
, sizeof(moderr
));
657 fprintf(stderr
, "%s\n", moderr
);
660 XFREE(MTYPE_TMP
, oc
);
663 zprivs_init(di
->privs
);
665 master
= thread_master_create(NULL
);
666 signal_init(master
, di
->n_signals
, di
->signals
);
670 di
->db_file
= dbfile_default
;
671 db_init(di
->db_file
);
674 if (di
->flags
& FRR_LIMITED_CLI
)
679 vty_init(master
, di
->log_always
);
681 log_filter_cmd_init();
691 nb_init(master
, di
->yang_modules
, di
->n_yang_modules
);
692 if (nb_db_init() != NB_OK
)
693 flog_warn(EC_LIB_NB_DATABASE
,
694 "%s: failed to initialize northbound database",
700 const char *frr_get_progname(void)
702 return di
? di
->progname
: NULL
;
705 enum frr_cli_mode
frr_get_cli_mode(void)
707 return di
? di
->cli_mode
: FRR_CLI_CLASSIC
;
710 static int rcvd_signal
= 0;
712 static void rcv_signal(int signum
)
714 rcvd_signal
= signum
;
715 /* poll() is interrupted by the signal; handled below */
718 static void frr_daemon_wait(int fd
)
720 struct pollfd pfd
[1];
724 sigset_t sigs
, prevsigs
;
727 sigaddset(&sigs
, SIGTSTP
);
728 sigaddset(&sigs
, SIGQUIT
);
729 sigaddset(&sigs
, SIGINT
);
730 sigprocmask(SIG_BLOCK
, &sigs
, &prevsigs
);
732 struct sigaction sa
= {
733 .sa_handler
= rcv_signal
, .sa_flags
= SA_RESETHAND
,
735 sigemptyset(&sa
.sa_mask
);
736 sigaction(SIGTSTP
, &sa
, NULL
);
737 sigaction(SIGQUIT
, &sa
, NULL
);
738 sigaction(SIGINT
, &sa
, NULL
);
745 pfd
[0].events
= POLLIN
;
749 #if defined(HAVE_PPOLL)
750 ret
= ppoll(pfd
, 1, NULL
, &prevsigs
);
751 #elif defined(HAVE_POLLTS)
752 ret
= pollts(pfd
, 1, NULL
, &prevsigs
);
754 /* racy -- only used on FreeBSD 9 */
756 sigprocmask(SIG_SETMASK
, &prevsigs
, &tmpsigs
);
757 ret
= poll(pfd
, 1, -1);
758 sigprocmask(SIG_SETMASK
, &tmpsigs
, NULL
);
760 if (ret
< 0 && errno
!= EINTR
&& errno
!= EAGAIN
) {
764 switch (rcvd_signal
) {
768 nrecv
= recv(fd
, buf
, sizeof(buf
), 0);
770 && (errno
== EINTR
|| errno
== EAGAIN
));
773 sigaction(SIGTSTP
, &sa
, NULL
);
785 exitpid
= waitpid(-1, &exitstat
, WNOHANG
);
787 /* child successfully went to main loop & closed socket */
790 /* child failed one way or another ... */
791 if (WIFEXITED(exitstat
) && WEXITSTATUS(exitstat
) == 0)
792 /* can happen in --terminal case if exit is fast enough */
794 else if (WIFEXITED(exitstat
))
795 fprintf(stderr
, "%s failed to start, exited %d\n", di
->name
,
796 WEXITSTATUS(exitstat
));
797 else if (WIFSIGNALED(exitstat
))
798 fprintf(stderr
, "%s crashed in startup, signal %d\n", di
->name
,
801 fprintf(stderr
, "%s failed to start, unknown problem\n",
806 static int daemon_ctl_sock
= -1;
808 static void frr_daemonize(void)
813 if (socketpair(AF_UNIX
, SOCK_STREAM
, 0, fds
)) {
814 perror("socketpair() for daemon control");
833 daemon_ctl_sock
= fds
[1];
838 frr_daemon_wait(fds
[0]);
842 * Why is this a thread?
844 * The read in of config for integrated config happens *after*
845 * thread execution starts( because it is passed in via a vtysh -b -n )
846 * While if you are not using integrated config we want the ability
847 * to read the config in after thread execution starts, so that
848 * we can match this behavior.
850 static int frr_config_read_in(struct thread
*t
)
852 if (!vty_read_config(NULL
, di
->config_file
, config_default
) &&
853 di
->backup_config_file
) {
854 char *orig
= XSTRDUP(MTYPE_TMP
, host_config_get());
856 zlog_info("Attempting to read backup config file: %s specified",
857 di
->backup_config_file
);
858 vty_read_config(NULL
, di
->backup_config_file
, config_default
);
860 host_config_set(orig
);
861 XFREE(MTYPE_TMP
, orig
);
865 * Update the shared candidate after reading the startup configuration.
867 pthread_rwlock_rdlock(&running_config
->lock
);
869 nb_config_replace(vty_shared_candidate_config
, running_config
,
872 pthread_rwlock_unlock(&running_config
->lock
);
877 void frr_config_fork(void)
879 hook_call(frr_late_init
, master
);
881 if (!(di
->flags
& FRR_NO_CFG_PID_DRY
)) {
882 /* Don't start execution if we are in dry-run mode */
884 frr_config_read_in(NULL
);
888 thread_add_event(master
, frr_config_read_in
, NULL
, 0,
892 if (di
->daemon_mode
|| di
->terminal
)
896 di
->pid_file
= pidfile_default
;
897 pid_output(di
->pid_file
);
900 static void frr_vty_serv(void)
902 /* allow explicit override of vty_path in the future
903 * (not currently set anywhere) */
908 snprintf(defvtydir
, sizeof(defvtydir
), "%s", frr_vtydir
);
910 dir
= di
->vty_sock_path
? di
->vty_sock_path
: defvtydir
;
913 snprintf(vtypath_default
, sizeof(vtypath_default
),
914 "%s/%s-%d.vty", dir
, di
->name
, di
->instance
);
916 snprintf(vtypath_default
, sizeof(vtypath_default
),
917 "%s/%s.vty", dir
, di
->name
);
919 di
->vty_path
= vtypath_default
;
922 vty_serv_sock(di
->vty_addr
, di
->vty_port
, di
->vty_path
);
925 static void frr_check_detach(void)
927 if (nodetach_term
|| nodetach_daemon
)
930 if (daemon_ctl_sock
!= -1)
931 close(daemon_ctl_sock
);
932 daemon_ctl_sock
= -1;
935 static void frr_terminal_close(int isexit
)
939 nodetach_term
= false;
942 if (!di
->daemon_mode
|| isexit
) {
943 printf("\n%s exiting\n", di
->name
);
948 printf("\n%s daemonizing\n", di
->name
);
952 nullfd
= open("/dev/null", O_RDONLY
| O_NOCTTY
);
954 flog_err_sys(EC_LIB_SYSTEM_CALL
,
955 "%s: failed to open /dev/null: %s", __func__
,
956 safe_strerror(errno
));
965 static struct thread
*daemon_ctl_thread
= NULL
;
967 static int frr_daemon_ctl(struct thread
*t
)
972 nr
= recv(daemon_ctl_sock
, buf
, sizeof(buf
), 0);
973 if (nr
< 0 && (errno
== EINTR
|| errno
== EAGAIN
))
979 case 'S': /* SIGTSTP */
981 if (send(daemon_ctl_sock
, "s", 1, 0) < 0)
982 zlog_err("%s send(\"s\") error (SIGTSTP propagation)",
983 (di
&& di
->name
? di
->name
: ""));
985 case 'R': /* SIGTCNT [implicit] */
988 case 'I': /* SIGINT */
989 di
->daemon_mode
= false;
992 case 'Q': /* SIGQUIT */
993 di
->daemon_mode
= true;
999 thread_add_read(master
, frr_daemon_ctl
, NULL
, daemon_ctl_sock
,
1000 &daemon_ctl_thread
);
1004 void frr_detach(void)
1006 nodetach_daemon
= false;
1010 void frr_run(struct thread_master
*master
)
1012 char instanceinfo
[64] = "";
1017 snprintf(instanceinfo
, sizeof(instanceinfo
), "instance %u ",
1020 zlog_notice("%s %s starting: %svty@%d%s", di
->name
, FRR_VERSION
,
1021 instanceinfo
, di
->vty_port
, di
->startinfo
);
1024 nodetach_term
= true;
1026 vty_stdio(frr_terminal_close
);
1027 if (daemon_ctl_sock
!= -1) {
1028 set_nonblocking(daemon_ctl_sock
);
1029 thread_add_read(master
, frr_daemon_ctl
, NULL
,
1030 daemon_ctl_sock
, &daemon_ctl_thread
);
1032 } else if (di
->daemon_mode
) {
1033 int nullfd
= open("/dev/null", O_RDONLY
| O_NOCTTY
);
1035 flog_err_sys(EC_LIB_SYSTEM_CALL
,
1036 "%s: failed to open /dev/null: %s",
1037 __func__
, safe_strerror(errno
));
1048 /* end fixed stderr startup logging */
1049 zlog_startup_stderr
= false;
1051 struct thread thread
;
1052 while (thread_fetch(master
, &thread
))
1053 thread_call(&thread
);
1056 void frr_early_fini(void)
1058 hook_call(frr_early_fini
);
1067 hook_call(frr_fini
);
1069 /* memory_init -> nothing needed */
1078 zprivs_terminate(di
->privs
);
1079 /* signal_init -> nothing needed */
1080 thread_master_free(master
);
1083 /* frrmod_init -> nothing needed / hooks */
1085 if (!debug_memstats_at_exit
)
1088 have_leftovers
= log_memstats(stderr
, di
->name
);
1090 /* in case we decide at runtime that we want exit-memstats for
1091 * a daemon, but it has no stderr because it's daemonized
1092 * (only do this if we actually have something to print though)
1094 if (!have_leftovers
)
1097 snprintf(filename
, sizeof(filename
), "/tmp/frr-memstats-%s-%llu-%llu",
1098 di
->name
, (unsigned long long)getpid(),
1099 (unsigned long long)time(NULL
));
1101 fp
= fopen(filename
, "w");
1103 log_memstats(fp
, di
->name
);
1109 static const char interp
[]
1110 __attribute__((section(".interp"), used
)) = INTERP
;
1113 * executable entry point for libfrr.so
1115 * note that libc initialization is skipped for this so the set of functions
1116 * that can be called is rather limited
1118 extern void _libfrr_version(void)
1119 __attribute__((visibility("hidden"), noreturn
));
1120 void _libfrr_version(void)
1122 const char banner
[] =
1123 FRR_FULL_NAME
" " FRR_VERSION
".\n"
1124 FRR_COPYRIGHT GIT_INFO
"\n"
1125 "configured with:\n " FRR_CONFIG_ARGS
"\n";
1126 write(1, banner
, sizeof(banner
) - 1);