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 HANDLE g_hStartedEvent
;
40 struct scmdatabase
*active_database
;
42 DWORD service_pipe_timeout
= 10000;
43 DWORD service_kill_timeout
= 60000;
44 static DWORD default_preshutdown_timeout
= 180000;
45 static void *env
= 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(struct process_entry
**entry
)
73 *entry
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(**entry
));
75 return ERROR_NOT_ENOUGH_SERVER_MEMORY
;
76 (*entry
)->control_pipe
= INVALID_HANDLE_VALUE
;
77 /* all other fields are zero */
81 static void free_process_entry(struct process_entry
*entry
)
83 CloseHandle(entry
->process
);
84 CloseHandle(entry
->control_mutex
);
85 CloseHandle(entry
->control_pipe
);
86 CloseHandle(entry
->overlapped_event
);
87 CloseHandle(entry
->status_changed_event
);
88 HeapFree(GetProcessHeap(), 0, entry
);
91 DWORD
service_create(LPCWSTR name
, struct service_entry
**entry
)
95 *entry
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(**entry
));
97 return ERROR_NOT_ENOUGH_SERVER_MEMORY
;
98 (*entry
)->name
= strdupW(name
);
101 HeapFree(GetProcessHeap(), 0, *entry
);
102 return ERROR_NOT_ENOUGH_SERVER_MEMORY
;
104 if ((err
= process_create(&(*entry
)->process
)) != ERROR_SUCCESS
)
106 HeapFree(GetProcessHeap(), 0, (*entry
)->process
);
107 HeapFree(GetProcessHeap(), 0, *entry
);
110 (*entry
)->status
.dwCurrentState
= SERVICE_STOPPED
;
111 (*entry
)->status
.dwWin32ExitCode
= ERROR_SERVICE_NEVER_STARTED
;
112 (*entry
)->preshutdown_timeout
= default_preshutdown_timeout
;
113 /* all other fields are zero */
114 return ERROR_SUCCESS
;
117 void free_service_entry(struct service_entry
*entry
)
119 HeapFree(GetProcessHeap(), 0, entry
->name
);
120 HeapFree(GetProcessHeap(), 0, entry
->config
.lpBinaryPathName
);
121 HeapFree(GetProcessHeap(), 0, entry
->config
.lpDependencies
);
122 HeapFree(GetProcessHeap(), 0, entry
->config
.lpLoadOrderGroup
);
123 HeapFree(GetProcessHeap(), 0, entry
->config
.lpServiceStartName
);
124 HeapFree(GetProcessHeap(), 0, entry
->config
.lpDisplayName
);
125 HeapFree(GetProcessHeap(), 0, entry
->description
);
126 HeapFree(GetProcessHeap(), 0, entry
->dependOnServices
);
127 HeapFree(GetProcessHeap(), 0, entry
->dependOnGroups
);
128 free_process_entry(entry
->process
);
129 HeapFree(GetProcessHeap(), 0, entry
);
132 static DWORD
load_service_config(HKEY hKey
, struct service_entry
*entry
)
134 DWORD err
, value
= 0;
137 if ((err
= load_reg_string(hKey
, SZ_IMAGE_PATH
, TRUE
, &entry
->config
.lpBinaryPathName
)) != 0)
139 if ((err
= load_reg_string(hKey
, SZ_GROUP
, 0, &entry
->config
.lpLoadOrderGroup
)) != 0)
141 if ((err
= load_reg_string(hKey
, SZ_OBJECT_NAME
, TRUE
, &entry
->config
.lpServiceStartName
)) != 0)
143 if ((err
= load_reg_string(hKey
, SZ_DISPLAY_NAME
, 0, &entry
->config
.lpDisplayName
)) != 0)
145 if ((err
= load_reg_string(hKey
, SZ_DESCRIPTION
, 0, &entry
->description
)) != 0)
147 if ((err
= load_reg_multisz(hKey
, SZ_DEPEND_ON_SERVICE
, TRUE
, &entry
->dependOnServices
)) != 0)
149 if ((err
= load_reg_multisz(hKey
, SZ_DEPEND_ON_GROUP
, FALSE
, &entry
->dependOnGroups
)) != 0)
152 if ((err
= load_reg_dword(hKey
, SZ_TYPE
, &entry
->config
.dwServiceType
)) != 0)
154 if ((err
= load_reg_dword(hKey
, SZ_START
, &entry
->config
.dwStartType
)) != 0)
156 if ((err
= load_reg_dword(hKey
, SZ_ERROR
, &entry
->config
.dwErrorControl
)) != 0)
158 if ((err
= load_reg_dword(hKey
, SZ_TAG
, &entry
->config
.dwTagId
)) != 0)
160 if ((err
= load_reg_dword(hKey
, SZ_PRESHUTDOWN
, &entry
->preshutdown_timeout
)) != 0)
163 if (load_reg_dword(hKey
, SZ_WOW64
, &value
) == 0 && value
== 1)
164 entry
->is_wow64
= TRUE
;
166 WINE_TRACE("Image path = %s\n", wine_dbgstr_w(entry
->config
.lpBinaryPathName
) );
167 WINE_TRACE("Group = %s\n", wine_dbgstr_w(entry
->config
.lpLoadOrderGroup
) );
168 WINE_TRACE("Service account name = %s\n", wine_dbgstr_w(entry
->config
.lpServiceStartName
) );
169 WINE_TRACE("Display name = %s\n", wine_dbgstr_w(entry
->config
.lpDisplayName
) );
170 WINE_TRACE("Service dependencies : %s\n", entry
->dependOnServices
[0] ? "" : "(none)");
171 for (wptr
= entry
->dependOnServices
; *wptr
; wptr
+= strlenW(wptr
) + 1)
172 WINE_TRACE(" * %s\n", wine_dbgstr_w(wptr
));
173 WINE_TRACE("Group dependencies : %s\n", entry
->dependOnGroups
[0] ? "" : "(none)");
174 for (wptr
= entry
->dependOnGroups
; *wptr
; wptr
+= strlenW(wptr
) + 1)
175 WINE_TRACE(" * %s\n", wine_dbgstr_w(wptr
));
177 return ERROR_SUCCESS
;
180 static DWORD
reg_set_string_value(HKEY hKey
, LPCWSTR value_name
, LPCWSTR string
)
185 err
= RegDeleteValueW(hKey
, value_name
);
186 if (err
!= ERROR_FILE_NOT_FOUND
)
189 return ERROR_SUCCESS
;
192 return RegSetValueExW(hKey
, value_name
, 0, REG_SZ
, (const BYTE
*)string
, sizeof(WCHAR
)*(strlenW(string
) + 1));
195 static DWORD
reg_set_multisz_value(HKEY hKey
, LPCWSTR value_name
, LPCWSTR string
)
202 err
= RegDeleteValueW(hKey
, value_name
);
203 if (err
!= ERROR_FILE_NOT_FOUND
)
206 return ERROR_SUCCESS
;
210 while (*ptr
) ptr
+= strlenW(ptr
) + 1;
211 return RegSetValueExW(hKey
, value_name
, 0, REG_MULTI_SZ
, (const BYTE
*)string
, sizeof(WCHAR
)*(ptr
- string
+ 1));
214 DWORD
save_service_config(struct service_entry
*entry
)
219 err
= RegCreateKeyW(entry
->db
->root_key
, entry
->name
, &hKey
);
220 if (err
!= ERROR_SUCCESS
)
223 if ((err
= reg_set_string_value(hKey
, SZ_DISPLAY_NAME
, entry
->config
.lpDisplayName
)) != 0)
225 if ((err
= reg_set_string_value(hKey
, SZ_IMAGE_PATH
, entry
->config
.lpBinaryPathName
)) != 0)
227 if ((err
= reg_set_string_value(hKey
, SZ_GROUP
, entry
->config
.lpLoadOrderGroup
)) != 0)
229 if ((err
= reg_set_string_value(hKey
, SZ_OBJECT_NAME
, entry
->config
.lpServiceStartName
)) != 0)
231 if ((err
= reg_set_string_value(hKey
, SZ_DESCRIPTION
, entry
->description
)) != 0)
233 if ((err
= reg_set_multisz_value(hKey
, SZ_DEPEND_ON_SERVICE
, entry
->dependOnServices
)) != 0)
235 if ((err
= reg_set_multisz_value(hKey
, SZ_DEPEND_ON_GROUP
, entry
->dependOnGroups
)) != 0)
237 if ((err
= RegSetValueExW(hKey
, SZ_START
, 0, REG_DWORD
, (LPBYTE
)&entry
->config
.dwStartType
, sizeof(DWORD
))) != 0)
239 if ((err
= RegSetValueExW(hKey
, SZ_ERROR
, 0, REG_DWORD
, (LPBYTE
)&entry
->config
.dwErrorControl
, sizeof(DWORD
))) != 0)
241 if ((err
= RegSetValueExW(hKey
, SZ_TYPE
, 0, REG_DWORD
, (LPBYTE
)&entry
->config
.dwServiceType
, sizeof(DWORD
))) != 0)
243 if ((err
= RegSetValueExW(hKey
, SZ_PRESHUTDOWN
, 0, REG_DWORD
, (LPBYTE
)&entry
->preshutdown_timeout
, sizeof(DWORD
))) != 0)
245 if ((err
= RegSetValueExW(hKey
, SZ_PRESHUTDOWN
, 0, REG_DWORD
, (LPBYTE
)&entry
->preshutdown_timeout
, sizeof(DWORD
))) != 0)
249 const DWORD is_wow64
= 1;
250 if ((err
= RegSetValueExW(hKey
, SZ_WOW64
, 0, REG_DWORD
, (LPBYTE
)&is_wow64
, sizeof(DWORD
))) != 0)
254 if (entry
->config
.dwTagId
)
255 err
= RegSetValueExW(hKey
, SZ_TAG
, 0, REG_DWORD
, (LPBYTE
)&entry
->config
.dwTagId
, sizeof(DWORD
));
257 err
= RegDeleteValueW(hKey
, SZ_TAG
);
259 if (err
!= 0 && err
!= ERROR_FILE_NOT_FOUND
)
268 DWORD
scmdatabase_add_service(struct scmdatabase
*db
, struct service_entry
*service
)
272 if ((err
= save_service_config(service
)) != ERROR_SUCCESS
)
274 WINE_ERR("Couldn't store service configuration: error %u\n", err
);
275 return ERROR_GEN_FAILURE
;
278 list_add_tail(&db
->services
, &service
->entry
);
279 return ERROR_SUCCESS
;
282 static DWORD
scmdatabase_remove_service(struct scmdatabase
*db
, struct service_entry
*service
)
286 err
= RegDeleteTreeW(db
->root_key
, service
->name
);
291 list_remove(&service
->entry
);
292 service
->entry
.next
= service
->entry
.prev
= NULL
;
293 return ERROR_SUCCESS
;
296 static void scmdatabase_autostart_services(struct scmdatabase
*db
)
298 struct service_entry
**services_list
;
300 unsigned int size
= 32;
301 struct service_entry
*service
;
303 services_list
= HeapAlloc(GetProcessHeap(), 0, size
* sizeof(services_list
[0]));
307 scmdatabase_lock(db
);
309 LIST_FOR_EACH_ENTRY(service
, &db
->services
, struct service_entry
, entry
)
311 if (service
->config
.dwStartType
== SERVICE_BOOT_START
||
312 service
->config
.dwStartType
== SERVICE_SYSTEM_START
||
313 service
->config
.dwStartType
== SERVICE_AUTO_START
)
317 struct service_entry
**slist_new
;
319 slist_new
= HeapReAlloc(GetProcessHeap(), 0, services_list
, size
* sizeof(services_list
[0]));
322 services_list
= slist_new
;
324 services_list
[i
] = service
;
325 InterlockedIncrement(&service
->ref_count
);
330 scmdatabase_unlock(db
);
333 for (i
= 0; i
< size
; i
++)
336 service
= services_list
[i
];
337 err
= service_start(service
, 0, NULL
);
338 if (err
!= ERROR_SUCCESS
)
339 WINE_FIXME("Auto-start service %s failed to start: %d\n",
340 wine_dbgstr_w(service
->name
), err
);
341 release_service(service
);
344 HeapFree(GetProcessHeap(), 0, services_list
);
347 static void scmdatabase_wait_terminate(struct scmdatabase
*db
)
349 struct service_entry
*service
;
352 scmdatabase_lock(db
);
356 LIST_FOR_EACH_ENTRY(service
, &db
->services
, struct service_entry
, entry
)
358 struct process_entry
*process
= service
->process
;
359 if (process
->process
)
361 scmdatabase_unlock(db
);
362 WaitForSingleObject(process
->process
, INFINITE
);
363 scmdatabase_lock(db
);
364 CloseHandle(process
->process
);
365 process
->process
= NULL
;
371 scmdatabase_unlock(db
);
374 BOOL
validate_service_name(LPCWSTR name
)
376 return (name
&& name
[0] && !strchrW(name
, '/') && !strchrW(name
, '\\'));
379 BOOL
validate_service_config(struct service_entry
*entry
)
381 if (entry
->config
.dwServiceType
& SERVICE_WIN32
&& (entry
->config
.lpBinaryPathName
== NULL
|| !entry
->config
.lpBinaryPathName
[0]))
383 WINE_ERR("Service %s is Win32 but has no image path set\n", wine_dbgstr_w(entry
->name
));
387 switch (entry
->config
.dwServiceType
)
389 case SERVICE_KERNEL_DRIVER
:
390 case SERVICE_FILE_SYSTEM_DRIVER
:
391 case SERVICE_WIN32_OWN_PROCESS
:
392 case SERVICE_WIN32_SHARE_PROCESS
:
395 case SERVICE_WIN32_OWN_PROCESS
| SERVICE_INTERACTIVE_PROCESS
:
396 case SERVICE_WIN32_SHARE_PROCESS
| SERVICE_INTERACTIVE_PROCESS
:
397 /* These can be only run as LocalSystem */
398 if (entry
->config
.lpServiceStartName
&& strcmpiW(entry
->config
.lpServiceStartName
, SZ_LOCAL_SYSTEM
) != 0)
400 WINE_ERR("Service %s is interactive but has a start name\n", wine_dbgstr_w(entry
->name
));
405 WINE_ERR("Service %s has an unknown service type (0x%x)\n", wine_dbgstr_w(entry
->name
), entry
->config
.dwServiceType
);
409 /* StartType can only be a single value (if several values are mixed the result is probably not what was intended) */
410 if (entry
->config
.dwStartType
> SERVICE_DISABLED
)
412 WINE_ERR("Service %s has an unknown start type\n", wine_dbgstr_w(entry
->name
));
416 /* SERVICE_BOOT_START and SERVICE_SYSTEM_START are only allowed for driver services */
417 if (((entry
->config
.dwStartType
== SERVICE_BOOT_START
) || (entry
->config
.dwStartType
== SERVICE_SYSTEM_START
)) &&
418 ((entry
->config
.dwServiceType
& SERVICE_WIN32_OWN_PROCESS
) || (entry
->config
.dwServiceType
& SERVICE_WIN32_SHARE_PROCESS
)))
420 WINE_ERR("Service %s - SERVICE_BOOT_START and SERVICE_SYSTEM_START are only allowed for driver services\n", wine_dbgstr_w(entry
->name
));
424 if (entry
->config
.lpServiceStartName
== NULL
)
425 entry
->config
.lpServiceStartName
= strdupW(SZ_LOCAL_SYSTEM
);
431 struct service_entry
*scmdatabase_find_service(struct scmdatabase
*db
, LPCWSTR name
)
433 struct service_entry
*service
;
435 LIST_FOR_EACH_ENTRY(service
, &db
->services
, struct service_entry
, entry
)
437 if (strcmpiW(name
, service
->name
) == 0)
444 struct service_entry
*scmdatabase_find_service_by_displayname(struct scmdatabase
*db
, LPCWSTR name
)
446 struct service_entry
*service
;
448 LIST_FOR_EACH_ENTRY(service
, &db
->services
, struct service_entry
, entry
)
450 if (service
->config
.lpDisplayName
&& strcmpiW(name
, service
->config
.lpDisplayName
) == 0)
457 void release_service(struct service_entry
*service
)
459 if (InterlockedDecrement(&service
->ref_count
) == 0 && is_marked_for_delete(service
))
461 scmdatabase_lock(service
->db
);
462 scmdatabase_remove_service(service
->db
, service
);
463 scmdatabase_unlock(service
->db
);
464 free_service_entry(service
);
468 static DWORD
scmdatabase_create(struct scmdatabase
**db
)
472 *db
= HeapAlloc(GetProcessHeap(), 0, sizeof(**db
));
474 return ERROR_NOT_ENOUGH_SERVER_MEMORY
;
476 (*db
)->service_start_lock
= FALSE
;
477 list_init(&(*db
)->services
);
479 InitializeCriticalSection(&(*db
)->cs
);
480 (*db
)->cs
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": scmdatabase");
482 err
= RegCreateKeyExW(HKEY_LOCAL_MACHINE
, SZ_SERVICES_KEY
, 0, NULL
,
483 REG_OPTION_NON_VOLATILE
, MAXIMUM_ALLOWED
, NULL
,
484 &(*db
)->root_key
, NULL
);
485 if (err
!= ERROR_SUCCESS
)
486 HeapFree(GetProcessHeap(), 0, *db
);
491 static void scmdatabase_destroy(struct scmdatabase
*db
)
493 RegCloseKey(db
->root_key
);
494 db
->cs
.DebugInfo
->Spare
[0] = 0;
495 DeleteCriticalSection(&db
->cs
);
496 HeapFree(GetProcessHeap(), 0, db
);
499 static DWORD
scmdatabase_load_services(struct scmdatabase
*db
)
504 for (i
= 0; TRUE
; i
++)
506 WCHAR szName
[MAX_SERVICE_NAME
];
507 struct service_entry
*entry
;
510 err
= RegEnumKeyW(db
->root_key
, i
, szName
, MAX_SERVICE_NAME
);
511 if (err
== ERROR_NO_MORE_ITEMS
)
516 WINE_ERR("Error %d reading key %d name - skipping\n", err
, i
);
520 err
= service_create(szName
, &entry
);
521 if (err
!= ERROR_SUCCESS
)
524 WINE_TRACE("Loading service %s\n", wine_dbgstr_w(szName
));
525 err
= RegOpenKeyExW(db
->root_key
, szName
, 0, KEY_READ
, &hServiceKey
);
526 if (err
== ERROR_SUCCESS
)
528 err
= load_service_config(hServiceKey
, entry
);
529 RegCloseKey(hServiceKey
);
532 if (err
!= ERROR_SUCCESS
)
534 WINE_ERR("Error %d reading registry key for service %s - skipping\n", err
, wine_dbgstr_w(szName
));
535 free_service_entry(entry
);
539 if (entry
->config
.dwServiceType
== 0)
541 /* Maybe an application only wrote some configuration in the service key. Continue silently */
542 WINE_TRACE("Even the service type not set for service %s - skipping\n", wine_dbgstr_w(szName
));
543 free_service_entry(entry
);
547 if (!validate_service_config(entry
))
549 WINE_ERR("Invalid configuration of service %s - skipping\n", wine_dbgstr_w(szName
));
550 free_service_entry(entry
);
554 entry
->status
.dwServiceType
= entry
->config
.dwServiceType
;
557 list_add_tail(&db
->services
, &entry
->entry
);
559 return ERROR_SUCCESS
;
562 DWORD
scmdatabase_lock_startup(struct scmdatabase
*db
)
564 if (InterlockedCompareExchange(&db
->service_start_lock
, TRUE
, FALSE
))
565 return ERROR_SERVICE_DATABASE_LOCKED
;
566 return ERROR_SUCCESS
;
569 void scmdatabase_unlock_startup(struct scmdatabase
*db
)
571 InterlockedCompareExchange(&db
->service_start_lock
, FALSE
, TRUE
);
574 void scmdatabase_lock(struct scmdatabase
*db
)
576 EnterCriticalSection(&db
->cs
);
579 void scmdatabase_unlock(struct scmdatabase
*db
)
581 LeaveCriticalSection(&db
->cs
);
584 void service_lock(struct service_entry
*service
)
586 EnterCriticalSection(&service
->db
->cs
);
589 void service_unlock(struct service_entry
*service
)
591 LeaveCriticalSection(&service
->db
->cs
);
594 /* only one service started at a time, so there is no race on the registry
596 static LPWSTR
service_get_pipe_name(void)
598 static const WCHAR format
[] = { '\\','\\','.','\\','p','i','p','e','\\',
599 'n','e','t','\\','N','t','C','o','n','t','r','o','l','P','i','p','e','%','u',0};
600 static const WCHAR service_current_key_str
[] = { 'S','Y','S','T','E','M','\\',
601 'C','u','r','r','e','n','t','C','o','n','t','r','o','l','S','e','t','\\',
602 'C','o','n','t','r','o','l','\\',
603 'S','e','r','v','i','c','e','C','u','r','r','e','n','t',0};
606 HKEY service_current_key
;
607 DWORD service_current
= -1;
611 ret
= RegCreateKeyExW(HKEY_LOCAL_MACHINE
, service_current_key_str
, 0,
612 NULL
, REG_OPTION_VOLATILE
, KEY_SET_VALUE
| KEY_QUERY_VALUE
, NULL
,
613 &service_current_key
, NULL
);
614 if (ret
!= ERROR_SUCCESS
)
616 len
= sizeof(service_current
);
617 ret
= RegQueryValueExW(service_current_key
, NULL
, NULL
, &type
,
618 (BYTE
*)&service_current
, &len
);
619 if ((ret
== ERROR_SUCCESS
&& type
== REG_DWORD
) || ret
== ERROR_FILE_NOT_FOUND
)
622 RegSetValueExW(service_current_key
, NULL
, 0, REG_DWORD
,
623 (BYTE
*)&service_current
, sizeof(service_current
));
625 RegCloseKey(service_current_key
);
626 if ((ret
!= ERROR_SUCCESS
|| type
!= REG_DWORD
) && (ret
!= ERROR_FILE_NOT_FOUND
))
628 len
= sizeof(format
)/sizeof(WCHAR
) + 10 /* strlenW("4294967295") */;
629 name
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
632 snprintfW(name
, len
, format
, service_current
);
636 static DWORD
get_service_binary_path(const struct service_entry
*service_entry
, WCHAR
**path
)
638 DWORD size
= ExpandEnvironmentStringsW(service_entry
->config
.lpBinaryPathName
, NULL
, 0);
640 *path
= HeapAlloc(GetProcessHeap(), 0, size
*sizeof(WCHAR
));
642 return ERROR_NOT_ENOUGH_SERVER_MEMORY
;
644 ExpandEnvironmentStringsW(service_entry
->config
.lpBinaryPathName
, *path
, size
);
646 if (service_entry
->config
.dwServiceType
== SERVICE_KERNEL_DRIVER
||
647 service_entry
->config
.dwServiceType
== SERVICE_FILE_SYSTEM_DRIVER
)
649 static const WCHAR winedeviceW
[] = {'\\','w','i','n','e','d','e','v','i','c','e','.','e','x','e',' ',0};
650 WCHAR system_dir
[MAX_PATH
];
653 GetSystemDirectoryW( system_dir
, MAX_PATH
);
656 if (!GetBinaryTypeW( *path
, &type
))
658 HeapFree( GetProcessHeap(), 0, *path
);
659 return GetLastError();
661 if (type
== SCS_32BIT_BINARY
) GetSystemWow64DirectoryW( system_dir
, MAX_PATH
);
664 len
= strlenW( system_dir
) + sizeof(winedeviceW
)/sizeof(WCHAR
) + strlenW(service_entry
->name
);
665 HeapFree( GetProcessHeap(), 0, *path
);
666 if (!(*path
= HeapAlloc( GetProcessHeap(), 0, len
* sizeof(WCHAR
) )))
667 return ERROR_NOT_ENOUGH_SERVER_MEMORY
;
669 lstrcpyW( *path
, system_dir
);
670 lstrcatW( *path
, winedeviceW
);
671 lstrcatW( *path
, service_entry
->name
);
672 return ERROR_SUCCESS
;
675 /* if service image is configured to systemdir, redirect it to wow64 systemdir */
676 if (service_entry
->is_wow64
)
678 WCHAR system_dir
[MAX_PATH
], *redirected
;
681 GetSystemDirectoryW( system_dir
, MAX_PATH
);
682 len
= strlenW( system_dir
);
684 if (strncmpiW( system_dir
, *path
, len
))
685 return ERROR_SUCCESS
;
687 GetSystemWow64DirectoryW( system_dir
, MAX_PATH
);
689 redirected
= HeapAlloc( GetProcessHeap(), 0, (strlenW( *path
) + strlenW( system_dir
))*sizeof(WCHAR
));
692 HeapFree( GetProcessHeap(), 0, *path
);
693 return ERROR_NOT_ENOUGH_SERVER_MEMORY
;
696 strcpyW( redirected
, system_dir
);
697 strcatW( redirected
, &(*path
)[len
] );
698 HeapFree( GetProcessHeap(), 0, *path
);
700 TRACE("redirected to %s\n", debugstr_w(redirected
));
703 return ERROR_SUCCESS
;
706 static DWORD
service_start_process(struct service_entry
*service_entry
, HANDLE
*process
)
708 PROCESS_INFORMATION pi
;
714 service_lock(service_entry
);
720 if (OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY
|TOKEN_DUPLICATE
, &htok
))
721 CreateEnvironmentBlock(&env
, htok
, FALSE
);
724 WINE_ERR("failed to create services environment\n");
727 if ((err
= get_service_binary_path(service_entry
, &path
)))
729 service_unlock(service_entry
);
733 ZeroMemory(&si
, sizeof(STARTUPINFOW
));
734 si
.cb
= sizeof(STARTUPINFOW
);
735 if (!(service_entry
->config
.dwServiceType
& SERVICE_INTERACTIVE_PROCESS
))
737 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};
738 si
.lpDesktop
= desktopW
;
741 service_entry
->status
.dwCurrentState
= SERVICE_START_PENDING
;
743 service_unlock(service_entry
);
745 r
= CreateProcessW(NULL
, path
, NULL
, NULL
, FALSE
, CREATE_UNICODE_ENVIRONMENT
, env
, NULL
, &si
, &pi
);
746 HeapFree(GetProcessHeap(),0,path
);
749 service_lock(service_entry
);
750 service_entry
->status
.dwCurrentState
= SERVICE_STOPPED
;
751 service_unlock(service_entry
);
752 return GetLastError();
755 service_entry
->status
.dwProcessId
= pi
.dwProcessId
;
756 service_entry
->process
->process
= pi
.hProcess
;
757 *process
= pi
.hProcess
;
758 CloseHandle( pi
.hThread
);
760 return ERROR_SUCCESS
;
763 static DWORD
service_wait_for_startup(struct service_entry
*service_entry
, HANDLE process_handle
)
765 HANDLE handles
[2] = { service_entry
->process
->status_changed_event
, process_handle
};
768 WINE_TRACE("%p\n", service_entry
);
770 ret
= WaitForMultipleObjects( 2, handles
, FALSE
, service_pipe_timeout
);
771 if (ret
!= WAIT_OBJECT_0
)
772 return ERROR_SERVICE_REQUEST_TIMEOUT
;
773 service_lock(service_entry
);
774 state
= service_entry
->status
.dwCurrentState
;
775 service_unlock(service_entry
);
776 if (state
== SERVICE_START_PENDING
)
778 WINE_TRACE("Service state changed to SERVICE_START_PENDING\n");
779 return ERROR_SUCCESS
;
781 else if (state
== SERVICE_RUNNING
)
783 WINE_TRACE("Service started successfully\n");
784 return ERROR_SUCCESS
;
787 return ERROR_SERVICE_REQUEST_TIMEOUT
;
790 /******************************************************************************
791 * service_send_start_message
793 static BOOL
service_send_start_message(struct service_entry
*service
, HANDLE process_handle
,
794 LPCWSTR
*argv
, DWORD argc
)
796 struct process_entry
*process
= service
->process
;
797 OVERLAPPED overlapped
;
798 DWORD i
, len
, result
;
799 service_start_info
*ssi
;
803 WINE_TRACE("%s %p %d\n", wine_dbgstr_w(service
->name
), argv
, argc
);
805 overlapped
.hEvent
= process
->overlapped_event
;
806 if (!ConnectNamedPipe(process
->control_pipe
, &overlapped
))
808 if (GetLastError() == ERROR_IO_PENDING
)
811 handles
[0] = process
->overlapped_event
;
812 handles
[1] = process_handle
;
813 if (WaitForMultipleObjects( 2, handles
, FALSE
, service_pipe_timeout
) != WAIT_OBJECT_0
)
814 CancelIo(process
->control_pipe
);
815 if (!HasOverlappedIoCompleted( &overlapped
))
817 WINE_ERR( "service %s failed to start\n", wine_dbgstr_w( service
->name
));
821 else if (GetLastError() != ERROR_PIPE_CONNECTED
)
823 WINE_ERR("pipe connect failed\n");
828 /* calculate how much space do we need to send the startup info */
829 len
= strlenW(service
->name
) + 1;
830 for (i
=0; i
<argc
; i
++)
831 len
+= strlenW(argv
[i
])+1;
834 ssi
= HeapAlloc(GetProcessHeap(),0,FIELD_OFFSET(service_start_info
, data
[len
]));
835 ssi
->cmd
= WINESERV_STARTINFO
;
837 ssi
->total_size
= FIELD_OFFSET(service_start_info
, data
[len
]);
838 ssi
->name_size
= strlenW(service
->name
) + 1;
839 strcpyW( ssi
->data
, service
->name
);
841 /* copy service args into a single buffer*/
842 p
= &ssi
->data
[ssi
->name_size
];
843 for (i
=0; i
<argc
; i
++)
850 r
= process_send_command( process
, ssi
, ssi
->total_size
, &result
);
853 SetLastError(result
);
857 HeapFree(GetProcessHeap(),0,ssi
);
862 DWORD
service_start(struct service_entry
*service
, DWORD service_argc
, LPCWSTR
*service_argv
)
864 struct process_entry
*process
= service
->process
;
867 HANDLE process_handle
= NULL
;
869 err
= scmdatabase_lock_startup(service
->db
);
870 if (err
!= ERROR_SUCCESS
)
873 if (WaitForSingleObject(process
->process
, 0) == WAIT_TIMEOUT
)
875 scmdatabase_unlock_startup(service
->db
);
876 return ERROR_SERVICE_ALREADY_RUNNING
;
879 CloseHandle(process
->control_pipe
);
880 process
->control_mutex
= CreateMutexW(NULL
, TRUE
, NULL
);
881 service
->force_shutdown
= FALSE
;
883 if (!process
->status_changed_event
)
884 process
->status_changed_event
= CreateEventW(NULL
, FALSE
, FALSE
, NULL
);
885 if (!process
->overlapped_event
)
886 process
->overlapped_event
= CreateEventW(NULL
, TRUE
, FALSE
, NULL
);
888 name
= service_get_pipe_name();
889 process
->control_pipe
= CreateNamedPipeW(name
, PIPE_ACCESS_DUPLEX
| FILE_FLAG_OVERLAPPED
,
890 PIPE_TYPE_BYTE
|PIPE_WAIT
, 1, 256, 256, 10000, NULL
);
891 HeapFree(GetProcessHeap(), 0, name
);
892 if (process
->control_pipe
== INVALID_HANDLE_VALUE
)
894 WINE_ERR("failed to create pipe for %s, error = %d\n",
895 wine_dbgstr_w(service
->name
), GetLastError());
896 err
= GetLastError();
900 err
= service_start_process(service
, &process_handle
);
901 if (err
== ERROR_SUCCESS
)
903 if (!service_send_start_message(service
, process_handle
, service_argv
, service_argc
))
904 err
= ERROR_SERVICE_REQUEST_TIMEOUT
;
907 if (err
== ERROR_SUCCESS
)
908 err
= service_wait_for_startup(service
, process_handle
);
911 if (err
== ERROR_SUCCESS
)
912 ReleaseMutex(process
->control_mutex
);
914 service_terminate(service
);
915 scmdatabase_unlock_startup(service
->db
);
917 WINE_TRACE("returning %d\n", err
);
922 void service_terminate(struct service_entry
*service
)
924 struct process_entry
*process
= service
->process
;
926 service_lock(service
);
927 TerminateProcess(process
->process
, 0);
928 CloseHandle(process
->process
);
929 process
->process
= NULL
;
930 CloseHandle(process
->status_changed_event
);
931 process
->status_changed_event
= NULL
;
932 CloseHandle(process
->control_mutex
);
933 process
->control_mutex
= NULL
;
934 CloseHandle(process
->control_pipe
);
935 process
->control_pipe
= INVALID_HANDLE_VALUE
;
937 service
->status
.dwProcessId
= 0;
938 service
->status
.dwCurrentState
= SERVICE_STOPPED
;
939 service_unlock(service
);
942 static void load_registry_parameters(void)
944 static const WCHAR controlW
[] =
945 { 'S','y','s','t','e','m','\\',
946 'C','u','r','r','e','n','t','C','o','n','t','r','o','l','S','e','t','\\',
947 'C','o','n','t','r','o','l',0 };
948 static const WCHAR pipetimeoutW
[] =
949 {'S','e','r','v','i','c','e','s','P','i','p','e','T','i','m','e','o','u','t',0};
950 static const WCHAR killtimeoutW
[] =
951 {'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};
954 DWORD type
, count
, val
;
956 if (RegOpenKeyW( HKEY_LOCAL_MACHINE
, controlW
, &key
)) return;
958 count
= sizeof(buffer
);
959 if (!RegQueryValueExW( key
, pipetimeoutW
, NULL
, &type
, (BYTE
*)buffer
, &count
) &&
960 type
== REG_SZ
&& (val
= atoiW( buffer
)))
961 service_pipe_timeout
= val
;
963 count
= sizeof(buffer
);
964 if (!RegQueryValueExW( key
, killtimeoutW
, NULL
, &type
, (BYTE
*)buffer
, &count
) &&
965 type
== REG_SZ
&& (val
= atoiW( buffer
)))
966 service_kill_timeout
= val
;
971 int main(int argc
, char *argv
[])
973 static const WCHAR svcctl_started_event
[] = SVCCTL_STARTED_EVENT
;
976 g_hStartedEvent
= CreateEventW(NULL
, TRUE
, FALSE
, svcctl_started_event
);
977 load_registry_parameters();
978 err
= scmdatabase_create(&active_database
);
979 if (err
!= ERROR_SUCCESS
)
981 if ((err
= scmdatabase_load_services(active_database
)) != ERROR_SUCCESS
)
983 if ((err
= RPC_Init()) == ERROR_SUCCESS
)
985 scmdatabase_autostart_services(active_database
);
987 scmdatabase_wait_terminate(active_database
);
989 scmdatabase_destroy(active_database
);
991 DestroyEnvironmentBlock(env
);
993 WINE_TRACE("services.exe exited with code %d\n", err
);