]> code.delx.au - pulseaudio/commitdiff
core-util: introduce FD_CLOEXEC wrappers for open/socket/pipe/accept
authorLennart Poettering <lennart@poettering.net>
Fri, 30 Oct 2009 02:30:42 +0000 (03:30 +0100)
committerLennart Poettering <lennart@poettering.net>
Fri, 30 Oct 2009 02:30:42 +0000 (03:30 +0100)
configure.ac
src/pulsecore/core-util.c
src/pulsecore/core-util.h

index d7f55acf46f9c53b7bc0a235c7a345616b997949..1022a6eae1fd84545a091900d537fc341a958309 100644 (file)
@@ -445,7 +445,7 @@ AC_CHECK_FUNCS_ONCE([lstat])
 
 # Non-standard
 
-AC_CHECK_FUNCS_ONCE([setresuid setresgid setreuid setregid seteuid setegid ppoll strsignal sig2str strtof_l])
+AC_CHECK_FUNCS_ONCE([setresuid setresgid setreuid setregid seteuid setegid ppoll strsignal sig2str strtof_l pipe2 accept4])
 
 AC_FUNC_ALLOCA
 
index 27e09cbce6622dae80d9d4d98a5dd3d49e0e25ad..34516eea5b7071b99ac8e465192c08da1b8a3a02 100644 (file)
@@ -2889,3 +2889,82 @@ const char *pa_get_temp_dir(void) {
 
     return "/tmp";
 }
+
+int pa_open_cloexec(const char *fn, int flags, mode_t mode) {
+    int fd;
+
+#ifdef O_NOCTTY
+    flags |= O_NOCTTY;
+#endif
+
+#ifdef O_CLOEXEC
+    if ((fd = open(fn, flags|O_CLOEXEC, mode)) >= 0)
+        return fd;
+
+    if (errno != EINVAL)
+        return fd;
+#endif
+
+    if ((fd = open(fn, flags, mode)) < 0)
+        return fd;
+
+    pa_make_fd_cloexec(fd);
+    return fd;
+}
+
+int pa_socket_cloexec(int domain, int type, int protocol) {
+    int fd;
+
+#ifdef SOCK_CLOEXEC
+    if ((fd = socket(domain, type | SOCK_CLOEXEC, protocol)) >= 0)
+        return fd;
+
+    if (errno != EINVAL)
+        return fd;
+#endif
+
+    if ((fd = socket(domain, type, protocol)) < 0)
+        return fd;
+
+    pa_make_fd_cloexec(fd);
+    return fd;
+}
+
+int pa_pipe_cloexec(int pipefd[2]) {
+    int r;
+
+#ifdef HAVE_PIPE2
+    if ((r = pipe2(pipefd, O_CLOEXEC)) >= 0)
+        return r;
+
+    if (errno != EINVAL && errno != ENOSYS)
+        return r;
+#endif
+
+    if ((r = pipe(pipefd)) < 0)
+        return r;
+
+    pa_make_fd_cloexec(pipefd[0]);
+    pa_make_fd_cloexec(pipefd[1]);
+
+    return 0;
+}
+
+int pa_accept_cloexec(int sockfd, struct sockaddr *addr, socklen_t *addrlen) {
+    int fd;
+
+#ifdef HAVE_ACCEPT4
+    if ((fd = accept4(sockfd, addr, addrlen, SOCK_CLOEXEC)) >= 0)
+        return fd;
+
+    if (errno != EINVAL && errno != ENOSYS)
+        return fd;
+#endif
+
+    if ((fd = accept(sockfd, addr, addrlen)) < 0)
+        return fd;
+
+    pa_make_fd_cloexec(fd);
+
+    return 0;
+}
index 9986b14a022e46f41c0effdcdeede20450f8f533..323fdcb466d3697efe64beb2f63fab7ed3aae29a 100644 (file)
@@ -28,6 +28,7 @@
 #include <stdarg.h>
 #include <stdio.h>
 #include <string.h>
+#include <sys/socket.h>
 
 #ifdef HAVE_SYS_RESOURCE_H
 #include <sys/resource.h>
@@ -258,4 +259,9 @@ pa_bool_t pa_run_from_build_tree(void);
 
 const char *pa_get_temp_dir(void);
 
+int pa_open_cloexec(const char *fn, int flags, mode_t mode);
+int pa_socket_cloexec(int domain, int type, int protocol);
+int pa_pipe_cloexec(int pipefd[2]);
+int pa_accept_cloexec(int sockfd, struct sockaddr *addr, socklen_t *addrlen);
+
 #endif