]> code.delx.au - pulseaudio/blobdiff - polyp/polyplib-context.c
Make the whole stuff LGPL only
[pulseaudio] / polyp / polyplib-context.c
index caaa1dbb9b28261395f64beadac38062483d5e60..b15dd8e7d496603b80a209759d26d7278ed9e115 100644 (file)
@@ -4,7 +4,7 @@
   This file is part of polypaudio.
  
   polypaudio is free software; you can redistribute it and/or modify
-  it under the terms of the GNU General Public License as published
+  it under the terms of the GNU Lesser General Public License as published
   by the Free Software Foundation; either version 2 of the License,
   or (at your option) any later version.
  
@@ -13,7 +13,7 @@
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
   General Public License for more details.
  
-  You should have received a copy of the GNU General Public License
+  You should have received a copy of the GNU Lesser General Public License
   along with polypaudio; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
   USA.
@@ -34,6 +34,7 @@
 #include <sys/stat.h>
 #include <errno.h>
 #include <sys/wait.h>
+#include <signal.h>
 
 #include "polyplib-internal.h"
 #include "polyplib-context.h"
 #include "dynarray.h"
 #include "socket-client.h"
 #include "pstream-util.h"
-#include "authkey.h"
 #include "util.h"
 #include "xmalloc.h"
 #include "log.h"
