]> code.delx.au - pulseaudio/blob - src/polypcore/pid.c
* split pa_cstrerror() into its own file polypcore/core-error.[ch]
[pulseaudio] / src / polypcore / pid.c
1 /* $Id$ */
2
3 /***
4 This file is part of polypaudio.
5
6 polypaudio 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.
10
11 polypaudio 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.
15
16 You should have received a copy of the GNU Lesser General Public
17 License along with polypaudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19 USA.
20 ***/
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <fcntl.h>
27 #include <unistd.h>
28 #include <errno.h>
29 #include <sys/types.h>
30 #include <sys/stat.h>
31 #include <string.h>
32 #include <stdio.h>
33 #include <assert.h>
34 #include <stdlib.h>
35 #include <limits.h>
36 #include <signal.h>
37
38 #ifdef HAVE_WINDOWS_H
39 #include <windows.h>
40 #endif
41
42 #include <polyp/xmalloc.h>
43
44 #include <polypcore/core-error.h>
45 #include <polypcore/core-util.h>
46 #include <polypcore/log.h>
47
48 #include "pid.h"
49
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) {
53 ssize_t r;
54 char t[20], *e;
55 uint32_t pid;
56
57 assert(fn && fd >= 0);
58
59 if ((r = pa_loop_read(fd, t, sizeof(t)-1)) < 0) {
60 pa_log_warn(__FILE__": WARNING: failed to read PID file '%s': %s",
61 fn, pa_cstrerror(errno));
62 return (pid_t) -1;
63 }
64
65 if (r == 0)
66 return (pid_t) 0;
67
68 t[r] = 0;
69 if ((e = strchr(t, '\n')))
70 *e = 0;
71
72 if (pa_atou(t, &pid) < 0) {
73 pa_log(__FILE__": WARNING: failed to parse PID file '%s'", fn);
74 return (pid_t) -1;
75 }
76
77 return (pid_t) pid;
78 }
79
80 static int open_pid_file(const char *fn, int mode) {
81 int fd = -1;
82 int lock = -1;
83
84 for (;;) {
85 struct stat st;
86
87 pa_make_secure_parent_dir(fn);
88
89 if ((fd = open(fn, mode, S_IRUSR|S_IWUSR)) < 0) {
90 if (mode != O_RDONLY || errno != ENOENT)
91 pa_log_warn(__FILE__": WARNING: failed to open PID file '%s': %s",
92 fn, pa_cstrerror(errno));
93 goto fail;
94 }
95
96 /* Try to lock the file. If that fails, go without */
97 if (pa_lock_fd(fd, 1) < 0)
98 goto fail;
99
100 if (fstat(fd, &st) < 0) {
101 pa_log_warn(__FILE__": WARNING: failed to fstat() PID file '%s': %s",
102 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 (close(fd) < 0) {
114 pa_log_warn(__FILE__": WARNING: failed to close file '%s': %s",
115 fn, pa_cstrerror(errno));
116 goto fail;
117 }
118
119 fd = -1;
120 }
121
122 return fd;
123
124 fail:
125
126 if (fd < 0) {
127 if (lock >= 0)
128 pa_lock_fd(fd, 0);
129
130 close(fd);
131 }
132
133 return -1;
134 }
135
136 /* Create a new PID file for the current process. */
137 int pa_pid_file_create(void) {
138 int fd = -1;
139 int ret = -1;
140 char fn[PATH_MAX];
141 char t[20];
142 pid_t pid;
143 size_t l;
144
145 #ifdef OS_IS_WIN32
146 HANDLE process;
147 #endif
148
149 pa_runtime_path("pid", fn, sizeof(fn));
150
151 if ((fd = open_pid_file(fn, O_CREAT|O_RDWR)) < 0)
152 goto fail;
153
154 if ((pid = read_pid(fn, fd)) == (pid_t) -1)
155 pa_log(__FILE__": corrupt PID file, overwriting.");
156 else if (pid > 0) {
157 #ifdef OS_IS_WIN32
158 if ((process = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, pid)) != NULL) {
159 CloseHandle(process);
160 #else
161 if (kill(pid, 0) >= 0 || errno != ESRCH) {
162 #endif
163 pa_log(__FILE__": daemon already running.");
164 goto fail;
165 }
166
167 pa_log(__FILE__": stale PID file, overwriting.");
168 }
169
170 /* Overwrite the current PID file */
171 if (lseek(fd, 0, SEEK_SET) == (off_t) -1 || ftruncate(fd, 0) < 0) {
172 pa_log(__FILE__": failed to truncate PID file '%s': %s",
173 fn, pa_cstrerror(errno));
174 goto fail;
175 }
176
177 snprintf(t, sizeof(t), "%lu\n", (unsigned long) getpid());
178 l = strlen(t);
179
180 if (pa_loop_write(fd, t, l) != (ssize_t) l) {
181 pa_log(__FILE__": failed to write PID file.");
182 goto fail;
183 }
184
185 ret = 0;
186
187 fail:
188 if (fd >= 0) {
189 pa_lock_fd(fd, 0);
190 close(fd);
191 }
192
193 return ret;
194 }
195
196 /* Remove the PID file, if it is ours */
197 int pa_pid_file_remove(void) {
198 int fd = -1;
199 char fn[PATH_MAX];
200 int ret = -1;
201 pid_t pid;
202 char *p;
203
204 pa_runtime_path("pid", fn, sizeof(fn));
205
206 if ((fd = open_pid_file(fn, O_RDWR)) < 0) {
207 pa_log_warn(__FILE__": 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(__FILE__": WARNING: PID file '%s' not mine!", fn);
217 goto fail;
218 }
219
220 if (ftruncate(fd, 0) < 0) {
221 pa_log_warn(__FILE__": 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(__FILE__": WARNING: failed to remove PID file '%s': %s",
234 fn, pa_cstrerror(errno));
235 goto fail;
236 }
237
238 if ((p = pa_parent_dir(fn))) {
239 rmdir(p);
240 pa_xfree(p);
241 }
242
243 ret = 0;
244
245 fail:
246
247 if (fd >= 0) {
248 pa_lock_fd(fd, 0);
249 close(fd);
250 }
251
252 return ret;
253 }
254
255 /* Check whether the daemon is currently running, i.e. if a PID file
256 * exists and the PID therein too. Returns 0 on succcess, -1
257 * otherwise. If pid is non-NULL and a running daemon was found,
258 * return its PID therein */
259 int pa_pid_file_check_running(pid_t *pid) {
260 return pa_pid_file_kill(0, pid);
261 }
262
263 #ifndef OS_IS_WIN32
264
265 /* Kill a current running daemon. Return non-zero on success, -1
266 * otherwise. If successful *pid contains the PID of the daemon
267 * process. */
268 int pa_pid_file_kill(int sig, pid_t *pid) {
269 int fd = -1;
270 char fn[PATH_MAX];
271 int ret = -1;
272 pid_t _pid;
273
274 if (!pid)
275 pid = &_pid;
276
277 pa_runtime_path("pid", fn, sizeof(fn));
278
279 if ((fd = open_pid_file(fn, O_RDONLY)) < 0)
280 goto fail;
281
282 if ((*pid = read_pid(fn, fd)) == (pid_t) -1)
283 goto fail;
284
285 ret = kill(*pid, sig);
286
287 fail:
288
289 if (fd >= 0) {
290 pa_lock_fd(fd, 0);
291 close(fd);
292 }
293
294 return ret;
295
296 }
297
298 #else /* OS_IS_WIN32 */
299
300 int pa_pid_file_kill(int sig, pid_t *pid) {
301 return -1;
302 }
303
304 #endif