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,
71 struct sc_manager_handle
/* service control manager handle */
74 struct scmdatabase
*db
;
77 struct sc_service_handle
/* service handle */
80 struct service_entry
*service_entry
;
85 struct scmdatabase
*db
;
88 static HANDLE timeout_queue_event
;
89 static CRITICAL_SECTION timeout_queue_cs
;
90 static CRITICAL_SECTION_DEBUG timeout_queue_cs_debug
=
92 0, 0, &timeout_queue_cs
,
93 { &timeout_queue_cs_debug
.ProcessLocksList
, &timeout_queue_cs_debug
.ProcessLocksList
},
94 0, 0, { (DWORD_PTR
)(__FILE__
": timeout_queue_cs") }
96 static CRITICAL_SECTION timeout_queue_cs
= { &timeout_queue_cs_debug
, -1, 0, 0, 0, 0 };
97 static struct list timeout_queue
= LIST_INIT(timeout_queue
);
98 struct timeout_queue_elem
103 void (*func
)(struct service_entry
*);
104 struct service_entry
*service_entry
;
107 static void run_after_timeout(void (*func
)(struct service_entry
*), struct service_entry
*service
, DWORD timeout
)
109 struct timeout_queue_elem
*elem
= HeapAlloc(GetProcessHeap(), 0, sizeof(struct timeout_queue_elem
));
117 InterlockedIncrement(&service
->ref_count
);
119 elem
->service_entry
= service
;
121 GetSystemTimeAsFileTime(&elem
->time
);
122 time
.u
.LowPart
= elem
->time
.dwLowDateTime
;
123 time
.u
.HighPart
= elem
->time
.dwHighDateTime
;
124 time
.QuadPart
+= (ULONGLONG
)timeout
* 10000;
125 elem
->time
.dwLowDateTime
= time
.u
.LowPart
;
126 elem
->time
.dwHighDateTime
= time
.u
.HighPart
;
128 EnterCriticalSection(&timeout_queue_cs
);
129 list_add_head(&timeout_queue
, &elem
->entry
);
130 LeaveCriticalSection(&timeout_queue_cs
);
132 SetEvent(timeout_queue_event
);
135 static void free_service_strings(struct service_entry
*old
, struct service_entry
*new)
137 QUERY_SERVICE_CONFIGW
*old_cfg
= &old
->config
;
138 QUERY_SERVICE_CONFIGW
*new_cfg
= &new->config
;
140 if (old_cfg
->lpBinaryPathName
!= new_cfg
->lpBinaryPathName
)
141 HeapFree(GetProcessHeap(), 0, old_cfg
->lpBinaryPathName
);
143 if (old_cfg
->lpLoadOrderGroup
!= new_cfg
->lpLoadOrderGroup
)
144 HeapFree(GetProcessHeap(), 0, old_cfg
->lpLoadOrderGroup
);
146 if (old_cfg
->lpServiceStartName
!= new_cfg
->lpServiceStartName
)
147 HeapFree(GetProcessHeap(), 0, old_cfg
->lpServiceStartName
);
149 if (old_cfg
->lpDisplayName
!= new_cfg
->lpDisplayName
)
150 HeapFree(GetProcessHeap(), 0, old_cfg
->lpDisplayName
);
152 if (old
->dependOnServices
!= new->dependOnServices
)
153 HeapFree(GetProcessHeap(), 0, old
->dependOnServices
);
155 if (old
->dependOnGroups
!= new->dependOnGroups
)
156 HeapFree(GetProcessHeap(), 0, old
->dependOnGroups
);
159 /* Check if the given handle is of the required type and allows the requested access. */
160 static DWORD
validate_context_handle(SC_RPC_HANDLE handle
, DWORD type
, DWORD needed_access
, struct sc_handle
**out_hdr
)
162 struct sc_handle
*hdr
= handle
;
164 if (type
!= SC_HTYPE_DONT_CARE
&& hdr
->type
!= type
)
166 WINE_ERR("Handle is of an invalid type (%d, %d)\n", hdr
->type
, type
);
167 return ERROR_INVALID_HANDLE
;
170 if ((needed_access
& hdr
->access
) != needed_access
)
172 WINE_ERR("Access denied - handle created with access %x, needed %x\n", hdr
->access
, needed_access
);
173 return ERROR_ACCESS_DENIED
;
177 return ERROR_SUCCESS
;
180 static DWORD
validate_scm_handle(SC_RPC_HANDLE handle
, DWORD needed_access
, struct sc_manager_handle
**manager
)
182 struct sc_handle
*hdr
;
183 DWORD err
= validate_context_handle(handle
, SC_HTYPE_MANAGER
, needed_access
, &hdr
);
184 if (err
== ERROR_SUCCESS
)
185 *manager
= (struct sc_manager_handle
*)hdr
;
189 static DWORD
validate_service_handle(SC_RPC_HANDLE handle
, DWORD needed_access
, struct sc_service_handle
**service
)
191 struct sc_handle
*hdr
;
192 DWORD err
= validate_context_handle(handle
, SC_HTYPE_SERVICE
, needed_access
, &hdr
);
193 if (err
== ERROR_SUCCESS
)
194 *service
= (struct sc_service_handle
*)hdr
;
198 DWORD __cdecl
svcctl_OpenSCManagerW(
199 MACHINE_HANDLEW MachineName
, /* Note: this parameter is ignored */
200 LPCWSTR DatabaseName
,
202 SC_RPC_HANDLE
*handle
)
204 struct sc_manager_handle
*manager
;
206 WINE_TRACE("(%s, %s, %x)\n", wine_dbgstr_w(MachineName
), wine_dbgstr_w(DatabaseName
), dwAccessMask
);
208 if (DatabaseName
!= NULL
&& DatabaseName
[0])
210 if (strcmpW(DatabaseName
, SERVICES_FAILED_DATABASEW
) == 0)
211 return ERROR_DATABASE_DOES_NOT_EXIST
;
212 if (strcmpW(DatabaseName
, SERVICES_ACTIVE_DATABASEW
) != 0)
213 return ERROR_INVALID_NAME
;
216 if (!(manager
= HeapAlloc(GetProcessHeap(), 0, sizeof(*manager
))))
217 return ERROR_NOT_ENOUGH_SERVER_MEMORY
;
219 manager
->hdr
.type
= SC_HTYPE_MANAGER
;
221 if (dwAccessMask
& MAXIMUM_ALLOWED
)
222 dwAccessMask
|= SC_MANAGER_ALL_ACCESS
;
223 manager
->hdr
.access
= dwAccessMask
;
224 RtlMapGenericMask(&manager
->hdr
.access
, &g_scm_generic
);
225 manager
->db
= active_database
;
226 *handle
= &manager
->hdr
;
228 return ERROR_SUCCESS
;
231 static void SC_RPC_HANDLE_destroy(SC_RPC_HANDLE handle
)
233 struct sc_handle
*hdr
= handle
;
236 case SC_HTYPE_MANAGER
:
238 struct sc_manager_handle
*manager
= (struct sc_manager_handle
*)hdr
;
239 HeapFree(GetProcessHeap(), 0, manager
);
242 case SC_HTYPE_SERVICE
:
244 struct sc_service_handle
*service
= (struct sc_service_handle
*)hdr
;
245 release_service(service
->service_entry
);
246 HeapFree(GetProcessHeap(), 0, service
);
250 WINE_ERR("invalid handle type %d\n", hdr
->type
);
251 RpcRaiseException(ERROR_INVALID_HANDLE
);
255 DWORD __cdecl
svcctl_GetServiceDisplayNameW(
256 SC_RPC_HANDLE hSCManager
,
257 LPCWSTR lpServiceName
,
261 struct sc_manager_handle
*manager
;
262 struct service_entry
*entry
;
265 WINE_TRACE("(%s, %d)\n", wine_dbgstr_w(lpServiceName
), *cchBufSize
);
267 if ((err
= validate_scm_handle(hSCManager
, 0, &manager
)) != ERROR_SUCCESS
)
270 scmdatabase_lock(manager
->db
);
272 entry
= scmdatabase_find_service(manager
->db
, lpServiceName
);
277 name
= get_display_name(entry
);
279 if (len
<= *cchBufSize
)
282 memcpy(lpBuffer
, name
, (len
+ 1)*sizeof(*name
));
285 err
= ERROR_INSUFFICIENT_BUFFER
;
289 err
= ERROR_SERVICE_DOES_NOT_EXIST
;
291 scmdatabase_unlock(manager
->db
);
293 if (err
!= ERROR_SUCCESS
)
299 DWORD __cdecl
svcctl_GetServiceKeyNameW(
300 SC_RPC_HANDLE hSCManager
,
301 LPCWSTR lpServiceDisplayName
,
305 struct service_entry
*entry
;
306 struct sc_manager_handle
*manager
;
309 WINE_TRACE("(%s, %d)\n", wine_dbgstr_w(lpServiceDisplayName
), *cchBufSize
);
311 if ((err
= validate_scm_handle(hSCManager
, 0, &manager
)) != ERROR_SUCCESS
)
314 scmdatabase_lock(manager
->db
);
316 entry
= scmdatabase_find_service_by_displayname(manager
->db
, lpServiceDisplayName
);
320 len
= strlenW(entry
->name
);
321 if (len
<= *cchBufSize
)
324 memcpy(lpBuffer
, entry
->name
, (len
+ 1)*sizeof(*entry
->name
));
327 err
= ERROR_INSUFFICIENT_BUFFER
;
331 err
= ERROR_SERVICE_DOES_NOT_EXIST
;
333 scmdatabase_unlock(manager
->db
);
335 if (err
!= ERROR_SUCCESS
)
341 static DWORD
create_handle_for_service(struct service_entry
*entry
, DWORD dwDesiredAccess
, SC_RPC_HANDLE
*phService
)
343 struct sc_service_handle
*service
;
345 if (!(service
= HeapAlloc(GetProcessHeap(), 0, sizeof(*service
))))
347 release_service(entry
);
348 return ERROR_NOT_ENOUGH_SERVER_MEMORY
;
351 if (dwDesiredAccess
& MAXIMUM_ALLOWED
)
352 dwDesiredAccess
|= SERVICE_ALL_ACCESS
;
354 service
->hdr
.type
= SC_HTYPE_SERVICE
;
355 service
->hdr
.access
= dwDesiredAccess
;
356 RtlMapGenericMask(&service
->hdr
.access
, &g_svc_generic
);
357 service
->service_entry
= entry
;
359 *phService
= &service
->hdr
;
360 return ERROR_SUCCESS
;
363 DWORD __cdecl
svcctl_OpenServiceW(
364 SC_RPC_HANDLE hSCManager
,
365 LPCWSTR lpServiceName
,
366 DWORD dwDesiredAccess
,
367 SC_RPC_HANDLE
*phService
)
369 struct sc_manager_handle
*manager
;
370 struct service_entry
*entry
;
373 WINE_TRACE("(%s, 0x%x)\n", wine_dbgstr_w(lpServiceName
), dwDesiredAccess
);
375 if ((err
= validate_scm_handle(hSCManager
, 0, &manager
)) != ERROR_SUCCESS
)
377 if (!validate_service_name(lpServiceName
))
378 return ERROR_INVALID_NAME
;
380 scmdatabase_lock(manager
->db
);
381 entry
= scmdatabase_find_service(manager
->db
, lpServiceName
);
383 InterlockedIncrement(&entry
->ref_count
);
384 scmdatabase_unlock(manager
->db
);
387 return ERROR_SERVICE_DOES_NOT_EXIST
;
389 return create_handle_for_service(entry
, dwDesiredAccess
, phService
);
392 static DWORD
parse_dependencies(const WCHAR
*dependencies
, struct service_entry
*entry
)
394 WCHAR
*services
= NULL
, *groups
, *s
;
395 DWORD len
, len_services
= 0, len_groups
= 0;
396 const WCHAR
*ptr
= dependencies
;
398 if (!dependencies
|| !dependencies
[0])
400 entry
->dependOnServices
= NULL
;
401 entry
->dependOnGroups
= NULL
;
402 return ERROR_SUCCESS
;
407 len
= strlenW(ptr
) + 1;
408 if (ptr
[0] == '+' && ptr
[1])
409 len_groups
+= len
- 1;
414 if (!len_services
) entry
->dependOnServices
= NULL
;
417 services
= HeapAlloc(GetProcessHeap(), 0, (len_services
+ 1) * sizeof(WCHAR
));
419 return ERROR_OUTOFMEMORY
;
425 len
= strlenW(ptr
) + 1;
434 entry
->dependOnServices
= services
;
436 if (!len_groups
) entry
->dependOnGroups
= NULL
;
439 groups
= HeapAlloc(GetProcessHeap(), 0, (len_groups
+ 1) * sizeof(WCHAR
));
442 HeapFree(GetProcessHeap(), 0, services
);
443 return ERROR_OUTOFMEMORY
;
449 len
= strlenW(ptr
) + 1;
450 if (ptr
[0] == '+' && ptr
[1])
458 entry
->dependOnGroups
= groups
;
461 return ERROR_SUCCESS
;
464 static DWORD
create_serviceW(
465 SC_RPC_HANDLE hSCManager
,
466 LPCWSTR lpServiceName
,
467 LPCWSTR lpDisplayName
,
468 DWORD dwDesiredAccess
,
471 DWORD dwErrorControl
,
472 LPCWSTR lpBinaryPathName
,
473 LPCWSTR lpLoadOrderGroup
,
475 const BYTE
*lpDependencies
,
476 DWORD dwDependenciesSize
,
477 LPCWSTR lpServiceStartName
,
478 const BYTE
*lpPassword
,
479 DWORD dwPasswordSize
,
480 SC_RPC_HANDLE
*phService
,
483 struct service_entry
*entry
, *found
;
484 struct sc_manager_handle
*manager
;
487 WINE_TRACE("(%s, %s, 0x%x, %s)\n", wine_dbgstr_w(lpServiceName
), wine_dbgstr_w(lpDisplayName
), dwDesiredAccess
, wine_dbgstr_w(lpBinaryPathName
));
489 if ((err
= validate_scm_handle(hSCManager
, SC_MANAGER_CREATE_SERVICE
, &manager
)) != ERROR_SUCCESS
)
492 if (!validate_service_name(lpServiceName
))
493 return ERROR_INVALID_NAME
;
494 if (!check_multisz((LPCWSTR
)lpDependencies
, dwDependenciesSize
) || !lpServiceName
[0] || !lpBinaryPathName
[0])
495 return ERROR_INVALID_PARAMETER
;
498 WINE_FIXME("Don't know how to add a password\n"); /* I always get ERROR_GEN_FAILURE */
500 err
= service_create(lpServiceName
, &entry
);
501 if (err
!= ERROR_SUCCESS
)
504 err
= parse_dependencies((LPCWSTR
)lpDependencies
, entry
);
505 if (err
!= ERROR_SUCCESS
) {
506 free_service_entry(entry
);
510 entry
->is_wow64
= is_wow64
;
511 entry
->config
.dwServiceType
= entry
->status
.dwServiceType
= dwServiceType
;
512 entry
->config
.dwStartType
= dwStartType
;
513 entry
->config
.dwErrorControl
= dwErrorControl
;
514 entry
->config
.lpBinaryPathName
= strdupW(lpBinaryPathName
);
515 entry
->config
.lpLoadOrderGroup
= strdupW(lpLoadOrderGroup
);
516 entry
->config
.lpServiceStartName
= strdupW(lpServiceStartName
);
517 entry
->config
.lpDisplayName
= strdupW(lpDisplayName
);
519 if (lpdwTagId
) /* TODO: In most situations a non-NULL TagId will generate an ERROR_INVALID_PARAMETER. */
520 entry
->config
.dwTagId
= *lpdwTagId
;
522 entry
->config
.dwTagId
= 0;
524 /* other fields NULL*/
526 if (!validate_service_config(entry
))
528 WINE_ERR("Invalid data while trying to create service\n");
529 free_service_entry(entry
);
530 return ERROR_INVALID_PARAMETER
;
533 scmdatabase_lock(manager
->db
);
535 if ((found
= scmdatabase_find_service(manager
->db
, lpServiceName
)))
537 err
= is_marked_for_delete(found
) ? ERROR_SERVICE_MARKED_FOR_DELETE
: ERROR_SERVICE_EXISTS
;
538 scmdatabase_unlock(manager
->db
);
539 free_service_entry(entry
);
543 if (scmdatabase_find_service_by_displayname(manager
->db
, get_display_name(entry
)))
545 scmdatabase_unlock(manager
->db
);
546 free_service_entry(entry
);
547 return ERROR_DUPLICATE_SERVICE_NAME
;
550 err
= scmdatabase_add_service(manager
->db
, entry
);
551 if (err
!= ERROR_SUCCESS
)
553 scmdatabase_unlock(manager
->db
);
554 free_service_entry(entry
);
557 scmdatabase_unlock(manager
->db
);
559 return create_handle_for_service(entry
, dwDesiredAccess
, phService
);
562 DWORD __cdecl
svcctl_CreateServiceW(
563 SC_RPC_HANDLE hSCManager
,
564 LPCWSTR lpServiceName
,
565 LPCWSTR lpDisplayName
,
566 DWORD dwDesiredAccess
,
569 DWORD dwErrorControl
,
570 LPCWSTR lpBinaryPathName
,
571 LPCWSTR lpLoadOrderGroup
,
573 const BYTE
*lpDependencies
,
574 DWORD dwDependenciesSize
,
575 LPCWSTR lpServiceStartName
,
576 const BYTE
*lpPassword
,
577 DWORD dwPasswordSize
,
578 SC_RPC_HANDLE
*phService
)
580 WINE_TRACE("(%s, %s, 0x%x, %s)\n", wine_dbgstr_w(lpServiceName
), wine_dbgstr_w(lpDisplayName
), dwDesiredAccess
, wine_dbgstr_w(lpBinaryPathName
));
581 return create_serviceW(hSCManager
, lpServiceName
, lpDisplayName
, dwDesiredAccess
, dwServiceType
, dwStartType
,
582 dwErrorControl
, lpBinaryPathName
, lpLoadOrderGroup
, lpdwTagId
, lpDependencies
, dwDependenciesSize
, lpServiceStartName
,
583 lpPassword
, dwPasswordSize
, phService
, FALSE
);
586 DWORD __cdecl
svcctl_DeleteService(
587 SC_RPC_HANDLE hService
)
589 struct sc_service_handle
*service
;
592 if ((err
= validate_service_handle(hService
, DELETE
, &service
)) != ERROR_SUCCESS
)
595 service_lock(service
->service_entry
);
597 if (!is_marked_for_delete(service
->service_entry
))
598 err
= mark_for_delete(service
->service_entry
);
600 err
= ERROR_SERVICE_MARKED_FOR_DELETE
;
602 service_unlock(service
->service_entry
);
607 DWORD __cdecl
svcctl_QueryServiceConfigW(
608 SC_RPC_HANDLE hService
,
609 QUERY_SERVICE_CONFIGW
*config
,
613 struct sc_service_handle
*service
;
616 WINE_TRACE("(%p)\n", config
);
618 if ((err
= validate_service_handle(hService
, SERVICE_QUERY_CONFIG
, &service
)) != 0)
621 service_lock(service
->service_entry
);
622 config
->dwServiceType
= service
->service_entry
->config
.dwServiceType
;
623 config
->dwStartType
= service
->service_entry
->config
.dwStartType
;
624 config
->dwErrorControl
= service
->service_entry
->config
.dwErrorControl
;
625 config
->lpBinaryPathName
= strdupW(service
->service_entry
->config
.lpBinaryPathName
);
626 config
->lpLoadOrderGroup
= strdupW(service
->service_entry
->config
.lpLoadOrderGroup
);
627 config
->dwTagId
= service
->service_entry
->config
.dwTagId
;
628 config
->lpDependencies
= NULL
; /* TODO */
629 config
->lpServiceStartName
= strdupW(service
->service_entry
->config
.lpServiceStartName
);
630 config
->lpDisplayName
= strdupW(service
->service_entry
->config
.lpDisplayName
);
631 service_unlock(service
->service_entry
);
633 return ERROR_SUCCESS
;
636 DWORD __cdecl
svcctl_ChangeServiceConfigW(
637 SC_RPC_HANDLE hService
,
640 DWORD dwErrorControl
,
641 LPCWSTR lpBinaryPathName
,
642 LPCWSTR lpLoadOrderGroup
,
644 const BYTE
*lpDependencies
,
645 DWORD dwDependenciesSize
,
646 LPCWSTR lpServiceStartName
,
647 const BYTE
*lpPassword
,
648 DWORD dwPasswordSize
,
649 LPCWSTR lpDisplayName
)
651 struct service_entry new_entry
, *entry
;
652 struct sc_service_handle
*service
;
657 if ((err
= validate_service_handle(hService
, SERVICE_CHANGE_CONFIG
, &service
)) != 0)
660 if (!check_multisz((LPCWSTR
)lpDependencies
, dwDependenciesSize
))
661 return ERROR_INVALID_PARAMETER
;
663 /* first check if the new configuration is correct */
664 service_lock(service
->service_entry
);
666 if (is_marked_for_delete(service
->service_entry
))
668 service_unlock(service
->service_entry
);
669 return ERROR_SERVICE_MARKED_FOR_DELETE
;
672 if (lpDisplayName
!= NULL
&&
673 (entry
= scmdatabase_find_service_by_displayname(service
->service_entry
->db
, lpDisplayName
)) &&
674 (entry
!= service
->service_entry
))
676 service_unlock(service
->service_entry
);
677 return ERROR_DUPLICATE_SERVICE_NAME
;
680 new_entry
= *service
->service_entry
;
682 if (dwServiceType
!= SERVICE_NO_CHANGE
)
683 new_entry
.config
.dwServiceType
= dwServiceType
;
685 if (dwStartType
!= SERVICE_NO_CHANGE
)
686 new_entry
.config
.dwStartType
= dwStartType
;
688 if (dwErrorControl
!= SERVICE_NO_CHANGE
)
689 new_entry
.config
.dwErrorControl
= dwErrorControl
;
691 if (lpBinaryPathName
!= NULL
)
692 new_entry
.config
.lpBinaryPathName
= (LPWSTR
)lpBinaryPathName
;
694 if (lpLoadOrderGroup
!= NULL
)
695 new_entry
.config
.lpLoadOrderGroup
= (LPWSTR
)lpLoadOrderGroup
;
697 if (lpdwTagId
!= NULL
)
698 WINE_FIXME("Changing tag id not supported\n");
700 if (lpServiceStartName
!= NULL
)
701 new_entry
.config
.lpServiceStartName
= (LPWSTR
)lpServiceStartName
;
703 if (lpPassword
!= NULL
)
704 WINE_FIXME("Setting password not supported\n");
706 if (lpDisplayName
!= NULL
)
707 new_entry
.config
.lpDisplayName
= (LPWSTR
)lpDisplayName
;
709 err
= parse_dependencies((LPCWSTR
)lpDependencies
, &new_entry
);
710 if (err
!= ERROR_SUCCESS
)
712 service_unlock(service
->service_entry
);
716 if (!validate_service_config(&new_entry
))
718 WINE_ERR("The configuration after the change wouldn't be valid\n");
719 service_unlock(service
->service_entry
);
720 return ERROR_INVALID_PARAMETER
;
723 /* configuration OK. The strings needs to be duplicated */
724 if (lpBinaryPathName
!= NULL
)
725 new_entry
.config
.lpBinaryPathName
= strdupW(lpBinaryPathName
);
727 if (lpLoadOrderGroup
!= NULL
)
728 new_entry
.config
.lpLoadOrderGroup
= strdupW(lpLoadOrderGroup
);
730 if (lpServiceStartName
!= NULL
)
731 new_entry
.config
.lpServiceStartName
= strdupW(lpServiceStartName
);
733 if (lpDisplayName
!= NULL
)
734 new_entry
.config
.lpDisplayName
= strdupW(lpDisplayName
);
736 /* try to save to Registry, commit or rollback depending on success */
737 err
= save_service_config(&new_entry
);
738 if (ERROR_SUCCESS
== err
)
740 free_service_strings(service
->service_entry
, &new_entry
);
741 *service
->service_entry
= new_entry
;
743 else free_service_strings(&new_entry
, service
->service_entry
);
744 service_unlock(service
->service_entry
);
749 DWORD __cdecl
svcctl_SetServiceStatus(
750 SC_RPC_HANDLE hServiceStatus
,
751 LPSERVICE_STATUS lpServiceStatus
)
753 struct sc_service_handle
*service
;
756 WINE_TRACE("(%p, %p)\n", hServiceStatus
, lpServiceStatus
);
758 if ((err
= validate_service_handle(hServiceStatus
, SERVICE_SET_STATUS
, &service
)) != 0)
761 service_lock(service
->service_entry
);
762 /* FIXME: be a bit more discriminant about what parts of the status we set
763 * and check that fields are valid */
764 service
->service_entry
->status
.dwServiceType
= lpServiceStatus
->dwServiceType
;
765 service
->service_entry
->status
.dwCurrentState
= lpServiceStatus
->dwCurrentState
;
766 service
->service_entry
->status
.dwControlsAccepted
= lpServiceStatus
->dwControlsAccepted
;
767 service
->service_entry
->status
.dwWin32ExitCode
= lpServiceStatus
->dwWin32ExitCode
;
768 service
->service_entry
->status
.dwServiceSpecificExitCode
= lpServiceStatus
->dwServiceSpecificExitCode
;
769 service
->service_entry
->status
.dwCheckPoint
= lpServiceStatus
->dwCheckPoint
;
770 service
->service_entry
->status
.dwWaitHint
= lpServiceStatus
->dwWaitHint
;
771 service_unlock(service
->service_entry
);
773 if (lpServiceStatus
->dwCurrentState
== SERVICE_STOPPED
)
774 run_after_timeout(service_terminate
, service
->service_entry
, service_kill_timeout
);
775 else if (service
->service_entry
->process
->status_changed_event
)
776 SetEvent(service
->service_entry
->process
->status_changed_event
);
778 return ERROR_SUCCESS
;
781 DWORD __cdecl
svcctl_ChangeServiceConfig2W( SC_RPC_HANDLE hService
, SC_RPC_CONFIG_INFOW config
)
783 struct sc_service_handle
*service
;
786 if ((err
= validate_service_handle(hService
, SERVICE_CHANGE_CONFIG
, &service
)) != 0)
789 switch (config
.dwInfoLevel
)
791 case SERVICE_CONFIG_DESCRIPTION
:
795 if (config
.u
.descr
->lpDescription
[0])
797 if (!(descr
= strdupW( config
.u
.descr
->lpDescription
)))
798 return ERROR_NOT_ENOUGH_MEMORY
;
801 WINE_TRACE( "changing service %p descr to %s\n", service
, wine_dbgstr_w(descr
) );
802 service_lock( service
->service_entry
);
803 HeapFree( GetProcessHeap(), 0, service
->service_entry
->description
);
804 service
->service_entry
->description
= descr
;
805 save_service_config( service
->service_entry
);
806 service_unlock( service
->service_entry
);
809 case SERVICE_CONFIG_FAILURE_ACTIONS
:
810 WINE_FIXME( "SERVICE_CONFIG_FAILURE_ACTIONS not implemented: period %u msg %s cmd %s\n",
811 config
.u
.actions
->dwResetPeriod
,
812 wine_dbgstr_w(config
.u
.actions
->lpRebootMsg
),
813 wine_dbgstr_w(config
.u
.actions
->lpCommand
) );
815 case SERVICE_CONFIG_PRESHUTDOWN_INFO
:
816 WINE_TRACE( "changing service %p preshutdown timeout to %d\n",
817 service
, config
.u
.preshutdown
->dwPreshutdownTimeout
);
818 service_lock( service
->service_entry
);
819 service
->service_entry
->preshutdown_timeout
= config
.u
.preshutdown
->dwPreshutdownTimeout
;
820 save_service_config( service
->service_entry
);
821 service_unlock( service
->service_entry
);
824 WINE_FIXME("level %u not implemented\n", config
.dwInfoLevel
);
825 err
= ERROR_INVALID_LEVEL
;
831 DWORD __cdecl
svcctl_QueryServiceConfig2W( SC_RPC_HANDLE hService
, DWORD level
,
832 BYTE
*buffer
, DWORD size
, LPDWORD needed
)
834 struct sc_service_handle
*service
;
837 memset(buffer
, 0, size
);
839 if ((err
= validate_service_handle(hService
, SERVICE_QUERY_STATUS
, &service
)) != 0)
844 case SERVICE_CONFIG_DESCRIPTION
:
846 SERVICE_DESCRIPTIONW
*descr
= (SERVICE_DESCRIPTIONW
*)buffer
;
848 service_lock(service
->service_entry
);
849 *needed
= sizeof(*descr
);
850 if (service
->service_entry
->description
)
851 *needed
+= (strlenW(service
->service_entry
->description
) + 1) * sizeof(WCHAR
);
854 if (service
->service_entry
->description
)
856 /* store a buffer offset instead of a pointer */
857 descr
->lpDescription
= (WCHAR
*)((BYTE
*)(descr
+ 1) - buffer
);
858 strcpyW( (WCHAR
*)(descr
+ 1), service
->service_entry
->description
);
860 else descr
->lpDescription
= NULL
;
862 else err
= ERROR_INSUFFICIENT_BUFFER
;
863 service_unlock(service
->service_entry
);
867 case SERVICE_CONFIG_PRESHUTDOWN_INFO
:
868 service_lock(service
->service_entry
);
870 *needed
= sizeof(SERVICE_PRESHUTDOWN_INFO
);
872 ((LPSERVICE_PRESHUTDOWN_INFO
)buffer
)->dwPreshutdownTimeout
=
873 service
->service_entry
->preshutdown_timeout
;
874 else err
= ERROR_INSUFFICIENT_BUFFER
;
876 service_unlock(service
->service_entry
);
880 WINE_FIXME("level %u not implemented\n", level
);
881 err
= ERROR_INVALID_LEVEL
;
887 DWORD __cdecl
svcctl_QueryServiceStatusEx(
888 SC_RPC_HANDLE hService
,
889 SC_STATUS_TYPE InfoLevel
,
892 LPDWORD pcbBytesNeeded
)
894 struct sc_service_handle
*service
;
896 LPSERVICE_STATUS_PROCESS pSvcStatusData
;
898 memset(lpBuffer
, 0, cbBufSize
);
900 if ((err
= validate_service_handle(hService
, SERVICE_QUERY_STATUS
, &service
)) != 0)
903 if (InfoLevel
!= SC_STATUS_PROCESS_INFO
)
904 return ERROR_INVALID_LEVEL
;
906 pSvcStatusData
= (LPSERVICE_STATUS_PROCESS
) lpBuffer
;
907 if (pSvcStatusData
== NULL
)
908 return ERROR_INVALID_PARAMETER
;
910 if (cbBufSize
< sizeof(SERVICE_STATUS_PROCESS
))
912 if( pcbBytesNeeded
!= NULL
)
913 *pcbBytesNeeded
= sizeof(SERVICE_STATUS_PROCESS
);
915 return ERROR_INSUFFICIENT_BUFFER
;
918 service_lock(service
->service_entry
);
920 pSvcStatusData
->dwServiceType
= service
->service_entry
->status
.dwServiceType
;
921 pSvcStatusData
->dwCurrentState
= service
->service_entry
->status
.dwCurrentState
;
922 pSvcStatusData
->dwControlsAccepted
= service
->service_entry
->status
.dwControlsAccepted
;
923 pSvcStatusData
->dwWin32ExitCode
= service
->service_entry
->status
.dwWin32ExitCode
;
924 pSvcStatusData
->dwServiceSpecificExitCode
= service
->service_entry
->status
.dwServiceSpecificExitCode
;
925 pSvcStatusData
->dwCheckPoint
= service
->service_entry
->status
.dwCheckPoint
;
926 pSvcStatusData
->dwWaitHint
= service
->service_entry
->status
.dwWaitHint
;
927 pSvcStatusData
->dwProcessId
= service
->service_entry
->status
.dwProcessId
;
928 pSvcStatusData
->dwServiceFlags
= service
->service_entry
->status
.dwServiceFlags
;
930 service_unlock(service
->service_entry
);
932 return ERROR_SUCCESS
;
935 /******************************************************************************
936 * service_accepts_control
938 static BOOL
service_accepts_control(const struct service_entry
*service
, DWORD dwControl
)
940 DWORD a
= service
->status
.dwControlsAccepted
;
944 case SERVICE_CONTROL_INTERROGATE
:
946 case SERVICE_CONTROL_STOP
:
947 if (a
&SERVICE_ACCEPT_STOP
)
950 case SERVICE_CONTROL_SHUTDOWN
:
951 if (a
&SERVICE_ACCEPT_SHUTDOWN
)
954 case SERVICE_CONTROL_PAUSE
:
955 case SERVICE_CONTROL_CONTINUE
:
956 if (a
&SERVICE_ACCEPT_PAUSE_CONTINUE
)
959 case SERVICE_CONTROL_PARAMCHANGE
:
960 if (a
&SERVICE_ACCEPT_PARAMCHANGE
)
963 case SERVICE_CONTROL_NETBINDADD
:
964 case SERVICE_CONTROL_NETBINDREMOVE
:
965 case SERVICE_CONTROL_NETBINDENABLE
:
966 case SERVICE_CONTROL_NETBINDDISABLE
:
967 if (a
&SERVICE_ACCEPT_NETBINDCHANGE
)
969 case SERVICE_CONTROL_HARDWAREPROFILECHANGE
:
970 if (a
&SERVICE_ACCEPT_HARDWAREPROFILECHANGE
)
973 case SERVICE_CONTROL_POWEREVENT
:
974 if (a
&SERVICE_ACCEPT_POWEREVENT
)
977 case SERVICE_CONTROL_SESSIONCHANGE
:
978 if (a
&SERVICE_ACCEPT_SESSIONCHANGE
)
985 /******************************************************************************
986 * process_send_command
988 BOOL
process_send_command(struct process_entry
*process
, const void *data
, DWORD size
, DWORD
*result
)
990 OVERLAPPED overlapped
;
994 overlapped
.hEvent
= process
->overlapped_event
;
995 r
= WriteFile(process
->control_pipe
, data
, size
, &count
, &overlapped
);
996 if (!r
&& GetLastError() == ERROR_IO_PENDING
)
998 ret
= WaitForSingleObject(process
->overlapped_event
, service_pipe_timeout
);
999 if (ret
== WAIT_TIMEOUT
)
1001 WINE_ERR("sending command timed out\n");
1002 *result
= ERROR_SERVICE_REQUEST_TIMEOUT
;
1005 r
= GetOverlappedResult(process
->control_pipe
, &overlapped
, &count
, FALSE
);
1007 if (!r
|| count
!= size
)
1009 WINE_ERR("service protocol error - failed to write pipe!\n");
1010 *result
= (!r
? GetLastError() : ERROR_WRITE_FAULT
);
1013 r
= ReadFile(process
->control_pipe
, result
, sizeof *result
, &count
, &overlapped
);
1014 if (!r
&& GetLastError() == ERROR_IO_PENDING
)
1016 ret
= WaitForSingleObject(process
->overlapped_event
, service_pipe_timeout
);
1017 if (ret
== WAIT_TIMEOUT
)
1019 WINE_ERR("receiving command result timed out\n");
1020 *result
= ERROR_SERVICE_REQUEST_TIMEOUT
;
1023 r
= GetOverlappedResult(process
->control_pipe
, &overlapped
, &count
, FALSE
);
1025 if (!r
|| count
!= sizeof *result
)
1027 WINE_ERR("service protocol error - failed to read pipe "
1028 "r = %d count = %d!\n", r
, count
);
1029 *result
= (!r
? GetLastError() : ERROR_READ_FAULT
);
1033 *result
= ERROR_SUCCESS
;
1037 /******************************************************************************
1038 * process_send_control
1040 static BOOL
process_send_control(struct process_entry
*process
, const WCHAR
*name
, DWORD dwControl
, DWORD
*result
)
1042 service_start_info
*ssi
;
1046 /* calculate how much space we need to send the startup info */
1047 len
= strlenW(name
) + 1;
1049 ssi
= HeapAlloc(GetProcessHeap(),0,FIELD_OFFSET(service_start_info
, data
[len
]));
1050 ssi
->cmd
= WINESERV_SENDCONTROL
;
1051 ssi
->control
= dwControl
;
1052 ssi
->total_size
= FIELD_OFFSET(service_start_info
, data
[len
]);
1053 ssi
->name_size
= strlenW(name
) + 1;
1054 strcpyW(ssi
->data
, name
);
1056 r
= process_send_command(process
, ssi
, ssi
->total_size
, result
);
1057 HeapFree( GetProcessHeap(), 0, ssi
);
1061 DWORD __cdecl
svcctl_StartServiceW(
1062 SC_RPC_HANDLE hService
,
1063 DWORD dwNumServiceArgs
,
1064 LPCWSTR
*lpServiceArgVectors
)
1066 struct sc_service_handle
*service
;
1069 WINE_TRACE("(%p, %d, %p)\n", hService
, dwNumServiceArgs
, lpServiceArgVectors
);
1071 if ((err
= validate_service_handle(hService
, SERVICE_START
, &service
)) != 0)
1074 if (service
->service_entry
->config
.dwStartType
== SERVICE_DISABLED
)
1075 return ERROR_SERVICE_DISABLED
;
1077 err
= service_start(service
->service_entry
, dwNumServiceArgs
, lpServiceArgVectors
);
1082 DWORD __cdecl
svcctl_ControlService(
1083 SC_RPC_HANDLE hService
,
1085 SERVICE_STATUS
*lpServiceStatus
)
1087 DWORD access_required
;
1088 struct sc_service_handle
*service
;
1091 HANDLE control_mutex
;
1093 WINE_TRACE("(%p, %d, %p)\n", hService
, dwControl
, lpServiceStatus
);
1097 case SERVICE_CONTROL_CONTINUE
:
1098 case SERVICE_CONTROL_NETBINDADD
:
1099 case SERVICE_CONTROL_NETBINDDISABLE
:
1100 case SERVICE_CONTROL_NETBINDENABLE
:
1101 case SERVICE_CONTROL_NETBINDREMOVE
:
1102 case SERVICE_CONTROL_PARAMCHANGE
:
1103 case SERVICE_CONTROL_PAUSE
:
1104 access_required
= SERVICE_PAUSE_CONTINUE
;
1106 case SERVICE_CONTROL_INTERROGATE
:
1107 access_required
= SERVICE_INTERROGATE
;
1109 case SERVICE_CONTROL_STOP
:
1110 access_required
= SERVICE_STOP
;
1113 if (dwControl
>= 128 && dwControl
<= 255)
1114 access_required
= SERVICE_USER_DEFINED_CONTROL
;
1116 return ERROR_INVALID_PARAMETER
;
1119 if ((result
= validate_service_handle(hService
, access_required
, &service
)) != 0)
1122 service_lock(service
->service_entry
);
1124 result
= ERROR_SUCCESS
;
1125 switch (service
->service_entry
->status
.dwCurrentState
)
1127 case SERVICE_STOPPED
:
1128 result
= ERROR_SERVICE_NOT_ACTIVE
;
1130 case SERVICE_START_PENDING
:
1131 if (dwControl
==SERVICE_CONTROL_STOP
)
1134 case SERVICE_STOP_PENDING
:
1135 result
= ERROR_SERVICE_CANNOT_ACCEPT_CTRL
;
1139 if (result
== ERROR_SUCCESS
&& service
->service_entry
->force_shutdown
)
1141 result
= ERROR_SERVICE_CANNOT_ACCEPT_CTRL
;
1142 service_terminate(service
->service_entry
);
1145 if (result
!= ERROR_SUCCESS
)
1147 if (lpServiceStatus
)
1149 lpServiceStatus
->dwServiceType
= service
->service_entry
->status
.dwServiceType
;
1150 lpServiceStatus
->dwCurrentState
= service
->service_entry
->status
.dwCurrentState
;
1151 lpServiceStatus
->dwControlsAccepted
= service
->service_entry
->status
.dwControlsAccepted
;
1152 lpServiceStatus
->dwWin32ExitCode
= service
->service_entry
->status
.dwWin32ExitCode
;
1153 lpServiceStatus
->dwServiceSpecificExitCode
= service
->service_entry
->status
.dwServiceSpecificExitCode
;
1154 lpServiceStatus
->dwCheckPoint
= service
->service_entry
->status
.dwCheckPoint
;
1155 lpServiceStatus
->dwWaitHint
= service
->service_entry
->status
.dwWaitHint
;
1157 service_unlock(service
->service_entry
);
1161 if (!service_accepts_control(service
->service_entry
, dwControl
))
1163 service_unlock(service
->service_entry
);
1164 return ERROR_INVALID_SERVICE_CONTROL
;
1167 /* Remember that we tried to shutdown this service. When the service is
1168 * still running on the second invocation, it will be forcefully killed. */
1169 if (dwControl
== SERVICE_CONTROL_STOP
)
1170 service
->service_entry
->force_shutdown
= TRUE
;
1172 control_mutex
= service
->service_entry
->process
->control_mutex
;
1173 service_unlock(service
->service_entry
);
1175 ret
= WaitForSingleObject(control_mutex
, 30000);
1176 if (ret
!= WAIT_OBJECT_0
)
1177 return ERROR_SERVICE_REQUEST_TIMEOUT
;
1179 process_send_control(service
->service_entry
->process
, service
->service_entry
->name
, dwControl
, &result
);
1181 if (lpServiceStatus
)
1183 service_lock(service
->service_entry
);
1184 lpServiceStatus
->dwServiceType
= service
->service_entry
->status
.dwServiceType
;
1185 lpServiceStatus
->dwCurrentState
= service
->service_entry
->status
.dwCurrentState
;
1186 lpServiceStatus
->dwControlsAccepted
= service
->service_entry
->status
.dwControlsAccepted
;
1187 lpServiceStatus
->dwWin32ExitCode
= service
->service_entry
->status
.dwWin32ExitCode
;
1188 lpServiceStatus
->dwServiceSpecificExitCode
= service
->service_entry
->status
.dwServiceSpecificExitCode
;
1189 lpServiceStatus
->dwCheckPoint
= service
->service_entry
->status
.dwCheckPoint
;
1190 lpServiceStatus
->dwWaitHint
= service
->service_entry
->status
.dwWaitHint
;
1191 service_unlock(service
->service_entry
);
1194 ReleaseMutex(control_mutex
);
1198 DWORD __cdecl
svcctl_CloseServiceHandle(
1199 SC_RPC_HANDLE
*handle
)
1201 WINE_TRACE("(&%p)\n", *handle
);
1203 SC_RPC_HANDLE_destroy(*handle
);
1206 return ERROR_SUCCESS
;
1209 static void SC_RPC_LOCK_destroy(SC_RPC_LOCK hLock
)
1211 struct sc_lock
*lock
= hLock
;
1212 scmdatabase_unlock_startup(lock
->db
);
1213 HeapFree(GetProcessHeap(), 0, lock
);
1216 void __RPC_USER
SC_RPC_LOCK_rundown(SC_RPC_LOCK hLock
)
1218 SC_RPC_LOCK_destroy(hLock
);
1221 DWORD __cdecl
svcctl_LockServiceDatabase(
1222 SC_RPC_HANDLE hSCManager
,
1223 SC_RPC_LOCK
*phLock
)
1225 struct sc_manager_handle
*manager
;
1226 struct sc_lock
*lock
;
1229 WINE_TRACE("(%p, %p)\n", hSCManager
, phLock
);
1231 if ((err
= validate_scm_handle(hSCManager
, SC_MANAGER_LOCK
, &manager
)) != ERROR_SUCCESS
)
1234 err
= scmdatabase_lock_startup(manager
->db
);
1235 if (err
!= ERROR_SUCCESS
)
1238 lock
= HeapAlloc(GetProcessHeap(), 0, sizeof(struct sc_lock
));
1241 scmdatabase_unlock_startup(manager
->db
);
1242 return ERROR_NOT_ENOUGH_SERVER_MEMORY
;
1245 lock
->db
= manager
->db
;
1248 return ERROR_SUCCESS
;
1251 DWORD __cdecl
svcctl_UnlockServiceDatabase(
1252 SC_RPC_LOCK
*phLock
)
1254 WINE_TRACE("(&%p)\n", *phLock
);
1256 SC_RPC_LOCK_destroy(*phLock
);
1259 return ERROR_SUCCESS
;
1262 static BOOL
map_state(DWORD state
, DWORD mask
)
1266 case SERVICE_START_PENDING
:
1267 case SERVICE_STOP_PENDING
:
1268 case SERVICE_RUNNING
:
1269 case SERVICE_CONTINUE_PENDING
:
1270 case SERVICE_PAUSE_PENDING
:
1271 case SERVICE_PAUSED
:
1272 if (SERVICE_ACTIVE
& mask
) return TRUE
;
1274 case SERVICE_STOPPED
:
1275 if (SERVICE_INACTIVE
& mask
) return TRUE
;
1278 WINE_ERR("unknown state %u\n", state
);
1284 DWORD __cdecl
svcctl_EnumServicesStatusW(
1285 SC_RPC_HANDLE hmngr
,
1294 DWORD err
, sz
, total_size
, num_services
;
1296 struct sc_manager_handle
*manager
;
1297 struct service_entry
*service
;
1298 ENUM_SERVICE_STATUSW
*s
;
1300 WINE_TRACE("(%p, 0x%x, 0x%x, %p, %u, %p, %p, %p)\n", hmngr
, type
, state
, buffer
, size
, needed
, returned
, resume
);
1302 if (!type
|| !state
)
1303 return ERROR_INVALID_PARAMETER
;
1305 if ((err
= validate_scm_handle(hmngr
, SC_MANAGER_ENUMERATE_SERVICE
, &manager
)) != ERROR_SUCCESS
)
1309 WINE_FIXME("resume index not supported\n");
1311 scmdatabase_lock(manager
->db
);
1313 total_size
= num_services
= 0;
1314 LIST_FOR_EACH_ENTRY(service
, &manager
->db
->services
, struct service_entry
, entry
)
1316 if ((service
->status
.dwServiceType
& type
) && map_state(service
->status
.dwCurrentState
, state
))
1318 total_size
+= sizeof(ENUM_SERVICE_STATUSW
);
1319 total_size
+= (strlenW(service
->name
) + 1) * sizeof(WCHAR
);
1320 if (service
->config
.lpDisplayName
)
1322 total_size
+= (strlenW(service
->config
.lpDisplayName
) + 1) * sizeof(WCHAR
);
1328 *needed
= total_size
;
1329 if (total_size
> size
)
1331 scmdatabase_unlock(manager
->db
);
1332 return ERROR_MORE_DATA
;
1334 s
= (ENUM_SERVICE_STATUSW
*)buffer
;
1335 offset
= num_services
* sizeof(ENUM_SERVICE_STATUSW
);
1336 LIST_FOR_EACH_ENTRY(service
, &manager
->db
->services
, struct service_entry
, entry
)
1338 if ((service
->status
.dwServiceType
& type
) && map_state(service
->status
.dwCurrentState
, state
))
1340 sz
= (strlenW(service
->name
) + 1) * sizeof(WCHAR
);
1341 memcpy(buffer
+ offset
, service
->name
, sz
);
1342 s
->lpServiceName
= (WCHAR
*)offset
; /* store a buffer offset instead of a pointer */
1345 if (!service
->config
.lpDisplayName
) s
->lpDisplayName
= NULL
;
1348 sz
= (strlenW(service
->config
.lpDisplayName
) + 1) * sizeof(WCHAR
);
1349 memcpy(buffer
+ offset
, service
->config
.lpDisplayName
, sz
);
1350 s
->lpDisplayName
= (WCHAR
*)offset
;
1353 memcpy(&s
->ServiceStatus
, &service
->status
, sizeof(SERVICE_STATUS
));
1357 *returned
= num_services
;
1359 scmdatabase_unlock(manager
->db
);
1360 return ERROR_SUCCESS
;
1363 static struct service_entry
*find_service_by_group(struct scmdatabase
*db
, const WCHAR
*group
)
1365 struct service_entry
*service
;
1366 LIST_FOR_EACH_ENTRY(service
, &db
->services
, struct service_entry
, entry
)
1368 if (service
->config
.lpLoadOrderGroup
&& !strcmpiW(group
, service
->config
.lpLoadOrderGroup
))
1374 static BOOL
match_group(const WCHAR
*g1
, const WCHAR
*g2
)
1376 if (!g2
) return TRUE
;
1377 if (!g2
[0] && (!g1
|| !g1
[0])) return TRUE
;
1378 if (g1
&& !strcmpW(g1
, g2
)) return TRUE
;
1382 DWORD __cdecl
svcctl_EnumServicesStatusExA(
1383 SC_RPC_HANDLE scmanager
,
1384 SC_ENUM_TYPE info_level
,
1386 DWORD service_state
,
1390 DWORD
*services_count
,
1391 DWORD
*resume_index
,
1395 return ERROR_CALL_NOT_IMPLEMENTED
;
1398 DWORD __cdecl
svcctl_EnumServicesStatusExW(
1399 SC_RPC_HANDLE hmngr
,
1400 SC_ENUM_TYPE info_level
,
1407 DWORD
*resume_handle
,
1410 DWORD err
, sz
, total_size
, num_services
;
1412 struct sc_manager_handle
*manager
;
1413 struct service_entry
*service
;
1414 ENUM_SERVICE_STATUS_PROCESSW
*s
;
1416 WINE_TRACE("(%p, 0x%x, 0x%x, %p, %u, %p, %p, %s)\n", hmngr
, type
, state
, buffer
, size
,
1417 needed
, returned
, wine_dbgstr_w(group
));
1420 FIXME("resume handle not supported\n");
1422 if (!type
|| !state
)
1423 return ERROR_INVALID_PARAMETER
;
1425 if ((err
= validate_scm_handle(hmngr
, SC_MANAGER_ENUMERATE_SERVICE
, &manager
)) != ERROR_SUCCESS
)
1428 scmdatabase_lock(manager
->db
);
1430 if (group
&& !find_service_by_group(manager
->db
, group
))
1432 scmdatabase_unlock(manager
->db
);
1433 return ERROR_SERVICE_DOES_NOT_EXIST
;
1436 total_size
= num_services
= 0;
1437 LIST_FOR_EACH_ENTRY(service
, &manager
->db
->services
, struct service_entry
, entry
)
1439 if ((service
->status
.dwServiceType
& type
) && map_state(service
->status
.dwCurrentState
, state
)
1440 && match_group(service
->config
.lpLoadOrderGroup
, group
))
1442 total_size
+= sizeof(ENUM_SERVICE_STATUS_PROCESSW
);
1443 total_size
+= (strlenW(service
->name
) + 1) * sizeof(WCHAR
);
1444 if (service
->config
.lpDisplayName
)
1446 total_size
+= (strlenW(service
->config
.lpDisplayName
) + 1) * sizeof(WCHAR
);
1452 *needed
= total_size
;
1453 if (total_size
> size
)
1455 scmdatabase_unlock(manager
->db
);
1456 return ERROR_MORE_DATA
;
1458 s
= (ENUM_SERVICE_STATUS_PROCESSW
*)buffer
;
1459 offset
= num_services
* sizeof(ENUM_SERVICE_STATUS_PROCESSW
);
1460 LIST_FOR_EACH_ENTRY(service
, &manager
->db
->services
, struct service_entry
, entry
)
1462 if ((service
->status
.dwServiceType
& type
) && map_state(service
->status
.dwCurrentState
, state
)
1463 && match_group(service
->config
.lpLoadOrderGroup
, group
))
1465 sz
= (strlenW(service
->name
) + 1) * sizeof(WCHAR
);
1466 memcpy(buffer
+ offset
, service
->name
, sz
);
1467 s
->lpServiceName
= (WCHAR
*)offset
; /* store a buffer offset instead of a pointer */
1470 if (!service
->config
.lpDisplayName
) s
->lpDisplayName
= NULL
;
1473 sz
= (strlenW(service
->config
.lpDisplayName
) + 1) * sizeof(WCHAR
);
1474 memcpy(buffer
+ offset
, service
->config
.lpDisplayName
, sz
);
1475 s
->lpDisplayName
= (WCHAR
*)offset
;
1478 s
->ServiceStatusProcess
= service
->status
;
1482 *returned
= num_services
;
1484 scmdatabase_unlock(manager
->db
);
1485 return ERROR_SUCCESS
;
1488 DWORD __cdecl
svcctl_unknown43(void)
1491 return ERROR_CALL_NOT_IMPLEMENTED
;
1494 DWORD __cdecl
svcctl_CreateServiceWOW64A(
1495 SC_RPC_HANDLE scmanager
,
1501 DWORD error_control
,
1503 LPCSTR loadordergroup
,
1505 const BYTE
*dependencies
,
1508 const BYTE
*password
,
1509 DWORD password_size
,
1510 SC_RPC_HANDLE
*service
)
1513 return ERROR_CALL_NOT_IMPLEMENTED
;
1516 DWORD __cdecl
svcctl_CreateServiceWOW64W(
1517 SC_RPC_HANDLE scmanager
,
1518 LPCWSTR servicename
,
1519 LPCWSTR displayname
,
1523 DWORD error_control
,
1525 LPCWSTR loadordergroup
,
1527 const BYTE
*dependencies
,
1530 const BYTE
*password
,
1531 DWORD password_size
,
1532 SC_RPC_HANDLE
*service
)
1534 WINE_TRACE("(%s, %s, 0x%x, %s)\n", wine_dbgstr_w(servicename
), wine_dbgstr_w(displayname
), accessmask
, wine_dbgstr_w(imagepath
));
1535 return create_serviceW(scmanager
, servicename
, displayname
, accessmask
, service_type
, start_type
, error_control
, imagepath
,
1536 loadordergroup
, tagid
, dependencies
, depend_size
, start_name
, password
, password_size
, service
, TRUE
);
1539 DWORD __cdecl
svcctl_unknown46(void)
1542 return ERROR_CALL_NOT_IMPLEMENTED
;
1545 DWORD __cdecl
svcctl_NotifyServiceStatusChange(
1546 SC_RPC_HANDLE service
,
1547 SC_RPC_NOTIFY_PARAMS params
,
1548 GUID
*clientprocessguid
,
1549 GUID
*scmprocessguid
,
1550 BOOL
*createremotequeue
,
1551 SC_NOTIFY_RPC_HANDLE
*notify
)
1554 return ERROR_CALL_NOT_IMPLEMENTED
;
1557 DWORD __cdecl
svcctl_GetNotifyResults(
1558 SC_NOTIFY_RPC_HANDLE notify
,
1559 SC_RPC_NOTIFY_PARAMS_LIST
**params
)
1562 return ERROR_CALL_NOT_IMPLEMENTED
;
1565 DWORD __cdecl
svcctl_CloseNotifyHandle(
1566 SC_NOTIFY_RPC_HANDLE
*notify
,
1570 return ERROR_CALL_NOT_IMPLEMENTED
;
1573 DWORD __cdecl
svcctl_ControlServiceExA(
1574 SC_RPC_HANDLE service
,
1577 SC_RPC_SERVICE_CONTROL_IN_PARAMSA
*in_params
,
1578 SC_RPC_SERVICE_CONTROL_OUT_PARAMSA
*out_params
)
1581 return ERROR_CALL_NOT_IMPLEMENTED
;
1584 DWORD __cdecl
svcctl_ControlServiceExW(
1585 SC_RPC_HANDLE service
,
1588 SC_RPC_SERVICE_CONTROL_IN_PARAMSW
*in_params
,
1589 SC_RPC_SERVICE_CONTROL_OUT_PARAMSW
*out_params
)
1592 return ERROR_CALL_NOT_IMPLEMENTED
;
1595 DWORD __cdecl
svcctl_unknown52(void)
1598 return ERROR_CALL_NOT_IMPLEMENTED
;
1601 DWORD __cdecl
svcctl_unknown53(void)
1604 return ERROR_CALL_NOT_IMPLEMENTED
;
1607 DWORD __cdecl
svcctl_unknown54(void)
1610 return ERROR_CALL_NOT_IMPLEMENTED
;
1613 DWORD __cdecl
svcctl_unknown55(void)
1616 return ERROR_CALL_NOT_IMPLEMENTED
;
1619 DWORD __cdecl
svcctl_QueryServiceConfigEx(
1620 SC_RPC_HANDLE service
,
1622 SC_RPC_CONFIG_INFOW
*info
)
1625 return ERROR_CALL_NOT_IMPLEMENTED
;
1628 DWORD __cdecl
svcctl_QueryServiceObjectSecurity(
1629 SC_RPC_HANDLE service
,
1630 SECURITY_INFORMATION info
,
1636 return ERROR_CALL_NOT_IMPLEMENTED
;
1639 DWORD __cdecl
svcctl_SetServiceObjectSecurity(
1640 SC_RPC_HANDLE service
,
1641 SECURITY_INFORMATION info
,
1646 return ERROR_CALL_NOT_IMPLEMENTED
;
1649 DWORD __cdecl
svcctl_QueryServiceStatus(
1650 SC_RPC_HANDLE service
,
1651 SERVICE_STATUS
*status
)
1654 return ERROR_CALL_NOT_IMPLEMENTED
;
1657 DWORD __cdecl
svcctl_NotifyBootConfigStatus(
1658 SVCCTL_HANDLEW machinename
,
1659 DWORD boot_acceptable
)
1662 return ERROR_CALL_NOT_IMPLEMENTED
;
1665 DWORD __cdecl
svcctl_SCSetServiceBitsW(void)
1668 return ERROR_CALL_NOT_IMPLEMENTED
;
1671 DWORD __cdecl
svcctl_EnumDependentServicesW(
1672 SC_RPC_HANDLE service
,
1677 DWORD
*services_ret
)
1680 return ERROR_CALL_NOT_IMPLEMENTED
;
1683 DWORD __cdecl
svcctl_QueryServiceLockStatusW(
1684 SC_RPC_HANDLE scmanager
,
1685 QUERY_SERVICE_LOCK_STATUSW
*status
,
1690 return ERROR_CALL_NOT_IMPLEMENTED
;
1693 DWORD __cdecl
svcctl_SCSetServiceBitsA(void)
1696 return ERROR_CALL_NOT_IMPLEMENTED
;
1699 DWORD __cdecl
svcctl_ChangeServiceConfigA(
1700 SC_RPC_HANDLE service
,
1703 DWORD error_control
,
1705 LPSTR loadordergroup
,
1711 DWORD password_size
,
1715 return ERROR_CALL_NOT_IMPLEMENTED
;
1718 DWORD __cdecl
svcctl_CreateServiceA(
1719 SC_RPC_HANDLE scmanager
,
1722 DWORD desiredaccess
,
1725 DWORD error_control
,
1727 LPCSTR loadordergroup
,
1729 const BYTE
*dependencies
,
1732 const BYTE
*password
,
1733 DWORD password_size
,
1734 SC_RPC_HANDLE
*service
)
1737 return ERROR_CALL_NOT_IMPLEMENTED
;
1740 DWORD __cdecl
svcctl_EnumDependentServicesA(
1741 SC_RPC_HANDLE service
,
1746 DWORD
*services_ret
)
1749 return ERROR_CALL_NOT_IMPLEMENTED
;
1752 DWORD __cdecl
svcctl_EnumServicesStatusA(
1753 SC_RPC_HANDLE hmngr
,
1763 return ERROR_CALL_NOT_IMPLEMENTED
;
1766 DWORD __cdecl
svcctl_OpenSCManagerA(
1767 MACHINE_HANDLEA MachineName
,
1768 LPCSTR DatabaseName
,
1770 SC_RPC_HANDLE
*handle
)
1773 return ERROR_CALL_NOT_IMPLEMENTED
;
1776 DWORD __cdecl
svcctl_OpenServiceA(
1777 SC_RPC_HANDLE hSCManager
,
1778 LPCSTR lpServiceName
,
1779 DWORD dwDesiredAccess
,
1780 SC_RPC_HANDLE
*phService
)
1783 return ERROR_CALL_NOT_IMPLEMENTED
;
1786 DWORD __cdecl
svcctl_QueryServiceConfigA(
1787 SC_RPC_HANDLE hService
,
1788 QUERY_SERVICE_CONFIGA
*config
,
1793 return ERROR_CALL_NOT_IMPLEMENTED
;
1796 DWORD __cdecl
svcctl_QueryServiceLockStatusA(
1797 SC_RPC_HANDLE scmanager
,
1798 QUERY_SERVICE_LOCK_STATUSA
*status
,
1803 return ERROR_CALL_NOT_IMPLEMENTED
;
1806 DWORD __cdecl
svcctl_StartServiceA(
1807 SC_RPC_HANDLE service
,
1812 return ERROR_CALL_NOT_IMPLEMENTED
;
1815 DWORD __cdecl
svcctl_GetServiceDisplayNameA(
1816 SC_RPC_HANDLE hSCManager
,
1822 return ERROR_CALL_NOT_IMPLEMENTED
;
1825 DWORD __cdecl
svcctl_GetServiceKeyNameA(
1826 SC_RPC_HANDLE hSCManager
,
1832 return ERROR_CALL_NOT_IMPLEMENTED
;
1835 DWORD __cdecl
svcctl_GetCurrentGroupStateW(void)
1838 return ERROR_CALL_NOT_IMPLEMENTED
;
1841 DWORD __cdecl
svcctl_EnumServiceGroupW(
1842 SC_RPC_HANDLE scmanager
,
1844 DWORD service_state
,
1848 DWORD
*returned_size
,
1849 DWORD
*resume_index
,
1853 return ERROR_CALL_NOT_IMPLEMENTED
;
1856 DWORD __cdecl
svcctl_ChangeServiceConfig2A(
1857 SC_RPC_HANDLE service
,
1858 SC_RPC_CONFIG_INFOA info
)
1861 return ERROR_CALL_NOT_IMPLEMENTED
;
1864 DWORD __cdecl
svcctl_QueryServiceConfig2A(
1865 SC_RPC_HANDLE service
,
1872 return ERROR_CALL_NOT_IMPLEMENTED
;
1875 DWORD
RPC_Init(void)
1877 WCHAR transport
[] = SVCCTL_TRANSPORT
;
1878 WCHAR endpoint
[] = SVCCTL_ENDPOINT
;
1881 if ((err
= RpcServerUseProtseqEpW(transport
, 0, endpoint
, NULL
)) != ERROR_SUCCESS
)
1883 WINE_ERR("RpcServerUseProtseq failed with error %u\n", err
);
1887 if ((err
= RpcServerRegisterIf(svcctl_v2_0_s_ifspec
, 0, 0)) != ERROR_SUCCESS
)
1889 WINE_ERR("RpcServerRegisterIf failed with error %u\n", err
);
1893 if ((err
= RpcServerListen(1, RPC_C_LISTEN_MAX_CALLS_DEFAULT
, TRUE
)) != ERROR_SUCCESS
)
1895 WINE_ERR("RpcServerListen failed with error %u\n", err
);
1898 return ERROR_SUCCESS
;
1901 DWORD
events_loop(void)
1903 struct timeout_queue_elem
*iter
, *iter_safe
;
1905 HANDLE wait_handles
[MAXIMUM_WAIT_OBJECTS
];
1906 DWORD timeout
= INFINITE
;
1908 wait_handles
[0] = __wine_make_process_system();
1909 wait_handles
[1] = CreateEventW(NULL
, FALSE
, FALSE
, NULL
);
1910 timeout_queue_event
= wait_handles
[1];
1912 SetEvent(g_hStartedEvent
);
1914 WINE_TRACE("Entered main loop\n");
1918 DWORD num_handles
= 2;
1920 /* monitor tracked process handles for process end */
1921 EnterCriticalSection(&timeout_queue_cs
);
1922 LIST_FOR_EACH_ENTRY(iter
, &timeout_queue
, struct timeout_queue_elem
, entry
)
1924 if(num_handles
== MAXIMUM_WAIT_OBJECTS
){
1925 WINE_TRACE("Exceeded maximum wait object count\n");
1928 wait_handles
[num_handles
] = iter
->service_entry
->process
->process
;
1931 LeaveCriticalSection(&timeout_queue_cs
);
1933 err
= WaitForMultipleObjects(num_handles
, wait_handles
, FALSE
, timeout
);
1934 WINE_TRACE("Wait returned %d\n", err
);
1936 if(err
> WAIT_OBJECT_0
|| err
== WAIT_TIMEOUT
)
1939 ULARGE_INTEGER time
;
1942 GetSystemTimeAsFileTime(&cur_time
);
1943 time
.u
.LowPart
= cur_time
.dwLowDateTime
;
1944 time
.u
.HighPart
= cur_time
.dwHighDateTime
;
1946 EnterCriticalSection(&timeout_queue_cs
);
1948 LIST_FOR_EACH_ENTRY_SAFE(iter
, iter_safe
, &timeout_queue
, struct timeout_queue_elem
, entry
)
1950 if(CompareFileTime(&cur_time
, &iter
->time
) >= 0 ||
1951 (err
> WAIT_OBJECT_0
+ 1 && idx
== err
- WAIT_OBJECT_0
- 2))
1953 LeaveCriticalSection(&timeout_queue_cs
);
1954 iter
->func(iter
->service_entry
);
1955 EnterCriticalSection(&timeout_queue_cs
);
1957 release_service(iter
->service_entry
);
1958 list_remove(&iter
->entry
);
1959 HeapFree(GetProcessHeap(), 0, iter
);
1963 ULARGE_INTEGER time_diff
;
1965 time_diff
.u
.LowPart
= iter
->time
.dwLowDateTime
;
1966 time_diff
.u
.HighPart
= iter
->time
.dwHighDateTime
;
1967 time_diff
.QuadPart
= (time_diff
.QuadPart
-time
.QuadPart
)/10000;
1969 if(time_diff
.QuadPart
< timeout
)
1970 timeout
= time_diff
.QuadPart
;
1974 LeaveCriticalSection(&timeout_queue_cs
);
1976 if(timeout
!= INFINITE
)
1979 } while (err
!= WAIT_OBJECT_0
);
1981 WINE_TRACE("Object signaled - wine shutdown\n");
1982 EnterCriticalSection(&timeout_queue_cs
);
1983 LIST_FOR_EACH_ENTRY_SAFE(iter
, iter_safe
, &timeout_queue
, struct timeout_queue_elem
, entry
)
1985 LeaveCriticalSection(&timeout_queue_cs
);
1986 iter
->func(iter
->service_entry
);
1987 EnterCriticalSection(&timeout_queue_cs
);
1989 release_service(iter
->service_entry
);
1990 list_remove(&iter
->entry
);
1991 HeapFree(GetProcessHeap(), 0, iter
);
1993 LeaveCriticalSection(&timeout_queue_cs
);
1995 CloseHandle(wait_handles
[0]);
1996 CloseHandle(wait_handles
[1]);
1997 return ERROR_SUCCESS
;
2000 void __RPC_USER
SC_RPC_HANDLE_rundown(SC_RPC_HANDLE handle
)
2002 SC_RPC_HANDLE_destroy(handle
);
2005 void __RPC_USER
SC_NOTIFY_RPC_HANDLE_rundown(SC_NOTIFY_RPC_HANDLE handle
)
2009 void __RPC_FAR
* __RPC_USER
MIDL_user_allocate(SIZE_T len
)
2011 return HeapAlloc(GetProcessHeap(), 0, len
);
2014 void __RPC_USER
MIDL_user_free(void __RPC_FAR
* ptr
)
2016 HeapFree(GetProcessHeap(), 0, ptr
);