summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/Makefile.am29
-rw-r--r--src/dfork.c207
-rw-r--r--src/dfork.h87
-rw-r--r--src/dlog.c58
-rw-r--r--src/dlog.h63
-rw-r--r--src/dnonblock.c37
-rw-r--r--src/dnonblock.h37
-rw-r--r--src/dpid.c124
-rw-r--r--src/dpid.h73
-rw-r--r--src/dsignal.c132
-rw-r--r--src/dsignal.h65
11 files changed, 912 insertions, 0 deletions
diff --git a/src/Makefile.am b/src/Makefile.am
new file mode 100644
index 0000000..129660f
--- /dev/null
+++ b/src/Makefile.am
@@ -0,0 +1,29 @@
+# $Id$
+#
+# This file is part of libdaemon.
+#
+# libdaemon is free software; you can redistribute it and/or modify it
+# under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# libdaemon is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+# General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with libdaemon; if not, write to the Free Software Foundation,
+# Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
+
+pkginclude_HEADERS = dlog.h dfork.h dsignal.h dnonblock.h dpid.h
+
+lib_LTLIBRARIES = libdaemon.la
+
+libdaemon_la_SOURCES = \
+ dlog.c dlog.h \
+ dfork.c dfork.h \
+ dsignal.c dsignal.h \
+ dnonblock.c dnonblock.h \
+ dpid.c dpid.h
+
diff --git a/src/dfork.c b/src/dfork.c
new file mode 100644
index 0000000..f9e10fb
--- /dev/null
+++ b/src/dfork.c
@@ -0,0 +1,207 @@
+/* $Id$ */
+
+/*
+ * This file is part of libdaemon.
+ *
+ * libdaemon is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * libdaemon is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with libdaemon; if not, write to the Free Software Foundation,
+ * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
+ */
+
+#include <sys/types.h>
+#include <unistd.h>
+#include <errno.h>
+#include <string.h>
+#include <fcntl.h>
+#include <sys/stat.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+#include "dfork.h"
+#include "dnonblock.h"
+#include "dlog.h"
+
+static int _daemon_retval_pipe[2] = { -1, -1 };
+
+static int _null_open(int f, int fd) {
+ int fd2;
+
+ if ((fd2 = open("/dev/null", f)) < 0)
+ return -1;
+
+ if (fd2 == fd)
+ return fd;
+
+ if (dup2(fd2, fd) < 0)
+ return -1;
+
+ close(fd2);
+ return fd;
+}
+
+pid_t daemon_fork(void) {
+ pid_t pid;
+ int _pipe[2];
+ FILE *pipe_in, *pipe_out;
+
+ pid_t p = (pid_t) -1;
+
+ if (pipe(_pipe) < 0 || !(pipe_out = fdopen(_pipe[0], "r")) || !(pipe_in = fdopen(_pipe[1], "w"))) {
+ daemon_log(LOG_ERR, "pipe() failed: %s", strerror(errno));
+ return (pid_t) -1;
+ }
+
+ daemon_nonblock(_pipe[1], 1);
+
+ if ((pid = fork()) < 0) { // First fork
+ daemon_log(LOG_ERR, "First fork() failed: %s\n", strerror(errno));
+ fclose(pipe_in);
+ fclose(pipe_out);
+ return (pid_t) -1;
+
+ } else if (pid == 0) {
+ // First child
+
+ fclose(pipe_out);
+
+ if (_null_open(O_RDONLY, 0) < 0) {
+ daemon_log(LOG_ERR, "Failed to open /dev/null for STDIN: %s", strerror(errno));
+ goto fail;
+ }
+
+ if (_null_open(O_WRONLY, 1) < 0) {
+ daemon_log(LOG_ERR, "Failed to open /dev/null for STDOUT: %s", strerror(errno));
+ goto fail;
+ }
+
+ if (_null_open(O_WRONLY, 2) < 0) {
+ daemon_log(LOG_ERR, "Failed to open /dev/null for STDERR: %s", strerror(errno));
+ goto fail;
+ }
+
+ setsid();
+ umask(0777);
+ chdir("/");
+
+ if ((pid = fork()) < 0) { // Second fork
+ daemon_log(LOG_ERR, "Second fork() failed: %s", strerror(errno));
+ goto fail;
+
+ } else if (pid == 0) {
+ // Second child
+ p = getpid();
+ fwrite(&p, sizeof(p), 1, pipe_in);
+ fclose(pipe_in);
+
+
+ if (daemon_log_use & DAEMON_LOG_AUTO)
+ daemon_log_use = DAEMON_LOG_SYSLOG;
+
+ return 0;
+
+ } else {
+ // Second father
+ fclose(pipe_in);
+ exit(0);
+ }
+
+ fail:
+ fwrite(&p, sizeof(p), 1, pipe_in);
+ fclose(pipe_in);
+ exit(0);
+
+ } else { // First father
+ if (fread(&p, sizeof(p), 1, pipe_out) != 1)
+ p = (pid_t) -1;
+
+ fclose(pipe_out);
+ return p;
+ }
+
+}
+
+int daemon_retval_init(void) {
+ if (pipe(_daemon_retval_pipe) < 0)
+ return -1;
+
+ return 0;
+}
+
+void daemon_retval_done(void) {
+ if (_daemon_retval_pipe[0] >= 0)
+ close(_daemon_retval_pipe[0]);
+
+ if (_daemon_retval_pipe[1] >= 0)
+ close(_daemon_retval_pipe[1]);
+
+ _daemon_retval_pipe[0] = _daemon_retval_pipe[1] = -1;
+}
+
+int daemon_retval_send(int i) {
+ ssize_t r;
+ r = write(_daemon_retval_pipe[1], &i, sizeof(i));
+
+ daemon_retval_done();
+
+ if (r != sizeof(i)) {
+
+ if (r < 0)
+ daemon_log(LOG_ERR, "read() failed while writing return value to pipe: %s", strerror(errno));
+ else
+ daemon_log(LOG_ERR, "write() too short while writing return value from pipe");
+
+ return -1;
+ }
+
+ return 0;
+}
+
+int daemon_retval_wait(int timeout) {
+ ssize_t r;
+ int i;
+
+ if (timeout > 0) {
+ struct timeval tv = { timeout, 0 };
+ int s;
+ fd_set fds;
+ FD_ZERO(&fds);
+ FD_SET(_daemon_retval_pipe[0], &fds);
+
+ if ((s = select(FD_SETSIZE, &fds, 0, 0, &tv)) != 1) {
+
+ if (s < 0)
+ daemon_log(LOG_ERR, "select() failed while waiting for return value: %s", strerror(errno));
+ else
+ daemon_log(LOG_ERR, "Timeout reached while wating for return value");
+
+ return -1;
+ }
+ }
+
+ if ((r = read(_daemon_retval_pipe[0], &i, sizeof(i))) != sizeof(i)) {
+
+ if (r < 0)
+ daemon_log(LOG_ERR, "read() failed while reading return value from pipe: %s", strerror(errno));
+ else if (r == 0)
+ daemon_log(LOG_ERR, "read() failed with EOF while reading return value from pipe.");
+ else if (r > 0)
+ daemon_log(LOG_ERR, "read() too short while reading return value from pipe.");
+
+ return -1;
+ }
+
+ daemon_retval_done();
+
+ return i;
+}
+
diff --git a/src/dfork.h b/src/dfork.h
new file mode 100644
index 0000000..1554184
--- /dev/null
+++ b/src/dfork.h
@@ -0,0 +1,87 @@
+#ifndef foodaemonforkhfoo
+#define foodaemonforkhfoo
+
+/*
+ * This file is part of libdaemon.
+ *
+ * libdaemon is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * libdaemon is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with libdaemon; if not, write to the Free Software Foundation,
+ * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
+ */
+
+#include <sys/types.h>
+
+/** \mainpage
+ *
+ * For a brief explanation of libdaemons's purpose, have a look on <a
+ * href="../../README.html">the README file</a>. Thank you!
+ *
+ */
+
+/** \example testd.c
+ * This is an example for the usage of libdaemon
+ */
+
+/** \file
+ *
+ * Contains an API for doing a daemonizing fork().
+ *
+ * You may daemonize by calling daemon_fork(), a function similar to
+ * the plain fork(). If you want to return a return value of the
+ * initialization procedure of the child from the parent, you may use
+ * the daemon_retval_xxx() functions.
+ */
+
+/** Does a daemonizing fork(). For the new daemon process STDIN,
+ * STDOUT, STDERR are connected to /dev/null, the process is a session
+ * leader, the current directory is changed to /, the umask is set to
+ * 777.
+ * @return zero on success in the child, the PID of the new
+ * daemon process in the parent, nonzero on failure
+ */
+pid_t daemon_fork(void);
+
+/** Initializes the library for allowing the passing of a return value
+ * from the daemon initialization to the parent process. Call this
+ * before calling any of the other daemon_retval_xxx() functions in
+ * the parent, before forking.
+ * @return zero on success, nonzero on failure.
+ */
+int daemon_retval_init(void);
+
+/** Frees the resources allocated by daemon_retval_init(). This should
+ * be called if neither daemon_retval_wait() nor daemon_retval_send()
+ * is used. If a fork took place, the function should be called in
+ * both the parent and the daemon. */
+void daemon_retval_done(void);
+
+/** Wait the specified amount of seconds for the response of the
+ * daemon process and return the integer passed to
+ * daemon_retval_send() in the daemon process. Should be called just
+ * once from the parent process only. A subsequent call to
+ * daemon_retval_done() in the parent is ignored.
+ * @param timeout The timeout in seconds
+ * @return The integer passed daemon_retval_send() in the daemon
+ * process, or negative on failure.
+ */
+int daemon_retval_wait(int timeout);
+
+/** Send the specified integer to the parent process. Should be called
+ * just once from the daemon process only. A subsequent call to
+ * daemon_retval_done() in the daemon is ignored.
+ * @param s The integer to pass to daemon_retval_wait() in the parent process
+ * @return Zero on success, nonzero on failure.
+ */
+int daemon_retval_send(int s);
+
+#endif
diff --git a/src/dlog.c b/src/dlog.c
new file mode 100644
index 0000000..36ce5dc
--- /dev/null
+++ b/src/dlog.c
@@ -0,0 +1,58 @@
+/*
+ * This file is part of libdaemon.
+ *
+ * libdaemon is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * libdaemon is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with libdaemon; if not, write to the Free Software Foundation,
+ * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
+ */
+
+#include <stdarg.h>
+#include <stdio.h>
+#include <string.h>
+
+#include "dlog.h"
+
+enum daemon_log_flags daemon_log_use = DAEMON_LOG_AUTO|DAEMON_LOG_STDERR;
+char* daemon_log_ident = 0;
+
+void daemon_log(int prio, const char* template, ...) {
+ va_list arglist;
+ va_start(arglist, template);
+ if (daemon_log_use & DAEMON_LOG_SYSLOG) {
+ openlog(daemon_log_ident ? daemon_log_ident : "UNKNOWN", LOG_PID|LOG_NDELAY, LOG_DAEMON);
+ vsyslog(prio, template, arglist);
+ closelog();
+ }
+
+ if (daemon_log_use & DAEMON_LOG_STDERR) {
+ vfprintf(stderr, template, arglist);
+ fprintf(stderr, "\n");
+ }
+
+ if (daemon_log_use & DAEMON_LOG_STDOUT) {
+ vfprintf(stdout, template, arglist);
+ fprintf(stdout, "\n");
+ }
+
+
+ va_end(arglist);
+}
+
+char *daemon_ident_from_argv0(char *argv0) {
+ char *p;
+
+ if ((p = strrchr(argv0, '/')))
+ return p+1;
+
+ return argv0;
+}
diff --git a/src/dlog.h b/src/dlog.h
new file mode 100644
index 0000000..8edf6b3
--- /dev/null
+++ b/src/dlog.h
@@ -0,0 +1,63 @@
+#ifndef foodaemonloghfoo
+#define foodaemonloghfoo
+
+/*
+ * This file is part of libdaemon.
+ *
+ * libdaemon is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * libdaemon is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with libdaemon; if not, write to the Free Software Foundation,
+ * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
+ */
+
+#include <syslog.h>
+
+
+/** \file
+ *
+ * Contains a robust API for logging messages
+ */
+
+
+/** Specifies where to send the log messages to
+ */
+enum daemon_log_flags {
+ DAEMON_LOG_SYSLOG = 1, /**< Log messages are written to syslog */
+ DAEMON_LOG_STDERR = 2, /**< Log messages are written to STDERR */
+ DAEMON_LOG_STDOUT = 4, /**< Log messages are written to STDOUT */
+ DAEMON_LOG_AUTO = 8 /**< If this is set a daemon_fork() will
+ change this to DAEMON_LOG_SYSLOG in
+ the daemon process. */
+};
+
+/** This variable is used to specify the log target(s) to use. Defaults to DAEMON_LOG_STDERR|DAEMON_LOG_AUTO */
+extern enum daemon_log_flags daemon_log_use;
+
+/** Specifies the syslog identification, use daemon_ident_from_argv0()
+ * to set this to a sensible value or generate your own. */
+extern char* daemon_log_ident;
+
+/** Log a message using printf format strings using the specified syslog priority
+ * @param prio The syslog priority (PRIO_xxx constants)
+ * @param t,... The text message to log
+ */
+void daemon_log(int prio, const char* t, ...);
+
+/** Return a sensible syslog identification for daemon_log_ident
+ * generated from argv[0]. This will return a pointer to the file name
+ * of argv[0], i.e. strrchr(argv[0], '\')+1
+ * @param argv0 argv[0] as passed to main()
+ * @return The identification string
+ */
+char *daemon_ident_from_argv0(char *argv0);
+
+#endif
diff --git a/src/dnonblock.c b/src/dnonblock.c
new file mode 100644
index 0000000..af24be2
--- /dev/null
+++ b/src/dnonblock.c
@@ -0,0 +1,37 @@
+/*
+ * This file is part of libdaemon.
+ *
+ * libdaemon is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * libdaemon is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with libdaemon; if not, write to the Free Software Foundation,
+ * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
+ */
+
+#include <fcntl.h>
+
+#include "dnonblock.h"
+
+int daemon_nonblock(int fd, int b) {
+ int a;
+ if ((a = fcntl(fd, F_GETFL)) < 0)
+ return -1;
+
+ if (b)
+ a |= O_NDELAY;
+ else
+ a &= ~O_NDELAY;
+
+ if (fcntl(fd, F_SETFL, a) < 0)
+ return -1;
+
+ return 0;
+}
diff --git a/src/dnonblock.h b/src/dnonblock.h
new file mode 100644
index 0000000..62a45b3
--- /dev/null
+++ b/src/dnonblock.h
@@ -0,0 +1,37 @@
+#ifndef foodaemonnonblockhfoo
+#define foodaemonnonblockhfoo
+
+/*
+ * This file is part of libdaemon.
+ *
+ * libdaemon is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * libdaemon is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with libdaemon; if not, write to the Free Software Foundation,
+ * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
+ */
+
+/** \file
+ *
+ * Contains a single function used to change a file descriptor to
+ * non-blocking mode using fcntl().
+ */
+
+/** Change the passed file descriptor to non-blocking or blocking
+ * mode, depending on b.
+ * @param fd The file descriptor to manipulation
+ * @param b TRUE if non-blocking mode should be enabled, FALSE if it
+ * should be disabled
+ * @return Zero on success, nonzero on failure.
+ */
+int daemon_nonblock(int fd, int b);
+
+#endif
diff --git a/src/dpid.c b/src/dpid.c
new file mode 100644
index 0000000..6d91cae
--- /dev/null
+++ b/src/dpid.c
@@ -0,0 +1,124 @@
+/*
+ * This file is part of libdaemon.
+ *
+ * libdaemon is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * libdaemon is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with libdaemon; if not, write to the Free Software Foundation,
+ * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
+ */
+
+#include <limits.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <signal.h>
+#include <string.h>
+#include <unistd.h>
+#include <errno.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+
+#include "dpid.h"
+#include "dlog.h"
+
+#define VARRUN "/var/run"
+
+const char *daemon_pid_file_ident = 0;
+daemon_pid_file_proc_t daemon_pid_file_proc = daemon_pid_file_proc_default;
+
+const char *daemon_pid_file_proc_default(void) {
+ static char fn[PATH_MAX];
+ snprintf(fn, sizeof(fn), "%s/%s.pid", VARRUN, daemon_pid_file_ident ? daemon_pid_file_ident : "unknown");
+ return fn;
+}
+
+pid_t daemon_pid_file_is_running(void) {
+ const char *fn;
+ static char txt[256];
+ FILE *f;
+ pid_t pid;
+
+
+ if (!(fn = daemon_pid_file_proc()))
+ return (pid_t) -1;
+
+ if (!(f = fopen(fn, "r")))
+ return (pid_t) -1;
+
+ if (!(fgets(txt, sizeof(txt), f))) {
+ daemon_log(LOG_WARNING, "PID file corrupt, removing. (%s)", fn);
+ unlink(fn);
+ fclose(f);
+ return (pid_t) -1;
+ }
+
+ fclose(f);
+
+ if ((pid = (pid_t) atoi(txt)) <= 0) {
+ daemon_log(LOG_WARNING, "PID file corrupt, removing. (%s)", fn);
+ unlink(fn);
+ return (pid_t) -1;
+ }
+
+ if (kill(pid, 0) != 0 && errno != EPERM) {
+ daemon_log(LOG_WARNING, "Daemon %u killed: %s; removing PID file. (%s)", pid, strerror(errno), fn);
+ unlink(fn);
+ return (pid_t) -1;
+ }
+
+ return pid;
+}
+
+int daemon_pid_file_kill(int s) {
+ pid_t pid;
+
+ if ((pid = daemon_pid_file_is_running()) < 0)
+ return -1;
+
+ if (kill(pid, s) < 0)
+ return -1;
+
+ return 0;
+}
+
+int daemon_pid_file_create(void) {
+ const char *fn;
+ FILE *f;
+ mode_t save;
+
+ if (!(fn = daemon_pid_file_proc()))
+ return -1;
+
+ save = umask(022);
+
+ if (!(f = fopen(fn, "w")))
+ return -1;
+
+ fprintf(f, "%u\n", getpid());
+ fclose(f);
+
+ umask(save);
+
+ return 0;
+}
+
+int daemon_pid_file_remove(void) {
+ const char *fn;
+
+ if (!(fn = daemon_pid_file_proc()))
+ return -1;
+
+ if (unlink(fn) < 0)
+ return -1;
+
+ return 0;
+}
+
diff --git a/src/dpid.h b/src/dpid.h
new file mode 100644
index 0000000..8dde464
--- /dev/null
+++ b/src/dpid.h
@@ -0,0 +1,73 @@
+#ifndef foodaemonpidhfoo
+#define foodaemonpidhfoo
+
+/*
+ * This file is part of libdaemon.
+ *
+ * libdaemon is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * libdaemon is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with libdaemon; if not, write to the Free Software Foundation,
+ * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
+ */
+
+#include <sys/types.h>
+
+/** \file
+ *
+ * Contains an API for manipulating PID files.
+ */
+
+/** Prototype of a function for generating the name of a PID file.
+ */
+typedef const char* (*daemon_pid_file_proc_t)(void);
+
+/** Identification string for the PID file name, only used when
+ * daemon_pid_file_proc is set to daemon_pid_file_proc_default(). Use
+ * daemon_ident_from_argv0() to generate an identification string from
+ * argv[0]
+ */
+extern const char *daemon_pid_file_ident;
+
+/** A function pointer which is used to generate the name of the PID
+ * file to manipulate. Points to daemon_pid_file_proc_default() by
+ * default.
+ */
+extern daemon_pid_file_proc_t daemon_pid_file_proc;
+
+/** A function for creating a pid file name from
+ * daemon_pid_file_ident
+ * @return The PID file path
+ */
+const char *daemon_pid_file_proc_default(void);
+
+/** Creates PID pid file for the current process
+ * @return zero on success, nonzero on failure
+ */
+int daemon_pid_file_create(void);
+
+/** Removes the PID file of the current process
+ * @return zero on success, nonzero on failure
+ */
+int daemon_pid_file_remove(void);
+
+/** Returns the PID file of a running daemon, if available
+ * @return The PID or negative on failure
+ */
+pid_t daemon_pid_file_is_running(void);
+
+/** Kills a running daemon, if available
+ * @param s The signal to send
+ * @return zero on success, nonzero on failure
+ */
+int daemon_pid_file_kill(int s);
+
+#endif
diff --git a/src/dsignal.c b/src/dsignal.c
new file mode 100644
index 0000000..c41aa9d
--- /dev/null
+++ b/src/dsignal.c
@@ -0,0 +1,132 @@
+/*
+ * This file is part of libdaemon.
+ *
+ * libdaemon is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * libdaemon is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with libdaemon; if not, write to the Free Software Foundation,
+ * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
+ */
+
+#define _GNU_SOURCE
+
+#include <fcntl.h>
+#include <unistd.h>
+#include <errno.h>
+#include <string.h>
+#include <stdarg.h>
+#include <signal.h>
+
+#include "dsignal.h"
+#include "dlog.h"
+#include "dnonblock.h"
+
+static int _signal_pipe[2] = { -1, -1 };
+
+static void _sigfunc(int s) {
+ write(_signal_pipe[1], &s, sizeof(s));
+}
+
+static int _init(void) {
+
+ if (_signal_pipe[0] < 0 || _signal_pipe[1] < 0) {
+ if (pipe(_signal_pipe) < 0) {
+ daemon_log(LOG_ERR, "pipe(): %s", strerror(errno));
+ return -1;
+ }
+
+ if (daemon_nonblock(_signal_pipe[0], 1) < 0 || daemon_nonblock(_signal_pipe[1], 1) < 0)
+ return -1;
+
+ }
+
+ return 0;
+}
+
+int daemon_signal_install(int s){
+ struct sigaction sa;
+
+ if (_init() < 0)
+ return -1;
+
+ memset(&sa, 0, sizeof(sa));
+ sa.sa_handler = _sigfunc;
+ sigemptyset(&sa.sa_mask);
+ sa.sa_flags = SA_RESTART;
+
+ if (sigaction(s, &sa, NULL) < 0) {
+ daemon_log(LOG_ERR, "sigaction(%s, ...) failed.", strsignal(s));
+ return -1;
+ }
+
+ return 0;
+}
+
+int daemon_signal_init(int s, ...) {
+ int sig, r = 0;
+
+ va_list ap;
+ va_start(ap, s);
+
+ if (_init() < 0)
+ return -1;
+
+ sig = s;
+ while (sig > 0) {
+ if (daemon_signal_install(sig) < 0) {
+ r = -1;
+ break;
+ }
+
+ sig = va_arg(ap, int);
+ }
+
+ va_end(ap);
+
+
+ return r;
+}
+
+void daemon_signal_done(void) {
+ if (_signal_pipe[0] != -1)
+ close(_signal_pipe[0]);
+
+ if (_signal_pipe[1] != -1)
+ close(_signal_pipe[1]);
+
+ _signal_pipe[0] = _signal_pipe[1] = -1;
+}
+
+int daemon_signal_next(void) {
+ int s;
+ ssize_t r;
+
+ if ((r = read(_signal_pipe[0], &s, sizeof(s))) == sizeof(s))
+ return s;
+
+
+ if (r < 0) {
+
+ if (errno == EAGAIN)
+ return 0;
+ else {
+ daemon_log(LOG_ERR, "read(signal_pipe[0], ...): %s", strerror(errno));
+ return -1;
+ }
+ }
+
+ daemon_log(LOG_ERR, "Short read() on signal pipe.");
+ return -1;
+}
+
+int daemon_signal_fd(void) {
+ return _signal_pipe[0];
+}
diff --git a/src/dsignal.h b/src/dsignal.h
new file mode 100644
index 0000000..a26b5cc
--- /dev/null
+++ b/src/dsignal.h
@@ -0,0 +1,65 @@
+#ifndef foodaemonsignalhfoo
+#define foodaemonsignalhfoo
+
+/*
+ * This file is part of libdaemon.
+ *
+ * libdaemon is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * libdaemon is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with libdaemon; if not, write to the Free Software Foundation,
+ * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
+ */
+
+/** \file
+ *
+ * Contains the API for serializing signals to a pipe for
+ * usage with select() or poll().
+ *
+ * You should register all signals you
+ * wish to handle with select() in your main loop with
+ * daemon_signal_init() or daemon_signal_install(). After that you
+ * should sleep on the file descriptor returned by daemon_signal_fd()
+ * and get the next signal recieved with daemon_signal_next(). You
+ * should call daemon_signal_done() before exiting.
+ */
+
+/** Installs signal handlers for the specified signals
+ * @param s, ... The signals to install handlers for. The list should be terminated by -1
+ * @return zero on success, nonzero on failure
+ */
+int daemon_signal_init(int s, ...);
+
+/** Install a signal handler for the specified signal
+ * @param s The signalto install handler for
+ * @return zero onsuccess,nonzero on failure
+ */
+int daemon_signal_install(int s);
+
+/** Free resources of signal handling, should be called before daemon exit
+ */
+void daemon_signal_done(void);
+
+/** Return the next signal recieved. This function will not
+ * block. Instead it returns 0 if no signal is queued.
+ * @return The next queued signal if one is queued, zero if none is
+ * queued, negative on failure.
+ */
+int daemon_signal_next(void);
+
+/** Return the file descriptor the daemon should select() on for
+ * reading. Whenever the descriptor is ready you should call
+ * daemon_signal_next() to get the next signal queued.
+ * @return The file descriptor or negative on failure
+ */
+int daemon_signal_fd(void);
+
+#endif