summaryrefslogtreecommitdiffstats
path: root/src/pulsecore/pid.c
diff options
context:
space:
mode:
authorLennart Poettering <lennart@poettering.net>2008-05-21 22:50:58 +0000
committerLennart Poettering <lennart@poettering.net>2008-05-21 22:50:58 +0000
commit28405e3dcfba8b8b813678a79ca8194cccc7bb76 (patch)
tree692168798eb41ad24db7834f230e8e6e7a8e0b0d /src/pulsecore/pid.c
parent6be0c7526cf6451d4089b5123ceae6fa3911e512 (diff)
big mumbo jumo of interleaved patches.
* Use seperate "state" and "config" paths * Pass the fact that we are in system mode via an env var $PULSE_SYSTEM instead of as var in pa_core * Properly check proc name when checking PID files. Don't check exename, because we cannot read that for other uids git-svn-id: file:///home/lennart/svn/public/pulseaudio/trunk@2480 fefdeb5f-60dc-0310-8127-8f9354f1896f
Diffstat (limited to 'src/pulsecore/pid.c')
-rw-r--r--src/pulsecore/pid.c97
1 files changed, 73 insertions, 24 deletions
diff --git a/src/pulsecore/pid.c b/src/pulsecore/pid.c
index 2ff132bb..9e93234f 100644
--- a/src/pulsecore/pid.c
+++ b/src/pulsecore/pid.c
@@ -140,8 +140,51 @@ fail:
return -1;
}
+static int proc_name_ours(pid_t pid, const char *procname) {
+#ifdef __linux__
+ char bn[PATH_MAX];
+ FILE *f;
+
+ pa_snprintf(bn, sizeof(bn), "/proc/%lu/stat", (unsigned long) pid);
+
+ if (!(f = fopen(bn, "r"))) {
+ pa_log_info("Failed to open %s: %s", bn, pa_cstrerror(errno));
+ return -1;
+ } else {
+ char *expected;
+ pa_bool_t good;
+ char stored[64];
+
+ if (!(fgets(stored, sizeof(stored), f))) {
+ pa_log_info("Failed to read from %s: %s", bn, feof(f) ? "EOF" : pa_cstrerror(errno));
+ fclose(f);
+ return -1;
+ }
+
+ fclose(f);
+
+ expected = pa_sprintf_malloc("%lu (%s)", (unsigned long) pid, procname);
+ good = pa_startswith(stored, expected);
+ pa_xfree(expected);
+
+#if !defined(__OPTIMIZE__)
+ if (!good) {
+ /* libtool likes to rename our binary names ... */
+ expected = pa_sprintf_malloc("%lu (lt-%s)", (unsigned long) pid, procname);
+ good = pa_startswith(stored, expected);
+ pa_xfree(expected);
+ }
+#endif
+
+ return !!good;
+ }
+#endif
+
+ return 1;
+}
+
/* Create a new PID file for the current process. */
-int pa_pid_file_create(void) {
+int pa_pid_file_create(const char *procname) {
int fd = -1;
int ret = -1;
char t[20];
@@ -153,7 +196,8 @@ int pa_pid_file_create(void) {
HANDLE process;
#endif
- fn = pa_runtime_path("pid");
+ if (!(fn = pa_runtime_path("pid")))
+ goto fail;
if ((fd = open_pid_file(fn, O_CREAT|O_RDWR)) < 0)
goto fail;
@@ -161,14 +205,23 @@ int pa_pid_file_create(void) {
if ((pid = read_pid(fn, fd)) == (pid_t) -1)
pa_log_warn("Corrupt PID file, overwriting.");
else if (pid > 0) {
+
#ifdef OS_IS_WIN32
if ((process = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, pid)) != NULL) {
CloseHandle(process);
#else
if (kill(pid, 0) >= 0 || errno != ESRCH) {
#endif
- pa_log("Daemon already running.");
- goto fail;
+ int ours = 1;
+
+ if (procname)
+ if ((ours = proc_name_ours(pid, procname)) < 0)
+ goto fail;
+
+ if (ours) {
+ pa_log("Daemon already running.");
+ goto fail;
+ }
}
pa_log_warn("Stale PID file, overwriting.");
@@ -212,7 +265,8 @@ int pa_pid_file_remove(void) {
int ret = -1;
pid_t pid;
- fn = pa_runtime_path("pid");
+ if (!(fn = pa_runtime_path("pid")))
+ goto fail;
if ((fd = open_pid_file(fn, O_RDWR)) < 0) {
pa_log_warn("Failed to open PID file '%s': %s", fn, pa_cstrerror(errno));
@@ -234,7 +288,7 @@ int pa_pid_file_remove(void) {
#ifdef OS_IS_WIN32
pa_lock_fd(fd, 0);
- close(fd);
+ pa_close(fd);
fd = -1;
#endif
@@ -265,8 +319,8 @@ fail:
* exists and the PID therein too. Returns 0 on succcess, -1
* otherwise. If pid is non-NULL and a running daemon was found,
* return its PID therein */
-int pa_pid_file_check_running(pid_t *pid, const char *binary_name) {
- return pa_pid_file_kill(0, pid, binary_name);
+int pa_pid_file_check_running(pid_t *pid, const char *procname) {
+ return pa_pid_file_kill(0, pid, procname);
}
#ifndef OS_IS_WIN32
@@ -274,7 +328,7 @@ int pa_pid_file_check_running(pid_t *pid, const char *binary_name) {
/* Kill a current running daemon. Return non-zero on success, -1
* otherwise. If successful *pid contains the PID of the daemon
* process. */
-int pa_pid_file_kill(int sig, pid_t *pid, const char *binary_name) {
+int pa_pid_file_kill(int sig, pid_t *pid, const char *procname) {
int fd = -1;
char *fn;
int ret = -1;
@@ -282,10 +336,12 @@ int pa_pid_file_kill(int sig, pid_t *pid, const char *binary_name) {
#ifdef __linux__
char *e = NULL;
#endif
+
if (!pid)
pid = &_pid;
- fn = pa_runtime_path("pid");
+ if (!(fn = pa_runtime_path("pid")))
+ goto fail;
if ((fd = open_pid_file(fn, O_RDONLY)) < 0)
goto fail;
@@ -293,22 +349,15 @@ int pa_pid_file_kill(int sig, pid_t *pid, const char *binary_name) {
if ((*pid = read_pid(fn, fd)) == (pid_t) -1)
goto fail;
-#ifdef __linux__
- if (binary_name) {
- pa_snprintf(fn, sizeof(fn), "/proc/%lu/exe", (unsigned long) pid);
+ if (procname) {
+ int ours;
- if ((e = pa_readlink(fn))) {
- char *f = pa_path_get_filename(e);
- if (strcmp(f, binary_name)
-#if !defined(__OPTIMIZE__)
- /* libtool likes to rename our binary names ... */
- && !(pa_startswith(f, "lt-") && strcmp(f+3, binary_name) == 0)
-#endif
- )
- goto fail;
- }
+ if ((ours = proc_name_ours(*pid, procname)) < 0)
+ goto fail;
+
+ if (!ours)
+ goto fail;
}
-#endif
ret = kill(*pid, sig);