aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorMax Kellermann <max@duempel.org>2011-07-01 10:50:15 +0200
committerMax Kellermann <max@duempel.org>2011-07-01 10:50:15 +0200
commit08e2e2e7910bcf4f0d50fae8feb4431781fc9040 (patch)
treefb5d70880f5f101d907ff8b329b6e286593ff326 /src
parent531c0067eca936119aca1bcb042befa6dcdab8b8 (diff)
downloadmpd-08e2e2e7910bcf4f0d50fae8feb4431781fc9040.tar.gz
mpd-08e2e2e7910bcf4f0d50fae8feb4431781fc9040.tar.xz
mpd-08e2e2e7910bcf4f0d50fae8feb4431781fc9040.zip
fd_util: add function socketpair_cloexec_nonblock()
Diffstat (limited to 'src')
-rw-r--r--src/fd_util.c23
-rw-r--r--src/fd_util.h7
2 files changed, 30 insertions, 0 deletions
diff --git a/src/fd_util.c b/src/fd_util.c
index 5fcf84be3..36f6e2fc9 100644
--- a/src/fd_util.c
+++ b/src/fd_util.c
@@ -206,6 +206,29 @@ socketpair_cloexec(int domain, int type, int protocol, int sv[2])
return ret;
}
+int
+socketpair_cloexec_nonblock(int domain, int type, int protocol, int sv[2])
+{
+ int ret;
+
+#if defined(SOCK_CLOEXEC) && defined(SOCK_NONBLOCK)
+ ret = socketpair(domain, type | SOCK_CLOEXEC | SOCK_NONBLOCK, protocol,
+ sv);
+ if (ret >= 0 || errno != EINVAL)
+ return ret;
+#endif
+
+ ret = socketpair(domain, type, protocol, sv);
+ if (ret >= 0) {
+ fd_set_cloexec(sv[0], true);
+ fd_set_nonblock(sv[0]);
+ fd_set_cloexec(sv[1], true);
+ fd_set_nonblock(sv[1]);
+ }
+
+ return ret;
+}
+
#endif
int
diff --git a/src/fd_util.h b/src/fd_util.h
index 494b39ed6..96ad784d4 100644
--- a/src/fd_util.h
+++ b/src/fd_util.h
@@ -89,6 +89,13 @@ pipe_cloexec_nonblock(int fd[2]);
int
socketpair_cloexec(int domain, int type, int protocol, int sv[2]);
+/**
+ * Wrapper for socketpair(), which sets the flags CLOEXEC and NONBLOCK
+ * (atomically if supported by the OS).
+ */
+int
+socketpair_cloexec_nonblock(int domain, int type, int protocol, int sv[2]);
+
#endif
/**