]>
code.delx.au - pulseaudio/blob - polyp/pid.c
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>
41 static pid_t
read_pid(const char *fn
, int fd
) {
43 char t
[20], *e
= NULL
;
46 assert(fn
&& fd
>= 0);
48 if ((r
= pa_loop_read(fd
, t
, sizeof(t
)-1)) < 0) {
49 pa_log(__FILE__
": WARNING: failed to read PID file '%s': %s\n", fn
, strerror(errno
));
58 if (!t
[0] || (pid
= strtol(t
, &e
, 0)) == 0 || (*e
!= 0 && *e
!= '\n')) {
59 pa_log(__FILE__
": WARNING: failed to parse PID file '%s'\n", fn
);
66 int pa_pid_file_create(void) {
67 int fd
= -1, lock
= -1;
74 pa_runtime_path("pid", fn
, sizeof(fn
));
75 if ((fd
= open(fn
, O_CREAT
|O_RDWR
, S_IRUSR
|S_IWUSR
)) < 0) {
76 pa_log(__FILE__
": WARNING: failed to open PID file '%s': %s\n", fn
, strerror(errno
));
80 lock
= pa_lock_fd(fd
, 1);
82 if ((pid
= read_pid(fn
, fd
)) == (pid_t
) -1)
83 pa_log(__FILE__
": corrupt PID file, overwriting.\n");
85 if (kill(pid
, 0) >= 0 || errno
!= ESRCH
) {
86 pa_log(__FILE__
": valid PID file.\n");
90 pa_log(__FILE__
": stale PID file, overwriting.\n");
93 lseek(fd
, 0, SEEK_SET
);
95 snprintf(t
, sizeof(t
), "%lu", (unsigned long) getpid());
98 if (pa_loop_write(fd
, t
, l
) != (ssize_t
) l
) {
99 pa_log(__FILE__
": failed to write PID file.\n");
116 int pa_pid_file_remove(void) {
117 int fd
= -1, lock
= -1;
122 pa_runtime_path("pid", fn
, sizeof(fn
));
123 if ((fd
= open(fn
, O_RDWR
)) < 0) {
124 pa_log(__FILE__
": WARNING: failed to open PID file '%s': %s\n", fn
, strerror(errno
));
128 lock
= pa_lock_fd(fd
, 1);
130 if ((pid
= read_pid(fn
, fd
)) == (pid_t
) -1)
133 if (pid
!= getpid()) {
134 pa_log(__FILE__
": WARNING: PID file '%s' not mine!\n", fn
);
138 if (ftruncate(fd
, 0) < 0) {
139 pa_log(__FILE__
": failed to truncate PID file '%s': %s\n", fn
, strerror(errno
));
143 if (unlink(fn
) < 0) {
144 pa_log(__FILE__
": failed to remove PID file '%s': %s\n", fn
, strerror(errno
));
162 int pa_pid_file_check_running(pid_t
*pid
) {
163 return pa_pid_file_kill(0, pid
);
166 int pa_pid_file_kill(int sig
, pid_t
*pid
) {
167 int fd
= -1, lock
= -1;
175 pa_runtime_path("pid", fn
, sizeof(fn
));
176 if ((fd
= open(fn
, O_RDONLY
)) < 0)
179 lock
= pa_lock_fd(fd
, 1);
181 if ((*pid
= read_pid(fn
, fd
)) == (pid_t
) -1)
184 ret
= kill(*pid
, sig
);