2015-04-04 17:43:56 +00:00
|
|
|
/*
|
|
|
|
* This file Copyright (C) 2015 Mnemosyne LLC
|
|
|
|
*
|
|
|
|
* It may be used under the GNU GPL versions 2 or 3
|
|
|
|
* or any future license endorsed by Mnemosyne LLC.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2017-04-21 07:40:57 +00:00
|
|
|
#include <process.h> /* _beginthreadex() */
|
2015-04-04 17:43:56 +00:00
|
|
|
|
|
|
|
#include <windows.h>
|
|
|
|
|
|
|
|
#include <libtransmission/transmission.h>
|
|
|
|
#include <libtransmission/error.h>
|
|
|
|
#include <libtransmission/log.h>
|
2020-08-18 11:09:30 +00:00
|
|
|
#include <libtransmission/tr-macros.h>
|
2015-04-04 17:43:56 +00:00
|
|
|
#include <libtransmission/utils.h>
|
|
|
|
|
|
|
|
#include "daemon.h"
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
|
|
|
#ifndef SERVICE_ACCEPT_PRESHUTDOWN
|
2017-04-19 12:04:45 +00:00
|
|
|
#define SERVICE_ACCEPT_PRESHUTDOWN 0x00000100
|
2015-04-04 17:43:56 +00:00
|
|
|
#endif
|
|
|
|
#ifndef SERVICE_CONTROL_PRESHUTDOWN
|
2017-04-19 12:04:45 +00:00
|
|
|
#define SERVICE_CONTROL_PRESHUTDOWN 0x0000000F
|
2015-04-04 17:43:56 +00:00
|
|
|
#endif
|
|
|
|
|
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
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
static LPCWSTR const service_name = L"TransmissionDaemon";
|
2015-04-04 17:43:56 +00:00
|
|
|
|
2021-10-06 16:32:17 +00:00
|
|
|
static SERVICE_STATUS_HANDLE status_handle = nullptr;
|
2015-04-04 17:43:56 +00:00
|
|
|
static DWORD current_state = SERVICE_STOPPED;
|
2021-10-06 16:32:17 +00:00
|
|
|
static HANDLE service_thread = nullptr;
|
|
|
|
static HANDLE service_stop_thread = nullptr;
|
2015-04-04 17:43:56 +00:00
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
static void set_system_error(tr_error** error, DWORD code, char const* message)
|
2015-04-04 17:43:56 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
char* const system_message = tr_win32_format_message(code);
|
|
|
|
tr_error_set(error, code, "%s (0x%08lx): %s", message, code, system_message);
|
|
|
|
tr_free(system_message);
|
2015-04-04 17:43:56 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
static void do_log_system_error(char const* file, int line, tr_log_level level, DWORD code, char const* message)
|
2015-04-04 17:43:56 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
char* const system_message = tr_win32_format_message(code);
|
|
|
|
tr_logAddMessage(file, line, level, "[dtr_daemon] %s (0x%08lx): %s", message, code, system_message);
|
|
|
|
tr_free(system_message);
|
2015-04-04 17:43:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#define log_system_error(level, code, message) \
|
2017-04-19 12:04:45 +00:00
|
|
|
do \
|
2015-04-04 17:43:56 +00:00
|
|
|
{ \
|
2017-04-20 16:02:19 +00:00
|
|
|
DWORD const local_code = (code); \
|
2021-08-15 09:41:48 +00:00
|
|
|
\
|
2017-04-19 12:04:45 +00:00
|
|
|
if (tr_logLevelIsActive((level))) \
|
|
|
|
{ \
|
|
|
|
do_log_system_error(__FILE__, __LINE__, (level), local_code, (message)); \
|
|
|
|
} \
|
2021-08-15 09:41:48 +00:00
|
|
|
} while (0)
|
2015-04-04 17:43:56 +00:00
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2021-10-10 16:52:26 +00:00
|
|
|
static BOOL WINAPI handle_console_ctrl([[maybe_unused]] DWORD control_type)
|
2015-04-04 17:43:56 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
callbacks->on_stop(callback_arg);
|
|
|
|
return TRUE;
|
2015-04-04 17:43:56 +00:00
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
static void update_service_status(
|
|
|
|
DWORD new_state,
|
|
|
|
DWORD win32_exit_code,
|
|
|
|
DWORD service_specific_exit_code,
|
|
|
|
DWORD check_point,
|
2017-04-19 12:04:45 +00:00
|
|
|
DWORD wait_hint)
|
2015-04-04 17:43:56 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
SERVICE_STATUS status;
|
|
|
|
status.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
|
|
|
|
status.dwCurrentState = new_state;
|
2021-08-15 09:41:48 +00:00
|
|
|
status.dwControlsAccepted = new_state != SERVICE_RUNNING ?
|
|
|
|
0 :
|
|
|
|
SERVICE_ACCEPT_PRESHUTDOWN | SERVICE_ACCEPT_SHUTDOWN | SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_PARAMCHANGE;
|
2017-04-19 12:04:45 +00:00
|
|
|
status.dwWin32ExitCode = service_specific_exit_code == 0 ? win32_exit_code : ERROR_SERVICE_SPECIFIC_ERROR;
|
|
|
|
status.dwServiceSpecificExitCode = service_specific_exit_code;
|
|
|
|
status.dwCheckPoint = check_point;
|
|
|
|
status.dwWaitHint = wait_hint;
|
|
|
|
|
|
|
|
if (SetServiceStatus(status_handle, &status))
|
|
|
|
{
|
|
|
|
current_state = new_state;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
log_system_error(TR_LOG_DEBUG, GetLastError(), "SetServiceStatus() failed");
|
|
|
|
}
|
2015-04-04 17:43:56 +00:00
|
|
|
}
|
|
|
|
|
2021-09-19 20:41:35 +00:00
|
|
|
#define TR_MAX(a, b) (((a) > (b)) ? (a) : (b))
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static unsigned int __stdcall service_stop_thread_main(void* param)
|
2015-04-04 17:43:56 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
callbacks->on_stop(callback_arg);
|
2015-04-04 17:43:56 +00:00
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
DWORD const sleep_time = 500;
|
2017-04-19 12:04:45 +00:00
|
|
|
DWORD wait_time = (DWORD)(UINT_PTR)param;
|
|
|
|
|
|
|
|
for (DWORD checkpoint = 2; WaitForSingleObject(service_thread, sleep_time) == WAIT_TIMEOUT; ++checkpoint)
|
2015-04-04 17:43:56 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
wait_time = wait_time >= sleep_time ? wait_time - sleep_time : 0;
|
2021-09-19 20:41:35 +00:00
|
|
|
update_service_status(SERVICE_STOP_PENDING, NO_ERROR, 0, checkpoint, TR_MAX(wait_time, sleep_time * 2));
|
2015-04-04 17:43:56 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return 0;
|
2015-04-04 17:43:56 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static void stop_service(void)
|
2015-04-04 17:43:56 +00:00
|
|
|
{
|
2021-10-06 16:32:17 +00:00
|
|
|
if (service_stop_thread != nullptr)
|
2015-04-04 18:28:13 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
return;
|
2015-04-04 18:28:13 +00:00
|
|
|
}
|
2015-04-04 17:43:56 +00:00
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
DWORD const wait_time = 30 * 1000;
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
update_service_status(SERVICE_STOP_PENDING, NO_ERROR, 0, 1, wait_time);
|
2015-04-04 17:43:56 +00:00
|
|
|
|
2021-10-06 16:32:17 +00:00
|
|
|
service_stop_thread = (HANDLE)
|
|
|
|
_beginthreadex(nullptr, 0, &service_stop_thread_main, (LPVOID)(UINT_PTR)wait_time, 0, nullptr);
|
2015-04-04 17:43:56 +00:00
|
|
|
|
2021-10-06 16:32:17 +00:00
|
|
|
if (service_stop_thread == nullptr)
|
2015-04-04 17:43:56 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
log_system_error(TR_LOG_DEBUG, GetLastError(), "_beginthreadex() failed, trying to stop synchronously");
|
|
|
|
service_stop_thread_main((LPVOID)(UINT_PTR)wait_time);
|
2015-04-04 17:43:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-10 16:52:26 +00:00
|
|
|
static DWORD WINAPI handle_service_ctrl(
|
|
|
|
DWORD control_code,
|
|
|
|
[[maybe_unused]] DWORD event_type,
|
|
|
|
[[maybe_unused]] LPVOID event_data,
|
|
|
|
[[maybe_unused]] LPVOID context)
|
2015-04-04 17:43:56 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
switch (control_code)
|
2015-04-04 17:43:56 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
case SERVICE_CONTROL_PRESHUTDOWN:
|
|
|
|
case SERVICE_CONTROL_SHUTDOWN:
|
|
|
|
case SERVICE_CONTROL_STOP:
|
|
|
|
stop_service();
|
2015-04-04 17:43:56 +00:00
|
|
|
return NO_ERROR;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case SERVICE_CONTROL_PARAMCHANGE:
|
|
|
|
callbacks->on_reconfigure(callback_arg);
|
2015-04-04 17:43:56 +00:00
|
|
|
return NO_ERROR;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case SERVICE_CONTROL_INTERROGATE:
|
|
|
|
update_service_status(current_state, NO_ERROR, 0, 0, 0);
|
2015-04-04 17:43:56 +00:00
|
|
|
return NO_ERROR;
|
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return ERROR_CALL_NOT_IMPLEMENTED;
|
2015-04-04 17:43:56 +00:00
|
|
|
}
|
|
|
|
|
2021-10-10 16:52:26 +00:00
|
|
|
static unsigned int __stdcall service_thread_main([[maybe_unused]] void* context)
|
2015-04-04 17:43:56 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
return callbacks->on_start(callback_arg, false);
|
2015-04-04 17:43:56 +00:00
|
|
|
}
|
|
|
|
|
2021-10-10 16:52:26 +00:00
|
|
|
static VOID WINAPI service_main([[maybe_unused]] DWORD argc, [[maybe_unused]] LPWSTR* argv)
|
2015-04-04 17:43:56 +00:00
|
|
|
{
|
2021-10-06 16:32:17 +00:00
|
|
|
status_handle = RegisterServiceCtrlHandlerExW(service_name, &handle_service_ctrl, nullptr);
|
2015-04-04 17:43:56 +00:00
|
|
|
|
2021-10-06 16:32:17 +00:00
|
|
|
if (status_handle == nullptr)
|
2015-04-04 17:43:56 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
log_system_error(TR_LOG_ERROR, GetLastError(), "RegisterServiceCtrlHandlerEx() failed");
|
|
|
|
return;
|
2015-04-04 17:43:56 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
update_service_status(SERVICE_START_PENDING, NO_ERROR, 0, 1, 1000);
|
2015-04-04 17:43:56 +00:00
|
|
|
|
2021-10-06 16:32:17 +00:00
|
|
|
service_thread = (HANDLE)_beginthreadex(nullptr, 0, &service_thread_main, nullptr, 0, nullptr);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2021-10-06 16:32:17 +00:00
|
|
|
if (service_thread == nullptr)
|
2015-04-04 17:43:56 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
log_system_error(TR_LOG_ERROR, GetLastError(), "_beginthreadex() failed");
|
|
|
|
return;
|
2015-04-04 17:43:56 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
update_service_status(SERVICE_RUNNING, NO_ERROR, 0, 0, 0);
|
2015-04-04 17:43:56 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (WaitForSingleObject(service_thread, INFINITE) != WAIT_OBJECT_0)
|
|
|
|
{
|
|
|
|
log_system_error(TR_LOG_ERROR, GetLastError(), "WaitForSingleObject() failed");
|
|
|
|
}
|
2015-04-04 17:43:56 +00:00
|
|
|
|
2021-10-06 16:32:17 +00:00
|
|
|
if (service_stop_thread != nullptr)
|
2015-04-04 17:43:56 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
WaitForSingleObject(service_stop_thread, INFINITE);
|
|
|
|
CloseHandle(service_stop_thread);
|
2015-04-04 17:43:56 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
DWORD exit_code;
|
|
|
|
|
|
|
|
if (!GetExitCodeThread(service_thread, &exit_code))
|
|
|
|
{
|
|
|
|
exit_code = 1;
|
|
|
|
}
|
2015-04-04 17:43:56 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
CloseHandle(service_thread);
|
2015-04-04 17:43:56 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
update_service_status(SERVICE_STOPPED, NO_ERROR, exit_code, 0, 0);
|
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 (!SetConsoleCtrlHandler(&handle_console_ctrl, TRUE))
|
2015-04-04 17:43:56 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
set_system_error(error, GetLastError(), "SetConsoleCtrlHandler() failed");
|
|
|
|
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, true);
|
2015-04-04 17:43:56 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
else
|
2015-04-04 17:43:56 +00:00
|
|
|
{
|
2021-08-15 09:41:48 +00:00
|
|
|
SERVICE_TABLE_ENTRY const service_table[] = {
|
2017-04-19 12:04:45 +00:00
|
|
|
{ (LPWSTR)service_name, &service_main },
|
2021-10-06 16:32:17 +00:00
|
|
|
{ nullptr, nullptr },
|
2015-04-04 17:43:56 +00:00
|
|
|
};
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (!StartServiceCtrlDispatcherW(service_table))
|
2015-04-04 17:43:56 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
set_system_error(error, GetLastError(), "StartServiceCtrlDispatcher() failed");
|
|
|
|
return false;
|
2015-04-04 17:43:56 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
*exit_code = 0;
|
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
|
|
|
}
|