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
30 #include "wine/list.h"
31 #include "wine/debug.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(service
);
38 static const GENERIC_MAPPING g_scm_generic
=
40 (STANDARD_RIGHTS_READ
| SC_MANAGER_ENUMERATE_SERVICE
| SC_MANAGER_QUERY_LOCK_STATUS
),
41 (STANDARD_RIGHTS_WRITE
| SC_MANAGER_CREATE_SERVICE
| SC_MANAGER_MODIFY_BOOT_CONFIG
),
42 (STANDARD_RIGHTS_EXECUTE
| SC_MANAGER_CONNECT
| SC_MANAGER_LOCK
),
46 static const GENERIC_MAPPING g_svc_generic
=
48 (STANDARD_RIGHTS_READ
| SERVICE_QUERY_CONFIG
| SERVICE_QUERY_STATUS
| SERVICE_INTERROGATE
| SERVICE_ENUMERATE_DEPENDENTS
),
49 (STANDARD_RIGHTS_WRITE
| SERVICE_CHANGE_CONFIG
),
50 (STANDARD_RIGHTS_EXECUTE
| SERVICE_START
| SERVICE_STOP
| SERVICE_PAUSE_CONTINUE
| SERVICE_USER_DEFINED_CONTROL
),
56 SC_HTYPE_DONT_CARE
= 0,
68 struct sc_manager_handle
/* service control manager handle */
71 struct scmdatabase
*db
;
74 struct sc_notify_handle
80 SC_RPC_NOTIFY_PARAMS_LIST
*params_list
;
83 struct sc_service_handle
/* service handle */
88 struct service_entry
*service_entry
;
89 struct sc_notify_handle
*notify
;
92 static void sc_notify_retain(struct sc_notify_handle
*notify
)
94 InterlockedIncrement(¬ify
->ref
);
97 static void sc_notify_release(struct sc_notify_handle
*notify
)
99 ULONG r
= InterlockedDecrement(¬ify
->ref
);
102 CloseHandle(notify
->event
);
103 if (notify
->params_list
)
104 free(notify
->params_list
->NotifyParamsArray
[0].params
);
105 free(notify
->params_list
);
112 struct scmdatabase
*db
;
115 static const WCHAR emptyW
[] = {0};
116 static PTP_CLEANUP_GROUP cleanup_group
;
119 static void CALLBACK
group_cancel_callback(void *object
, void *userdata
)
121 struct process_entry
*process
= object
;
122 release_process(process
);
125 static void CALLBACK
terminate_callback(TP_CALLBACK_INSTANCE
*instance
, void *context
,
126 TP_WAIT
*wait
, TP_WAIT_RESULT result
)
128 struct process_entry
*process
= context
;
129 if (result
== WAIT_TIMEOUT
) process_terminate(process
);
130 release_process(process
);
131 CloseThreadpoolWait(wait
);
134 static void terminate_after_timeout(struct process_entry
*process
, DWORD timeout
)
136 TP_CALLBACK_ENVIRON environment
;
137 LARGE_INTEGER timestamp
;
141 memset(&environment
, 0, sizeof(environment
));
142 environment
.Version
= 1;
143 environment
.CleanupGroup
= cleanup_group
;
144 environment
.CleanupGroupCancelCallback
= group_cancel_callback
;
146 timestamp
.QuadPart
= (ULONGLONG
)timeout
* -10000;
147 ft
.dwLowDateTime
= timestamp
.u
.LowPart
;
148 ft
.dwHighDateTime
= timestamp
.u
.HighPart
;
150 if ((wait
= CreateThreadpoolWait(terminate_callback
, grab_process(process
), &environment
)))
151 SetThreadpoolWait(wait
, process
->process
, &ft
);
153 release_process(process
);
156 static void CALLBACK
shutdown_callback(TP_CALLBACK_INSTANCE
*instance
, void *context
)
158 struct process_entry
*process
= context
;
161 result
= WaitForSingleObject(process
->control_mutex
, 30000);
162 if (result
== WAIT_OBJECT_0
)
164 process_send_control(process
, FALSE
, emptyW
, SERVICE_CONTROL_STOP
, NULL
, 0, &result
);
165 ReleaseMutex(process
->control_mutex
);
168 release_process(process
);
171 static void shutdown_shared_process(struct process_entry
*process
)
173 TP_CALLBACK_ENVIRON environment
;
174 struct service_entry
*service
;
175 struct scmdatabase
*db
= process
->db
;
177 scmdatabase_lock(db
);
178 LIST_FOR_EACH_ENTRY(service
, &db
->services
, struct service_entry
, entry
)
180 if (service
->process
!= process
) continue;
181 service
->status
.dwCurrentState
= SERVICE_STOP_PENDING
;
183 scmdatabase_unlock(db
);
185 memset(&environment
, 0, sizeof(environment
));
186 environment
.Version
= 1;
187 environment
.CleanupGroup
= cleanup_group
;
188 environment
.CleanupGroupCancelCallback
= group_cancel_callback
;
190 if (!TrySubmitThreadpoolCallback(shutdown_callback
, grab_process(process
), &environment
))
191 release_process(process
);
194 static void free_service_strings(struct service_entry
*old
, struct service_entry
*new)
196 QUERY_SERVICE_CONFIGW
*old_cfg
= &old
->config
;
197 QUERY_SERVICE_CONFIGW
*new_cfg
= &new->config
;
199 if (old_cfg
->lpBinaryPathName
!= new_cfg
->lpBinaryPathName
)
200 free(old_cfg
->lpBinaryPathName
);
202 if (old_cfg
->lpLoadOrderGroup
!= new_cfg
->lpLoadOrderGroup
)
203 free(old_cfg
->lpLoadOrderGroup
);
205 if (old_cfg
->lpServiceStartName
!= new_cfg
->lpServiceStartName
)
206 free(old_cfg
->lpServiceStartName
);
208 if (old_cfg
->lpDisplayName
!= new_cfg
->lpDisplayName
)
209 free(old_cfg
->lpDisplayName
);
211 if (old
->dependOnServices
!= new->dependOnServices
)
212 free(old
->dependOnServices
);
214 if (old
->dependOnGroups
!= new->dependOnGroups
)
215 free(old
->dependOnGroups
);
218 /* Check if the given handle is of the required type and allows the requested access. */
219 static DWORD
validate_context_handle(SC_RPC_HANDLE handle
, DWORD type
, DWORD needed_access
, struct sc_handle
**out_hdr
)
221 struct sc_handle
*hdr
= handle
;
223 if (type
!= SC_HTYPE_DONT_CARE
&& hdr
->type
!= type
)
225 WINE_ERR("Handle is of an invalid type (%d, %ld)\n", hdr
->type
, type
);
226 return ERROR_INVALID_HANDLE
;
229 if ((needed_access
& hdr
->access
) != needed_access
)
231 WINE_ERR("Access denied - handle created with access %lx, needed %lx\n", hdr
->access
, needed_access
);
232 return ERROR_ACCESS_DENIED
;
236 return ERROR_SUCCESS
;
239 static DWORD
validate_scm_handle(SC_RPC_HANDLE handle
, DWORD needed_access
, struct sc_manager_handle
**manager
)
241 struct sc_handle
*hdr
;
242 DWORD err
= validate_context_handle(handle
, SC_HTYPE_MANAGER
, needed_access
, &hdr
);
243 if (err
== ERROR_SUCCESS
)
244 *manager
= (struct sc_manager_handle
*)hdr
;
248 static DWORD
validate_service_handle(SC_RPC_HANDLE handle
, DWORD needed_access
, struct sc_service_handle
**service
)
250 struct sc_handle
*hdr
;
251 DWORD err
= validate_context_handle(handle
, SC_HTYPE_SERVICE
, needed_access
, &hdr
);
252 if (err
== ERROR_SUCCESS
)
253 *service
= (struct sc_service_handle
*)hdr
;
257 static DWORD
validate_notify_handle(SC_RPC_HANDLE handle
, DWORD needed_access
, struct sc_notify_handle
**notify
)
259 struct sc_handle
*hdr
;
260 DWORD err
= validate_context_handle(handle
, SC_HTYPE_NOTIFY
, needed_access
, &hdr
);
261 if (err
== ERROR_SUCCESS
)
262 *notify
= (struct sc_notify_handle
*)hdr
;
266 DWORD __cdecl
svcctl_OpenSCManagerW(
267 MACHINE_HANDLEW MachineName
, /* Note: this parameter is ignored */
268 LPCWSTR DatabaseName
,
270 SC_RPC_HANDLE
*handle
)
272 struct sc_manager_handle
*manager
;
274 WINE_TRACE("(%s, %s, %lx)\n", wine_dbgstr_w(MachineName
), wine_dbgstr_w(DatabaseName
), dwAccessMask
);
276 if (DatabaseName
!= NULL
&& DatabaseName
[0])
278 if (lstrcmpW(DatabaseName
, SERVICES_FAILED_DATABASEW
) == 0)
279 return ERROR_DATABASE_DOES_NOT_EXIST
;
280 if (lstrcmpW(DatabaseName
, SERVICES_ACTIVE_DATABASEW
) != 0)
281 return ERROR_INVALID_NAME
;
284 if (!(manager
= malloc(sizeof(*manager
))))
285 return ERROR_NOT_ENOUGH_SERVER_MEMORY
;
287 manager
->hdr
.type
= SC_HTYPE_MANAGER
;
289 if (dwAccessMask
& MAXIMUM_ALLOWED
)
290 dwAccessMask
|= SC_MANAGER_ALL_ACCESS
;
291 manager
->hdr
.access
= dwAccessMask
;
292 RtlMapGenericMask(&manager
->hdr
.access
, &g_scm_generic
);
293 manager
->db
= active_database
;
294 *handle
= &manager
->hdr
;
296 return ERROR_SUCCESS
;
299 static void SC_RPC_HANDLE_destroy(SC_RPC_HANDLE handle
)
301 struct sc_handle
*hdr
= handle
;
304 case SC_HTYPE_MANAGER
:
306 struct sc_manager_handle
*manager
= (struct sc_manager_handle
*)hdr
;
310 case SC_HTYPE_SERVICE
:
312 struct sc_service_handle
*service
= (struct sc_service_handle
*)hdr
;
313 service_lock(service
->service_entry
);
314 list_remove(&service
->entry
);
317 SetEvent(service
->notify
->event
);
318 sc_notify_release(service
->notify
);
320 service_unlock(service
->service_entry
);
321 release_service(service
->service_entry
);
326 WINE_ERR("invalid handle type %d\n", hdr
->type
);
327 RpcRaiseException(ERROR_INVALID_HANDLE
);
331 DWORD __cdecl
svcctl_GetServiceDisplayNameW(
332 SC_RPC_HANDLE hSCManager
,
333 LPCWSTR lpServiceName
,
337 struct sc_manager_handle
*manager
;
338 struct service_entry
*entry
;
341 WINE_TRACE("(%s, %ld)\n", wine_dbgstr_w(lpServiceName
), *cchBufSize
);
343 if ((err
= validate_scm_handle(hSCManager
, 0, &manager
)) != ERROR_SUCCESS
)
346 scmdatabase_lock(manager
->db
);
348 entry
= scmdatabase_find_service(manager
->db
, lpServiceName
);
353 name
= get_display_name(entry
);
354 len
= lstrlenW(name
);
355 if (len
<= *cchBufSize
)
358 memcpy(lpBuffer
, name
, (len
+ 1)*sizeof(*name
));
361 err
= ERROR_INSUFFICIENT_BUFFER
;
365 err
= ERROR_SERVICE_DOES_NOT_EXIST
;
367 scmdatabase_unlock(manager
->db
);
369 if (err
!= ERROR_SUCCESS
)
375 DWORD __cdecl
svcctl_GetServiceKeyNameW(
376 SC_RPC_HANDLE hSCManager
,
377 LPCWSTR lpServiceDisplayName
,
381 struct service_entry
*entry
;
382 struct sc_manager_handle
*manager
;
385 WINE_TRACE("(%s, %ld)\n", wine_dbgstr_w(lpServiceDisplayName
), *cchBufSize
);
387 if ((err
= validate_scm_handle(hSCManager
, 0, &manager
)) != ERROR_SUCCESS
)
390 scmdatabase_lock(manager
->db
);
392 entry
= scmdatabase_find_service_by_displayname(manager
->db
, lpServiceDisplayName
);
396 len
= lstrlenW(entry
->name
);
397 if (len
<= *cchBufSize
)
400 memcpy(lpBuffer
, entry
->name
, (len
+ 1)*sizeof(*entry
->name
));
403 err
= ERROR_INSUFFICIENT_BUFFER
;
407 err
= ERROR_SERVICE_DOES_NOT_EXIST
;
409 scmdatabase_unlock(manager
->db
);
411 if (err
!= ERROR_SUCCESS
)
417 static DWORD
create_handle_for_service(struct service_entry
*entry
, DWORD dwDesiredAccess
, SC_RPC_HANDLE
*phService
)
419 struct sc_service_handle
*service
;
421 if (!(service
= malloc(sizeof(*service
))))
423 release_service(entry
);
424 return ERROR_NOT_ENOUGH_SERVER_MEMORY
;
427 if (dwDesiredAccess
& MAXIMUM_ALLOWED
)
428 dwDesiredAccess
|= SERVICE_ALL_ACCESS
;
430 service
->hdr
.type
= SC_HTYPE_SERVICE
;
431 service
->hdr
.access
= dwDesiredAccess
;
432 service
->notify
= NULL
;
433 service
->status_notified
= FALSE
;
434 RtlMapGenericMask(&service
->hdr
.access
, &g_svc_generic
);
437 service
->service_entry
= entry
;
438 list_add_tail(&entry
->handles
, &service
->entry
);
439 service_unlock(entry
);
441 *phService
= &service
->hdr
;
442 return ERROR_SUCCESS
;
445 DWORD __cdecl
svcctl_OpenServiceW(
446 SC_RPC_HANDLE hSCManager
,
447 LPCWSTR lpServiceName
,
448 DWORD dwDesiredAccess
,
449 SC_RPC_HANDLE
*phService
)
451 struct sc_manager_handle
*manager
;
452 struct service_entry
*entry
;
455 WINE_TRACE("(%s, 0x%lx)\n", wine_dbgstr_w(lpServiceName
), dwDesiredAccess
);
457 if ((err
= validate_scm_handle(hSCManager
, 0, &manager
)) != ERROR_SUCCESS
)
459 if (!validate_service_name(lpServiceName
))
460 return ERROR_INVALID_NAME
;
462 scmdatabase_lock(manager
->db
);
463 entry
= grab_service(scmdatabase_find_service(manager
->db
, lpServiceName
));
464 scmdatabase_unlock(manager
->db
);
467 return ERROR_SERVICE_DOES_NOT_EXIST
;
469 return create_handle_for_service(entry
, dwDesiredAccess
, phService
);
472 static DWORD
parse_dependencies(const WCHAR
*dependencies
, struct service_entry
*entry
)
474 WCHAR
*services
= NULL
, *groups
, *s
;
475 DWORD len
, len_services
= 0, len_groups
= 0;
476 const WCHAR
*ptr
= dependencies
;
478 if (!dependencies
|| !dependencies
[0])
480 entry
->dependOnServices
= NULL
;
481 entry
->dependOnGroups
= NULL
;
482 return ERROR_SUCCESS
;
487 len
= lstrlenW(ptr
) + 1;
488 if (ptr
[0] == '+' && ptr
[1])
489 len_groups
+= len
- 1;
494 if (!len_services
) entry
->dependOnServices
= NULL
;
497 services
= malloc((len_services
+ 1) * sizeof(WCHAR
));
499 return ERROR_OUTOFMEMORY
;
505 len
= lstrlenW(ptr
) + 1;
514 entry
->dependOnServices
= services
;
516 if (!len_groups
) entry
->dependOnGroups
= NULL
;
519 groups
= malloc((len_groups
+ 1) * sizeof(WCHAR
));
523 return ERROR_OUTOFMEMORY
;
529 len
= lstrlenW(ptr
) + 1;
530 if (ptr
[0] == '+' && ptr
[1])
532 lstrcpyW(s
, ptr
+ 1);
538 entry
->dependOnGroups
= groups
;
541 return ERROR_SUCCESS
;
544 static DWORD
create_serviceW(
545 SC_RPC_HANDLE hSCManager
,
546 LPCWSTR lpServiceName
,
547 LPCWSTR lpDisplayName
,
548 DWORD dwDesiredAccess
,
551 DWORD dwErrorControl
,
552 LPCWSTR lpBinaryPathName
,
553 LPCWSTR lpLoadOrderGroup
,
555 const BYTE
*lpDependencies
,
556 DWORD dwDependenciesSize
,
557 LPCWSTR lpServiceStartName
,
558 const BYTE
*lpPassword
,
559 DWORD dwPasswordSize
,
560 SC_RPC_HANDLE
*phService
,
563 struct service_entry
*entry
, *found
;
564 struct sc_manager_handle
*manager
;
567 WINE_TRACE("(%s, %s, 0x%lx, %s)\n", wine_dbgstr_w(lpServiceName
), wine_dbgstr_w(lpDisplayName
), dwDesiredAccess
, wine_dbgstr_w(lpBinaryPathName
));
569 if ((err
= validate_scm_handle(hSCManager
, SC_MANAGER_CREATE_SERVICE
, &manager
)) != ERROR_SUCCESS
)
572 if (!validate_service_name(lpServiceName
))
573 return ERROR_INVALID_NAME
;
574 if (!check_multisz((LPCWSTR
)lpDependencies
, dwDependenciesSize
) || !lpServiceName
[0] || !lpBinaryPathName
[0])
575 return ERROR_INVALID_PARAMETER
;
578 WINE_FIXME("Don't know how to add a password\n"); /* I always get ERROR_GEN_FAILURE */
580 err
= service_create(lpServiceName
, &entry
);
581 if (err
!= ERROR_SUCCESS
)
584 err
= parse_dependencies((LPCWSTR
)lpDependencies
, entry
);
585 if (err
!= ERROR_SUCCESS
) {
586 free_service_entry(entry
);
590 entry
->is_wow64
= is_wow64
;
591 entry
->config
.dwServiceType
= entry
->status
.dwServiceType
= dwServiceType
;
592 entry
->config
.dwStartType
= dwStartType
;
593 entry
->config
.dwErrorControl
= dwErrorControl
;
594 entry
->config
.lpBinaryPathName
= wcsdup(lpBinaryPathName
);
595 entry
->config
.lpLoadOrderGroup
= wcsdup(lpLoadOrderGroup
);
596 entry
->config
.lpServiceStartName
= wcsdup(lpServiceStartName
);
597 entry
->config
.lpDisplayName
= wcsdup(lpDisplayName
);
599 if (lpdwTagId
) /* TODO: In most situations a non-NULL TagId will generate an ERROR_INVALID_PARAMETER. */
600 entry
->config
.dwTagId
= *lpdwTagId
;
602 entry
->config
.dwTagId
= 0;
604 /* other fields NULL*/
606 if (!validate_service_config(entry
))
608 WINE_ERR("Invalid data while trying to create service\n");
609 free_service_entry(entry
);
610 return ERROR_INVALID_PARAMETER
;
613 scmdatabase_lock(manager
->db
);
615 if ((found
= scmdatabase_find_service(manager
->db
, lpServiceName
)))
617 err
= is_marked_for_delete(found
) ? ERROR_SERVICE_MARKED_FOR_DELETE
: ERROR_SERVICE_EXISTS
;
618 scmdatabase_unlock(manager
->db
);
619 free_service_entry(entry
);
623 if (scmdatabase_find_service_by_displayname(manager
->db
, get_display_name(entry
)))
625 scmdatabase_unlock(manager
->db
);
626 free_service_entry(entry
);
627 return ERROR_DUPLICATE_SERVICE_NAME
;
630 err
= scmdatabase_add_service(manager
->db
, entry
);
631 if (err
!= ERROR_SUCCESS
)
633 scmdatabase_unlock(manager
->db
);
634 free_service_entry(entry
);
637 scmdatabase_unlock(manager
->db
);
639 return create_handle_for_service(entry
, dwDesiredAccess
, phService
);
642 DWORD __cdecl
svcctl_CreateServiceW(
643 SC_RPC_HANDLE hSCManager
,
644 LPCWSTR lpServiceName
,
645 LPCWSTR lpDisplayName
,
646 DWORD dwDesiredAccess
,
649 DWORD dwErrorControl
,
650 LPCWSTR lpBinaryPathName
,
651 LPCWSTR lpLoadOrderGroup
,
653 const BYTE
*lpDependencies
,
654 DWORD dwDependenciesSize
,
655 LPCWSTR lpServiceStartName
,
656 const BYTE
*lpPassword
,
657 DWORD dwPasswordSize
,
658 SC_RPC_HANDLE
*phService
)
660 WINE_TRACE("(%s, %s, 0x%lx, %s)\n", wine_dbgstr_w(lpServiceName
), wine_dbgstr_w(lpDisplayName
), dwDesiredAccess
, wine_dbgstr_w(lpBinaryPathName
));
661 return create_serviceW(hSCManager
, lpServiceName
, lpDisplayName
, dwDesiredAccess
, dwServiceType
, dwStartType
,
662 dwErrorControl
, lpBinaryPathName
, lpLoadOrderGroup
, lpdwTagId
, lpDependencies
, dwDependenciesSize
, lpServiceStartName
,
663 lpPassword
, dwPasswordSize
, phService
, FALSE
);
666 DWORD __cdecl
svcctl_DeleteService(
667 SC_RPC_HANDLE hService
)
669 struct sc_service_handle
*service
;
672 if ((err
= validate_service_handle(hService
, DELETE
, &service
)) != ERROR_SUCCESS
)
675 service_lock(service
->service_entry
);
677 if (!is_marked_for_delete(service
->service_entry
))
678 err
= mark_for_delete(service
->service_entry
);
680 err
= ERROR_SERVICE_MARKED_FOR_DELETE
;
682 service_unlock(service
->service_entry
);
687 DWORD __cdecl
svcctl_QueryServiceConfigW(
688 SC_RPC_HANDLE hService
,
689 QUERY_SERVICE_CONFIGW
*config
,
693 struct sc_service_handle
*service
;
696 WINE_TRACE("(%p)\n", config
);
698 if ((err
= validate_service_handle(hService
, SERVICE_QUERY_CONFIG
, &service
)) != 0)
701 service_lock(service
->service_entry
);
702 config
->dwServiceType
= service
->service_entry
->config
.dwServiceType
;
703 config
->dwStartType
= service
->service_entry
->config
.dwStartType
;
704 config
->dwErrorControl
= service
->service_entry
->config
.dwErrorControl
;
705 config
->lpBinaryPathName
= wcsdup(service
->service_entry
->config
.lpBinaryPathName
);
706 config
->lpLoadOrderGroup
= wcsdup(service
->service_entry
->config
.lpLoadOrderGroup
);
707 config
->dwTagId
= service
->service_entry
->config
.dwTagId
;
708 config
->lpDependencies
= NULL
; /* TODO */
709 config
->lpServiceStartName
= wcsdup(service
->service_entry
->config
.lpServiceStartName
);
710 config
->lpDisplayName
= wcsdup(service
->service_entry
->config
.lpDisplayName
);
711 service_unlock(service
->service_entry
);
713 return ERROR_SUCCESS
;
716 DWORD __cdecl
svcctl_ChangeServiceConfigW(
717 SC_RPC_HANDLE hService
,
720 DWORD dwErrorControl
,
721 LPCWSTR lpBinaryPathName
,
722 LPCWSTR lpLoadOrderGroup
,
724 const BYTE
*lpDependencies
,
725 DWORD dwDependenciesSize
,
726 LPCWSTR lpServiceStartName
,
727 const BYTE
*lpPassword
,
728 DWORD dwPasswordSize
,
729 LPCWSTR lpDisplayName
)
731 struct service_entry new_entry
, *entry
;
732 struct sc_service_handle
*service
;
737 if ((err
= validate_service_handle(hService
, SERVICE_CHANGE_CONFIG
, &service
)) != 0)
740 if (!check_multisz((LPCWSTR
)lpDependencies
, dwDependenciesSize
))
741 return ERROR_INVALID_PARAMETER
;
743 /* first check if the new configuration is correct */
744 service_lock(service
->service_entry
);
746 if (is_marked_for_delete(service
->service_entry
))
748 service_unlock(service
->service_entry
);
749 return ERROR_SERVICE_MARKED_FOR_DELETE
;
752 if (lpDisplayName
!= NULL
&&
753 (entry
= scmdatabase_find_service_by_displayname(service
->service_entry
->db
, lpDisplayName
)) &&
754 (entry
!= service
->service_entry
))
756 service_unlock(service
->service_entry
);
757 return ERROR_DUPLICATE_SERVICE_NAME
;
760 new_entry
= *service
->service_entry
;
762 if (dwServiceType
!= SERVICE_NO_CHANGE
)
763 new_entry
.config
.dwServiceType
= dwServiceType
;
765 if (dwStartType
!= SERVICE_NO_CHANGE
)
766 new_entry
.config
.dwStartType
= dwStartType
;
768 if (dwErrorControl
!= SERVICE_NO_CHANGE
)
769 new_entry
.config
.dwErrorControl
= dwErrorControl
;
771 if (lpBinaryPathName
!= NULL
)
772 new_entry
.config
.lpBinaryPathName
= (LPWSTR
)lpBinaryPathName
;
774 if (lpLoadOrderGroup
!= NULL
)
775 new_entry
.config
.lpLoadOrderGroup
= (LPWSTR
)lpLoadOrderGroup
;
777 if (lpdwTagId
!= NULL
)
778 WINE_FIXME("Changing tag id not supported\n");
780 if (lpServiceStartName
!= NULL
)
781 new_entry
.config
.lpServiceStartName
= (LPWSTR
)lpServiceStartName
;
783 if (lpPassword
!= NULL
)
784 WINE_FIXME("Setting password not supported\n");
786 if (lpDisplayName
!= NULL
)
787 new_entry
.config
.lpDisplayName
= (LPWSTR
)lpDisplayName
;
789 err
= parse_dependencies((LPCWSTR
)lpDependencies
, &new_entry
);
790 if (err
!= ERROR_SUCCESS
)
792 service_unlock(service
->service_entry
);
796 if (!validate_service_config(&new_entry
))
798 WINE_ERR("The configuration after the change wouldn't be valid\n");
799 service_unlock(service
->service_entry
);
800 return ERROR_INVALID_PARAMETER
;
803 /* configuration OK. The strings needs to be duplicated */
804 if (lpBinaryPathName
!= NULL
)
805 new_entry
.config
.lpBinaryPathName
= wcsdup(lpBinaryPathName
);
807 if (lpLoadOrderGroup
!= NULL
)
808 new_entry
.config
.lpLoadOrderGroup
= wcsdup(lpLoadOrderGroup
);
810 if (lpServiceStartName
!= NULL
)
811 new_entry
.config
.lpServiceStartName
= wcsdup(lpServiceStartName
);
813 if (lpDisplayName
!= NULL
)
814 new_entry
.config
.lpDisplayName
= wcsdup(lpDisplayName
);
816 /* try to save to Registry, commit or rollback depending on success */
817 err
= save_service_config(&new_entry
);
818 if (ERROR_SUCCESS
== err
)
820 free_service_strings(service
->service_entry
, &new_entry
);
821 *service
->service_entry
= new_entry
;
823 else free_service_strings(&new_entry
, service
->service_entry
);
824 service_unlock(service
->service_entry
);
829 static void fill_status_process(SERVICE_STATUS_PROCESS
*status
, struct service_entry
*service
)
831 struct process_entry
*process
= service
->process
;
832 memcpy(status
, &service
->status
, sizeof(service
->status
));
833 status
->dwProcessId
= 0;
834 if (process
&& !(service
->status
.dwServiceType
& SERVICE_DRIVER
))
835 status
->dwProcessId
= process
->process_id
;
836 status
->dwServiceFlags
= 0;
839 static void fill_notify(struct sc_notify_handle
*notify
, struct service_entry
*service
)
841 SC_RPC_NOTIFY_PARAMS_LIST
*list
;
842 SERVICE_NOTIFY_STATUS_CHANGE_PARAMS_2
*cparams
;
844 list
= calloc(1, sizeof(SC_RPC_NOTIFY_PARAMS_LIST
));
847 if (!(cparams
= calloc(1, sizeof(SERVICE_NOTIFY_STATUS_CHANGE_PARAMS_2
))))
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].params
= cparams
;
864 InterlockedExchangePointer((void**)¬ify
->params_list
, list
);
866 SetEvent(notify
->event
);
869 void notify_service_state(struct service_entry
*service
)
871 struct sc_service_handle
*service_handle
;
874 mask
= 1 << (service
->status
.dwCurrentState
- SERVICE_STOPPED
);
875 LIST_FOR_EACH_ENTRY(service_handle
, &service
->handles
, struct sc_service_handle
, entry
)
877 struct sc_notify_handle
*notify
= service_handle
->notify
;
878 if (notify
&& (notify
->notify_mask
& mask
))
880 fill_notify(notify
, service
);
881 sc_notify_release(notify
);
882 service_handle
->notify
= NULL
;
883 service_handle
->status_notified
= TRUE
;
886 service_handle
->status_notified
= FALSE
;
890 DWORD __cdecl
svcctl_SetServiceStatus(SC_RPC_HANDLE handle
, SERVICE_STATUS
*status
)
892 struct sc_service_handle
*service
;
893 struct process_entry
*process
;
896 WINE_TRACE("(%p, %p)\n", handle
, status
);
898 if ((err
= validate_service_handle(handle
, SERVICE_SET_STATUS
, &service
)) != 0)
901 service_lock(service
->service_entry
);
903 /* FIXME: be a bit more discriminant about what parts of the status we set
904 * and check that fields are valid */
905 service
->service_entry
->status
.dwCurrentState
= status
->dwCurrentState
;
906 service
->service_entry
->status
.dwControlsAccepted
= status
->dwControlsAccepted
;
907 service
->service_entry
->status
.dwWin32ExitCode
= status
->dwWin32ExitCode
;
908 service
->service_entry
->status
.dwServiceSpecificExitCode
= status
->dwServiceSpecificExitCode
;
909 service
->service_entry
->status
.dwCheckPoint
= status
->dwCheckPoint
;
910 service
->service_entry
->status
.dwWaitHint
= status
->dwWaitHint
;
911 SetEvent(service
->service_entry
->status_changed_event
);
913 if ((process
= service
->service_entry
->process
) &&
914 status
->dwCurrentState
== SERVICE_STOPPED
)
916 service
->service_entry
->process
= NULL
;
917 if (!--process
->use_count
)
918 terminate_after_timeout(process
, service_kill_timeout
);
919 if (service
->service_entry
->shared_process
&& process
->use_count
<= 1)
920 shutdown_shared_process(process
);
921 release_process(process
);
924 notify_service_state(service
->service_entry
);
925 service_unlock(service
->service_entry
);
927 return ERROR_SUCCESS
;
930 DWORD __cdecl
svcctl_ChangeServiceConfig2W( SC_RPC_HANDLE hService
, SC_RPC_CONFIG_INFOW config
)
932 struct sc_service_handle
*service
;
935 if ((err
= validate_service_handle(hService
, SERVICE_CHANGE_CONFIG
, &service
)) != 0)
938 switch (config
.dwInfoLevel
)
940 case SERVICE_CONFIG_DESCRIPTION
:
944 if (!config
.descr
->lpDescription
)
947 if (config
.descr
->lpDescription
[0])
949 if (!(descr
= wcsdup( config
.descr
->lpDescription
)))
950 return ERROR_NOT_ENOUGH_MEMORY
;
953 WINE_TRACE( "changing service %p descr to %s\n", service
, wine_dbgstr_w(descr
) );
954 service_lock( service
->service_entry
);
955 free( service
->service_entry
->description
);
956 service
->service_entry
->description
= descr
;
957 save_service_config( service
->service_entry
);
958 service_unlock( service
->service_entry
);
961 case SERVICE_CONFIG_FAILURE_ACTIONS
:
962 WINE_FIXME( "SERVICE_CONFIG_FAILURE_ACTIONS not implemented: period %lu msg %s cmd %s\n",
963 config
.actions
->dwResetPeriod
,
964 wine_dbgstr_w(config
.actions
->lpRebootMsg
),
965 wine_dbgstr_w(config
.actions
->lpCommand
) );
967 case SERVICE_CONFIG_PRESHUTDOWN_INFO
:
968 WINE_TRACE( "changing service %p preshutdown timeout to %ld\n",
969 service
, config
.preshutdown
->dwPreshutdownTimeout
);
970 service_lock( service
->service_entry
);
971 service
->service_entry
->preshutdown_timeout
= config
.preshutdown
->dwPreshutdownTimeout
;
972 save_service_config( service
->service_entry
);
973 service_unlock( service
->service_entry
);
976 WINE_FIXME("level %lu not implemented\n", config
.dwInfoLevel
);
977 err
= ERROR_INVALID_LEVEL
;
983 DWORD __cdecl
svcctl_QueryServiceConfig2W( SC_RPC_HANDLE hService
, DWORD level
,
984 BYTE
*buffer
, DWORD size
, LPDWORD needed
)
986 struct sc_service_handle
*service
;
989 memset(buffer
, 0, size
);
991 if ((err
= validate_service_handle(hService
, SERVICE_QUERY_CONFIG
, &service
)) != 0)
996 case SERVICE_CONFIG_DESCRIPTION
:
998 struct service_description
*desc
= (struct service_description
*)buffer
;
999 DWORD total_size
= sizeof(*desc
);
1001 service_lock(service
->service_entry
);
1002 if (service
->service_entry
->description
)
1003 total_size
+= lstrlenW(service
->service_entry
->description
) * sizeof(WCHAR
);
1005 *needed
= total_size
;
1006 if (size
>= total_size
)
1008 if (service
->service_entry
->description
)
1010 lstrcpyW( desc
->description
, service
->service_entry
->description
);
1011 desc
->size
= total_size
- FIELD_OFFSET(struct service_description
, description
);
1015 desc
->description
[0] = 0;
1019 else err
= ERROR_INSUFFICIENT_BUFFER
;
1020 service_unlock(service
->service_entry
);
1024 case SERVICE_CONFIG_PRESHUTDOWN_INFO
:
1025 service_lock(service
->service_entry
);
1027 *needed
= sizeof(SERVICE_PRESHUTDOWN_INFO
);
1028 if (size
>= *needed
)
1029 ((LPSERVICE_PRESHUTDOWN_INFO
)buffer
)->dwPreshutdownTimeout
=
1030 service
->service_entry
->preshutdown_timeout
;
1031 else err
= ERROR_INSUFFICIENT_BUFFER
;
1033 service_unlock(service
->service_entry
);
1037 WINE_FIXME("level %lu not implemented\n", level
);
1038 err
= ERROR_INVALID_LEVEL
;
1044 DWORD __cdecl
svcctl_QueryServiceStatusEx(
1045 SC_RPC_HANDLE hService
,
1046 SC_STATUS_TYPE InfoLevel
,
1049 LPDWORD pcbBytesNeeded
)
1051 struct sc_service_handle
*service
;
1053 LPSERVICE_STATUS_PROCESS pSvcStatusData
;
1055 memset(lpBuffer
, 0, cbBufSize
);
1057 if ((err
= validate_service_handle(hService
, SERVICE_QUERY_STATUS
, &service
)) != 0)
1060 if (InfoLevel
!= SC_STATUS_PROCESS_INFO
)
1061 return ERROR_INVALID_LEVEL
;
1063 pSvcStatusData
= (LPSERVICE_STATUS_PROCESS
) lpBuffer
;
1064 if (pSvcStatusData
== NULL
)
1065 return ERROR_INVALID_PARAMETER
;
1067 if (cbBufSize
< sizeof(SERVICE_STATUS_PROCESS
))
1069 if( pcbBytesNeeded
!= NULL
)
1070 *pcbBytesNeeded
= sizeof(SERVICE_STATUS_PROCESS
);
1072 return ERROR_INSUFFICIENT_BUFFER
;
1075 service_lock(service
->service_entry
);
1076 fill_status_process(pSvcStatusData
, service
->service_entry
);
1077 service_unlock(service
->service_entry
);
1079 return ERROR_SUCCESS
;
1082 /******************************************************************************
1083 * service_accepts_control
1085 static BOOL
service_accepts_control(const struct service_entry
*service
, DWORD dwControl
)
1087 DWORD a
= service
->status
.dwControlsAccepted
;
1089 if (dwControl
>= 128 && dwControl
<= 255)
1094 case SERVICE_CONTROL_INTERROGATE
:
1096 case SERVICE_CONTROL_STOP
:
1097 if (a
&SERVICE_ACCEPT_STOP
)
1100 case SERVICE_CONTROL_SHUTDOWN
:
1101 if (a
&SERVICE_ACCEPT_SHUTDOWN
)
1104 case SERVICE_CONTROL_PAUSE
:
1105 case SERVICE_CONTROL_CONTINUE
:
1106 if (a
&SERVICE_ACCEPT_PAUSE_CONTINUE
)
1109 case SERVICE_CONTROL_PARAMCHANGE
:
1110 if (a
&SERVICE_ACCEPT_PARAMCHANGE
)
1113 case SERVICE_CONTROL_NETBINDADD
:
1114 case SERVICE_CONTROL_NETBINDREMOVE
:
1115 case SERVICE_CONTROL_NETBINDENABLE
:
1116 case SERVICE_CONTROL_NETBINDDISABLE
:
1117 if (a
&SERVICE_ACCEPT_NETBINDCHANGE
)
1119 case SERVICE_CONTROL_HARDWAREPROFILECHANGE
:
1120 if (a
&SERVICE_ACCEPT_HARDWAREPROFILECHANGE
)
1123 case SERVICE_CONTROL_POWEREVENT
:
1124 if (a
&SERVICE_ACCEPT_POWEREVENT
)
1127 case SERVICE_CONTROL_SESSIONCHANGE
:
1128 if (a
&SERVICE_ACCEPT_SESSIONCHANGE
)
1135 /******************************************************************************
1136 * process_send_command
1138 static BOOL
process_send_command(struct process_entry
*process
, const void *data
, DWORD size
, DWORD
*result
)
1140 OVERLAPPED overlapped
;
1144 overlapped
.Offset
= 0;
1145 overlapped
.OffsetHigh
= 0;
1146 overlapped
.hEvent
= process
->overlapped_event
;
1147 r
= WriteFile(process
->control_pipe
, data
, size
, &count
, &overlapped
);
1148 if (!r
&& GetLastError() == ERROR_IO_PENDING
)
1150 ret
= WaitForSingleObject(process
->overlapped_event
, service_pipe_timeout
);
1151 if (ret
== WAIT_TIMEOUT
)
1153 WINE_ERR("sending command timed out\n");
1154 *result
= ERROR_SERVICE_REQUEST_TIMEOUT
;
1157 r
= GetOverlappedResult(process
->control_pipe
, &overlapped
, &count
, FALSE
);
1159 if (!r
|| count
!= size
)
1161 WINE_ERR("service protocol error - failed to write pipe!\n");
1162 *result
= (!r
? GetLastError() : ERROR_WRITE_FAULT
);
1165 r
= ReadFile(process
->control_pipe
, result
, sizeof *result
, &count
, &overlapped
);
1166 if (!r
&& GetLastError() == ERROR_IO_PENDING
)
1168 ret
= WaitForSingleObject(process
->overlapped_event
, service_pipe_timeout
);
1169 if (ret
== WAIT_TIMEOUT
)
1171 WINE_ERR("receiving command result timed out\n");
1172 *result
= ERROR_SERVICE_REQUEST_TIMEOUT
;
1175 r
= GetOverlappedResult(process
->control_pipe
, &overlapped
, &count
, FALSE
);
1177 if (!r
|| count
!= sizeof *result
)
1179 WINE_ERR("service protocol error - failed to read pipe "
1180 "r = %d count = %ld!\n", r
, count
);
1181 *result
= (!r
? GetLastError() : ERROR_READ_FAULT
);
1188 /******************************************************************************
1189 * process_send_control
1191 BOOL
process_send_control(struct process_entry
*process
, BOOL shared_process
, const WCHAR
*name
,
1192 DWORD control
, const BYTE
*data
, DWORD data_size
, DWORD
*result
)
1194 service_start_info
*ssi
;
1200 control
|= SERVICE_CONTROL_FORWARD_FLAG
;
1201 data
= (BYTE
*)name
;
1202 data_size
= (lstrlenW(name
) + 1) * sizeof(WCHAR
);
1206 /* calculate how much space we need to send the startup info */
1207 len
= (lstrlenW(name
) + 1) * sizeof(WCHAR
) + data_size
;
1209 ssi
= malloc(FIELD_OFFSET(service_start_info
, data
[len
]));
1210 ssi
->magic
= SERVICE_PROTOCOL_MAGIC
;
1211 ssi
->control
= control
;
1212 ssi
->total_size
= FIELD_OFFSET(service_start_info
, data
[len
]);
1213 ssi
->name_size
= lstrlenW(name
) + 1;
1214 lstrcpyW((WCHAR
*)ssi
->data
, name
);
1215 if (data_size
) memcpy(&ssi
->data
[ssi
->name_size
* sizeof(WCHAR
)], data
, data_size
);
1217 r
= process_send_command(process
, ssi
, ssi
->total_size
, result
);
1222 DWORD __cdecl
svcctl_StartServiceW(
1223 SC_RPC_HANDLE hService
,
1224 DWORD dwNumServiceArgs
,
1225 LPCWSTR
*lpServiceArgVectors
)
1227 struct sc_service_handle
*service
;
1230 WINE_TRACE("(%p, %ld, %p)\n", hService
, dwNumServiceArgs
, lpServiceArgVectors
);
1232 if ((err
= validate_service_handle(hService
, SERVICE_START
, &service
)) != 0)
1235 if (service
->service_entry
->config
.dwStartType
== SERVICE_DISABLED
)
1236 return ERROR_SERVICE_DISABLED
;
1238 if (!scmdatabase_lock_startup(service
->service_entry
->db
, 3000))
1239 return ERROR_SERVICE_DATABASE_LOCKED
;
1241 err
= service_start(service
->service_entry
, dwNumServiceArgs
, lpServiceArgVectors
);
1243 scmdatabase_unlock_startup(service
->service_entry
->db
);
1247 DWORD __cdecl
svcctl_ControlService(
1248 SC_RPC_HANDLE hService
,
1250 SERVICE_STATUS
*lpServiceStatus
)
1252 DWORD access_required
;
1253 struct sc_service_handle
*service
;
1254 struct process_entry
*process
;
1255 BOOL shared_process
;
1258 WINE_TRACE("(%p, %ld, %p)\n", hService
, dwControl
, lpServiceStatus
);
1262 case SERVICE_CONTROL_CONTINUE
:
1263 case SERVICE_CONTROL_NETBINDADD
:
1264 case SERVICE_CONTROL_NETBINDDISABLE
:
1265 case SERVICE_CONTROL_NETBINDENABLE
:
1266 case SERVICE_CONTROL_NETBINDREMOVE
:
1267 case SERVICE_CONTROL_PARAMCHANGE
:
1268 case SERVICE_CONTROL_PAUSE
:
1269 access_required
= SERVICE_PAUSE_CONTINUE
;
1271 case SERVICE_CONTROL_INTERROGATE
:
1272 access_required
= SERVICE_INTERROGATE
;
1274 case SERVICE_CONTROL_STOP
:
1275 access_required
= SERVICE_STOP
;
1278 if (dwControl
>= 128 && dwControl
<= 255)
1279 access_required
= SERVICE_USER_DEFINED_CONTROL
;
1281 return ERROR_INVALID_PARAMETER
;
1284 if ((result
= validate_service_handle(hService
, access_required
, &service
)) != 0)
1287 service_lock(service
->service_entry
);
1289 result
= ERROR_SUCCESS
;
1290 switch (service
->service_entry
->status
.dwCurrentState
)
1292 case SERVICE_STOPPED
:
1293 result
= ERROR_SERVICE_NOT_ACTIVE
;
1295 case SERVICE_START_PENDING
:
1296 if (dwControl
==SERVICE_CONTROL_STOP
)
1299 case SERVICE_STOP_PENDING
:
1300 result
= ERROR_SERVICE_CANNOT_ACCEPT_CTRL
;
1304 if (result
== ERROR_SUCCESS
&& service
->service_entry
->force_shutdown
)
1306 result
= ERROR_SERVICE_CANNOT_ACCEPT_CTRL
;
1307 if ((process
= service
->service_entry
->process
))
1309 service
->service_entry
->process
= NULL
;
1310 if (!--process
->use_count
) process_terminate(process
);
1311 release_process(process
);
1315 if (result
!= ERROR_SUCCESS
)
1317 if (lpServiceStatus
) *lpServiceStatus
= service
->service_entry
->status
;
1318 service_unlock(service
->service_entry
);
1322 if (!service_accepts_control(service
->service_entry
, dwControl
))
1324 service_unlock(service
->service_entry
);
1325 return ERROR_INVALID_SERVICE_CONTROL
;
1328 /* Remember that we tried to shutdown this service. When the service is
1329 * still running on the second invocation, it will be forcefully killed. */
1330 if (dwControl
== SERVICE_CONTROL_STOP
)
1331 service
->service_entry
->force_shutdown
= TRUE
;
1333 /* Hold a reference to the process while sending the command. */
1334 process
= grab_process(service
->service_entry
->process
);
1335 shared_process
= service
->service_entry
->shared_process
;
1336 service_unlock(service
->service_entry
);
1339 return ERROR_SERVICE_CANNOT_ACCEPT_CTRL
;
1341 result
= WaitForSingleObject(process
->control_mutex
, 30000);
1342 if (result
!= WAIT_OBJECT_0
)
1344 release_process(process
);
1345 return ERROR_SERVICE_REQUEST_TIMEOUT
;
1348 if (process_send_control(process
, shared_process
, service
->service_entry
->name
,
1349 dwControl
, NULL
, 0, &result
))
1350 result
= ERROR_SUCCESS
;
1352 if (lpServiceStatus
)
1354 service_lock(service
->service_entry
);
1355 *lpServiceStatus
= service
->service_entry
->status
;
1356 service_unlock(service
->service_entry
);
1359 ReleaseMutex(process
->control_mutex
);
1360 release_process(process
);
1364 DWORD __cdecl
svcctl_CloseServiceHandle(
1365 SC_RPC_HANDLE
*handle
)
1367 WINE_TRACE("(&%p)\n", *handle
);
1369 SC_RPC_HANDLE_destroy(*handle
);
1372 return ERROR_SUCCESS
;
1375 void __RPC_USER
SC_RPC_LOCK_rundown(SC_RPC_LOCK hLock
)
1379 DWORD __cdecl
svcctl_LockServiceDatabase(SC_RPC_HANDLE manager
, SC_RPC_LOCK
*lock
)
1381 TRACE("(%p, %p)\n", manager
, lock
);
1383 *lock
= (SC_RPC_LOCK
)0xdeadbeef;
1384 return ERROR_SUCCESS
;
1387 DWORD __cdecl
svcctl_UnlockServiceDatabase(SC_RPC_LOCK
*lock
)
1389 TRACE("(&%p)\n", *lock
);
1392 return ERROR_SUCCESS
;
1395 static BOOL
map_state(DWORD state
, DWORD mask
)
1399 case SERVICE_START_PENDING
:
1400 case SERVICE_STOP_PENDING
:
1401 case SERVICE_RUNNING
:
1402 case SERVICE_CONTINUE_PENDING
:
1403 case SERVICE_PAUSE_PENDING
:
1404 case SERVICE_PAUSED
:
1405 if (SERVICE_ACTIVE
& mask
) return TRUE
;
1407 case SERVICE_STOPPED
:
1408 if (SERVICE_INACTIVE
& mask
) return TRUE
;
1411 WINE_ERR("unknown state %lu\n", state
);
1417 DWORD __cdecl
svcctl_EnumServicesStatusW(
1418 SC_RPC_HANDLE hmngr
,
1427 DWORD err
, sz
, total_size
, num_services
, offset
;
1428 struct sc_manager_handle
*manager
;
1429 struct service_entry
*service
;
1430 struct enum_service_status
*s
;
1432 WINE_TRACE("(%p, 0x%lx, 0x%lx, %p, %lu, %p, %p, %p)\n", hmngr
, type
, state
, buffer
, size
, needed
, returned
, resume
);
1434 if (!type
|| !state
)
1435 return ERROR_INVALID_PARAMETER
;
1437 if ((err
= validate_scm_handle(hmngr
, SC_MANAGER_ENUMERATE_SERVICE
, &manager
)) != ERROR_SUCCESS
)
1441 WINE_FIXME("resume index not supported\n");
1443 scmdatabase_lock(manager
->db
);
1445 total_size
= num_services
= 0;
1446 LIST_FOR_EACH_ENTRY(service
, &manager
->db
->services
, struct service_entry
, entry
)
1448 if ((service
->status
.dwServiceType
& type
) && map_state(service
->status
.dwCurrentState
, state
))
1450 total_size
+= sizeof(*s
);
1451 total_size
+= (lstrlenW(service
->name
) + 1) * sizeof(WCHAR
);
1452 if (service
->config
.lpDisplayName
)
1454 total_size
+= (lstrlenW(service
->config
.lpDisplayName
) + 1) * sizeof(WCHAR
);
1460 *needed
= total_size
;
1461 if (total_size
> size
)
1463 scmdatabase_unlock(manager
->db
);
1464 return ERROR_MORE_DATA
;
1466 s
= (struct enum_service_status
*)buffer
;
1467 offset
= num_services
* sizeof(struct enum_service_status
);
1468 LIST_FOR_EACH_ENTRY(service
, &manager
->db
->services
, struct service_entry
, entry
)
1470 if ((service
->status
.dwServiceType
& type
) && map_state(service
->status
.dwCurrentState
, state
))
1472 sz
= (lstrlenW(service
->name
) + 1) * sizeof(WCHAR
);
1473 memcpy(buffer
+ offset
, service
->name
, sz
);
1474 s
->service_name
= offset
;
1477 if (!service
->config
.lpDisplayName
) s
->display_name
= 0;
1480 sz
= (lstrlenW(service
->config
.lpDisplayName
) + 1) * sizeof(WCHAR
);
1481 memcpy(buffer
+ offset
, service
->config
.lpDisplayName
, sz
);
1482 s
->display_name
= offset
;
1485 s
->service_status
= service
->status
;
1489 *returned
= num_services
;
1491 scmdatabase_unlock(manager
->db
);
1492 return ERROR_SUCCESS
;
1495 static struct service_entry
*find_service_by_group(struct scmdatabase
*db
, const WCHAR
*group
)
1497 struct service_entry
*service
;
1498 LIST_FOR_EACH_ENTRY(service
, &db
->services
, struct service_entry
, entry
)
1500 if (service
->config
.lpLoadOrderGroup
&& !wcsicmp(group
, service
->config
.lpLoadOrderGroup
))
1506 static BOOL
match_group(const WCHAR
*g1
, const WCHAR
*g2
)
1508 if (!g2
) return TRUE
;
1509 if (!g2
[0] && (!g1
|| !g1
[0])) return TRUE
;
1510 if (g1
&& !lstrcmpW(g1
, g2
)) return TRUE
;
1514 DWORD __cdecl
svcctl_EnumServicesStatusExA(
1515 SC_RPC_HANDLE scmanager
,
1516 SC_ENUM_TYPE info_level
,
1518 DWORD service_state
,
1522 DWORD
*services_count
,
1523 DWORD
*resume_index
,
1527 return ERROR_CALL_NOT_IMPLEMENTED
;
1530 DWORD __cdecl
svcctl_EnumServicesStatusExW(
1531 SC_RPC_HANDLE hmngr
,
1532 SC_ENUM_TYPE info_level
,
1539 DWORD
*resume_handle
,
1542 DWORD err
, sz
, total_size
, num_services
;
1544 struct sc_manager_handle
*manager
;
1545 struct service_entry
*service
;
1546 struct enum_service_status_process
*s
;
1548 WINE_TRACE("(%p, 0x%lx, 0x%lx, %p, %lu, %p, %p, %s)\n", hmngr
, type
, state
, buffer
, size
,
1549 needed
, returned
, wine_dbgstr_w(group
));
1552 FIXME("resume handle not supported\n");
1554 if (!type
|| !state
)
1555 return ERROR_INVALID_PARAMETER
;
1557 if ((err
= validate_scm_handle(hmngr
, SC_MANAGER_ENUMERATE_SERVICE
, &manager
)) != ERROR_SUCCESS
)
1560 scmdatabase_lock(manager
->db
);
1562 if (group
&& !find_service_by_group(manager
->db
, group
))
1564 scmdatabase_unlock(manager
->db
);
1565 return ERROR_SERVICE_DOES_NOT_EXIST
;
1568 total_size
= num_services
= 0;
1569 LIST_FOR_EACH_ENTRY(service
, &manager
->db
->services
, struct service_entry
, entry
)
1571 if ((service
->status
.dwServiceType
& type
) && map_state(service
->status
.dwCurrentState
, state
)
1572 && match_group(service
->config
.lpLoadOrderGroup
, group
))
1574 total_size
+= sizeof(*s
);
1575 total_size
+= (lstrlenW(service
->name
) + 1) * sizeof(WCHAR
);
1576 if (service
->config
.lpDisplayName
)
1578 total_size
+= (lstrlenW(service
->config
.lpDisplayName
) + 1) * sizeof(WCHAR
);
1584 *needed
= total_size
;
1585 if (total_size
> size
)
1587 scmdatabase_unlock(manager
->db
);
1588 return ERROR_MORE_DATA
;
1590 s
= (struct enum_service_status_process
*)buffer
;
1591 offset
= num_services
* sizeof(*s
);
1592 LIST_FOR_EACH_ENTRY(service
, &manager
->db
->services
, struct service_entry
, entry
)
1594 if ((service
->status
.dwServiceType
& type
) && map_state(service
->status
.dwCurrentState
, state
)
1595 && match_group(service
->config
.lpLoadOrderGroup
, group
))
1597 sz
= (lstrlenW(service
->name
) + 1) * sizeof(WCHAR
);
1598 memcpy(buffer
+ offset
, service
->name
, sz
);
1599 s
->service_name
= offset
;
1602 if (!service
->config
.lpDisplayName
) s
->display_name
= 0;
1605 sz
= (lstrlenW(service
->config
.lpDisplayName
) + 1) * sizeof(WCHAR
);
1606 memcpy(buffer
+ offset
, service
->config
.lpDisplayName
, sz
);
1607 s
->display_name
= offset
;
1610 fill_status_process(&s
->service_status_process
, service
);
1614 *returned
= num_services
;
1616 scmdatabase_unlock(manager
->db
);
1617 return ERROR_SUCCESS
;
1620 DWORD __cdecl
svcctl_unknown43(void)
1623 return ERROR_CALL_NOT_IMPLEMENTED
;
1626 DWORD __cdecl
svcctl_CreateServiceWOW64A(
1627 SC_RPC_HANDLE scmanager
,
1633 DWORD error_control
,
1635 LPCSTR loadordergroup
,
1637 const BYTE
*dependencies
,
1640 const BYTE
*password
,
1641 DWORD password_size
,
1642 SC_RPC_HANDLE
*service
)
1645 return ERROR_CALL_NOT_IMPLEMENTED
;
1648 DWORD __cdecl
svcctl_CreateServiceWOW64W(
1649 SC_RPC_HANDLE scmanager
,
1650 LPCWSTR servicename
,
1651 LPCWSTR displayname
,
1655 DWORD error_control
,
1657 LPCWSTR loadordergroup
,
1659 const BYTE
*dependencies
,
1662 const BYTE
*password
,
1663 DWORD password_size
,
1664 SC_RPC_HANDLE
*service
)
1666 WINE_TRACE("(%s, %s, 0x%lx, %s)\n", wine_dbgstr_w(servicename
), wine_dbgstr_w(displayname
), accessmask
, wine_dbgstr_w(imagepath
));
1667 return create_serviceW(scmanager
, servicename
, displayname
, accessmask
, service_type
, start_type
, error_control
, imagepath
,
1668 loadordergroup
, tagid
, dependencies
, depend_size
, start_name
, password
, password_size
, service
, TRUE
);
1671 DWORD __cdecl
svcctl_unknown46(void)
1674 return ERROR_CALL_NOT_IMPLEMENTED
;
1677 DWORD __cdecl
svcctl_NotifyServiceStatusChange(
1678 SC_RPC_HANDLE handle
,
1679 SC_RPC_NOTIFY_PARAMS params
,
1680 GUID
*clientprocessguid
,
1681 GUID
*scmprocessguid
,
1682 BOOL
*createremotequeue
,
1683 SC_NOTIFY_RPC_HANDLE
*hNotify
)
1686 struct sc_manager_handle
*manager
= NULL
;
1687 struct sc_service_handle
*service
= NULL
;
1688 struct sc_notify_handle
*notify
;
1689 struct sc_handle
*hdr
= handle
;
1691 WINE_TRACE("(%p, NotifyMask: 0x%lx, %p, %p, %p, %p)\n", handle
,
1692 params
.params
->dwNotifyMask
, clientprocessguid
, scmprocessguid
,
1693 createremotequeue
, hNotify
);
1697 case SC_HTYPE_SERVICE
:
1698 err
= validate_service_handle(handle
, SERVICE_QUERY_STATUS
, &service
);
1700 case SC_HTYPE_MANAGER
:
1701 err
= validate_scm_handle(handle
, SC_MANAGER_ENUMERATE_SERVICE
, &manager
);
1704 err
= ERROR_INVALID_HANDLE
;
1708 if (err
!= ERROR_SUCCESS
)
1713 WARN("Need support for service creation/deletion notifications\n");
1714 return ERROR_CALL_NOT_IMPLEMENTED
;
1717 notify
= calloc(1, sizeof(*notify
));
1719 return ERROR_NOT_ENOUGH_SERVER_MEMORY
;
1721 notify
->hdr
.type
= SC_HTYPE_NOTIFY
;
1722 notify
->hdr
.access
= 0;
1724 notify
->event
= CreateEventW(NULL
, TRUE
, FALSE
, NULL
);
1726 notify
->notify_mask
= params
.params
->dwNotifyMask
;
1728 service_lock(service
->service_entry
);
1730 if (service
->notify
)
1732 service_unlock(service
->service_entry
);
1733 sc_notify_release(notify
);
1734 return ERROR_ALREADY_REGISTERED
;
1737 mask
= 1 << (service
->service_entry
->status
.dwCurrentState
- SERVICE_STOPPED
);
1738 if (!service
->status_notified
&& (notify
->notify_mask
& mask
))
1740 fill_notify(notify
, service
->service_entry
);
1741 service
->status_notified
= TRUE
;
1745 sc_notify_retain(notify
);
1746 service
->notify
= notify
;
1749 sc_notify_retain(notify
);
1750 *hNotify
= ¬ify
->hdr
;
1752 service_unlock(service
->service_entry
);
1754 return ERROR_SUCCESS
;
1757 DWORD __cdecl
svcctl_GetNotifyResults(
1758 SC_NOTIFY_RPC_HANDLE hNotify
,
1759 SC_RPC_NOTIFY_PARAMS_LIST
**pList
)
1762 struct sc_notify_handle
*notify
;
1764 WINE_TRACE("(%p, %p)\n", hNotify
, pList
);
1767 return ERROR_INVALID_PARAMETER
;
1771 if ((err
= validate_notify_handle(hNotify
, 0, ¬ify
)) != 0)
1774 sc_notify_retain(notify
);
1775 /* block until there is a result */
1776 err
= WaitForSingleObject(notify
->event
, INFINITE
);
1778 if (err
!= WAIT_OBJECT_0
)
1780 sc_notify_release(notify
);
1784 *pList
= InterlockedExchangePointer((void**)¬ify
->params_list
, NULL
);
1787 sc_notify_release(notify
);
1788 return ERROR_REQUEST_ABORTED
;
1791 sc_notify_release(notify
);
1793 return ERROR_SUCCESS
;
1796 DWORD __cdecl
svcctl_CloseNotifyHandle(
1797 SC_NOTIFY_RPC_HANDLE
*hNotify
,
1800 struct sc_notify_handle
*notify
;
1803 WINE_TRACE("(%p, %p)\n", hNotify
, apc_fired
);
1805 if ((err
= validate_notify_handle(*hNotify
, 0, ¬ify
)) != 0)
1808 sc_notify_release(notify
);
1810 return ERROR_SUCCESS
;
1813 DWORD __cdecl
svcctl_ControlServiceExA(
1814 SC_RPC_HANDLE service
,
1817 SC_RPC_SERVICE_CONTROL_IN_PARAMSA
*in_params
,
1818 SC_RPC_SERVICE_CONTROL_OUT_PARAMSA
*out_params
)
1821 return ERROR_CALL_NOT_IMPLEMENTED
;
1824 DWORD __cdecl
svcctl_ControlServiceExW(
1825 SC_RPC_HANDLE service
,
1828 SC_RPC_SERVICE_CONTROL_IN_PARAMSW
*in_params
,
1829 SC_RPC_SERVICE_CONTROL_OUT_PARAMSW
*out_params
)
1832 return ERROR_CALL_NOT_IMPLEMENTED
;
1835 DWORD __cdecl
svcctl_unknown52(void)
1838 return ERROR_CALL_NOT_IMPLEMENTED
;
1841 DWORD __cdecl
svcctl_unknown53(void)
1844 return ERROR_CALL_NOT_IMPLEMENTED
;
1847 DWORD __cdecl
svcctl_unknown54(void)
1850 return ERROR_CALL_NOT_IMPLEMENTED
;
1853 DWORD __cdecl
svcctl_unknown55(void)
1856 return ERROR_CALL_NOT_IMPLEMENTED
;
1859 DWORD __cdecl
svcctl_QueryServiceConfigEx(
1860 SC_RPC_HANDLE service
,
1862 SC_RPC_CONFIG_INFOW
*info
)
1865 return ERROR_CALL_NOT_IMPLEMENTED
;
1868 DWORD __cdecl
svcctl_QueryServiceObjectSecurity(
1869 SC_RPC_HANDLE service
,
1870 SECURITY_INFORMATION info
,
1876 return ERROR_CALL_NOT_IMPLEMENTED
;
1879 DWORD __cdecl
svcctl_SetServiceObjectSecurity(
1880 SC_RPC_HANDLE service
,
1881 SECURITY_INFORMATION info
,
1886 return ERROR_CALL_NOT_IMPLEMENTED
;
1889 DWORD __cdecl
svcctl_QueryServiceStatus(
1890 SC_RPC_HANDLE service
,
1891 SERVICE_STATUS
*status
)
1894 return ERROR_CALL_NOT_IMPLEMENTED
;
1897 DWORD __cdecl
svcctl_NotifyBootConfigStatus(
1898 SVCCTL_HANDLEW machinename
,
1899 DWORD boot_acceptable
)
1902 return ERROR_CALL_NOT_IMPLEMENTED
;
1905 DWORD __cdecl
svcctl_SCSetServiceBitsW(void)
1908 return ERROR_CALL_NOT_IMPLEMENTED
;
1911 DWORD __cdecl
svcctl_EnumDependentServicesW(
1912 SC_RPC_HANDLE service
,
1917 DWORD
*services_ret
)
1920 return ERROR_CALL_NOT_IMPLEMENTED
;
1923 DWORD __cdecl
svcctl_QueryServiceLockStatusW(
1924 SC_RPC_HANDLE scmanager
,
1925 QUERY_SERVICE_LOCK_STATUSW
*status
,
1930 return ERROR_CALL_NOT_IMPLEMENTED
;
1933 DWORD __cdecl
svcctl_SCSetServiceBitsA(void)
1936 return ERROR_CALL_NOT_IMPLEMENTED
;
1939 DWORD __cdecl
svcctl_ChangeServiceConfigA(
1940 SC_RPC_HANDLE service
,
1943 DWORD error_control
,
1945 LPSTR loadordergroup
,
1951 DWORD password_size
,
1955 return ERROR_CALL_NOT_IMPLEMENTED
;
1958 DWORD __cdecl
svcctl_CreateServiceA(
1959 SC_RPC_HANDLE scmanager
,
1962 DWORD desiredaccess
,
1965 DWORD error_control
,
1967 LPCSTR loadordergroup
,
1969 const BYTE
*dependencies
,
1972 const BYTE
*password
,
1973 DWORD password_size
,
1974 SC_RPC_HANDLE
*service
)
1977 return ERROR_CALL_NOT_IMPLEMENTED
;
1980 DWORD __cdecl
svcctl_EnumDependentServicesA(
1981 SC_RPC_HANDLE service
,
1986 DWORD
*services_ret
)
1989 return ERROR_CALL_NOT_IMPLEMENTED
;
1992 DWORD __cdecl
svcctl_EnumServicesStatusA(
1993 SC_RPC_HANDLE hmngr
,
2003 return ERROR_CALL_NOT_IMPLEMENTED
;
2006 DWORD __cdecl
svcctl_OpenSCManagerA(
2007 MACHINE_HANDLEA MachineName
,
2008 LPCSTR DatabaseName
,
2010 SC_RPC_HANDLE
*handle
)
2013 return ERROR_CALL_NOT_IMPLEMENTED
;
2016 DWORD __cdecl
svcctl_OpenServiceA(
2017 SC_RPC_HANDLE hSCManager
,
2018 LPCSTR lpServiceName
,
2019 DWORD dwDesiredAccess
,
2020 SC_RPC_HANDLE
*phService
)
2023 return ERROR_CALL_NOT_IMPLEMENTED
;
2026 DWORD __cdecl
svcctl_QueryServiceConfigA(
2027 SC_RPC_HANDLE hService
,
2028 QUERY_SERVICE_CONFIGA
*config
,
2033 return ERROR_CALL_NOT_IMPLEMENTED
;
2036 DWORD __cdecl
svcctl_QueryServiceLockStatusA(
2037 SC_RPC_HANDLE scmanager
,
2038 QUERY_SERVICE_LOCK_STATUSA
*status
,
2043 return ERROR_CALL_NOT_IMPLEMENTED
;
2046 DWORD __cdecl
svcctl_StartServiceA(
2047 SC_RPC_HANDLE service
,
2052 return ERROR_CALL_NOT_IMPLEMENTED
;
2055 DWORD __cdecl
svcctl_GetServiceDisplayNameA(
2056 SC_RPC_HANDLE hSCManager
,
2062 return ERROR_CALL_NOT_IMPLEMENTED
;
2065 DWORD __cdecl
svcctl_GetServiceKeyNameA(
2066 SC_RPC_HANDLE hSCManager
,
2072 return ERROR_CALL_NOT_IMPLEMENTED
;
2075 DWORD __cdecl
svcctl_GetCurrentGroupStateW(void)
2078 return ERROR_CALL_NOT_IMPLEMENTED
;
2081 DWORD __cdecl
svcctl_EnumServiceGroupW(
2082 SC_RPC_HANDLE scmanager
,
2084 DWORD service_state
,
2088 DWORD
*returned_size
,
2089 DWORD
*resume_index
,
2093 return ERROR_CALL_NOT_IMPLEMENTED
;
2096 DWORD __cdecl
svcctl_ChangeServiceConfig2A(
2097 SC_RPC_HANDLE service
,
2098 SC_RPC_CONFIG_INFOA info
)
2101 return ERROR_CALL_NOT_IMPLEMENTED
;
2104 DWORD __cdecl
svcctl_QueryServiceConfig2A(
2105 SC_RPC_HANDLE service
,
2112 return ERROR_CALL_NOT_IMPLEMENTED
;
2115 DWORD
RPC_Init(void)
2117 WCHAR transport
[] = SVCCTL_TRANSPORT
;
2118 WCHAR endpoint
[] = SVCCTL_ENDPOINT
;
2121 if (!(cleanup_group
= CreateThreadpoolCleanupGroup()))
2123 WINE_ERR("CreateThreadpoolCleanupGroup failed with error %lu\n", GetLastError());
2124 return GetLastError();
2127 if ((err
= RpcServerUseProtseqEpW(transport
, 0, endpoint
, NULL
)) != ERROR_SUCCESS
)
2129 WINE_ERR("RpcServerUseProtseq failed with error %lu\n", err
);
2133 if ((err
= RpcServerRegisterIf(svcctl_v2_0_s_ifspec
, 0, 0)) != ERROR_SUCCESS
)
2135 WINE_ERR("RpcServerRegisterIf failed with error %lu\n", err
);
2139 if ((err
= RpcServerListen(1, RPC_C_LISTEN_MAX_CALLS_DEFAULT
, TRUE
)) != ERROR_SUCCESS
)
2141 WINE_ERR("RpcServerListen failed with error %lu\n", err
);
2145 NtSetInformationProcess( GetCurrentProcess(), ProcessWineMakeProcessSystem
,
2146 &exit_event
, sizeof(HANDLE
*) );
2147 return ERROR_SUCCESS
;
2152 RpcMgmtStopServerListening(NULL
);
2153 RpcServerUnregisterIf(svcctl_v2_0_s_ifspec
, NULL
, TRUE
);
2154 RpcMgmtWaitServerListen();
2156 CloseThreadpoolCleanupGroupMembers(cleanup_group
, TRUE
, NULL
);
2157 CloseThreadpoolCleanupGroup(cleanup_group
);
2158 CloseHandle(exit_event
);
2161 void __RPC_USER
SC_RPC_HANDLE_rundown(SC_RPC_HANDLE handle
)
2163 SC_RPC_HANDLE_destroy(handle
);
2166 void __RPC_USER
SC_NOTIFY_RPC_HANDLE_rundown(SC_NOTIFY_RPC_HANDLE handle
)
2170 void __RPC_FAR
* __RPC_USER
MIDL_user_allocate(SIZE_T len
)
2175 void __RPC_USER
MIDL_user_free(void __RPC_FAR
* ptr
)