Skip to content

Commit

Permalink
WIP: Added option to daemonize app and set PID file.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jiri Hnidek committed Mar 2, 2015
1 parent 3910bb6 commit 815aeed
Showing 1 changed file with 132 additions and 15 deletions.
147 changes: 132 additions & 15 deletions src/daemon.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,16 @@
#include <signal.h>
#include <getopt.h>
#include <string.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>

static int running = 0;
static int delay = 1;
static int counter = 0;
static char *conf_file_name = NULL;
static char *pid_file_name = NULL;
static int pid_fd = -1;
static char *app_name = NULL;
static FILE *log_stream;

Expand Down Expand Up @@ -99,16 +104,109 @@ int test_conf_file(char *_conf_file_name)
* \brief Callback function for handling signals.
* \param sig identifier of signal
*/
void vs_handle_signal(int sig)
void handle_signal(int sig)
{
if(sig == SIGINT) {
fprintf(log_stream, "Debug: stopping daemon ...\n");
/* Unlock and close lockfile */
if(pid_fd != -1) {
lockf(pid_fd, F_ULOCK, 0);
close(pid_fd);
}
/* Try to delete lockfile */
if(pid_file_name != NULL) {
unlink(pid_file_name);
}
running = 0;
/* Reset signal handling to default behavior */
signal(SIGINT, SIG_DFL);
} else if(sig == SIGHUP) {
fprintf(log_stream, "Debug: reloading daemon config file ...\n");
read_conf_file();
} else if(sig == SIGCHLD) {
fprintf(log_stream, "Debug: received SIGCHLD signal\n");
}
}

/**
* \brief This function will daemonize this app
*/
static void daemonize()
{
pid_t pid = 0;
int fd;

/* Fork off the parent process */
pid = fork();

/* An error occurred */
if(pid < 0) {
exit(EXIT_FAILURE);
}

/* Success: Let the parent terminate */
if(pid > 0) {
exit(EXIT_SUCCESS);
}

/* On success: The child process becomes session leader */
if(setsid() < 0) {
exit(EXIT_FAILURE);
}

/* Ignore signal sent from child to parent process */
signal(SIGCHLD, SIG_IGN);

/* Fork off for the second time*/
pid = fork();

/* An error occurred */
if(pid < 0) {
exit(EXIT_FAILURE);
}

/* Success: Let the parent terminate */
if(pid > 0) {
exit(EXIT_SUCCESS);
}

/* Set new file permissions */
umask(0);

/* Change the working directory to the root directory */
/* or another appropriated directory */
chdir("/");

/* Close all open file descriptors */
for(fd = sysconf(_SC_OPEN_MAX); fd > 0; fd--)
{
close(fd);
}

/* Reopen stdin (fd = 0), stdout (fd = 1), stderr (fd = 2) */
stdin = fopen("/dev/null", "r");
stdout = fopen("/dev/null", "w+");
stderr = fopen("/dev/null", "w+");

/* Try to write PID of daemon to lockfile */
if(pid_file_name != NULL)
{
char str[256];
pid_fd = open(pid_file_name, O_RDWR|O_CREAT, 0640);
if(pid_fd < 0)
{
/* Can't open lockfile */
exit(EXIT_FAILURE);
}
if(lockf(pid_fd, F_TLOCK, 0) < 0)
{
/* Can't lock file */
exit(EXIT_FAILURE);
}
/* Get current PID */
sprintf(str, "%d\n", getpid());
/* Write PID to lockfile */
write(pid_fd, str, strlen(str));
}
}

Expand All @@ -119,39 +217,50 @@ void print_help(void)
{
printf("\n Usage: %s [OPTIONS]\n\n", app_name);
printf(" Options:\n");
printf(" -h --help Print this help\n");
printf(" -c --confile filename Read configuration from the file\n");
printf(" -t --testconf filename Test configuration file\n");
printf(" -l --logfile filename Write logs to the file\n");
printf(" -h --help Print this help\n");
printf(" -c --conf_file filename Read configuration from the file\n");
printf(" -t --test_conf filename Test configuration file\n");
printf(" -l --log_file filename Write logs to the file\n");
printf(" -d --daemon Daemonize this application\n");
printf(" -p --pid_file filename PID file used by daemonized app\n");
printf("\n");
}

/* Main function */
int main(int argc, char *argv[])
{
static struct option long_options[] = {
{"confile", required_argument, 0, 'c'},
{"testconf", required_argument, 0, 't'},
{"logfile", required_argument, 0, 'l'},
{"conf_file", required_argument, 0, 'c'},
{"test_conf", required_argument, 0, 't'},
{"log_file", required_argument, 0, 'l'},
{"help", no_argument, 0, 'h'},
{"daemon", no_argument, 0, 'd'},
{"pid_file", required_argument, 0, 'p'},
{NULL, 0, 0, 0}
};
int value, option_index = 0;
char *log_file_name = NULL;
int start_daemonized = 0;

app_name = argv[0];

/* Try to process all command line arguments */
while( (value = getopt_long(argc, argv, "c:l:t:h", long_options, &option_index)) != -1) {
while( (value = getopt_long(argc, argv, "c:l:t:p:dh", long_options, &option_index)) != -1) {
switch(value) {
case 'c':
conf_file_name = strdup(optarg);
break;
case 't':
return test_conf_file(optarg);
case 'l':
log_file_name = strdup(optarg);
break;
case 'p':
pid_file_name = strdup(optarg);
break;
case 't':
return test_conf_file(optarg);
case 'd':
start_daemonized = 1;
break;
case 'h':
print_help();
return EXIT_SUCCESS;
Expand All @@ -163,14 +272,21 @@ int main(int argc, char *argv[])
}
}

/* Daemon will handle two signals */
signal(SIGINT, vs_handle_signal);
signal(SIGHUP, vs_handle_signal);
/* When daemonizing is requested at command line. */
if(start_daemonized == 1) {
/* It is also possible to use glibc function deamon()
* at this point, but it is useful to customize your daemon. */
daemonize();
}

/* Open system log and write message to it */
openlog(argv[0], LOG_PID|LOG_CONS, LOG_USER);
openlog(argv[0], LOG_PID|LOG_CONS, LOG_DAEMON);
syslog(LOG_INFO, "Started %s", app_name);

/* Daemon will handle two signals */
signal(SIGINT, handle_signal);
signal(SIGHUP, handle_signal);

/* Try to open log file to this daemon */
if(log_file_name != NULL) {
log_stream = fopen(log_file_name, "a+");
Expand Down Expand Up @@ -214,6 +330,7 @@ int main(int argc, char *argv[])
/* Free allocated memory */
if(conf_file_name != NULL) free(conf_file_name);
if(log_file_name != NULL) free(log_file_name);
if(pid_file_name != NULL) free(pid_file_name);

return EXIT_SUCCESS;
}

0 comments on commit 815aeed

Please sign in to comment.