Move xmalloc to the public side (libpolyp).
[profile/ivi/pulseaudio.git] / 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/util.h>
45 #include <polypcore/log.h>
46
47 #include "pid.h"
48
49 /* Read the PID data from the file descriptor fd, and return it. If no
50  * pid could be read, return 0, on failure (pid_t) -1 */
51 static pid_t read_pid(const char *fn, int fd) {
52     ssize_t r;
53     char t[20], *e;
54     uint32_t pid;
55
56     assert(fn && fd >= 0);
57
58     if ((r = pa_loop_read(fd, t, sizeof(t)-1)) < 0) {
59         pa_log(__FILE__": WARNING: failed to read PID file '%s': %s", fn, strerror(errno));
60         return (pid_t) -1;
61     }
62
63     if (r == 0)
64         return (pid_t) 0;
65     
66     t[r] = 0;
67     if ((e = strchr(t, '\n')))
68         *e = 0;
69
70     if (pa_atou(t, &pid) < 0) {
71         pa_log(__FILE__": WARNING: failed to parse PID file '%s'", fn);
72         return (pid_t) -1;
73     }
74
75     return (pid_t) pid;
76 }
77
78 static int open_pid_file(const char *fn, int mode) {
79     int fd = -1;
80     int lock = -1;
81     
82     for (;;) {
83         struct stat st;
84         
85         pa_make_secure_parent_dir(fn);
86         
87         if ((fd = open(fn, mode, S_IRUSR|S_IWUSR)) < 0) {
88             if (mode != O_RDONLY || errno != ENOENT)
89                 pa_log(__FILE__": WARNING: failed to open PID file '%s': %s", fn, strerror(errno));
90             goto fail;
91         }
92
93         /* Try to lock the file. If that fails, go without */
94         if (pa_lock_fd(fd, 1) < 0)
95             goto fail;
96         
97         if (fstat(fd, &st) < 0) {
98             pa_log(__FILE__": Failed to fstat() PID file '%s': %s", fn, strerror(errno));
99             goto fail;
100         }
101
102         /* Does the file still exist in the file system? When ye, w're done, otherwise restart */
103         if (st.st_nlink >= 1)
104             break;
105
106         if (pa_lock_fd(fd, 0) < 0)
107             goto fail;
108
109         if (close(fd) < 0) {
110             pa_log(__FILE__": Failed to close file '%s': %s", fn, strerror(errno));
111             goto fail;
112         }
113
114         fd = -1;
115     }
116
117     return fd;
118
119 fail:
120
121     if (fd < 0) {
122         if (lock >= 0)
123             pa_lock_fd(fd, 0);
124         
125         close(fd);
126     }
127
128     return -1;
129 }
130
131 /* Create a new PID file for the current process. */
132 int pa_pid_file_create(void) {
133     int fd = -1;
134     int ret = -1;
135     char fn[PATH_MAX];
136     char t[20];
137     pid_t pid;
138     size_t l;
139
140 #ifdef OS_IS_WIN32
141     HANDLE process;
142 #endif
143
144     pa_runtime_path("pid", fn, sizeof(fn));
145
146     if ((fd = open_pid_file(fn, O_CREAT|O_RDWR)) < 0)
147         goto fail;
148
149     if ((pid = read_pid(fn, fd)) == (pid_t) -1)
150         pa_log(__FILE__": corrupt PID file, overwriting.");
151     else if (pid > 0) {
152 #ifdef OS_IS_WIN32
153         if ((process = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, pid)) != NULL) {
154             CloseHandle(process);
155 #else
156         if (kill(pid, 0) >= 0 || errno != ESRCH) {
157 #endif
158             pa_log(__FILE__": daemon already running.");
159             goto fail;
160         }
161
162         pa_log(__FILE__": stale PID file, overwriting.");
163     }
164
165     /* Overwrite the current PID file */
166     if (lseek(fd, 0, SEEK_SET) == (off_t) -1 || ftruncate(fd, 0) < 0) {
167         pa_log(__FILE__": failed to truncate PID fil: %s.", strerror(errno));
168         goto fail;
169     }
170     
171     snprintf(t, sizeof(t), "%lu\n", (unsigned long) getpid());
172     l = strlen(t);
173     
174     if (pa_loop_write(fd, t, l) != (ssize_t) l) {
175         pa_log(__FILE__": failed to write PID file.");
176         goto fail;
177     }
178
179     ret = 0;
180     
181 fail:
182     if (fd >= 0) {
183         pa_lock_fd(fd, 0);
184         close(fd);
185     }
186     
187     return ret;
188 }
189
190 /* Remove the PID file, if it is ours */
191 int pa_pid_file_remove(void) {
192     int fd = -1;
193     char fn[PATH_MAX];
194     int ret = -1;
195     pid_t pid;
196     char *p;
197
198     pa_runtime_path("pid", fn, sizeof(fn));
199
200     if ((fd = open_pid_file(fn, O_RDWR)) < 0) {
201         pa_log(__FILE__": WARNING: failed to open PID file '%s': %s", fn, strerror(errno));
202         goto fail;
203     }
204
205     if ((pid = read_pid(fn, fd)) == (pid_t) -1)
206         goto fail;
207
208     if (pid != getpid()) {
209         pa_log(__FILE__": WARNING: PID file '%s' not mine!", fn);
210         goto fail;
211     }
212
213     if (ftruncate(fd, 0) < 0) {
214         pa_log(__FILE__": failed to truncate PID file '%s': %s", fn, strerror(errno));
215         goto fail;
216     }
217
218 #ifdef OS_IS_WIN32
219     pa_lock_fd(fd, 0);
220     close(fd);
221     fd = -1;
222 #endif
223
224     if (unlink(fn) < 0) {
225         pa_log(__FILE__": failed to remove PID file '%s': %s", fn, strerror(errno));
226         goto fail;
227     }
228
229     if ((p = pa_parent_dir(fn))) {
230         rmdir(p);
231         pa_xfree(p);
232     }
233     
234     ret = 0;
235     
236 fail:
237
238     if (fd >= 0) {
239         pa_lock_fd(fd, 0);
240         close(fd);
241     }
242
243     return ret;
244 }
245
246 /* Check whether the daemon is currently running, i.e. if a PID file
247  * exists and the PID therein too. Returns 0 on succcess, -1
248  * otherwise. If pid is non-NULL and a running daemon was found,
249  * return its PID therein */
250 int pa_pid_file_check_running(pid_t *pid) {
251     return pa_pid_file_kill(0, pid);
252 }
253
254 #ifndef OS_IS_WIN32
255
256 /* Kill a current running daemon. Return non-zero on success, -1
257  * otherwise. If successful *pid contains the PID of the daemon
258  * process. */
259 int pa_pid_file_kill(int sig, pid_t *pid) {
260     int fd = -1;
261     char fn[PATH_MAX];
262     int ret = -1;
263     pid_t _pid;
264
265     if (!pid)
266         pid = &_pid;
267     
268     pa_runtime_path("pid", fn, sizeof(fn));
269     
270     if ((fd = open_pid_file(fn, O_RDONLY)) < 0)
271         goto fail;
272     
273     if ((*pid = read_pid(fn, fd)) == (pid_t) -1)
274         goto fail;
275
276     ret = kill(*pid, sig);
277     
278 fail:
279     
280     if (fd >= 0) {
281         pa_lock_fd(fd, 0);
282         close(fd);
283     }
284
285     return ret;
286     
287 }
288
289 #else /* OS_IS_WIN32 */
290
291 int pa_pid_file_kill(int sig, pid_t *pid) {
292     return -1;
293 }
294
295 #endif