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 void __RPC_USER
SC_RPC_LOCK_rundown(SC_RPC_LOCK hLock
)
1372 DWORD __cdecl
svcctl_LockServiceDatabase(SC_RPC_HANDLE manager
, SC_RPC_LOCK
*lock
)
1374 TRACE("(%p, %p)\n", manager
, lock
);
1376 *lock
= (SC_RPC_LOCK
)0xdeadbeef;
1377 return ERROR_SUCCESS
;
1380 DWORD __cdecl
svcctl_UnlockServiceDatabase(SC_RPC_LOCK
*lock
)
1382 TRACE("(&%p)\n", *lock
);
1385 return ERROR_SUCCESS
;
1388 static BOOL
map_state(DWORD state
, DWORD mask
)
1392 case SERVICE_START_PENDING
:
1393 case SERVICE_STOP_PENDING
:
1394 case SERVICE_RUNNING
:
1395 case SERVICE_CONTINUE_PENDING
:
1396 case SERVICE_PAUSE_PENDING
:
1397 case SERVICE_PAUSED
:
1398 if (SERVICE_ACTIVE
& mask
) return TRUE
;
1400 case SERVICE_STOPPED
:
1401 if (SERVICE_INACTIVE
& mask
) return TRUE
;
1404 WINE_ERR("unknown state %u\n", state
);
1410 DWORD __cdecl
svcctl_EnumServicesStatusW(
1411 SC_RPC_HANDLE hmngr
,
1420 DWORD err
, sz
, total_size
, num_services
, offset
;
1421 struct sc_manager_handle
*manager
;
1422 struct service_entry
*service
;
1423 struct enum_service_status
*s
;
1425 WINE_TRACE("(%p, 0x%x, 0x%x, %p, %u, %p, %p, %p)\n", hmngr
, type
, state
, buffer
, size
, needed
, returned
, resume
);
1427 if (!type
|| !state
)
1428 return ERROR_INVALID_PARAMETER
;
1430 if ((err
= validate_scm_handle(hmngr
, SC_MANAGER_ENUMERATE_SERVICE
, &manager
)) != ERROR_SUCCESS
)
1434 WINE_FIXME("resume index not supported\n");
1436 scmdatabase_lock(manager
->db
);
1438 total_size
= num_services
= 0;
1439 LIST_FOR_EACH_ENTRY(service
, &manager
->db
->services
, struct service_entry
, entry
)
1441 if ((service
->status
.dwServiceType
& type
) && map_state(service
->status
.dwCurrentState
, state
))
1443 total_size
+= sizeof(*s
);
1444 total_size
+= (lstrlenW(service
->name
) + 1) * sizeof(WCHAR
);
1445 if (service
->config
.lpDisplayName
)
1447 total_size
+= (lstrlenW(service
->config
.lpDisplayName
) + 1) * sizeof(WCHAR
);
1453 *needed
= total_size
;
1454 if (total_size
> size
)
1456 scmdatabase_unlock(manager
->db
);
1457 return ERROR_MORE_DATA
;
1459 s
= (struct enum_service_status
*)buffer
;
1460 offset
= num_services
* sizeof(struct enum_service_status
);
1461 LIST_FOR_EACH_ENTRY(service
, &manager
->db
->services
, struct service_entry
, entry
)
1463 if ((service
->status
.dwServiceType
& type
) && map_state(service
->status
.dwCurrentState
, state
))
1465 sz
= (lstrlenW(service
->name
) + 1) * sizeof(WCHAR
);
1466 memcpy(buffer
+ offset
, service
->name
, sz
);
1467 s
->service_name
= offset
;
1470 if (!service
->config
.lpDisplayName
) s
->display_name
= 0;
1473 sz
= (lstrlenW(service
->config
.lpDisplayName
) + 1) * sizeof(WCHAR
);
1474 memcpy(buffer
+ offset
, service
->config
.lpDisplayName
, sz
);
1475 s
->display_name
= offset
;
1478 s
->service_status
= service
->status
;
1482 *returned
= num_services
;
1484 scmdatabase_unlock(manager
->db
);
1485 return ERROR_SUCCESS
;
1488 static struct service_entry
*find_service_by_group(struct scmdatabase
*db
, const WCHAR
*group
)
1490 struct service_entry
*service
;
1491 LIST_FOR_EACH_ENTRY(service
, &db
->services
, struct service_entry
, entry
)
1493 if (service
->config
.lpLoadOrderGroup
&& !wcsicmp(group
, service
->config
.lpLoadOrderGroup
))
1499 static BOOL
match_group(const WCHAR
*g1
, const WCHAR
*g2
)
1501 if (!g2
) return TRUE
;
1502 if (!g2
[0] && (!g1
|| !g1
[0])) return TRUE
;
1503 if (g1
&& !lstrcmpW(g1
, g2
)) return TRUE
;
1507 DWORD __cdecl
svcctl_EnumServicesStatusExA(
1508 SC_RPC_HANDLE scmanager
,
1509 SC_ENUM_TYPE info_level
,
1511 DWORD service_state
,
1515 DWORD
*services_count
,
1516 DWORD
*resume_index
,
1520 return ERROR_CALL_NOT_IMPLEMENTED
;
1523 DWORD __cdecl
svcctl_EnumServicesStatusExW(
1524 SC_RPC_HANDLE hmngr
,
1525 SC_ENUM_TYPE info_level
,
1532 DWORD
*resume_handle
,
1535 DWORD err
, sz
, total_size
, num_services
;
1537 struct sc_manager_handle
*manager
;
1538 struct service_entry
*service
;
1539 struct enum_service_status_process
*s
;
1541 WINE_TRACE("(%p, 0x%x, 0x%x, %p, %u, %p, %p, %s)\n", hmngr
, type
, state
, buffer
, size
,
1542 needed
, returned
, wine_dbgstr_w(group
));
1545 FIXME("resume handle not supported\n");
1547 if (!type
|| !state
)
1548 return ERROR_INVALID_PARAMETER
;
1550 if ((err
= validate_scm_handle(hmngr
, SC_MANAGER_ENUMERATE_SERVICE
, &manager
)) != ERROR_SUCCESS
)
1553 scmdatabase_lock(manager
->db
);
1555 if (group
&& !find_service_by_group(manager
->db
, group
))
1557 scmdatabase_unlock(manager
->db
);
1558 return ERROR_SERVICE_DOES_NOT_EXIST
;
1561 total_size
= num_services
= 0;
1562 LIST_FOR_EACH_ENTRY(service
, &manager
->db
->services
, struct service_entry
, entry
)
1564 if ((service
->status
.dwServiceType
& type
) && map_state(service
->status
.dwCurrentState
, state
)
1565 && match_group(service
->config
.lpLoadOrderGroup
, group
))
1567 total_size
+= sizeof(*s
);
1568 total_size
+= (lstrlenW(service
->name
) + 1) * sizeof(WCHAR
);
1569 if (service
->config
.lpDisplayName
)
1571 total_size
+= (lstrlenW(service
->config
.lpDisplayName
) + 1) * sizeof(WCHAR
);
1577 *needed
= total_size
;
1578 if (total_size
> size
)
1580 scmdatabase_unlock(manager
->db
);
1581 return ERROR_MORE_DATA
;
1583 s
= (struct enum_service_status_process
*)buffer
;
1584 offset
= num_services
* sizeof(*s
);
1585 LIST_FOR_EACH_ENTRY(service
, &manager
->db
->services
, struct service_entry
, entry
)
1587 if ((service
->status
.dwServiceType
& type
) && map_state(service
->status
.dwCurrentState
, state
)
1588 && match_group(service
->config
.lpLoadOrderGroup
, group
))
1590 sz
= (lstrlenW(service
->name
) + 1) * sizeof(WCHAR
);
1591 memcpy(buffer
+ offset
, service
->name
, sz
);
1592 s
->service_name
= offset
;
1595 if (!service
->config
.lpDisplayName
) s
->display_name
= 0;
1598 sz
= (lstrlenW(service
->config
.lpDisplayName
) + 1) * sizeof(WCHAR
);
1599 memcpy(buffer
+ offset
, service
->config
.lpDisplayName
, sz
);
1600 s
->display_name
= offset
;
1603 fill_status_process(&s
->service_status_process
, service
);
1607 *returned
= num_services
;
1609 scmdatabase_unlock(manager
->db
);
1610 return ERROR_SUCCESS
;
1613 DWORD __cdecl
svcctl_unknown43(void)
1616 return ERROR_CALL_NOT_IMPLEMENTED
;
1619 DWORD __cdecl
svcctl_CreateServiceWOW64A(
1620 SC_RPC_HANDLE scmanager
,
1626 DWORD error_control
,
1628 LPCSTR loadordergroup
,
1630 const BYTE
*dependencies
,
1633 const BYTE
*password
,
1634 DWORD password_size
,
1635 SC_RPC_HANDLE
*service
)
1638 return ERROR_CALL_NOT_IMPLEMENTED
;
1641 DWORD __cdecl
svcctl_CreateServiceWOW64W(
1642 SC_RPC_HANDLE scmanager
,
1643 LPCWSTR servicename
,
1644 LPCWSTR displayname
,
1648 DWORD error_control
,
1650 LPCWSTR loadordergroup
,
1652 const BYTE
*dependencies
,
1655 const BYTE
*password
,
1656 DWORD password_size
,
1657 SC_RPC_HANDLE
*service
)
1659 WINE_TRACE("(%s, %s, 0x%x, %s)\n", wine_dbgstr_w(servicename
), wine_dbgstr_w(displayname
), accessmask
, wine_dbgstr_w(imagepath
));
1660 return create_serviceW(scmanager
, servicename
, displayname
, accessmask
, service_type
, start_type
, error_control
, imagepath
,
1661 loadordergroup
, tagid
, dependencies
, depend_size
, start_name
, password
, password_size
, service
, TRUE
);
1664 DWORD __cdecl
svcctl_unknown46(void)
1667 return ERROR_CALL_NOT_IMPLEMENTED
;
1670 DWORD __cdecl
svcctl_NotifyServiceStatusChange(
1671 SC_RPC_HANDLE handle
,
1672 SC_RPC_NOTIFY_PARAMS params
,
1673 GUID
*clientprocessguid
,
1674 GUID
*scmprocessguid
,
1675 BOOL
*createremotequeue
,
1676 SC_NOTIFY_RPC_HANDLE
*hNotify
)
1679 struct sc_manager_handle
*manager
= NULL
;
1680 struct sc_service_handle
*service
= NULL
;
1681 struct sc_notify_handle
*notify
;
1682 struct sc_handle
*hdr
= handle
;
1684 WINE_TRACE("(%p, NotifyMask: 0x%x, %p, %p, %p, %p)\n", handle
,
1685 params
.u
.params
->dwNotifyMask
, clientprocessguid
, scmprocessguid
,
1686 createremotequeue
, hNotify
);
1690 case SC_HTYPE_SERVICE
:
1691 err
= validate_service_handle(handle
, SERVICE_QUERY_STATUS
, &service
);
1693 case SC_HTYPE_MANAGER
:
1694 err
= validate_scm_handle(handle
, SC_MANAGER_ENUMERATE_SERVICE
, &manager
);
1697 err
= ERROR_INVALID_HANDLE
;
1701 if (err
!= ERROR_SUCCESS
)
1706 WARN("Need support for service creation/deletion notifications\n");
1707 return ERROR_CALL_NOT_IMPLEMENTED
;
1710 notify
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*notify
));
1712 return ERROR_NOT_ENOUGH_SERVER_MEMORY
;
1714 notify
->hdr
.type
= SC_HTYPE_NOTIFY
;
1715 notify
->hdr
.access
= 0;
1717 notify
->event
= CreateEventW(NULL
, TRUE
, FALSE
, NULL
);
1719 notify
->notify_mask
= params
.u
.params
->dwNotifyMask
;
1721 service_lock(service
->service_entry
);
1723 if (service
->notify
)
1725 service_unlock(service
->service_entry
);
1726 sc_notify_release(notify
);
1727 return ERROR_ALREADY_REGISTERED
;
1730 mask
= 1 << (service
->service_entry
->status
.dwCurrentState
- SERVICE_STOPPED
);
1731 if (!service
->status_notified
&& (notify
->notify_mask
& mask
))
1733 fill_notify(notify
, service
->service_entry
);
1734 service
->status_notified
= TRUE
;
1738 sc_notify_retain(notify
);
1739 service
->notify
= notify
;
1742 sc_notify_retain(notify
);
1743 *hNotify
= ¬ify
->hdr
;
1745 service_unlock(service
->service_entry
);
1747 return ERROR_SUCCESS
;
1750 DWORD __cdecl
svcctl_GetNotifyResults(
1751 SC_NOTIFY_RPC_HANDLE hNotify
,
1752 SC_RPC_NOTIFY_PARAMS_LIST
**pList
)
1755 struct sc_notify_handle
*notify
;
1757 WINE_TRACE("(%p, %p)\n", hNotify
, pList
);
1760 return ERROR_INVALID_PARAMETER
;
1764 if ((err
= validate_notify_handle(hNotify
, 0, ¬ify
)) != 0)
1767 sc_notify_retain(notify
);
1768 /* block until there is a result */
1769 err
= WaitForSingleObject(notify
->event
, INFINITE
);
1771 if (err
!= WAIT_OBJECT_0
)
1773 sc_notify_release(notify
);
1777 *pList
= InterlockedExchangePointer((void**)¬ify
->params_list
, NULL
);
1780 sc_notify_release(notify
);
1781 return ERROR_REQUEST_ABORTED
;
1784 sc_notify_release(notify
);
1786 return ERROR_SUCCESS
;
1789 DWORD __cdecl
svcctl_CloseNotifyHandle(
1790 SC_NOTIFY_RPC_HANDLE
*hNotify
,
1793 struct sc_notify_handle
*notify
;
1796 WINE_TRACE("(%p, %p)\n", hNotify
, apc_fired
);
1798 if ((err
= validate_notify_handle(*hNotify
, 0, ¬ify
)) != 0)
1801 sc_notify_release(notify
);
1803 return ERROR_SUCCESS
;
1806 DWORD __cdecl
svcctl_ControlServiceExA(
1807 SC_RPC_HANDLE service
,
1810 SC_RPC_SERVICE_CONTROL_IN_PARAMSA
*in_params
,
1811 SC_RPC_SERVICE_CONTROL_OUT_PARAMSA
*out_params
)
1814 return ERROR_CALL_NOT_IMPLEMENTED
;
1817 DWORD __cdecl
svcctl_ControlServiceExW(
1818 SC_RPC_HANDLE service
,
1821 SC_RPC_SERVICE_CONTROL_IN_PARAMSW
*in_params
,
1822 SC_RPC_SERVICE_CONTROL_OUT_PARAMSW
*out_params
)
1825 return ERROR_CALL_NOT_IMPLEMENTED
;
1828 DWORD __cdecl
svcctl_unknown52(void)
1831 return ERROR_CALL_NOT_IMPLEMENTED
;
1834 DWORD __cdecl
svcctl_unknown53(void)
1837 return ERROR_CALL_NOT_IMPLEMENTED
;
1840 DWORD __cdecl
svcctl_unknown54(void)
1843 return ERROR_CALL_NOT_IMPLEMENTED
;
1846 DWORD __cdecl
svcctl_unknown55(void)
1849 return ERROR_CALL_NOT_IMPLEMENTED
;
1852 DWORD __cdecl
svcctl_QueryServiceConfigEx(
1853 SC_RPC_HANDLE service
,
1855 SC_RPC_CONFIG_INFOW
*info
)
1858 return ERROR_CALL_NOT_IMPLEMENTED
;
1861 DWORD __cdecl
svcctl_QueryServiceObjectSecurity(
1862 SC_RPC_HANDLE service
,
1863 SECURITY_INFORMATION info
,
1869 return ERROR_CALL_NOT_IMPLEMENTED
;
1872 DWORD __cdecl
svcctl_SetServiceObjectSecurity(
1873 SC_RPC_HANDLE service
,
1874 SECURITY_INFORMATION info
,
1879 return ERROR_CALL_NOT_IMPLEMENTED
;
1882 DWORD __cdecl
svcctl_QueryServiceStatus(
1883 SC_RPC_HANDLE service
,
1884 SERVICE_STATUS
*status
)
1887 return ERROR_CALL_NOT_IMPLEMENTED
;
1890 DWORD __cdecl
svcctl_NotifyBootConfigStatus(
1891 SVCCTL_HANDLEW machinename
,
1892 DWORD boot_acceptable
)
1895 return ERROR_CALL_NOT_IMPLEMENTED
;
1898 DWORD __cdecl
svcctl_SCSetServiceBitsW(void)
1901 return ERROR_CALL_NOT_IMPLEMENTED
;
1904 DWORD __cdecl
svcctl_EnumDependentServicesW(
1905 SC_RPC_HANDLE service
,
1910 DWORD
*services_ret
)
1913 return ERROR_CALL_NOT_IMPLEMENTED
;
1916 DWORD __cdecl
svcctl_QueryServiceLockStatusW(
1917 SC_RPC_HANDLE scmanager
,
1918 QUERY_SERVICE_LOCK_STATUSW
*status
,
1923 return ERROR_CALL_NOT_IMPLEMENTED
;
1926 DWORD __cdecl
svcctl_SCSetServiceBitsA(void)
1929 return ERROR_CALL_NOT_IMPLEMENTED
;
1932 DWORD __cdecl
svcctl_ChangeServiceConfigA(
1933 SC_RPC_HANDLE service
,
1936 DWORD error_control
,
1938 LPSTR loadordergroup
,
1944 DWORD password_size
,
1948 return ERROR_CALL_NOT_IMPLEMENTED
;
1951 DWORD __cdecl
svcctl_CreateServiceA(
1952 SC_RPC_HANDLE scmanager
,
1955 DWORD desiredaccess
,
1958 DWORD error_control
,
1960 LPCSTR loadordergroup
,
1962 const BYTE
*dependencies
,
1965 const BYTE
*password
,
1966 DWORD password_size
,
1967 SC_RPC_HANDLE
*service
)
1970 return ERROR_CALL_NOT_IMPLEMENTED
;
1973 DWORD __cdecl
svcctl_EnumDependentServicesA(
1974 SC_RPC_HANDLE service
,
1979 DWORD
*services_ret
)
1982 return ERROR_CALL_NOT_IMPLEMENTED
;
1985 DWORD __cdecl
svcctl_EnumServicesStatusA(
1986 SC_RPC_HANDLE hmngr
,
1996 return ERROR_CALL_NOT_IMPLEMENTED
;
1999 DWORD __cdecl
svcctl_OpenSCManagerA(
2000 MACHINE_HANDLEA MachineName
,
2001 LPCSTR DatabaseName
,
2003 SC_RPC_HANDLE
*handle
)
2006 return ERROR_CALL_NOT_IMPLEMENTED
;
2009 DWORD __cdecl
svcctl_OpenServiceA(
2010 SC_RPC_HANDLE hSCManager
,
2011 LPCSTR lpServiceName
,
2012 DWORD dwDesiredAccess
,
2013 SC_RPC_HANDLE
*phService
)
2016 return ERROR_CALL_NOT_IMPLEMENTED
;
2019 DWORD __cdecl
svcctl_QueryServiceConfigA(
2020 SC_RPC_HANDLE hService
,
2021 QUERY_SERVICE_CONFIGA
*config
,
2026 return ERROR_CALL_NOT_IMPLEMENTED
;
2029 DWORD __cdecl
svcctl_QueryServiceLockStatusA(
2030 SC_RPC_HANDLE scmanager
,
2031 QUERY_SERVICE_LOCK_STATUSA
*status
,
2036 return ERROR_CALL_NOT_IMPLEMENTED
;
2039 DWORD __cdecl
svcctl_StartServiceA(
2040 SC_RPC_HANDLE service
,
2045 return ERROR_CALL_NOT_IMPLEMENTED
;
2048 DWORD __cdecl
svcctl_GetServiceDisplayNameA(
2049 SC_RPC_HANDLE hSCManager
,
2055 return ERROR_CALL_NOT_IMPLEMENTED
;
2058 DWORD __cdecl
svcctl_GetServiceKeyNameA(
2059 SC_RPC_HANDLE hSCManager
,
2065 return ERROR_CALL_NOT_IMPLEMENTED
;
2068 DWORD __cdecl
svcctl_GetCurrentGroupStateW(void)
2071 return ERROR_CALL_NOT_IMPLEMENTED
;
2074 DWORD __cdecl
svcctl_EnumServiceGroupW(
2075 SC_RPC_HANDLE scmanager
,
2077 DWORD service_state
,
2081 DWORD
*returned_size
,
2082 DWORD
*resume_index
,
2086 return ERROR_CALL_NOT_IMPLEMENTED
;
2089 DWORD __cdecl
svcctl_ChangeServiceConfig2A(
2090 SC_RPC_HANDLE service
,
2091 SC_RPC_CONFIG_INFOA info
)
2094 return ERROR_CALL_NOT_IMPLEMENTED
;
2097 DWORD __cdecl
svcctl_QueryServiceConfig2A(
2098 SC_RPC_HANDLE service
,
2105 return ERROR_CALL_NOT_IMPLEMENTED
;
2108 DWORD
RPC_Init(void)
2110 WCHAR transport
[] = SVCCTL_TRANSPORT
;
2111 WCHAR endpoint
[] = SVCCTL_ENDPOINT
;
2114 if (!(cleanup_group
= CreateThreadpoolCleanupGroup()))
2116 WINE_ERR("CreateThreadpoolCleanupGroup failed with error %u\n", GetLastError());
2117 return GetLastError();
2120 if ((err
= RpcServerUseProtseqEpW(transport
, 0, endpoint
, NULL
)) != ERROR_SUCCESS
)
2122 WINE_ERR("RpcServerUseProtseq failed with error %u\n", err
);
2126 if ((err
= RpcServerRegisterIf(svcctl_v2_0_s_ifspec
, 0, 0)) != ERROR_SUCCESS
)
2128 WINE_ERR("RpcServerRegisterIf failed with error %u\n", err
);
2132 if ((err
= RpcServerListen(1, RPC_C_LISTEN_MAX_CALLS_DEFAULT
, TRUE
)) != ERROR_SUCCESS
)
2134 WINE_ERR("RpcServerListen failed with error %u\n", err
);
2138 exit_event
= __wine_make_process_system();
2139 return ERROR_SUCCESS
;
2144 RpcMgmtStopServerListening(NULL
);
2145 RpcServerUnregisterIf(svcctl_v2_0_s_ifspec
, NULL
, TRUE
);
2146 RpcMgmtWaitServerListen();
2148 CloseThreadpoolCleanupGroupMembers(cleanup_group
, TRUE
, NULL
);
2149 CloseThreadpoolCleanupGroup(cleanup_group
);
2150 CloseHandle(exit_event
);
2153 void __RPC_USER
SC_RPC_HANDLE_rundown(SC_RPC_HANDLE handle
)
2155 SC_RPC_HANDLE_destroy(handle
);
2158 void __RPC_USER
SC_NOTIFY_RPC_HANDLE_rundown(SC_NOTIFY_RPC_HANDLE handle
)
2162 void __RPC_FAR
* __RPC_USER
MIDL_user_allocate(SIZE_T len
)
2164 return HeapAlloc(GetProcessHeap(), 0, len
);
2167 void __RPC_USER
MIDL_user_free(void __RPC_FAR
* ptr
)
2169 HeapFree(GetProcessHeap(), 0, ptr
);