2022-01-20 18:27:56 +00:00
|
|
|
// This file Copyright © 2015-2022 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 <libtransmission/transmission.h>
|
|
|
|
#include <libtransmission/error.h>
|
|
|
|
#include <libtransmission/utils.h>
|
|
|
|
|
|
|
|
#include "daemon.h"
|
|
|
|
|
2021-10-06 16:32:17 +00:00
|
|
|
static dtr_callbacks const* callbacks = nullptr;
|
|
|
|
static void* callback_arg = nullptr;
|
2015-04-04 17:43:56 +00:00
|
|
|
|
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
|
|
|
|
|
|
|
|
static int sigfd = -1;
|
2015-04-04 17:43:56 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static void handle_signal(int sig)
|
2015-04-04 17:43:56 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
switch (sig)
|
2015-04-04 17:43:56 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
case SIGHUP:
|
|
|
|
callbacks->on_reconfigure(callback_arg);
|
2015-04-04 17:43:56 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case SIGINT:
|
|
|
|
case SIGTERM:
|
|
|
|
callbacks->on_stop(callback_arg);
|
2015-04-04 17:43:56 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
default:
|
2022-05-04 23:54:41 +00:00
|
|
|
assert("Unexpected signal" && 0);
|
2015-04-04 17:43:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-07 20:12:35 +00:00
|
|
|
static void handle_signals(evutil_socket_t fd, short /*what*/, void* /*arg*/)
|
|
|
|
{
|
|
|
|
struct signalfd_siginfo fdsi;
|
|
|
|
|
|
|
|
if (read(fd, &fdsi, sizeof(fdsi)) != sizeof(fdsi))
|
|
|
|
assert("Error reading signal descriptor" && 0);
|
|
|
|
else
|
|
|
|
handle_signal(fdsi.ssi_signo);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool setup_signals(void* arg)
|
|
|
|
{
|
|
|
|
sigset_t mask = {};
|
|
|
|
struct event* sigev = nullptr;
|
|
|
|
struct event_base* base = static_cast<struct event_base*>(arg);
|
|
|
|
|
|
|
|
sigemptyset(&mask);
|
|
|
|
sigaddset(&mask, SIGINT);
|
|
|
|
sigaddset(&mask, SIGTERM);
|
|
|
|
sigaddset(&mask, SIGHUP);
|
|
|
|
|
|
|
|
if (sigprocmask(SIG_BLOCK, &mask, nullptr) < 0)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
sigfd = signalfd(-1, &mask, 0);
|
|
|
|
if (sigfd < 0)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
sigev = event_new(base, sigfd, EV_READ | EV_PERSIST, handle_signals, nullptr);
|
|
|
|
if (sigev == nullptr)
|
|
|
|
{
|
|
|
|
close(sigfd);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (event_add(sigev, nullptr) < 0)
|
|
|
|
{
|
|
|
|
event_del(sigev);
|
|
|
|
close(sigfd);
|
|
|
|
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-08 09:23:54 +00:00
|
|
|
static void reconfigure(evutil_socket_t /*fd*/, short /*events*/, void* /*arg*/)
|
2015-04-04 17:43:56 +00:00
|
|
|
{
|
2022-09-08 09:23:54 +00:00
|
|
|
callbacks->on_reconfigure(callback_arg);
|
2015-04-04 17:43:56 +00:00
|
|
|
}
|
|
|
|
|
2022-09-08 09:23:54 +00:00
|
|
|
static void stop(evutil_socket_t /*fd*/, short /*events*/, void* /*arg*/)
|
2015-04-04 17:43:56 +00:00
|
|
|
{
|
2022-09-08 09:23:54 +00:00
|
|
|
callbacks->on_stop(callback_arg);
|
2015-04-04 17:43:56 +00:00
|
|
|
}
|
|
|
|
|
2022-09-08 09:23:54 +00:00
|
|
|
static bool setup_signal(struct event_base* base, int sig, void (*callback)(evutil_socket_t, short, void*))
|
2015-04-04 17:43:56 +00:00
|
|
|
{
|
2022-09-08 09:23:54 +00:00
|
|
|
struct event* sigev = evsignal_new(base, sig, callback, nullptr);
|
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-08 09:23:54 +00:00
|
|
|
static bool setup_signals(void* arg)
|
2015-04-04 17:43:56 +00:00
|
|
|
{
|
2022-09-08 09:23:54 +00:00
|
|
|
struct event_base* base = static_cast<struct event_base*>(arg);
|
2015-04-04 17:43:56 +00:00
|
|
|
|
2022-09-08 09:23:54 +00:00
|
|
|
return (setup_signal(base, SIGHUP, reconfigure) && setup_signal(base, SIGINT, stop) && setup_signal(base, SIGTERM, stop));
|
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
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
bool dtr_daemon(dtr_callbacks const* cb, void* cb_arg, bool foreground, int* exit_code, tr_error** error)
|
2015-04-04 17:43:56 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
callbacks = cb;
|
|
|
|
callback_arg = cb_arg;
|
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-07 20:12:35 +00:00
|
|
|
*exit_code = cb->on_start(cb_arg, setup_signals, 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
|
|
|
}
|