2010-09-21 03:28:08 +02:00
|
|
|
/*
|
2011-01-29 10:13:54 +01:00
|
|
|
* Copyright (C) 2003-2011 The Music Player Daemon Project
|
2010-09-21 03:28:08 +02:00
|
|
|
* http://www.musicpd.org
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
2012-09-28 00:40:00 +02:00
|
|
|
#include "Main.hxx"
|
2010-09-21 03:28:08 +02:00
|
|
|
|
|
|
|
#ifdef WIN32
|
|
|
|
|
2013-10-15 09:21:13 +02:00
|
|
|
#include "Compiler.h"
|
2013-01-09 23:12:53 +01:00
|
|
|
#include "GlobalEvents.hxx"
|
2013-09-05 18:20:52 +02:00
|
|
|
#include "system/FatalError.hxx"
|
2010-09-21 03:28:08 +02:00
|
|
|
|
2013-01-13 10:20:32 +01:00
|
|
|
#include <cstdlib>
|
|
|
|
#include <atomic>
|
|
|
|
|
2010-09-21 03:28:08 +02:00
|
|
|
#include <glib.h>
|
|
|
|
|
|
|
|
#include <windows.h>
|
|
|
|
|
|
|
|
static int service_argc;
|
|
|
|
static char **service_argv;
|
|
|
|
static char service_name[] = "";
|
2013-01-13 10:20:32 +01:00
|
|
|
static std::atomic_bool running;
|
2010-09-21 03:28:08 +02:00
|
|
|
static SERVICE_STATUS_HANDLE service_handle;
|
|
|
|
|
|
|
|
static void WINAPI
|
|
|
|
service_main(DWORD argc, CHAR *argv[]);
|
|
|
|
|
|
|
|
static SERVICE_TABLE_ENTRY service_registry[] = {
|
|
|
|
{service_name, service_main},
|
2013-10-19 18:19:03 +02:00
|
|
|
{nullptr, nullptr}
|
2010-09-21 03:28:08 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
static void
|
|
|
|
service_notify_status(DWORD status_code)
|
|
|
|
{
|
|
|
|
SERVICE_STATUS current_status;
|
|
|
|
|
|
|
|
current_status.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
|
|
|
|
current_status.dwControlsAccepted = status_code == SERVICE_START_PENDING
|
|
|
|
? 0
|
|
|
|
: SERVICE_ACCEPT_SHUTDOWN | SERVICE_ACCEPT_STOP;
|
|
|
|
|
|
|
|
current_status.dwCurrentState = status_code;
|
|
|
|
current_status.dwWin32ExitCode = NO_ERROR;
|
|
|
|
current_status.dwCheckPoint = 0;
|
|
|
|
current_status.dwWaitHint = 1000;
|
|
|
|
|
|
|
|
SetServiceStatus(service_handle, ¤t_status);
|
|
|
|
}
|
|
|
|
|
|
|
|
static DWORD WINAPI
|
2013-08-04 23:48:01 +02:00
|
|
|
service_dispatcher(gcc_unused DWORD control, gcc_unused DWORD event_type,
|
|
|
|
gcc_unused void *event_data, gcc_unused void *context)
|
2010-09-21 03:28:08 +02:00
|
|
|
{
|
|
|
|
switch (control) {
|
|
|
|
case SERVICE_CONTROL_SHUTDOWN:
|
|
|
|
case SERVICE_CONTROL_STOP:
|
2013-01-09 23:12:53 +01:00
|
|
|
GlobalEvents::Emit(GlobalEvents::SHUTDOWN);
|
2010-09-21 03:28:08 +02:00
|
|
|
return NO_ERROR;
|
|
|
|
default:
|
|
|
|
return NO_ERROR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void WINAPI
|
2013-08-04 23:48:01 +02:00
|
|
|
service_main(gcc_unused DWORD argc, gcc_unused CHAR *argv[])
|
2010-09-21 03:28:08 +02:00
|
|
|
{
|
|
|
|
DWORD error_code;
|
|
|
|
gchar* error_message;
|
|
|
|
|
|
|
|
service_handle =
|
|
|
|
RegisterServiceCtrlHandlerEx(service_name,
|
2013-10-19 18:19:03 +02:00
|
|
|
service_dispatcher, nullptr);
|
2010-09-21 03:28:08 +02:00
|
|
|
|
|
|
|
if (service_handle == 0) {
|
|
|
|
error_code = GetLastError();
|
|
|
|
error_message = g_win32_error_message(error_code);
|
2013-09-05 18:20:52 +02:00
|
|
|
FormatFatalError("RegisterServiceCtrlHandlerEx() failed: %s",
|
|
|
|
error_message);
|
2010-09-21 03:28:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
service_notify_status(SERVICE_START_PENDING);
|
|
|
|
mpd_main(service_argc, service_argv);
|
|
|
|
service_notify_status(SERVICE_STOPPED);
|
|
|
|
}
|
|
|
|
|
|
|
|
static BOOL WINAPI
|
|
|
|
console_handler(DWORD event)
|
|
|
|
{
|
|
|
|
switch (event) {
|
|
|
|
case CTRL_C_EVENT:
|
|
|
|
case CTRL_CLOSE_EVENT:
|
2013-01-13 10:20:32 +01:00
|
|
|
if (running.load()) {
|
|
|
|
// Recent msdn docs that process is terminated
|
|
|
|
// if this function returns TRUE.
|
|
|
|
// We initiate correct shutdown sequence (if possible).
|
|
|
|
// Once main() returns CRT will terminate our process
|
|
|
|
// regardless our thread is still active.
|
|
|
|
// If this did not happen within 3 seconds
|
|
|
|
// let's shutdown anyway.
|
2013-01-09 23:12:53 +01:00
|
|
|
GlobalEvents::Emit(GlobalEvents::SHUTDOWN);
|
2013-01-13 10:20:32 +01:00
|
|
|
// Under debugger it's better to wait indefinitely
|
|
|
|
// to allow debugging of shutdown code.
|
|
|
|
Sleep(IsDebuggerPresent() ? INFINITE : 3000);
|
|
|
|
}
|
|
|
|
// If we're not running main loop there is no chance for
|
|
|
|
// clean shutdown.
|
|
|
|
std::exit(EXIT_FAILURE);
|
2010-09-21 03:28:08 +02:00
|
|
|
return TRUE;
|
|
|
|
default:
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int win32_main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
DWORD error_code;
|
|
|
|
gchar* error_message;
|
|
|
|
|
|
|
|
service_argc = argc;
|
|
|
|
service_argv = argv;
|
|
|
|
|
|
|
|
if (StartServiceCtrlDispatcher(service_registry))
|
|
|
|
return 0; /* run as service successefully */
|
|
|
|
|
|
|
|
error_code = GetLastError();
|
|
|
|
if (error_code == ERROR_FAILED_SERVICE_CONTROLLER_CONNECT) {
|
|
|
|
/* running as console app */
|
2013-01-13 10:20:32 +01:00
|
|
|
running.store(false);
|
2010-09-21 03:28:08 +02:00
|
|
|
SetConsoleTitle("Music Player Daemon");
|
|
|
|
SetConsoleCtrlHandler(console_handler, TRUE);
|
|
|
|
return mpd_main(argc, argv);
|
|
|
|
}
|
|
|
|
|
|
|
|
error_message = g_win32_error_message(error_code);
|
2013-09-05 18:20:52 +02:00
|
|
|
FormatFatalError("StartServiceCtrlDispatcher() failed: %s",
|
|
|
|
error_message);
|
2010-09-21 03:28:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void win32_app_started()
|
|
|
|
{
|
|
|
|
if (service_handle != 0)
|
|
|
|
service_notify_status(SERVICE_RUNNING);
|
|
|
|
else
|
2013-01-13 10:20:32 +01:00
|
|
|
running.store(true);
|
2010-09-21 03:28:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void win32_app_stopping()
|
|
|
|
{
|
|
|
|
if (service_handle != 0)
|
|
|
|
service_notify_status(SERVICE_STOP_PENDING);
|
|
|
|
else
|
2013-01-13 10:20:32 +01:00
|
|
|
running.store(false);
|
2010-09-21 03:28:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|