]> code.delx.au - pulseaudio/commitdiff
Add module-tunnel
authorLennart Poettering <lennart@poettering.net>
Tue, 28 Sep 2004 22:47:48 +0000 (22:47 +0000)
committerLennart Poettering <lennart@poettering.net>
Tue, 28 Sep 2004 22:47:48 +0000 (22:47 +0000)
add proper locking when autospawning a daemon

git-svn-id: file:///home/lennart/svn/public/pulseaudio/trunk@245 fefdeb5f-60dc-0310-8127-8f9354f1896f

14 files changed:
doc/todo
polyp/Makefile.am
polyp/authkey.c
polyp/module-match.c
polyp/module-protocol-stub.c
polyp/module-tunnel.c [new file with mode: 0644]
polyp/native-common.h
polyp/polyplib-context.c
polyp/polyplib-internal.h
polyp/sink.h
polyp/socket-util.c
polyp/socket-util.h
polyp/util.c
polyp/util.h

index eb8ebfb3f33f0e12fa6acf30bde393c0fa938a94..ad272ae4c63df0852af3ef97db342549a14ca0f7 100644 (file)
--- a/doc/todo
+++ b/doc/todo
@@ -7,12 +7,12 @@
 - make mcalign merge chunks
 - option to use default fragment size on alsa drivers
 - improve module-oss-mmap latency measurement
-- proper locking around native protocol socket
 - pacat sample type args
 - filter capture data in client through alignment
 - add redirection module
 - add radio module
 - make autoload list use idxset
+- libwrap
 
 ** later ***
 - xmlrpc/http
index d73c27bcf8c36e9ff6af13344924d62961d536ea..9f6fa1d52c44463cac0a714b63cc4d6c55806246 100644 (file)
@@ -106,7 +106,8 @@ modlib_LTLIBRARIES= \
                module-combine.la \
                module-esound-compat-spawnfd.la \
                module-esound-compat-spawnpid.la \
-               module-match.la
+               module-match.la \
+               module-tunnel.la
 
 lib_LTLIBRARIES= \
                libpolyp-@PA_MAJORMINOR@.la \
@@ -310,6 +311,10 @@ module_match_la_SOURCES = module-match.c
 module_match_la_LDFLAGS = -module -avoid-version
 module_match_la_LIBADD = $(AM_LIBADD)
 
+module_tunnel_la_SOURCES = module-tunnel.c
+module_tunnel_la_LDFLAGS = -module -avoid-version
+module_tunnel_la_LIBADD = $(AM_LIBADD) libsocket-client.la libpstream.la libpstream-util.la libpdispatch.la libtagstruct.la libauthkey.la
+
 module_esound_compat_spawnfd_la_SOURCES = module-esound-compat-spawnfd.c
 module_esound_compat_spawnfd_la_LDFLAGS = -module -avoid-version
 module_esound_compat_spawnfd_la_LIBADD = $(AM_LIBADD)
index 1e2edb07dbe4a360c54abed75c043b0e8b16af3c..c8d8576b95ca7ca3333cdbcdb9025cfda622c17f 100644 (file)
@@ -97,7 +97,7 @@ static int load(const char *fn, void *data, size_t length) {
             writable = 0;
     }
 
