40cc824898a10c3193639236503af7dbeed0d4f9
[profile/ivi/pulseaudio.git] / src / pulsecore / pid.c
1 /* $Id$ */
2
3 /***
4   This file is part of PulseAudio.
5
6   PulseAudio 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   PulseAudio 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 PulseAudio; 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 <pulse/xmalloc.h>
43
44 #include <pulsecore/core-error.h>
45 #include <pulsecore/core-util.h>
46 #include <pulsecore/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, NULL)) < 0) {
60         pa_log_warn("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("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
83     for (;;) {
84         struct stat st;
85
86         if ((fd = open(fn, mode, S_IRUSR|S_IWUSR)) < 0) {
87             if (mode != O_RDONLY || errno != ENOENT)
88                 pa_log_warn("WARNING: failed to open PID file '%s': %s",
89                     fn, pa_cstrerror(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_warn("WARNING: failed to fstat() PID file '%s': %s",
99                 fn, pa_cstrerror(errno));
100             goto fail;
101         }
102
103         /* Does the file still exist in the file system? When ye, w're done, otherwise restart */
104         if (st.st_nlink >= 1)
105             break;
106
107         if (pa_lock_fd(fd, 0) < 0)
108             goto fail;
109
110         if (close(fd) < 0) {
111             pa_log_warn("WARNING: failed to close file '%s': %s",
112                 fn, pa_cstrerror(errno));
113             goto fail;
114         }
115
116         fd = -1;
117     }
118
119     return fd;
120
121 fail:
122
123     if (fd >= 0) {
124         pa_lock_fd(fd, 0);
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("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("daemon already running.");
159             goto fail;
160         }
161
162         pa_log("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("failed to truncate PID file '%s': %s",
168             fn, pa_cstrerror(errno));
169         goto fail;
170     }
171
172     snprintf(t, sizeof(t), "%lu\n", (unsigned long) getpid());
173     l = strlen(t);
174
175     if (pa_loop_write(fd, t, l, NULL) != (ssize_t) l) {
176         pa_log("failed to write PID file.");
177         goto fail;
178     }
179
180     ret = 0;
181
182 fail:
183     if (fd >= 0) {
184         pa_lock_fd(fd, 0);
185         close(fd);
186     }
187
188     return ret;
189 }
190
191 /* Remove the PID file, if it is ours */
192 int pa_pid_file_remove(void) {
193     int fd = -1;
194     char fn[PATH_MAX];
195     int ret = -1;
196     pid_t pid;
197
198     pa_runtime_path("pid", fn, sizeof(fn));
199
200     if ((fd = open_pid_file(fn, O_RDWR)) < 0) {
201         pa_log_warn("WARNING: failed to open PID file '%s': %s",
202             fn, pa_cstrerror(errno));
203         goto fail;
204     }
205
206     if ((pid = read_pid(fn, fd)) == (pid_t) -1)
207         goto fail;
208
209     if (pid != getpid()) {
210         pa_log("WARNING: PID file '%s' not mine!", fn);
211         goto fail;
212     }
213
214     if (ftruncate(fd, 0) < 0) {
215         pa_log_warn("WARNING: failed to truncate PID file '%s': %s",
216             fn, pa_cstrerror(errno));
217         goto fail;
218     }
219
220 #ifdef OS_IS_WIN32
221     pa_lock_fd(fd, 0);
222     close(fd);
223     fd = -1;
224 #endif
225
226     if (unlink(fn) < 0) {
227         pa_log_warn("WARNING: failed to remove PID file '%s': %s",
228             fn, pa_cstrerror(errno));
229         goto fail;
230     }
231
232     ret = 0;
233
234 fail:
235
236     if (fd >= 0) {
237         pa_lock_fd(fd, 0);
238         close(fd);
239     }
240
241     return ret;
242 }
243
244 /* Check whether the daemon is currently running, i.e. if a PID file
245  * exists and the PID therein too. Returns 0 on succcess, -1
246  * otherwise. If pid is non-NULL and a running daemon was found,
247  * return its PID therein */
248 int pa_pid_file_check_running(pid_t *pid) {
249     return pa_pid_file_kill(0, pid);
250 }
251
252 #ifndef OS_IS_WIN32
253
254 /* Kill a current running daemon. Return non-zero on success, -1
255  * otherwise. If successful *pid contains the PID of the daemon
256  * process. */
257 int pa_pid_file_kill(int sig, pid_t *pid) {
258     int fd = -1;
259     char fn[PATH_MAX];
260     int ret = -1;
261     pid_t _pid;
262
263     if (!pid)
264         pid = &_pid;
265
266     pa_runtime_path("pid", fn, sizeof(fn));
267
268     if ((fd = open_pid_file(fn, O_RDONLY)) < 0)
269         goto fail;
270
271     if ((*pid = read_pid(fn, fd)) == (pid_t) -1)
272         goto fail;
273
274     ret = kill(*pid, sig);
275
276 fail:
277
278     if (fd >= 0) {
279         pa_lock_fd(fd, 0);
280         close(fd);
281     }
282
283     return ret;
284
285 }
286
287 #else /* OS_IS_WIN32 */
288
289 int pa_pid_file_kill(int sig, pid_t *pid) {
290     return -1;
291 }
292
293 #endif