-
Notifications
You must be signed in to change notification settings - Fork 0
/
service.c
191 lines (154 loc) · 5.48 KB
/
service.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/*
Copyright (c) 1998-2007, Purdue University
All rights reserved.
Redistribution and use in source and binary forms are permitted provided
that:
(1) source distributions retain this entire copyright notice and comment,
and
(2) distributions including binaries display the following acknowledgement:
"This product includes software developed by Purdue University & Rochester Institute of Technology."
in the documentation or other materials provided with the distribution
and in all advertising materials mentioning features or use of this
software.
The name of the University may not be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
This software was developed by:
Curtis Smith
Purdue University
Engineering Computer Network
465 Northwestern Avenue
West Lafayette, Indiana 47907-2035 U.S.A.
Send all comments, suggestions, or bug reports to:
*/
/* Include files */
#include "main.h"
#include "log.h"
/* Service */
/* Effect main loop */
BOOL ServiceIsRunning = TRUE;
/* Forward */
static void WINAPI ServiceMain(DWORD argc, LPTSTR * argv);
/* Service dispatch table */
static SERVICE_TABLE_ENTRY ServiceDispatchTable[] = {
{ "EvtSys", ServiceMain },
{ NULL, NULL }
};
/* Service handle */
static SERVICE_STATUS_HANDLE ServiceStatusHandle;
static SERVICE_STATUS ServiceStatus;
/* Install a service */
int ServiceInstall()
{
SC_HANDLE service_manager;
SC_HANDLE new_service;
/* Get a connection to the service manager */
service_manager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
if (service_manager == NULL) {
Log(LOG_ERROR|LOG_SYS, "Cannot initialize access to the service manager");
return 1;
}
/* Create a new service */
new_service = CreateService(service_manager, "EvtSys", "Eventlog to Syslog", SERVICE_ALL_ACCESS, SERVICE_WIN32_OWN_PROCESS, SERVICE_AUTO_START, SERVICE_ERROR_IGNORE, ProgramExePath, NULL, NULL, "eventlog\0", NULL, NULL);
if (new_service == NULL)
Log(LOG_ERROR|LOG_SYS, "Cannot create service");
else
CloseServiceHandle(new_service);
/* Close the connection to the manager */
CloseServiceHandle(service_manager);
/* Return status */
return new_service == NULL;
}
/* Remove a service */
int ServiceRemove()
{
int status = 1;
SC_HANDLE service_manager;
SC_HANDLE service_handle;
SERVICE_STATUS query_status;
/* Get a connection to the service manager */
service_manager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
if (service_manager == NULL) {
Log(LOG_ERROR|LOG_SYS, "Cannot initialize access to the service manager");
return 1;
}
/* Connect to service */
service_handle = OpenService(service_manager, "EvtSys", SERVICE_ALL_ACCESS);
if (service_handle == NULL) {
Log(LOG_ERROR|LOG_SYS, "Cannot open service");
} else {
/* Check that service is stopped */
if (QueryServiceStatus(service_handle, &query_status) && query_status.dwCurrentState != SERVICE_STOPPED) {
Log(LOG_ERROR, "Service currently in operation - Stop service and rerun the uninstall.");
} else {
/* Delete service */
if (DeleteService(service_handle) == FALSE)
Log(LOG_ERROR|LOG_SYS, "Cannot delete service");
else
status = 0;
}
/* Close connection to service */
CloseServiceHandle(service_handle);
}
/* Close connection to service manager */
CloseServiceHandle(service_manager);
/* Return status */
return status;
}
/* Process service changes */
static void WINAPI ServiceChange(DWORD code)
{
/* Check for stop request
(we may want to implement "pause" and "continue" later on) */
if (code == SERVICE_CONTROL_STOP) {
/* Terminate loop */
ServiceIsRunning = FALSE;
/* Wait until service stops */
while (ServiceStatus.dwCurrentState != SERVICE_STOPPED)
Sleep(500);
}
}
/* Process main loop */
static void WINAPI ServiceMain(DWORD argc, LPTSTR * argv)
{
/* Register a control function to the service manager */
ServiceStatusHandle = RegisterServiceCtrlHandler("EvtSys", ServiceChange);
if (ServiceStatusHandle == 0) {
Log(LOG_ERROR|LOG_SYS, "Cannot register a control handler for service");
return;
}
/* Send start report */
ServiceStatus.dwServiceType = SERVICE_WIN32;
ServiceStatus.dwCurrentState = SERVICE_RUNNING;
ServiceStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP;
ServiceStatus.dwWin32ExitCode = 0;
ServiceStatus.dwServiceSpecificExitCode = 0;
ServiceStatus.dwCheckPoint = 0;
ServiceStatus.dwWaitHint = 0;
if (SetServiceStatus(ServiceStatusHandle, &ServiceStatus) == FALSE) {
Log(LOG_ERROR|LOG_SYS, "Cannot send start service status update");
return;
}
/* Process main loop */
MainLoop();
/* Send stop message */
ServiceStatus.dwCurrentState = SERVICE_STOPPED;
/* Report status */
if (SetServiceStatus(ServiceStatusHandle, &ServiceStatus) == FALSE) {
Log(LOG_ERROR|LOG_SYS, "Cannot send change service status update");
return;
}
}
/* Start service dispatcher */
DWORD WINAPI ServiceStart()
{
/* Start service dispatch */
if (StartServiceCtrlDispatcher(ServiceDispatchTable) == FALSE) {
Log(LOG_ERROR|LOG_SYS, "Cannot start service dispatcher");
return 1;
}
return 0;
}