From 91511db7500439088b8b3bb08ce6ee6f8f8e6499 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Sat, 18 Dec 2004 23:29:50 +0000 Subject: rename src/ to libdaemon/ git-svn-id: file:///home/lennart/svn/public/libdaemon/trunk@71 153bfa13-eec0-0310-be40-b0cb6a0e1b4b --- Makefile.am | 2 +- configure.ac | 4 +- libdaemon/Makefile.am | 42 +++++++ libdaemon/daemon.h | 36 ++++++ libdaemon/dexec.c | 177 ++++++++++++++++++++++++++ libdaemon/dexec.h | 55 ++++++++ libdaemon/dfork.c | 340 ++++++++++++++++++++++++++++++++++++++++++++++++++ libdaemon/dfork.h | 104 +++++++++++++++ libdaemon/dlog.c | 64 ++++++++++ libdaemon/dlog.h | 71 +++++++++++ libdaemon/dnonblock.c | 43 +++++++ libdaemon/dnonblock.h | 47 +++++++ libdaemon/dpid.c | 161 ++++++++++++++++++++++++ libdaemon/dpid.h | 97 ++++++++++++++ libdaemon/dsignal.c | 152 ++++++++++++++++++++++ libdaemon/dsignal.h | 76 +++++++++++ src/Makefile.am | 42 ------- src/daemon.h | 36 ------ src/dexec.c | 177 -------------------------- src/dexec.h | 55 -------- src/dfork.c | 340 -------------------------------------------------- src/dfork.h | 104 --------------- src/dlog.c | 64 ---------- src/dlog.h | 71 ----------- src/dnonblock.c | 43 ------- src/dnonblock.h | 47 ------- src/dpid.c | 161 ------------------------ src/dpid.h | 97 -------------- src/dsignal.c | 152 ---------------------- src/dsignal.h | 76 ----------- 30 files changed, 1468 insertions(+), 1468 deletions(-) create mode 100644 libdaemon/Makefile.am create mode 100644 libdaemon/daemon.h create mode 100644 libdaemon/dexec.c create mode 100644 libdaemon/dexec.h create mode 100644 libdaemon/dfork.c create mode 100644 libdaemon/dfork.h create mode 100644 libdaemon/dlog.c create mode 100644 libdaemon/dlog.h create mode 100644 libdaemon/dnonblock.c create mode 100644 libdaemon/dnonblock.h create mode 100644 libdaemon/dpid.c create mode 100644 libdaemon/dpid.h create mode 100644 libdaemon/dsignal.c create mode 100644 libdaemon/dsignal.h delete mode 100644 src/Makefile.am delete mode 100644 src/daemon.h delete mode 100644 src/dexec.c delete mode 100644 src/dexec.h delete mode 100644 src/dfork.c delete mode 100644 src/dfork.h delete mode 100644 src/dlog.c delete mode 100644 src/dlog.h delete mode 100644 src/dnonblock.c delete mode 100644 src/dnonblock.h delete mode 100644 src/dpid.c delete mode 100644 src/dpid.h delete mode 100644 src/dsignal.c delete mode 100644 src/dsignal.h diff --git a/Makefile.am b/Makefile.am index 379769e..aa41f00 100644 --- a/Makefile.am +++ b/Makefile.am @@ -17,7 +17,7 @@ # Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. EXTRA_DIST=bootstrap.sh README LICENSE libdaemon.spec -SUBDIRS=src doc examples +SUBDIRS=libdaemon doc examples MAINTAINERCLEANFILES = README libdaemon.spec noinst_DATA = README libdaemon.spec diff --git a/configure.ac b/configure.ac index 8879e24..c89af5a 100644 --- a/configure.ac +++ b/configure.ac @@ -21,7 +21,7 @@ AC_PREREQ(2.59) AC_INIT([libdaemon],[0.7],[mzqnrzba (at) 0pointer (dot) de]) -AC_CONFIG_SRCDIR([src/dfork.c]) +AC_CONFIG_SRCDIR([libdaemon/dfork.c]) AC_CONFIG_HEADERS([config.h]) AM_INIT_AUTOMAKE([foreign -Wall]) @@ -87,5 +87,5 @@ fi AM_CONDITIONAL([USE_LYNX], [test "x$lynx" = xyes]) -AC_CONFIG_FILES([src/Makefile Makefile doc/Makefile doc/README.html doc/doxygen.conf examples/Makefile libdaemon.spec libdaemon.pc]) +AC_CONFIG_FILES([libdaemon/Makefile Makefile doc/Makefile doc/README.html doc/doxygen.conf examples/Makefile libdaemon.spec libdaemon.pc]) AC_OUTPUT diff --git a/libdaemon/Makefile.am b/libdaemon/Makefile.am new file mode 100644 index 0000000..c14000c --- /dev/null +++ b/libdaemon/Makefile.am @@ -0,0 +1,42 @@ +# $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. + +AM_CFLAGS=-D_GNU_SOURCE + +pkginclude_HEADERS = \ + dlog.h \ + dfork.h \ + dsignal.h \ + dnonblock.h \ + dpid.h \ + dexec.h \ + daemon.h + +lib_LTLIBRARIES = libdaemon.la + +libdaemon_la_SOURCES = \ + dlog.c \ + dfork.c \ + dsignal.c \ + dnonblock.c \ + dpid.c \ + dexec.c \ + daemon.h \ + $(pkg_include_HEADERS) + +libdaemon_la_LDFLAGS = -version-info 2:1:2 diff --git a/libdaemon/daemon.h b/libdaemon/daemon.h new file mode 100644 index 0000000..eca859c --- /dev/null +++ b/libdaemon/daemon.h @@ -0,0 +1,36 @@ +#ifndef foodaemonhfoo +#define foodaemonhfoo + +/* $Id: exec.h 4 2003-08-10 19:56:53Z lennart $ */ + +/* + * 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 + * + * A header file including all other header files part of libdaemon + */ + +#include +#include +#include +#include +#include +#include + +#endif diff --git a/libdaemon/dexec.c b/libdaemon/dexec.c new file mode 100644 index 0000000..e1d1910 --- /dev/null +++ b/libdaemon/dexec.c @@ -0,0 +1,177 @@ +/* $Id: exec.c 31 2003-11-05 22:04:26Z lennart $ */ + +/* + * 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. + */ + +#ifdef HAVE_CONFIG_H +#include +#endif + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "dlog.h" +#include "dsignal.h" + +#include "dexec.h" + +#define MAX_ARGS 100 + +int daemon_exec(const char *dir, int *ret, const char *prog, ...) { + pid_t pid; + int p[2]; + unsigned n = 0; + static char buf[256]; + int sigfd, r; + fd_set fds; + va_list ap; + + if (pipe(p) < 0) { + daemon_log(LOG_ERR, "pipe() failed: %s", strerror(errno)); + return -1; + } + + if ((pid = fork()) < 0) { + daemon_log(LOG_ERR, "fork() failed: %s", strerror(errno)); + return -1; + + } else if (pid == 0) { + char *args[MAX_ARGS]; + int i; + + if (p[1] != 1) + dup2(p[1], 1); + + if (p[1] != 2) + dup2(p[1], 2); + + if (p[0] > 2) + close(p[0]); + + if (p[1] > 2) + close(p[1]); + + close(0); + if (open("/dev/null", O_RDONLY) != 0) { + daemon_log(LOG_ERR, "Unable to open /dev/null as STDIN"); + _exit(EXIT_FAILURE); + } + + umask(0022); /* Set up a sane umask */ + + if (dir && chdir(dir) < 0) { + daemon_log(LOG_WARNING, "Failed to change to directory '%s'", dir); + chdir("/"); + } + + va_start(ap, prog); + for (i = 0; i < MAX_ARGS-1; i++) + if (!(args[i] = va_arg(ap, char*))) + break; + args[i] = NULL; + va_end(ap); + + execv(prog, args); + + daemon_log(LOG_ERR, "execv(%s) failed: %s\n", prog, strerror(errno)); + + _exit(EXIT_FAILURE); + } + + close(p[1]); + + FD_ZERO(&fds); + FD_SET(p[0], &fds); + FD_SET(sigfd = daemon_signal_fd(), &fds); + + n = 0; + + for (;;) { + fd_set qfds = fds; + + if (select(FD_SETSIZE, &qfds, NULL, NULL, NULL) < 0) { + + if (errno == EINTR) + continue; + + daemon_log(LOG_ERR, "select() failed: %s", strerror(errno)); + return -1; + } + + + if (FD_ISSET(p[0], &qfds)) { + char c; + + if (read(p[0], &c, 1) != 1) + break; + + buf[n] = c; + + if (c == '\n' || n >= sizeof(buf) - 2) { + if (c != '\n') n++; + buf[n] = 0; + + if (buf[0]) + daemon_log(LOG_INFO, "client: %s", buf); + + n = 0; + } else + n++; + } + + if (FD_ISSET(sigfd, &qfds)) { + int sig; + + if ((sig = daemon_signal_next()) < 0) { + daemon_log(LOG_ERR, "daemon_signal_next(): %s", strerror(errno)); + break; + } + + if (sig != SIGCHLD) { + daemon_log(LOG_WARNING, "Killing child."); + kill(pid, SIGTERM); + } + } + } + + if (n > 0) { + buf[n] = 0; + daemon_log(LOG_WARNING, "client: %s", buf); + } + + waitpid(pid, &r, 0); + + close(p[0]); + + if (!WIFEXITED(r)) + return -1; + + if (ret) + *ret = WEXITSTATUS(r); + return 0; +} diff --git a/libdaemon/dexec.h b/libdaemon/dexec.h new file mode 100644 index 0000000..d15d49c --- /dev/null +++ b/libdaemon/dexec.h @@ -0,0 +1,55 @@ +#ifndef foodexechfoo +#define foodexechfoo + +/* $Id: exec.h 4 2003-08-10 19:56:53Z lennart $ */ + +/* + * 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. + */ + +#ifdef __cplusplus +extern "C" { +#endif + +/** \file + * + * Contains a robust API for running sub processes with STDOUT and + * STDERR redirected to syslog + */ + +/** If this variable is defined to 1 iff daemon_exec() is supported.*/ +#define DAEMON_EXEC_AVAILABLE 1 + +/** Run the specified executable with the specified arguments in the + * specified directory and return the return value of the program in + * the specified pointer. The calling process is blocked until the + * child finishes and all child output (either STDOUT or STDIN) has + * been written to syslog. + * + * @param dir Working directory for the process. + * @param ret A pointer to an integer to write the return value of the program to. + * @param prog The path to the executable + * @param ... The arguments to be passed to the program, followed by a (char *) NULL + * @return Nonzero on failure, zero on success + */ +int daemon_exec(const char *dir, int *ret, const char *prog, ...); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/libdaemon/dfork.c b/libdaemon/dfork.c new file mode 100644 index 0000000..2fa9f30 --- /dev/null +++ b/libdaemon/dfork.c @@ -0,0 +1,340 @@ +/* $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. + */ + +#ifdef HAVE_CONFIG_H +#include +#endif + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#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; +} + +static ssize_t atomic_read(int fd, void *d, size_t l) { + ssize_t t = 0; + + while (l > 0) { + ssize_t r; + + if ((r = read(fd, d, l)) <= 0) { + + if (r < 0) + return t > 0 ? t : -1; + else + return t; + } + + t += r; + d = (char*) d + r; + l -= r; + } + + return t; +} + +static ssize_t atomic_write(int fd, const void *d, size_t l) { + ssize_t t = 0; + + while (l > 0) { + ssize_t r; + + if ((r = write(fd, d, l)) <= 0) { + + if (r < 0) + return t > 0 ? t : -1; + else + return t; + } + + t += r; + d = (char*) d + r; + l -= r; + } + + return t; +} + +static int move_fd_up(int *fd) { + assert(fd); + + while (*fd <= 2) { + if ((*fd = dup(*fd)) < 0) { + daemon_log(LOG_ERR, "dup(): %s", strerror(errno)); + return -1; + } + } + + return 0; +} + +static void sigchld(int s) { +} + +pid_t daemon_fork(void) { + pid_t pid; + int pipe_fds[2] = {-1, -1}; + struct sigaction sa_old, sa_new; + sigset_t ss_old, ss_new; + + memset(&sa_new, 0, sizeof(sa_new)); + sa_new.sa_handler = sigchld; + sa_new.sa_flags = SA_RESTART; + + if (sigaction(SIGCHLD, &sa_new, &sa_old) < 0) { + daemon_log(LOG_ERR, "sigaction() failed: %s", strerror(errno)); + return (pid_t) -1; + } + + sigemptyset(&ss_new); + sigaddset(&ss_new, SIGCHLD); + + if (sigprocmask(SIG_UNBLOCK, &ss_new, &ss_old) < 0) { + daemon_log(LOG_ERR, "sigprocmask() failed: %s", strerror(errno)); + sigaction(SIGCHLD, &sa_old, NULL); + return (pid_t) -1; + } + + if (pipe(pipe_fds) < 0) { + daemon_log(LOG_ERR, "pipe() failed: %s", strerror(errno)); + sigaction(SIGCHLD, &sa_old, NULL); + sigprocmask(SIG_SETMASK, &ss_old, NULL); + return (pid_t) -1; + } + + if ((pid = fork()) < 0) { // First fork + daemon_log(LOG_ERR, "First fork() failed: %s\n", strerror(errno)); + close(pipe_fds[0]); + close(pipe_fds[1]); + sigaction(SIGCHLD, &sa_old, NULL); + sigprocmask(SIG_SETMASK, &ss_old, NULL); + return (pid_t) -1; + + } else if (pid == 0) { + pid_t dpid; + + /* First child */ + + sigaction(SIGCHLD, &sa_old, NULL); + sigprocmask(SIG_SETMASK, &ss_old, NULL); + close(pipe_fds[0]); + + /* Move file descriptors up*/ + if (move_fd_up(&pipe_fds[1]) < 0) + goto fail; + if (_daemon_retval_pipe[0] >= 0 && move_fd_up(&_daemon_retval_pipe[0]) < 0) + goto fail; + if (_daemon_retval_pipe[1] >= 0 && move_fd_up(&_daemon_retval_pipe[1]) < 0) + goto fail; + + 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) { + int tty_fd; + /* Second child */ + + if (daemon_log_use & DAEMON_LOG_AUTO) + daemon_log_use = DAEMON_LOG_SYSLOG; + + signal(SIGTTOU, SIG_IGN); + signal(SIGTTIN, SIG_IGN); + signal(SIGTSTP, SIG_IGN); + + setsid(); + setpgrp(); + + if ((tty_fd = open("/dev/tty", O_RDWR)) >= 0) { + ioctl(tty_fd, TIOCNOTTY, NULL); + close(tty_fd); + } + + dpid = getpid(); + if (atomic_write(pipe_fds[1], &dpid, sizeof(dpid)) != sizeof(dpid)) + goto fail; + close(pipe_fds[1]); + + + return 0; + + } else { + /* Second father */ + close(pipe_fds[1]); + exit(0); + } + + fail: + dpid = (pid_t) -1; + if (atomic_write(pipe_fds[1], &dpid, sizeof(dpid)) != sizeof(dpid)) + daemon_log(LOG_ERR, "Failed to write error PID."); + close(pipe_fds[1]); + exit(0); + + } else { + /* First father */ + pid_t dpid; + + close(pipe_fds[1]); + waitpid(pid, NULL, WUNTRACED); + + sigprocmask(SIG_SETMASK, &ss_old, NULL); + sigaction(SIGCHLD, &sa_old, NULL); + + if (atomic_read(pipe_fds[0], &dpid, sizeof(dpid)) != sizeof(dpid)) { + daemon_log(LOG_ERR, "Failed to read daemon PID."); + dpid = (pid_t) -1; + } + + close(pipe_fds[0]); + return dpid; + } + +} + +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 = atomic_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; + int s; + fd_set fds; + + tv.tv_sec = timeout; + tv.tv_usec = 0; + + 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 = atomic_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/libdaemon/dfork.h b/libdaemon/dfork.h new file mode 100644 index 0000000..faac652 --- /dev/null +++ b/libdaemon/dfork.h @@ -0,0 +1,104 @@ +#ifndef foodaemonforkhfoo +#define foodaemonforkhfoo + +/* $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 + +#ifdef __cplusplus +extern "C" { +#endif + +/** \mainpage + * + * For a brief explanation of libdaemons's purpose, have a look on the README file. 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 On success, the PID of the child process is returned in the + * parent's thread of execution, and a 0 is returned in the child's + * thread of execution. On failure, -1 will be returned in the + * parent's context, no child process will be created, and errno will + * be set appropriately. + */ +pid_t daemon_fork(void); + +/** Allocate and initialize resources required by the + * daemon_retval_xxx() functions. These functions allow the child to + * send a value to the parent after completing its initialisation. + * Call this 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 called in the current process. The resources allocated by + * daemon_retval_init() should be freed in both parent and daemon + * process. This may be achieved by using daemon_retval_wait() + * resp. daemon_retval_send(), or by using daemon_retval_done(). + */ +void daemon_retval_done(void); + +/** Return the value sent by the child via the daemon_retval_send() + * function, but wait only the specified number of seconds before + * timing out and returning a negative number. Should be called just + * once from the parent process only. A subsequent call to + * daemon_retval_done() in the parent is ignored. + * + * @param timeout Thetimeout in seconds + * @return The integer passed daemon_retval_send() in the daemon process, or -1 on failure. + */ +int daemon_retval_wait(int timeout); + +/** Send the specified integer to the parent process. Do not send -1 + * because this signifies a library error. 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); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/libdaemon/dlog.c b/libdaemon/dlog.c new file mode 100644 index 0000000..1fc3e08 --- /dev/null +++ b/libdaemon/dlog.c @@ -0,0 +1,64 @@ +/* $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. + */ + +#ifdef HAVE_CONFIG_H +#include +#endif + +#include +#include +#include + +#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/libdaemon/dlog.h b/libdaemon/dlog.h new file mode 100644 index 0000000..0f74dde --- /dev/null +++ b/libdaemon/dlog.h @@ -0,0 +1,71 @@ +#ifndef foodaemonloghfoo +#define foodaemonloghfoo + +/* $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 + +#ifdef __cplusplus +extern "C" { +#endif + +/** \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); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/libdaemon/dnonblock.c b/libdaemon/dnonblock.c new file mode 100644 index 0000000..6c50279 --- /dev/null +++ b/libdaemon/dnonblock.c @@ -0,0 +1,43 @@ +/* $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. + */ + +#ifdef HAVE_CONFIG_H +#include +#endif + +#include + +#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/libdaemon/dnonblock.h b/libdaemon/dnonblock.h new file mode 100644 index 0000000..16efa25 --- /dev/null +++ b/libdaemon/dnonblock.h @@ -0,0 +1,47 @@ +#ifndef foodaemonnonblockhfoo +#define foodaemonnonblockhfoo + +/* $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. + */ + +#ifdef __cplusplus +extern "C" { +#endif + +/** \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); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/libdaemon/dpid.c b/libdaemon/dpid.c new file mode 100644 index 0000000..e41e37c --- /dev/null +++ b/libdaemon/dpid.c @@ -0,0 +1,161 @@ +/* $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. + */ + +#ifdef HAVE_CONFIG_H +#include +#endif + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#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_kill_wait(int s, int m) { + pid_t pid; + time_t t; + + if ((pid = daemon_pid_file_is_running()) < 0) + return -1; + + if (kill(pid, s) < 0) + return -1; + + t = time(NULL) + m; + + for (;;) { + int r; + struct timeval tv = { 0, 100000 }; + + if (time(NULL) > t) + return -1; + + if ((r = kill(pid, 0)) < 0 && errno != ESRCH) + return -1; + + if (r) + return 0; + + if (select(0, NULL, NULL, NULL, &tv) < 0) + return -1; + } +} + +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/libdaemon/dpid.h b/libdaemon/dpid.h new file mode 100644 index 0000000..cea9613 --- /dev/null +++ b/libdaemon/dpid.h @@ -0,0 +1,97 @@ +#ifndef foodaemonpidhfoo +#define foodaemonpidhfoo + +/* $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 + +#ifdef __cplusplus +extern "C" { +#endif + +/** \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); + +/** If this variable is defined to 1 iff daemon_pid_file_kill_wait() is supported.*/ +#define DAEMON_PID_FILE_KILL_WAIT_AVAILABLE 1 + +/** Similar to daemon_pid_file_kill() but waits until the process + * died. This functions is new in libdaemon 0.3. The macro + * DAEMON_PID_FILE_KILL_WAIT_AVAILABLE is defined iff libdaemon + * supports this function. + * + * @param s The signal to send + * @param m Seconds to wait at maximum + * @return zero on success, nonzero on failure (timeout condition is considered a failure) + */ +int daemon_pid_file_kill_wait(int s, int m); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/libdaemon/dsignal.c b/libdaemon/dsignal.c new file mode 100644 index 0000000..2eafc0c --- /dev/null +++ b/libdaemon/dsignal.c @@ -0,0 +1,152 @@ +/* $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. + */ + +#ifdef HAVE_CONFIG_H +#include +#endif + +#include +#include +#include +#include +#include +#include + +#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){ + sigset_t sigset; + struct sigaction sa; + + if (_init() < 0) + return -1; + + if (sigemptyset(&sigset) < 0) { + daemon_log(LOG_ERR, "sigemptyset(): %s", strerror(errno)); + return -1; + } + + if (sigaddset(&sigset, s) < 0) { + daemon_log(LOG_ERR, "sigaddyset(): %s", strerror(errno)); + return -1; + } + + if (sigprocmask(SIG_UNBLOCK, &sigset, NULL) < 0) { + daemon_log(LOG_ERR, "sigprocmask(): %s", strerror(errno)); + 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: %s", strsignal(s), strerror(errno)); + 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/libdaemon/dsignal.h b/libdaemon/dsignal.h new file mode 100644 index 0000000..ef3342c --- /dev/null +++ b/libdaemon/dsignal.h @@ -0,0 +1,76 @@ +#ifndef foodaemonsignalhfoo +#define foodaemonsignalhfoo + +/* $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. + */ + +#ifdef __cplusplus +extern "C" { +#endif + +/** \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 0 + * @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); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/src/Makefile.am b/src/Makefile.am deleted file mode 100644 index c14000c..0000000 --- a/src/Makefile.am +++ /dev/null @@ -1,42 +0,0 @@ -# $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. - -AM_CFLAGS=-D_GNU_SOURCE - -pkginclude_HEADERS = \ - dlog.h \ - dfork.h \ - dsignal.h \ - dnonblock.h \ - dpid.h \ - dexec.h \ - daemon.h - -lib_LTLIBRARIES = libdaemon.la - -libdaemon_la_SOURCES = \ - dlog.c \ - dfork.c \ - dsignal.c \ - dnonblock.c \ - dpid.c \ - dexec.c \ - daemon.h \ - $(pkg_include_HEADERS) - -libdaemon_la_LDFLAGS = -version-info 2:1:2 diff --git a/src/daemon.h b/src/daemon.h deleted file mode 100644 index eca859c..0000000 --- a/src/daemon.h +++ /dev/null @@ -1,36 +0,0 @@ -#ifndef foodaemonhfoo -#define foodaemonhfoo - -/* $Id: exec.h 4 2003-08-10 19:56:53Z lennart $ */ - -/* - * 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 - * - * A header file including all other header files part of libdaemon - */ - -#include -#include -#include -#include -#include -#include - -#endif diff --git a/src/dexec.c b/src/dexec.c deleted file mode 100644 index e1d1910..0000000 --- a/src/dexec.c +++ /dev/null @@ -1,177 +0,0 @@ -/* $Id: exec.c 31 2003-11-05 22:04:26Z lennart $ */ - -/* - * 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. - */ - -#ifdef HAVE_CONFIG_H -#include -#endif - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include "dlog.h" -#include "dsignal.h" - -#include "dexec.h" - -#define MAX_ARGS 100 - -int daemon_exec(const char *dir, int *ret, const char *prog, ...) { - pid_t pid; - int p[2]; - unsigned n = 0; - static char buf[256]; - int sigfd, r; - fd_set fds; - va_list ap; - - if (pipe(p) < 0) { - daemon_log(LOG_ERR, "pipe() failed: %s", strerror(errno)); - return -1; - } - - if ((pid = fork()) < 0) { - daemon_log(LOG_ERR, "fork() failed: %s", strerror(errno)); - return -1; - - } else if (pid == 0) { - char *args[MAX_ARGS]; - int i; - - if (p[1] != 1) - dup2(p[1], 1); - - if (p[1] != 2) - dup2(p[1], 2); - - if (p[0] > 2) - close(p[0]); - - if (p[1] > 2) - close(p[1]); - - close(0); - if (open("/dev/null", O_RDONLY) != 0) { - daemon_log(LOG_ERR, "Unable to open /dev/null as STDIN"); - _exit(EXIT_FAILURE); - } - - umask(0022); /* Set up a sane umask */ - - if (dir && chdir(dir) < 0) { - daemon_log(LOG_WARNING, "Failed to change to directory '%s'", dir); - chdir("/"); - } - - va_start(ap, prog); - for (i = 0; i < MAX_ARGS-1; i++) - if (!(args[i] = va_arg(ap, char*))) - break; - args[i] = NULL; - va_end(ap); - - execv(prog, args); - - daemon_log(LOG_ERR, "execv(%s) failed: %s\n", prog, strerror(errno)); - - _exit(EXIT_FAILURE); - } - - close(p[1]); - - FD_ZERO(&fds); - FD_SET(p[0], &fds); - FD_SET(sigfd = daemon_signal_fd(), &fds); - - n = 0; - - for (;;) { - fd_set qfds = fds; - - if (select(FD_SETSIZE, &qfds, NULL, NULL, NULL) < 0) { - - if (errno == EINTR) - continue; - - daemon_log(LOG_ERR, "select() failed: %s", strerror(errno)); - return -1; - } - - - if (FD_ISSET(p[0], &qfds)) { - char c; - - if (read(p[0], &c, 1) != 1) - break; - - buf[n] = c; - - if (c == '\n' || n >= sizeof(buf) - 2) { - if (c != '\n') n++; - buf[n] = 0; - - if (buf[0]) - daemon_log(LOG_INFO, "client: %s", buf); - - n = 0; - } else - n++; - } - - if (FD_ISSET(sigfd, &qfds)) { - int sig; - - if ((sig = daemon_signal_next()) < 0) { - daemon_log(LOG_ERR, "daemon_signal_next(): %s", strerror(errno)); - break; - } - - if (sig != SIGCHLD) { - daemon_log(LOG_WARNING, "Killing child."); - kill(pid, SIGTERM); - } - } - } - - if (n > 0) { - buf[n] = 0; - daemon_log(LOG_WARNING, "client: %s", buf); - } - - waitpid(pid, &r, 0); - - close(p[0]); - - if (!WIFEXITED(r)) - return -1; - - if (ret) - *ret = WEXITSTATUS(r); - return 0; -} diff --git a/src/dexec.h b/src/dexec.h deleted file mode 100644 index d15d49c..0000000 --- a/src/dexec.h +++ /dev/null @@ -1,55 +0,0 @@ -#ifndef foodexechfoo -#define foodexechfoo - -/* $Id: exec.h 4 2003-08-10 19:56:53Z lennart $ */ - -/* - * 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. - */ - -#ifdef __cplusplus -extern "C" { -#endif - -/** \file - * - * Contains a robust API for running sub processes with STDOUT and - * STDERR redirected to syslog - */ - -/** If this variable is defined to 1 iff daemon_exec() is supported.*/ -#define DAEMON_EXEC_AVAILABLE 1 - -/** Run the specified executable with the specified arguments in the - * specified directory and return the return value of the program in - * the specified pointer. The calling process is blocked until the - * child finishes and all child output (either STDOUT or STDIN) has - * been written to syslog. - * - * @param dir Working directory for the process. - * @param ret A pointer to an integer to write the return value of the program to. - * @param prog The path to the executable - * @param ... The arguments to be passed to the program, followed by a (char *) NULL - * @return Nonzero on failure, zero on success - */ -int daemon_exec(const char *dir, int *ret, const char *prog, ...); - -#ifdef __cplusplus -} -#endif - -#endif diff --git a/src/dfork.c b/src/dfork.c deleted file mode 100644 index 2fa9f30..0000000 --- a/src/dfork.c +++ /dev/null @@ -1,340 +0,0 @@ -/* $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. - */ - -#ifdef HAVE_CONFIG_H -#include -#endif - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#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; -} - -static ssize_t atomic_read(int fd, void *d, size_t l) { - ssize_t t = 0; - - while (l > 0) { - ssize_t r; - - if ((r = read(fd, d, l)) <= 0) { - - if (r < 0) - return t > 0 ? t : -1; - else - return t; - } - - t += r; - d = (char*) d + r; - l -= r; - } - - return t; -} - -static ssize_t atomic_write(int fd, const void *d, size_t l) { - ssize_t t = 0; - - while (l > 0) { - ssize_t r; - - if ((r = write(fd, d, l)) <= 0) { - - if (r < 0) - return t > 0 ? t : -1; - else - return t; - } - - t += r; - d = (char*) d + r; - l -= r; - } - - return t; -} - -static int move_fd_up(int *fd) { - assert(fd); - - while (*fd <= 2) { - if ((*fd = dup(*fd)) < 0) { - daemon_log(LOG_ERR, "dup(): %s", strerror(errno)); - return -1; - } - } - - return 0; -} - -static void sigchld(int s) { -} - -pid_t daemon_fork(void) { - pid_t pid; - int pipe_fds[2] = {-1, -1}; - struct sigaction sa_old, sa_new; - sigset_t ss_old, ss_new; - - memset(&sa_new, 0, sizeof(sa_new)); - sa_new.sa_handler = sigchld; - sa_new.sa_flags = SA_RESTART; - - if (sigaction(SIGCHLD, &sa_new, &sa_old) < 0) { - daemon_log(LOG_ERR, "sigaction() failed: %s", strerror(errno)); - return (pid_t) -1; - } - - sigemptyset(&ss_new); - sigaddset(&ss_new, SIGCHLD); - - if (sigprocmask(SIG_UNBLOCK, &ss_new, &ss_old) < 0) { - daemon_log(LOG_ERR, "sigprocmask() failed: %s", strerror(errno)); - sigaction(SIGCHLD, &sa_old, NULL); - return (pid_t) -1; - } - - if (pipe(pipe_fds) < 0) { - daemon_log(LOG_ERR, "pipe() failed: %s", strerror(errno)); - sigaction(SIGCHLD, &sa_old, NULL); - sigprocmask(SIG_SETMASK, &ss_old, NULL); - return (pid_t) -1; - } - - if ((pid = fork()) < 0) { // First fork - daemon_log(LOG_ERR, "First fork() failed: %s\n", strerror(errno)); - close(pipe_fds[0]); - close(pipe_fds[1]); - sigaction(SIGCHLD, &sa_old, NULL); - sigprocmask(SIG_SETMASK, &ss_old, NULL); - return (pid_t) -1; - - } else if (pid == 0) { - pid_t dpid; - - /* First child */ - - sigaction(SIGCHLD, &sa_old, NULL); - sigprocmask(SIG_SETMASK, &ss_old, NULL); - close(pipe_fds[0]); - - /* Move file descriptors up*/ - if (move_fd_up(&pipe_fds[1]) < 0) - goto fail; - if (_daemon_retval_pipe[0] >= 0 && move_fd_up(&_daemon_retval_pipe[0]) < 0) - goto fail; - if (_daemon_retval_pipe[1] >= 0 && move_fd_up(&_daemon_retval_pipe[1]) < 0) - goto fail; - - 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) { - int tty_fd; - /* Second child */ - - if (daemon_log_use & DAEMON_LOG_AUTO) - daemon_log_use = DAEMON_LOG_SYSLOG; - - signal(SIGTTOU, SIG_IGN); - signal(SIGTTIN, SIG_IGN); - signal(SIGTSTP, SIG_IGN); - - setsid(); - setpgrp(); - - if ((tty_fd = open("/dev/tty", O_RDWR)) >= 0) { - ioctl(tty_fd, TIOCNOTTY, NULL); - close(tty_fd); - } - - dpid = getpid(); - if (atomic_write(pipe_fds[1], &dpid, sizeof(dpid)) != sizeof(dpid)) - goto fail; - close(pipe_fds[1]); - - - return 0; - - } else { - /* Second father */ - close(pipe_fds[1]); - exit(0); - } - - fail: - dpid = (pid_t) -1; - if (atomic_write(pipe_fds[1], &dpid, sizeof(dpid)) != sizeof(dpid)) - daemon_log(LOG_ERR, "Failed to write error PID."); - close(pipe_fds[1]); - exit(0); - - } else { - /* First father */ - pid_t dpid; - - close(pipe_fds[1]); - waitpid(pid, NULL, WUNTRACED); - - sigprocmask(SIG_SETMASK, &ss_old, NULL); - sigaction(SIGCHLD, &sa_old, NULL); - - if (atomic_read(pipe_fds[0], &dpid, sizeof(dpid)) != sizeof(dpid)) { - daemon_log(LOG_ERR, "Failed to read daemon PID."); - dpid = (pid_t) -1; - } - - close(pipe_fds[0]); - return dpid; - } - -} - -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 = atomic_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; - int s; - fd_set fds; - - tv.tv_sec = timeout; - tv.tv_usec = 0; - - 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 = atomic_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 deleted file mode 100644 index faac652..0000000 --- a/src/dfork.h +++ /dev/null @@ -1,104 +0,0 @@ -#ifndef foodaemonforkhfoo -#define foodaemonforkhfoo - -/* $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 - -#ifdef __cplusplus -extern "C" { -#endif - -/** \mainpage - * - * For a brief explanation of libdaemons's purpose, have a look on the README file. 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 On success, the PID of the child process is returned in the - * parent's thread of execution, and a 0 is returned in the child's - * thread of execution. On failure, -1 will be returned in the - * parent's context, no child process will be created, and errno will - * be set appropriately. - */ -pid_t daemon_fork(void); - -/** Allocate and initialize resources required by the - * daemon_retval_xxx() functions. These functions allow the child to - * send a value to the parent after completing its initialisation. - * Call this 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 called in the current process. The resources allocated by - * daemon_retval_init() should be freed in both parent and daemon - * process. This may be achieved by using daemon_retval_wait() - * resp. daemon_retval_send(), or by using daemon_retval_done(). - */ -void daemon_retval_done(void); - -/** Return the value sent by the child via the daemon_retval_send() - * function, but wait only the specified number of seconds before - * timing out and returning a negative number. Should be called just - * once from the parent process only. A subsequent call to - * daemon_retval_done() in the parent is ignored. - * - * @param timeout Thetimeout in seconds - * @return The integer passed daemon_retval_send() in the daemon process, or -1 on failure. - */ -int daemon_retval_wait(int timeout); - -/** Send the specified integer to the parent process. Do not send -1 - * because this signifies a library error. 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); - -#ifdef __cplusplus -} -#endif - -#endif diff --git a/src/dlog.c b/src/dlog.c deleted file mode 100644 index 1fc3e08..0000000 --- a/src/dlog.c +++ /dev/null @@ -1,64 +0,0 @@ -/* $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. - */ - -#ifdef HAVE_CONFIG_H -#include -#endif - -#include -#include -#include - -#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 deleted file mode 100644 index 0f74dde..0000000 --- a/src/dlog.h +++ /dev/null @@ -1,71 +0,0 @@ -#ifndef foodaemonloghfoo -#define foodaemonloghfoo - -/* $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 - -#ifdef __cplusplus -extern "C" { -#endif - -/** \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); - -#ifdef __cplusplus -} -#endif - -#endif diff --git a/src/dnonblock.c b/src/dnonblock.c deleted file mode 100644 index 6c50279..0000000 --- a/src/dnonblock.c +++ /dev/null @@ -1,43 +0,0 @@ -/* $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. - */ - -#ifdef HAVE_CONFIG_H -#include -#endif - -#include - -#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 deleted file mode 100644 index 16efa25..0000000 --- a/src/dnonblock.h +++ /dev/null @@ -1,47 +0,0 @@ -#ifndef foodaemonnonblockhfoo -#define foodaemonnonblockhfoo - -/* $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. - */ - -#ifdef __cplusplus -extern "C" { -#endif - -/** \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); - -#ifdef __cplusplus -} -#endif - -#endif diff --git a/src/dpid.c b/src/dpid.c deleted file mode 100644 index e41e37c..0000000 --- a/src/dpid.c +++ /dev/null @@ -1,161 +0,0 @@ -/* $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. - */ - -#ifdef HAVE_CONFIG_H -#include -#endif - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#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_kill_wait(int s, int m) { - pid_t pid; - time_t t; - - if ((pid = daemon_pid_file_is_running()) < 0) - return -1; - - if (kill(pid, s) < 0) - return -1; - - t = time(NULL) + m; - - for (;;) { - int r; - struct timeval tv = { 0, 100000 }; - - if (time(NULL) > t) - return -1; - - if ((r = kill(pid, 0)) < 0 && errno != ESRCH) - return -1; - - if (r) - return 0; - - if (select(0, NULL, NULL, NULL, &tv) < 0) - return -1; - } -} - -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 deleted file mode 100644 index cea9613..0000000 --- a/src/dpid.h +++ /dev/null @@ -1,97 +0,0 @@ -#ifndef foodaemonpidhfoo -#define foodaemonpidhfoo - -/* $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 - -#ifdef __cplusplus -extern "C" { -#endif - -/** \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); - -/** If this variable is defined to 1 iff daemon_pid_file_kill_wait() is supported.*/ -#define DAEMON_PID_FILE_KILL_WAIT_AVAILABLE 1 - -/** Similar to daemon_pid_file_kill() but waits until the process - * died. This functions is new in libdaemon 0.3. The macro - * DAEMON_PID_FILE_KILL_WAIT_AVAILABLE is defined iff libdaemon - * supports this function. - * - * @param s The signal to send - * @param m Seconds to wait at maximum - * @return zero on success, nonzero on failure (timeout condition is considered a failure) - */ -int daemon_pid_file_kill_wait(int s, int m); - -#ifdef __cplusplus -} -#endif - -#endif diff --git a/src/dsignal.c b/src/dsignal.c deleted file mode 100644 index 2eafc0c..0000000 --- a/src/dsignal.c +++ /dev/null @@ -1,152 +0,0 @@ -/* $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. - */ - -#ifdef HAVE_CONFIG_H -#include -#endif - -#include -#include -#include -#include -#include -#include - -#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){ - sigset_t sigset; - struct sigaction sa; - - if (_init() < 0) - return -1; - - if (sigemptyset(&sigset) < 0) { - daemon_log(LOG_ERR, "sigemptyset(): %s", strerror(errno)); - return -1; - } - - if (sigaddset(&sigset, s) < 0) { - daemon_log(LOG_ERR, "sigaddyset(): %s", strerror(errno)); - return -1; - } - - if (sigprocmask(SIG_UNBLOCK, &sigset, NULL) < 0) { - daemon_log(LOG_ERR, "sigprocmask(): %s", strerror(errno)); - 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: %s", strsignal(s), strerror(errno)); - 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 deleted file mode 100644 index ef3342c..0000000 --- a/src/dsignal.h +++ /dev/null @@ -1,76 +0,0 @@ -#ifndef foodaemonsignalhfoo -#define foodaemonsignalhfoo - -/* $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. - */ - -#ifdef __cplusplus -extern "C" { -#endif - -/** \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 0 - * @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); - -#ifdef __cplusplus -} -#endif - -#endif -- cgit