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.c74
1 files changed, 43 insertions, 31 deletions
diff --git a/src/pulsecore/pid.c b/src/pulsecore/pid.c
index 5e670e17..55ff2088 100644
--- a/src/pulsecore/pid.c
+++ b/src/pulsecore/pid.c
@@ -33,7 +33,6 @@
#include <sys/stat.h>
#include <string.h>
#include <stdio.h>
-#include <assert.h>
#include <stdlib.h>
#include <limits.h>
#include <signal.h>
@@ -47,6 +46,7 @@
#include <pulsecore/core-error.h>
#include <pulsecore/core-util.h>
#include <pulsecore/log.h>
+#include <pulsecore/macro.h>
#include "pid.h"
@@ -57,11 +57,11 @@ static pid_t read_pid(const char *fn, int fd) {
char t[20], *e;
uint32_t pid;
- assert(fn && fd >= 0);
+ pa_assert(fn);
+ pa_assert(fd >= 0);
if ((r = pa_loop_read(fd, t, sizeof(t)-1, NULL)) < 0) {
- pa_log_warn("WARNING: failed to read PID file '%s': %s",
- fn, pa_cstrerror(errno));
+ pa_log_warn("Failed to read PID file '%s': %s", fn, pa_cstrerror(errno));
return (pid_t) -1;
}
@@ -73,7 +73,7 @@ static pid_t read_pid(const char *fn, int fd) {
*e = 0;
if (pa_atou(t, &pid) < 0) {
- pa_log("WARNING: failed to parse PID file '%s'", fn);
+ pa_log_warn("Failed to parse PID file '%s'", fn);
return (pid_t) -1;
}
@@ -83,13 +83,22 @@ static pid_t read_pid(const char *fn, int fd) {
static int open_pid_file(const char *fn, int mode) {
int fd = -1;
+ pa_assert(fn);
+
for (;;) {
struct stat st;
- if ((fd = open(fn, mode, S_IRUSR|S_IWUSR)) < 0) {
+ if ((fd = open(fn, mode
+#ifdef O_NOCTTY
+ |O_NOCTTY
+#endif
+#ifdef O_NOFOLLOW
+ |O_NOFOLLOW
+#endif
+ , S_IRUSR|S_IWUSR
+ )) < 0) {
if (mode != O_RDONLY || errno != ENOENT)
- pa_log_warn("WARNING: failed to open PID file '%s': %s",
- fn, pa_cstrerror(errno));
+ pa_log_warn("Failed to open PID file '%s': %s", fn, pa_cstrerror(errno));
goto fail;
}
@@ -98,8 +107,7 @@ static int open_pid_file(const char *fn, int mode) {
goto fail;
if (fstat(fd, &st) < 0) {
- pa_log_warn("WARNING: failed to fstat() PID file '%s': %s",
- fn, pa_cstrerror(errno));
+ pa_log_warn("Failed to fstat() PID file '%s': %s", fn, pa_cstrerror(errno));
goto fail;
}
@@ -110,9 +118,9 @@ static int open_pid_file(const char *fn, int mode) {
if (pa_lock_fd(fd, 0) < 0)
goto fail;
- if (close(fd) < 0) {
- pa_log_warn("WARNING: failed to close file '%s': %s",
- fn, pa_cstrerror(errno));
+ if (pa_close(fd) < 0) {
+ pa_log_warn("Failed to close file '%s': %s", fn, pa_cstrerror(errno));
+ fd = -1;
goto fail;
}
@@ -125,7 +133,7 @@ fail:
if (fd >= 0) {
pa_lock_fd(fd, 0);
- close(fd);
+ pa_close(fd);
}
return -1;
@@ -150,7 +158,7 @@ int pa_pid_file_create(void) {
goto fail;
if ((pid = read_pid(fn, fd)) == (pid_t) -1)
- pa_log("corrupt PID file, overwriting.");
+ pa_log_warn("Corrupt PID file, overwriting.");
else if (pid > 0) {
#ifdef OS_IS_WIN32
if ((process = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, pid)) != NULL) {
@@ -158,25 +166,24 @@ int pa_pid_file_create(void) {
#else
if (kill(pid, 0) >= 0 || errno != ESRCH) {
#endif
- pa_log("daemon already running.");
+ pa_log("Daemon already running.");
goto fail;
}
- pa_log("stale PID file, overwriting.");
+ pa_log_warn("Stale PID file, overwriting.");
}
/* Overwrite the current PID file */
if (lseek(fd, 0, SEEK_SET) == (off_t) -1 || ftruncate(fd, 0) < 0) {
- pa_log("failed to truncate PID file '%s': %s",
- fn, pa_cstrerror(errno));
+ pa_log("Failed to truncate PID file '%s': %s", fn, pa_cstrerror(errno));
goto fail;
}
- snprintf(t, sizeof(t), "%lu\n", (unsigned long) getpid());
+ pa_snprintf(t, sizeof(t), "%lu\n", (unsigned long) getpid());
l = strlen(t);
if (pa_loop_write(fd, t, l, NULL) != (ssize_t) l) {
- pa_log("failed to write PID file.");
+ pa_log("Failed to write PID file.");
goto fail;
}
@@ -185,7 +192,11 @@ int pa_pid_file_create(void) {
fail:
if (fd >= 0) {
pa_lock_fd(fd, 0);
- close(fd);
+
+ if (pa_close(fd) < 0) {
+ pa_log("Failed to close PID file '%s': %s", fn, pa_cstrerror(errno));
+ ret = -1;
+ }
}
return ret;
@@ -201,8 +212,7 @@ int pa_pid_file_remove(void) {
pa_runtime_path("pid", fn, sizeof(fn));
if ((fd = open_pid_file(fn, O_RDWR)) < 0) {
- pa_log_warn("WARNING: failed to open PID file '%s': %s",
- fn, pa_cstrerror(errno));
+ pa_log_warn("Failed to open PID file '%s': %s", fn, pa_cstrerror(errno));
goto fail;
}
@@ -210,13 +220,12 @@ int pa_pid_file_remove(void) {
goto fail;
if (pid != getpid()) {
- pa_log("WARNING: PID file '%s' not mine!", fn);
+ pa_log("PID file '%s' not mine!", fn);
goto fail;
}
if (ftruncate(fd, 0) < 0) {
- pa_log_warn("WARNING: failed to truncate PID file '%s': %s",
- fn, pa_cstrerror(errno));
+ pa_log_warn("Failed to truncate PID file '%s': %s", fn, pa_cstrerror(errno));
goto fail;
}
@@ -227,8 +236,7 @@ int pa_pid_file_remove(void) {
#endif
if (unlink(fn) < 0) {
- pa_log_warn("WARNING: failed to remove PID file '%s': %s",
- fn, pa_cstrerror(errno));
+ pa_log_warn("Failed to remove PID file '%s': %s", fn, pa_cstrerror(errno));
goto fail;
}
@@ -238,7 +246,11 @@ fail:
if (fd >= 0) {
pa_lock_fd(fd, 0);
- close(fd);
+
+ if (pa_close(fd) < 0) {
+ pa_log_warn("Failed to close PID file '%s': %s", fn, pa_cstrerror(errno));
+ ret = -1;
+ }
}
return ret;
@@ -280,7 +292,7 @@ fail:
if (fd >= 0) {
pa_lock_fd(fd, 0);
- close(fd);
+ pa_close(fd);
}
return ret;