aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTimo Weingärtner <timo@tiwe.de>2021-04-27 12:50:51 +0200
committerTimo Weingärtner <timo@tiwe.de>2026-03-07 22:25:16 +0100
commit56ced7e19f4bce304f567aea3d6cd67769bb89e3 (patch)
tree1244ec06dca0f9eafba80c3676a6c5abcd671601
parentdaf69a87ec7d01673362c1c9bbdbcb61c71ab83e (diff)
downloadssh-agent-filter-56ced7e19f4bce304f567aea3d6cd67769bb89e3.tar.gz
handle some edge cases when dup2()ing /dev/null
-rw-r--r--ssh-agent-filter.C12
1 files changed, 7 insertions, 5 deletions
diff --git a/ssh-agent-filter.C b/ssh-agent-filter.C
index 252e017..b806a85 100644
--- a/ssh-agent-filter.C
+++ b/ssh-agent-filter.C
@@ -602,11 +602,13 @@ int main (int const argc, char const * const * const argv) {
// the following stuff is optional, so we don't do error checking
setsid();
static_cast<void>(chdir("/"));
- int devnull = open("/dev/null", O_RDWR);
- dup2(devnull, 0);
- dup2(devnull, 1);
- dup2(devnull, 2);
- close(devnull);
+ if (int devnull = open("/dev/null", O_RDWR); devnull != -1) {
+ dup2(devnull, 0);
+ dup2(devnull, 1);
+ dup2(devnull, 2);
+ if (devnull > 2)
+ close(devnull);
+ }
} else {
cout << "copy this to another terminal:" << endl;
cout << "SSH_AUTH_SOCK='" << path.native() << "'; export SSH_AUTH_SOCK;" << endl;