[MSIEXEC]
[reactos.git] / reactos / base / system / msiexec / service.c
1 /*
2 * msiexec.exe implementation
3 *
4 * Copyright 2007 Google (James Hawkins)
5 *
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.
10 *
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.
15 *
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
19 */
20
21 #include "precomp.h"
22
23 static SERVICE_STATUS_HANDLE hstatus;
24
25 static HANDLE thread;
26 static HANDLE kill_event;
27
28 static void KillService(void)
29 {
30 WINE_TRACE("Killing service\n");
31 SetEvent(kill_event);
32 }
33
34 static BOOL UpdateSCMStatus(DWORD dwCurrentState, DWORD dwWin32ExitCode,
35 DWORD dwServiceSpecificExitCode)
36 {
37 SERVICE_STATUS status;
38
39 status.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
40 status.dwCurrentState = dwCurrentState;
41
42 if (dwCurrentState == SERVICE_START_PENDING)
43 status.dwControlsAccepted = 0;
44 else
45 {
46 status.dwControlsAccepted = SERVICE_ACCEPT_STOP |
47 SERVICE_ACCEPT_PAUSE_CONTINUE |
48 SERVICE_ACCEPT_SHUTDOWN;
49 }
50
51 if (dwServiceSpecificExitCode == 0)
52 {
53 status.dwWin32ExitCode = dwWin32ExitCode;
54 }
55 else
56 {
57 status.dwWin32ExitCode = ERROR_SERVICE_SPECIFIC_ERROR;
58 }
59
60 status.dwServiceSpecificExitCode = dwServiceSpecificExitCode;
61 status.dwCheckPoint = 0;
62 status.dwWaitHint = 0;
63
64 if (!SetServiceStatus(hstatus, &status))
65 {
66 fprintf(stderr, "Failed to set service status\n");
67 KillService();
68 return FALSE;
69 }
70
71 return TRUE;
72 }
73
74 static void WINAPI ServiceCtrlHandler(DWORD code)
75 {
76 DWORD state = SERVICE_RUNNING;
77
78 WINE_TRACE("%u\n", code);
79
80 switch (code)
81 {
82 case SERVICE_CONTROL_SHUTDOWN:
83 case SERVICE_CONTROL_STOP:
84 UpdateSCMStatus(SERVICE_STOP_PENDING, NO_ERROR, 0);
85 KillService();
86 state = SERVICE_STOPPED;
87 break;
88 default:
89 fprintf(stderr, "Unhandled service control code: %u\n", code);
90 break;
91 }
92
93 UpdateSCMStatus(state, NO_ERROR, 0);
94 }
95
96 static DWORD WINAPI ServiceExecutionThread(LPVOID param)
97 {
98 WaitForSingleObject(kill_event, INFINITE);
99
100 return 0;
101 }
102
103 static BOOL StartServiceThread(void)
104 {
105 DWORD id;
106
107 thread = CreateThread(0, 0, ServiceExecutionThread, 0, 0, &id);
108 if (!thread)
109 {
110 fprintf(stderr, "Failed to create thread\n");
111 return FALSE;
112 }
113
114 return TRUE;
115 }
116
117 static void WINAPI ServiceMain(DWORD argc, LPSTR *argv)
118 {
119 hstatus = RegisterServiceCtrlHandlerA("MSIServer", ServiceCtrlHandler);
120 if (!hstatus)
121 {
122 fprintf(stderr, "Failed to register service ctrl handler\n");
123 return;
124 }
125
126 UpdateSCMStatus(SERVICE_START_PENDING, NO_ERROR, 0);
127
128 kill_event = CreateEventW(0, TRUE, FALSE, 0);
129 if (!kill_event)
130 {
131 fprintf(stderr, "Failed to create event\n");
132 KillService();
133 UpdateSCMStatus(SERVICE_STOPPED, NO_ERROR, 0);
134 return;
135 }
136
137 if (!StartServiceThread())
138 {
139 KillService();
140 UpdateSCMStatus(SERVICE_STOPPED, NO_ERROR, 0);
141 return;
142 }
143
144 UpdateSCMStatus(SERVICE_RUNNING, NO_ERROR, 0);
145
146 WaitForSingleObject(kill_event, INFINITE);
147 KillService();
148
149 UpdateSCMStatus(SERVICE_STOPPED, NO_ERROR, 0);
150 }
151
152 DWORD DoService(void)
153 {
154 char service_name[] = "MSIServer";
155
156 const SERVICE_TABLE_ENTRYA service[] =
157 {
158 {service_name, ServiceMain},
159 {NULL, NULL},
160 };
161
162 WINE_TRACE("Starting MSIServer service\n");
163
164 if (!StartServiceCtrlDispatcherA(service))
165 {
166 fprintf(stderr, "Failed to start MSIServer service\n");
167 return 1;
168 }
169
170 return 0;
171 }