2 * Services.exe - RPC functions
4 * Copyright 2007 Google (Mikolaj Zalewski)
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #define WIN32_LEAN_AND_MEAN
30 #include "wine/list.h"
31 #include "wine/unicode.h"
32 #include "wine/debug.h"
37 extern HANDLE CDECL
__wine_make_process_system(void);
39 WINE_DEFAULT_DEBUG_CHANNEL(service
);
41 static const GENERIC_MAPPING g_scm_generic
=
43 (STANDARD_RIGHTS_READ
| SC_MANAGER_ENUMERATE_SERVICE
| SC_MANAGER_QUERY_LOCK_STATUS
),
44 (STANDARD_RIGHTS_WRITE
| SC_MANAGER_CREATE_SERVICE
| SC_MANAGER_MODIFY_BOOT_CONFIG
),
45 (STANDARD_RIGHTS_EXECUTE
| SC_MANAGER_CONNECT
| SC_MANAGER_LOCK
),
49 static const GENERIC_MAPPING g_svc_generic
=
51 (STANDARD_RIGHTS_READ
| SERVICE_QUERY_CONFIG
| SERVICE_QUERY_STATUS
| SERVICE_INTERROGATE
| SERVICE_ENUMERATE_DEPENDENTS
),
52 (STANDARD_RIGHTS_WRITE
| SERVICE_CHANGE_CONFIG
),
53 (STANDARD_RIGHTS_EXECUTE
| SERVICE_START
| SERVICE_STOP
| SERVICE_PAUSE_CONTINUE
| SERVICE_USER_DEFINED_CONTROL
),
59 SC_HTYPE_DONT_CARE
= 0,
70 struct sc_manager_handle
/* service control manager handle */
73 struct scmdatabase
*db
;
76 struct sc_service_handle
/* service handle */
79 struct service_entry
*service_entry
;
84 struct scmdatabase
*db
;
87 /* Check if the given handle is of the required type and allows the requested access. */
88 static DWORD
validate_context_handle(SC_RPC_HANDLE handle
, DWORD type
, DWORD needed_access
, struct sc_handle
**out_hdr
)
90 struct sc_handle
*hdr
= handle
;
92 if (type
!= SC_HTYPE_DONT_CARE
&& hdr
->type
!= type
)
94 WINE_ERR("Handle is of an invalid type (%d, %d)\n", hdr
->type
, type
);
95 return ERROR_INVALID_HANDLE
;
98 if ((needed_access
& hdr
->access
) != needed_access
)
100 WINE_ERR("Access denied - handle created with access %x, needed %x\n", hdr
->access
, needed_access
);
101 return ERROR_ACCESS_DENIED
;
105 return ERROR_SUCCESS
;
108 static DWORD
validate_scm_handle(SC_RPC_HANDLE handle
, DWORD needed_access
, struct sc_manager_handle
**manager
)
110 struct sc_handle
*hdr
;
111 DWORD err
= validate_context_handle(handle
, SC_HTYPE_MANAGER
, needed_access
, &hdr
);
112 if (err
== ERROR_SUCCESS
)
113 *manager
= (struct sc_manager_handle
*)hdr
;
117 static DWORD
validate_service_handle(SC_RPC_HANDLE handle
, DWORD needed_access
, struct sc_service_handle
**service
)
119 struct sc_handle
*hdr
;
120 DWORD err
= validate_context_handle(handle
, SC_HTYPE_SERVICE
, needed_access
, &hdr
);
121 if (err
== ERROR_SUCCESS
)
122 *service
= (struct sc_service_handle
*)hdr
;
126 DWORD
svcctl_OpenSCManagerW(
127 MACHINE_HANDLEW MachineName
, /* Note: this parameter is ignored */
128 LPCWSTR DatabaseName
,
130 SC_RPC_HANDLE
*handle
)
132 struct sc_manager_handle
*manager
;
134 WINE_TRACE("(%s, %s, %x)\n", wine_dbgstr_w(MachineName
), wine_dbgstr_w(DatabaseName
), dwAccessMask
);
136 if (DatabaseName
!= NULL
&& DatabaseName
[0])
138 if (strcmpW(DatabaseName
, SERVICES_FAILED_DATABASEW
) == 0)
139 return ERROR_DATABASE_DOES_NOT_EXIST
;
140 if (strcmpW(DatabaseName
, SERVICES_ACTIVE_DATABASEW
) != 0)
141 return ERROR_INVALID_NAME
;
144 if (!(manager
= HeapAlloc(GetProcessHeap(), 0, sizeof(*manager
))))
145 return ERROR_NOT_ENOUGH_SERVER_MEMORY
;
147 manager
->hdr
.type
= SC_HTYPE_MANAGER
;
149 if (dwAccessMask
& MAXIMUM_ALLOWED
)
150 dwAccessMask
|= SC_MANAGER_ALL_ACCESS
;
151 manager
->hdr
.access
= dwAccessMask
;
152 RtlMapGenericMask(&manager
->hdr
.access
, &g_scm_generic
);
153 manager
->db
= active_database
;
154 *handle
= &manager
->hdr
;
156 return ERROR_SUCCESS
;
159 static void SC_RPC_HANDLE_destroy(SC_RPC_HANDLE handle
)
161 struct sc_handle
*hdr
= handle
;
164 case SC_HTYPE_MANAGER
:
166 struct sc_manager_handle
*manager
= (struct sc_manager_handle
*)hdr
;
167 HeapFree(GetProcessHeap(), 0, manager
);
170 case SC_HTYPE_SERVICE
:
172 struct sc_service_handle
*service
= (struct sc_service_handle
*)hdr
;
173 release_service(service
->service_entry
);
174 HeapFree(GetProcessHeap(), 0, service
);
178 WINE_ERR("invalid handle type %d\n", hdr
->type
);
179 RpcRaiseException(ERROR_INVALID_HANDLE
);
183 DWORD
svcctl_GetServiceDisplayNameW(
184 SC_RPC_HANDLE hSCManager
,
185 LPCWSTR lpServiceName
,
189 struct sc_manager_handle
*manager
;
190 struct service_entry
*entry
;
193 WINE_TRACE("(%s, %d)\n", wine_dbgstr_w(lpServiceName
), *cchBufSize
);
195 if ((err
= validate_scm_handle(hSCManager
, 0, &manager
)) != ERROR_SUCCESS
)
198 scmdatabase_lock_shared(manager
->db
);
200 entry
= scmdatabase_find_service(manager
->db
, lpServiceName
);
205 service_lock_shared(entry
);
206 name
= get_display_name(entry
);
208 if (len
<= *cchBufSize
)
211 memcpy(lpBuffer
, name
, (len
+ 1)*sizeof(*name
));
214 err
= ERROR_INSUFFICIENT_BUFFER
;
216 service_unlock(entry
);
219 err
= ERROR_SERVICE_DOES_NOT_EXIST
;
221 scmdatabase_unlock(manager
->db
);
223 if (err
!= ERROR_SUCCESS
)
229 DWORD
svcctl_GetServiceKeyNameW(
230 SC_RPC_HANDLE hSCManager
,
231 LPCWSTR lpServiceDisplayName
,
235 struct service_entry
*entry
;
236 struct sc_manager_handle
*manager
;
239 WINE_TRACE("(%s, %d)\n", wine_dbgstr_w(lpServiceDisplayName
), *cchBufSize
);
241 if ((err
= validate_scm_handle(hSCManager
, 0, &manager
)) != ERROR_SUCCESS
)
244 scmdatabase_lock_shared(manager
->db
);
246 entry
= scmdatabase_find_service_by_displayname(manager
->db
, lpServiceDisplayName
);
250 service_lock_shared(entry
);
251 len
= strlenW(entry
->name
);
252 if (len
<= *cchBufSize
)
255 memcpy(lpBuffer
, entry
->name
, (len
+ 1)*sizeof(*entry
->name
));
258 err
= ERROR_INSUFFICIENT_BUFFER
;
260 service_unlock(entry
);
263 err
= ERROR_SERVICE_DOES_NOT_EXIST
;
265 scmdatabase_unlock(manager
->db
);
267 if (err
!= ERROR_SUCCESS
)
273 static DWORD
create_handle_for_service(struct service_entry
*entry
, DWORD dwDesiredAccess
, SC_RPC_HANDLE
*phService
)
275 struct sc_service_handle
*service
;
277 if (!(service
= HeapAlloc(GetProcessHeap(), 0, sizeof(*service
))))
279 release_service(entry
);
280 return ERROR_NOT_ENOUGH_SERVER_MEMORY
;
283 service
->hdr
.type
= SC_HTYPE_SERVICE
;
284 service
->hdr
.access
= dwDesiredAccess
;
285 RtlMapGenericMask(&service
->hdr
.access
, &g_svc_generic
);
286 service
->service_entry
= entry
;
287 if (dwDesiredAccess
& MAXIMUM_ALLOWED
)
288 dwDesiredAccess
|= SERVICE_ALL_ACCESS
;
290 *phService
= &service
->hdr
;
291 return ERROR_SUCCESS
;
294 DWORD
svcctl_OpenServiceW(
295 SC_RPC_HANDLE hSCManager
,
296 LPCWSTR lpServiceName
,
297 DWORD dwDesiredAccess
,
298 SC_RPC_HANDLE
*phService
)
300 struct sc_manager_handle
*manager
;
301 struct service_entry
*entry
;
304 WINE_TRACE("(%s, 0x%x)\n", wine_dbgstr_w(lpServiceName
), dwDesiredAccess
);
306 if ((err
= validate_scm_handle(hSCManager
, 0, &manager
)) != ERROR_SUCCESS
)
308 if (!validate_service_name(lpServiceName
))
309 return ERROR_INVALID_NAME
;
311 scmdatabase_lock_shared(manager
->db
);
312 entry
= scmdatabase_find_service(manager
->db
, lpServiceName
);
314 InterlockedIncrement(&entry
->ref_count
);
315 scmdatabase_unlock(manager
->db
);
318 return ERROR_SERVICE_DOES_NOT_EXIST
;
320 return create_handle_for_service(entry
, dwDesiredAccess
, phService
);
323 DWORD
svcctl_CreateServiceW(
324 SC_RPC_HANDLE hSCManager
,
325 LPCWSTR lpServiceName
,
326 LPCWSTR lpDisplayName
,
327 DWORD dwDesiredAccess
,
330 DWORD dwErrorControl
,
331 LPCWSTR lpBinaryPathName
,
332 LPCWSTR lpLoadOrderGroup
,
334 const BYTE
*lpDependencies
,
335 DWORD dwDependenciesSize
,
336 LPCWSTR lpServiceStartName
,
337 const BYTE
*lpPassword
,
338 DWORD dwPasswordSize
,
339 SC_RPC_HANDLE
*phService
)
341 struct sc_manager_handle
*manager
;
342 struct service_entry
*entry
;
345 WINE_TRACE("(%s, %s, 0x%x, %s)\n", wine_dbgstr_w(lpServiceName
), wine_dbgstr_w(lpDisplayName
), dwDesiredAccess
, wine_dbgstr_w(lpBinaryPathName
));
347 if ((err
= validate_scm_handle(hSCManager
, SC_MANAGER_CREATE_SERVICE
, &manager
)) != ERROR_SUCCESS
)
350 if (!validate_service_name(lpServiceName
))
351 return ERROR_INVALID_NAME
;
352 if (!check_multisz((LPCWSTR
)lpDependencies
, dwDependenciesSize
) || !lpServiceName
[0] || !lpBinaryPathName
[0])
353 return ERROR_INVALID_PARAMETER
;
356 WINE_FIXME("Don't know how to add a password\n"); /* I always get ERROR_GEN_FAILURE */
358 WINE_FIXME("Dependencies not supported yet\n");
360 err
= service_create(lpServiceName
, &entry
);
361 if (err
!= ERROR_SUCCESS
)
363 entry
->ref_count
= 1;
364 entry
->config
.dwServiceType
= entry
->status
.dwServiceType
= dwServiceType
;
365 entry
->config
.dwStartType
= dwStartType
;
366 entry
->config
.dwErrorControl
= dwErrorControl
;
367 entry
->config
.lpBinaryPathName
= strdupW(lpBinaryPathName
);
368 entry
->config
.lpLoadOrderGroup
= strdupW(lpLoadOrderGroup
);
369 entry
->config
.lpServiceStartName
= strdupW(lpServiceStartName
);
370 entry
->config
.lpDisplayName
= strdupW(lpDisplayName
);
372 if (lpdwTagId
) /* TODO: In most situations a non-NULL TagId will generate an ERROR_INVALID_PARAMETER. */
373 entry
->config
.dwTagId
= *lpdwTagId
;
375 entry
->config
.dwTagId
= 0;
377 /* other fields NULL*/
379 if (!validate_service_config(entry
))
381 WINE_ERR("Invalid data while trying to create service\n");
382 free_service_entry(entry
);
383 return ERROR_INVALID_PARAMETER
;
386 scmdatabase_lock_exclusive(manager
->db
);
388 if (scmdatabase_find_service(manager
->db
, lpServiceName
))
390 scmdatabase_unlock(manager
->db
);
391 free_service_entry(entry
);
392 return ERROR_SERVICE_EXISTS
;
395 if (scmdatabase_find_service_by_displayname(manager
->db
, get_display_name(entry
)))
397 scmdatabase_unlock(manager
->db
);
398 free_service_entry(entry
);
399 return ERROR_DUPLICATE_SERVICE_NAME
;
402 err
= scmdatabase_add_service(manager
->db
, entry
);
403 if (err
!= ERROR_SUCCESS
)
405 scmdatabase_unlock(manager
->db
);
406 free_service_entry(entry
);
409 scmdatabase_unlock(manager
->db
);
411 return create_handle_for_service(entry
, dwDesiredAccess
, phService
);
414 DWORD
svcctl_DeleteService(
415 SC_RPC_HANDLE hService
)
417 struct sc_service_handle
*service
;
420 if ((err
= validate_service_handle(hService
, DELETE
, &service
)) != ERROR_SUCCESS
)
423 scmdatabase_lock_exclusive(service
->service_entry
->db
);
424 service_lock_exclusive(service
->service_entry
);
426 if (!is_marked_for_delete(service
->service_entry
))
427 err
= scmdatabase_remove_service(service
->service_entry
->db
, service
->service_entry
);
429 err
= ERROR_SERVICE_MARKED_FOR_DELETE
;
431 service_unlock(service
->service_entry
);
432 scmdatabase_unlock(service
->service_entry
->db
);
437 DWORD
svcctl_QueryServiceConfigW(
438 SC_RPC_HANDLE hService
,
439 QUERY_SERVICE_CONFIGW
*config
)
441 struct sc_service_handle
*service
;
444 WINE_TRACE("(%p)\n", config
);
446 if ((err
= validate_service_handle(hService
, SERVICE_QUERY_CONFIG
, &service
)) != 0)
449 service_lock_shared(service
->service_entry
);
450 config
->dwServiceType
= service
->service_entry
->config
.dwServiceType
;
451 config
->dwStartType
= service
->service_entry
->config
.dwStartType
;
452 config
->dwErrorControl
= service
->service_entry
->config
.dwErrorControl
;
453 config
->lpBinaryPathName
= strdupW(service
->service_entry
->config
.lpBinaryPathName
);
454 config
->lpLoadOrderGroup
= strdupW(service
->service_entry
->config
.lpLoadOrderGroup
);
455 config
->dwTagId
= service
->service_entry
->config
.dwTagId
;
456 config
->lpDependencies
= NULL
; /* TODO */
457 config
->lpServiceStartName
= strdupW(service
->service_entry
->config
.lpServiceStartName
);
458 config
->lpDisplayName
= strdupW(service
->service_entry
->config
.lpDisplayName
);
459 service_unlock(service
->service_entry
);
461 return ERROR_SUCCESS
;
464 DWORD
svcctl_ChangeServiceConfigW(
465 SC_RPC_HANDLE hService
,
468 DWORD dwErrorControl
,
469 LPCWSTR lpBinaryPathName
,
470 LPCWSTR lpLoadOrderGroup
,
472 const BYTE
*lpDependencies
,
473 DWORD dwDependenciesSize
,
474 LPCWSTR lpServiceStartName
,
475 const BYTE
*lpPassword
,
476 DWORD dwPasswordSize
,
477 LPCWSTR lpDisplayName
)
479 struct service_entry new_entry
, *entry
;
480 struct sc_service_handle
*service
;
485 if ((err
= validate_service_handle(hService
, SERVICE_CHANGE_CONFIG
, &service
)) != 0)
488 if (!check_multisz((LPCWSTR
)lpDependencies
, dwDependenciesSize
))
489 return ERROR_INVALID_PARAMETER
;
491 /* first check if the new configuration is correct */
492 service_lock_exclusive(service
->service_entry
);
494 if (is_marked_for_delete(service
->service_entry
))
496 service_unlock(service
->service_entry
);
497 return ERROR_SERVICE_MARKED_FOR_DELETE
;
500 if (lpDisplayName
!= NULL
&&
501 (entry
= scmdatabase_find_service_by_displayname(service
->service_entry
->db
, lpDisplayName
)) &&
502 (entry
!= service
->service_entry
))
504 service_unlock(service
->service_entry
);
505 return ERROR_DUPLICATE_SERVICE_NAME
;
508 new_entry
= *service
->service_entry
;
510 if (dwServiceType
!= SERVICE_NO_CHANGE
)
511 new_entry
.config
.dwServiceType
= dwServiceType
;
513 if (dwStartType
!= SERVICE_NO_CHANGE
)
514 new_entry
.config
.dwStartType
= dwStartType
;
516 if (dwErrorControl
!= SERVICE_NO_CHANGE
)
517 new_entry
.config
.dwErrorControl
= dwErrorControl
;
519 if (lpBinaryPathName
!= NULL
)
520 new_entry
.config
.lpBinaryPathName
= (LPWSTR
)lpBinaryPathName
;
522 if (lpLoadOrderGroup
!= NULL
)
523 new_entry
.config
.lpLoadOrderGroup
= (LPWSTR
)lpLoadOrderGroup
;
525 if (lpdwTagId
!= NULL
)
526 WINE_FIXME("Changing tag id not supported\n");
528 if (lpDependencies
!= NULL
)
529 WINE_FIXME("Changing dependencies not supported\n");
531 if (lpServiceStartName
!= NULL
)
532 new_entry
.config
.lpServiceStartName
= (LPWSTR
)lpServiceStartName
;
534 if (lpPassword
!= NULL
)
535 WINE_FIXME("Setting password not supported\n");
537 if (lpDisplayName
!= NULL
)
538 new_entry
.config
.lpDisplayName
= (LPWSTR
)lpDisplayName
;
540 if (!validate_service_config(&new_entry
))
542 WINE_ERR("The configuration after the change wouldn't be valid\n");
543 service_unlock(service
->service_entry
);
544 return ERROR_INVALID_PARAMETER
;
547 /* configuration OK. The strings needs to be duplicated */
548 if (lpBinaryPathName
!= NULL
)
550 HeapFree(GetProcessHeap(), 0, service
->service_entry
->config
.lpBinaryPathName
);
551 new_entry
.config
.lpBinaryPathName
= strdupW(lpBinaryPathName
);
554 if (lpLoadOrderGroup
!= NULL
)
556 HeapFree(GetProcessHeap(), 0, service
->service_entry
->config
.lpLoadOrderGroup
);
557 new_entry
.config
.lpLoadOrderGroup
= strdupW(lpLoadOrderGroup
);
560 if (lpServiceStartName
!= NULL
)
562 HeapFree(GetProcessHeap(), 0, service
->service_entry
->config
.lpServiceStartName
);
563 new_entry
.config
.lpServiceStartName
= strdupW(lpServiceStartName
);
566 if (lpDisplayName
!= NULL
)
568 HeapFree(GetProcessHeap(), 0, service
->service_entry
->config
.lpDisplayName
);
569 new_entry
.config
.lpDisplayName
= strdupW(lpDisplayName
);
572 *service
->service_entry
= new_entry
;
573 save_service_config(service
->service_entry
);
574 service_unlock(service
->service_entry
);
576 return ERROR_SUCCESS
;
579 DWORD
svcctl_SetServiceStatus(
580 SC_RPC_HANDLE hServiceStatus
,
581 LPSERVICE_STATUS lpServiceStatus
)
583 struct sc_service_handle
*service
;
586 WINE_TRACE("(%p, %p)\n", hServiceStatus
, lpServiceStatus
);
588 if ((err
= validate_service_handle(hServiceStatus
, SERVICE_SET_STATUS
, &service
)) != 0)
591 service_lock_exclusive(service
->service_entry
);
592 /* FIXME: be a bit more discriminant about what parts of the status we set
593 * and check that fields are valid */
594 service
->service_entry
->status
.dwServiceType
= lpServiceStatus
->dwServiceType
;
595 service
->service_entry
->status
.dwCurrentState
= lpServiceStatus
->dwCurrentState
;
596 service
->service_entry
->status
.dwControlsAccepted
= lpServiceStatus
->dwControlsAccepted
;
597 service
->service_entry
->status
.dwWin32ExitCode
= lpServiceStatus
->dwWin32ExitCode
;
598 service
->service_entry
->status
.dwServiceSpecificExitCode
= lpServiceStatus
->dwServiceSpecificExitCode
;
599 service
->service_entry
->status
.dwCheckPoint
= lpServiceStatus
->dwCheckPoint
;
600 service
->service_entry
->status
.dwWaitHint
= lpServiceStatus
->dwWaitHint
;
601 service_unlock(service
->service_entry
);
603 if (service
->service_entry
->status_changed_event
)
604 SetEvent(service
->service_entry
->status_changed_event
);
606 return ERROR_SUCCESS
;
609 DWORD
svcctl_ChangeServiceConfig2W( SC_RPC_HANDLE hService
, DWORD level
, SERVICE_CONFIG2W
*config
)
611 struct sc_service_handle
*service
;
614 if ((err
= validate_service_handle(hService
, SERVICE_CHANGE_CONFIG
, &service
)) != 0)
619 case SERVICE_CONFIG_DESCRIPTION
:
623 if (config
->descr
.lpDescription
[0])
625 if (!(descr
= strdupW( config
->descr
.lpDescription
)))
626 return ERROR_NOT_ENOUGH_MEMORY
;
629 WINE_TRACE( "changing service %p descr to %s\n", service
, wine_dbgstr_w(descr
) );
630 service_lock_exclusive( service
->service_entry
);
631 HeapFree( GetProcessHeap(), 0, service
->service_entry
->description
);
632 service
->service_entry
->description
= descr
;
633 save_service_config( service
->service_entry
);
634 service_unlock( service
->service_entry
);
637 case SERVICE_CONFIG_FAILURE_ACTIONS
:
638 WINE_FIXME( "SERVICE_CONFIG_FAILURE_ACTIONS not implemented: period %u msg %s cmd %s\n",
639 config
->actions
.dwResetPeriod
,
640 wine_dbgstr_w(config
->actions
.lpRebootMsg
),
641 wine_dbgstr_w(config
->actions
.lpCommand
) );
644 WINE_FIXME("level %u not implemented\n", level
);
645 err
= ERROR_INVALID_LEVEL
;
651 DWORD
svcctl_QueryServiceConfig2W( SC_RPC_HANDLE hService
, DWORD level
,
652 BYTE
*buffer
, DWORD size
, LPDWORD needed
)
654 struct sc_service_handle
*service
;
657 memset(buffer
, 0, size
);
659 if ((err
= validate_service_handle(hService
, SERVICE_QUERY_STATUS
, &service
)) != 0)
664 case SERVICE_CONFIG_DESCRIPTION
:
666 SERVICE_DESCRIPTIONW
*descr
= (SERVICE_DESCRIPTIONW
*)buffer
;
668 service_lock_shared(service
->service_entry
);
669 *needed
= sizeof(*descr
);
670 if (service
->service_entry
->description
)
671 *needed
+= (strlenW(service
->service_entry
->description
) + 1) * sizeof(WCHAR
);
674 if (service
->service_entry
->description
)
676 /* store a buffer offset instead of a pointer */
677 descr
->lpDescription
= (WCHAR
*)((BYTE
*)(descr
+ 1) - buffer
);
678 strcpyW( (WCHAR
*)(descr
+ 1), service
->service_entry
->description
);
680 else descr
->lpDescription
= NULL
;
682 else err
= ERROR_INSUFFICIENT_BUFFER
;
683 service_unlock(service
->service_entry
);
688 WINE_FIXME("level %u not implemented\n", level
);
689 err
= ERROR_INVALID_LEVEL
;
695 DWORD
svcctl_QueryServiceStatusEx(
696 SC_RPC_HANDLE hService
,
697 SC_STATUS_TYPE InfoLevel
,
700 LPDWORD pcbBytesNeeded
)
702 struct sc_service_handle
*service
;
704 LPSERVICE_STATUS_PROCESS pSvcStatusData
;
706 memset(lpBuffer
, 0, cbBufSize
);
708 if ((err
= validate_service_handle(hService
, SERVICE_QUERY_STATUS
, &service
)) != 0)
711 if (InfoLevel
!= SC_STATUS_PROCESS_INFO
)
712 return ERROR_INVALID_LEVEL
;
714 pSvcStatusData
= (LPSERVICE_STATUS_PROCESS
) lpBuffer
;
715 if (pSvcStatusData
== NULL
)
716 return ERROR_INVALID_PARAMETER
;
718 if (cbBufSize
< sizeof(SERVICE_STATUS_PROCESS
))
720 if( pcbBytesNeeded
!= NULL
)
721 *pcbBytesNeeded
= sizeof(SERVICE_STATUS_PROCESS
);
723 return ERROR_INSUFFICIENT_BUFFER
;
726 service_lock_shared(service
->service_entry
);
728 pSvcStatusData
->dwServiceType
= service
->service_entry
->status
.dwServiceType
;
729 pSvcStatusData
->dwCurrentState
= service
->service_entry
->status
.dwCurrentState
;
730 pSvcStatusData
->dwControlsAccepted
= service
->service_entry
->status
.dwControlsAccepted
;
731 pSvcStatusData
->dwWin32ExitCode
= service
->service_entry
->status
.dwWin32ExitCode
;
732 pSvcStatusData
->dwServiceSpecificExitCode
= service
->service_entry
->status
.dwServiceSpecificExitCode
;
733 pSvcStatusData
->dwCheckPoint
= service
->service_entry
->status
.dwCheckPoint
;
734 pSvcStatusData
->dwWaitHint
= service
->service_entry
->status
.dwWaitHint
;
735 pSvcStatusData
->dwProcessId
= service
->service_entry
->status
.dwProcessId
;
736 pSvcStatusData
->dwServiceFlags
= service
->service_entry
->status
.dwServiceFlags
;
738 service_unlock(service
->service_entry
);
740 return ERROR_SUCCESS
;
743 /******************************************************************************
744 * service_accepts_control
746 static BOOL
service_accepts_control(const struct service_entry
*service
, DWORD dwControl
)
748 DWORD a
= service
->status
.dwControlsAccepted
;
752 case SERVICE_CONTROL_INTERROGATE
:
754 case SERVICE_CONTROL_STOP
:
755 if (a
&SERVICE_ACCEPT_STOP
)
758 case SERVICE_CONTROL_SHUTDOWN
:
759 if (a
&SERVICE_ACCEPT_SHUTDOWN
)
762 case SERVICE_CONTROL_PAUSE
:
763 case SERVICE_CONTROL_CONTINUE
:
764 if (a
&SERVICE_ACCEPT_PAUSE_CONTINUE
)
767 case SERVICE_CONTROL_PARAMCHANGE
:
768 if (a
&SERVICE_ACCEPT_PARAMCHANGE
)
771 case SERVICE_CONTROL_NETBINDADD
:
772 case SERVICE_CONTROL_NETBINDREMOVE
:
773 case SERVICE_CONTROL_NETBINDENABLE
:
774 case SERVICE_CONTROL_NETBINDDISABLE
:
775 if (a
&SERVICE_ACCEPT_NETBINDCHANGE
)
777 case SERVICE_CONTROL_HARDWAREPROFILECHANGE
:
778 if (a
&SERVICE_ACCEPT_HARDWAREPROFILECHANGE
)
781 case SERVICE_CONTROL_POWEREVENT
:
782 if (a
&SERVICE_ACCEPT_POWEREVENT
)
785 case SERVICE_CONTROL_SESSIONCHANGE
:
786 if (a
&SERVICE_ACCEPT_SESSIONCHANGE
)
793 /******************************************************************************
794 * service_send_control
796 static BOOL
service_send_control(struct service_entry
*service
, HANDLE pipe
, DWORD dwControl
, DWORD
*result
)
798 service_start_info
*ssi
;
799 DWORD len
, count
= 0;
802 /* calculate how much space we need to send the startup info */
803 len
= strlenW(service
->name
) + 1;
805 ssi
= HeapAlloc(GetProcessHeap(),0,FIELD_OFFSET(service_start_info
, data
[len
]));
806 ssi
->cmd
= WINESERV_SENDCONTROL
;
807 ssi
->control
= dwControl
;
808 ssi
->total_size
= FIELD_OFFSET(service_start_info
, data
[len
]);
809 ssi
->name_size
= strlenW(service
->name
) + 1;
810 strcpyW( ssi
->data
, service
->name
);
812 r
= WriteFile(pipe
, ssi
, ssi
->total_size
, &count
, NULL
);
813 if (!r
|| count
!= ssi
->total_size
)
815 WINE_ERR("service protocol error - failed to write pipe!\n");
818 r
= ReadFile(pipe
, result
, sizeof *result
, &count
, NULL
);
819 if (!r
|| count
!= sizeof *result
)
820 WINE_ERR("service protocol error - failed to read pipe "
821 "r = %d count = %d!\n", r
, count
);
825 DWORD
svcctl_StartServiceW(
826 SC_RPC_HANDLE hService
,
827 DWORD dwNumServiceArgs
,
828 LPCWSTR
*lpServiceArgVectors
)
830 struct sc_service_handle
*service
;
833 WINE_TRACE("(%p, %d, %p)\n", hService
, dwNumServiceArgs
, lpServiceArgVectors
);
835 if ((err
= validate_service_handle(hService
, SERVICE_START
, &service
)) != 0)
838 err
= service_start(service
->service_entry
, dwNumServiceArgs
, lpServiceArgVectors
);
843 DWORD
svcctl_ControlService(
844 SC_RPC_HANDLE hService
,
846 SERVICE_STATUS
*lpServiceStatus
)
848 DWORD access_required
;
849 struct sc_service_handle
*service
;
852 HANDLE control_mutex
;
855 WINE_TRACE("(%p, %d, %p)\n", hService
, dwControl
, lpServiceStatus
);
859 case SERVICE_CONTROL_CONTINUE
:
860 case SERVICE_CONTROL_NETBINDADD
:
861 case SERVICE_CONTROL_NETBINDDISABLE
:
862 case SERVICE_CONTROL_NETBINDENABLE
:
863 case SERVICE_CONTROL_NETBINDREMOVE
:
864 case SERVICE_CONTROL_PARAMCHANGE
:
865 case SERVICE_CONTROL_PAUSE
:
866 access_required
= SERVICE_PAUSE_CONTINUE
;
868 case SERVICE_CONTROL_INTERROGATE
:
869 access_required
= SERVICE_INTERROGATE
;
871 case SERVICE_CONTROL_STOP
:
872 access_required
= SERVICE_STOP
;
875 if (dwControl
>= 128 && dwControl
<= 255)
876 access_required
= SERVICE_USER_DEFINED_CONTROL
;
878 return ERROR_INVALID_PARAMETER
;
881 if ((err
= validate_service_handle(hService
, access_required
, &service
)) != 0)
884 service_lock_exclusive(service
->service_entry
);
888 lpServiceStatus
->dwServiceType
= service
->service_entry
->status
.dwServiceType
;
889 lpServiceStatus
->dwCurrentState
= service
->service_entry
->status
.dwCurrentState
;
890 lpServiceStatus
->dwControlsAccepted
= service
->service_entry
->status
.dwControlsAccepted
;
891 lpServiceStatus
->dwWin32ExitCode
= service
->service_entry
->status
.dwWin32ExitCode
;
892 lpServiceStatus
->dwServiceSpecificExitCode
= service
->service_entry
->status
.dwServiceSpecificExitCode
;
893 lpServiceStatus
->dwCheckPoint
= service
->service_entry
->status
.dwCheckPoint
;
894 lpServiceStatus
->dwWaitHint
= service
->service_entry
->status
.dwWaitHint
;
897 if (!service_accepts_control(service
->service_entry
, dwControl
))
899 service_unlock(service
->service_entry
);
900 return ERROR_INVALID_SERVICE_CONTROL
;
903 switch (service
->service_entry
->status
.dwCurrentState
)
905 case SERVICE_STOPPED
:
906 service_unlock(service
->service_entry
);
907 return ERROR_SERVICE_NOT_ACTIVE
;
908 case SERVICE_START_PENDING
:
909 if (dwControl
==SERVICE_CONTROL_STOP
)
912 case SERVICE_STOP_PENDING
:
913 service_unlock(service
->service_entry
);
914 return ERROR_SERVICE_CANNOT_ACCEPT_CTRL
;
917 /* prevent races by caching these variables and clearing them on
918 * stop here instead of outside the services lock */
919 control_mutex
= service
->service_entry
->control_mutex
;
920 control_pipe
= service
->service_entry
->control_pipe
;
921 if (dwControl
== SERVICE_CONTROL_STOP
)
923 service
->service_entry
->control_mutex
= NULL
;
924 service
->service_entry
->control_pipe
= INVALID_HANDLE_VALUE
;
927 service_unlock(service
->service_entry
);
929 ret
= WaitForSingleObject(control_mutex
, 30000);
930 if (ret
== WAIT_OBJECT_0
)
932 DWORD result
= ERROR_SUCCESS
;
934 ret
= service_send_control(service
->service_entry
, control_pipe
, dwControl
, &result
);
936 if (dwControl
== SERVICE_CONTROL_STOP
)
938 CloseHandle(control_mutex
);
939 CloseHandle(control_pipe
);
942 ReleaseMutex(control_mutex
);
948 if (dwControl
== SERVICE_CONTROL_STOP
)
950 CloseHandle(control_mutex
);
951 CloseHandle(control_pipe
);
953 return ERROR_SERVICE_REQUEST_TIMEOUT
;
957 DWORD
svcctl_CloseServiceHandle(
958 SC_RPC_HANDLE
*handle
)
960 WINE_TRACE("(&%p)\n", *handle
);
962 SC_RPC_HANDLE_destroy(*handle
);
965 return ERROR_SUCCESS
;
968 static void SC_RPC_LOCK_destroy(SC_RPC_LOCK hLock
)
970 struct sc_lock
*lock
= hLock
;
971 scmdatabase_unlock_startup(lock
->db
);
972 HeapFree(GetProcessHeap(), 0, lock
);
975 void __RPC_USER
SC_RPC_LOCK_rundown(SC_RPC_LOCK hLock
)
977 SC_RPC_LOCK_destroy(hLock
);
980 DWORD
svcctl_LockServiceDatabase(
981 SC_RPC_HANDLE hSCManager
,
984 struct sc_manager_handle
*manager
;
985 struct sc_lock
*lock
;
988 WINE_TRACE("(%p, %p)\n", hSCManager
, phLock
);
990 if ((err
= validate_scm_handle(hSCManager
, SC_MANAGER_LOCK
, &manager
)) != ERROR_SUCCESS
)
993 err
= scmdatabase_lock_startup(manager
->db
);
994 if (err
!= ERROR_SUCCESS
)
997 lock
= HeapAlloc(GetProcessHeap(), 0, sizeof(struct sc_lock
));
1000 scmdatabase_unlock_startup(manager
->db
);
1001 return ERROR_NOT_ENOUGH_SERVER_MEMORY
;
1004 lock
->db
= manager
->db
;
1007 return ERROR_SUCCESS
;
1010 DWORD
svcctl_UnlockServiceDatabase(
1011 SC_RPC_LOCK
*phLock
)
1013 WINE_TRACE("(&%p)\n", *phLock
);
1015 SC_RPC_LOCK_destroy(*phLock
);
1018 return ERROR_SUCCESS
;
1021 DWORD
svcctl_QueryServiceObjectSecurity(
1025 return ERROR_CALL_NOT_IMPLEMENTED
;
1028 DWORD
svcctl_SetServiceObjectSecurity(
1032 return ERROR_CALL_NOT_IMPLEMENTED
;
1035 DWORD
svcctl_QueryServiceStatus(
1039 return ERROR_CALL_NOT_IMPLEMENTED
;
1043 DWORD
svcctl_NotifyBootConfigStatus(
1047 return ERROR_CALL_NOT_IMPLEMENTED
;
1050 DWORD
svcctl_SCSetServiceBitsW(
1054 return ERROR_CALL_NOT_IMPLEMENTED
;
1058 DWORD
svcctl_EnumDependentServicesW(
1062 return ERROR_CALL_NOT_IMPLEMENTED
;
1065 DWORD
svcctl_EnumServicesStatusW(
1069 return ERROR_CALL_NOT_IMPLEMENTED
;
1073 DWORD
svcctl_QueryServiceLockStatusW(
1077 return ERROR_CALL_NOT_IMPLEMENTED
;
1080 DWORD
svcctl_SCSetServiceBitsA(
1084 return ERROR_CALL_NOT_IMPLEMENTED
;
1087 DWORD
svcctl_ChangeServiceConfigA(
1091 return ERROR_CALL_NOT_IMPLEMENTED
;
1094 DWORD
svcctl_CreateServiceA(
1098 return ERROR_CALL_NOT_IMPLEMENTED
;
1101 DWORD
svcctl_EnumDependentServicesA(
1105 return ERROR_CALL_NOT_IMPLEMENTED
;
1108 DWORD
svcctl_EnumServicesStatusA(
1112 return ERROR_CALL_NOT_IMPLEMENTED
;
1115 DWORD
svcctl_OpenSCManagerA(
1119 return ERROR_CALL_NOT_IMPLEMENTED
;
1122 DWORD
svcctl_OpenServiceA(
1126 return ERROR_CALL_NOT_IMPLEMENTED
;
1129 DWORD
svcctl_QueryServiceConfigA(
1133 return ERROR_CALL_NOT_IMPLEMENTED
;
1136 DWORD
svcctl_QueryServiceLockStatusA(
1140 return ERROR_CALL_NOT_IMPLEMENTED
;
1143 DWORD
svcctl_StartServiceA(
1147 return ERROR_CALL_NOT_IMPLEMENTED
;
1150 DWORD
svcctl_GetServiceDisplayNameA(
1154 return ERROR_CALL_NOT_IMPLEMENTED
;
1157 DWORD
svcctl_GetServiceKeyNameA(
1161 return ERROR_CALL_NOT_IMPLEMENTED
;
1164 DWORD
svcctl_GetCurrentGroupStateW(
1168 return ERROR_CALL_NOT_IMPLEMENTED
;
1171 DWORD
svcctl_EnumServiceGroupW(
1175 return ERROR_CALL_NOT_IMPLEMENTED
;
1178 DWORD
svcctl_ChangeServiceConfig2A(
1182 return ERROR_CALL_NOT_IMPLEMENTED
;
1185 DWORD
svcctl_QueryServiceConfig2A(
1189 return ERROR_CALL_NOT_IMPLEMENTED
;
1193 DWORD
RPC_Init(void)
1195 WCHAR transport
[] = SVCCTL_TRANSPORT
;
1196 WCHAR endpoint
[] = SVCCTL_ENDPOINT
;
1199 if ((err
= RpcServerUseProtseqEpW(transport
, 0, endpoint
, NULL
)) != ERROR_SUCCESS
)
1201 WINE_ERR("RpcServerUseProtseq failed with error %u\n", err
);
1205 if ((err
= RpcServerRegisterIf(svcctl_v2_0_s_ifspec
, 0, 0)) != ERROR_SUCCESS
)
1207 WINE_ERR("RpcServerRegisterIf failed with error %u\n", err
);
1211 if ((err
= RpcServerListen(1, RPC_C_LISTEN_MAX_CALLS_DEFAULT
, TRUE
)) != ERROR_SUCCESS
)
1213 WINE_ERR("RpcServerListen failed with error %u\n", err
);
1216 return ERROR_SUCCESS
;
1219 DWORD
RPC_MainLoop(void)
1222 HANDLE hExitEvent
= __wine_make_process_system();
1224 SetEvent(g_hStartedEvent
);
1226 WINE_TRACE("Entered main loop\n");
1230 err
= WaitForSingleObjectEx(hExitEvent
, INFINITE
, TRUE
);
1231 WINE_TRACE("Wait returned %d\n", err
);
1232 } while (err
!= WAIT_OBJECT_0
);
1234 WINE_TRACE("Object signaled - wine shutdown\n");
1235 CloseHandle(hExitEvent
);
1236 return ERROR_SUCCESS
;
1239 void __RPC_USER
SC_RPC_HANDLE_rundown(SC_RPC_HANDLE handle
)
1241 SC_RPC_HANDLE_destroy(handle
);
1244 void __RPC_FAR
* __RPC_USER
MIDL_user_allocate(SIZE_T len
)
1246 return HeapAlloc(GetProcessHeap(), 0, len
);
1249 void __RPC_USER
MIDL_user_free(void __RPC_FAR
* ptr
)
1251 HeapFree(GetProcessHeap(), 0, ptr
);