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 NONAMELESSUNION
31 #include "wine/list.h"
32 #include "wine/unicode.h"
33 #include "wine/debug.h"
38 extern HANDLE CDECL
__wine_make_process_system(void);
40 WINE_DEFAULT_DEBUG_CHANNEL(service
);
42 static const GENERIC_MAPPING g_scm_generic
=
44 (STANDARD_RIGHTS_READ
| SC_MANAGER_ENUMERATE_SERVICE
| SC_MANAGER_QUERY_LOCK_STATUS
),
45 (STANDARD_RIGHTS_WRITE
| SC_MANAGER_CREATE_SERVICE
| SC_MANAGER_MODIFY_BOOT_CONFIG
),
46 (STANDARD_RIGHTS_EXECUTE
| SC_MANAGER_CONNECT
| SC_MANAGER_LOCK
),
50 static const GENERIC_MAPPING g_svc_generic
=
52 (STANDARD_RIGHTS_READ
| SERVICE_QUERY_CONFIG
| SERVICE_QUERY_STATUS
| SERVICE_INTERROGATE
| SERVICE_ENUMERATE_DEPENDENTS
),
53 (STANDARD_RIGHTS_WRITE
| SERVICE_CHANGE_CONFIG
),
54 (STANDARD_RIGHTS_EXECUTE
| SERVICE_START
| SERVICE_STOP
| SERVICE_PAUSE_CONTINUE
| SERVICE_USER_DEFINED_CONTROL
),
60 SC_HTYPE_DONT_CARE
= 0,
72 struct sc_manager_handle
/* service control manager handle */
75 struct scmdatabase
*db
;
78 struct sc_service_handle
/* service handle */
81 struct service_entry
*service_entry
;
84 struct sc_notify_handle
87 struct sc_service_handle
*service
;
91 SC_RPC_NOTIFY_PARAMS_LIST
*params_list
;
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, %d)\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 %x, needed %x\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, %x)\n", wine_dbgstr_w(MachineName
), wine_dbgstr_w(DatabaseName
), dwAccessMask
);
276 if (DatabaseName
!= NULL
&& DatabaseName
[0])
278 if (strcmpW(DatabaseName
, SERVICES_FAILED_DATABASEW
) == 0)
279 return ERROR_DATABASE_DOES_NOT_EXIST
;
280 if (strcmpW(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 if (service
->service_entry
->notify
&&
315 service
->service_entry
->notify
->service
== service
)
317 SetEvent(service
->service_entry
->notify
->event
);
318 sc_notify_release(service
->service_entry
->notify
);
319 service
->service_entry
->notify
= NULL
;
321 service_unlock(service
->service_entry
);
322 release_service(service
->service_entry
);
323 HeapFree(GetProcessHeap(), 0, service
);
327 WINE_ERR("invalid handle type %d\n", hdr
->type
);
328 RpcRaiseException(ERROR_INVALID_HANDLE
);
332 DWORD __cdecl
svcctl_GetServiceDisplayNameW(
333 SC_RPC_HANDLE hSCManager
,
334 LPCWSTR lpServiceName
,
338 struct sc_manager_handle
*manager
;
339 struct service_entry
*entry
;
342 WINE_TRACE("(%s, %d)\n", wine_dbgstr_w(lpServiceName
), *cchBufSize
);
344 if ((err
= validate_scm_handle(hSCManager
, 0, &manager
)) != ERROR_SUCCESS
)
347 scmdatabase_lock(manager
->db
);
349 entry
= scmdatabase_find_service(manager
->db
, lpServiceName
);
354 name
= get_display_name(entry
);
356 if (len
<= *cchBufSize
)
359 memcpy(lpBuffer
, name
, (len
+ 1)*sizeof(*name
));
362 err
= ERROR_INSUFFICIENT_BUFFER
;
366 err
= ERROR_SERVICE_DOES_NOT_EXIST
;
368 scmdatabase_unlock(manager
->db
);
370 if (err
!= ERROR_SUCCESS
)
376 DWORD __cdecl
svcctl_GetServiceKeyNameW(
377 SC_RPC_HANDLE hSCManager
,
378 LPCWSTR lpServiceDisplayName
,
382 struct service_entry
*entry
;
383 struct sc_manager_handle
*manager
;
386 WINE_TRACE("(%s, %d)\n", wine_dbgstr_w(lpServiceDisplayName
), *cchBufSize
);
388 if ((err
= validate_scm_handle(hSCManager
, 0, &manager
)) != ERROR_SUCCESS
)
391 scmdatabase_lock(manager
->db
);
393 entry
= scmdatabase_find_service_by_displayname(manager
->db
, lpServiceDisplayName
);
397 len
= strlenW(entry
->name
);
398 if (len
<= *cchBufSize
)
401 memcpy(lpBuffer
, entry
->name
, (len
+ 1)*sizeof(*entry
->name
));
404 err
= ERROR_INSUFFICIENT_BUFFER
;
408 err
= ERROR_SERVICE_DOES_NOT_EXIST
;
410 scmdatabase_unlock(manager
->db
);
412 if (err
!= ERROR_SUCCESS
)
418 static DWORD
create_handle_for_service(struct service_entry
*entry
, DWORD dwDesiredAccess
, SC_RPC_HANDLE
*phService
)
420 struct sc_service_handle
*service
;
422 if (!(service
= HeapAlloc(GetProcessHeap(), 0, sizeof(*service
))))
424 release_service(entry
);
425 return ERROR_NOT_ENOUGH_SERVER_MEMORY
;
428 if (dwDesiredAccess
& MAXIMUM_ALLOWED
)
429 dwDesiredAccess
|= SERVICE_ALL_ACCESS
;
431 service
->hdr
.type
= SC_HTYPE_SERVICE
;
432 service
->hdr
.access
= dwDesiredAccess
;
433 RtlMapGenericMask(&service
->hdr
.access
, &g_svc_generic
);
434 service
->service_entry
= entry
;
436 *phService
= &service
->hdr
;
437 return ERROR_SUCCESS
;
440 DWORD __cdecl
svcctl_OpenServiceW(
441 SC_RPC_HANDLE hSCManager
,
442 LPCWSTR lpServiceName
,
443 DWORD dwDesiredAccess
,
444 SC_RPC_HANDLE
*phService
)
446 struct sc_manager_handle
*manager
;
447 struct service_entry
*entry
;
450 WINE_TRACE("(%s, 0x%x)\n", wine_dbgstr_w(lpServiceName
), dwDesiredAccess
);
452 if ((err
= validate_scm_handle(hSCManager
, 0, &manager
)) != ERROR_SUCCESS
)
454 if (!validate_service_name(lpServiceName
))
455 return ERROR_INVALID_NAME
;
457 scmdatabase_lock(manager
->db
);
458 entry
= grab_service(scmdatabase_find_service(manager
->db
, lpServiceName
));
459 scmdatabase_unlock(manager
->db
);
462 return ERROR_SERVICE_DOES_NOT_EXIST
;
464 return create_handle_for_service(entry
, dwDesiredAccess
, phService
);
467 static DWORD
parse_dependencies(const WCHAR
*dependencies
, struct service_entry
*entry
)
469 WCHAR
*services
= NULL
, *groups
, *s
;
470 DWORD len
, len_services
= 0, len_groups
= 0;
471 const WCHAR
*ptr
= dependencies
;
473 if (!dependencies
|| !dependencies
[0])
475 entry
->dependOnServices
= NULL
;
476 entry
->dependOnGroups
= NULL
;
477 return ERROR_SUCCESS
;
482 len
= strlenW(ptr
) + 1;
483 if (ptr
[0] == '+' && ptr
[1])
484 len_groups
+= len
- 1;
489 if (!len_services
) entry
->dependOnServices
= NULL
;
492 services
= HeapAlloc(GetProcessHeap(), 0, (len_services
+ 1) * sizeof(WCHAR
));
494 return ERROR_OUTOFMEMORY
;
500 len
= strlenW(ptr
) + 1;
509 entry
->dependOnServices
= services
;
511 if (!len_groups
) entry
->dependOnGroups
= NULL
;
514 groups
= HeapAlloc(GetProcessHeap(), 0, (len_groups
+ 1) * sizeof(WCHAR
));
517 HeapFree(GetProcessHeap(), 0, services
);
518 return ERROR_OUTOFMEMORY
;
524 len
= strlenW(ptr
) + 1;
525 if (ptr
[0] == '+' && ptr
[1])
533 entry
->dependOnGroups
= groups
;
536 return ERROR_SUCCESS
;
539 static DWORD
create_serviceW(
540 SC_RPC_HANDLE hSCManager
,
541 LPCWSTR lpServiceName
,
542 LPCWSTR lpDisplayName
,
543 DWORD dwDesiredAccess
,
546 DWORD dwErrorControl
,
547 LPCWSTR lpBinaryPathName
,
548 LPCWSTR lpLoadOrderGroup
,
550 const BYTE
*lpDependencies
,
551 DWORD dwDependenciesSize
,
552 LPCWSTR lpServiceStartName
,
553 const BYTE
*lpPassword
,
554 DWORD dwPasswordSize
,
555 SC_RPC_HANDLE
*phService
,
558 struct service_entry
*entry
, *found
;
559 struct sc_manager_handle
*manager
;
562 WINE_TRACE("(%s, %s, 0x%x, %s)\n", wine_dbgstr_w(lpServiceName
), wine_dbgstr_w(lpDisplayName
), dwDesiredAccess
, wine_dbgstr_w(lpBinaryPathName
));
564 if ((err
= validate_scm_handle(hSCManager
, SC_MANAGER_CREATE_SERVICE
, &manager
)) != ERROR_SUCCESS
)
567 if (!validate_service_name(lpServiceName
))
568 return ERROR_INVALID_NAME
;
569 if (!check_multisz((LPCWSTR
)lpDependencies
, dwDependenciesSize
) || !lpServiceName
[0] || !lpBinaryPathName
[0])
570 return ERROR_INVALID_PARAMETER
;
573 WINE_FIXME("Don't know how to add a password\n"); /* I always get ERROR_GEN_FAILURE */
575 err
= service_create(lpServiceName
, &entry
);
576 if (err
!= ERROR_SUCCESS
)
579 err
= parse_dependencies((LPCWSTR
)lpDependencies
, entry
);
580 if (err
!= ERROR_SUCCESS
) {
581 free_service_entry(entry
);
585 entry
->is_wow64
= is_wow64
;
586 entry
->config
.dwServiceType
= entry
->status
.dwServiceType
= dwServiceType
;
587 entry
->config
.dwStartType
= dwStartType
;
588 entry
->config
.dwErrorControl
= dwErrorControl
;
589 entry
->config
.lpBinaryPathName
= strdupW(lpBinaryPathName
);
590 entry
->config
.lpLoadOrderGroup
= strdupW(lpLoadOrderGroup
);
591 entry
->config
.lpServiceStartName
= strdupW(lpServiceStartName
);
592 entry
->config
.lpDisplayName
= strdupW(lpDisplayName
);
594 if (lpdwTagId
) /* TODO: In most situations a non-NULL TagId will generate an ERROR_INVALID_PARAMETER. */
595 entry
->config
.dwTagId
= *lpdwTagId
;
597 entry
->config
.dwTagId
= 0;
599 /* other fields NULL*/
601 if (!validate_service_config(entry
))
603 WINE_ERR("Invalid data while trying to create service\n");
604 free_service_entry(entry
);
605 return ERROR_INVALID_PARAMETER
;
608 scmdatabase_lock(manager
->db
);
610 if ((found
= scmdatabase_find_service(manager
->db
, lpServiceName
)))
612 err
= is_marked_for_delete(found
) ? ERROR_SERVICE_MARKED_FOR_DELETE
: ERROR_SERVICE_EXISTS
;
613 scmdatabase_unlock(manager
->db
);
614 free_service_entry(entry
);
618 if (scmdatabase_find_service_by_displayname(manager
->db
, get_display_name(entry
)))
620 scmdatabase_unlock(manager
->db
);
621 free_service_entry(entry
);
622 return ERROR_DUPLICATE_SERVICE_NAME
;
625 err
= scmdatabase_add_service(manager
->db
, entry
);
626 if (err
!= ERROR_SUCCESS
)
628 scmdatabase_unlock(manager
->db
);
629 free_service_entry(entry
);
632 scmdatabase_unlock(manager
->db
);
634 return create_handle_for_service(entry
, dwDesiredAccess
, phService
);
637 DWORD __cdecl
svcctl_CreateServiceW(
638 SC_RPC_HANDLE hSCManager
,
639 LPCWSTR lpServiceName
,
640 LPCWSTR lpDisplayName
,
641 DWORD dwDesiredAccess
,
644 DWORD dwErrorControl
,
645 LPCWSTR lpBinaryPathName
,
646 LPCWSTR lpLoadOrderGroup
,
648 const BYTE
*lpDependencies
,
649 DWORD dwDependenciesSize
,
650 LPCWSTR lpServiceStartName
,
651 const BYTE
*lpPassword
,
652 DWORD dwPasswordSize
,
653 SC_RPC_HANDLE
*phService
)
655 WINE_TRACE("(%s, %s, 0x%x, %s)\n", wine_dbgstr_w(lpServiceName
), wine_dbgstr_w(lpDisplayName
), dwDesiredAccess
, wine_dbgstr_w(lpBinaryPathName
));
656 return create_serviceW(hSCManager
, lpServiceName
, lpDisplayName
, dwDesiredAccess
, dwServiceType
, dwStartType
,
657 dwErrorControl
, lpBinaryPathName
, lpLoadOrderGroup
, lpdwTagId
, lpDependencies
, dwDependenciesSize
, lpServiceStartName
,
658 lpPassword
, dwPasswordSize
, phService
, FALSE
);
661 DWORD __cdecl
svcctl_DeleteService(
662 SC_RPC_HANDLE hService
)
664 struct sc_service_handle
*service
;
667 if ((err
= validate_service_handle(hService
, DELETE
, &service
)) != ERROR_SUCCESS
)
670 service_lock(service
->service_entry
);
672 if (!is_marked_for_delete(service
->service_entry
))
673 err
= mark_for_delete(service
->service_entry
);
675 err
= ERROR_SERVICE_MARKED_FOR_DELETE
;
677 service_unlock(service
->service_entry
);
682 DWORD __cdecl
svcctl_QueryServiceConfigW(
683 SC_RPC_HANDLE hService
,
684 QUERY_SERVICE_CONFIGW
*config
,
688 struct sc_service_handle
*service
;
691 WINE_TRACE("(%p)\n", config
);
693 if ((err
= validate_service_handle(hService
, SERVICE_QUERY_CONFIG
, &service
)) != 0)
696 service_lock(service
->service_entry
);
697 config
->dwServiceType
= service
->service_entry
->config
.dwServiceType
;
698 config
->dwStartType
= service
->service_entry
->config
.dwStartType
;
699 config
->dwErrorControl
= service
->service_entry
->config
.dwErrorControl
;
700 config
->lpBinaryPathName
= strdupW(service
->service_entry
->config
.lpBinaryPathName
);
701 config
->lpLoadOrderGroup
= strdupW(service
->service_entry
->config
.lpLoadOrderGroup
);
702 config
->dwTagId
= service
->service_entry
->config
.dwTagId
;
703 config
->lpDependencies
= NULL
; /* TODO */
704 config
->lpServiceStartName
= strdupW(service
->service_entry
->config
.lpServiceStartName
);
705 config
->lpDisplayName
= strdupW(service
->service_entry
->config
.lpDisplayName
);
706 service_unlock(service
->service_entry
);
708 return ERROR_SUCCESS
;
711 DWORD __cdecl
svcctl_ChangeServiceConfigW(
712 SC_RPC_HANDLE hService
,
715 DWORD dwErrorControl
,
716 LPCWSTR lpBinaryPathName
,
717 LPCWSTR lpLoadOrderGroup
,
719 const BYTE
*lpDependencies
,
720 DWORD dwDependenciesSize
,
721 LPCWSTR lpServiceStartName
,
722 const BYTE
*lpPassword
,
723 DWORD dwPasswordSize
,
724 LPCWSTR lpDisplayName
)
726 struct service_entry new_entry
, *entry
;
727 struct sc_service_handle
*service
;
732 if ((err
= validate_service_handle(hService
, SERVICE_CHANGE_CONFIG
, &service
)) != 0)
735 if (!check_multisz((LPCWSTR
)lpDependencies
, dwDependenciesSize
))
736 return ERROR_INVALID_PARAMETER
;
738 /* first check if the new configuration is correct */
739 service_lock(service
->service_entry
);
741 if (is_marked_for_delete(service
->service_entry
))
743 service_unlock(service
->service_entry
);
744 return ERROR_SERVICE_MARKED_FOR_DELETE
;
747 if (lpDisplayName
!= NULL
&&
748 (entry
= scmdatabase_find_service_by_displayname(service
->service_entry
->db
, lpDisplayName
)) &&
749 (entry
!= service
->service_entry
))
751 service_unlock(service
->service_entry
);
752 return ERROR_DUPLICATE_SERVICE_NAME
;
755 new_entry
= *service
->service_entry
;
757 if (dwServiceType
!= SERVICE_NO_CHANGE
)
758 new_entry
.config
.dwServiceType
= dwServiceType
;
760 if (dwStartType
!= SERVICE_NO_CHANGE
)
761 new_entry
.config
.dwStartType
= dwStartType
;
763 if (dwErrorControl
!= SERVICE_NO_CHANGE
)
764 new_entry
.config
.dwErrorControl
= dwErrorControl
;
766 if (lpBinaryPathName
!= NULL
)
767 new_entry
.config
.lpBinaryPathName
= (LPWSTR
)lpBinaryPathName
;
769 if (lpLoadOrderGroup
!= NULL
)
770 new_entry
.config
.lpLoadOrderGroup
= (LPWSTR
)lpLoadOrderGroup
;
772 if (lpdwTagId
!= NULL
)
773 WINE_FIXME("Changing tag id not supported\n");
775 if (lpServiceStartName
!= NULL
)
776 new_entry
.config
.lpServiceStartName
= (LPWSTR
)lpServiceStartName
;
778 if (lpPassword
!= NULL
)
779 WINE_FIXME("Setting password not supported\n");
781 if (lpDisplayName
!= NULL
)
782 new_entry
.config
.lpDisplayName
= (LPWSTR
)lpDisplayName
;
784 err
= parse_dependencies((LPCWSTR
)lpDependencies
, &new_entry
);
785 if (err
!= ERROR_SUCCESS
)
787 service_unlock(service
->service_entry
);
791 if (!validate_service_config(&new_entry
))
793 WINE_ERR("The configuration after the change wouldn't be valid\n");
794 service_unlock(service
->service_entry
);
795 return ERROR_INVALID_PARAMETER
;
798 /* configuration OK. The strings needs to be duplicated */
799 if (lpBinaryPathName
!= NULL
)
800 new_entry
.config
.lpBinaryPathName
= strdupW(lpBinaryPathName
);
802 if (lpLoadOrderGroup
!= NULL
)
803 new_entry
.config
.lpLoadOrderGroup
= strdupW(lpLoadOrderGroup
);
805 if (lpServiceStartName
!= NULL
)
806 new_entry
.config
.lpServiceStartName
= strdupW(lpServiceStartName
);
808 if (lpDisplayName
!= NULL
)
809 new_entry
.config
.lpDisplayName
= strdupW(lpDisplayName
);
811 /* try to save to Registry, commit or rollback depending on success */
812 err
= save_service_config(&new_entry
);
813 if (ERROR_SUCCESS
== err
)
815 free_service_strings(service
->service_entry
, &new_entry
);
816 *service
->service_entry
= new_entry
;
818 else free_service_strings(&new_entry
, service
->service_entry
);
819 service_unlock(service
->service_entry
);
824 static void fill_notify(struct sc_notify_handle
*notify
)
826 SC_RPC_NOTIFY_PARAMS_LIST
*list
;
827 SERVICE_NOTIFY_STATUS_CHANGE_PARAMS_2
*cparams
;
829 list
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
,
830 sizeof(SC_RPC_NOTIFY_PARAMS_LIST
) + sizeof(SERVICE_NOTIFY_STATUS_CHANGE_PARAMS_2
));
834 cparams
= (SERVICE_NOTIFY_STATUS_CHANGE_PARAMS_2
*)(list
+ 1);
836 cparams
->dwNotifyMask
= notify
->notify_mask
;
837 memcpy(&cparams
->ServiceStatus
, ¬ify
->service
->service_entry
->status
,
838 sizeof(SERVICE_STATUS_PROCESS
));
839 cparams
->dwNotificationStatus
= ERROR_SUCCESS
;
840 cparams
->dwNotificationTriggered
= 1 << (cparams
->ServiceStatus
.dwCurrentState
- SERVICE_STOPPED
);
841 cparams
->pszServiceNames
= NULL
;
845 list
->NotifyParamsArray
[0].dwInfoLevel
= 2;
846 list
->NotifyParamsArray
[0].u
.params
= cparams
;
848 InterlockedExchangePointer((void**)¬ify
->params_list
, list
);
850 SetEvent(notify
->event
);
853 DWORD __cdecl
svcctl_SetServiceStatus(
854 SC_RPC_HANDLE hServiceStatus
,
855 LPSERVICE_STATUS lpServiceStatus
)
857 struct sc_service_handle
*service
;
858 struct process_entry
*process
;
861 WINE_TRACE("(%p, %p)\n", hServiceStatus
, lpServiceStatus
);
863 if ((err
= validate_service_handle(hServiceStatus
, SERVICE_SET_STATUS
, &service
)) != 0)
866 service_lock(service
->service_entry
);
868 /* FIXME: be a bit more discriminant about what parts of the status we set
869 * and check that fields are valid */
870 service
->service_entry
->status
= *lpServiceStatus
;
871 SetEvent(service
->service_entry
->status_changed_event
);
873 if ((process
= service
->service_entry
->process
) &&
874 lpServiceStatus
->dwCurrentState
== SERVICE_STOPPED
)
876 service
->service_entry
->process
= NULL
;
877 if (!--process
->use_count
)
878 terminate_after_timeout(process
, service_kill_timeout
);
879 if (service
->service_entry
->shared_process
&& process
->use_count
<= 1)
880 shutdown_shared_process(process
);
881 release_process(process
);
884 mask
= 1 << (service
->service_entry
->status
.dwCurrentState
- SERVICE_STOPPED
);
885 if (service
->service_entry
->notify
&&
886 (service
->service_entry
->notify
->notify_mask
& mask
))
888 struct sc_notify_handle
*notify
= service
->service_entry
->notify
;
890 service
->service_entry
->notify
= NULL
;
891 sc_notify_release(notify
);
892 service
->service_entry
->status_notified
= TRUE
;
895 service
->service_entry
->status_notified
= FALSE
;
897 service_unlock(service
->service_entry
);
899 return ERROR_SUCCESS
;
902 DWORD __cdecl
svcctl_ChangeServiceConfig2W( SC_RPC_HANDLE hService
, SC_RPC_CONFIG_INFOW config
)
904 struct sc_service_handle
*service
;
907 if ((err
= validate_service_handle(hService
, SERVICE_CHANGE_CONFIG
, &service
)) != 0)
910 switch (config
.dwInfoLevel
)
912 case SERVICE_CONFIG_DESCRIPTION
:
916 if (!config
.u
.descr
->lpDescription
)
919 if (config
.u
.descr
->lpDescription
[0])
921 if (!(descr
= strdupW( config
.u
.descr
->lpDescription
)))
922 return ERROR_NOT_ENOUGH_MEMORY
;
925 WINE_TRACE( "changing service %p descr to %s\n", service
, wine_dbgstr_w(descr
) );
926 service_lock( service
->service_entry
);
927 HeapFree( GetProcessHeap(), 0, service
->service_entry
->description
);
928 service
->service_entry
->description
= descr
;
929 save_service_config( service
->service_entry
);
930 service_unlock( service
->service_entry
);
933 case SERVICE_CONFIG_FAILURE_ACTIONS
:
934 WINE_FIXME( "SERVICE_CONFIG_FAILURE_ACTIONS not implemented: period %u msg %s cmd %s\n",
935 config
.u
.actions
->dwResetPeriod
,
936 wine_dbgstr_w(config
.u
.actions
->lpRebootMsg
),
937 wine_dbgstr_w(config
.u
.actions
->lpCommand
) );
939 case SERVICE_CONFIG_PRESHUTDOWN_INFO
:
940 WINE_TRACE( "changing service %p preshutdown timeout to %d\n",
941 service
, config
.u
.preshutdown
->dwPreshutdownTimeout
);
942 service_lock( service
->service_entry
);
943 service
->service_entry
->preshutdown_timeout
= config
.u
.preshutdown
->dwPreshutdownTimeout
;
944 save_service_config( service
->service_entry
);
945 service_unlock( service
->service_entry
);
948 WINE_FIXME("level %u not implemented\n", config
.dwInfoLevel
);
949 err
= ERROR_INVALID_LEVEL
;
955 DWORD __cdecl
svcctl_QueryServiceConfig2W( SC_RPC_HANDLE hService
, DWORD level
,
956 BYTE
*buffer
, DWORD size
, LPDWORD needed
)
958 struct sc_service_handle
*service
;
961 memset(buffer
, 0, size
);
963 if ((err
= validate_service_handle(hService
, SERVICE_QUERY_STATUS
, &service
)) != 0)
968 case SERVICE_CONFIG_DESCRIPTION
:
970 struct service_description
*desc
= (struct service_description
*)buffer
;
971 DWORD total_size
= sizeof(*desc
);
973 service_lock(service
->service_entry
);
974 if (service
->service_entry
->description
)
975 total_size
+= strlenW(service
->service_entry
->description
) * sizeof(WCHAR
);
977 *needed
= total_size
;
978 if (size
>= total_size
)
980 if (service
->service_entry
->description
)
982 strcpyW( desc
->description
, service
->service_entry
->description
);
983 desc
->size
= total_size
- FIELD_OFFSET(struct service_description
, description
);
987 desc
->description
[0] = 0;
991 else err
= ERROR_INSUFFICIENT_BUFFER
;
992 service_unlock(service
->service_entry
);
996 case SERVICE_CONFIG_PRESHUTDOWN_INFO
:
997 service_lock(service
->service_entry
);
999 *needed
= sizeof(SERVICE_PRESHUTDOWN_INFO
);
1000 if (size
>= *needed
)
1001 ((LPSERVICE_PRESHUTDOWN_INFO
)buffer
)->dwPreshutdownTimeout
=
1002 service
->service_entry
->preshutdown_timeout
;
1003 else err
= ERROR_INSUFFICIENT_BUFFER
;
1005 service_unlock(service
->service_entry
);
1009 WINE_FIXME("level %u not implemented\n", level
);
1010 err
= ERROR_INVALID_LEVEL
;
1016 static void fill_status_process(SERVICE_STATUS_PROCESS
*status
, struct service_entry
*service
)
1018 struct process_entry
*process
= service
->process
;
1019 memcpy(status
, &service
->status
, sizeof(service
->status
));
1020 status
->dwProcessId
= process
? process
->process_id
: 0;
1021 status
->dwServiceFlags
= 0;
1024 DWORD __cdecl
svcctl_QueryServiceStatusEx(
1025 SC_RPC_HANDLE hService
,
1026 SC_STATUS_TYPE InfoLevel
,
1029 LPDWORD pcbBytesNeeded
)
1031 struct sc_service_handle
*service
;
1033 LPSERVICE_STATUS_PROCESS pSvcStatusData
;
1035 memset(lpBuffer
, 0, cbBufSize
);
1037 if ((err
= validate_service_handle(hService
, SERVICE_QUERY_STATUS
, &service
)) != 0)
1040 if (InfoLevel
!= SC_STATUS_PROCESS_INFO
)
1041 return ERROR_INVALID_LEVEL
;
1043 pSvcStatusData
= (LPSERVICE_STATUS_PROCESS
) lpBuffer
;
1044 if (pSvcStatusData
== NULL
)
1045 return ERROR_INVALID_PARAMETER
;
1047 if (cbBufSize
< sizeof(SERVICE_STATUS_PROCESS
))
1049 if( pcbBytesNeeded
!= NULL
)
1050 *pcbBytesNeeded
= sizeof(SERVICE_STATUS_PROCESS
);
1052 return ERROR_INSUFFICIENT_BUFFER
;
1055 service_lock(service
->service_entry
);
1056 fill_status_process(pSvcStatusData
, service
->service_entry
);
1057 service_unlock(service
->service_entry
);
1059 return ERROR_SUCCESS
;
1062 /******************************************************************************
1063 * service_accepts_control
1065 static BOOL
service_accepts_control(const struct service_entry
*service
, DWORD dwControl
)
1067 DWORD a
= service
->status
.dwControlsAccepted
;
1069 if (dwControl
>= 128 && dwControl
<= 255)
1074 case SERVICE_CONTROL_INTERROGATE
:
1076 case SERVICE_CONTROL_STOP
:
1077 if (a
&SERVICE_ACCEPT_STOP
)
1080 case SERVICE_CONTROL_SHUTDOWN
:
1081 if (a
&SERVICE_ACCEPT_SHUTDOWN
)
1084 case SERVICE_CONTROL_PAUSE
:
1085 case SERVICE_CONTROL_CONTINUE
:
1086 if (a
&SERVICE_ACCEPT_PAUSE_CONTINUE
)
1089 case SERVICE_CONTROL_PARAMCHANGE
:
1090 if (a
&SERVICE_ACCEPT_PARAMCHANGE
)
1093 case SERVICE_CONTROL_NETBINDADD
:
1094 case SERVICE_CONTROL_NETBINDREMOVE
:
1095 case SERVICE_CONTROL_NETBINDENABLE
:
1096 case SERVICE_CONTROL_NETBINDDISABLE
:
1097 if (a
&SERVICE_ACCEPT_NETBINDCHANGE
)
1099 case SERVICE_CONTROL_HARDWAREPROFILECHANGE
:
1100 if (a
&SERVICE_ACCEPT_HARDWAREPROFILECHANGE
)
1103 case SERVICE_CONTROL_POWEREVENT
:
1104 if (a
&SERVICE_ACCEPT_POWEREVENT
)
1107 case SERVICE_CONTROL_SESSIONCHANGE
:
1108 if (a
&SERVICE_ACCEPT_SESSIONCHANGE
)
1115 /******************************************************************************
1116 * process_send_command
1118 static BOOL
process_send_command(struct process_entry
*process
, const void *data
, DWORD size
, DWORD
*result
)
1120 OVERLAPPED overlapped
;
1124 overlapped
.hEvent
= process
->overlapped_event
;
1125 r
= WriteFile(process
->control_pipe
, data
, size
, &count
, &overlapped
);
1126 if (!r
&& GetLastError() == ERROR_IO_PENDING
)
1128 ret
= WaitForSingleObject(process
->overlapped_event
, service_pipe_timeout
);
1129 if (ret
== WAIT_TIMEOUT
)
1131 WINE_ERR("sending command timed out\n");
1132 *result
= ERROR_SERVICE_REQUEST_TIMEOUT
;
1135 r
= GetOverlappedResult(process
->control_pipe
, &overlapped
, &count
, FALSE
);
1137 if (!r
|| count
!= size
)
1139 WINE_ERR("service protocol error - failed to write pipe!\n");
1140 *result
= (!r
? GetLastError() : ERROR_WRITE_FAULT
);
1143 r
= ReadFile(process
->control_pipe
, result
, sizeof *result
, &count
, &overlapped
);
1144 if (!r
&& GetLastError() == ERROR_IO_PENDING
)
1146 ret
= WaitForSingleObject(process
->overlapped_event
, service_pipe_timeout
);
1147 if (ret
== WAIT_TIMEOUT
)
1149 WINE_ERR("receiving command result timed out\n");
1150 *result
= ERROR_SERVICE_REQUEST_TIMEOUT
;
1153 r
= GetOverlappedResult(process
->control_pipe
, &overlapped
, &count
, FALSE
);
1155 if (!r
|| count
!= sizeof *result
)
1157 WINE_ERR("service protocol error - failed to read pipe "
1158 "r = %d count = %d!\n", r
, count
);
1159 *result
= (!r
? GetLastError() : ERROR_READ_FAULT
);
1166 /******************************************************************************
1167 * process_send_control
1169 BOOL
process_send_control(struct process_entry
*process
, BOOL shared_process
, const WCHAR
*name
,
1170 DWORD control
, const BYTE
*data
, DWORD data_size
, DWORD
*result
)
1172 service_start_info
*ssi
;
1178 control
|= SERVICE_CONTROL_FORWARD_FLAG
;
1179 data
= (BYTE
*)name
;
1180 data_size
= (strlenW(name
) + 1) * sizeof(WCHAR
);
1184 /* calculate how much space we need to send the startup info */
1185 len
= (strlenW(name
) + 1) * sizeof(WCHAR
) + data_size
;
1187 ssi
= HeapAlloc(GetProcessHeap(),0,FIELD_OFFSET(service_start_info
, data
[len
]));
1188 ssi
->magic
= SERVICE_PROTOCOL_MAGIC
;
1189 ssi
->control
= control
;
1190 ssi
->total_size
= FIELD_OFFSET(service_start_info
, data
[len
]);
1191 ssi
->name_size
= strlenW(name
) + 1;
1192 strcpyW((WCHAR
*)ssi
->data
, name
);
1193 if (data_size
) memcpy(&ssi
->data
[ssi
->name_size
* sizeof(WCHAR
)], data
, data_size
);
1195 r
= process_send_command(process
, ssi
, ssi
->total_size
, result
);
1196 HeapFree( GetProcessHeap(), 0, ssi
);
1200 DWORD __cdecl
svcctl_StartServiceW(
1201 SC_RPC_HANDLE hService
,
1202 DWORD dwNumServiceArgs
,
1203 LPCWSTR
*lpServiceArgVectors
)
1205 struct sc_service_handle
*service
;
1208 WINE_TRACE("(%p, %d, %p)\n", hService
, dwNumServiceArgs
, lpServiceArgVectors
);
1210 if ((err
= validate_service_handle(hService
, SERVICE_START
, &service
)) != 0)
1213 if (service
->service_entry
->config
.dwStartType
== SERVICE_DISABLED
)
1214 return ERROR_SERVICE_DISABLED
;
1216 if (!scmdatabase_lock_startup(service
->service_entry
->db
))
1217 return ERROR_SERVICE_DATABASE_LOCKED
;
1219 err
= service_start(service
->service_entry
, dwNumServiceArgs
, lpServiceArgVectors
);
1221 scmdatabase_unlock_startup(service
->service_entry
->db
);
1225 DWORD __cdecl
svcctl_ControlService(
1226 SC_RPC_HANDLE hService
,
1228 SERVICE_STATUS
*lpServiceStatus
)
1230 DWORD access_required
;
1231 struct sc_service_handle
*service
;
1232 struct process_entry
*process
;
1233 BOOL shared_process
;
1236 WINE_TRACE("(%p, %d, %p)\n", hService
, dwControl
, lpServiceStatus
);
1240 case SERVICE_CONTROL_CONTINUE
:
1241 case SERVICE_CONTROL_NETBINDADD
:
1242 case SERVICE_CONTROL_NETBINDDISABLE
:
1243 case SERVICE_CONTROL_NETBINDENABLE
:
1244 case SERVICE_CONTROL_NETBINDREMOVE
:
1245 case SERVICE_CONTROL_PARAMCHANGE
:
1246 case SERVICE_CONTROL_PAUSE
:
1247 access_required
= SERVICE_PAUSE_CONTINUE
;
1249 case SERVICE_CONTROL_INTERROGATE
:
1250 access_required
= SERVICE_INTERROGATE
;
1252 case SERVICE_CONTROL_STOP
:
1253 access_required
= SERVICE_STOP
;
1256 if (dwControl
>= 128 && dwControl
<= 255)
1257 access_required
= SERVICE_USER_DEFINED_CONTROL
;
1259 return ERROR_INVALID_PARAMETER
;
1262 if ((result
= validate_service_handle(hService
, access_required
, &service
)) != 0)
1265 service_lock(service
->service_entry
);
1267 result
= ERROR_SUCCESS
;
1268 switch (service
->service_entry
->status
.dwCurrentState
)
1270 case SERVICE_STOPPED
:
1271 result
= ERROR_SERVICE_NOT_ACTIVE
;
1273 case SERVICE_START_PENDING
:
1274 if (dwControl
==SERVICE_CONTROL_STOP
)
1277 case SERVICE_STOP_PENDING
:
1278 result
= ERROR_SERVICE_CANNOT_ACCEPT_CTRL
;
1282 if (result
== ERROR_SUCCESS
&& service
->service_entry
->force_shutdown
)
1284 result
= ERROR_SERVICE_CANNOT_ACCEPT_CTRL
;
1285 if ((process
= service
->service_entry
->process
))
1287 service
->service_entry
->process
= NULL
;
1288 if (!--process
->use_count
) process_terminate(process
);
1289 release_process(process
);
1293 if (result
!= ERROR_SUCCESS
)
1295 if (lpServiceStatus
) *lpServiceStatus
= service
->service_entry
->status
;
1296 service_unlock(service
->service_entry
);
1300 if (!service_accepts_control(service
->service_entry
, dwControl
))
1302 service_unlock(service
->service_entry
);
1303 return ERROR_INVALID_SERVICE_CONTROL
;
1306 /* Remember that we tried to shutdown this service. When the service is
1307 * still running on the second invocation, it will be forcefully killed. */
1308 if (dwControl
== SERVICE_CONTROL_STOP
)
1309 service
->service_entry
->force_shutdown
= TRUE
;
1311 /* Hold a reference to the process while sending the command. */
1312 process
= grab_process(service
->service_entry
->process
);
1313 shared_process
= service
->service_entry
->shared_process
;
1314 service_unlock(service
->service_entry
);
1317 return ERROR_SERVICE_CANNOT_ACCEPT_CTRL
;
1319 result
= WaitForSingleObject(process
->control_mutex
, 30000);
1320 if (result
!= WAIT_OBJECT_0
)
1322 release_process(process
);
1323 return ERROR_SERVICE_REQUEST_TIMEOUT
;
1326 if (process_send_control(process
, shared_process
, service
->service_entry
->name
,
1327 dwControl
, NULL
, 0, &result
))
1328 result
= ERROR_SUCCESS
;
1330 if (lpServiceStatus
)
1332 service_lock(service
->service_entry
);
1333 *lpServiceStatus
= service
->service_entry
->status
;
1334 service_unlock(service
->service_entry
);
1337 ReleaseMutex(process
->control_mutex
);
1338 release_process(process
);
1342 DWORD __cdecl
svcctl_CloseServiceHandle(
1343 SC_RPC_HANDLE
*handle
)
1345 WINE_TRACE("(&%p)\n", *handle
);
1347 SC_RPC_HANDLE_destroy(*handle
);
1350 return ERROR_SUCCESS
;
1353 static void SC_RPC_LOCK_destroy(SC_RPC_LOCK hLock
)
1355 struct sc_lock
*lock
= hLock
;
1356 scmdatabase_unlock_startup(lock
->db
);
1357 HeapFree(GetProcessHeap(), 0, lock
);
1360 void __RPC_USER
SC_RPC_LOCK_rundown(SC_RPC_LOCK hLock
)
1362 SC_RPC_LOCK_destroy(hLock
);
1365 DWORD __cdecl
svcctl_LockServiceDatabase(
1366 SC_RPC_HANDLE hSCManager
,
1367 SC_RPC_LOCK
*phLock
)
1369 struct sc_manager_handle
*manager
;
1370 struct sc_lock
*lock
;
1373 WINE_TRACE("(%p, %p)\n", hSCManager
, phLock
);
1375 if ((err
= validate_scm_handle(hSCManager
, SC_MANAGER_LOCK
, &manager
)) != ERROR_SUCCESS
)
1378 if (!scmdatabase_lock_startup(manager
->db
))
1379 return ERROR_SERVICE_DATABASE_LOCKED
;
1381 lock
= HeapAlloc(GetProcessHeap(), 0, sizeof(struct sc_lock
));
1384 scmdatabase_unlock_startup(manager
->db
);
1385 return ERROR_NOT_ENOUGH_SERVER_MEMORY
;
1388 lock
->db
= manager
->db
;
1391 return ERROR_SUCCESS
;
1394 DWORD __cdecl
svcctl_UnlockServiceDatabase(
1395 SC_RPC_LOCK
*phLock
)
1397 WINE_TRACE("(&%p)\n", *phLock
);
1399 SC_RPC_LOCK_destroy(*phLock
);
1402 return ERROR_SUCCESS
;
1405 static BOOL
map_state(DWORD state
, DWORD mask
)
1409 case SERVICE_START_PENDING
:
1410 case SERVICE_STOP_PENDING
:
1411 case SERVICE_RUNNING
:
1412 case SERVICE_CONTINUE_PENDING
:
1413 case SERVICE_PAUSE_PENDING
:
1414 case SERVICE_PAUSED
:
1415 if (SERVICE_ACTIVE
& mask
) return TRUE
;
1417 case SERVICE_STOPPED
:
1418 if (SERVICE_INACTIVE
& mask
) return TRUE
;
1421 WINE_ERR("unknown state %u\n", state
);
1427 DWORD __cdecl
svcctl_EnumServicesStatusW(
1428 SC_RPC_HANDLE hmngr
,
1437 DWORD err
, sz
, total_size
, num_services
, offset
;
1438 struct sc_manager_handle
*manager
;
1439 struct service_entry
*service
;
1440 struct enum_service_status
*s
;
1442 WINE_TRACE("(%p, 0x%x, 0x%x, %p, %u, %p, %p, %p)\n", hmngr
, type
, state
, buffer
, size
, needed
, returned
, resume
);
1444 if (!type
|| !state
)
1445 return ERROR_INVALID_PARAMETER
;
1447 if ((err
= validate_scm_handle(hmngr
, SC_MANAGER_ENUMERATE_SERVICE
, &manager
)) != ERROR_SUCCESS
)
1451 WINE_FIXME("resume index not supported\n");
1453 scmdatabase_lock(manager
->db
);
1455 total_size
= num_services
= 0;
1456 LIST_FOR_EACH_ENTRY(service
, &manager
->db
->services
, struct service_entry
, entry
)
1458 if ((service
->status
.dwServiceType
& type
) && map_state(service
->status
.dwCurrentState
, state
))
1460 total_size
+= sizeof(*s
);
1461 total_size
+= (strlenW(service
->name
) + 1) * sizeof(WCHAR
);
1462 if (service
->config
.lpDisplayName
)
1464 total_size
+= (strlenW(service
->config
.lpDisplayName
) + 1) * sizeof(WCHAR
);
1470 *needed
= total_size
;
1471 if (total_size
> size
)
1473 scmdatabase_unlock(manager
->db
);
1474 return ERROR_MORE_DATA
;
1476 s
= (struct enum_service_status
*)buffer
;
1477 offset
= num_services
* sizeof(struct enum_service_status
);
1478 LIST_FOR_EACH_ENTRY(service
, &manager
->db
->services
, struct service_entry
, entry
)
1480 if ((service
->status
.dwServiceType
& type
) && map_state(service
->status
.dwCurrentState
, state
))
1482 sz
= (strlenW(service
->name
) + 1) * sizeof(WCHAR
);
1483 memcpy(buffer
+ offset
, service
->name
, sz
);
1484 s
->service_name
= offset
;
1487 if (!service
->config
.lpDisplayName
) s
->display_name
= 0;
1490 sz
= (strlenW(service
->config
.lpDisplayName
) + 1) * sizeof(WCHAR
);
1491 memcpy(buffer
+ offset
, service
->config
.lpDisplayName
, sz
);
1492 s
->display_name
= offset
;
1495 s
->service_status
= service
->status
;
1499 *returned
= num_services
;
1501 scmdatabase_unlock(manager
->db
);
1502 return ERROR_SUCCESS
;
1505 static struct service_entry
*find_service_by_group(struct scmdatabase
*db
, const WCHAR
*group
)
1507 struct service_entry
*service
;
1508 LIST_FOR_EACH_ENTRY(service
, &db
->services
, struct service_entry
, entry
)
1510 if (service
->config
.lpLoadOrderGroup
&& !strcmpiW(group
, service
->config
.lpLoadOrderGroup
))
1516 static BOOL
match_group(const WCHAR
*g1
, const WCHAR
*g2
)
1518 if (!g2
) return TRUE
;
1519 if (!g2
[0] && (!g1
|| !g1
[0])) return TRUE
;
1520 if (g1
&& !strcmpW(g1
, g2
)) return TRUE
;
1524 DWORD __cdecl
svcctl_EnumServicesStatusExA(
1525 SC_RPC_HANDLE scmanager
,
1526 SC_ENUM_TYPE info_level
,
1528 DWORD service_state
,
1532 DWORD
*services_count
,
1533 DWORD
*resume_index
,
1537 return ERROR_CALL_NOT_IMPLEMENTED
;
1540 DWORD __cdecl
svcctl_EnumServicesStatusExW(
1541 SC_RPC_HANDLE hmngr
,
1542 SC_ENUM_TYPE info_level
,
1549 DWORD
*resume_handle
,
1552 DWORD err
, sz
, total_size
, num_services
;
1554 struct sc_manager_handle
*manager
;
1555 struct service_entry
*service
;
1556 struct enum_service_status_process
*s
;
1558 WINE_TRACE("(%p, 0x%x, 0x%x, %p, %u, %p, %p, %s)\n", hmngr
, type
, state
, buffer
, size
,
1559 needed
, returned
, wine_dbgstr_w(group
));
1562 FIXME("resume handle not supported\n");
1564 if (!type
|| !state
)
1565 return ERROR_INVALID_PARAMETER
;
1567 if ((err
= validate_scm_handle(hmngr
, SC_MANAGER_ENUMERATE_SERVICE
, &manager
)) != ERROR_SUCCESS
)
1570 scmdatabase_lock(manager
->db
);
1572 if (group
&& !find_service_by_group(manager
->db
, group
))
1574 scmdatabase_unlock(manager
->db
);
1575 return ERROR_SERVICE_DOES_NOT_EXIST
;
1578 total_size
= num_services
= 0;
1579 LIST_FOR_EACH_ENTRY(service
, &manager
->db
->services
, struct service_entry
, entry
)
1581 if ((service
->status
.dwServiceType
& type
) && map_state(service
->status
.dwCurrentState
, state
)
1582 && match_group(service
->config
.lpLoadOrderGroup
, group
))
1584 total_size
+= sizeof(*s
);
1585 total_size
+= (strlenW(service
->name
) + 1) * sizeof(WCHAR
);
1586 if (service
->config
.lpDisplayName
)
1588 total_size
+= (strlenW(service
->config
.lpDisplayName
) + 1) * sizeof(WCHAR
);
1594 *needed
= total_size
;
1595 if (total_size
> size
)
1597 scmdatabase_unlock(manager
->db
);
1598 return ERROR_MORE_DATA
;
1600 s
= (struct enum_service_status_process
*)buffer
;
1601 offset
= num_services
* sizeof(*s
);
1602 LIST_FOR_EACH_ENTRY(service
, &manager
->db
->services
, struct service_entry
, entry
)
1604 if ((service
->status
.dwServiceType
& type
) && map_state(service
->status
.dwCurrentState
, state
)
1605 && match_group(service
->config
.lpLoadOrderGroup
, group
))
1607 sz
= (strlenW(service
->name
) + 1) * sizeof(WCHAR
);
1608 memcpy(buffer
+ offset
, service
->name
, sz
);
1609 s
->service_name
= offset
;
1612 if (!service
->config
.lpDisplayName
) s
->display_name
= 0;
1615 sz
= (strlenW(service
->config
.lpDisplayName
) + 1) * sizeof(WCHAR
);
1616 memcpy(buffer
+ offset
, service
->config
.lpDisplayName
, sz
);
1617 s
->display_name
= offset
;
1620 fill_status_process(&s
->service_status_process
, service
);
1624 *returned
= num_services
;
1626 scmdatabase_unlock(manager
->db
);
1627 return ERROR_SUCCESS
;
1630 DWORD __cdecl
svcctl_unknown43(void)
1633 return ERROR_CALL_NOT_IMPLEMENTED
;
1636 DWORD __cdecl
svcctl_CreateServiceWOW64A(
1637 SC_RPC_HANDLE scmanager
,
1643 DWORD error_control
,
1645 LPCSTR loadordergroup
,
1647 const BYTE
*dependencies
,
1650 const BYTE
*password
,
1651 DWORD password_size
,
1652 SC_RPC_HANDLE
*service
)
1655 return ERROR_CALL_NOT_IMPLEMENTED
;
1658 DWORD __cdecl
svcctl_CreateServiceWOW64W(
1659 SC_RPC_HANDLE scmanager
,
1660 LPCWSTR servicename
,
1661 LPCWSTR displayname
,
1665 DWORD error_control
,
1667 LPCWSTR loadordergroup
,
1669 const BYTE
*dependencies
,
1672 const BYTE
*password
,
1673 DWORD password_size
,
1674 SC_RPC_HANDLE
*service
)
1676 WINE_TRACE("(%s, %s, 0x%x, %s)\n", wine_dbgstr_w(servicename
), wine_dbgstr_w(displayname
), accessmask
, wine_dbgstr_w(imagepath
));
1677 return create_serviceW(scmanager
, servicename
, displayname
, accessmask
, service_type
, start_type
, error_control
, imagepath
,
1678 loadordergroup
, tagid
, dependencies
, depend_size
, start_name
, password
, password_size
, service
, TRUE
);
1681 DWORD __cdecl
svcctl_unknown46(void)
1684 return ERROR_CALL_NOT_IMPLEMENTED
;
1687 DWORD __cdecl
svcctl_NotifyServiceStatusChange(
1688 SC_RPC_HANDLE handle
,
1689 SC_RPC_NOTIFY_PARAMS params
,
1690 GUID
*clientprocessguid
,
1691 GUID
*scmprocessguid
,
1692 BOOL
*createremotequeue
,
1693 SC_NOTIFY_RPC_HANDLE
*hNotify
)
1696 struct sc_manager_handle
*manager
= NULL
;
1697 struct sc_service_handle
*service
= NULL
;
1698 struct sc_notify_handle
*notify
;
1699 struct sc_handle
*hdr
= handle
;
1701 WINE_TRACE("(%p, NotifyMask: 0x%x, %p, %p, %p, %p)\n", handle
,
1702 params
.u
.params
->dwNotifyMask
, clientprocessguid
, scmprocessguid
,
1703 createremotequeue
, hNotify
);
1707 case SC_HTYPE_SERVICE
:
1708 err
= validate_service_handle(handle
, SERVICE_QUERY_STATUS
, &service
);
1710 case SC_HTYPE_MANAGER
:
1711 err
= validate_scm_handle(handle
, SC_MANAGER_ENUMERATE_SERVICE
, &manager
);
1714 err
= ERROR_INVALID_HANDLE
;
1718 if (err
!= ERROR_SUCCESS
)
1723 WARN("Need support for service creation/deletion notifications\n");
1724 return ERROR_CALL_NOT_IMPLEMENTED
;
1727 notify
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*notify
));
1729 return ERROR_NOT_ENOUGH_SERVER_MEMORY
;
1731 notify
->hdr
.type
= SC_HTYPE_NOTIFY
;
1732 notify
->hdr
.access
= 0;
1734 notify
->service
= service
;
1736 notify
->event
= CreateEventW(NULL
, TRUE
, FALSE
, NULL
);
1738 notify
->notify_mask
= params
.u
.params
->dwNotifyMask
;
1740 service_lock(service
->service_entry
);
1742 if (service
->service_entry
->notify
)
1744 service_unlock(service
->service_entry
);
1745 HeapFree(GetProcessHeap(), 0, notify
);
1746 return ERROR_ALREADY_REGISTERED
;
1749 mask
= 1 << (service
->service_entry
->status
.dwCurrentState
- SERVICE_STOPPED
);
1750 if (!service
->service_entry
->status_notified
&&
1751 (notify
->notify_mask
& mask
))
1753 fill_notify(notify
);
1754 service
->service_entry
->status_notified
= TRUE
;
1758 sc_notify_retain(notify
);
1759 service
->service_entry
->notify
= notify
;
1762 sc_notify_retain(notify
);
1763 *hNotify
= ¬ify
->hdr
;
1765 service_unlock(service
->service_entry
);
1767 return ERROR_SUCCESS
;
1770 DWORD __cdecl
svcctl_GetNotifyResults(
1771 SC_NOTIFY_RPC_HANDLE hNotify
,
1772 SC_RPC_NOTIFY_PARAMS_LIST
**pList
)
1775 struct sc_notify_handle
*notify
;
1777 WINE_TRACE("(%p, %p)\n", hNotify
, pList
);
1780 return ERROR_INVALID_PARAMETER
;
1784 if ((err
= validate_notify_handle(hNotify
, 0, ¬ify
)) != 0)
1787 sc_notify_retain(notify
);
1788 /* block until there is a result */
1789 err
= WaitForSingleObject(notify
->event
, INFINITE
);
1791 if (err
!= WAIT_OBJECT_0
)
1793 sc_notify_release(notify
);
1797 *pList
= InterlockedExchangePointer((void**)¬ify
->params_list
, NULL
);
1800 sc_notify_release(notify
);
1801 return ERROR_REQUEST_ABORTED
;
1804 sc_notify_release(notify
);
1806 return ERROR_SUCCESS
;
1809 DWORD __cdecl
svcctl_CloseNotifyHandle(
1810 SC_NOTIFY_RPC_HANDLE
*hNotify
,
1813 struct sc_notify_handle
*notify
;
1816 WINE_TRACE("(%p, %p)\n", hNotify
, apc_fired
);
1818 if ((err
= validate_notify_handle(*hNotify
, 0, ¬ify
)) != 0)
1821 sc_notify_release(notify
);
1823 return ERROR_SUCCESS
;
1826 DWORD __cdecl
svcctl_ControlServiceExA(
1827 SC_RPC_HANDLE service
,
1830 SC_RPC_SERVICE_CONTROL_IN_PARAMSA
*in_params
,
1831 SC_RPC_SERVICE_CONTROL_OUT_PARAMSA
*out_params
)
1834 return ERROR_CALL_NOT_IMPLEMENTED
;
1837 DWORD __cdecl
svcctl_ControlServiceExW(
1838 SC_RPC_HANDLE service
,
1841 SC_RPC_SERVICE_CONTROL_IN_PARAMSW
*in_params
,
1842 SC_RPC_SERVICE_CONTROL_OUT_PARAMSW
*out_params
)
1845 return ERROR_CALL_NOT_IMPLEMENTED
;
1848 DWORD __cdecl
svcctl_unknown52(void)
1851 return ERROR_CALL_NOT_IMPLEMENTED
;
1854 DWORD __cdecl
svcctl_unknown53(void)
1857 return ERROR_CALL_NOT_IMPLEMENTED
;
1860 DWORD __cdecl
svcctl_unknown54(void)
1863 return ERROR_CALL_NOT_IMPLEMENTED
;
1866 DWORD __cdecl
svcctl_unknown55(void)
1869 return ERROR_CALL_NOT_IMPLEMENTED
;
1872 DWORD __cdecl
svcctl_QueryServiceConfigEx(
1873 SC_RPC_HANDLE service
,
1875 SC_RPC_CONFIG_INFOW
*info
)
1878 return ERROR_CALL_NOT_IMPLEMENTED
;
1881 DWORD __cdecl
svcctl_QueryServiceObjectSecurity(
1882 SC_RPC_HANDLE service
,
1883 SECURITY_INFORMATION info
,
1889 return ERROR_CALL_NOT_IMPLEMENTED
;
1892 DWORD __cdecl
svcctl_SetServiceObjectSecurity(
1893 SC_RPC_HANDLE service
,
1894 SECURITY_INFORMATION info
,
1899 return ERROR_CALL_NOT_IMPLEMENTED
;
1902 DWORD __cdecl
svcctl_QueryServiceStatus(
1903 SC_RPC_HANDLE service
,
1904 SERVICE_STATUS
*status
)
1907 return ERROR_CALL_NOT_IMPLEMENTED
;
1910 DWORD __cdecl
svcctl_NotifyBootConfigStatus(
1911 SVCCTL_HANDLEW machinename
,
1912 DWORD boot_acceptable
)
1915 return ERROR_CALL_NOT_IMPLEMENTED
;
1918 DWORD __cdecl
svcctl_SCSetServiceBitsW(void)
1921 return ERROR_CALL_NOT_IMPLEMENTED
;
1924 DWORD __cdecl
svcctl_EnumDependentServicesW(
1925 SC_RPC_HANDLE service
,
1930 DWORD
*services_ret
)
1933 return ERROR_CALL_NOT_IMPLEMENTED
;
1936 DWORD __cdecl
svcctl_QueryServiceLockStatusW(
1937 SC_RPC_HANDLE scmanager
,
1938 QUERY_SERVICE_LOCK_STATUSW
*status
,
1943 return ERROR_CALL_NOT_IMPLEMENTED
;
1946 DWORD __cdecl
svcctl_SCSetServiceBitsA(void)
1949 return ERROR_CALL_NOT_IMPLEMENTED
;
1952 DWORD __cdecl
svcctl_ChangeServiceConfigA(
1953 SC_RPC_HANDLE service
,
1956 DWORD error_control
,
1958 LPSTR loadordergroup
,
1964 DWORD password_size
,
1968 return ERROR_CALL_NOT_IMPLEMENTED
;
1971 DWORD __cdecl
svcctl_CreateServiceA(
1972 SC_RPC_HANDLE scmanager
,
1975 DWORD desiredaccess
,
1978 DWORD error_control
,
1980 LPCSTR loadordergroup
,
1982 const BYTE
*dependencies
,
1985 const BYTE
*password
,
1986 DWORD password_size
,
1987 SC_RPC_HANDLE
*service
)
1990 return ERROR_CALL_NOT_IMPLEMENTED
;
1993 DWORD __cdecl
svcctl_EnumDependentServicesA(
1994 SC_RPC_HANDLE service
,
1999 DWORD
*services_ret
)
2002 return ERROR_CALL_NOT_IMPLEMENTED
;
2005 DWORD __cdecl
svcctl_EnumServicesStatusA(
2006 SC_RPC_HANDLE hmngr
,
2016 return ERROR_CALL_NOT_IMPLEMENTED
;
2019 DWORD __cdecl
svcctl_OpenSCManagerA(
2020 MACHINE_HANDLEA MachineName
,
2021 LPCSTR DatabaseName
,
2023 SC_RPC_HANDLE
*handle
)
2026 return ERROR_CALL_NOT_IMPLEMENTED
;
2029 DWORD __cdecl
svcctl_OpenServiceA(
2030 SC_RPC_HANDLE hSCManager
,
2031 LPCSTR lpServiceName
,
2032 DWORD dwDesiredAccess
,
2033 SC_RPC_HANDLE
*phService
)
2036 return ERROR_CALL_NOT_IMPLEMENTED
;
2039 DWORD __cdecl
svcctl_QueryServiceConfigA(
2040 SC_RPC_HANDLE hService
,
2041 QUERY_SERVICE_CONFIGA
*config
,
2046 return ERROR_CALL_NOT_IMPLEMENTED
;
2049 DWORD __cdecl
svcctl_QueryServiceLockStatusA(
2050 SC_RPC_HANDLE scmanager
,
2051 QUERY_SERVICE_LOCK_STATUSA
*status
,
2056 return ERROR_CALL_NOT_IMPLEMENTED
;
2059 DWORD __cdecl
svcctl_StartServiceA(
2060 SC_RPC_HANDLE service
,
2065 return ERROR_CALL_NOT_IMPLEMENTED
;
2068 DWORD __cdecl
svcctl_GetServiceDisplayNameA(
2069 SC_RPC_HANDLE hSCManager
,
2075 return ERROR_CALL_NOT_IMPLEMENTED
;
2078 DWORD __cdecl
svcctl_GetServiceKeyNameA(
2079 SC_RPC_HANDLE hSCManager
,
2085 return ERROR_CALL_NOT_IMPLEMENTED
;
2088 DWORD __cdecl
svcctl_GetCurrentGroupStateW(void)
2091 return ERROR_CALL_NOT_IMPLEMENTED
;
2094 DWORD __cdecl
svcctl_EnumServiceGroupW(
2095 SC_RPC_HANDLE scmanager
,
2097 DWORD service_state
,
2101 DWORD
*returned_size
,
2102 DWORD
*resume_index
,
2106 return ERROR_CALL_NOT_IMPLEMENTED
;
2109 DWORD __cdecl
svcctl_ChangeServiceConfig2A(
2110 SC_RPC_HANDLE service
,
2111 SC_RPC_CONFIG_INFOA info
)
2114 return ERROR_CALL_NOT_IMPLEMENTED
;
2117 DWORD __cdecl
svcctl_QueryServiceConfig2A(
2118 SC_RPC_HANDLE service
,
2125 return ERROR_CALL_NOT_IMPLEMENTED
;
2128 DWORD
RPC_Init(void)
2130 WCHAR transport
[] = SVCCTL_TRANSPORT
;
2131 WCHAR endpoint
[] = SVCCTL_ENDPOINT
;
2134 if (!(cleanup_group
= CreateThreadpoolCleanupGroup()))
2136 WINE_ERR("CreateThreadpoolCleanupGroup failed with error %u\n", GetLastError());
2137 return GetLastError();
2140 if ((err
= RpcServerUseProtseqEpW(transport
, 0, endpoint
, NULL
)) != ERROR_SUCCESS
)
2142 WINE_ERR("RpcServerUseProtseq failed with error %u\n", err
);
2146 if ((err
= RpcServerRegisterIf(svcctl_v2_0_s_ifspec
, 0, 0)) != ERROR_SUCCESS
)
2148 WINE_ERR("RpcServerRegisterIf failed with error %u\n", err
);
2152 if ((err
= RpcServerListen(1, RPC_C_LISTEN_MAX_CALLS_DEFAULT
, TRUE
)) != ERROR_SUCCESS
)
2154 WINE_ERR("RpcServerListen failed with error %u\n", err
);
2158 exit_event
= __wine_make_process_system();
2159 return ERROR_SUCCESS
;
2164 RpcMgmtStopServerListening(NULL
);
2165 RpcServerUnregisterIf(svcctl_v2_0_s_ifspec
, NULL
, TRUE
);
2166 RpcMgmtWaitServerListen();
2168 CloseThreadpoolCleanupGroupMembers(cleanup_group
, TRUE
, NULL
);
2169 CloseThreadpoolCleanupGroup(cleanup_group
);
2170 CloseHandle(exit_event
);
2173 void __RPC_USER
SC_RPC_HANDLE_rundown(SC_RPC_HANDLE handle
)
2175 SC_RPC_HANDLE_destroy(handle
);
2178 void __RPC_USER
SC_NOTIFY_RPC_HANDLE_rundown(SC_NOTIFY_RPC_HANDLE handle
)
2182 void __RPC_FAR
* __RPC_USER
MIDL_user_allocate(SIZE_T len
)
2184 return HeapAlloc(GetProcessHeap(), 0, len
);
2187 void __RPC_USER
MIDL_user_free(void __RPC_FAR
* ptr
)
2189 HeapFree(GetProcessHeap(), 0, ptr
);