]> git.puffer.fish Git - mirror/frr.git/commitdiff
lib: --terminal option for all daemons
authorDavid Lamparter <equinox@opensourcerouting.org>
Thu, 1 Jun 2017 13:19:27 +0000 (15:19 +0200)
committerDavid Lamparter <equinox@opensourcerouting.org>
Wed, 2 Aug 2017 00:59:51 +0000 (02:59 +0200)
This allows, among other things, piping in a configuration from stdin.

Signed-off-by: David Lamparter <equinox@opensourcerouting.org>
lib/libfrr.c
lib/libfrr.h

index 73580b8a086427d406087eca916e31d5d74e90b3..efd8991e82ed6a00834d09e0d99de5f54c0cbc07 100644 (file)
@@ -95,12 +95,15 @@ static const struct option lo_cfg_pid_dry[] = {
        {"pid_file", required_argument, NULL, 'i'},
        {"config_file", required_argument, NULL, 'f'},
        {"dryrun", no_argument, NULL, 'C'},
+       {"terminal", no_argument, NULL, 't'},
        {NULL}};
 static const struct optspec os_cfg_pid_dry = {
-       "f:i:C",
+       "f:i:Ct",
        "  -f, --config_file  Set configuration file name\n"
        "  -i, --pid_file     Set process identifier file name\n"
-       "  -C, --dryrun       Check configuration for validity and exit\n",
+       "  -C, --dryrun       Check configuration for validity and exit\n"
+       "  -t, --terminal     Open terminal session on stdio\n"
+       "  -d -t              Daemonize after terminal session ends\n",
        lo_cfg_pid_dry};
 
 
@@ -234,6 +237,11 @@ static int frr_opt(int opt)
                        return 1;
                di->dryrun = 1;
                break;
+       case 't':
+               if (di->flags & FRR_NO_CFG_PID_DRY)
+                       return 1;
+               di->terminal = 1;
+               break;
        case 'z':
                if (di->flags & FRR_NO_ZCLIENT)
                        return 1;
@@ -489,6 +497,28 @@ void frr_vty_serv(void)
        vty_serv_sock(di->vty_addr, di->vty_port, di->vty_path);
 }
 
+static void frr_terminal_close(void)
+{
+       if (!di->daemon_mode) {
+               printf("\n%s exiting\n", di->name);
+               raise(SIGINT);
+       } else {
+               printf("\n%s daemonizing\n", di->name);
+               fflush(stdout);
+       }
+
+       int nullfd = open("/dev/null", O_RDONLY | O_NOCTTY);
+       dup2(nullfd, 0);
+       dup2(nullfd, 1);
+       dup2(nullfd, 2);
+       close(nullfd);
+
+       if (daemon_ctl_sock != -1) {
+               close(daemon_ctl_sock);
+               daemon_ctl_sock = -1;
+       }
+}
+
 void frr_run(struct thread_master *master)
 {
        char instanceinfo[64] = "";
@@ -502,7 +532,9 @@ void frr_run(struct thread_master *master)
        zlog_notice("%s %s starting: %svty@%d%s", di->name, FRR_VERSION,
                    instanceinfo, di->vty_port, di->startinfo);
 
-       if (daemon_ctl_sock != -1) {
+       if (di->terminal) {
+               vty_stdio(frr_terminal_close);
+       } else if (daemon_ctl_sock != -1) {
                close(daemon_ctl_sock);
                daemon_ctl_sock = -1;
        }
index 0f6ed0cb00fa8d7f7d9bc6cee00b4c3366cc476c..238a6f976da0d9e2f982fcb661040597ee5792bf 100644 (file)
@@ -49,6 +49,7 @@ struct frr_daemon_info {
        char *vty_sock_path;
        bool dryrun;
        bool daemon_mode;
+       bool terminal;
        const char *config_file;
        const char *pid_file;
        const char *vty_path;