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
= 20000;
44 static void *env
= NULL
;
46 static const int is_win64
= (sizeof(void *) > sizeof(int));
48 static const WCHAR SZ_LOCAL_SYSTEM
[] = {'L','o','c','a','l','S','y','s','t','e','m',0};
50 /* Registry constants */
51 static const WCHAR SZ_SERVICES_KEY
[] = { 'S','y','s','t','e','m','\\',
52 'C','u','r','r','e','n','t','C','o','n','t','r','o','l','S','e','t','\\',
53 'S','e','r','v','i','c','e','s',0 };
55 /* Service key values names */
56 static const WCHAR SZ_DISPLAY_NAME
[] = {'D','i','s','p','l','a','y','N','a','m','e',0 };
57 static const WCHAR SZ_TYPE
[] = {'T','y','p','e',0 };
58 static const WCHAR SZ_START
[] = {'S','t','a','r','t',0 };
59 static const WCHAR SZ_ERROR
[] = {'E','r','r','o','r','C','o','n','t','r','o','l',0 };
60 static const WCHAR SZ_IMAGE_PATH
[] = {'I','m','a','g','e','P','a','t','h',0};
61 static const WCHAR SZ_GROUP
[] = {'G','r','o','u','p',0};
62 static const WCHAR SZ_DEPEND_ON_SERVICE
[] = {'D','e','p','e','n','d','O','n','S','e','r','v','i','c','e',0};
63 static const WCHAR SZ_DEPEND_ON_GROUP
[] = {'D','e','p','e','n','d','O','n','G','r','o','u','p',0};
64 static const WCHAR SZ_OBJECT_NAME
[] = {'O','b','j','e','c','t','N','a','m','e',0};
65 static const WCHAR SZ_TAG
[] = {'T','a','g',0};
66 static const WCHAR SZ_DESCRIPTION
[] = {'D','e','s','c','r','i','p','t','i','o','n',0};
69 DWORD
service_create(LPCWSTR name
, struct service_entry
**entry
)
71 *entry
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(**entry
));
73 return ERROR_NOT_ENOUGH_SERVER_MEMORY
;
74 (*entry
)->name
= strdupW(name
);
77 HeapFree(GetProcessHeap(), 0, *entry
);
78 return ERROR_NOT_ENOUGH_SERVER_MEMORY
;
80 (*entry
)->control_pipe
= INVALID_HANDLE_VALUE
;
81 (*entry
)->status
.dwCurrentState
= SERVICE_STOPPED
;
82 (*entry
)->status
.dwWin32ExitCode
= ERROR_SERVICE_NEVER_STARTED
;
83 /* all other fields are zero */
87 void free_service_entry(struct service_entry
*entry
)
89 HeapFree(GetProcessHeap(), 0, entry
->name
);
90 HeapFree(GetProcessHeap(), 0, entry
->config
.lpBinaryPathName
);
91 HeapFree(GetProcessHeap(), 0, entry
->config
.lpDependencies
);
92 HeapFree(GetProcessHeap(), 0, entry
->config
.lpLoadOrderGroup
);
93 HeapFree(GetProcessHeap(), 0, entry
->config
.lpServiceStartName
);
94 HeapFree(GetProcessHeap(), 0, entry
->config
.lpDisplayName
);
95 HeapFree(GetProcessHeap(), 0, entry
->description
);
96 HeapFree(GetProcessHeap(), 0, entry
->dependOnServices
);
97 HeapFree(GetProcessHeap(), 0, entry
->dependOnGroups
);
98 CloseHandle(entry
->control_mutex
);
99 CloseHandle(entry
->control_pipe
);
100 CloseHandle(entry
->overlapped_event
);
101 CloseHandle(entry
->status_changed_event
);
102 HeapFree(GetProcessHeap(), 0, entry
);
105 static DWORD
load_service_config(HKEY hKey
, struct service_entry
*entry
)
110 if ((err
= load_reg_string(hKey
, SZ_IMAGE_PATH
, TRUE
, &entry
->config
.lpBinaryPathName
)) != 0)
112 if ((err
= load_reg_string(hKey
, SZ_GROUP
, 0, &entry
->config
.lpLoadOrderGroup
)) != 0)
114 if ((err
= load_reg_string(hKey
, SZ_OBJECT_NAME
, TRUE
, &entry
->config
.lpServiceStartName
)) != 0)
116 if ((err
= load_reg_string(hKey
, SZ_DISPLAY_NAME
, 0, &entry
->config
.lpDisplayName
)) != 0)
118 if ((err
= load_reg_string(hKey
, SZ_DESCRIPTION
, 0, &entry
->description
)) != 0)
120 if ((err
= load_reg_multisz(hKey
, SZ_DEPEND_ON_SERVICE
, TRUE
, &entry
->dependOnServices
)) != 0)
122 if ((err
= load_reg_multisz(hKey
, SZ_DEPEND_ON_GROUP
, FALSE
, &entry
->dependOnGroups
)) != 0)
125 if ((err
= load_reg_dword(hKey
, SZ_TYPE
, &entry
->config
.dwServiceType
)) != 0)
127 if ((err
= load_reg_dword(hKey
, SZ_START
, &entry
->config
.dwStartType
)) != 0)
129 if ((err
= load_reg_dword(hKey
, SZ_ERROR
, &entry
->config
.dwErrorControl
)) != 0)
131 if ((err
= load_reg_dword(hKey
, SZ_TAG
, &entry
->config
.dwTagId
)) != 0)
134 WINE_TRACE("Image path = %s\n", wine_dbgstr_w(entry
->config
.lpBinaryPathName
) );
135 WINE_TRACE("Group = %s\n", wine_dbgstr_w(entry
->config
.lpLoadOrderGroup
) );
136 WINE_TRACE("Service account name = %s\n", wine_dbgstr_w(entry
->config
.lpServiceStartName
) );
137 WINE_TRACE("Display name = %s\n", wine_dbgstr_w(entry
->config
.lpDisplayName
) );
138 WINE_TRACE("Service dependencies : %s\n", entry
->dependOnServices
[0] ? "" : "(none)");
139 for (wptr
= entry
->dependOnServices
; *wptr
; wptr
+= strlenW(wptr
) + 1)
140 WINE_TRACE(" * %s\n", wine_dbgstr_w(wptr
));
141 WINE_TRACE("Group dependencies : %s\n", entry
->dependOnGroups
[0] ? "" : "(none)");
142 for (wptr
= entry
->dependOnGroups
; *wptr
; wptr
+= strlenW(wptr
) + 1)
143 WINE_TRACE(" * %s\n", wine_dbgstr_w(wptr
));
145 return ERROR_SUCCESS
;
148 static DWORD
reg_set_string_value(HKEY hKey
, LPCWSTR value_name
, LPCWSTR string
)
153 err
= RegDeleteValueW(hKey
, value_name
);
154 if (err
!= ERROR_FILE_NOT_FOUND
)
157 return ERROR_SUCCESS
;
160 return RegSetValueExW(hKey
, value_name
, 0, REG_SZ
, (const BYTE
*)string
, sizeof(WCHAR
)*(strlenW(string
) + 1));
163 static DWORD
reg_set_multisz_value(HKEY hKey
, LPCWSTR value_name
, LPCWSTR string
)
170 err
= RegDeleteValueW(hKey
, value_name
);
171 if (err
!= ERROR_FILE_NOT_FOUND
)
174 return ERROR_SUCCESS
;
178 while (*ptr
) ptr
+= strlenW(ptr
) + 1;
179 return RegSetValueExW(hKey
, value_name
, 0, REG_MULTI_SZ
, (const BYTE
*)string
, sizeof(WCHAR
)*(ptr
- string
+ 1));
182 DWORD
save_service_config(struct service_entry
*entry
)
187 err
= RegCreateKeyW(entry
->db
->root_key
, entry
->name
, &hKey
);
188 if (err
!= ERROR_SUCCESS
)
191 if ((err
= reg_set_string_value(hKey
, SZ_DISPLAY_NAME
, entry
->config
.lpDisplayName
)) != 0)
193 if ((err
= reg_set_string_value(hKey
, SZ_IMAGE_PATH
, entry
->config
.lpBinaryPathName
)) != 0)
195 if ((err
= reg_set_string_value(hKey
, SZ_GROUP
, entry
->config
.lpLoadOrderGroup
)) != 0)
197 if ((err
= reg_set_string_value(hKey
, SZ_OBJECT_NAME
, entry
->config
.lpServiceStartName
)) != 0)
199 if ((err
= reg_set_string_value(hKey
, SZ_DESCRIPTION
, entry
->description
)) != 0)
201 if ((err
= reg_set_multisz_value(hKey
, SZ_DEPEND_ON_SERVICE
, entry
->dependOnServices
)) != 0)
203 if ((err
= reg_set_multisz_value(hKey
, SZ_DEPEND_ON_GROUP
, entry
->dependOnGroups
)) != 0)
205 if ((err
= RegSetValueExW(hKey
, SZ_START
, 0, REG_DWORD
, (LPBYTE
)&entry
->config
.dwStartType
, sizeof(DWORD
))) != 0)
207 if ((err
= RegSetValueExW(hKey
, SZ_ERROR
, 0, REG_DWORD
, (LPBYTE
)&entry
->config
.dwErrorControl
, sizeof(DWORD
))) != 0)
210 if ((err
= RegSetValueExW(hKey
, SZ_TYPE
, 0, REG_DWORD
, (LPBYTE
)&entry
->config
.dwServiceType
, sizeof(DWORD
))) != 0)
213 if (entry
->config
.dwTagId
)
214 err
= RegSetValueExW(hKey
, SZ_TAG
, 0, REG_DWORD
, (LPBYTE
)&entry
->config
.dwTagId
, sizeof(DWORD
));
216 err
= RegDeleteValueW(hKey
, SZ_TAG
);
218 if (err
!= 0 && err
!= ERROR_FILE_NOT_FOUND
)
227 DWORD
scmdatabase_add_service(struct scmdatabase
*db
, struct service_entry
*service
)
231 if ((err
= save_service_config(service
)) != ERROR_SUCCESS
)
233 WINE_ERR("Couldn't store service configuration: error %u\n", err
);
234 return ERROR_GEN_FAILURE
;
237 list_add_tail(&db
->services
, &service
->entry
);
238 return ERROR_SUCCESS
;
241 DWORD
scmdatabase_remove_service(struct scmdatabase
*db
, struct service_entry
*service
)
245 err
= RegDeleteTreeW(db
->root_key
, service
->name
);
250 list_remove(&service
->entry
);
251 service
->entry
.next
= service
->entry
.prev
= NULL
;
252 return ERROR_SUCCESS
;
255 static void scmdatabase_autostart_services(struct scmdatabase
*db
)
257 struct service_entry
**services_list
;
259 unsigned int size
= 32;
260 struct service_entry
*service
;
262 services_list
= HeapAlloc(GetProcessHeap(), 0, size
* sizeof(services_list
[0]));
266 scmdatabase_lock_shared(db
);
268 LIST_FOR_EACH_ENTRY(service
, &db
->services
, struct service_entry
, entry
)
270 if (service
->config
.dwStartType
== SERVICE_BOOT_START
||
271 service
->config
.dwStartType
== SERVICE_SYSTEM_START
||
272 service
->config
.dwStartType
== SERVICE_AUTO_START
)
276 struct service_entry
**slist_new
;
278 slist_new
= HeapReAlloc(GetProcessHeap(), 0, services_list
, size
* sizeof(services_list
[0]));
281 services_list
= slist_new
;
283 services_list
[i
] = service
;
284 service
->ref_count
++;
289 scmdatabase_unlock(db
);
292 for (i
= 0; i
< size
; i
++)
295 const WCHAR
*argv
[2];
296 service
= services_list
[i
];
297 argv
[0] = service
->name
;
299 err
= service_start(service
, 1, argv
);
300 if (err
!= ERROR_SUCCESS
)
301 WINE_FIXME("Auto-start service %s failed to start: %d\n",
302 wine_dbgstr_w(service
->name
), err
);
303 release_service(service
);
306 HeapFree(GetProcessHeap(), 0, services_list
);
309 BOOL
validate_service_name(LPCWSTR name
)
311 return (name
&& name
[0] && !strchrW(name
, '/') && !strchrW(name
, '\\'));
314 BOOL
validate_service_config(struct service_entry
*entry
)
316 if (entry
->config
.dwServiceType
& SERVICE_WIN32
&& (entry
->config
.lpBinaryPathName
== NULL
|| !entry
->config
.lpBinaryPathName
[0]))
318 WINE_ERR("Service %s is Win32 but has no image path set\n", wine_dbgstr_w(entry
->name
));
322 switch (entry
->config
.dwServiceType
)
324 case SERVICE_KERNEL_DRIVER
:
325 case SERVICE_FILE_SYSTEM_DRIVER
:
326 case SERVICE_WIN32_OWN_PROCESS
:
327 case SERVICE_WIN32_SHARE_PROCESS
:
330 case SERVICE_WIN32_OWN_PROCESS
| SERVICE_INTERACTIVE_PROCESS
:
331 case SERVICE_WIN32_SHARE_PROCESS
| SERVICE_INTERACTIVE_PROCESS
:
332 /* These can be only run as LocalSystem */
333 if (entry
->config
.lpServiceStartName
&& strcmpiW(entry
->config
.lpServiceStartName
, SZ_LOCAL_SYSTEM
) != 0)
335 WINE_ERR("Service %s is interactive but has a start name\n", wine_dbgstr_w(entry
->name
));
340 WINE_ERR("Service %s has an unknown service type (0x%x)\n", wine_dbgstr_w(entry
->name
), entry
->config
.dwServiceType
);
344 /* StartType can only be a single value (if several values are mixed the result is probably not what was intended) */
345 if (entry
->config
.dwStartType
> SERVICE_DISABLED
)
347 WINE_ERR("Service %s has an unknown start type\n", wine_dbgstr_w(entry
->name
));
351 /* SERVICE_BOOT_START and SERVICE_SYSTEM_START are only allowed for driver services */
352 if (((entry
->config
.dwStartType
== SERVICE_BOOT_START
) || (entry
->config
.dwStartType
== SERVICE_SYSTEM_START
)) &&
353 ((entry
->config
.dwServiceType
& SERVICE_WIN32_OWN_PROCESS
) || (entry
->config
.dwServiceType
& SERVICE_WIN32_SHARE_PROCESS
)))
355 WINE_ERR("Service %s - SERVICE_BOOT_START and SERVICE_SYSTEM_START are only allowed for driver services\n", wine_dbgstr_w(entry
->name
));
359 if (entry
->config
.lpServiceStartName
== NULL
)
360 entry
->config
.lpServiceStartName
= strdupW(SZ_LOCAL_SYSTEM
);
366 struct service_entry
*scmdatabase_find_service(struct scmdatabase
*db
, LPCWSTR name
)
368 struct service_entry
*service
;
370 LIST_FOR_EACH_ENTRY(service
, &db
->services
, struct service_entry
, entry
)
372 if (strcmpiW(name
, service
->name
) == 0)
379 struct service_entry
*scmdatabase_find_service_by_displayname(struct scmdatabase
*db
, LPCWSTR name
)
381 struct service_entry
*service
;
383 LIST_FOR_EACH_ENTRY(service
, &db
->services
, struct service_entry
, entry
)
385 if (service
->config
.lpDisplayName
&& strcmpiW(name
, service
->config
.lpDisplayName
) == 0)
392 void release_service(struct service_entry
*service
)
394 if (InterlockedDecrement(&service
->ref_count
) == 0 && is_marked_for_delete(service
))
395 free_service_entry(service
);
398 static DWORD
scmdatabase_create(struct scmdatabase
**db
)
402 *db
= HeapAlloc(GetProcessHeap(), 0, sizeof(**db
));
404 return ERROR_NOT_ENOUGH_SERVER_MEMORY
;
406 (*db
)->service_start_lock
= FALSE
;
407 list_init(&(*db
)->services
);
409 InitializeCriticalSection(&(*db
)->cs
);
410 (*db
)->cs
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": scmdatabase");
412 err
= RegCreateKeyExW(HKEY_LOCAL_MACHINE
, SZ_SERVICES_KEY
, 0, NULL
,
413 REG_OPTION_NON_VOLATILE
, MAXIMUM_ALLOWED
, NULL
,
414 &(*db
)->root_key
, NULL
);
415 if (err
!= ERROR_SUCCESS
)
416 HeapFree(GetProcessHeap(), 0, *db
);
421 static void scmdatabase_destroy(struct scmdatabase
*db
)
423 RegCloseKey(db
->root_key
);
424 db
->cs
.DebugInfo
->Spare
[0] = 0;
425 DeleteCriticalSection(&db
->cs
);
426 HeapFree(GetProcessHeap(), 0, db
);
429 static DWORD
scmdatabase_load_services(struct scmdatabase
*db
)
434 for (i
= 0; TRUE
; i
++)
436 WCHAR szName
[MAX_SERVICE_NAME
];
437 struct service_entry
*entry
;
440 err
= RegEnumKeyW(db
->root_key
, i
, szName
, MAX_SERVICE_NAME
);
441 if (err
== ERROR_NO_MORE_ITEMS
)
446 WINE_ERR("Error %d reading key %d name - skipping\n", err
, i
);
450 err
= service_create(szName
, &entry
);
451 if (err
!= ERROR_SUCCESS
)
454 WINE_TRACE("Loading service %s\n", wine_dbgstr_w(szName
));
455 err
= RegOpenKeyExW(db
->root_key
, szName
, 0, KEY_READ
, &hServiceKey
);
456 if (err
== ERROR_SUCCESS
)
458 err
= load_service_config(hServiceKey
, entry
);
459 RegCloseKey(hServiceKey
);
462 if (err
!= ERROR_SUCCESS
)
464 WINE_ERR("Error %d reading registry key for service %s - skipping\n", err
, wine_dbgstr_w(szName
));
465 free_service_entry(entry
);
469 if (entry
->config
.dwServiceType
== 0)
471 /* Maybe an application only wrote some configuration in the service key. Continue silently */
472 WINE_TRACE("Even the service type not set for service %s - skipping\n", wine_dbgstr_w(szName
));
473 free_service_entry(entry
);
477 if (!validate_service_config(entry
))
479 WINE_ERR("Invalid configuration of service %s - skipping\n", wine_dbgstr_w(szName
));
480 free_service_entry(entry
);
484 entry
->status
.dwServiceType
= entry
->config
.dwServiceType
;
487 list_add_tail(&db
->services
, &entry
->entry
);
489 return ERROR_SUCCESS
;
492 DWORD
scmdatabase_lock_startup(struct scmdatabase
*db
)
494 if (InterlockedCompareExchange(&db
->service_start_lock
, TRUE
, FALSE
))
495 return ERROR_SERVICE_DATABASE_LOCKED
;
496 return ERROR_SUCCESS
;
499 void scmdatabase_unlock_startup(struct scmdatabase
*db
)
501 InterlockedCompareExchange(&db
->service_start_lock
, FALSE
, TRUE
);
504 void scmdatabase_lock_shared(struct scmdatabase
*db
)
506 EnterCriticalSection(&db
->cs
);
509 void scmdatabase_lock_exclusive(struct scmdatabase
*db
)
511 EnterCriticalSection(&db
->cs
);
514 void scmdatabase_unlock(struct scmdatabase
*db
)
516 LeaveCriticalSection(&db
->cs
);
519 void service_lock_shared(struct service_entry
*service
)
521 EnterCriticalSection(&service
->db
->cs
);
524 void service_lock_exclusive(struct service_entry
*service
)
526 EnterCriticalSection(&service
->db
->cs
);
529 void service_unlock(struct service_entry
*service
)
531 LeaveCriticalSection(&service
->db
->cs
);
534 /* only one service started at a time, so there is no race on the registry
536 static LPWSTR
service_get_pipe_name(void)
538 static const WCHAR format
[] = { '\\','\\','.','\\','p','i','p','e','\\',
539 'n','e','t','\\','N','t','C','o','n','t','r','o','l','P','i','p','e','%','u',0};
540 static const WCHAR service_current_key_str
[] = { 'S','Y','S','T','E','M','\\',
541 'C','u','r','r','e','n','t','C','o','n','t','r','o','l','S','e','t','\\',
542 'C','o','n','t','r','o','l','\\',
543 'S','e','r','v','i','c','e','C','u','r','r','e','n','t',0};
546 HKEY service_current_key
;
547 DWORD service_current
= -1;
551 ret
= RegCreateKeyExW(HKEY_LOCAL_MACHINE
, service_current_key_str
, 0,
552 NULL
, REG_OPTION_VOLATILE
, KEY_SET_VALUE
| KEY_QUERY_VALUE
, NULL
,
553 &service_current_key
, NULL
);
554 if (ret
!= ERROR_SUCCESS
)
556 len
= sizeof(service_current
);
557 ret
= RegQueryValueExW(service_current_key
, NULL
, NULL
, &type
,
558 (BYTE
*)&service_current
, &len
);
559 if ((ret
== ERROR_SUCCESS
&& type
== REG_DWORD
) || ret
== ERROR_FILE_NOT_FOUND
)
562 RegSetValueExW(service_current_key
, NULL
, 0, REG_DWORD
,
563 (BYTE
*)&service_current
, sizeof(service_current
));
565 RegCloseKey(service_current_key
);
566 if ((ret
!= ERROR_SUCCESS
|| type
!= REG_DWORD
) && (ret
!= ERROR_FILE_NOT_FOUND
))
568 len
= sizeof(format
)/sizeof(WCHAR
) + 10 /* strlenW("4294967295") */;
569 name
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
572 snprintfW(name
, len
, format
, service_current
);
576 static DWORD
service_start_process(struct service_entry
*service_entry
, HANDLE
*process
)
578 PROCESS_INFORMATION pi
;
584 service_lock_exclusive(service_entry
);
590 if (OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY
|TOKEN_DUPLICATE
, &htok
))
591 CreateEnvironmentBlock(&env
, htok
, FALSE
);
594 WINE_ERR("failed to create services environment\n");
597 size
= ExpandEnvironmentStringsW(service_entry
->config
.lpBinaryPathName
,NULL
,0);
598 path
= HeapAlloc(GetProcessHeap(),0,size
*sizeof(WCHAR
));
601 service_unlock(service_entry
);
602 return ERROR_NOT_ENOUGH_SERVER_MEMORY
;
604 ExpandEnvironmentStringsW(service_entry
->config
.lpBinaryPathName
,path
,size
);
606 if (service_entry
->config
.dwServiceType
== SERVICE_KERNEL_DRIVER
)
608 static const WCHAR winedeviceW
[] = {'\\','w','i','n','e','d','e','v','i','c','e','.','e','x','e',' ',0};
609 WCHAR system_dir
[MAX_PATH
];
612 GetSystemDirectoryW( system_dir
, MAX_PATH
);
615 if (!GetBinaryTypeW( path
, &type
))
617 HeapFree( GetProcessHeap(), 0, path
);
618 service_unlock(service_entry
);
619 return GetLastError();
621 if (type
== SCS_32BIT_BINARY
) GetSystemWow64DirectoryW( system_dir
, MAX_PATH
);
624 len
= strlenW( system_dir
) + sizeof(winedeviceW
)/sizeof(WCHAR
) + strlenW(service_entry
->name
);
625 HeapFree( GetProcessHeap(), 0, path
);
626 if (!(path
= HeapAlloc( GetProcessHeap(), 0, len
* sizeof(WCHAR
) )))
628 service_unlock(service_entry
);
629 return ERROR_NOT_ENOUGH_SERVER_MEMORY
;
631 lstrcpyW( path
, system_dir
);
632 lstrcatW( path
, winedeviceW
);
633 lstrcatW( path
, service_entry
->name
);
636 ZeroMemory(&si
, sizeof(STARTUPINFOW
));
637 si
.cb
= sizeof(STARTUPINFOW
);
638 if (!(service_entry
->config
.dwServiceType
& SERVICE_INTERACTIVE_PROCESS
))
640 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};
641 si
.lpDesktop
= desktopW
;
644 service_entry
->status
.dwCurrentState
= SERVICE_START_PENDING
;
646 service_unlock(service_entry
);
648 r
= CreateProcessW(NULL
, path
, NULL
, NULL
, FALSE
, CREATE_UNICODE_ENVIRONMENT
, env
, NULL
, &si
, &pi
);
649 HeapFree(GetProcessHeap(),0,path
);
652 service_lock_exclusive(service_entry
);
653 service_entry
->status
.dwCurrentState
= SERVICE_STOPPED
;
654 service_unlock(service_entry
);
655 return GetLastError();
658 service_entry
->status
.dwProcessId
= pi
.dwProcessId
;
659 *process
= pi
.hProcess
;
660 CloseHandle( pi
.hThread
);
662 return ERROR_SUCCESS
;
665 static DWORD
service_wait_for_startup(struct service_entry
*service_entry
, HANDLE process_handle
)
667 WINE_TRACE("%p\n", service_entry
);
671 DWORD dwCurrentStatus
;
672 HANDLE handles
[2] = { service_entry
->status_changed_event
, process_handle
};
674 ret
= WaitForMultipleObjects( 2, handles
, FALSE
, service_pipe_timeout
);
675 if (ret
!= WAIT_OBJECT_0
)
676 return ERROR_SERVICE_REQUEST_TIMEOUT
;
677 service_lock_shared(service_entry
);
678 dwCurrentStatus
= service_entry
->status
.dwCurrentState
;
679 service_unlock(service_entry
);
680 if (dwCurrentStatus
== SERVICE_RUNNING
)
682 WINE_TRACE("Service started successfully\n");
683 return ERROR_SUCCESS
;
685 if (dwCurrentStatus
!= SERVICE_START_PENDING
)
686 return ERROR_SERVICE_REQUEST_TIMEOUT
;
690 /******************************************************************************
691 * service_send_start_message
693 static BOOL
service_send_start_message(struct service_entry
*service
, HANDLE process_handle
,
694 LPCWSTR
*argv
, DWORD argc
)
696 OVERLAPPED overlapped
;
697 DWORD i
, len
, result
;
698 service_start_info
*ssi
;
702 WINE_TRACE("%s %p %d\n", wine_dbgstr_w(service
->name
), argv
, argc
);
704 overlapped
.hEvent
= service
->overlapped_event
;
705 if (!ConnectNamedPipe(service
->control_pipe
, &overlapped
))
707 if (GetLastError() == ERROR_IO_PENDING
)
710 handles
[0] = service
->overlapped_event
;
711 handles
[1] = process_handle
;
712 if (WaitForMultipleObjects( 2, handles
, FALSE
, service_pipe_timeout
) != WAIT_OBJECT_0
)
713 CancelIo( service
->control_pipe
);
714 if (!HasOverlappedCompleted( &overlapped
))
716 WINE_ERR( "service %s failed to start\n", wine_dbgstr_w( service
->name
));
720 else if (GetLastError() != ERROR_PIPE_CONNECTED
)
722 WINE_ERR("pipe connect failed\n");
727 /* calculate how much space do we need to send the startup info */
728 len
= strlenW(service
->name
) + 1;
729 for (i
=0; i
<argc
; i
++)
730 len
+= strlenW(argv
[i
])+1;
733 ssi
= HeapAlloc(GetProcessHeap(),0,FIELD_OFFSET(service_start_info
, data
[len
]));
734 ssi
->cmd
= WINESERV_STARTINFO
;
736 ssi
->total_size
= FIELD_OFFSET(service_start_info
, data
[len
]);
737 ssi
->name_size
= strlenW(service
->name
) + 1;
738 strcpyW( ssi
->data
, service
->name
);
740 /* copy service args into a single buffer*/
741 p
= &ssi
->data
[ssi
->name_size
];
742 for (i
=0; i
<argc
; i
++)
749 r
= service_send_command( service
, service
->control_pipe
, ssi
, ssi
->total_size
, &result
);
752 SetLastError(result
);
756 HeapFree(GetProcessHeap(),0,ssi
);
761 DWORD
service_start(struct service_entry
*service
, DWORD service_argc
, LPCWSTR
*service_argv
)
765 HANDLE process_handle
= NULL
;
767 err
= scmdatabase_lock_startup(service
->db
);
768 if (err
!= ERROR_SUCCESS
)
771 if (service
->control_pipe
!= INVALID_HANDLE_VALUE
)
773 scmdatabase_unlock_startup(service
->db
);
774 return ERROR_SERVICE_ALREADY_RUNNING
;
777 service
->control_mutex
= CreateMutexW(NULL
, TRUE
, NULL
);
779 if (!service
->status_changed_event
)
780 service
->status_changed_event
= CreateEventW(NULL
, FALSE
, FALSE
, NULL
);
781 if (!service
->overlapped_event
)
782 service
->overlapped_event
= CreateEventW(NULL
, TRUE
, FALSE
, NULL
);
784 name
= service_get_pipe_name();
785 service
->control_pipe
= CreateNamedPipeW(name
, PIPE_ACCESS_DUPLEX
| FILE_FLAG_OVERLAPPED
,
786 PIPE_TYPE_BYTE
|PIPE_WAIT
, 1, 256, 256, 10000, NULL
);
787 HeapFree(GetProcessHeap(), 0, name
);
788 if (service
->control_pipe
==INVALID_HANDLE_VALUE
)
790 WINE_ERR("failed to create pipe for %s, error = %d\n",
791 wine_dbgstr_w(service
->name
), GetLastError());
792 err
= GetLastError();
796 err
= service_start_process(service
, &process_handle
);
797 if (err
== ERROR_SUCCESS
)
799 if (!service_send_start_message(service
, process_handle
, service_argv
, service_argc
))
800 err
= ERROR_SERVICE_REQUEST_TIMEOUT
;
803 if (err
== ERROR_SUCCESS
)
804 err
= service_wait_for_startup(service
, process_handle
);
807 CloseHandle(process_handle
);
810 if (err
== ERROR_SUCCESS
)
811 ReleaseMutex(service
->control_mutex
);
814 CloseHandle(service
->overlapped_event
);
815 service
->overlapped_event
= NULL
;
816 CloseHandle(service
->status_changed_event
);
817 service
->status_changed_event
= NULL
;
818 CloseHandle(service
->control_mutex
);
819 service
->control_mutex
= NULL
;
820 if (service
->control_pipe
!= INVALID_HANDLE_VALUE
)
821 CloseHandle(service
->control_pipe
);
822 service
->control_pipe
= INVALID_HANDLE_VALUE
;
824 service
->status
.dwProcessId
= 0;
825 service_lock_exclusive(service
);
826 service
->status
.dwCurrentState
= SERVICE_STOPPED
;
827 service_unlock(service
);
829 scmdatabase_unlock_startup(service
->db
);
831 WINE_TRACE("returning %d\n", err
);
836 static void load_registry_parameters(void)
838 static const WCHAR controlW
[] =
839 { 'S','y','s','t','e','m','\\',
840 'C','u','r','r','e','n','t','C','o','n','t','r','o','l','S','e','t','\\',
841 'C','o','n','t','r','o','l',0 };
842 static const WCHAR pipetimeoutW
[] =
843 {'S','e','r','v','i','c','e','s','P','i','p','e','T','i','m','e','o','u','t',0};
844 static const WCHAR killtimeoutW
[] =
845 {'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};
848 DWORD type
, count
, val
;
850 if (RegOpenKeyW( HKEY_LOCAL_MACHINE
, controlW
, &key
)) return;
852 count
= sizeof(buffer
);
853 if (!RegQueryValueExW( key
, pipetimeoutW
, NULL
, &type
, (BYTE
*)buffer
, &count
) &&
854 type
== REG_SZ
&& (val
= atoiW( buffer
)))
855 service_pipe_timeout
= val
;
857 count
= sizeof(buffer
);
858 if (!RegQueryValueExW( key
, killtimeoutW
, NULL
, &type
, (BYTE
*)buffer
, &count
) &&
859 type
== REG_SZ
&& (val
= atoiW( buffer
)))
860 service_kill_timeout
= val
;
865 int main(int argc
, char *argv
[])
867 static const WCHAR svcctl_started_event
[] = SVCCTL_STARTED_EVENT
;
870 g_hStartedEvent
= CreateEventW(NULL
, TRUE
, FALSE
, svcctl_started_event
);
871 load_registry_parameters();
872 err
= scmdatabase_create(&active_database
);
873 if (err
!= ERROR_SUCCESS
)
875 if ((err
= scmdatabase_load_services(active_database
)) != ERROR_SUCCESS
)
877 if ((err
= RPC_Init()) == ERROR_SUCCESS
)
879 scmdatabase_autostart_services(active_database
);
882 scmdatabase_destroy(active_database
);
884 DestroyEnvironmentBlock(env
);