-    unlock = pa_lock_file(fd, 1) >= 0;
+    unlock = pa_lock_fd(fd, 1) >= 0;
 
     if ((r = pa_loop_read(fd, data, length)) < 0) {
         pa_log(__FILE__": failed to read cookie file '%s'\n", fn);
@@ -122,7 +122,7 @@ finish:
     if (fd >= 0) {
         
         if (unlock)
-            pa_lock_file(fd, 0);
+            pa_lock_fd(fd, 0);
         
         close(fd);
     }
@@ -147,15 +147,19 @@ int pa_authkey_load(const char *path, void *data, size_t length) {
 int pa_authkey_load_from_home(const char *fn, void *data, size_t length) {
     char *home;
     char path[PATH_MAX];
+    char *p;
 
     assert(fn && data && length);
-    
-    if (!(home = getenv("HOME")))
-        return -2;
-    
-    snprintf(path, sizeof(path), "%s/%s", home, fn);
 
-    return pa_authkey_load(path, data, length);
+    if (fn[0] != '/') {
+        if (!(home = getenv("HOME")))
+            return -2;
+        
+        snprintf(p = path, sizeof(path), "%s/%s", home, fn);
+    } else
+        p = fn;
+        
+    return pa_authkey_load(p, data, length);
 }
 
 int pa_authkey_load_auto(const char *fn, void *data, size_t length) {
index 964ff2fd73fb1af0ad39ab8e4ad5fe03bca35792..380c6011a6b8a9c0c716d59818ccfd9bd5e1907a 100644 (file)
@@ -164,7 +164,6 @@ static void callback(struct pa_core *c, enum pa_subscription_event_type t, uint3
 
 int pa__init(struct pa_core *c, struct pa_module*m) {
     struct pa_modargs *ma = NULL;
-    int ret = -1;
     const char *table_file;
     struct userdata *u;
     assert(c && m);
@@ -172,25 +171,28 @@ int pa__init(struct pa_core *c, struct pa_module*m) {
     if (!(ma = pa_modargs_new(m->argument, valid_modargs)) ||
         !(table_file = pa_modargs_get_value(ma, "table", NULL))) {
         pa_log(__FILE__": Failed to parse module arguments\n");
-        goto finish;
+        goto fail;
     }
 
     u = pa_xmalloc(sizeof(struct userdata));
     u->rules = NULL;
     u->subscription = NULL;
+    m->userdata = u;
     
     if (load_rules(u, table_file) < 0)
-        goto finish;
+        goto fail;
 
     u->subscription = pa_subscription_new(c, PA_SUBSCRIPTION_MASK_SINK_INPUT, callback, u);
-    
-    ret = 0;
 
-finish:
+    pa_modargs_free(ma);
+    return 0;
+
+fail:
+    pa__done(c, m);
+
     if (ma)
         pa_modargs_free(ma);
-
-    return ret;
+    return  -1;
 }
 
 void pa__done(struct pa_core *c, struct pa_module*m) {
index 17c491dea1ddd9bb043ae07530d17cff2edeefd3..fc1e9fd92769eb2d609701ce25854ab035bf6f2b 100644 (file)
@@ -37,6 +37,7 @@
 #include "util.h"
 #include "modargs.h"
 #include "log.h"
+#include "native-common.h"
 
 PA_MODULE_AUTHOR("Lennart Poettering")
 PA_MODULE_VERSION(PACKAGE_VERSION)
@@ -72,7 +73,7 @@ PA_MODULE_VERSION(PACKAGE_VERSION)
   #include "protocol-native.h"
   #define protocol_new pa_protocol_native_new
   #define protocol_free pa_protocol_native_free
-  #define IPV4_PORT 4713
+  #define IPV4_PORT PA_NATIVE_DEFAULT_PORT
   #define UNIX_SOCKET "/tmp/polypaudio/native"
   #define MODULE_ARGUMENTS "public", "cookie",
   PA_MODULE_DESCRIPTION("Native protocol "SOCKET_DESCRIPTION)
diff --git a/polyp/module-tunnel.c b/polyp/module-tunnel.c
new file mode 100644 (file)
index 0000000..0c78b13
--- /dev/null
@@ -0,0 +1,423 @@
+/* $Id$ */
+
+/***
+  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
+  by the Free Software Foundation; either version 2 of the License,
+  or (at your option) any later version.
+  polypaudio is distributed in the hope that it will be useful, but
+  WITHOUT ANY WARRANTY; without even the implied warranty of
+  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
+  along with polypaudio; if not, write to the Free Software
+  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
+  USA.
+***/
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <unistd.h>
+#include <assert.h>
+#include <string.h>
+#include <errno.h>
+#include <sys/types.h>
+#include <regex.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+#include "module.h"
+#include "util.h"
+#include "modargs.h"
+#include "log.h"
+#include "subscribe.h"
+#include "xmalloc.h"
+#include "sink-input.h"
+#include "pdispatch.h"
+#include "pstream.h"
+#include "pstream-util.h"
+#include "authkey.h"
+#include "socket-client.h"
+#include "socket-util.h"
+
+PA_MODULE_AUTHOR("Lennart Poettering")
+PA_MODULE_DESCRIPTION("Tunnel module")
+PA_MODULE_USAGE("server=<filename> sink=<remote sink name> cookie=<filename> format=<sample format> channels=<number of channels> rate=<sample rate> sink_name=<name for the local sink>")
+PA_MODULE_VERSION(PACKAGE_VERSION)
+
+#define DEFAULT_SINK_NAME "tunnel"
+
+#define DEFAULT_TLENGTH (10240*8)
+#define DEFAULT_MAXLENGTH ((DEFAULT_TLENGTH*3)/2)
+#define DEFAULT_PREBUF DEFAULT_TLENGTH
+#define DEFAULT_MINREQ 512
+#define DEFAULT_FRAGSIZE 1024
+
+#define DEFAULT_TIMEOUT 5
+
+static const char* const valid_modargs[] = {
+    "server",
+    "sink",
+    "cookie",
+    "format",
+    "channels",
+    "rate",
+    "sink_name",
+    NULL,
+};
+
+static void command_stream_killed(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata);
+static void command_request(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata);
+
+static const struct pa_pdispatch_command command_table[PA_COMMAND_MAX] = {
+    [PA_COMMAND_REQUEST] = { command_request },
+    [PA_COMMAND_PLAYBACK_STREAM_KILLED] = { command_stream_killed },
+    [PA_COMMAND_RECORD_STREAM_KILLED] = { command_stream_killed },
+};
+
+struct userdata {
+    struct pa_socket_client *client;
+    struct pa_pstream *pstream;
+    struct pa_pdispatch *pdispatch;
+
+    char *server_name, *sink_name;
+    
+    struct pa_sink *sink;
+    struct pa_module *module;
+    struct pa_core *core;
+
+    uint8_t auth_cookie[PA_NATIVE_COOKIE_LENGTH];
+
+    uint32_t ctag;
+    uint32_t device_index;
+    uint32_t requested_bytes;
+    uint32_t channel;
+};
+
+
+static void close_stuff(struct userdata *u) {
+    if (u->pstream) {
+        pa_pstream_close(u->pstream);
+        pa_pstream_unref(u->pstream);
+        u->pstream = NULL;
+    }
+
+    if (u->pdispatch) {
+        pa_pdispatch_unref(u->pdispatch);
+        u->pdispatch = NULL;
+    }
+
+    if (u->client) {
+        pa_socket_client_unref(u->client);
+        u->client = NULL;
+    }
+
+    if (u->sink) {
+        pa_sink_disconnect(u->sink);
+        pa_sink_unref(u->sink);
+        u->sink = NULL;
+    }
+}
+
+static void die(struct userdata *u) {
+    assert(u);
+    close_stuff(u);
+    pa_module_unload_request(u->module);
+}
+
+static void request_bytes(struct userdata *u) {
+    assert(u);
+
+    if (!u->pstream)
+        return;
+    
+    while (u->requested_bytes > 0) {
+        struct pa_memchunk chunk;
+        if (pa_sink_render(u->sink, u->requested_bytes, &chunk) < 0)
+            return;
+
+        pa_pstream_send_memblock(u->pstream, u->channel, 0, &chunk);
+        pa_memblock_unref(chunk.memblock);
+
+        if (chunk.length > u->requested_bytes)
+            u->requested_bytes = 0;
+        else
+            u->requested_bytes -= chunk.length;
+    }
+}
+
+static void command_stream_killed(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
+    struct userdata *u = userdata;
+    assert(pd && t && u && u->pdispatch == pd);
+
+    pa_log(__FILE__": stream killed\n");
+    die(u);
+}
+
+static void command_request(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
+    struct userdata *u = userdata;
+    uint32_t bytes, channel;
+    assert(pd && command == PA_COMMAND_REQUEST && t && u && u->pdispatch == pd);
+
+    if (pa_tagstruct_getu32(t, &channel) < 0 ||
+        pa_tagstruct_getu32(t, &bytes) < 0 ||
+        !pa_tagstruct_eof(t)) {
+        pa_log(__FILE__": invalid protocol reply\n");
+        die(u);
+        return;
+    }
+
+    if (channel != u->channel) {
+        pa_log(__FILE__": recieved data for invalid channel\n");
+        die(u);
+        return;
+    }
+    
+    u->requested_bytes += bytes;
+    request_bytes(u);
+}
+
+
+static void create_stream_callback(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
+    struct userdata *u = userdata;
+    assert(pd && u && u->pdispatch == pd);
+
+    if (command != PA_COMMAND_REPLY) {
+        if (command == PA_COMMAND_ERROR)
+            pa_log(__FILE__": failed to create stream.\n");
+        else
+            pa_log(__FILE__": protocol error.\n");
+        die(u);
+        return;
+    }
+
+    if (pa_tagstruct_getu32(t, &u->channel) < 0 ||
+        pa_tagstruct_getu32(t, &u->device_index) < 0 ||
+        pa_tagstruct_getu32(t, &u->requested_bytes) < 0 ||
+        !pa_tagstruct_eof(t)) {
+        pa_log(__FILE__": invalid reply.\n");
+        die(u);
+        return;
+    }
+
+    request_bytes(u);
+}
+
+static void setup_complete_callback(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
+    struct userdata *u = userdata;
+    struct pa_tagstruct *reply;
+    char name[256], un[128], hn[128];
+    assert(pd && u && u->pdispatch == pd);
+
+    if (command != PA_COMMAND_REPLY || !pa_tagstruct_eof(t)) {
+        if (command == PA_COMMAND_ERROR)
+            pa_log(__FILE__": failed to authenticate\n");
+        else
+            pa_log(__FILE__": protocol error.\n");
+        die(u);
+        return;
+    }
+
+    snprintf(name, sizeof(name), "Tunnel from host '%s', user '%s', sink '%s'",
+             pa_get_host_name(hn, sizeof(hn)),
+             pa_get_user_name(un, sizeof(un)),
+             u->sink->name);
+    
+    reply = pa_tagstruct_new(NULL, 0);
+    pa_tagstruct_putu32(reply, PA_COMMAND_SET_CLIENT_NAME);
+    pa_tagstruct_putu32(reply, tag = u->ctag++);
+    pa_tagstruct_puts(reply, name);
+    pa_pstream_send_tagstruct(u->pstream, reply);
+    /* We ignore the server's reply here */
+
+    reply = pa_tagstruct_new(NULL, 0);
+    pa_tagstruct_putu32(reply, PA_COMMAND_CREATE_PLAYBACK_STREAM);
+    pa_tagstruct_putu32(reply, tag = u->ctag++);
+    pa_tagstruct_puts(reply, name);
+    pa_tagstruct_put_sample_spec(reply, &u->sink->sample_spec);
+    pa_tagstruct_putu32(reply, PA_INVALID_INDEX);
+    pa_tagstruct_puts(reply, u->sink_name);
+    pa_tagstruct_putu32(reply, DEFAULT_MAXLENGTH);
+    pa_tagstruct_put_boolean(reply, 0);
+    pa_tagstruct_putu32(reply, DEFAULT_TLENGTH);
+    pa_tagstruct_putu32(reply, DEFAULT_PREBUF);
+    pa_tagstruct_putu32(reply, DEFAULT_MINREQ);
+    pa_tagstruct_putu32(reply, PA_VOLUME_NORM);
+    
+    pa_pstream_send_tagstruct(u->pstream, reply);
+    pa_pdispatch_register_reply(u->pdispatch, tag, DEFAULT_TIMEOUT, create_stream_callback, u);
+}
+
+static void pstream_die_callback(struct pa_pstream *p, void *userdata) {
+    struct userdata *u = userdata;
+    assert(p && u);
+
+    pa_log(__FILE__": stream died.\n");
+    die(u);
+}
+
+
+static void pstream_packet_callback(struct pa_pstream *p, struct pa_packet *packet, void *userdata) {
+    struct userdata *u = userdata;
+    assert(p && packet && u);
+
+    if (pa_pdispatch_run(u->pdispatch, packet, u) < 0) {
+        pa_log(__FILE__": invalid packet\n");
+        die(u);
+    }
+}
+
+static void on_connection(struct pa_socket_client *sc, struct pa_iochannel *io, void *userdata) {
+    struct userdata *u = userdata;
+    struct pa_tagstruct *t;
+    uint32_t tag;
+    assert(sc && io && u && u->client == sc);
+
+    pa_socket_client_unref(u->client);
+    u->client = NULL;
+    
+    if (!io) {
+        pa_log(__FILE__": connection failed.\n");
+        pa_module_unload_request(u->module);
+        return;
+    }
+
+    u->pstream = pa_pstream_new(u->core->mainloop, io, u->core->memblock_stat);
+    u->pdispatch = pa_pdispatch_new(u->core->mainloop, command_table, PA_COMMAND_MAX);
+
+    pa_pstream_set_die_callback(u->pstream, pstream_die_callback, u);
+    pa_pstream_set_recieve_packet_callback(u->pstream, pstream_packet_callback, u);
+    
+    t = pa_tagstruct_new(NULL, 0);
+    pa_tagstruct_putu32(t, PA_COMMAND_AUTH);
+    pa_tagstruct_putu32(t, tag = u->ctag++);
+    pa_tagstruct_put_arbitrary(t, u->auth_cookie, sizeof(u->auth_cookie));
+    pa_pstream_send_tagstruct(u->pstream, t);
+    pa_pdispatch_register_reply(u->pdispatch, tag, DEFAULT_TIMEOUT, setup_complete_callback, u);
+    
+}
+
+static void sink_notify(struct pa_sink*sink) {
+    struct userdata *u;
+    assert(sink && sink->userdata);
+    u = sink->userdata;
+
+    request_bytes(u);
+}
+
+static pa_usec_t sink_get_latency(struct pa_sink *sink) {
+    struct userdata *u;
+    assert(sink && sink->userdata);
+    u = sink->userdata;
+
+    return 0;
+}
+
+int pa__init(struct pa_core *c, struct pa_module*m) {
+    struct pa_modargs *ma = NULL;
+    struct userdata *u = NULL;
+    struct pa_sample_spec ss;
+    assert(c && m);
+
+    if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
+        pa_log(__FILE__": failed to parse module arguments\n");
+        
+    }
+
+    u = pa_xmalloc(sizeof(struct userdata));
+    m->userdata = u;
+    u->module = m;
+    u->core = c;
+    u->client = NULL;
+    u->pdispatch = NULL;
+    u->pstream = NULL;
+    u->server_name = u->sink_name = NULL;
+    u->sink = NULL;
+    u->ctag = 1;
+    u->device_index = u->channel = PA_INVALID_INDEX;
+    u->requested_bytes = 0;
+
+    if (pa_authkey_load_from_home(pa_modargs_get_value(ma, "cookie", PA_NATIVE_COOKIE_FILE), u->auth_cookie, sizeof(u->auth_cookie)) < 0) {
+        pa_log(__FILE__": failed to load cookie.\n");
+        goto fail;
+    }
+    
+    if (!(u->server_name = pa_xstrdup(pa_modargs_get_value(ma, "server", NULL)))) {
+        pa_log(__FILE__": no server specified.\n");
+        goto fail;
+    }
+
+    u->sink_name = pa_xstrdup(pa_modargs_get_value(ma, "sink", NULL));
+
+    ss = c->default_sample_spec;
+    if (pa_modargs_get_sample_spec(ma, &ss) < 0) {
+        pa_log(__FILE__": invalid sample format specification\n");
+        goto fail;
+    }
+
+    if (u->server_name[0] == '/')
+        u->client = pa_socket_client_new_unix(c->mainloop, u->server_name);
+    else {
+        size_t len; 
+        struct sockaddr *sa;
+
+        if (!(sa = pa_resolve_server(u->server_name, &len, PA_NATIVE_DEFAULT_PORT))) {
+            pa_log(__FILE__": failed to resolve server '%s'\n", u->server_name);
+            goto fail;
+        }
+
+        u->client = pa_socket_client_new_sockaddr(c->mainloop, sa, len);
+        pa_xfree(sa);
+    }
+    
+    if (!u->client)
+        goto fail;
+
+    pa_socket_client_set_callback(u->client, on_connection, u);
+    
+    if (!(u->sink = pa_sink_new(c, pa_modargs_get_value(ma, "sink_name", DEFAULT_SINK_NAME), 0, &ss))) {
+        pa_log(__FILE__": failed to create sink.\n");
+        goto fail;
+    }
+
+    u->sink->notify = sink_notify;
+    u->sink->get_latency = sink_get_latency;
+    u->sink->userdata = u;
+    u->sink->description = pa_sprintf_malloc("Tunnel to '%s%s%s'", u->sink_name ? u->sink_name : "", u->sink_name ? "@" : "", u->server_name);
+
+    pa_sink_set_owner(u->sink, m);
+
+    pa_modargs_free(ma);
+
+    return 0;
+    
+fail:
+    pa__done(c, m);
+
+    if (ma)
+        pa_modargs_free(ma);
+    return  -1;
+}
+
+void pa__done(struct pa_core *c, struct pa_module*m) {
+    struct userdata* u;
+    assert(c && m);
+
+    if (!(u = m->userdata))
+        return;
+
+    close_stuff(u);
+
+    pa_xfree(u->sink_name);
+    pa_xfree(u->server_name);
+
+    pa_xfree(u);
+}
+
+
index 5ecb26c071117a2c6533b17339b13347b4ce34b8..33ecdc76fe7c0ab73f2cb2323d95e5d3a85f157c 100644 (file)
@@ -96,6 +96,8 @@ enum {
 #define PA_NATIVE_COOKIE_LENGTH 256
 #define PA_NATIVE_COOKIE_FILE ".polypaudio-cookie"
 
+#define PA_NATIVE_DEFAULT_PORT 4713
+
 PA_C_DECL_END
 
 #endif
index d793c186f13aef5accac56d231a5c555199c982d..d2fae0af9870b0a29f4d7a8e2fed5c30879ad32d 100644 (file)
 #include "xmalloc.h"
 #include "log.h"
 #include "client-conf.h"
+#include "socket-util.h"
 
 #define DEFAULT_SERVER "/tmp/polypaudio/native"
+#define AUTOSPAWN_LOCK "/tmp/polypaudio/autospawn.lock"
 
 static const struct pa_pdispatch_command command_table[PA_COMMAND_MAX] = {
     [PA_COMMAND_REQUEST] = { pa_command_request },
@@ -347,38 +349,6 @@ finish:
     pa_context_unref(c);
 }
 
-static struct sockaddr *resolve_server(const char *server, size_t *len) {
-    struct sockaddr *sa;
-    struct addrinfo hints, *result = NULL;
-    char *port, host[256];
-    assert(server && len);
-
-    snprintf(host, sizeof(host), "%s", server);
-    host[strcspn(host, ":")] = 0;
-    
-    if ((port = strrchr(server, ':')))
-        port++;
-    
-    if (!port)
-        port = DEFAULT_PORT;
-
-    memset(&hints, 0, sizeof(hints));
-    hints.ai_family = PF_UNSPEC;
-    hints.ai_socktype = SOCK_STREAM;
-    hints.ai_protocol = 0;
-
-    if (getaddrinfo(host, port, &hints, &result) != 0)
-        return NULL;
-    assert(result);
-    
-    sa = pa_xmalloc(*len = result->ai_addrlen);
-    memcpy(sa, result->ai_addr, *len);
-
-    freeaddrinfo(result);
-    
-    return sa;
-}
-
 static int default_server_is_running(void) {
     struct stat st;
     
@@ -499,9 +469,21 @@ int pa_context_connect(struct pa_context *c, const char *server, int spawn, cons
     if (!server)
         server = c->conf->default_server;
 
-    if (!server && spawn && c->conf->autospawn && !default_server_is_running())
-        return context_connect_spawn(c, api);
+    if (!server && spawn && c->conf->autospawn) {
+        int lock_fd = pa_lock_lockfile(AUTOSPAWN_LOCK);
+        
+        if (!default_server_is_running()) {
+            int r = context_connect_spawn(c, api);
+
+            if (lock_fd >= 0)
+                pa_unlock_lockfile(lock_fd);
+            return r;
+        }
 
+        if (lock_fd >= 0)
+            pa_unlock_lockfile(lock_fd);
+    }
+    
     if (!server)
         server = DEFAULT_SERVER;
 
@@ -520,7 +502,7 @@ int pa_context_connect(struct pa_context *c, const char *server, int spawn, cons
         struct sockaddr* sa;
         size_t sa_len;
 
-        if (!(sa = resolve_server(server, &sa_len))) {
+        if (!(sa = pa_resolve_server(server, &sa_len, PA_NATIVE_DEFAULT_PORT))) {
             pa_context_fail(c, PA_ERROR_INVALIDSERVER);
             goto finish;
         }
index 656d07dbe3a825871506cf6699140d5b507fdd1d..c10ca515ae22256cf163879c59b0a42cc82aa4ed 100644 (file)
@@ -41,8 +41,7 @@
 #define DEFAULT_MINREQ 512
 #define DEFAULT_FRAGSIZE 1024
 
-#define DEFAULT_TIMEOUT (5*60)
-#define DEFAULT_PORT "4713"
+#define DEFAULT_TIMEOUT (10)
 
 #define ENV_AUTOSPAWNED "POLYP_AUTOSPAWNED"
 
index b34a736cc64290739c8bd94c31e1c7cbbb197258..a2d34ee5b526f5189abce82a6d96ef402a42096c 100644 (file)
@@ -64,7 +64,6 @@ void pa_sink_disconnect(struct pa_sink* s);
 void pa_sink_unref(struct pa_sink*s);
 struct pa_sink* pa_sink_ref(struct pa_sink *s);
 
-
 int pa_sink_render(struct pa_sink*s, size_t length, struct pa_memchunk *result);
 void pa_sink_render_full(struct pa_sink *s, size_t length, struct pa_memchunk *result);
 int pa_sink_render_into(struct pa_sink*s, struct pa_memchunk *target);
index 20380653d9a57e9146ac0a9058431d38cea3f426..499739bd34dc36e74a7e6f76052f985a09cf07b0 100644 (file)
@@ -37,6 +37,7 @@
 #include <sys/types.h>
 #include <netinet/tcp.h>
 #include <netinet/ip.h>
+#include <netdb.h>
 
 #include "socket-util.h"
 #include "util.h"
@@ -219,3 +220,35 @@ finish:
     return ret;
 }
 
+struct sockaddr *pa_resolve_server(const char *server, size_t *len, uint16_t nport) {
+    struct sockaddr *sa;
+    struct addrinfo hints, *result = NULL;
+    char *port, host[256], tmp[16];
+    assert(server && len);
+
+    snprintf(host, sizeof(host), "%s", server);
+    host[strcspn(host, ":")] = 0;
+    
+    if ((port = strrchr(server, ':')))
+        port++;
+    
+    if (!port) 
+        snprintf(port = tmp, sizeof(tmp), "%u", nport);
+
+    memset(&hints, 0, sizeof(hints));
+    hints.ai_family = PF_UNSPEC;
+    hints.ai_socktype = SOCK_STREAM;
+    hints.ai_protocol = 0;
+
+    if (getaddrinfo(host, port, &hints, &result) != 0)
+        return NULL;
+    assert(result);
+    
+    sa = pa_xmalloc(*len = result->ai_addrlen);
+    memcpy(sa, result->ai_addr, *len);
+
+    freeaddrinfo(result);
+    
+    return sa;
+}
+
index 85133febd85fdedfa32bb2a2acfaf821da58a0d5..46fcfdc97da44cd331a112725092dad7b06b7fe3 100644 (file)
@@ -38,4 +38,6 @@ int pa_unix_socket_remove_stale(const char *fn);
 int pa_unix_socket_make_secure_dir(const char *fn);
 int pa_unix_socket_remove_secure_dir(const char *fn);
 
+struct sockaddr *pa_resolve_server(const char *server, size_t *len, uint16_t port);
+
 #endif
index fa33ffffea4941e21c69465e02374a6a2d78bd2c..70ec120d338d51aa2cc3fa432147ad9fc2989534 100644 (file)
@@ -502,7 +502,7 @@ finish:
     return r;
 }
 
-int pa_lock_file(int fd, int b) {
+int pa_lock_fd(int fd, int b) {
 
     struct flock flock;
 
@@ -525,3 +525,44 @@ char* pa_strip_nl(char *s) {
     s[strcspn(s, "\r\n")] = 0;
     return s;
 }
+
+int pa_lock_lockfile(const char *fn) {
+    int fd;
+    assert(fn);
+
+    if ((fd = open(fn, O_CREAT|O_RDWR, S_IRUSR|S_IWUSR)) < 0) {
+        pa_log(__FILE__": failed to create lock file '%s'\n", fn);
+        goto fail;
+    }
+
+    if (pa_lock_fd(fd, 1) < 0)
+        goto fail;
+
+    return fd;
+
+fail:
+
+    if (fd >= 0)
+        close(fd);
+
+    return -1;
+}
+
+
+int pa_unlock_lockfile(int fd) {
+    int r = 0;
+    assert(fd >= 0);
+
+    if (pa_lock_fd(fd, 0) < 0) {
+        pa_log(__FILE__": WARNING: failed to unlock file.\n");
+        r = -1;
+    }
+
+    if (close(fd) < 0) {
+        pa_log(__FILE__": WARNING: failed to close lock file.\n");
+        r = -1;
+    }
+
+    return r;
+}
+
index e1910c831cb1889281800b46a9e50f6248791aff..b0a1a0337d374116fa1980787a847ba468fde37a 100644 (file)
@@ -70,7 +70,9 @@ int pa_parse_resample_method(const char *string);
 
 int pa_uid_in_group(const char *name, gid_t *gid);
 
-int pa_lock_file(int fd, int b);
+int pa_lock_fd(int fd, int b);
 
+int pa_lock_lockfile(const char *fn);
+int pa_unlock_lockfile(int fd);
 
 #endif