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_status_process(SERVICE_STATUS_PROCESS
*status
, struct service_entry
*service
)
826 struct process_entry
*process
= service
->process
;
827 memcpy(status
, &service
->status
, sizeof(service
->status
));
828 status
->dwProcessId
= process
? process
->process_id
: 0;
829 status
->dwServiceFlags
= 0;
832 static void fill_notify(struct sc_notify_handle
*notify
)
834 SC_RPC_NOTIFY_PARAMS_LIST
*list
;
835 SERVICE_NOTIFY_STATUS_CHANGE_PARAMS_2
*cparams
;
837 list
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
,
838 sizeof(SC_RPC_NOTIFY_PARAMS_LIST
) + sizeof(SERVICE_NOTIFY_STATUS_CHANGE_PARAMS_2
));
842 cparams
= (SERVICE_NOTIFY_STATUS_CHANGE_PARAMS_2
*)(list
+ 1);
844 cparams
->dwNotifyMask
= notify
->notify_mask
;
845 fill_status_process(&cparams
->ServiceStatus
, notify
->service
->service_entry
);
846 cparams
->dwNotificationStatus
= ERROR_SUCCESS
;
847 cparams
->dwNotificationTriggered
= 1 << (cparams
->ServiceStatus
.dwCurrentState
- SERVICE_STOPPED
);
848 cparams
->pszServiceNames
= NULL
;
852 list
->NotifyParamsArray
[0].dwInfoLevel
= 2;
853 list
->NotifyParamsArray
[0].u
.params
= cparams
;
855 InterlockedExchangePointer((void**)¬ify
->params_list
, list
);
857 SetEvent(notify
->event
);
860 DWORD __cdecl
svcctl_SetServiceStatus(
861 SC_RPC_HANDLE hServiceStatus
,
862 LPSERVICE_STATUS lpServiceStatus
)
864 struct sc_service_handle
*service
;
865 struct process_entry
*process
;
868 WINE_TRACE("(%p, %p)\n", hServiceStatus
, lpServiceStatus
);
870 if ((err
= validate_service_handle(hServiceStatus
, SERVICE_SET_STATUS
, &service
)) != 0)
873 service_lock(service
->service_entry
);
875 /* FIXME: be a bit more discriminant about what parts of the status we set
876 * and check that fields are valid */
877 service
->service_entry
->status
= *lpServiceStatus
;
878 SetEvent(service
->service_entry
->status_changed_event
);
880 if ((process
= service
->service_entry
->process
) &&
881 lpServiceStatus
->dwCurrentState
== SERVICE_STOPPED
)
883 service
->service_entry
->process
= NULL
;
884 if (!--process
->use_count
)
885 terminate_after_timeout(process
, service_kill_timeout
);
886 if (service
->service_entry
->shared_process
&& process
->use_count
<= 1)
887 shutdown_shared_process(process
);
888 release_process(process
);
891 mask
= 1 << (service
->service_entry
->status
.dwCurrentState
- SERVICE_STOPPED
);
892 if (service
->service_entry
->notify
&&
893 (service
->service_entry
->notify
->notify_mask
& mask
))
895 struct sc_notify_handle
*notify
= service
->service_entry
->notify
;
897 service
->service_entry
->notify
= NULL
;
898 sc_notify_release(notify
);
899 service
->service_entry
->status_notified
= TRUE
;
902 service
->service_entry
->status_notified
= FALSE
;
904 service_unlock(service
->service_entry
);
906 return ERROR_SUCCESS
;
909 DWORD __cdecl
svcctl_ChangeServiceConfig2W( SC_RPC_HANDLE hService
, SC_RPC_CONFIG_INFOW config
)
911 struct sc_service_handle
*service
;
914 if ((err
= validate_service_handle(hService
, SERVICE_CHANGE_CONFIG
, &service
)) != 0)
917 switch (config
.dwInfoLevel
)
919 case SERVICE_CONFIG_DESCRIPTION
:
923 if (!config
.u
.descr
->lpDescription
)
926 if (config
.u
.descr
->lpDescription
[0])
928 if (!(descr
= strdupW( config
.u
.descr
->lpDescription
)))
929 return ERROR_NOT_ENOUGH_MEMORY
;
932 WINE_TRACE( "changing service %p descr to %s\n", service
, wine_dbgstr_w(descr
) );
933 service_lock( service
->service_entry
);
934 HeapFree( GetProcessHeap(), 0, service
->service_entry
->description
);
935 service
->service_entry
->description
= descr
;
936 save_service_config( service
->service_entry
);
937 service_unlock( service
->service_entry
);
940 case SERVICE_CONFIG_FAILURE_ACTIONS
:
941 WINE_FIXME( "SERVICE_CONFIG_FAILURE_ACTIONS not implemented: period %u msg %s cmd %s\n",
942 config
.u
.actions
->dwResetPeriod
,
943 wine_dbgstr_w(config
.u
.actions
->lpRebootMsg
),
944 wine_dbgstr_w(config
.u
.actions
->lpCommand
) );
946 case SERVICE_CONFIG_PRESHUTDOWN_INFO
:
947 WINE_TRACE( "changing service %p preshutdown timeout to %d\n",
948 service
, config
.u
.preshutdown
->dwPreshutdownTimeout
);
949 service_lock( service
->service_entry
);
950 service
->service_entry
->preshutdown_timeout
= config
.u
.preshutdown
->dwPreshutdownTimeout
;
951 save_service_config( service
->service_entry
);
952 service_unlock( service
->service_entry
);
955 WINE_FIXME("level %u not implemented\n", config
.dwInfoLevel
);
956 err
= ERROR_INVALID_LEVEL
;
962 DWORD __cdecl
svcctl_QueryServiceConfig2W( SC_RPC_HANDLE hService
, DWORD level
,
963 BYTE
*buffer
, DWORD size
, LPDWORD needed
)
965 struct sc_service_handle
*service
;
968 memset(buffer
, 0, size
);
970 if ((err
= validate_service_handle(hService
, SERVICE_QUERY_STATUS
, &service
)) != 0)
975 case SERVICE_CONFIG_DESCRIPTION
:
977 struct service_description
*desc
= (struct service_description
*)buffer
;
978 DWORD total_size
= sizeof(*desc
);
980 service_lock(service
->service_entry
);
981 if (service
->service_entry
->description
)
982 total_size
+= strlenW(service
->service_entry
->description
) * sizeof(WCHAR
);
984 *needed
= total_size
;
985 if (size
>= total_size
)
987 if (service
->service_entry
->description
)
989 strcpyW( desc
->description
, service
->service_entry
->description
);
990 desc
->size
= total_size
- FIELD_OFFSET(struct service_description
, description
);
994 desc
->description
[0] = 0;
998 else err
= ERROR_INSUFFICIENT_BUFFER
;
999 service_unlock(service
->service_entry
);
1003 case SERVICE_CONFIG_PRESHUTDOWN_INFO
:
1004 service_lock(service
->service_entry
);
1006 *needed
= sizeof(SERVICE_PRESHUTDOWN_INFO
);
1007 if (size
>= *needed
)
1008 ((LPSERVICE_PRESHUTDOWN_INFO
)buffer
)->dwPreshutdownTimeout
=
1009 service
->service_entry
->preshutdown_timeout
;
1010 else err
= ERROR_INSUFFICIENT_BUFFER
;
1012 service_unlock(service
->service_entry
);
1016 WINE_FIXME("level %u not implemented\n", level
);
1017 err
= ERROR_INVALID_LEVEL
;
1023 DWORD __cdecl
svcctl_QueryServiceStatusEx(
1024 SC_RPC_HANDLE hService
,
1025 SC_STATUS_TYPE InfoLevel
,
1028 LPDWORD pcbBytesNeeded
)
1030 struct sc_service_handle
*service
;
1032 LPSERVICE_STATUS_PROCESS pSvcStatusData
;
1034 memset(lpBuffer
, 0, cbBufSize
);
1036 if ((err
= validate_service_handle(hService
, SERVICE_QUERY_STATUS
, &service
)) != 0)
1039 if (InfoLevel
!= SC_STATUS_PROCESS_INFO
)
1040 return ERROR_INVALID_LEVEL
;
1042 pSvcStatusData
= (LPSERVICE_STATUS_PROCESS
) lpBuffer
;
1043 if (pSvcStatusData
== NULL
)
1044 return ERROR_INVALID_PARAMETER
;
1046 if (cbBufSize
< sizeof(SERVICE_STATUS_PROCESS
))
1048 if( pcbBytesNeeded
!= NULL
)
1049 *pcbBytesNeeded
= sizeof(SERVICE_STATUS_PROCESS
);
1051 return ERROR_INSUFFICIENT_BUFFER
;
1054 service_lock(service
->service_entry
);
1055 fill_status_process(pSvcStatusData
, service
->service_entry
);
1056 service_unlock(service
->service_entry
);
1058 return ERROR_SUCCESS
;
1061 /******************************************************************************
1062 * service_accepts_control
1064 static BOOL
service_accepts_control(const struct service_entry
*service
, DWORD dwControl
)
1066 DWORD a
= service
->status
.dwControlsAccepted
;
1068 if (dwControl
>= 128 && dwControl
<= 255)
1073 case SERVICE_CONTROL_INTERROGATE
:
1075 case SERVICE_CONTROL_STOP
:
1076 if (a
&SERVICE_ACCEPT_STOP
)
1079 case SERVICE_CONTROL_SHUTDOWN
:
1080 if (a
&SERVICE_ACCEPT_SHUTDOWN
)
1083 case SERVICE_CONTROL_PAUSE
:
1084 case SERVICE_CONTROL_CONTINUE
:
1085 if (a
&SERVICE_ACCEPT_PAUSE_CONTINUE
)
1088 case SERVICE_CONTROL_PARAMCHANGE
:
1089 if (a
&SERVICE_ACCEPT_PARAMCHANGE
)
1092 case SERVICE_CONTROL_NETBINDADD
:
1093 case SERVICE_CONTROL_NETBINDREMOVE
:
1094 case SERVICE_CONTROL_NETBINDENABLE
:
1095 case SERVICE_CONTROL_NETBINDDISABLE
:
1096 if (a
&SERVICE_ACCEPT_NETBINDCHANGE
)
1098 case SERVICE_CONTROL_HARDWAREPROFILECHANGE
:
1099 if (a
&SERVICE_ACCEPT_HARDWAREPROFILECHANGE
)
1102 case SERVICE_CONTROL_POWEREVENT
:
1103 if (a
&SERVICE_ACCEPT_POWEREVENT
)
1106 case SERVICE_CONTROL_SESSIONCHANGE
:
1107 if (a
&SERVICE_ACCEPT_SESSIONCHANGE
)
1114 /******************************************************************************
1115 * process_send_command
1117 static BOOL
process_send_command(struct process_entry
*process
, const void *data
, DWORD size
, DWORD
*result
)
1119 OVERLAPPED overlapped
;
1123 overlapped
.hEvent
= process
->overlapped_event
;
1124 r
= WriteFile(process
->control_pipe
, data
, size
, &count
, &overlapped
);
1125 if (!r
&& GetLastError() == ERROR_IO_PENDING
)
1127 ret
= WaitForSingleObject(process
->overlapped_event
, service_pipe_timeout
);
1128 if (ret
== WAIT_TIMEOUT
)
1130 WINE_ERR("sending command timed out\n");
1131 *result
= ERROR_SERVICE_REQUEST_TIMEOUT
;
1134 r
= GetOverlappedResult(process
->control_pipe
, &overlapped
, &count
, FALSE
);
1136 if (!r
|| count
!= size
)
1138 WINE_ERR("service protocol error - failed to write pipe!\n");
1139 *result
= (!r
? GetLastError() : ERROR_WRITE_FAULT
);
1142 r
= ReadFile(process
->control_pipe
, result
, sizeof *result
, &count
, &overlapped
);
1143 if (!r
&& GetLastError() == ERROR_IO_PENDING
)
1145 ret
= WaitForSingleObject(process
->overlapped_event
, service_pipe_timeout
);
1146 if (ret
== WAIT_TIMEOUT
)
1148 WINE_ERR("receiving command result timed out\n");
1149 *result
= ERROR_SERVICE_REQUEST_TIMEOUT
;
1152 r
= GetOverlappedResult(process
->control_pipe
, &overlapped
, &count
, FALSE
);
1154 if (!r
|| count
!= sizeof *result
)
1156 WINE_ERR("service protocol error - failed to read pipe "
1157 "r = %d count = %d!\n", r
, count
);
1158 *result
= (!r
? GetLastError() : ERROR_READ_FAULT
);
1165 /******************************************************************************
1166 * process_send_control
1168 BOOL
process_send_control(struct process_entry
*process
, BOOL shared_process
, const WCHAR
*name
,
1169 DWORD control
, const BYTE
*data
, DWORD data_size
, DWORD
*result
)
1171 service_start_info
*ssi
;
1177 control
|= SERVICE_CONTROL_FORWARD_FLAG
;
1178 data
= (BYTE
*)name
;
1179 data_size
= (strlenW(name
) + 1) * sizeof(WCHAR
);
1183 /* calculate how much space we need to send the startup info */
1184 len
= (strlenW(name
) + 1) * sizeof(WCHAR
) + data_size
;
1186 ssi
= HeapAlloc(GetProcessHeap(),0,FIELD_OFFSET(service_start_info
, data
[len
]));
1187 ssi
->magic
= SERVICE_PROTOCOL_MAGIC
;
1188 ssi
->control
= control
;
1189 ssi
->total_size
= FIELD_OFFSET(service_start_info
, data
[len
]);
1190 ssi
->name_size
= strlenW(name
) + 1;
1191 strcpyW((WCHAR
*)ssi
->data
, name
);
1192 if (data_size
) memcpy(&ssi
->data
[ssi
->name_size
* sizeof(WCHAR
)], data
, data_size
);
1194 r
= process_send_command(process
, ssi
, ssi
->total_size
, result
);
1195 HeapFree( GetProcessHeap(), 0, ssi
);
1199 DWORD __cdecl
svcctl_StartServiceW(
1200 SC_RPC_HANDLE hService
,
1201 DWORD dwNumServiceArgs
,
1202 LPCWSTR
*lpServiceArgVectors
)
1204 struct sc_service_handle
*service
;
1207 WINE_TRACE("(%p, %d, %p)\n", hService
, dwNumServiceArgs
, lpServiceArgVectors
);
1209 if ((err
= validate_service_handle(hService
, SERVICE_START
, &service
)) != 0)
1212 if (service
->service_entry
->config
.dwStartType
== SERVICE_DISABLED
)
1213 return ERROR_SERVICE_DISABLED
;
1215 if (!scmdatabase_lock_startup(service
->service_entry
->db
, 3000))
1216 return ERROR_SERVICE_DATABASE_LOCKED
;
1218 err
= service_start(service
->service_entry
, dwNumServiceArgs
, lpServiceArgVectors
);
1220 scmdatabase_unlock_startup(service
->service_entry
->db
);
1224 DWORD __cdecl
svcctl_ControlService(
1225 SC_RPC_HANDLE hService
,
1227 SERVICE_STATUS
*lpServiceStatus
)
1229 DWORD access_required
;
1230 struct sc_service_handle
*service
;
1231 struct process_entry
*process
;
1232 BOOL shared_process
;
1235 WINE_TRACE("(%p, %d, %p)\n", hService
, dwControl
, lpServiceStatus
);
1239 case SERVICE_CONTROL_CONTINUE
:
1240 case SERVICE_CONTROL_NETBINDADD
:
1241 case SERVICE_CONTROL_NETBINDDISABLE
:
1242 case SERVICE_CONTROL_NETBINDENABLE
:
1243 case SERVICE_CONTROL_NETBINDREMOVE
:
1244 case SERVICE_CONTROL_PARAMCHANGE
:
1245 case SERVICE_CONTROL_PAUSE
:
1246 access_required
= SERVICE_PAUSE_CONTINUE
;
1248 case SERVICE_CONTROL_INTERROGATE
:
1249 access_required
= SERVICE_INTERROGATE
;
1251 case SERVICE_CONTROL_STOP
:
1252 access_required
= SERVICE_STOP
;
1255 if (dwControl
>= 128 && dwControl
<= 255)
1256 access_required
= SERVICE_USER_DEFINED_CONTROL
;
1258 return ERROR_INVALID_PARAMETER
;
1261 if ((result
= validate_service_handle(hService
, access_required
, &service
)) != 0)
1264 service_lock(service
->service_entry
);
1266 result
= ERROR_SUCCESS
;
1267 switch (service
->service_entry
->status
.dwCurrentState
)
1269 case SERVICE_STOPPED
:
1270 result
= ERROR_SERVICE_NOT_ACTIVE
;
1272 case SERVICE_START_PENDING
:
1273 if (dwControl
==SERVICE_CONTROL_STOP
)
1276 case SERVICE_STOP_PENDING
:
1277 result
= ERROR_SERVICE_CANNOT_ACCEPT_CTRL
;
1281 if (result
== ERROR_SUCCESS
&& service
->service_entry
->force_shutdown
)
1283 result
= ERROR_SERVICE_CANNOT_ACCEPT_CTRL
;
1284 if ((process
= service
->service_entry
->process
))
1286 service
->service_entry
->process
= NULL
;
1287 if (!--process
->use_count
) process_terminate(process
);
1288 release_process(process
);
1292 if (result
!= ERROR_SUCCESS
)
1294 if (lpServiceStatus
) *lpServiceStatus
= service
->service_entry
->status
;
1295 service_unlock(service
->service_entry
);
1299 if (!service_accepts_control(service
->service_entry
, dwControl
))
1301 service_unlock(service
->service_entry
);
1302 return ERROR_INVALID_SERVICE_CONTROL
;
1305 /* Remember that we tried to shutdown this service. When the service is
1306 * still running on the second invocation, it will be forcefully killed. */
1307 if (dwControl
== SERVICE_CONTROL_STOP
)
1308 service
->service_entry
->force_shutdown
= TRUE
;
1310 /* Hold a reference to the process while sending the command. */
1311 process
= grab_process(service
->service_entry
->process
);
1312 shared_process
= service
->service_entry
->shared_process
;
1313 service_unlock(service
->service_entry
);
1316 return ERROR_SERVICE_CANNOT_ACCEPT_CTRL
;
1318 result
= WaitForSingleObject(process
->control_mutex
, 30000);
1319 if (result
!= WAIT_OBJECT_0
)
1321 release_process(process
);
1322 return ERROR_SERVICE_REQUEST_TIMEOUT
;
1325 if (process_send_control(process
, shared_process
, service
->service_entry
->name
,
1326 dwControl
, NULL
, 0, &result
))
1327 result
= ERROR_SUCCESS
;
1329 if (lpServiceStatus
)
1331 service_lock(service
->service_entry
);
1332 *lpServiceStatus
= service
->service_entry
->status
;
1333 service_unlock(service
->service_entry
);
1336 ReleaseMutex(process
->control_mutex
);
1337 release_process(process
);
1341 DWORD __cdecl
svcctl_CloseServiceHandle(
1342 SC_RPC_HANDLE
*handle
)
1344 WINE_TRACE("(&%p)\n", *handle
);
1346 SC_RPC_HANDLE_destroy(*handle
);
1349 return ERROR_SUCCESS
;
1352 static void SC_RPC_LOCK_destroy(SC_RPC_LOCK hLock
)
1354 struct sc_lock
*lock
= hLock
;
1355 scmdatabase_unlock_startup(lock
->db
);
1356 HeapFree(GetProcessHeap(), 0, lock
);
1359 void __RPC_USER
SC_RPC_LOCK_rundown(SC_RPC_LOCK hLock
)
1361 SC_RPC_LOCK_destroy(hLock
);
1364 DWORD __cdecl
svcctl_LockServiceDatabase(
1365 SC_RPC_HANDLE hSCManager
,
1366 SC_RPC_LOCK
*phLock
)
1368 struct sc_manager_handle
*manager
;
1369 struct sc_lock
*lock
;
1372 WINE_TRACE("(%p, %p)\n", hSCManager
, phLock
);
1374 if ((err
= validate_scm_handle(hSCManager
, SC_MANAGER_LOCK
, &manager
)) != ERROR_SUCCESS
)
1377 if (!scmdatabase_lock_startup(manager
->db
, 0))
1378 return ERROR_SERVICE_DATABASE_LOCKED
;
1380 lock
= HeapAlloc(GetProcessHeap(), 0, sizeof(struct sc_lock
));
1383 scmdatabase_unlock_startup(manager
->db
);
1384 return ERROR_NOT_ENOUGH_SERVER_MEMORY
;
1387 lock
->db
= manager
->db
;
1390 return ERROR_SUCCESS
;
1393 DWORD __cdecl
svcctl_UnlockServiceDatabase(
1394 SC_RPC_LOCK
*phLock
)
1396 WINE_TRACE("(&%p)\n", *phLock
);
1398 SC_RPC_LOCK_destroy(*phLock
);
1401 return ERROR_SUCCESS
;
1404 static BOOL
map_state(DWORD state
, DWORD mask
)
1408 case SERVICE_START_PENDING
:
1409 case SERVICE_STOP_PENDING
:
1410 case SERVICE_RUNNING
:
1411 case SERVICE_CONTINUE_PENDING
:
1412 case SERVICE_PAUSE_PENDING
:
1413 case SERVICE_PAUSED
:
1414 if (SERVICE_ACTIVE
& mask
) return TRUE
;
1416 case SERVICE_STOPPED
:
1417 if (SERVICE_INACTIVE
& mask
) return TRUE
;
1420 WINE_ERR("unknown state %u\n", state
);
1426 DWORD __cdecl
svcctl_EnumServicesStatusW(
1427 SC_RPC_HANDLE hmngr
,
1436 DWORD err
, sz
, total_size
, num_services
, offset
;
1437 struct sc_manager_handle
*manager
;
1438 struct service_entry
*service
;
1439 struct enum_service_status
*s
;
1441 WINE_TRACE("(%p, 0x%x, 0x%x, %p, %u, %p, %p, %p)\n", hmngr
, type
, state
, buffer
, size
, needed
, returned
, resume
);
1443 if (!type
|| !state
)
1444 return ERROR_INVALID_PARAMETER
;
1446 if ((err
= validate_scm_handle(hmngr
, SC_MANAGER_ENUMERATE_SERVICE
, &manager
)) != ERROR_SUCCESS
)
1450 WINE_FIXME("resume index not supported\n");
1452 scmdatabase_lock(manager
->db
);
1454 total_size
= num_services
= 0;
1455 LIST_FOR_EACH_ENTRY(service
, &manager
->db
->services
, struct service_entry
, entry
)
1457 if ((service
->status
.dwServiceType
& type
) && map_state(service
->status
.dwCurrentState
, state
))
1459 total_size
+= sizeof(*s
);
1460 total_size
+= (strlenW(service
->name
) + 1) * sizeof(WCHAR
);
1461 if (service
->config
.lpDisplayName
)
1463 total_size
+= (strlenW(service
->config
.lpDisplayName
) + 1) * sizeof(WCHAR
);
1469 *needed
= total_size
;
1470 if (total_size
> size
)
1472 scmdatabase_unlock(manager
->db
);
1473 return ERROR_MORE_DATA
;
1475 s
= (struct enum_service_status
*)buffer
;
1476 offset
= num_services
* sizeof(struct enum_service_status
);
1477 LIST_FOR_EACH_ENTRY(service
, &manager
->db
->services
, struct service_entry
, entry
)
1479 if ((service
->status
.dwServiceType
& type
) && map_state(service
->status
.dwCurrentState
, state
))
1481 sz
= (strlenW(service
->name
) + 1) * sizeof(WCHAR
);
1482 memcpy(buffer
+ offset
, service
->name
, sz
);
1483 s
->service_name
= offset
;
1486 if (!service
->config
.lpDisplayName
) s
->display_name
= 0;
1489 sz
= (strlenW(service
->config
.lpDisplayName
) + 1) * sizeof(WCHAR
);
1490 memcpy(buffer
+ offset
, service
->config
.lpDisplayName
, sz
);
1491 s
->display_name
= offset
;
1494 s
->service_status
= service
->status
;
1498 *returned
= num_services
;
1500 scmdatabase_unlock(manager
->db
);
1501 return ERROR_SUCCESS
;
1504 static struct service_entry
*find_service_by_group(struct scmdatabase
*db
, const WCHAR
*group
)
1506 struct service_entry
*service
;
1507 LIST_FOR_EACH_ENTRY(service
, &db
->services
, struct service_entry
, entry
)
1509 if (service
->config
.lpLoadOrderGroup
&& !strcmpiW(group
, service
->config
.lpLoadOrderGroup
))
1515 static BOOL
match_group(const WCHAR
*g1
, const WCHAR
*g2
)
1517 if (!g2
) return TRUE
;
1518 if (!g2
[0] && (!g1
|| !g1
[0])) return TRUE
;
1519 if (g1
&& !strcmpW(g1
, g2
)) return TRUE
;
1523 DWORD __cdecl
svcctl_EnumServicesStatusExA(
1524 SC_RPC_HANDLE scmanager
,
1525 SC_ENUM_TYPE info_level
,
1527 DWORD service_state
,
1531 DWORD
*services_count
,
1532 DWORD
*resume_index
,
1536 return ERROR_CALL_NOT_IMPLEMENTED
;
1539 DWORD __cdecl
svcctl_EnumServicesStatusExW(
1540 SC_RPC_HANDLE hmngr
,
1541 SC_ENUM_TYPE info_level
,
1548 DWORD
*resume_handle
,
1551 DWORD err
, sz
, total_size
, num_services
;
1553 struct sc_manager_handle
*manager
;
1554 struct service_entry
*service
;
1555 struct enum_service_status_process
*s
;
1557 WINE_TRACE("(%p, 0x%x, 0x%x, %p, %u, %p, %p, %s)\n", hmngr
, type
, state
, buffer
, size
,
1558 needed
, returned
, wine_dbgstr_w(group
));
1561 FIXME("resume handle not supported\n");
1563 if (!type
|| !state
)
1564 return ERROR_INVALID_PARAMETER
;
1566 if ((err
= validate_scm_handle(hmngr
, SC_MANAGER_ENUMERATE_SERVICE
, &manager
)) != ERROR_SUCCESS
)
1569 scmdatabase_lock(manager
->db
);
1571 if (group
&& !find_service_by_group(manager
->db
, group
))
1573 scmdatabase_unlock(manager
->db
);
1574 return ERROR_SERVICE_DOES_NOT_EXIST
;
1577 total_size
= num_services
= 0;
1578 LIST_FOR_EACH_ENTRY(service
, &manager
->db
->services
, struct service_entry
, entry
)
1580 if ((service
->status
.dwServiceType
& type
) && map_state(service
->status
.dwCurrentState
, state
)
1581 && match_group(service
->config
.lpLoadOrderGroup
, group
))
1583 total_size
+= sizeof(*s
);
1584 total_size
+= (strlenW(service
->name
) + 1) * sizeof(WCHAR
);
1585 if (service
->config
.lpDisplayName
)
1587 total_size
+= (strlenW(service
->config
.lpDisplayName
) + 1) * sizeof(WCHAR
);
1593 *needed
= total_size
;
1594 if (total_size
> size
)
1596 scmdatabase_unlock(manager
->db
);
1597 return ERROR_MORE_DATA
;
1599 s
= (struct enum_service_status_process
*)buffer
;
1600 offset
= num_services
* sizeof(*s
);
1601 LIST_FOR_EACH_ENTRY(service
, &manager
->db
->services
, struct service_entry
, entry
)
1603 if ((service
->status
.dwServiceType
& type
) && map_state(service
->status
.dwCurrentState
, state
)
1604 && match_group(service
->config
.lpLoadOrderGroup
, group
))
1606 sz
= (strlenW(service
->name
) + 1) * sizeof(WCHAR
);
1607 memcpy(buffer
+ offset
, service
->name
, sz
);
1608 s
->service_name
= offset
;
1611 if (!service
->config
.lpDisplayName
) s
->display_name
= 0;
1614 sz
= (strlenW(service
->config
.lpDisplayName
) + 1) * sizeof(WCHAR
);
1615 memcpy(buffer
+ offset
, service
->config
.lpDisplayName
, sz
);
1616 s
->display_name
= offset
;
1619 fill_status_process(&s
->service_status_process
, service
);
1623 *returned
= num_services
;
1625 scmdatabase_unlock(manager
->db
);
1626 return ERROR_SUCCESS
;
1629 DWORD __cdecl
svcctl_unknown43(void)
1632 return ERROR_CALL_NOT_IMPLEMENTED
;
1635 DWORD __cdecl
svcctl_CreateServiceWOW64A(
1636 SC_RPC_HANDLE scmanager
,
1642 DWORD error_control
,
1644 LPCSTR loadordergroup
,
1646 const BYTE
*dependencies
,
1649 const BYTE
*password
,
1650 DWORD password_size
,
1651 SC_RPC_HANDLE
*service
)
1654 return ERROR_CALL_NOT_IMPLEMENTED
;
1657 DWORD __cdecl
svcctl_CreateServiceWOW64W(
1658 SC_RPC_HANDLE scmanager
,
1659 LPCWSTR servicename
,
1660 LPCWSTR displayname
,
1664 DWORD error_control
,
1666 LPCWSTR loadordergroup
,
1668 const BYTE
*dependencies
,
1671 const BYTE
*password
,
1672 DWORD password_size
,
1673 SC_RPC_HANDLE
*service
)
1675 WINE_TRACE("(%s, %s, 0x%x, %s)\n", wine_dbgstr_w(servicename
), wine_dbgstr_w(displayname
), accessmask
, wine_dbgstr_w(imagepath
));
1676 return create_serviceW(scmanager
, servicename
, displayname
, accessmask
, service_type
, start_type
, error_control
, imagepath
,
1677 loadordergroup
, tagid
, dependencies
, depend_size
, start_name
, password
, password_size
, service
, TRUE
);
1680 DWORD __cdecl
svcctl_unknown46(void)
1683 return ERROR_CALL_NOT_IMPLEMENTED
;
1686 DWORD __cdecl
svcctl_NotifyServiceStatusChange(
1687 SC_RPC_HANDLE handle
,
1688 SC_RPC_NOTIFY_PARAMS params
,
1689 GUID
*clientprocessguid
,
1690 GUID
*scmprocessguid
,
1691 BOOL
*createremotequeue
,
1692 SC_NOTIFY_RPC_HANDLE
*hNotify
)
1695 struct sc_manager_handle
*manager
= NULL
;
1696 struct sc_service_handle
*service
= NULL
;
1697 struct sc_notify_handle
*notify
;
1698 struct sc_handle
*hdr
= handle
;
1700 WINE_TRACE("(%p, NotifyMask: 0x%x, %p, %p, %p, %p)\n", handle
,
1701 params
.u
.params
->dwNotifyMask
, clientprocessguid
, scmprocessguid
,
1702 createremotequeue
, hNotify
);
1706 case SC_HTYPE_SERVICE
:
1707 err
= validate_service_handle(handle
, SERVICE_QUERY_STATUS
, &service
);
1709 case SC_HTYPE_MANAGER
:
1710 err
= validate_scm_handle(handle
, SC_MANAGER_ENUMERATE_SERVICE
, &manager
);
1713 err
= ERROR_INVALID_HANDLE
;
1717 if (err
!= ERROR_SUCCESS
)
1722 WARN("Need support for service creation/deletion notifications\n");
1723 return ERROR_CALL_NOT_IMPLEMENTED
;
1726 notify
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*notify
));
1728 return ERROR_NOT_ENOUGH_SERVER_MEMORY
;
1730 notify
->hdr
.type
= SC_HTYPE_NOTIFY
;
1731 notify
->hdr
.access
= 0;
1733 notify
->service
= service
;
1735 notify
->event
= CreateEventW(NULL
, TRUE
, FALSE
, NULL
);
1737 notify
->notify_mask
= params
.u
.params
->dwNotifyMask
;
1739 service_lock(service
->service_entry
);
1741 if (service
->service_entry
->notify
)
1743 service_unlock(service
->service_entry
);
1744 HeapFree(GetProcessHeap(), 0, notify
);
1745 return ERROR_ALREADY_REGISTERED
;
1748 mask
= 1 << (service
->service_entry
->status
.dwCurrentState
- SERVICE_STOPPED
);
1749 if (!service
->service_entry
->status_notified
&&
1750 (notify
->notify_mask
& mask
))
1752 fill_notify(notify
);
1753 service
->service_entry
->status_notified
= TRUE
;
1757 sc_notify_retain(notify
);
1758 service
->service_entry
->notify
= notify
;
1761 sc_notify_retain(notify
);
1762 *hNotify
= ¬ify
->hdr
;
1764 service_unlock(service
->service_entry
);
1766 return ERROR_SUCCESS
;
1769 DWORD __cdecl
svcctl_GetNotifyResults(
1770 SC_NOTIFY_RPC_HANDLE hNotify
,
1771 SC_RPC_NOTIFY_PARAMS_LIST
**pList
)
1774 struct sc_notify_handle
*notify
;
1776 WINE_TRACE("(%p, %p)\n", hNotify
, pList
);
1779 return ERROR_INVALID_PARAMETER
;
1783 if ((err
= validate_notify_handle(hNotify
, 0, ¬ify
)) != 0)
1786 sc_notify_retain(notify
);
1787 /* block until there is a result */
1788 err
= WaitForSingleObject(notify
->event
, INFINITE
);
1790 if (err
!= WAIT_OBJECT_0
)
1792 sc_notify_release(notify
);
1796 *pList
= InterlockedExchangePointer((void**)¬ify
->params_list
, NULL
);
1799 sc_notify_release(notify
);
1800 return ERROR_REQUEST_ABORTED
;
1803 sc_notify_release(notify
);
1805 return ERROR_SUCCESS
;
1808 DWORD __cdecl
svcctl_CloseNotifyHandle(
1809 SC_NOTIFY_RPC_HANDLE
*hNotify
,
1812 struct sc_notify_handle
*notify
;
1815 WINE_TRACE("(%p, %p)\n", hNotify
, apc_fired
);
1817 if ((err
= validate_notify_handle(*hNotify
, 0, ¬ify
)) != 0)
1820 sc_notify_release(notify
);
1822 return ERROR_SUCCESS
;
1825 DWORD __cdecl
svcctl_ControlServiceExA(
1826 SC_RPC_HANDLE service
,
1829 SC_RPC_SERVICE_CONTROL_IN_PARAMSA
*in_params
,
1830 SC_RPC_SERVICE_CONTROL_OUT_PARAMSA
*out_params
)
1833 return ERROR_CALL_NOT_IMPLEMENTED
;
1836 DWORD __cdecl
svcctl_ControlServiceExW(
1837 SC_RPC_HANDLE service
,
1840 SC_RPC_SERVICE_CONTROL_IN_PARAMSW
*in_params
,
1841 SC_RPC_SERVICE_CONTROL_OUT_PARAMSW
*out_params
)
1844 return ERROR_CALL_NOT_IMPLEMENTED
;
1847 DWORD __cdecl
svcctl_unknown52(void)
1850 return ERROR_CALL_NOT_IMPLEMENTED
;
1853 DWORD __cdecl
svcctl_unknown53(void)
1856 return ERROR_CALL_NOT_IMPLEMENTED
;
1859 DWORD __cdecl
svcctl_unknown54(void)
1862 return ERROR_CALL_NOT_IMPLEMENTED
;
1865 DWORD __cdecl
svcctl_unknown55(void)
1868 return ERROR_CALL_NOT_IMPLEMENTED
;
1871 DWORD __cdecl
svcctl_QueryServiceConfigEx(
1872 SC_RPC_HANDLE service
,
1874 SC_RPC_CONFIG_INFOW
*info
)
1877 return ERROR_CALL_NOT_IMPLEMENTED
;
1880 DWORD __cdecl
svcctl_QueryServiceObjectSecurity(
1881 SC_RPC_HANDLE service
,
1882 SECURITY_INFORMATION info
,
1888 return ERROR_CALL_NOT_IMPLEMENTED
;
1891 DWORD __cdecl
svcctl_SetServiceObjectSecurity(
1892 SC_RPC_HANDLE service
,
1893 SECURITY_INFORMATION info
,
1898 return ERROR_CALL_NOT_IMPLEMENTED
;
1901 DWORD __cdecl
svcctl_QueryServiceStatus(
1902 SC_RPC_HANDLE service
,
1903 SERVICE_STATUS
*status
)
1906 return ERROR_CALL_NOT_IMPLEMENTED
;
1909 DWORD __cdecl
svcctl_NotifyBootConfigStatus(
1910 SVCCTL_HANDLEW machinename
,
1911 DWORD boot_acceptable
)
1914 return ERROR_CALL_NOT_IMPLEMENTED
;
1917 DWORD __cdecl
svcctl_SCSetServiceBitsW(void)
1920 return ERROR_CALL_NOT_IMPLEMENTED
;
1923 DWORD __cdecl
svcctl_EnumDependentServicesW(
1924 SC_RPC_HANDLE service
,
1929 DWORD
*services_ret
)
1932 return ERROR_CALL_NOT_IMPLEMENTED
;
1935 DWORD __cdecl
svcctl_QueryServiceLockStatusW(
1936 SC_RPC_HANDLE scmanager
,
1937 QUERY_SERVICE_LOCK_STATUSW
*status
,
1942 return ERROR_CALL_NOT_IMPLEMENTED
;
1945 DWORD __cdecl
svcctl_SCSetServiceBitsA(void)
1948 return ERROR_CALL_NOT_IMPLEMENTED
;
1951 DWORD __cdecl
svcctl_ChangeServiceConfigA(
1952 SC_RPC_HANDLE service
,
1955 DWORD error_control
,
1957 LPSTR loadordergroup
,
1963 DWORD password_size
,
1967 return ERROR_CALL_NOT_IMPLEMENTED
;
1970 DWORD __cdecl
svcctl_CreateServiceA(
1971 SC_RPC_HANDLE scmanager
,
1974 DWORD desiredaccess
,
1977 DWORD error_control
,
1979 LPCSTR loadordergroup
,
1981 const BYTE
*dependencies
,
1984 const BYTE
*password
,
1985 DWORD password_size
,
1986 SC_RPC_HANDLE
*service
)
1989 return ERROR_CALL_NOT_IMPLEMENTED
;
1992 DWORD __cdecl
svcctl_EnumDependentServicesA(
1993 SC_RPC_HANDLE service
,
1998 DWORD
*services_ret
)
2001 return ERROR_CALL_NOT_IMPLEMENTED
;
2004 DWORD __cdecl
svcctl_EnumServicesStatusA(
2005 SC_RPC_HANDLE hmngr
,
2015 return ERROR_CALL_NOT_IMPLEMENTED
;
2018 DWORD __cdecl
svcctl_OpenSCManagerA(
2019 MACHINE_HANDLEA MachineName
,
2020 LPCSTR DatabaseName
,
2022 SC_RPC_HANDLE
*handle
)
2025 return ERROR_CALL_NOT_IMPLEMENTED
;
2028 DWORD __cdecl
svcctl_OpenServiceA(
2029 SC_RPC_HANDLE hSCManager
,
2030 LPCSTR lpServiceName
,
2031 DWORD dwDesiredAccess
,
2032 SC_RPC_HANDLE
*phService
)
2035 return ERROR_CALL_NOT_IMPLEMENTED
;
2038 DWORD __cdecl
svcctl_QueryServiceConfigA(
2039 SC_RPC_HANDLE hService
,
2040 QUERY_SERVICE_CONFIGA
*config
,
2045 return ERROR_CALL_NOT_IMPLEMENTED
;
2048 DWORD __cdecl
svcctl_QueryServiceLockStatusA(
2049 SC_RPC_HANDLE scmanager
,
2050 QUERY_SERVICE_LOCK_STATUSA
*status
,
2055 return ERROR_CALL_NOT_IMPLEMENTED
;
2058 DWORD __cdecl
svcctl_StartServiceA(
2059 SC_RPC_HANDLE service
,
2064 return ERROR_CALL_NOT_IMPLEMENTED
;
2067 DWORD __cdecl
svcctl_GetServiceDisplayNameA(
2068 SC_RPC_HANDLE hSCManager
,
2074 return ERROR_CALL_NOT_IMPLEMENTED
;
2077 DWORD __cdecl
svcctl_GetServiceKeyNameA(
2078 SC_RPC_HANDLE hSCManager
,
2084 return ERROR_CALL_NOT_IMPLEMENTED
;
2087 DWORD __cdecl
svcctl_GetCurrentGroupStateW(void)
2090 return ERROR_CALL_NOT_IMPLEMENTED
;
2093 DWORD __cdecl
svcctl_EnumServiceGroupW(
2094 SC_RPC_HANDLE scmanager
,
2096 DWORD service_state
,
2100 DWORD
*returned_size
,
2101 DWORD
*resume_index
,
2105 return ERROR_CALL_NOT_IMPLEMENTED
;
2108 DWORD __cdecl
svcctl_ChangeServiceConfig2A(
2109 SC_RPC_HANDLE service
,
2110 SC_RPC_CONFIG_INFOA info
)
2113 return ERROR_CALL_NOT_IMPLEMENTED
;
2116 DWORD __cdecl
svcctl_QueryServiceConfig2A(
2117 SC_RPC_HANDLE service
,
2124 return ERROR_CALL_NOT_IMPLEMENTED
;
2127 DWORD
RPC_Init(void)
2129 WCHAR transport
[] = SVCCTL_TRANSPORT
;
2130 WCHAR endpoint
[] = SVCCTL_ENDPOINT
;
2133 if (!(cleanup_group
= CreateThreadpoolCleanupGroup()))
2135 WINE_ERR("CreateThreadpoolCleanupGroup failed with error %u\n", GetLastError());
2136 return GetLastError();
2139 if ((err
= RpcServerUseProtseqEpW(transport
, 0, endpoint
, NULL
)) != ERROR_SUCCESS
)
2141 WINE_ERR("RpcServerUseProtseq failed with error %u\n", err
);
2145 if ((err
= RpcServerRegisterIf(svcctl_v2_0_s_ifspec
, 0, 0)) != ERROR_SUCCESS
)
2147 WINE_ERR("RpcServerRegisterIf failed with error %u\n", err
);
2151 if ((err
= RpcServerListen(1, RPC_C_LISTEN_MAX_CALLS_DEFAULT
, TRUE
)) != ERROR_SUCCESS
)
2153 WINE_ERR("RpcServerListen failed with error %u\n", err
);
2157 exit_event
= __wine_make_process_system();
2158 return ERROR_SUCCESS
;
2163 RpcMgmtStopServerListening(NULL
);
2164 RpcServerUnregisterIf(svcctl_v2_0_s_ifspec
, NULL
, TRUE
);
2165 RpcMgmtWaitServerListen();
2167 CloseThreadpoolCleanupGroupMembers(cleanup_group
, TRUE
, NULL
);
2168 CloseThreadpoolCleanupGroup(cleanup_group
);
2169 CloseHandle(exit_event
);
2172 void __RPC_USER
SC_RPC_HANDLE_rundown(SC_RPC_HANDLE handle
)
2174 SC_RPC_HANDLE_destroy(handle
);
2177 void __RPC_USER
SC_NOTIFY_RPC_HANDLE_rundown(SC_NOTIFY_RPC_HANDLE handle
)
2181 void __RPC_FAR
* __RPC_USER
MIDL_user_allocate(SIZE_T len
)
2183 return HeapAlloc(GetProcessHeap(), 0, len
);
2186 void __RPC_USER
MIDL_user_free(void __RPC_FAR
* ptr
)
2188 HeapFree(GetProcessHeap(), 0, ptr
);