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>
44 #include <pulse/xmalloc.h>
46 #include <pulsecore/core-error.h>
47 #include <pulsecore/core-util.h>
48 #include <pulsecore/log.h>
49 #include <pulsecore/macro.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
) {
63 if ((r
= pa_loop_read(fd
, t
, sizeof(t
)-1, NULL
)) < 0) {
64 pa_log_warn("Failed to read PID file '%s': %s", fn
, pa_cstrerror(errno
));
72 if ((e
= strchr(t
, '\n')))
75 if (pa_atou(t
, &pid
) < 0) {
76 pa_log_warn("Failed to parse PID file '%s'", fn
);
83 static int open_pid_file(const char *fn
, int mode
) {
91 if ((fd
= open(fn
, mode
100 if (mode
!= O_RDONLY
|| errno
!= ENOENT
)
101 pa_log_warn("Failed to open PID file '%s': %s", fn
, pa_cstrerror(errno
));
105 /* Try to lock the file. If that fails, go without */
106 if (pa_lock_fd(fd
, 1) < 0)
109 if (fstat(fd
, &st
) < 0) {
110 pa_log_warn("Failed to fstat() PID file '%s': %s", fn
, pa_cstrerror(errno
));
114 /* Does the file still exist in the file system? When ye, w're done, otherwise restart */
115 if (st
.st_nlink
>= 1)
118 if (pa_lock_fd(fd
, 0) < 0)
121 if (pa_close(fd
) < 0) {
122 pa_log_warn("Failed to close file '%s': %s", fn
, pa_cstrerror(errno
));
142 /* Create a new PID file for the current process. */
143 int pa_pid_file_create(void) {
155 pa_runtime_path("pid", fn
, sizeof(fn
));
157 if ((fd
= open_pid_file(fn
, O_CREAT
|O_RDWR
)) < 0)
160 if ((pid
= read_pid(fn
, fd
)) == (pid_t
) -1)
161 pa_log_warn("Corrupt PID file, overwriting.");
164 if ((process
= OpenProcess(PROCESS_QUERY_INFORMATION
, FALSE
, pid
)) != NULL
) {
165 CloseHandle(process
);
167 if (kill(pid
, 0) >= 0 || errno
!= ESRCH
) {
169 pa_log("Daemon already running.");
173 pa_log_warn("Stale PID file, overwriting.");
176 /* Overwrite the current PID file */
177 if (lseek(fd
, 0, SEEK_SET
) == (off_t
) -1 || ftruncate(fd
, 0) < 0) {
178 pa_log("Failed to truncate PID file '%s': %s", fn
, pa_cstrerror(errno
));
182 pa_snprintf(t
, sizeof(t
), "%lu\n", (unsigned long) getpid());
185 if (pa_loop_write(fd
, t
, l
, NULL
) != (ssize_t
) l
) {
186 pa_log("Failed to write PID file.");
196 if (pa_close(fd
) < 0) {
197 pa_log("Failed to close PID file '%s': %s", fn
, pa_cstrerror(errno
));
205 /* Remove the PID file, if it is ours */
206 int pa_pid_file_remove(void) {
212 pa_runtime_path("pid", fn
, sizeof(fn
));
214 if ((fd
= open_pid_file(fn
, O_RDWR
)) < 0) {
215 pa_log_warn("Failed to open PID file '%s': %s", fn
, pa_cstrerror(errno
));
219 if ((pid
= read_pid(fn
, fd
)) == (pid_t
) -1)
222 if (pid
!= getpid()) {
223 pa_log("PID file '%s' not mine!", fn
);
227 if (ftruncate(fd
, 0) < 0) {
228 pa_log_warn("Failed to truncate PID file '%s': %s", fn
, pa_cstrerror(errno
));
238 if (unlink(fn
) < 0) {
239 pa_log_warn("Failed to remove PID file '%s': %s", fn
, pa_cstrerror(errno
));
250 if (pa_close(fd
) < 0) {
251 pa_log_warn("Failed to close PID file '%s': %s", fn
, pa_cstrerror(errno
));
259 /* Check whether the daemon is currently running, i.e. if a PID file
260 * exists and the PID therein too. Returns 0 on succcess, -1
261 * otherwise. If pid is non-NULL and a running daemon was found,
262 * return its PID therein */
263 int pa_pid_file_check_running(pid_t
*pid
) {
264 return pa_pid_file_kill(0, pid
);
269 /* Kill a current running daemon. Return non-zero on success, -1
270 * otherwise. If successful *pid contains the PID of the daemon
272 int pa_pid_file_kill(int sig
, pid_t
*pid
) {
281 pa_runtime_path("pid", fn
, sizeof(fn
));
283 if ((fd
= open_pid_file(fn
, O_RDONLY
)) < 0)
286 if ((*pid
= read_pid(fn
, fd
)) == (pid_t
) -1)
289 ret
= kill(*pid
, sig
);
302 #else /* OS_IS_WIN32 */
304 int pa_pid_file_kill(int sig
, pid_t
*pid
) {