d3d10core: Implement d3d10_device_VSGetShaderResources().
[wine.git] / programs / services / rpc.c
blob3955e09876530d840f931922abbe7b6104736f21
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 HANDLE timeout_queue_event;
88 static CRITICAL_SECTION timeout_queue_cs;
89 static CRITICAL_SECTION_DEBUG timeout_queue_cs_debug =
91 0, 0, &timeout_queue_cs,
92 { &timeout_queue_cs_debug.ProcessLocksList, &timeout_queue_cs_debug.ProcessLocksList },
93 0, 0, { (DWORD_PTR)(__FILE__ ": timeout_queue_cs") }
95 static CRITICAL_SECTION timeout_queue_cs = { &timeout_queue_cs_debug, -1, 0, 0, 0, 0 };
96 static struct list timeout_queue = LIST_INIT(timeout_queue);
97 struct timeout_queue_elem
99 struct list entry;
101 FILETIME time;
102 void (*func)(struct service_entry*);
103 struct service_entry *service_entry;
106 static void run_after_timeout(void (*func)(struct service_entry*), struct service_entry *service, DWORD timeout)
108 struct timeout_queue_elem *elem = HeapAlloc(GetProcessHeap(), 0, sizeof(struct timeout_queue_elem));
109 ULARGE_INTEGER time;
111 if(!elem) {
112 func(service);
113 return;
116 service->ref_count++;
117 elem->func = func;
118 elem->service_entry = service;
120 GetSystemTimeAsFileTime(&elem->time);
121 time.u.LowPart = elem->time.dwLowDateTime;
122 time.u.HighPart = elem->time.dwHighDateTime;
123 time.QuadPart += timeout*10000000;
124 elem->time.dwLowDateTime = time.u.LowPart;
125 elem->time.dwHighDateTime = time.u.HighPart;
127 EnterCriticalSection(&timeout_queue_cs);
128 list_add_head(&timeout_queue, &elem->entry);
129 LeaveCriticalSection(&timeout_queue_cs);
131 SetEvent(timeout_queue_event);
134 static void free_service_strings(struct service_entry *old, struct service_entry *new)
136 QUERY_SERVICE_CONFIGW *old_cfg = &old->config;
137 QUERY_SERVICE_CONFIGW *new_cfg = &new->config;
139 if (old_cfg->lpBinaryPathName != new_cfg->lpBinaryPathName)
140 HeapFree(GetProcessHeap(), 0, old_cfg->lpBinaryPathName);
142 if (old_cfg->lpLoadOrderGroup != new_cfg->lpLoadOrderGroup)
143 HeapFree(GetProcessHeap(), 0, old_cfg->lpLoadOrderGroup);
145 if (old_cfg->lpServiceStartName != new_cfg->lpServiceStartName)
146 HeapFree(GetProcessHeap(), 0, old_cfg->lpServiceStartName);
148 if (old_cfg->lpDisplayName != new_cfg->lpDisplayName)
149 HeapFree(GetProcessHeap(), 0, old_cfg->lpDisplayName);
151 if (old->dependOnServices != new->dependOnServices)
152 HeapFree(GetProcessHeap(), 0, old->dependOnServices);
154 if (old->dependOnGroups != new->dependOnGroups)
155 HeapFree(GetProcessHeap(), 0, old->dependOnGroups);
158 /* Check if the given handle is of the required type and allows the requested access. */
159 static DWORD validate_context_handle(SC_RPC_HANDLE handle, DWORD type, DWORD needed_access, struct sc_handle **out_hdr)
161 struct sc_handle *hdr = handle;
163 if (type != SC_HTYPE_DONT_CARE && hdr->type != type)
165 WINE_ERR("Handle is of an invalid type (%d, %d)\n", hdr->type, type);
166 return ERROR_INVALID_HANDLE;
169 if ((needed_access & hdr->access) != needed_access)
171 WINE_ERR("Access denied - handle created with access %x, needed %x\n", hdr->access, needed_access);
172 return ERROR_ACCESS_DENIED;
175 *out_hdr = hdr;
176 return ERROR_SUCCESS;
179 static DWORD validate_scm_handle(SC_RPC_HANDLE handle, DWORD needed_access, struct sc_manager_handle **manager)
181 struct sc_handle *hdr;
182 DWORD err = validate_context_handle(handle, SC_HTYPE_MANAGER, needed_access, &hdr);
183 if (err == ERROR_SUCCESS)
184 *manager = (struct sc_manager_handle *)hdr;
185 return err;
188 static DWORD validate_service_handle(SC_RPC_HANDLE handle, DWORD needed_access, struct sc_service_handle **service)
190 struct sc_handle *hdr;
191 DWORD err = validate_context_handle(handle, SC_HTYPE_SERVICE, needed_access, &hdr);
192 if (err == ERROR_SUCCESS)
193 *service = (struct sc_service_handle *)hdr;
194 return err;
197 DWORD __cdecl svcctl_OpenSCManagerW(
198 MACHINE_HANDLEW MachineName, /* Note: this parameter is ignored */
199 LPCWSTR DatabaseName,
200 DWORD dwAccessMask,
201 SC_RPC_HANDLE *handle)
203 struct sc_manager_handle *manager;
205 WINE_TRACE("(%s, %s, %x)\n", wine_dbgstr_w(MachineName), wine_dbgstr_w(DatabaseName), dwAccessMask);
207 if (DatabaseName != NULL && DatabaseName[0])
209 if (strcmpW(DatabaseName, SERVICES_FAILED_DATABASEW) == 0)
210 return ERROR_DATABASE_DOES_NOT_EXIST;
211 if (strcmpW(DatabaseName, SERVICES_ACTIVE_DATABASEW) != 0)
212 return ERROR_INVALID_NAME;
215 if (!(manager = HeapAlloc(GetProcessHeap(), 0, sizeof(*manager))))
216 return ERROR_NOT_ENOUGH_SERVER_MEMORY;
218 manager->hdr.type = SC_HTYPE_MANAGER;
220 if (dwAccessMask & MAXIMUM_ALLOWED)
221 dwAccessMask |= SC_MANAGER_ALL_ACCESS;
222 manager->hdr.access = dwAccessMask;
223 RtlMapGenericMask(&manager->hdr.access, &g_scm_generic);
224 manager->db = active_database;
225 *handle = &manager->hdr;
227 return ERROR_SUCCESS;
230 static void SC_RPC_HANDLE_destroy(SC_RPC_HANDLE handle)
232 struct sc_handle *hdr = handle;
233 switch (hdr->type)
235 case SC_HTYPE_MANAGER:
237 struct sc_manager_handle *manager = (struct sc_manager_handle *)hdr;
238 HeapFree(GetProcessHeap(), 0, manager);
239 break;
241 case SC_HTYPE_SERVICE:
243 struct sc_service_handle *service = (struct sc_service_handle *)hdr;
244 release_service(service->service_entry);
245 HeapFree(GetProcessHeap(), 0, service);
246 break;
248 default:
249 WINE_ERR("invalid handle type %d\n", hdr->type);
250 RpcRaiseException(ERROR_INVALID_HANDLE);
254 DWORD __cdecl svcctl_GetServiceDisplayNameW(
255 SC_RPC_HANDLE hSCManager,
256 LPCWSTR lpServiceName,
257 WCHAR *lpBuffer,
258 DWORD *cchBufSize)
260 struct sc_manager_handle *manager;
261 struct service_entry *entry;
262 DWORD err;
264 WINE_TRACE("(%s, %d)\n", wine_dbgstr_w(lpServiceName), *cchBufSize);
266 if ((err = validate_scm_handle(hSCManager, 0, &manager)) != ERROR_SUCCESS)
267 return err;
269 scmdatabase_lock_shared(manager->db);
271 entry = scmdatabase_find_service(manager->db, lpServiceName);
272 if (entry != NULL)
274 LPCWSTR name;
275 int len;
276 service_lock_shared(entry);
277 name = get_display_name(entry);
278 len = strlenW(name);
279 if (len <= *cchBufSize)
281 err = ERROR_SUCCESS;
282 memcpy(lpBuffer, name, (len + 1)*sizeof(*name));
284 else
285 err = ERROR_INSUFFICIENT_BUFFER;
286 *cchBufSize = len;
287 service_unlock(entry);
289 else
290 err = ERROR_SERVICE_DOES_NOT_EXIST;
292 scmdatabase_unlock(manager->db);
294 if (err != ERROR_SUCCESS)
295 lpBuffer[0] = 0;
297 return err;
300 DWORD __cdecl svcctl_GetServiceKeyNameW(
301 SC_RPC_HANDLE hSCManager,
302 LPCWSTR lpServiceDisplayName,
303 WCHAR *lpBuffer,
304 DWORD *cchBufSize)
306 struct service_entry *entry;
307 struct sc_manager_handle *manager;
308 DWORD err;
310 WINE_TRACE("(%s, %d)\n", wine_dbgstr_w(lpServiceDisplayName), *cchBufSize);
312 if ((err = validate_scm_handle(hSCManager, 0, &manager)) != ERROR_SUCCESS)
313 return err;
315 scmdatabase_lock_shared(manager->db);
317 entry = scmdatabase_find_service_by_displayname(manager->db, lpServiceDisplayName);
318 if (entry != NULL)
320 int len;
321 service_lock_shared(entry);
322 len = strlenW(entry->name);
323 if (len <= *cchBufSize)
325 err = ERROR_SUCCESS;
326 memcpy(lpBuffer, entry->name, (len + 1)*sizeof(*entry->name));
328 else
329 err = ERROR_INSUFFICIENT_BUFFER;
330 *cchBufSize = len;
331 service_unlock(entry);
333 else
334 err = ERROR_SERVICE_DOES_NOT_EXIST;
336 scmdatabase_unlock(manager->db);
338 if (err != ERROR_SUCCESS)
339 lpBuffer[0] = 0;
341 return err;
344 static DWORD create_handle_for_service(struct service_entry *entry, DWORD dwDesiredAccess, SC_RPC_HANDLE *phService)
346 struct sc_service_handle *service;
348 if (!(service = HeapAlloc(GetProcessHeap(), 0, sizeof(*service))))
350 release_service(entry);
351 return ERROR_NOT_ENOUGH_SERVER_MEMORY;
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;
358 if (dwDesiredAccess & MAXIMUM_ALLOWED)
359 dwDesiredAccess |= SERVICE_ALL_ACCESS;
361 *phService = &service->hdr;
362 return ERROR_SUCCESS;
365 DWORD __cdecl svcctl_OpenServiceW(
366 SC_RPC_HANDLE hSCManager,
367 LPCWSTR lpServiceName,
368 DWORD dwDesiredAccess,
369 SC_RPC_HANDLE *phService)
371 struct sc_manager_handle *manager;
372 struct service_entry *entry;
373 DWORD err;
375 WINE_TRACE("(%s, 0x%x)\n", wine_dbgstr_w(lpServiceName), dwDesiredAccess);
377 if ((err = validate_scm_handle(hSCManager, 0, &manager)) != ERROR_SUCCESS)
378 return err;
379 if (!validate_service_name(lpServiceName))
380 return ERROR_INVALID_NAME;
382 scmdatabase_lock_shared(manager->db);
383 entry = scmdatabase_find_service(manager->db, lpServiceName);
384 if (entry != NULL)
385 InterlockedIncrement(&entry->ref_count);
386 scmdatabase_unlock(manager->db);
388 if (entry == NULL)
389 return ERROR_SERVICE_DOES_NOT_EXIST;
391 return create_handle_for_service(entry, dwDesiredAccess, phService);
394 static DWORD parse_dependencies(const WCHAR *dependencies, struct service_entry *entry)
396 WCHAR *services = NULL, *groups, *s;
397 DWORD len, len_services = 0, len_groups = 0;
398 const WCHAR *ptr = dependencies;
400 if (!dependencies || !dependencies[0])
402 entry->dependOnServices = NULL;
403 entry->dependOnGroups = NULL;
404 return ERROR_SUCCESS;
407 while (*ptr)
409 len = strlenW(ptr) + 1;
410 if (ptr[0] == '+' && ptr[1])
411 len_groups += len - 1;
412 else
413 len_services += len;
414 ptr += len;
416 if (!len_services) entry->dependOnServices = NULL;
417 else
419 services = HeapAlloc(GetProcessHeap(), 0, (len_services + 1) * sizeof(WCHAR));
420 if (!services)
421 return ERROR_OUTOFMEMORY;
423 s = services;
424 ptr = dependencies;
425 while (*ptr)
427 len = strlenW(ptr) + 1;
428 if (*ptr != '+')
430 strcpyW(s, ptr);
431 s += len;
433 ptr += len;
435 *s = 0;
436 entry->dependOnServices = services;
438 if (!len_groups) entry->dependOnGroups = NULL;
439 else
441 groups = HeapAlloc(GetProcessHeap(), 0, (len_groups + 1) * sizeof(WCHAR));
442 if (!groups)
444 HeapFree(GetProcessHeap(), 0, services);
445 return ERROR_OUTOFMEMORY;
447 s = groups;
448 ptr = dependencies;
449 while (*ptr)
451 len = strlenW(ptr) + 1;
452 if (ptr[0] == '+' && ptr[1])
454 strcpyW(s, ptr + 1);
455 s += len - 1;
457 ptr += len;
459 *s = 0;
460 entry->dependOnGroups = groups;
463 return ERROR_SUCCESS;
466 DWORD __cdecl svcctl_CreateServiceW(
467 SC_RPC_HANDLE hSCManager,
468 LPCWSTR lpServiceName,
469 LPCWSTR lpDisplayName,
470 DWORD dwDesiredAccess,
471 DWORD dwServiceType,
472 DWORD dwStartType,
473 DWORD dwErrorControl,
474 LPCWSTR lpBinaryPathName,
475 LPCWSTR lpLoadOrderGroup,
476 DWORD *lpdwTagId,
477 const BYTE *lpDependencies,
478 DWORD dwDependenciesSize,
479 LPCWSTR lpServiceStartName,
480 const BYTE *lpPassword,
481 DWORD dwPasswordSize,
482 SC_RPC_HANDLE *phService)
484 struct service_entry *entry, *found;
485 struct sc_manager_handle *manager;
486 DWORD err;
488 WINE_TRACE("(%s, %s, 0x%x, %s)\n", wine_dbgstr_w(lpServiceName), wine_dbgstr_w(lpDisplayName), dwDesiredAccess, wine_dbgstr_w(lpBinaryPathName));
490 if ((err = validate_scm_handle(hSCManager, SC_MANAGER_CREATE_SERVICE, &manager)) != ERROR_SUCCESS)
491 return err;
493 if (!validate_service_name(lpServiceName))
494 return ERROR_INVALID_NAME;
495 if (!check_multisz((LPCWSTR)lpDependencies, dwDependenciesSize) || !lpServiceName[0] || !lpBinaryPathName[0])
496 return ERROR_INVALID_PARAMETER;
498 if (lpPassword)
499 WINE_FIXME("Don't know how to add a password\n"); /* I always get ERROR_GEN_FAILURE */
501 err = service_create(lpServiceName, &entry);
502 if (err != ERROR_SUCCESS)
503 return err;
505 err = parse_dependencies((LPCWSTR)lpDependencies, entry);
506 if (err != ERROR_SUCCESS) {
507 free_service_entry(entry);
508 return err;
511 entry->ref_count = 1;
512 entry->config.dwServiceType = entry->status.dwServiceType = dwServiceType;
513 entry->config.dwStartType = dwStartType;
514 entry->config.dwErrorControl = dwErrorControl;
515 entry->config.lpBinaryPathName = strdupW(lpBinaryPathName);
516 entry->config.lpLoadOrderGroup = strdupW(lpLoadOrderGroup);
517 entry->config.lpServiceStartName = strdupW(lpServiceStartName);
518 entry->config.lpDisplayName = strdupW(lpDisplayName);
520 if (lpdwTagId) /* TODO: In most situations a non-NULL TagId will generate an ERROR_INVALID_PARAMETER. */
521 entry->config.dwTagId = *lpdwTagId;
522 else
523 entry->config.dwTagId = 0;
525 /* other fields NULL*/
527 if (!validate_service_config(entry))
529 WINE_ERR("Invalid data while trying to create service\n");
530 free_service_entry(entry);
531 return ERROR_INVALID_PARAMETER;
534 scmdatabase_lock_exclusive(manager->db);
536 if ((found = scmdatabase_find_service(manager->db, lpServiceName)))
538 service_lock_exclusive(found);
539 err = is_marked_for_delete(found) ? ERROR_SERVICE_MARKED_FOR_DELETE : ERROR_SERVICE_EXISTS;
540 service_unlock(found);
541 scmdatabase_unlock(manager->db);
542 free_service_entry(entry);
543 return err;
546 if (scmdatabase_find_service_by_displayname(manager->db, get_display_name(entry)))
548 scmdatabase_unlock(manager->db);
549 free_service_entry(entry);
550 return ERROR_DUPLICATE_SERVICE_NAME;
553 err = scmdatabase_add_service(manager->db, entry);
554 if (err != ERROR_SUCCESS)
556 scmdatabase_unlock(manager->db);
557 free_service_entry(entry);
558 return err;
560 scmdatabase_unlock(manager->db);
562 return create_handle_for_service(entry, dwDesiredAccess, phService);
565 DWORD __cdecl svcctl_DeleteService(
566 SC_RPC_HANDLE hService)
568 struct sc_service_handle *service;
569 DWORD err;
571 if ((err = validate_service_handle(hService, DELETE, &service)) != ERROR_SUCCESS)
572 return err;
574 service_lock_exclusive(service->service_entry);
576 if (!is_marked_for_delete(service->service_entry))
577 err = mark_for_delete(service->service_entry);
578 else
579 err = ERROR_SERVICE_MARKED_FOR_DELETE;
581 service_unlock(service->service_entry);
583 return err;
586 DWORD __cdecl svcctl_QueryServiceConfigW(
587 SC_RPC_HANDLE hService,
588 QUERY_SERVICE_CONFIGW *config)
590 struct sc_service_handle *service;
591 DWORD err;
593 WINE_TRACE("(%p)\n", config);
595 if ((err = validate_service_handle(hService, SERVICE_QUERY_CONFIG, &service)) != 0)
596 return err;
598 service_lock_shared(service->service_entry);
599 config->dwServiceType = service->service_entry->config.dwServiceType;
600 config->dwStartType = service->service_entry->config.dwStartType;
601 config->dwErrorControl = service->service_entry->config.dwErrorControl;
602 config->lpBinaryPathName = strdupW(service->service_entry->config.lpBinaryPathName);
603 config->lpLoadOrderGroup = strdupW(service->service_entry->config.lpLoadOrderGroup);
604 config->dwTagId = service->service_entry->config.dwTagId;
605 config->lpDependencies = NULL; /* TODO */
606 config->lpServiceStartName = strdupW(service->service_entry->config.lpServiceStartName);
607 config->lpDisplayName = strdupW(service->service_entry->config.lpDisplayName);
608 service_unlock(service->service_entry);
610 return ERROR_SUCCESS;
613 DWORD __cdecl svcctl_ChangeServiceConfigW(
614 SC_RPC_HANDLE hService,
615 DWORD dwServiceType,
616 DWORD dwStartType,
617 DWORD dwErrorControl,
618 LPCWSTR lpBinaryPathName,
619 LPCWSTR lpLoadOrderGroup,
620 DWORD *lpdwTagId,
621 const BYTE *lpDependencies,
622 DWORD dwDependenciesSize,
623 LPCWSTR lpServiceStartName,
624 const BYTE *lpPassword,
625 DWORD dwPasswordSize,
626 LPCWSTR lpDisplayName)
628 struct service_entry new_entry, *entry;
629 struct sc_service_handle *service;
630 DWORD err;
632 WINE_TRACE("\n");
634 if ((err = validate_service_handle(hService, SERVICE_CHANGE_CONFIG, &service)) != 0)
635 return err;
637 if (!check_multisz((LPCWSTR)lpDependencies, dwDependenciesSize))
638 return ERROR_INVALID_PARAMETER;
640 /* first check if the new configuration is correct */
641 service_lock_exclusive(service->service_entry);
643 if (is_marked_for_delete(service->service_entry))
645 service_unlock(service->service_entry);
646 return ERROR_SERVICE_MARKED_FOR_DELETE;
649 if (lpDisplayName != NULL &&
650 (entry = scmdatabase_find_service_by_displayname(service->service_entry->db, lpDisplayName)) &&
651 (entry != service->service_entry))
653 service_unlock(service->service_entry);
654 return ERROR_DUPLICATE_SERVICE_NAME;
657 new_entry = *service->service_entry;
659 if (dwServiceType != SERVICE_NO_CHANGE)
660 new_entry.config.dwServiceType = dwServiceType;
662 if (dwStartType != SERVICE_NO_CHANGE)
663 new_entry.config.dwStartType = dwStartType;
665 if (dwErrorControl != SERVICE_NO_CHANGE)
666 new_entry.config.dwErrorControl = dwErrorControl;
668 if (lpBinaryPathName != NULL)
669 new_entry.config.lpBinaryPathName = (LPWSTR)lpBinaryPathName;
671 if (lpLoadOrderGroup != NULL)
672 new_entry.config.lpLoadOrderGroup = (LPWSTR)lpLoadOrderGroup;
674 if (lpdwTagId != NULL)
675 WINE_FIXME("Changing tag id not supported\n");
677 if (lpServiceStartName != NULL)
678 new_entry.config.lpServiceStartName = (LPWSTR)lpServiceStartName;
680 if (lpPassword != NULL)
681 WINE_FIXME("Setting password not supported\n");
683 if (lpDisplayName != NULL)
684 new_entry.config.lpDisplayName = (LPWSTR)lpDisplayName;
686 err = parse_dependencies((LPCWSTR)lpDependencies, &new_entry);
687 if (err != ERROR_SUCCESS)
689 service_unlock(service->service_entry);
690 return err;
693 if (!validate_service_config(&new_entry))
695 WINE_ERR("The configuration after the change wouldn't be valid\n");
696 service_unlock(service->service_entry);
697 return ERROR_INVALID_PARAMETER;
700 /* configuration OK. The strings needs to be duplicated */
701 if (lpBinaryPathName != NULL)
702 new_entry.config.lpBinaryPathName = strdupW(lpBinaryPathName);
704 if (lpLoadOrderGroup != NULL)
705 new_entry.config.lpLoadOrderGroup = strdupW(lpLoadOrderGroup);
707 if (lpServiceStartName != NULL)
708 new_entry.config.lpServiceStartName = strdupW(lpServiceStartName);
710 if (lpDisplayName != NULL)
711 new_entry.config.lpDisplayName = strdupW(lpDisplayName);
713 /* try to save to Registry, commit or rollback depending on success */
714 err = save_service_config(&new_entry);
715 if (ERROR_SUCCESS == err)
717 free_service_strings(service->service_entry, &new_entry);
718 *service->service_entry = new_entry;
720 else free_service_strings(&new_entry, service->service_entry);
721 service_unlock(service->service_entry);
723 return err;
726 DWORD __cdecl svcctl_SetServiceStatus(
727 SC_RPC_HANDLE hServiceStatus,
728 LPSERVICE_STATUS lpServiceStatus)
730 struct sc_service_handle *service;
731 DWORD err;
733 WINE_TRACE("(%p, %p)\n", hServiceStatus, lpServiceStatus);
735 if ((err = validate_service_handle(hServiceStatus, SERVICE_SET_STATUS, &service)) != 0)
736 return err;
738 service_lock_exclusive(service->service_entry);
739 /* FIXME: be a bit more discriminant about what parts of the status we set
740 * and check that fields are valid */
741 service->service_entry->status.dwServiceType = lpServiceStatus->dwServiceType;
742 service->service_entry->status.dwCurrentState = lpServiceStatus->dwCurrentState;
743 service->service_entry->status.dwControlsAccepted = lpServiceStatus->dwControlsAccepted;
744 service->service_entry->status.dwWin32ExitCode = lpServiceStatus->dwWin32ExitCode;
745 service->service_entry->status.dwServiceSpecificExitCode = lpServiceStatus->dwServiceSpecificExitCode;
746 service->service_entry->status.dwCheckPoint = lpServiceStatus->dwCheckPoint;
747 service->service_entry->status.dwWaitHint = lpServiceStatus->dwWaitHint;
748 service_unlock(service->service_entry);
750 if (lpServiceStatus->dwCurrentState == SERVICE_STOPPED)
751 run_after_timeout(service_terminate, service->service_entry, service_kill_timeout);
752 else if (service->service_entry->status_changed_event)
753 SetEvent(service->service_entry->status_changed_event);
755 return ERROR_SUCCESS;
758 DWORD __cdecl svcctl_ChangeServiceConfig2W( SC_RPC_HANDLE hService, DWORD level, SERVICE_CONFIG2W *config )
760 struct sc_service_handle *service;
761 DWORD err;
763 if ((err = validate_service_handle(hService, SERVICE_CHANGE_CONFIG, &service)) != 0)
764 return err;
766 switch (level)
768 case SERVICE_CONFIG_DESCRIPTION:
770 WCHAR *descr = NULL;
772 if (config->descr.lpDescription[0])
774 if (!(descr = strdupW( config->descr.lpDescription )))
775 return ERROR_NOT_ENOUGH_MEMORY;
778 WINE_TRACE( "changing service %p descr to %s\n", service, wine_dbgstr_w(descr) );
779 service_lock_exclusive( service->service_entry );
780 HeapFree( GetProcessHeap(), 0, service->service_entry->description );
781 service->service_entry->description = descr;
782 save_service_config( service->service_entry );
783 service_unlock( service->service_entry );
785 break;
786 case SERVICE_CONFIG_FAILURE_ACTIONS:
787 WINE_FIXME( "SERVICE_CONFIG_FAILURE_ACTIONS not implemented: period %u msg %s cmd %s\n",
788 config->actions.dwResetPeriod,
789 wine_dbgstr_w(config->actions.lpRebootMsg),
790 wine_dbgstr_w(config->actions.lpCommand) );
791 break;
792 case SERVICE_CONFIG_PRESHUTDOWN_INFO:
793 WINE_TRACE( "changing service %p preshutdown timeout to %d\n",
794 service, config->preshutdown.dwPreshutdownTimeout );
795 service_lock_exclusive( service->service_entry );
796 service->service_entry->preshutdown_timeout = config->preshutdown.dwPreshutdownTimeout;
797 save_service_config( service->service_entry );
798 service_unlock( service->service_entry );
799 break;
800 default:
801 WINE_FIXME("level %u not implemented\n", level);
802 err = ERROR_INVALID_LEVEL;
803 break;
805 return err;
808 DWORD __cdecl svcctl_QueryServiceConfig2W( SC_RPC_HANDLE hService, DWORD level,
809 BYTE *buffer, DWORD size, LPDWORD needed )
811 struct sc_service_handle *service;
812 DWORD err;
814 memset(buffer, 0, size);
816 if ((err = validate_service_handle(hService, SERVICE_QUERY_STATUS, &service)) != 0)
817 return err;
819 switch (level)
821 case SERVICE_CONFIG_DESCRIPTION:
823 SERVICE_DESCRIPTIONW *descr = (SERVICE_DESCRIPTIONW *)buffer;
825 service_lock_shared(service->service_entry);
826 *needed = sizeof(*descr);
827 if (service->service_entry->description)
828 *needed += (strlenW(service->service_entry->description) + 1) * sizeof(WCHAR);
829 if (size >= *needed)
831 if (service->service_entry->description)
833 /* store a buffer offset instead of a pointer */
834 descr->lpDescription = (WCHAR *)((BYTE *)(descr + 1) - buffer);
835 strcpyW( (WCHAR *)(descr + 1), service->service_entry->description );
837 else descr->lpDescription = NULL;
839 else err = ERROR_INSUFFICIENT_BUFFER;
840 service_unlock(service->service_entry);
842 break;
844 case SERVICE_CONFIG_PRESHUTDOWN_INFO:
845 service_lock_shared(service->service_entry);
847 *needed = sizeof(SERVICE_PRESHUTDOWN_INFO);
848 if (size >= *needed)
849 ((LPSERVICE_PRESHUTDOWN_INFO)buffer)->dwPreshutdownTimeout =
850 service->service_entry->preshutdown_timeout;
851 else err = ERROR_INSUFFICIENT_BUFFER;
853 service_unlock(service->service_entry);
854 break;
856 default:
857 WINE_FIXME("level %u not implemented\n", level);
858 err = ERROR_INVALID_LEVEL;
859 break;
861 return err;
864 DWORD __cdecl svcctl_QueryServiceStatusEx(
865 SC_RPC_HANDLE hService,
866 SC_STATUS_TYPE InfoLevel,
867 BYTE *lpBuffer,
868 DWORD cbBufSize,
869 LPDWORD pcbBytesNeeded)
871 struct sc_service_handle *service;
872 DWORD err;
873 LPSERVICE_STATUS_PROCESS pSvcStatusData;
875 memset(lpBuffer, 0, cbBufSize);
877 if ((err = validate_service_handle(hService, SERVICE_QUERY_STATUS, &service)) != 0)
878 return err;
880 if (InfoLevel != SC_STATUS_PROCESS_INFO)
881 return ERROR_INVALID_LEVEL;
883 pSvcStatusData = (LPSERVICE_STATUS_PROCESS) lpBuffer;
884 if (pSvcStatusData == NULL)
885 return ERROR_INVALID_PARAMETER;
887 if (cbBufSize < sizeof(SERVICE_STATUS_PROCESS))
889 if( pcbBytesNeeded != NULL)
890 *pcbBytesNeeded = sizeof(SERVICE_STATUS_PROCESS);
892 return ERROR_INSUFFICIENT_BUFFER;
895 service_lock_shared(service->service_entry);
897 pSvcStatusData->dwServiceType = service->service_entry->status.dwServiceType;
898 pSvcStatusData->dwCurrentState = service->service_entry->status.dwCurrentState;
899 pSvcStatusData->dwControlsAccepted = service->service_entry->status.dwControlsAccepted;
900 pSvcStatusData->dwWin32ExitCode = service->service_entry->status.dwWin32ExitCode;
901 pSvcStatusData->dwServiceSpecificExitCode = service->service_entry->status.dwServiceSpecificExitCode;
902 pSvcStatusData->dwCheckPoint = service->service_entry->status.dwCheckPoint;
903 pSvcStatusData->dwWaitHint = service->service_entry->status.dwWaitHint;
904 pSvcStatusData->dwProcessId = service->service_entry->status.dwProcessId;
905 pSvcStatusData->dwServiceFlags = service->service_entry->status.dwServiceFlags;
907 service_unlock(service->service_entry);
909 return ERROR_SUCCESS;
912 /******************************************************************************
913 * service_accepts_control
915 static BOOL service_accepts_control(const struct service_entry *service, DWORD dwControl)
917 DWORD a = service->status.dwControlsAccepted;
919 switch (dwControl)
921 case SERVICE_CONTROL_INTERROGATE:
922 return TRUE;
923 case SERVICE_CONTROL_STOP:
924 if (a&SERVICE_ACCEPT_STOP)
925 return TRUE;
926 break;
927 case SERVICE_CONTROL_SHUTDOWN:
928 if (a&SERVICE_ACCEPT_SHUTDOWN)
929 return TRUE;
930 break;
931 case SERVICE_CONTROL_PAUSE:
932 case SERVICE_CONTROL_CONTINUE:
933 if (a&SERVICE_ACCEPT_PAUSE_CONTINUE)
934 return TRUE;
935 break;
936 case SERVICE_CONTROL_PARAMCHANGE:
937 if (a&SERVICE_ACCEPT_PARAMCHANGE)
938 return TRUE;
939 break;
940 case SERVICE_CONTROL_NETBINDADD:
941 case SERVICE_CONTROL_NETBINDREMOVE:
942 case SERVICE_CONTROL_NETBINDENABLE:
943 case SERVICE_CONTROL_NETBINDDISABLE:
944 if (a&SERVICE_ACCEPT_NETBINDCHANGE)
945 return TRUE;
946 case SERVICE_CONTROL_HARDWAREPROFILECHANGE:
947 if (a&SERVICE_ACCEPT_HARDWAREPROFILECHANGE)
948 return TRUE;
949 break;
950 case SERVICE_CONTROL_POWEREVENT:
951 if (a&SERVICE_ACCEPT_POWEREVENT)
952 return TRUE;
953 break;
954 case SERVICE_CONTROL_SESSIONCHANGE:
955 if (a&SERVICE_ACCEPT_SESSIONCHANGE)
956 return TRUE;
957 break;
959 return FALSE;
962 /******************************************************************************
963 * service_send_command
965 BOOL service_send_command( struct service_entry *service, HANDLE pipe,
966 const void *data, DWORD size, DWORD *result )
968 OVERLAPPED overlapped;
969 DWORD count, ret;
970 BOOL r;
972 overlapped.hEvent = service->overlapped_event;
973 r = WriteFile(pipe, data, size, &count, &overlapped);
974 if (!r && GetLastError() == ERROR_IO_PENDING)
976 ret = WaitForSingleObject( service->overlapped_event, service_pipe_timeout );
977 if (ret == WAIT_TIMEOUT)
979 WINE_ERR("sending command timed out\n");
980 *result = ERROR_SERVICE_REQUEST_TIMEOUT;
981 return FALSE;
983 r = GetOverlappedResult( pipe, &overlapped, &count, FALSE );
985 if (!r || count != size)
987 WINE_ERR("service protocol error - failed to write pipe!\n");
988 *result = (!r ? GetLastError() : ERROR_WRITE_FAULT);
989 return FALSE;
991 r = ReadFile(pipe, result, sizeof *result, &count, &overlapped);
992 if (!r && GetLastError() == ERROR_IO_PENDING)
994 ret = WaitForSingleObject( service->overlapped_event, service_pipe_timeout );
995 if (ret == WAIT_TIMEOUT)
997 WINE_ERR("receiving command result timed out\n");
998 *result = ERROR_SERVICE_REQUEST_TIMEOUT;
999 return FALSE;
1001 r = GetOverlappedResult( pipe, &overlapped, &count, FALSE );
1003 if (!r || count != sizeof *result)
1005 WINE_ERR("service protocol error - failed to read pipe "
1006 "r = %d count = %d!\n", r, count);
1007 *result = (!r ? GetLastError() : ERROR_READ_FAULT);
1008 return FALSE;
1011 *result = ERROR_SUCCESS;
1012 return TRUE;
1015 /******************************************************************************
1016 * service_send_control
1018 static BOOL service_send_control(struct service_entry *service, HANDLE pipe, DWORD dwControl, DWORD *result)
1020 service_start_info *ssi;
1021 DWORD len;
1022 BOOL r;
1024 /* calculate how much space we need to send the startup info */
1025 len = strlenW(service->name) + 1;
1027 ssi = HeapAlloc(GetProcessHeap(),0,FIELD_OFFSET(service_start_info, data[len]));
1028 ssi->cmd = WINESERV_SENDCONTROL;
1029 ssi->control = dwControl;
1030 ssi->total_size = FIELD_OFFSET(service_start_info, data[len]);
1031 ssi->name_size = strlenW(service->name) + 1;
1032 strcpyW( ssi->data, service->name );
1034 r = service_send_command( service, pipe, ssi, ssi->total_size, result );
1035 HeapFree( GetProcessHeap(), 0, ssi );
1036 return r;
1039 DWORD __cdecl svcctl_StartServiceW(
1040 SC_RPC_HANDLE hService,
1041 DWORD dwNumServiceArgs,
1042 LPCWSTR *lpServiceArgVectors)
1044 struct sc_service_handle *service;
1045 DWORD err;
1047 WINE_TRACE("(%p, %d, %p)\n", hService, dwNumServiceArgs, lpServiceArgVectors);
1049 if ((err = validate_service_handle(hService, SERVICE_START, &service)) != 0)
1050 return err;
1052 if (service->service_entry->config.dwStartType == SERVICE_DISABLED)
1053 return ERROR_SERVICE_DISABLED;
1055 err = service_start(service->service_entry, dwNumServiceArgs, lpServiceArgVectors);
1057 return err;
1060 DWORD __cdecl svcctl_ControlService(
1061 SC_RPC_HANDLE hService,
1062 DWORD dwControl,
1063 SERVICE_STATUS *lpServiceStatus)
1065 DWORD access_required;
1066 struct sc_service_handle *service;
1067 DWORD result;
1068 BOOL ret;
1069 HANDLE control_mutex;
1071 WINE_TRACE("(%p, %d, %p)\n", hService, dwControl, lpServiceStatus);
1073 switch (dwControl)
1075 case SERVICE_CONTROL_CONTINUE:
1076 case SERVICE_CONTROL_NETBINDADD:
1077 case SERVICE_CONTROL_NETBINDDISABLE:
1078 case SERVICE_CONTROL_NETBINDENABLE:
1079 case SERVICE_CONTROL_NETBINDREMOVE:
1080 case SERVICE_CONTROL_PARAMCHANGE:
1081 case SERVICE_CONTROL_PAUSE:
1082 access_required = SERVICE_PAUSE_CONTINUE;
1083 break;
1084 case SERVICE_CONTROL_INTERROGATE:
1085 access_required = SERVICE_INTERROGATE;
1086 break;
1087 case SERVICE_CONTROL_STOP:
1088 access_required = SERVICE_STOP;
1089 break;
1090 default:
1091 if (dwControl >= 128 && dwControl <= 255)
1092 access_required = SERVICE_USER_DEFINED_CONTROL;
1093 else
1094 return ERROR_INVALID_PARAMETER;
1097 if ((result = validate_service_handle(hService, access_required, &service)) != 0)
1098 return result;
1100 service_lock_exclusive(service->service_entry);
1102 result = ERROR_SUCCESS;
1103 switch (service->service_entry->status.dwCurrentState)
1105 case SERVICE_STOPPED:
1106 result = ERROR_SERVICE_NOT_ACTIVE;
1107 break;
1108 case SERVICE_START_PENDING:
1109 if (dwControl==SERVICE_CONTROL_STOP)
1110 break;
1111 /* fall through */
1112 case SERVICE_STOP_PENDING:
1113 result = ERROR_SERVICE_CANNOT_ACCEPT_CTRL;
1114 break;
1117 if (result==ERROR_SUCCESS && !service->service_entry->control_mutex) {
1118 result = ERROR_SERVICE_CANNOT_ACCEPT_CTRL;
1119 service_terminate(service->service_entry);
1122 if (result != ERROR_SUCCESS)
1124 if (lpServiceStatus)
1126 lpServiceStatus->dwServiceType = service->service_entry->status.dwServiceType;
1127 lpServiceStatus->dwCurrentState = service->service_entry->status.dwCurrentState;
1128 lpServiceStatus->dwControlsAccepted = service->service_entry->status.dwControlsAccepted;
1129 lpServiceStatus->dwWin32ExitCode = service->service_entry->status.dwWin32ExitCode;
1130 lpServiceStatus->dwServiceSpecificExitCode = service->service_entry->status.dwServiceSpecificExitCode;
1131 lpServiceStatus->dwCheckPoint = service->service_entry->status.dwCheckPoint;
1132 lpServiceStatus->dwWaitHint = service->service_entry->status.dwWaitHint;
1134 service_unlock(service->service_entry);
1135 return result;
1138 if (!service_accepts_control(service->service_entry, dwControl))
1140 service_unlock(service->service_entry);
1141 return ERROR_INVALID_SERVICE_CONTROL;
1144 /* prevent races by caching control_mutex and clearing it on
1145 * stop instead of outside the services lock */
1146 control_mutex = service->service_entry->control_mutex;
1147 if (dwControl == SERVICE_CONTROL_STOP)
1148 service->service_entry->control_mutex = NULL;
1150 service_unlock(service->service_entry);
1152 ret = WaitForSingleObject(control_mutex, 30000);
1153 if (ret == WAIT_OBJECT_0)
1155 service_send_control(service->service_entry, service->service_entry->control_pipe,
1156 dwControl, &result);
1158 if (lpServiceStatus)
1160 service_lock_shared(service->service_entry);
1161 lpServiceStatus->dwServiceType = service->service_entry->status.dwServiceType;
1162 lpServiceStatus->dwCurrentState = service->service_entry->status.dwCurrentState;
1163 lpServiceStatus->dwControlsAccepted = service->service_entry->status.dwControlsAccepted;
1164 lpServiceStatus->dwWin32ExitCode = service->service_entry->status.dwWin32ExitCode;
1165 lpServiceStatus->dwServiceSpecificExitCode = service->service_entry->status.dwServiceSpecificExitCode;
1166 lpServiceStatus->dwCheckPoint = service->service_entry->status.dwCheckPoint;
1167 lpServiceStatus->dwWaitHint = service->service_entry->status.dwWaitHint;
1168 service_unlock(service->service_entry);
1171 if (dwControl == SERVICE_CONTROL_STOP)
1172 CloseHandle(control_mutex);
1173 else
1174 ReleaseMutex(control_mutex);
1176 return result;
1178 else
1180 if (dwControl == SERVICE_CONTROL_STOP)
1181 CloseHandle(control_mutex);
1182 return ERROR_SERVICE_REQUEST_TIMEOUT;
1186 DWORD __cdecl svcctl_CloseServiceHandle(
1187 SC_RPC_HANDLE *handle)
1189 WINE_TRACE("(&%p)\n", *handle);
1191 SC_RPC_HANDLE_destroy(*handle);
1192 *handle = NULL;
1194 return ERROR_SUCCESS;
1197 static void SC_RPC_LOCK_destroy(SC_RPC_LOCK hLock)
1199 struct sc_lock *lock = hLock;
1200 scmdatabase_unlock_startup(lock->db);
1201 HeapFree(GetProcessHeap(), 0, lock);
1204 void __RPC_USER SC_RPC_LOCK_rundown(SC_RPC_LOCK hLock)
1206 SC_RPC_LOCK_destroy(hLock);
1209 DWORD __cdecl svcctl_LockServiceDatabase(
1210 SC_RPC_HANDLE hSCManager,
1211 SC_RPC_LOCK *phLock)
1213 struct sc_manager_handle *manager;
1214 struct sc_lock *lock;
1215 DWORD err;
1217 WINE_TRACE("(%p, %p)\n", hSCManager, phLock);
1219 if ((err = validate_scm_handle(hSCManager, SC_MANAGER_LOCK, &manager)) != ERROR_SUCCESS)
1220 return err;
1222 err = scmdatabase_lock_startup(manager->db);
1223 if (err != ERROR_SUCCESS)
1224 return err;
1226 lock = HeapAlloc(GetProcessHeap(), 0, sizeof(struct sc_lock));
1227 if (!lock)
1229 scmdatabase_unlock_startup(manager->db);
1230 return ERROR_NOT_ENOUGH_SERVER_MEMORY;
1233 lock->db = manager->db;
1234 *phLock = lock;
1236 return ERROR_SUCCESS;
1239 DWORD __cdecl svcctl_UnlockServiceDatabase(
1240 SC_RPC_LOCK *phLock)
1242 WINE_TRACE("(&%p)\n", *phLock);
1244 SC_RPC_LOCK_destroy(*phLock);
1245 *phLock = NULL;
1247 return ERROR_SUCCESS;
1250 static BOOL map_state(DWORD state, DWORD mask)
1252 switch (state)
1254 case SERVICE_START_PENDING:
1255 case SERVICE_STOP_PENDING:
1256 case SERVICE_RUNNING:
1257 case SERVICE_CONTINUE_PENDING:
1258 case SERVICE_PAUSE_PENDING:
1259 case SERVICE_PAUSED:
1260 if (SERVICE_ACTIVE & mask) return TRUE;
1261 break;
1262 case SERVICE_STOPPED:
1263 if (SERVICE_INACTIVE & mask) return TRUE;
1264 break;
1265 default:
1266 WINE_ERR("unknown state %u\n", state);
1267 break;
1269 return FALSE;
1272 DWORD __cdecl svcctl_EnumServicesStatusW(
1273 SC_RPC_HANDLE hmngr,
1274 DWORD type,
1275 DWORD state,
1276 BYTE *buffer,
1277 DWORD size,
1278 LPDWORD needed,
1279 LPDWORD returned)
1281 DWORD err, sz, total_size, num_services;
1282 DWORD_PTR offset;
1283 struct sc_manager_handle *manager;
1284 struct service_entry *service;
1285 ENUM_SERVICE_STATUSW *s;
1287 WINE_TRACE("(%p, 0x%x, 0x%x, %p, %u, %p, %p)\n", hmngr, type, state, buffer, size, needed, returned);
1289 if (!type || !state)
1290 return ERROR_INVALID_PARAMETER;
1292 if ((err = validate_scm_handle(hmngr, SC_MANAGER_ENUMERATE_SERVICE, &manager)) != ERROR_SUCCESS)
1293 return err;
1295 scmdatabase_lock_exclusive(manager->db);
1297 total_size = num_services = 0;
1298 LIST_FOR_EACH_ENTRY(service, &manager->db->services, struct service_entry, entry)
1300 if ((service->status.dwServiceType & type) && map_state(service->status.dwCurrentState, state))
1302 total_size += sizeof(ENUM_SERVICE_STATUSW);
1303 total_size += (strlenW(service->name) + 1) * sizeof(WCHAR);
1304 if (service->config.lpDisplayName)
1306 total_size += (strlenW(service->config.lpDisplayName) + 1) * sizeof(WCHAR);
1308 num_services++;
1311 *returned = 0;
1312 *needed = total_size;
1313 if (total_size > size)
1315 scmdatabase_unlock(manager->db);
1316 return ERROR_MORE_DATA;
1318 s = (ENUM_SERVICE_STATUSW *)buffer;
1319 offset = num_services * sizeof(ENUM_SERVICE_STATUSW);
1320 LIST_FOR_EACH_ENTRY(service, &manager->db->services, struct service_entry, entry)
1322 if ((service->status.dwServiceType & type) && map_state(service->status.dwCurrentState, state))
1324 sz = (strlenW(service->name) + 1) * sizeof(WCHAR);
1325 memcpy(buffer + offset, service->name, sz);
1326 s->lpServiceName = (WCHAR *)offset; /* store a buffer offset instead of a pointer */
1327 offset += sz;
1329 if (!service->config.lpDisplayName) s->lpDisplayName = NULL;
1330 else
1332 sz = (strlenW(service->config.lpDisplayName) + 1) * sizeof(WCHAR);
1333 memcpy(buffer + offset, service->config.lpDisplayName, sz);
1334 s->lpDisplayName = (WCHAR *)offset;
1335 offset += sz;
1337 memcpy(&s->ServiceStatus, &service->status, sizeof(SERVICE_STATUS));
1338 s++;
1341 *returned = num_services;
1342 *needed = 0;
1343 scmdatabase_unlock(manager->db);
1344 return ERROR_SUCCESS;
1347 static struct service_entry *find_service_by_group(struct scmdatabase *db, const WCHAR *group)
1349 struct service_entry *service;
1350 LIST_FOR_EACH_ENTRY(service, &db->services, struct service_entry, entry)
1352 if (service->config.lpLoadOrderGroup && !strcmpiW(group, service->config.lpLoadOrderGroup))
1353 return service;
1355 return NULL;
1358 static BOOL match_group(const WCHAR *g1, const WCHAR *g2)
1360 if (!g2) return TRUE;
1361 if (!g2[0] && (!g1 || !g1[0])) return TRUE;
1362 if (g1 && !strcmpW(g1, g2)) return TRUE;
1363 return FALSE;
1366 DWORD __cdecl svcctl_EnumServicesStatusExW(
1367 SC_RPC_HANDLE hmngr,
1368 DWORD type,
1369 DWORD state,
1370 BYTE *buffer,
1371 DWORD size,
1372 LPDWORD needed,
1373 LPDWORD returned,
1374 LPCWSTR group)
1376 DWORD err, sz, total_size, num_services;
1377 DWORD_PTR offset;
1378 struct sc_manager_handle *manager;
1379 struct service_entry *service;
1380 ENUM_SERVICE_STATUS_PROCESSW *s;
1382 WINE_TRACE("(%p, 0x%x, 0x%x, %p, %u, %p, %p, %s)\n", hmngr, type, state, buffer, size,
1383 needed, returned, wine_dbgstr_w(group));
1385 if (!type || !state)
1386 return ERROR_INVALID_PARAMETER;
1388 if ((err = validate_scm_handle(hmngr, SC_MANAGER_ENUMERATE_SERVICE, &manager)) != ERROR_SUCCESS)
1389 return err;
1391 scmdatabase_lock_exclusive(manager->db);
1393 if (group && !find_service_by_group(manager->db, group))
1395 scmdatabase_unlock(manager->db);
1396 return ERROR_SERVICE_DOES_NOT_EXIST;
1399 total_size = num_services = 0;
1400 LIST_FOR_EACH_ENTRY(service, &manager->db->services, struct service_entry, entry)
1402 if ((service->status.dwServiceType & type) && map_state(service->status.dwCurrentState, state)
1403 && match_group(service->config.lpLoadOrderGroup, group))
1405 total_size += sizeof(ENUM_SERVICE_STATUS_PROCESSW);
1406 total_size += (strlenW(service->name) + 1) * sizeof(WCHAR);
1407 if (service->config.lpDisplayName)
1409 total_size += (strlenW(service->config.lpDisplayName) + 1) * sizeof(WCHAR);
1411 num_services++;
1414 *returned = 0;
1415 *needed = total_size;
1416 if (total_size > size)
1418 scmdatabase_unlock(manager->db);
1419 return ERROR_MORE_DATA;
1421 s = (ENUM_SERVICE_STATUS_PROCESSW *)buffer;
1422 offset = num_services * sizeof(ENUM_SERVICE_STATUS_PROCESSW);
1423 LIST_FOR_EACH_ENTRY(service, &manager->db->services, struct service_entry, entry)
1425 if ((service->status.dwServiceType & type) && map_state(service->status.dwCurrentState, state)
1426 && match_group(service->config.lpLoadOrderGroup, group))
1428 sz = (strlenW(service->name) + 1) * sizeof(WCHAR);
1429 memcpy(buffer + offset, service->name, sz);
1430 s->lpServiceName = (WCHAR *)offset; /* store a buffer offset instead of a pointer */
1431 offset += sz;
1433 if (!service->config.lpDisplayName) s->lpDisplayName = NULL;
1434 else
1436 sz = (strlenW(service->config.lpDisplayName) + 1) * sizeof(WCHAR);
1437 memcpy(buffer + offset, service->config.lpDisplayName, sz);
1438 s->lpDisplayName = (WCHAR *)offset;
1439 offset += sz;
1441 s->ServiceStatusProcess = service->status;
1442 s++;
1445 *returned = num_services;
1446 *needed = 0;
1447 scmdatabase_unlock(manager->db);
1448 return ERROR_SUCCESS;
1451 DWORD __cdecl svcctl_QueryServiceObjectSecurity(void)
1453 WINE_FIXME("\n");
1454 return ERROR_CALL_NOT_IMPLEMENTED;
1457 DWORD __cdecl svcctl_SetServiceObjectSecurity(void)
1459 WINE_FIXME("\n");
1460 return ERROR_CALL_NOT_IMPLEMENTED;
1463 DWORD __cdecl svcctl_QueryServiceStatus(void)
1465 WINE_FIXME("\n");
1466 return ERROR_CALL_NOT_IMPLEMENTED;
1470 DWORD __cdecl svcctl_NotifyBootConfigStatus(void)
1472 WINE_FIXME("\n");
1473 return ERROR_CALL_NOT_IMPLEMENTED;
1476 DWORD __cdecl svcctl_SCSetServiceBitsW(void)
1478 WINE_FIXME("\n");
1479 return ERROR_CALL_NOT_IMPLEMENTED;
1483 DWORD __cdecl svcctl_EnumDependentServicesW(void)
1485 WINE_FIXME("\n");
1486 return ERROR_CALL_NOT_IMPLEMENTED;
1489 DWORD __cdecl svcctl_QueryServiceLockStatusW(void)
1491 WINE_FIXME("\n");
1492 return ERROR_CALL_NOT_IMPLEMENTED;
1495 DWORD __cdecl svcctl_SCSetServiceBitsA(void)
1497 WINE_FIXME("\n");
1498 return ERROR_CALL_NOT_IMPLEMENTED;
1501 DWORD __cdecl svcctl_ChangeServiceConfigA(void)
1503 WINE_FIXME("\n");
1504 return ERROR_CALL_NOT_IMPLEMENTED;
1507 DWORD __cdecl svcctl_CreateServiceA(void)
1509 WINE_FIXME("\n");
1510 return ERROR_CALL_NOT_IMPLEMENTED;
1513 DWORD __cdecl svcctl_EnumDependentServicesA(void)
1515 WINE_FIXME("\n");
1516 return ERROR_CALL_NOT_IMPLEMENTED;
1519 DWORD __cdecl svcctl_EnumServicesStatusA(void)
1521 WINE_FIXME("\n");
1522 return ERROR_CALL_NOT_IMPLEMENTED;
1525 DWORD __cdecl svcctl_OpenSCManagerA(void)
1527 WINE_FIXME("\n");
1528 return ERROR_CALL_NOT_IMPLEMENTED;
1531 DWORD __cdecl svcctl_OpenServiceA(void)
1533 WINE_FIXME("\n");
1534 return ERROR_CALL_NOT_IMPLEMENTED;
1537 DWORD __cdecl svcctl_QueryServiceConfigA(void)
1539 WINE_FIXME("\n");
1540 return ERROR_CALL_NOT_IMPLEMENTED;
1543 DWORD __cdecl svcctl_QueryServiceLockStatusA(void)
1545 WINE_FIXME("\n");
1546 return ERROR_CALL_NOT_IMPLEMENTED;
1549 DWORD __cdecl svcctl_StartServiceA(void)
1551 WINE_FIXME("\n");
1552 return ERROR_CALL_NOT_IMPLEMENTED;
1555 DWORD __cdecl svcctl_GetServiceDisplayNameA(void)
1557 WINE_FIXME("\n");
1558 return ERROR_CALL_NOT_IMPLEMENTED;
1561 DWORD __cdecl svcctl_GetServiceKeyNameA(void)
1563 WINE_FIXME("\n");
1564 return ERROR_CALL_NOT_IMPLEMENTED;
1567 DWORD __cdecl svcctl_GetCurrentGroupStateW(void)
1569 WINE_FIXME("\n");
1570 return ERROR_CALL_NOT_IMPLEMENTED;
1573 DWORD __cdecl svcctl_EnumServiceGroupW(void)
1575 WINE_FIXME("\n");
1576 return ERROR_CALL_NOT_IMPLEMENTED;
1579 DWORD __cdecl svcctl_ChangeServiceConfig2A(void)
1581 WINE_FIXME("\n");
1582 return ERROR_CALL_NOT_IMPLEMENTED;
1585 DWORD __cdecl svcctl_QueryServiceConfig2A(void)
1587 WINE_FIXME("\n");
1588 return ERROR_CALL_NOT_IMPLEMENTED;
1592 DWORD RPC_Init(void)
1594 WCHAR transport[] = SVCCTL_TRANSPORT;
1595 WCHAR endpoint[] = SVCCTL_ENDPOINT;
1596 DWORD err;
1598 if ((err = RpcServerUseProtseqEpW(transport, 0, endpoint, NULL)) != ERROR_SUCCESS)
1600 WINE_ERR("RpcServerUseProtseq failed with error %u\n", err);
1601 return err;
1604 if ((err = RpcServerRegisterIf(svcctl_v2_0_s_ifspec, 0, 0)) != ERROR_SUCCESS)
1606 WINE_ERR("RpcServerRegisterIf failed with error %u\n", err);
1607 return err;
1610 if ((err = RpcServerListen(1, RPC_C_LISTEN_MAX_CALLS_DEFAULT, TRUE)) != ERROR_SUCCESS)
1612 WINE_ERR("RpcServerListen failed with error %u\n", err);
1613 return err;
1615 return ERROR_SUCCESS;
1618 DWORD events_loop(void)
1620 struct timeout_queue_elem *iter, *iter_safe;
1621 DWORD err;
1622 HANDLE wait_handles[2];
1623 DWORD timeout = INFINITE;
1625 wait_handles[0] = __wine_make_process_system();
1626 wait_handles[1] = CreateEventW(NULL, FALSE, FALSE, NULL);
1627 timeout_queue_event = wait_handles[1];
1629 SetEvent(g_hStartedEvent);
1631 WINE_TRACE("Entered main loop\n");
1635 err = WaitForMultipleObjects(2, wait_handles, FALSE, timeout);
1636 WINE_TRACE("Wait returned %d\n", err);
1638 if(err==WAIT_OBJECT_0+1 || err==WAIT_TIMEOUT)
1640 FILETIME cur_time;
1641 ULARGE_INTEGER time;
1643 GetSystemTimeAsFileTime(&cur_time);
1644 time.u.LowPart = cur_time.dwLowDateTime;
1645 time.u.HighPart = cur_time.dwHighDateTime;
1647 EnterCriticalSection(&timeout_queue_cs);
1648 timeout = INFINITE;
1649 LIST_FOR_EACH_ENTRY_SAFE(iter, iter_safe, &timeout_queue, struct timeout_queue_elem, entry)
1651 if(CompareFileTime(&cur_time, &iter->time) >= 0)
1653 LeaveCriticalSection(&timeout_queue_cs);
1654 iter->func(iter->service_entry);
1655 EnterCriticalSection(&timeout_queue_cs);
1657 release_service(iter->service_entry);
1658 list_remove(&iter->entry);
1659 HeapFree(GetProcessHeap(), 0, iter);
1661 else
1663 ULARGE_INTEGER time_diff;
1665 time_diff.u.LowPart = iter->time.dwLowDateTime;
1666 time_diff.u.HighPart = iter->time.dwHighDateTime;
1667 time_diff.QuadPart = (time_diff.QuadPart-time.QuadPart)/10000;
1669 if(time_diff.QuadPart < timeout)
1670 timeout = time_diff.QuadPart;
1673 LeaveCriticalSection(&timeout_queue_cs);
1675 if(timeout != INFINITE)
1676 timeout += 1000;
1678 } while (err != WAIT_OBJECT_0);
1680 WINE_TRACE("Object signaled - wine shutdown\n");
1681 EnterCriticalSection(&timeout_queue_cs);
1682 LIST_FOR_EACH_ENTRY_SAFE(iter, iter_safe, &timeout_queue, struct timeout_queue_elem, entry)
1684 LeaveCriticalSection(&timeout_queue_cs);
1685 iter->func(iter->service_entry);
1686 EnterCriticalSection(&timeout_queue_cs);
1688 release_service(iter->service_entry);
1689 list_remove(&iter->entry);
1690 HeapFree(GetProcessHeap(), 0, iter);
1692 LeaveCriticalSection(&timeout_queue_cs);
1694 CloseHandle(wait_handles[0]);
1695 CloseHandle(wait_handles[1]);
1696 return ERROR_SUCCESS;
1699 void __RPC_USER SC_RPC_HANDLE_rundown(SC_RPC_HANDLE handle)
1701 SC_RPC_HANDLE_destroy(handle);
1704 void __RPC_FAR * __RPC_USER MIDL_user_allocate(SIZE_T len)
1706 return HeapAlloc(GetProcessHeap(), 0, len);
1709 void __RPC_USER MIDL_user_free(void __RPC_FAR * ptr)
1711 HeapFree(GetProcessHeap(), 0, ptr);