libdaemon  0.14
testd.c

This is an example for the usage of libdaemon

/***
This file is part of libdaemon.
Copyright 2003-2008 Lennart Poettering
libdaemon is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as
published by the Free Software Foundation, either version 2.1 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with libdaemon. If not, see
<http://www.gnu.org/licenses/>.
***/
#include <signal.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/unistd.h>
#include <sys/select.h>
#include <libdaemon/dlog.h>
#include <libdaemon/dpid.h>
int main(int argc, char *argv[]) {
pid_t pid;
/* Reset signal handlers */
if (daemon_reset_sigs(-1) < 0) {
daemon_log(LOG_ERR, "Failed to reset all signal handlers: %s", strerror(errno));
return 1;
}
/* Unblock signals */
if (daemon_unblock_sigs(-1) < 0) {
daemon_log(LOG_ERR, "Failed to unblock all signals: %s", strerror(errno));
return 1;
}
/* Set indetification string for the daemon for both syslog and PID file */
/* Check if we are called with -k parameter */
if (argc >= 2 && !strcmp(argv[1], "-k")) {
int ret;
/* Kill daemon with SIGTERM */
/* Check if the new function daemon_pid_file_kill_wait() is available, if it is, use it. */
if ((ret = daemon_pid_file_kill_wait(SIGTERM, 5)) < 0)
daemon_log(LOG_WARNING, "Failed to kill daemon: %s", strerror(errno));
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 */
if (daemon_retval_init() < 0) {
daemon_log(LOG_ERR, "Failed to create pipe.");
return 1;
}
/* Do the fork */
if ((pid = daemon_fork()) < 0) {
/* Exit on error */
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 receive return value from daemon process: %s", strerror(errno));
return 255;
}
daemon_log(ret != 0 ? LOG_ERR : LOG_INFO, "Daemon returned %i as return value.", ret);
return ret;
} else { /* The daemon */
int fd, quit = 0;
fd_set fds;
/* Close FDs */
if (daemon_close_all(-1) < 0) {
daemon_log(LOG_ERR, "Failed to close all file descriptors: %s", strerror(errno));
/* Send the error condition to the parent process */
goto finish;
}
/* Create the PID file */
daemon_log(LOG_ERR, "Could not create PID file (%s).", strerror(errno));
goto finish;
}
/* Initialize signal handling */
if (daemon_signal_init(SIGINT, SIGTERM, SIGQUIT, SIGHUP, 0) < 0) {
daemon_log(LOG_ERR, "Could not register signal handlers (%s).", strerror(errno));
goto finish;
}
/*... do some further init work here */
/* Send OK to parent process */
daemon_log(LOG_INFO, "Sucessfully started");
/* Prepare for select() on the signal fd */
FD_ZERO(&fds);
FD_SET(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, &fds2)) {
int sig;
/* Get signal */
if ((sig = daemon_signal_next()) <= 0) {
daemon_log(LOG_ERR, "daemon_signal_next() failed: %s", strerror(errno));
break;
}
/* Dispatch signal */
switch (sig) {
case SIGINT:
case SIGQUIT:
case SIGTERM:
daemon_log(LOG_WARNING, "Got SIGINT, SIGQUIT or SIGTERM.");
quit = 1;
break;
case SIGHUP:
daemon_log(LOG_INFO, "Got a HUP");
daemon_exec("/", NULL, "/bin/ls", "ls", (char*) NULL);
break;
}
}
}
/* Do a cleanup */
finish:
daemon_log(LOG_INFO, "Exiting...");
return 0;
}
}
dlog.h
Contains a robust API for logging messages.
daemon_retval_send
int daemon_retval_send(int s)
Send the specified integer to the parent process.
dpid.h
Contains an API for manipulating PID files.
daemon_signal_init
int daemon_signal_init(int s,...)
Installs signal handlers for the specified signals.
daemon_unblock_sigs
int daemon_unblock_sigs(int except,...)
Unblock all signals except those passed.
dsignal.h
Contains the API for serializing signals to a pipe for usage with select() or poll().
daemon_signal_fd
int daemon_signal_fd(void)
Return the file descriptor the daemon should select() on for reading.
daemon_pid_file_remove
int daemon_pid_file_remove(void)
Removes the PID file of the current process.
daemon_pid_file_is_running
pid_t daemon_pid_file_is_running(void)
Returns the PID file of a running daemon, if available.
daemon_reset_sigs
int daemon_reset_sigs(int except,...)
Reset all signal handlers except those passed.
daemon_log_ident
const char * daemon_log_ident
Specifies the syslog identification, use daemon_ident_from_argv0() to set this to a sensible value or...
dfork.h
Contains an API for doing a daemonizing fork().
daemon_close_all
int daemon_close_all(int except_fd,...)
Close all file descriptors except those passed.
daemon_pid_file_kill_wait
int daemon_pid_file_kill_wait(int s, int m)
Similar to daemon_pid_file_kill() but waits until the process died.
daemon_pid_file_ident
const char * daemon_pid_file_ident
Identification string for the PID file name, only used when daemon_pid_file_proc is set to daemon_pid...
daemon_retval_done
void daemon_retval_done(void)
Frees the resources allocated by daemon_retval_init().
daemon_exec
int daemon_exec(const char *dir, int *ret, const char *prog,...) DAEMON_GCC_SENTINEL
Run the specified executable with the specified arguments in the specified directory and return the r...
dexec.h
Contains a robust API for running sub processes with STDOUT and STDERR redirected to syslog.
daemon_signal_next
int daemon_signal_next(void)
Return the next signal received.
daemon_retval_init
int daemon_retval_init(void)
Allocate and initialize resources required by the daemon_retval_xxx() functions.
daemon_fork
pid_t daemon_fork(void)
Does a daemonizing fork().
daemon_signal_done
void daemon_signal_done(void)
Free resources of signal handling, should be called before daemon exit.
daemon_retval_wait
int daemon_retval_wait(int timeout)
Return the value sent by the child via the daemon_retval_send() function, but wait only the specified...
daemon_pid_file_create
int daemon_pid_file_create(void)
Creates PID pid file for the current process.
daemon_ident_from_argv0
char * daemon_ident_from_argv0(char *argv0)
Return a sensible syslog identification for daemon_log_ident generated from argv[0].
daemon_log
void daemon_log(int prio, const char *t,...) DAEMON_GCC_PRINTF_ATTR(2
Log a message using printf format strings using the specified syslog priority.