33 static SERVICE_STATUS_HANDLE service_status_handle = 0;
35 static int service_argc = 0;
37 static char **service_argv = NULL;
39 static int service_initialized = 0;
41 int main(
int argc,
char **argv);
46 int SCRunningAsService(
void)
48 HANDLE h = INVALID_HANDLE_VALUE;
49 if ((h = CreateFile(
"CONIN$", GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, 0)) == INVALID_HANDLE_VALUE) {
61 static void SCAtExitHandler(
void)
63 SERVICE_STATUS status = {
66 SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN,
76 if (!SetServiceStatus(service_status_handle, &status)) {
77 SCLogWarning(
"Can't set service status: %d", (
int)GetLastError());
79 SCLogInfo(
"Service status set to: SERVICE_STOPPED");
86 static DWORD WINAPI SCServiceCtrlHandlerEx(DWORD code, DWORD etype, LPVOID edata, LPVOID context)
88 if (code == SERVICE_CONTROL_SHUTDOWN || code == SERVICE_CONTROL_STOP) {
89 SERVICE_STATUS status = {
92 SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN,
99 SCLogInfo(
"Service control handler called with %s control code.",
100 ((code == SERVICE_CONTROL_SHUTDOWN) ? (
"SERVICE_CONTROL_SHUTDOWN") : (
"SERVICE_CONTROL_STOP")));
103 if (!SetServiceStatus(service_status_handle, &status)) {
104 SCLogWarning(
"Can't set service status: %d", (
int)GetLastError());
106 SCLogInfo(
"Service status set to: SERVICE_STOP_PENDING");
115 return ERROR_CALL_NOT_IMPLEMENTED;
121 static void WINAPI SCServiceMain(uint32_t argc,
char** argv)
123 SERVICE_STATUS status = {
126 SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN,
133 if ((service_status_handle = RegisterServiceCtrlHandlerEx((
char *)
PROG_NAME, SCServiceCtrlHandlerEx, NULL)) == (SERVICE_STATUS_HANDLE)0) {
134 SCLogError(
"Can't register service control handler: %d", (
int)GetLastError());
139 if (atexit(SCAtExitHandler)) {
140 SCLogWarning(
"Can't register exit handler: %d", (
int)GetLastError());
144 if (!SetServiceStatus(service_status_handle, &status)) {
145 SCLogWarning(
"Can't set service status: %d", (
int)GetLastError());
147 SCLogInfo(
"Service status set to: SERVICE_RUNNING");
153 main(service_argc, service_argv);
158 status.dwCurrentState = SERVICE_STOPPED;
160 if (!SetServiceStatus(service_status_handle, &status)) {
161 SCLogWarning(
"Can't set service status: %d", (
int)GetLastError());
163 SCLogInfo(
"Service status set to: SERVICE_STOPPED");
173 int SCServiceInit(
int argc,
char **argv)
175 SERVICE_TABLE_ENTRY DispatchTable[] = {
176 {(
char *)
PROG_NAME, (LPSERVICE_MAIN_FUNCTION) SCServiceMain},
181 if (service_initialized) {
190 service_initialized = 1;
192 SCLogInfo(
"Entering service control dispatcher...");
194 if (!StartServiceCtrlDispatcher(DispatchTable)) {
199 SCLogInfo(
"Leaving service control dispatcher.");
211 int SCServiceInstall(
int argc,
char **argv)
214 SC_HANDLE service = NULL;
215 SC_HANDLE scm = NULL;
220 memset(path, 0,
sizeof(path));
222 if (GetModuleFileName(NULL, path, MAX_PATH) == 0 ){
223 SCLogError(
"Can't get path to service binary: %d", (
int)GetLastError());
228 for (i = 1; i < argc; i++) {
229 if ((strlen(argv[i]) <= strlen(
"--service-install")) && (strncmp(
"--service-install", argv[i], strlen(argv[i])) == 0)) {
232 strlcat(path,
" ",
sizeof(path) - strlen(path) - 1);
233 strlcat(path, argv[i],
sizeof(path) - strlen(path) - 1);
236 if ((scm = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS)) == NULL) {
237 SCLogError(
"Can't open SCM: %d", (
int)GetLastError());
241 service = CreateService(
246 SERVICE_WIN32_OWN_PROCESS,
247 SERVICE_DEMAND_START,
248 SERVICE_ERROR_NORMAL,
256 if (service == NULL) {
257 SCLogError(
"Can't create service: %d", (
int)GetLastError());
266 CloseServiceHandle(service);
270 CloseServiceHandle(scm);
282 int SCServiceRemove(
void)
284 SERVICE_STATUS status;
285 SC_HANDLE service = NULL;
286 SC_HANDLE scm = NULL;
290 if ((scm = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS)) == NULL) {
291 SCLogError(
"Can't open SCM: %d", (
int)GetLastError());
295 if ((service = OpenService(scm,
PROG_NAME, SERVICE_ALL_ACCESS)) == NULL) {
296 SCLogError(
"Can't open service: %d", (
int)GetLastError());
300 if (!QueryServiceStatus(service, &status)) {
301 SCLogError(
"Can't query service status: %d", (
int)GetLastError());
305 if (status.dwCurrentState != SERVICE_STOPPED) {
306 SCLogError(
"Service isn't in stopped state: %d", (
int)GetLastError());
310 if (!DeleteService(service)) {
311 SCLogError(
"Can't delete service: %d", (
int)GetLastError());
320 CloseServiceHandle(service);
324 CloseServiceHandle(scm);
336 int SCServiceChangeParams(
int argc,
char **argv)
339 SC_HANDLE service = NULL;
340 SC_HANDLE scm = NULL;
345 memset(path, 0,
sizeof(path));
347 if (GetModuleFileName(NULL, path, MAX_PATH) == 0 ){
348 SCLogError(
"Can't get path to service binary: %d", (
int)GetLastError());
353 for (i = 1; i < argc; i++) {
354 if ((strlen(argv[i]) <= strlen(
"--service-change-params")) && (strncmp(
"--service-change-params", argv[i], strlen(argv[i])) == 0)) {
357 strlcat(path,
" ",
sizeof(path) - strlen(path) - 1);
358 strlcat(path, argv[i],
sizeof(path) - strlen(path) - 1);
361 if ((scm = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS)) == NULL) {
362 SCLogError(
"Can't open SCM: %d", (
int)GetLastError());
366 if ((service = OpenService(scm,
PROG_NAME, SERVICE_ALL_ACCESS)) == NULL) {
367 SCLogError(
"Can't open service: %d", (
int)GetLastError());
371 if (!ChangeServiceConfig(
373 SERVICE_WIN32_OWN_PROCESS,
374 SERVICE_DEMAND_START,
375 SERVICE_ERROR_NORMAL,
384 SCLogError(
"Can't change service configuration: %d", (
int)GetLastError());