+#include "client-conf.h"
+#include "socket-util.h"
+
+#ifdef HAVE_X11
+#include "client-conf-x11.h"
+#endif
+
+#define AUTOSPAWN_LOCK "autospawn.lock"
+
+
 
 static const struct pa_pdispatch_command command_table[PA_COMMAND_MAX] = {
     [PA_COMMAND_REQUEST] = { pa_command_request },
@@ -55,6 +65,16 @@ static const struct pa_pdispatch_command command_table[PA_COMMAND_MAX] = {
     [PA_COMMAND_SUBSCRIBE_EVENT] = { pa_command_subscribe_event },
 };
 
+static void unlock_autospawn_lock_file(struct pa_context *c) {
+    assert(c);
+    
+    if (c->autospawn_lock_fd >= 0) {
+        pa_unlock_lockfile(c->autospawn_lock_fd);
+        c->autospawn_lock_fd = -1;
+    }
+    
+}
+
 struct pa_context *pa_context_new(struct pa_mainloop_api *mainloop, const char *name) {
     struct pa_context *c;
     assert(mainloop && name);
@@ -84,14 +104,29 @@ struct pa_context *pa_context_new(struct pa_mainloop_api *mainloop, const char *
     c->subscribe_userdata = NULL;
 
     c->memblock_stat = pa_memblock_stat_new();
+    c->local = -1;
+    c->server_list = NULL;
+    c->autospawn_lock_fd = -1;
+    memset(&c->spawn_api, 0, sizeof(c->spawn_api));
+    c->do_autospawn = 0;
+    
+    pa_check_signal_is_blocked(SIGPIPE);
+
+    c->conf = pa_client_conf_new();
+    pa_client_conf_load(c->conf, NULL);
+#ifdef HAVE_X11
+    pa_client_conf_from_x11(c->conf, NULL);
+#endif
+    pa_client_conf_env(c->conf);
     
-    pa_check_for_sigpipe();
     return c;
 }
 
 static void context_free(struct pa_context *c) {
     assert(c);
 
+    unlock_autospawn_lock_file(c);
+
     while (c->operations)
         pa_operation_cancel(c->operations);
 
@@ -113,6 +148,11 @@ static void context_free(struct pa_context *c) {
         pa_dynarray_free(c->playback_streams, NULL, NULL);
 
     pa_memblock_stat_unref(c->memblock_stat);
+
+    if (c->conf)
+        pa_client_conf_free(c->conf);
+
+    pa_strlist_free(c->server_list);
     
     pa_xfree(c->name);
     pa_xfree(c);
@@ -206,17 +246,21 @@ static void pstream_memblock_callback(struct pa_pstream *p, uint32_t channel, ui
     pa_context_ref(c);
     
     if ((s = pa_dynarray_get(c->record_streams, channel))) {
-        if (s->read_callback)
+        if (s->read_callback) {
             s->read_callback(s, (uint8_t*) chunk->memblock->data + chunk->index, chunk->length, s->read_userdata);
+            s->counter += chunk->length;
+        }
     }
 
     pa_context_unref(c);
 }
 
 int pa_context_handle_error(struct pa_context *c, uint32_t command, struct pa_tagstruct *t) {
-    assert(c && t);
+    assert(c);
 
     if (command == PA_COMMAND_ERROR) {
+        assert(t);
+        
         if (pa_tagstruct_getu32(t, &c->error) < 0) {
             pa_context_fail(c, PA_ERROR_PROTOCOL);
             return -1;
@@ -252,7 +296,7 @@ static void setup_complete_callback(struct pa_pdispatch *pd, uint32_t command, u
             struct pa_tagstruct *t;
             t = pa_tagstruct_new(NULL, 0);
             assert(t);
-            pa_tagstruct_putu32(t, PA_COMMAND_SET_NAME);
+            pa_tagstruct_putu32(t, PA_COMMAND_SET_CLIENT_NAME);
             pa_tagstruct_putu32(t, tag = c->ctag++);
             pa_tagstruct_puts(t, c->name);
             pa_pstream_send_tagstruct(c->pstream, t);
@@ -293,7 +337,7 @@ static void setup_context(struct pa_context *c, struct pa_iochannel *io) {
     c->pdispatch = pa_pdispatch_new(c->mainloop, command_table, PA_COMMAND_MAX);
     assert(c->pdispatch);
 
-    if (pa_authkey_load_from_home(PA_NATIVE_COOKIE_FILE, c->auth_cookie, sizeof(c->auth_cookie)) < 0) {
+    if (!c->conf->cookie_valid) {
         pa_context_fail(c, PA_ERROR_AUTHKEY);
         goto finish;
     }
@@ -302,7 +346,7 @@ static void setup_context(struct pa_context *c, struct pa_iochannel *io) {
     assert(t);
     pa_tagstruct_putu32(t, PA_COMMAND_AUTH);
     pa_tagstruct_putu32(t, tag = c->ctag++);
-    pa_tagstruct_put_arbitrary(t, c->auth_cookie, sizeof(c->auth_cookie));
+    pa_tagstruct_put_arbitrary(t, c->conf->cookie, sizeof(c->conf->cookie));
     pa_pstream_send_tagstruct(c->pstream, t);
     pa_pdispatch_register_reply(c->pdispatch, tag, DEFAULT_TIMEOUT, setup_complete_callback, c);
 
@@ -313,93 +357,237 @@ finish:
     pa_context_unref(c);
 }
 
-static void on_connection(struct pa_socket_client *client, struct pa_iochannel*io, void *userdata) {
-    struct pa_context *c = userdata;
-    assert(client && c && c->state == PA_CONTEXT_CONNECTING);
+static void on_connection(struct pa_socket_client *client, struct pa_iochannel*io, void *userdata);
+
+static int context_connect_spawn(struct pa_context *c) {
+    pid_t pid;
+    int status, r;
+    int fds[2] = { -1, -1} ;
+    struct pa_iochannel *io;
 
     pa_context_ref(c);
     
-    pa_socket_client_unref(client);
-    c->client = NULL;
+    if (socketpair(AF_UNIX, SOCK_STREAM, 0, fds) < 0) {
+        pa_log(__FILE__": socketpair() failed: %s\n", strerror(errno));
+        pa_context_fail(c, PA_ERROR_INTERNAL);
+        goto fail;
+    }
 
-    if (!io) {
+    pa_fd_set_cloexec(fds[0], 1);
+    
+    pa_socket_low_delay(fds[0]);
+    pa_socket_low_delay(fds[1]);
+
+    if (c->spawn_api.prefork)
+        c->spawn_api.prefork();
+
+    if ((pid = fork()) < 0) {
+        pa_log(__FILE__": fork() failed: %s\n", strerror(errno));
+        pa_context_fail(c, PA_ERROR_INTERNAL);
+
+        if (c->spawn_api.postfork)
+            c->spawn_api.postfork();
+        
+        goto fail;
+    } else if (!pid) {
+        /* Child */
+
+        char t[128];
+        const char *state = NULL;
+#define MAX_ARGS 64
+        char *argv[MAX_ARGS+1];
+        int n;
+
+        /* Not required, since fds[0] has CLOEXEC enabled anyway */
+        close(fds[0]);
+        
+        if (c->spawn_api.atfork)
+            c->spawn_api.atfork();
+
+        /* Setup argv */
+
+        n = 0;
+        
+        argv[n++] = c->conf->daemon_binary;
+        argv[n++] = "--daemonize=yes";
+        
+        snprintf(t, sizeof(t), "-Lmodule-native-protocol-fd fd=%i", fds[1]);
+        argv[n++] = strdup(t);
+
+        while (n < MAX_ARGS) {
+            char *a;
+
+            if (!(a = pa_split_spaces(c->conf->extra_arguments, &state)))
+                break;
+            
+            argv[n++] = a;
+        }
+
+        argv[n++] = NULL;
+
+        execv(argv[0], argv);
+        _exit(1);
+#undef MAX_ARGS
+    } 
+
+    /* Parent */
+
+    r = waitpid(pid, &status, 0);
+
+    if (c->spawn_api.postfork)
+        c->spawn_api.postfork();
+        
+    if (r < 0) {
+        pa_log(__FILE__": waitpid() failed: %s\n", strerror(errno));
+        pa_context_fail(c, PA_ERROR_INTERNAL);
+        goto fail;
+    } else if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
         pa_context_fail(c, PA_ERROR_CONNECTIONREFUSED);
-        goto finish;
+        goto fail;
     }
 
+    close(fds[1]);
+
+    c->local = 1;
+    
+    io = pa_iochannel_new(c->mainloop, fds[0], fds[0]);
+
     setup_context(c, io);
+    unlock_autospawn_lock_file(c);
+
+    pa_context_unref(c);
+
+    return 0;
+
+fail:
+    if (fds[0] != -1)
+        close(fds[0]);
+    if (fds[1] != -1)
+        close(fds[1]);
+
+    unlock_autospawn_lock_file(c);
 
-finish:
     pa_context_unref(c);
+
+    return -1;
 }
 
-static struct sockaddr *resolve_server(const char *server, size_t *len) {
-    struct sockaddr *sa;
-    struct addrinfo hints, *result = NULL;
-    char *port;
-    assert(server && len);
+static int try_next_connection(struct pa_context *c) {
+    char *u = NULL;
+    int r = -1;
+    assert(c && !c->client);
 
-    if ((port = strrchr(server, ':')))
-        port++;
-    if (!port)
-        port = DEFAULT_PORT;
+    for (;;) {
+        if (u)
+            pa_xfree(u);
+        u = NULL;
+        
+        c->server_list = pa_strlist_pop(c->server_list, &u);
+        
+        if (!u) {
 
-    memset(&hints, 0, sizeof(hints));
-    hints.ai_family = PF_UNSPEC;
-    hints.ai_socktype = SOCK_STREAM;
-    hints.ai_protocol = 0;
+            if (c->do_autospawn) {
+                r = context_connect_spawn(c);
+                goto finish;
+            }
+            
+            pa_context_fail(c, PA_ERROR_CONNECTIONREFUSED);
+            goto finish;
+        }
+        
+/*          pa_log(__FILE__": Trying to connect to %s...\n", u);  */
+        
+        if (!(c->client = pa_socket_client_new_string(c->mainloop, u, PA_NATIVE_DEFAULT_PORT)))
+            continue;
+        
+        c->local = pa_socket_client_is_local(c->client);
+        pa_socket_client_set_callback(c->client, on_connection, c);
+        break;
+    }
 
-    if (getaddrinfo(server, port, &hints, &result) != 0)
-        return NULL;
-    assert(result);
+    r = 0;
+
+finish:
+    if (u)
+        pa_xfree(u);
     
-    sa = pa_xmalloc(*len = result->ai_addrlen);
-    memcpy(sa, result->ai_addr, *len);
+    return r;
+}
+
+static void on_connection(struct pa_socket_client *client, struct pa_iochannel*io, void *userdata) {
+    struct pa_context *c = userdata;
+    assert(client && c && c->state == PA_CONTEXT_CONNECTING);
 
-    freeaddrinfo(result);
+    pa_context_ref(c);
     
-    return sa;
+    pa_socket_client_unref(client);
+    c->client = NULL;
+
+    if (!io) {
+        /* Try the item in the list */
+        if (errno == ECONNREFUSED || errno == ETIMEDOUT || errno == EHOSTUNREACH) {
+            try_next_connection(c);
+            goto finish;
+        }
+
+        pa_context_fail(c, PA_ERROR_CONNECTIONREFUSED);
+        goto finish;
+    }
+
+    unlock_autospawn_lock_file(c);
+    setup_context(c, io);
+
+finish:
+    pa_context_unref(c);
 }
 
-int pa_context_connect(struct pa_context *c, const char *server) {
+int pa_context_connect(struct pa_context *c, const char *server, int spawn, const struct pa_spawn_api *api) {
     int r = -1;
     assert(c && c->ref >= 1 && c->state == PA_CONTEXT_UNCONNECTED);
 
-    pa_context_ref(c);
-    
     if (!server)
-        if (!(server = getenv(ENV_DEFAULT_SERVER)))
-            server = DEFAULT_SERVER;
+        server = c->conf->default_server;
+
 
-    assert(!c->client);
+    pa_context_ref(c);
+
+    assert(!c->server_list);
     
-    if (*server == '/') {
-        if (!(c->client = pa_socket_client_new_unix(c->mainloop, server))) {
-            pa_context_fail(c, PA_ERROR_CONNECTIONREFUSED);
+    if (server) {
+        if (!(c->server_list = pa_strlist_parse(server))) {
+            pa_context_fail(c, PA_ERROR_INVALIDSERVER);
             goto finish;
         }
     } else {
-        struct sockaddr* sa;
-        size_t sa_len;
+        char *d;
+        char ufn[PATH_MAX];
 
-        if (!(sa = resolve_server(server, &sa_len))) {
-            pa_context_fail(c, PA_ERROR_INVALIDSERVER);
-            goto finish;
-        }
+        /* Prepend in reverse order */
+        
+        if ((d = getenv("DISPLAY")))
+            c->server_list = pa_strlist_prepend(c->server_list, d);
+        
+        c->server_list = pa_strlist_prepend(c->server_list, "tcp6:localhost");
+        c->server_list = pa_strlist_prepend(c->server_list, "localhost");
+        c->server_list = pa_strlist_prepend(c->server_list, pa_runtime_path(PA_NATIVE_DEFAULT_UNIX_SOCKET, ufn, sizeof(ufn)));
 
-        c->client = pa_socket_client_new_sockaddr(c->mainloop, sa, sa_len);
-        pa_xfree(sa);
+        /* Wrap the connection attempts in a single transaction for sane autospwan locking */
+        if (spawn && c->conf->autospawn) {
+            char lf[PATH_MAX];
 
-        if (!c->client) {
-            pa_context_fail(c, PA_ERROR_CONNECTIONREFUSED);
-            goto finish;
+            pa_runtime_path(AUTOSPAWN_LOCK, lf, sizeof(lf));
+            assert(c->autospawn_lock_fd <= 0);
+            c->autospawn_lock_fd = pa_lock_lockfile(lf);
+
+            if (api)
+                c->spawn_api = *api;
+            c->do_autospawn = 1;
         }
+
     }
 
-    pa_socket_client_set_callback(c->client, on_connection, c);
     pa_context_set_state(c, PA_CONTEXT_CONNECTING);
-
-    r = 0;
+    r = try_next_connection(c);
     
 finish:
     pa_context_unref(c);
@@ -431,11 +619,10 @@ void pa_context_set_state_callback(struct pa_context *c, void (*cb)(struct pa_co
 int pa_context_is_pending(struct pa_context *c) {
     assert(c && c->ref >= 1);
 
-    if (c->state != PA_CONTEXT_READY)
-        return 0;
-
-    assert(c->pstream && c->pdispatch);
-    return pa_pstream_is_pending(c->pstream) || pa_pdispatch_is_pending(c->pdispatch);
+/*     pa_log("pstream: %i\n", pa_pstream_is_pending(c->pstream)); */
+/*     pa_log("pdispatch: %i\n", pa_pdispatch_is_pending(c->pdispatch)); */
+    
+    return (c->pstream && pa_pstream_is_pending(c->pstream)) || (c->pdispatch && pa_pdispatch_is_pending(c->pdispatch)) || c->client;
 }
 
 static void set_dispatch_callbacks(struct pa_operation *o);
@@ -555,98 +742,6 @@ struct pa_operation* pa_context_send_simple_command(struct pa_context *c, uint32
     return pa_operation_ref(o);
 }
 
-const char* pa_get_library_version(void) {
-    return PACKAGE_VERSION;
-}
-
-static int is_running(void) {
-    struct stat st;
-    
-    if (DEFAULT_SERVER[0] != '/')
-        return 1;
-
-    if (stat(DEFAULT_SERVER, &st) < 0)
-        return 0;
-
-    return 1;
-}
-
-int pa_context_connect_spawn(struct pa_context *c, void (*atfork)(void), void (*prefork)(void), void (*postfork)(void)) {
-    pid_t pid;
-    int status, r;
-    int fds[2] = { -1, -1} ;
-    struct pa_iochannel *io;
-    
-    if (getenv(ENV_DEFAULT_SERVER) || is_running())
-        return pa_context_connect(c, NULL);
-
-    if (socketpair(AF_UNIX, SOCK_STREAM, 0, fds) < 0) {
-        pa_log(__FILE__": socketpair() failed: %s\n", strerror(errno));
-        pa_context_fail(c, PA_ERROR_INTERNAL);
-        goto fail;
-    }
-
-    if (prefork)
-        prefork();
-
-    if ((pid = fork()) < 0) {
-        pa_log(__FILE__": fork() failed: %s\n", strerror(errno));
-        pa_context_fail(c, PA_ERROR_INTERNAL);
-
-        if (postfork)
-            postfork();
-        
-        goto fail;
-    } else if (!pid) {
-        char t[64];
-        char *p;
-        /* Child */
-
-        close(fds[0]);
-        
-        if (atfork)
-            atfork();
-
-        if (!(p = getenv(ENV_DEFAULT_BINARY)))
-            p = POLYPAUDIO_BINARY;
-        
-        snprintf(t, sizeof(t), "-Lmodule-native-protocol-fd fd=%i", fds[1]);
-        execl(p, p, "-r", "-D", "-lsyslog", "-X 5", t, NULL);
-        
-        exit(1);
-    } 
-
-    /* Parent */
-
-    r = waitpid(pid, &status, 0);
-
-    if (postfork)
-        postfork();
-        
-    if (r < 0) {
-        pa_log(__FILE__": waitpid() failed: %s\n", strerror(errno));
-        pa_context_fail(c, PA_ERROR_INTERNAL);
-        goto fail;
-    } else if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
-        pa_context_fail(c, PA_ERROR_CONNECTIONREFUSED);
-        goto fail;
-    }
-
-    close(fds[1]);
-    
-    io = pa_iochannel_new(c->mainloop, fds[0], fds[0]);
-    setup_context(c, io);
-    return 0;
-
-fail:
-    if (fds[0] != -1)
-        close(fds[0]);
-    if (fds[1] != -1)
-        close(fds[1]);
-
-    return -1;
-}
-
 struct pa_operation* pa_context_set_default_sink(struct pa_context *c, const char *name, void(*cb)(struct pa_context*c, int success, void *userdata), void *userdata) {
     struct pa_tagstruct *t;
     struct pa_operation *o;
@@ -686,3 +781,33 @@ struct pa_operation* pa_context_set_default_source(struct pa_context *c, const c
 
     return pa_operation_ref(o);
 }
+
+int pa_context_is_local(struct pa_context *c) {
+    assert(c);
+    return c->local;
+}
+
+struct pa_operation* pa_context_set_name(struct pa_context *c, const char *name, void(*cb)(struct pa_context*c, int success,  void *userdata), void *userdata) {
+    struct pa_tagstruct *t;
+    struct pa_operation *o;
+    uint32_t tag;
+    assert(c && name && cb);
+
+    o = pa_operation_new(c, NULL);
+    o->callback = cb;
+    o->userdata = userdata;
+
+    t = pa_tagstruct_new(NULL, 0);
+    pa_tagstruct_putu32(t, PA_COMMAND_SET_CLIENT_NAME);
+    pa_tagstruct_putu32(t, tag = c->ctag++);
+    pa_tagstruct_puts(t, name);
+    pa_pstream_send_tagstruct(c->pstream, t);
+    pa_pdispatch_register_reply(c->pdispatch, tag, DEFAULT_TIMEOUT,  pa_context_simple_ack_callback, o);
+
+    return pa_operation_ref(o);
+}
+
+const char* pa_get_library_version(void) {
+    return PACKAGE_VERSION;
+}
+