make O_CLOEXEC, O_NONBLOCK and socket low latency fd ops more uniform: always return...
authorLennart Poettering <lennart@poettering.net>
Wed, 19 Sep 2007 00:12:01 +0000 (00:12 +0000)
committerLennart Poettering <lennart@poettering.net>
Wed, 19 Sep 2007 00:12:01 +0000 (00:12 +0000)
git-svn-id: file:///home/lennart/svn/public/pulseaudio/branches/lennart@1867 fefdeb5f-60dc-0310-8127-8f9354f1896f

16 files changed:
src/daemon/cpulimit.c
src/modules/module-pipe-sink.c
src/modules/module-pipe-source.c
src/modules/oss-util.c
src/modules/rtp/module-rtp-send.c
src/pulse/context.c
src/pulse/mainloop-signal.c
src/pulse/mainloop.c
src/pulsecore/core-util.c
src/pulsecore/core-util.h
src/pulsecore/fdsem.c
src/pulsecore/iochannel.c
src/pulsecore/socket-client.c
src/pulsecore/socket-server.c
src/pulsecore/socket-util.c
src/pulsecore/socket-util.h

index 0fe11ea..a61f43e 100644 (file)
@@ -184,10 +184,10 @@ int pa_cpu_limit_init(pa_mainloop_api *m) {
         return -1;
     }
 
-    pa_make_nonblock_fd(the_pipe[0]);
-    pa_make_nonblock_fd(the_pipe[1]);
-    pa_fd_set_cloexec(the_pipe[0], 1);
-    pa_fd_set_cloexec(the_pipe[1], 1);
+    pa_make_fd_nonblock(the_pipe[0]);
+    pa_make_fd_nonblock(the_pipe[1]);
+    pa_make_fd_cloexec(the_pipe[0]);
+    pa_make_fd_cloexec(the_pipe[1]);
 
     api = m;
     io_event = api->io_new(m, the_pipe[0], PA_IO_EVENT_INPUT, callback, NULL);
index a1bdc8f..edacf04 100644 (file)
@@ -236,8 +236,8 @@ int pa__init(pa_module*m) {
         goto fail;
     }
 
