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
29 #include "wine/unicode.h"
30 #include "wine/debug.h"
35 #define MAX_SERVICE_NAME 260
37 WINE_DEFAULT_DEBUG_CHANNEL(service
);
39 struct scmdatabase
*active_database
;
41 DWORD service_pipe_timeout
= 10000;
42 DWORD service_kill_timeout
= 60000;
43 static DWORD default_preshutdown_timeout
= 180000;
44 static void *environment
= NULL
;
45 static HKEY service_current_key
= NULL
;
47 static const BOOL is_win64
= (sizeof(void *) > sizeof(int));
49 static const WCHAR SZ_LOCAL_SYSTEM
[] = {'L','o','c','a','l','S','y','s','t','e','m',0};
51 /* Registry constants */
52 static const WCHAR SZ_SERVICES_KEY
[] = { 'S','y','s','t','e','m','\\',
53 'C','u','r','r','e','n','t','C','o','n','t','r','o','l','S','e','t','\\',
54 'S','e','r','v','i','c','e','s',0 };
56 /* Service key values names */
57 static const WCHAR SZ_DISPLAY_NAME
[] = {'D','i','s','p','l','a','y','N','a','m','e',0 };
58 static const WCHAR SZ_TYPE
[] = {'T','y','p','e',0 };
59 static const WCHAR SZ_START
[] = {'S','t','a','r','t',0 };
60 static const WCHAR SZ_ERROR
[] = {'E','r','r','o','r','C','o','n','t','r','o','l',0 };
61 static const WCHAR SZ_IMAGE_PATH
[] = {'I','m','a','g','e','P','a','t','h',0};
62 static const WCHAR SZ_GROUP
[] = {'G','r','o','u','p',0};
63 static const WCHAR SZ_DEPEND_ON_SERVICE
[] = {'D','e','p','e','n','d','O','n','S','e','r','v','i','c','e',0};
64 static const WCHAR SZ_DEPEND_ON_GROUP
[] = {'D','e','p','e','n','d','O','n','G','r','o','u','p',0};
65 static const WCHAR SZ_OBJECT_NAME
[] = {'O','b','j','e','c','t','N','a','m','e',0};
66 static const WCHAR SZ_TAG
[] = {'T','a','g',0};
67 static const WCHAR SZ_DESCRIPTION
[] = {'D','e','s','c','r','i','p','t','i','o','n',0};
68 static const WCHAR SZ_PRESHUTDOWN
[] = {'P','r','e','s','h','u','t','d','o','w','n','T','i','m','e','o','u','t',0};
69 static const WCHAR SZ_WOW64
[] = {'W','O','W','6','4',0};
71 static DWORD
process_create(const WCHAR
*name
, struct process_entry
**entry
)
75 *entry
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(**entry
));
77 return ERROR_NOT_ENOUGH_SERVER_MEMORY
;
78 (*entry
)->ref_count
= 1;
79 (*entry
)->control_mutex
= CreateMutexW(NULL
, TRUE
, NULL
);
80 if (!(*entry
)->control_mutex
)
82 (*entry
)->overlapped_event
= CreateEventW(NULL
, TRUE
, FALSE
, NULL
);
83 if (!(*entry
)->overlapped_event
)
85 (*entry
)->control_pipe
= CreateNamedPipeW(name
, PIPE_ACCESS_DUPLEX
| FILE_FLAG_OVERLAPPED
,
86 PIPE_TYPE_BYTE
|PIPE_WAIT
, 1, 256, 256, 10000, NULL
);
87 if ((*entry
)->control_pipe
== INVALID_HANDLE_VALUE
)
89 /* all other fields are zero */
94 if ((*entry
)->control_mutex
)
95 CloseHandle((*entry
)->control_mutex
);
96 if ((*entry
)->overlapped_event
)
97 CloseHandle((*entry
)->overlapped_event
);
98 HeapFree(GetProcessHeap(), 0, *entry
);
102 static void free_process_entry(struct process_entry
*entry
)
104 CloseHandle(entry
->process
);
105 CloseHandle(entry
->control_mutex
);
106 CloseHandle(entry
->control_pipe
);
107 CloseHandle(entry
->overlapped_event
);
108 HeapFree(GetProcessHeap(), 0, entry
);
111 DWORD
service_create(LPCWSTR name
, struct service_entry
**entry
)
113 *entry
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(**entry
));
115 return ERROR_NOT_ENOUGH_SERVER_MEMORY
;
116 (*entry
)->name
= strdupW(name
);
119 HeapFree(GetProcessHeap(), 0, *entry
);
120 return ERROR_NOT_ENOUGH_SERVER_MEMORY
;
122 (*entry
)->status_changed_event
= CreateEventW(NULL
, TRUE
, FALSE
, NULL
);
123 if (!(*entry
)->status_changed_event
)
125 HeapFree(GetProcessHeap(), 0, (*entry
)->name
);
126 HeapFree(GetProcessHeap(), 0, *entry
);
127 return GetLastError();
129 (*entry
)->ref_count
= 1;
130 (*entry
)->status
.dwCurrentState
= SERVICE_STOPPED
;
131 (*entry
)->status
.dwWin32ExitCode
= ERROR_SERVICE_NEVER_STARTED
;
132 (*entry
)->preshutdown_timeout
= default_preshutdown_timeout
;
133 /* all other fields are zero */
134 return ERROR_SUCCESS
;
137 void free_service_entry(struct service_entry
*entry
)
139 CloseHandle(entry
->status_changed_event
);
140 HeapFree(GetProcessHeap(), 0, entry
->name
);
141 HeapFree(GetProcessHeap(), 0, entry
->config
.lpBinaryPathName
);
142 HeapFree(GetProcessHeap(), 0, entry
->config
.lpDependencies
);
143 HeapFree(GetProcessHeap(), 0, entry
->config
.lpLoadOrderGroup
);
144 HeapFree(GetProcessHeap(), 0, entry
->config
.lpServiceStartName
);
145 HeapFree(GetProcessHeap(), 0, entry
->config
.lpDisplayName
);
146 HeapFree(GetProcessHeap(), 0, entry
->description
);
147 HeapFree(GetProcessHeap(), 0, entry
->dependOnServices
);
148 HeapFree(GetProcessHeap(), 0, entry
->dependOnGroups
);
149 if (entry
->process
) release_process(entry
->process
);
150 HeapFree(GetProcessHeap(), 0, entry
);
153 static DWORD
load_service_config(HKEY hKey
, struct service_entry
*entry
)
155 DWORD err
, value
= 0;
158 if ((err
= load_reg_string(hKey
, SZ_IMAGE_PATH
, TRUE
, &entry
->config
.lpBinaryPathName
)) != 0)
160 if ((err
= load_reg_string(hKey
, SZ_GROUP
, 0, &entry
->config
.lpLoadOrderGroup
)) != 0)
162 if ((err
= load_reg_string(hKey
, SZ_OBJECT_NAME
, TRUE
, &entry
->config
.lpServiceStartName
)) != 0)
164 if ((err
= load_reg_string(hKey
, SZ_DISPLAY_NAME
, 0, &entry
->config
.lpDisplayName
)) != 0)
166 if ((err
= load_reg_string(hKey
, SZ_DESCRIPTION
, 0, &entry
->description
)) != 0)
168 if ((err
= load_reg_multisz(hKey
, SZ_DEPEND_ON_SERVICE
, TRUE
, &entry
->dependOnServices
)) != 0)
170 if ((err
= load_reg_multisz(hKey
, SZ_DEPEND_ON_GROUP
, FALSE
, &entry
->dependOnGroups
)) != 0)
173 if ((err
= load_reg_dword(hKey
, SZ_TYPE
, &entry
->config
.dwServiceType
)) != 0)
175 if ((err
= load_reg_dword(hKey
, SZ_START
, &entry
->config
.dwStartType
)) != 0)
177 if ((err
= load_reg_dword(hKey
, SZ_ERROR
, &entry
->config
.dwErrorControl
)) != 0)
179 if ((err
= load_reg_dword(hKey
, SZ_TAG
, &entry
->config
.dwTagId
)) != 0)
181 if ((err
= load_reg_dword(hKey
, SZ_PRESHUTDOWN
, &entry
->preshutdown_timeout
)) != 0)
184 if (load_reg_dword(hKey
, SZ_WOW64
, &value
) == 0 && value
== 1)
185 entry
->is_wow64
= TRUE
;
187 WINE_TRACE("Image path = %s\n", wine_dbgstr_w(entry
->config
.lpBinaryPathName
) );
188 WINE_TRACE("Group = %s\n", wine_dbgstr_w(entry
->config
.lpLoadOrderGroup
) );
189 WINE_TRACE("Service account name = %s\n", wine_dbgstr_w(entry
->config
.lpServiceStartName
) );
190 WINE_TRACE("Display name = %s\n", wine_dbgstr_w(entry
->config
.lpDisplayName
) );
191 WINE_TRACE("Service dependencies : %s\n", entry
->dependOnServices
[0] ? "" : "(none)");
192 for (wptr
= entry
->dependOnServices
; *wptr
; wptr
+= strlenW(wptr
) + 1)
193 WINE_TRACE(" * %s\n", wine_dbgstr_w(wptr
));
194 WINE_TRACE("Group dependencies : %s\n", entry
->dependOnGroups
[0] ? "" : "(none)");
195 for (wptr
= entry
->dependOnGroups
; *wptr
; wptr
+= strlenW(wptr
) + 1)
196 WINE_TRACE(" * %s\n", wine_dbgstr_w(wptr
));
198 return ERROR_SUCCESS
;
201 static DWORD
reg_set_string_value(HKEY hKey
, LPCWSTR value_name
, LPCWSTR string
)
206 err
= RegDeleteValueW(hKey
, value_name
);
207 if (err
!= ERROR_FILE_NOT_FOUND
)
210 return ERROR_SUCCESS
;
213 return RegSetValueExW(hKey
, value_name
, 0, REG_SZ
, (const BYTE
*)string
, sizeof(WCHAR
)*(strlenW(string
) + 1));
216 static DWORD
reg_set_multisz_value(HKEY hKey
, LPCWSTR value_name
, LPCWSTR string
)
223 err
= RegDeleteValueW(hKey
, value_name
);
224 if (err
!= ERROR_FILE_NOT_FOUND
)
227 return ERROR_SUCCESS
;
231 while (*ptr
) ptr
+= strlenW(ptr
) + 1;
232 return RegSetValueExW(hKey
, value_name
, 0, REG_MULTI_SZ
, (const BYTE
*)string
, sizeof(WCHAR
)*(ptr
- string
+ 1));
235 DWORD
save_service_config(struct service_entry
*entry
)
240 err
= RegCreateKeyW(entry
->db
->root_key
, entry
->name
, &hKey
);
241 if (err
!= ERROR_SUCCESS
)
244 if ((err
= reg_set_string_value(hKey
, SZ_DISPLAY_NAME
, entry
->config
.lpDisplayName
)) != 0)
246 if ((err
= reg_set_string_value(hKey
, SZ_IMAGE_PATH
, entry
->config
.lpBinaryPathName
)) != 0)
248 if ((err
= reg_set_string_value(hKey
, SZ_GROUP
, entry
->config
.lpLoadOrderGroup
)) != 0)
250 if ((err
= reg_set_string_value(hKey
, SZ_OBJECT_NAME
, entry
->config
.lpServiceStartName
)) != 0)
252 if ((err
= reg_set_string_value(hKey
, SZ_DESCRIPTION
, entry
->description
)) != 0)
254 if ((err
= reg_set_multisz_value(hKey
, SZ_DEPEND_ON_SERVICE
, entry
->dependOnServices
)) != 0)
256 if ((err
= reg_set_multisz_value(hKey
, SZ_DEPEND_ON_GROUP
, entry
->dependOnGroups
)) != 0)
258 if ((err
= RegSetValueExW(hKey
, SZ_START
, 0, REG_DWORD
, (LPBYTE
)&entry
->config
.dwStartType
, sizeof(DWORD
))) != 0)
260 if ((err
= RegSetValueExW(hKey
, SZ_ERROR
, 0, REG_DWORD
, (LPBYTE
)&entry
->config
.dwErrorControl
, sizeof(DWORD
))) != 0)
262 if ((err
= RegSetValueExW(hKey
, SZ_TYPE
, 0, REG_DWORD
, (LPBYTE
)&entry
->config
.dwServiceType
, sizeof(DWORD
))) != 0)
264 if ((err
= RegSetValueExW(hKey
, SZ_PRESHUTDOWN
, 0, REG_DWORD
, (LPBYTE
)&entry
->preshutdown_timeout
, sizeof(DWORD
))) != 0)
266 if ((err
= RegSetValueExW(hKey
, SZ_PRESHUTDOWN
, 0, REG_DWORD
, (LPBYTE
)&entry
->preshutdown_timeout
, sizeof(DWORD
))) != 0)
270 const DWORD is_wow64
= 1;
271 if ((err
= RegSetValueExW(hKey
, SZ_WOW64
, 0, REG_DWORD
, (LPBYTE
)&is_wow64
, sizeof(DWORD
))) != 0)
275 if (entry
->config
.dwTagId
)
276 err
= RegSetValueExW(hKey
, SZ_TAG
, 0, REG_DWORD
, (LPBYTE
)&entry
->config
.dwTagId
, sizeof(DWORD
));
278 err
= RegDeleteValueW(hKey
, SZ_TAG
);
280 if (err
!= 0 && err
!= ERROR_FILE_NOT_FOUND
)
289 static void scmdatabase_add_process(struct scmdatabase
*db
, struct process_entry
*process
)
292 list_add_tail(&db
->processes
, &process
->entry
);
295 static void scmdatabase_remove_process(struct scmdatabase
*db
, struct process_entry
*process
)
297 list_remove(&process
->entry
);
298 process
->entry
.next
= process
->entry
.prev
= NULL
;
301 DWORD
scmdatabase_add_service(struct scmdatabase
*db
, struct service_entry
*service
)
305 if ((err
= save_service_config(service
)) != ERROR_SUCCESS
)
307 WINE_ERR("Couldn't store service configuration: error %u\n", err
);
308 return ERROR_GEN_FAILURE
;
311 list_add_tail(&db
->services
, &service
->entry
);
312 return ERROR_SUCCESS
;
315 static void scmdatabase_remove_service(struct scmdatabase
*db
, struct service_entry
*service
)
317 RegDeleteTreeW(db
->root_key
, service
->name
);
318 list_remove(&service
->entry
);
319 service
->entry
.next
= service
->entry
.prev
= NULL
;
322 static int compare_tags(const void *a
, const void *b
)
324 struct service_entry
*service_a
= *(struct service_entry
**)a
;
325 struct service_entry
*service_b
= *(struct service_entry
**)b
;
326 return service_a
->config
.dwTagId
- service_b
->config
.dwTagId
;
329 static void scmdatabase_autostart_services(struct scmdatabase
*db
)
331 struct service_entry
**services_list
;
333 unsigned int size
= 32;
334 struct service_entry
*service
;
336 services_list
= HeapAlloc(GetProcessHeap(), 0, size
* sizeof(services_list
[0]));
340 scmdatabase_lock(db
);
342 LIST_FOR_EACH_ENTRY(service
, &db
->services
, struct service_entry
, entry
)
344 if (service
->config
.dwStartType
== SERVICE_BOOT_START
||
345 service
->config
.dwStartType
== SERVICE_SYSTEM_START
||
346 service
->config
.dwStartType
== SERVICE_AUTO_START
)
350 struct service_entry
**slist_new
;
352 slist_new
= HeapReAlloc(GetProcessHeap(), 0, services_list
, size
* sizeof(services_list
[0]));
355 services_list
= slist_new
;
357 services_list
[i
++] = grab_service(service
);
362 scmdatabase_unlock(db
);
363 qsort(services_list
, size
, sizeof(services_list
[0]), compare_tags
);
364 while (!scmdatabase_lock_startup(db
)) Sleep(10);
366 for (i
= 0; i
< size
; i
++)
369 service
= services_list
[i
];
370 err
= service_start(service
, 0, NULL
);
371 if (err
!= ERROR_SUCCESS
)
372 WINE_FIXME("Auto-start service %s failed to start: %d\n",
373 wine_dbgstr_w(service
->name
), err
);
374 release_service(service
);
377 scmdatabase_unlock_startup(db
);
378 HeapFree(GetProcessHeap(), 0, services_list
);
381 static void scmdatabase_wait_terminate(struct scmdatabase
*db
)
383 struct list pending
= LIST_INIT(pending
);
386 scmdatabase_lock(db
);
387 list_move_tail(&pending
, &db
->processes
);
388 while ((ptr
= list_head(&pending
)))
390 struct process_entry
*process
= grab_process(LIST_ENTRY(ptr
, struct process_entry
, entry
));
392 scmdatabase_unlock(db
);
393 WaitForSingleObject(process
->process
, INFINITE
);
394 scmdatabase_lock(db
);
396 list_remove(&process
->entry
);
397 list_add_tail(&db
->processes
, &process
->entry
);
398 release_process(process
);
400 scmdatabase_unlock(db
);
403 BOOL
validate_service_name(LPCWSTR name
)
405 return (name
&& name
[0] && !strchrW(name
, '/') && !strchrW(name
, '\\'));
408 BOOL
validate_service_config(struct service_entry
*entry
)
410 if (entry
->config
.dwServiceType
& SERVICE_WIN32
&& (entry
->config
.lpBinaryPathName
== NULL
|| !entry
->config
.lpBinaryPathName
[0]))
412 WINE_ERR("Service %s is Win32 but has no image path set\n", wine_dbgstr_w(entry
->name
));
416 switch (entry
->config
.dwServiceType
)
418 case SERVICE_KERNEL_DRIVER
:
419 case SERVICE_FILE_SYSTEM_DRIVER
:
420 case SERVICE_WIN32_OWN_PROCESS
:
421 case SERVICE_WIN32_SHARE_PROCESS
:
424 case SERVICE_WIN32_OWN_PROCESS
| SERVICE_INTERACTIVE_PROCESS
:
425 case SERVICE_WIN32_SHARE_PROCESS
| SERVICE_INTERACTIVE_PROCESS
:
426 /* These can be only run as LocalSystem */
427 if (entry
->config
.lpServiceStartName
&& strcmpiW(entry
->config
.lpServiceStartName
, SZ_LOCAL_SYSTEM
) != 0)
429 WINE_ERR("Service %s is interactive but has a start name\n", wine_dbgstr_w(entry
->name
));
434 WINE_ERR("Service %s has an unknown service type (0x%x)\n", wine_dbgstr_w(entry
->name
), entry
->config
.dwServiceType
);
438 /* StartType can only be a single value (if several values are mixed the result is probably not what was intended) */
439 if (entry
->config
.dwStartType
> SERVICE_DISABLED
)
441 WINE_ERR("Service %s has an unknown start type\n", wine_dbgstr_w(entry
->name
));
445 /* SERVICE_BOOT_START and SERVICE_SYSTEM_START are only allowed for driver services */
446 if (((entry
->config
.dwStartType
== SERVICE_BOOT_START
) || (entry
->config
.dwStartType
== SERVICE_SYSTEM_START
)) &&
447 ((entry
->config
.dwServiceType
& SERVICE_WIN32_OWN_PROCESS
) || (entry
->config
.dwServiceType
& SERVICE_WIN32_SHARE_PROCESS
)))
449 WINE_ERR("Service %s - SERVICE_BOOT_START and SERVICE_SYSTEM_START are only allowed for driver services\n", wine_dbgstr_w(entry
->name
));
453 if (entry
->config
.lpServiceStartName
== NULL
)
454 entry
->config
.lpServiceStartName
= strdupW(SZ_LOCAL_SYSTEM
);
460 struct service_entry
*scmdatabase_find_service(struct scmdatabase
*db
, LPCWSTR name
)
462 struct service_entry
*service
;
464 LIST_FOR_EACH_ENTRY(service
, &db
->services
, struct service_entry
, entry
)
466 if (strcmpiW(name
, service
->name
) == 0)
473 struct service_entry
*scmdatabase_find_service_by_displayname(struct scmdatabase
*db
, LPCWSTR name
)
475 struct service_entry
*service
;
477 LIST_FOR_EACH_ENTRY(service
, &db
->services
, struct service_entry
, entry
)
479 if (service
->config
.lpDisplayName
&& strcmpiW(name
, service
->config
.lpDisplayName
) == 0)
486 struct process_entry
*grab_process(struct process_entry
*process
)
489 InterlockedIncrement(&process
->ref_count
);
493 void release_process(struct process_entry
*process
)
495 struct scmdatabase
*db
= process
->db
;
497 scmdatabase_lock(db
);
498 if (InterlockedDecrement(&process
->ref_count
) == 0)
500 scmdatabase_remove_process(db
, process
);
501 free_process_entry(process
);
503 scmdatabase_unlock(db
);
506 struct service_entry
*grab_service(struct service_entry
*service
)
509 InterlockedIncrement(&service
->ref_count
);
513 void release_service(struct service_entry
*service
)
515 struct scmdatabase
*db
= service
->db
;
517 scmdatabase_lock(db
);
518 if (InterlockedDecrement(&service
->ref_count
) == 0 && is_marked_for_delete(service
))
520 scmdatabase_remove_service(db
, service
);
521 free_service_entry(service
);
523 scmdatabase_unlock(db
);
526 static DWORD
scmdatabase_create(struct scmdatabase
**db
)
530 *db
= HeapAlloc(GetProcessHeap(), 0, sizeof(**db
));
532 return ERROR_NOT_ENOUGH_SERVER_MEMORY
;
534 (*db
)->service_start_lock
= FALSE
;
535 list_init(&(*db
)->processes
);
536 list_init(&(*db
)->services
);
538 InitializeCriticalSection(&(*db
)->cs
);
539 (*db
)->cs
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": scmdatabase");
541 err
= RegCreateKeyExW(HKEY_LOCAL_MACHINE
, SZ_SERVICES_KEY
, 0, NULL
,
542 REG_OPTION_NON_VOLATILE
, MAXIMUM_ALLOWED
, NULL
,
543 &(*db
)->root_key
, NULL
);
544 if (err
!= ERROR_SUCCESS
)
545 HeapFree(GetProcessHeap(), 0, *db
);
550 static void scmdatabase_destroy(struct scmdatabase
*db
)
552 RegCloseKey(db
->root_key
);
553 db
->cs
.DebugInfo
->Spare
[0] = 0;
554 DeleteCriticalSection(&db
->cs
);
555 HeapFree(GetProcessHeap(), 0, db
);
558 static DWORD
scmdatabase_load_services(struct scmdatabase
*db
)
563 for (i
= 0; TRUE
; i
++)
565 WCHAR szName
[MAX_SERVICE_NAME
];
566 struct service_entry
*entry
;
569 err
= RegEnumKeyW(db
->root_key
, i
, szName
, MAX_SERVICE_NAME
);
570 if (err
== ERROR_NO_MORE_ITEMS
)
575 WINE_ERR("Error %d reading key %d name - skipping\n", err
, i
);
579 err
= service_create(szName
, &entry
);
580 if (err
!= ERROR_SUCCESS
)
583 WINE_TRACE("Loading service %s\n", wine_dbgstr_w(szName
));
584 err
= RegOpenKeyExW(db
->root_key
, szName
, 0, KEY_READ
, &hServiceKey
);
585 if (err
== ERROR_SUCCESS
)
587 err
= load_service_config(hServiceKey
, entry
);
588 RegCloseKey(hServiceKey
);
591 if (err
!= ERROR_SUCCESS
)
593 WINE_ERR("Error %d reading registry key for service %s - skipping\n", err
, wine_dbgstr_w(szName
));
594 free_service_entry(entry
);
598 if (entry
->config
.dwServiceType
== 0)
600 /* Maybe an application only wrote some configuration in the service key. Continue silently */
601 WINE_TRACE("Even the service type not set for service %s - skipping\n", wine_dbgstr_w(szName
));
602 free_service_entry(entry
);
606 if (!validate_service_config(entry
))
608 WINE_ERR("Invalid configuration of service %s - skipping\n", wine_dbgstr_w(szName
));
609 free_service_entry(entry
);
613 entry
->status
.dwServiceType
= entry
->config
.dwServiceType
;
616 list_add_tail(&db
->services
, &entry
->entry
);
617 release_service(entry
);
619 return ERROR_SUCCESS
;
622 BOOL
scmdatabase_lock_startup(struct scmdatabase
*db
)
624 return !InterlockedCompareExchange(&db
->service_start_lock
, TRUE
, FALSE
);
627 void scmdatabase_unlock_startup(struct scmdatabase
*db
)
629 InterlockedCompareExchange(&db
->service_start_lock
, FALSE
, TRUE
);
632 void scmdatabase_lock(struct scmdatabase
*db
)
634 EnterCriticalSection(&db
->cs
);
637 void scmdatabase_unlock(struct scmdatabase
*db
)
639 LeaveCriticalSection(&db
->cs
);
642 void service_lock(struct service_entry
*service
)
644 EnterCriticalSection(&service
->db
->cs
);
647 void service_unlock(struct service_entry
*service
)
649 LeaveCriticalSection(&service
->db
->cs
);
652 /* only one service started at a time, so there is no race on the registry
654 static LPWSTR
service_get_pipe_name(void)
656 static const WCHAR format
[] = { '\\','\\','.','\\','p','i','p','e','\\',
657 'n','e','t','\\','N','t','C','o','n','t','r','o','l','P','i','p','e','%','u',0};
658 static WCHAR name
[sizeof(format
)/sizeof(WCHAR
) + 10]; /* strlenW("4294967295") */
659 static DWORD service_current
= 0;
660 DWORD len
, value
= -1;
665 ret
= RegQueryValueExW(service_current_key
, NULL
, NULL
, &type
,
666 (BYTE
*)&value
, &len
);
667 if (ret
== ERROR_SUCCESS
&& type
== REG_DWORD
)
668 service_current
= max(service_current
, value
+ 1);
669 RegSetValueExW(service_current_key
, NULL
, 0, REG_DWORD
,
670 (BYTE
*)&service_current
, sizeof(service_current
));
671 sprintfW(name
, format
, service_current
);
676 static DWORD
get_service_binary_path(const struct service_entry
*service_entry
, WCHAR
**path
)
678 DWORD size
= ExpandEnvironmentStringsW(service_entry
->config
.lpBinaryPathName
, NULL
, 0);
680 *path
= HeapAlloc(GetProcessHeap(), 0, size
*sizeof(WCHAR
));
682 return ERROR_NOT_ENOUGH_SERVER_MEMORY
;
684 ExpandEnvironmentStringsW(service_entry
->config
.lpBinaryPathName
, *path
, size
);
686 /* if service image is configured to systemdir, redirect it to wow64 systemdir */
687 if (service_entry
->is_wow64
)
689 WCHAR system_dir
[MAX_PATH
], *redirected
;
692 GetSystemDirectoryW( system_dir
, MAX_PATH
);
693 len
= strlenW( system_dir
);
695 if (strncmpiW( system_dir
, *path
, len
))
696 return ERROR_SUCCESS
;
698 GetSystemWow64DirectoryW( system_dir
, MAX_PATH
);
700 redirected
= HeapAlloc( GetProcessHeap(), 0, (strlenW( *path
) + strlenW( system_dir
))*sizeof(WCHAR
));
703 HeapFree( GetProcessHeap(), 0, *path
);
704 return ERROR_NOT_ENOUGH_SERVER_MEMORY
;
707 strcpyW( redirected
, system_dir
);
708 strcatW( redirected
, &(*path
)[len
] );
709 HeapFree( GetProcessHeap(), 0, *path
);
711 TRACE("redirected to %s\n", debugstr_w(redirected
));
714 return ERROR_SUCCESS
;
717 static DWORD
get_winedevice_binary_path(WCHAR
**path
, BOOL
*is_wow64
)
719 static const WCHAR winedeviceW
[] = {'\\','w','i','n','e','d','e','v','i','c','e','.','e','x','e',0};
720 WCHAR system_dir
[MAX_PATH
];
725 else if (GetBinaryTypeW(*path
, &type
))
726 *is_wow64
= (type
== SCS_32BIT_BINARY
);
728 return GetLastError();
730 GetSystemDirectoryW(system_dir
, MAX_PATH
);
731 HeapFree(GetProcessHeap(), 0, *path
);
732 if (!(*path
= HeapAlloc(GetProcessHeap(), 0, strlenW(system_dir
) * sizeof(WCHAR
) + sizeof(winedeviceW
))))
733 return ERROR_NOT_ENOUGH_SERVER_MEMORY
;
735 strcpyW(*path
, system_dir
);
736 strcatW(*path
, winedeviceW
);
737 return ERROR_SUCCESS
;
740 static struct process_entry
*get_winedevice_process(struct service_entry
*service_entry
, WCHAR
*path
, BOOL is_wow64
)
742 struct service_entry
*winedevice_entry
;
744 if (!service_entry
->config
.lpLoadOrderGroup
)
747 LIST_FOR_EACH_ENTRY(winedevice_entry
, &service_entry
->db
->services
, struct service_entry
, entry
)
749 if (winedevice_entry
->status
.dwCurrentState
!= SERVICE_START_PENDING
&&
750 winedevice_entry
->status
.dwCurrentState
!= SERVICE_RUNNING
) continue;
751 if (!winedevice_entry
->process
) continue;
753 if (winedevice_entry
->is_wow64
!= is_wow64
) continue;
754 if (!winedevice_entry
->config
.lpBinaryPathName
) continue;
755 if (strcmpW(winedevice_entry
->config
.lpBinaryPathName
, path
)) continue;
757 if (!winedevice_entry
->config
.lpLoadOrderGroup
) continue;
758 if (strcmpW(winedevice_entry
->config
.lpLoadOrderGroup
, service_entry
->config
.lpLoadOrderGroup
)) continue;
760 return grab_process(winedevice_entry
->process
);
766 static DWORD
add_winedevice_service(const struct service_entry
*service
, WCHAR
*path
, BOOL is_wow64
,
767 struct service_entry
**entry
)
769 static const WCHAR format
[] = {'W','i','n','e','d','e','v','i','c','e','%','u',0};
770 static WCHAR name
[sizeof(format
)/sizeof(WCHAR
) + 10]; /* strlenW("4294967295") */
771 static DWORD current
= 0;
772 struct scmdatabase
*db
= service
->db
;
777 sprintfW(name
, format
, ++current
);
778 if (!scmdatabase_find_service(db
, name
)) break;
781 err
= service_create(name
, entry
);
782 if (err
!= ERROR_SUCCESS
)
785 (*entry
)->is_wow64
= is_wow64
;
786 (*entry
)->config
.dwServiceType
= SERVICE_WIN32_OWN_PROCESS
;
787 (*entry
)->config
.dwStartType
= SERVICE_DEMAND_START
;
788 (*entry
)->status
.dwServiceType
= (*entry
)->config
.dwServiceType
;
790 if (!((*entry
)->config
.lpBinaryPathName
= strdupW(path
)))
792 if (!((*entry
)->config
.lpServiceStartName
= strdupW(SZ_LOCAL_SYSTEM
)))
794 if (!((*entry
)->config
.lpDisplayName
= strdupW(name
)))
796 if (service
->config
.lpLoadOrderGroup
&&
797 !((*entry
)->config
.lpLoadOrderGroup
= strdupW(service
->config
.lpLoadOrderGroup
)))
802 list_add_tail(&db
->services
, &(*entry
)->entry
);
803 mark_for_delete(*entry
);
804 return ERROR_SUCCESS
;
807 free_service_entry(*entry
);
808 return ERROR_NOT_ENOUGH_SERVER_MEMORY
;
811 static DWORD
service_start_process(struct service_entry
*service_entry
, struct process_entry
**new_process
,
812 BOOL
*shared_process
)
814 struct process_entry
*process
;
815 PROCESS_INFORMATION pi
;
817 BOOL is_wow64
= FALSE
;
823 service_lock(service_entry
);
825 if ((process
= service_entry
->process
))
827 if (WaitForSingleObject(process
->process
, 0) == WAIT_TIMEOUT
)
829 service_unlock(service_entry
);
830 return ERROR_SERVICE_ALREADY_RUNNING
;
832 service_entry
->process
= NULL
;
833 process
->use_count
--;
834 release_process(process
);
837 service_entry
->force_shutdown
= FALSE
;
839 if ((err
= get_service_binary_path(service_entry
, &path
)))
841 service_unlock(service_entry
);
845 if (service_entry
->config
.dwServiceType
== SERVICE_KERNEL_DRIVER
||
846 service_entry
->config
.dwServiceType
== SERVICE_FILE_SYSTEM_DRIVER
)
848 struct service_entry
*winedevice_entry
;
851 if ((err
= get_winedevice_binary_path(&path
, &is_wow64
)))
853 service_unlock(service_entry
);
854 HeapFree(GetProcessHeap(), 0, path
);
858 if ((process
= get_winedevice_process(service_entry
, path
, is_wow64
)))
860 HeapFree(GetProcessHeap(), 0, path
);
864 err
= add_winedevice_service(service_entry
, path
, is_wow64
, &winedevice_entry
);
865 HeapFree(GetProcessHeap(), 0, path
);
866 if (err
!= ERROR_SUCCESS
)
868 service_unlock(service_entry
);
872 group
= strdupW(winedevice_entry
->config
.lpLoadOrderGroup
);
873 service_unlock(service_entry
);
875 err
= service_start(winedevice_entry
, group
!= NULL
, (const WCHAR
**)&group
);
876 HeapFree(GetProcessHeap(), 0, group
);
877 if (err
!= ERROR_SUCCESS
)
879 release_service(winedevice_entry
);
883 service_lock(service_entry
);
884 process
= grab_process(winedevice_entry
->process
);
885 release_service(winedevice_entry
);
889 service_unlock(service_entry
);
890 return ERROR_SERVICE_REQUEST_TIMEOUT
;
894 service_entry
->status
.dwCurrentState
= SERVICE_START_PENDING
;
895 service_entry
->status
.dwControlsAccepted
= 0;
896 ResetEvent(service_entry
->status_changed_event
);
898 service_entry
->process
= grab_process(process
);
899 service_entry
->shared_process
= *shared_process
= TRUE
;
900 process
->use_count
++;
901 service_unlock(service_entry
);
903 err
= WaitForSingleObject(process
->control_mutex
, 30000);
904 if (err
!= WAIT_OBJECT_0
)
906 release_process(process
);
907 return ERROR_SERVICE_REQUEST_TIMEOUT
;
910 *new_process
= process
;
911 return ERROR_SUCCESS
;
914 if ((err
= process_create(service_get_pipe_name(), &process
)))
916 WINE_ERR("failed to create process object for %s, error = %u\n",
917 wine_dbgstr_w(service_entry
->name
), err
);
918 service_unlock(service_entry
);
919 HeapFree(GetProcessHeap(), 0, path
);
923 ZeroMemory(&si
, sizeof(STARTUPINFOW
));
924 si
.cb
= sizeof(STARTUPINFOW
);
925 if (!(service_entry
->config
.dwServiceType
& SERVICE_INTERACTIVE_PROCESS
))
927 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};
928 si
.lpDesktop
= desktopW
;
931 if (!environment
&& OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY
| TOKEN_DUPLICATE
, &token
))
933 CreateEnvironmentBlock(&environment
, token
, FALSE
);
937 service_entry
->status
.dwCurrentState
= SERVICE_START_PENDING
;
938 service_entry
->status
.dwControlsAccepted
= 0;
939 ResetEvent(service_entry
->status_changed_event
);
941 scmdatabase_add_process(service_entry
->db
, process
);
942 service_entry
->process
= grab_process(process
);
943 service_entry
->shared_process
= *shared_process
= FALSE
;
944 process
->use_count
++;
945 service_unlock(service_entry
);
947 r
= CreateProcessW(NULL
, path
, NULL
, NULL
, FALSE
, CREATE_UNICODE_ENVIRONMENT
, environment
, NULL
, &si
, &pi
);
948 HeapFree(GetProcessHeap(), 0, path
);
951 err
= GetLastError();
952 process_terminate(process
);
953 release_process(process
);
957 process
->process_id
= pi
.dwProcessId
;
958 process
->process
= pi
.hProcess
;
959 CloseHandle( pi
.hThread
);
961 *new_process
= process
;
962 return ERROR_SUCCESS
;
965 static DWORD
service_wait_for_startup(struct service_entry
*service
, struct process_entry
*process
)
967 HANDLE handles
[2] = { service
->status_changed_event
, process
->process
};
970 result
= WaitForMultipleObjects( 2, handles
, FALSE
, service_pipe_timeout
);
971 if (result
!= WAIT_OBJECT_0
)
972 return ERROR_SERVICE_REQUEST_TIMEOUT
;
974 service_lock(service
);
975 result
= service
->status
.dwCurrentState
;
976 service_unlock(service
);
978 return (result
== SERVICE_START_PENDING
|| result
== SERVICE_RUNNING
) ?
979 ERROR_SUCCESS
: ERROR_SERVICE_REQUEST_TIMEOUT
;
982 /******************************************************************************
983 * process_send_start_message
985 static DWORD
process_send_start_message(struct process_entry
*process
, BOOL shared_process
,
986 const WCHAR
*name
, const WCHAR
**argv
, DWORD argc
)
988 OVERLAPPED overlapped
;
989 DWORD i
, len
, result
;
992 WINE_TRACE("%p %s %p %d\n", process
, wine_dbgstr_w(name
), argv
, argc
);
994 overlapped
.hEvent
= process
->overlapped_event
;
995 if (!ConnectNamedPipe(process
->control_pipe
, &overlapped
))
997 if (GetLastError() == ERROR_IO_PENDING
)
1000 handles
[0] = process
->overlapped_event
;
1001 handles
[1] = process
->process
;
1002 if (WaitForMultipleObjects( 2, handles
, FALSE
, service_pipe_timeout
) != WAIT_OBJECT_0
)
1003 CancelIo(process
->control_pipe
);
1004 if (!HasOverlappedIoCompleted( &overlapped
))
1006 WINE_ERR("service %s failed to start\n", wine_dbgstr_w(name
));
1007 return ERROR_SERVICE_REQUEST_TIMEOUT
;
1010 else if (GetLastError() != ERROR_PIPE_CONNECTED
)
1012 WINE_ERR("pipe connect failed\n");
1013 return ERROR_SERVICE_REQUEST_TIMEOUT
;
1017 len
= strlenW(name
) + 1;
1018 for (i
= 0; i
< argc
; i
++)
1019 len
+= strlenW(argv
[i
])+1;
1020 len
= (len
+ 1) * sizeof(WCHAR
);
1022 if (!(str
= HeapAlloc(GetProcessHeap(), 0, len
)))
1023 return ERROR_NOT_ENOUGH_SERVER_MEMORY
;
1027 p
+= strlenW(name
) + 1;
1028 for (i
= 0; i
< argc
; i
++)
1030 strcpyW(p
, argv
[i
]);
1031 p
+= strlenW(p
) + 1;
1035 if (!process_send_control(process
, shared_process
, name
,
1036 SERVICE_CONTROL_START
, (const BYTE
*)str
, len
, &result
))
1037 result
= ERROR_SERVICE_REQUEST_TIMEOUT
;
1039 HeapFree(GetProcessHeap(), 0, str
);
1043 DWORD
service_start(struct service_entry
*service
, DWORD service_argc
, LPCWSTR
*service_argv
)
1045 struct process_entry
*process
= NULL
;
1046 BOOL shared_process
;
1049 err
= service_start_process(service
, &process
, &shared_process
);
1050 if (err
== ERROR_SUCCESS
)
1052 err
= process_send_start_message(process
, shared_process
, service
->name
, service_argv
, service_argc
);
1054 if (err
== ERROR_SUCCESS
)
1055 err
= service_wait_for_startup(service
, process
);
1057 if (err
!= ERROR_SUCCESS
)
1059 service_lock(service
);
1060 service
->status
.dwCurrentState
= SERVICE_STOPPED
;
1061 service
->process
= NULL
;
1062 if (!--process
->use_count
) process_terminate(process
);
1063 release_process(process
);
1064 service_unlock(service
);
1067 ReleaseMutex(process
->control_mutex
);
1068 release_process(process
);
1071 WINE_TRACE("returning %d\n", err
);
1075 void process_terminate(struct process_entry
*process
)
1077 struct scmdatabase
*db
= process
->db
;
1078 struct service_entry
*service
;
1080 scmdatabase_lock(db
);
1081 TerminateProcess(process
->process
, 0);
1082 LIST_FOR_EACH_ENTRY(service
, &db
->services
, struct service_entry
, entry
)
1084 if (service
->process
!= process
) continue;
1085 service
->status
.dwCurrentState
= SERVICE_STOPPED
;
1086 service
->process
= NULL
;
1087 process
->use_count
--;
1088 release_process(process
);
1090 scmdatabase_unlock(db
);
1093 static void load_registry_parameters(void)
1095 static const WCHAR controlW
[] =
1096 { 'S','y','s','t','e','m','\\',
1097 'C','u','r','r','e','n','t','C','o','n','t','r','o','l','S','e','t','\\',
1098 'C','o','n','t','r','o','l',0 };
1099 static const WCHAR pipetimeoutW
[] =
1100 {'S','e','r','v','i','c','e','s','P','i','p','e','T','i','m','e','o','u','t',0};
1101 static const WCHAR killtimeoutW
[] =
1102 {'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};
1105 DWORD type
, count
, val
;
1107 if (RegOpenKeyW( HKEY_LOCAL_MACHINE
, controlW
, &key
)) return;
1109 count
= sizeof(buffer
);
1110 if (!RegQueryValueExW( key
, pipetimeoutW
, NULL
, &type
, (BYTE
*)buffer
, &count
) &&
1111 type
== REG_SZ
&& (val
= atoiW( buffer
)))
1112 service_pipe_timeout
= val
;
1114 count
= sizeof(buffer
);
1115 if (!RegQueryValueExW( key
, killtimeoutW
, NULL
, &type
, (BYTE
*)buffer
, &count
) &&
1116 type
== REG_SZ
&& (val
= atoiW( buffer
)))
1117 service_kill_timeout
= val
;
1122 int main(int argc
, char *argv
[])
1124 static const WCHAR service_current_key_str
[] = { 'S','Y','S','T','E','M','\\',
1125 'C','u','r','r','e','n','t','C','o','n','t','r','o','l','S','e','t','\\',
1126 'C','o','n','t','r','o','l','\\',
1127 'S','e','r','v','i','c','e','C','u','r','r','e','n','t',0};
1128 static const WCHAR svcctl_started_event
[] = SVCCTL_STARTED_EVENT
;
1129 HANDLE started_event
;
1132 started_event
= CreateEventW(NULL
, TRUE
, FALSE
, svcctl_started_event
);
1134 err
= RegCreateKeyExW(HKEY_LOCAL_MACHINE
, service_current_key_str
, 0,
1135 NULL
, REG_OPTION_VOLATILE
, KEY_SET_VALUE
| KEY_QUERY_VALUE
, NULL
,
1136 &service_current_key
, NULL
);
1137 if (err
!= ERROR_SUCCESS
)
1140 load_registry_parameters();
1141 err
= scmdatabase_create(&active_database
);
1142 if (err
!= ERROR_SUCCESS
)
1144 if ((err
= scmdatabase_load_services(active_database
)) != ERROR_SUCCESS
)
1146 if ((err
= RPC_Init()) == ERROR_SUCCESS
)
1148 scmdatabase_autostart_services(active_database
);
1149 SetEvent(started_event
);
1150 WaitForSingleObject(exit_event
, INFINITE
);
1151 scmdatabase_wait_terminate(active_database
);
1154 scmdatabase_destroy(active_database
);
1156 DestroyEnvironmentBlock(environment
);
1158 WINE_TRACE("services.exe exited with code %d\n", err
);