From 164df6604c4d2cb3e3441c697e38e94b7ca57db4 Mon Sep 17 00:00:00 2001 From: Ralf Ramsauer Date: Thu, 14 May 2015 15:30:55 +0200 Subject: [PATCH] Added proper daemonization --- daemon.cpp | 72 +++++++++++++++++++++++++++++++++++++++ daemon.h | 13 +++++++ scripts/doorlockd | 1 + scripts/doorlockd-start | 12 ------- scripts/doorlockd-stop | 12 ------- scripts/doorlockd.service | 4 +-- 6 files changed, 88 insertions(+), 26 deletions(-) create mode 100644 daemon.cpp create mode 100644 daemon.h create mode 100644 scripts/doorlockd delete mode 100755 scripts/doorlockd-start delete mode 100755 scripts/doorlockd-stop diff --git a/daemon.cpp b/daemon.cpp new file mode 100644 index 0000000..9117b93 --- /dev/null +++ b/daemon.cpp @@ -0,0 +1,72 @@ +#include +#include +#include + +#include +#include +#include +#include + +#include "daemon.h" + +using namespace std; + +void daemonize(const bool daemonize, + const string &dir, + const string &stdinfile, + const string &stdoutfile, + const string &stderrfile, + const string &pidFile) +{ + umask(0); + + rlimit rl; + if (getrlimit(RLIMIT_NOFILE, &rl) < 0) + { + throw runtime_error(strerror(errno)); + } + + if (daemonize == true) + { + pid_t pid; + if ((pid = fork()) < 0) + { + throw runtime_error(strerror(errno)); + } else if (pid != 0) { + exit(0); + } + + pid = setsid(); + + ofstream pidStream; + pidStream.exceptions(ofstream::failbit | ofstream::badbit); + pidStream.open(pidFile, ofstream::trunc); + pidStream << pid << endl; + } + + if (!dir.empty() && chdir(dir.c_str()) < 0) + { + throw runtime_error(strerror(errno)); + } + + if (rl.rlim_max == RLIM_INFINITY) + { + rl.rlim_max = 1024; + } + + for (unsigned int i = 0; i < rl.rlim_max; i++) + { + close(i); + } + + int fd0 = open(stdinfile.c_str(), O_RDONLY); + int fd1 = open(stdoutfile.c_str(), + O_WRONLY|O_CREAT|O_APPEND, S_IRUSR|S_IWUSR); + int fd2 = open(stderrfile.c_str(), + O_WRONLY|O_CREAT|O_APPEND, S_IRUSR|S_IWUSR); + + if (fd0 != STDIN_FILENO || fd1 != STDOUT_FILENO || fd2 != STDERR_FILENO) + { + throw runtime_error("new standard file descriptors were not opened as expected"); + } +} diff --git a/daemon.h b/daemon.h new file mode 100644 index 0000000..ba7e3b5 --- /dev/null +++ b/daemon.h @@ -0,0 +1,13 @@ +#ifndef DAEMON_H +#define DAEMON_H + +#include + +void daemonize(const bool daemonize, + const std::string &dir, + const std::string &stdinfile, + const std::string &stdoutfile, + const std::string &stderrfile, + const std::string &pidFile); + +#endif diff --git a/scripts/doorlockd b/scripts/doorlockd new file mode 100644 index 0000000..cda6141 --- /dev/null +++ b/scripts/doorlockd @@ -0,0 +1 @@ +LDAPTLS_REQCERT=never diff --git a/scripts/doorlockd-start b/scripts/doorlockd-start deleted file mode 100755 index 5af044b..0000000 --- a/scripts/doorlockd-start +++ /dev/null @@ -1,12 +0,0 @@ -#!/bin/bash -PIDFILE=/tmp/doorlockd-pid - -gpio load spi -gpio load i2c - -/home/ralf/doorlockd-build/doorlockd & -PID=$! - -echo $PID > $PIDFILE - -wait $PID diff --git a/scripts/doorlockd-stop b/scripts/doorlockd-stop deleted file mode 100755 index 31a6608..0000000 --- a/scripts/doorlockd-stop +++ /dev/null @@ -1,12 +0,0 @@ -#!/bin/bash - -PIDFILE=/tmp/doorlockd-pid - -if [ -f $PIDFILE ] -then - kill `cat $PIDFILE` - rm $PIDFILE -else - echo "PID file not existent" -fi - diff --git a/scripts/doorlockd.service b/scripts/doorlockd.service index f83ed44..d505e78 100644 --- a/scripts/doorlockd.service +++ b/scripts/doorlockd.service @@ -5,8 +5,8 @@ After=network.target [Service] User=root Group=root -ExecStart=/usr/local/share/doorlockd/doorlockd-start -ExecStop=/usr/local/share/doorlockd/doorlockd-stop +EnvironmentFile=-/etc/sysconfig/doorlockd +ExecStart=/usr/local/sbin/doorlockd -f [Install] WantedBy=multi-user.target