diff options
Diffstat (limited to 'lib/libfrr.c')
| -rw-r--r-- | lib/libfrr.c | 59 |
1 files changed, 43 insertions, 16 deletions
diff --git a/lib/libfrr.c b/lib/libfrr.c index 07e2eafec5..33237df5fc 100644 --- a/lib/libfrr.c +++ b/lib/libfrr.c @@ -130,6 +130,7 @@ static const struct optspec os_always = { " --limit-fds Limit number of fds supported\n", lo_always}; +static bool logging_to_stdout = false; /* set when --log stdout specified */ static const struct option lo_cfg[] = { {"config_file", required_argument, NULL, 'f'}, @@ -738,6 +739,11 @@ struct event_loop *frr_init(void) while ((log_arg = log_args_pop(di->early_logging))) { command_setup_early_logging(log_arg->target, di->early_loglevel); + /* this is a bit of a hack, + but need to notice when + the target is stdout */ + if (strcmp(log_arg->target, "stdout") == 0) + logging_to_stdout = true; XFREE(MTYPE_TMP, log_arg); } @@ -1030,7 +1036,7 @@ void frr_config_fork(void) zlog_tls_buffer_init(); } -static void frr_vty_serv(void) +void frr_vty_serv_start(void) { /* allow explicit override of vty_path in the future * (not currently set anywhere) */ @@ -1052,7 +1058,15 @@ static void frr_vty_serv(void) di->vty_path = vtypath_default; } - vty_serv_sock(di->vty_addr, di->vty_port, di->vty_path); + vty_serv_start(di->vty_addr, di->vty_port, di->vty_path); +} + +void frr_vty_serv_stop(void) +{ + vty_serv_stop(); + + if (di->vty_path) + unlink(di->vty_path); } static void frr_check_detach(void) @@ -1088,9 +1102,15 @@ static void frr_terminal_close(int isexit) "%s: failed to open /dev/null: %s", __func__, safe_strerror(errno)); } else { - dup2(nullfd, 0); - dup2(nullfd, 1); - dup2(nullfd, 2); + int fd; + /* + * only redirect stdin, stdout, stderr to null when a tty also + * don't redirect when stdout is set with --log stdout + */ + for (fd = 2; fd >= 0; fd--) + if (isatty(fd) && + (fd != STDOUT_FILENO || !logging_to_stdout)) + dup2(nullfd, fd); close(nullfd); } } @@ -1143,7 +1163,8 @@ void frr_run(struct event_loop *master) { char instanceinfo[64] = ""; - frr_vty_serv(); + if (!(di->flags & FRR_MANUAL_VTY_START)) + frr_vty_serv_start(); if (di->instance) snprintf(instanceinfo, sizeof(instanceinfo), "instance %u ", @@ -1168,9 +1189,16 @@ void frr_run(struct event_loop *master) "%s: failed to open /dev/null: %s", __func__, safe_strerror(errno)); } else { - dup2(nullfd, 0); - dup2(nullfd, 1); - dup2(nullfd, 2); + int fd; + /* + * only redirect stdin, stdout, stderr to null when a + * tty also don't redirect when stdout is set with --log + * stdout + */ + for (fd = 2; fd >= 0; fd--) + if (isatty(fd) && + (fd != STDOUT_FILENO || !logging_to_stdout)) + dup2(nullfd, fd); close(nullfd); } @@ -1194,7 +1222,7 @@ void frr_fini(void) { FILE *fp; char filename[128]; - int have_leftovers; + int have_leftovers = 0; hook_call(frr_fini); @@ -1220,16 +1248,15 @@ void frr_fini(void) /* frrmod_init -> nothing needed / hooks */ rcu_shutdown(); - if (!debug_memstats_at_exit) - return; - - have_leftovers = log_memstats(stderr, di->name); + /* also log memstats to stderr when stderr goes to a file*/ + if (debug_memstats_at_exit || !isatty(STDERR_FILENO)) + have_leftovers = log_memstats(stderr, di->name); /* in case we decide at runtime that we want exit-memstats for - * a daemon, but it has no stderr because it's daemonized + * a daemon * (only do this if we actually have something to print though) */ - if (!have_leftovers) + if (!debug_memstats_at_exit || !have_leftovers) return; snprintf(filename, sizeof(filename), "/tmp/frr-memstats-%s-%llu-%llu", |
