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 WINE_DEFAULT_DEBUG_CHANNEL(service
);
40 static const GENERIC_MAPPING g_scm_generic
=
42 (STANDARD_RIGHTS_READ
| SC_MANAGER_ENUMERATE_SERVICE
| SC_MANAGER_QUERY_LOCK_STATUS
),
43 (STANDARD_RIGHTS_WRITE
| SC_MANAGER_CREATE_SERVICE
| SC_MANAGER_MODIFY_BOOT_CONFIG
),
44 (STANDARD_RIGHTS_EXECUTE
| SC_MANAGER_CONNECT
| SC_MANAGER_LOCK
),
48 static const GENERIC_MAPPING g_svc_generic
=
50 (STANDARD_RIGHTS_READ
| SERVICE_QUERY_CONFIG
| SERVICE_QUERY_STATUS
| SERVICE_INTERROGATE
| SERVICE_ENUMERATE_DEPENDENTS
),
51 (STANDARD_RIGHTS_WRITE
| SERVICE_CHANGE_CONFIG
),
52 (STANDARD_RIGHTS_EXECUTE
| SERVICE_START
| SERVICE_STOP
| SERVICE_PAUSE_CONTINUE
| SERVICE_USER_DEFINED_CONTROL
),
58 SC_HTYPE_DONT_CARE
= 0,
70 struct sc_manager_handle
/* service control manager handle */
73 struct scmdatabase
*db
;
76 struct sc_notify_handle
82 SC_RPC_NOTIFY_PARAMS_LIST
*params_list
;
85 struct sc_service_handle
/* service handle */
90 struct service_entry
*service_entry
;
91 struct sc_notify_handle
*notify
;
94 static void sc_notify_retain(struct sc_notify_handle
*notify
)
96 InterlockedIncrement(¬ify
->ref
);
99 static void sc_notify_release(struct sc_notify_handle
*notify
)
101 ULONG r
= InterlockedDecrement(¬ify
->ref
);
104 CloseHandle(notify
->event
);
105 if (notify
->params_list
)
106 free(notify
->params_list
->NotifyParamsArray
[0].params
);
107 free(notify
->params_list
);
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 free(old_cfg
->lpBinaryPathName
);
204 if (old_cfg
->lpLoadOrderGroup
!= new_cfg
->lpLoadOrderGroup
)
205 free(old_cfg
->lpLoadOrderGroup
);
207 if (old_cfg
->lpServiceStartName
!= new_cfg
->lpServiceStartName
)
208 free(old_cfg
->lpServiceStartName
);
210 if (old_cfg
->lpDisplayName
!= new_cfg
->lpDisplayName
)
211 free(old_cfg
->lpDisplayName
);
213 if (old
->dependOnServices
!= new->dependOnServices
)
214 free(old
->dependOnServices
);
216 if (old
->dependOnGroups
!= new->dependOnGroups
)
217 free(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, %ld)\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 %lx, needed %lx\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, %lx)\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
= malloc(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
;
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
);
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, %ld)\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, %ld)\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
= malloc(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%lx)\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
= malloc((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
= malloc((len_groups
+ 1) * sizeof(WCHAR
));
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%lx, %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
= wcsdup(lpBinaryPathName
);
597 entry
->config
.lpLoadOrderGroup
= wcsdup(lpLoadOrderGroup
);
598 entry
->config
.lpServiceStartName
= wcsdup(lpServiceStartName
);
599 entry
->config
.lpDisplayName
= wcsdup(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%lx, %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
= wcsdup(service
->service_entry
->config
.lpBinaryPathName
);
708 config
->lpLoadOrderGroup
= wcsdup(service
->service_entry
->config
.lpLoadOrderGroup
);
709 config
->dwTagId
= service
->service_entry
->config
.dwTagId
;
710 config
->lpDependencies
= NULL
; /* TODO */
711 config
->lpServiceStartName
= wcsdup(service
->service_entry
->config
.lpServiceStartName
);
712 config
->lpDisplayName
= wcsdup(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
= wcsdup(lpBinaryPathName
);
809 if (lpLoadOrderGroup
!= NULL
)
810 new_entry
.config
.lpLoadOrderGroup
= wcsdup(lpLoadOrderGroup
);
812 if (lpServiceStartName
!= NULL
)
813 new_entry
.config
.lpServiceStartName
= wcsdup(lpServiceStartName
);
815 if (lpDisplayName
!= NULL
)
816 new_entry
.config
.lpDisplayName
= wcsdup(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
= calloc(1, sizeof(SC_RPC_NOTIFY_PARAMS_LIST
));
849 if (!(cparams
= calloc(1, sizeof(SERVICE_NOTIFY_STATUS_CHANGE_PARAMS_2
))))
855 cparams
->dwNotifyMask
= notify
->notify_mask
;
856 fill_status_process(&cparams
->ServiceStatus
, service
);
857 cparams
->dwNotificationStatus
= ERROR_SUCCESS
;
858 cparams
->dwNotificationTriggered
= 1 << (cparams
->ServiceStatus
.dwCurrentState
- SERVICE_STOPPED
);
859 cparams
->pszServiceNames
= NULL
;
863 list
->NotifyParamsArray
[0].dwInfoLevel
= 2;
864 list
->NotifyParamsArray
[0].params
= cparams
;
866 InterlockedExchangePointer((void**)¬ify
->params_list
, list
);
868 SetEvent(notify
->event
);
871 void notify_service_state(struct service_entry
*service
)
873 struct sc_service_handle
*service_handle
;
876 mask
= 1 << (service
->status
.dwCurrentState
- SERVICE_STOPPED
);
877 LIST_FOR_EACH_ENTRY(service_handle
, &service
->handles
, struct sc_service_handle
, entry
)
879 struct sc_notify_handle
*notify
= service_handle
->notify
;
880 if (notify
&& (notify
->notify_mask
& mask
))
882 fill_notify(notify
, service
);
883 sc_notify_release(notify
);
884 service_handle
->notify
= NULL
;
885 service_handle
->status_notified
= TRUE
;
888 service_handle
->status_notified
= FALSE
;
892 DWORD __cdecl
svcctl_SetServiceStatus(SC_RPC_HANDLE handle
, SERVICE_STATUS
*status
)
894 struct sc_service_handle
*service
;
895 struct process_entry
*process
;
898 WINE_TRACE("(%p, %p)\n", handle
, status
);
900 if ((err
= validate_service_handle(handle
, SERVICE_SET_STATUS
, &service
)) != 0)
903 service_lock(service
->service_entry
);
905 /* FIXME: be a bit more discriminant about what parts of the status we set
906 * and check that fields are valid */
907 service
->service_entry
->status
.dwCurrentState
= status
->dwCurrentState
;
908 service
->service_entry
->status
.dwControlsAccepted
= status
->dwControlsAccepted
;
909 service
->service_entry
->status
.dwWin32ExitCode
= status
->dwWin32ExitCode
;
910 service
->service_entry
->status
.dwServiceSpecificExitCode
= status
->dwServiceSpecificExitCode
;
911 service
->service_entry
->status
.dwCheckPoint
= status
->dwCheckPoint
;
912 service
->service_entry
->status
.dwWaitHint
= status
->dwWaitHint
;
913 SetEvent(service
->service_entry
->status_changed_event
);
915 if ((process
= service
->service_entry
->process
) &&
916 status
->dwCurrentState
== SERVICE_STOPPED
)
918 service
->service_entry
->process
= NULL
;
919 if (!--process
->use_count
)
920 terminate_after_timeout(process
, service_kill_timeout
);
921 if (service
->service_entry
->shared_process
&& process
->use_count
<= 1)
922 shutdown_shared_process(process
);
923 release_process(process
);
926 notify_service_state(service
->service_entry
);
927 service_unlock(service
->service_entry
);
929 return ERROR_SUCCESS
;
932 DWORD __cdecl
svcctl_ChangeServiceConfig2W( SC_RPC_HANDLE hService
, SC_RPC_CONFIG_INFOW config
)
934 struct sc_service_handle
*service
;
937 if ((err
= validate_service_handle(hService
, SERVICE_CHANGE_CONFIG
, &service
)) != 0)
940 switch (config
.dwInfoLevel
)
942 case SERVICE_CONFIG_DESCRIPTION
:
946 if (!config
.descr
->lpDescription
)
949 if (config
.descr
->lpDescription
[0])
951 if (!(descr
= wcsdup( config
.descr
->lpDescription
)))
952 return ERROR_NOT_ENOUGH_MEMORY
;
955 WINE_TRACE( "changing service %p descr to %s\n", service
, wine_dbgstr_w(descr
) );
956 service_lock( service
->service_entry
);
957 free( service
->service_entry
->description
);
958 service
->service_entry
->description
= descr
;
959 save_service_config( service
->service_entry
);
960 service_unlock( service
->service_entry
);
963 case SERVICE_CONFIG_FAILURE_ACTIONS
:
964 WINE_FIXME( "SERVICE_CONFIG_FAILURE_ACTIONS not implemented: period %lu msg %s cmd %s\n",
965 config
.actions
->dwResetPeriod
,
966 wine_dbgstr_w(config
.actions
->lpRebootMsg
),
967 wine_dbgstr_w(config
.actions
->lpCommand
) );
969 case SERVICE_CONFIG_PRESHUTDOWN_INFO
:
970 WINE_TRACE( "changing service %p preshutdown timeout to %ld\n",
971 service
, config
.preshutdown
->dwPreshutdownTimeout
);
972 service_lock( service
->service_entry
);
973 service
->service_entry
->preshutdown_timeout
= config
.preshutdown
->dwPreshutdownTimeout
;
974 save_service_config( service
->service_entry
);
975 service_unlock( service
->service_entry
);
978 WINE_FIXME("level %lu not implemented\n", config
.dwInfoLevel
);
979 err
= ERROR_INVALID_LEVEL
;
985 DWORD __cdecl
svcctl_QueryServiceConfig2W( SC_RPC_HANDLE hService
, DWORD level
,
986 BYTE
*buffer
, DWORD size
, LPDWORD needed
)
988 struct sc_service_handle
*service
;
991 memset(buffer
, 0, size
);
993 if ((err
= validate_service_handle(hService
, SERVICE_QUERY_CONFIG
, &service
)) != 0)
998 case SERVICE_CONFIG_DESCRIPTION
:
1000 struct service_description
*desc
= (struct service_description
*)buffer
;
1001 DWORD total_size
= sizeof(*desc
);
1003 service_lock(service
->service_entry
);
1004 if (service
->service_entry
->description
)
1005 total_size
+= lstrlenW(service
->service_entry
->description
) * sizeof(WCHAR
);
1007 *needed
= total_size
;
1008 if (size
>= total_size
)
1010 if (service
->service_entry
->description
)
1012 lstrcpyW( desc
->description
, service
->service_entry
->description
);
1013 desc
->size
= total_size
- FIELD_OFFSET(struct service_description
, description
);
1017 desc
->description
[0] = 0;
1021 else err
= ERROR_INSUFFICIENT_BUFFER
;
1022 service_unlock(service
->service_entry
);
1026 case SERVICE_CONFIG_PRESHUTDOWN_INFO
:
1027 service_lock(service
->service_entry
);
1029 *needed
= sizeof(SERVICE_PRESHUTDOWN_INFO
);
1030 if (size
>= *needed
)
1031 ((LPSERVICE_PRESHUTDOWN_INFO
)buffer
)->dwPreshutdownTimeout
=
1032 service
->service_entry
->preshutdown_timeout
;
1033 else err
= ERROR_INSUFFICIENT_BUFFER
;
1035 service_unlock(service
->service_entry
);
1039 WINE_FIXME("level %lu not implemented\n", level
);
1040 err
= ERROR_INVALID_LEVEL
;
1046 DWORD __cdecl
svcctl_QueryServiceStatusEx(
1047 SC_RPC_HANDLE hService
,
1048 SC_STATUS_TYPE InfoLevel
,
1051 LPDWORD pcbBytesNeeded
)
1053 struct sc_service_handle
*service
;
1055 LPSERVICE_STATUS_PROCESS pSvcStatusData
;
1057 memset(lpBuffer
, 0, cbBufSize
);
1059 if ((err
= validate_service_handle(hService
, SERVICE_QUERY_STATUS
, &service
)) != 0)
1062 if (InfoLevel
!= SC_STATUS_PROCESS_INFO
)
1063 return ERROR_INVALID_LEVEL
;
1065 pSvcStatusData
= (LPSERVICE_STATUS_PROCESS
) lpBuffer
;
1066 if (pSvcStatusData
== NULL
)
1067 return ERROR_INVALID_PARAMETER
;
1069 if (cbBufSize
< sizeof(SERVICE_STATUS_PROCESS
))
1071 if( pcbBytesNeeded
!= NULL
)
1072 *pcbBytesNeeded
= sizeof(SERVICE_STATUS_PROCESS
);
1074 return ERROR_INSUFFICIENT_BUFFER
;
1077 service_lock(service
->service_entry
);
1078 fill_status_process(pSvcStatusData
, service
->service_entry
);
1079 service_unlock(service
->service_entry
);
1081 return ERROR_SUCCESS
;
1084 /******************************************************************************
1085 * service_accepts_control
1087 static BOOL
service_accepts_control(const struct service_entry
*service
, DWORD dwControl
)
1089 DWORD a
= service
->status
.dwControlsAccepted
;
1091 if (dwControl
>= 128 && dwControl
<= 255)
1096 case SERVICE_CONTROL_INTERROGATE
:
1098 case SERVICE_CONTROL_STOP
:
1099 if (a
&SERVICE_ACCEPT_STOP
)
1102 case SERVICE_CONTROL_SHUTDOWN
:
1103 if (a
&SERVICE_ACCEPT_SHUTDOWN
)
1106 case SERVICE_CONTROL_PAUSE
:
1107 case SERVICE_CONTROL_CONTINUE
:
1108 if (a
&SERVICE_ACCEPT_PAUSE_CONTINUE
)
1111 case SERVICE_CONTROL_PARAMCHANGE
:
1112 if (a
&SERVICE_ACCEPT_PARAMCHANGE
)
1115 case SERVICE_CONTROL_NETBINDADD
:
1116 case SERVICE_CONTROL_NETBINDREMOVE
:
1117 case SERVICE_CONTROL_NETBINDENABLE
:
1118 case SERVICE_CONTROL_NETBINDDISABLE
:
1119 if (a
&SERVICE_ACCEPT_NETBINDCHANGE
)
1121 case SERVICE_CONTROL_HARDWAREPROFILECHANGE
:
1122 if (a
&SERVICE_ACCEPT_HARDWAREPROFILECHANGE
)
1125 case SERVICE_CONTROL_POWEREVENT
:
1126 if (a
&SERVICE_ACCEPT_POWEREVENT
)
1129 case SERVICE_CONTROL_SESSIONCHANGE
:
1130 if (a
&SERVICE_ACCEPT_SESSIONCHANGE
)
1137 /******************************************************************************
1138 * process_send_command
1140 static BOOL
process_send_command(struct process_entry
*process
, const void *data
, DWORD size
, DWORD
*result
)
1142 OVERLAPPED overlapped
;
1146 overlapped
.u
.s
.Offset
= 0;
1147 overlapped
.u
.s
.OffsetHigh
= 0;
1148 overlapped
.hEvent
= process
->overlapped_event
;
1149 r
= WriteFile(process
->control_pipe
, data
, size
, &count
, &overlapped
);
1150 if (!r
&& GetLastError() == ERROR_IO_PENDING
)
1152 ret
= WaitForSingleObject(process
->overlapped_event
, service_pipe_timeout
);
1153 if (ret
== WAIT_TIMEOUT
)
1155 WINE_ERR("sending command timed out\n");
1156 *result
= ERROR_SERVICE_REQUEST_TIMEOUT
;
1159 r
= GetOverlappedResult(process
->control_pipe
, &overlapped
, &count
, FALSE
);
1161 if (!r
|| count
!= size
)
1163 WINE_ERR("service protocol error - failed to write pipe!\n");
1164 *result
= (!r
? GetLastError() : ERROR_WRITE_FAULT
);
1167 r
= ReadFile(process
->control_pipe
, result
, sizeof *result
, &count
, &overlapped
);
1168 if (!r
&& GetLastError() == ERROR_IO_PENDING
)
1170 ret
= WaitForSingleObject(process
->overlapped_event
, service_pipe_timeout
);
1171 if (ret
== WAIT_TIMEOUT
)
1173 WINE_ERR("receiving command result timed out\n");
1174 *result
= ERROR_SERVICE_REQUEST_TIMEOUT
;
1177 r
= GetOverlappedResult(process
->control_pipe
, &overlapped
, &count
, FALSE
);
1179 if (!r
|| count
!= sizeof *result
)
1181 WINE_ERR("service protocol error - failed to read pipe "
1182 "r = %d count = %ld!\n", r
, count
);
1183 *result
= (!r
? GetLastError() : ERROR_READ_FAULT
);
1190 /******************************************************************************
1191 * process_send_control
1193 BOOL
process_send_control(struct process_entry
*process
, BOOL shared_process
, const WCHAR
*name
,
1194 DWORD control
, const BYTE
*data
, DWORD data_size
, DWORD
*result
)
1196 service_start_info
*ssi
;
1202 control
|= SERVICE_CONTROL_FORWARD_FLAG
;
1203 data
= (BYTE
*)name
;
1204 data_size
= (lstrlenW(name
) + 1) * sizeof(WCHAR
);
1208 /* calculate how much space we need to send the startup info */
1209 len
= (lstrlenW(name
) + 1) * sizeof(WCHAR
) + data_size
;
1211 ssi
= malloc(FIELD_OFFSET(service_start_info
, data
[len
]));
1212 ssi
->magic
= SERVICE_PROTOCOL_MAGIC
;
1213 ssi
->control
= control
;
1214 ssi
->total_size
= FIELD_OFFSET(service_start_info
, data
[len
]);
1215 ssi
->name_size
= lstrlenW(name
) + 1;
1216 lstrcpyW((WCHAR
*)ssi
->data
, name
);
1217 if (data_size
) memcpy(&ssi
->data
[ssi
->name_size
* sizeof(WCHAR
)], data
, data_size
);
1219 r
= process_send_command(process
, ssi
, ssi
->total_size
, result
);
1224 DWORD __cdecl
svcctl_StartServiceW(
1225 SC_RPC_HANDLE hService
,
1226 DWORD dwNumServiceArgs
,
1227 LPCWSTR
*lpServiceArgVectors
)
1229 struct sc_service_handle
*service
;
1232 WINE_TRACE("(%p, %ld, %p)\n", hService
, dwNumServiceArgs
, lpServiceArgVectors
);
1234 if ((err
= validate_service_handle(hService
, SERVICE_START
, &service
)) != 0)
1237 if (service
->service_entry
->config
.dwStartType
== SERVICE_DISABLED
)
1238 return ERROR_SERVICE_DISABLED
;
1240 if (!scmdatabase_lock_startup(service
->service_entry
->db
, 3000))
1241 return ERROR_SERVICE_DATABASE_LOCKED
;
1243 err
= service_start(service
->service_entry
, dwNumServiceArgs
, lpServiceArgVectors
);
1245 scmdatabase_unlock_startup(service
->service_entry
->db
);
1249 DWORD __cdecl
svcctl_ControlService(
1250 SC_RPC_HANDLE hService
,
1252 SERVICE_STATUS
*lpServiceStatus
)
1254 DWORD access_required
;
1255 struct sc_service_handle
*service
;
1256 struct process_entry
*process
;
1257 BOOL shared_process
;
1260 WINE_TRACE("(%p, %ld, %p)\n", hService
, dwControl
, lpServiceStatus
);
1264 case SERVICE_CONTROL_CONTINUE
:
1265 case SERVICE_CONTROL_NETBINDADD
:
1266 case SERVICE_CONTROL_NETBINDDISABLE
:
1267 case SERVICE_CONTROL_NETBINDENABLE
:
1268 case SERVICE_CONTROL_NETBINDREMOVE
:
1269 case SERVICE_CONTROL_PARAMCHANGE
:
1270 case SERVICE_CONTROL_PAUSE
:
1271 access_required
= SERVICE_PAUSE_CONTINUE
;
1273 case SERVICE_CONTROL_INTERROGATE
:
1274 access_required
= SERVICE_INTERROGATE
;
1276 case SERVICE_CONTROL_STOP
:
1277 access_required
= SERVICE_STOP
;
1280 if (dwControl
>= 128 && dwControl
<= 255)
1281 access_required
= SERVICE_USER_DEFINED_CONTROL
;
1283 return ERROR_INVALID_PARAMETER
;
1286 if ((result
= validate_service_handle(hService
, access_required
, &service
)) != 0)
1289 service_lock(service
->service_entry
);
1291 result
= ERROR_SUCCESS
;
1292 switch (service
->service_entry
->status
.dwCurrentState
)
1294 case SERVICE_STOPPED
:
1295 result
= ERROR_SERVICE_NOT_ACTIVE
;
1297 case SERVICE_START_PENDING
:
1298 if (dwControl
==SERVICE_CONTROL_STOP
)
1301 case SERVICE_STOP_PENDING
:
1302 result
= ERROR_SERVICE_CANNOT_ACCEPT_CTRL
;
1306 if (result
== ERROR_SUCCESS
&& service
->service_entry
->force_shutdown
)
1308 result
= ERROR_SERVICE_CANNOT_ACCEPT_CTRL
;
1309 if ((process
= service
->service_entry
->process
))
1311 service
->service_entry
->process
= NULL
;
1312 if (!--process
->use_count
) process_terminate(process
);
1313 release_process(process
);
1317 if (result
!= ERROR_SUCCESS
)
1319 if (lpServiceStatus
) *lpServiceStatus
= service
->service_entry
->status
;
1320 service_unlock(service
->service_entry
);
1324 if (!service_accepts_control(service
->service_entry
, dwControl
))
1326 service_unlock(service
->service_entry
);
1327 return ERROR_INVALID_SERVICE_CONTROL
;
1330 /* Remember that we tried to shutdown this service. When the service is
1331 * still running on the second invocation, it will be forcefully killed. */
1332 if (dwControl
== SERVICE_CONTROL_STOP
)
1333 service
->service_entry
->force_shutdown
= TRUE
;
1335 /* Hold a reference to the process while sending the command. */
1336 process
= grab_process(service
->service_entry
->process
);
1337 shared_process
= service
->service_entry
->shared_process
;
1338 service_unlock(service
->service_entry
);
1341 return ERROR_SERVICE_CANNOT_ACCEPT_CTRL
;
1343 result
= WaitForSingleObject(process
->control_mutex
, 30000);
1344 if (result
!= WAIT_OBJECT_0
)
1346 release_process(process
);
1347 return ERROR_SERVICE_REQUEST_TIMEOUT
;
1350 if (process_send_control(process
, shared_process
, service
->service_entry
->name
,
1351 dwControl
, NULL
, 0, &result
))
1352 result
= ERROR_SUCCESS
;
1354 if (lpServiceStatus
)
1356 service_lock(service
->service_entry
);
1357 *lpServiceStatus
= service
->service_entry
->status
;
1358 service_unlock(service
->service_entry
);
1361 ReleaseMutex(process
->control_mutex
);
1362 release_process(process
);
1366 DWORD __cdecl
svcctl_CloseServiceHandle(
1367 SC_RPC_HANDLE
*handle
)
1369 WINE_TRACE("(&%p)\n", *handle
);
1371 SC_RPC_HANDLE_destroy(*handle
);
1374 return ERROR_SUCCESS
;
1377 void __RPC_USER
SC_RPC_LOCK_rundown(SC_RPC_LOCK hLock
)
1381 DWORD __cdecl
svcctl_LockServiceDatabase(SC_RPC_HANDLE manager
, SC_RPC_LOCK
*lock
)
1383 TRACE("(%p, %p)\n", manager
, lock
);
1385 *lock
= (SC_RPC_LOCK
)0xdeadbeef;
1386 return ERROR_SUCCESS
;
1389 DWORD __cdecl
svcctl_UnlockServiceDatabase(SC_RPC_LOCK
*lock
)
1391 TRACE("(&%p)\n", *lock
);
1394 return ERROR_SUCCESS
;
1397 static BOOL
map_state(DWORD state
, DWORD mask
)
1401 case SERVICE_START_PENDING
:
1402 case SERVICE_STOP_PENDING
:
1403 case SERVICE_RUNNING
:
1404 case SERVICE_CONTINUE_PENDING
:
1405 case SERVICE_PAUSE_PENDING
:
1406 case SERVICE_PAUSED
:
1407 if (SERVICE_ACTIVE
& mask
) return TRUE
;
1409 case SERVICE_STOPPED
:
1410 if (SERVICE_INACTIVE
& mask
) return TRUE
;
1413 WINE_ERR("unknown state %lu\n", state
);
1419 DWORD __cdecl
svcctl_EnumServicesStatusW(
1420 SC_RPC_HANDLE hmngr
,
1429 DWORD err
, sz
, total_size
, num_services
, offset
;
1430 struct sc_manager_handle
*manager
;
1431 struct service_entry
*service
;
1432 struct enum_service_status
*s
;
1434 WINE_TRACE("(%p, 0x%lx, 0x%lx, %p, %lu, %p, %p, %p)\n", hmngr
, type
, state
, buffer
, size
, needed
, returned
, resume
);
1436 if (!type
|| !state
)
1437 return ERROR_INVALID_PARAMETER
;
1439 if ((err
= validate_scm_handle(hmngr
, SC_MANAGER_ENUMERATE_SERVICE
, &manager
)) != ERROR_SUCCESS
)
1443 WINE_FIXME("resume index not supported\n");
1445 scmdatabase_lock(manager
->db
);
1447 total_size
= num_services
= 0;
1448 LIST_FOR_EACH_ENTRY(service
, &manager
->db
->services
, struct service_entry
, entry
)
1450 if ((service
->status
.dwServiceType
& type
) && map_state(service
->status
.dwCurrentState
, state
))
1452 total_size
+= sizeof(*s
);
1453 total_size
+= (lstrlenW(service
->name
) + 1) * sizeof(WCHAR
);
1454 if (service
->config
.lpDisplayName
)
1456 total_size
+= (lstrlenW(service
->config
.lpDisplayName
) + 1) * sizeof(WCHAR
);
1462 *needed
= total_size
;
1463 if (total_size
> size
)
1465 scmdatabase_unlock(manager
->db
);
1466 return ERROR_MORE_DATA
;
1468 s
= (struct enum_service_status
*)buffer
;
1469 offset
= num_services
* sizeof(struct enum_service_status
);
1470 LIST_FOR_EACH_ENTRY(service
, &manager
->db
->services
, struct service_entry
, entry
)
1472 if ((service
->status
.dwServiceType
& type
) && map_state(service
->status
.dwCurrentState
, state
))
1474 sz
= (lstrlenW(service
->name
) + 1) * sizeof(WCHAR
);
1475 memcpy(buffer
+ offset
, service
->name
, sz
);
1476 s
->service_name
= offset
;
1479 if (!service
->config
.lpDisplayName
) s
->display_name
= 0;
1482 sz
= (lstrlenW(service
->config
.lpDisplayName
) + 1) * sizeof(WCHAR
);
1483 memcpy(buffer
+ offset
, service
->config
.lpDisplayName
, sz
);
1484 s
->display_name
= offset
;
1487 s
->service_status
= service
->status
;
1491 *returned
= num_services
;
1493 scmdatabase_unlock(manager
->db
);
1494 return ERROR_SUCCESS
;
1497 static struct service_entry
*find_service_by_group(struct scmdatabase
*db
, const WCHAR
*group
)
1499 struct service_entry
*service
;
1500 LIST_FOR_EACH_ENTRY(service
, &db
->services
, struct service_entry
, entry
)
1502 if (service
->config
.lpLoadOrderGroup
&& !wcsicmp(group
, service
->config
.lpLoadOrderGroup
))
1508 static BOOL
match_group(const WCHAR
*g1
, const WCHAR
*g2
)
1510 if (!g2
) return TRUE
;
1511 if (!g2
[0] && (!g1
|| !g1
[0])) return TRUE
;
1512 if (g1
&& !lstrcmpW(g1
, g2
)) return TRUE
;
1516 DWORD __cdecl
svcctl_EnumServicesStatusExA(
1517 SC_RPC_HANDLE scmanager
,
1518 SC_ENUM_TYPE info_level
,
1520 DWORD service_state
,
1524 DWORD
*services_count
,
1525 DWORD
*resume_index
,
1529 return ERROR_CALL_NOT_IMPLEMENTED
;
1532 DWORD __cdecl
svcctl_EnumServicesStatusExW(
1533 SC_RPC_HANDLE hmngr
,
1534 SC_ENUM_TYPE info_level
,
1541 DWORD
*resume_handle
,
1544 DWORD err
, sz
, total_size
, num_services
;
1546 struct sc_manager_handle
*manager
;
1547 struct service_entry
*service
;
1548 struct enum_service_status_process
*s
;
1550 WINE_TRACE("(%p, 0x%lx, 0x%lx, %p, %lu, %p, %p, %s)\n", hmngr
, type
, state
, buffer
, size
,
1551 needed
, returned
, wine_dbgstr_w(group
));
1554 FIXME("resume handle not supported\n");
1556 if (!type
|| !state
)
1557 return ERROR_INVALID_PARAMETER
;
1559 if ((err
= validate_scm_handle(hmngr
, SC_MANAGER_ENUMERATE_SERVICE
, &manager
)) != ERROR_SUCCESS
)
1562 scmdatabase_lock(manager
->db
);
1564 if (group
&& !find_service_by_group(manager
->db
, group
))
1566 scmdatabase_unlock(manager
->db
);
1567 return ERROR_SERVICE_DOES_NOT_EXIST
;
1570 total_size
= num_services
= 0;
1571 LIST_FOR_EACH_ENTRY(service
, &manager
->db
->services
, struct service_entry
, entry
)
1573 if ((service
->status
.dwServiceType
& type
) && map_state(service
->status
.dwCurrentState
, state
)
1574 && match_group(service
->config
.lpLoadOrderGroup
, group
))
1576 total_size
+= sizeof(*s
);
1577 total_size
+= (lstrlenW(service
->name
) + 1) * sizeof(WCHAR
);
1578 if (service
->config
.lpDisplayName
)
1580 total_size
+= (lstrlenW(service
->config
.lpDisplayName
) + 1) * sizeof(WCHAR
);
1586 *needed
= total_size
;
1587 if (total_size
> size
)
1589 scmdatabase_unlock(manager
->db
);
1590 return ERROR_MORE_DATA
;
1592 s
= (struct enum_service_status_process
*)buffer
;
1593 offset
= num_services
* sizeof(*s
);
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 sz
= (lstrlenW(service
->name
) + 1) * sizeof(WCHAR
);
1600 memcpy(buffer
+ offset
, service
->name
, sz
);
1601 s
->service_name
= offset
;
1604 if (!service
->config
.lpDisplayName
) s
->display_name
= 0;
1607 sz
= (lstrlenW(service
->config
.lpDisplayName
) + 1) * sizeof(WCHAR
);
1608 memcpy(buffer
+ offset
, service
->config
.lpDisplayName
, sz
);
1609 s
->display_name
= offset
;
1612 fill_status_process(&s
->service_status_process
, service
);
1616 *returned
= num_services
;
1618 scmdatabase_unlock(manager
->db
);
1619 return ERROR_SUCCESS
;
1622 DWORD __cdecl
svcctl_unknown43(void)
1625 return ERROR_CALL_NOT_IMPLEMENTED
;
1628 DWORD __cdecl
svcctl_CreateServiceWOW64A(
1629 SC_RPC_HANDLE scmanager
,
1635 DWORD error_control
,
1637 LPCSTR loadordergroup
,
1639 const BYTE
*dependencies
,
1642 const BYTE
*password
,
1643 DWORD password_size
,
1644 SC_RPC_HANDLE
*service
)
1647 return ERROR_CALL_NOT_IMPLEMENTED
;
1650 DWORD __cdecl
svcctl_CreateServiceWOW64W(
1651 SC_RPC_HANDLE scmanager
,
1652 LPCWSTR servicename
,
1653 LPCWSTR displayname
,
1657 DWORD error_control
,
1659 LPCWSTR loadordergroup
,
1661 const BYTE
*dependencies
,
1664 const BYTE
*password
,
1665 DWORD password_size
,
1666 SC_RPC_HANDLE
*service
)
1668 WINE_TRACE("(%s, %s, 0x%lx, %s)\n", wine_dbgstr_w(servicename
), wine_dbgstr_w(displayname
), accessmask
, wine_dbgstr_w(imagepath
));
1669 return create_serviceW(scmanager
, servicename
, displayname
, accessmask
, service_type
, start_type
, error_control
, imagepath
,
1670 loadordergroup
, tagid
, dependencies
, depend_size
, start_name
, password
, password_size
, service
, TRUE
);
1673 DWORD __cdecl
svcctl_unknown46(void)
1676 return ERROR_CALL_NOT_IMPLEMENTED
;
1679 DWORD __cdecl
svcctl_NotifyServiceStatusChange(
1680 SC_RPC_HANDLE handle
,
1681 SC_RPC_NOTIFY_PARAMS params
,
1682 GUID
*clientprocessguid
,
1683 GUID
*scmprocessguid
,
1684 BOOL
*createremotequeue
,
1685 SC_NOTIFY_RPC_HANDLE
*hNotify
)
1688 struct sc_manager_handle
*manager
= NULL
;
1689 struct sc_service_handle
*service
= NULL
;
1690 struct sc_notify_handle
*notify
;
1691 struct sc_handle
*hdr
= handle
;
1693 WINE_TRACE("(%p, NotifyMask: 0x%lx, %p, %p, %p, %p)\n", handle
,
1694 params
.params
->dwNotifyMask
, clientprocessguid
, scmprocessguid
,
1695 createremotequeue
, hNotify
);
1699 case SC_HTYPE_SERVICE
:
1700 err
= validate_service_handle(handle
, SERVICE_QUERY_STATUS
, &service
);
1702 case SC_HTYPE_MANAGER
:
1703 err
= validate_scm_handle(handle
, SC_MANAGER_ENUMERATE_SERVICE
, &manager
);
1706 err
= ERROR_INVALID_HANDLE
;
1710 if (err
!= ERROR_SUCCESS
)
1715 WARN("Need support for service creation/deletion notifications\n");
1716 return ERROR_CALL_NOT_IMPLEMENTED
;
1719 notify
= calloc(1, sizeof(*notify
));
1721 return ERROR_NOT_ENOUGH_SERVER_MEMORY
;
1723 notify
->hdr
.type
= SC_HTYPE_NOTIFY
;
1724 notify
->hdr
.access
= 0;
1726 notify
->event
= CreateEventW(NULL
, TRUE
, FALSE
, NULL
);
1728 notify
->notify_mask
= params
.params
->dwNotifyMask
;
1730 service_lock(service
->service_entry
);
1732 if (service
->notify
)
1734 service_unlock(service
->service_entry
);
1735 sc_notify_release(notify
);
1736 return ERROR_ALREADY_REGISTERED
;
1739 mask
= 1 << (service
->service_entry
->status
.dwCurrentState
- SERVICE_STOPPED
);
1740 if (!service
->status_notified
&& (notify
->notify_mask
& mask
))
1742 fill_notify(notify
, service
->service_entry
);
1743 service
->status_notified
= TRUE
;
1747 sc_notify_retain(notify
);
1748 service
->notify
= notify
;
1751 sc_notify_retain(notify
);
1752 *hNotify
= ¬ify
->hdr
;
1754 service_unlock(service
->service_entry
);
1756 return ERROR_SUCCESS
;
1759 DWORD __cdecl
svcctl_GetNotifyResults(
1760 SC_NOTIFY_RPC_HANDLE hNotify
,
1761 SC_RPC_NOTIFY_PARAMS_LIST
**pList
)
1764 struct sc_notify_handle
*notify
;
1766 WINE_TRACE("(%p, %p)\n", hNotify
, pList
);
1769 return ERROR_INVALID_PARAMETER
;
1773 if ((err
= validate_notify_handle(hNotify
, 0, ¬ify
)) != 0)
1776 sc_notify_retain(notify
);
1777 /* block until there is a result */
1778 err
= WaitForSingleObject(notify
->event
, INFINITE
);
1780 if (err
!= WAIT_OBJECT_0
)
1782 sc_notify_release(notify
);
1786 *pList
= InterlockedExchangePointer((void**)¬ify
->params_list
, NULL
);
1789 sc_notify_release(notify
);
1790 return ERROR_REQUEST_ABORTED
;
1793 sc_notify_release(notify
);
1795 return ERROR_SUCCESS
;
1798 DWORD __cdecl
svcctl_CloseNotifyHandle(
1799 SC_NOTIFY_RPC_HANDLE
*hNotify
,
1802 struct sc_notify_handle
*notify
;
1805 WINE_TRACE("(%p, %p)\n", hNotify
, apc_fired
);
1807 if ((err
= validate_notify_handle(*hNotify
, 0, ¬ify
)) != 0)
1810 sc_notify_release(notify
);
1812 return ERROR_SUCCESS
;
1815 DWORD __cdecl
svcctl_ControlServiceExA(
1816 SC_RPC_HANDLE service
,
1819 SC_RPC_SERVICE_CONTROL_IN_PARAMSA
*in_params
,
1820 SC_RPC_SERVICE_CONTROL_OUT_PARAMSA
*out_params
)
1823 return ERROR_CALL_NOT_IMPLEMENTED
;
1826 DWORD __cdecl
svcctl_ControlServiceExW(
1827 SC_RPC_HANDLE service
,
1830 SC_RPC_SERVICE_CONTROL_IN_PARAMSW
*in_params
,
1831 SC_RPC_SERVICE_CONTROL_OUT_PARAMSW
*out_params
)
1834 return ERROR_CALL_NOT_IMPLEMENTED
;
1837 DWORD __cdecl
svcctl_unknown52(void)
1840 return ERROR_CALL_NOT_IMPLEMENTED
;
1843 DWORD __cdecl
svcctl_unknown53(void)
1846 return ERROR_CALL_NOT_IMPLEMENTED
;
1849 DWORD __cdecl
svcctl_unknown54(void)
1852 return ERROR_CALL_NOT_IMPLEMENTED
;
1855 DWORD __cdecl
svcctl_unknown55(void)
1858 return ERROR_CALL_NOT_IMPLEMENTED
;
1861 DWORD __cdecl
svcctl_QueryServiceConfigEx(
1862 SC_RPC_HANDLE service
,
1864 SC_RPC_CONFIG_INFOW
*info
)
1867 return ERROR_CALL_NOT_IMPLEMENTED
;
1870 DWORD __cdecl
svcctl_QueryServiceObjectSecurity(
1871 SC_RPC_HANDLE service
,
1872 SECURITY_INFORMATION info
,
1878 return ERROR_CALL_NOT_IMPLEMENTED
;
1881 DWORD __cdecl
svcctl_SetServiceObjectSecurity(
1882 SC_RPC_HANDLE service
,
1883 SECURITY_INFORMATION info
,
1888 return ERROR_CALL_NOT_IMPLEMENTED
;
1891 DWORD __cdecl
svcctl_QueryServiceStatus(
1892 SC_RPC_HANDLE service
,
1893 SERVICE_STATUS
*status
)
1896 return ERROR_CALL_NOT_IMPLEMENTED
;
1899 DWORD __cdecl
svcctl_NotifyBootConfigStatus(
1900 SVCCTL_HANDLEW machinename
,
1901 DWORD boot_acceptable
)
1904 return ERROR_CALL_NOT_IMPLEMENTED
;
1907 DWORD __cdecl
svcctl_SCSetServiceBitsW(void)
1910 return ERROR_CALL_NOT_IMPLEMENTED
;
1913 DWORD __cdecl
svcctl_EnumDependentServicesW(
1914 SC_RPC_HANDLE service
,
1919 DWORD
*services_ret
)
1922 return ERROR_CALL_NOT_IMPLEMENTED
;
1925 DWORD __cdecl
svcctl_QueryServiceLockStatusW(
1926 SC_RPC_HANDLE scmanager
,
1927 QUERY_SERVICE_LOCK_STATUSW
*status
,
1932 return ERROR_CALL_NOT_IMPLEMENTED
;
1935 DWORD __cdecl
svcctl_SCSetServiceBitsA(void)
1938 return ERROR_CALL_NOT_IMPLEMENTED
;
1941 DWORD __cdecl
svcctl_ChangeServiceConfigA(
1942 SC_RPC_HANDLE service
,
1945 DWORD error_control
,
1947 LPSTR loadordergroup
,
1953 DWORD password_size
,
1957 return ERROR_CALL_NOT_IMPLEMENTED
;
1960 DWORD __cdecl
svcctl_CreateServiceA(
1961 SC_RPC_HANDLE scmanager
,
1964 DWORD desiredaccess
,
1967 DWORD error_control
,
1969 LPCSTR loadordergroup
,
1971 const BYTE
*dependencies
,
1974 const BYTE
*password
,
1975 DWORD password_size
,
1976 SC_RPC_HANDLE
*service
)
1979 return ERROR_CALL_NOT_IMPLEMENTED
;
1982 DWORD __cdecl
svcctl_EnumDependentServicesA(
1983 SC_RPC_HANDLE service
,
1988 DWORD
*services_ret
)
1991 return ERROR_CALL_NOT_IMPLEMENTED
;
1994 DWORD __cdecl
svcctl_EnumServicesStatusA(
1995 SC_RPC_HANDLE hmngr
,
2005 return ERROR_CALL_NOT_IMPLEMENTED
;
2008 DWORD __cdecl
svcctl_OpenSCManagerA(
2009 MACHINE_HANDLEA MachineName
,
2010 LPCSTR DatabaseName
,
2012 SC_RPC_HANDLE
*handle
)
2015 return ERROR_CALL_NOT_IMPLEMENTED
;
2018 DWORD __cdecl
svcctl_OpenServiceA(
2019 SC_RPC_HANDLE hSCManager
,
2020 LPCSTR lpServiceName
,
2021 DWORD dwDesiredAccess
,
2022 SC_RPC_HANDLE
*phService
)
2025 return ERROR_CALL_NOT_IMPLEMENTED
;
2028 DWORD __cdecl
svcctl_QueryServiceConfigA(
2029 SC_RPC_HANDLE hService
,
2030 QUERY_SERVICE_CONFIGA
*config
,
2035 return ERROR_CALL_NOT_IMPLEMENTED
;
2038 DWORD __cdecl
svcctl_QueryServiceLockStatusA(
2039 SC_RPC_HANDLE scmanager
,
2040 QUERY_SERVICE_LOCK_STATUSA
*status
,
2045 return ERROR_CALL_NOT_IMPLEMENTED
;
2048 DWORD __cdecl
svcctl_StartServiceA(
2049 SC_RPC_HANDLE service
,
2054 return ERROR_CALL_NOT_IMPLEMENTED
;
2057 DWORD __cdecl
svcctl_GetServiceDisplayNameA(
2058 SC_RPC_HANDLE hSCManager
,
2064 return ERROR_CALL_NOT_IMPLEMENTED
;
2067 DWORD __cdecl
svcctl_GetServiceKeyNameA(
2068 SC_RPC_HANDLE hSCManager
,
2074 return ERROR_CALL_NOT_IMPLEMENTED
;
2077 DWORD __cdecl
svcctl_GetCurrentGroupStateW(void)
2080 return ERROR_CALL_NOT_IMPLEMENTED
;
2083 DWORD __cdecl
svcctl_EnumServiceGroupW(
2084 SC_RPC_HANDLE scmanager
,
2086 DWORD service_state
,
2090 DWORD
*returned_size
,
2091 DWORD
*resume_index
,
2095 return ERROR_CALL_NOT_IMPLEMENTED
;
2098 DWORD __cdecl
svcctl_ChangeServiceConfig2A(
2099 SC_RPC_HANDLE service
,
2100 SC_RPC_CONFIG_INFOA info
)
2103 return ERROR_CALL_NOT_IMPLEMENTED
;
2106 DWORD __cdecl
svcctl_QueryServiceConfig2A(
2107 SC_RPC_HANDLE service
,
2114 return ERROR_CALL_NOT_IMPLEMENTED
;
2117 DWORD
RPC_Init(void)
2119 WCHAR transport
[] = SVCCTL_TRANSPORT
;
2120 WCHAR endpoint
[] = SVCCTL_ENDPOINT
;
2123 if (!(cleanup_group
= CreateThreadpoolCleanupGroup()))
2125 WINE_ERR("CreateThreadpoolCleanupGroup failed with error %lu\n", GetLastError());
2126 return GetLastError();
2129 if ((err
= RpcServerUseProtseqEpW(transport
, 0, endpoint
, NULL
)) != ERROR_SUCCESS
)
2131 WINE_ERR("RpcServerUseProtseq failed with error %lu\n", err
);
2135 if ((err
= RpcServerRegisterIf(svcctl_v2_0_s_ifspec
, 0, 0)) != ERROR_SUCCESS
)
2137 WINE_ERR("RpcServerRegisterIf failed with error %lu\n", err
);
2141 if ((err
= RpcServerListen(1, RPC_C_LISTEN_MAX_CALLS_DEFAULT
, TRUE
)) != ERROR_SUCCESS
)
2143 WINE_ERR("RpcServerListen failed with error %lu\n", err
);
2147 NtSetInformationProcess( GetCurrentProcess(), ProcessWineMakeProcessSystem
,
2148 &exit_event
, sizeof(HANDLE
*) );
2149 return ERROR_SUCCESS
;
2154 RpcMgmtStopServerListening(NULL
);
2155 RpcServerUnregisterIf(svcctl_v2_0_s_ifspec
, NULL
, TRUE
);
2156 RpcMgmtWaitServerListen();
2158 CloseThreadpoolCleanupGroupMembers(cleanup_group
, TRUE
, NULL
);
2159 CloseThreadpoolCleanupGroup(cleanup_group
);
2160 CloseHandle(exit_event
);
2163 void __RPC_USER
SC_RPC_HANDLE_rundown(SC_RPC_HANDLE handle
)
2165 SC_RPC_HANDLE_destroy(handle
);
2168 void __RPC_USER
SC_NOTIFY_RPC_HANDLE_rundown(SC_NOTIFY_RPC_HANDLE handle
)
2172 void __RPC_FAR
* __RPC_USER
MIDL_user_allocate(SIZE_T len
)
2177 void __RPC_USER
MIDL_user_free(void __RPC_FAR
* ptr
)