diff options
author | djm@openbsd.org <djm@openbsd.org> | 2020-10-03 09:22:26 +0000 |
---|---|---|
committer | Damien Miller <djm@mindrot.org> | 2020-10-03 19:34:24 +1000 |
commit | 396d32f3a1a16e54df2a76b2a9b237868580dcbe (patch) | |
tree | 77019a916fcb969986a349aaede7409a25778b6a /ssh.c | |
parent | 1286981d08b8429a64613215ce8bff3f6b32488a (diff) | |
download | openssh-git-396d32f3a1a16e54df2a76b2a9b237868580dcbe.tar.gz |
upstream: There are lots of place where we want to redirect stdin,
stdout and/or stderr to /dev/null. Factor all these out to a single
stdfd_devnull() function that allows selection of which of these to redirect.
ok markus@
OpenBSD-Commit-ID: 3033ba5a4c47cacfd5def020d42cabc52fad3099
Diffstat (limited to 'ssh.c')
-rw-r--r-- | ssh.c | 48 |
1 files changed, 8 insertions, 40 deletions
@@ -1,4 +1,4 @@ -/* $OpenBSD: ssh.c,v 1.536 2020/09/21 07:29:09 djm Exp $ */ +/* $OpenBSD: ssh.c,v 1.537 2020/10/03 09:22:26 djm Exp $ */ /* * Author: Tatu Ylonen <ylo@cs.hut.fi> * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland @@ -1698,7 +1698,6 @@ static void control_persist_detach(void) { pid_t pid; - int devnull, keep_stderr; debug("%s: backgrounding master process", __func__); @@ -1725,18 +1724,8 @@ control_persist_detach(void) /* muxclient() doesn't return on success. */ fatal("Failed to connect to new control master"); } - if ((devnull = open(_PATH_DEVNULL, O_RDWR)) == -1) { - error("%s: open(\"/dev/null\"): %s", __func__, - strerror(errno)); - } else { - keep_stderr = log_is_on_stderr() && debug_flag; - if (dup2(devnull, STDIN_FILENO) == -1 || - dup2(devnull, STDOUT_FILENO) == -1 || - (!keep_stderr && dup2(devnull, STDERR_FILENO) == -1)) - error("%s: dup2: %s", __func__, strerror(errno)); - if (devnull > STDERR_FILENO) - close(devnull); - } + if (stdfd_devnull(1, 1, !(log_is_on_stderr() && debug_flag)) == -1) + error("%s: stdfd_devnull failed", __func__); daemon(1, 1); setproctitle("%s [mux]", options.control_path); } @@ -1745,26 +1734,14 @@ control_persist_detach(void) static void fork_postauth(void) { - int devnull, keep_stderr; - if (need_controlpersist_detach) control_persist_detach(); debug("forking to background"); fork_after_authentication_flag = 0; if (daemon(1, 1) == -1) fatal("daemon() failed: %.200s", strerror(errno)); - if ((devnull = open(_PATH_DEVNULL, O_WRONLY)) == -1) - error("%s: open %s: %s", __func__, - _PATH_DEVNULL, strerror(errno)); - else { - keep_stderr = log_is_on_stderr() && debug_flag; - if (dup2(devnull, STDIN_FILENO) == -1 || - dup2(devnull, STDOUT_FILENO) == -1 || - (!keep_stderr && dup2(devnull, STDOUT_FILENO) == -1)) - fatal("%s: dup2() stdio failed", __func__); - if (devnull > STDERR_FILENO) - close(devnull); - } + if (stdfd_devnull(1, 1, !(log_is_on_stderr() && debug_flag)) == -1) + error("%s: stdfd_devnull failed", __func__); } static void @@ -2075,7 +2052,7 @@ ssh_session2_open(struct ssh *ssh) static int ssh_session2(struct ssh *ssh, struct passwd *pw) { - int r, devnull, id = -1; + int r, id = -1; char *cp, *tun_fwd_ifname = NULL; /* XXX should be pre-session */ @@ -2162,17 +2139,8 @@ ssh_session2(struct ssh *ssh, struct passwd *pw) * NB. this can only happen after LocalCommand has completed, * as it may want to write to stdout. */ - if (!need_controlpersist_detach) { - if ((devnull = open(_PATH_DEVNULL, O_WRONLY)) == -1) { - error("%s: open %s: %s", __func__, - _PATH_DEVNULL, strerror(errno)); - } else { - if (dup2(devnull, STDOUT_FILENO) == -1) - fatal("%s: dup2() stdout failed", __func__); - if (devnull > STDERR_FILENO) - close(devnull); - } - } + if (!need_controlpersist_detach && stdfd_devnull(0, 1, 0) == -1) + error("%s: stdfd_devnull failed", __func__); /* * If requested and we are not interested in replies to remote |