2023-02-11 20:49:42 +00:00
|
|
|
// This file Copyright © 2015-2023 Mnemosyne LLC.
|
2022-02-07 16:25:02 +00:00
|
|
|
// It may be used under GPLv2 (SPDX: GPL-2.0-only), GPLv3 (SPDX: GPL-3.0-only),
|
2022-01-20 18:27:56 +00:00
|
|
|
// or any future license endorsed by Mnemosyne LLC.
|
|
|
|
// License text can be found in the licenses/ folder.
|
2015-04-04 17:43:56 +00:00
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <pthread.h>
|
2022-09-07 20:12:35 +00:00
|
|
|
#ifdef HAVE_SYS_SIGNALFD_H
|
|
|
|
#include <sys/signalfd.h>
|
|
|
|
#endif /* signalfd API */
|
2022-09-08 09:23:54 +00:00
|
|
|
#include <event2/event.h>
|
2015-04-04 17:43:56 +00:00
|
|
|
#include <signal.h>
|
2017-04-21 07:40:57 +00:00
|
|
|
#include <stdlib.h> /* abort(), daemon(), exit() */
|
|
|
|
#include <fcntl.h> /* open() */
|
|
|
|
#include <unistd.h> /* fork(), setsid(), chdir(), dup2(), close(), pipe() */
|
2015-04-04 17:43:56 +00:00
|
|
|
|
2022-04-08 01:50:26 +00:00
|
|
|
#include <string_view>
|
|
|
|
|
2022-04-07 22:26:59 +00:00
|
|
|
#include <fmt/format.h>
|
|
|
|
|
2015-04-04 17:43:56 +00:00
|
|
|
#include "daemon.h"
|
|
|
|
|
2021-12-28 02:32:22 +00:00
|
|
|
static void set_system_error(tr_error** error, int code, std::string_view message)
|
2015-04-04 17:43:56 +00:00
|
|
|
{
|
2022-04-07 22:26:59 +00:00
|
|
|
tr_error_set(error, code, fmt::format(FMT_STRING("{:s}: {:s} ({:d}"), message, tr_strerror(code), code));
|
2015-04-04 17:43:56 +00:00
|
|
|
}
|
|
|
|
|
2022-09-08 09:23:54 +00:00
|
|
|
#ifdef HAVE_SYS_SIGNALFD_H
|
|
|
|
|
2022-09-13 06:37:12 +00:00
|
|
|
static void handle_signals(evutil_socket_t fd, short /*what*/, void* arg)
|
2022-09-07 20:12:35 +00:00
|
|
|
{
|
|
|
|
struct signalfd_siginfo fdsi;
|
2022-09-13 06:37:12 +00:00
|
|
|
auto* const daemon = static_cast<tr_daemon*>(arg);
|
2022-09-07 20:12:35 +00:00
|
|
|
|
|
|
|
if (read(fd, &fdsi, sizeof(fdsi)) != sizeof(fdsi))
|
|
|
|
assert("Error reading signal descriptor" && 0);
|
|
|
|
else
|
2022-09-13 06:37:12 +00:00
|
|
|
{
|
|
|
|
switch (fdsi.ssi_signo)
|
|
|
|
{
|
|
|
|
case SIGHUP:
|
|
|
|
daemon->reconfigure();
|
|
|
|
break;
|
|
|
|
case SIGINT:
|
|
|
|
case SIGTERM:
|
|
|
|
daemon->stop();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
assert("Unexpected signal" && 0);
|
|
|
|
}
|
|
|
|
}
|
2022-09-07 20:12:35 +00:00
|
|
|
}
|
|
|
|
|
2022-09-13 06:37:12 +00:00
|
|
|
bool tr_daemon::setup_signals()
|
2022-09-07 20:12:35 +00:00
|
|
|
{
|
|
|
|
sigset_t mask = {};
|
|
|
|
struct event* sigev = nullptr;
|
2022-09-13 06:37:12 +00:00
|
|
|
struct event_base* base = ev_base_;
|
2022-09-07 20:12:35 +00:00
|
|
|
|
|
|
|
sigemptyset(&mask);
|
|
|
|
sigaddset(&mask, SIGINT);
|
|
|
|
sigaddset(&mask, SIGTERM);
|
|
|
|
sigaddset(&mask, SIGHUP);
|
|
|
|
|
|
|
|
if (sigprocmask(SIG_BLOCK, &mask, nullptr) < 0)
|
|
|
|
return false;
|
|
|
|
|
2022-09-13 06:37:12 +00:00
|
|
|
sigfd_ = signalfd(-1, &mask, 0);
|
|
|
|
if (sigfd_ < 0)
|
2022-09-07 20:12:35 +00:00
|
|
|
return false;
|
|
|
|
|
2022-09-13 06:37:12 +00:00
|
|
|
sigev = event_new(base, sigfd_, EV_READ | EV_PERSIST, handle_signals, this);
|
2022-09-07 20:12:35 +00:00
|
|
|
if (sigev == nullptr)
|
|
|
|
{
|
2022-09-13 06:37:12 +00:00
|
|
|
close(sigfd_);
|
2022-09-07 20:12:35 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (event_add(sigev, nullptr) < 0)
|
|
|
|
{
|
|
|
|
event_del(sigev);
|
2022-09-13 06:37:12 +00:00
|
|
|
close(sigfd_);
|
2022-09-07 20:12:35 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-09-08 09:23:54 +00:00
|
|
|
#else /* no signalfd API, use evsignal */
|
2022-09-07 20:12:35 +00:00
|
|
|
|
2022-09-13 06:37:12 +00:00
|
|
|
static void reconfigureMarshall(evutil_socket_t /*fd*/, short /*events*/, void* arg)
|
2015-04-04 17:43:56 +00:00
|
|
|
{
|
2022-09-13 06:37:12 +00:00
|
|
|
static_cast<tr_daemon*>(arg)->reconfigure();
|
2015-04-04 17:43:56 +00:00
|
|
|
}
|
|
|
|
|
2022-09-13 06:37:12 +00:00
|
|
|
static void stopMarshall(evutil_socket_t /*fd*/, short /*events*/, void* arg)
|
2015-04-04 17:43:56 +00:00
|
|
|
{
|
2022-09-13 06:37:12 +00:00
|
|
|
static_cast<tr_daemon*>(arg)->stop();
|
2015-04-04 17:43:56 +00:00
|
|
|
}
|
|
|
|
|
2022-09-13 06:37:12 +00:00
|
|
|
static bool setup_signal(struct event_base* base, int sig, void (*callback)(evutil_socket_t, short, void*), void* arg)
|
2015-04-04 17:43:56 +00:00
|
|
|
{
|
2022-09-13 06:37:12 +00:00
|
|
|
struct event* sigev = evsignal_new(base, sig, callback, arg);
|
2015-04-04 17:43:56 +00:00
|
|
|
|
2022-09-08 09:23:54 +00:00
|
|
|
if (sigev == nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
return false;
|
2015-04-04 17:43:56 +00:00
|
|
|
|
2022-09-08 09:23:54 +00:00
|
|
|
if (evsignal_add(sigev, nullptr) < 0)
|
2015-04-04 17:43:56 +00:00
|
|
|
{
|
2022-09-08 09:23:54 +00:00
|
|
|
event_free(sigev);
|
2017-04-19 12:04:45 +00:00
|
|
|
return false;
|
2015-04-04 17:43:56 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return true;
|
2015-04-04 17:43:56 +00:00
|
|
|
}
|
|
|
|
|
2022-09-13 06:37:12 +00:00
|
|
|
bool tr_daemon::setup_signals()
|
2015-04-04 17:43:56 +00:00
|
|
|
{
|
2022-09-13 06:37:12 +00:00
|
|
|
return setup_signal(ev_base_, SIGHUP, reconfigureMarshall, this) && setup_signal(ev_base_, SIGINT, stopMarshall, this) &&
|
|
|
|
setup_signal(ev_base_, SIGTERM, stopMarshall, this);
|
2015-04-04 17:43:56 +00:00
|
|
|
}
|
|
|
|
|
2022-09-08 09:23:54 +00:00
|
|
|
#endif /* HAVE_SYS_SIGNALFD_H */
|
2015-04-04 17:43:56 +00:00
|
|
|
|
2022-09-13 06:37:12 +00:00
|
|
|
bool tr_daemon::spawn(bool foreground, int* exit_code, tr_error** error)
|
2015-04-04 17:43:56 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
*exit_code = 1;
|
2015-04-04 17:43:56 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (!foreground)
|
2015-04-04 17:43:56 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
#if defined(HAVE_DAEMON) && !defined(__APPLE__) && !defined(__UCLIBC__)
|
2015-04-04 17:43:56 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (daemon(true, false) == -1)
|
2015-04-04 17:43:56 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
set_system_error(error, errno, "daemon() failed");
|
|
|
|
return false;
|
2015-04-04 17:43:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
2017-04-21 07:40:57 +00:00
|
|
|
/* this is loosely based off of glibc's daemon() implementation
|
2022-04-21 14:28:38 +00:00
|
|
|
* https://sourceware.org/git/?p=glibc.git;a=blob_plain;f=misc/daemon.c */
|
2015-04-04 17:43:56 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
switch (fork())
|
2015-04-04 17:43:56 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
case -1:
|
|
|
|
set_system_error(error, errno, "fork() failed");
|
2015-04-04 17:43:56 +00:00
|
|
|
return false;
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
case 0:
|
2015-04-04 17:43:56 +00:00
|
|
|
break;
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
default:
|
2015-04-04 17:43:56 +00:00
|
|
|
*exit_code = 0;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (setsid() == -1)
|
2015-04-04 17:43:56 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
set_system_error(error, errno, "setsid() failed");
|
|
|
|
return false;
|
2015-04-04 17:43:56 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/*
|
|
|
|
if (chdir("/") == -1)
|
2015-04-04 17:43:56 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
set_system_error(error, errno, "chdir() failed");
|
|
|
|
return false;
|
2015-04-04 17:43:56 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
*/
|
2015-04-04 17:43:56 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
int const fd = open("/dev/null", O_RDWR, 0);
|
2017-04-19 12:04:45 +00:00
|
|
|
dup2(fd, STDIN_FILENO);
|
|
|
|
dup2(fd, STDOUT_FILENO);
|
|
|
|
dup2(fd, STDERR_FILENO);
|
|
|
|
close(fd);
|
|
|
|
}
|
2015-04-04 17:43:56 +00:00
|
|
|
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2022-09-13 06:37:12 +00:00
|
|
|
*exit_code = start(foreground);
|
2015-04-04 17:43:56 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return true;
|
2015-04-04 17:43:56 +00:00
|
|
|
}
|