-    pa_fd_set_cloexec(u->fd, 1);
-    pa_make_nonblock_fd(u->fd);
+    pa_make_fd_cloexec(u->fd);
+    pa_make_fd_nonblock(u->fd);
 
     if (fstat(u->fd, &st) < 0) {
         pa_log("fstat('%s'): %s", u->filename, pa_cstrerror(errno));
index 382da8f..2313df6 100644 (file)
@@ -214,8 +214,8 @@ int pa__init(pa_module*m) {
         goto fail;
     }
 
-    pa_fd_set_cloexec(u->fd, 1);
-    pa_make_nonblock_fd(u->fd);
+    pa_make_fd_cloexec(u->fd);
+    pa_make_fd_nonblock(u->fd);
 
     if (fstat(u->fd, &st) < 0) {
         pa_log("fstat('%s'): %s",u->filename, pa_cstrerror(errno));
index 3649880..3bef8a3 100644 (file)
@@ -141,7 +141,7 @@ success:
 #endif
                  *pcaps & DSP_CAP_TRIGGER ? " TRIGGER" : "");
 
-    pa_fd_set_cloexec(fd, 1);
+    pa_make_fd_cloexec(fd);
 
     return fd;
 
index 8fac44f..f79867c 100644 (file)
@@ -280,8 +280,10 @@ int pa__init(pa_module*m) {
     }
 
     /* If the socket queue is full, let's drop packets */
-    pa_make_nonblock_fd(fd);
-    pa_socket_udp_low_delay(fd);
+    pa_make_fd_nonblock(fd);
+    pa_make_udp_socket_low_delay(fd);
+    pa_make_fd_cloexec(fd);
+    pa_make_fd_cloexec(sap_fd);
 
     pa_source_output_new_data_init(&data);
     data.name = "RTP Monitor Stream";
index a39646d..805cd44 100644 (file)
@@ -498,10 +498,10 @@ static int context_connect_spawn(pa_context *c) {
         goto fail;
     }
 
-    pa_fd_set_cloexec(fds[0], 1);
+    pa_make_fd_cloexec(fds[0]);
 
-    pa_socket_low_delay(fds[0]);
-    pa_socket_low_delay(fds[1]);
+    pa_make_socket_low_delay(fds[0]);
+    pa_make_socket_low_delay(fds[1]);
 
     if (c->spawn_api.prefork)
         c->spawn_api.prefork();
index b6414c4..a986b24 100644 (file)
@@ -123,10 +123,10 @@ int pa_signal_init(pa_mainloop_api *a) {
         return -1;
     }
 
-    pa_make_nonblock_fd(signal_pipe[0]);
-    pa_make_nonblock_fd(signal_pipe[1]);
-    pa_assert_se(pa_fd_set_cloexec(signal_pipe[0], 1) == 0);
-    pa_assert_se(pa_fd_set_cloexec(signal_pipe[1], 1) == 0);
+    pa_make_fd_nonblock(signal_pipe[0]);
+    pa_make_fd_nonblock(signal_pipe[1]);
+    pa_make_fd_cloexec(signal_pipe[0]);
+    pa_make_fd_cloexec(signal_pipe[1]);
 
     api = a;
 
index c69c665..bab8eb5 100644 (file)
@@ -457,10 +457,10 @@ pa_mainloop *pa_mainloop_new(void) {
         return NULL;
     }
 
-    pa_make_nonblock_fd(m->wakeup_pipe[0]);
-    pa_make_nonblock_fd(m->wakeup_pipe[1]);
-    pa_fd_set_cloexec(m->wakeup_pipe[0], 1);
-    pa_fd_set_cloexec(m->wakeup_pipe[1], 1);
+    pa_make_fd_nonblock(m->wakeup_pipe[0]);
+    pa_make_fd_nonblock(m->wakeup_pipe[1]);
+    pa_make_fd_cloexec(m->wakeup_pipe[0]);
+    pa_make_fd_cloexec(m->wakeup_pipe[1]);
     m->wakeup_requested = 0;
 
     PA_LLIST_HEAD_INIT(pa_io_event, m->io_events);
index afd89ba..5532c40 100644 (file)
@@ -134,23 +134,42 @@ int pa_set_root(HANDLE handle) {
 #endif
 
 /** Make a file descriptor nonblock. Doesn't do any error checking */
-void pa_make_nonblock_fd(int fd) {
+void pa_make_fd_nonblock(int fd) {
+
 #ifdef O_NONBLOCK
     int v;
     pa_assert(fd >= 0);
 
-    if ((v = fcntl(fd, F_GETFL)) >= 0)
-        if (!(v & O_NONBLOCK))
-            fcntl(fd, F_SETFL, v|O_NONBLOCK);
+    pa_assert_se((v = fcntl(fd, F_GETFL)) >= 0);
+
+    if (!(v & O_NONBLOCK))
+        pa_assert_se(fcntl(fd, F_SETFL, v|O_NONBLOCK) >= 0);
+    
 #elif defined(OS_IS_WIN32)
     u_long arg = 1;
     if (ioctlsocket(fd, FIONBIO, &arg) < 0) {
-        if (WSAGetLastError() == WSAENOTSOCK)
-            pa_log_warn("Only sockets can be made non-blocking!");
+        pa_assert_se(WSAGetLastError() == WSAENOTSOCK);
+        pa_log_warn("Only sockets can be made non-blocking!");
     }
 #else
     pa_log_warn("Non-blocking I/O not supported.!");
 #endif
+
+}
+
+/* Set the FD_CLOEXEC flag for a fd */
+void pa_make_fd_cloexec(int fd) {
+
+#ifdef FD_CLOEXEC
+    int v;
+    pa_assert(fd >= 0);
+
+    pa_assert_se((v = fcntl(fd, F_GETFD, 0)) >= 0);
+
+    if (!(v & FD_CLOEXEC))
+        pa_assert_se(fcntl(fd, F_SETFD, v|FD_CLOEXEC) >= 0);
+#endif
+
 }
 
 /** Creates a directory securely */
@@ -552,25 +571,6 @@ void pa_reset_priority(void) {
 #endif
 }
 
-/* Set the FD_CLOEXEC flag for a fd */
-int pa_fd_set_cloexec(int fd, int b) {
-
-#ifdef FD_CLOEXEC
-    int v;
-    pa_assert(fd >= 0);
-
-    if ((v = fcntl(fd, F_GETFD, 0)) < 0)
-        return -1;
-
-    v = (v & ~FD_CLOEXEC) | (b ? FD_CLOEXEC : 0);
-
-    if (fcntl(fd, F_SETFD, v) < 0)
-        return -1;
-#endif
-
-    return 0;
-}
-
 /* Try to parse a boolean string value.*/
 int pa_parse_boolean(const char *v) {
 
@@ -629,12 +629,12 @@ const char *pa_sig2str(int sig) {
 
     if (sig <= 0)
         goto fail;
-
 #ifdef NSIG
-       if (sig >= NSIG)
-               goto fail;
+    if (sig >= NSIG)
+        goto fail;
 #endif
-
+        
 #ifdef HAVE_SIG2STR
     {
         char buf[SIG2STR_MAX];
index b8ef464..44c7574 100644 (file)
@@ -34,7 +34,8 @@
 
 struct timeval;
 
-void pa_make_nonblock_fd(int fd);
+void pa_make_fd_nonblock(int fd);
+void pa_make_fd_cloexec(int fd);
 
 int pa_make_secure_dir(const char* dir, mode_t m, uid_t uid, gid_t gid);
 int pa_make_secure_parent_dir(const char *fn, mode_t, uid_t uid, gid_t gid);
@@ -59,8 +60,6 @@ void pa_make_realtime(void);
 void pa_raise_priority(void);
 void pa_reset_priority(void);
 
-int pa_fd_set_cloexec(int fd, int b);
-
 int pa_parse_boolean(const char *s) PA_GCC_PURE;
 
 char *pa_split(const char *c, const char*delimiters, const char **state);
index 68207a7..c81797e 100644 (file)
@@ -54,8 +54,8 @@ pa_fdsem *pa_fdsem_new(void) {
         return NULL;
     }
 
-    pa_fd_set_cloexec(f->fds[0], 1);
-    pa_fd_set_cloexec(f->fds[1], 1);
+    pa_make_fd_cloexec(f->fds[0]);
+    pa_make_fd_cloexec(f->fds[1]);
 
     pa_atomic_store(&f->waiting, 0);
     pa_atomic_store(&f->signalled, 0);
index 2f586cb..90136fe 100644 (file)
@@ -145,17 +145,17 @@ pa_iochannel* pa_iochannel_new(pa_mainloop_api*m, int ifd, int ofd) {
 
     if (ifd == ofd) {
         pa_assert(ifd >= 0);
-        pa_make_nonblock_fd(io->ifd);
+        pa_make_fd_nonblock(io->ifd);
         io->input_event = io->output_event = m->io_new(m, ifd, PA_IO_EVENT_INPUT|PA_IO_EVENT_OUTPUT, callback, io);
     } else {
 
         if (ifd >= 0) {
-            pa_make_nonblock_fd(io->ifd);
+            pa_make_fd_nonblock(io->ifd);
             io->input_event = m->io_new(m, ifd, PA_IO_EVENT_INPUT, callback, io);
         }
 
         if (ofd >= 0) {
-            pa_make_nonblock_fd(io->ofd);
+            pa_make_fd_nonblock(io->ofd);
             io->output_event = m->io_new(m, ofd, PA_IO_EVENT_OUTPUT, callback, io);
         }
     }
index 360a0b4..6748c28 100644 (file)
@@ -208,7 +208,7 @@ static int do_connect(pa_socket_client *c, const struct sockaddr *sa, socklen_t
     pa_assert(sa);
     pa_assert(len > 0);
 
-    pa_make_nonblock_fd(c->fd);
+    pa_make_fd_nonblock(c->fd);
 
     if ((r = connect(c->fd, sa, len)) < 0) {
 #ifdef OS_IS_WIN32
@@ -293,11 +293,11 @@ static int sockaddr_prepare(pa_socket_client *c, const struct sockaddr *sa, size
         return -1;
     }
 
-    pa_fd_set_cloexec(c->fd, 1);
+    pa_make_fd_cloexec(c->fd);
     if (sa->sa_family == AF_INET || sa->sa_family == AF_INET6)
-        pa_socket_tcp_low_delay(c->fd);
+        pa_make_tcp_socket_low_delay(c->fd);
     else
-        pa_socket_low_delay(c->fd);
+        pa_make_socket_low_delay(c->fd);
 
     if (do_connect(c, sa, salen) < 0)
         return -1;
index 502005d..e0d2f16 100644 (file)
@@ -111,7 +111,7 @@ static void callback(pa_mainloop_api *mainloop, pa_io_event *e, int fd, PA_GCC_U
         goto finish;
     }
 
-    pa_fd_set_cloexec(nfd, 1);
+    pa_make_fd_cloexec(nfd);
 
     if (!s->on_connection) {
         pa_close(nfd);
@@ -137,9 +137,9 @@ static void callback(pa_mainloop_api *mainloop, pa_io_event *e, int fd, PA_GCC_U
 
     /* There should be a check for socket type here */
     if (s->type == SOCKET_SERVER_IPV4)
-        pa_socket_tcp_low_delay(fd);
+        pa_make_tcp_socket_low_delay(fd);
     else
-        pa_socket_low_delay(fd);
+        pa_make_socket_low_delay(fd);
 
     pa_assert_se(io = pa_iochannel_new(s->mainloop, nfd, nfd));
     s->on_connection(s, io, s->userdata);
@@ -193,13 +193,13 @@ pa_socket_server* pa_socket_server_new_unix(pa_mainloop_api *m, const char *file
         goto fail;
     }
 
-    pa_fd_set_cloexec(fd, 1);
+    pa_make_fd_cloexec(fd);
 
     sa.sun_family = AF_UNIX;
     strncpy(sa.sun_path, filename, sizeof(sa.sun_path)-1);
     sa.sun_path[sizeof(sa.sun_path) - 1] = 0;
 
-    pa_socket_low_delay(fd);
+    pa_make_socket_low_delay(fd);
 
     if (bind(fd, (struct sockaddr*) &sa, SUN_LEN(&sa)) < 0) {
         pa_log("bind(): %s", pa_cstrerror(errno));
@@ -253,14 +253,14 @@ pa_socket_server* pa_socket_server_new_ipv4(pa_mainloop_api *m, uint32_t address
         goto fail;
     }
 
-    pa_fd_set_cloexec(fd, 1);
+    pa_make_fd_cloexec(fd);
 
 #ifdef SO_REUSEADDR
     if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0)
         pa_log("setsockopt(): %s", pa_cstrerror(errno));
 #endif
 
-    pa_socket_tcp_low_delay(fd);
+    pa_make_tcp_socket_low_delay(fd);
 
     memset(&sa, 0, sizeof(sa));
     sa.sin_family = AF_INET;
@@ -305,7 +305,7 @@ pa_socket_server* pa_socket_server_new_ipv6(pa_mainloop_api *m, const uint8_t ad
         goto fail;
     }
 
-    pa_fd_set_cloexec(fd, 1);
+    pa_make_fd_cloexec(fd);
 
 #ifdef IPV6_V6ONLY
     if (setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &on, sizeof(on)) < 0)
@@ -317,7 +317,7 @@ pa_socket_server* pa_socket_server_new_ipv6(pa_mainloop_api *m, const uint8_t ad
         pa_log("setsockopt(SOL_SOCKET, SO_REUSEADDR, 1): %s", pa_cstrerror(errno));
 #endif
 
-    pa_socket_tcp_low_delay(fd);
+    pa_make_tcp_socket_low_delay(fd);
 
     memset(&sa, 0, sizeof(sa));
     sa.sin6_family = AF_INET6;
index b7361e9..085edeb 100644 (file)
@@ -143,83 +143,64 @@ void pa_socket_peer_to_string(int fd, char *c, size_t l) {
     pa_snprintf(c, l, "Unknown client");
 }
 
-int pa_socket_low_delay(int fd) {
+void pa_make_socket_low_delay(int fd) {
     
 #ifdef SO_PRIORITY
     int priority;
     pa_assert(fd >= 0);
 
     priority = 6;
-    if (setsockopt(fd, SOL_SOCKET, SO_PRIORITY, (void*)&priority, sizeof(priority)) < 0) {
+    if (setsockopt(fd, SOL_SOCKET, SO_PRIORITY, (void*)&priority, sizeof(priority)) < 0)
         pa_log_warn("SO_PRIORITY failed: %s", pa_cstrerror(errno));
-        return -1;
-    }
 #endif
-
-    return 0;
 }
 
-int pa_socket_tcp_low_delay(int fd) {
-    int ret, tos, on;
-
+void pa_make_tcp_socket_low_delay(int fd) {
     pa_assert(fd >= 0);
-
-    ret = pa_socket_low_delay(fd);
-
-    on = 1;
-    tos = 0;
+    
+    pa_make_socket_low_delay(fd);
 
 #if defined(SOL_TCP) || defined(IPPROTO_TCP)
+    {
+        int on = 1;
 #if defined(SOL_TCP)
-    if (setsockopt(fd, SOL_TCP, TCP_NODELAY, (void*)&on, sizeof(on)) < 0)
+        if (setsockopt(fd, SOL_TCP, TCP_NODELAY, (void*)&on, sizeof(on)) < 0)
 #else
-    if (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (void*)&on, sizeof(on)) < 0)
+        if (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (void*)&on, sizeof(on)) < 0)
 #endif
-    {
-        pa_log_warn("TCP_NODELAY failed: %s", pa_cstrerror(errno));
-        ret = -1;
+            pa_log_warn("TCP_NODELAY failed: %s", pa_cstrerror(errno));
     }
 #endif
-
+        
 #if defined(IPTOS_LOWDELAY) && defined(IP_TOS) && (defined(SOL_IP) || defined(IPPROTO_IP))
-    tos = IPTOS_LOWDELAY;
+    {
+        int tos = IPTOS_LOWDELAY;
 #ifdef SOL_IP
-    if (setsockopt(fd, SOL_IP, IP_TOS, (void*)&tos, sizeof(tos)) < 0)
+        if (setsockopt(fd, SOL_IP, IP_TOS, (void*)&tos, sizeof(tos)) < 0)
 #else
-    if (setsockopt(fd, IPPROTO_IP, IP_TOS, (void*)&tos, sizeof(tos)) < 0)
+        if (setsockopt(fd, IPPROTO_IP, IP_TOS, (void*)&tos, sizeof(tos)) < 0)
 #endif
-    {
-        pa_log_warn("IP_TOS failed: %s", pa_cstrerror(errno));
-        ret = -1;
+            pa_log_warn("IP_TOS failed: %s", pa_cstrerror(errno));
     }
 #endif
-
-    return ret;
 }
 
-int pa_socket_udp_low_delay(int fd) {
-    int ret, tos;
-
+void pa_make_udp_socket_low_delay(int fd) {
     pa_assert(fd >= 0);
-
-    ret = pa_socket_low_delay(fd);
-
-    tos = 0;
-
+    
+    pa_make_socket_low_delay(fd);
+    
 #if defined(IPTOS_LOWDELAY) && defined(IP_TOS) && (defined(SOL_IP) || defined(IPPROTO_IP))
-    tos = IPTOS_LOWDELAY;
+    {
+        int tos = IPTOS_LOWDELAY;
 #ifdef SOL_IP
-    if (setsockopt(fd, SOL_IP, IP_TOS, (void*)&tos, sizeof(tos)) < 0)
+        if (setsockopt(fd, SOL_IP, IP_TOS, (void*)&tos, sizeof(tos)) < 0)
 #else
-    if (setsockopt(fd, IPPROTO_IP, IP_TOS, (void*)&tos, sizeof(tos)) < 0)
+        if (setsockopt(fd, IPPROTO_IP, IP_TOS, (void*)&tos, sizeof(tos)) < 0)
 #endif
-    {
-        ret = -1;
-        pa_log_warn("IP_TOS failed: %s", pa_cstrerror(errno));
+            pa_log_warn("IP_TOS failed: %s", pa_cstrerror(errno));
     }
 #endif
-
-    return ret;
 }
 
 int pa_socket_set_rcvbuf(int fd, size_t l) {
index abe9ce1..a0344c6 100644 (file)
@@ -29,9 +29,9 @@
 
 void pa_socket_peer_to_string(int fd, char *c, size_t l);
 
-int pa_socket_low_delay(int fd);
-int pa_socket_tcp_low_delay(int fd);
-int pa_socket_udp_low_delay(int fd);
+void pa_make_socket_low_delay(int fd);
+void pa_make_tcp_socket_low_delay(int fd);
+void pa_make_udp_socket_low_delay(int fd);
 
 int pa_socket_set_sndbuf(int fd, size_t l);
 int pa_socket_set_rcvbuf(int fd, size_t l);