]> code.delx.au - pulseaudio/blob - src/pulsecore/pid.c
remaining s/assert/pa_assert/ and refcnt.h modernizations
[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("WARNING: failed to read PID file '%s': %s",
65 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("WARNING: 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, S_IRUSR|S_IWUSR)) < 0) {
93 if (mode != O_RDONLY || errno != ENOENT)
94 pa_log_warn("WARNING: failed to open PID file '%s': %s",
95 fn, pa_cstrerror(errno));
96 goto fail;
97 }
98
99 /* Try to lock the file. If that fails, go without */
100 if (pa_lock_fd(fd, 1) < 0)
101 goto fail;
102
103 if (fstat(fd, &st) < 0) {
104 pa_log_warn("WARNING: failed to fstat() PID file '%s': %s",
105 fn, pa_cstrerror(errno));
106 goto fail;
107 }
108
109 /* Does the file still exist in the file system? When ye, w're done, otherwise restart */
110 if (st.st_nlink >= 1)
111 break;
112
113 if (pa_lock_fd(fd, 0) < 0)
114 goto fail;
115
116 if (close(fd) < 0) {
117 pa_log_warn("WARNING: failed to close file '%s': %s",
118 fn, pa_cstrerror(errno));
119 goto fail;
120 }
121
122 fd = -1;
123 }
124
125 return fd;
126
127 fail:
128
129 if (fd >= 0) {
130 pa_lock_fd(fd, 0);
131 close(fd);
132 }
133
134 return -1;
135 }
136
137 /* Create a new PID file for the current process. */
138 int pa_pid_file_create(void) {
139 int fd = -1;
140 int ret = -1;
141 char fn[PATH_MAX];
142 char t[20];
143 pid_t pid;
144 size_t l;
145
146 #ifdef OS_IS_WIN32
147 HANDLE process;
148 #endif
149
150 pa_runtime_path("pid", fn, sizeof(fn));
151
152 if ((fd = open_pid_file(fn, O_CREAT|O_RDWR)) < 0)
153 goto fail;
154
155 if ((pid = read_pid(fn, fd)) == (pid_t) -1)
156 pa_log("corrupt PID file, overwriting.");
157 else if (pid > 0) {
158 #ifdef OS_IS_WIN32
159 if ((process = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, pid)) != NULL) {
160 CloseHandle(process);
161 #else
162 if (kill(pid, 0) >= 0 || errno != ESRCH) {
163 #endif
164 pa_log("daemon already running.");
165 goto fail;
166 }
167
168 pa_log("stale PID file, overwriting.");
169 }
170
171 /* Overwrite the current PID file */
172 if (lseek(fd, 0, SEEK_SET) == (off_t) -1 || ftruncate(fd, 0) < 0) {
173 pa_log("failed to truncate PID file '%s': %s",
174 fn, pa_cstrerror(errno));
175 goto fail;
176 }
177
178 pa_snprintf(t, sizeof(t), "%lu\n", (unsigned long) getpid());
179 l = strlen(t);
180
181 if (pa_loop_write(fd, t, l, NULL) != (ssize_t) l) {
182 pa_log("failed to write PID file.");
183 goto fail;
184 }
185
186 ret = 0;
187
188 fail:
189 if (fd >= 0) {
190 pa_lock_fd(fd, 0);
191 close(fd);
192 }
193
194 return ret;
195 }
196
197 /* Remove the PID file, if it is ours */
198 int pa_pid_file_remove(void) {
199 int fd = -1;
200 char fn[PATH_MAX];
201 int ret = -1;
202 pid_t pid;
203
204 pa_runtime_path("pid", fn, sizeof(fn));
205
206 if ((fd = open_pid_file(fn, O_RDWR)) < 0) {
207 pa_log_warn("WARNING: failed to open PID file '%s': %s",
208 fn, pa_cstrerror(errno));
209 goto fail;
210 }
211
212 if ((pid = read_pid(fn, fd)) == (pid_t) -1)
213 goto fail;
214
215 if (pid != getpid()) {
216 pa_log("WARNING: PID file '%s' not mine!", fn);
217 goto fail;
218 }
219
220 if (ftruncate(fd, 0) < 0) {
221 pa_log_warn("WARNING: failed to truncate PID file '%s': %s",
222 fn, pa_cstrerror(errno));
223 goto fail;
224 }
225
226 #ifdef OS_IS_WIN32
227 pa_lock_fd(fd, 0);
228 close(fd);
229 fd = -1;
230 #endif
231
232 if (unlink(fn) < 0) {
233 pa_log_warn("WARNING: failed to remove PID file '%s': %s",
234 fn, pa_cstrerror(errno));
235 goto fail;
236 }
237
238 ret = 0;
239
240 fail:
241
242 if (fd >= 0) {
243 pa_lock_fd(fd, 0);
244 pa_assert_se(close(fd) == 0);
245 }
246
247 return ret;
248 }
249
250 /* Check whether the daemon is currently running, i.e. if a PID file
251 * exists and the PID therein too. Returns 0 on succcess, -1
252 * otherwise. If pid is non-NULL and a running daemon was found,
253 * return its PID therein */
254 int pa_pid_file_check_running(pid_t *pid) {
255 return pa_pid_file_kill(0, pid);
256 }
257
258 #ifndef OS_IS_WIN32
259
260 /* Kill a current running daemon. Return non-zero on success, -1
261 * otherwise. If successful *pid contains the PID of the daemon
262 * process. */
263 int pa_pid_file_kill(int sig, pid_t *pid) {
264 int fd = -1;
265 char fn[PATH_MAX];
266 int ret = -1;
267 pid_t _pid;
268
269 if (!pid)
270 pid = &_pid;
271
272 pa_runtime_path("pid", fn, sizeof(fn));
273
274 if ((fd = open_pid_file(fn, O_RDONLY)) < 0)
275 goto fail;
276
277 if ((*pid = read_pid(fn, fd)) == (pid_t) -1)
278 goto fail;
279
280 ret = kill(*pid, sig);
281
282 fail:
283
284 if (fd >= 0) {
285 pa_lock_fd(fd, 0);
286 pa_assert_se(close(fd) == 0);
287 }
288
289 return ret;
290
291 }
292
293 #else /* OS_IS_WIN32 */
294
295 int pa_pid_file_kill(int sig, pid_t *pid) {
296 return -1;
297 }
298
299 #endif