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