Win32Main.cxx 4.13 KB
Newer Older
1
/*
2
 * Copyright 2003-2016 The Music Player Daemon Project
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
 * 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"
Max Kellermann's avatar
Max Kellermann committed
21
#include "Main.hxx"
22 23 24

#ifdef WIN32

25
#include "Compiler.h"
26
#include "GlobalEvents.hxx"
27
#include "system/FatalError.hxx"
28

29 30 31
#include <cstdlib>
#include <atomic>

32
#include <windows.h>
33
#include <tchar.h>
34 35 36

static int service_argc;
static char **service_argv;
37
static TCHAR service_name[] = _T("");
38
static std::atomic_bool running;
39 40 41
static SERVICE_STATUS_HANDLE service_handle;

static void WINAPI
42
service_main(DWORD argc, LPTSTR argv[]);
43

44
static constexpr SERVICE_TABLE_ENTRY service_registry[] = {
45
	{service_name, service_main},
46
	{nullptr, nullptr}
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
};

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, &current_status);
}

static DWORD WINAPI
68 69
service_dispatcher(gcc_unused DWORD control, gcc_unused DWORD event_type,
		   gcc_unused void *event_data, gcc_unused void *context)
70 71 72 73
{
	switch (control) {
	case SERVICE_CONTROL_SHUTDOWN:
	case SERVICE_CONTROL_STOP:
74
		GlobalEvents::Emit(GlobalEvents::SHUTDOWN);
75 76 77 78 79 80 81
		return NO_ERROR;
	default:
		return NO_ERROR;
	}
}

static void WINAPI
82
service_main(gcc_unused DWORD argc, gcc_unused LPTSTR argv[])
83 84 85
{
	service_handle =
		RegisterServiceCtrlHandlerEx(service_name,
86
					     service_dispatcher, nullptr);
87

88 89
	if (service_handle == 0)
		FatalSystemError("RegisterServiceCtrlHandlerEx() failed");
90 91 92 93 94 95 96 97 98 99 100 101

	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:
102 103 104 105 106 107 108 109
		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.
110
			GlobalEvents::Emit(GlobalEvents::SHUTDOWN);
111 112 113 114 115 116 117
			// 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);
118 119 120 121 122 123 124 125 126 127 128 129 130 131
		return TRUE;
	default:
		return FALSE;
	}
}

int win32_main(int argc, char *argv[])
{
	service_argc = argc;
	service_argv = argv;

	if (StartServiceCtrlDispatcher(service_registry))
		return 0; /* run as service successefully */

132
	const DWORD error_code = GetLastError();
133 134
	if (error_code == ERROR_FAILED_SERVICE_CONTROLLER_CONNECT) {
		/* running as console app */
135
		running.store(false);
136
		SetConsoleTitle(_T("Music Player Daemon"));
137 138 139 140
		SetConsoleCtrlHandler(console_handler, TRUE);
		return mpd_main(argc, argv);
	}

141
	FatalSystemError("StartServiceCtrlDispatcher() failed", error_code);
142 143 144 145 146 147 148
}

void win32_app_started()
{
	if (service_handle != 0)
		service_notify_status(SERVICE_RUNNING);
	else
149
		running.store(true);
150 151 152 153 154 155 156
}

void win32_app_stopping()
{
	if (service_handle != 0)
		service_notify_status(SERVICE_STOP_PENDING);
	else
157
		running.store(false);
158 159 160
}

#endif