msgsm32.acm: Use standard dlopen() instead of the libwine wrappers.
[wine/zf.git] / programs / services / services.c
blobd9c4e484e425fbe2b8396bcba970c3e359ecfb63
1 /*
2 * Services - controls services keeps track of their state
4 * Copyright 2007 Google (Mikolaj Zalewski)
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 <stdarg.h>
24 #include <stdio.h>
25 #include <assert.h>
26 #include <windows.h>
27 #include <winsvc.h>
28 #include <rpc.h>
29 #include <userenv.h>
30 #include <setupapi.h>
32 #include "wine/debug.h"
33 #include "wine/heap.h"
34 #include "svcctl.h"
36 #include "services.h"
38 #define MAX_SERVICE_NAME 260
40 WINE_DEFAULT_DEBUG_CHANNEL(service);
42 struct scmdatabase *active_database;
44 DWORD service_pipe_timeout = 10000;
45 DWORD service_kill_timeout = 60000;
46 static DWORD default_preshutdown_timeout = 180000;
47 static DWORD autostart_delay = 120000;
48 static void *environment = NULL;
49 static HKEY service_current_key = NULL;
51 static const BOOL is_win64 = (sizeof(void *) > sizeof(int));
53 static const WCHAR SZ_LOCAL_SYSTEM[] = {'L','o','c','a','l','S','y','s','t','e','m',0};
55 /* Registry constants */
56 static const WCHAR SZ_SERVICES_KEY[] = { 'S','y','s','t','e','m','\\',
57 'C','u','r','r','e','n','t','C','o','n','t','r','o','l','S','e','t','\\',
58 'S','e','r','v','i','c','e','s',0 };
60 /* Service key values names */
61 static const WCHAR SZ_DISPLAY_NAME[] = {'D','i','s','p','l','a','y','N','a','m','e',0 };
62 static const WCHAR SZ_TYPE[] = {'T','y','p','e',0 };
63 static const WCHAR SZ_START[] = {'S','t','a','r','t',0 };
64 static const WCHAR SZ_ERROR[] = {'E','r','r','o','r','C','o','n','t','r','o','l',0 };
65 static const WCHAR SZ_IMAGE_PATH[] = {'I','m','a','g','e','P','a','t','h',0};
66 static const WCHAR SZ_GROUP[] = {'G','r','o','u','p',0};
67 static const WCHAR SZ_DEPEND_ON_SERVICE[] = {'D','e','p','e','n','d','O','n','S','e','r','v','i','c','e',0};
68 static const WCHAR SZ_DEPEND_ON_GROUP[] = {'D','e','p','e','n','d','O','n','G','r','o','u','p',0};
69 static const WCHAR SZ_OBJECT_NAME[] = {'O','b','j','e','c','t','N','a','m','e',0};
70 static const WCHAR SZ_TAG[] = {'T','a','g',0};
71 static const WCHAR SZ_DESCRIPTION[] = {'D','e','s','c','r','i','p','t','i','o','n',0};
72 static const WCHAR SZ_PRESHUTDOWN[] = {'P','r','e','s','h','u','t','d','o','w','n','T','i','m','e','o','u','t',0};
73 static const WCHAR SZ_WOW64[] = {'W','O','W','6','4',0};
74 static const WCHAR SZ_DELAYED_AUTOSTART[] = {'D','e','l','a','y','e','d','A','u','t','o','S','t','a','r','t',0};
76 static DWORD process_create(const WCHAR *name, struct process_entry **entry)
78 DWORD err;
80 *entry = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(**entry));
81 if (!*entry)
82 return ERROR_NOT_ENOUGH_SERVER_MEMORY;
83 (*entry)->ref_count = 1;
84 (*entry)->control_mutex = CreateMutexW(NULL, TRUE, NULL);
85 if (!(*entry)->control_mutex)
86 goto error;
87 (*entry)->overlapped_event = CreateEventW(NULL, TRUE, FALSE, NULL);
88 if (!(*entry)->overlapped_event)
89 goto error;
90 (*entry)->control_pipe = CreateNamedPipeW(name, PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED,
91 PIPE_TYPE_BYTE|PIPE_WAIT, 1, 256, 256, 10000, NULL);
92 if ((*entry)->control_pipe == INVALID_HANDLE_VALUE)
93 goto error;
94 /* all other fields are zero */
95 return ERROR_SUCCESS;
97 error:
98 err = GetLastError();
99 if ((*entry)->control_mutex)
100 CloseHandle((*entry)->control_mutex);
101 if ((*entry)->overlapped_event)
102 CloseHandle((*entry)->overlapped_event);
103 HeapFree(GetProcessHeap(), 0, *entry);
104 return err;
107 static void free_process_entry(struct process_entry *entry)
109 CloseHandle(entry->process);
110 CloseHandle(entry->control_mutex);
111 CloseHandle(entry->control_pipe);
112 CloseHandle(entry->overlapped_event);
113 HeapFree(GetProcessHeap(), 0, entry);
116 DWORD service_create(LPCWSTR name, struct service_entry **entry)
118 *entry = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(**entry));
119 if (!*entry)
120 return ERROR_NOT_ENOUGH_SERVER_MEMORY;
121 (*entry)->name = strdupW(name);
122 list_init(&(*entry)->handles);
123 if (!(*entry)->name)
125 HeapFree(GetProcessHeap(), 0, *entry);
126 return ERROR_NOT_ENOUGH_SERVER_MEMORY;
128 (*entry)->status_changed_event = CreateEventW(NULL, TRUE, FALSE, NULL);
129 if (!(*entry)->status_changed_event)
131 HeapFree(GetProcessHeap(), 0, (*entry)->name);
132 HeapFree(GetProcessHeap(), 0, *entry);
133 return GetLastError();
135 (*entry)->ref_count = 1;
136 (*entry)->status.dwCurrentState = SERVICE_STOPPED;
137 (*entry)->status.dwWin32ExitCode = ERROR_SERVICE_NEVER_STARTED;
138 (*entry)->preshutdown_timeout = default_preshutdown_timeout;
139 /* all other fields are zero */
140 return ERROR_SUCCESS;
143 void free_service_entry(struct service_entry *entry)
145 assert(list_empty(&entry->handles));
146 CloseHandle(entry->status_changed_event);
147 HeapFree(GetProcessHeap(), 0, entry->name);
148 HeapFree(GetProcessHeap(), 0, entry->config.lpBinaryPathName);
149 HeapFree(GetProcessHeap(), 0, entry->config.lpDependencies);
150 HeapFree(GetProcessHeap(), 0, entry->config.lpLoadOrderGroup);
151 HeapFree(GetProcessHeap(), 0, entry->config.lpServiceStartName);
152 HeapFree(GetProcessHeap(), 0, entry->config.lpDisplayName);
153 HeapFree(GetProcessHeap(), 0, entry->description);
154 HeapFree(GetProcessHeap(), 0, entry->dependOnServices);
155 HeapFree(GetProcessHeap(), 0, entry->dependOnGroups);
156 if (entry->process) release_process(entry->process);
157 HeapFree(GetProcessHeap(), 0, entry);
160 static DWORD load_service_config(HKEY hKey, struct service_entry *entry)
162 DWORD err, value = 0;
163 WCHAR *wptr;
165 if ((err = load_reg_string(hKey, SZ_IMAGE_PATH, TRUE, &entry->config.lpBinaryPathName)) != 0)
166 return err;
167 if ((err = load_reg_string(hKey, SZ_GROUP, 0, &entry->config.lpLoadOrderGroup)) != 0)
168 return err;
169 if ((err = load_reg_string(hKey, SZ_OBJECT_NAME, TRUE, &entry->config.lpServiceStartName)) != 0)
170 return err;
171 if ((err = load_reg_string(hKey, SZ_DISPLAY_NAME, 0, &entry->config.lpDisplayName)) != 0)
172 return err;
173 if ((err = load_reg_string(hKey, SZ_DESCRIPTION, 0, &entry->description)) != 0)
174 return err;
175 if ((err = load_reg_multisz(hKey, SZ_DEPEND_ON_SERVICE, TRUE, &entry->dependOnServices)) != 0)
176 return err;
177 if ((err = load_reg_multisz(hKey, SZ_DEPEND_ON_GROUP, FALSE, &entry->dependOnGroups)) != 0)
178 return err;
180 if ((err = load_reg_dword(hKey, SZ_TYPE, &entry->config.dwServiceType)) != 0)
181 return err;
182 if ((err = load_reg_dword(hKey, SZ_START, &entry->config.dwStartType)) != 0)
183 return err;
184 if ((err = load_reg_dword(hKey, SZ_ERROR, &entry->config.dwErrorControl)) != 0)
185 return err;
186 if ((err = load_reg_dword(hKey, SZ_TAG, &entry->config.dwTagId)) != 0)
187 return err;
188 if ((err = load_reg_dword(hKey, SZ_PRESHUTDOWN, &entry->preshutdown_timeout)) != 0)
189 return err;
191 if (load_reg_dword(hKey, SZ_WOW64, &value) == 0 && value == 1)
192 entry->is_wow64 = TRUE;
193 if (load_reg_dword(hKey, SZ_DELAYED_AUTOSTART, &value) == 0 && value == 1)
194 entry->delayed_autostart = TRUE;
196 WINE_TRACE("Image path = %s\n", wine_dbgstr_w(entry->config.lpBinaryPathName) );
197 WINE_TRACE("Group = %s\n", wine_dbgstr_w(entry->config.lpLoadOrderGroup) );
198 WINE_TRACE("Service account name = %s\n", wine_dbgstr_w(entry->config.lpServiceStartName) );
199 WINE_TRACE("Display name = %s\n", wine_dbgstr_w(entry->config.lpDisplayName) );
200 WINE_TRACE("Service dependencies : %s\n", entry->dependOnServices[0] ? "" : "(none)");
201 for (wptr = entry->dependOnServices; *wptr; wptr += lstrlenW(wptr) + 1)
202 WINE_TRACE(" * %s\n", wine_dbgstr_w(wptr));
203 WINE_TRACE("Group dependencies : %s\n", entry->dependOnGroups[0] ? "" : "(none)");
204 for (wptr = entry->dependOnGroups; *wptr; wptr += lstrlenW(wptr) + 1)
205 WINE_TRACE(" * %s\n", wine_dbgstr_w(wptr));
207 return ERROR_SUCCESS;
210 static DWORD reg_set_string_value(HKEY hKey, LPCWSTR value_name, LPCWSTR string)
212 if (!string)
214 DWORD err;
215 err = RegDeleteValueW(hKey, value_name);
216 if (err != ERROR_FILE_NOT_FOUND)
217 return err;
219 return ERROR_SUCCESS;
222 return RegSetValueExW(hKey, value_name, 0, REG_SZ, (const BYTE*)string, sizeof(WCHAR)*(lstrlenW(string) + 1));
225 static DWORD reg_set_multisz_value(HKEY hKey, LPCWSTR value_name, LPCWSTR string)
227 const WCHAR *ptr;
229 if (!string)
231 DWORD err;
232 err = RegDeleteValueW(hKey, value_name);
233 if (err != ERROR_FILE_NOT_FOUND)
234 return err;
236 return ERROR_SUCCESS;
239 ptr = string;
240 while (*ptr) ptr += lstrlenW(ptr) + 1;
241 return RegSetValueExW(hKey, value_name, 0, REG_MULTI_SZ, (const BYTE*)string, sizeof(WCHAR)*(ptr - string + 1));
244 DWORD save_service_config(struct service_entry *entry)
246 DWORD err;
247 HKEY hKey = NULL;
249 err = RegCreateKeyW(entry->db->root_key, entry->name, &hKey);
250 if (err != ERROR_SUCCESS)
251 goto cleanup;
253 if ((err = reg_set_string_value(hKey, SZ_DISPLAY_NAME, entry->config.lpDisplayName)) != 0)
254 goto cleanup;
255 if ((err = reg_set_string_value(hKey, SZ_IMAGE_PATH, entry->config.lpBinaryPathName)) != 0)
256 goto cleanup;
257 if ((err = reg_set_string_value(hKey, SZ_GROUP, entry->config.lpLoadOrderGroup)) != 0)
258 goto cleanup;
259 if ((err = reg_set_string_value(hKey, SZ_OBJECT_NAME, entry->config.lpServiceStartName)) != 0)
260 goto cleanup;
261 if ((err = reg_set_string_value(hKey, SZ_DESCRIPTION, entry->description)) != 0)
262 goto cleanup;
263 if ((err = reg_set_multisz_value(hKey, SZ_DEPEND_ON_SERVICE, entry->dependOnServices)) != 0)
264 goto cleanup;
265 if ((err = reg_set_multisz_value(hKey, SZ_DEPEND_ON_GROUP, entry->dependOnGroups)) != 0)
266 goto cleanup;
267 if ((err = RegSetValueExW(hKey, SZ_START, 0, REG_DWORD, (LPBYTE)&entry->config.dwStartType, sizeof(DWORD))) != 0)
268 goto cleanup;
269 if ((err = RegSetValueExW(hKey, SZ_ERROR, 0, REG_DWORD, (LPBYTE)&entry->config.dwErrorControl, sizeof(DWORD))) != 0)
270 goto cleanup;
271 if ((err = RegSetValueExW(hKey, SZ_TYPE, 0, REG_DWORD, (LPBYTE)&entry->config.dwServiceType, sizeof(DWORD))) != 0)
272 goto cleanup;
273 if ((err = RegSetValueExW(hKey, SZ_PRESHUTDOWN, 0, REG_DWORD, (LPBYTE)&entry->preshutdown_timeout, sizeof(DWORD))) != 0)
274 goto cleanup;
275 if ((err = RegSetValueExW(hKey, SZ_PRESHUTDOWN, 0, REG_DWORD, (LPBYTE)&entry->preshutdown_timeout, sizeof(DWORD))) != 0)
276 goto cleanup;
277 if (entry->is_wow64)
279 const DWORD is_wow64 = 1;
280 if ((err = RegSetValueExW(hKey, SZ_WOW64, 0, REG_DWORD, (LPBYTE)&is_wow64, sizeof(DWORD))) != 0)
281 goto cleanup;
284 if (entry->config.dwTagId)
285 err = RegSetValueExW(hKey, SZ_TAG, 0, REG_DWORD, (LPBYTE)&entry->config.dwTagId, sizeof(DWORD));
286 else
287 err = RegDeleteValueW(hKey, SZ_TAG);
289 if (err != 0 && err != ERROR_FILE_NOT_FOUND)
290 goto cleanup;
292 err = ERROR_SUCCESS;
293 cleanup:
294 RegCloseKey(hKey);
295 return err;
298 static void scmdatabase_add_process(struct scmdatabase *db, struct process_entry *process)
300 process->db = db;
301 list_add_tail(&db->processes, &process->entry);
304 static void scmdatabase_remove_process(struct scmdatabase *db, struct process_entry *process)
306 list_remove(&process->entry);
307 process->entry.next = process->entry.prev = NULL;
310 DWORD scmdatabase_add_service(struct scmdatabase *db, struct service_entry *service)
312 int err;
313 service->db = db;
314 if ((err = save_service_config(service)) != ERROR_SUCCESS)
316 WINE_ERR("Couldn't store service configuration: error %u\n", err);
317 return ERROR_GEN_FAILURE;
320 list_add_tail(&db->services, &service->entry);
321 return ERROR_SUCCESS;
324 static void scmdatabase_remove_service(struct scmdatabase *db, struct service_entry *service)
326 RegDeleteTreeW(db->root_key, service->name);
327 list_remove(&service->entry);
328 service->entry.next = service->entry.prev = NULL;
331 static int __cdecl compare_tags(const void *a, const void *b)
333 struct service_entry *service_a = *(struct service_entry **)a;
334 struct service_entry *service_b = *(struct service_entry **)b;
335 return service_a->config.dwTagId - service_b->config.dwTagId;
338 static PTP_CLEANUP_GROUP delayed_autostart_cleanup;
340 struct delayed_autostart_params
342 unsigned int count;
343 struct service_entry **services;
346 static void CALLBACK delayed_autostart_cancel_callback(void *object, void *userdata)
348 struct delayed_autostart_params *params = object;
349 while(params->count--)
350 release_service(params->services[params->count]);
351 heap_free(params->services);
352 heap_free(params);
355 static void CALLBACK delayed_autostart_callback(TP_CALLBACK_INSTANCE *instance, void *context,
356 TP_TIMER *timer)
358 struct delayed_autostart_params *params = context;
359 struct service_entry *service;
360 unsigned int i;
361 DWORD err;
363 scmdatabase_lock_startup(active_database, INFINITE);
365 for (i = 0; i < params->count; i++)
367 service = params->services[i];
368 if (service->status.dwCurrentState == SERVICE_STOPPED)
370 TRACE("Starting delayed auto-start service %s\n", debugstr_w(service->name));
371 err = service_start(service, 0, NULL);
372 if (err != ERROR_SUCCESS)
373 FIXME("Delayed auto-start service %s failed to start: %d\n",
374 wine_dbgstr_w(service->name), err);
376 release_service(service);
379 scmdatabase_unlock_startup(active_database);
381 heap_free(params->services);
382 heap_free(params);
383 CloseThreadpoolTimer(timer);
386 static BOOL schedule_delayed_autostart(struct service_entry **services, unsigned int count)
388 struct delayed_autostart_params *params;
389 TP_CALLBACK_ENVIRON environment;
390 LARGE_INTEGER timestamp;
391 TP_TIMER *timer;
392 FILETIME ft;
394 if (!(delayed_autostart_cleanup = CreateThreadpoolCleanupGroup()))
396 ERR("CreateThreadpoolCleanupGroup failed with error %u\n", GetLastError());
397 return FALSE;
400 if (!(params = heap_alloc(sizeof(*params)))) return FALSE;
401 params->count = count;
402 params->services = services;
404 memset(&environment, 0, sizeof(environment));
405 environment.Version = 1;
406 environment.CleanupGroup = delayed_autostart_cleanup;
407 environment.CleanupGroupCancelCallback = delayed_autostart_cancel_callback;
409 timestamp.QuadPart = (ULONGLONG)autostart_delay * -10000;
410 ft.dwLowDateTime = timestamp.u.LowPart;
411 ft.dwHighDateTime = timestamp.u.HighPart;
413 if (!(timer = CreateThreadpoolTimer(delayed_autostart_callback, params, &environment)))
415 ERR("CreateThreadpoolWait failed: %u\n", GetLastError());
416 heap_free(params);
417 return FALSE;
420 SetThreadpoolTimer(timer, &ft, 0, 0);
421 return TRUE;
424 static BOOL is_root_pnp_service(HDEVINFO set, const struct service_entry *service)
426 SP_DEVINFO_DATA device = {sizeof(device)};
427 WCHAR name[MAX_SERVICE_NAME];
428 unsigned int i;
430 for (i = 0; SetupDiEnumDeviceInfo(set, i, &device); ++i)
432 if (SetupDiGetDeviceRegistryPropertyW(set, &device, SPDRP_SERVICE, NULL,
433 (BYTE *)name, sizeof(name), NULL)
434 && !wcsicmp(name, service->name))
436 return TRUE;
440 return FALSE;
443 static void scmdatabase_autostart_services(struct scmdatabase *db)
445 static const WCHAR rootW[] = {'R','O','O','T',0};
446 struct service_entry **services_list;
447 unsigned int i = 0;
448 unsigned int size = 32;
449 unsigned int delayed_cnt = 0;
450 struct service_entry *service;
451 HDEVINFO set;
453 services_list = HeapAlloc(GetProcessHeap(), 0, size * sizeof(services_list[0]));
454 if (!services_list)
455 return;
457 if ((set = SetupDiGetClassDevsW( NULL, rootW, NULL, DIGCF_ALLCLASSES )) == INVALID_HANDLE_VALUE)
458 WINE_ERR("Failed to enumerate devices, error %#x.\n", GetLastError());
460 scmdatabase_lock(db);
462 LIST_FOR_EACH_ENTRY(service, &db->services, struct service_entry, entry)
464 if (service->config.dwStartType == SERVICE_BOOT_START ||
465 service->config.dwStartType == SERVICE_SYSTEM_START ||
466 service->config.dwStartType == SERVICE_AUTO_START ||
467 (set != INVALID_HANDLE_VALUE && is_root_pnp_service(set, service)))
469 if (i+1 >= size)
471 struct service_entry **slist_new;
472 size *= 2;
473 slist_new = HeapReAlloc(GetProcessHeap(), 0, services_list, size * sizeof(services_list[0]));
474 if (!slist_new)
475 break;
476 services_list = slist_new;
478 services_list[i++] = grab_service(service);
481 size = i;
483 scmdatabase_unlock(db);
484 qsort(services_list, size, sizeof(services_list[0]), compare_tags);
485 scmdatabase_lock_startup(db, INFINITE);
487 for (i = 0; i < size; i++)
489 DWORD err;
490 service = services_list[i];
491 if (service->delayed_autostart)
493 TRACE("delayed starting %s\n", wine_dbgstr_w(service->name));
494 services_list[delayed_cnt++] = service;
495 continue;
497 err = service_start(service, 0, NULL);
498 if (err != ERROR_SUCCESS)
499 WINE_FIXME("Auto-start service %s failed to start: %d\n",
500 wine_dbgstr_w(service->name), err);
501 release_service(service);
504 scmdatabase_unlock_startup(db);
506 if (!delayed_cnt || !schedule_delayed_autostart(services_list, delayed_cnt))
507 heap_free(services_list);
508 SetupDiDestroyDeviceInfoList(set);
511 static void scmdatabase_wait_terminate(struct scmdatabase *db)
513 struct list pending = LIST_INIT(pending);
514 void *ptr;
516 scmdatabase_lock(db);
517 list_move_tail(&pending, &db->processes);
518 while ((ptr = list_head(&pending)))
520 struct process_entry *process = grab_process(LIST_ENTRY(ptr, struct process_entry, entry));
522 scmdatabase_unlock(db);
523 WaitForSingleObject(process->process, INFINITE);
524 scmdatabase_lock(db);
526 list_remove(&process->entry);
527 list_add_tail(&db->processes, &process->entry);
528 release_process(process);
530 scmdatabase_unlock(db);
533 BOOL validate_service_name(LPCWSTR name)
535 return (name && name[0] && !wcschr(name, '/') && !wcschr(name, '\\'));
538 BOOL validate_service_config(struct service_entry *entry)
540 if (entry->config.dwServiceType & SERVICE_WIN32 && (entry->config.lpBinaryPathName == NULL || !entry->config.lpBinaryPathName[0]))
542 WINE_ERR("Service %s is Win32 but has no image path set\n", wine_dbgstr_w(entry->name));
543 return FALSE;
546 switch (entry->config.dwServiceType)
548 case SERVICE_KERNEL_DRIVER:
549 case SERVICE_FILE_SYSTEM_DRIVER:
550 case SERVICE_WIN32_OWN_PROCESS:
551 case SERVICE_WIN32_SHARE_PROCESS:
552 /* No problem */
553 break;
554 case SERVICE_WIN32_OWN_PROCESS | SERVICE_INTERACTIVE_PROCESS:
555 case SERVICE_WIN32_SHARE_PROCESS | SERVICE_INTERACTIVE_PROCESS:
556 /* These can be only run as LocalSystem */
557 if (entry->config.lpServiceStartName && wcsicmp(entry->config.lpServiceStartName, SZ_LOCAL_SYSTEM) != 0)
559 WINE_ERR("Service %s is interactive but has a start name\n", wine_dbgstr_w(entry->name));
560 return FALSE;
562 break;
563 default:
564 WINE_ERR("Service %s has an unknown service type (0x%x)\n", wine_dbgstr_w(entry->name), entry->config.dwServiceType);
565 return FALSE;
568 /* StartType can only be a single value (if several values are mixed the result is probably not what was intended) */
569 if (entry->config.dwStartType > SERVICE_DISABLED)
571 WINE_ERR("Service %s has an unknown start type\n", wine_dbgstr_w(entry->name));
572 return FALSE;
575 /* SERVICE_BOOT_START and SERVICE_SYSTEM_START are only allowed for driver services */
576 if (((entry->config.dwStartType == SERVICE_BOOT_START) || (entry->config.dwStartType == SERVICE_SYSTEM_START)) &&
577 ((entry->config.dwServiceType & SERVICE_WIN32_OWN_PROCESS) || (entry->config.dwServiceType & SERVICE_WIN32_SHARE_PROCESS)))
579 WINE_ERR("Service %s - SERVICE_BOOT_START and SERVICE_SYSTEM_START are only allowed for driver services\n", wine_dbgstr_w(entry->name));
580 return FALSE;
583 if (entry->config.lpServiceStartName == NULL)
584 entry->config.lpServiceStartName = strdupW(SZ_LOCAL_SYSTEM);
586 return TRUE;
590 struct service_entry *scmdatabase_find_service(struct scmdatabase *db, LPCWSTR name)
592 struct service_entry *service;
594 LIST_FOR_EACH_ENTRY(service, &db->services, struct service_entry, entry)
596 if (wcsicmp(name, service->name) == 0)
597 return service;
600 return NULL;
603 struct service_entry *scmdatabase_find_service_by_displayname(struct scmdatabase *db, LPCWSTR name)
605 struct service_entry *service;
607 LIST_FOR_EACH_ENTRY(service, &db->services, struct service_entry, entry)
609 if (service->config.lpDisplayName && wcsicmp(name, service->config.lpDisplayName) == 0)
610 return service;
613 return NULL;
616 struct process_entry *grab_process(struct process_entry *process)
618 if (process)
619 InterlockedIncrement(&process->ref_count);
620 return process;
623 void release_process(struct process_entry *process)
625 struct scmdatabase *db = process->db;
627 scmdatabase_lock(db);
628 if (InterlockedDecrement(&process->ref_count) == 0)
630 scmdatabase_remove_process(db, process);
631 free_process_entry(process);
633 scmdatabase_unlock(db);
636 struct service_entry *grab_service(struct service_entry *service)
638 if (service)
639 InterlockedIncrement(&service->ref_count);
640 return service;
643 void release_service(struct service_entry *service)
645 struct scmdatabase *db = service->db;
647 scmdatabase_lock(db);
648 if (InterlockedDecrement(&service->ref_count) == 0 && is_marked_for_delete(service))
650 scmdatabase_remove_service(db, service);
651 free_service_entry(service);
653 scmdatabase_unlock(db);
656 static DWORD scmdatabase_create(struct scmdatabase **db)
658 DWORD err;
660 *db = HeapAlloc(GetProcessHeap(), 0, sizeof(**db));
661 if (!*db)
662 return ERROR_NOT_ENOUGH_SERVER_MEMORY;
664 (*db)->service_start_lock = FALSE;
665 list_init(&(*db)->processes);
666 list_init(&(*db)->services);
668 InitializeCriticalSection(&(*db)->cs);
669 (*db)->cs.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": scmdatabase");
671 err = RegCreateKeyExW(HKEY_LOCAL_MACHINE, SZ_SERVICES_KEY, 0, NULL,
672 REG_OPTION_NON_VOLATILE, MAXIMUM_ALLOWED, NULL,
673 &(*db)->root_key, NULL);
674 if (err != ERROR_SUCCESS)
675 HeapFree(GetProcessHeap(), 0, *db);
677 return err;
680 static void scmdatabase_destroy(struct scmdatabase *db)
682 RegCloseKey(db->root_key);
683 db->cs.DebugInfo->Spare[0] = 0;
684 DeleteCriticalSection(&db->cs);
685 HeapFree(GetProcessHeap(), 0, db);
688 static DWORD scmdatabase_load_services(struct scmdatabase *db)
690 DWORD err;
691 int i;
693 for (i = 0; TRUE; i++)
695 WCHAR szName[MAX_SERVICE_NAME];
696 struct service_entry *entry;
697 HKEY hServiceKey;
699 err = RegEnumKeyW(db->root_key, i, szName, MAX_SERVICE_NAME);
700 if (err == ERROR_NO_MORE_ITEMS)
701 break;
703 if (err != 0)
705 WINE_ERR("Error %d reading key %d name - skipping\n", err, i);
706 continue;
709 err = service_create(szName, &entry);
710 if (err != ERROR_SUCCESS)
711 break;
713 WINE_TRACE("Loading service %s\n", wine_dbgstr_w(szName));
714 err = RegOpenKeyExW(db->root_key, szName, 0, KEY_READ, &hServiceKey);
715 if (err == ERROR_SUCCESS)
717 err = load_service_config(hServiceKey, entry);
718 RegCloseKey(hServiceKey);
721 if (err != ERROR_SUCCESS)
723 WINE_ERR("Error %d reading registry key for service %s - skipping\n", err, wine_dbgstr_w(szName));
724 free_service_entry(entry);
725 continue;
728 if (entry->config.dwServiceType == 0)
730 /* Maybe an application only wrote some configuration in the service key. Continue silently */
731 WINE_TRACE("Even the service type not set for service %s - skipping\n", wine_dbgstr_w(szName));
732 free_service_entry(entry);
733 continue;
736 if (!validate_service_config(entry))
738 WINE_ERR("Invalid configuration of service %s - skipping\n", wine_dbgstr_w(szName));
739 free_service_entry(entry);
740 continue;
743 entry->status.dwServiceType = entry->config.dwServiceType;
744 entry->db = db;
746 list_add_tail(&db->services, &entry->entry);
747 release_service(entry);
749 return ERROR_SUCCESS;
752 BOOL scmdatabase_lock_startup(struct scmdatabase *db, int timeout)
754 while (InterlockedCompareExchange(&db->service_start_lock, TRUE, FALSE))
756 if (timeout != INFINITE)
758 timeout -= 10;
759 if (timeout <= 0) return FALSE;
761 Sleep(10);
763 return TRUE;
766 void scmdatabase_unlock_startup(struct scmdatabase *db)
768 InterlockedCompareExchange(&db->service_start_lock, FALSE, TRUE);
771 void scmdatabase_lock(struct scmdatabase *db)
773 EnterCriticalSection(&db->cs);
776 void scmdatabase_unlock(struct scmdatabase *db)
778 LeaveCriticalSection(&db->cs);
781 void service_lock(struct service_entry *service)
783 EnterCriticalSection(&service->db->cs);
786 void service_unlock(struct service_entry *service)
788 LeaveCriticalSection(&service->db->cs);
791 /* only one service started at a time, so there is no race on the registry
792 * value here */
793 static LPWSTR service_get_pipe_name(void)
795 static const WCHAR format[] = { '\\','\\','.','\\','p','i','p','e','\\',
796 'n','e','t','\\','N','t','C','o','n','t','r','o','l','P','i','p','e','%','u',0};
797 static WCHAR name[ARRAY_SIZE(format) + 10]; /* lstrlenW("4294967295") */
798 static DWORD service_current = 0;
799 DWORD len, value = -1;
800 LONG ret;
801 DWORD type;
803 len = sizeof(value);
804 ret = RegQueryValueExW(service_current_key, NULL, NULL, &type,
805 (BYTE *)&value, &len);
806 if (ret == ERROR_SUCCESS && type == REG_DWORD)
807 service_current = max(service_current, value + 1);
808 RegSetValueExW(service_current_key, NULL, 0, REG_DWORD,
809 (BYTE *)&service_current, sizeof(service_current));
810 swprintf(name, ARRAY_SIZE(name), format, service_current);
811 service_current++;
812 return name;
815 static DWORD get_service_binary_path(const struct service_entry *service_entry, WCHAR **path)
817 DWORD size = ExpandEnvironmentStringsW(service_entry->config.lpBinaryPathName, NULL, 0);
819 *path = HeapAlloc(GetProcessHeap(), 0, size*sizeof(WCHAR));
820 if (!*path)
821 return ERROR_NOT_ENOUGH_SERVER_MEMORY;
823 ExpandEnvironmentStringsW(service_entry->config.lpBinaryPathName, *path, size);
825 /* if service image is configured to systemdir, redirect it to wow64 systemdir */
826 if (service_entry->is_wow64 && !(service_entry->config.dwServiceType & (SERVICE_FILE_SYSTEM_DRIVER | SERVICE_KERNEL_DRIVER)))
828 WCHAR system_dir[MAX_PATH], *redirected;
829 DWORD len;
831 GetSystemDirectoryW( system_dir, MAX_PATH );
832 len = lstrlenW( system_dir );
834 if (wcsnicmp( system_dir, *path, len ))
835 return ERROR_SUCCESS;
837 GetSystemWow64DirectoryW( system_dir, MAX_PATH );
839 redirected = HeapAlloc( GetProcessHeap(), 0, (lstrlenW( *path ) + lstrlenW( system_dir ))*sizeof(WCHAR));
840 if (!redirected)
842 HeapFree( GetProcessHeap(), 0, *path );
843 return ERROR_NOT_ENOUGH_SERVER_MEMORY;
846 lstrcpyW( redirected, system_dir );
847 lstrcatW( redirected, &(*path)[len] );
848 HeapFree( GetProcessHeap(), 0, *path );
849 *path = redirected;
850 TRACE("redirected to %s\n", debugstr_w(redirected));
853 return ERROR_SUCCESS;
856 static DWORD get_winedevice_binary_path(struct service_entry *service_entry, WCHAR **path, BOOL *is_wow64)
858 static const WCHAR winedeviceW[] = {'\\','w','i','n','e','d','e','v','i','c','e','.','e','x','e',0};
859 WCHAR system_dir[MAX_PATH];
860 DWORD type;
862 if (!is_win64)
863 *is_wow64 = FALSE;
864 else if (GetBinaryTypeW(*path, &type))
865 *is_wow64 = (type == SCS_32BIT_BINARY);
866 else
867 *is_wow64 = service_entry->is_wow64;
869 GetSystemDirectoryW(system_dir, MAX_PATH);
870 HeapFree(GetProcessHeap(), 0, *path);
871 if (!(*path = HeapAlloc(GetProcessHeap(), 0, lstrlenW(system_dir) * sizeof(WCHAR) + sizeof(winedeviceW))))
872 return ERROR_NOT_ENOUGH_SERVER_MEMORY;
874 lstrcpyW(*path, system_dir);
875 lstrcatW(*path, winedeviceW);
876 return ERROR_SUCCESS;
879 static struct process_entry *get_winedevice_process(struct service_entry *service_entry, WCHAR *path, BOOL is_wow64)
881 struct service_entry *winedevice_entry;
883 if (!service_entry->config.lpLoadOrderGroup)
884 return NULL;
886 LIST_FOR_EACH_ENTRY(winedevice_entry, &service_entry->db->services, struct service_entry, entry)
888 if (winedevice_entry->status.dwCurrentState != SERVICE_START_PENDING &&
889 winedevice_entry->status.dwCurrentState != SERVICE_RUNNING) continue;
890 if (!winedevice_entry->process) continue;
892 if (winedevice_entry->is_wow64 != is_wow64) continue;
893 if (!winedevice_entry->config.lpBinaryPathName) continue;
894 if (lstrcmpW(winedevice_entry->config.lpBinaryPathName, path)) continue;
896 if (!winedevice_entry->config.lpLoadOrderGroup) continue;
897 if (lstrcmpW(winedevice_entry->config.lpLoadOrderGroup, service_entry->config.lpLoadOrderGroup)) continue;
899 return grab_process(winedevice_entry->process);
902 return NULL;
905 static DWORD add_winedevice_service(const struct service_entry *service, WCHAR *path, BOOL is_wow64,
906 struct service_entry **entry)
908 static const WCHAR format[] = {'W','i','n','e','d','e','v','i','c','e','%','u',0};
909 static WCHAR name[ARRAY_SIZE(format) + 10]; /* lstrlenW("4294967295") */
910 static DWORD current = 0;
911 struct scmdatabase *db = service->db;
912 DWORD err;
914 for (;;)
916 swprintf(name, ARRAY_SIZE(name), format, ++current);
917 if (!scmdatabase_find_service(db, name)) break;
920 err = service_create(name, entry);
921 if (err != ERROR_SUCCESS)
922 return err;
924 (*entry)->is_wow64 = is_wow64;
925 (*entry)->config.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
926 (*entry)->config.dwStartType = SERVICE_DEMAND_START;
927 (*entry)->status.dwServiceType = (*entry)->config.dwServiceType;
929 if (!((*entry)->config.lpBinaryPathName = strdupW(path)))
930 goto error;
931 if (!((*entry)->config.lpServiceStartName = strdupW(SZ_LOCAL_SYSTEM)))
932 goto error;
933 if (!((*entry)->config.lpDisplayName = strdupW(name)))
934 goto error;
935 if (service->config.lpLoadOrderGroup &&
936 !((*entry)->config.lpLoadOrderGroup = strdupW(service->config.lpLoadOrderGroup)))
937 goto error;
939 (*entry)->db = db;
941 list_add_tail(&db->services, &(*entry)->entry);
942 mark_for_delete(*entry);
943 return ERROR_SUCCESS;
945 error:
946 free_service_entry(*entry);
947 return ERROR_NOT_ENOUGH_SERVER_MEMORY;
950 static DWORD service_start_process(struct service_entry *service_entry, struct process_entry **new_process,
951 BOOL *shared_process)
953 struct process_entry *process;
954 PROCESS_INFORMATION pi;
955 STARTUPINFOW si;
956 BOOL is_wow64 = FALSE;
957 HANDLE token;
958 WCHAR *path;
959 DWORD err;
960 BOOL r;
962 service_lock(service_entry);
964 if ((process = service_entry->process))
966 if (WaitForSingleObject(process->process, 0) == WAIT_TIMEOUT)
968 service_unlock(service_entry);
969 return ERROR_SERVICE_ALREADY_RUNNING;
971 service_entry->process = NULL;
972 process->use_count--;
973 release_process(process);
976 service_entry->force_shutdown = FALSE;
978 if ((err = get_service_binary_path(service_entry, &path)))
980 service_unlock(service_entry);
981 return err;
984 if (service_entry->config.dwServiceType == SERVICE_KERNEL_DRIVER ||
985 service_entry->config.dwServiceType == SERVICE_FILE_SYSTEM_DRIVER)
987 struct service_entry *winedevice_entry;
988 WCHAR *group;
990 if ((err = get_winedevice_binary_path(service_entry, &path, &is_wow64)))
992 service_unlock(service_entry);
993 HeapFree(GetProcessHeap(), 0, path);
994 return err;
997 if ((process = get_winedevice_process(service_entry, path, is_wow64)))
999 HeapFree(GetProcessHeap(), 0, path);
1000 goto found;
1003 err = add_winedevice_service(service_entry, path, is_wow64, &winedevice_entry);
1004 HeapFree(GetProcessHeap(), 0, path);
1005 if (err != ERROR_SUCCESS)
1007 service_unlock(service_entry);
1008 return err;
1011 group = strdupW(winedevice_entry->config.lpLoadOrderGroup);
1012 service_unlock(service_entry);
1014 err = service_start(winedevice_entry, group != NULL, (const WCHAR **)&group);
1015 HeapFree(GetProcessHeap(), 0, group);
1016 if (err != ERROR_SUCCESS)
1018 release_service(winedevice_entry);
1019 return err;
1022 service_lock(service_entry);
1023 process = grab_process(winedevice_entry->process);
1024 release_service(winedevice_entry);
1026 if (!process)
1028 service_unlock(service_entry);
1029 return ERROR_SERVICE_REQUEST_TIMEOUT;
1032 found:
1033 service_entry->status.dwCurrentState = SERVICE_START_PENDING;
1034 service_entry->status.dwControlsAccepted = 0;
1035 ResetEvent(service_entry->status_changed_event);
1037 service_entry->process = grab_process(process);
1038 service_entry->shared_process = *shared_process = TRUE;
1039 process->use_count++;
1040 service_unlock(service_entry);
1042 err = WaitForSingleObject(process->control_mutex, 30000);
1043 if (err != WAIT_OBJECT_0)
1045 release_process(process);
1046 return ERROR_SERVICE_REQUEST_TIMEOUT;
1049 *new_process = process;
1050 return ERROR_SUCCESS;
1053 if ((err = process_create(service_get_pipe_name(), &process)))
1055 WINE_ERR("failed to create process object for %s, error = %u\n",
1056 wine_dbgstr_w(service_entry->name), err);
1057 service_unlock(service_entry);
1058 HeapFree(GetProcessHeap(), 0, path);
1059 return err;
1062 ZeroMemory(&si, sizeof(STARTUPINFOW));
1063 si.cb = sizeof(STARTUPINFOW);
1064 if (!(service_entry->config.dwServiceType & SERVICE_INTERACTIVE_PROCESS))
1066 static WCHAR desktopW[] = {'_','_','w','i','n','e','s','e','r','v','i','c','e','_','w','i','n','s','t','a','t','i','o','n','\\','D','e','f','a','u','l','t',0};
1067 si.lpDesktop = desktopW;
1070 if (!environment && OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY | TOKEN_DUPLICATE, &token))
1072 CreateEnvironmentBlock(&environment, token, FALSE);
1073 CloseHandle(token);
1076 service_entry->status.dwCurrentState = SERVICE_START_PENDING;
1077 service_entry->status.dwControlsAccepted = 0;
1078 ResetEvent(service_entry->status_changed_event);
1080 scmdatabase_add_process(service_entry->db, process);
1081 service_entry->process = grab_process(process);
1082 service_entry->shared_process = *shared_process = FALSE;
1083 process->use_count++;
1084 service_unlock(service_entry);
1086 r = CreateProcessW(NULL, path, NULL, NULL, FALSE, CREATE_UNICODE_ENVIRONMENT, environment, NULL, &si, &pi);
1087 HeapFree(GetProcessHeap(), 0, path);
1088 if (!r)
1090 err = GetLastError();
1091 process_terminate(process);
1092 release_process(process);
1093 return err;
1096 process->process_id = pi.dwProcessId;
1097 process->process = pi.hProcess;
1098 CloseHandle( pi.hThread );
1100 *new_process = process;
1101 return ERROR_SUCCESS;
1104 static DWORD service_wait_for_startup(struct service_entry *service, struct process_entry *process)
1106 HANDLE handles[2] = { service->status_changed_event, process->process };
1107 DWORD result;
1109 result = WaitForMultipleObjects( 2, handles, FALSE, service_pipe_timeout );
1110 if (result != WAIT_OBJECT_0)
1111 return ERROR_SERVICE_REQUEST_TIMEOUT;
1113 service_lock(service);
1114 result = service->status.dwCurrentState;
1115 service_unlock(service);
1117 return (result == SERVICE_START_PENDING || result == SERVICE_RUNNING) ?
1118 ERROR_SUCCESS : ERROR_SERVICE_REQUEST_TIMEOUT;
1121 /******************************************************************************
1122 * process_send_start_message
1124 static DWORD process_send_start_message(struct process_entry *process, BOOL shared_process,
1125 const WCHAR *name, const WCHAR **argv, DWORD argc)
1127 OVERLAPPED overlapped;
1128 DWORD i, len, result;
1129 WCHAR *str, *p;
1131 WINE_TRACE("%p %s %p %d\n", process, wine_dbgstr_w(name), argv, argc);
1133 overlapped.hEvent = process->overlapped_event;
1134 if (!ConnectNamedPipe(process->control_pipe, &overlapped))
1136 if (GetLastError() == ERROR_IO_PENDING)
1138 HANDLE handles[2];
1139 handles[0] = process->overlapped_event;
1140 handles[1] = process->process;
1141 if (WaitForMultipleObjects( 2, handles, FALSE, service_pipe_timeout ) != WAIT_OBJECT_0)
1142 CancelIo(process->control_pipe);
1143 if (!HasOverlappedIoCompleted( &overlapped ))
1145 WINE_ERR("service %s failed to start\n", wine_dbgstr_w(name));
1146 return ERROR_SERVICE_REQUEST_TIMEOUT;
1149 else if (GetLastError() != ERROR_PIPE_CONNECTED)
1151 WINE_ERR("pipe connect failed\n");
1152 return ERROR_SERVICE_REQUEST_TIMEOUT;
1156 len = lstrlenW(name) + 1;
1157 for (i = 0; i < argc; i++)
1158 len += lstrlenW(argv[i])+1;
1159 len = (len + 1) * sizeof(WCHAR);
1161 if (!(str = HeapAlloc(GetProcessHeap(), 0, len)))
1162 return ERROR_NOT_ENOUGH_SERVER_MEMORY;
1164 p = str;
1165 lstrcpyW(p, name);
1166 p += lstrlenW(name) + 1;
1167 for (i = 0; i < argc; i++)
1169 lstrcpyW(p, argv[i]);
1170 p += lstrlenW(p) + 1;
1172 *p = 0;
1174 if (!process_send_control(process, shared_process, name,
1175 SERVICE_CONTROL_START, (const BYTE *)str, len, &result))
1176 result = ERROR_SERVICE_REQUEST_TIMEOUT;
1178 HeapFree(GetProcessHeap(), 0, str);
1179 return result;
1182 DWORD service_start(struct service_entry *service, DWORD service_argc, LPCWSTR *service_argv)
1184 struct process_entry *process = NULL;
1185 BOOL shared_process;
1186 DWORD err;
1188 err = service_start_process(service, &process, &shared_process);
1189 if (err == ERROR_SUCCESS)
1191 err = process_send_start_message(process, shared_process, service->name, service_argv, service_argc);
1193 if (err == ERROR_SUCCESS)
1194 err = service_wait_for_startup(service, process);
1196 if (err != ERROR_SUCCESS)
1198 service_lock(service);
1199 if (service->process)
1201 service->status.dwCurrentState = SERVICE_STOPPED;
1202 service->process = NULL;
1203 if (!--process->use_count) process_terminate(process);
1204 release_process(process);
1206 service_unlock(service);
1209 ReleaseMutex(process->control_mutex);
1210 release_process(process);
1213 WINE_TRACE("returning %d\n", err);
1214 return err;
1217 void process_terminate(struct process_entry *process)
1219 struct scmdatabase *db = process->db;
1220 struct service_entry *service;
1222 scmdatabase_lock(db);
1223 TerminateProcess(process->process, 0);
1224 LIST_FOR_EACH_ENTRY(service, &db->services, struct service_entry, entry)
1226 if (service->process != process) continue;
1227 service->status.dwCurrentState = SERVICE_STOPPED;
1228 service->process = NULL;
1229 process->use_count--;
1230 release_process(process);
1232 scmdatabase_unlock(db);
1235 static void load_registry_parameters(void)
1237 static const WCHAR controlW[] =
1238 { 'S','y','s','t','e','m','\\',
1239 'C','u','r','r','e','n','t','C','o','n','t','r','o','l','S','e','t','\\',
1240 'C','o','n','t','r','o','l',0 };
1241 static const WCHAR pipetimeoutW[] =
1242 {'S','e','r','v','i','c','e','s','P','i','p','e','T','i','m','e','o','u','t',0};
1243 static const WCHAR killtimeoutW[] =
1244 {'W','a','i','t','T','o','K','i','l','l','S','e','r','v','i','c','e','T','i','m','e','o','u','t',0};
1245 static const WCHAR autostartdelayW[] =
1246 {'A','u','t','o','S','t','a','r','t','D','e','l','a','y',0};
1247 HKEY key;
1248 WCHAR buffer[64];
1249 DWORD type, count, val;
1251 if (RegOpenKeyW( HKEY_LOCAL_MACHINE, controlW, &key )) return;
1253 count = sizeof(buffer);
1254 if (!RegQueryValueExW( key, pipetimeoutW, NULL, &type, (BYTE *)buffer, &count ) &&
1255 type == REG_SZ && (val = wcstol( buffer, NULL, 10 )))
1256 service_pipe_timeout = val;
1258 count = sizeof(buffer);
1259 if (!RegQueryValueExW( key, killtimeoutW, NULL, &type, (BYTE *)buffer, &count ) &&
1260 type == REG_SZ && (val = wcstol( buffer, NULL, 10 )))
1261 service_kill_timeout = val;
1263 count = sizeof(val);
1264 if (!RegQueryValueExW( key, autostartdelayW, NULL, &type, (BYTE *)&val, &count ) && type == REG_DWORD)
1265 autostart_delay = val;
1267 RegCloseKey( key );
1270 int __cdecl main(int argc, char *argv[])
1272 static const WCHAR service_current_key_str[] = { 'S','Y','S','T','E','M','\\',
1273 'C','u','r','r','e','n','t','C','o','n','t','r','o','l','S','e','t','\\',
1274 'C','o','n','t','r','o','l','\\',
1275 'S','e','r','v','i','c','e','C','u','r','r','e','n','t',0};
1276 static const WCHAR svcctl_started_event[] = SVCCTL_STARTED_EVENT;
1277 HANDLE started_event;
1278 DWORD err;
1280 started_event = CreateEventW(NULL, TRUE, FALSE, svcctl_started_event);
1282 err = RegCreateKeyExW(HKEY_LOCAL_MACHINE, service_current_key_str, 0,
1283 NULL, REG_OPTION_VOLATILE, KEY_SET_VALUE | KEY_QUERY_VALUE, NULL,
1284 &service_current_key, NULL);
1285 if (err != ERROR_SUCCESS)
1286 return err;
1288 load_registry_parameters();
1289 err = scmdatabase_create(&active_database);
1290 if (err != ERROR_SUCCESS)
1291 return err;
1292 if ((err = scmdatabase_load_services(active_database)) != ERROR_SUCCESS)
1293 return err;
1294 if ((err = RPC_Init()) == ERROR_SUCCESS)
1296 scmdatabase_autostart_services(active_database);
1297 SetEvent(started_event);
1298 WaitForSingleObject(exit_event, INFINITE);
1299 scmdatabase_wait_terminate(active_database);
1300 if (delayed_autostart_cleanup)
1302 CloseThreadpoolCleanupGroupMembers(delayed_autostart_cleanup, TRUE, NULL);
1303 CloseThreadpoolCleanupGroup(delayed_autostart_cleanup);
1305 RPC_Stop();
1307 scmdatabase_destroy(active_database);
1308 if (environment)
1309 DestroyEnvironmentBlock(environment);
1311 WINE_TRACE("services.exe exited with code %d\n", err);
1312 return err;