From e774e8ef3467e81ed502e19b1719b4ab384d0083 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Thu, 31 Jul 2003 14:18:41 +0000 Subject: Moved to trunk git-svn-id: file:///home/lennart/svn/public/libdaemon/trunk@38 153bfa13-eec0-0310-be40-b0cb6a0e1b4b --- examples/Makefile.am | 24 ++++++++ examples/testd.c | 158 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 182 insertions(+) create mode 100644 examples/Makefile.am create mode 100644 examples/testd.c (limited to 'examples') diff --git a/examples/Makefile.am b/examples/Makefile.am new file mode 100644 index 0000000..47c567d --- /dev/null +++ b/examples/Makefile.am @@ -0,0 +1,24 @@ +# $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. + +noinst_PROGRAMS = testd + +AM_CPPFLAGS=-I../src + +testd_SOURCES=testd.c +testd_LDADD=../src/libdaemon.la diff --git a/examples/testd.c b/examples/testd.c new file mode 100644 index 0000000..12b2940 --- /dev/null +++ b/examples/testd.c @@ -0,0 +1,158 @@ +/* $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 +#include +#include + +#include +#include +#include +#include + +int main(int argc, char *argv[]) { + pid_t pid; + + /* Set indetification string for the daemon for both syslog and PID file */ + daemon_pid_file_ident = daemon_log_ident = daemon_ident_from_argv0(argv[0]); + + /* Check if we are called with -k parameter */ + if (argc >= 2 && !strcmp(argv[1], "-k")) { + int ret; + + /* Kill daemon with SIGINT */ + if ((ret = daemon_pid_file_kill(SIGINT)) < 0) + daemon_log(LOG_WARNING, "Failed to kill daemon"); + + return ret < 0 ? 1 : 0; + } + + /* Check that the daemon is not rung twice a the same time */ + if ((pid = daemon_pid_file_is_running()) >= 0) { + daemon_log(LOG_ERR, "Daemon already running on PID file %u", pid); + return 1; + + } + + /* Prepare for return value passing from the initialization procedure of the daemon process */ + daemon_retval_init(); + + /* Do the fork */ + if ((pid = daemon_fork()) < 0) { + + /* Exit on error */ + daemon_retval_done(); + return 1; + + } else if (pid) { /* The parent */ + int ret; + + /* Wait for 20 seconds for the return value passed from the daemon process */ + if ((ret = daemon_retval_wait(20)) < 0) { + daemon_log(LOG_ERR, "Could not recieve return value from daemon process."); + return 255; + } + + return ret; + + } else { /* The daemon */ + int fd, quit = 0; + fd_set fds; + + /* Create the PID file */ + if (daemon_pid_file_create() < 0) { + daemon_log(LOG_ERR, "Could not create PID file (%s).", strerror(errno)); + + /* Send the error condition to the parent process */ + daemon_retval_send(1); + goto finish; + } + + /* Initialize signal handling */ + if (daemon_signal_init(SIGINT, SIGQUIT, SIGHUP, 0) < 0) { + daemon_log(LOG_ERR, "Could not register signal handlers (%s).", strerror(errno)); + daemon_retval_send(2); + goto finish; + } + + /*... do some further init work here */ + + + /* Send OK to parent process */ + daemon_retval_send(0); + + daemon_log(LOG_INFO, "Sucessfully started"); + + + /* Prepare for select() on the signal fd */ + FD_ZERO(&fds); + FD_SET(fd = daemon_signal_fd(), &fds); + + while (!quit) { + fd_set fds2 = fds; + + /* Wait for an incoming signal */ + if (select(FD_SETSIZE, &fds2, 0, 0, 0) < 0) { + + /* If we've been interrupted by an incoming signal, continue */ + if (errno == EINTR) + continue; + + daemon_log(LOG_ERR, "select(): %s", strerror(errno)); + break; + } + + /* Check if a signal has been recieved */ + if (FD_ISSET(fd, &fds)) { + int sig; + + /* Get signal */ + if ((sig = daemon_signal_next()) <= 0) { + daemon_log(LOG_ERR, "daemon_signal_next() failed."); + break; + } + + /* Dispatch signal */ + switch (sig) { + + case SIGINT: + case SIGQUIT: + daemon_log(LOG_WARNING, "Got SIGINT or SIGQUIT"); + quit = 1; + break; + + case SIGHUP: + daemon_log(LOG_INFO, "Got a HUP"); + break; + + } + } + } + + /* Do a cleanup */ +finish: + daemon_log(LOG_INFO, "Exiting..."); + + daemon_signal_done(); + daemon_pid_file_remove(); + + return 0; + } +} -- cgit