]>
code.delx.au - pulseaudio/blob - polyp/pid.c
2fac687ec876685064be879f18657a00ff905531
4 This file is part of polypaudio.
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.
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.
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
29 #include <sys/types.h>
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
) {
49 assert(fn
&& fd
>= 0);
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
));
60 if ((e
= strchr(t
, '\n')))
63 if (pa_atou(t
, &pid
) < 0) {
64 pa_log(__FILE__
": WARNING: failed to parse PID file '%s'\n", fn
);
71 static int open_pid_file(const char *fn
, int mode
) {
78 pa_make_secure_parent_dir(fn
);
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
));
86 /* Try to lock the file. If that fails, go without */
87 if (pa_lock_fd(fd
, 1) < 0)
90 if (fstat(fd
, &st
) < 0) {
91 pa_log(__FILE__
": Failed to fstat() PID file '%s': %s\n", fn
, strerror(errno
));
95 /* Does the file still exist in the file system? When ye, w're done, otherwise restart */
99 if (pa_lock_fd(fd
, 0) < 0)
103 pa_log(__FILE__
": Failed to close file '%s': %s\n", fn
, strerror(errno
));
124 /* Create a new PID file for the current process. */
125 int pa_pid_file_create(void) {
133 pa_runtime_path("pid", fn
, sizeof(fn
));
135 if ((fd
= open_pid_file(fn
, O_CREAT
|O_RDWR
)) < 0)
138 if ((pid
= read_pid(fn
, fd
)) == (pid_t
) -1)
139 pa_log(__FILE__
": corrupt PID file, overwriting.\n");
141 if (kill(pid
, 0) >= 0 || errno
!= ESRCH
) {
142 pa_log(__FILE__
": daemon already running.\n");
146 pa_log(__FILE__
": stale PID file, overwriting.\n");
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
));
155 snprintf(t
, sizeof(t
), "%lu\n", (unsigned long) getpid());
158 if (pa_loop_write(fd
, t
, l
) != (ssize_t
) l
) {
159 pa_log(__FILE__
": failed to write PID file.\n");
174 /* Remove the PID file, if it is ours */
175 int pa_pid_file_remove(void) {
181 pa_runtime_path("pid", fn
, sizeof(fn
));
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
));
188 if ((pid
= read_pid(fn
, fd
)) == (pid_t
) -1)
191 if (pid
!= getpid()) {
192 pa_log(__FILE__
": WARNING: PID file '%s' not mine!\n", fn
);
196 if (ftruncate(fd
, 0) < 0) {
197 pa_log(__FILE__
": failed to truncate PID file '%s': %s\n", fn
, strerror(errno
));
201 if (unlink(fn
) < 0) {
202 pa_log(__FILE__
": failed to remove PID file '%s': %s\n", fn
, strerror(errno
));
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
);
226 /* Kill a current running daemon. Return non-zero on success, -1
227 * otherwise. If successful *pid contains the PID of the daemon
229 int pa_pid_file_kill(int sig
, pid_t
*pid
) {
238 pa_runtime_path("pid", fn
, sizeof(fn
));
240 if ((fd
= open_pid_file(fn
, O_RDONLY
)) < 0)
243 if ((*pid
= read_pid(fn
, fd
)) == (pid_t
) -1)
246 ret
= kill(*pid
, sig
);