1
0
mirror of https://github.com/binary-kitchen/doorlockd synced 2024-05-31 22:22:34 +02:00
doorlockd-mirror/doorlockd/daemon/daemon.cpp
Ralf Ramsauer 49a5b88f6c Big rewrite of several things
- Data type Token changed from uint64_t to std::string
- Added new class "Request" that describes a JSON TCP request
- Classes may now throw Responses for proper error handling
- Removed JSON parsing from Logic
- proper Error handling everywhere
- Many small fixes
- removed unnecessary includes
- removed using namespace std everywhere
2015-10-01 22:09:55 +02:00

51 lines
1.1 KiB
C++

#include <stdexcept>
#include <cstring>
#include <fstream>
#include <sys/resource.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#include "daemon.h"
void daemonize(const std::string &dir,
const std::string &stdinfile,
const std::string &stdoutfile,
const std::string &stderrfile)
{
umask(0);
rlimit rl;
if (getrlimit(RLIMIT_NOFILE, &rl) < 0)
{
throw std::runtime_error(strerror(errno));
}
if (!dir.empty() && chdir(dir.c_str()) < 0)
{
throw std::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 std::runtime_error("new standard file descriptors were not opened as expected");
}
}