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