2 * Services.exe - RPC functions
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
22 #define NONAMELESSSTRUCT
23 #define NONAMELESSUNION
32 #include "wine/list.h"
33 #include "wine/debug.h"
38 extern HANDLE CDECL
__wine_make_process_system(void);
40 WINE_DEFAULT_DEBUG_CHANNEL(service
);
42 static const GENERIC_MAPPING g_scm_generic
=
44 (STANDARD_RIGHTS_READ
| SC_MANAGER_ENUMERATE_SERVICE
| SC_MANAGER_QUERY_LOCK_STATUS
),
45 (STANDARD_RIGHTS_WRITE
| SC_MANAGER_CREATE_SERVICE
| SC_MANAGER_MODIFY_BOOT_CONFIG
),
46 (STANDARD_RIGHTS_EXECUTE
| SC_MANAGER_CONNECT
| SC_MANAGER_LOCK
),
50 static const GENERIC_MAPPING g_svc_generic
=
52 (STANDARD_RIGHTS_READ
| SERVICE_QUERY_CONFIG
| SERVICE_QUERY_STATUS
| SERVICE_INTERROGATE
| SERVICE_ENUMERATE_DEPENDENTS
),
53 (STANDARD_RIGHTS_WRITE
| SERVICE_CHANGE_CONFIG
),
54 (STANDARD_RIGHTS_EXECUTE
| SERVICE_START
| SERVICE_STOP
| SERVICE_PAUSE_CONTINUE
| SERVICE_USER_DEFINED_CONTROL
),
60 SC_HTYPE_DONT_CARE
= 0,
72 struct sc_manager_handle
/* service control manager handle */
75 struct scmdatabase
*db
;
78 struct sc_notify_handle
84 SC_RPC_NOTIFY_PARAMS_LIST
*params_list
;
87 struct sc_service_handle
/* service handle */
92 struct service_entry
*service_entry
;
93 struct sc_notify_handle
*notify
;
96 static void sc_notify_retain(struct sc_notify_handle
*notify
)
98 InterlockedIncrement(¬ify
->ref
);
101 static void sc_notify_release(struct sc_notify_handle
*notify
)
103 ULONG r
= InterlockedDecrement(¬ify
->ref
);
106 CloseHandle(notify
->event
);
107 HeapFree(GetProcessHeap(), 0, notify
->params_list
);
108 HeapFree(GetProcessHeap(), 0, notify
);
114 struct scmdatabase
*db
;
117 static const WCHAR emptyW
[] = {0};
118 static PTP_CLEANUP_GROUP cleanup_group
;
121 static void CALLBACK
group_cancel_callback(void *object
, void *userdata
)
123 struct process_entry
*process
= object
;
124 release_process(process
);
127 static void CALLBACK
terminate_callback(TP_CALLBACK_INSTANCE
*instance
, void *context
,
128 TP_WAIT
*wait
, TP_WAIT_RESULT result
)
130 struct process_entry
*process
= context
;
131 if (result
== WAIT_TIMEOUT
) process_terminate(process
);
132 release_process(process
);
133 CloseThreadpoolWait(wait
);
136 static void terminate_after_timeout(struct process_entry
*process
, DWORD timeout
)
138 TP_CALLBACK_ENVIRON environment
;
139 LARGE_INTEGER timestamp
;
143 memset(&environment
, 0, sizeof(environment
));
144 environment
.Version
= 1;
145 environment
.CleanupGroup
= cleanup_group
;
146 environment
.CleanupGroupCancelCallback
= group_cancel_callback
;
148 timestamp
.QuadPart
= (ULONGLONG
)timeout
* -10000;
149 ft
.dwLowDateTime
= timestamp
.u
.LowPart
;
150 ft
.dwHighDateTime
= timestamp
.u
.HighPart
;
152 if ((wait
= CreateThreadpoolWait(terminate_callback
, grab_process(process
), &environment
)))
153 SetThreadpoolWait(wait
, process
->process
, &ft
);
155 release_process(process
);
158 static void CALLBACK
shutdown_callback(TP_CALLBACK_INSTANCE
*instance
, void *context
)
160 struct process_entry
*process
= context
;
163 result
= WaitForSingleObject(process
->control_mutex
, 30000);
164 if (result
== WAIT_OBJECT_0
)
166 process_send_control(process
, FALSE
, emptyW
, SERVICE_CONTROL_STOP
, NULL
, 0, &result
);
167 ReleaseMutex(process
->control_mutex
);
170 release_process(process
);
173 static void shutdown_shared_process(struct process_entry
*process
)
175 TP_CALLBACK_ENVIRON environment
;
176 struct service_entry
*service
;
177 struct scmdatabase
*db
= process
->db
;
179 scmdatabase_lock(db
);
180 LIST_FOR_EACH_ENTRY(service
, &db
->services
, struct service_entry
, entry
)
182 if (service
->process
!= process
) continue;
183 service
->status
.dwCurrentState
= SERVICE_STOP_PENDING
;
185 scmdatabase_unlock(db
);
187 memset(&environment
, 0, sizeof(environment
));
188 environment
.Version
= 1;
189 environment
.CleanupGroup
= cleanup_group
;
190 environment
.CleanupGroupCancelCallback
= group_cancel_callback
;
192 if (!TrySubmitThreadpoolCallback(shutdown_callback
, grab_process(process
), &environment
))
193 release_process(process
);
196 static void free_service_strings(struct service_entry
*old
, struct service_entry
*new)
198 QUERY_SERVICE_CONFIGW
*old_cfg
= &old
->config
;
199 QUERY_SERVICE_CONFIGW
*new_cfg
= &new->config
;
201 if (old_cfg
->lpBinaryPathName
!= new_cfg
->lpBinaryPathName
)
202 HeapFree(GetProcessHeap(), 0, old_cfg
->lpBinaryPathName
);
204 if (old_cfg
->lpLoadOrderGroup
!= new_cfg
->lpLoadOrderGroup
)
205 HeapFree(GetProcessHeap(), 0, old_cfg
->lpLoadOrderGroup
);
207 if (old_cfg
->lpServiceStartName
!= new_cfg
->lpServiceStartName
)
208 HeapFree(GetProcessHeap(), 0, old_cfg
->lpServiceStartName
);
210 if (old_cfg
->lpDisplayName
!= new_cfg
->lpDisplayName
)
211 HeapFree(GetProcessHeap(), 0, old_cfg
->lpDisplayName
);
213 if (old
->dependOnServices
!= new->dependOnServices
)
214 HeapFree(GetProcessHeap(), 0, old
->dependOnServices
);
216 if (old
->dependOnGroups
!= new->dependOnGroups
)
217 HeapFree(GetProcessHeap(), 0, old
->dependOnGroups
);
220 /* Check if the given handle is of the required type and allows the requested access. */
221 static DWORD
validate_context_handle(SC_RPC_HANDLE handle
, DWORD type
, DWORD needed_access
, struct sc_handle
**out_hdr
)
223 struct sc_handle
*hdr
= handle
;
225 if (type
!= SC_HTYPE_DONT_CARE
&& hdr
->type
!= type
)
227 WINE_ERR("Handle is of an invalid type (%d, %d)\n", hdr
->type
, type
);
228 return ERROR_INVALID_HANDLE
;
231 if ((needed_access
& hdr
->access
) != needed_access
)
233 WINE_ERR("Access denied - handle created with access %x, needed %x\n", hdr
->access
, needed_access
);
234 return ERROR_ACCESS_DENIED
;
238 return ERROR_SUCCESS
;
241 static DWORD
validate_scm_handle(SC_RPC_HANDLE handle
, DWORD needed_access
, struct sc_manager_handle
**manager
)
243 struct sc_handle
*hdr
;
244 DWORD err
= validate_context_handle(handle
, SC_HTYPE_MANAGER
, needed_access
, &hdr
);
245 if (err
== ERROR_SUCCESS
)
246 *manager
= (struct sc_manager_handle
*)hdr
;
250 static DWORD
validate_service_handle(SC_RPC_HANDLE handle
, DWORD needed_access
, struct sc_service_handle
**service
)
252 struct sc_handle
*hdr
;
253 DWORD err
= validate_context_handle(handle
, SC_HTYPE_SERVICE
, needed_access
, &hdr
);
254 if (err
== ERROR_SUCCESS
)
255 *service
= (struct sc_service_handle
*)hdr
;
259 static DWORD
validate_notify_handle(SC_RPC_HANDLE handle
, DWORD needed_access
, struct sc_notify_handle
**notify
)
261 struct sc_handle
*hdr
;
262 DWORD err
= validate_context_handle(handle
, SC_HTYPE_NOTIFY
, needed_access
, &hdr
);
263 if (err
== ERROR_SUCCESS
)
264 *notify
= (struct sc_notify_handle
*)hdr
;
268 DWORD __cdecl
svcctl_OpenSCManagerW(
269 MACHINE_HANDLEW MachineName
, /* Note: this parameter is ignored */
270 LPCWSTR DatabaseName
,
272 SC_RPC_HANDLE
*handle
)
274 struct sc_manager_handle
*manager
;
276 WINE_TRACE("(%s, %s, %x)\n", wine_dbgstr_w(MachineName
), wine_dbgstr_w(DatabaseName
), dwAccessMask
);
278 if (DatabaseName
!= NULL
&& DatabaseName
[0])
280 if (lstrcmpW(DatabaseName
, SERVICES_FAILED_DATABASEW
) == 0)
281 return ERROR_DATABASE_DOES_NOT_EXIST
;
282 if (lstrcmpW(DatabaseName
, SERVICES_ACTIVE_DATABASEW
) != 0)
283 return ERROR_INVALID_NAME
;
286 if (!(manager
= HeapAlloc(GetProcessHeap(), 0, sizeof(*manager
))))
287 return ERROR_NOT_ENOUGH_SERVER_MEMORY
;
289 manager
->hdr
.type
= SC_HTYPE_MANAGER
;
291 if (dwAccessMask
& MAXIMUM_ALLOWED
)
292 dwAccessMask
|= SC_MANAGER_ALL_ACCESS
;
293 manager
->hdr
.access
= dwAccessMask
;
294 RtlMapGenericMask(&manager
->hdr
.access
, &g_scm_generic
);
295 manager
->db
= active_database
;
296 *handle
= &manager
->hdr
;
298 return ERROR_SUCCESS
;
301 static void SC_RPC_HANDLE_destroy(SC_RPC_HANDLE handle
)
303 struct sc_handle
*hdr
= handle
;
306 case SC_HTYPE_MANAGER
:
308 struct sc_manager_handle
*manager
= (struct sc_manager_handle
*)hdr
;
309 HeapFree(GetProcessHeap(), 0, manager
);
312 case SC_HTYPE_SERVICE
:
314 struct sc_service_handle
*service
= (struct sc_service_handle
*)hdr
;
315 service_lock(service
->service_entry
);
316 list_remove(&service
->entry
);
319 SetEvent(service
->notify
->event
);
320 sc_notify_release(service
->notify
);
322 service_unlock(service
->service_entry
);
323 release_service(service
->service_entry
);
324 HeapFree(GetProcessHeap(), 0, service
);
328 WINE_ERR("invalid handle type %d\n", hdr
->type
);
329 RpcRaiseException(ERROR_INVALID_HANDLE
);
333 DWORD __cdecl
svcctl_GetServiceDisplayNameW(
334 SC_RPC_HANDLE hSCManager
,
335 LPCWSTR lpServiceName
,
339 struct sc_manager_handle
*manager
;
340 struct service_entry
*entry
;
343 WINE_TRACE("(%s, %d)\n", wine_dbgstr_w(lpServiceName
), *cchBufSize
);
345 if ((err
= validate_scm_handle(hSCManager
, 0, &manager
)) != ERROR_SUCCESS
)
348 scmdatabase_lock(manager
->db
);
350 entry
= scmdatabase_find_service(manager
->db
, lpServiceName
);
355 name
= get_display_name(entry
);
356 len
= lstrlenW(name
);
357 if (len
<= *cchBufSize
)
360 memcpy(lpBuffer
, name
, (len
+ 1)*sizeof(*name
));
363 err
= ERROR_INSUFFICIENT_BUFFER
;
367 err
= ERROR_SERVICE_DOES_NOT_EXIST
;
369 scmdatabase_unlock(manager
->db
);
371 if (err
!= ERROR_SUCCESS
)
377 DWORD __cdecl
svcctl_GetServiceKeyNameW(
378 SC_RPC_HANDLE hSCManager
,
379 LPCWSTR lpServiceDisplayName
,
383 struct service_entry
*entry
;
384 struct sc_manager_handle
*manager
;
387 WINE_TRACE("(%s, %d)\n", wine_dbgstr_w(lpServiceDisplayName
), *cchBufSize
);
389 if ((err
= validate_scm_handle(hSCManager
, 0, &manager
)) != ERROR_SUCCESS
)
392 scmdatabase_lock(manager
->db
);
394 entry
= scmdatabase_find_service_by_displayname(manager
->db
, lpServiceDisplayName
);
398 len
= lstrlenW(entry
->name
);
399 if (len
<= *cchBufSize
)
402 memcpy(lpBuffer
, entry
->name
, (len
+ 1)*sizeof(*entry
->name
));
405 err
= ERROR_INSUFFICIENT_BUFFER
;
409 err
= ERROR_SERVICE_DOES_NOT_EXIST
;
411 scmdatabase_unlock(manager
->db
);
413 if (err
!= ERROR_SUCCESS
)
419 static DWORD
create_handle_for_service(struct service_entry
*entry
, DWORD dwDesiredAccess
, SC_RPC_HANDLE
*phService
)
421 struct sc_service_handle
*service
;
423 if (!(service
= HeapAlloc(GetProcessHeap(), 0, sizeof(*service
))))
425 release_service(entry
);
426 return ERROR_NOT_ENOUGH_SERVER_MEMORY
;
429 if (dwDesiredAccess
& MAXIMUM_ALLOWED
)
430 dwDesiredAccess
|= SERVICE_ALL_ACCESS
;
432 service
->hdr
.type
= SC_HTYPE_SERVICE
;
433 service
->hdr
.access
= dwDesiredAccess
;
434 service
->notify
= NULL
;
435 service
->status_notified
= FALSE
;
436 RtlMapGenericMask(&service
->hdr
.access
, &g_svc_generic
);
439 service
->service_entry
= entry
;
440 list_add_tail(&entry
->handles
, &service
->entry
);
441 service_unlock(entry
);
443 *phService
= &service
->hdr
;
444 return ERROR_SUCCESS
;
447 DWORD __cdecl
svcctl_OpenServiceW(
448 SC_RPC_HANDLE hSCManager
,
449 LPCWSTR lpServiceName
,
450 DWORD dwDesiredAccess
,
451 SC_RPC_HANDLE
*phService
)
453 struct sc_manager_handle
*manager
;
454 struct service_entry
*entry
;
457 WINE_TRACE("(%s, 0x%x)\n", wine_dbgstr_w(lpServiceName
), dwDesiredAccess
);
459 if ((err
= validate_scm_handle(hSCManager
, 0, &manager
)) != ERROR_SUCCESS
)
461 if (!validate_service_name(lpServiceName
))
462 return ERROR_INVALID_NAME
;
464 scmdatabase_lock(manager
->db
);
465 entry
= grab_service(scmdatabase_find_service(manager
->db
, lpServiceName
));
466 scmdatabase_unlock(manager
->db
);
469 return ERROR_SERVICE_DOES_NOT_EXIST
;
471 return create_handle_for_service(entry
, dwDesiredAccess
, phService
);
474 static DWORD
parse_dependencies(const WCHAR
*dependencies
, struct service_entry
*entry
)
476 WCHAR
*services
= NULL
, *groups
, *s
;
477 DWORD len
, len_services
= 0, len_groups
= 0;
478 const WCHAR
*ptr
= dependencies
;
480 if (!dependencies
|| !dependencies
[0])
482 entry
->dependOnServices
= NULL
;
483 entry
->dependOnGroups
= NULL
;
484 return ERROR_SUCCESS
;
489 len
= lstrlenW(ptr
) + 1;
490 if (ptr
[0] == '+' && ptr
[1])
491 len_groups
+= len
- 1;
496 if (!len_services
) entry
->dependOnServices
= NULL
;
499 services
= HeapAlloc(GetProcessHeap(), 0, (len_services
+ 1) * sizeof(WCHAR
));
501 return ERROR_OUTOFMEMORY
;
507 len
= lstrlenW(ptr
) + 1;
516 entry
->dependOnServices
= services
;
518 if (!len_groups
) entry
->dependOnGroups
= NULL
;
521 groups
= HeapAlloc(GetProcessHeap(), 0, (len_groups
+ 1) * sizeof(WCHAR
));
524 HeapFree(GetProcessHeap(), 0, services
);
525 return ERROR_OUTOFMEMORY
;
531 len
= lstrlenW(ptr
) + 1;
532 if (ptr
[0] == '+' && ptr
[1])
534 lstrcpyW(s
, ptr
+ 1);
540 entry
->dependOnGroups
= groups
;
543 return ERROR_SUCCESS
;
546 static DWORD
create_serviceW(
547 SC_RPC_HANDLE hSCManager
,
548 LPCWSTR lpServiceName
,
549 LPCWSTR lpDisplayName
,
550 DWORD dwDesiredAccess
,
553 DWORD dwErrorControl
,
554 LPCWSTR lpBinaryPathName
,
555 LPCWSTR lpLoadOrderGroup
,
557 const BYTE
*lpDependencies
,
558 DWORD dwDependenciesSize
,
559 LPCWSTR lpServiceStartName
,
560 const BYTE
*lpPassword
,
561 DWORD dwPasswordSize
,
562 SC_RPC_HANDLE
*phService
,
565 struct service_entry
*entry
, *found
;
566 struct sc_manager_handle
*manager
;
569 WINE_TRACE("(%s, %s, 0x%x, %s)\n", wine_dbgstr_w(lpServiceName
), wine_dbgstr_w(lpDisplayName
), dwDesiredAccess
, wine_dbgstr_w(lpBinaryPathName
));
571 if ((err
= validate_scm_handle(hSCManager
, SC_MANAGER_CREATE_SERVICE
, &manager
)) != ERROR_SUCCESS
)
574 if (!validate_service_name(lpServiceName
))
575 return ERROR_INVALID_NAME
;
576 if (!check_multisz((LPCWSTR
)lpDependencies
, dwDependenciesSize
) || !lpServiceName
[0] || !lpBinaryPathName
[0])
577 return ERROR_INVALID_PARAMETER
;
580 WINE_FIXME("Don't know how to add a password\n"); /* I always get ERROR_GEN_FAILURE */
582 err
= service_create(lpServiceName
, &entry
);
583 if (err
!= ERROR_SUCCESS
)
586 err
= parse_dependencies((LPCWSTR
)lpDependencies
, entry
);
587 if (err
!= ERROR_SUCCESS
) {
588 free_service_entry(entry
);
592 entry
->is_wow64
= is_wow64
;
593 entry
->config
.dwServiceType
= entry
->status
.dwServiceType
= dwServiceType
;
594 entry
->config
.dwStartType
= dwStartType
;
595 entry
->config
.dwErrorControl
= dwErrorControl
;
596 entry
->config
.lpBinaryPathName
= strdupW(lpBinaryPathName
);
597 entry
->config
.lpLoadOrderGroup
= strdupW(lpLoadOrderGroup
);
598 entry
->config
.lpServiceStartName
= strdupW(lpServiceStartName
);
599 entry
->config
.lpDisplayName
= strdupW(lpDisplayName
);
601 if (lpdwTagId
) /* TODO: In most situations a non-NULL TagId will generate an ERROR_INVALID_PARAMETER. */
602 entry
->config
.dwTagId
= *lpdwTagId
;
604 entry
->config
.dwTagId
= 0;
606 /* other fields NULL*/
608 if (!validate_service_config(entry
))
610 WINE_ERR("Invalid data while trying to create service\n");
611 free_service_entry(entry
);
612 return ERROR_INVALID_PARAMETER
;
615 scmdatabase_lock(manager
->db
);
617 if ((found
= scmdatabase_find_service(manager
->db
, lpServiceName
)))
619 err
= is_marked_for_delete(found
) ? ERROR_SERVICE_MARKED_FOR_DELETE
: ERROR_SERVICE_EXISTS
;
620 scmdatabase_unlock(manager
->db
);
621 free_service_entry(entry
);
625 if (scmdatabase_find_service_by_displayname(manager
->db
, get_display_name(entry
)))
627 scmdatabase_unlock(manager
->db
);
628 free_service_entry(entry
);
629 return ERROR_DUPLICATE_SERVICE_NAME
;
632 err
= scmdatabase_add_service(manager
->db
, entry
);
633 if (err
!= ERROR_SUCCESS
)
635 scmdatabase_unlock(manager
->db
);
636 free_service_entry(entry
);
639 scmdatabase_unlock(manager
->db
);
641 return create_handle_for_service(entry
, dwDesiredAccess
, phService
);
644 DWORD __cdecl
svcctl_CreateServiceW(
645 SC_RPC_HANDLE hSCManager
,
646 LPCWSTR lpServiceName
,
647 LPCWSTR lpDisplayName
,
648 DWORD dwDesiredAccess
,
651 DWORD dwErrorControl
,
652 LPCWSTR lpBinaryPathName
,
653 LPCWSTR lpLoadOrderGroup
,
655 const BYTE
*lpDependencies
,
656 DWORD dwDependenciesSize
,
657 LPCWSTR lpServiceStartName
,
658 const BYTE
*lpPassword
,
659 DWORD dwPasswordSize
,
660 SC_RPC_HANDLE
*phService
)
662 WINE_TRACE("(%s, %s, 0x%x, %s)\n", wine_dbgstr_w(lpServiceName
), wine_dbgstr_w(lpDisplayName
), dwDesiredAccess
, wine_dbgstr_w(lpBinaryPathName
));
663 return create_serviceW(hSCManager
, lpServiceName
, lpDisplayName
, dwDesiredAccess
, dwServiceType
, dwStartType
,
664 dwErrorControl
, lpBinaryPathName
, lpLoadOrderGroup
, lpdwTagId
, lpDependencies
, dwDependenciesSize
, lpServiceStartName
,
665 lpPassword
, dwPasswordSize
, phService
, FALSE
);
668 DWORD __cdecl
svcctl_DeleteService(
669 SC_RPC_HANDLE hService
)
671 struct sc_service_handle
*service
;
674 if ((err
= validate_service_handle(hService
, DELETE
, &service
)) != ERROR_SUCCESS
)
677 service_lock(service
->service_entry
);
679 if (!is_marked_for_delete(service
->service_entry
))
680 err
= mark_for_delete(service
->service_entry
);
682 err
= ERROR_SERVICE_MARKED_FOR_DELETE
;
684 service_unlock(service
->service_entry
);
689 DWORD __cdecl
svcctl_QueryServiceConfigW(
690 SC_RPC_HANDLE hService
,
691 QUERY_SERVICE_CONFIGW
*config
,
695 struct sc_service_handle
*service
;
698 WINE_TRACE("(%p)\n", config
);
700 if ((err
= validate_service_handle(hService
, SERVICE_QUERY_CONFIG
, &service
)) != 0)
703 service_lock(service
->service_entry
);
704 config
->dwServiceType
= service
->service_entry
->config
.dwServiceType
;
705 config
->dwStartType
= service
->service_entry
->config
.dwStartType
;
706 config
->dwErrorControl
= service
->service_entry
->config
.dwErrorControl
;
707 config
->lpBinaryPathName
= strdupW(service
->service_entry
->config
.lpBinaryPathName
);
708 config
->lpLoadOrderGroup
= strdupW(service
->service_entry
->config
.lpLoadOrderGroup
);
709 config
->dwTagId
= service
->service_entry
->config
.dwTagId
;
710 config
->lpDependencies
= NULL
; /* TODO */
711 config
->lpServiceStartName
= strdupW(service
->service_entry
->config
.lpServiceStartName
);
712 config
->lpDisplayName
= strdupW(service
->service_entry
->config
.lpDisplayName
);
713 service_unlock(service
->service_entry
);
715 return ERROR_SUCCESS
;
718 DWORD __cdecl
svcctl_ChangeServiceConfigW(
719 SC_RPC_HANDLE hService
,
722 DWORD dwErrorControl
,
723 LPCWSTR lpBinaryPathName
,
724 LPCWSTR lpLoadOrderGroup
,
726 const BYTE
*lpDependencies
,
727 DWORD dwDependenciesSize
,
728 LPCWSTR lpServiceStartName
,
729 const BYTE
*lpPassword
,
730 DWORD dwPasswordSize
,
731 LPCWSTR lpDisplayName
)
733 struct service_entry new_entry
, *entry
;
734 struct sc_service_handle
*service
;
739 if ((err
= validate_service_handle(hService
, SERVICE_CHANGE_CONFIG
, &service
)) != 0)
742 if (!check_multisz((LPCWSTR
)lpDependencies
, dwDependenciesSize
))
743 return ERROR_INVALID_PARAMETER
;
745 /* first check if the new configuration is correct */
746 service_lock(service
->service_entry
);
748 if (is_marked_for_delete(service
->service_entry
))
750 service_unlock(service
->service_entry
);
751 return ERROR_SERVICE_MARKED_FOR_DELETE
;
754 if (lpDisplayName
!= NULL
&&
755 (entry
= scmdatabase_find_service_by_displayname(service
->service_entry
->db
, lpDisplayName
)) &&
756 (entry
!= service
->service_entry
))
758 service_unlock(service
->service_entry
);
759 return ERROR_DUPLICATE_SERVICE_NAME
;
762 new_entry
= *service
->service_entry
;
764 if (dwServiceType
!= SERVICE_NO_CHANGE
)
765 new_entry
.config
.dwServiceType
= dwServiceType
;
767 if (dwStartType
!= SERVICE_NO_CHANGE
)
768 new_entry
.config
.dwStartType
= dwStartType
;
770 if (dwErrorControl
!= SERVICE_NO_CHANGE
)
771 new_entry
.config
.dwErrorControl
= dwErrorControl
;
773 if (lpBinaryPathName
!= NULL
)
774 new_entry
.config
.lpBinaryPathName
= (LPWSTR
)lpBinaryPathName
;
776 if (lpLoadOrderGroup
!= NULL
)
777 new_entry
.config
.lpLoadOrderGroup
= (LPWSTR
)lpLoadOrderGroup
;
779 if (lpdwTagId
!= NULL
)
780 WINE_FIXME("Changing tag id not supported\n");
782 if (lpServiceStartName
!= NULL
)
783 new_entry
.config
.lpServiceStartName
= (LPWSTR
)lpServiceStartName
;
785 if (lpPassword
!= NULL
)
786 WINE_FIXME("Setting password not supported\n");
788 if (lpDisplayName
!= NULL
)
789 new_entry
.config
.lpDisplayName
= (LPWSTR
)lpDisplayName
;
791 err
= parse_dependencies((LPCWSTR
)lpDependencies
, &new_entry
);
792 if (err
!= ERROR_SUCCESS
)
794 service_unlock(service
->service_entry
);
798 if (!validate_service_config(&new_entry
))
800 WINE_ERR("The configuration after the change wouldn't be valid\n");
801 service_unlock(service
->service_entry
);
802 return ERROR_INVALID_PARAMETER
;
805 /* configuration OK. The strings needs to be duplicated */
806 if (lpBinaryPathName
!= NULL
)
807 new_entry
.config
.lpBinaryPathName
= strdupW(lpBinaryPathName
);
809 if (lpLoadOrderGroup
!= NULL
)
810 new_entry
.config
.lpLoadOrderGroup
= strdupW(lpLoadOrderGroup
);
812 if (lpServiceStartName
!= NULL
)
813 new_entry
.config
.lpServiceStartName
= strdupW(lpServiceStartName
);
815 if (lpDisplayName
!= NULL
)
816 new_entry
.config
.lpDisplayName
= strdupW(lpDisplayName
);
818 /* try to save to Registry, commit or rollback depending on success */
819 err
= save_service_config(&new_entry
);
820 if (ERROR_SUCCESS
== err
)
822 free_service_strings(service
->service_entry
, &new_entry
);
823 *service
->service_entry
= new_entry
;
825 else free_service_strings(&new_entry
, service
->service_entry
);
826 service_unlock(service
->service_entry
);
831 static void fill_status_process(SERVICE_STATUS_PROCESS
*status
, struct service_entry
*service
)
833 struct process_entry
*process
= service
->process
;
834 memcpy(status
, &service
->status
, sizeof(service
->status
));
835 status
->dwProcessId
= 0;
836 if (process
&& !(service
->status
.dwServiceType
& SERVICE_DRIVER
))
837 status
->dwProcessId
= process
->process_id
;
838 status
->dwServiceFlags
= 0;
841 static void fill_notify(struct sc_notify_handle
*notify
, struct service_entry
*service
)
843 SC_RPC_NOTIFY_PARAMS_LIST
*list
;
844 SERVICE_NOTIFY_STATUS_CHANGE_PARAMS_2
*cparams
;
846 list
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
,
847 sizeof(SC_RPC_NOTIFY_PARAMS_LIST
) + sizeof(SERVICE_NOTIFY_STATUS_CHANGE_PARAMS_2
));
851 cparams
= (SERVICE_NOTIFY_STATUS_CHANGE_PARAMS_2
*)(list
+ 1);
853 cparams
->dwNotifyMask
= notify
->notify_mask
;
854 fill_status_process(&cparams
->ServiceStatus
, service
);
855 cparams
->dwNotificationStatus
= ERROR_SUCCESS
;
856 cparams
->dwNotificationTriggered
= 1 << (cparams
->ServiceStatus
.dwCurrentState
- SERVICE_STOPPED
);
857 cparams
->pszServiceNames
= NULL
;
861 list
->NotifyParamsArray
[0].dwInfoLevel
= 2;
862 list
->NotifyParamsArray
[0].u
.params
= cparams
;
864 InterlockedExchangePointer((void**)¬ify
->params_list
, list
);
866 SetEvent(notify
->event
);
869 DWORD __cdecl
svcctl_SetServiceStatus(SC_RPC_HANDLE handle
, SERVICE_STATUS
*status
)
871 struct sc_service_handle
*service
, *service_handle
;
872 struct process_entry
*process
;
875 WINE_TRACE("(%p, %p)\n", handle
, status
);
877 if ((err
= validate_service_handle(handle
, SERVICE_SET_STATUS
, &service
)) != 0)
880 service_lock(service
->service_entry
);
882 /* FIXME: be a bit more discriminant about what parts of the status we set
883 * and check that fields are valid */
884 service
->service_entry
->status
.dwCurrentState
= status
->dwCurrentState
;
885 service
->service_entry
->status
.dwControlsAccepted
= status
->dwControlsAccepted
;
886 service
->service_entry
->status
.dwWin32ExitCode
= status
->dwWin32ExitCode
;
887 service
->service_entry
->status
.dwServiceSpecificExitCode
= status
->dwServiceSpecificExitCode
;
888 service
->service_entry
->status
.dwCheckPoint
= status
->dwCheckPoint
;
889 service
->service_entry
->status
.dwWaitHint
= status
->dwWaitHint
;
890 SetEvent(service
->service_entry
->status_changed_event
);
892 if ((process
= service
->service_entry
->process
) &&
893 status
->dwCurrentState
== SERVICE_STOPPED
)
895 service
->service_entry
->process
= NULL
;
896 if (!--process
->use_count
)
897 terminate_after_timeout(process
, service_kill_timeout
);
898 if (service
->service_entry
->shared_process
&& process
->use_count
<= 1)
899 shutdown_shared_process(process
);
900 release_process(process
);
903 mask
= 1 << (service
->service_entry
->status
.dwCurrentState
- SERVICE_STOPPED
);
904 LIST_FOR_EACH_ENTRY(service_handle
, &service
->service_entry
->handles
, struct sc_service_handle
, entry
)
906 struct sc_notify_handle
*notify
= service_handle
->notify
;
907 if (notify
&& (notify
->notify_mask
& mask
))
909 fill_notify(notify
, service
->service_entry
);
910 sc_notify_release(notify
);
911 service_handle
->notify
= NULL
;
912 service_handle
->status_notified
= TRUE
;
915 service_handle
->status_notified
= FALSE
;
918 service_unlock(service
->service_entry
);
920 return ERROR_SUCCESS
;
923 DWORD __cdecl
svcctl_ChangeServiceConfig2W( SC_RPC_HANDLE hService
, SC_RPC_CONFIG_INFOW config
)
925 struct sc_service_handle
*service
;
928 if ((err
= validate_service_handle(hService
, SERVICE_CHANGE_CONFIG
, &service
)) != 0)
931 switch (config
.dwInfoLevel
)
933 case SERVICE_CONFIG_DESCRIPTION
:
937 if (!config
.u
.descr
->lpDescription
)
940 if (config
.u
.descr
->lpDescription
[0])
942 if (!(descr
= strdupW( config
.u
.descr
->lpDescription
)))
943 return ERROR_NOT_ENOUGH_MEMORY
;
946 WINE_TRACE( "changing service %p descr to %s\n", service
, wine_dbgstr_w(descr
) );
947 service_lock( service
->service_entry
);
948 HeapFree( GetProcessHeap(), 0, service
->service_entry
->description
);
949 service
->service_entry
->description
= descr
;
950 save_service_config( service
->service_entry
);
951 service_unlock( service
->service_entry
);
954 case SERVICE_CONFIG_FAILURE_ACTIONS
:
955 WINE_FIXME( "SERVICE_CONFIG_FAILURE_ACTIONS not implemented: period %u msg %s cmd %s\n",
956 config
.u
.actions
->dwResetPeriod
,
957 wine_dbgstr_w(config
.u
.actions
->lpRebootMsg
),
958 wine_dbgstr_w(config
.u
.actions
->lpCommand
) );
960 case SERVICE_CONFIG_PRESHUTDOWN_INFO
:
961 WINE_TRACE( "changing service %p preshutdown timeout to %d\n",
962 service
, config
.u
.preshutdown
->dwPreshutdownTimeout
);
963 service_lock( service
->service_entry
);
964 service
->service_entry
->preshutdown_timeout
= config
.u
.preshutdown
->dwPreshutdownTimeout
;
965 save_service_config( service
->service_entry
);
966 service_unlock( service
->service_entry
);
969 WINE_FIXME("level %u not implemented\n", config
.dwInfoLevel
);
970 err
= ERROR_INVALID_LEVEL
;
976 DWORD __cdecl
svcctl_QueryServiceConfig2W( SC_RPC_HANDLE hService
, DWORD level
,
977 BYTE
*buffer
, DWORD size
, LPDWORD needed
)
979 struct sc_service_handle
*service
;
982 memset(buffer
, 0, size
);
984 if ((err
= validate_service_handle(hService
, SERVICE_QUERY_STATUS
, &service
)) != 0)
989 case SERVICE_CONFIG_DESCRIPTION
:
991 struct service_description
*desc
= (struct service_description
*)buffer
;
992 DWORD total_size
= sizeof(*desc
);
994 service_lock(service
->service_entry
);
995 if (service
->service_entry
->description
)
996 total_size
+= lstrlenW(service
->service_entry
->description
) * sizeof(WCHAR
);
998 *needed
= total_size
;
999 if (size
>= total_size
)
1001 if (service
->service_entry
->description
)
1003 lstrcpyW( desc
->description
, service
->service_entry
->description
);
1004 desc
->size
= total_size
- FIELD_OFFSET(struct service_description
, description
);
1008 desc
->description
[0] = 0;
1012 else err
= ERROR_INSUFFICIENT_BUFFER
;
1013 service_unlock(service
->service_entry
);
1017 case SERVICE_CONFIG_PRESHUTDOWN_INFO
:
1018 service_lock(service
->service_entry
);
1020 *needed
= sizeof(SERVICE_PRESHUTDOWN_INFO
);
1021 if (size
>= *needed
)
1022 ((LPSERVICE_PRESHUTDOWN_INFO
)buffer
)->dwPreshutdownTimeout
=
1023 service
->service_entry
->preshutdown_timeout
;
1024 else err
= ERROR_INSUFFICIENT_BUFFER
;
1026 service_unlock(service
->service_entry
);
1030 WINE_FIXME("level %u not implemented\n", level
);
1031 err
= ERROR_INVALID_LEVEL
;
1037 DWORD __cdecl
svcctl_QueryServiceStatusEx(
1038 SC_RPC_HANDLE hService
,
1039 SC_STATUS_TYPE InfoLevel
,
1042 LPDWORD pcbBytesNeeded
)
1044 struct sc_service_handle
*service
;
1046 LPSERVICE_STATUS_PROCESS pSvcStatusData
;
1048 memset(lpBuffer
, 0, cbBufSize
);
1050 if ((err
= validate_service_handle(hService
, SERVICE_QUERY_STATUS
, &service
)) != 0)
1053 if (InfoLevel
!= SC_STATUS_PROCESS_INFO
)
1054 return ERROR_INVALID_LEVEL
;
1056 pSvcStatusData
= (LPSERVICE_STATUS_PROCESS
) lpBuffer
;
1057 if (pSvcStatusData
== NULL
)
1058 return ERROR_INVALID_PARAMETER
;
1060 if (cbBufSize
< sizeof(SERVICE_STATUS_PROCESS
))
1062 if( pcbBytesNeeded
!= NULL
)
1063 *pcbBytesNeeded
= sizeof(SERVICE_STATUS_PROCESS
);
1065 return ERROR_INSUFFICIENT_BUFFER
;
1068 service_lock(service
->service_entry
);
1069 fill_status_process(pSvcStatusData
, service
->service_entry
);
1070 service_unlock(service
->service_entry
);
1072 return ERROR_SUCCESS
;
1075 /******************************************************************************
1076 * service_accepts_control
1078 static BOOL
service_accepts_control(const struct service_entry
*service
, DWORD dwControl
)
1080 DWORD a
= service
->status
.dwControlsAccepted
;
1082 if (dwControl
>= 128 && dwControl
<= 255)
1087 case SERVICE_CONTROL_INTERROGATE
:
1089 case SERVICE_CONTROL_STOP
:
1090 if (a
&SERVICE_ACCEPT_STOP
)
1093 case SERVICE_CONTROL_SHUTDOWN
:
1094 if (a
&SERVICE_ACCEPT_SHUTDOWN
)
1097 case SERVICE_CONTROL_PAUSE
:
1098 case SERVICE_CONTROL_CONTINUE
:
1099 if (a
&SERVICE_ACCEPT_PAUSE_CONTINUE
)
1102 case SERVICE_CONTROL_PARAMCHANGE
:
1103 if (a
&SERVICE_ACCEPT_PARAMCHANGE
)
1106 case SERVICE_CONTROL_NETBINDADD
:
1107 case SERVICE_CONTROL_NETBINDREMOVE
:
1108 case SERVICE_CONTROL_NETBINDENABLE
:
1109 case SERVICE_CONTROL_NETBINDDISABLE
:
1110 if (a
&SERVICE_ACCEPT_NETBINDCHANGE
)
1112 case SERVICE_CONTROL_HARDWAREPROFILECHANGE
:
1113 if (a
&SERVICE_ACCEPT_HARDWAREPROFILECHANGE
)
1116 case SERVICE_CONTROL_POWEREVENT
:
1117 if (a
&SERVICE_ACCEPT_POWEREVENT
)
1120 case SERVICE_CONTROL_SESSIONCHANGE
:
1121 if (a
&SERVICE_ACCEPT_SESSIONCHANGE
)
1128 /******************************************************************************
1129 * process_send_command
1131 static BOOL
process_send_command(struct process_entry
*process
, const void *data
, DWORD size
, DWORD
*result
)
1133 OVERLAPPED overlapped
;
1137 overlapped
.u
.s
.Offset
= 0;
1138 overlapped
.u
.s
.OffsetHigh
= 0;
1139 overlapped
.hEvent
= process
->overlapped_event
;
1140 r
= WriteFile(process
->control_pipe
, data
, size
, &count
, &overlapped
);
1141 if (!r
&& GetLastError() == ERROR_IO_PENDING
)
1143 ret
= WaitForSingleObject(process
->overlapped_event
, service_pipe_timeout
);
1144 if (ret
== WAIT_TIMEOUT
)
1146 WINE_ERR("sending command timed out\n");
1147 *result
= ERROR_SERVICE_REQUEST_TIMEOUT
;
1150 r
= GetOverlappedResult(process
->control_pipe
, &overlapped
, &count
, FALSE
);
1152 if (!r
|| count
!= size
)
1154 WINE_ERR("service protocol error - failed to write pipe!\n");
1155 *result
= (!r
? GetLastError() : ERROR_WRITE_FAULT
);
1158 r
= ReadFile(process
->control_pipe
, result
, sizeof *result
, &count
, &overlapped
);
1159 if (!r
&& GetLastError() == ERROR_IO_PENDING
)
1161 ret
= WaitForSingleObject(process
->overlapped_event
, service_pipe_timeout
);
1162 if (ret
== WAIT_TIMEOUT
)
1164 WINE_ERR("receiving command result timed out\n");
1165 *result
= ERROR_SERVICE_REQUEST_TIMEOUT
;
1168 r
= GetOverlappedResult(process
->control_pipe
, &overlapped
, &count
, FALSE
);
1170 if (!r
|| count
!= sizeof *result
)
1172 WINE_ERR("service protocol error - failed to read pipe "
1173 "r = %d count = %d!\n", r
, count
);
1174 *result
= (!r
? GetLastError() : ERROR_READ_FAULT
);
1181 /******************************************************************************
1182 * process_send_control
1184 BOOL
process_send_control(struct process_entry
*process
, BOOL shared_process
, const WCHAR
*name
,
1185 DWORD control
, const BYTE
*data
, DWORD data_size
, DWORD
*result
)
1187 service_start_info
*ssi
;
1193 control
|= SERVICE_CONTROL_FORWARD_FLAG
;
1194 data
= (BYTE
*)name
;
1195 data_size
= (lstrlenW(name
) + 1) * sizeof(WCHAR
);
1199 /* calculate how much space we need to send the startup info */
1200 len
= (lstrlenW(name
) + 1) * sizeof(WCHAR
) + data_size
;
1202 ssi
= HeapAlloc(GetProcessHeap(),0,FIELD_OFFSET(service_start_info
, data
[len
]));
1203 ssi
->magic
= SERVICE_PROTOCOL_MAGIC
;
1204 ssi
->control
= control
;
1205 ssi
->total_size
= FIELD_OFFSET(service_start_info
, data
[len
]);
1206 ssi
->name_size
= lstrlenW(name
) + 1;
1207 lstrcpyW((WCHAR
*)ssi
->data
, name
);
1208 if (data_size
) memcpy(&ssi
->data
[ssi
->name_size
* sizeof(WCHAR
)], data
, data_size
);
1210 r
= process_send_command(process
, ssi
, ssi
->total_size
, result
);
1211 HeapFree( GetProcessHeap(), 0, ssi
);
1215 DWORD __cdecl
svcctl_StartServiceW(
1216 SC_RPC_HANDLE hService
,
1217 DWORD dwNumServiceArgs
,
1218 LPCWSTR
*lpServiceArgVectors
)
1220 struct sc_service_handle
*service
;
1223 WINE_TRACE("(%p, %d, %p)\n", hService
, dwNumServiceArgs
, lpServiceArgVectors
);
1225 if ((err
= validate_service_handle(hService
, SERVICE_START
, &service
)) != 0)
1228 if (service
->service_entry
->config
.dwStartType
== SERVICE_DISABLED
)
1229 return ERROR_SERVICE_DISABLED
;
1231 if (!scmdatabase_lock_startup(service
->service_entry
->db
, 3000))
1232 return ERROR_SERVICE_DATABASE_LOCKED
;
1234 err
= service_start(service
->service_entry
, dwNumServiceArgs
, lpServiceArgVectors
);
1236 scmdatabase_unlock_startup(service
->service_entry
->db
);
1240 DWORD __cdecl
svcctl_ControlService(
1241 SC_RPC_HANDLE hService
,
1243 SERVICE_STATUS
*lpServiceStatus
)
1245 DWORD access_required
;
1246 struct sc_service_handle
*service
;
1247 struct process_entry
*process
;
1248 BOOL shared_process
;
1251 WINE_TRACE("(%p, %d, %p)\n", hService
, dwControl
, lpServiceStatus
);
1255 case SERVICE_CONTROL_CONTINUE
:
1256 case SERVICE_CONTROL_NETBINDADD
:
1257 case SERVICE_CONTROL_NETBINDDISABLE
:
1258 case SERVICE_CONTROL_NETBINDENABLE
:
1259 case SERVICE_CONTROL_NETBINDREMOVE
:
1260 case SERVICE_CONTROL_PARAMCHANGE
:
1261 case SERVICE_CONTROL_PAUSE
:
1262 access_required
= SERVICE_PAUSE_CONTINUE
;
1264 case SERVICE_CONTROL_INTERROGATE
:
1265 access_required
= SERVICE_INTERROGATE
;
1267 case SERVICE_CONTROL_STOP
:
1268 access_required
= SERVICE_STOP
;
1271 if (dwControl
>= 128 && dwControl
<= 255)
1272 access_required
= SERVICE_USER_DEFINED_CONTROL
;
1274 return ERROR_INVALID_PARAMETER
;
1277 if ((result
= validate_service_handle(hService
, access_required
, &service
)) != 0)
1280 service_lock(service
->service_entry
);
1282 result
= ERROR_SUCCESS
;
1283 switch (service
->service_entry
->status
.dwCurrentState
)
1285 case SERVICE_STOPPED
:
1286 result
= ERROR_SERVICE_NOT_ACTIVE
;
1288 case SERVICE_START_PENDING
:
1289 if (dwControl
==SERVICE_CONTROL_STOP
)
1292 case SERVICE_STOP_PENDING
:
1293 result
= ERROR_SERVICE_CANNOT_ACCEPT_CTRL
;
1297 if (result
== ERROR_SUCCESS
&& service
->service_entry
->force_shutdown
)
1299 result
= ERROR_SERVICE_CANNOT_ACCEPT_CTRL
;
1300 if ((process
= service
->service_entry
->process
))
1302 service
->service_entry
->process
= NULL
;
1303 if (!--process
->use_count
) process_terminate(process
);
1304 release_process(process
);
1308 if (result
!= ERROR_SUCCESS
)
1310 if (lpServiceStatus
) *lpServiceStatus
= service
->service_entry
->status
;
1311 service_unlock(service
->service_entry
);
1315 if (!service_accepts_control(service
->service_entry
, dwControl
))
1317 service_unlock(service
->service_entry
);
1318 return ERROR_INVALID_SERVICE_CONTROL
;
1321 /* Remember that we tried to shutdown this service. When the service is
1322 * still running on the second invocation, it will be forcefully killed. */
1323 if (dwControl
== SERVICE_CONTROL_STOP
)
1324 service
->service_entry
->force_shutdown
= TRUE
;
1326 /* Hold a reference to the process while sending the command. */
1327 process
= grab_process(service
->service_entry
->process
);
1328 shared_process
= service
->service_entry
->shared_process
;
1329 service_unlock(service
->service_entry
);
1332 return ERROR_SERVICE_CANNOT_ACCEPT_CTRL
;
1334 result
= WaitForSingleObject(process
->control_mutex
, 30000);
1335 if (result
!= WAIT_OBJECT_0
)
1337 release_process(process
);
1338 return ERROR_SERVICE_REQUEST_TIMEOUT
;
1341 if (process_send_control(process
, shared_process
, service
->service_entry
->name
,
1342 dwControl
, NULL
, 0, &result
))
1343 result
= ERROR_SUCCESS
;
1345 if (lpServiceStatus
)
1347 service_lock(service
->service_entry
);
1348 *lpServiceStatus
= service
->service_entry
->status
;
1349 service_unlock(service
->service_entry
);
1352 ReleaseMutex(process
->control_mutex
);
1353 release_process(process
);
1357 DWORD __cdecl
svcctl_CloseServiceHandle(
1358 SC_RPC_HANDLE
*handle
)
1360 WINE_TRACE("(&%p)\n", *handle
);
1362 SC_RPC_HANDLE_destroy(*handle
);
1365 return ERROR_SUCCESS
;
1368 static void SC_RPC_LOCK_destroy(SC_RPC_LOCK hLock
)
1370 struct sc_lock
*lock
= hLock
;
1371 scmdatabase_unlock_startup(lock
->db
);
1372 HeapFree(GetProcessHeap(), 0, lock
);
1375 void __RPC_USER
SC_RPC_LOCK_rundown(SC_RPC_LOCK hLock
)
1377 SC_RPC_LOCK_destroy(hLock
);
1380 DWORD __cdecl
svcctl_LockServiceDatabase(
1381 SC_RPC_HANDLE hSCManager
,
1382 SC_RPC_LOCK
*phLock
)
1384 struct sc_manager_handle
*manager
;
1385 struct sc_lock
*lock
;
1388 WINE_TRACE("(%p, %p)\n", hSCManager
, phLock
);
1390 if ((err
= validate_scm_handle(hSCManager
, SC_MANAGER_LOCK
, &manager
)) != ERROR_SUCCESS
)
1393 if (!scmdatabase_lock_startup(manager
->db
, 0))
1394 return ERROR_SERVICE_DATABASE_LOCKED
;
1396 lock
= HeapAlloc(GetProcessHeap(), 0, sizeof(struct sc_lock
));
1399 scmdatabase_unlock_startup(manager
->db
);
1400 return ERROR_NOT_ENOUGH_SERVER_MEMORY
;
1403 lock
->db
= manager
->db
;
1406 return ERROR_SUCCESS
;
1409 DWORD __cdecl
svcctl_UnlockServiceDatabase(
1410 SC_RPC_LOCK
*phLock
)
1412 WINE_TRACE("(&%p)\n", *phLock
);
1414 SC_RPC_LOCK_destroy(*phLock
);
1417 return ERROR_SUCCESS
;
1420 static BOOL
map_state(DWORD state
, DWORD mask
)
1424 case SERVICE_START_PENDING
:
1425 case SERVICE_STOP_PENDING
:
1426 case SERVICE_RUNNING
:
1427 case SERVICE_CONTINUE_PENDING
:
1428 case SERVICE_PAUSE_PENDING
:
1429 case SERVICE_PAUSED
:
1430 if (SERVICE_ACTIVE
& mask
) return TRUE
;
1432 case SERVICE_STOPPED
:
1433 if (SERVICE_INACTIVE
& mask
) return TRUE
;
1436 WINE_ERR("unknown state %u\n", state
);
1442 DWORD __cdecl
svcctl_EnumServicesStatusW(
1443 SC_RPC_HANDLE hmngr
,
1452 DWORD err
, sz
, total_size
, num_services
, offset
;
1453 struct sc_manager_handle
*manager
;
1454 struct service_entry
*service
;
1455 struct enum_service_status
*s
;
1457 WINE_TRACE("(%p, 0x%x, 0x%x, %p, %u, %p, %p, %p)\n", hmngr
, type
, state
, buffer
, size
, needed
, returned
, resume
);
1459 if (!type
|| !state
)
1460 return ERROR_INVALID_PARAMETER
;
1462 if ((err
= validate_scm_handle(hmngr
, SC_MANAGER_ENUMERATE_SERVICE
, &manager
)) != ERROR_SUCCESS
)
1466 WINE_FIXME("resume index not supported\n");
1468 scmdatabase_lock(manager
->db
);
1470 total_size
= num_services
= 0;
1471 LIST_FOR_EACH_ENTRY(service
, &manager
->db
->services
, struct service_entry
, entry
)
1473 if ((service
->status
.dwServiceType
& type
) && map_state(service
->status
.dwCurrentState
, state
))
1475 total_size
+= sizeof(*s
);
1476 total_size
+= (lstrlenW(service
->name
) + 1) * sizeof(WCHAR
);
1477 if (service
->config
.lpDisplayName
)
1479 total_size
+= (lstrlenW(service
->config
.lpDisplayName
) + 1) * sizeof(WCHAR
);
1485 *needed
= total_size
;
1486 if (total_size
> size
)
1488 scmdatabase_unlock(manager
->db
);
1489 return ERROR_MORE_DATA
;
1491 s
= (struct enum_service_status
*)buffer
;
1492 offset
= num_services
* sizeof(struct enum_service_status
);
1493 LIST_FOR_EACH_ENTRY(service
, &manager
->db
->services
, struct service_entry
, entry
)
1495 if ((service
->status
.dwServiceType
& type
) && map_state(service
->status
.dwCurrentState
, state
))
1497 sz
= (lstrlenW(service
->name
) + 1) * sizeof(WCHAR
);
1498 memcpy(buffer
+ offset
, service
->name
, sz
);
1499 s
->service_name
= offset
;
1502 if (!service
->config
.lpDisplayName
) s
->display_name
= 0;
1505 sz
= (lstrlenW(service
->config
.lpDisplayName
) + 1) * sizeof(WCHAR
);
1506 memcpy(buffer
+ offset
, service
->config
.lpDisplayName
, sz
);
1507 s
->display_name
= offset
;
1510 s
->service_status
= service
->status
;
1514 *returned
= num_services
;
1516 scmdatabase_unlock(manager
->db
);
1517 return ERROR_SUCCESS
;
1520 static struct service_entry
*find_service_by_group(struct scmdatabase
*db
, const WCHAR
*group
)
1522 struct service_entry
*service
;
1523 LIST_FOR_EACH_ENTRY(service
, &db
->services
, struct service_entry
, entry
)
1525 if (service
->config
.lpLoadOrderGroup
&& !wcsicmp(group
, service
->config
.lpLoadOrderGroup
))
1531 static BOOL
match_group(const WCHAR
*g1
, const WCHAR
*g2
)
1533 if (!g2
) return TRUE
;
1534 if (!g2
[0] && (!g1
|| !g1
[0])) return TRUE
;
1535 if (g1
&& !lstrcmpW(g1
, g2
)) return TRUE
;
1539 DWORD __cdecl
svcctl_EnumServicesStatusExA(
1540 SC_RPC_HANDLE scmanager
,
1541 SC_ENUM_TYPE info_level
,
1543 DWORD service_state
,
1547 DWORD
*services_count
,
1548 DWORD
*resume_index
,
1552 return ERROR_CALL_NOT_IMPLEMENTED
;
1555 DWORD __cdecl
svcctl_EnumServicesStatusExW(
1556 SC_RPC_HANDLE hmngr
,
1557 SC_ENUM_TYPE info_level
,
1564 DWORD
*resume_handle
,
1567 DWORD err
, sz
, total_size
, num_services
;
1569 struct sc_manager_handle
*manager
;
1570 struct service_entry
*service
;
1571 struct enum_service_status_process
*s
;
1573 WINE_TRACE("(%p, 0x%x, 0x%x, %p, %u, %p, %p, %s)\n", hmngr
, type
, state
, buffer
, size
,
1574 needed
, returned
, wine_dbgstr_w(group
));
1577 FIXME("resume handle not supported\n");
1579 if (!type
|| !state
)
1580 return ERROR_INVALID_PARAMETER
;
1582 if ((err
= validate_scm_handle(hmngr
, SC_MANAGER_ENUMERATE_SERVICE
, &manager
)) != ERROR_SUCCESS
)
1585 scmdatabase_lock(manager
->db
);
1587 if (group
&& !find_service_by_group(manager
->db
, group
))
1589 scmdatabase_unlock(manager
->db
);
1590 return ERROR_SERVICE_DOES_NOT_EXIST
;
1593 total_size
= num_services
= 0;
1594 LIST_FOR_EACH_ENTRY(service
, &manager
->db
->services
, struct service_entry
, entry
)
1596 if ((service
->status
.dwServiceType
& type
) && map_state(service
->status
.dwCurrentState
, state
)
1597 && match_group(service
->config
.lpLoadOrderGroup
, group
))
1599 total_size
+= sizeof(*s
);
1600 total_size
+= (lstrlenW(service
->name
) + 1) * sizeof(WCHAR
);
1601 if (service
->config
.lpDisplayName
)
1603 total_size
+= (lstrlenW(service
->config
.lpDisplayName
) + 1) * sizeof(WCHAR
);
1609 *needed
= total_size
;
1610 if (total_size
> size
)
1612 scmdatabase_unlock(manager
->db
);
1613 return ERROR_MORE_DATA
;
1615 s
= (struct enum_service_status_process
*)buffer
;
1616 offset
= num_services
* sizeof(*s
);
1617 LIST_FOR_EACH_ENTRY(service
, &manager
->db
->services
, struct service_entry
, entry
)
1619 if ((service
->status
.dwServiceType
& type
) && map_state(service
->status
.dwCurrentState
, state
)
1620 && match_group(service
->config
.lpLoadOrderGroup
, group
))
1622 sz
= (lstrlenW(service
->name
) + 1) * sizeof(WCHAR
);
1623 memcpy(buffer
+ offset
, service
->name
, sz
);
1624 s
->service_name
= offset
;
1627 if (!service
->config
.lpDisplayName
) s
->display_name
= 0;
1630 sz
= (lstrlenW(service
->config
.lpDisplayName
) + 1) * sizeof(WCHAR
);
1631 memcpy(buffer
+ offset
, service
->config
.lpDisplayName
, sz
);
1632 s
->display_name
= offset
;
1635 fill_status_process(&s
->service_status_process
, service
);
1639 *returned
= num_services
;
1641 scmdatabase_unlock(manager
->db
);
1642 return ERROR_SUCCESS
;
1645 DWORD __cdecl
svcctl_unknown43(void)
1648 return ERROR_CALL_NOT_IMPLEMENTED
;
1651 DWORD __cdecl
svcctl_CreateServiceWOW64A(
1652 SC_RPC_HANDLE scmanager
,
1658 DWORD error_control
,
1660 LPCSTR loadordergroup
,
1662 const BYTE
*dependencies
,
1665 const BYTE
*password
,
1666 DWORD password_size
,
1667 SC_RPC_HANDLE
*service
)
1670 return ERROR_CALL_NOT_IMPLEMENTED
;
1673 DWORD __cdecl
svcctl_CreateServiceWOW64W(
1674 SC_RPC_HANDLE scmanager
,
1675 LPCWSTR servicename
,
1676 LPCWSTR displayname
,
1680 DWORD error_control
,
1682 LPCWSTR loadordergroup
,
1684 const BYTE
*dependencies
,
1687 const BYTE
*password
,
1688 DWORD password_size
,
1689 SC_RPC_HANDLE
*service
)
1691 WINE_TRACE("(%s, %s, 0x%x, %s)\n", wine_dbgstr_w(servicename
), wine_dbgstr_w(displayname
), accessmask
, wine_dbgstr_w(imagepath
));
1692 return create_serviceW(scmanager
, servicename
, displayname
, accessmask
, service_type
, start_type
, error_control
, imagepath
,
1693 loadordergroup
, tagid
, dependencies
, depend_size
, start_name
, password
, password_size
, service
, TRUE
);
1696 DWORD __cdecl
svcctl_unknown46(void)
1699 return ERROR_CALL_NOT_IMPLEMENTED
;
1702 DWORD __cdecl
svcctl_NotifyServiceStatusChange(
1703 SC_RPC_HANDLE handle
,
1704 SC_RPC_NOTIFY_PARAMS params
,
1705 GUID
*clientprocessguid
,
1706 GUID
*scmprocessguid
,
1707 BOOL
*createremotequeue
,
1708 SC_NOTIFY_RPC_HANDLE
*hNotify
)
1711 struct sc_manager_handle
*manager
= NULL
;
1712 struct sc_service_handle
*service
= NULL
;
1713 struct sc_notify_handle
*notify
;
1714 struct sc_handle
*hdr
= handle
;
1716 WINE_TRACE("(%p, NotifyMask: 0x%x, %p, %p, %p, %p)\n", handle
,
1717 params
.u
.params
->dwNotifyMask
, clientprocessguid
, scmprocessguid
,
1718 createremotequeue
, hNotify
);
1722 case SC_HTYPE_SERVICE
:
1723 err
= validate_service_handle(handle
, SERVICE_QUERY_STATUS
, &service
);
1725 case SC_HTYPE_MANAGER
:
1726 err
= validate_scm_handle(handle
, SC_MANAGER_ENUMERATE_SERVICE
, &manager
);
1729 err
= ERROR_INVALID_HANDLE
;
1733 if (err
!= ERROR_SUCCESS
)
1738 WARN("Need support for service creation/deletion notifications\n");
1739 return ERROR_CALL_NOT_IMPLEMENTED
;
1742 notify
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*notify
));
1744 return ERROR_NOT_ENOUGH_SERVER_MEMORY
;
1746 notify
->hdr
.type
= SC_HTYPE_NOTIFY
;
1747 notify
->hdr
.access
= 0;
1749 notify
->event
= CreateEventW(NULL
, TRUE
, FALSE
, NULL
);
1751 notify
->notify_mask
= params
.u
.params
->dwNotifyMask
;
1753 service_lock(service
->service_entry
);
1755 if (service
->notify
)
1757 service_unlock(service
->service_entry
);
1758 sc_notify_release(notify
);
1759 return ERROR_ALREADY_REGISTERED
;
1762 mask
= 1 << (service
->service_entry
->status
.dwCurrentState
- SERVICE_STOPPED
);
1763 if (!service
->status_notified
&& (notify
->notify_mask
& mask
))
1765 fill_notify(notify
, service
->service_entry
);
1766 service
->status_notified
= TRUE
;
1770 sc_notify_retain(notify
);
1771 service
->notify
= notify
;
1774 sc_notify_retain(notify
);
1775 *hNotify
= ¬ify
->hdr
;
1777 service_unlock(service
->service_entry
);
1779 return ERROR_SUCCESS
;
1782 DWORD __cdecl
svcctl_GetNotifyResults(
1783 SC_NOTIFY_RPC_HANDLE hNotify
,
1784 SC_RPC_NOTIFY_PARAMS_LIST
**pList
)
1787 struct sc_notify_handle
*notify
;
1789 WINE_TRACE("(%p, %p)\n", hNotify
, pList
);
1792 return ERROR_INVALID_PARAMETER
;
1796 if ((err
= validate_notify_handle(hNotify
, 0, ¬ify
)) != 0)
1799 sc_notify_retain(notify
);
1800 /* block until there is a result */
1801 err
= WaitForSingleObject(notify
->event
, INFINITE
);
1803 if (err
!= WAIT_OBJECT_0
)
1805 sc_notify_release(notify
);
1809 *pList
= InterlockedExchangePointer((void**)¬ify
->params_list
, NULL
);
1812 sc_notify_release(notify
);
1813 return ERROR_REQUEST_ABORTED
;
1816 sc_notify_release(notify
);
1818 return ERROR_SUCCESS
;
1821 DWORD __cdecl
svcctl_CloseNotifyHandle(
1822 SC_NOTIFY_RPC_HANDLE
*hNotify
,
1825 struct sc_notify_handle
*notify
;
1828 WINE_TRACE("(%p, %p)\n", hNotify
, apc_fired
);
1830 if ((err
= validate_notify_handle(*hNotify
, 0, ¬ify
)) != 0)
1833 sc_notify_release(notify
);
1835 return ERROR_SUCCESS
;
1838 DWORD __cdecl
svcctl_ControlServiceExA(
1839 SC_RPC_HANDLE service
,
1842 SC_RPC_SERVICE_CONTROL_IN_PARAMSA
*in_params
,
1843 SC_RPC_SERVICE_CONTROL_OUT_PARAMSA
*out_params
)
1846 return ERROR_CALL_NOT_IMPLEMENTED
;
1849 DWORD __cdecl
svcctl_ControlServiceExW(
1850 SC_RPC_HANDLE service
,
1853 SC_RPC_SERVICE_CONTROL_IN_PARAMSW
*in_params
,
1854 SC_RPC_SERVICE_CONTROL_OUT_PARAMSW
*out_params
)
1857 return ERROR_CALL_NOT_IMPLEMENTED
;
1860 DWORD __cdecl
svcctl_unknown52(void)
1863 return ERROR_CALL_NOT_IMPLEMENTED
;
1866 DWORD __cdecl
svcctl_unknown53(void)
1869 return ERROR_CALL_NOT_IMPLEMENTED
;
1872 DWORD __cdecl
svcctl_unknown54(void)
1875 return ERROR_CALL_NOT_IMPLEMENTED
;
1878 DWORD __cdecl
svcctl_unknown55(void)
1881 return ERROR_CALL_NOT_IMPLEMENTED
;
1884 DWORD __cdecl
svcctl_QueryServiceConfigEx(
1885 SC_RPC_HANDLE service
,
1887 SC_RPC_CONFIG_INFOW
*info
)
1890 return ERROR_CALL_NOT_IMPLEMENTED
;
1893 DWORD __cdecl
svcctl_QueryServiceObjectSecurity(
1894 SC_RPC_HANDLE service
,
1895 SECURITY_INFORMATION info
,
1901 return ERROR_CALL_NOT_IMPLEMENTED
;
1904 DWORD __cdecl
svcctl_SetServiceObjectSecurity(
1905 SC_RPC_HANDLE service
,
1906 SECURITY_INFORMATION info
,
1911 return ERROR_CALL_NOT_IMPLEMENTED
;
1914 DWORD __cdecl
svcctl_QueryServiceStatus(
1915 SC_RPC_HANDLE service
,
1916 SERVICE_STATUS
*status
)
1919 return ERROR_CALL_NOT_IMPLEMENTED
;
1922 DWORD __cdecl
svcctl_NotifyBootConfigStatus(
1923 SVCCTL_HANDLEW machinename
,
1924 DWORD boot_acceptable
)
1927 return ERROR_CALL_NOT_IMPLEMENTED
;
1930 DWORD __cdecl
svcctl_SCSetServiceBitsW(void)
1933 return ERROR_CALL_NOT_IMPLEMENTED
;
1936 DWORD __cdecl
svcctl_EnumDependentServicesW(
1937 SC_RPC_HANDLE service
,
1942 DWORD
*services_ret
)
1945 return ERROR_CALL_NOT_IMPLEMENTED
;
1948 DWORD __cdecl
svcctl_QueryServiceLockStatusW(
1949 SC_RPC_HANDLE scmanager
,
1950 QUERY_SERVICE_LOCK_STATUSW
*status
,
1955 return ERROR_CALL_NOT_IMPLEMENTED
;
1958 DWORD __cdecl
svcctl_SCSetServiceBitsA(void)
1961 return ERROR_CALL_NOT_IMPLEMENTED
;
1964 DWORD __cdecl
svcctl_ChangeServiceConfigA(
1965 SC_RPC_HANDLE service
,
1968 DWORD error_control
,
1970 LPSTR loadordergroup
,
1976 DWORD password_size
,
1980 return ERROR_CALL_NOT_IMPLEMENTED
;
1983 DWORD __cdecl
svcctl_CreateServiceA(
1984 SC_RPC_HANDLE scmanager
,
1987 DWORD desiredaccess
,
1990 DWORD error_control
,
1992 LPCSTR loadordergroup
,
1994 const BYTE
*dependencies
,
1997 const BYTE
*password
,
1998 DWORD password_size
,
1999 SC_RPC_HANDLE
*service
)
2002 return ERROR_CALL_NOT_IMPLEMENTED
;
2005 DWORD __cdecl
svcctl_EnumDependentServicesA(
2006 SC_RPC_HANDLE service
,
2011 DWORD
*services_ret
)
2014 return ERROR_CALL_NOT_IMPLEMENTED
;
2017 DWORD __cdecl
svcctl_EnumServicesStatusA(
2018 SC_RPC_HANDLE hmngr
,
2028 return ERROR_CALL_NOT_IMPLEMENTED
;
2031 DWORD __cdecl
svcctl_OpenSCManagerA(
2032 MACHINE_HANDLEA MachineName
,
2033 LPCSTR DatabaseName
,
2035 SC_RPC_HANDLE
*handle
)
2038 return ERROR_CALL_NOT_IMPLEMENTED
;
2041 DWORD __cdecl
svcctl_OpenServiceA(
2042 SC_RPC_HANDLE hSCManager
,
2043 LPCSTR lpServiceName
,
2044 DWORD dwDesiredAccess
,
2045 SC_RPC_HANDLE
*phService
)
2048 return ERROR_CALL_NOT_IMPLEMENTED
;
2051 DWORD __cdecl
svcctl_QueryServiceConfigA(
2052 SC_RPC_HANDLE hService
,
2053 QUERY_SERVICE_CONFIGA
*config
,
2058 return ERROR_CALL_NOT_IMPLEMENTED
;
2061 DWORD __cdecl
svcctl_QueryServiceLockStatusA(
2062 SC_RPC_HANDLE scmanager
,
2063 QUERY_SERVICE_LOCK_STATUSA
*status
,
2068 return ERROR_CALL_NOT_IMPLEMENTED
;
2071 DWORD __cdecl
svcctl_StartServiceA(
2072 SC_RPC_HANDLE service
,
2077 return ERROR_CALL_NOT_IMPLEMENTED
;
2080 DWORD __cdecl
svcctl_GetServiceDisplayNameA(
2081 SC_RPC_HANDLE hSCManager
,
2087 return ERROR_CALL_NOT_IMPLEMENTED
;
2090 DWORD __cdecl
svcctl_GetServiceKeyNameA(
2091 SC_RPC_HANDLE hSCManager
,
2097 return ERROR_CALL_NOT_IMPLEMENTED
;
2100 DWORD __cdecl
svcctl_GetCurrentGroupStateW(void)
2103 return ERROR_CALL_NOT_IMPLEMENTED
;
2106 DWORD __cdecl
svcctl_EnumServiceGroupW(
2107 SC_RPC_HANDLE scmanager
,
2109 DWORD service_state
,
2113 DWORD
*returned_size
,
2114 DWORD
*resume_index
,
2118 return ERROR_CALL_NOT_IMPLEMENTED
;
2121 DWORD __cdecl
svcctl_ChangeServiceConfig2A(
2122 SC_RPC_HANDLE service
,
2123 SC_RPC_CONFIG_INFOA info
)
2126 return ERROR_CALL_NOT_IMPLEMENTED
;
2129 DWORD __cdecl
svcctl_QueryServiceConfig2A(
2130 SC_RPC_HANDLE service
,
2137 return ERROR_CALL_NOT_IMPLEMENTED
;
2140 DWORD
RPC_Init(void)
2142 WCHAR transport
[] = SVCCTL_TRANSPORT
;
2143 WCHAR endpoint
[] = SVCCTL_ENDPOINT
;
2146 if (!(cleanup_group
= CreateThreadpoolCleanupGroup()))
2148 WINE_ERR("CreateThreadpoolCleanupGroup failed with error %u\n", GetLastError());
2149 return GetLastError();
2152 if ((err
= RpcServerUseProtseqEpW(transport
, 0, endpoint
, NULL
)) != ERROR_SUCCESS
)
2154 WINE_ERR("RpcServerUseProtseq failed with error %u\n", err
);
2158 if ((err
= RpcServerRegisterIf(svcctl_v2_0_s_ifspec
, 0, 0)) != ERROR_SUCCESS
)
2160 WINE_ERR("RpcServerRegisterIf failed with error %u\n", err
);
2164 if ((err
= RpcServerListen(1, RPC_C_LISTEN_MAX_CALLS_DEFAULT
, TRUE
)) != ERROR_SUCCESS
)
2166 WINE_ERR("RpcServerListen failed with error %u\n", err
);
2170 exit_event
= __wine_make_process_system();
2171 return ERROR_SUCCESS
;
2176 RpcMgmtStopServerListening(NULL
);
2177 RpcServerUnregisterIf(svcctl_v2_0_s_ifspec
, NULL
, TRUE
);
2178 RpcMgmtWaitServerListen();
2180 CloseThreadpoolCleanupGroupMembers(cleanup_group
, TRUE
, NULL
);
2181 CloseThreadpoolCleanupGroup(cleanup_group
);
2182 CloseHandle(exit_event
);
2185 void __RPC_USER
SC_RPC_HANDLE_rundown(SC_RPC_HANDLE handle
)
2187 SC_RPC_HANDLE_destroy(handle
);
2190 void __RPC_USER
SC_NOTIFY_RPC_HANDLE_rundown(SC_NOTIFY_RPC_HANDLE handle
)
2194 void __RPC_FAR
* __RPC_USER
MIDL_user_allocate(SIZE_T len
)
2196 return HeapAlloc(GetProcessHeap(), 0, len
);
2199 void __RPC_USER
MIDL_user_free(void __RPC_FAR
* ptr
)
2201 HeapFree(GetProcessHeap(), 0, ptr
);