]> code.delx.au - pulseaudio/blobdiff - src/pulsecore/iochannel.c
remap: Change remapping function argument type from void to int16_t / float as approp...
[pulseaudio] / src / pulsecore / iochannel.c
index 01f17ab32db3d8bb48a20679b919f6da45c5f728..dce67340aad075feb2dc6d9ea2faab6e8d37ebdf 100644 (file)
@@ -1,5 +1,3 @@
-/* $Id$ */
-
 /***
   This file is part of PulseAudio.
 
 #endif
 
 #include <stdlib.h>
-#include <fcntl.h>
 #include <unistd.h>
 #include <errno.h>
 
-#ifdef HAVE_SYS_SOCKET_H
-#include <sys/socket.h>
-#endif
 #ifdef HAVE_SYS_UN_H
 #include <sys/un.h>
 #endif
 
-#include "winsock.h"
-
 #include <pulse/xmalloc.h>
 
 #include <pulsecore/core-error.h>
 #include <pulsecore/core-util.h>
+#include <pulsecore/socket.h>
 #include <pulsecore/socket-util.h>
 #include <pulsecore/log.h>
 #include <pulsecore/macro.h>
@@ -58,39 +51,85 @@ struct pa_iochannel {
     pa_iochannel_cb_t callback;
     void*userdata;
 
-    pa_bool_t readable;
-    pa_bool_t writable;
-    pa_bool_t hungup;
-
-    pa_bool_t no_close;
+    bool readable:1;
+    bool writable:1;
+    bool hungup:1;
+    bool no_close:1;
 
     pa_io_event* input_event, *output_event;
 };
 
-static void enable_mainloop_sources(pa_iochannel *io) {
+static void callback(pa_mainloop_api* m, pa_io_event *e, int fd, pa_io_event_flags_t f, void *userdata);
+
+static void delete_events(pa_iochannel *io) {
+    pa_assert(io);
+
+    if (io->input_event)
+        io->mainloop->io_free(io->input_event);
+
+    if (io->output_event && io->output_event != io->input_event)
+        io->mainloop->io_free(io->output_event);
+
+    io->input_event = io->output_event = NULL;
+}
+
+static void enable_events(pa_iochannel *io) {
     pa_assert(io);
 
-    if (io->input_event == io->output_event && io->input_event) {
+    if (io->hungup) {
+        delete_events(io);
+        return;
+    }
+
+    if (io->ifd == io->ofd && io->ifd >= 0) {
         pa_io_event_flags_t f = PA_IO_EVENT_NULL;
-        pa_assert(io->input_event);
 
         if (!io->readable)
             f |= PA_IO_EVENT_INPUT;
         if (!io->writable)
             f |= PA_IO_EVENT_OUTPUT;
 
-        io->mainloop->io_enable(io->input_event, f);
+        pa_assert(io->input_event == io->output_event);
+
+        if (f != PA_IO_EVENT_NULL) {
+            if (io->input_event)
+                io->mainloop->io_enable(io->input_event, f);
+            else
+                io->input_event = io->output_event = io->mainloop->io_new(io->mainloop, io->ifd, f, callback, io);
+        } else
+            delete_events(io);
+
     } else {
-        if (io->input_event)
-            io->mainloop->io_enable(io->input_event, io->readable ? PA_IO_EVENT_NULL : PA_IO_EVENT_INPUT);
-        if (io->output_event)
-            io->mainloop->io_enable(io->output_event, io->writable ? PA_IO_EVENT_NULL : PA_IO_EVENT_OUTPUT);
+
+        if (io->ifd >= 0) {
+            if (!io->readable) {
+                if (io->input_event)
+                    io->mainloop->io_enable(io->input_event, PA_IO_EVENT_INPUT);
+                else
+                    io->input_event = io->mainloop->io_new(io->mainloop, io->ifd, PA_IO_EVENT_INPUT, callback, io);
+            } else if (io->input_event) {
+                io->mainloop->io_free(io->input_event);
+                io->input_event = NULL;
+            }
+        }
+
+        if (io->ofd >= 0) {
+            if (!io->writable) {
+                if (io->output_event)
+                    io->mainloop->io_enable(io->output_event, PA_IO_EVENT_OUTPUT);
+                else
+                    io->output_event = io->mainloop->io_new(io->mainloop, io->ofd, PA_IO_EVENT_OUTPUT, callback, io);
+            } else if (io->output_event) {
+                io->mainloop->io_free(io->output_event);
+                io->output_event = NULL;
+            }
+        }
     }
 }
 
 static void callback(pa_mainloop_api* m, pa_io_event *e, int fd, pa_io_event_flags_t f, void *userdata) {
     pa_iochannel *io = userdata;
-    pa_bool_t changed = FALSE;
+    bool changed = false;
 
     pa_assert(m);
     pa_assert(e);
@@ -98,24 +137,24 @@ static void callback(pa_mainloop_api* m, pa_io_event *e, int fd, pa_io_event_fla
     pa_assert(userdata);
 
     if ((f & (PA_IO_EVENT_HANGUP|PA_IO_EVENT_ERROR)) && !io->hungup) {
-        io->hungup = TRUE;
-        changed = TRUE;
+        io->hungup = true;
+        changed = true;
     }
 
     if ((f & PA_IO_EVENT_INPUT) && !io->readable) {
-        io->readable = TRUE;
-        changed = TRUE;
+        io->readable = true;
+        changed = true;
         pa_assert(e == io->input_event);
     }
 
     if ((f & PA_IO_EVENT_OUTPUT) && !io->writable) {
-        io->writable = TRUE;
-        changed = TRUE;
+        io->writable = true;
+        changed = true;
         pa_assert(e == io->output_event);
     }
 
     if (changed) {
-        enable_mainloop_sources(io);
+        enable_events(io);
 
         if (io->callback)
             io->callback(io, io->userdata);
@@ -128,49 +167,25 @@ pa_iochannel* pa_iochannel_new(pa_mainloop_api*m, int ifd, int ofd) {
     pa_assert(m);
     pa_assert(ifd >= 0 || ofd >= 0);
 
-    io = pa_xnew(pa_iochannel, 1);
+    io = pa_xnew0(pa_iochannel, 1);
     io->ifd = ifd;
     io->ofd = ofd;
-    io->ifd_type = io->ofd_type = 0;
     io->mainloop = m;
 
-    io->userdata = NULL;
-    io->callback = NULL;
-    io->readable = FALSE;
-    io->writable = FALSE;
-    io->hungup = FALSE;
-    io->no_close = FALSE;
-
-    io->input_event = io->output_event = NULL;
-
-    if (ifd == ofd) {
-        pa_assert(ifd >= 0);
+    if (io->ifd >= 0)
         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_fd_nonblock(io->ifd);
-            io->input_event = m->io_new(m, ifd, PA_IO_EVENT_INPUT, callback, io);
-        }
-
-        if (ofd >= 0) {
-            pa_make_fd_nonblock(io->ofd);
-            io->output_event = m->io_new(m, ofd, PA_IO_EVENT_OUTPUT, callback, io);
-        }
-    }
+    if (io->ofd >= 0 && io->ofd != io->ifd)
+        pa_make_fd_nonblock(io->ofd);
 
+    enable_events(io);
     return io;
 }
 
 void pa_iochannel_free(pa_iochannel*io) {
     pa_assert(io);
 
-    if (io->input_event)
-        io->mainloop->io_free(io->input_event);
-
-    if (io->output_event && (io->output_event != io->input_event))
-        io->mainloop->io_free(io->output_event);
+    delete_events(io);
 
     if (!io->no_close) {
         if (io->ifd >= 0)
@@ -182,19 +197,19 @@ void pa_iochannel_free(pa_iochannel*io) {
     pa_xfree(io);
 }
 
-pa_bool_t pa_iochannel_is_readable(pa_iochannel*io) {
+bool pa_iochannel_is_readable(pa_iochannel*io) {
     pa_assert(io);
 
     return io->readable || io->hungup;
 }
 
-pa_bool_t pa_iochannel_is_writable(pa_iochannel*io) {
+bool pa_iochannel_is_writable(pa_iochannel*io) {
     pa_assert(io);
 
     return io->writable && !io->hungup;
 }
 
-pa_bool_t pa_iochannel_is_hungup(pa_iochannel*io) {
+bool pa_iochannel_is_hungup(pa_iochannel*io) {
     pa_assert(io);
 
     return io->hungup;
@@ -208,11 +223,22 @@ ssize_t pa_iochannel_write(pa_iochannel*io, const void*data, size_t l) {
     pa_assert(l);
     pa_assert(io->ofd >= 0);
 
-    if ((r = pa_write(io->ofd, data, l, &io->ofd_type)) >= 0) {
-        io->writable = FALSE;
-        enable_mainloop_sources(io);
+    r = pa_write(io->ofd, data, l, &io->ofd_type);
+
+    if ((size_t) r == l)
+        return r; /* Fast path - we almost always successfully write everything */
+
+    if (r < 0) {
+        if (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK)
+            r = 0;
+        else
+            return r;
     }
 
