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"
38 #include "lib_errors.h"
40 #include "northbound_cli.h"
41 #include "northbound_db.h"
43 DEFINE_HOOK(frr_late_init
, (struct thread_master
* tm
), (tm
))
44 DEFINE_KOOH(frr_early_fini
, (), ())
45 DEFINE_KOOH(frr_fini
, (), ())
47 const char frr_sysconfdir
[] = SYSCONFDIR
;
48 const char frr_vtydir
[] = DAEMON_VTY_DIR
;
50 const char frr_dbdir
[] = DAEMON_DB_DIR
;
52 const char frr_moduledir
[] = MODULE_PATH
;
54 char frr_protoname
[256] = "NONE";
55 char frr_protonameinst
[256] = "NONE";
57 char config_default
[512];
58 char frr_zclientpath
[256];
59 static char pidfile_default
[512];
61 static char dbfile_default
[512];
63 static char vtypath_default
[256];
65 bool debug_memstats_at_exit
= false;
66 static bool nodetach_term
, nodetach_daemon
;
68 static char comb_optstr
[256];
69 static struct option comb_lo
[64];
70 static struct option
*comb_next_lo
= &comb_lo
[0];
71 static char comb_helpstr
[4096];
76 const struct option
*longopts
;
79 static void opt_extend(const struct optspec
*os
)
81 const struct option
*lo
;
83 strcat(comb_optstr
, os
->optstr
);
84 strcat(comb_helpstr
, os
->helpstr
);
85 for (lo
= os
->longopts
; lo
->name
; lo
++)
86 memcpy(comb_next_lo
++, lo
, sizeof(*lo
));
90 #define OPTION_VTYSOCK 1000
91 #define OPTION_MODULEDIR 1002
92 #define OPTION_LOG 1003
93 #define OPTION_LOGLEVEL 1004
94 #define OPTION_TCLI 1005
95 #define OPTION_DB_FILE 1006
97 static const struct option lo_always
[] = {
98 {"help", no_argument
, NULL
, 'h'},
99 {"version", no_argument
, NULL
, 'v'},
100 {"daemon", no_argument
, NULL
, 'd'},
101 {"module", no_argument
, NULL
, 'M'},
102 {"vty_socket", required_argument
, NULL
, OPTION_VTYSOCK
},
103 {"moduledir", required_argument
, NULL
, OPTION_MODULEDIR
},
104 {"log", required_argument
, NULL
, OPTION_LOG
},
105 {"log-level", required_argument
, NULL
, OPTION_LOGLEVEL
},
106 {"tcli", no_argument
, NULL
, OPTION_TCLI
},
108 static const struct optspec os_always
= {
110 " -h, --help Display this help and exit\n"
111 " -v, --version Print program version\n"
112 " -d, --daemon Runs in daemon mode\n"
113 " -M, --module Load specified module\n"
114 " --vty_socket Override vty socket path\n"
115 " --moduledir Override modules directory\n"
116 " --log Set Logging to stdout, syslog, or file:<name>\n"
117 " --log-level Set Logging Level to use, debug, info, warn, etc\n"
118 " --tcli Use transaction-based CLI\n",
122 static const struct option lo_cfg_pid_dry
[] = {
123 {"pid_file", required_argument
, NULL
, 'i'},
124 {"config_file", required_argument
, NULL
, 'f'},
126 {"db_file", required_argument
, NULL
, OPTION_DB_FILE
},
128 {"pathspace", required_argument
, NULL
, 'N'},
129 {"dryrun", no_argument
, NULL
, 'C'},
130 {"terminal", no_argument
, NULL
, 't'},
132 static const struct optspec os_cfg_pid_dry
= {
134 " -f, --config_file Set configuration file name\n"
135 " -i, --pid_file Set process identifier file name\n"
137 " --db_file Set database file name\n"
139 " -N, --pathspace Insert prefix into config & socket paths\n"
140 " -C, --dryrun Check configuration for validity and exit\n"
141 " -t, --terminal Open terminal session on stdio\n"
142 " -d -t Daemonize after terminal session ends\n",
146 static const struct option lo_zclient
[] = {
147 {"socket", required_argument
, NULL
, 'z'},
149 static const struct optspec os_zclient
= {
150 "z:", " -z, --socket Set path of zebra socket\n", lo_zclient
};
153 static const struct option lo_vty
[] = {
154 {"vty_addr", required_argument
, NULL
, 'A'},
155 {"vty_port", required_argument
, NULL
, 'P'},
157 static const struct optspec os_vty
= {
159 " -A, --vty_addr Set vty's bind address\n"
160 " -P, --vty_port Set vty's port number\n",
164 static const struct option lo_user
[] = {{"user", required_argument
, NULL
, 'u'},
165 {"group", required_argument
, NULL
, 'g'},
167 static const struct optspec os_user
= {"u:g:",
168 " -u, --user User to run as\n"
169 " -g, --group Group to run as\n",
173 bool frr_zclient_addr(struct sockaddr_storage
*sa
, socklen_t
*sa_len
,
176 memset(sa
, 0, sizeof(*sa
));
179 path
= ZEBRA_SERV_PATH
;
181 if (!strncmp(path
, ZAPI_TCP_PATHNAME
, strlen(ZAPI_TCP_PATHNAME
))) {
182 /* note: this functionality is disabled at bottom */
184 int port
= ZEBRA_PORT
;
186 struct sockaddr_in
*sin
= NULL
;
187 struct sockaddr_in6
*sin6
= NULL
;
189 path
+= strlen(ZAPI_TCP_PATHNAME
);
209 port
= strtoul(path
, &err
, 10);
220 sin
= (struct sockaddr_in
*)sa
;
221 sin
->sin_port
= htons(port
);
222 sin
->sin_addr
.s_addr
= htonl(INADDR_LOOPBACK
);
223 *sa_len
= sizeof(struct sockaddr_in
);
224 #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
225 sin
->sin_len
= *sa_len
;
229 sin6
= (struct sockaddr_in6
*)sa
;
230 sin6
->sin6_port
= htons(port
);
231 inet_pton(AF_INET6
, "::1", &sin6
->sin6_addr
);
232 *sa_len
= sizeof(struct sockaddr_in6
);
234 sin6
->sin6_len
= *sa_len
;
240 /* force-disable this path, because tcp-zebra is a
241 * SECURITY ISSUE. there are no checks at all against
242 * untrusted users on the local system connecting on TCP
243 * and injecting bogus routing data into the entire routing
246 * The functionality is only left here because it may be
247 * useful during development, in order to be able to get
248 * tcpdump or wireshark watching ZAPI as TCP. If you want
249 * to do that, flip the #if 1 above to #if 0. */
250 memset(sa
, 0, sizeof(*sa
));
254 /* "sun" is a #define on solaris */
255 struct sockaddr_un
*suna
= (struct sockaddr_un
*)sa
;
257 suna
->sun_family
= AF_UNIX
;
258 strlcpy(suna
->sun_path
, path
, sizeof(suna
->sun_path
));
259 #ifdef HAVE_STRUCT_SOCKADDR_UN_SUN_LEN
260 *sa_len
= suna
->sun_len
= SUN_LEN(suna
);
262 *sa_len
= sizeof(suna
->sun_family
) + strlen(suna
->sun_path
);
263 #endif /* HAVE_STRUCT_SOCKADDR_UN_SUN_LEN */
265 /* this is left here for future reference; Linux abstract
266 * socket namespace support can be enabled by replacing
267 * above #if 0 with #ifdef GNU_LINUX.
269 * THIS IS A SECURITY ISSUE, the abstract socket namespace
270 * does not have user/group permission control on sockets.
271 * we'd need to implement SCM_CREDENTIALS support first to
272 * check that only proper users can connect to abstract
273 * sockets. (same problem as tcp-zebra, except there is a
274 * fix with SCM_CREDENTIALS. tcp-zebra has no such fix.)
276 if (suna
->sun_path
[0] == '@')
277 suna
->sun_path
[0] = '\0';
283 static struct frr_daemon_info
*di
= NULL
;
285 void frr_preinit(struct frr_daemon_info
*daemon
, int argc
, char **argv
)
289 /* basename(), opencoded. */
290 char *p
= strrchr(argv
[0], '/');
291 di
->progname
= p
? p
+ 1 : argv
[0];
295 opt_extend(&os_always
);
296 if (!(di
->flags
& FRR_NO_CFG_PID_DRY
))
297 opt_extend(&os_cfg_pid_dry
);
298 if (!(di
->flags
& FRR_NO_PRIVSEP
))
299 opt_extend(&os_user
);
300 if (!(di
->flags
& FRR_NO_ZCLIENT
))
301 opt_extend(&os_zclient
);
302 if (!(di
->flags
& FRR_NO_TCPVTY
))
304 if (di
->flags
& FRR_DETACH_LATER
)
305 nodetach_daemon
= true;
307 snprintf(config_default
, sizeof(config_default
), "%s/%s.conf",
308 frr_sysconfdir
, di
->name
);
309 snprintf(pidfile_default
, sizeof(pidfile_default
), "%s/%s.pid",
310 frr_vtydir
, di
->name
);
312 snprintf(dbfile_default
, sizeof(dbfile_default
), "%s/%s.db",
313 frr_dbdir
, di
->name
);
316 strlcpy(frr_protoname
, di
->logname
, sizeof(frr_protoname
));
317 strlcpy(frr_protonameinst
, di
->logname
, sizeof(frr_protonameinst
));
319 strlcpy(frr_zclientpath
, ZEBRA_SERV_PATH
, sizeof(frr_zclientpath
));
321 di
->cli_mode
= FRR_CLI_CLASSIC
;
324 void frr_opt_add(const char *optstr
, const struct option
*longopts
,
327 const struct optspec main_opts
= {optstr
, helpstr
, longopts
};
328 opt_extend(&main_opts
);
331 void frr_help_exit(int status
)
333 FILE *target
= status
? stderr
: stdout
;
336 fprintf(stderr
, "Invalid options.\n\n");
339 di
->printhelp(target
);
341 fprintf(target
, "Usage: %s [OPTION...]\n\n%s%s%s\n\n%s",
342 di
->progname
, di
->proghelp
, di
->copyright
? "\n\n" : "",
343 di
->copyright
? di
->copyright
: "", comb_helpstr
);
344 fprintf(target
, "\nReport bugs to %s\n", FRR_BUG_ADDRESS
);
348 struct option_chain
{
349 struct option_chain
*next
;
353 static struct option_chain
*modules
= NULL
, **modnext
= &modules
;
354 static int errors
= 0;
356 static int frr_opt(int opt
)
358 static int vty_port_set
= 0;
359 static int vty_addr_set
= 0;
360 struct option_chain
*oc
;
368 print_version(di
->progname
);
375 oc
= XMALLOC(MTYPE_TMP
, sizeof(*oc
));
382 if (di
->flags
& FRR_NO_CFG_PID_DRY
)
384 di
->pid_file
= optarg
;
387 if (di
->flags
& FRR_NO_CFG_PID_DRY
)
389 di
->config_file
= optarg
;
392 if (di
->flags
& FRR_NO_CFG_PID_DRY
)
396 "-N/--pathspace option specified more than once!\n");
400 if (strchr(optarg
, '/') || strchr(optarg
, '.')) {
402 "slashes or dots are not permitted in the --pathspace option.\n");
406 di
->pathspace
= optarg
;
410 if (di
->flags
& FRR_NO_CFG_PID_DRY
)
412 di
->db_file
= optarg
;
416 if (di
->flags
& FRR_NO_CFG_PID_DRY
)
421 if (di
->flags
& FRR_NO_CFG_PID_DRY
)
426 if (di
->flags
& FRR_NO_ZCLIENT
)
428 strlcpy(frr_zclientpath
, optarg
, sizeof(frr_zclientpath
));
431 if (di
->flags
& FRR_NO_TCPVTY
)
435 "-A option specified more than once!\n");
440 di
->vty_addr
= optarg
;
443 if (di
->flags
& FRR_NO_TCPVTY
)
447 "-P option specified more than once!\n");
452 di
->vty_port
= strtoul(optarg
, &err
, 0);
453 if (*err
|| !*optarg
) {
455 "invalid port number \"%s\" for -P option\n",
462 if (di
->vty_sock_path
) {
464 "--vty_socket option specified more than once!\n");
468 di
->vty_sock_path
= optarg
;
470 case OPTION_MODULEDIR
:
471 if (di
->module_path
) {
473 "----moduledir option specified more than once!\n");
477 di
->module_path
= optarg
;
480 di
->cli_mode
= FRR_CLI_TRANSACTIONAL
;
483 if (di
->flags
& FRR_NO_PRIVSEP
)
485 di
->privs
->user
= optarg
;
488 if (di
->flags
& FRR_NO_PRIVSEP
)
490 di
->privs
->group
= optarg
;
493 di
->early_logging
= optarg
;
495 case OPTION_LOGLEVEL
:
496 di
->early_loglevel
= optarg
;
504 int frr_getopt(int argc
, char *const argv
[], int *longindex
)
509 comb_next_lo
->name
= NULL
;
512 opt
= getopt_long(argc
, argv
, comb_optstr
, comb_lo
, &lidx
);
517 if (opt
== -1 && errors
)
524 static void frr_mkdir(const char *path
, bool strip
)
529 struct zprivs_ids_t ids
;
532 char *slash
= strrchr(path
, '/');
537 if (plen
> sizeof(buf
) - 1)
539 memcpy(buf
, path
, plen
);
544 /* o+rx (..5) is needed for the frrvty group to work properly;
545 * without it, users in the frrvty group can't access the vty sockets.
548 ret
= mkdir(path
, 0755);
552 /* if EEXIST, return without touching the permissions,
553 * so user-set custom permissions are left in place
558 flog_err(EC_LIB_SYSTEM_CALL
, "failed to mkdir \"%s\": %s", path
,
563 zprivs_get_ids(&ids
);
564 if (chown(path
, ids
.uid_normal
, ids
.gid_normal
))
565 flog_err(EC_LIB_SYSTEM_CALL
, "failed to chown \"%s\": %s", path
,
569 static struct thread_master
*master
;
570 struct thread_master
*frr_init(void)
572 struct option_chain
*oc
;
573 struct frrmod_runtime
*module
;
575 char p_instance
[16] = "", p_pathspace
[256] = "";
577 dir
= di
->module_path
? di
->module_path
: frr_moduledir
;
582 snprintf(frr_protonameinst
, sizeof(frr_protonameinst
), "%s[%u]",
583 di
->logname
, di
->instance
);
584 snprintf(p_instance
, sizeof(p_instance
), "-%d", di
->instance
);
587 snprintf(p_pathspace
, sizeof(p_pathspace
), "%s/",
590 snprintf(config_default
, sizeof(config_default
), "%s%s%s%s.conf",
591 frr_sysconfdir
, p_pathspace
, di
->name
, p_instance
);
592 snprintf(pidfile_default
, sizeof(pidfile_default
), "%s/%s%s%s.pid",
593 frr_vtydir
, p_pathspace
, di
->name
, p_instance
);
595 snprintf(dbfile_default
, sizeof(dbfile_default
), "%s/%s%s%s.db",
596 frr_dbdir
, p_pathspace
, di
->name
, p_instance
);
599 zprivs_preinit(di
->privs
);
601 openzlog(di
->progname
, di
->logname
, di
->instance
,
602 LOG_CONS
| LOG_NDELAY
| LOG_PID
, LOG_DAEMON
);
604 command_setup_early_logging(di
->early_logging
, di
->early_loglevel
);
606 if (!frr_zclient_addr(&zclient_addr
, &zclient_addr_len
,
608 fprintf(stderr
, "Invalid zserv socket path: %s\n",
613 /* don't mkdir these as root... */
614 if (!(di
->flags
& FRR_NO_PRIVSEP
)) {
615 if (!di
->pid_file
|| !di
->vty_path
)
616 frr_mkdir(frr_vtydir
, false);
618 frr_mkdir(di
->pid_file
, true);
620 frr_mkdir(di
->vty_path
, true);
623 frrmod_init(di
->module
);
625 modules
= (oc
= modules
)->next
;
626 module
= frrmod_load(oc
->arg
, dir
, moderr
, sizeof(moderr
));
628 fprintf(stderr
, "%s\n", moderr
);
631 XFREE(MTYPE_TMP
, oc
);
634 zprivs_init(di
->privs
);
636 master
= thread_master_create(NULL
);
637 signal_init(master
, di
->n_signals
, di
->signals
);
641 di
->db_file
= dbfile_default
;
642 db_init(di
->db_file
);
645 if (di
->flags
& FRR_LIMITED_CLI
)
657 nb_init(master
, di
->yang_modules
, di
->n_yang_modules
);
658 if (nb_db_init() != NB_OK
)
659 flog_warn(EC_LIB_NB_DATABASE
,
660 "%s: failed to initialize northbound database",
666 const char *frr_get_progname(void)
668 return di
? di
->progname
: NULL
;
671 enum frr_cli_mode
frr_get_cli_mode(void)
673 return di
? di
->cli_mode
: FRR_CLI_CLASSIC
;
676 static int rcvd_signal
= 0;
678 static void rcv_signal(int signum
)
680 rcvd_signal
= signum
;
681 /* poll() is interrupted by the signal; handled below */
684 static void frr_daemon_wait(int fd
)
686 struct pollfd pfd
[1];
690 sigset_t sigs
, prevsigs
;
693 sigaddset(&sigs
, SIGTSTP
);
694 sigaddset(&sigs
, SIGQUIT
);
695 sigaddset(&sigs
, SIGINT
);
696 sigprocmask(SIG_BLOCK
, &sigs
, &prevsigs
);
698 struct sigaction sa
= {
699 .sa_handler
= rcv_signal
, .sa_flags
= SA_RESETHAND
,
701 sigemptyset(&sa
.sa_mask
);
702 sigaction(SIGTSTP
, &sa
, NULL
);
703 sigaction(SIGQUIT
, &sa
, NULL
);
704 sigaction(SIGINT
, &sa
, NULL
);
711 pfd
[0].events
= POLLIN
;
715 #if defined(HAVE_PPOLL)
716 ret
= ppoll(pfd
, 1, NULL
, &prevsigs
);
717 #elif defined(HAVE_POLLTS)
718 ret
= pollts(pfd
, 1, NULL
, &prevsigs
);
720 /* racy -- only used on FreeBSD 9 */
722 sigprocmask(SIG_SETMASK
, &prevsigs
, &tmpsigs
);
723 ret
= poll(pfd
, 1, -1);
724 sigprocmask(SIG_SETMASK
, &tmpsigs
, NULL
);
726 if (ret
< 0 && errno
!= EINTR
&& errno
!= EAGAIN
) {
730 switch (rcvd_signal
) {
734 nrecv
= recv(fd
, buf
, sizeof(buf
), 0);
736 && (errno
== EINTR
|| errno
== EAGAIN
));
739 sigaction(SIGTSTP
, &sa
, NULL
);
751 exitpid
= waitpid(-1, &exitstat
, WNOHANG
);
753 /* child successfully went to main loop & closed socket */
756 /* child failed one way or another ... */
757 if (WIFEXITED(exitstat
) && WEXITSTATUS(exitstat
) == 0)
758 /* can happen in --terminal case if exit is fast enough */
760 else if (WIFEXITED(exitstat
))
761 fprintf(stderr
, "%s failed to start, exited %d\n", di
->name
,
762 WEXITSTATUS(exitstat
));
763 else if (WIFSIGNALED(exitstat
))
764 fprintf(stderr
, "%s crashed in startup, signal %d\n", di
->name
,
767 fprintf(stderr
, "%s failed to start, unknown problem\n",
772 static int daemon_ctl_sock
= -1;
774 static void frr_daemonize(void)
779 if (socketpair(AF_UNIX
, SOCK_STREAM
, 0, fds
)) {
780 perror("socketpair() for daemon control");
799 daemon_ctl_sock
= fds
[1];
804 frr_daemon_wait(fds
[0]);
808 * Why is this a thread?
810 * The read in of config for integrated config happens *after*
811 * thread execution starts( because it is passed in via a vtysh -b -n )
812 * While if you are not using integrated config we want the ability
813 * to read the config in after thread execution starts, so that
814 * we can match this behavior.
816 static int frr_config_read_in(struct thread
*t
)
818 if (!vty_read_config(NULL
, di
->config_file
, config_default
) &&
819 di
->backup_config_file
) {
820 char *orig
= XSTRDUP(MTYPE_TMP
, host_config_get());
822 zlog_info("Attempting to read backup config file: %s specified",
823 di
->backup_config_file
);
824 vty_read_config(NULL
, di
->backup_config_file
, config_default
);
826 host_config_set(orig
);
827 XFREE(MTYPE_TMP
, orig
);
831 * Update the shared candidate after reading the startup configuration.
833 pthread_rwlock_rdlock(&running_config
->lock
);
835 nb_config_replace(vty_shared_candidate_config
, running_config
,
838 pthread_rwlock_unlock(&running_config
->lock
);
843 void frr_config_fork(void)
845 hook_call(frr_late_init
, master
);
847 if (!(di
->flags
& FRR_NO_CFG_PID_DRY
)) {
848 /* Don't start execution if we are in dry-run mode */
850 frr_config_read_in(NULL
);
854 thread_add_event(master
, frr_config_read_in
, NULL
, 0,
858 if (di
->daemon_mode
|| di
->terminal
)
862 di
->pid_file
= pidfile_default
;
863 pid_output(di
->pid_file
);
866 static void frr_vty_serv(void)
868 /* allow explicit override of vty_path in the future
869 * (not currently set anywhere) */
874 snprintf(defvtydir
, sizeof(defvtydir
), "%s%s%s", frr_vtydir
,
875 di
->pathspace
? "/" : "",
876 di
->pathspace
? di
->pathspace
: "");
878 dir
= di
->vty_sock_path
? di
->vty_sock_path
: defvtydir
;
881 snprintf(vtypath_default
, sizeof(vtypath_default
),
882 "%s/%s-%d.vty", dir
, di
->name
, di
->instance
);
884 snprintf(vtypath_default
, sizeof(vtypath_default
),
885 "%s/%s.vty", dir
, di
->name
);
887 di
->vty_path
= vtypath_default
;
890 vty_serv_sock(di
->vty_addr
, di
->vty_port
, di
->vty_path
);
893 static void frr_check_detach(void)
895 if (nodetach_term
|| nodetach_daemon
)
898 if (daemon_ctl_sock
!= -1)
899 close(daemon_ctl_sock
);
900 daemon_ctl_sock
= -1;
903 static void frr_terminal_close(int isexit
)
907 nodetach_term
= false;
910 if (!di
->daemon_mode
|| isexit
) {
911 printf("\n%s exiting\n", di
->name
);
916 printf("\n%s daemonizing\n", di
->name
);
920 nullfd
= open("/dev/null", O_RDONLY
| O_NOCTTY
);
922 flog_err_sys(EC_LIB_SYSTEM_CALL
,
923 "%s: failed to open /dev/null: %s", __func__
,
924 safe_strerror(errno
));
933 static struct thread
*daemon_ctl_thread
= NULL
;
935 static int frr_daemon_ctl(struct thread
*t
)
940 nr
= recv(daemon_ctl_sock
, buf
, sizeof(buf
), 0);
941 if (nr
< 0 && (errno
== EINTR
|| errno
== EAGAIN
))
947 case 'S': /* SIGTSTP */
949 if (send(daemon_ctl_sock
, "s", 1, 0) < 0)
950 zlog_err("%s send(\"s\") error (SIGTSTP propagation)",
951 (di
&& di
->name
? di
->name
: ""));
953 case 'R': /* SIGTCNT [implicit] */
956 case 'I': /* SIGINT */
957 di
->daemon_mode
= false;
960 case 'Q': /* SIGQUIT */
961 di
->daemon_mode
= true;
967 thread_add_read(master
, frr_daemon_ctl
, NULL
, daemon_ctl_sock
,
972 void frr_detach(void)
974 nodetach_daemon
= false;
978 void frr_run(struct thread_master
*master
)
980 char instanceinfo
[64] = "";
985 snprintf(instanceinfo
, sizeof(instanceinfo
), "instance %u ",
988 zlog_notice("%s %s starting: %svty@%d%s", di
->name
, FRR_VERSION
,
989 instanceinfo
, di
->vty_port
, di
->startinfo
);
992 nodetach_term
= true;
994 vty_stdio(frr_terminal_close
);
995 if (daemon_ctl_sock
!= -1) {
996 set_nonblocking(daemon_ctl_sock
);
997 thread_add_read(master
, frr_daemon_ctl
, NULL
,
998 daemon_ctl_sock
, &daemon_ctl_thread
);
1000 } else if (di
->daemon_mode
) {
1001 int nullfd
= open("/dev/null", O_RDONLY
| O_NOCTTY
);
1003 flog_err_sys(EC_LIB_SYSTEM_CALL
,
1004 "%s: failed to open /dev/null: %s",
1005 __func__
, safe_strerror(errno
));
1016 /* end fixed stderr startup logging */
1017 zlog_startup_stderr
= false;
1019 struct thread thread
;
1020 while (thread_fetch(master
, &thread
))
1021 thread_call(&thread
);
1024 void frr_early_fini(void)
1026 hook_call(frr_early_fini
);
1035 hook_call(frr_fini
);
1037 /* memory_init -> nothing needed */
1046 zprivs_terminate(di
->privs
);
1047 /* signal_init -> nothing needed */
1048 thread_master_free(master
);
1051 /* frrmod_init -> nothing needed / hooks */
1053 if (!debug_memstats_at_exit
)
1056 have_leftovers
= log_memstats(stderr
, di
->name
);
1058 /* in case we decide at runtime that we want exit-memstats for
1059 * a daemon, but it has no stderr because it's daemonized
1060 * (only do this if we actually have something to print though)
1062 if (!have_leftovers
)
1065 snprintf(filename
, sizeof(filename
), "/tmp/frr-memstats-%s-%llu-%llu",
1066 di
->name
, (unsigned long long)getpid(),
1067 (unsigned long long)time(NULL
));
1069 fp
= fopen(filename
, "w");
1071 log_memstats(fp
, di
->name
);
1077 static const char interp
[]
1078 __attribute__((section(".interp"), used
)) = INTERP
;
1081 * executable entry point for libfrr.so
1083 * note that libc initialization is skipped for this so the set of functions
1084 * that can be called is rather limited
1086 extern void _libfrr_version(void)
1087 __attribute__((visibility("hidden"), noreturn
));
1088 void _libfrr_version(void)
1090 const char banner
[] =
1091 FRR_FULL_NAME
" " FRR_VERSION
".\n"
1092 FRR_COPYRIGHT GIT_INFO
"\n"
1093 "configured with:\n " FRR_CONFIG_ARGS
"\n";
1094 write(1, banner
, sizeof(banner
) - 1);