summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorDavid Lamparter <equinox@opensourcerouting.org>2018-09-27 04:18:48 +0200
committerDavid Lamparter <equinox@opensourcerouting.org>2018-10-02 12:27:47 +0200
commit0a7c7856e3eb6db017d3d0c4d19d55c6cdb212e9 (patch)
tree4a6cb7cee5c17e1e51ba8fe6b81e5c1b40e628ba /lib
parentMerge pull request #3102 from ton31337/feature/match_blackhole_nexthops (diff)
downloadfrr-0a7c7856e3eb6db017d3d0c4d19d55c6cdb212e9.tar.xz
frr-0a7c7856e3eb6db017d3d0c4d19d55c6cdb212e9.zip
watchfrr, lib: cleanup & delay detaching
This cleans up watchfrr to be more "normal" like the other daemons in terms of what it does in main(), i.e. using the full frr_*() call set. Also, this changes the startup behaviour on watchfrr to stay attached on the daemon's parent process until startup is really complete. This should allow removing the "watchfrr.started" hack at some point. Signed-off-by: David Lamparter <equinox@diac24.net>
Diffstat (limited to 'lib')
-rw-r--r--lib/libfrr.c48
-rw-r--r--lib/libfrr.h16
2 files changed, 46 insertions, 18 deletions
diff --git a/lib/libfrr.c b/lib/libfrr.c
index 2bce4766d..252132201 100644
--- a/lib/libfrr.c
+++ b/lib/libfrr.c
@@ -54,6 +54,7 @@ static char pidfile_default[512];
static char vtypath_default[256];
bool debug_memstats_at_exit = 0;
+static bool nodetach_term, nodetach_daemon;
static char comb_optstr[256];
static struct option comb_lo[64];
@@ -281,6 +282,8 @@ void frr_preinit(struct frr_daemon_info *daemon, int argc, char **argv)
opt_extend(&os_zclient);
if (!(di->flags & FRR_NO_TCPVTY))
opt_extend(&os_vty);
+ if (di->flags & FRR_DETACH_LATER)
+ nodetach_daemon = true;
snprintf(config_default, sizeof(config_default), "%s/%s.conf",
frr_sysconfdir, di->name);
@@ -767,13 +770,16 @@ void frr_config_fork(void)
{
hook_call(frr_late_init, master);
- /* Don't start execution if we are in dry-run mode */
- if (di->dryrun) {
- frr_config_read_in(NULL);
- exit(0);
- }
+ if (!(di->flags & FRR_NO_CFG_PID_DRY)) {
+ /* Don't start execution if we are in dry-run mode */
+ if (di->dryrun) {
+ frr_config_read_in(NULL);
+ exit(0);
+ }
- thread_add_event(master, frr_config_read_in, NULL, 0, &di->read_in);
+ thread_add_event(master, frr_config_read_in, NULL, 0,
+ &di->read_in);
+ }
if (di->daemon_mode || di->terminal)
frr_daemonize();
@@ -783,7 +789,7 @@ void frr_config_fork(void)
pid_output(di->pid_file);
}
-void frr_vty_serv(void)
+static void frr_vty_serv(void)
{
/* allow explicit override of vty_path in the future
* (not currently set anywhere) */
@@ -810,14 +816,22 @@ void frr_vty_serv(void)
vty_serv_sock(di->vty_addr, di->vty_port, di->vty_path);
}
+static void frr_check_detach(void)
+{
+ if (nodetach_term || nodetach_daemon)
+ return;
+
+ if (daemon_ctl_sock != -1)
+ close(daemon_ctl_sock);
+ daemon_ctl_sock = -1;
+}
+
static void frr_terminal_close(int isexit)
{
int nullfd;
- if (daemon_ctl_sock != -1) {
- close(daemon_ctl_sock);
- daemon_ctl_sock = -1;
- }
+ nodetach_term = false;
+ frr_check_detach();
if (!di->daemon_mode || isexit) {
printf("\n%s exiting\n", di->name);
@@ -881,6 +895,12 @@ out:
return 0;
}
+void frr_detach(void)
+{
+ nodetach_daemon = false;
+ frr_check_detach();
+}
+
void frr_run(struct thread_master *master)
{
char instanceinfo[64] = "";
@@ -895,6 +915,8 @@ void frr_run(struct thread_master *master)
instanceinfo, di->vty_port, di->startinfo);
if (di->terminal) {
+ nodetach_term = true;
+
vty_stdio(frr_terminal_close);
if (daemon_ctl_sock != -1) {
set_nonblocking(daemon_ctl_sock);
@@ -914,9 +936,7 @@ void frr_run(struct thread_master *master)
close(nullfd);
}
- if (daemon_ctl_sock != -1)
- close(daemon_ctl_sock);
- daemon_ctl_sock = -1;
+ frr_check_detach();
}
/* end fixed stderr startup logging */
diff --git a/lib/libfrr.h b/lib/libfrr.h
index d25527990..db58ff92b 100644
--- a/lib/libfrr.h
+++ b/lib/libfrr.h
@@ -29,11 +29,21 @@
#include "module.h"
#include "hook.h"
+/* The following options disable specific command line options that
+ * are not applicable for a particular daemon.
+ */
#define FRR_NO_PRIVSEP (1 << 0)
#define FRR_NO_TCPVTY (1 << 1)
#define FRR_LIMITED_CLI (1 << 2)
-#define FRR_NO_CFG_PID_DRY (1 << 3)
+#define FRR_NO_CFG_PID_DRY (1 << 3)
#define FRR_NO_ZCLIENT (1 << 4)
+/* If FRR_DETACH_LATER is used, the daemon will keep its parent running
+ * until frr_detach() is called. Normally "somedaemon -d" returns once the
+ * main event loop is reached in the daemon; use this for extra startup bits.
+ *
+ * Does nothing if -d isn't used.
+ */
+#define FRR_DETACH_LATER (1 << 5)
struct frr_daemon_info {
unsigned flags;
@@ -102,10 +112,8 @@ extern struct thread_master *frr_init(void);
DECLARE_HOOK(frr_late_init, (struct thread_master * tm), (tm))
extern void frr_config_fork(void);
-extern void frr_vty_serv(void);
-
-/* note: contains call to frr_vty_serv() */
extern void frr_run(struct thread_master *master);
+extern void frr_detach(void);
extern bool frr_zclient_addr(struct sockaddr_storage *sa, socklen_t *sa_len,
const char *path);