4 This file is part of PulseAudio.
6 PulseAudio is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as
8 published by the Free Software Foundation; either version 2 of the
9 License, or (at your option) any later version.
11 PulseAudio is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with PulseAudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
29 #include <sys/types.h>
42 #include <pulse/xmalloc.h>
44 #include <pulsecore/core-error.h>
45 #include <pulsecore/core-util.h>
46 #include <pulsecore/log.h>
50 /* Read the PID data from the file descriptor fd, and return it. If no
51 * pid could be read, return 0, on failure (pid_t) -1 */
52 static pid_t
read_pid(const char *fn
, int fd
) {
57 assert(fn
&& fd
>= 0);
59 if ((r
= pa_loop_read(fd
, t
, sizeof(t
)-1, NULL
)) < 0) {
60 pa_log_warn(__FILE__
": WARNING: failed to read PID file '%s': %s",
61 fn
, pa_cstrerror(errno
));
69 if ((e
= strchr(t
, '\n')))
72 if (pa_atou(t
, &pid
) < 0) {
73 pa_log(__FILE__
": WARNING: failed to parse PID file '%s'", fn
);
80 static int open_pid_file(const char *fn
, int mode
) {
86 if ((fd
= open(fn
, mode
, S_IRUSR
|S_IWUSR
)) < 0) {
87 if (mode
!= O_RDONLY
|| errno
!= ENOENT
)
88 pa_log_warn(__FILE__
": WARNING: failed to open PID file '%s': %s",
89 fn
, pa_cstrerror(errno
));
93 /* Try to lock the file. If that fails, go without */
94 if (pa_lock_fd(fd
, 1) < 0)
97 if (fstat(fd
, &st
) < 0) {
98 pa_log_warn(__FILE__
": WARNING: failed to fstat() PID file '%s': %s",
99 fn
, pa_cstrerror(errno
));
103 /* Does the file still exist in the file system? When ye, w're done, otherwise restart */
104 if (st
.st_nlink
>= 1)
107 if (pa_lock_fd(fd
, 0) < 0)
111 pa_log_warn(__FILE__
": WARNING: failed to close file '%s': %s",
112 fn
, pa_cstrerror(errno
));
131 /* Create a new PID file for the current process. */
132 int pa_pid_file_create(void) {
144 pa_runtime_path("pid", fn
, sizeof(fn
));
146 if ((fd
= open_pid_file(fn
, O_CREAT
|O_RDWR
)) < 0)
149 if ((pid
= read_pid(fn
, fd
)) == (pid_t
) -1)
150 pa_log(__FILE__
": corrupt PID file, overwriting.");
153 if ((process
= OpenProcess(PROCESS_QUERY_INFORMATION
, FALSE
, pid
)) != NULL
) {
154 CloseHandle(process
);
156 if (kill(pid
, 0) >= 0 || errno
!= ESRCH
) {
158 pa_log(__FILE__
": daemon already running.");
162 pa_log(__FILE__
": stale PID file, overwriting.");
165 /* Overwrite the current PID file */
166 if (lseek(fd
, 0, SEEK_SET
) == (off_t
) -1 || ftruncate(fd
, 0) < 0) {
167 pa_log(__FILE__
": failed to truncate PID file '%s': %s",
168 fn
, pa_cstrerror(errno
));
172 snprintf(t
, sizeof(t
), "%lu\n", (unsigned long) getpid());
175 if (pa_loop_write(fd
, t
, l
, NULL
) != (ssize_t
) l
) {
176 pa_log(__FILE__
": failed to write PID file.");
191 /* Remove the PID file, if it is ours */
192 int pa_pid_file_remove(void) {
198 pa_runtime_path("pid", fn
, sizeof(fn
));
200 if ((fd
= open_pid_file(fn
, O_RDWR
)) < 0) {
201 pa_log_warn(__FILE__
": WARNING: failed to open PID file '%s': %s",
202 fn
, pa_cstrerror(errno
));
206 if ((pid
= read_pid(fn
, fd
)) == (pid_t
) -1)
209 if (pid
!= getpid()) {
210 pa_log(__FILE__
": WARNING: PID file '%s' not mine!", fn
);
214 if (ftruncate(fd
, 0) < 0) {
215 pa_log_warn(__FILE__
": WARNING: failed to truncate PID file '%s': %s",
216 fn
, pa_cstrerror(errno
));
226 if (unlink(fn
) < 0) {
227 pa_log_warn(__FILE__
": WARNING: failed to remove PID file '%s': %s",
228 fn
, pa_cstrerror(errno
));
244 /* Check whether the daemon is currently running, i.e. if a PID file
245 * exists and the PID therein too. Returns 0 on succcess, -1
246 * otherwise. If pid is non-NULL and a running daemon was found,
247 * return its PID therein */
248 int pa_pid_file_check_running(pid_t
*pid
) {
249 return pa_pid_file_kill(0, pid
);
254 /* Kill a current running daemon. Return non-zero on success, -1
255 * otherwise. If successful *pid contains the PID of the daemon
257 int pa_pid_file_kill(int sig
, pid_t
*pid
) {
266 pa_runtime_path("pid", fn
, sizeof(fn
));
268 if ((fd
= open_pid_file(fn
, O_RDONLY
)) < 0)
271 if ((*pid
= read_pid(fn
, fd
)) == (pid_t
) -1)
274 ret
= kill(*pid
, sig
);
287 #else /* OS_IS_WIN32 */
289 int pa_pid_file_kill(int sig
, pid_t
*pid
) {