]>
git.proxmox.com Git - mirror_frr.git/blob - lib/libfrr.c
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 DEFINE_HOOK(frr_late_init
, (struct thread_master
* tm
), (tm
))
40 DEFINE_KOOH(frr_early_fini
, (), ())
41 DEFINE_KOOH(frr_fini
, (), ())
43 const char frr_sysconfdir
[] = SYSCONFDIR
;
44 const char frr_vtydir
[] = DAEMON_VTY_DIR
;
45 const char frr_moduledir
[] = MODULE_PATH
;
47 char frr_protoname
[256] = "NONE";
48 char frr_protonameinst
[256] = "NONE";
50 char config_default
[256];
51 char frr_zclientpath
[256];
52 static char pidfile_default
[256];
53 static char vtypath_default
[256];
55 static char comb_optstr
[256];
56 static struct option comb_lo
[64];
57 static struct option
*comb_next_lo
= &comb_lo
[0];
58 static char comb_helpstr
[4096];
63 const struct option
*longopts
;
66 static void opt_extend(const struct optspec
*os
)
68 const struct option
*lo
;
70 strcat(comb_optstr
, os
->optstr
);
71 strcat(comb_helpstr
, os
->helpstr
);
72 for (lo
= os
->longopts
; lo
->name
; lo
++)
73 memcpy(comb_next_lo
++, lo
, sizeof(*lo
));
77 #define OPTION_VTYSOCK 1000
78 #define OPTION_MODULEDIR 1002
80 static const struct option lo_always
[] = {
81 {"help", no_argument
, NULL
, 'h'},
82 {"version", no_argument
, NULL
, 'v'},
83 {"daemon", no_argument
, NULL
, 'd'},
84 {"module", no_argument
, NULL
, 'M'},
85 {"vty_socket", required_argument
, NULL
, OPTION_VTYSOCK
},
86 {"moduledir", required_argument
, NULL
, OPTION_MODULEDIR
},
88 static const struct optspec os_always
= {
90 " -h, --help Display this help and exit\n"
91 " -v, --version Print program version\n"
92 " -d, --daemon Runs in daemon mode\n"
93 " -M, --module Load specified module\n"
94 " --vty_socket Override vty socket path\n"
95 " --moduledir Override modules directory\n",
99 static const struct option lo_cfg_pid_dry
[] = {
100 {"pid_file", required_argument
, NULL
, 'i'},
101 {"config_file", required_argument
, NULL
, 'f'},
102 {"dryrun", no_argument
, NULL
, 'C'},
103 {"terminal", no_argument
, NULL
, 't'},
105 static const struct optspec os_cfg_pid_dry
= {
107 " -f, --config_file Set configuration file name\n"
108 " -i, --pid_file Set process identifier file name\n"
109 " -C, --dryrun Check configuration for validity and exit\n"
110 " -t, --terminal Open terminal session on stdio\n"
111 " -d -t Daemonize after terminal session ends\n",
115 static const struct option lo_zclient
[] = {
116 {"socket", required_argument
, NULL
, 'z'},
118 static const struct optspec os_zclient
= {
119 "z:", " -z, --socket Set path of zebra socket\n", lo_zclient
};
122 static const struct option lo_vty
[] = {
123 {"vty_addr", required_argument
, NULL
, 'A'},
124 {"vty_port", required_argument
, NULL
, 'P'},
126 static const struct optspec os_vty
= {
128 " -A, --vty_addr Set vty's bind address\n"
129 " -P, --vty_port Set vty's port number\n",
133 static const struct option lo_user
[] = {{"user", required_argument
, NULL
, 'u'},
134 {"group", required_argument
, NULL
, 'g'},
136 static const struct optspec os_user
= {"u:g:",
137 " -u, --user User to run as\n"
138 " -g, --group Group to run as\n",
142 bool frr_zclient_addr(struct sockaddr_storage
*sa
, socklen_t
*sa_len
,
145 memset(sa
, 0, sizeof(*sa
));
148 path
= ZEBRA_SERV_PATH
;
150 if (!strncmp(path
, ZAPI_TCP_PATHNAME
, strlen(ZAPI_TCP_PATHNAME
))) {
151 /* note: this functionality is disabled at bottom */
153 int port
= ZEBRA_PORT
;
155 struct sockaddr_in
*sin
= NULL
;
156 struct sockaddr_in6
*sin6
= NULL
;
158 path
+= strlen(ZAPI_TCP_PATHNAME
);
178 port
= strtoul(path
, &err
, 10);
189 sin
= (struct sockaddr_in
*)sa
;
190 sin
->sin_port
= htons(port
);
191 sin
->sin_addr
.s_addr
= htonl(INADDR_LOOPBACK
);
192 *sa_len
= sizeof(struct sockaddr_in
);
193 #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
194 sin
->sin_len
= *sa_len
;
198 sin6
= (struct sockaddr_in6
*)sa
;
199 sin6
->sin6_port
= htons(port
);
200 inet_pton(AF_INET6
, "::1", &sin6
->sin6_addr
);
201 *sa_len
= sizeof(struct sockaddr_in6
);
203 sin6
->sin6_len
= *sa_len
;
209 /* force-disable this path, because tcp-zebra is a
210 * SECURITY ISSUE. there are no checks at all against
211 * untrusted users on the local system connecting on TCP
212 * and injecting bogus routing data into the entire routing
215 * The functionality is only left here because it may be
216 * useful during development, in order to be able to get
217 * tcpdump or wireshark watching ZAPI as TCP. If you want
218 * to do that, flip the #if 1 above to #if 0. */
219 memset(sa
, 0, sizeof(*sa
));
223 /* "sun" is a #define on solaris */
224 struct sockaddr_un
*suna
= (struct sockaddr_un
*)sa
;
226 suna
->sun_family
= AF_UNIX
;
227 strlcpy(suna
->sun_path
, path
, sizeof(suna
->sun_path
));
228 #ifdef HAVE_STRUCT_SOCKADDR_UN_SUN_LEN
229 *sa_len
= suna
->sun_len
= SUN_LEN(suna
);
231 *sa_len
= sizeof(suna
->sun_family
) + strlen(suna
->sun_path
);
232 #endif /* HAVE_STRUCT_SOCKADDR_UN_SUN_LEN */
234 /* this is left here for future reference; Linux abstract
235 * socket namespace support can be enabled by replacing
236 * above #if 0 with #ifdef GNU_LINUX.
238 * THIS IS A SECURITY ISSUE, the abstract socket namespace
239 * does not have user/group permission control on sockets.
240 * we'd need to implement SCM_CREDENTIALS support first to
241 * check that only proper users can connect to abstract
242 * sockets. (same problem as tcp-zebra, except there is a
243 * fix with SCM_CREDENTIALS. tcp-zebra has no such fix.)
245 if (suna
->sun_path
[0] == '@')
246 suna
->sun_path
[0] = '\0';
252 static struct frr_daemon_info
*di
= NULL
;
254 void frr_preinit(struct frr_daemon_info
*daemon
, int argc
, char **argv
)
258 /* basename(), opencoded. */
259 char *p
= strrchr(argv
[0], '/');
260 di
->progname
= p
? p
+ 1 : argv
[0];
264 opt_extend(&os_always
);
265 if (!(di
->flags
& FRR_NO_CFG_PID_DRY
))
266 opt_extend(&os_cfg_pid_dry
);
267 if (!(di
->flags
& FRR_NO_PRIVSEP
))
268 opt_extend(&os_user
);
269 if (!(di
->flags
& FRR_NO_ZCLIENT
))
270 opt_extend(&os_zclient
);
271 if (!(di
->flags
& FRR_NO_TCPVTY
))
274 snprintf(config_default
, sizeof(config_default
), "%s/%s.conf",
275 frr_sysconfdir
, di
->name
);
276 snprintf(pidfile_default
, sizeof(pidfile_default
), "%s/%s.pid",
277 frr_vtydir
, di
->name
);
279 strlcpy(frr_protoname
, di
->logname
, sizeof(frr_protoname
));
280 strlcpy(frr_protonameinst
, di
->logname
, sizeof(frr_protonameinst
));
282 strlcpy(frr_zclientpath
, ZEBRA_SERV_PATH
, sizeof(frr_zclientpath
));
285 void frr_opt_add(const char *optstr
, const struct option
*longopts
,
288 const struct optspec main_opts
= {optstr
, helpstr
, longopts
};
289 opt_extend(&main_opts
);
292 void frr_help_exit(int status
)
294 FILE *target
= status
? stderr
: stdout
;
297 fprintf(stderr
, "Invalid options.\n\n");
300 di
->printhelp(target
);
302 fprintf(target
, "Usage: %s [OPTION...]\n\n%s%s%s\n\n%s",
303 di
->progname
, di
->proghelp
, di
->copyright
? "\n\n" : "",
304 di
->copyright
? di
->copyright
: "", comb_helpstr
);
305 fprintf(target
, "\nReport bugs to %s\n", FRR_BUG_ADDRESS
);
309 struct option_chain
{
310 struct option_chain
*next
;
314 static struct option_chain
*modules
= NULL
, **modnext
= &modules
;
315 static int errors
= 0;
317 static int frr_opt(int opt
)
319 static int vty_port_set
= 0;
320 static int vty_addr_set
= 0;
321 struct option_chain
*oc
;
329 print_version(di
->progname
);
336 oc
= XMALLOC(MTYPE_TMP
, sizeof(*oc
));
343 if (di
->flags
& FRR_NO_CFG_PID_DRY
)
345 di
->pid_file
= optarg
;
348 if (di
->flags
& FRR_NO_CFG_PID_DRY
)
350 di
->config_file
= optarg
;
353 if (di
->flags
& FRR_NO_CFG_PID_DRY
)
358 if (di
->flags
& FRR_NO_CFG_PID_DRY
)
363 if (di
->flags
& FRR_NO_ZCLIENT
)
365 strlcpy(frr_zclientpath
, optarg
, sizeof(frr_zclientpath
));
368 if (di
->flags
& FRR_NO_TCPVTY
)
372 "-A option specified more than once!\n");
377 di
->vty_addr
= optarg
;
380 if (di
->flags
& FRR_NO_TCPVTY
)
384 "-P option specified more than once!\n");
389 di
->vty_port
= strtoul(optarg
, &err
, 0);
390 if (*err
|| !*optarg
) {
392 "invalid port number \"%s\" for -P option\n",
399 if (di
->vty_sock_path
) {
401 "--vty_socket option specified more than once!\n");
405 di
->vty_sock_path
= optarg
;
407 case OPTION_MODULEDIR
:
408 if (di
->module_path
) {
410 "----moduledir option specified more than once!\n");
414 di
->module_path
= optarg
;
417 if (di
->flags
& FRR_NO_PRIVSEP
)
419 di
->privs
->user
= optarg
;
422 if (di
->flags
& FRR_NO_PRIVSEP
)
424 di
->privs
->group
= optarg
;
432 int frr_getopt(int argc
, char *const argv
[], int *longindex
)
437 comb_next_lo
->name
= NULL
;
440 opt
= getopt_long(argc
, argv
, comb_optstr
, comb_lo
, &lidx
);
445 if (opt
== -1 && errors
)
452 static void frr_mkdir(const char *path
, bool strip
)
457 struct zprivs_ids_t ids
;
460 char *slash
= strrchr(path
, '/');
465 if (plen
> sizeof(buf
) - 1)
467 memcpy(buf
, path
, plen
);
472 /* o+rx (..5) is needed for the frrvty group to work properly;
473 * without it, users in the frrvty group can't access the vty sockets.
476 ret
= mkdir(path
, 0755);
480 /* if EEXIST, return without touching the permissions,
481 * so user-set custom permissions are left in place
486 zlog_warn("failed to mkdir \"%s\": %s", path
, strerror(errno
));
490 zprivs_get_ids(&ids
);
491 if (chown(path
, ids
.uid_normal
, ids
.gid_normal
))
492 zlog_warn("failed to chown \"%s\": %s", path
, strerror(errno
));
495 static struct thread_master
*master
;
496 struct thread_master
*frr_init(void)
498 struct option_chain
*oc
;
499 struct frrmod_runtime
*module
;
502 dir
= di
->module_path
? di
->module_path
: frr_moduledir
;
507 snprintf(frr_protonameinst
, sizeof(frr_protonameinst
), "%s[%u]",
508 di
->logname
, di
->instance
);
510 zprivs_preinit(di
->privs
);
512 openzlog(di
->progname
, di
->logname
, di
->instance
,
513 LOG_CONS
| LOG_NDELAY
| LOG_PID
, LOG_DAEMON
);
514 #if defined(HAVE_CUMULUS)
515 zlog_set_level(ZLOG_DEST_SYSLOG
, zlog_default
->default_lvl
);
518 if (!frr_zclient_addr(&zclient_addr
, &zclient_addr_len
,
520 fprintf(stderr
, "Invalid zserv socket path: %s\n",
525 /* don't mkdir these as root... */
526 if (!(di
->flags
& FRR_NO_PRIVSEP
)) {
527 if (!di
->pid_file
|| !di
->vty_path
)
528 frr_mkdir(frr_vtydir
, false);
530 frr_mkdir(di
->pid_file
, true);
532 frr_mkdir(di
->vty_path
, true);
535 frrmod_init(di
->module
);
537 modules
= (oc
= modules
)->next
;
538 module
= frrmod_load(oc
->arg
, dir
, moderr
, sizeof(moderr
));
540 fprintf(stderr
, "%s\n", moderr
);
543 XFREE(MTYPE_TMP
, oc
);
546 zprivs_init(di
->privs
);
548 master
= thread_master_create(NULL
);
549 signal_init(master
, di
->n_signals
, di
->signals
);
551 if (di
->flags
& FRR_LIMITED_CLI
)
561 static int rcvd_signal
= 0;
563 static void rcv_signal(int signum
)
565 rcvd_signal
= signum
;
566 /* poll() is interrupted by the signal; handled below */
569 static void frr_daemon_wait(int fd
)
571 struct pollfd pfd
[1];
575 sigset_t sigs
, prevsigs
;
578 sigaddset(&sigs
, SIGTSTP
);
579 sigaddset(&sigs
, SIGQUIT
);
580 sigaddset(&sigs
, SIGINT
);
581 sigprocmask(SIG_BLOCK
, &sigs
, &prevsigs
);
583 struct sigaction sa
= {
584 .sa_handler
= rcv_signal
, .sa_flags
= SA_RESETHAND
,
586 sigemptyset(&sa
.sa_mask
);
587 sigaction(SIGTSTP
, &sa
, NULL
);
588 sigaction(SIGQUIT
, &sa
, NULL
);
589 sigaction(SIGINT
, &sa
, NULL
);
596 pfd
[0].events
= POLLIN
;
600 #if defined(HAVE_PPOLL)
601 ret
= ppoll(pfd
, 1, NULL
, &prevsigs
);
602 #elif defined(HAVE_POLLTS)
603 ret
= pollts(pfd
, 1, NULL
, &prevsigs
);
605 /* racy -- only used on FreeBSD 9 */
607 sigprocmask(SIG_SETMASK
, &prevsigs
, &tmpsigs
);
608 ret
= poll(pfd
, 1, -1);
609 sigprocmask(SIG_SETMASK
, &tmpsigs
, NULL
);
611 if (ret
< 0 && errno
!= EINTR
&& errno
!= EAGAIN
) {
615 switch (rcvd_signal
) {
619 nrecv
= recv(fd
, buf
, sizeof(buf
), 0);
621 && (errno
== EINTR
|| errno
== EAGAIN
));
624 sigaction(SIGTSTP
, &sa
, NULL
);
636 exitpid
= waitpid(-1, &exitstat
, WNOHANG
);
638 /* child successfully went to main loop & closed socket */
641 /* child failed one way or another ... */
642 if (WIFEXITED(exitstat
))
643 fprintf(stderr
, "%s failed to start, exited %d\n", di
->name
,
644 WEXITSTATUS(exitstat
));
645 else if (WIFSIGNALED(exitstat
))
646 fprintf(stderr
, "%s crashed in startup, signal %d\n", di
->name
,
649 fprintf(stderr
, "%s failed to start, unknown problem\n",
654 static int daemon_ctl_sock
= -1;
656 static void frr_daemonize(void)
661 if (socketpair(AF_UNIX
, SOCK_STREAM
, 0, fds
)) {
662 perror("socketpair() for daemon control");
681 daemon_ctl_sock
= fds
[1];
686 frr_daemon_wait(fds
[0]);
689 void frr_config_fork(void)
691 hook_call(frr_late_init
, master
);
694 snprintf(config_default
, sizeof(config_default
),
695 "%s/%s-%d.conf", frr_sysconfdir
, di
->name
,
697 snprintf(pidfile_default
, sizeof(pidfile_default
),
698 "%s/%s-%d.pid", frr_vtydir
, di
->name
, di
->instance
);
701 vty_read_config(di
->config_file
, config_default
);
703 /* Don't start execution if we are in dry-run mode */
707 if (di
->daemon_mode
|| di
->terminal
)
711 di
->pid_file
= pidfile_default
;
712 pid_output(di
->pid_file
);
715 void frr_vty_serv(void)
717 /* allow explicit override of vty_path in the future
718 * (not currently set anywhere) */
721 dir
= di
->vty_sock_path
? di
->vty_sock_path
: frr_vtydir
;
724 snprintf(vtypath_default
, sizeof(vtypath_default
),
725 "%s/%s-%d.vty", dir
, di
->name
, di
->instance
);
727 snprintf(vtypath_default
, sizeof(vtypath_default
),
728 "%s/%s.vty", dir
, di
->name
);
730 di
->vty_path
= vtypath_default
;
733 vty_serv_sock(di
->vty_addr
, di
->vty_port
, di
->vty_path
);
736 static void frr_terminal_close(int isexit
)
738 if (daemon_ctl_sock
!= -1) {
739 close(daemon_ctl_sock
);
740 daemon_ctl_sock
= -1;
743 if (!di
->daemon_mode
|| isexit
) {
744 printf("\n%s exiting\n", di
->name
);
749 printf("\n%s daemonizing\n", di
->name
);
753 int nullfd
= open("/dev/null", O_RDONLY
| O_NOCTTY
);
760 static struct thread
*daemon_ctl_thread
= NULL
;
762 static int frr_daemon_ctl(struct thread
*t
)
767 nr
= recv(daemon_ctl_sock
, buf
, sizeof(buf
), 0);
768 if (nr
< 0 && (errno
== EINTR
|| errno
== EAGAIN
))
774 case 'S': /* SIGTSTP */
776 send(daemon_ctl_sock
, "s", 1, 0);
778 case 'R': /* SIGTCNT [implicit] */
781 case 'I': /* SIGINT */
782 di
->daemon_mode
= false;
785 case 'Q': /* SIGQUIT */
786 di
->daemon_mode
= true;
792 thread_add_read(master
, frr_daemon_ctl
, NULL
, daemon_ctl_sock
,
797 void frr_run(struct thread_master
*master
)
799 char instanceinfo
[64] = "";
804 snprintf(instanceinfo
, sizeof(instanceinfo
), "instance %u ",
807 zlog_notice("%s %s starting: %svty@%d%s", di
->name
, FRR_VERSION
,
808 instanceinfo
, di
->vty_port
, di
->startinfo
);
811 vty_stdio(frr_terminal_close
);
812 if (daemon_ctl_sock
!= -1) {
813 set_nonblocking(daemon_ctl_sock
);
814 thread_add_read(master
, frr_daemon_ctl
, NULL
,
815 daemon_ctl_sock
, &daemon_ctl_thread
);
817 } else if (di
->daemon_mode
) {
818 int nullfd
= open("/dev/null", O_RDONLY
| O_NOCTTY
);
824 if (daemon_ctl_sock
!= -1)
825 close(daemon_ctl_sock
);
826 daemon_ctl_sock
= -1;
829 /* end fixed stderr startup logging */
830 zlog_startup_stderr
= false;
832 struct thread thread
;
833 while (thread_fetch(master
, &thread
))
834 thread_call(&thread
);
837 void frr_early_fini(void)
839 hook_call(frr_early_fini
);
846 /* memory_init -> nothing needed */
849 zprivs_terminate(di
->privs
);
850 /* signal_init -> nothing needed */
851 thread_master_free(master
);
853 /* frrmod_init -> nothing needed / hooks */