gdi32: Avoid redundant computation of the gradient bounding rectangle.
[wine/multimedia.git] / programs / services / rpc.c
blob579602a40b163a587cbfa20bf28a020bba3ed793
1 /*
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
23 #include <stdarg.h>
24 #include <windows.h>
25 #include <winternl.h>
26 #include <winsvc.h>
27 #include <ntsecapi.h>
28 #include <rpc.h>
30 #include "wine/list.h"
31 #include "wine/unicode.h"
32 #include "wine/debug.h"
34 #include "services.h"
35 #include "svcctl.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),
46 SC_MANAGER_ALL_ACCESS
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),
54 SERVICE_ALL_ACCESS
57 typedef enum
59 SC_HTYPE_DONT_CARE = 0,
60 SC_HTYPE_MANAGER,
61 SC_HTYPE_SERVICE
62 } SC_HANDLE_TYPE;
64 struct sc_handle
66 SC_HANDLE_TYPE type;
67 DWORD access;
70 struct sc_manager_handle /* service control manager handle */
72 struct sc_handle hdr;
73 struct scmdatabase *db;
76 struct sc_service_handle /* service handle */
78 struct sc_handle hdr;
79 struct service_entry *service_entry;
82 struct sc_lock
84 struct scmdatabase *db;
87 static void free_service_strings(struct service_entry *old, struct service_entry *new)
89 QUERY_SERVICE_CONFIGW *old_cfg = &old->config;
90 QUERY_SERVICE_CONFIGW *new_cfg = &new->config;
92 if (old_cfg->lpBinaryPathName != new_cfg->lpBinaryPathName)
93 HeapFree(GetProcessHeap(), 0, old_cfg->lpBinaryPathName);
95 if (old_cfg->lpLoadOrderGroup != new_cfg->lpLoadOrderGroup)
96 HeapFree(GetProcessHeap(), 0, old_cfg->lpLoadOrderGroup);
98 if (old_cfg->lpServiceStartName != new_cfg->lpServiceStartName)
99 HeapFree(GetProcessHeap(), 0, old_cfg->lpServiceStartName);
101 if (old_cfg->lpDisplayName != new_cfg->lpDisplayName)
102 HeapFree(GetProcessHeap(), 0, old_cfg->lpDisplayName);
104 if (old->dependOnServices != new->dependOnServices)
105 HeapFree(GetProcessHeap(), 0, old->dependOnServices);
107 if (old->dependOnGroups != new->dependOnGroups)
108 HeapFree(GetProcessHeap(), 0, old->dependOnGroups);
111 /* Check if the given handle is of the required type and allows the requested access. */
112 static DWORD validate_context_handle(SC_RPC_HANDLE handle, DWORD type, DWORD needed_access, struct sc_handle **out_hdr)
114 struct sc_handle *hdr = handle;
116 if (type != SC_HTYPE_DONT_CARE && hdr->type != type)
118 WINE_ERR("Handle is of an invalid type (%d, %d)\n", hdr->type, type);
119 return ERROR_INVALID_HANDLE;
122 if ((needed_access & hdr->access) != needed_access)
124 WINE_ERR("Access denied - handle created with access %x, needed %x\n", hdr->access, needed_access);
125 return ERROR_ACCESS_DENIED;
128 *out_hdr = hdr;
129 return ERROR_SUCCESS;
132 static DWORD validate_scm_handle(SC_RPC_HANDLE handle, DWORD needed_access, struct sc_manager_handle **manager)
134 struct sc_handle *hdr;
135 DWORD err = validate_context_handle(handle, SC_HTYPE_MANAGER, needed_access, &hdr);
136 if (err == ERROR_SUCCESS)
137 *manager = (struct sc_manager_handle *)hdr;
138 return err;
141 static DWORD validate_service_handle(SC_RPC_HANDLE handle, DWORD needed_access, struct sc_service_handle **service)
143 struct sc_handle *hdr;
144 DWORD err = validate_context_handle(handle, SC_HTYPE_SERVICE, needed_access, &hdr);
145 if (err == ERROR_SUCCESS)
146 *service = (struct sc_service_handle *)hdr;
147 return err;
150 DWORD __cdecl svcctl_OpenSCManagerW(
151 MACHINE_HANDLEW MachineName, /* Note: this parameter is ignored */
152 LPCWSTR DatabaseName,
153 DWORD dwAccessMask,
154 SC_RPC_HANDLE *handle)
156 struct sc_manager_handle *manager;
158 WINE_TRACE("(%s, %s, %x)\n", wine_dbgstr_w(MachineName), wine_dbgstr_w(DatabaseName), dwAccessMask);
160 if (DatabaseName != NULL && DatabaseName[0])
162 if (strcmpW(DatabaseName, SERVICES_FAILED_DATABASEW) == 0)
163 return ERROR_DATABASE_DOES_NOT_EXIST;
164 if (strcmpW(DatabaseName, SERVICES_ACTIVE_DATABASEW) != 0)
165 return ERROR_INVALID_NAME;
168 if (!(manager = HeapAlloc(GetProcessHeap(), 0, sizeof(*manager))))
169 return ERROR_NOT_ENOUGH_SERVER_MEMORY;
171 manager->hdr.type = SC_HTYPE_MANAGER;
173 if (dwAccessMask & MAXIMUM_ALLOWED)
174 dwAccessMask |= SC_MANAGER_ALL_ACCESS;
175 manager->hdr.access = dwAccessMask;
176 RtlMapGenericMask(&manager->hdr.access, &g_scm_generic);
177 manager->db = active_database;
178 *handle = &manager->hdr;
180 return ERROR_SUCCESS;
183 static void SC_RPC_HANDLE_destroy(SC_RPC_HANDLE handle)
185 struct sc_handle *hdr = handle;
186 switch (hdr->type)
188 case SC_HTYPE_MANAGER:
190 struct sc_manager_handle *manager = (struct sc_manager_handle *)hdr;
191 HeapFree(GetProcessHeap(), 0, manager);
192 break;
194 case SC_HTYPE_SERVICE:
196 struct sc_service_handle *service = (struct sc_service_handle *)hdr;
197 release_service(service->service_entry);
198 HeapFree(GetProcessHeap(), 0, service);
199 break;
201 default:
202 WINE_ERR("invalid handle type %d\n", hdr->type);
203 RpcRaiseException(ERROR_INVALID_HANDLE);
207 DWORD __cdecl svcctl_GetServiceDisplayNameW(
208 SC_RPC_HANDLE hSCManager,
209 LPCWSTR lpServiceName,
210 WCHAR *lpBuffer,
211 DWORD *cchBufSize)
213 struct sc_manager_handle *manager;
214 struct service_entry *entry;
215 DWORD err;
217 WINE_TRACE("(%s, %d)\n", wine_dbgstr_w(lpServiceName), *cchBufSize);
219 if ((err = validate_scm_handle(hSCManager, 0, &manager)) != ERROR_SUCCESS)
220 return err;
222 scmdatabase_lock_shared(manager->db);
224 entry = scmdatabase_find_service(manager->db, lpServiceName);
225 if (entry != NULL)
227 LPCWSTR name;
228 int len;
229 service_lock_shared(entry);
230 name = get_display_name(entry);
231 len = strlenW(name);
232 if (len <= *cchBufSize)
234 err = ERROR_SUCCESS;
235 memcpy(lpBuffer, name, (len + 1)*sizeof(*name));
237 else
238 err = ERROR_INSUFFICIENT_BUFFER;
239 *cchBufSize = len;
240 service_unlock(entry);
242 else
243 err = ERROR_SERVICE_DOES_NOT_EXIST;
245 scmdatabase_unlock(manager->db);
247 if (err != ERROR_SUCCESS)
248 lpBuffer[0] = 0;
250 return err;
253 DWORD __cdecl svcctl_GetServiceKeyNameW(
254 SC_RPC_HANDLE hSCManager,
255 LPCWSTR lpServiceDisplayName,
256 WCHAR *lpBuffer,
257 DWORD *cchBufSize)
259 struct service_entry *entry;
260 struct sc_manager_handle *manager;
261 DWORD err;
263 WINE_TRACE("(%s, %d)\n", wine_dbgstr_w(lpServiceDisplayName), *cchBufSize);
265 if ((err = validate_scm_handle(hSCManager, 0, &manager)) != ERROR_SUCCESS)
266 return err;
268 scmdatabase_lock_shared(manager->db);
270 entry = scmdatabase_find_service_by_displayname(manager->db, lpServiceDisplayName);
271 if (entry != NULL)
273 int len;
274 service_lock_shared(entry);
275 len = strlenW(entry->name);
276 if (len <= *cchBufSize)
278 err = ERROR_SUCCESS;
279 memcpy(lpBuffer, entry->name, (len + 1)*sizeof(*entry->name));
281 else
282 err = ERROR_INSUFFICIENT_BUFFER;
283 *cchBufSize = len;
284 service_unlock(entry);
286 else
287 err = ERROR_SERVICE_DOES_NOT_EXIST;
289 scmdatabase_unlock(manager->db);
291 if (err != ERROR_SUCCESS)
292 lpBuffer[0] = 0;
294 return err;
297 static DWORD create_handle_for_service(struct service_entry *entry, DWORD dwDesiredAccess, SC_RPC_HANDLE *phService)
299 struct sc_service_handle *service;
301 if (!(service = HeapAlloc(GetProcessHeap(), 0, sizeof(*service))))
303 release_service(entry);
304 return ERROR_NOT_ENOUGH_SERVER_MEMORY;
307 service->hdr.type = SC_HTYPE_SERVICE;
308 service->hdr.access = dwDesiredAccess;
309 RtlMapGenericMask(&service->hdr.access, &g_svc_generic);
310 service->service_entry = entry;
311 if (dwDesiredAccess & MAXIMUM_ALLOWED)
312 dwDesiredAccess |= SERVICE_ALL_ACCESS;
314 *phService = &service->hdr;
315 return ERROR_SUCCESS;
318 DWORD __cdecl svcctl_OpenServiceW(
319 SC_RPC_HANDLE hSCManager,
320 LPCWSTR lpServiceName,
321 DWORD dwDesiredAccess,
322 SC_RPC_HANDLE *phService)
324 struct sc_manager_handle *manager;
325 struct service_entry *entry;
326 DWORD err;
328 WINE_TRACE("(%s, 0x%x)\n", wine_dbgstr_w(lpServiceName), dwDesiredAccess);
330 if ((err = validate_scm_handle(hSCManager, 0, &manager)) != ERROR_SUCCESS)
331 return err;
332 if (!validate_service_name(lpServiceName))
333 return ERROR_INVALID_NAME;
335 scmdatabase_lock_shared(manager->db);
336 entry = scmdatabase_find_service(manager->db, lpServiceName);
337 if (entry != NULL)
338 InterlockedIncrement(&entry->ref_count);
339 scmdatabase_unlock(manager->db);
341 if (entry == NULL)
342 return ERROR_SERVICE_DOES_NOT_EXIST;
344 return create_handle_for_service(entry, dwDesiredAccess, phService);
347 static DWORD parse_dependencies(const WCHAR *dependencies, struct service_entry *entry)
349 WCHAR *services = NULL, *groups, *s;
350 DWORD len, len_services = 0, len_groups = 0;
351 const WCHAR *ptr = dependencies;
353 if (!dependencies || !dependencies[0])
355 entry->dependOnServices = NULL;
356 entry->dependOnGroups = NULL;
357 return ERROR_SUCCESS;
360 while (*ptr)
362 len = strlenW(ptr) + 1;
363 if (ptr[0] == '+' && ptr[1])
364 len_groups += len - 1;
365 else
366 len_services += len;
367 ptr += len;
369 if (!len_services) entry->dependOnServices = NULL;
370 else
372 services = HeapAlloc(GetProcessHeap(), 0, (len_services + 1) * sizeof(WCHAR));
373 if (!services)
374 return ERROR_OUTOFMEMORY;
376 s = services;
377 ptr = dependencies;
378 while (*ptr)
380 len = strlenW(ptr) + 1;
381 if (*ptr != '+')
383 strcpyW(s, ptr);
384 s += len;
386 ptr += len;
388 *s = 0;
389 entry->dependOnServices = services;
391 if (!len_groups) entry->dependOnGroups = NULL;
392 else
394 groups = HeapAlloc(GetProcessHeap(), 0, (len_groups + 1) * sizeof(WCHAR));
395 if (!groups)
397 HeapFree(GetProcessHeap(), 0, services);
398 return ERROR_OUTOFMEMORY;
400 s = groups;
401 ptr = dependencies;
402 while (*ptr)
404 len = strlenW(ptr) + 1;
405 if (ptr[0] == '+' && ptr[1])
407 strcpyW(s, ptr + 1);
408 s += len - 1;
410 ptr += len;
412 *s = 0;
413 entry->dependOnGroups = groups;
416 return ERROR_SUCCESS;
419 DWORD __cdecl svcctl_CreateServiceW(
420 SC_RPC_HANDLE hSCManager,
421 LPCWSTR lpServiceName,
422 LPCWSTR lpDisplayName,
423 DWORD dwDesiredAccess,
424 DWORD dwServiceType,
425 DWORD dwStartType,
426 DWORD dwErrorControl,
427 LPCWSTR lpBinaryPathName,
428 LPCWSTR lpLoadOrderGroup,
429 DWORD *lpdwTagId,
430 const BYTE *lpDependencies,
431 DWORD dwDependenciesSize,
432 LPCWSTR lpServiceStartName,
433 const BYTE *lpPassword,
434 DWORD dwPasswordSize,
435 SC_RPC_HANDLE *phService)
437 struct sc_manager_handle *manager;
438 struct service_entry *entry;
439 DWORD err;
441 WINE_TRACE("(%s, %s, 0x%x, %s)\n", wine_dbgstr_w(lpServiceName), wine_dbgstr_w(lpDisplayName), dwDesiredAccess, wine_dbgstr_w(lpBinaryPathName));
443 if ((err = validate_scm_handle(hSCManager, SC_MANAGER_CREATE_SERVICE, &manager)) != ERROR_SUCCESS)
444 return err;
446 if (!validate_service_name(lpServiceName))
447 return ERROR_INVALID_NAME;
448 if (!check_multisz((LPCWSTR)lpDependencies, dwDependenciesSize) || !lpServiceName[0] || !lpBinaryPathName[0])
449 return ERROR_INVALID_PARAMETER;
451 if (lpPassword)
452 WINE_FIXME("Don't know how to add a password\n"); /* I always get ERROR_GEN_FAILURE */
454 err = service_create(lpServiceName, &entry);
455 if (err != ERROR_SUCCESS)
456 return err;
458 err = parse_dependencies((LPCWSTR)lpDependencies, entry);
459 if (err != ERROR_SUCCESS)
460 return err;
462 entry->ref_count = 1;
463 entry->config.dwServiceType = entry->status.dwServiceType = dwServiceType;
464 entry->config.dwStartType = dwStartType;
465 entry->config.dwErrorControl = dwErrorControl;
466 entry->config.lpBinaryPathName = strdupW(lpBinaryPathName);
467 entry->config.lpLoadOrderGroup = strdupW(lpLoadOrderGroup);
468 entry->config.lpServiceStartName = strdupW(lpServiceStartName);
469 entry->config.lpDisplayName = strdupW(lpDisplayName);
471 if (lpdwTagId) /* TODO: In most situations a non-NULL TagId will generate an ERROR_INVALID_PARAMETER. */
472 entry->config.dwTagId = *lpdwTagId;
473 else
474 entry->config.dwTagId = 0;
476 /* other fields NULL*/
478 if (!validate_service_config(entry))
480 WINE_ERR("Invalid data while trying to create service\n");
481 free_service_entry(entry);
482 return ERROR_INVALID_PARAMETER;
485 scmdatabase_lock_exclusive(manager->db);
487 if (scmdatabase_find_service(manager->db, lpServiceName))
489 scmdatabase_unlock(manager->db);
490 free_service_entry(entry);
491 return ERROR_SERVICE_EXISTS;
494 if (scmdatabase_find_service_by_displayname(manager->db, get_display_name(entry)))
496 scmdatabase_unlock(manager->db);
497 free_service_entry(entry);
498 return ERROR_DUPLICATE_SERVICE_NAME;
501 err = scmdatabase_add_service(manager->db, entry);
502 if (err != ERROR_SUCCESS)
504 scmdatabase_unlock(manager->db);
505 free_service_entry(entry);
506 return err;
508 scmdatabase_unlock(manager->db);
510 return create_handle_for_service(entry, dwDesiredAccess, phService);
513 DWORD __cdecl svcctl_DeleteService(
514 SC_RPC_HANDLE hService)
516 struct sc_service_handle *service;
517 DWORD err;
519 if ((err = validate_service_handle(hService, DELETE, &service)) != ERROR_SUCCESS)
520 return err;
522 scmdatabase_lock_exclusive(service->service_entry->db);
523 service_lock_exclusive(service->service_entry);
525 if (!is_marked_for_delete(service->service_entry))
526 err = scmdatabase_remove_service(service->service_entry->db, service->service_entry);
527 else
528 err = ERROR_SERVICE_MARKED_FOR_DELETE;
530 service_unlock(service->service_entry);
531 scmdatabase_unlock(service->service_entry->db);
533 return err;
536 DWORD __cdecl svcctl_QueryServiceConfigW(
537 SC_RPC_HANDLE hService,
538 QUERY_SERVICE_CONFIGW *config)
540 struct sc_service_handle *service;
541 DWORD err;
543 WINE_TRACE("(%p)\n", config);
545 if ((err = validate_service_handle(hService, SERVICE_QUERY_CONFIG, &service)) != 0)
546 return err;
548 service_lock_shared(service->service_entry);
549 config->dwServiceType = service->service_entry->config.dwServiceType;
550 config->dwStartType = service->service_entry->config.dwStartType;
551 config->dwErrorControl = service->service_entry->config.dwErrorControl;
552 config->lpBinaryPathName = strdupW(service->service_entry->config.lpBinaryPathName);
553 config->lpLoadOrderGroup = strdupW(service->service_entry->config.lpLoadOrderGroup);
554 config->dwTagId = service->service_entry->config.dwTagId;
555 config->lpDependencies = NULL; /* TODO */
556 config->lpServiceStartName = strdupW(service->service_entry->config.lpServiceStartName);
557 config->lpDisplayName = strdupW(service->service_entry->config.lpDisplayName);
558 service_unlock(service->service_entry);
560 return ERROR_SUCCESS;
563 DWORD __cdecl svcctl_ChangeServiceConfigW(
564 SC_RPC_HANDLE hService,
565 DWORD dwServiceType,
566 DWORD dwStartType,
567 DWORD dwErrorControl,
568 LPCWSTR lpBinaryPathName,
569 LPCWSTR lpLoadOrderGroup,
570 DWORD *lpdwTagId,
571 const BYTE *lpDependencies,
572 DWORD dwDependenciesSize,
573 LPCWSTR lpServiceStartName,
574 const BYTE *lpPassword,
575 DWORD dwPasswordSize,
576 LPCWSTR lpDisplayName)
578 struct service_entry new_entry, *entry;
579 struct sc_service_handle *service;
580 DWORD err;
582 WINE_TRACE("\n");
584 if ((err = validate_service_handle(hService, SERVICE_CHANGE_CONFIG, &service)) != 0)
585 return err;
587 if (!check_multisz((LPCWSTR)lpDependencies, dwDependenciesSize))
588 return ERROR_INVALID_PARAMETER;
590 /* first check if the new configuration is correct */
591 service_lock_exclusive(service->service_entry);
593 if (is_marked_for_delete(service->service_entry))
595 service_unlock(service->service_entry);
596 return ERROR_SERVICE_MARKED_FOR_DELETE;
599 if (lpDisplayName != NULL &&
600 (entry = scmdatabase_find_service_by_displayname(service->service_entry->db, lpDisplayName)) &&
601 (entry != service->service_entry))
603 service_unlock(service->service_entry);
604 return ERROR_DUPLICATE_SERVICE_NAME;
607 new_entry = *service->service_entry;
609 if (dwServiceType != SERVICE_NO_CHANGE)
610 new_entry.config.dwServiceType = dwServiceType;
612 if (dwStartType != SERVICE_NO_CHANGE)
613 new_entry.config.dwStartType = dwStartType;
615 if (dwErrorControl != SERVICE_NO_CHANGE)
616 new_entry.config.dwErrorControl = dwErrorControl;
618 if (lpBinaryPathName != NULL)
619 new_entry.config.lpBinaryPathName = (LPWSTR)lpBinaryPathName;
621 if (lpLoadOrderGroup != NULL)
622 new_entry.config.lpLoadOrderGroup = (LPWSTR)lpLoadOrderGroup;
624 if (lpdwTagId != NULL)
625 WINE_FIXME("Changing tag id not supported\n");
627 if (lpServiceStartName != NULL)
628 new_entry.config.lpServiceStartName = (LPWSTR)lpServiceStartName;
630 if (lpPassword != NULL)
631 WINE_FIXME("Setting password not supported\n");
633 if (lpDisplayName != NULL)
634 new_entry.config.lpDisplayName = (LPWSTR)lpDisplayName;
636 err = parse_dependencies((LPCWSTR)lpDependencies, &new_entry);
637 if (err != ERROR_SUCCESS)
639 service_unlock(service->service_entry);
640 return err;
643 if (!validate_service_config(&new_entry))
645 WINE_ERR("The configuration after the change wouldn't be valid\n");
646 service_unlock(service->service_entry);
647 return ERROR_INVALID_PARAMETER;
650 /* configuration OK. The strings needs to be duplicated */
651 if (lpBinaryPathName != NULL)
652 new_entry.config.lpBinaryPathName = strdupW(lpBinaryPathName);
654 if (lpLoadOrderGroup != NULL)
655 new_entry.config.lpLoadOrderGroup = strdupW(lpLoadOrderGroup);
657 if (lpServiceStartName != NULL)
658 new_entry.config.lpServiceStartName = strdupW(lpServiceStartName);
660 if (lpDisplayName != NULL)
661 new_entry.config.lpDisplayName = strdupW(lpDisplayName);
663 /* try to save to Registry, commit or rollback depending on success */
664 err = save_service_config(&new_entry);
665 if (ERROR_SUCCESS == err)
667 free_service_strings(service->service_entry, &new_entry);
668 *service->service_entry = new_entry;
670 else free_service_strings(&new_entry, service->service_entry);
671 service_unlock(service->service_entry);
673 return err;
676 DWORD __cdecl svcctl_SetServiceStatus(
677 SC_RPC_HANDLE hServiceStatus,
678 LPSERVICE_STATUS lpServiceStatus)
680 struct sc_service_handle *service;
681 DWORD err;
683 WINE_TRACE("(%p, %p)\n", hServiceStatus, lpServiceStatus);
685 if ((err = validate_service_handle(hServiceStatus, SERVICE_SET_STATUS, &service)) != 0)
686 return err;
688 service_lock_exclusive(service->service_entry);
689 /* FIXME: be a bit more discriminant about what parts of the status we set
690 * and check that fields are valid */
691 service->service_entry->status.dwServiceType = lpServiceStatus->dwServiceType;
692 service->service_entry->status.dwCurrentState = lpServiceStatus->dwCurrentState;
693 service->service_entry->status.dwControlsAccepted = lpServiceStatus->dwControlsAccepted;
694 service->service_entry->status.dwWin32ExitCode = lpServiceStatus->dwWin32ExitCode;
695 service->service_entry->status.dwServiceSpecificExitCode = lpServiceStatus->dwServiceSpecificExitCode;
696 service->service_entry->status.dwCheckPoint = lpServiceStatus->dwCheckPoint;
697 service->service_entry->status.dwWaitHint = lpServiceStatus->dwWaitHint;
698 service_unlock(service->service_entry);
700 if (lpServiceStatus->dwCurrentState == SERVICE_STOPPED)
701 service_terminate(service->service_entry);
702 else if (service->service_entry->status_changed_event)
703 SetEvent(service->service_entry->status_changed_event);
705 return ERROR_SUCCESS;
708 DWORD __cdecl svcctl_ChangeServiceConfig2W( SC_RPC_HANDLE hService, DWORD level, SERVICE_CONFIG2W *config )
710 struct sc_service_handle *service;
711 DWORD err;
713 if ((err = validate_service_handle(hService, SERVICE_CHANGE_CONFIG, &service)) != 0)
714 return err;
716 switch (level)
718 case SERVICE_CONFIG_DESCRIPTION:
720 WCHAR *descr = NULL;
722 if (config->descr.lpDescription[0])
724 if (!(descr = strdupW( config->descr.lpDescription )))
725 return ERROR_NOT_ENOUGH_MEMORY;
728 WINE_TRACE( "changing service %p descr to %s\n", service, wine_dbgstr_w(descr) );
729 service_lock_exclusive( service->service_entry );
730 HeapFree( GetProcessHeap(), 0, service->service_entry->description );
731 service->service_entry->description = descr;
732 save_service_config( service->service_entry );
733 service_unlock( service->service_entry );
735 break;
736 case SERVICE_CONFIG_FAILURE_ACTIONS:
737 WINE_FIXME( "SERVICE_CONFIG_FAILURE_ACTIONS not implemented: period %u msg %s cmd %s\n",
738 config->actions.dwResetPeriod,
739 wine_dbgstr_w(config->actions.lpRebootMsg),
740 wine_dbgstr_w(config->actions.lpCommand) );
741 break;
742 case SERVICE_CONFIG_PRESHUTDOWN_INFO:
743 WINE_TRACE( "changing service %p preshutdown timeout to %d\n",
744 service, config->preshutdown.dwPreshutdownTimeout );
745 service_lock_exclusive( service->service_entry );
746 service->service_entry->preshutdown_timeout = config->preshutdown.dwPreshutdownTimeout;
747 save_service_config( service->service_entry );
748 service_unlock( service->service_entry );
749 break;
750 default:
751 WINE_FIXME("level %u not implemented\n", level);
752 err = ERROR_INVALID_LEVEL;
753 break;
755 return err;
758 DWORD __cdecl svcctl_QueryServiceConfig2W( SC_RPC_HANDLE hService, DWORD level,
759 BYTE *buffer, DWORD size, LPDWORD needed )
761 struct sc_service_handle *service;
762 DWORD err;
764 memset(buffer, 0, size);
766 if ((err = validate_service_handle(hService, SERVICE_QUERY_STATUS, &service)) != 0)
767 return err;
769 switch (level)
771 case SERVICE_CONFIG_DESCRIPTION:
773 SERVICE_DESCRIPTIONW *descr = (SERVICE_DESCRIPTIONW *)buffer;
775 service_lock_shared(service->service_entry);
776 *needed = sizeof(*descr);
777 if (service->service_entry->description)
778 *needed += (strlenW(service->service_entry->description) + 1) * sizeof(WCHAR);
779 if (size >= *needed)
781 if (service->service_entry->description)
783 /* store a buffer offset instead of a pointer */
784 descr->lpDescription = (WCHAR *)((BYTE *)(descr + 1) - buffer);
785 strcpyW( (WCHAR *)(descr + 1), service->service_entry->description );
787 else descr->lpDescription = NULL;
789 else err = ERROR_INSUFFICIENT_BUFFER;
790 service_unlock(service->service_entry);
792 break;
794 case SERVICE_CONFIG_PRESHUTDOWN_INFO:
795 service_lock_shared(service->service_entry);
797 *needed = sizeof(SERVICE_PRESHUTDOWN_INFO);
798 if (size >= *needed)
799 ((LPSERVICE_PRESHUTDOWN_INFO)buffer)->dwPreshutdownTimeout =
800 service->service_entry->preshutdown_timeout;
801 else err = ERROR_INSUFFICIENT_BUFFER;
803 service_unlock(service->service_entry);
804 break;
806 default:
807 WINE_FIXME("level %u not implemented\n", level);
808 err = ERROR_INVALID_LEVEL;
809 break;
811 return err;
814 DWORD __cdecl svcctl_QueryServiceStatusEx(
815 SC_RPC_HANDLE hService,
816 SC_STATUS_TYPE InfoLevel,
817 BYTE *lpBuffer,
818 DWORD cbBufSize,
819 LPDWORD pcbBytesNeeded)
821 struct sc_service_handle *service;
822 DWORD err;
823 LPSERVICE_STATUS_PROCESS pSvcStatusData;
825 memset(lpBuffer, 0, cbBufSize);
827 if ((err = validate_service_handle(hService, SERVICE_QUERY_STATUS, &service)) != 0)
828 return err;
830 if (InfoLevel != SC_STATUS_PROCESS_INFO)
831 return ERROR_INVALID_LEVEL;
833 pSvcStatusData = (LPSERVICE_STATUS_PROCESS) lpBuffer;
834 if (pSvcStatusData == NULL)
835 return ERROR_INVALID_PARAMETER;
837 if (cbBufSize < sizeof(SERVICE_STATUS_PROCESS))
839 if( pcbBytesNeeded != NULL)
840 *pcbBytesNeeded = sizeof(SERVICE_STATUS_PROCESS);
842 return ERROR_INSUFFICIENT_BUFFER;
845 service_lock_shared(service->service_entry);
847 pSvcStatusData->dwServiceType = service->service_entry->status.dwServiceType;
848 pSvcStatusData->dwCurrentState = service->service_entry->status.dwCurrentState;
849 pSvcStatusData->dwControlsAccepted = service->service_entry->status.dwControlsAccepted;
850 pSvcStatusData->dwWin32ExitCode = service->service_entry->status.dwWin32ExitCode;
851 pSvcStatusData->dwServiceSpecificExitCode = service->service_entry->status.dwServiceSpecificExitCode;
852 pSvcStatusData->dwCheckPoint = service->service_entry->status.dwCheckPoint;
853 pSvcStatusData->dwWaitHint = service->service_entry->status.dwWaitHint;
854 pSvcStatusData->dwProcessId = service->service_entry->status.dwProcessId;
855 pSvcStatusData->dwServiceFlags = service->service_entry->status.dwServiceFlags;
857 service_unlock(service->service_entry);
859 return ERROR_SUCCESS;
862 /******************************************************************************
863 * service_accepts_control
865 static BOOL service_accepts_control(const struct service_entry *service, DWORD dwControl)
867 DWORD a = service->status.dwControlsAccepted;
869 switch (dwControl)
871 case SERVICE_CONTROL_INTERROGATE:
872 return TRUE;
873 case SERVICE_CONTROL_STOP:
874 if (a&SERVICE_ACCEPT_STOP)
875 return TRUE;
876 break;
877 case SERVICE_CONTROL_SHUTDOWN:
878 if (a&SERVICE_ACCEPT_SHUTDOWN)
879 return TRUE;
880 break;
881 case SERVICE_CONTROL_PAUSE:
882 case SERVICE_CONTROL_CONTINUE:
883 if (a&SERVICE_ACCEPT_PAUSE_CONTINUE)
884 return TRUE;
885 break;
886 case SERVICE_CONTROL_PARAMCHANGE:
887 if (a&SERVICE_ACCEPT_PARAMCHANGE)
888 return TRUE;
889 break;
890 case SERVICE_CONTROL_NETBINDADD:
891 case SERVICE_CONTROL_NETBINDREMOVE:
892 case SERVICE_CONTROL_NETBINDENABLE:
893 case SERVICE_CONTROL_NETBINDDISABLE:
894 if (a&SERVICE_ACCEPT_NETBINDCHANGE)
895 return TRUE;
896 case SERVICE_CONTROL_HARDWAREPROFILECHANGE:
897 if (a&SERVICE_ACCEPT_HARDWAREPROFILECHANGE)
898 return TRUE;
899 break;
900 case SERVICE_CONTROL_POWEREVENT:
901 if (a&SERVICE_ACCEPT_POWEREVENT)
902 return TRUE;
903 break;
904 case SERVICE_CONTROL_SESSIONCHANGE:
905 if (a&SERVICE_ACCEPT_SESSIONCHANGE)
906 return TRUE;
907 break;
909 return FALSE;
912 /******************************************************************************
913 * service_send_command
915 BOOL service_send_command( struct service_entry *service, HANDLE pipe,
916 const void *data, DWORD size, DWORD *result )
918 OVERLAPPED overlapped;
919 DWORD count, ret;
920 BOOL r;
922 overlapped.hEvent = service->overlapped_event;
923 r = WriteFile(pipe, data, size, &count, &overlapped);
924 if (!r && GetLastError() == ERROR_IO_PENDING)
926 ret = WaitForSingleObject( service->overlapped_event, service_pipe_timeout );
927 if (ret == WAIT_TIMEOUT)
929 WINE_ERR("sending command timed out\n");
930 *result = ERROR_SERVICE_REQUEST_TIMEOUT;
931 return FALSE;
933 r = GetOverlappedResult( pipe, &overlapped, &count, FALSE );
935 if (!r || count != size)
937 WINE_ERR("service protocol error - failed to write pipe!\n");
938 *result = (!r ? GetLastError() : ERROR_WRITE_FAULT);
939 return FALSE;
941 r = ReadFile(pipe, result, sizeof *result, &count, &overlapped);
942 if (!r && GetLastError() == ERROR_IO_PENDING)
944 ret = WaitForSingleObject( service->overlapped_event, service_pipe_timeout );
945 if (ret == WAIT_TIMEOUT)
947 WINE_ERR("receiving command result timed out\n");
948 *result = ERROR_SERVICE_REQUEST_TIMEOUT;
949 return FALSE;
951 r = GetOverlappedResult( pipe, &overlapped, &count, FALSE );
953 if (!r || count != sizeof *result)
955 WINE_ERR("service protocol error - failed to read pipe "
956 "r = %d count = %d!\n", r, count);
957 *result = (!r ? GetLastError() : ERROR_READ_FAULT);
958 return FALSE;
961 *result = ERROR_SUCCESS;
962 return TRUE;
965 /******************************************************************************
966 * service_send_control
968 static BOOL service_send_control(struct service_entry *service, HANDLE pipe, DWORD dwControl, DWORD *result)
970 service_start_info *ssi;
971 DWORD len;
972 BOOL r;
974 /* calculate how much space we need to send the startup info */
975 len = strlenW(service->name) + 1;
977 ssi = HeapAlloc(GetProcessHeap(),0,FIELD_OFFSET(service_start_info, data[len]));
978 ssi->cmd = WINESERV_SENDCONTROL;
979 ssi->control = dwControl;
980 ssi->total_size = FIELD_OFFSET(service_start_info, data[len]);
981 ssi->name_size = strlenW(service->name) + 1;
982 strcpyW( ssi->data, service->name );
984 r = service_send_command( service, pipe, ssi, ssi->total_size, result );
985 HeapFree( GetProcessHeap(), 0, ssi );
986 return r;
989 DWORD __cdecl svcctl_StartServiceW(
990 SC_RPC_HANDLE hService,
991 DWORD dwNumServiceArgs,
992 LPCWSTR *lpServiceArgVectors)
994 struct sc_service_handle *service;
995 DWORD err;
997 WINE_TRACE("(%p, %d, %p)\n", hService, dwNumServiceArgs, lpServiceArgVectors);
999 if ((err = validate_service_handle(hService, SERVICE_START, &service)) != 0)
1000 return err;
1002 if (service->service_entry->config.dwStartType == SERVICE_DISABLED)
1003 return ERROR_SERVICE_DISABLED;
1005 err = service_start(service->service_entry, dwNumServiceArgs, lpServiceArgVectors);
1007 return err;
1010 DWORD __cdecl svcctl_ControlService(
1011 SC_RPC_HANDLE hService,
1012 DWORD dwControl,
1013 SERVICE_STATUS *lpServiceStatus)
1015 DWORD access_required;
1016 struct sc_service_handle *service;
1017 DWORD result;
1018 BOOL ret;
1019 HANDLE control_mutex;
1021 WINE_TRACE("(%p, %d, %p)\n", hService, dwControl, lpServiceStatus);
1023 switch (dwControl)
1025 case SERVICE_CONTROL_CONTINUE:
1026 case SERVICE_CONTROL_NETBINDADD:
1027 case SERVICE_CONTROL_NETBINDDISABLE:
1028 case SERVICE_CONTROL_NETBINDENABLE:
1029 case SERVICE_CONTROL_NETBINDREMOVE:
1030 case SERVICE_CONTROL_PARAMCHANGE:
1031 case SERVICE_CONTROL_PAUSE:
1032 access_required = SERVICE_PAUSE_CONTINUE;
1033 break;
1034 case SERVICE_CONTROL_INTERROGATE:
1035 access_required = SERVICE_INTERROGATE;
1036 break;
1037 case SERVICE_CONTROL_STOP:
1038 access_required = SERVICE_STOP;
1039 break;
1040 default:
1041 if (dwControl >= 128 && dwControl <= 255)
1042 access_required = SERVICE_USER_DEFINED_CONTROL;
1043 else
1044 return ERROR_INVALID_PARAMETER;
1047 if ((result = validate_service_handle(hService, access_required, &service)) != 0)
1048 return result;
1050 service_lock_exclusive(service->service_entry);
1052 result = ERROR_SUCCESS;
1053 switch (service->service_entry->status.dwCurrentState)
1055 case SERVICE_STOPPED:
1056 result = ERROR_SERVICE_NOT_ACTIVE;
1057 break;
1058 case SERVICE_START_PENDING:
1059 if (dwControl==SERVICE_CONTROL_STOP)
1060 break;
1061 /* fall thru */
1062 case SERVICE_STOP_PENDING:
1063 result = ERROR_SERVICE_CANNOT_ACCEPT_CTRL;
1064 break;
1067 if (result==ERROR_SUCCESS && !service->service_entry->control_mutex) {
1068 result = ERROR_SERVICE_CANNOT_ACCEPT_CTRL;
1069 service_terminate(service->service_entry);
1072 if (result != ERROR_SUCCESS)
1074 if (lpServiceStatus)
1076 lpServiceStatus->dwServiceType = service->service_entry->status.dwServiceType;
1077 lpServiceStatus->dwCurrentState = service->service_entry->status.dwCurrentState;
1078 lpServiceStatus->dwControlsAccepted = service->service_entry->status.dwControlsAccepted;
1079 lpServiceStatus->dwWin32ExitCode = service->service_entry->status.dwWin32ExitCode;
1080 lpServiceStatus->dwServiceSpecificExitCode = service->service_entry->status.dwServiceSpecificExitCode;
1081 lpServiceStatus->dwCheckPoint = service->service_entry->status.dwCheckPoint;
1082 lpServiceStatus->dwWaitHint = service->service_entry->status.dwWaitHint;
1084 service_unlock(service->service_entry);
1085 return result;
1088 if (!service_accepts_control(service->service_entry, dwControl))
1090 service_unlock(service->service_entry);
1091 return ERROR_INVALID_SERVICE_CONTROL;
1094 /* prevent races by caching control_mutex and clearing it on
1095 * stop instead of outside the services lock */
1096 control_mutex = service->service_entry->control_mutex;
1097 if (dwControl == SERVICE_CONTROL_STOP)
1098 service->service_entry->control_mutex = NULL;
1100 service_unlock(service->service_entry);
1102 ret = WaitForSingleObject(control_mutex, 30000);
1103 if (ret == WAIT_OBJECT_0)
1105 service_send_control(service->service_entry, service->service_entry->control_pipe,
1106 dwControl, &result);
1108 if (lpServiceStatus)
1110 service_lock_shared(service->service_entry);
1111 lpServiceStatus->dwServiceType = service->service_entry->status.dwServiceType;
1112 lpServiceStatus->dwCurrentState = service->service_entry->status.dwCurrentState;
1113 lpServiceStatus->dwControlsAccepted = service->service_entry->status.dwControlsAccepted;
1114 lpServiceStatus->dwWin32ExitCode = service->service_entry->status.dwWin32ExitCode;
1115 lpServiceStatus->dwServiceSpecificExitCode = service->service_entry->status.dwServiceSpecificExitCode;
1116 lpServiceStatus->dwCheckPoint = service->service_entry->status.dwCheckPoint;
1117 lpServiceStatus->dwWaitHint = service->service_entry->status.dwWaitHint;
1118 service_unlock(service->service_entry);
1121 if (dwControl == SERVICE_CONTROL_STOP)
1122 CloseHandle(control_mutex);
1123 else
1124 ReleaseMutex(control_mutex);
1126 return result;
1128 else
1130 if (dwControl == SERVICE_CONTROL_STOP)
1131 CloseHandle(control_mutex);
1132 return ERROR_SERVICE_REQUEST_TIMEOUT;
1136 DWORD __cdecl svcctl_CloseServiceHandle(
1137 SC_RPC_HANDLE *handle)
1139 WINE_TRACE("(&%p)\n", *handle);
1141 SC_RPC_HANDLE_destroy(*handle);
1142 *handle = NULL;
1144 return ERROR_SUCCESS;
1147 static void SC_RPC_LOCK_destroy(SC_RPC_LOCK hLock)
1149 struct sc_lock *lock = hLock;
1150 scmdatabase_unlock_startup(lock->db);
1151 HeapFree(GetProcessHeap(), 0, lock);
1154 void __RPC_USER SC_RPC_LOCK_rundown(SC_RPC_LOCK hLock)
1156 SC_RPC_LOCK_destroy(hLock);
1159 DWORD __cdecl svcctl_LockServiceDatabase(
1160 SC_RPC_HANDLE hSCManager,
1161 SC_RPC_LOCK *phLock)
1163 struct sc_manager_handle *manager;
1164 struct sc_lock *lock;
1165 DWORD err;
1167 WINE_TRACE("(%p, %p)\n", hSCManager, phLock);
1169 if ((err = validate_scm_handle(hSCManager, SC_MANAGER_LOCK, &manager)) != ERROR_SUCCESS)
1170 return err;
1172 err = scmdatabase_lock_startup(manager->db);
1173 if (err != ERROR_SUCCESS)
1174 return err;
1176 lock = HeapAlloc(GetProcessHeap(), 0, sizeof(struct sc_lock));
1177 if (!lock)
1179 scmdatabase_unlock_startup(manager->db);
1180 return ERROR_NOT_ENOUGH_SERVER_MEMORY;
1183 lock->db = manager->db;
1184 *phLock = lock;
1186 return ERROR_SUCCESS;
1189 DWORD __cdecl svcctl_UnlockServiceDatabase(
1190 SC_RPC_LOCK *phLock)
1192 WINE_TRACE("(&%p)\n", *phLock);
1194 SC_RPC_LOCK_destroy(*phLock);
1195 *phLock = NULL;
1197 return ERROR_SUCCESS;
1200 static BOOL map_state(DWORD state, DWORD mask)
1202 switch (state)
1204 case SERVICE_START_PENDING:
1205 case SERVICE_STOP_PENDING:
1206 case SERVICE_RUNNING:
1207 case SERVICE_CONTINUE_PENDING:
1208 case SERVICE_PAUSE_PENDING:
1209 case SERVICE_PAUSED:
1210 if (SERVICE_ACTIVE & mask) return TRUE;
1211 break;
1212 case SERVICE_STOPPED:
1213 if (SERVICE_INACTIVE & mask) return TRUE;
1214 break;
1215 default:
1216 WINE_ERR("unknown state %u\n", state);
1217 break;
1219 return FALSE;
1222 DWORD __cdecl svcctl_EnumServicesStatusW(
1223 SC_RPC_HANDLE hmngr,
1224 DWORD type,
1225 DWORD state,
1226 BYTE *buffer,
1227 DWORD size,
1228 LPDWORD needed,
1229 LPDWORD returned)
1231 DWORD err, sz, total_size, num_services;
1232 DWORD_PTR offset;
1233 struct sc_manager_handle *manager;
1234 struct service_entry *service;
1235 ENUM_SERVICE_STATUSW *s;
1237 WINE_TRACE("(%p, 0x%x, 0x%x, %p, %u, %p, %p)\n", hmngr, type, state, buffer, size, needed, returned);
1239 if (!type || !state)
1240 return ERROR_INVALID_PARAMETER;
1242 if ((err = validate_scm_handle(hmngr, SC_MANAGER_ENUMERATE_SERVICE, &manager)) != ERROR_SUCCESS)
1243 return err;
1245 scmdatabase_lock_exclusive(manager->db);
1247 total_size = num_services = 0;
1248 LIST_FOR_EACH_ENTRY(service, &manager->db->services, struct service_entry, entry)
1250 if ((service->status.dwServiceType & type) && map_state(service->status.dwCurrentState, state))
1252 total_size += sizeof(ENUM_SERVICE_STATUSW);
1253 total_size += (strlenW(service->name) + 1) * sizeof(WCHAR);
1254 if (service->config.lpDisplayName)
1256 total_size += (strlenW(service->config.lpDisplayName) + 1) * sizeof(WCHAR);
1258 num_services++;
1261 *returned = 0;
1262 *needed = total_size;
1263 if (total_size > size)
1265 scmdatabase_unlock(manager->db);
1266 return ERROR_MORE_DATA;
1268 s = (ENUM_SERVICE_STATUSW *)buffer;
1269 offset = num_services * sizeof(ENUM_SERVICE_STATUSW);
1270 LIST_FOR_EACH_ENTRY(service, &manager->db->services, struct service_entry, entry)
1272 if ((service->status.dwServiceType & type) && map_state(service->status.dwCurrentState, state))
1274 sz = (strlenW(service->name) + 1) * sizeof(WCHAR);
1275 memcpy(buffer + offset, service->name, sz);
1276 s->lpServiceName = (WCHAR *)offset; /* store a buffer offset instead of a pointer */
1277 offset += sz;
1279 if (!service->config.lpDisplayName) s->lpDisplayName = NULL;
1280 else
1282 sz = (strlenW(service->config.lpDisplayName) + 1) * sizeof(WCHAR);
1283 memcpy(buffer + offset, service->config.lpDisplayName, sz);
1284 s->lpDisplayName = (WCHAR *)offset;
1285 offset += sz;
1287 memcpy(&s->ServiceStatus, &service->status, sizeof(SERVICE_STATUS));
1288 s++;
1291 *returned = num_services;
1292 *needed = 0;
1293 scmdatabase_unlock(manager->db);
1294 return ERROR_SUCCESS;
1297 static struct service_entry *find_service_by_group(struct scmdatabase *db, const WCHAR *group)
1299 struct service_entry *service;
1300 LIST_FOR_EACH_ENTRY(service, &db->services, struct service_entry, entry)
1302 if (service->config.lpLoadOrderGroup && !strcmpiW(group, service->config.lpLoadOrderGroup))
1303 return service;
1305 return NULL;
1308 static BOOL match_group(const WCHAR *g1, const WCHAR *g2)
1310 if (!g2) return TRUE;
1311 if (!g2[0] && (!g1 || !g1[0])) return TRUE;
1312 if (g1 && !strcmpW(g1, g2)) return TRUE;
1313 return FALSE;
1316 DWORD __cdecl svcctl_EnumServicesStatusExW(
1317 SC_RPC_HANDLE hmngr,
1318 DWORD type,
1319 DWORD state,
1320 BYTE *buffer,
1321 DWORD size,
1322 LPDWORD needed,
1323 LPDWORD returned,
1324 LPCWSTR group)
1326 DWORD err, sz, total_size, num_services;
1327 DWORD_PTR offset;
1328 struct sc_manager_handle *manager;
1329 struct service_entry *service;
1330 ENUM_SERVICE_STATUS_PROCESSW *s;
1332 WINE_TRACE("(%p, 0x%x, 0x%x, %p, %u, %p, %p, %s)\n", hmngr, type, state, buffer, size,
1333 needed, returned, wine_dbgstr_w(group));
1335 if (!type || !state)
1336 return ERROR_INVALID_PARAMETER;
1338 if ((err = validate_scm_handle(hmngr, SC_MANAGER_ENUMERATE_SERVICE, &manager)) != ERROR_SUCCESS)
1339 return err;
1341 scmdatabase_lock_exclusive(manager->db);
1343 if (group && !find_service_by_group(manager->db, group))
1345 scmdatabase_unlock(manager->db);
1346 return ERROR_SERVICE_DOES_NOT_EXIST;
1349 total_size = num_services = 0;
1350 LIST_FOR_EACH_ENTRY(service, &manager->db->services, struct service_entry, entry)
1352 if ((service->status.dwServiceType & type) && map_state(service->status.dwCurrentState, state)
1353 && match_group(service->config.lpLoadOrderGroup, group))
1355 total_size += sizeof(ENUM_SERVICE_STATUS_PROCESSW);
1356 total_size += (strlenW(service->name) + 1) * sizeof(WCHAR);
1357 if (service->config.lpDisplayName)
1359 total_size += (strlenW(service->config.lpDisplayName) + 1) * sizeof(WCHAR);
1361 num_services++;
1364 *returned = 0;
1365 *needed = total_size;
1366 if (total_size > size)
1368 scmdatabase_unlock(manager->db);
1369 return ERROR_MORE_DATA;
1371 s = (ENUM_SERVICE_STATUS_PROCESSW *)buffer;
1372 offset = num_services * sizeof(ENUM_SERVICE_STATUS_PROCESSW);
1373 LIST_FOR_EACH_ENTRY(service, &manager->db->services, struct service_entry, entry)
1375 if ((service->status.dwServiceType & type) && map_state(service->status.dwCurrentState, state)
1376 && match_group(service->config.lpLoadOrderGroup, group))
1378 sz = (strlenW(service->name) + 1) * sizeof(WCHAR);
1379 memcpy(buffer + offset, service->name, sz);
1380 s->lpServiceName = (WCHAR *)offset; /* store a buffer offset instead of a pointer */
1381 offset += sz;
1383 if (!service->config.lpDisplayName) s->lpDisplayName = NULL;
1384 else
1386 sz = (strlenW(service->config.lpDisplayName) + 1) * sizeof(WCHAR);
1387 memcpy(buffer + offset, service->config.lpDisplayName, sz);
1388 s->lpDisplayName = (WCHAR *)offset;
1389 offset += sz;
1391 s->ServiceStatusProcess = service->status;
1392 s++;
1395 *returned = num_services;
1396 *needed = 0;
1397 scmdatabase_unlock(manager->db);
1398 return ERROR_SUCCESS;
1401 DWORD __cdecl svcctl_QueryServiceObjectSecurity(void)
1403 WINE_FIXME("\n");
1404 return ERROR_CALL_NOT_IMPLEMENTED;
1407 DWORD __cdecl svcctl_SetServiceObjectSecurity(void)
1409 WINE_FIXME("\n");
1410 return ERROR_CALL_NOT_IMPLEMENTED;
1413 DWORD __cdecl svcctl_QueryServiceStatus(void)
1415 WINE_FIXME("\n");
1416 return ERROR_CALL_NOT_IMPLEMENTED;
1420 DWORD __cdecl svcctl_NotifyBootConfigStatus(void)
1422 WINE_FIXME("\n");
1423 return ERROR_CALL_NOT_IMPLEMENTED;
1426 DWORD __cdecl svcctl_SCSetServiceBitsW(void)
1428 WINE_FIXME("\n");
1429 return ERROR_CALL_NOT_IMPLEMENTED;
1433 DWORD __cdecl svcctl_EnumDependentServicesW(void)
1435 WINE_FIXME("\n");
1436 return ERROR_CALL_NOT_IMPLEMENTED;
1439 DWORD __cdecl svcctl_QueryServiceLockStatusW(void)
1441 WINE_FIXME("\n");
1442 return ERROR_CALL_NOT_IMPLEMENTED;
1445 DWORD __cdecl svcctl_SCSetServiceBitsA(void)
1447 WINE_FIXME("\n");
1448 return ERROR_CALL_NOT_IMPLEMENTED;
1451 DWORD __cdecl svcctl_ChangeServiceConfigA(void)
1453 WINE_FIXME("\n");
1454 return ERROR_CALL_NOT_IMPLEMENTED;
1457 DWORD __cdecl svcctl_CreateServiceA(void)
1459 WINE_FIXME("\n");
1460 return ERROR_CALL_NOT_IMPLEMENTED;
1463 DWORD __cdecl svcctl_EnumDependentServicesA(void)
1465 WINE_FIXME("\n");
1466 return ERROR_CALL_NOT_IMPLEMENTED;
1469 DWORD __cdecl svcctl_EnumServicesStatusA(void)
1471 WINE_FIXME("\n");
1472 return ERROR_CALL_NOT_IMPLEMENTED;
1475 DWORD __cdecl svcctl_OpenSCManagerA(void)
1477 WINE_FIXME("\n");
1478 return ERROR_CALL_NOT_IMPLEMENTED;
1481 DWORD __cdecl svcctl_OpenServiceA(void)
1483 WINE_FIXME("\n");
1484 return ERROR_CALL_NOT_IMPLEMENTED;
1487 DWORD __cdecl svcctl_QueryServiceConfigA(void)
1489 WINE_FIXME("\n");
1490 return ERROR_CALL_NOT_IMPLEMENTED;
1493 DWORD __cdecl svcctl_QueryServiceLockStatusA(void)
1495 WINE_FIXME("\n");
1496 return ERROR_CALL_NOT_IMPLEMENTED;
1499 DWORD __cdecl svcctl_StartServiceA(void)
1501 WINE_FIXME("\n");
1502 return ERROR_CALL_NOT_IMPLEMENTED;
1505 DWORD __cdecl svcctl_GetServiceDisplayNameA(void)
1507 WINE_FIXME("\n");
1508 return ERROR_CALL_NOT_IMPLEMENTED;
1511 DWORD __cdecl svcctl_GetServiceKeyNameA(void)
1513 WINE_FIXME("\n");
1514 return ERROR_CALL_NOT_IMPLEMENTED;
1517 DWORD __cdecl svcctl_GetCurrentGroupStateW(void)
1519 WINE_FIXME("\n");
1520 return ERROR_CALL_NOT_IMPLEMENTED;
1523 DWORD __cdecl svcctl_EnumServiceGroupW(void)
1525 WINE_FIXME("\n");
1526 return ERROR_CALL_NOT_IMPLEMENTED;
1529 DWORD __cdecl svcctl_ChangeServiceConfig2A(void)
1531 WINE_FIXME("\n");
1532 return ERROR_CALL_NOT_IMPLEMENTED;
1535 DWORD __cdecl svcctl_QueryServiceConfig2A(void)
1537 WINE_FIXME("\n");
1538 return ERROR_CALL_NOT_IMPLEMENTED;
1542 DWORD RPC_Init(void)
1544 WCHAR transport[] = SVCCTL_TRANSPORT;
1545 WCHAR endpoint[] = SVCCTL_ENDPOINT;
1546 DWORD err;
1548 if ((err = RpcServerUseProtseqEpW(transport, 0, endpoint, NULL)) != ERROR_SUCCESS)
1550 WINE_ERR("RpcServerUseProtseq failed with error %u\n", err);
1551 return err;
1554 if ((err = RpcServerRegisterIf(svcctl_v2_0_s_ifspec, 0, 0)) != ERROR_SUCCESS)
1556 WINE_ERR("RpcServerRegisterIf failed with error %u\n", err);
1557 return err;
1560 if ((err = RpcServerListen(1, RPC_C_LISTEN_MAX_CALLS_DEFAULT, TRUE)) != ERROR_SUCCESS)
1562 WINE_ERR("RpcServerListen failed with error %u\n", err);
1563 return err;
1565 return ERROR_SUCCESS;
1568 DWORD RPC_MainLoop(void)
1570 DWORD err;
1571 HANDLE hExitEvent = __wine_make_process_system();
1573 SetEvent(g_hStartedEvent);
1575 WINE_TRACE("Entered main loop\n");
1579 err = WaitForSingleObjectEx(hExitEvent, INFINITE, TRUE);
1580 WINE_TRACE("Wait returned %d\n", err);
1581 } while (err != WAIT_OBJECT_0);
1583 WINE_TRACE("Object signaled - wine shutdown\n");
1584 CloseHandle(hExitEvent);
1585 return ERROR_SUCCESS;
1588 void __RPC_USER SC_RPC_HANDLE_rundown(SC_RPC_HANDLE handle)
1590 SC_RPC_HANDLE_destroy(handle);
1593 void __RPC_FAR * __RPC_USER MIDL_user_allocate(SIZE_T len)
1595 return HeapAlloc(GetProcessHeap(), 0, len);
1598 void __RPC_USER MIDL_user_free(void __RPC_FAR * ptr)
1600 HeapFree(GetProcessHeap(), 0, ptr);