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
28 #include "wine/unicode.h"
29 #include "wine/debug.h"
34 #define MAX_SERVICE_NAME 260
36 WINE_DEFAULT_DEBUG_CHANNEL(service
);
38 HANDLE g_hStartedEvent
;
39 struct scmdatabase
*active_database
;
41 static const int is_win64
= (sizeof(void *) > sizeof(int));
43 static const WCHAR SZ_LOCAL_SYSTEM
[] = {'L','o','c','a','l','S','y','s','t','e','m',0};
45 /* Registry constants */
46 static const WCHAR SZ_SERVICES_KEY
[] = { 'S','y','s','t','e','m','\\',
47 'C','u','r','r','e','n','t','C','o','n','t','r','o','l','S','e','t','\\',
48 'S','e','r','v','i','c','e','s',0 };
50 /* Service key values names */
51 static const WCHAR SZ_DISPLAY_NAME
[] = {'D','i','s','p','l','a','y','N','a','m','e',0 };
52 static const WCHAR SZ_TYPE
[] = {'T','y','p','e',0 };
53 static const WCHAR SZ_START
[] = {'S','t','a','r','t',0 };
54 static const WCHAR SZ_ERROR
[] = {'E','r','r','o','r','C','o','n','t','r','o','l',0 };
55 static const WCHAR SZ_IMAGE_PATH
[] = {'I','m','a','g','e','P','a','t','h',0};
56 static const WCHAR SZ_GROUP
[] = {'G','r','o','u','p',0};
57 static const WCHAR SZ_DEPEND_ON_SERVICE
[] = {'D','e','p','e','n','d','O','n','S','e','r','v','i','c','e',0};
58 static const WCHAR SZ_DEPEND_ON_GROUP
[] = {'D','e','p','e','n','d','O','n','G','r','o','u','p',0};
59 static const WCHAR SZ_OBJECT_NAME
[] = {'O','b','j','e','c','t','N','a','m','e',0};
60 static const WCHAR SZ_TAG
[] = {'T','a','g',0};
61 static const WCHAR SZ_DESCRIPTION
[] = {'D','e','s','c','r','i','p','t','i','o','n',0};
64 DWORD
service_create(LPCWSTR name
, struct service_entry
**entry
)
66 *entry
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(**entry
));
68 return ERROR_NOT_ENOUGH_SERVER_MEMORY
;
69 (*entry
)->name
= strdupW(name
);
72 HeapFree(GetProcessHeap(), 0, *entry
);
73 return ERROR_NOT_ENOUGH_SERVER_MEMORY
;
75 (*entry
)->control_pipe
= INVALID_HANDLE_VALUE
;
76 (*entry
)->status
.dwCurrentState
= SERVICE_STOPPED
;
77 (*entry
)->status
.dwWin32ExitCode
= ERROR_SERVICE_NEVER_STARTED
;
78 /* all other fields are zero */
82 void free_service_entry(struct service_entry
*entry
)
84 HeapFree(GetProcessHeap(), 0, entry
->name
);
85 HeapFree(GetProcessHeap(), 0, entry
->config
.lpBinaryPathName
);
86 HeapFree(GetProcessHeap(), 0, entry
->config
.lpDependencies
);
87 HeapFree(GetProcessHeap(), 0, entry
->config
.lpLoadOrderGroup
);
88 HeapFree(GetProcessHeap(), 0, entry
->config
.lpServiceStartName
);
89 HeapFree(GetProcessHeap(), 0, entry
->config
.lpDisplayName
);
90 HeapFree(GetProcessHeap(), 0, entry
->description
);
91 HeapFree(GetProcessHeap(), 0, entry
->dependOnServices
);
92 HeapFree(GetProcessHeap(), 0, entry
->dependOnGroups
);
93 CloseHandle(entry
->control_mutex
);
94 CloseHandle(entry
->control_pipe
);
95 CloseHandle(entry
->status_changed_event
);
96 HeapFree(GetProcessHeap(), 0, entry
);
99 static DWORD
load_service_config(HKEY hKey
, struct service_entry
*entry
)
104 if ((err
= load_reg_string(hKey
, SZ_IMAGE_PATH
, TRUE
, &entry
->config
.lpBinaryPathName
)) != 0)
106 if ((err
= load_reg_string(hKey
, SZ_GROUP
, 0, &entry
->config
.lpLoadOrderGroup
)) != 0)
108 if ((err
= load_reg_string(hKey
, SZ_OBJECT_NAME
, TRUE
, &entry
->config
.lpServiceStartName
)) != 0)
110 if ((err
= load_reg_string(hKey
, SZ_DISPLAY_NAME
, 0, &entry
->config
.lpDisplayName
)) != 0)
112 if ((err
= load_reg_string(hKey
, SZ_DESCRIPTION
, 0, &entry
->description
)) != 0)
114 if ((err
= load_reg_multisz(hKey
, SZ_DEPEND_ON_SERVICE
, TRUE
, &entry
->dependOnServices
)) != 0)
116 if ((err
= load_reg_multisz(hKey
, SZ_DEPEND_ON_GROUP
, FALSE
, &entry
->dependOnGroups
)) != 0)
119 if ((err
= load_reg_dword(hKey
, SZ_TYPE
, &entry
->config
.dwServiceType
)) != 0)
121 if ((err
= load_reg_dword(hKey
, SZ_START
, &entry
->config
.dwStartType
)) != 0)
123 if ((err
= load_reg_dword(hKey
, SZ_ERROR
, &entry
->config
.dwErrorControl
)) != 0)
125 if ((err
= load_reg_dword(hKey
, SZ_TAG
, &entry
->config
.dwTagId
)) != 0)
128 WINE_TRACE("Image path = %s\n", wine_dbgstr_w(entry
->config
.lpBinaryPathName
) );
129 WINE_TRACE("Group = %s\n", wine_dbgstr_w(entry
->config
.lpLoadOrderGroup
) );
130 WINE_TRACE("Service account name = %s\n", wine_dbgstr_w(entry
->config
.lpServiceStartName
) );
131 WINE_TRACE("Display name = %s\n", wine_dbgstr_w(entry
->config
.lpDisplayName
) );
132 WINE_TRACE("Service dependencies : %s\n", entry
->dependOnServices
[0] ? "" : "(none)");
133 for (wptr
= entry
->dependOnServices
; *wptr
; wptr
+= strlenW(wptr
) + 1)
134 WINE_TRACE(" * %s\n", wine_dbgstr_w(wptr
));
135 WINE_TRACE("Group dependencies : %s\n", entry
->dependOnGroups
[0] ? "" : "(none)");
136 for (wptr
= entry
->dependOnGroups
; *wptr
; wptr
+= strlenW(wptr
) + 1)
137 WINE_TRACE(" * %s\n", wine_dbgstr_w(wptr
));
139 return ERROR_SUCCESS
;
142 static DWORD
reg_set_string_value(HKEY hKey
, LPCWSTR value_name
, LPCWSTR string
)
147 err
= RegDeleteValueW(hKey
, value_name
);
148 if (err
!= ERROR_FILE_NOT_FOUND
)
151 return ERROR_SUCCESS
;
154 return RegSetValueExW(hKey
, value_name
, 0, REG_SZ
, (const BYTE
*)string
, sizeof(WCHAR
)*(strlenW(string
) + 1));
157 DWORD
save_service_config(struct service_entry
*entry
)
162 err
= RegCreateKeyW(entry
->db
->root_key
, entry
->name
, &hKey
);
163 if (err
!= ERROR_SUCCESS
)
166 if ((err
= reg_set_string_value(hKey
, SZ_DISPLAY_NAME
, entry
->config
.lpDisplayName
)) != 0)
168 if ((err
= reg_set_string_value(hKey
, SZ_IMAGE_PATH
, entry
->config
.lpBinaryPathName
)) != 0)
170 if ((err
= reg_set_string_value(hKey
, SZ_GROUP
, entry
->config
.lpLoadOrderGroup
)) != 0)
172 if ((err
= reg_set_string_value(hKey
, SZ_OBJECT_NAME
, entry
->config
.lpServiceStartName
)) != 0)
174 if ((err
= reg_set_string_value(hKey
, SZ_DESCRIPTION
, entry
->description
)) != 0)
176 if ((err
= RegSetValueExW(hKey
, SZ_START
, 0, REG_DWORD
, (LPBYTE
)&entry
->config
.dwStartType
, sizeof(DWORD
))) != 0)
178 if ((err
= RegSetValueExW(hKey
, SZ_ERROR
, 0, REG_DWORD
, (LPBYTE
)&entry
->config
.dwErrorControl
, sizeof(DWORD
))) != 0)
181 if ((err
= RegSetValueExW(hKey
, SZ_TYPE
, 0, REG_DWORD
, (LPBYTE
)&entry
->config
.dwServiceType
, sizeof(DWORD
))) != 0)
184 if (entry
->config
.dwTagId
)
185 err
= RegSetValueExW(hKey
, SZ_TAG
, 0, REG_DWORD
, (LPBYTE
)&entry
->config
.dwTagId
, sizeof(DWORD
));
187 err
= RegDeleteValueW(hKey
, SZ_TAG
);
189 if (err
!= 0 && err
!= ERROR_FILE_NOT_FOUND
)
198 DWORD
scmdatabase_add_service(struct scmdatabase
*db
, struct service_entry
*service
)
202 if ((err
= save_service_config(service
)) != ERROR_SUCCESS
)
204 WINE_ERR("Couldn't store service configuration: error %u\n", err
);
205 return ERROR_GEN_FAILURE
;
208 list_add_tail(&db
->services
, &service
->entry
);
209 return ERROR_SUCCESS
;
212 DWORD
scmdatabase_remove_service(struct scmdatabase
*db
, struct service_entry
*service
)
216 err
= RegDeleteTreeW(db
->root_key
, service
->name
);
221 list_remove(&service
->entry
);
222 service
->entry
.next
= service
->entry
.prev
= NULL
;
223 return ERROR_SUCCESS
;
226 static void scmdatabase_autostart_services(struct scmdatabase
*db
)
228 struct service_entry
**services_list
;
230 unsigned int size
= 32;
231 struct service_entry
*service
;
233 services_list
= HeapAlloc(GetProcessHeap(), 0, size
* sizeof(services_list
[0]));
237 scmdatabase_lock_shared(db
);
239 LIST_FOR_EACH_ENTRY(service
, &db
->services
, struct service_entry
, entry
)
241 if (service
->config
.dwStartType
== SERVICE_BOOT_START
||
242 service
->config
.dwStartType
== SERVICE_SYSTEM_START
||
243 service
->config
.dwStartType
== SERVICE_AUTO_START
)
247 struct service_entry
**slist_new
;
249 slist_new
= HeapReAlloc(GetProcessHeap(), 0, services_list
, size
* sizeof(services_list
[0]));
252 services_list
= slist_new
;
254 services_list
[i
] = service
;
255 service
->ref_count
++;
260 scmdatabase_unlock(db
);
263 for (i
= 0; i
< size
; i
++)
266 const WCHAR
*argv
[2];
267 service
= services_list
[i
];
268 argv
[0] = service
->name
;
270 err
= service_start(service
, 1, argv
);
271 /* FIXME: do something if the service failed to start */
272 release_service(service
);
275 HeapFree(GetProcessHeap(), 0, services_list
);
278 BOOL
validate_service_name(LPCWSTR name
)
280 return (name
&& name
[0] && !strchrW(name
, '/') && !strchrW(name
, '\\'));
283 BOOL
validate_service_config(struct service_entry
*entry
)
285 if (entry
->config
.dwServiceType
& SERVICE_WIN32
&& (entry
->config
.lpBinaryPathName
== NULL
|| !entry
->config
.lpBinaryPathName
[0]))
287 WINE_ERR("Service %s is Win32 but has no image path set\n", wine_dbgstr_w(entry
->name
));
291 switch (entry
->config
.dwServiceType
)
293 case SERVICE_KERNEL_DRIVER
:
294 case SERVICE_FILE_SYSTEM_DRIVER
:
295 case SERVICE_WIN32_OWN_PROCESS
:
296 case SERVICE_WIN32_SHARE_PROCESS
:
299 case SERVICE_WIN32_OWN_PROCESS
| SERVICE_INTERACTIVE_PROCESS
:
300 case SERVICE_WIN32_SHARE_PROCESS
| SERVICE_INTERACTIVE_PROCESS
:
301 /* These can be only run as LocalSystem */
302 if (entry
->config
.lpServiceStartName
&& strcmpiW(entry
->config
.lpServiceStartName
, SZ_LOCAL_SYSTEM
) != 0)
304 WINE_ERR("Service %s is interactive but has a start name\n", wine_dbgstr_w(entry
->name
));
309 WINE_ERR("Service %s has an unknown service type\n", wine_dbgstr_w(entry
->name
));
313 /* StartType can only be a single value (if several values are mixed the result is probably not what was intended) */
314 if (entry
->config
.dwStartType
> SERVICE_DISABLED
)
316 WINE_ERR("Service %s has an unknown start type\n", wine_dbgstr_w(entry
->name
));
320 /* SERVICE_BOOT_START and SERVICE_SYSTEM_START are only allowed for driver services */
321 if (((entry
->config
.dwStartType
== SERVICE_BOOT_START
) || (entry
->config
.dwStartType
== SERVICE_SYSTEM_START
)) &&
322 ((entry
->config
.dwServiceType
& SERVICE_WIN32_OWN_PROCESS
) || (entry
->config
.dwServiceType
& SERVICE_WIN32_SHARE_PROCESS
)))
324 WINE_ERR("Service %s - SERVICE_BOOT_START and SERVICE_SYSTEM_START are only allowed for driver services\n", wine_dbgstr_w(entry
->name
));
328 if (entry
->config
.lpServiceStartName
== NULL
)
329 entry
->config
.lpServiceStartName
= strdupW(SZ_LOCAL_SYSTEM
);
335 struct service_entry
*scmdatabase_find_service(struct scmdatabase
*db
, LPCWSTR name
)
337 struct service_entry
*service
;
339 LIST_FOR_EACH_ENTRY(service
, &db
->services
, struct service_entry
, entry
)
341 if (strcmpiW(name
, service
->name
) == 0)
348 struct service_entry
*scmdatabase_find_service_by_displayname(struct scmdatabase
*db
, LPCWSTR name
)
350 struct service_entry
*service
;
352 LIST_FOR_EACH_ENTRY(service
, &db
->services
, struct service_entry
, entry
)
354 if (service
->config
.lpDisplayName
&& strcmpiW(name
, service
->config
.lpDisplayName
) == 0)
361 void release_service(struct service_entry
*service
)
363 if (InterlockedDecrement(&service
->ref_count
) == 0 && is_marked_for_delete(service
))
364 free_service_entry(service
);
367 static DWORD
scmdatabase_create(struct scmdatabase
**db
)
371 *db
= HeapAlloc(GetProcessHeap(), 0, sizeof(**db
));
373 return ERROR_NOT_ENOUGH_SERVER_MEMORY
;
375 (*db
)->service_start_lock
= FALSE
;
376 list_init(&(*db
)->services
);
378 InitializeCriticalSection(&(*db
)->cs
);
380 err
= RegCreateKeyExW(HKEY_LOCAL_MACHINE
, SZ_SERVICES_KEY
, 0, NULL
,
381 REG_OPTION_NON_VOLATILE
, MAXIMUM_ALLOWED
, NULL
,
382 &(*db
)->root_key
, NULL
);
383 if (err
!= ERROR_SUCCESS
)
384 HeapFree(GetProcessHeap(), 0, *db
);
389 static void scmdatabase_destroy(struct scmdatabase
*db
)
391 RegCloseKey(db
->root_key
);
392 DeleteCriticalSection(&db
->cs
);
393 HeapFree(GetProcessHeap(), 0, db
);
396 static DWORD
scmdatabase_load_services(struct scmdatabase
*db
)
401 for (i
= 0; TRUE
; i
++)
403 WCHAR szName
[MAX_SERVICE_NAME
];
404 struct service_entry
*entry
;
407 err
= RegEnumKeyW(db
->root_key
, i
, szName
, MAX_SERVICE_NAME
);
408 if (err
== ERROR_NO_MORE_ITEMS
)
413 WINE_ERR("Error %d reading key %d name - skipping\n", err
, i
);
417 err
= service_create(szName
, &entry
);
418 if (err
!= ERROR_SUCCESS
)
421 WINE_TRACE("Loading service %s\n", wine_dbgstr_w(szName
));
422 err
= RegOpenKeyExW(db
->root_key
, szName
, 0, KEY_READ
, &hServiceKey
);
423 if (err
== ERROR_SUCCESS
)
425 err
= load_service_config(hServiceKey
, entry
);
426 RegCloseKey(hServiceKey
);
429 if (err
!= ERROR_SUCCESS
)
431 WINE_ERR("Error %d reading registry key for service %s - skipping\n", err
, wine_dbgstr_w(szName
));
432 free_service_entry(entry
);
436 if (entry
->config
.dwServiceType
== 0)
438 /* Maybe an application only wrote some configuration in the service key. Continue silently */
439 WINE_TRACE("Even the service type not set for service %s - skipping\n", wine_dbgstr_w(szName
));
440 free_service_entry(entry
);
444 if (!validate_service_config(entry
))
446 WINE_ERR("Invalid configuration of service %s - skipping\n", wine_dbgstr_w(szName
));
447 free_service_entry(entry
);
451 entry
->status
.dwServiceType
= entry
->config
.dwServiceType
;
454 list_add_tail(&db
->services
, &entry
->entry
);
456 return ERROR_SUCCESS
;
459 DWORD
scmdatabase_lock_startup(struct scmdatabase
*db
)
461 if (InterlockedCompareExchange(&db
->service_start_lock
, TRUE
, FALSE
))
462 return ERROR_SERVICE_DATABASE_LOCKED
;
463 return ERROR_SUCCESS
;
466 void scmdatabase_unlock_startup(struct scmdatabase
*db
)
468 InterlockedCompareExchange(&db
->service_start_lock
, FALSE
, TRUE
);
471 void scmdatabase_lock_shared(struct scmdatabase
*db
)
473 EnterCriticalSection(&db
->cs
);
476 void scmdatabase_lock_exclusive(struct scmdatabase
*db
)
478 EnterCriticalSection(&db
->cs
);
481 void scmdatabase_unlock(struct scmdatabase
*db
)
483 LeaveCriticalSection(&db
->cs
);
486 void service_lock_shared(struct service_entry
*service
)
488 EnterCriticalSection(&service
->db
->cs
);
491 void service_lock_exclusive(struct service_entry
*service
)
493 EnterCriticalSection(&service
->db
->cs
);
496 void service_unlock(struct service_entry
*service
)
498 LeaveCriticalSection(&service
->db
->cs
);
501 /* only one service started at a time, so there is no race on the registry
503 static LPWSTR
service_get_pipe_name(void)
505 static const WCHAR format
[] = { '\\','\\','.','\\','p','i','p','e','\\',
506 'n','e','t','\\','N','t','C','o','n','t','r','o','l','P','i','p','e','%','u',0};
507 static const WCHAR service_current_key_str
[] = { 'S','Y','S','T','E','M','\\',
508 'C','u','r','r','e','n','t','C','o','n','t','r','o','l','S','e','t','\\',
509 'C','o','n','t','r','o','l','\\',
510 'S','e','r','v','i','c','e','C','u','r','r','e','n','t',0};
513 HKEY service_current_key
;
514 DWORD service_current
= -1;
518 ret
= RegCreateKeyExW(HKEY_LOCAL_MACHINE
, service_current_key_str
, 0,
519 NULL
, REG_OPTION_VOLATILE
, KEY_SET_VALUE
| KEY_QUERY_VALUE
, NULL
,
520 &service_current_key
, NULL
);
521 if (ret
!= ERROR_SUCCESS
)
523 len
= sizeof(service_current
);
524 ret
= RegQueryValueExW(service_current_key
, NULL
, NULL
, &type
,
525 (BYTE
*)&service_current
, &len
);
526 if ((ret
== ERROR_SUCCESS
&& type
== REG_DWORD
) || ret
== ERROR_FILE_NOT_FOUND
)
529 RegSetValueExW(service_current_key
, NULL
, 0, REG_DWORD
,
530 (BYTE
*)&service_current
, sizeof(service_current
));
532 RegCloseKey(service_current_key
);
533 if ((ret
!= ERROR_SUCCESS
|| type
!= REG_DWORD
) && (ret
!= ERROR_FILE_NOT_FOUND
))
535 len
= sizeof(format
)/sizeof(WCHAR
) + 10 /* strlenW("4294967295") */;
536 name
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
539 snprintfW(name
, len
, format
, service_current
);
543 static DWORD
service_start_process(struct service_entry
*service_entry
, HANDLE
*process
)
545 PROCESS_INFORMATION pi
;
551 service_lock_exclusive(service_entry
);
553 size
= ExpandEnvironmentStringsW(service_entry
->config
.lpBinaryPathName
,NULL
,0);
554 path
= HeapAlloc(GetProcessHeap(),0,size
*sizeof(WCHAR
));
555 if (!path
) return ERROR_NOT_ENOUGH_SERVER_MEMORY
;
556 ExpandEnvironmentStringsW(service_entry
->config
.lpBinaryPathName
,path
,size
);
558 if (service_entry
->config
.dwServiceType
== SERVICE_KERNEL_DRIVER
)
560 static const WCHAR winedeviceW
[] = {'\\','w','i','n','e','d','e','v','i','c','e','.','e','x','e',' ',0};
561 WCHAR system_dir
[MAX_PATH
];
564 GetSystemDirectoryW( system_dir
, MAX_PATH
);
567 if (!GetBinaryTypeW( path
, &type
))
569 HeapFree( GetProcessHeap(), 0, path
);
570 return GetLastError();
572 if (type
== SCS_32BIT_BINARY
) GetSystemWow64DirectoryW( system_dir
, MAX_PATH
);
575 len
= strlenW( system_dir
) + sizeof(winedeviceW
)/sizeof(WCHAR
) + strlenW(service_entry
->name
);
576 HeapFree( GetProcessHeap(), 0, path
);
577 if (!(path
= HeapAlloc( GetProcessHeap(), 0, len
* sizeof(WCHAR
) )))
578 return ERROR_NOT_ENOUGH_SERVER_MEMORY
;
579 lstrcpyW( path
, system_dir
);
580 lstrcatW( path
, winedeviceW
);
581 lstrcatW( path
, service_entry
->name
);
584 ZeroMemory(&si
, sizeof(STARTUPINFOW
));
585 si
.cb
= sizeof(STARTUPINFOW
);
586 if (!(service_entry
->config
.dwServiceType
& SERVICE_INTERACTIVE_PROCESS
))
588 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};
589 si
.lpDesktop
= desktopW
;
592 service_entry
->status
.dwCurrentState
= SERVICE_START_PENDING
;
594 service_unlock(service_entry
);
596 r
= CreateProcessW(NULL
, path
, NULL
, NULL
, FALSE
, 0, NULL
, NULL
, &si
, &pi
);
597 HeapFree(GetProcessHeap(),0,path
);
600 service_lock_exclusive(service_entry
);
601 service_entry
->status
.dwCurrentState
= SERVICE_STOPPED
;
602 service_unlock(service_entry
);
603 return GetLastError();
606 service_entry
->status
.dwProcessId
= pi
.dwProcessId
;
607 *process
= pi
.hProcess
;
608 CloseHandle( pi
.hThread
);
610 return ERROR_SUCCESS
;
613 static DWORD
service_wait_for_startup(struct service_entry
*service_entry
, HANDLE process_handle
)
615 WINE_TRACE("%p\n", service_entry
);
619 DWORD dwCurrentStatus
;
620 HANDLE handles
[2] = { service_entry
->status_changed_event
, process_handle
};
622 ret
= WaitForMultipleObjects(sizeof(handles
)/sizeof(handles
[0]), handles
, FALSE
, 20000);
623 if (ret
!= WAIT_OBJECT_0
)
624 return ERROR_SERVICE_REQUEST_TIMEOUT
;
625 service_lock_shared(service_entry
);
626 dwCurrentStatus
= service_entry
->status
.dwCurrentState
;
627 service_unlock(service_entry
);
628 if (dwCurrentStatus
== SERVICE_RUNNING
)
630 WINE_TRACE("Service started successfully\n");
631 return ERROR_SUCCESS
;
633 if (dwCurrentStatus
!= SERVICE_START_PENDING
)
634 return ERROR_SERVICE_REQUEST_TIMEOUT
;
638 /******************************************************************************
639 * service_send_start_message
641 static BOOL
service_send_start_message(struct service_entry
*service
, LPCWSTR
*argv
, DWORD argc
)
643 DWORD i
, len
, count
, result
;
644 service_start_info
*ssi
;
648 WINE_TRACE("%s %p %d\n", wine_dbgstr_w(service
->name
), argv
, argc
);
650 /* FIXME: this can block so should be done in another thread */
651 r
= ConnectNamedPipe(service
->control_pipe
, NULL
);
652 if (!r
&& GetLastError() != ERROR_PIPE_CONNECTED
)
654 WINE_ERR("pipe connect failed\n");
658 /* calculate how much space do we need to send the startup info */
659 len
= strlenW(service
->name
) + 1;
660 for (i
=0; i
<argc
; i
++)
661 len
+= strlenW(argv
[i
])+1;
664 ssi
= HeapAlloc(GetProcessHeap(),0,FIELD_OFFSET(service_start_info
, data
[len
]));
665 ssi
->cmd
= WINESERV_STARTINFO
;
667 ssi
->total_size
= FIELD_OFFSET(service_start_info
, data
[len
]);
668 ssi
->name_size
= strlenW(service
->name
) + 1;
669 strcpyW( ssi
->data
, service
->name
);
671 /* copy service args into a single buffer*/
672 p
= &ssi
->data
[ssi
->name_size
];
673 for (i
=0; i
<argc
; i
++)
680 r
= WriteFile(service
->control_pipe
, ssi
, ssi
->total_size
, &count
, NULL
);
683 r
= ReadFile(service
->control_pipe
, &result
, sizeof result
, &count
, NULL
);
686 SetLastError(result
);
691 HeapFree(GetProcessHeap(),0,ssi
);
696 DWORD
service_start(struct service_entry
*service
, DWORD service_argc
, LPCWSTR
*service_argv
)
700 HANDLE process_handle
= NULL
;
702 err
= scmdatabase_lock_startup(service
->db
);
703 if (err
!= ERROR_SUCCESS
)
706 if (service
->control_pipe
!= INVALID_HANDLE_VALUE
)
708 scmdatabase_unlock_startup(service
->db
);
709 return ERROR_SERVICE_ALREADY_RUNNING
;
712 service
->control_mutex
= CreateMutexW(NULL
, TRUE
, NULL
);
714 if (!service
->status_changed_event
)
715 service
->status_changed_event
= CreateEventW(NULL
, FALSE
, FALSE
, NULL
);
717 name
= service_get_pipe_name();
718 service
->control_pipe
= CreateNamedPipeW(name
, PIPE_ACCESS_DUPLEX
,
719 PIPE_TYPE_BYTE
|PIPE_WAIT
, 1, 256, 256, 10000, NULL
);
720 HeapFree(GetProcessHeap(), 0, name
);
721 if (service
->control_pipe
==INVALID_HANDLE_VALUE
)
723 WINE_ERR("failed to create pipe for %s, error = %d\n",
724 wine_dbgstr_w(service
->name
), GetLastError());
725 scmdatabase_unlock_startup(service
->db
);
726 return GetLastError();
729 err
= service_start_process(service
, &process_handle
);
731 if (err
== ERROR_SUCCESS
)
733 if (!service_send_start_message(service
, service_argv
, service_argc
))
734 err
= ERROR_SERVICE_REQUEST_TIMEOUT
;
737 if (err
== ERROR_SUCCESS
)
738 err
= service_wait_for_startup(service
, process_handle
);
741 CloseHandle(process_handle
);
743 ReleaseMutex(service
->control_mutex
);
744 scmdatabase_unlock_startup(service
->db
);
746 WINE_TRACE("returning %d\n", err
);
752 int main(int argc
, char *argv
[])
754 static const WCHAR svcctl_started_event
[] = SVCCTL_STARTED_EVENT
;
756 g_hStartedEvent
= CreateEventW(NULL
, TRUE
, FALSE
, svcctl_started_event
);
757 err
= scmdatabase_create(&active_database
);
758 if (err
!= ERROR_SUCCESS
)
760 if ((err
= scmdatabase_load_services(active_database
)) != ERROR_SUCCESS
)
762 if ((err
= RPC_Init()) == ERROR_SUCCESS
)
764 scmdatabase_autostart_services(active_database
);
767 scmdatabase_destroy(active_database
);