-
Notifications
You must be signed in to change notification settings - Fork 3
/
netpipe-svc.c
160 lines (130 loc) · 4.42 KB
/
netpipe-svc.c
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
#include "compat-header.h"
#include "netpipe.h"
/************************************************************************/
/* GLOBAL VARIABLES */
/************************************************************************/
static SERVICE_STATUS _statusRecord;
static SERVICE_STATUS_HANDLE _statusHandle = NULL;
static HANDLE _exitEventHandle = NULL;
static BOOL _testRun = FALSE;
static int _argc = 0;
static char **_argv = NULL;
static DWORD _ReportError(const char *Text, DWORD Code)
{
fprintf(stderr, "%s: %u\n", Text, Code);
return Code;
}
static DWORD WINAPI _NetpipeServiceHandlerEx(DWORD dwControl, DWORD dwEventType, LPVOID lpEventData, LPVOID lpContext
)
{
DWORD ret = NO_ERROR;
switch (dwControl) {
case SERVICE_CONTROL_STOP:
_statusRecord.dwCurrentState = SERVICE_STOP_PENDING;
SetServiceStatus(_statusHandle, &_statusRecord);
NetPipeTerminate();
SetEvent(_exitEventHandle);
break;
default:
break;
}
return ret;
}
void WINAPI ServiceMain(DWORD argc, LPWSTR *argv)
{
DWORD dwError = 0;
int argCount = 0;
char **args = NULL;
if (!_testRun) {
memset(&_statusRecord, 0, sizeof(_statusRecord));
_statusHandle = RegisterServiceCtrlHandlerExW(L"Netpipe", _NetpipeServiceHandlerEx, NULL);
if (_statusHandle != NULL) {
_statusRecord.dwCurrentState = SERVICE_START_PENDING;
_statusRecord.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
if (SetServiceStatus(_statusHandle, &_statusRecord)) {
_exitEventHandle = CreateEventW(NULL, TRUE, FALSE, NULL);
if (_exitEventHandle == NULL) {
_statusRecord.dwCurrentState = SERVICE_STOPPED;
SetServiceStatus(_statusHandle, &_statusRecord);
}
}
if (_exitEventHandle == NULL)
_statusHandle = NULL;
}
}
if (_testRun || _statusHandle != NULL) {
argCount = _argc;
args = _argv;
if (dwError == ERROR_SUCCESS) {
if (!_testRun) {
_statusRecord.dwCurrentState = SERVICE_RUNNING;
_statusRecord.dwControlsAccepted = SERVICE_ACCEPT_STOP;
SetServiceStatus(_statusHandle, &_statusRecord);
}
NetPipeMain(argCount, args);
if (!_testRun) {
WaitForSingleObject(_exitEventHandle, INFINITE);
_statusRecord.dwCurrentState = SERVICE_STOPPED;
SetServiceStatus(_statusHandle, &_statusRecord);
CloseHandle(_exitEventHandle);
}
}
if (_statusHandle != NULL) {
_statusRecord.dwCurrentState = SERVICE_STOPPED;
SetServiceStatus(_statusHandle, &_statusRecord);
}
}
return;
}
int main(int argc, char *argv[])
{
int ret = 0;
switch (argc) {
case 2: {
SC_HANDLE hScm = NULL;
SC_HANDLE hService = NULL;
if (_stricmp(argv[1], "/install") == 0) {
DWORD moduleNameLen = 0;
wchar_t moduleName[MAX_PATH];
memset(moduleName, 0, sizeof(moduleName));
moduleNameLen = GetModuleFileNameW(NULL, moduleName, MAX_PATH);
if (moduleNameLen > 0) {
hScm = OpenSCManagerW(NULL, NULL, SC_MANAGER_CREATE_SERVICE | SC_MANAGER_CONNECT);
if (hScm != NULL) {
hService = CreateServiceW(hScm, L"NetPipe", L"Network to Network Pipe Service", SERVICE_ALL_ACCESS, SERVICE_WIN32_OWN_PROCESS, SERVICE_DEMAND_START, SERVICE_ERROR_NORMAL, moduleName, NULL, NULL, NULL, NULL, NULL);
if (hService != NULL)
CloseServiceHandle(hService);
if (hService == NULL && GetLastError() != ERROR_SERVICE_EXISTS)
_ReportError("CreateService", GetLastError());
CloseServiceHandle(hScm);
} else _ReportError("OpenSCManager", GetLastError());
} else _ReportError("GetModuleFileName", GetLastError());
} else if (_stricmp(argv[1], "/uninstall") == 0) {
hScm = OpenSCManagerW(NULL, NULL, SC_MANAGER_CONNECT);
if (hScm != NULL) {
hService = OpenServiceW(hScm, L"NetPipe", DELETE);
if (hService != NULL) {
if (!DeleteService(hService))
ret = _ReportError("DeleteService", GetLastError());
CloseServiceHandle(hService);
} else ret = _ReportError("OpenService", GetLastError());
CloseServiceHandle(hScm);
} else ret = _ReportError("OpenSCManager", GetLastError());
} else if (_stricmp(argv[1], "/test") == 0) {
_testRun = TRUE;
ServiceMain(0, NULL);
}
default: {
SERVICE_TABLE_ENTRYW svcTable[2];
_argc = argc;
_argv = argv;
memset(svcTable, 0, sizeof(svcTable));
svcTable[0].lpServiceName = L"Netpipe";
svcTable[0].lpServiceProc = ServiceMain;
if (!StartServiceCtrlDispatcherW(svcTable))
ret = GetLastError();
} break;
} break;
}
return ret;
}