Release 1.3.2.
[wine/gsoc-2012-control.git] / programs / msiexec / service.c
blob3ccdb5bb58b8763f96da494f7eac0bfcdcb801c4
1 /*
2 * msiexec.exe implementation
4 * Copyright 2007 Google (James Hawkins)
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #define WIN32_LEAN_AND_MEAN
23 #include <windows.h>
24 #include <stdio.h>
26 #include "wine/debug.h"
28 WINE_DEFAULT_DEBUG_CHANNEL(msiexec);
30 static SERVICE_STATUS_HANDLE hstatus;
32 static HANDLE thread;
33 static HANDLE kill_event;
35 static void KillService(void)
37 WINE_TRACE("Killing service\n");
38 SetEvent(kill_event);
41 static BOOL UpdateSCMStatus(DWORD dwCurrentState, DWORD dwWin32ExitCode,
42 DWORD dwServiceSpecificExitCode)
44 SERVICE_STATUS status;
46 status.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
47 status.dwCurrentState = dwCurrentState;
49 if (dwCurrentState == SERVICE_START_PENDING)
50 status.dwControlsAccepted = 0;
51 else
53 status.dwControlsAccepted = SERVICE_ACCEPT_STOP |
54 SERVICE_ACCEPT_PAUSE_CONTINUE |
55 SERVICE_ACCEPT_SHUTDOWN;
58 if (dwServiceSpecificExitCode == 0)
60 status.dwWin32ExitCode = dwWin32ExitCode;
62 else
64 status.dwWin32ExitCode = ERROR_SERVICE_SPECIFIC_ERROR;
67 status.dwServiceSpecificExitCode = dwServiceSpecificExitCode;
68 status.dwCheckPoint = 0;
69 status.dwWaitHint = 0;
71 if (!SetServiceStatus(hstatus, &status))
73 fprintf(stderr, "Failed to set service status\n");
74 KillService();
75 return FALSE;
78 return TRUE;
81 static void WINAPI ServiceCtrlHandler(DWORD code)
83 WINE_TRACE("%d\n", code);
85 switch (code)
87 case SERVICE_CONTROL_SHUTDOWN:
88 case SERVICE_CONTROL_STOP:
89 UpdateSCMStatus(SERVICE_STOP_PENDING, NO_ERROR, 0);
90 KillService();
91 return;
92 default:
93 fprintf(stderr, "Unhandled service control code: %d\n", code);
94 break;
97 UpdateSCMStatus(SERVICE_RUNNING, NO_ERROR, 0);
100 static DWORD WINAPI ServiceExecutionThread(LPVOID param)
102 while (TRUE)
104 /* do nothing */
107 return 0;
110 static BOOL StartServiceThread(void)
112 DWORD id;
114 thread = CreateThread(0, 0, ServiceExecutionThread, 0, 0, &id);
115 if (!thread)
117 fprintf(stderr, "Failed to create thread\n");
118 return FALSE;
121 return TRUE;
124 static void WINAPI ServiceMain(DWORD argc, LPSTR *argv)
126 hstatus = RegisterServiceCtrlHandlerA("MSIServer", ServiceCtrlHandler);
127 if (!hstatus)
129 fprintf(stderr, "Failed to register service ctrl handler\n");
130 return;
133 UpdateSCMStatus(SERVICE_START_PENDING, NO_ERROR, 0);
135 kill_event = CreateEventW(0, TRUE, FALSE, 0);
136 if (!kill_event)
138 fprintf(stderr, "Failed to create event\n");
139 KillService();
140 return;
143 if (!StartServiceThread())
145 KillService();
146 return;
149 UpdateSCMStatus(SERVICE_RUNNING, NO_ERROR, 0);
151 WaitForSingleObject(kill_event, INFINITE);
152 KillService();
155 DWORD DoService(void)
157 char service_name[] = "MSIServer";
159 const SERVICE_TABLE_ENTRYA service[] =
161 {service_name, ServiceMain},
162 {NULL, NULL},
165 WINE_TRACE("Starting MSIServer service\n");
167 if (!StartServiceCtrlDispatcherA(service))
169 fprintf(stderr, "Failed to start MSIServer service\n");
170 return 1;
173 return 0;