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-util.h>
45 #include <polypcore/log.h>
49 /* Read the PID data from the file descriptor fd, and return it. If no
50 * pid could be read, return 0, on failure (pid_t) -1 */
51 static pid_t read_pid(const char *fn, int fd) {
56 assert(fn && fd >= 0);
58 if ((r = pa_loop_read(fd, t, sizeof(t)-1)) < 0) {
59 pa_log(__FILE__": WARNING: failed to read PID file '%s': %s", fn, strerror(errno));
67 if ((e = strchr(t, '\n')))
70 if (pa_atou(t, &pid) < 0) {
71 pa_log(__FILE__": WARNING: failed to parse PID file '%s'", fn);
78 static int open_pid_file(const char *fn, int mode) {
85 pa_make_secure_parent_dir(fn);
87 if ((fd = open(fn, mode, S_IRUSR|S_IWUSR)) < 0) {
88 if (mode != O_RDONLY || errno != ENOENT)
89 pa_log(__FILE__": WARNING: failed to open PID file '%s': %s", fn, strerror(errno));
93 /* Try to lock the file. If that fails, go without */
94 if (pa_lock_fd(fd, 1) < 0)
97 if (fstat(fd, &st) < 0) {
98 pa_log(__FILE__": Failed to fstat() PID file '%s': %s", fn, strerror(errno));
102 /* Does the file still exist in the file system? When ye, w're done, otherwise restart */
103 if (st.st_nlink >= 1)
106 if (pa_lock_fd(fd, 0) < 0)
110 pa_log(__FILE__": Failed to close file '%s': %s", fn, strerror(errno));
131 /* Create a new PID file for the current process. */
132 int pa_pid_file_create(void) {
144 pa_runtime_path("pid", fn, sizeof(fn));
146 if ((fd = open_pid_file(fn, O_CREAT|O_RDWR)) < 0)
149 if ((pid = read_pid(fn, fd)) == (pid_t) -1)
150 pa_log(__FILE__": corrupt PID file, overwriting.");
153 if ((process = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, pid)) != NULL) {
154 CloseHandle(process);
156 if (kill(pid, 0) >= 0 || errno != ESRCH) {
158 pa_log(__FILE__": daemon already running.");
162 pa_log(__FILE__": stale PID file, overwriting.");
165 /* Overwrite the current PID file */
166 if (lseek(fd, 0, SEEK_SET) == (off_t) -1 || ftruncate(fd, 0) < 0) {
167 pa_log(__FILE__": failed to truncate PID fil: %s.", strerror(errno));
171 snprintf(t, sizeof(t), "%lu\n", (unsigned long) getpid());
174 if (pa_loop_write(fd, t, l) != (ssize_t) l) {
175 pa_log(__FILE__": failed to write PID file.");
190 /* Remove the PID file, if it is ours */
191 int pa_pid_file_remove(void) {
198 pa_runtime_path("pid", fn, sizeof(fn));
200 if ((fd = open_pid_file(fn, O_RDWR)) < 0) {
201 pa_log(__FILE__": WARNING: failed to open PID file '%s': %s", fn, strerror(errno));
205 if ((pid = read_pid(fn, fd)) == (pid_t) -1)
208 if (pid != getpid()) {
209 pa_log(__FILE__": WARNING: PID file '%s' not mine!", fn);
213 if (ftruncate(fd, 0) < 0) {
214 pa_log(__FILE__": failed to truncate PID file '%s': %s", fn, strerror(errno));
224 if (unlink(fn) < 0) {
225 pa_log(__FILE__": failed to remove PID file '%s': %s", fn, strerror(errno));
229 if ((p = pa_parent_dir(fn))) {
246 /* Check whether the daemon is currently running, i.e. if a PID file
247 * exists and the PID therein too. Returns 0 on succcess, -1
248 * otherwise. If pid is non-NULL and a running daemon was found,
249 * return its PID therein */
250 int pa_pid_file_check_running(pid_t *pid) {
251 return pa_pid_file_kill(0, pid);
256 /* Kill a current running daemon. Return non-zero on success, -1
257 * otherwise. If successful *pid contains the PID of the daemon
259 int pa_pid_file_kill(int sig, pid_t *pid) {
268 pa_runtime_path("pid", fn, sizeof(fn));
270 if ((fd = open_pid_file(fn, O_RDONLY)) < 0)
273 if ((*pid = read_pid(fn, fd)) == (pid_t) -1)
276 ret = kill(*pid, sig);
289 #else /* OS_IS_WIN32 */
291 int pa_pid_file_kill(int sig, pid_t *pid) {