summaryrefslogtreecommitdiffstats
path: root/src/pulsecore
diff options
context:
space:
mode:
Diffstat (limited to 'src/pulsecore')
-rw-r--r--src/pulsecore/authkey.c37
-rw-r--r--src/pulsecore/core-scache.c5
-rw-r--r--src/pulsecore/core-util.c106
-rw-r--r--src/pulsecore/core-util.h5
-rw-r--r--src/pulsecore/lock-autospawn.c66
-rw-r--r--src/pulsecore/memblock.c23
-rw-r--r--src/pulsecore/proplist-util.c6
-rw-r--r--src/pulsecore/time-smoother.c41
-rw-r--r--src/pulsecore/time-smoother.h2
9 files changed, 202 insertions, 89 deletions
diff --git a/src/pulsecore/authkey.c b/src/pulsecore/authkey.c
index 1e31d076..15613e27 100644
--- a/src/pulsecore/authkey.c
+++ b/src/pulsecore/authkey.c
@@ -36,6 +36,7 @@
#include <sys/stat.h>
#include <pulse/util.h>
+#include <pulse/xmalloc.h>
#include <pulsecore/core-error.h>
#include <pulsecore/core-util.h>
#include <pulsecore/log.h>
@@ -147,47 +148,46 @@ int pa_authkey_load(const char *path, void *data, size_t length) {
/* If the specified file path starts with / return it, otherwise
* return path prepended with home directory */
-static const char *normalize_path(const char *fn, char *s, size_t l) {
+static char *normalize_path(const char *fn) {
pa_assert(fn);
- pa_assert(s);
- pa_assert(l > 0);
#ifndef OS_IS_WIN32
if (fn[0] != '/') {
#else
if (strlen(fn) < 3 || !isalpha(fn[0]) || fn[1] != ':' || fn[2] != '\\') {
#endif
- char homedir[PATH_MAX];
+ char *homedir, *s;
- if (!pa_get_home_dir(homedir, sizeof(homedir)))
+ if (!(homedir = pa_get_home_dir_malloc()))
return NULL;
-#ifndef OS_IS_WIN32
- pa_snprintf(s, l, "%s/%s", homedir, fn);
-#else
- pa_snprintf(s, l, "%s\\%s", homedir, fn);
-#endif
+ s = pa_sprintf_malloc("%s" PA_PATH_SEP "%s", homedir, fn);
+ pa_xfree(homedir);
+
return s;
}
- return fn;
+ return pa_xstrdup(fn);
}
/* Load a cookie from a file in the home directory. If the specified
* path starts with /, use it as absolute path instead. */
int pa_authkey_load_auto(const char *fn, void *data, size_t length) {
- char path[PATH_MAX];
- const char *p;
+ char *p;
+ int ret;
pa_assert(fn);
pa_assert(data);
pa_assert(length > 0);
- if (!(p = normalize_path(fn, path, sizeof(path))))
+ if (!(p = normalize_path(fn)))
return -2;
- return pa_authkey_load(p, data, length);
+ ret = pa_authkey_load(p, data, length);
+ pa_xfree(p);
+
+ return ret;
}
/* Store the specified cookie in the specified cookie file */
@@ -195,14 +195,13 @@ int pa_authkey_save(const char *fn, const void *data, size_t length) {
int fd = -1;
int unlock = 0, ret = -1;
ssize_t r;
- char path[PATH_MAX];
- const char *p;
+ char *p;
pa_assert(fn);
pa_assert(data);
pa_assert(length > 0);
- if (!(p = normalize_path(fn, path, sizeof(path))))
+ if (!(p = normalize_path(fn)))
return -2;
if ((fd = open(p, O_RDWR|O_CREAT|O_NOCTTY, S_IRUSR|S_IWUSR)) < 0) {
@@ -232,5 +231,7 @@ finish:
}
}
+ pa_xfree(p);
+
return ret;
}
diff --git a/src/pulsecore/core-scache.c b/src/pulsecore/core-scache.c
index 4c5a4b26..fde12ecf 100644
--- a/src/pulsecore/core-scache.c
+++ b/src/pulsecore/core-scache.c
@@ -494,13 +494,14 @@ int pa_scache_add_directory_lazy(pa_core *c, const char *pathname) {
struct dirent *e;
while ((e = readdir(dir))) {
- char p[PATH_MAX];
+ char *p;
if (e->d_name[0] == '.')
continue;
- pa_snprintf(p, sizeof(p), "%s/%s", pathname, e->d_name);
+ p = pa_sprintf_malloc("%s" PA_PATH_SEP "%s", pathname, e->d_name);
add_file(c, p);
+ pa_xfree(p);
}
closedir(dir);
diff --git a/src/pulsecore/core-util.c b/src/pulsecore/core-util.c
index 04e7eb24..d4baf697 100644
--- a/src/pulsecore/core-util.c
+++ b/src/pulsecore/core-util.c
@@ -552,12 +552,20 @@ char *pa_vsprintf_malloc(const char *format, va_list ap) {
/* Similar to OpenBSD's strlcpy() function */
char *pa_strlcpy(char *b, const char *s, size_t l) {
+ size_t k;
+
pa_assert(b);
pa_assert(s);
pa_assert(l > 0);
- strncpy(b, s, l);
- b[l-1] = 0;
+ k = strlen(s);
+
+ if (k > l-1)
+ k = l-1;
+
+ memcpy(b, s, k);
+ b[k] = 0;
+
return b;
}
@@ -1328,26 +1336,32 @@ int pa_unlock_lockfile(const char *fn, int fd) {
}
static char *get_pulse_home(void) {
- char h[PATH_MAX];
+ char *h;
struct stat st;
+ char *ret = NULL;
- if (!pa_get_home_dir(h, sizeof(h))) {
+ if (!(h = pa_get_home_dir_malloc())) {
pa_log_error("Failed to get home directory.");
return NULL;
}
if (stat(h, &st) < 0) {
pa_log_error("Failed to stat home directory %s: %s", h, pa_cstrerror(errno));
- return NULL;
+ goto finish;
}
if (st.st_uid != getuid()) {
pa_log_error("Home directory %s not ours.", h);
errno = EACCES;
- return NULL;
+ goto finish;
}
- return pa_sprintf_malloc("%s" PA_PATH_SEP ".pulse", h);
+ ret = pa_sprintf_malloc("%s" PA_PATH_SEP ".pulse", h);
+
+finish:
+ pa_xfree(h);
+
+ return ret;
}
char *pa_get_state_dir(void) {
@@ -1372,6 +1386,50 @@ char *pa_get_state_dir(void) {
return d;
}
+char *pa_get_home_dir_malloc(void) {
+ char *homedir;
+ size_t allocated = 128;
+
+ for (;;) {
+ homedir = pa_xmalloc(allocated);
+
+ if (!pa_get_home_dir(homedir, allocated)) {
+ pa_xfree(homedir);
+ return NULL;
+ }
+
+ if (strlen(homedir) < allocated - 1)
+ break;
+
+ pa_xfree(homedir);
+ allocated *= 2;
+ }
+
+ return homedir;
+}
+
+char *pa_get_binary_name_malloc(void) {
+ char *t;
+ size_t allocated = 128;
+
+ for (;;) {
+ t = pa_xmalloc(allocated);
+
+ if (!pa_get_binary_name(t, allocated)) {
+ pa_xfree(t);
+ return NULL;
+ }
+
+ if (strlen(t) < allocated - 1)
+ break;
+
+ pa_xfree(t);
+ allocated *= 2;
+ }
+
+ return t;
+}
+
static char* make_random_dir(mode_t m) {
static const char table[] =
"abcdefghijklmnopqrstuvwxyz"
@@ -1481,7 +1539,7 @@ char *pa_get_runtime_dir(void) {
goto fail;
}
- k = pa_sprintf_malloc("%s" PA_PATH_SEP "%s:runtime", d, mid);
+ k = pa_sprintf_malloc("%s" PA_PATH_SEP "%s-runtime", d, mid);
pa_xfree(d);
pa_xfree(mid);
@@ -1626,14 +1684,15 @@ FILE *pa_open_config_file(const char *global, const char *local, const char *env
if (local) {
const char *e;
char *lfn;
- char h[PATH_MAX];
+ char *h;
FILE *f;
if ((e = getenv("PULSE_CONFIG_PATH")))
fn = lfn = pa_sprintf_malloc("%s" PA_PATH_SEP "%s", e, local);
- else if (pa_get_home_dir(h, sizeof(h)))
+ else if ((h = pa_get_home_dir_malloc())) {
fn = lfn = pa_sprintf_malloc("%s" PA_PATH_SEP ".pulse" PA_PATH_SEP "%s", h, local);
- else
+ pa_xfree(h);
+ } else
return NULL;
#ifdef OS_IS_WIN32
@@ -1713,13 +1772,14 @@ char *pa_find_config_file(const char *global, const char *local, const char *env
if (local) {
const char *e;
char *lfn;
- char h[PATH_MAX];
+ char *h;
if ((e = getenv("PULSE_CONFIG_PATH")))
fn = lfn = pa_sprintf_malloc("%s" PA_PATH_SEP "%s", e, local);
- else if (pa_get_home_dir(h, sizeof(h)))
+ else if ((h = pa_get_home_dir_malloc())) {
fn = lfn = pa_sprintf_malloc("%s" PA_PATH_SEP ".pulse" PA_PATH_SEP "%s", h, local);
- else
+ pa_xfree(h);
+ } else
return NULL;
#ifdef OS_IS_WIN32
@@ -1904,7 +1964,7 @@ static char *get_path(const char *fn, pa_bool_t prependmid, pa_bool_t rt) {
return NULL;
}
- r = pa_sprintf_malloc("%s" PA_PATH_SEP "%s:%s", rtp, mid, fn);
+ r = pa_sprintf_malloc("%s" PA_PATH_SEP "%s-%s", rtp, mid, fn);
pa_xfree(mid);
} else
r = pa_sprintf_malloc("%s" PA_PATH_SEP "%s", rtp, fn);
@@ -2779,3 +2839,19 @@ char* pa_maybe_prefix_path(const char *path, const char *prefix) {
return pa_sprintf_malloc("%s" PA_PATH_SEP "%s", prefix, path);
}
+
+size_t pa_pipe_buf(int fd) {
+
+#ifdef _PC_PIPE_BUF
+ long n;
+
+ if ((n = fpathconf(fd, _PC_PIPE_BUF)) >= 0)
+ return (size_t) n;
+#endif
+
+#ifdef PIPE_BUF
+ return PIPE_BUF;
+#else
+ return 4096;
+#endif
+}
diff --git a/src/pulsecore/core-util.h b/src/pulsecore/core-util.h
index 96a0480a..6de4b771 100644
--- a/src/pulsecore/core-util.h
+++ b/src/pulsecore/core-util.h
@@ -126,6 +126,8 @@ char* pa_find_config_file(const char *global, const char *local, const char *env
char *pa_get_runtime_dir(void);
char *pa_get_state_dir(void);
+char *pa_get_home_dir_malloc(void);
+char *pa_get_binary_name_malloc(void);
char *pa_runtime_path(const char *fn);
char *pa_state_path(const char *fn, pa_bool_t prepend_machine_id);
@@ -236,4 +238,7 @@ char **pa_split_spaces_strv(const char *s);
char* pa_maybe_prefix_path(const char *path, const char *prefix);
+/* Returns size of the specified pipe or 4096 on failure */
+size_t pa_pipe_buf(int fd);
+
#endif
diff --git a/src/pulsecore/lock-autospawn.c b/src/pulsecore/lock-autospawn.c
index 4436974d..c0df7938 100644
--- a/src/pulsecore/lock-autospawn.c
+++ b/src/pulsecore/lock-autospawn.c
@@ -55,10 +55,16 @@ static pa_mutex *mutex;
static unsigned n_ref = 0;
static int lock_fd = -1;
static pa_mutex *lock_fd_mutex = NULL;
-static pa_bool_t taken = FALSE;
-static pa_thread *thread;
+static pa_thread *thread = NULL;
static int pipe_fd[2] = { -1, -1 };
+static enum {
+ STATE_IDLE,
+ STATE_OWNING,
+ STATE_TAKEN,
+ STATE_FAILED
+} state = STATE_IDLE;
+
static void destroy_mutex(void) PA_GCC_DESTRUCTOR;
static int ref(void) {
@@ -67,15 +73,16 @@ static int ref(void) {
pa_assert(pipe_fd[0] >= 0);
pa_assert(pipe_fd[1] >= 0);
+ pa_assert(lock_fd_mutex);
n_ref++;
return 0;
}
- pa_assert(lock_fd < 0);
pa_assert(!lock_fd_mutex);
- pa_assert(!taken);
+ pa_assert(state == STATE_IDLE);
+ pa_assert(lock_fd < 0);
pa_assert(!thread);
pa_assert(pipe_fd[0] < 0);
pa_assert(pipe_fd[1] < 0);
@@ -83,14 +90,14 @@ static int ref(void) {
if (pipe(pipe_fd) < 0)
return -1;
- lock_fd_mutex = pa_mutex_new(FALSE, FALSE);
-
pa_make_fd_cloexec(pipe_fd[0]);
pa_make_fd_cloexec(pipe_fd[1]);
pa_make_fd_nonblock(pipe_fd[1]);
pa_make_fd_nonblock(pipe_fd[0]);
+ lock_fd_mutex = pa_mutex_new(FALSE, FALSE);
+
n_ref = 1;
return 0;
}
@@ -107,15 +114,18 @@ static void unref(pa_bool_t after_fork) {
if (n_ref > 0)
return;
- pa_assert(!taken);
-
if (thread) {
pa_thread_free(thread);
thread = NULL;
}
pa_mutex_lock(lock_fd_mutex);
- if (lock_fd >= 0) {
+
+ pa_assert(state != STATE_TAKEN);
+
+ if (state == STATE_OWNING) {
+
+ pa_assert(lock_fd >= 0);
if (after_fork)
pa_close(lock_fd);
@@ -127,10 +137,12 @@ static void unref(pa_bool_t after_fork) {
pa_unlock_lockfile(lf, lock_fd);
pa_xfree(lf);
-
- lock_fd = -1;
}
}
+
+ lock_fd = -1;
+ state = STATE_IDLE;
+
pa_mutex_unlock(lock_fd_mutex);
pa_mutex_free(lock_fd_mutex);
@@ -205,15 +217,24 @@ static void thread_func(void *u) {
if (!(lf = pa_runtime_path(AUTOSPAWN_LOCK))) {
pa_log_warn(_("Cannot access autospawn lock."));
- goto finish;
+ goto fail;
}
if ((fd = pa_lock_lockfile(lf)) < 0)
- goto finish;
+ goto fail;
pa_mutex_lock(lock_fd_mutex);
- pa_assert(lock_fd < 0);
+ pa_assert(state == STATE_IDLE);
lock_fd = fd;
+ state = STATE_OWNING;
+ pa_mutex_unlock(lock_fd_mutex);
+
+ goto finish;
+
+fail:
+ pa_mutex_lock(lock_fd_mutex);
+ pa_assert(state == STATE_IDLE);
+ state = STATE_FAILED;
pa_mutex_unlock(lock_fd_mutex);
finish:
@@ -238,12 +259,10 @@ static void create_mutex(void) {
}
static void destroy_mutex(void) {
-
if (mutex)
pa_mutex_free(mutex);
}
-
int pa_autospawn_lock_init(void) {
int ret = -1;
@@ -273,13 +292,18 @@ int pa_autospawn_lock_acquire(pa_bool_t block) {
empty_pipe();
- if (lock_fd >= 0 && !taken) {
- taken = TRUE;
+ if (state == STATE_OWNING) {
+ state = STATE_TAKEN;
ret = 1;
break;
}
- if (lock_fd < 0)
+ if (state == STATE_FAILED) {
+ ret = -1;
+ break;
+ }
+
+ if (state == STATE_IDLE)
if (start_thread() < 0)
break;
@@ -310,8 +334,8 @@ void pa_autospawn_lock_release(void) {
pa_mutex_lock(mutex);
pa_assert(n_ref >= 1);
- pa_assert(taken);
- taken = FALSE;
+ pa_assert(state == STATE_TAKEN);
+ state = STATE_OWNING;
ping();
diff --git a/src/pulsecore/memblock.c b/src/pulsecore/memblock.c
index 2c3f98a5..3bc10de5 100644
--- a/src/pulsecore/memblock.c
+++ b/src/pulsecore/memblock.c
@@ -96,6 +96,7 @@ struct pa_memimport_segment {
unsigned n_blocks;
};
+/* A collection of multiple segments */
struct pa_memimport {
pa_mutex *mutex;
@@ -514,9 +515,9 @@ static void memblock_free(pa_memblock *b) {
pa_mutex_lock(import->mutex);
- pa_hashmap_remove(
- import->blocks,
- PA_UINT32_TO_PTR(b->per_type.imported.id));
+ pa_assert_se(pa_hashmap_remove(
+ import->blocks,
+ PA_UINT32_TO_PTR(b->per_type.imported.id)));
pa_assert(segment->n_blocks >= 1);
if (-- segment->n_blocks <= 0)
@@ -677,9 +678,9 @@ static void memblock_replace_import(pa_memblock *b) {
pa_mutex_lock(import->mutex);
- pa_hashmap_remove(
- import->blocks,
- PA_UINT32_TO_PTR(b->per_type.imported.id));
+ pa_assert_se(pa_hashmap_remove(
+ import->blocks,
+ PA_UINT32_TO_PTR(b->per_type.imported.id)));
memblock_make_local(b);
@@ -960,6 +961,11 @@ pa_memblock* pa_memimport_get(pa_memimport *i, uint32_t block_id, uint32_t shm_i
pa_mutex_lock(i->mutex);
+ if ((b = pa_hashmap_get(i->blocks, PA_UINT32_TO_PTR(block_id)))) {
+ pa_memblock_ref(b);
+ goto finish;
+ }
+
if (pa_hashmap_size(i->blocks) >= PA_MEMIMPORT_SLOTS_MAX)
goto finish;
@@ -989,12 +995,11 @@ pa_memblock* pa_memimport_get(pa_memimport *i, uint32_t block_id, uint32_t shm_i
seg->n_blocks++;
+ stat_add(b);
+
finish:
pa_mutex_unlock(i->mutex);
- if (b)
- stat_add(b);
-
return b;
}
diff --git a/src/pulsecore/proplist-util.c b/src/pulsecore/proplist-util.c
index d9769bc7..23864bcb 100644
--- a/src/pulsecore/proplist-util.c
+++ b/src/pulsecore/proplist-util.c
@@ -186,10 +186,12 @@ void pa_init_proplist(pa_proplist *p) {
}
if (!pa_proplist_contains(p, PA_PROP_APPLICATION_PROCESS_BINARY)) {
- char t[PATH_MAX];
- if (pa_get_binary_name(t, sizeof(t))) {
+ char *t;
+
+ if ((t = pa_get_binary_name_malloc())) {
char *c = pa_utf8_filter(t);
pa_proplist_sets(p, PA_PROP_APPLICATION_PROCESS_BINARY, c);
+ pa_xfree(t);
pa_xfree(c);
}
}
diff --git a/src/pulsecore/time-smoother.c b/src/pulsecore/time-smoother.c
index 9d5a0705..d6c37878 100644
--- a/src/pulsecore/time-smoother.c
+++ b/src/pulsecore/time-smoother.c
@@ -108,29 +108,11 @@ pa_smoother* pa_smoother_new(
s = pa_xnew(pa_smoother, 1);
s->adjust_time = adjust_time;
s->history_time = history_time;
- s->time_offset = 0;
+ s->min_history = min_history;
s->monotonic = monotonic;
-
- s->px = s->py = 0;
- s->dp = 1;
-
- s->ex = s->ey = s->ry = 0;
- s->de = 1;
-
- s->history_idx = 0;
- s->n_history = 0;
-
- s->last_y = s->last_x = 0;
-
- s->abc_valid = FALSE;
-
- s->paused = FALSE;
s->smoothing = smoothing;
- s->min_history = min_history;
-
- s->paused = paused;
- s->time_offset = s->pause_time = time_offset;
+ pa_smoother_reset(s, time_offset, paused);
return s;
}
@@ -514,9 +496,26 @@ pa_usec_t pa_smoother_translate(pa_smoother *s, pa_usec_t x, pa_usec_t y_delay)
return (pa_usec_t) llrint((double) y_delay / nde);
}
-void pa_smoother_reset(pa_smoother *s) {
+void pa_smoother_reset(pa_smoother *s, pa_usec_t time_offset, pa_bool_t paused) {
pa_assert(s);
+ s->px = s->py = 0;
+ s->dp = 1;
+
+ s->ex = s->ey = s->ry = 0;
+ s->de = 1;
+
+ s->history_idx = 0;
s->n_history = 0;
+
+ s->last_y = s->last_x = 0;
+
s->abc_valid = FALSE;
+
+ s->paused = paused;
+ s->time_offset = s->pause_time = time_offset;
+
+#ifdef DEBUG_DATA
+ pa_log_debug("reset()");
+#endif
}
diff --git a/src/pulsecore/time-smoother.h b/src/pulsecore/time-smoother.h
index 5244a7e7..63d33e48 100644
--- a/src/pulsecore/time-smoother.h
+++ b/src/pulsecore/time-smoother.h
@@ -52,7 +52,7 @@ void pa_smoother_set_time_offset(pa_smoother *s, pa_usec_t x_offset);
void pa_smoother_pause(pa_smoother *s, pa_usec_t x);
void pa_smoother_resume(pa_smoother *s, pa_usec_t x, pa_bool_t abrupt);
-void pa_smoother_reset(pa_smoother *s);
+void pa_smoother_reset(pa_smoother *s, pa_usec_t time_offset, pa_bool_t paused);
void pa_smoother_fix_now(pa_smoother *s);