]> code.delx.au - pulseaudio/blob - src/pulsecore/pid.c
55ff2088b1a307b24d9152c3d530b2af4dc1a851
[pulseaudio] / 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
46 #include <pulsecore/core-error.h>
47 #include <pulsecore/core-util.h>
48 #include <pulsecore/log.h>
49 #include <pulsecore/macro.h>
50
51 #include "pid.h"
52
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) {
56 ssize_t r;
57 char t[20], *e;
58 uint32_t pid;
59
60 pa_assert(fn);
61 pa_assert(fd >= 0);
62
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));
65 return (pid_t) -1;
66 }
67
68 if (r == 0)
69 return (pid_t) 0;
70
71 t[r] = 0;
72 if ((e = strchr(t, '\n')))
73 *e = 0;
74
75 if (pa_atou(t, &pid) < 0) {
76 pa_log_warn("Failed to parse PID file '%s'", fn);
77 return (pid_t) -1;
78 }
79
80 return (pid_t) pid;
81 }
82
83 static int open_pid_file(const char *fn, int mode) {
84 int fd = -1;
85
86 pa_assert(fn);
87
88 for (;;) {
89 struct stat st;
90
91 if ((fd = open(fn, mode
92 #ifdef O_NOCTTY
93 |O_NOCTTY
94 #endif
95 #ifdef O_NOFOLLOW
96 |O_NOFOLLOW
97 #endif
98 , S_IRUSR|S_IWUSR
99 )) < 0) {
100 if (mode != O_RDONLY || errno != ENOENT)
101 pa_log_warn("Failed to open PID file '%s': %s", fn, pa_cstrerror(errno));
102 goto fail;
103 }
104
105 /* Try to lock the file. If that fails, go without */
106 if (pa_lock_fd(fd, 1) < 0)
107 goto fail;
108
109 if (fstat(fd, &st) < 0) {
110 pa_log_warn("Failed to fstat() PID file '%s': %s", fn, pa_cstrerror(errno));
111 goto fail;
112 }
113
114 /* Does the file still exist in the file system? When ye, w're done, otherwise restart */
115 if (st.st_nlink >= 1)
116 break;
117
118 if (pa_lock_fd(fd, 0) < 0)
119 goto fail;
120
121 if (pa_close(fd) < 0) {
122 pa_log_warn("Failed to close file '%s': %s", fn, pa_cstrerror(errno));
123 fd = -1;
124 goto fail;
125 }
126
127 fd = -1;
128 }
129
130 return fd;
131
132 fail:
133
134 if (fd >= 0) {
135 pa_lock_fd(fd, 0);
136 pa_close(fd);
137 }
138
139 return -1;
140 }
141
142 /* Create a new PID file for the current process. */
143 int pa_pid_file_create(void) {
144 int fd = -1;
145 int ret = -1;
146 char fn[PATH_MAX];
147 char t[20];
148 pid_t pid;
149 size_t l;
150
151 #ifdef OS_IS_WIN32
152 HANDLE process;
153 #endif
154
155 pa_runtime_path("pid", fn, sizeof(fn));
156
157 if ((fd = open_pid_file(fn, O_CREAT|O_RDWR)) < 0)
158 goto fail;
159
160 if ((pid = read_pid(fn, fd)) == (pid_t) -1)
161 pa_log_warn("Corrupt PID file, overwriting.");
162 else if (pid > 0) {
163 #ifdef OS_IS_WIN32
164 if ((process = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, pid)) != NULL) {
165 CloseHandle(process);
166 #else
167 if (kill(pid, 0) >= 0 || errno != ESRCH) {
168 #endif
169 pa_log("Daemon already running.");
170 goto fail;
171 }
172
173 pa_log_warn("Stale PID file, overwriting.");
174 }
175
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));
179 goto fail;
180 }
181
182 pa_snprintf(t, sizeof(t), "%lu\n", (unsigned long) getpid());
183 l = strlen(t);
184
185 if (pa_loop_write(fd, t, l, NULL) != (ssize_t) l) {
186 pa_log("Failed to write PID file.");
187 goto fail;
188 }
189
190 ret = 0;
191
192 fail:
193 if (fd >= 0) {
194 pa_lock_fd(fd, 0);
195
196 if (pa_close(fd) < 0) {
197 pa_log("Failed to close PID file '%s': %s", fn, pa_cstrerror(errno));
198 ret = -1;
199 }
200 }
201
202 return ret;
203 }
204
205 /* Remove the PID file, if it is ours */
206 int pa_pid_file_remove(void) {
207 int fd = -1;
208 char fn[PATH_MAX];
209 int ret = -1;
210 pid_t pid;
211
212 pa_runtime_path("pid", fn, sizeof(fn));
213
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));
216 goto fail;
217 }
218
219 if ((pid = read_pid(fn, fd)) == (pid_t) -1)
220 goto fail;
221
222 if (pid != getpid()) {
223 pa_log("PID file '%s' not mine!", fn);
224 goto fail;
225 }
226
227 if (ftruncate(fd, 0) < 0) {
228 pa_log_warn("Failed to truncate PID file '%s': %s", fn, pa_cstrerror(errno));
229 goto fail;
230 }
231
232 #ifdef OS_IS_WIN32
233 pa_lock_fd(fd, 0);
234 close(fd);
235 fd = -1;
236 #endif
237
238 if (unlink(fn) < 0) {
239 pa_log_warn("Failed to remove PID file '%s': %s", fn, pa_cstrerror(errno));
240 goto fail;
241 }
242
243 ret = 0;
244
245 fail:
246
247 if (fd >= 0) {
248 pa_lock_fd(fd, 0);
249
250 if (pa_close(fd) < 0) {
251 pa_log_warn("Failed to close PID file '%s': %s", fn, pa_cstrerror(errno));
252 ret = -1;
253 }
254 }
255
256 return ret;
257 }
258
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);
265 }
266
267 #ifndef OS_IS_WIN32
268
269 /* Kill a current running daemon. Return non-zero on success, -1
270 * otherwise. If successful *pid contains the PID of the daemon
271 * process. */
272 int pa_pid_file_kill(int sig, pid_t *pid) {
273 int fd = -1;
274 char fn[PATH_MAX];
275 int ret = -1;
276 pid_t _pid;
277
278 if (!pid)
279 pid = &_pid;
280
281 pa_runtime_path("pid", fn, sizeof(fn));
282
283 if ((fd = open_pid_file(fn, O_RDONLY)) < 0)
284 goto fail;
285
286 if ((*pid = read_pid(fn, fd)) == (pid_t) -1)
287 goto fail;
288
289 ret = kill(*pid, sig);
290
291 fail:
292
293 if (fd >= 0) {
294 pa_lock_fd(fd, 0);
295 pa_close(fd);
296 }
297
298 return ret;
299
300 }
301
302 #else /* OS_IS_WIN32 */
303
304 int pa_pid_file_kill(int sig, pid_t *pid) {
305 return -1;
306 }
307
308 #endif