4 This file is part of PulseAudio.
6 Copyright 2004-2006 Lennart Poettering
7 Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
9 PulseAudio is free software; you can redistribute it and/or modify
10 it under the terms of the GNU Lesser General Public License as
11 published by the Free Software Foundation; either version 2 of the
12 License, or (at your option) any later version.
14 PulseAudio is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 General Public License for more details.
19 You should have received a copy of the GNU Lesser General Public
20 License along with PulseAudio; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
32 #include <sys/types.h>
45 #include <pulse/xmalloc.h>
47 #include <pulsecore/core-error.h>
48 #include <pulsecore/core-util.h>
49 #include <pulsecore/log.h>
53 /* Read the PID data from the file descriptor fd, and return it. If no
54 * pid could be read, return 0, on failure (pid_t) -1 */
55 static pid_t read_pid(const char *fn, int fd) {
60 assert(fn && fd >= 0);
62 if ((r = pa_loop_read(fd, t, sizeof(t)-1, NULL)) < 0) {
63 pa_log_warn("WARNING: failed to read PID file '%s': %s",
64 fn, pa_cstrerror(errno));
72 if ((e = strchr(t, '\n')))
75 if (pa_atou(t, &pid) < 0) {
76 pa_log("WARNING: failed to parse PID file '%s'", fn);
83 static int open_pid_file(const char *fn, int mode) {
89 if ((fd = open(fn, mode, S_IRUSR|S_IWUSR)) < 0) {
90 if (mode != O_RDONLY || errno != ENOENT)
91 pa_log_warn("WARNING: failed to open PID file '%s': %s",
92 fn, pa_cstrerror(errno));
96 /* Try to lock the file. If that fails, go without */
97 if (pa_lock_fd(fd, 1) < 0)
100 if (fstat(fd, &st) < 0) {
101 pa_log_warn("WARNING: failed to fstat() PID file '%s': %s",
102 fn, pa_cstrerror(errno));
106 /* Does the file still exist in the file system? When ye, w're done, otherwise restart */
107 if (st.st_nlink >= 1)
110 if (pa_lock_fd(fd, 0) < 0)
114 pa_log_warn("WARNING: failed to close file '%s': %s",
115 fn, pa_cstrerror(errno));
134 /* Create a new PID file for the current process. */
135 int pa_pid_file_create(void) {
147 pa_runtime_path("pid", fn, sizeof(fn));
149 if ((fd = open_pid_file(fn, O_CREAT|O_RDWR)) < 0)
152 if ((pid = read_pid(fn, fd)) == (pid_t) -1)
153 pa_log("corrupt PID file, overwriting.");
156 if ((process = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, pid)) != NULL) {
157 CloseHandle(process);
159 if (kill(pid, 0) >= 0 || errno != ESRCH) {
161 pa_log("daemon already running.");
165 pa_log("stale PID file, overwriting.");
168 /* Overwrite the current PID file */
169 if (lseek(fd, 0, SEEK_SET) == (off_t) -1 || ftruncate(fd, 0) < 0) {
170 pa_log("failed to truncate PID file '%s': %s",
171 fn, pa_cstrerror(errno));
175 snprintf(t, sizeof(t), "%lu\n", (unsigned long) getpid());
178 if (pa_loop_write(fd, t, l, NULL) != (ssize_t) l) {
179 pa_log("failed to write PID file.");
194 /* Remove the PID file, if it is ours */
195 int pa_pid_file_remove(void) {
201 pa_runtime_path("pid", fn, sizeof(fn));
203 if ((fd = open_pid_file(fn, O_RDWR)) < 0) {
204 pa_log_warn("WARNING: failed to open PID file '%s': %s",
205 fn, pa_cstrerror(errno));
209 if ((pid = read_pid(fn, fd)) == (pid_t) -1)
212 if (pid != getpid()) {
213 pa_log("WARNING: PID file '%s' not mine!", fn);
217 if (ftruncate(fd, 0) < 0) {
218 pa_log_warn("WARNING: failed to truncate PID file '%s': %s",
219 fn, pa_cstrerror(errno));
229 if (unlink(fn) < 0) {
230 pa_log_warn("WARNING: failed to remove PID file '%s': %s",
231 fn, pa_cstrerror(errno));
247 /* Check whether the daemon is currently running, i.e. if a PID file
248 * exists and the PID therein too. Returns 0 on succcess, -1
249 * otherwise. If pid is non-NULL and a running daemon was found,
250 * return its PID therein */
251 int pa_pid_file_check_running(pid_t *pid) {
252 return pa_pid_file_kill(0, pid);
257 /* Kill a current running daemon. Return non-zero on success, -1
258 * otherwise. If successful *pid contains the PID of the daemon
260 int pa_pid_file_kill(int sig, pid_t *pid) {
269 pa_runtime_path("pid", fn, sizeof(fn));
271 if ((fd = open_pid_file(fn, O_RDONLY)) < 0)
274 if ((*pid = read_pid(fn, fd)) == (pid_t) -1)
277 ret = kill(*pid, sig);
290 #else /* OS_IS_WIN32 */
292 int pa_pid_file_kill(int sig, pid_t *pid) {