Added proper daemonization
This commit is contained in:
parent
c392f79237
commit
164df6604c
|
@ -0,0 +1,72 @@
|
|||
#include <stdexcept>
|
||||
#include <cstring>
|
||||
#include <fstream>
|
||||
|
||||
#include <sys/resource.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/stat.h>
|
||||
#include <fcntl.h>
|
||||
|
||||
#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");
|
||||
}
|
||||
}
|
|
@ -0,0 +1,13 @@
|
|||
#ifndef DAEMON_H
|
||||
#define DAEMON_H
|
||||
|
||||
#include <string>
|
||||
|
||||
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
|
|
@ -0,0 +1 @@
|
|||
LDAPTLS_REQCERT=never
|
|
@ -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
|
|
@ -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
|
||||
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue