gdiplus: In GdipImageSelectActiveFrame rely on codec->select_func() to fail.
[wine.git] / programs / services / services.c
blobb45022eaec866e7baecfebf9d56c7cb73eaa3f12
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 <winternl.h>
29 #include <rpc.h>
30 #include <userenv.h>
31 #include <setupapi.h>
33 #include "wine/debug.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;
50 static HANDLE job_object, job_completion_port;
52 static const BOOL is_win64 = (sizeof(void *) > sizeof(int));
54 static const WCHAR SZ_LOCAL_SYSTEM[] = {'L','o','c','a','l','S','y','s','t','e','m',0};
56 /* Registry constants */
57 static const WCHAR SZ_SERVICES_KEY[] = { 'S','y','s','t','e','m','\\',
58 'C','u','r','r','e','n','t','C','o','n','t','r','o','l','S','e','t','\\',
59 'S','e','r','v','i','c','e','s',0 };
61 /* Service key values names */
62 static const WCHAR SZ_DISPLAY_NAME[] = {'D','i','s','p','l','a','y','N','a','m','e',0 };
63 static const WCHAR SZ_TYPE[] = {'T','y','p','e',0 };
64 static const WCHAR SZ_START[] = {'S','t','a','r','t',0 };
65 static const WCHAR SZ_ERROR[] = {'E','r','r','o','r','C','o','n','t','r','o','l',0 };
66 static const WCHAR SZ_IMAGE_PATH[] = {'I','m','a','g','e','P','a','t','h',0};
67 static const WCHAR SZ_GROUP[] = {'G','r','o','u','p',0};
68 static const WCHAR SZ_DEPEND_ON_SERVICE[] = {'D','e','p','e','n','d','O','n','S','e','r','v','i','c','e',0};
69 static const WCHAR SZ_DEPEND_ON_GROUP[] = {'D','e','p','e','n','d','O','n','G','r','o','u','p',0};
70 static const WCHAR SZ_OBJECT_NAME[] = {'O','b','j','e','c','t','N','a','m','e',0};
71 static const WCHAR SZ_TAG[] = {'T','a','g',0};
72 static const WCHAR SZ_DESCRIPTION[] = {'D','e','s','c','r','i','p','t','i','o','n',0};
73 static const WCHAR SZ_PRESHUTDOWN[] = {'P','r','e','s','h','u','t','d','o','w','n','T','i','m','e','o','u','t',0};
74 static const WCHAR SZ_WOW64[] = {'W','O','W','6','4',0};
75 static const WCHAR SZ_DELAYED_AUTOSTART[] = {'D','e','l','a','y','e','d','A','u','t','o','S','t','a','r','t',0};
77 static DWORD process_create(const WCHAR *name, struct process_entry **entry)
79 DWORD err;
81 *entry = calloc(1, sizeof(**entry));
82 if (!*entry)
83 return ERROR_NOT_ENOUGH_SERVER_MEMORY;
84 (*entry)->ref_count = 1;
85 (*entry)->control_mutex = CreateMutexW(NULL, TRUE, NULL);
86 if (!(*entry)->control_mutex)
87 goto error;
88 (*entry)->overlapped_event = CreateEventW(NULL, TRUE, FALSE, NULL);
89 if (!(*entry)->overlapped_event)
90 goto error;
91 (*entry)->control_pipe = CreateNamedPipeW(name, PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED,
92 PIPE_TYPE_BYTE|PIPE_WAIT, 1, 256, 256, 10000, NULL);
93 if ((*entry)->control_pipe == INVALID_HANDLE_VALUE)
94 goto error;
95 /* all other fields are zero */
96 return ERROR_SUCCESS;
98 error:
99 err = GetLastError();
100 if ((*entry)->control_mutex)
101 CloseHandle((*entry)->control_mutex);
102 if ((*entry)->overlapped_event)
103 CloseHandle((*entry)->overlapped_event);
104 free(*entry);
105 return err;
108 static void free_process_entry(struct process_entry *entry)
110 CloseHandle(entry->process);
111 CloseHandle(entry->control_mutex);
112 CloseHandle(entry->control_pipe);
113 CloseHandle(entry->overlapped_event);
114 free(entry);
117 DWORD service_create(LPCWSTR name, struct service_entry **entry)
119 *entry = calloc(1, sizeof(**entry));
120 if (!*entry)
121 return ERROR_NOT_ENOUGH_SERVER_MEMORY;
122 (*entry)->name = wcsdup(name);
123 list_init(&(*entry)->handles);
124 if (!(*entry)->name)
126 free(*entry);
127 return ERROR_NOT_ENOUGH_SERVER_MEMORY;
129 (*entry)->status_changed_event = CreateEventW(NULL, TRUE, FALSE, NULL);
130 if (!(*entry)->status_changed_event)
132 free((*entry)->name);
133 free(*entry);
134 return GetLastError();
136 (*entry)->ref_count = 1;
137 (*entry)->status.dwCurrentState = SERVICE_STOPPED;
138 (*entry)->status.dwWin32ExitCode = ERROR_SERVICE_NEVER_STARTED;
139 (*entry)->preshutdown_timeout = default_preshutdown_timeout;
140 /* all other fields are zero */
141 return ERROR_SUCCESS;
144 void free_service_entry(struct service_entry *entry)
146 assert(list_empty(&entry->handles));
147 CloseHandle(entry->status_changed_event);
148 free(entry->name);
149 free(entry->config.lpBinaryPathName);
150 free(entry->config.lpDependencies);
151 free(entry->config.lpLoadOrderGroup);
152 free(entry->config.lpServiceStartName);
153 free(entry->config.lpDisplayName);
154 free(entry->description);
155 free(entry->dependOnServices);
156 free(entry->dependOnGroups);
157 if (entry->process) release_process(entry->process);
158 free(entry);
161 static DWORD load_service_config(HKEY hKey, struct service_entry *entry)
163 DWORD err, value = 0;
164 WCHAR *wptr;
166 if ((err = load_reg_string(hKey, SZ_IMAGE_PATH, TRUE, &entry->config.lpBinaryPathName)) != 0)
167 return err;
168 if ((err = load_reg_string(hKey, SZ_GROUP, 0, &entry->config.lpLoadOrderGroup)) != 0)
169 return err;
170 if ((err = load_reg_string(hKey, SZ_OBJECT_NAME, TRUE, &entry->config.lpServiceStartName)) != 0)
171 return err;
172 if ((err = load_reg_string(hKey, SZ_DISPLAY_NAME, 0, &entry->config.lpDisplayName)) != 0)
173 return err;
174 if ((err = load_reg_string(hKey, SZ_DESCRIPTION, 0, &entry->description)) != 0)
175 return err;
176 if ((err = load_reg_multisz(hKey, SZ_DEPEND_ON_SERVICE, TRUE, &entry->dependOnServices)) != 0)
177 return err;
178 if ((err = load_reg_multisz(hKey, SZ_DEPEND_ON_GROUP, FALSE, &entry->dependOnGroups)) != 0)
179 return err;
181 if ((err = load_reg_dword(hKey, SZ_TYPE, &entry->config.dwServiceType)) != 0)
182 return err;
183 if ((err = load_reg_dword(hKey, SZ_START, &entry->config.dwStartType)) != 0)
184 return err;
185 if ((err = load_reg_dword(hKey, SZ_ERROR, &entry->config.dwErrorControl)) != 0)
186 return err;
187 if ((err = load_reg_dword(hKey, SZ_TAG, &entry->config.dwTagId)) != 0)
188 return err;
189 if ((err = load_reg_dword(hKey, SZ_PRESHUTDOWN, &entry->preshutdown_timeout)) != 0)
190 return err;
192 if (load_reg_dword(hKey, SZ_WOW64, &value) == 0 && value == 1)
193 entry->is_wow64 = TRUE;
194 if (load_reg_dword(hKey, SZ_DELAYED_AUTOSTART, &value) == 0 && value == 1)
195 entry->delayed_autostart = TRUE;
197 WINE_TRACE("Image path = %s\n", wine_dbgstr_w(entry->config.lpBinaryPathName) );
198 WINE_TRACE("Group = %s\n", wine_dbgstr_w(entry->config.lpLoadOrderGroup) );
199 WINE_TRACE("Service account name = %s\n", wine_dbgstr_w(entry->config.lpServiceStartName) );
200 WINE_TRACE("Display name = %s\n", wine_dbgstr_w(entry->config.lpDisplayName) );
201 WINE_TRACE("Service dependencies : %s\n", entry->dependOnServices[0] ? "" : "(none)");
202 for (wptr = entry->dependOnServices; *wptr; wptr += lstrlenW(wptr) + 1)
203 WINE_TRACE(" * %s\n", wine_dbgstr_w(wptr));
204 WINE_TRACE("Group dependencies : %s\n", entry->dependOnGroups[0] ? "" : "(none)");
205 for (wptr = entry->dependOnGroups; *wptr; wptr += lstrlenW(wptr) + 1)
206 WINE_TRACE(" * %s\n", wine_dbgstr_w(wptr));
208 return ERROR_SUCCESS;
211 static DWORD reg_set_string_value(HKEY hKey, LPCWSTR value_name, LPCWSTR string)
213 if (!string)
215 DWORD err;
216 err = RegDeleteValueW(hKey, value_name);
217 if (err != ERROR_FILE_NOT_FOUND)
218 return err;
220 return ERROR_SUCCESS;
223 return RegSetValueExW(hKey, value_name, 0, REG_SZ, (const BYTE*)string, sizeof(WCHAR)*(lstrlenW(string) + 1));
226 static DWORD reg_set_multisz_value(HKEY hKey, LPCWSTR value_name, LPCWSTR string)
228 const WCHAR *ptr;
230 if (!string)
232 DWORD err;
233 err = RegDeleteValueW(hKey, value_name);
234 if (err != ERROR_FILE_NOT_FOUND)
235 return err;
237 return ERROR_SUCCESS;
240 ptr = string;
241 while (*ptr) ptr += lstrlenW(ptr) + 1;
242 return RegSetValueExW(hKey, value_name, 0, REG_MULTI_SZ, (const BYTE*)string, sizeof(WCHAR)*(ptr - string + 1));
245 DWORD save_service_config(struct service_entry *entry)
247 DWORD err;
248 HKEY hKey = NULL;
250 err = RegCreateKeyW(entry->db->root_key, entry->name, &hKey);
251 if (err != ERROR_SUCCESS)
252 goto cleanup;
254 if ((err = reg_set_string_value(hKey, SZ_DISPLAY_NAME, entry->config.lpDisplayName)) != 0)
255 goto cleanup;
256 if ((err = reg_set_string_value(hKey, SZ_IMAGE_PATH, entry->config.lpBinaryPathName)) != 0)
257 goto cleanup;
258 if ((err = reg_set_string_value(hKey, SZ_GROUP, entry->config.lpLoadOrderGroup)) != 0)
259 goto cleanup;
260 if ((err = reg_set_string_value(hKey, SZ_OBJECT_NAME, entry->config.lpServiceStartName)) != 0)
261 goto cleanup;
262 if ((err = reg_set_string_value(hKey, SZ_DESCRIPTION, entry->description)) != 0)
263 goto cleanup;
264 if ((err = reg_set_multisz_value(hKey, SZ_DEPEND_ON_SERVICE, entry->dependOnServices)) != 0)
265 goto cleanup;
266 if ((err = reg_set_multisz_value(hKey, SZ_DEPEND_ON_GROUP, entry->dependOnGroups)) != 0)
267 goto cleanup;
268 if ((err = RegSetValueExW(hKey, SZ_START, 0, REG_DWORD, (LPBYTE)&entry->config.dwStartType, sizeof(DWORD))) != 0)
269 goto cleanup;
270 if ((err = RegSetValueExW(hKey, SZ_ERROR, 0, REG_DWORD, (LPBYTE)&entry->config.dwErrorControl, sizeof(DWORD))) != 0)
271 goto cleanup;
272 if ((err = RegSetValueExW(hKey, SZ_TYPE, 0, REG_DWORD, (LPBYTE)&entry->config.dwServiceType, sizeof(DWORD))) != 0)
273 goto cleanup;
274 if ((err = RegSetValueExW(hKey, SZ_PRESHUTDOWN, 0, REG_DWORD, (LPBYTE)&entry->preshutdown_timeout, sizeof(DWORD))) != 0)
275 goto cleanup;
276 if ((err = RegSetValueExW(hKey, SZ_PRESHUTDOWN, 0, REG_DWORD, (LPBYTE)&entry->preshutdown_timeout, sizeof(DWORD))) != 0)
277 goto cleanup;
278 if (entry->is_wow64)
280 const DWORD is_wow64 = 1;
281 if ((err = RegSetValueExW(hKey, SZ_WOW64, 0, REG_DWORD, (LPBYTE)&is_wow64, sizeof(DWORD))) != 0)
282 goto cleanup;
285 if (entry->config.dwTagId)
286 err = RegSetValueExW(hKey, SZ_TAG, 0, REG_DWORD, (LPBYTE)&entry->config.dwTagId, sizeof(DWORD));
287 else
288 err = RegDeleteValueW(hKey, SZ_TAG);
290 if (err != 0 && err != ERROR_FILE_NOT_FOUND)
291 goto cleanup;
293 err = ERROR_SUCCESS;
294 cleanup:
295 RegCloseKey(hKey);
296 return err;
299 static void scmdatabase_add_process(struct scmdatabase *db, struct process_entry *process)
301 process->db = db;
302 list_add_tail(&db->processes, &process->entry);
305 static void scmdatabase_remove_process(struct scmdatabase *db, struct process_entry *process)
307 list_remove(&process->entry);
308 process->entry.next = process->entry.prev = NULL;
311 DWORD scmdatabase_add_service(struct scmdatabase *db, struct service_entry *service)
313 int err;
314 service->db = db;
315 if ((err = save_service_config(service)) != ERROR_SUCCESS)
317 WINE_ERR("Couldn't store service configuration: error %u\n", err);
318 return ERROR_GEN_FAILURE;
321 list_add_tail(&db->services, &service->entry);
322 return ERROR_SUCCESS;
325 static void scmdatabase_remove_service(struct scmdatabase *db, struct service_entry *service)
327 RegDeleteTreeW(db->root_key, service->name);
328 list_remove(&service->entry);
329 service->entry.next = service->entry.prev = NULL;
332 static int __cdecl compare_tags(const void *a, const void *b)
334 struct service_entry *service_a = *(struct service_entry **)a;
335 struct service_entry *service_b = *(struct service_entry **)b;
336 return service_a->config.dwTagId - service_b->config.dwTagId;
339 static PTP_CLEANUP_GROUP delayed_autostart_cleanup;
341 struct delayed_autostart_params
343 unsigned int count;
344 struct service_entry **services;
347 static void CALLBACK delayed_autostart_cancel_callback(void *object, void *userdata)
349 struct delayed_autostart_params *params = object;
350 while(params->count--)
351 release_service(params->services[params->count]);
352 free(params->services);
353 free(params);
356 static void CALLBACK delayed_autostart_callback(TP_CALLBACK_INSTANCE *instance, void *context,
357 TP_TIMER *timer)
359 struct delayed_autostart_params *params = context;
360 struct service_entry *service;
361 unsigned int i;
362 DWORD err;
364 scmdatabase_lock_startup(active_database, INFINITE);
366 for (i = 0; i < params->count; i++)
368 service = params->services[i];
369 if (service->status.dwCurrentState == SERVICE_STOPPED)
371 TRACE("Starting delayed auto-start service %s\n", debugstr_w(service->name));
372 err = service_start(service, 0, NULL);
373 if (err != ERROR_SUCCESS)
374 FIXME("Delayed auto-start service %s failed to start: %ld\n",
375 wine_dbgstr_w(service->name), err);
377 release_service(service);
380 scmdatabase_unlock_startup(active_database);
382 free(params->services);
383 free(params);
384 CloseThreadpoolTimer(timer);
387 static BOOL schedule_delayed_autostart(struct service_entry **services, unsigned int count)
389 struct delayed_autostart_params *params;
390 TP_CALLBACK_ENVIRON environment;
391 LARGE_INTEGER timestamp;
392 TP_TIMER *timer;
393 FILETIME ft;
395 if (!(delayed_autostart_cleanup = CreateThreadpoolCleanupGroup()))
397 ERR("CreateThreadpoolCleanupGroup failed with error %lu\n", GetLastError());
398 return FALSE;
401 if (!(params = malloc(sizeof(*params)))) return FALSE;
402 params->count = count;
403 params->services = services;
405 memset(&environment, 0, sizeof(environment));
406 environment.Version = 1;
407 environment.CleanupGroup = delayed_autostart_cleanup;
408 environment.CleanupGroupCancelCallback = delayed_autostart_cancel_callback;
410 timestamp.QuadPart = (ULONGLONG)autostart_delay * -10000;
411 ft.dwLowDateTime = timestamp.u.LowPart;
412 ft.dwHighDateTime = timestamp.u.HighPart;
414 if (!(timer = CreateThreadpoolTimer(delayed_autostart_callback, params, &environment)))
416 ERR("CreateThreadpoolWait failed: %lu\n", GetLastError());
417 free(params);
418 return FALSE;
421 SetThreadpoolTimer(timer, &ft, 0, 0);
422 return TRUE;
425 static BOOL is_root_pnp_service(HDEVINFO set, const struct service_entry *service)
427 SP_DEVINFO_DATA device = {sizeof(device)};
428 WCHAR name[MAX_SERVICE_NAME];
429 unsigned int i;
431 for (i = 0; SetupDiEnumDeviceInfo(set, i, &device); ++i)
433 if (SetupDiGetDeviceRegistryPropertyW(set, &device, SPDRP_SERVICE, NULL,
434 (BYTE *)name, sizeof(name), NULL)
435 && !wcsicmp(name, service->name))
437 return TRUE;
441 return FALSE;
444 static void scmdatabase_autostart_services(struct scmdatabase *db)
446 static const WCHAR rootW[] = {'R','O','O','T',0};
447 struct service_entry **services_list;
448 unsigned int i = 0;
449 unsigned int size = 32;
450 unsigned int delayed_cnt = 0;
451 struct service_entry *service;
452 HDEVINFO set;
454 services_list = malloc(size * sizeof(services_list[0]));
455 if (!services_list)
456 return;
458 if ((set = SetupDiGetClassDevsW( NULL, rootW, NULL, DIGCF_ALLCLASSES )) == INVALID_HANDLE_VALUE)
459 WINE_ERR("Failed to enumerate devices, error %#lx.\n", GetLastError());
461 scmdatabase_lock(db);
463 LIST_FOR_EACH_ENTRY(service, &db->services, struct service_entry, entry)
465 if (service->config.dwStartType == SERVICE_BOOT_START ||
466 service->config.dwStartType == SERVICE_SYSTEM_START ||
467 service->config.dwStartType == SERVICE_AUTO_START ||
468 (set != INVALID_HANDLE_VALUE && is_root_pnp_service(set, service)))
470 if (i+1 >= size)
472 struct service_entry **slist_new;
473 size *= 2;
474 slist_new = realloc(services_list, size * sizeof(services_list[0]));
475 if (!slist_new)
476 break;
477 services_list = slist_new;
479 services_list[i++] = grab_service(service);
482 size = i;
484 scmdatabase_unlock(db);
485 qsort(services_list, size, sizeof(services_list[0]), compare_tags);
486 scmdatabase_lock_startup(db, INFINITE);
488 for (i = 0; i < size; i++)
490 DWORD err;
491 service = services_list[i];
492 if (service->delayed_autostart)
494 TRACE("delayed starting %s\n", wine_dbgstr_w(service->name));
495 services_list[delayed_cnt++] = service;
496 continue;
498 err = service_start(service, 0, NULL);
499 if (err != ERROR_SUCCESS)
500 WINE_FIXME("Auto-start service %s failed to start: %ld\n",
501 wine_dbgstr_w(service->name), err);
502 release_service(service);
505 scmdatabase_unlock_startup(db);
507 if (!delayed_cnt || !schedule_delayed_autostart(services_list, delayed_cnt))
508 free(services_list);
509 SetupDiDestroyDeviceInfoList(set);
512 static void scmdatabase_wait_terminate(struct scmdatabase *db)
514 struct list pending = LIST_INIT(pending);
515 void *ptr;
517 scmdatabase_lock(db);
518 list_move_tail(&pending, &db->processes);
519 while ((ptr = list_head(&pending)))
521 struct process_entry *process = grab_process(LIST_ENTRY(ptr, struct process_entry, entry));
523 process_terminate(process);
524 scmdatabase_unlock(db);
525 WaitForSingleObject(process->process, INFINITE);
526 scmdatabase_lock(db);
528 list_remove(&process->entry);
529 list_add_tail(&db->processes, &process->entry);
530 release_process(process);
532 scmdatabase_unlock(db);
535 BOOL validate_service_name(LPCWSTR name)
537 return (name && name[0] && !wcschr(name, '/') && !wcschr(name, '\\'));
540 BOOL validate_service_config(struct service_entry *entry)
542 if (entry->config.dwServiceType & SERVICE_WIN32 && (entry->config.lpBinaryPathName == NULL || !entry->config.lpBinaryPathName[0]))
544 WINE_ERR("Service %s is Win32 but has no image path set\n", wine_dbgstr_w(entry->name));
545 return FALSE;
548 switch (entry->config.dwServiceType)
550 case SERVICE_KERNEL_DRIVER:
551 case SERVICE_FILE_SYSTEM_DRIVER:
552 case SERVICE_WIN32_OWN_PROCESS:
553 case SERVICE_WIN32_SHARE_PROCESS:
554 /* No problem */
555 break;
556 case SERVICE_WIN32_OWN_PROCESS | SERVICE_INTERACTIVE_PROCESS:
557 case SERVICE_WIN32_SHARE_PROCESS | SERVICE_INTERACTIVE_PROCESS:
558 /* These can be only run as LocalSystem */
559 if (entry->config.lpServiceStartName && wcsicmp(entry->config.lpServiceStartName, SZ_LOCAL_SYSTEM) != 0)
561 WINE_ERR("Service %s is interactive but has a start name\n", wine_dbgstr_w(entry->name));
562 return FALSE;
564 break;
565 default:
566 WINE_ERR("Service %s has an unknown service type (0x%lx)\n", wine_dbgstr_w(entry->name), entry->config.dwServiceType);
567 return FALSE;
570 /* StartType can only be a single value (if several values are mixed the result is probably not what was intended) */
571 if (entry->config.dwStartType > SERVICE_DISABLED)
573 WINE_ERR("Service %s has an unknown start type\n", wine_dbgstr_w(entry->name));
574 return FALSE;
577 /* SERVICE_BOOT_START and SERVICE_SYSTEM_START are only allowed for driver services */
578 if (((entry->config.dwStartType == SERVICE_BOOT_START) || (entry->config.dwStartType == SERVICE_SYSTEM_START)) &&
579 ((entry->config.dwServiceType & SERVICE_WIN32_OWN_PROCESS) || (entry->config.dwServiceType & SERVICE_WIN32_SHARE_PROCESS)))
581 WINE_ERR("Service %s - SERVICE_BOOT_START and SERVICE_SYSTEM_START are only allowed for driver services\n", wine_dbgstr_w(entry->name));
582 return FALSE;
585 if (entry->config.lpServiceStartName == NULL)
586 entry->config.lpServiceStartName = wcsdup(SZ_LOCAL_SYSTEM);
588 return TRUE;
592 struct service_entry *scmdatabase_find_service(struct scmdatabase *db, LPCWSTR name)
594 struct service_entry *service;
596 LIST_FOR_EACH_ENTRY(service, &db->services, struct service_entry, entry)
598 if (wcsicmp(name, service->name) == 0)
599 return service;
602 return NULL;
605 struct service_entry *scmdatabase_find_service_by_displayname(struct scmdatabase *db, LPCWSTR name)
607 struct service_entry *service;
609 LIST_FOR_EACH_ENTRY(service, &db->services, struct service_entry, entry)
611 if (service->config.lpDisplayName && wcsicmp(name, service->config.lpDisplayName) == 0)
612 return service;
615 return NULL;
618 struct process_entry *grab_process(struct process_entry *process)
620 if (process)
621 InterlockedIncrement(&process->ref_count);
622 return process;
625 void release_process(struct process_entry *process)
627 struct scmdatabase *db = process->db;
629 scmdatabase_lock(db);
630 if (InterlockedDecrement(&process->ref_count) == 0)
632 scmdatabase_remove_process(db, process);
633 free_process_entry(process);
635 scmdatabase_unlock(db);
638 struct service_entry *grab_service(struct service_entry *service)
640 if (service)
641 InterlockedIncrement(&service->ref_count);
642 return service;
645 void release_service(struct service_entry *service)
647 struct scmdatabase *db = service->db;
649 scmdatabase_lock(db);
650 if (InterlockedDecrement(&service->ref_count) == 0 && is_marked_for_delete(service))
652 scmdatabase_remove_service(db, service);
653 free_service_entry(service);
655 scmdatabase_unlock(db);
658 static DWORD scmdatabase_create(struct scmdatabase **db)
660 DWORD err;
662 *db = malloc(sizeof(**db));
663 if (!*db)
664 return ERROR_NOT_ENOUGH_SERVER_MEMORY;
666 (*db)->service_start_lock = FALSE;
667 list_init(&(*db)->processes);
668 list_init(&(*db)->services);
670 InitializeCriticalSection(&(*db)->cs);
671 (*db)->cs.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": scmdatabase");
673 err = RegCreateKeyExW(HKEY_LOCAL_MACHINE, SZ_SERVICES_KEY, 0, NULL,
674 REG_OPTION_NON_VOLATILE, MAXIMUM_ALLOWED, NULL,
675 &(*db)->root_key, NULL);
676 if (err != ERROR_SUCCESS)
677 free(*db);
679 return err;
682 static void scmdatabase_destroy(struct scmdatabase *db)
684 RegCloseKey(db->root_key);
685 db->cs.DebugInfo->Spare[0] = 0;
686 DeleteCriticalSection(&db->cs);
687 free(db);
690 static DWORD scmdatabase_load_services(struct scmdatabase *db)
692 DWORD err;
693 int i;
695 for (i = 0; TRUE; i++)
697 WCHAR szName[MAX_SERVICE_NAME];
698 struct service_entry *entry;
699 HKEY hServiceKey;
701 err = RegEnumKeyW(db->root_key, i, szName, MAX_SERVICE_NAME);
702 if (err == ERROR_NO_MORE_ITEMS)
703 break;
705 if (err != 0)
707 WINE_ERR("Error %ld reading key %d name - skipping\n", err, i);
708 continue;
711 err = service_create(szName, &entry);
712 if (err != ERROR_SUCCESS)
713 break;
715 WINE_TRACE("Loading service %s\n", wine_dbgstr_w(szName));
716 err = RegOpenKeyExW(db->root_key, szName, 0, KEY_READ, &hServiceKey);
717 if (err == ERROR_SUCCESS)
719 err = load_service_config(hServiceKey, entry);
720 RegCloseKey(hServiceKey);
723 if (err != ERROR_SUCCESS)
725 WINE_ERR("Error %ld reading registry key for service %s - skipping\n", err, wine_dbgstr_w(szName));
726 free_service_entry(entry);
727 continue;
730 if (entry->config.dwServiceType == 0)
732 /* Maybe an application only wrote some configuration in the service key. Continue silently */
733 WINE_TRACE("Even the service type not set for service %s - skipping\n", wine_dbgstr_w(szName));
734 free_service_entry(entry);
735 continue;
738 if (!validate_service_config(entry))
740 WINE_ERR("Invalid configuration of service %s - skipping\n", wine_dbgstr_w(szName));
741 free_service_entry(entry);
742 continue;
745 entry->status.dwServiceType = entry->config.dwServiceType;
746 entry->db = db;
748 list_add_tail(&db->services, &entry->entry);
749 release_service(entry);
751 return ERROR_SUCCESS;
754 BOOL scmdatabase_lock_startup(struct scmdatabase *db, int timeout)
756 while (InterlockedCompareExchange(&db->service_start_lock, TRUE, FALSE))
758 if (timeout != INFINITE)
760 timeout -= 10;
761 if (timeout <= 0) return FALSE;
763 Sleep(10);
765 return TRUE;
768 void scmdatabase_unlock_startup(struct scmdatabase *db)
770 InterlockedCompareExchange(&db->service_start_lock, FALSE, TRUE);
773 void scmdatabase_lock(struct scmdatabase *db)
775 EnterCriticalSection(&db->cs);
778 void scmdatabase_unlock(struct scmdatabase *db)
780 LeaveCriticalSection(&db->cs);
783 void service_lock(struct service_entry *service)
785 EnterCriticalSection(&service->db->cs);
788 void service_unlock(struct service_entry *service)
790 LeaveCriticalSection(&service->db->cs);
793 /* only one service started at a time, so there is no race on the registry
794 * value here */
795 static LPWSTR service_get_pipe_name(void)
797 static const WCHAR format[] = { '\\','\\','.','\\','p','i','p','e','\\',
798 'n','e','t','\\','N','t','C','o','n','t','r','o','l','P','i','p','e','%','u',0};
799 static WCHAR name[ARRAY_SIZE(format) + 10]; /* lstrlenW("4294967295") */
800 static DWORD service_current = 0;
801 DWORD len, value = -1;
802 LONG ret;
803 DWORD type;
805 len = sizeof(value);
806 ret = RegQueryValueExW(service_current_key, NULL, NULL, &type,
807 (BYTE *)&value, &len);
808 if (ret == ERROR_SUCCESS && type == REG_DWORD)
809 service_current = max(service_current, value + 1);
810 RegSetValueExW(service_current_key, NULL, 0, REG_DWORD,
811 (BYTE *)&service_current, sizeof(service_current));
812 swprintf(name, ARRAY_SIZE(name), format, service_current);
813 service_current++;
814 return name;
817 static DWORD get_service_binary_path(const struct service_entry *service_entry, WCHAR **path)
819 DWORD size = ExpandEnvironmentStringsW(service_entry->config.lpBinaryPathName, NULL, 0);
821 *path = malloc(size * sizeof(WCHAR));
822 if (!*path)
823 return ERROR_NOT_ENOUGH_SERVER_MEMORY;
825 ExpandEnvironmentStringsW(service_entry->config.lpBinaryPathName, *path, size);
827 /* if service image is configured to systemdir, redirect it to wow64 systemdir */
828 if (service_entry->is_wow64 && !(service_entry->config.dwServiceType & (SERVICE_FILE_SYSTEM_DRIVER | SERVICE_KERNEL_DRIVER)))
830 WCHAR system_dir[MAX_PATH], *redirected;
831 DWORD len;
833 GetSystemDirectoryW( system_dir, MAX_PATH );
834 len = lstrlenW( system_dir );
836 if (wcsnicmp( system_dir, *path, len ))
837 return ERROR_SUCCESS;
839 GetSystemWow64DirectoryW( system_dir, MAX_PATH );
841 redirected = malloc( (wcslen( *path ) + wcslen( system_dir )) * sizeof(WCHAR) );
842 if (!redirected)
844 free( *path );
845 return ERROR_NOT_ENOUGH_SERVER_MEMORY;
848 lstrcpyW( redirected, system_dir );
849 lstrcatW( redirected, &(*path)[len] );
850 free( *path );
851 *path = redirected;
852 TRACE("redirected to %s\n", debugstr_w(redirected));
855 return ERROR_SUCCESS;
858 static DWORD get_winedevice_binary_path(struct service_entry *service_entry, WCHAR **path, BOOL *is_wow64)
860 static const WCHAR winedeviceW[] = {'\\','w','i','n','e','d','e','v','i','c','e','.','e','x','e',0};
861 WCHAR system_dir[MAX_PATH];
862 DWORD type;
864 if (!is_win64)
865 *is_wow64 = FALSE;
866 else if (GetBinaryTypeW(*path, &type))
867 *is_wow64 = (type == SCS_32BIT_BINARY);
868 else
869 *is_wow64 = service_entry->is_wow64;
871 GetSystemDirectoryW(system_dir, MAX_PATH);
872 free(*path);
873 if (!(*path = malloc(wcslen(system_dir) * sizeof(WCHAR) + sizeof(winedeviceW))))
874 return ERROR_NOT_ENOUGH_SERVER_MEMORY;
876 lstrcpyW(*path, system_dir);
877 lstrcatW(*path, winedeviceW);
878 return ERROR_SUCCESS;
881 static struct process_entry *get_winedevice_process(struct service_entry *service_entry, WCHAR *path, BOOL is_wow64)
883 struct service_entry *winedevice_entry;
885 if (!service_entry->config.lpLoadOrderGroup)
886 return NULL;
888 LIST_FOR_EACH_ENTRY(winedevice_entry, &service_entry->db->services, struct service_entry, entry)
890 if (winedevice_entry->status.dwCurrentState != SERVICE_START_PENDING &&
891 winedevice_entry->status.dwCurrentState != SERVICE_RUNNING) continue;
892 if (!winedevice_entry->process) continue;
894 if (winedevice_entry->is_wow64 != is_wow64) continue;
895 if (!winedevice_entry->config.lpBinaryPathName) continue;
896 if (lstrcmpW(winedevice_entry->config.lpBinaryPathName, path)) continue;
898 if (!winedevice_entry->config.lpLoadOrderGroup) continue;
899 if (lstrcmpW(winedevice_entry->config.lpLoadOrderGroup, service_entry->config.lpLoadOrderGroup)) continue;
901 return grab_process(winedevice_entry->process);
904 return NULL;
907 static DWORD add_winedevice_service(const struct service_entry *service, WCHAR *path, BOOL is_wow64,
908 struct service_entry **entry)
910 static const WCHAR format[] = {'W','i','n','e','d','e','v','i','c','e','%','u',0};
911 static WCHAR name[ARRAY_SIZE(format) + 10]; /* lstrlenW("4294967295") */
912 static DWORD current = 0;
913 struct scmdatabase *db = service->db;
914 DWORD err;
916 for (;;)
918 swprintf(name, ARRAY_SIZE(name), format, ++current);
919 if (!scmdatabase_find_service(db, name)) break;
922 err = service_create(name, entry);
923 if (err != ERROR_SUCCESS)
924 return err;
926 (*entry)->is_wow64 = is_wow64;
927 (*entry)->config.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
928 (*entry)->config.dwStartType = SERVICE_DEMAND_START;
929 (*entry)->status.dwServiceType = (*entry)->config.dwServiceType;
931 if (!((*entry)->config.lpBinaryPathName = wcsdup(path)))
932 goto error;
933 if (!((*entry)->config.lpServiceStartName = wcsdup(SZ_LOCAL_SYSTEM)))
934 goto error;
935 if (!((*entry)->config.lpDisplayName = wcsdup(name)))
936 goto error;
937 if (service->config.lpLoadOrderGroup &&
938 !((*entry)->config.lpLoadOrderGroup = wcsdup(service->config.lpLoadOrderGroup)))
939 goto error;
941 (*entry)->db = db;
943 list_add_tail(&db->services, &(*entry)->entry);
944 mark_for_delete(*entry);
945 return ERROR_SUCCESS;
947 error:
948 free_service_entry(*entry);
949 return ERROR_NOT_ENOUGH_SERVER_MEMORY;
952 static DWORD service_start_process(struct service_entry *service_entry, struct process_entry **new_process,
953 BOOL *shared_process)
955 struct process_entry *process;
956 PROCESS_INFORMATION pi;
957 STARTUPINFOW si;
958 BOOL is_wow64 = FALSE;
959 HANDLE token;
960 WCHAR *path;
961 DWORD err;
962 BOOL r;
964 service_lock(service_entry);
966 if ((process = service_entry->process))
968 if (WaitForSingleObject(process->process, 0) == WAIT_TIMEOUT)
970 service_unlock(service_entry);
971 return ERROR_SERVICE_ALREADY_RUNNING;
973 service_entry->process = NULL;
974 process->use_count--;
975 release_process(process);
978 service_entry->force_shutdown = FALSE;
980 if ((err = get_service_binary_path(service_entry, &path)))
982 service_unlock(service_entry);
983 return err;
986 if (service_entry->config.dwServiceType == SERVICE_KERNEL_DRIVER ||
987 service_entry->config.dwServiceType == SERVICE_FILE_SYSTEM_DRIVER)
989 struct service_entry *winedevice_entry;
990 WCHAR *group;
992 if ((err = get_winedevice_binary_path(service_entry, &path, &is_wow64)))
994 service_unlock(service_entry);
995 free(path);
996 return err;
999 if ((process = get_winedevice_process(service_entry, path, is_wow64)))
1001 free(path);
1002 goto found;
1005 err = add_winedevice_service(service_entry, path, is_wow64, &winedevice_entry);
1006 free(path);
1007 if (err != ERROR_SUCCESS)
1009 service_unlock(service_entry);
1010 return err;
1013 group = wcsdup(winedevice_entry->config.lpLoadOrderGroup);
1014 service_unlock(service_entry);
1016 err = service_start(winedevice_entry, group != NULL, (const WCHAR **)&group);
1017 free(group);
1018 if (err != ERROR_SUCCESS)
1020 release_service(winedevice_entry);
1021 return err;
1024 service_lock(service_entry);
1025 process = grab_process(winedevice_entry->process);
1026 release_service(winedevice_entry);
1028 if (!process)
1030 service_unlock(service_entry);
1031 return ERROR_SERVICE_REQUEST_TIMEOUT;
1034 found:
1035 service_entry->status.dwCurrentState = SERVICE_START_PENDING;
1036 service_entry->status.dwControlsAccepted = 0;
1037 ResetEvent(service_entry->status_changed_event);
1039 service_entry->process = grab_process(process);
1040 service_entry->shared_process = *shared_process = TRUE;
1041 process->use_count++;
1042 service_unlock(service_entry);
1044 err = WaitForSingleObject(process->control_mutex, 30000);
1045 if (err != WAIT_OBJECT_0)
1047 release_process(process);
1048 return ERROR_SERVICE_REQUEST_TIMEOUT;
1051 *new_process = process;
1052 return ERROR_SUCCESS;
1055 if ((err = process_create(service_get_pipe_name(), &process)))
1057 WINE_ERR("failed to create process object for %s, error = %lu\n",
1058 wine_dbgstr_w(service_entry->name), err);
1059 service_unlock(service_entry);
1060 free(path);
1061 return err;
1064 ZeroMemory(&si, sizeof(STARTUPINFOW));
1065 si.cb = sizeof(STARTUPINFOW);
1066 if (!(service_entry->config.dwServiceType & SERVICE_INTERACTIVE_PROCESS))
1068 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};
1069 si.lpDesktop = desktopW;
1072 if (!environment && OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY | TOKEN_DUPLICATE, &token))
1074 WCHAR val[16];
1075 CreateEnvironmentBlock(&environment, token, FALSE);
1076 if (GetEnvironmentVariableW( L"WINEBOOTSTRAPMODE", val, ARRAY_SIZE(val) ))
1078 UNICODE_STRING name = RTL_CONSTANT_STRING(L"WINEBOOTSTRAPMODE");
1079 UNICODE_STRING value;
1081 RtlInitUnicodeString( &value, val );
1082 RtlSetEnvironmentVariable( (WCHAR **)&environment, &name, &value );
1084 CloseHandle(token);
1087 service_entry->status.dwCurrentState = SERVICE_START_PENDING;
1088 service_entry->status.dwControlsAccepted = 0;
1089 ResetEvent(service_entry->status_changed_event);
1091 scmdatabase_add_process(service_entry->db, process);
1092 service_entry->process = grab_process(process);
1093 service_entry->shared_process = *shared_process = FALSE;
1094 process->use_count++;
1095 service_unlock(service_entry);
1097 r = CreateProcessW(NULL, path, NULL, NULL, FALSE, CREATE_UNICODE_ENVIRONMENT | DETACHED_PROCESS, environment, NULL, &si, &pi);
1098 free(path);
1099 if (!r)
1101 err = GetLastError();
1102 process_terminate(process);
1103 release_process(process);
1104 return err;
1106 if (!AssignProcessToJobObject(job_object, pi.hProcess))
1107 WINE_ERR("Could not add object to job.\n");
1109 process->process_id = pi.dwProcessId;
1110 process->process = pi.hProcess;
1111 CloseHandle( pi.hThread );
1113 *new_process = process;
1114 return ERROR_SUCCESS;
1117 static DWORD service_wait_for_startup(struct service_entry *service, struct process_entry *process)
1119 HANDLE handles[2] = { service->status_changed_event, process->process };
1120 DWORD result;
1122 result = WaitForMultipleObjects( 2, handles, FALSE, service_pipe_timeout );
1123 if (result != WAIT_OBJECT_0)
1124 return ERROR_SERVICE_REQUEST_TIMEOUT;
1126 service_lock(service);
1127 result = service->status.dwCurrentState;
1128 service_unlock(service);
1130 return (result == SERVICE_START_PENDING || result == SERVICE_RUNNING) ?
1131 ERROR_SUCCESS : ERROR_SERVICE_REQUEST_TIMEOUT;
1134 /******************************************************************************
1135 * process_send_start_message
1137 static DWORD process_send_start_message(struct process_entry *process, BOOL shared_process,
1138 const WCHAR *name, const WCHAR **argv, DWORD argc)
1140 OVERLAPPED overlapped;
1141 DWORD i, len, result;
1142 WCHAR *str, *p;
1144 WINE_TRACE("%p %s %p %ld\n", process, wine_dbgstr_w(name), argv, argc);
1146 overlapped.hEvent = process->overlapped_event;
1147 if (!ConnectNamedPipe(process->control_pipe, &overlapped))
1149 if (GetLastError() == ERROR_IO_PENDING)
1151 HANDLE handles[2];
1152 handles[0] = process->overlapped_event;
1153 handles[1] = process->process;
1154 if (WaitForMultipleObjects( 2, handles, FALSE, service_pipe_timeout ) != WAIT_OBJECT_0)
1155 CancelIo(process->control_pipe);
1156 if (!GetOverlappedResult(process->control_pipe, &overlapped, &len, FALSE))
1158 WINE_ERR("service %s failed to start\n", wine_dbgstr_w(name));
1159 return ERROR_SERVICE_REQUEST_TIMEOUT;
1162 else if (GetLastError() != ERROR_PIPE_CONNECTED)
1164 WINE_ERR("pipe connect failed\n");
1165 return ERROR_SERVICE_REQUEST_TIMEOUT;
1169 len = lstrlenW(name) + 1;
1170 for (i = 0; i < argc; i++)
1171 len += lstrlenW(argv[i])+1;
1172 len = (len + 1) * sizeof(WCHAR);
1174 if (!(str = malloc(len)))
1175 return ERROR_NOT_ENOUGH_SERVER_MEMORY;
1177 p = str;
1178 lstrcpyW(p, name);
1179 p += lstrlenW(name) + 1;
1180 for (i = 0; i < argc; i++)
1182 lstrcpyW(p, argv[i]);
1183 p += lstrlenW(p) + 1;
1185 *p = 0;
1187 if (!process_send_control(process, shared_process, name,
1188 SERVICE_CONTROL_START, (const BYTE *)str, len, &result))
1189 result = ERROR_SERVICE_REQUEST_TIMEOUT;
1191 free(str);
1192 return result;
1195 DWORD service_start(struct service_entry *service, DWORD service_argc, LPCWSTR *service_argv)
1197 struct process_entry *process = NULL;
1198 BOOL shared_process;
1199 DWORD err;
1201 err = service_start_process(service, &process, &shared_process);
1202 if (err == ERROR_SUCCESS)
1204 err = process_send_start_message(process, shared_process, service->name, service_argv, service_argc);
1206 if (err == ERROR_SUCCESS)
1207 err = service_wait_for_startup(service, process);
1209 if (err != ERROR_SUCCESS)
1211 service_lock(service);
1212 if (service->process)
1214 service->status.dwCurrentState = SERVICE_STOPPED;
1215 service->process = NULL;
1216 if (!--process->use_count) process_terminate(process);
1217 release_process(process);
1219 service_unlock(service);
1222 ReleaseMutex(process->control_mutex);
1223 release_process(process);
1226 WINE_TRACE("returning %ld\n", err);
1227 return err;
1230 void process_terminate(struct process_entry *process)
1232 struct scmdatabase *db = process->db;
1233 struct service_entry *service;
1235 scmdatabase_lock(db);
1236 TerminateProcess(process->process, 0);
1237 LIST_FOR_EACH_ENTRY(service, &db->services, struct service_entry, entry)
1239 if (service->process != process) continue;
1240 service->status.dwCurrentState = SERVICE_STOPPED;
1241 service->process = NULL;
1242 process->use_count--;
1243 release_process(process);
1245 scmdatabase_unlock(db);
1248 static void load_registry_parameters(void)
1250 static const WCHAR controlW[] =
1251 { 'S','y','s','t','e','m','\\',
1252 'C','u','r','r','e','n','t','C','o','n','t','r','o','l','S','e','t','\\',
1253 'C','o','n','t','r','o','l',0 };
1254 static const WCHAR pipetimeoutW[] =
1255 {'S','e','r','v','i','c','e','s','P','i','p','e','T','i','m','e','o','u','t',0};
1256 static const WCHAR killtimeoutW[] =
1257 {'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};
1258 static const WCHAR autostartdelayW[] =
1259 {'A','u','t','o','S','t','a','r','t','D','e','l','a','y',0};
1260 HKEY key;
1261 WCHAR buffer[64];
1262 DWORD type, count, val;
1264 if (RegOpenKeyW( HKEY_LOCAL_MACHINE, controlW, &key )) return;
1266 count = sizeof(buffer);
1267 if (!RegQueryValueExW( key, pipetimeoutW, NULL, &type, (BYTE *)buffer, &count ) &&
1268 type == REG_SZ && (val = wcstol( buffer, NULL, 10 )))
1269 service_pipe_timeout = val;
1271 count = sizeof(buffer);
1272 if (!RegQueryValueExW( key, killtimeoutW, NULL, &type, (BYTE *)buffer, &count ) &&
1273 type == REG_SZ && (val = wcstol( buffer, NULL, 10 )))
1274 service_kill_timeout = val;
1276 count = sizeof(val);
1277 if (!RegQueryValueExW( key, autostartdelayW, NULL, &type, (BYTE *)&val, &count ) && type == REG_DWORD)
1278 autostart_delay = val;
1280 RegCloseKey( key );
1283 static DWORD WINAPI process_monitor_thread_proc( void *arg )
1285 struct scmdatabase *db = active_database;
1286 struct service_entry *service;
1287 struct process_entry *process;
1288 OVERLAPPED *overlapped;
1289 ULONG_PTR value;
1290 DWORD key, pid;
1292 while (GetQueuedCompletionStatus(job_completion_port, &key, &value, &overlapped, INFINITE))
1294 if (!key)
1295 break;
1296 if (key != JOB_OBJECT_MSG_EXIT_PROCESS)
1297 continue;
1298 pid = (ULONG_PTR)overlapped;
1299 WINE_TRACE("pid %04lx exited.\n", pid);
1300 scmdatabase_lock(db);
1301 LIST_FOR_EACH_ENTRY(service, &db->services, struct service_entry, entry)
1303 if (service->status.dwCurrentState != SERVICE_RUNNING || !service->process
1304 || service->process->process_id != pid) continue;
1306 WINE_TRACE("Stopping service %s.\n", debugstr_w(service->config.lpBinaryPathName));
1307 service->status.dwCurrentState = SERVICE_STOPPED;
1308 service->status.dwControlsAccepted = 0;
1309 service->status.dwWin32ExitCode = ERROR_PROCESS_ABORTED;
1310 service->status.dwServiceSpecificExitCode = 0;
1311 service->status.dwCheckPoint = 0;
1312 service->status.dwWaitHint = 0;
1313 SetEvent(service->status_changed_event);
1315 process = service->process;
1316 service->process = NULL;
1317 process->use_count--;
1318 release_process(process);
1319 notify_service_state(service);
1321 scmdatabase_unlock(db);
1323 WINE_TRACE("Terminating.\n");
1324 return 0;
1327 int __cdecl main(int argc, char *argv[])
1329 static const WCHAR service_current_key_str[] = { 'S','Y','S','T','E','M','\\',
1330 'C','u','r','r','e','n','t','C','o','n','t','r','o','l','S','e','t','\\',
1331 'C','o','n','t','r','o','l','\\',
1332 'S','e','r','v','i','c','e','C','u','r','r','e','n','t',0};
1333 static const WCHAR svcctl_started_event[] = SVCCTL_STARTED_EVENT;
1334 JOBOBJECT_EXTENDED_LIMIT_INFORMATION job_limit;
1335 JOBOBJECT_ASSOCIATE_COMPLETION_PORT port_info;
1336 HANDLE started_event, process_monitor_thread;
1337 DWORD err;
1339 job_object = CreateJobObjectW(NULL, NULL);
1340 job_limit.BasicLimitInformation.LimitFlags = JOB_OBJECT_LIMIT_BREAKAWAY_OK | JOB_OBJECT_LIMIT_SILENT_BREAKAWAY_OK;
1341 if (!SetInformationJobObject(job_object, JobObjectExtendedLimitInformation, &job_limit, sizeof(job_limit)))
1343 WINE_ERR("Failed to initialized job object, err %lu.\n", GetLastError());
1344 return GetLastError();
1346 job_completion_port = CreateIoCompletionPort(INVALID_HANDLE_VALUE, NULL, 0, 1);
1347 port_info.CompletionPort = job_completion_port;
1348 port_info.CompletionKey = job_object;
1349 if (!SetInformationJobObject(job_object, JobObjectAssociateCompletionPortInformation,
1350 &port_info, sizeof(port_info)))
1352 WINE_ERR("Failed to set completion port for job, err %lu.\n", GetLastError());
1353 return GetLastError();
1356 started_event = CreateEventW(NULL, TRUE, FALSE, svcctl_started_event);
1358 err = RegCreateKeyExW(HKEY_LOCAL_MACHINE, service_current_key_str, 0,
1359 NULL, REG_OPTION_VOLATILE, KEY_SET_VALUE | KEY_QUERY_VALUE, NULL,
1360 &service_current_key, NULL);
1361 if (err != ERROR_SUCCESS)
1362 return err;
1364 load_registry_parameters();
1365 err = scmdatabase_create(&active_database);
1366 if (err != ERROR_SUCCESS)
1367 return err;
1368 if ((err = scmdatabase_load_services(active_database)) != ERROR_SUCCESS)
1369 return err;
1370 if ((err = RPC_Init()) == ERROR_SUCCESS)
1372 scmdatabase_autostart_services(active_database);
1373 process_monitor_thread = CreateThread(NULL, 0, process_monitor_thread_proc, NULL, 0, NULL);
1374 SetEvent(started_event);
1375 WaitForSingleObject(exit_event, INFINITE);
1376 PostQueuedCompletionStatus(job_completion_port, 0, 0, NULL);
1377 WaitForSingleObject(process_monitor_thread, INFINITE);
1378 scmdatabase_wait_terminate(active_database);
1379 if (delayed_autostart_cleanup)
1381 CloseThreadpoolCleanupGroupMembers(delayed_autostart_cleanup, TRUE, NULL);
1382 CloseThreadpoolCleanupGroup(delayed_autostart_cleanup);
1384 RPC_Stop();
1386 scmdatabase_destroy(active_database);
1387 if (environment)
1388 DestroyEnvironmentBlock(environment);
1390 WINE_TRACE("services.exe exited with code %ld\n", err);
1391 return err;