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 HeapFree(GetProcessHeap(), 0, notify
->params_list
);
106 HeapFree(GetProcessHeap(), 0, notify
);
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 HeapFree(GetProcessHeap(), 0, old_cfg
->lpBinaryPathName
);
202 if (old_cfg
->lpLoadOrderGroup
!= new_cfg
->lpLoadOrderGroup
)
203 HeapFree(GetProcessHeap(), 0, old_cfg
->lpLoadOrderGroup
);
205 if (old_cfg
->lpServiceStartName
!= new_cfg
->lpServiceStartName
)
206 HeapFree(GetProcessHeap(), 0, old_cfg
->lpServiceStartName
);
208 if (old_cfg
->lpDisplayName
!= new_cfg
->lpDisplayName
)
209 HeapFree(GetProcessHeap(), 0, old_cfg
->lpDisplayName
);
211 if (old
->dependOnServices
!= new->dependOnServices
)
212 HeapFree(GetProcessHeap(), 0, old
->dependOnServices
);
214 if (old
->dependOnGroups
!= new->dependOnGroups
)
215 HeapFree(GetProcessHeap(), 0, 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
= HeapAlloc(GetProcessHeap(), 0, 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
;
307 HeapFree(GetProcessHeap(), 0, manager
);
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
);
322 HeapFree(GetProcessHeap(), 0, service
);
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
= HeapAlloc(GetProcessHeap(), 0, 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
= HeapAlloc(GetProcessHeap(), 0, (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
= HeapAlloc(GetProcessHeap(), 0, (len_groups
+ 1) * sizeof(WCHAR
));
522 HeapFree(GetProcessHeap(), 0, services
);
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
= strdupW(lpBinaryPathName
);
595 entry
->config
.lpLoadOrderGroup
= strdupW(lpLoadOrderGroup
);
596 entry
->config
.lpServiceStartName
= strdupW(lpServiceStartName
);
597 entry
->config
.lpDisplayName
= strdupW(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
= strdupW(service
->service_entry
->config
.lpBinaryPathName
);
706 config
->lpLoadOrderGroup
= strdupW(service
->service_entry
->config
.lpLoadOrderGroup
);
707 config
->dwTagId
= service
->service_entry
->config
.dwTagId
;
708 config
->lpDependencies
= NULL
; /* TODO */
709 config
->lpServiceStartName
= strdupW(service
->service_entry
->config
.lpServiceStartName
);
710 config
->lpDisplayName
= strdupW(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
= strdupW(lpBinaryPathName
);
807 if (lpLoadOrderGroup
!= NULL
)
808 new_entry
.config
.lpLoadOrderGroup
= strdupW(lpLoadOrderGroup
);
810 if (lpServiceStartName
!= NULL
)
811 new_entry
.config
.lpServiceStartName
= strdupW(lpServiceStartName
);
813 if (lpDisplayName
!= NULL
)
814 new_entry
.config
.lpDisplayName
= strdupW(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
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
,
845 sizeof(SC_RPC_NOTIFY_PARAMS_LIST
) + sizeof(SERVICE_NOTIFY_STATUS_CHANGE_PARAMS_2
));
849 cparams
= (SERVICE_NOTIFY_STATUS_CHANGE_PARAMS_2
*)(list
+ 1);
851 cparams
->dwNotifyMask
= notify
->notify_mask
;
852 fill_status_process(&cparams
->ServiceStatus
, service
);
853 cparams
->dwNotificationStatus
= ERROR_SUCCESS
;
854 cparams
->dwNotificationTriggered
= 1 << (cparams
->ServiceStatus
.dwCurrentState
- SERVICE_STOPPED
);
855 cparams
->pszServiceNames
= NULL
;
859 list
->NotifyParamsArray
[0].dwInfoLevel
= 2;
860 list
->NotifyParamsArray
[0].params
= cparams
;
862 InterlockedExchangePointer((void**)¬ify
->params_list
, list
);
864 SetEvent(notify
->event
);
867 DWORD __cdecl
svcctl_SetServiceStatus(SC_RPC_HANDLE handle
, SERVICE_STATUS
*status
)
869 struct sc_service_handle
*service
, *service_handle
;
870 struct process_entry
*process
;
873 WINE_TRACE("(%p, %p)\n", handle
, status
);
875 if ((err
= validate_service_handle(handle
, SERVICE_SET_STATUS
, &service
)) != 0)
878 service_lock(service
->service_entry
);
880 /* FIXME: be a bit more discriminant about what parts of the status we set
881 * and check that fields are valid */
882 service
->service_entry
->status
.dwCurrentState
= status
->dwCurrentState
;
883 service
->service_entry
->status
.dwControlsAccepted
= status
->dwControlsAccepted
;
884 service
->service_entry
->status
.dwWin32ExitCode
= status
->dwWin32ExitCode
;
885 service
->service_entry
->status
.dwServiceSpecificExitCode
= status
->dwServiceSpecificExitCode
;
886 service
->service_entry
->status
.dwCheckPoint
= status
->dwCheckPoint
;
887 service
->service_entry
->status
.dwWaitHint
= status
->dwWaitHint
;
888 SetEvent(service
->service_entry
->status_changed_event
);
890 if ((process
= service
->service_entry
->process
) &&
891 status
->dwCurrentState
== SERVICE_STOPPED
)
893 service
->service_entry
->process
= NULL
;
894 if (!--process
->use_count
)
895 terminate_after_timeout(process
, service_kill_timeout
);
896 if (service
->service_entry
->shared_process
&& process
->use_count
<= 1)
897 shutdown_shared_process(process
);
898 release_process(process
);
901 mask
= 1 << (service
->service_entry
->status
.dwCurrentState
- SERVICE_STOPPED
);
902 LIST_FOR_EACH_ENTRY(service_handle
, &service
->service_entry
->handles
, struct sc_service_handle
, entry
)
904 struct sc_notify_handle
*notify
= service_handle
->notify
;
905 if (notify
&& (notify
->notify_mask
& mask
))
907 fill_notify(notify
, service
->service_entry
);
908 sc_notify_release(notify
);
909 service_handle
->notify
= NULL
;
910 service_handle
->status_notified
= TRUE
;
913 service_handle
->status_notified
= FALSE
;
916 service_unlock(service
->service_entry
);
918 return ERROR_SUCCESS
;
921 DWORD __cdecl
svcctl_ChangeServiceConfig2W( SC_RPC_HANDLE hService
, SC_RPC_CONFIG_INFOW config
)
923 struct sc_service_handle
*service
;
926 if ((err
= validate_service_handle(hService
, SERVICE_CHANGE_CONFIG
, &service
)) != 0)
929 switch (config
.dwInfoLevel
)
931 case SERVICE_CONFIG_DESCRIPTION
:
935 if (!config
.descr
->lpDescription
)
938 if (config
.descr
->lpDescription
[0])
940 if (!(descr
= strdupW( config
.descr
->lpDescription
)))
941 return ERROR_NOT_ENOUGH_MEMORY
;
944 WINE_TRACE( "changing service %p descr to %s\n", service
, wine_dbgstr_w(descr
) );
945 service_lock( service
->service_entry
);
946 HeapFree( GetProcessHeap(), 0, service
->service_entry
->description
);
947 service
->service_entry
->description
= descr
;
948 save_service_config( service
->service_entry
);
949 service_unlock( service
->service_entry
);
952 case SERVICE_CONFIG_FAILURE_ACTIONS
:
953 WINE_FIXME( "SERVICE_CONFIG_FAILURE_ACTIONS not implemented: period %lu msg %s cmd %s\n",
954 config
.actions
->dwResetPeriod
,
955 wine_dbgstr_w(config
.actions
->lpRebootMsg
),
956 wine_dbgstr_w(config
.actions
->lpCommand
) );
958 case SERVICE_CONFIG_PRESHUTDOWN_INFO
:
959 WINE_TRACE( "changing service %p preshutdown timeout to %ld\n",
960 service
, config
.preshutdown
->dwPreshutdownTimeout
);
961 service_lock( service
->service_entry
);
962 service
->service_entry
->preshutdown_timeout
= config
.preshutdown
->dwPreshutdownTimeout
;
963 save_service_config( service
->service_entry
);
964 service_unlock( service
->service_entry
);
967 WINE_FIXME("level %lu not implemented\n", config
.dwInfoLevel
);
968 err
= ERROR_INVALID_LEVEL
;
974 DWORD __cdecl
svcctl_QueryServiceConfig2W( SC_RPC_HANDLE hService
, DWORD level
,
975 BYTE
*buffer
, DWORD size
, LPDWORD needed
)
977 struct sc_service_handle
*service
;
980 memset(buffer
, 0, size
);
982 if ((err
= validate_service_handle(hService
, SERVICE_QUERY_CONFIG
, &service
)) != 0)
987 case SERVICE_CONFIG_DESCRIPTION
:
989 struct service_description
*desc
= (struct service_description
*)buffer
;
990 DWORD total_size
= sizeof(*desc
);
992 service_lock(service
->service_entry
);
993 if (service
->service_entry
->description
)
994 total_size
+= lstrlenW(service
->service_entry
->description
) * sizeof(WCHAR
);
996 *needed
= total_size
;
997 if (size
>= total_size
)
999 if (service
->service_entry
->description
)
1001 lstrcpyW( desc
->description
, service
->service_entry
->description
);
1002 desc
->size
= total_size
- FIELD_OFFSET(struct service_description
, description
);
1006 desc
->description
[0] = 0;
1010 else err
= ERROR_INSUFFICIENT_BUFFER
;
1011 service_unlock(service
->service_entry
);
1015 case SERVICE_CONFIG_PRESHUTDOWN_INFO
:
1016 service_lock(service
->service_entry
);
1018 *needed
= sizeof(SERVICE_PRESHUTDOWN_INFO
);
1019 if (size
>= *needed
)
1020 ((LPSERVICE_PRESHUTDOWN_INFO
)buffer
)->dwPreshutdownTimeout
=
1021 service
->service_entry
->preshutdown_timeout
;
1022 else err
= ERROR_INSUFFICIENT_BUFFER
;
1024 service_unlock(service
->service_entry
);
1028 WINE_FIXME("level %lu not implemented\n", level
);
1029 err
= ERROR_INVALID_LEVEL
;
1035 DWORD __cdecl
svcctl_QueryServiceStatusEx(
1036 SC_RPC_HANDLE hService
,
1037 SC_STATUS_TYPE InfoLevel
,
1040 LPDWORD pcbBytesNeeded
)
1042 struct sc_service_handle
*service
;
1044 LPSERVICE_STATUS_PROCESS pSvcStatusData
;
1046 memset(lpBuffer
, 0, cbBufSize
);
1048 if ((err
= validate_service_handle(hService
, SERVICE_QUERY_STATUS
, &service
)) != 0)
1051 if (InfoLevel
!= SC_STATUS_PROCESS_INFO
)
1052 return ERROR_INVALID_LEVEL
;
1054 pSvcStatusData
= (LPSERVICE_STATUS_PROCESS
) lpBuffer
;
1055 if (pSvcStatusData
== NULL
)
1056 return ERROR_INVALID_PARAMETER
;
1058 if (cbBufSize
< sizeof(SERVICE_STATUS_PROCESS
))
1060 if( pcbBytesNeeded
!= NULL
)
1061 *pcbBytesNeeded
= sizeof(SERVICE_STATUS_PROCESS
);
1063 return ERROR_INSUFFICIENT_BUFFER
;
1066 service_lock(service
->service_entry
);
1067 fill_status_process(pSvcStatusData
, service
->service_entry
);
1068 service_unlock(service
->service_entry
);
1070 return ERROR_SUCCESS
;
1073 /******************************************************************************
1074 * service_accepts_control
1076 static BOOL
service_accepts_control(const struct service_entry
*service
, DWORD dwControl
)
1078 DWORD a
= service
->status
.dwControlsAccepted
;
1080 if (dwControl
>= 128 && dwControl
<= 255)
1085 case SERVICE_CONTROL_INTERROGATE
:
1087 case SERVICE_CONTROL_STOP
:
1088 if (a
&SERVICE_ACCEPT_STOP
)
1091 case SERVICE_CONTROL_SHUTDOWN
:
1092 if (a
&SERVICE_ACCEPT_SHUTDOWN
)
1095 case SERVICE_CONTROL_PAUSE
:
1096 case SERVICE_CONTROL_CONTINUE
:
1097 if (a
&SERVICE_ACCEPT_PAUSE_CONTINUE
)
1100 case SERVICE_CONTROL_PARAMCHANGE
:
1101 if (a
&SERVICE_ACCEPT_PARAMCHANGE
)
1104 case SERVICE_CONTROL_NETBINDADD
:
1105 case SERVICE_CONTROL_NETBINDREMOVE
:
1106 case SERVICE_CONTROL_NETBINDENABLE
:
1107 case SERVICE_CONTROL_NETBINDDISABLE
:
1108 if (a
&SERVICE_ACCEPT_NETBINDCHANGE
)
1110 case SERVICE_CONTROL_HARDWAREPROFILECHANGE
:
1111 if (a
&SERVICE_ACCEPT_HARDWAREPROFILECHANGE
)
1114 case SERVICE_CONTROL_POWEREVENT
:
1115 if (a
&SERVICE_ACCEPT_POWEREVENT
)
1118 case SERVICE_CONTROL_SESSIONCHANGE
:
1119 if (a
&SERVICE_ACCEPT_SESSIONCHANGE
)
1126 /******************************************************************************
1127 * process_send_command
1129 static BOOL
process_send_command(struct process_entry
*process
, const void *data
, DWORD size
, DWORD
*result
)
1131 OVERLAPPED overlapped
;
1135 overlapped
.u
.s
.Offset
= 0;
1136 overlapped
.u
.s
.OffsetHigh
= 0;
1137 overlapped
.hEvent
= process
->overlapped_event
;
1138 r
= WriteFile(process
->control_pipe
, data
, size
, &count
, &overlapped
);
1139 if (!r
&& GetLastError() == ERROR_IO_PENDING
)
1141 ret
= WaitForSingleObject(process
->overlapped_event
, service_pipe_timeout
);
1142 if (ret
== WAIT_TIMEOUT
)
1144 WINE_ERR("sending command timed out\n");
1145 *result
= ERROR_SERVICE_REQUEST_TIMEOUT
;
1148 r
= GetOverlappedResult(process
->control_pipe
, &overlapped
, &count
, FALSE
);
1150 if (!r
|| count
!= size
)
1152 WINE_ERR("service protocol error - failed to write pipe!\n");
1153 *result
= (!r
? GetLastError() : ERROR_WRITE_FAULT
);
1156 r
= ReadFile(process
->control_pipe
, result
, sizeof *result
, &count
, &overlapped
);
1157 if (!r
&& GetLastError() == ERROR_IO_PENDING
)
1159 ret
= WaitForSingleObject(process
->overlapped_event
, service_pipe_timeout
);
1160 if (ret
== WAIT_TIMEOUT
)
1162 WINE_ERR("receiving command result timed out\n");
1163 *result
= ERROR_SERVICE_REQUEST_TIMEOUT
;
1166 r
= GetOverlappedResult(process
->control_pipe
, &overlapped
, &count
, FALSE
);
1168 if (!r
|| count
!= sizeof *result
)
1170 WINE_ERR("service protocol error - failed to read pipe "
1171 "r = %d count = %ld!\n", r
, count
);
1172 *result
= (!r
? GetLastError() : ERROR_READ_FAULT
);
1179 /******************************************************************************
1180 * process_send_control
1182 BOOL
process_send_control(struct process_entry
*process
, BOOL shared_process
, const WCHAR
*name
,
1183 DWORD control
, const BYTE
*data
, DWORD data_size
, DWORD
*result
)
1185 service_start_info
*ssi
;
1191 control
|= SERVICE_CONTROL_FORWARD_FLAG
;
1192 data
= (BYTE
*)name
;
1193 data_size
= (lstrlenW(name
) + 1) * sizeof(WCHAR
);
1197 /* calculate how much space we need to send the startup info */
1198 len
= (lstrlenW(name
) + 1) * sizeof(WCHAR
) + data_size
;
1200 ssi
= HeapAlloc(GetProcessHeap(),0,FIELD_OFFSET(service_start_info
, data
[len
]));
1201 ssi
->magic
= SERVICE_PROTOCOL_MAGIC
;
1202 ssi
->control
= control
;
1203 ssi
->total_size
= FIELD_OFFSET(service_start_info
, data
[len
]);
1204 ssi
->name_size
= lstrlenW(name
) + 1;
1205 lstrcpyW((WCHAR
*)ssi
->data
, name
);
1206 if (data_size
) memcpy(&ssi
->data
[ssi
->name_size
* sizeof(WCHAR
)], data
, data_size
);
1208 r
= process_send_command(process
, ssi
, ssi
->total_size
, result
);
1209 HeapFree( GetProcessHeap(), 0, ssi
);
1213 DWORD __cdecl
svcctl_StartServiceW(
1214 SC_RPC_HANDLE hService
,
1215 DWORD dwNumServiceArgs
,
1216 LPCWSTR
*lpServiceArgVectors
)
1218 struct sc_service_handle
*service
;
1221 WINE_TRACE("(%p, %ld, %p)\n", hService
, dwNumServiceArgs
, lpServiceArgVectors
);
1223 if ((err
= validate_service_handle(hService
, SERVICE_START
, &service
)) != 0)
1226 if (service
->service_entry
->config
.dwStartType
== SERVICE_DISABLED
)
1227 return ERROR_SERVICE_DISABLED
;
1229 if (!scmdatabase_lock_startup(service
->service_entry
->db
, 3000))
1230 return ERROR_SERVICE_DATABASE_LOCKED
;
1232 err
= service_start(service
->service_entry
, dwNumServiceArgs
, lpServiceArgVectors
);
1234 scmdatabase_unlock_startup(service
->service_entry
->db
);
1238 DWORD __cdecl
svcctl_ControlService(
1239 SC_RPC_HANDLE hService
,
1241 SERVICE_STATUS
*lpServiceStatus
)
1243 DWORD access_required
;
1244 struct sc_service_handle
*service
;
1245 struct process_entry
*process
;
1246 BOOL shared_process
;
1249 WINE_TRACE("(%p, %ld, %p)\n", hService
, dwControl
, lpServiceStatus
);
1253 case SERVICE_CONTROL_CONTINUE
:
1254 case SERVICE_CONTROL_NETBINDADD
:
1255 case SERVICE_CONTROL_NETBINDDISABLE
:
1256 case SERVICE_CONTROL_NETBINDENABLE
:
1257 case SERVICE_CONTROL_NETBINDREMOVE
:
1258 case SERVICE_CONTROL_PARAMCHANGE
:
1259 case SERVICE_CONTROL_PAUSE
:
1260 access_required
= SERVICE_PAUSE_CONTINUE
;
1262 case SERVICE_CONTROL_INTERROGATE
:
1263 access_required
= SERVICE_INTERROGATE
;
1265 case SERVICE_CONTROL_STOP
:
1266 access_required
= SERVICE_STOP
;
1269 if (dwControl
>= 128 && dwControl
<= 255)
1270 access_required
= SERVICE_USER_DEFINED_CONTROL
;
1272 return ERROR_INVALID_PARAMETER
;
1275 if ((result
= validate_service_handle(hService
, access_required
, &service
)) != 0)
1278 service_lock(service
->service_entry
);
1280 result
= ERROR_SUCCESS
;
1281 switch (service
->service_entry
->status
.dwCurrentState
)
1283 case SERVICE_STOPPED
:
1284 result
= ERROR_SERVICE_NOT_ACTIVE
;
1286 case SERVICE_START_PENDING
:
1287 if (dwControl
==SERVICE_CONTROL_STOP
)
1290 case SERVICE_STOP_PENDING
:
1291 result
= ERROR_SERVICE_CANNOT_ACCEPT_CTRL
;
1295 if (result
== ERROR_SUCCESS
&& service
->service_entry
->force_shutdown
)
1297 result
= ERROR_SERVICE_CANNOT_ACCEPT_CTRL
;
1298 if ((process
= service
->service_entry
->process
))
1300 service
->service_entry
->process
= NULL
;
1301 if (!--process
->use_count
) process_terminate(process
);
1302 release_process(process
);
1306 if (result
!= ERROR_SUCCESS
)
1308 if (lpServiceStatus
) *lpServiceStatus
= service
->service_entry
->status
;
1309 service_unlock(service
->service_entry
);
1313 if (!service_accepts_control(service
->service_entry
, dwControl
))
1315 service_unlock(service
->service_entry
);
1316 return ERROR_INVALID_SERVICE_CONTROL
;
1319 /* Remember that we tried to shutdown this service. When the service is
1320 * still running on the second invocation, it will be forcefully killed. */
1321 if (dwControl
== SERVICE_CONTROL_STOP
)
1322 service
->service_entry
->force_shutdown
= TRUE
;
1324 /* Hold a reference to the process while sending the command. */
1325 process
= grab_process(service
->service_entry
->process
);
1326 shared_process
= service
->service_entry
->shared_process
;
1327 service_unlock(service
->service_entry
);
1330 return ERROR_SERVICE_CANNOT_ACCEPT_CTRL
;
1332 result
= WaitForSingleObject(process
->control_mutex
, 30000);
1333 if (result
!= WAIT_OBJECT_0
)
1335 release_process(process
);
1336 return ERROR_SERVICE_REQUEST_TIMEOUT
;
1339 if (process_send_control(process
, shared_process
, service
->service_entry
->name
,
1340 dwControl
, NULL
, 0, &result
))
1341 result
= ERROR_SUCCESS
;
1343 if (lpServiceStatus
)
1345 service_lock(service
->service_entry
);
1346 *lpServiceStatus
= service
->service_entry
->status
;
1347 service_unlock(service
->service_entry
);
1350 ReleaseMutex(process
->control_mutex
);
1351 release_process(process
);
1355 DWORD __cdecl
svcctl_CloseServiceHandle(
1356 SC_RPC_HANDLE
*handle
)
1358 WINE_TRACE("(&%p)\n", *handle
);
1360 SC_RPC_HANDLE_destroy(*handle
);
1363 return ERROR_SUCCESS
;
1366 void __RPC_USER
SC_RPC_LOCK_rundown(SC_RPC_LOCK hLock
)
1370 DWORD __cdecl
svcctl_LockServiceDatabase(SC_RPC_HANDLE manager
, SC_RPC_LOCK
*lock
)
1372 TRACE("(%p, %p)\n", manager
, lock
);
1374 *lock
= (SC_RPC_LOCK
)0xdeadbeef;
1375 return ERROR_SUCCESS
;
1378 DWORD __cdecl
svcctl_UnlockServiceDatabase(SC_RPC_LOCK
*lock
)
1380 TRACE("(&%p)\n", *lock
);
1383 return ERROR_SUCCESS
;
1386 static BOOL
map_state(DWORD state
, DWORD mask
)
1390 case SERVICE_START_PENDING
:
1391 case SERVICE_STOP_PENDING
:
1392 case SERVICE_RUNNING
:
1393 case SERVICE_CONTINUE_PENDING
:
1394 case SERVICE_PAUSE_PENDING
:
1395 case SERVICE_PAUSED
:
1396 if (SERVICE_ACTIVE
& mask
) return TRUE
;
1398 case SERVICE_STOPPED
:
1399 if (SERVICE_INACTIVE
& mask
) return TRUE
;
1402 WINE_ERR("unknown state %lu\n", state
);
1408 DWORD __cdecl
svcctl_EnumServicesStatusW(
1409 SC_RPC_HANDLE hmngr
,
1418 DWORD err
, sz
, total_size
, num_services
, offset
;
1419 struct sc_manager_handle
*manager
;
1420 struct service_entry
*service
;
1421 struct enum_service_status
*s
;
1423 WINE_TRACE("(%p, 0x%lx, 0x%lx, %p, %lu, %p, %p, %p)\n", hmngr
, type
, state
, buffer
, size
, needed
, returned
, resume
);
1425 if (!type
|| !state
)
1426 return ERROR_INVALID_PARAMETER
;
1428 if ((err
= validate_scm_handle(hmngr
, SC_MANAGER_ENUMERATE_SERVICE
, &manager
)) != ERROR_SUCCESS
)
1432 WINE_FIXME("resume index not supported\n");
1434 scmdatabase_lock(manager
->db
);
1436 total_size
= num_services
= 0;
1437 LIST_FOR_EACH_ENTRY(service
, &manager
->db
->services
, struct service_entry
, entry
)
1439 if ((service
->status
.dwServiceType
& type
) && map_state(service
->status
.dwCurrentState
, state
))
1441 total_size
+= sizeof(*s
);
1442 total_size
+= (lstrlenW(service
->name
) + 1) * sizeof(WCHAR
);
1443 if (service
->config
.lpDisplayName
)
1445 total_size
+= (lstrlenW(service
->config
.lpDisplayName
) + 1) * sizeof(WCHAR
);
1451 *needed
= total_size
;
1452 if (total_size
> size
)
1454 scmdatabase_unlock(manager
->db
);
1455 return ERROR_MORE_DATA
;
1457 s
= (struct enum_service_status
*)buffer
;
1458 offset
= num_services
* sizeof(struct enum_service_status
);
1459 LIST_FOR_EACH_ENTRY(service
, &manager
->db
->services
, struct service_entry
, entry
)
1461 if ((service
->status
.dwServiceType
& type
) && map_state(service
->status
.dwCurrentState
, state
))
1463 sz
= (lstrlenW(service
->name
) + 1) * sizeof(WCHAR
);
1464 memcpy(buffer
+ offset
, service
->name
, sz
);
1465 s
->service_name
= offset
;
1468 if (!service
->config
.lpDisplayName
) s
->display_name
= 0;
1471 sz
= (lstrlenW(service
->config
.lpDisplayName
) + 1) * sizeof(WCHAR
);
1472 memcpy(buffer
+ offset
, service
->config
.lpDisplayName
, sz
);
1473 s
->display_name
= offset
;
1476 s
->service_status
= service
->status
;
1480 *returned
= num_services
;
1482 scmdatabase_unlock(manager
->db
);
1483 return ERROR_SUCCESS
;
1486 static struct service_entry
*find_service_by_group(struct scmdatabase
*db
, const WCHAR
*group
)
1488 struct service_entry
*service
;
1489 LIST_FOR_EACH_ENTRY(service
, &db
->services
, struct service_entry
, entry
)
1491 if (service
->config
.lpLoadOrderGroup
&& !wcsicmp(group
, service
->config
.lpLoadOrderGroup
))
1497 static BOOL
match_group(const WCHAR
*g1
, const WCHAR
*g2
)
1499 if (!g2
) return TRUE
;
1500 if (!g2
[0] && (!g1
|| !g1
[0])) return TRUE
;
1501 if (g1
&& !lstrcmpW(g1
, g2
)) return TRUE
;
1505 DWORD __cdecl
svcctl_EnumServicesStatusExA(
1506 SC_RPC_HANDLE scmanager
,
1507 SC_ENUM_TYPE info_level
,
1509 DWORD service_state
,
1513 DWORD
*services_count
,
1514 DWORD
*resume_index
,
1518 return ERROR_CALL_NOT_IMPLEMENTED
;
1521 DWORD __cdecl
svcctl_EnumServicesStatusExW(
1522 SC_RPC_HANDLE hmngr
,
1523 SC_ENUM_TYPE info_level
,
1530 DWORD
*resume_handle
,
1533 DWORD err
, sz
, total_size
, num_services
;
1535 struct sc_manager_handle
*manager
;
1536 struct service_entry
*service
;
1537 struct enum_service_status_process
*s
;
1539 WINE_TRACE("(%p, 0x%lx, 0x%lx, %p, %lu, %p, %p, %s)\n", hmngr
, type
, state
, buffer
, size
,
1540 needed
, returned
, wine_dbgstr_w(group
));
1543 FIXME("resume handle not supported\n");
1545 if (!type
|| !state
)
1546 return ERROR_INVALID_PARAMETER
;
1548 if ((err
= validate_scm_handle(hmngr
, SC_MANAGER_ENUMERATE_SERVICE
, &manager
)) != ERROR_SUCCESS
)
1551 scmdatabase_lock(manager
->db
);
1553 if (group
&& !find_service_by_group(manager
->db
, group
))
1555 scmdatabase_unlock(manager
->db
);
1556 return ERROR_SERVICE_DOES_NOT_EXIST
;
1559 total_size
= num_services
= 0;
1560 LIST_FOR_EACH_ENTRY(service
, &manager
->db
->services
, struct service_entry
, entry
)
1562 if ((service
->status
.dwServiceType
& type
) && map_state(service
->status
.dwCurrentState
, state
)
1563 && match_group(service
->config
.lpLoadOrderGroup
, group
))
1565 total_size
+= sizeof(*s
);
1566 total_size
+= (lstrlenW(service
->name
) + 1) * sizeof(WCHAR
);
1567 if (service
->config
.lpDisplayName
)
1569 total_size
+= (lstrlenW(service
->config
.lpDisplayName
) + 1) * sizeof(WCHAR
);
1575 *needed
= total_size
;
1576 if (total_size
> size
)
1578 scmdatabase_unlock(manager
->db
);
1579 return ERROR_MORE_DATA
;
1581 s
= (struct enum_service_status_process
*)buffer
;
1582 offset
= num_services
* sizeof(*s
);
1583 LIST_FOR_EACH_ENTRY(service
, &manager
->db
->services
, struct service_entry
, entry
)
1585 if ((service
->status
.dwServiceType
& type
) && map_state(service
->status
.dwCurrentState
, state
)
1586 && match_group(service
->config
.lpLoadOrderGroup
, group
))
1588 sz
= (lstrlenW(service
->name
) + 1) * sizeof(WCHAR
);
1589 memcpy(buffer
+ offset
, service
->name
, sz
);
1590 s
->service_name
= offset
;
1593 if (!service
->config
.lpDisplayName
) s
->display_name
= 0;
1596 sz
= (lstrlenW(service
->config
.lpDisplayName
) + 1) * sizeof(WCHAR
);
1597 memcpy(buffer
+ offset
, service
->config
.lpDisplayName
, sz
);
1598 s
->display_name
= offset
;
1601 fill_status_process(&s
->service_status_process
, service
);
1605 *returned
= num_services
;
1607 scmdatabase_unlock(manager
->db
);
1608 return ERROR_SUCCESS
;
1611 DWORD __cdecl
svcctl_unknown43(void)
1614 return ERROR_CALL_NOT_IMPLEMENTED
;
1617 DWORD __cdecl
svcctl_CreateServiceWOW64A(
1618 SC_RPC_HANDLE scmanager
,
1624 DWORD error_control
,
1626 LPCSTR loadordergroup
,
1628 const BYTE
*dependencies
,
1631 const BYTE
*password
,
1632 DWORD password_size
,
1633 SC_RPC_HANDLE
*service
)
1636 return ERROR_CALL_NOT_IMPLEMENTED
;
1639 DWORD __cdecl
svcctl_CreateServiceWOW64W(
1640 SC_RPC_HANDLE scmanager
,
1641 LPCWSTR servicename
,
1642 LPCWSTR displayname
,
1646 DWORD error_control
,
1648 LPCWSTR loadordergroup
,
1650 const BYTE
*dependencies
,
1653 const BYTE
*password
,
1654 DWORD password_size
,
1655 SC_RPC_HANDLE
*service
)
1657 WINE_TRACE("(%s, %s, 0x%lx, %s)\n", wine_dbgstr_w(servicename
), wine_dbgstr_w(displayname
), accessmask
, wine_dbgstr_w(imagepath
));
1658 return create_serviceW(scmanager
, servicename
, displayname
, accessmask
, service_type
, start_type
, error_control
, imagepath
,
1659 loadordergroup
, tagid
, dependencies
, depend_size
, start_name
, password
, password_size
, service
, TRUE
);
1662 DWORD __cdecl
svcctl_unknown46(void)
1665 return ERROR_CALL_NOT_IMPLEMENTED
;
1668 DWORD __cdecl
svcctl_NotifyServiceStatusChange(
1669 SC_RPC_HANDLE handle
,
1670 SC_RPC_NOTIFY_PARAMS params
,
1671 GUID
*clientprocessguid
,
1672 GUID
*scmprocessguid
,
1673 BOOL
*createremotequeue
,
1674 SC_NOTIFY_RPC_HANDLE
*hNotify
)
1677 struct sc_manager_handle
*manager
= NULL
;
1678 struct sc_service_handle
*service
= NULL
;
1679 struct sc_notify_handle
*notify
;
1680 struct sc_handle
*hdr
= handle
;
1682 WINE_TRACE("(%p, NotifyMask: 0x%lx, %p, %p, %p, %p)\n", handle
,
1683 params
.params
->dwNotifyMask
, clientprocessguid
, scmprocessguid
,
1684 createremotequeue
, hNotify
);
1688 case SC_HTYPE_SERVICE
:
1689 err
= validate_service_handle(handle
, SERVICE_QUERY_STATUS
, &service
);
1691 case SC_HTYPE_MANAGER
:
1692 err
= validate_scm_handle(handle
, SC_MANAGER_ENUMERATE_SERVICE
, &manager
);
1695 err
= ERROR_INVALID_HANDLE
;
1699 if (err
!= ERROR_SUCCESS
)
1704 WARN("Need support for service creation/deletion notifications\n");
1705 return ERROR_CALL_NOT_IMPLEMENTED
;
1708 notify
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*notify
));
1710 return ERROR_NOT_ENOUGH_SERVER_MEMORY
;
1712 notify
->hdr
.type
= SC_HTYPE_NOTIFY
;
1713 notify
->hdr
.access
= 0;
1715 notify
->event
= CreateEventW(NULL
, TRUE
, FALSE
, NULL
);
1717 notify
->notify_mask
= params
.params
->dwNotifyMask
;
1719 service_lock(service
->service_entry
);
1721 if (service
->notify
)
1723 service_unlock(service
->service_entry
);
1724 sc_notify_release(notify
);
1725 return ERROR_ALREADY_REGISTERED
;
1728 mask
= 1 << (service
->service_entry
->status
.dwCurrentState
- SERVICE_STOPPED
);
1729 if (!service
->status_notified
&& (notify
->notify_mask
& mask
))
1731 fill_notify(notify
, service
->service_entry
);
1732 service
->status_notified
= TRUE
;
1736 sc_notify_retain(notify
);
1737 service
->notify
= notify
;
1740 sc_notify_retain(notify
);
1741 *hNotify
= ¬ify
->hdr
;
1743 service_unlock(service
->service_entry
);
1745 return ERROR_SUCCESS
;
1748 DWORD __cdecl
svcctl_GetNotifyResults(
1749 SC_NOTIFY_RPC_HANDLE hNotify
,
1750 SC_RPC_NOTIFY_PARAMS_LIST
**pList
)
1753 struct sc_notify_handle
*notify
;
1755 WINE_TRACE("(%p, %p)\n", hNotify
, pList
);
1758 return ERROR_INVALID_PARAMETER
;
1762 if ((err
= validate_notify_handle(hNotify
, 0, ¬ify
)) != 0)
1765 sc_notify_retain(notify
);
1766 /* block until there is a result */
1767 err
= WaitForSingleObject(notify
->event
, INFINITE
);
1769 if (err
!= WAIT_OBJECT_0
)
1771 sc_notify_release(notify
);
1775 *pList
= InterlockedExchangePointer((void**)¬ify
->params_list
, NULL
);
1778 sc_notify_release(notify
);
1779 return ERROR_REQUEST_ABORTED
;
1782 sc_notify_release(notify
);
1784 return ERROR_SUCCESS
;
1787 DWORD __cdecl
svcctl_CloseNotifyHandle(
1788 SC_NOTIFY_RPC_HANDLE
*hNotify
,
1791 struct sc_notify_handle
*notify
;
1794 WINE_TRACE("(%p, %p)\n", hNotify
, apc_fired
);
1796 if ((err
= validate_notify_handle(*hNotify
, 0, ¬ify
)) != 0)
1799 sc_notify_release(notify
);
1801 return ERROR_SUCCESS
;
1804 DWORD __cdecl
svcctl_ControlServiceExA(
1805 SC_RPC_HANDLE service
,
1808 SC_RPC_SERVICE_CONTROL_IN_PARAMSA
*in_params
,
1809 SC_RPC_SERVICE_CONTROL_OUT_PARAMSA
*out_params
)
1812 return ERROR_CALL_NOT_IMPLEMENTED
;
1815 DWORD __cdecl
svcctl_ControlServiceExW(
1816 SC_RPC_HANDLE service
,
1819 SC_RPC_SERVICE_CONTROL_IN_PARAMSW
*in_params
,
1820 SC_RPC_SERVICE_CONTROL_OUT_PARAMSW
*out_params
)
1823 return ERROR_CALL_NOT_IMPLEMENTED
;
1826 DWORD __cdecl
svcctl_unknown52(void)
1829 return ERROR_CALL_NOT_IMPLEMENTED
;
1832 DWORD __cdecl
svcctl_unknown53(void)
1835 return ERROR_CALL_NOT_IMPLEMENTED
;
1838 DWORD __cdecl
svcctl_unknown54(void)
1841 return ERROR_CALL_NOT_IMPLEMENTED
;
1844 DWORD __cdecl
svcctl_unknown55(void)
1847 return ERROR_CALL_NOT_IMPLEMENTED
;
1850 DWORD __cdecl
svcctl_QueryServiceConfigEx(
1851 SC_RPC_HANDLE service
,
1853 SC_RPC_CONFIG_INFOW
*info
)
1856 return ERROR_CALL_NOT_IMPLEMENTED
;
1859 DWORD __cdecl
svcctl_QueryServiceObjectSecurity(
1860 SC_RPC_HANDLE service
,
1861 SECURITY_INFORMATION info
,
1867 return ERROR_CALL_NOT_IMPLEMENTED
;
1870 DWORD __cdecl
svcctl_SetServiceObjectSecurity(
1871 SC_RPC_HANDLE service
,
1872 SECURITY_INFORMATION info
,
1877 return ERROR_CALL_NOT_IMPLEMENTED
;
1880 DWORD __cdecl
svcctl_QueryServiceStatus(
1881 SC_RPC_HANDLE service
,
1882 SERVICE_STATUS
*status
)
1885 return ERROR_CALL_NOT_IMPLEMENTED
;
1888 DWORD __cdecl
svcctl_NotifyBootConfigStatus(
1889 SVCCTL_HANDLEW machinename
,
1890 DWORD boot_acceptable
)
1893 return ERROR_CALL_NOT_IMPLEMENTED
;
1896 DWORD __cdecl
svcctl_SCSetServiceBitsW(void)
1899 return ERROR_CALL_NOT_IMPLEMENTED
;
1902 DWORD __cdecl
svcctl_EnumDependentServicesW(
1903 SC_RPC_HANDLE service
,
1908 DWORD
*services_ret
)
1911 return ERROR_CALL_NOT_IMPLEMENTED
;
1914 DWORD __cdecl
svcctl_QueryServiceLockStatusW(
1915 SC_RPC_HANDLE scmanager
,
1916 QUERY_SERVICE_LOCK_STATUSW
*status
,
1921 return ERROR_CALL_NOT_IMPLEMENTED
;
1924 DWORD __cdecl
svcctl_SCSetServiceBitsA(void)
1927 return ERROR_CALL_NOT_IMPLEMENTED
;
1930 DWORD __cdecl
svcctl_ChangeServiceConfigA(
1931 SC_RPC_HANDLE service
,
1934 DWORD error_control
,
1936 LPSTR loadordergroup
,
1942 DWORD password_size
,
1946 return ERROR_CALL_NOT_IMPLEMENTED
;
1949 DWORD __cdecl
svcctl_CreateServiceA(
1950 SC_RPC_HANDLE scmanager
,
1953 DWORD desiredaccess
,
1956 DWORD error_control
,
1958 LPCSTR loadordergroup
,
1960 const BYTE
*dependencies
,
1963 const BYTE
*password
,
1964 DWORD password_size
,
1965 SC_RPC_HANDLE
*service
)
1968 return ERROR_CALL_NOT_IMPLEMENTED
;
1971 DWORD __cdecl
svcctl_EnumDependentServicesA(
1972 SC_RPC_HANDLE service
,
1977 DWORD
*services_ret
)
1980 return ERROR_CALL_NOT_IMPLEMENTED
;
1983 DWORD __cdecl
svcctl_EnumServicesStatusA(
1984 SC_RPC_HANDLE hmngr
,
1994 return ERROR_CALL_NOT_IMPLEMENTED
;
1997 DWORD __cdecl
svcctl_OpenSCManagerA(
1998 MACHINE_HANDLEA MachineName
,
1999 LPCSTR DatabaseName
,
2001 SC_RPC_HANDLE
*handle
)
2004 return ERROR_CALL_NOT_IMPLEMENTED
;
2007 DWORD __cdecl
svcctl_OpenServiceA(
2008 SC_RPC_HANDLE hSCManager
,
2009 LPCSTR lpServiceName
,
2010 DWORD dwDesiredAccess
,
2011 SC_RPC_HANDLE
*phService
)
2014 return ERROR_CALL_NOT_IMPLEMENTED
;
2017 DWORD __cdecl
svcctl_QueryServiceConfigA(
2018 SC_RPC_HANDLE hService
,
2019 QUERY_SERVICE_CONFIGA
*config
,
2024 return ERROR_CALL_NOT_IMPLEMENTED
;
2027 DWORD __cdecl
svcctl_QueryServiceLockStatusA(
2028 SC_RPC_HANDLE scmanager
,
2029 QUERY_SERVICE_LOCK_STATUSA
*status
,
2034 return ERROR_CALL_NOT_IMPLEMENTED
;
2037 DWORD __cdecl
svcctl_StartServiceA(
2038 SC_RPC_HANDLE service
,
2043 return ERROR_CALL_NOT_IMPLEMENTED
;
2046 DWORD __cdecl
svcctl_GetServiceDisplayNameA(
2047 SC_RPC_HANDLE hSCManager
,
2053 return ERROR_CALL_NOT_IMPLEMENTED
;
2056 DWORD __cdecl
svcctl_GetServiceKeyNameA(
2057 SC_RPC_HANDLE hSCManager
,
2063 return ERROR_CALL_NOT_IMPLEMENTED
;
2066 DWORD __cdecl
svcctl_GetCurrentGroupStateW(void)
2069 return ERROR_CALL_NOT_IMPLEMENTED
;
2072 DWORD __cdecl
svcctl_EnumServiceGroupW(
2073 SC_RPC_HANDLE scmanager
,
2075 DWORD service_state
,
2079 DWORD
*returned_size
,
2080 DWORD
*resume_index
,
2084 return ERROR_CALL_NOT_IMPLEMENTED
;
2087 DWORD __cdecl
svcctl_ChangeServiceConfig2A(
2088 SC_RPC_HANDLE service
,
2089 SC_RPC_CONFIG_INFOA info
)
2092 return ERROR_CALL_NOT_IMPLEMENTED
;
2095 DWORD __cdecl
svcctl_QueryServiceConfig2A(
2096 SC_RPC_HANDLE service
,
2103 return ERROR_CALL_NOT_IMPLEMENTED
;
2106 DWORD
RPC_Init(void)
2108 WCHAR transport
[] = SVCCTL_TRANSPORT
;
2109 WCHAR endpoint
[] = SVCCTL_ENDPOINT
;
2112 if (!(cleanup_group
= CreateThreadpoolCleanupGroup()))
2114 WINE_ERR("CreateThreadpoolCleanupGroup failed with error %lu\n", GetLastError());
2115 return GetLastError();
2118 if ((err
= RpcServerUseProtseqEpW(transport
, 0, endpoint
, NULL
)) != ERROR_SUCCESS
)
2120 WINE_ERR("RpcServerUseProtseq failed with error %lu\n", err
);
2124 if ((err
= RpcServerRegisterIf(svcctl_v2_0_s_ifspec
, 0, 0)) != ERROR_SUCCESS
)
2126 WINE_ERR("RpcServerRegisterIf failed with error %lu\n", err
);
2130 if ((err
= RpcServerListen(1, RPC_C_LISTEN_MAX_CALLS_DEFAULT
, TRUE
)) != ERROR_SUCCESS
)
2132 WINE_ERR("RpcServerListen failed with error %lu\n", err
);
2136 NtSetInformationProcess( GetCurrentProcess(), ProcessWineMakeProcessSystem
,
2137 &exit_event
, sizeof(HANDLE
*) );
2138 return ERROR_SUCCESS
;
2143 RpcMgmtStopServerListening(NULL
);
2144 RpcServerUnregisterIf(svcctl_v2_0_s_ifspec
, NULL
, TRUE
);
2145 RpcMgmtWaitServerListen();
2147 CloseThreadpoolCleanupGroupMembers(cleanup_group
, TRUE
, NULL
);
2148 CloseThreadpoolCleanupGroup(cleanup_group
);
2149 CloseHandle(exit_event
);
2152 void __RPC_USER
SC_RPC_HANDLE_rundown(SC_RPC_HANDLE handle
)
2154 SC_RPC_HANDLE_destroy(handle
);
2157 void __RPC_USER
SC_NOTIFY_RPC_HANDLE_rundown(SC_NOTIFY_RPC_HANDLE handle
)
2161 void __RPC_FAR
* __RPC_USER
MIDL_user_allocate(SIZE_T len
)
2163 return HeapAlloc(GetProcessHeap(), 0, len
);
2166 void __RPC_USER
MIDL_user_free(void __RPC_FAR
* ptr
)
2168 HeapFree(GetProcessHeap(), 0, ptr
);