merge glitch-free branch back into trunk
[profile/ivi/pulseaudio.git] / src / pulsecore / pid.c
1 /* $Id$ */
2
3 /***
4   This file is part of PulseAudio.
5
6   Copyright 2004-2006 Lennart Poettering
7   Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
8
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.
13
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.
18
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
22   USA.
23 ***/
24
25 #ifdef HAVE_CONFIG_H
26 #include <config.h>
27 #endif
28
29 #include <fcntl.h>
30 #include <unistd.h>
31 #include <errno.h>
32 #include <sys/types.h>
33 #include <sys/stat.h>
34 #include <string.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <limits.h>
38 #include <signal.h>
39
40 #ifdef HAVE_WINDOWS_H
41 #include <windows.h>
42 #endif
43
44 #include <pulse/xmalloc.h>
45 #include <pulse/util.h>
46
47 #include <pulsecore/core-error.h>
48 #include <pulsecore/core-util.h>
49 #include <pulsecore/log.h>
50 #include <pulsecore/macro.h>
51
52 #include "pid.h"
53
54 /* Read the PID data from the file descriptor fd, and return it. If no
55  * pid could be read, return 0, on failure (pid_t) -1 */
56 static pid_t read_pid(const char *fn, int fd) {
57     ssize_t r;
58     char t[20], *e;
59     uint32_t pid;
60
61     pa_assert(fn);
62     pa_assert(fd >= 0);
63
64     if ((r = pa_loop_read(fd, t, sizeof(t)-1, NULL)) < 0) {
65         pa_log_warn("Failed to read PID file '%s': %s", fn, pa_cstrerror(errno));
66         return (pid_t) -1;
67     }
68
69     if (r == 0)
70         return (pid_t) 0;
71
72     t[r] = 0;
73     if ((e = strchr(t, '\n')))
74         *e = 0;
75
76     if (pa_atou(t, &pid) < 0) {
77         pa_log_warn("Failed to parse PID file '%s'", fn);
78         return (pid_t) -1;
79     }
80
81     return (pid_t) pid;
82 }
83
84 static int open_pid_file(const char *fn, int mode) {
85     int fd = -1;
86
87     pa_assert(fn);
88
89     for (;;) {
90         struct stat st;
91
92         if ((fd = open(fn, mode
93 #ifdef O_NOCTTY
94                        |O_NOCTTY
95 #endif
96 #ifdef O_NOFOLLOW
97                        |O_NOFOLLOW
98 #endif
99                        , S_IRUSR|S_IWUSR
100              )) < 0) {
101             if (mode != O_RDONLY || errno != ENOENT)
102                 pa_log_warn("Failed to open PID file '%s': %s", fn, pa_cstrerror(errno));
103             goto fail;
104         }
105
106         /* Try to lock the file. If that fails, go without */
107         if (pa_lock_fd(fd, 1) < 0)
108             goto fail;
109
110         if (fstat(fd, &st) < 0) {
111             pa_log_warn("Failed to fstat() PID file '%s': %s", fn, pa_cstrerror(errno));
112             goto fail;
113         }
114
115         /* Does the file still exist in the file system? When ye, w're done, otherwise restart */
116         if (st.st_nlink >= 1)
117             break;
118
119         if (pa_lock_fd(fd, 0) < 0)
120             goto fail;
121
122         if (pa_close(fd) < 0) {
123             pa_log_warn("Failed to close file '%s': %s", fn, pa_cstrerror(errno));
124             fd = -1;
125             goto fail;
126         }
127
128         fd = -1;
129     }
130
131     return fd;
132
133 fail:
134
135     if (fd >= 0) {
136         pa_lock_fd(fd, 0);
137         pa_close(fd);
138     }
139
140     return -1;
141 }
142
143 /* Create a new PID file for the current process. */
144 int pa_pid_file_create(void) {
145     int fd = -1;
146     int ret = -1;
147     char t[20];
148     pid_t pid;
149     size_t l;
150     char *fn;
151
152 #ifdef OS_IS_WIN32
153     HANDLE process;
154 #endif
155
156     fn = pa_runtime_path("pid");
157
158     if ((fd = open_pid_file(fn, O_CREAT|O_RDWR)) < 0)
159         goto fail;
160
161     if ((pid = read_pid(fn, fd)) == (pid_t) -1)
162         pa_log_warn("Corrupt PID file, overwriting.");
163     else if (pid > 0) {
164 #ifdef OS_IS_WIN32
165         if ((process = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, pid)) != NULL) {
166             CloseHandle(process);
167 #else
168         if (kill(pid, 0) >= 0 || errno != ESRCH) {
169 #endif
170             pa_log("Daemon already running.");
171             goto fail;
172         }
173
174         pa_log_warn("Stale PID file, overwriting.");
175     }
176
177     /* Overwrite the current PID file */
178     if (lseek(fd, 0, SEEK_SET) == (off_t) -1 || ftruncate(fd, 0) < 0) {
179         pa_log("Failed to truncate PID file '%s': %s", fn, pa_cstrerror(errno));
180         goto fail;
181     }
182
183     pa_snprintf(t, sizeof(t), "%lu\n", (unsigned long) getpid());
184     l = strlen(t);
185
186     if (pa_loop_write(fd, t, l, NULL) != (ssize_t) l) {
187         pa_log("Failed to write PID file.");
188         goto fail;
189     }
190
191     ret = 0;
192
193 fail:
194     if (fd >= 0) {
195         pa_lock_fd(fd, 0);
196
197         if (pa_close(fd) < 0) {
198             pa_log("Failed to close PID file '%s': %s", fn, pa_cstrerror(errno));
199             ret = -1;
200         }
201     }
202
203     pa_xfree(fn);
204
205     return ret;
206 }
207
208 /* Remove the PID file, if it is ours */
209 int pa_pid_file_remove(void) {
210     int fd = -1;
211     char *fn;
212     int ret = -1;
213     pid_t pid;
214
215     fn = pa_runtime_path("pid");
216
217     if ((fd = open_pid_file(fn, O_RDWR)) < 0) {
218         pa_log_warn("Failed to open PID file '%s': %s", fn, pa_cstrerror(errno));
219         goto fail;
220     }
221
222     if ((pid = read_pid(fn, fd)) == (pid_t) -1)
223         goto fail;
224
225     if (pid != getpid()) {
226         pa_log("PID file '%s' not mine!", fn);
227         goto fail;
228     }
229
230     if (ftruncate(fd, 0) < 0) {
231         pa_log_warn("Failed to truncate PID file '%s': %s", fn, pa_cstrerror(errno));
232         goto fail;
233     }
234
235 #ifdef OS_IS_WIN32
236     pa_lock_fd(fd, 0);
237     close(fd);
238     fd = -1;
239 #endif
240
241     if (unlink(fn) < 0) {
242         pa_log_warn("Failed to remove PID file '%s': %s", fn, pa_cstrerror(errno));
243         goto fail;
244     }
245
246     ret = 0;
247
248 fail:
249
250     if (fd >= 0) {
251         pa_lock_fd(fd, 0);
252
253         if (pa_close(fd) < 0) {
254             pa_log_warn("Failed to close PID file '%s': %s", fn, pa_cstrerror(errno));
255             ret = -1;
256         }
257     }
258
259     pa_xfree(fn);
260
261     return ret;
262 }
263
264 /* Check whether the daemon is currently running, i.e. if a PID file
265  * exists and the PID therein too. Returns 0 on succcess, -1
266  * otherwise. If pid is non-NULL and a running daemon was found,
267  * return its PID therein */
268 int pa_pid_file_check_running(pid_t *pid, const char *binary_name) {
269     return pa_pid_file_kill(0, pid, binary_name);
270 }
271
272 #ifndef OS_IS_WIN32
273
274 /* Kill a current running daemon. Return non-zero on success, -1
275  * otherwise. If successful *pid contains the PID of the daemon
276  * process. */
277 int pa_pid_file_kill(int sig, pid_t *pid, const char *binary_name) {
278     int fd = -1;
279     char *fn;
280     int ret = -1;
281     pid_t _pid;
282 #ifdef __linux__
283     char *e = NULL;
284 #endif
285     if (!pid)
286         pid = &_pid;
287
288     fn = pa_runtime_path("pid");
289
290     if ((fd = open_pid_file(fn, O_RDONLY)) < 0)
291         goto fail;
292
293     if ((*pid = read_pid(fn, fd)) == (pid_t) -1)
294         goto fail;
295
296 #ifdef __linux__
297     if (binary_name) {
298         pa_snprintf(fn, sizeof(fn), "/proc/%lu/exe", (unsigned long) pid);
299
300         if ((e = pa_readlink(fn))) {
301             char *f = pa_path_get_filename(e);
302             if (strcmp(f, binary_name)
303 #if !defined(__OPTIMIZE__)
304                 /* libtool likes to rename our binary names ... */
305                 && !(pa_startswith(f, "lt-") && strcmp(f+3, binary_name) == 0)
306 #endif
307             )
308                 goto fail;
309         }
310     }
311 #endif
312
313     ret = kill(*pid, sig);
314
315 fail:
316
317     if (fd >= 0) {
318         pa_lock_fd(fd, 0);
319         pa_close(fd);
320     }
321
322 #ifdef __linux__
323     pa_xfree(e);
324 #endif
325
326     pa_xfree(fn);
327
328     return ret;
329
330 }
331
332 #else /* OS_IS_WIN32 */
333
334 int pa_pid_file_kill(int sig, pid_t *pid, const char *exe_name) {
335     return -1;
336 }
337
338 #endif