+    /* Partial write - let's get a notification when we can write more */
+    io->writable = io->hungup = false;
+    enable_events(io);
+
     return r;
 }
 
@@ -224,8 +250,12 @@ ssize_t pa_iochannel_read(pa_iochannel*io, void*data, size_t l) {
     pa_assert(io->ifd >= 0);
 
     if ((r = pa_read(io->ifd, data, l, &io->ifd_type)) >= 0) {
-        io->readable = FALSE;
-        enable_mainloop_sources(io);
+
+        /* We also reset the hangup flag here to ensure that another
+         * IO callback is triggered so that we will again call into
+         * user code */
+        io->readable = io->hungup = false;
+        enable_events(io);
     }
 
     return r;
@@ -233,8 +263,15 @@ ssize_t pa_iochannel_read(pa_iochannel*io, void*data, size_t l) {
 
 #ifdef HAVE_CREDS
 
-pa_bool_t pa_iochannel_creds_supported(pa_iochannel *io) {
-    struct sockaddr_un sa;
+bool pa_iochannel_creds_supported(pa_iochannel *io) {
+    struct {
+        struct sockaddr sa;
+#ifdef HAVE_SYS_UN_H
+        struct sockaddr_un un;
+#endif
+        struct sockaddr_storage storage;
+    } sa;
+
     socklen_t l;
 
     pa_assert(io);
@@ -242,11 +279,10 @@ pa_bool_t pa_iochannel_creds_supported(pa_iochannel *io) {
     pa_assert(io->ofd == io->ifd);
 
     l = sizeof(sa);
+    if (getsockname(io->ifd, &sa.sa, &l) < 0)
+        return false;
 
-    if (getsockname(io->ifd, (struct sockaddr*) &sa, &l) < 0)
-        return 0;
-
-    return sa.sun_family == AF_UNIX;
+    return sa.sa.sa_family == AF_UNIX;
 }
 
 int pa_iochannel_creds_enable(pa_iochannel *io) {
@@ -267,26 +303,27 @@ ssize_t pa_iochannel_write_with_creds(pa_iochannel*io, const void*data, size_t l
     ssize_t r;
     struct msghdr mh;
     struct iovec iov;
-    uint8_t cmsg_data[CMSG_SPACE(sizeof(struct ucred))];
+    union {
+        struct cmsghdr hdr;
+        uint8_t data[CMSG_SPACE(sizeof(struct ucred))];
+    } cmsg;
     struct ucred *u;
-    struct cmsghdr *cmsg;
 
     pa_assert(io);
     pa_assert(data);
     pa_assert(l);
     pa_assert(io->ofd >= 0);
 
-    memset(&iov, 0, sizeof(iov));
+    pa_zero(iov);
     iov.iov_base = (void*) data;
     iov.iov_len = l;
 
-    memset(cmsg_data, 0, sizeof(cmsg_data));
-    cmsg = (struct cmsghdr*)  cmsg_data;
-    cmsg->cmsg_len = CMSG_LEN(sizeof(struct ucred));
-    cmsg->cmsg_level = SOL_SOCKET;
-    cmsg->cmsg_type = SCM_CREDENTIALS;
+    pa_zero(cmsg);
+    cmsg.hdr.cmsg_len = CMSG_LEN(sizeof(struct ucred));
+    cmsg.hdr.cmsg_level = SOL_SOCKET;
+    cmsg.hdr.cmsg_type = SCM_CREDENTIALS;
 
-    u = (struct ucred*) CMSG_DATA(cmsg);
+    u = (struct ucred*) CMSG_DATA(&cmsg.hdr);
 
     u->pid = getpid();
     if (ucred) {
@@ -297,28 +334,28 @@ ssize_t pa_iochannel_write_with_creds(pa_iochannel*io, const void*data, size_t l
         u->gid = getgid();
     }
 
-    memset(&mh, 0, sizeof(mh));
-    mh.msg_name = NULL;
-    mh.msg_namelen = 0;
+    pa_zero(mh);
     mh.msg_iov = &iov;
     mh.msg_iovlen = 1;
-    mh.msg_control = cmsg_data;
-    mh.msg_controllen = sizeof(cmsg_data);
-    mh.msg_flags = 0;
+    mh.msg_control = &cmsg;
+    mh.msg_controllen = sizeof(cmsg);
 
     if ((r = sendmsg(io->ofd, &mh, MSG_NOSIGNAL)) >= 0) {
-        io->writable = FALSE;
-        enable_mainloop_sources(io);
+        io->writable = io->hungup = false;
+        enable_events(io);
     }
 
     return r;
 }
 
-ssize_t pa_iochannel_read_with_creds(pa_iochannel*io, void*data, size_t l, pa_creds *creds, pa_bool_t *creds_valid) {
+ssize_t pa_iochannel_read_with_creds(pa_iochannel*io, void*data, size_t l, pa_creds *creds, bool *creds_valid) {
     ssize_t r;
     struct msghdr mh;
     struct iovec iov;
-    uint8_t cmsg_data[CMSG_SPACE(sizeof(struct ucred))];
+    union {
+        struct cmsghdr hdr;
+        uint8_t data[CMSG_SPACE(sizeof(struct ucred))];
+    } cmsg;
 
     pa_assert(io);
     pa_assert(data);
@@ -327,42 +364,38 @@ ssize_t pa_iochannel_read_with_creds(pa_iochannel*io, void*data, size_t l, pa_cr
     pa_assert(creds);
     pa_assert(creds_valid);
 
-    memset(&iov, 0, sizeof(iov));
+    pa_zero(iov);
     iov.iov_base = data;
     iov.iov_len = l;
 
-    memset(cmsg_data, 0, sizeof(cmsg_data));
-
-    memset(&mh, 0, sizeof(mh));
-    mh.msg_name = NULL;
-    mh.msg_namelen = 0;
+    pa_zero(cmsg);
+    pa_zero(mh);
     mh.msg_iov = &iov;
     mh.msg_iovlen = 1;
-    mh.msg_control = cmsg_data;
-    mh.msg_controllen = sizeof(cmsg_data);
-    mh.msg_flags = 0;
+    mh.msg_control = &cmsg;
+    mh.msg_controllen = sizeof(cmsg);
 
     if ((r = recvmsg(io->ifd, &mh, 0)) >= 0) {
-        struct cmsghdr *cmsg;
+        struct cmsghdr *cmh;
 
-        *creds_valid = 0;
+        *creds_valid = false;
 
-        for (cmsg = CMSG_FIRSTHDR(&mh); cmsg; cmsg = CMSG_NXTHDR(&mh, cmsg)) {
+        for (cmh = CMSG_FIRSTHDR(&mh); cmh; cmh = CMSG_NXTHDR(&mh, cmh)) {
 
-            if (cmsg->cmsg_level == SOL_SOCKET && cmsg->cmsg_type == SCM_CREDENTIALS) {
+            if (cmh->cmsg_level == SOL_SOCKET && cmh->cmsg_type == SCM_CREDENTIALS) {
                 struct ucred u;
-                pa_assert(cmsg->cmsg_len == CMSG_LEN(sizeof(struct ucred)));
-                memcpy(&u, CMSG_DATA(cmsg), sizeof(struct ucred));
+                pa_assert(cmh->cmsg_len == CMSG_LEN(sizeof(struct ucred)));
+                memcpy(&u, CMSG_DATA(cmh), sizeof(struct ucred));
 
                 creds->gid = u.gid;
                 creds->uid = u.uid;
-                *creds_valid = TRUE;
+                *creds_valid = true;
                 break;
             }
         }
 
-        io->readable = FALSE;
-        enable_mainloop_sources(io);
+        io->readable = io->hungup = false;
+        enable_events(io);
     }
 
     return r;
@@ -377,7 +410,7 @@ void pa_iochannel_set_callback(pa_iochannel*io, pa_iochannel_cb_t _callback, voi
     io->userdata = userdata;
 }
 
-void pa_iochannel_set_noclose(pa_iochannel*io, pa_bool_t b) {
+void pa_iochannel_set_noclose(pa_iochannel*io, bool b) {
     pa_assert(io);
 
     io->no_close = !!b;
@@ -420,3 +453,16 @@ int pa_iochannel_get_send_fd(pa_iochannel *io) {
 
     return io->ofd;
 }
+
+bool pa_iochannel_socket_is_local(pa_iochannel *io) {
+    pa_assert(io);
+
+    if (pa_socket_is_local(io->ifd))
+        return true;
+
+    if (io->ifd != io->ofd)
+        if (pa_socket_is_local(io->ofd))
+            return true;
+
+    return false;
+}