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>
|
|
|
|
#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-12-28 02:32:22 +00:00
|
|
|
using namespace std::literals;
|
|
|
|
|
2015-04-04 17:43:56 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
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
|
|
|
|
|
|
|
static int signal_pipe[2];
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static void send_signal_to_pipe(int sig)
|
2015-04-04 17:43:56 +00:00
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
int const old_errno = errno;
|
2015-04-04 17:43:56 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (write(signal_pipe[1], &sig, sizeof(sig)) == -1)
|
|
|
|
{
|
|
|
|
abort();
|
|
|
|
}
|
2015-04-04 17:43:56 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
errno = old_errno;
|
2015-04-04 17:43:56 +00:00
|
|
|
}
|
|
|
|
|
2021-10-24 16:41:54 +00:00
|
|
|
static void* signal_handler_thread_main(void* /*arg*/)
|
2015-04-04 17:43:56 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
int sig;
|
2015-04-04 17:43:56 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
while (read(signal_pipe[0], &sig, sizeof(sig)) == sizeof(sig) && sig != 0)
|
|
|
|
{
|
|
|
|
handle_signal(sig);
|
|
|
|
}
|
2015-04-04 17:43:56 +00:00
|
|
|
|
2021-10-06 16:32:17 +00:00
|
|
|
return nullptr;
|
2015-04-04 17:43:56 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static bool create_signal_pipe(tr_error** error)
|
2015-04-04 17:43:56 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
if (pipe(signal_pipe) == -1)
|
2015-04-04 17:43:56 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
set_system_error(error, errno, "pipe() failed");
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static void destroy_signal_pipe(void)
|
2015-04-04 17:43:56 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
close(signal_pipe[0]);
|
|
|
|
close(signal_pipe[1]);
|
2015-04-04 17:43:56 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static bool create_signal_handler_thread(pthread_t* thread, tr_error** error)
|
2015-04-04 17:43:56 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
if (!create_signal_pipe(error))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2015-04-04 17:43:56 +00:00
|
|
|
|
2021-10-06 16:32:17 +00:00
|
|
|
if ((errno = pthread_create(thread, nullptr, &signal_handler_thread_main, nullptr)) != 0)
|
2015-04-04 17:43:56 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
set_system_error(error, errno, "pthread_create() failed");
|
|
|
|
destroy_signal_pipe();
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static void destroy_signal_handler_thread(pthread_t thread)
|
2015-04-04 17:43:56 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
send_signal_to_pipe(0);
|
2021-10-06 16:32:17 +00:00
|
|
|
pthread_join(thread, nullptr);
|
2015-04-04 17:43:56 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
destroy_signal_pipe();
|
2015-04-04 17:43:56 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static bool setup_signal_handler(int sig, tr_error** error)
|
2015-04-04 17:43:56 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
assert(sig != 0);
|
2015-04-04 17:43:56 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (signal(sig, &send_signal_to_pipe) == SIG_ERR)
|
2015-04-04 17:43:56 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
set_system_error(error, errno, "signal() failed");
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
pthread_t signal_thread;
|
|
|
|
|
|
|
|
if (!create_signal_handler_thread(&signal_thread, error))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2015-04-04 17:43:56 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (!setup_signal_handler(SIGINT, error) || !setup_signal_handler(SIGTERM, error) || !setup_signal_handler(SIGHUP, error))
|
2015-04-04 17:43:56 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
destroy_signal_handler_thread(signal_thread);
|
|
|
|
return false;
|
2015-04-04 17:43:56 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
*exit_code = cb->on_start(cb_arg, foreground);
|
2015-04-04 17:43:56 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
destroy_signal_handler_thread(signal_thread);
|
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
|
|
|
}
|