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.
214 lines
5.8 KiB
214 lines
5.8 KiB
5 years ago
|
/*
|
||
|
* Copyright (c) 2013-2020, The PurpleI2P Project
|
||
|
*
|
||
|
* This file is part of Purple i2pd project and licensed under BSD3
|
||
|
*
|
||
|
* See full license text in LICENSE file at top of project tree
|
||
|
*/
|
||
|
|
||
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"
|
||
7 years ago
|
#include "Tunnel.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();
|
||
11 years ago
|
if (pid > 0) // parent
|
||
|
::exit (EXIT_SUCCESS);
|
||
11 years ago
|
|
||
11 years ago
|
if (pid < 0) // error
|
||
9 years ago
|
{
|
||
3 years ago
|
LogPrint(eLogError, "Daemon: Could not fork: ", strerror(errno));
|
||
3 years ago
|
std::cerr << "i2pd: Could not fork: " << strerror(errno) << std::endl;
|
||
11 years ago
|
return false;
|
||
9 years ago
|
}
|
||
11 years ago
|
|
||
|
// child
|
||
9 years ago
|
umask(S_IWGRP | S_IRWXO); // 0027
|
||
11 years ago
|
int sid = setsid();
|
||
|
if (sid < 0)
|
||
|
{
|
||
3 years ago
|
LogPrint(eLogError, "Daemon: Could not create process group.");
|
||
3 years ago
|
std::cerr << "i2pd: Could not create process group." << std::endl;
|
||
11 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)
|
||
|
{
|
||
3 years ago
|
LogPrint(eLogError, "Daemon: Could not chdir: ", strerror(errno));
|
||
3 years ago
|
std::cerr << "i2pd: Could not chdir: " << strerror(errno) << std::endl;
|
||
9 years ago
|
return false;
|
||
|
}
|
||
11 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) {
|
||
3 years ago
|
LogPrint(eLogInfo, "Daemon: Using system limit in ", limit.rlim_cur, " max open files");
|
||
8 years ago
|
} else if (nfiles <= limit.rlim_max) {
|
||
|
limit.rlim_cur = nfiles;
|
||
|
if (setrlimit(RLIMIT_NOFILE, &limit) == 0) {
|
||
3 years ago
|
LogPrint(eLogInfo, "Daemon: Set max number of open files to ",
|
||
8 years ago
|
nfiles, " (system limit is ", limit.rlim_max, ")");
|
||
|
} else {
|
||
3 years ago
|
LogPrint(eLogError, "Daemon: Can't set max number of open files: ", strerror(errno));
|
||
8 years ago
|
}
|
||
|
} 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) {
|
||
3 years ago
|
LogPrint(eLogError, "Daemon: Can't set max size of coredump: ", strerror(errno));
|
||
8 years ago
|
} else if (cfsize == 0) {
|
||
|
LogPrint(eLogInfo, "Daemon: coredumps disabled");
|
||
|
} else {
|
||
3 years ago
|
LogPrint(eLogInfo, "Daemon: Set max size of core files to ", cfsize / 1024, "Kb");
|
||
8 years ago
|
}
|
||
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)
|
||
|
{
|
||
3 years ago
|
LogPrint(eLogError, "Daemon: Could not create pid file ", pidfile, ": ", strerror(errno));
|
||
3 years ago
|
std::cerr << "i2pd: Could not create pid file " << pidfile << ": " << strerror(errno) << std::endl;
|
||
9 years ago
|
return false;
|
||
|
}
|
||
7 years ago
|
|
||
|
#ifndef ANDROID
|
||
9 years ago
|
if (lockf(pidFH, F_TLOCK, 0) != 0)
|
||
|
{
|
||
3 years ago
|
LogPrint(eLogError, "Daemon: Could not lock pid file ", pidfile, ": ", strerror(errno));
|
||
3 years ago
|
std::cerr << "i2pd: Could not lock pid file " << pidfile << ": " << strerror(errno) << std::endl;
|
||
9 years ago
|
return false;
|
||
|
}
|
||
7 years ago
|
#endif
|
||
9 years ago
|
char pid[10];
|
||
|
sprintf(pid, "%d\n", getpid());
|
||
|
ftruncate(pidFH, 0);
|
||
9 years ago
|
if (write(pidFH, pid, strlen(pid)) < 0)
|
||
|
{
|
||
3 years ago
|
LogPrint(eLogError, "Daemon: Could not write pidfile ", pidfile, ": ", strerror(errno));
|
||
|
std::cerr << "i2pd: Could not write pidfile " << pidfile << ": " << strerror(errno) << std::endl;
|
||
9 years ago
|
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);
|
||
5 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);
|
||
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
|
||
7 years ago
|
if (gracefulShutdownInterval <= 0 || i2p::tunnel::tunnels.CountTransitTunnels() <= 0)
|
||
8 years ago
|
{
|
||
9 years ago
|
LogPrint(eLogInfo, "Graceful shutdown");
|
||
|
return;
|
||
|
}
|
||
8 years ago
|
}
|
||
9 years ago
|
}
|
||
|
}
|
||
11 years ago
|
}
|
||
|
}
|
||
11 years ago
|
#endif
|