]> code.delx.au - pulseaudio/blobdiff - src/modules/module-protocol-stub.c
* split pa_cstrerror() into its own file polypcore/core-error.[ch]
[pulseaudio] / src / modules / module-protocol-stub.c
index 71e17cbcff359e5c7ad6a654576fd9e5f4c8f33f..2b4f303b111bff012f9b7b25490e951f31b6710d 100644 (file)
 
 #include "../polypcore/winsock.h"
 
+#include <polyp/xmalloc.h>
+
+#include <polypcore/core-error.h>
 #include <polypcore/module.h>
 #include <polypcore/socket-server.h>
 #include <polypcore/socket-util.h>
-#include <polypcore/util.h>
+#include <polypcore/core-util.h>
 #include <polypcore/modargs.h>
 #include <polypcore/log.h>
 #include <polypcore/native-common.h>
-#include <polypcore/xmalloc.h>
 
 #ifdef USE_TCP_SOCKETS
 #define SOCKET_DESCRIPTION "(TCP sockets)"
@@ -169,6 +171,7 @@ struct userdata {
     void *protocol_ipv6;
 #else
     void *protocol_unix;
+    char *socket_path;
 #endif
 };
 
@@ -179,8 +182,7 @@ int pa__init(pa_core *c, pa_module*m) {
     struct userdata *u = NULL;
 
 #if defined(USE_TCP_SOCKETS)
-    pa_socket_server *s_ipv4 = NULL;
-    pa_socket_server *s_ipv6 = NULL;
+    pa_socket_server *s_ipv4 = NULL, *s_ipv6 = NULL;
     int loopback = 1;
     uint32_t port = IPV4_PORT;
     const char *listen_on;
@@ -193,12 +195,14 @@ int pa__init(pa_core *c, pa_module*m) {
 
     assert(c && m);
 
-#if defined(USE_TCP_SOCKETS)
     if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
         pa_log(__FILE__": Failed to parse module arguments");
         goto finish;
     }
 
+    u = pa_xnew0(struct userdata, 1);
+
+#if defined(USE_TCP_SOCKETS)
     if (pa_modargs_get_value_boolean(ma, "loopback", &loopback) < 0) {
         pa_log(__FILE__": loopback= expects a boolean argument.");
         goto fail;
@@ -212,26 +216,34 @@ int pa__init(pa_core *c, pa_module*m) {
     listen_on = pa_modargs_get_value(ma, "listen", NULL);
 
     if (listen_on) {
-        s_ipv4 = pa_socket_server_new_ipv4_string(c->mainloop, listen_on, port, TCPWRAP_SERVICE);
         s_ipv6 = pa_socket_server_new_ipv6_string(c->mainloop, listen_on, port, TCPWRAP_SERVICE);
-        if (!s_ipv4 && !s_ipv6)
-            goto fail;
+        s_ipv4 = pa_socket_server_new_ipv4_string(c->mainloop, listen_on, port, TCPWRAP_SERVICE);
     } else if (loopback) {
-        s_ipv4 = pa_socket_server_new_ipv4_loopback(c->mainloop, port, TCPWRAP_SERVICE);
         s_ipv6 = pa_socket_server_new_ipv6_loopback(c->mainloop, port, TCPWRAP_SERVICE);
-        if (!s_ipv4 && !s_ipv6)
-            goto fail;
+        s_ipv4 = pa_socket_server_new_ipv4_loopback(c->mainloop, port, TCPWRAP_SERVICE);
     } else {
-        s_ipv4 = pa_socket_server_new_ipv4_any(c->mainloop, port, TCPWRAP_SERVICE);
         s_ipv6 = pa_socket_server_new_ipv6_any(c->mainloop, port, TCPWRAP_SERVICE);
-        if (!s_ipv4 && !s_ipv6)
-            goto fail;
+        s_ipv4 = pa_socket_server_new_ipv4_any(c->mainloop, port, TCPWRAP_SERVICE);
     }
+
+    if (!s_ipv4 && !s_ipv6)
+        goto fail;
+
+    if (s_ipv4)
+        if (!(u->protocol_ipv4 = protocol_new(c, s_ipv4, m, ma)))
+            pa_socket_server_unref(s_ipv4);
+
+    if (s_ipv6)
+        if (!(u->protocol_ipv6 = protocol_new(c, s_ipv6, m, ma)))
+            pa_socket_server_unref(s_ipv6);
+
+    if (!u->protocol_ipv4 && !u->protocol_ipv6)
+        goto fail;
+
 #else
     v = pa_modargs_get_value(ma, "socket", UNIX_SOCKET);
-    assert(v);
-
     pa_runtime_path(v, tmp, sizeof(tmp));
+    u->socket_path = pa_xstrdup(tmp);
 
     if (pa_make_secure_parent_dir(tmp) < 0) {
         pa_log(__FILE__": Failed to create secure socket directory.");
@@ -239,7 +251,7 @@ int pa__init(pa_core *c, pa_module*m) {
     }
 
     if ((r = pa_unix_socket_remove_stale(tmp)) < 0) {
-        pa_log(__FILE__": Failed to remove stale UNIX socket '%s': %s", tmp, strerror(errno));
+        pa_log(__FILE__": Failed to remove stale UNIX socket '%s': %s", tmp, pa_cstrerror(errno));
         goto fail;
     }
     
@@ -248,30 +260,14 @@ int pa__init(pa_core *c, pa_module*m) {
     
     if (!(s = pa_socket_server_new_unix(c->mainloop, tmp)))
         goto fail;
-#endif
-
-    u = pa_xmalloc0(sizeof(struct userdata));
 
-#if defined(USE_TCP_SOCKETS)
-    if (s_ipv4) {
-        u->protocol_ipv4 = protocol_new(c, s_ipv4, m, ma);
-        if (!u->protocol_ipv4)
-            pa_socket_server_unref(s_ipv4);
-    }
-
-    if (s_ipv6) {
-        u->protocol_ipv6 = protocol_new(c, s_ipv4, m, ma);
-        if (!u->protocol_ipv6)
-            pa_socket_server_unref(s_ipv6);
-    }
-
-    if (!u->protocol_ipv4 && !u->protocol_ipv6)
-        goto fail;
-#else
     if (!(u->protocol_unix = protocol_new(c, s, m, ma)))
         goto fail;
+
 #endif
 
+    m->userdata = u;
+
     ret = 0;
 
 finish:
@@ -290,7 +286,11 @@ fail:
 #else
         if (u->protocol_unix)
             protocol_free(u->protocol_unix);
+
+        if (u->socket_path)
+            pa_xfree(u->socket_path);
 #endif
+
         pa_xfree(u);
     } else {
 #if defined(USE_TCP_SOCKETS)
@@ -309,17 +309,11 @@ fail:
 
 void pa__done(pa_core *c, pa_module*m) {
     struct userdata *u;
-    assert(c && m);
-
-#if defined(USE_PROTOCOL_ESOUND) && !defined(USE_TCP_SOCKETS)
-       if (remove(ESD_UNIX_SOCKET_NAME) != 0)
-               pa_log("%s: Failed to remove %s : %s.", __FILE__, ESD_UNIX_SOCKET_NAME, strerror (errno));
-       if (remove(ESD_UNIX_SOCKET_DIR) != 0)
-               pa_log("%s: Failed to remove %s : %s.", __FILE__, ESD_UNIX_SOCKET_DIR, strerror (errno));
-#endif
+    
+    assert(c);
+    assert(m);
 
     u = m->userdata;
-    assert(u);
 
 #if defined(USE_TCP_SOCKETS)
     if (u->protocol_ipv4)
@@ -329,6 +323,19 @@ void pa__done(pa_core *c, pa_module*m) {
 #else
     if (u->protocol_unix)
         protocol_free(u->protocol_unix);
+
+    if (u->socket_path) {
+        char *p;
+        
+        if ((p = pa_parent_dir(u->socket_path))) {
+            if (rmdir(p) < 0 && errno != ENOENT && errno != ENOTEMPTY)
+                pa_log(__FILE__": Failed to remove %s: %s.", u->socket_path, pa_cstrerror(errno));
+
+            pa_xfree(p);
+        }
+
+        pa_xfree(u->socket_path);
+    }
 #endif
 
     pa_xfree(u);