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 <polypcore/util.h>
43 #include <polypcore/log.h>
47 /* Read the PID data from the file descriptor fd, and return it. If no
48 * pid could be read, return 0, on failure (pid_t) -1 */
49 static pid_t read_pid(const char *fn, int fd) {
54 assert(fn && fd >= 0);
56 if ((r = pa_loop_read(fd, t, sizeof(t)-1)) < 0) {
57 pa_log(__FILE__": WARNING: failed to read PID file '%s': %s", fn, strerror(errno));
65 if ((e = strchr(t, '\n')))
68 if (pa_atou(t, &pid) < 0) {
69 pa_log(__FILE__": WARNING: failed to parse PID file '%s'", fn);
76 static int open_pid_file(const char *fn, int mode) {
83 pa_make_secure_parent_dir(fn);
85 if ((fd = open(fn, mode, S_IRUSR|S_IWUSR)) < 0) {
86 if (mode != O_RDONLY || errno != ENOENT)
87 pa_log(__FILE__": WARNING: failed to open PID file '%s': %s", fn, strerror(errno));
91 /* Try to lock the file. If that fails, go without */
92 if (pa_lock_fd(fd, 1) < 0)
95 if (fstat(fd, &st) < 0) {
96 pa_log(__FILE__": Failed to fstat() PID file '%s': %s", fn, strerror(errno));
100 /* Does the file still exist in the file system? When ye, w're done, otherwise restart */
101 if (st.st_nlink >= 1)
104 if (pa_lock_fd(fd, 0) < 0)
108 pa_log(__FILE__": Failed to close file '%s': %s", fn, strerror(errno));
129 /* Create a new PID file for the current process. */
130 int pa_pid_file_create(void) {
142 pa_runtime_path("pid", fn, sizeof(fn));
144 if ((fd = open_pid_file(fn, O_CREAT|O_RDWR)) < 0)
147 if ((pid = read_pid(fn, fd)) == (pid_t) -1)
148 pa_log(__FILE__": corrupt PID file, overwriting.");
151 if ((process = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, pid)) != NULL) {
152 CloseHandle(process);
154 if (kill(pid, 0) >= 0 || errno != ESRCH) {
156 pa_log(__FILE__": daemon already running.");
160 pa_log(__FILE__": stale PID file, overwriting.");
163 /* Overwrite the current PID file */
164 if (lseek(fd, 0, SEEK_SET) == (off_t) -1 || ftruncate(fd, 0) < 0) {
165 pa_log(__FILE__": failed to truncate PID fil: %s.", strerror(errno));
169 snprintf(t, sizeof(t), "%lu\n", (unsigned long) getpid());
172 if (pa_loop_write(fd, t, l) != (ssize_t) l) {
173 pa_log(__FILE__": failed to write PID file.");
188 /* Remove the PID file, if it is ours */
189 int pa_pid_file_remove(void) {
195 pa_runtime_path("pid", fn, sizeof(fn));
197 if ((fd = open_pid_file(fn, O_RDWR)) < 0) {
198 pa_log(__FILE__": WARNING: failed to open PID file '%s': %s", fn, strerror(errno));
202 if ((pid = read_pid(fn, fd)) == (pid_t) -1)
205 if (pid != getpid()) {
206 pa_log(__FILE__": WARNING: PID file '%s' not mine!", fn);
210 if (ftruncate(fd, 0) < 0) {
211 pa_log(__FILE__": failed to truncate PID file '%s': %s", fn, strerror(errno));
221 if (unlink(fn) < 0) {
222 pa_log(__FILE__": failed to remove PID file '%s': %s", fn, strerror(errno));
238 /* Check whether the daemon is currently running, i.e. if a PID file
239 * exists and the PID therein too. Returns 0 on succcess, -1
240 * otherwise. If pid is non-NULL and a running daemon was found,
241 * return its PID therein */
242 int pa_pid_file_check_running(pid_t *pid) {
243 return pa_pid_file_kill(0, pid);
248 /* Kill a current running daemon. Return non-zero on success, -1
249 * otherwise. If successful *pid contains the PID of the daemon
251 int pa_pid_file_kill(int sig, pid_t *pid) {
260 pa_runtime_path("pid", fn, sizeof(fn));
262 if ((fd = open_pid_file(fn, O_RDONLY)) < 0)
265 if ((*pid = read_pid(fn, fd)) == (pid_t) -1)
268 ret = kill(*pid, sig);
281 #else /* OS_IS_WIN32 */
283 int pa_pid_file_kill(int sig, pid_t *pid) {