summaryrefslogtreecommitdiffstats
path: root/src/pulsecore/pid.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/pulsecore/pid.c')
-rw-r--r--src/pulsecore/pid.c40
1 files changed, 30 insertions, 10 deletions
diff --git a/src/pulsecore/pid.c b/src/pulsecore/pid.c
index addb17cc..bf9ba983 100644
--- a/src/pulsecore/pid.c
+++ b/src/pulsecore/pid.c
@@ -73,6 +73,7 @@ static pid_t read_pid(const char *fn, int fd) {
if (pa_atou(t, &pid) < 0) {
pa_log_warn("Failed to parse PID file '%s'", fn);
+ errno = EINVAL;
return (pid_t) -1;
}
@@ -110,7 +111,7 @@ static int open_pid_file(const char *fn, int mode) {
goto fail;
}
- /* Does the file still exist in the file system? When ye, w're done, otherwise restart */
+ /* Does the file still exist in the file system? When yes, we're done, otherwise restart */
if (st.st_nlink >= 1)
break;
@@ -131,8 +132,10 @@ static int open_pid_file(const char *fn, int mode) {
fail:
if (fd >= 0) {
+ int saved_errno = errno;
pa_lock_fd(fd, 0);
pa_close(fd);
+ errno = saved_errno;
}
return -1;
@@ -154,8 +157,11 @@ static int proc_name_ours(pid_t pid, const char *procname) {
char stored[64];
if (!(fgets(stored, sizeof(stored), f))) {
+ int saved_errno = feof(f) ? EINVAL : errno;
pa_log_info("Failed to read from %s: %s", bn, feof(f) ? "EOF" : pa_cstrerror(errno));
fclose(f);
+
+ errno = saved_errno;
return -1;
}
@@ -165,20 +171,22 @@ static int proc_name_ours(pid_t pid, const char *procname) {
good = pa_startswith(stored, expected);
pa_xfree(expected);
-#if !defined(__OPTIMIZE__)
+/*#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
+/*#endif*/
return !!good;
}
-#endif
+#else
return 1;
+#endif
+
}
/* Create a new PID file for the current process. */
@@ -203,6 +211,7 @@ int pa_pid_file_create(const char *procname) {
if ((pid = read_pid(fn, fd)) == (pid_t) -1)
pa_log_warn("Corrupt PID file, overwriting.");
else if (pid > 0) {
+ int ours = 1;
#ifdef OS_IS_WIN32
if ((process = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, pid)) != NULL) {
@@ -210,11 +219,13 @@ int pa_pid_file_create(const char *procname) {
#else
if (kill(pid, 0) >= 0 || errno != ESRCH) {
#endif
- int ours = 1;
if (procname)
- if ((ours = proc_name_ours(pid, procname)) < 0)
+ if ((ours = proc_name_ours(pid, procname)) < 0) {
+ pa_log_warn("Could not check to see if pid %lu is a pulseaudio process. "
+ "Asssuming it is and the daemon is already running.", (unsigned long) pid);
goto fail;
+ }
if (ours) {
pa_log("Daemon already running.");
@@ -227,7 +238,7 @@ int pa_pid_file_create(const char *procname) {
}
/* Overwrite the current PID file */
- if (lseek(fd, 0, SEEK_SET) == (off_t) -1 || ftruncate(fd, 0) < 0) {
+ if (lseek(fd, (off_t) 0, SEEK_SET) == (off_t) -1 || ftruncate(fd, (off_t) 0) < 0) {
pa_log("Failed to truncate PID file '%s': %s", fn, pa_cstrerror(errno));
goto fail;
}
@@ -280,7 +291,7 @@ int pa_pid_file_remove(void) {
goto fail;
}
- if (ftruncate(fd, 0) < 0) {
+ if (ftruncate(fd, (off_t) 0) < 0) {
pa_log_warn("Failed to truncate PID file '%s': %s", fn, pa_cstrerror(errno));
goto fail;
}
@@ -342,8 +353,13 @@ int pa_pid_file_kill(int sig, pid_t *pid, const char *procname) {
if (!(fn = pa_runtime_path("pid")))
goto fail;
- if ((fd = open_pid_file(fn, O_RDONLY)) < 0)
+ if ((fd = open_pid_file(fn, O_RDONLY)) < 0) {
+
+ if (errno == ENOENT)
+ errno = ESRCH;
+
goto fail;
+ }
if ((*pid = read_pid(fn, fd)) == (pid_t) -1)
goto fail;
@@ -354,8 +370,10 @@ int pa_pid_file_kill(int sig, pid_t *pid, const char *procname) {
if ((ours = proc_name_ours(*pid, procname)) < 0)
goto fail;
- if (!ours)
+ if (!ours) {
+ errno = ESRCH;
goto fail;
+ }
}
ret = kill(*pid, sig);
@@ -363,8 +381,10 @@ int pa_pid_file_kill(int sig, pid_t *pid, const char *procname) {
fail:
if (fd >= 0) {
+ int saved_errno = errno;
pa_lock_fd(fd, 0);
pa_close(fd);
+ errno = saved_errno;
}
#ifdef __linux__