diff options
Diffstat (limited to 'src/pulsecore/core-util.c')
-rw-r--r-- | src/pulsecore/core-util.c | 109 |
1 files changed, 76 insertions, 33 deletions
diff --git a/src/pulsecore/core-util.c b/src/pulsecore/core-util.c index c8ea4f52..d259fb16 100644 --- a/src/pulsecore/core-util.c +++ b/src/pulsecore/core-util.c @@ -1,5 +1,3 @@ -/* $Id$ */ - /*** This file is part of PulseAudio. @@ -1144,12 +1142,13 @@ fail: /* Unlock a temporary lcok file */ int pa_unlock_lockfile(const char *fn, int fd) { int r = 0; - pa_assert(fn); pa_assert(fd >= 0); - if (unlink(fn) < 0) { - pa_log_warn("Unable to remove lock file '%s': %s", fn, pa_cstrerror(errno)); - r = -1; + if (fn) { + if (unlink(fn) < 0) { + pa_log_warn("Unable to remove lock file '%s': %s", fn, pa_cstrerror(errno)); + r = -1; + } } if (pa_lock_fd(fd, 0) < 0) { @@ -1165,24 +1164,35 @@ int pa_unlock_lockfile(const char *fn, int fd) { return r; } -char *pa_get_runtime_dir(void) { +static char *get_dir(mode_t m, const char *env_name) { const char *e; char *d; - if ((e = getenv("PULSE_RUNTIME_PATH"))) + if ((e = getenv(env_name))) d = pa_xstrdup(e); else { char h[PATH_MAX]; + struct stat st; if (!pa_get_home_dir(h, sizeof(h))) { 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; + } + + if (st.st_uid != getuid()) { + pa_log_error("Home directory %s not ours.", d); + return NULL; + } + d = pa_sprintf_malloc("%s" PA_PATH_SEP ".pulse", h); } - if (pa_make_secure_dir(d, 0700, (pid_t) -1, (pid_t) -1) < 0) { + if (pa_make_secure_dir(d, m, (pid_t) -1, (pid_t) -1) < 0) { pa_log_error("Failed to create secure directory: %s", pa_cstrerror(errno)); return NULL; } @@ -1190,6 +1200,14 @@ char *pa_get_runtime_dir(void) { return d; } +char *pa_get_runtime_dir(void) { + return get_dir(pa_in_system_mode() ? 0755 : 0700, "PULSE_RUNTIME_PATH"); +} + +char *pa_get_state_dir(void) { + return get_dir(0700, "PULSE_STATE_PATH"); +} + /* Try to open a configuration file. If "env" is specified, open the * value of the specified environment variable. Otherwise look for a * file "local" in the home directory or a file "global" in global @@ -1418,7 +1436,7 @@ size_t pa_parsehex(const char *p, uint8_t *d, size_t dlength) { } /* Returns nonzero when *s starts with *pfx */ -int pa_startswith(const char *s, const char *pfx) { +pa_bool_t pa_startswith(const char *s, const char *pfx) { size_t l; pa_assert(s); @@ -1430,7 +1448,7 @@ int pa_startswith(const char *s, const char *pfx) { } /* Returns nonzero when *s ends with *sfx */ -int pa_endswith(const char *s, const char *sfx) { +pa_bool_t pa_endswith(const char *s, const char *sfx) { size_t l1, l2; pa_assert(s); @@ -1472,13 +1490,16 @@ char *pa_make_path_absolute(const char *p) { /* if fn is null return the PulseAudio run time path in s (~/.pulse) * if fn is non-null and starts with / return fn * otherwise append fn to the run time path and return it */ -char *pa_runtime_path(const char *fn) { +static char *get_path(const char *fn, pa_bool_t rt) { char *rtp; if (pa_is_path_absolute(fn)) return pa_xstrdup(fn); - rtp = pa_get_runtime_dir(); + rtp = rt ? pa_get_runtime_dir() : pa_get_state_dir(); + + if (!rtp) + return NULL; if (fn) { char *r; @@ -1489,6 +1510,14 @@ char *pa_runtime_path(const char *fn) { return rtp; } +char *pa_runtime_path(const char *fn) { + return get_path(fn, 1); +} + +char *pa_state_path(const char *fn) { + return get_path(fn, 0); +} + /* Convert the string s to a signed integer in *ret_i */ int pa_atoi(const char *s, int32_t *ret_i) { char *x = NULL; @@ -1541,13 +1570,13 @@ static void c_locale_destroy(void) { } #endif -int pa_atof(const char *s, float *ret_f) { +int pa_atod(const char *s, double *ret_d) { char *x = NULL; - float f; + double f; int r = 0; pa_assert(s); - pa_assert(ret_f); + pa_assert(ret_d); /* This should be locale independent */ @@ -1562,22 +1591,18 @@ int pa_atof(const char *s, float *ret_f) { if (c_locale) { errno = 0; - f = strtof_l(s, &x, c_locale); + f = strtod_l(s, &x, c_locale); } else #endif { errno = 0; -#ifdef HAVE_STRTOF - f = strtof(s, &x); -#else f = strtod(s, &x); -#endif } if (!x || *x || errno != 0) r = -1; else - *ret_f = f; + *ret_d = f; return r; } @@ -1774,10 +1799,11 @@ int pa_close_all(int except_fd, ...) { i = 0; if (except_fd >= 0) { + int fd; p[i++] = except_fd; - while ((p[i++] = va_arg(ap, int)) >= 0) - ; + while ((fd = va_arg(ap, int)) >= 0) + p[i++] = fd; } p[i] = -1; @@ -1803,6 +1829,7 @@ int pa_close_allv(const int except_fds[]) { struct dirent *de; while ((de = readdir(d))) { + pa_bool_t found; long l; char *e = NULL; int i; @@ -1826,17 +1853,23 @@ int pa_close_allv(const int except_fds[]) { return -1; } - if (fd <= 3) + if (fd < 3) continue; if (fd == dirfd(d)) continue; + found = FALSE; for (i = 0; except_fds[i] >= 0; i++) - if (except_fds[i] == fd) - continue; + if (except_fds[i] == fd) { + found = TRUE; + break; + } + + if (found) + continue; - if (close(fd) < 0) { + if (pa_close(fd) < 0) { saved_errno = errno; closedir(d); errno = saved_errno; @@ -1890,10 +1923,11 @@ int pa_unblock_sigs(int except, ...) { i = 0; if (except >= 1) { + int sig; p[i++] = except; - while ((p[i++] = va_arg(ap, int)) >= 0) - ; + while ((sig = va_arg(ap, int)) >= 0) + p[i++] = sig; } p[i] = -1; @@ -1957,12 +1991,12 @@ int pa_reset_sigsv(const int except[]) { int sig; for (sig = 1; sig < _NSIG; sig++) { - int reset = 1; + pa_bool_t reset = TRUE; switch (sig) { case SIGKILL: case SIGSTOP: - reset = 0; + reset = FALSE; break; default: { @@ -1970,7 +2004,7 @@ int pa_reset_sigsv(const int except[]) { for (i = 0; except[i] > 0; i++) { if (sig == except[i]) { - reset = 0; + reset = FALSE; break; } } @@ -2000,3 +2034,12 @@ void pa_set_env(const char *key, const char *value) { putenv(pa_sprintf_malloc("%s=%s", key, value)); } + +pa_bool_t pa_in_system_mode(void) { + const char *e; + + if (!(e = getenv("PULSE_SYSTEM"))) + return FALSE; + + return !!atoi(e); +} |