aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJason A. Donenfeld <Jason@zx2c4.com>2020-05-23 16:44:12 -0600
committerJason A. Donenfeld <Jason@zx2c4.com>2020-05-24 03:25:23 -0600
commit7e060f4c15d8f22fcbc972f7dd87e9fe69851e03 (patch)
tree5d653c3620b665ff9b99e2f3d3e73f12d4041461
parentportable -> master (diff)
downloadOpenSMTPD-7e060f4c15d8f22fcbc972f7dd87e9fe69851e03.tar.xz
OpenSMTPD-7e060f4c15d8f22fcbc972f7dd87e9fe69851e03.zip
compat: pipe2: enable O_CLOEXEC correctly and support O_NONBLOCK
O_CLOEXEC must be enabled with F_SETFD, not F_SETFL. Prior passing FD_CLOEXEC to F_SETFL resulted in wrong behavior. While we're fixing this, we can also add support for O_CLOEXEC, which we enable via F_SETFL. Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
-rw-r--r--openbsd-compat/pipe2.c22
1 files changed, 16 insertions, 6 deletions
diff --git a/openbsd-compat/pipe2.c b/openbsd-compat/pipe2.c
index 710f8df2..fd5c3f49 100644
--- a/openbsd-compat/pipe2.c
+++ b/openbsd-compat/pipe2.c
@@ -1,5 +1,6 @@
/*
* Copyright (c) 2020 Gilles Chehade <gilles@poolp.org>
+ * Copyright (C) 2020 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved.
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
@@ -25,12 +26,21 @@ pipe2(int pipefd[2], int flags)
if (pipe(pipefd) == -1)
return -1;
- if (fcntl(pipefd[0], F_SETFL, FD_CLOEXEC) == -1 ||
- fcntl(pipefd[1], F_SETFL, FD_CLOEXEC) == -1) {
- close(pipefd[0]);
- close(pipefd[1]);
- return -1;
- }
+ if ((flags & O_NONBLOCK) &&
+ (fcntl(pipefd[0], F_SETFL, O_NONBLOCK) == -1 ||
+ fcntl(pipefd[1], F_SETFL, O_NONBLOCK) == -1)) {
+ close(pipefd[0]);
+ close(pipefd[1]);
+ return -1;
+ }
+
+ if ((flags & O_CLOEXEC) &&
+ (fcntl(pipefd[0], F_SETFD, FD_CLOEXEC) == -1 ||
+ fcntl(pipefd[1], F_SETFD, FD_CLOEXEC) == -1)) {
+ close(pipefd[0]);
+ close(pipefd[1]);
+ return -1;
+ }
return 0;
}