mirror of https://github.com/PurpleI2P/i2pd.git
I2P: End-to-End encrypted and anonymous Internet
https://i2pd.website/
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
198 lines
5.0 KiB
198 lines
5.0 KiB
11 years ago
|
#include "Daemon.h"
|
||
|
|
||
|
#ifndef _WIN32
|
||
|
|
||
|
#include <signal.h>
|
||
|
#include <stdlib.h>
|
||
9 years ago
|
#include <thread>
|
||
11 years ago
|
#include <unistd.h>
|
||
11 years ago
|
#include <fcntl.h>
|
||
|
#include <sys/stat.h>
|
||
8 years ago
|
#include <sys/resource.h>
|
||
11 years ago
|
|
||
9 years ago
|
#include "Config.h"
|
||
9 years ago
|
#include "FS.h"
|
||
11 years ago
|
#include "Log.h"
|
||
9 years ago
|
#include "RouterContext.h"
|
||
8 years ago
|
#include "ClientContext.h"
|
||
11 years ago
|
|
||
11 years ago
|
void handle_signal(int sig)
|
||
|
{
|
||
|
switch (sig)
|
||
|
{
|
||
9 years ago
|
case SIGHUP:
|
||
8 years ago
|
LogPrint(eLogInfo, "Daemon: Got SIGHUP, reopening tunnel configuration...");
|
||
8 years ago
|
i2p::client::context.ReloadConfig();
|
||
9 years ago
|
break;
|
||
8 years ago
|
case SIGUSR1:
|
||
|
LogPrint(eLogInfo, "Daemon: Got SIGUSR1, reopening logs...");
|
||
|
i2p::log::Logger().Reopen ();
|
||
|
break;
|
||
9 years ago
|
case SIGINT:
|
||
8 years ago
|
if (i2p::context.AcceptsTunnels () && !Daemon.gracefulShutdownInterval)
|
||
8 years ago
|
{
|
||
9 years ago
|
i2p::context.SetAcceptsTunnels (false);
|
||
8 years ago
|
Daemon.gracefulShutdownInterval = 10*60; // 10 minutes
|
||
|
LogPrint(eLogInfo, "Graceful shutdown after ", Daemon.gracefulShutdownInterval, " seconds");
|
||
8 years ago
|
}
|
||
9 years ago
|
else
|
||
8 years ago
|
Daemon.running = 0;
|
||
|
break;
|
||
9 years ago
|
case SIGABRT:
|
||
|
case SIGTERM:
|
||
|
Daemon.running = 0; // Exit loop
|
||
11 years ago
|
break;
|
||
8 years ago
|
case SIGPIPE:
|
||
|
LogPrint(eLogInfo, "SIGPIPE received");
|
||
|
break;
|
||
11 years ago
|
}
|
||
|
}
|
||
|
|
||
|
namespace i2p
|
||
|
{
|
||
|
namespace util
|
||
|
{
|
||
|
bool DaemonLinux::start()
|
||
|
{
|
||
9 years ago
|
if (isDaemon)
|
||
11 years ago
|
{
|
||
|
pid_t pid;
|
||
|
pid = fork();
|
||
10 years ago
|
if (pid > 0) // parent
|
||
|
::exit (EXIT_SUCCESS);
|
||
11 years ago
|
|
||
10 years ago
|
if (pid < 0) // error
|
||
9 years ago
|
{
|
||
|
LogPrint(eLogError, "Daemon: could not fork: ", strerror(errno));
|
||
10 years ago
|
return false;
|
||
9 years ago
|
}
|
||
10 years ago
|
|
||
|
// child
|
||
9 years ago
|
umask(S_IWGRP | S_IRWXO); // 0027
|
||
11 years ago
|
int sid = setsid();
|
||
|
if (sid < 0)
|
||
|
{
|
||
9 years ago
|
LogPrint(eLogError, "Daemon: could not create process group.");
|
||
10 years ago
|
return false;
|
||
11 years ago
|
}
|
||
9 years ago
|
std::string d = i2p::fs::GetDataDir();
|
||
9 years ago
|
if (chdir(d.c_str()) != 0)
|
||
|
{
|
||
|
LogPrint(eLogError, "Daemon: could not chdir: ", strerror(errno));
|
||
|
return false;
|
||
|
}
|
||
10 years ago
|
|
||
9 years ago
|
// point std{in,out,err} descriptors to /dev/null
|
||
8 years ago
|
freopen("/dev/null", "r", stdin);
|
||
8 years ago
|
freopen("/dev/null", "w", stdout);
|
||
|
freopen("/dev/null", "w", stderr);
|
||
11 years ago
|
}
|
||
|
|
||
8 years ago
|
// set proc limits
|
||
|
struct rlimit limit;
|
||
|
uint16_t nfiles; i2p::config::GetOption("limits.openfiles", nfiles);
|
||
|
getrlimit(RLIMIT_NOFILE, &limit);
|
||
|
if (nfiles == 0) {
|
||
|
LogPrint(eLogInfo, "Daemon: using system limit in ", limit.rlim_cur, " max open files");
|
||
|
} else if (nfiles <= limit.rlim_max) {
|
||
|
limit.rlim_cur = nfiles;
|
||
|
if (setrlimit(RLIMIT_NOFILE, &limit) == 0) {
|
||
|
LogPrint(eLogInfo, "Daemon: set max number of open files to ",
|
||
|
nfiles, " (system limit is ", limit.rlim_max, ")");
|
||
|
} else {
|
||
|
LogPrint(eLogError, "Daemon: can't set max number of open files: ", strerror(errno));
|
||
|
}
|
||
|
} else {
|
||
|
LogPrint(eLogError, "Daemon: limits.openfiles exceeds system limit: ", limit.rlim_max);
|
||
|
}
|
||
|
uint32_t cfsize; i2p::config::GetOption("limits.coresize", cfsize);
|
||
8 years ago
|
if (cfsize) // core file size set
|
||
8 years ago
|
{
|
||
|
cfsize *= 1024;
|
||
8 years ago
|
getrlimit(RLIMIT_CORE, &limit);
|
||
|
if (cfsize <= limit.rlim_max) {
|
||
|
limit.rlim_cur = cfsize;
|
||
|
if (setrlimit(RLIMIT_CORE, &limit) != 0) {
|
||
|
LogPrint(eLogError, "Daemon: can't set max size of coredump: ", strerror(errno));
|
||
|
} else if (cfsize == 0) {
|
||
|
LogPrint(eLogInfo, "Daemon: coredumps disabled");
|
||
|
} else {
|
||
|
LogPrint(eLogInfo, "Daemon: set max size of core files to ", cfsize / 1024, "Kb");
|
||
|
}
|
||
8 years ago
|
} else {
|
||
8 years ago
|
LogPrint(eLogError, "Daemon: limits.coresize exceeds system limit: ", limit.rlim_max);
|
||
8 years ago
|
}
|
||
8 years ago
|
}
|
||
8 years ago
|
|
||
11 years ago
|
// Pidfile
|
||
9 years ago
|
// this code is c-styled and a bit ugly, but we need fd for locking pidfile
|
||
9 years ago
|
std::string pidfile; i2p::config::GetOption("pidfile", pidfile);
|
||
9 years ago
|
if (pidfile == "") {
|
||
9 years ago
|
pidfile = i2p::fs::DataDirPath("i2pd.pid");
|
||
9 years ago
|
}
|
||
9 years ago
|
if (pidfile != "") {
|
||
|
pidFH = open(pidfile.c_str(), O_RDWR | O_CREAT, 0600);
|
||
|
if (pidFH < 0)
|
||
|
{
|
||
|
LogPrint(eLogError, "Daemon: could not create pid file ", pidfile, ": ", strerror(errno));
|
||
|
return false;
|
||
|
}
|
||
|
if (lockf(pidFH, F_TLOCK, 0) != 0)
|
||
|
{
|
||
|
LogPrint(eLogError, "Daemon: could not lock pid file ", pidfile, ": ", strerror(errno));
|
||
|
return false;
|
||
|
}
|
||
|
char pid[10];
|
||
|
sprintf(pid, "%d\n", getpid());
|
||
|
ftruncate(pidFH, 0);
|
||
9 years ago
|
if (write(pidFH, pid, strlen(pid)) < 0)
|
||
|
{
|
||
|
LogPrint(eLogError, "Daemon: could not write pidfile: ", strerror(errno));
|
||
|
return false;
|
||
|
}
|
||
11 years ago
|
}
|
||
8 years ago
|
gracefulShutdownInterval = 0; // not specified
|
||
11 years ago
|
|
||
|
// Signal handler
|
||
|
struct sigaction sa;
|
||
|
sa.sa_handler = handle_signal;
|
||
|
sigemptyset(&sa.sa_mask);
|
||
|
sa.sa_flags = SA_RESTART;
|
||
|
sigaction(SIGHUP, &sa, 0);
|
||
8 years ago
|
sigaction(SIGUSR1, &sa, 0);
|
||
11 years ago
|
sigaction(SIGABRT, &sa, 0);
|
||
|
sigaction(SIGTERM, &sa, 0);
|
||
|
sigaction(SIGINT, &sa, 0);
|
||
8 years ago
|
sigaction(SIGPIPE, &sa, 0);
|
||
11 years ago
|
|
||
11 years ago
|
return Daemon_Singleton::start();
|
||
11 years ago
|
}
|
||
|
|
||
|
bool DaemonLinux::stop()
|
||
|
{
|
||
9 years ago
|
i2p::fs::Remove(pidfile);
|
||
11 years ago
|
|
||
8 years ago
|
return Daemon_Singleton::stop();
|
||
11 years ago
|
}
|
||
9 years ago
|
|
||
|
void DaemonLinux::run ()
|
||
|
{
|
||
|
while (running)
|
||
|
{
|
||
|
std::this_thread::sleep_for (std::chrono::seconds(1));
|
||
8 years ago
|
if (gracefulShutdownInterval)
|
||
9 years ago
|
{
|
||
8 years ago
|
gracefulShutdownInterval--; // - 1 second
|
||
8 years ago
|
if (gracefulShutdownInterval <= 0)
|
||
|
{
|
||
9 years ago
|
LogPrint(eLogInfo, "Graceful shutdown");
|
||
|
return;
|
||
|
}
|
||
8 years ago
|
}
|
||
9 years ago
|
}
|
||
|
}
|
||
11 years ago
|
}
|
||
|
}
|
||
|
|
||
11 years ago
|
#endif
|