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 #include "frr_pthread.h"
47 DEFINE_HOOK(frr_late_init
, (struct thread_master
* tm
), (tm
))
48 DEFINE_KOOH(frr_early_fini
, (), ())
49 DEFINE_KOOH(frr_fini
, (), ())
51 const char frr_sysconfdir
[] = SYSCONFDIR
;
54 const char frr_dbdir
[] = DAEMON_DB_DIR
;
56 const char frr_moduledir
[] = MODULE_PATH
;
58 char frr_protoname
[256] = "NONE";
59 char frr_protonameinst
[256] = "NONE";
61 char config_default
[512];
62 char frr_zclientpath
[256];
63 static char pidfile_default
[1024];
65 static char dbfile_default
[512];
67 static char vtypath_default
[512];
69 bool debug_memstats_at_exit
= false;
70 static bool nodetach_term
, nodetach_daemon
;
72 static char comb_optstr
[256];
73 static struct option comb_lo
[64];
74 static struct option
*comb_next_lo
= &comb_lo
[0];
75 static char comb_helpstr
[4096];
80 const struct option
*longopts
;
83 static void opt_extend(const struct optspec
*os
)
85 const struct option
*lo
;
87 strlcat(comb_optstr
, os
->optstr
, sizeof(comb_optstr
));
88 strlcat(comb_helpstr
, os
->helpstr
, sizeof(comb_helpstr
));
89 for (lo
= os
->longopts
; lo
->name
; lo
++)
90 memcpy(comb_next_lo
++, lo
, sizeof(*lo
));
94 #define OPTION_VTYSOCK 1000
95 #define OPTION_MODULEDIR 1002
96 #define OPTION_LOG 1003
97 #define OPTION_LOGLEVEL 1004
98 #define OPTION_TCLI 1005
99 #define OPTION_DB_FILE 1006
100 #define OPTION_LOGGING 1007
102 static const struct option lo_always
[] = {
103 {"help", no_argument
, NULL
, 'h'},
104 {"version", no_argument
, NULL
, 'v'},
105 {"daemon", no_argument
, NULL
, 'd'},
106 {"module", no_argument
, NULL
, 'M'},
107 {"vty_socket", required_argument
, NULL
, OPTION_VTYSOCK
},
108 {"moduledir", required_argument
, NULL
, OPTION_MODULEDIR
},
109 {"log", required_argument
, NULL
, OPTION_LOG
},
110 {"log-level", required_argument
, NULL
, OPTION_LOGLEVEL
},
111 {"tcli", no_argument
, NULL
, OPTION_TCLI
},
112 {"command-log-always", no_argument
, NULL
, OPTION_LOGGING
},
114 static const struct optspec os_always
= {
116 " -h, --help Display this help and exit\n"
117 " -v, --version Print program version\n"
118 " -d, --daemon Runs in daemon mode\n"
119 " -M, --module Load specified module\n"
120 " --vty_socket Override vty socket path\n"
121 " --moduledir Override modules directory\n"
122 " --log Set Logging to stdout, syslog, or file:<name>\n"
123 " --log-level Set Logging Level to use, debug, info, warn, etc\n"
124 " --tcli Use transaction-based CLI\n",
128 static const struct option lo_cfg_pid_dry
[] = {
129 {"pid_file", required_argument
, NULL
, 'i'},
130 {"config_file", required_argument
, NULL
, 'f'},
132 {"db_file", required_argument
, NULL
, OPTION_DB_FILE
},
134 {"pathspace", required_argument
, NULL
, 'N'},
135 {"dryrun", no_argument
, NULL
, 'C'},
136 {"terminal", no_argument
, NULL
, 't'},
138 static const struct optspec os_cfg_pid_dry
= {
140 " -f, --config_file Set configuration file name\n"
141 " -i, --pid_file Set process identifier file name\n"
143 " --db_file Set database file name\n"
145 " -N, --pathspace Insert prefix into config & socket paths\n"
146 " -C, --dryrun Check configuration for validity and exit\n"
147 " -t, --terminal Open terminal session on stdio\n"
148 " -d -t Daemonize after terminal session ends\n",
152 static const struct option lo_zclient
[] = {
153 {"socket", required_argument
, NULL
, 'z'},
155 static const struct optspec os_zclient
= {
156 "z:", " -z, --socket Set path of zebra socket\n", lo_zclient
};
159 static const struct option lo_vty
[] = {
160 {"vty_addr", required_argument
, NULL
, 'A'},
161 {"vty_port", required_argument
, NULL
, 'P'},
163 static const struct optspec os_vty
= {
165 " -A, --vty_addr Set vty's bind address\n"
166 " -P, --vty_port Set vty's port number\n",
170 static const struct option lo_user
[] = {{"user", required_argument
, NULL
, 'u'},
171 {"group", required_argument
, NULL
, 'g'},
173 static const struct optspec os_user
= {"u:g:",
174 " -u, --user User to run as\n"
175 " -g, --group Group to run as\n",
179 bool frr_zclient_addr(struct sockaddr_storage
*sa
, socklen_t
*sa_len
,
182 memset(sa
, 0, sizeof(*sa
));
185 path
= frr_zclientpath
;
187 if (!strncmp(path
, ZAPI_TCP_PATHNAME
, strlen(ZAPI_TCP_PATHNAME
))) {
188 /* note: this functionality is disabled at bottom */
190 int port
= ZEBRA_PORT
;
192 struct sockaddr_in
*sin
= NULL
;
193 struct sockaddr_in6
*sin6
= NULL
;
195 path
+= strlen(ZAPI_TCP_PATHNAME
);
215 port
= strtoul(path
, &err
, 10);
226 sin
= (struct sockaddr_in
*)sa
;
227 sin
->sin_port
= htons(port
);
228 sin
->sin_addr
.s_addr
= htonl(INADDR_LOOPBACK
);
229 *sa_len
= sizeof(struct sockaddr_in
);
230 #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
231 sin
->sin_len
= *sa_len
;
235 sin6
= (struct sockaddr_in6
*)sa
;
236 sin6
->sin6_port
= htons(port
);
237 inet_pton(AF_INET6
, "::1", &sin6
->sin6_addr
);
238 *sa_len
= sizeof(struct sockaddr_in6
);
240 sin6
->sin6_len
= *sa_len
;
246 /* force-disable this path, because tcp-zebra is a
247 * SECURITY ISSUE. there are no checks at all against
248 * untrusted users on the local system connecting on TCP
249 * and injecting bogus routing data into the entire routing
252 * The functionality is only left here because it may be
253 * useful during development, in order to be able to get
254 * tcpdump or wireshark watching ZAPI as TCP. If you want
255 * to do that, flip the #if 1 above to #if 0. */
256 memset(sa
, 0, sizeof(*sa
));
260 /* "sun" is a #define on solaris */
261 struct sockaddr_un
*suna
= (struct sockaddr_un
*)sa
;
263 suna
->sun_family
= AF_UNIX
;
264 strlcpy(suna
->sun_path
, path
, sizeof(suna
->sun_path
));
265 #ifdef HAVE_STRUCT_SOCKADDR_UN_SUN_LEN
266 *sa_len
= suna
->sun_len
= SUN_LEN(suna
);
268 *sa_len
= sizeof(suna
->sun_family
) + strlen(suna
->sun_path
);
269 #endif /* HAVE_STRUCT_SOCKADDR_UN_SUN_LEN */
271 /* this is left here for future reference; Linux abstract
272 * socket namespace support can be enabled by replacing
273 * above #if 0 with #ifdef GNU_LINUX.
275 * THIS IS A SECURITY ISSUE, the abstract socket namespace
276 * does not have user/group permission control on sockets.
277 * we'd need to implement SCM_CREDENTIALS support first to
278 * check that only proper users can connect to abstract
279 * sockets. (same problem as tcp-zebra, except there is a
280 * fix with SCM_CREDENTIALS. tcp-zebra has no such fix.)
282 if (suna
->sun_path
[0] == '@')
283 suna
->sun_path
[0] = '\0';
289 static struct frr_daemon_info
*di
= NULL
;
291 void frr_init_vtydir(void)
293 snprintf(frr_vtydir
, sizeof(frr_vtydir
), DAEMON_VTY_DIR
, "", "");
296 void frr_preinit(struct frr_daemon_info
*daemon
, int argc
, char **argv
)
300 /* basename(), opencoded. */
301 char *p
= strrchr(argv
[0], '/');
302 di
->progname
= p
? p
+ 1 : argv
[0];
306 opt_extend(&os_always
);
307 if (!(di
->flags
& FRR_NO_CFG_PID_DRY
))
308 opt_extend(&os_cfg_pid_dry
);
309 if (!(di
->flags
& FRR_NO_PRIVSEP
))
310 opt_extend(&os_user
);
311 if (!(di
->flags
& FRR_NO_ZCLIENT
))
312 opt_extend(&os_zclient
);
313 if (!(di
->flags
& FRR_NO_TCPVTY
))
315 if (di
->flags
& FRR_DETACH_LATER
)
316 nodetach_daemon
= true;
319 snprintf(config_default
, sizeof(config_default
), "%s/%s.conf",
320 frr_sysconfdir
, di
->name
);
321 snprintf(pidfile_default
, sizeof(pidfile_default
), "%s/%s.pid",
322 frr_vtydir
, di
->name
);
323 snprintf(frr_zclientpath
, sizeof(frr_zclientpath
),
324 ZEBRA_SERV_PATH
, "", "");
326 snprintf(dbfile_default
, sizeof(dbfile_default
), "%s/%s.db",
327 frr_dbdir
, di
->name
);
330 strlcpy(frr_protoname
, di
->logname
, sizeof(frr_protoname
));
331 strlcpy(frr_protonameinst
, di
->logname
, sizeof(frr_protonameinst
));
333 di
->cli_mode
= FRR_CLI_CLASSIC
;
336 void frr_opt_add(const char *optstr
, const struct option
*longopts
,
339 const struct optspec main_opts
= {optstr
, helpstr
, longopts
};
340 opt_extend(&main_opts
);
343 void frr_help_exit(int status
)
345 FILE *target
= status
? stderr
: stdout
;
348 fprintf(stderr
, "Invalid options.\n\n");
351 di
->printhelp(target
);
353 fprintf(target
, "Usage: %s [OPTION...]\n\n%s%s%s\n\n%s",
354 di
->progname
, di
->proghelp
, di
->copyright
? "\n\n" : "",
355 di
->copyright
? di
->copyright
: "", comb_helpstr
);
356 fprintf(target
, "\nReport bugs to %s\n", FRR_BUG_ADDRESS
);
360 struct option_chain
{
361 struct option_chain
*next
;
365 static struct option_chain
*modules
= NULL
, **modnext
= &modules
;
366 static int errors
= 0;
368 static int frr_opt(int opt
)
370 static int vty_port_set
= 0;
371 static int vty_addr_set
= 0;
372 struct option_chain
*oc
;
380 print_version(di
->progname
);
387 oc
= XMALLOC(MTYPE_TMP
, sizeof(*oc
));
394 if (di
->flags
& FRR_NO_CFG_PID_DRY
)
396 di
->pid_file
= optarg
;
399 if (di
->flags
& FRR_NO_CFG_PID_DRY
)
401 di
->config_file
= optarg
;
404 if (di
->flags
& FRR_NO_CFG_PID_DRY
)
408 "-N/--pathspace option specified more than once!\n");
414 "-N option overridden by -z for zebra named socket path\n");
416 if (strchr(optarg
, '/') || strchr(optarg
, '.')) {
418 "slashes or dots are not permitted in the --pathspace option.\n");
422 di
->pathspace
= optarg
;
425 snprintf(frr_zclientpath
, sizeof(frr_zclientpath
),
426 ZEBRA_SERV_PATH
, "/", di
->pathspace
);
427 snprintf(frr_vtydir
, sizeof(frr_vtydir
), DAEMON_VTY_DIR
, "/",
429 snprintf(pidfile_default
, sizeof(pidfile_default
), "%s/%s.pid",
430 frr_vtydir
, di
->name
);
434 if (di
->flags
& FRR_NO_CFG_PID_DRY
)
436 di
->db_file
= optarg
;
440 if (di
->flags
& FRR_NO_CFG_PID_DRY
)
445 if (di
->flags
& FRR_NO_CFG_PID_DRY
)
450 di
->zpathspace
= true;
453 "-z option overrides -N option for zebra named socket path\n");
454 if (di
->flags
& FRR_NO_ZCLIENT
)
456 strlcpy(frr_zclientpath
, optarg
, sizeof(frr_zclientpath
));
459 if (di
->flags
& FRR_NO_TCPVTY
)
463 "-A option specified more than once!\n");
468 di
->vty_addr
= optarg
;
471 if (di
->flags
& FRR_NO_TCPVTY
)
475 "-P option specified more than once!\n");
480 di
->vty_port
= strtoul(optarg
, &err
, 0);
481 if (*err
|| !*optarg
) {
483 "invalid port number \"%s\" for -P option\n",
490 if (di
->vty_sock_path
) {
492 "--vty_socket option specified more than once!\n");
496 di
->vty_sock_path
= optarg
;
498 case OPTION_MODULEDIR
:
499 if (di
->module_path
) {
501 "----moduledir option specified more than once!\n");
505 di
->module_path
= optarg
;
508 di
->cli_mode
= FRR_CLI_TRANSACTIONAL
;
511 if (di
->flags
& FRR_NO_PRIVSEP
)
513 di
->privs
->user
= optarg
;
516 if (di
->flags
& FRR_NO_PRIVSEP
)
518 di
->privs
->group
= optarg
;
521 di
->early_logging
= optarg
;
523 case OPTION_LOGLEVEL
:
524 di
->early_loglevel
= optarg
;
527 di
->log_always
= true;
535 int frr_getopt(int argc
, char *const argv
[], int *longindex
)
540 comb_next_lo
->name
= NULL
;
543 opt
= getopt_long(argc
, argv
, comb_optstr
, comb_lo
, &lidx
);
548 if (opt
== -1 && errors
)
555 static void frr_mkdir(const char *path
, bool strip
)
560 struct zprivs_ids_t ids
;
563 char *slash
= strrchr(path
, '/');
568 if (plen
> sizeof(buf
) - 1)
570 memcpy(buf
, path
, plen
);
575 /* o+rx (..5) is needed for the frrvty group to work properly;
576 * without it, users in the frrvty group can't access the vty sockets.
579 ret
= mkdir(path
, 0755);
583 /* if EEXIST, return without touching the permissions,
584 * so user-set custom permissions are left in place
589 flog_err(EC_LIB_SYSTEM_CALL
, "failed to mkdir \"%s\": %s", path
,
594 zprivs_get_ids(&ids
);
595 if (chown(path
, ids
.uid_normal
, ids
.gid_normal
))
596 flog_err(EC_LIB_SYSTEM_CALL
, "failed to chown \"%s\": %s", path
,
600 static struct thread_master
*master
;
601 struct thread_master
*frr_init(void)
603 struct option_chain
*oc
;
604 struct frrmod_runtime
*module
;
606 char p_instance
[16] = "", p_pathspace
[256] = "";
608 dir
= di
->module_path
? di
->module_path
: frr_moduledir
;
613 snprintf(frr_protonameinst
, sizeof(frr_protonameinst
), "%s[%u]",
614 di
->logname
, di
->instance
);
615 snprintf(p_instance
, sizeof(p_instance
), "-%d", di
->instance
);
618 snprintf(p_pathspace
, sizeof(p_pathspace
), "%s/",
621 snprintf(config_default
, sizeof(config_default
), "%s%s%s%s.conf",
622 frr_sysconfdir
, p_pathspace
, di
->name
, p_instance
);
623 snprintf(pidfile_default
, sizeof(pidfile_default
), "%s/%s%s.pid",
624 frr_vtydir
, di
->name
, p_instance
);
626 snprintf(dbfile_default
, sizeof(dbfile_default
), "%s/%s%s%s.db",
627 frr_dbdir
, p_pathspace
, di
->name
, p_instance
);
630 zprivs_preinit(di
->privs
);
632 openzlog(di
->progname
, di
->logname
, di
->instance
,
633 LOG_CONS
| LOG_NDELAY
| LOG_PID
, LOG_DAEMON
);
635 command_setup_early_logging(di
->early_logging
, di
->early_loglevel
);
637 if (!frr_zclient_addr(&zclient_addr
, &zclient_addr_len
,
639 fprintf(stderr
, "Invalid zserv socket path: %s\n",
644 /* don't mkdir these as root... */
645 if (!(di
->flags
& FRR_NO_PRIVSEP
)) {
646 if (!di
->pid_file
|| !di
->vty_path
)
647 frr_mkdir(frr_vtydir
, false);
649 frr_mkdir(di
->pid_file
, true);
651 frr_mkdir(di
->vty_path
, true);
654 frrmod_init(di
->module
);
656 modules
= (oc
= modules
)->next
;
657 module
= frrmod_load(oc
->arg
, dir
, moderr
, sizeof(moderr
));
659 fprintf(stderr
, "%s\n", moderr
);
662 XFREE(MTYPE_TMP
, oc
);
665 zprivs_init(di
->privs
);
667 master
= thread_master_create(NULL
);
668 signal_init(master
, di
->n_signals
, di
->signals
);
672 di
->db_file
= dbfile_default
;
673 db_init(di
->db_file
);
676 if (di
->flags
& FRR_LIMITED_CLI
)
681 vty_init(master
, di
->log_always
);
683 log_filter_cmd_init();
695 nb_init(master
, di
->yang_modules
, di
->n_yang_modules
);
696 if (nb_db_init() != NB_OK
)
697 flog_warn(EC_LIB_NB_DATABASE
,
698 "%s: failed to initialize northbound database",
704 const char *frr_get_progname(void)
706 return di
? di
->progname
: NULL
;
709 enum frr_cli_mode
frr_get_cli_mode(void)
711 return di
? di
->cli_mode
: FRR_CLI_CLASSIC
;
714 static int rcvd_signal
= 0;
716 static void rcv_signal(int signum
)
718 rcvd_signal
= signum
;
719 /* poll() is interrupted by the signal; handled below */
722 static void frr_daemon_wait(int fd
)
724 struct pollfd pfd
[1];
728 sigset_t sigs
, prevsigs
;
731 sigaddset(&sigs
, SIGTSTP
);
732 sigaddset(&sigs
, SIGQUIT
);
733 sigaddset(&sigs
, SIGINT
);
734 sigprocmask(SIG_BLOCK
, &sigs
, &prevsigs
);
736 struct sigaction sa
= {
737 .sa_handler
= rcv_signal
, .sa_flags
= SA_RESETHAND
,
739 sigemptyset(&sa
.sa_mask
);
740 sigaction(SIGTSTP
, &sa
, NULL
);
741 sigaction(SIGQUIT
, &sa
, NULL
);
742 sigaction(SIGINT
, &sa
, NULL
);
749 pfd
[0].events
= POLLIN
;
753 #if defined(HAVE_PPOLL)
754 ret
= ppoll(pfd
, 1, NULL
, &prevsigs
);
755 #elif defined(HAVE_POLLTS)
756 ret
= pollts(pfd
, 1, NULL
, &prevsigs
);
758 /* racy -- only used on FreeBSD 9 */
760 sigprocmask(SIG_SETMASK
, &prevsigs
, &tmpsigs
);
761 ret
= poll(pfd
, 1, -1);
762 sigprocmask(SIG_SETMASK
, &tmpsigs
, NULL
);
764 if (ret
< 0 && errno
!= EINTR
&& errno
!= EAGAIN
) {
768 switch (rcvd_signal
) {
772 nrecv
= recv(fd
, buf
, sizeof(buf
), 0);
774 && (errno
== EINTR
|| errno
== EAGAIN
));
777 sigaction(SIGTSTP
, &sa
, NULL
);
789 exitpid
= waitpid(-1, &exitstat
, WNOHANG
);
791 /* child successfully went to main loop & closed socket */
794 /* child failed one way or another ... */
795 if (WIFEXITED(exitstat
) && WEXITSTATUS(exitstat
) == 0)
796 /* can happen in --terminal case if exit is fast enough */
798 else if (WIFEXITED(exitstat
))
799 fprintf(stderr
, "%s failed to start, exited %d\n", di
->name
,
800 WEXITSTATUS(exitstat
));
801 else if (WIFSIGNALED(exitstat
))
802 fprintf(stderr
, "%s crashed in startup, signal %d\n", di
->name
,
805 fprintf(stderr
, "%s failed to start, unknown problem\n",
810 static int daemon_ctl_sock
= -1;
812 static void frr_daemonize(void)
817 if (socketpair(AF_UNIX
, SOCK_STREAM
, 0, fds
)) {
818 perror("socketpair() for daemon control");
837 daemon_ctl_sock
= fds
[1];
842 frr_daemon_wait(fds
[0]);
846 * Why is this a thread?
848 * The read in of config for integrated config happens *after*
849 * thread execution starts( because it is passed in via a vtysh -b -n )
850 * While if you are not using integrated config we want the ability
851 * to read the config in after thread execution starts, so that
852 * we can match this behavior.
854 static int frr_config_read_in(struct thread
*t
)
856 if (!vty_read_config(vty_shared_candidate_config
, di
->config_file
,
858 && di
->backup_config_file
) {
859 char *orig
= XSTRDUP(MTYPE_TMP
, host_config_get());
861 zlog_info("Attempting to read backup config file: %s specified",
862 di
->backup_config_file
);
863 vty_read_config(vty_shared_candidate_config
,
864 di
->backup_config_file
, config_default
);
866 host_config_set(orig
);
867 XFREE(MTYPE_TMP
, orig
);
871 * Automatically commit the candidate configuration after
872 * reading the configuration file.
874 if (frr_get_cli_mode() == FRR_CLI_TRANSACTIONAL
) {
877 ret
= nb_candidate_commit(vty_shared_candidate_config
,
878 NB_CLIENT_CLI
, NULL
, true,
879 "Read configuration file", NULL
);
880 if (ret
!= NB_OK
&& ret
!= NB_ERR_NO_CHANGES
)
881 zlog_err("%s: failed to read configuration file.",
888 void frr_config_fork(void)
890 hook_call(frr_late_init
, master
);
892 if (!(di
->flags
& FRR_NO_CFG_PID_DRY
)) {
893 /* Don't start execution if we are in dry-run mode */
895 frr_config_read_in(NULL
);
899 thread_add_event(master
, frr_config_read_in
, NULL
, 0,
903 if (di
->daemon_mode
|| di
->terminal
)
907 di
->pid_file
= pidfile_default
;
908 pid_output(di
->pid_file
);
911 static void frr_vty_serv(void)
913 /* allow explicit override of vty_path in the future
914 * (not currently set anywhere) */
919 snprintf(defvtydir
, sizeof(defvtydir
), "%s", frr_vtydir
);
921 dir
= di
->vty_sock_path
? di
->vty_sock_path
: defvtydir
;
924 snprintf(vtypath_default
, sizeof(vtypath_default
),
925 "%s/%s-%d.vty", dir
, di
->name
, di
->instance
);
927 snprintf(vtypath_default
, sizeof(vtypath_default
),
928 "%s/%s.vty", dir
, di
->name
);
930 di
->vty_path
= vtypath_default
;
933 vty_serv_sock(di
->vty_addr
, di
->vty_port
, di
->vty_path
);
936 static void frr_check_detach(void)
938 if (nodetach_term
|| nodetach_daemon
)
941 if (daemon_ctl_sock
!= -1)
942 close(daemon_ctl_sock
);
943 daemon_ctl_sock
= -1;
946 static void frr_terminal_close(int isexit
)
950 nodetach_term
= false;
953 if (!di
->daemon_mode
|| isexit
) {
954 printf("\n%s exiting\n", di
->name
);
959 printf("\n%s daemonizing\n", di
->name
);
963 nullfd
= open("/dev/null", O_RDONLY
| O_NOCTTY
);
965 flog_err_sys(EC_LIB_SYSTEM_CALL
,
966 "%s: failed to open /dev/null: %s", __func__
,
967 safe_strerror(errno
));
976 static struct thread
*daemon_ctl_thread
= NULL
;
978 static int frr_daemon_ctl(struct thread
*t
)
983 nr
= recv(daemon_ctl_sock
, buf
, sizeof(buf
), 0);
984 if (nr
< 0 && (errno
== EINTR
|| errno
== EAGAIN
))
990 case 'S': /* SIGTSTP */
992 if (send(daemon_ctl_sock
, "s", 1, 0) < 0)
993 zlog_err("%s send(\"s\") error (SIGTSTP propagation)",
994 (di
&& di
->name
? di
->name
: ""));
996 case 'R': /* SIGTCNT [implicit] */
999 case 'I': /* SIGINT */
1000 di
->daemon_mode
= false;
1003 case 'Q': /* SIGQUIT */
1004 di
->daemon_mode
= true;
1010 thread_add_read(master
, frr_daemon_ctl
, NULL
, daemon_ctl_sock
,
1011 &daemon_ctl_thread
);
1015 void frr_detach(void)
1017 nodetach_daemon
= false;
1021 void frr_run(struct thread_master
*master
)
1023 char instanceinfo
[64] = "";
1028 snprintf(instanceinfo
, sizeof(instanceinfo
), "instance %u ",
1031 zlog_notice("%s %s starting: %svty@%d%s", di
->name
, FRR_VERSION
,
1032 instanceinfo
, di
->vty_port
, di
->startinfo
);
1035 nodetach_term
= true;
1037 vty_stdio(frr_terminal_close
);
1038 if (daemon_ctl_sock
!= -1) {
1039 set_nonblocking(daemon_ctl_sock
);
1040 thread_add_read(master
, frr_daemon_ctl
, NULL
,
1041 daemon_ctl_sock
, &daemon_ctl_thread
);
1043 } else if (di
->daemon_mode
) {
1044 int nullfd
= open("/dev/null", O_RDONLY
| O_NOCTTY
);
1046 flog_err_sys(EC_LIB_SYSTEM_CALL
,
1047 "%s: failed to open /dev/null: %s",
1048 __func__
, safe_strerror(errno
));
1059 /* end fixed stderr startup logging */
1060 zlog_startup_stderr
= false;
1062 struct thread thread
;
1063 while (thread_fetch(master
, &thread
))
1064 thread_call(&thread
);
1067 void frr_early_fini(void)
1069 hook_call(frr_early_fini
);
1078 hook_call(frr_fini
);
1080 /* memory_init -> nothing needed */
1089 frr_pthread_finish();
1090 zprivs_terminate(di
->privs
);
1091 /* signal_init -> nothing needed */
1092 thread_master_free(master
);
1095 /* frrmod_init -> nothing needed / hooks */
1098 if (!debug_memstats_at_exit
)
1101 have_leftovers
= log_memstats(stderr
, di
->name
);
1103 /* in case we decide at runtime that we want exit-memstats for
1104 * a daemon, but it has no stderr because it's daemonized
1105 * (only do this if we actually have something to print though)
1107 if (!have_leftovers
)
1110 snprintf(filename
, sizeof(filename
), "/tmp/frr-memstats-%s-%llu-%llu",
1111 di
->name
, (unsigned long long)getpid(),
1112 (unsigned long long)time(NULL
));
1114 fp
= fopen(filename
, "w");
1116 log_memstats(fp
, di
->name
);
1122 static const char interp
[]
1123 __attribute__((section(".interp"), used
)) = INTERP
;
1126 * executable entry point for libfrr.so
1128 * note that libc initialization is skipped for this so the set of functions
1129 * that can be called is rather limited
1131 extern void _libfrr_version(void)
1132 __attribute__((visibility("hidden"), noreturn
));
1133 void _libfrr_version(void)
1135 const char banner
[] =
1136 FRR_FULL_NAME
" " FRR_VERSION
".\n"
1137 FRR_COPYRIGHT GIT_INFO
"\n"
1138 "configured with:\n " FRR_CONFIG_ARGS
"\n";
1139 write(1, banner
, sizeof(banner
) - 1);