summaryrefslogtreecommitdiff
path: root/utils/vmpi/vmpi_service/service_helpers.cpp
blob: a20c5dfc4d4485a2fe1b67645aec330c508be8a0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose: 
//
// $NoKeywords: $
//=============================================================================//

#include "stdafx.h"
#include "service_helpers.h"


static CRITICAL_SECTION g_CtrlHandlerMutex;

static void (*g_pInternalServiceFn)( void *pParam ) = NULL;
static void *g_pInternalServiceParam = NULL;

static volatile bool g_bShouldExit = false;


SERVICE_STATUS          MyServiceStatus; 
SERVICE_STATUS_HANDLE   MyServiceStatusHandle = NULL;


void WINAPI MyServiceCtrlHandler( DWORD Opcode )
{ 
    DWORD status; 
 
    switch(Opcode) 
    { 
        case SERVICE_CONTROL_STOP: 
			// Do whatever it takes to stop here. 
			ServiceHelpers_ExitEarly();

            MyServiceStatus.dwWin32ExitCode = 0; 
            MyServiceStatus.dwCurrentState  = SERVICE_STOPPED; 
 
            if ( !SetServiceStatus( MyServiceStatusHandle, &MyServiceStatus) )
            { 
                status = GetLastError(); 
                Msg( "[MY_SERVICE] SetServiceStatus error %ld\n", status ); 
            } 
 
            Msg( "[MY_SERVICE] Leaving MyService \n" ); 
            return; 
 
        case SERVICE_CONTROL_INTERROGATE: 
			// Fall through to send current status. 
            break; 
 
        default: 
            Msg("[MY_SERVICE] Unrecognized opcode %ld\n", Opcode ); 
    } 
 
    // Send current status. 
    if ( !SetServiceStatus( MyServiceStatusHandle, &MyServiceStatus ) )
    { 
        status = GetLastError(); 
        Msg( "[MY_SERVICE] SetServiceStatus error %ld\n", status ); 
    } 
}


void WINAPI MyServiceStart( DWORD argc, LPTSTR *argv ) 
{ 
    DWORD status; 

    MyServiceStatus.dwServiceType        = SERVICE_WIN32; 
    MyServiceStatus.dwCurrentState       = SERVICE_START_PENDING; 
    MyServiceStatus.dwControlsAccepted   = SERVICE_ACCEPT_STOP; 
    MyServiceStatus.dwWin32ExitCode      = 0; 
    MyServiceStatus.dwServiceSpecificExitCode = 0; 
    MyServiceStatus.dwCheckPoint         = 0; 
    MyServiceStatus.dwWaitHint           = 0; 
 
    MyServiceStatusHandle = RegisterServiceCtrlHandler( "MyService", MyServiceCtrlHandler ); 
    if ( MyServiceStatusHandle == (SERVICE_STATUS_HANDLE)0 ) 
    { 
        Msg("[MY_SERVICE] RegisterServiceCtrlHandler failed %d\n", GetLastError() );
        return; 
    } 

    // Initialization complete - report running status. 
    MyServiceStatus.dwCurrentState       = SERVICE_RUNNING; 
 
    if ( !SetServiceStatus( MyServiceStatusHandle, &MyServiceStatus ) )
    { 
        status = GetLastError(); 
        Msg( "[MY_SERVICE] SetServiceStatus error %ld\n", status );
    } 


	// Run the app's main in-thread loop.    
	g_pInternalServiceFn( g_pInternalServiceParam );


	// Tell the SCM that we're stopped.        
	MyServiceStatus.dwCurrentState       = SERVICE_STOPPED; 
    MyServiceStatus.dwWin32ExitCode      = NO_ERROR;
    MyServiceStatus.dwServiceSpecificExitCode = 0; 
    SetServiceStatus( MyServiceStatusHandle, &MyServiceStatus );


    // This is where the service does its work. 
    Msg( "[MY_SERVICE] Returning the Main Thread \n" ); 
}


void ServiceHelpers_Init()
{
	InitializeCriticalSection( &g_CtrlHandlerMutex );
}


bool ServiceHelpers_StartService( const char *pServiceName, void (*pFn)( void *pParam ), void *pParam )
{
	// Ok, just run the service.
	const SERVICE_TABLE_ENTRY DispatchTable[2] = 
	{
		{ (char*)pServiceName, MyServiceStart },
		{ NULL, NULL }
	};

	g_pInternalServiceFn = pFn;
	g_pInternalServiceParam = pParam;

	if ( StartServiceCtrlDispatcher( DispatchTable ) ) 
	{
		return true;
	}
	else
	{ 											    
		Msg( "StartServiceCtrlDispatcher error = '%s'\n", GetLastErrorString() ); 
		return false;
	}
}


void ServiceHelpers_ExitEarly()
{
	EnterCriticalSection( &g_CtrlHandlerMutex );
	g_bShouldExit = true;
	LeaveCriticalSection( &g_CtrlHandlerMutex );
}


bool ServiceHelpers_ShouldExit()
{
	EnterCriticalSection( &g_CtrlHandlerMutex );
	bool bRet = g_bShouldExit;
	LeaveCriticalSection( &g_CtrlHandlerMutex );

	return bRet;
}


char* GetLastErrorString()
{
	static char err[2048];
	
	LPVOID lpMsgBuf;
	FormatMessage( 
		FORMAT_MESSAGE_ALLOCATE_BUFFER | 
		FORMAT_MESSAGE_FROM_SYSTEM | 
		FORMAT_MESSAGE_IGNORE_INSERTS,
		NULL,
		GetLastError(),
		MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
		(LPTSTR) &lpMsgBuf,
		0,
		NULL 
	);

	strncpy( err, (char*)lpMsgBuf, sizeof( err ) );
	LocalFree( lpMsgBuf );

	err[ sizeof( err ) - 1 ] = 0;

	return err;
}