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 #include <polyp/xmalloc.h>
44 #include <polypcore/core-error.h>
45 #include <polypcore/core-util.h>
46 #include <polypcore/log.h>
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
) {
57 assert(fn
&& fd
>= 0);
59 if ((r
= pa_loop_read(fd
, t
, sizeof(t
)-1)) < 0) {
60 pa_log_warn(__FILE__
": WARNING: failed to read PID file '%s': %s",
61 fn
, pa_cstrerror(errno
));
69 if ((e
= strchr(t
, '\n')))
72 if (pa_atou(t
, &pid
) < 0) {
73 pa_log(__FILE__
": WARNING: failed to parse PID file '%s'", fn
);
80 static int open_pid_file(const char *fn
, int mode
) {
87 pa_make_secure_parent_dir(fn
);
89 if ((fd
= open(fn
, mode
, S_IRUSR
|S_IWUSR
)) < 0) {
90 if (mode
!= O_RDONLY
|| errno
!= ENOENT
)
91 pa_log_warn(__FILE__
": WARNING: failed to open PID file '%s': %s",
92 fn
, pa_cstrerror(errno
));
96 /* Try to lock the file. If that fails, go without */
97 if (pa_lock_fd(fd
, 1) < 0)
100 if (fstat(fd
, &st
) < 0) {
101 pa_log_warn(__FILE__
": WARNING: failed to fstat() PID file '%s': %s",
102 fn
, pa_cstrerror(errno
));
106 /* Does the file still exist in the file system? When ye, w're done, otherwise restart */
107 if (st
.st_nlink
>= 1)
110 if (pa_lock_fd(fd
, 0) < 0)
114 pa_log_warn(__FILE__
": WARNING: failed to close file '%s': %s",
115 fn
, pa_cstrerror(errno
));
136 /* Create a new PID file for the current process. */
137 int pa_pid_file_create(void) {
149 pa_runtime_path("pid", fn
, sizeof(fn
));
151 if ((fd
= open_pid_file(fn
, O_CREAT
|O_RDWR
)) < 0)
154 if ((pid
= read_pid(fn
, fd
)) == (pid_t
) -1)
155 pa_log(__FILE__
": corrupt PID file, overwriting.");
158 if ((process
= OpenProcess(PROCESS_QUERY_INFORMATION
, FALSE
, pid
)) != NULL
) {
159 CloseHandle(process
);
161 if (kill(pid
, 0) >= 0 || errno
!= ESRCH
) {
163 pa_log(__FILE__
": daemon already running.");
167 pa_log(__FILE__
": stale PID file, overwriting.");
170 /* Overwrite the current PID file */
171 if (lseek(fd
, 0, SEEK_SET
) == (off_t
) -1 || ftruncate(fd
, 0) < 0) {
172 pa_log(__FILE__
": failed to truncate PID file '%s': %s",
173 fn
, pa_cstrerror(errno
));
177 snprintf(t
, sizeof(t
), "%lu\n", (unsigned long) getpid());
180 if (pa_loop_write(fd
, t
, l
) != (ssize_t
) l
) {
181 pa_log(__FILE__
": failed to write PID file.");
196 /* Remove the PID file, if it is ours */
197 int pa_pid_file_remove(void) {
204 pa_runtime_path("pid", fn
, sizeof(fn
));
206 if ((fd
= open_pid_file(fn
, O_RDWR
)) < 0) {
207 pa_log_warn(__FILE__
": WARNING: failed to open PID file '%s': %s",
208 fn
, pa_cstrerror(errno
));
212 if ((pid
= read_pid(fn
, fd
)) == (pid_t
) -1)
215 if (pid
!= getpid()) {
216 pa_log(__FILE__
": WARNING: PID file '%s' not mine!", fn
);
220 if (ftruncate(fd
, 0) < 0) {
221 pa_log_warn(__FILE__
": WARNING: failed to truncate PID file '%s': %s",
222 fn
, pa_cstrerror(errno
));
232 if (unlink(fn
) < 0) {
233 pa_log_warn(__FILE__
": WARNING: failed to remove PID file '%s': %s",
234 fn
, pa_cstrerror(errno
));
238 if ((p
= pa_parent_dir(fn
))) {
255 /* Check whether the daemon is currently running, i.e. if a PID file
256 * exists and the PID therein too. Returns 0 on succcess, -1
257 * otherwise. If pid is non-NULL and a running daemon was found,
258 * return its PID therein */
259 int pa_pid_file_check_running(pid_t
*pid
) {
260 return pa_pid_file_kill(0, pid
);
265 /* Kill a current running daemon. Return non-zero on success, -1
266 * otherwise. If successful *pid contains the PID of the daemon
268 int pa_pid_file_kill(int sig
, pid_t
*pid
) {
277 pa_runtime_path("pid", fn
, sizeof(fn
));
279 if ((fd
= open_pid_file(fn
, O_RDONLY
)) < 0)
282 if ((*pid
= read_pid(fn
, fd
)) == (pid_t
) -1)
285 ret
= kill(*pid
, sig
);
298 #else /* OS_IS_WIN32 */
300 int pa_pid_file_kill(int sig
, pid_t
*pid
) {