2008-03-15 09:49:42 +01:00
|
|
|
/*
|
|
|
|
* Services - controls services keeps track of their state
|
|
|
|
*
|
|
|
|
* Copyright 2007 Google (Mikolaj Zalewski)
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define WIN32_LEAN_AND_MEAN
|
|
|
|
|
|
|
|
#include <stdarg.h>
|
2019-05-01 10:40:06 +02:00
|
|
|
#include <stdio.h>
|
2019-01-28 15:52:24 +01:00
|
|
|
#include <assert.h>
|
2008-03-15 10:38:45 +01:00
|
|
|
#include <windows.h>
|
|
|
|
#include <winsvc.h>
|
2021-04-08 12:23:09 +02:00
|
|
|
#include <winternl.h>
|
2008-03-28 17:56:00 +01:00
|
|
|
#include <rpc.h>
|
2011-11-15 12:55:58 +01:00
|
|
|
#include <userenv.h>
|
2019-06-22 17:12:34 +02:00
|
|
|
#include <setupapi.h>
|
2008-03-15 10:38:45 +01:00
|
|
|
|
|
|
|
#include "wine/debug.h"
|
2019-06-20 17:58:45 +02:00
|
|
|
#include "wine/heap.h"
|
2008-03-28 17:56:00 +01:00
|
|
|
#include "svcctl.h"
|
2008-03-15 10:38:45 +01:00
|
|
|
|
|
|
|
#include "services.h"
|
|
|
|
|
|
|
|
#define MAX_SERVICE_NAME 260
|
|
|
|
|
|
|
|
WINE_DEFAULT_DEBUG_CHANNEL(service);
|
|
|
|
|
2008-03-28 18:08:12 +01:00
|
|
|
struct scmdatabase *active_database;
|
2008-03-15 16:42:38 +01:00
|
|
|
|
2011-05-20 12:33:58 +02:00
|
|
|
DWORD service_pipe_timeout = 10000;
|
2012-10-22 14:26:10 +02:00
|
|
|
DWORD service_kill_timeout = 60000;
|
2011-11-30 15:28:26 +01:00
|
|
|
static DWORD default_preshutdown_timeout = 180000;
|
2019-06-20 17:58:45 +02:00
|
|
|
static DWORD autostart_delay = 120000;
|
2016-09-01 07:39:21 +02:00
|
|
|
static void *environment = NULL;
|
2016-04-18 17:07:06 +02:00
|
|
|
static HKEY service_current_key = NULL;
|
2011-05-20 12:33:58 +02:00
|
|
|
|
2013-10-09 00:06:17 +02:00
|
|
|
static const BOOL is_win64 = (sizeof(void *) > sizeof(int));
|
2010-06-01 13:38:21 +02:00
|
|
|
|
2008-03-15 16:42:38 +01:00
|
|
|
static const WCHAR SZ_LOCAL_SYSTEM[] = {'L','o','c','a','l','S','y','s','t','e','m',0};
|
|
|
|
|
2008-03-15 10:38:45 +01:00
|
|
|
/* Registry constants */
|
|
|
|
static const WCHAR SZ_SERVICES_KEY[] = { 'S','y','s','t','e','m','\\',
|
|
|
|
'C','u','r','r','e','n','t','C','o','n','t','r','o','l','S','e','t','\\',
|
|
|
|
'S','e','r','v','i','c','e','s',0 };
|
|
|
|
|
|
|
|
/* Service key values names */
|
|
|
|
static const WCHAR SZ_DISPLAY_NAME[] = {'D','i','s','p','l','a','y','N','a','m','e',0 };
|
|
|
|
static const WCHAR SZ_TYPE[] = {'T','y','p','e',0 };
|
|
|
|
static const WCHAR SZ_START[] = {'S','t','a','r','t',0 };
|
|
|
|
static const WCHAR SZ_ERROR[] = {'E','r','r','o','r','C','o','n','t','r','o','l',0 };
|
|
|
|
static const WCHAR SZ_IMAGE_PATH[] = {'I','m','a','g','e','P','a','t','h',0};
|
|
|
|
static const WCHAR SZ_GROUP[] = {'G','r','o','u','p',0};
|
|
|
|
static const WCHAR SZ_DEPEND_ON_SERVICE[] = {'D','e','p','e','n','d','O','n','S','e','r','v','i','c','e',0};
|
|
|
|
static const WCHAR SZ_DEPEND_ON_GROUP[] = {'D','e','p','e','n','d','O','n','G','r','o','u','p',0};
|
|
|
|
static const WCHAR SZ_OBJECT_NAME[] = {'O','b','j','e','c','t','N','a','m','e',0};
|
|
|
|
static const WCHAR SZ_TAG[] = {'T','a','g',0};
|
|
|
|
static const WCHAR SZ_DESCRIPTION[] = {'D','e','s','c','r','i','p','t','i','o','n',0};
|
2011-11-30 15:28:26 +01:00
|
|
|
static const WCHAR SZ_PRESHUTDOWN[] = {'P','r','e','s','h','u','t','d','o','w','n','T','i','m','e','o','u','t',0};
|
2015-02-26 23:30:54 +01:00
|
|
|
static const WCHAR SZ_WOW64[] = {'W','O','W','6','4',0};
|
2019-06-20 17:58:45 +02:00
|
|
|
static const WCHAR SZ_DELAYED_AUTOSTART[] = {'D','e','l','a','y','e','d','A','u','t','o','S','t','a','r','t',0};
|
2008-03-15 10:38:45 +01:00
|
|
|
|
2016-04-20 14:51:30 +02:00
|
|
|
static DWORD process_create(const WCHAR *name, struct process_entry **entry)
|
2016-03-03 06:28:16 +01:00
|
|
|
{
|
2016-04-20 14:51:30 +02:00
|
|
|
DWORD err;
|
|
|
|
|
2016-03-03 06:28:16 +01:00
|
|
|
*entry = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(**entry));
|
|
|
|
if (!*entry)
|
|
|
|
return ERROR_NOT_ENOUGH_SERVER_MEMORY;
|
2016-03-16 04:31:31 +01:00
|
|
|
(*entry)->ref_count = 1;
|
2016-04-20 14:51:30 +02:00
|
|
|
(*entry)->control_mutex = CreateMutexW(NULL, TRUE, NULL);
|
|
|
|
if (!(*entry)->control_mutex)
|
|
|
|
goto error;
|
|
|
|
(*entry)->overlapped_event = CreateEventW(NULL, TRUE, FALSE, NULL);
|
|
|
|
if (!(*entry)->overlapped_event)
|
|
|
|
goto error;
|
|
|
|
(*entry)->control_pipe = CreateNamedPipeW(name, PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED,
|
|
|
|
PIPE_TYPE_BYTE|PIPE_WAIT, 1, 256, 256, 10000, NULL);
|
|
|
|
if ((*entry)->control_pipe == INVALID_HANDLE_VALUE)
|
|
|
|
goto error;
|
2016-03-03 06:28:16 +01:00
|
|
|
/* all other fields are zero */
|
|
|
|
return ERROR_SUCCESS;
|
2016-04-20 14:51:30 +02:00
|
|
|
|
|
|
|
error:
|
|
|
|
err = GetLastError();
|
|
|
|
if ((*entry)->control_mutex)
|
|
|
|
CloseHandle((*entry)->control_mutex);
|
|
|
|
if ((*entry)->overlapped_event)
|
|
|
|
CloseHandle((*entry)->overlapped_event);
|
|
|
|
HeapFree(GetProcessHeap(), 0, *entry);
|
|
|
|
return err;
|
2016-03-03 06:28:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void free_process_entry(struct process_entry *entry)
|
|
|
|
{
|
|
|
|
CloseHandle(entry->process);
|
|
|
|
CloseHandle(entry->control_mutex);
|
|
|
|
CloseHandle(entry->control_pipe);
|
|
|
|
CloseHandle(entry->overlapped_event);
|
|
|
|
HeapFree(GetProcessHeap(), 0, entry);
|
|
|
|
}
|
|
|
|
|
2008-03-28 18:08:12 +01:00
|
|
|
DWORD service_create(LPCWSTR name, struct service_entry **entry)
|
|
|
|
{
|
|
|
|
*entry = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(**entry));
|
|
|
|
if (!*entry)
|
|
|
|
return ERROR_NOT_ENOUGH_SERVER_MEMORY;
|
|
|
|
(*entry)->name = strdupW(name);
|
2019-01-28 15:52:24 +01:00
|
|
|
list_init(&(*entry)->handles);
|
2008-03-28 18:08:12 +01:00
|
|
|
if (!(*entry)->name)
|
|
|
|
{
|
|
|
|
HeapFree(GetProcessHeap(), 0, *entry);
|
|
|
|
return ERROR_NOT_ENOUGH_SERVER_MEMORY;
|
|
|
|
}
|
2016-08-19 08:01:03 +02:00
|
|
|
(*entry)->status_changed_event = CreateEventW(NULL, TRUE, FALSE, NULL);
|
|
|
|
if (!(*entry)->status_changed_event)
|
|
|
|
{
|
|
|
|
HeapFree(GetProcessHeap(), 0, (*entry)->name);
|
|
|
|
HeapFree(GetProcessHeap(), 0, *entry);
|
|
|
|
return GetLastError();
|
|
|
|
}
|
2016-03-16 04:30:56 +01:00
|
|
|
(*entry)->ref_count = 1;
|
2008-11-25 11:56:33 +01:00
|
|
|
(*entry)->status.dwCurrentState = SERVICE_STOPPED;
|
|
|
|
(*entry)->status.dwWin32ExitCode = ERROR_SERVICE_NEVER_STARTED;
|
2011-11-30 15:28:26 +01:00
|
|
|
(*entry)->preshutdown_timeout = default_preshutdown_timeout;
|
2008-11-25 11:56:33 +01:00
|
|
|
/* all other fields are zero */
|
2008-03-28 18:08:12 +01:00
|
|
|
return ERROR_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2008-03-15 16:42:38 +01:00
|
|
|
void free_service_entry(struct service_entry *entry)
|
2008-03-15 10:38:45 +01:00
|
|
|
{
|
2019-01-28 15:52:24 +01:00
|
|
|
assert(list_empty(&entry->handles));
|
2016-08-19 08:01:03 +02:00
|
|
|
CloseHandle(entry->status_changed_event);
|
2008-03-15 10:38:45 +01:00
|
|
|
HeapFree(GetProcessHeap(), 0, entry->name);
|
|
|
|
HeapFree(GetProcessHeap(), 0, entry->config.lpBinaryPathName);
|
|
|
|
HeapFree(GetProcessHeap(), 0, entry->config.lpDependencies);
|
|
|
|
HeapFree(GetProcessHeap(), 0, entry->config.lpLoadOrderGroup);
|
|
|
|
HeapFree(GetProcessHeap(), 0, entry->config.lpServiceStartName);
|
|
|
|
HeapFree(GetProcessHeap(), 0, entry->config.lpDisplayName);
|
|
|
|
HeapFree(GetProcessHeap(), 0, entry->description);
|
2008-04-01 13:48:28 +02:00
|
|
|
HeapFree(GetProcessHeap(), 0, entry->dependOnServices);
|
|
|
|
HeapFree(GetProcessHeap(), 0, entry->dependOnGroups);
|
2016-04-20 14:51:30 +02:00
|
|
|
if (entry->process) release_process(entry->process);
|
2008-03-15 10:38:45 +01:00
|
|
|
HeapFree(GetProcessHeap(), 0, entry);
|
|
|
|
}
|
|
|
|
|
|
|
|
static DWORD load_service_config(HKEY hKey, struct service_entry *entry)
|
|
|
|
{
|
2015-02-26 23:30:54 +01:00
|
|
|
DWORD err, value = 0;
|
2008-03-15 10:38:45 +01:00
|
|
|
WCHAR *wptr;
|
|
|
|
|
|
|
|
if ((err = load_reg_string(hKey, SZ_IMAGE_PATH, TRUE, &entry->config.lpBinaryPathName)) != 0)
|
|
|
|
return err;
|
|
|
|
if ((err = load_reg_string(hKey, SZ_GROUP, 0, &entry->config.lpLoadOrderGroup)) != 0)
|
|
|
|
return err;
|
|
|
|
if ((err = load_reg_string(hKey, SZ_OBJECT_NAME, TRUE, &entry->config.lpServiceStartName)) != 0)
|
|
|
|
return err;
|
|
|
|
if ((err = load_reg_string(hKey, SZ_DISPLAY_NAME, 0, &entry->config.lpDisplayName)) != 0)
|
|
|
|
return err;
|
|
|
|
if ((err = load_reg_string(hKey, SZ_DESCRIPTION, 0, &entry->description)) != 0)
|
|
|
|
return err;
|
2008-09-17 00:35:01 +02:00
|
|
|
if ((err = load_reg_multisz(hKey, SZ_DEPEND_ON_SERVICE, TRUE, &entry->dependOnServices)) != 0)
|
2008-03-15 10:38:45 +01:00
|
|
|
return err;
|
2008-09-17 00:35:01 +02:00
|
|
|
if ((err = load_reg_multisz(hKey, SZ_DEPEND_ON_GROUP, FALSE, &entry->dependOnGroups)) != 0)
|
2008-03-15 10:38:45 +01:00
|
|
|
return err;
|
|
|
|
|
|
|
|
if ((err = load_reg_dword(hKey, SZ_TYPE, &entry->config.dwServiceType)) != 0)
|
|
|
|
return err;
|
|
|
|
if ((err = load_reg_dword(hKey, SZ_START, &entry->config.dwStartType)) != 0)
|
|
|
|
return err;
|
|
|
|
if ((err = load_reg_dword(hKey, SZ_ERROR, &entry->config.dwErrorControl)) != 0)
|
|
|
|
return err;
|
|
|
|
if ((err = load_reg_dword(hKey, SZ_TAG, &entry->config.dwTagId)) != 0)
|
|
|
|
return err;
|
2011-11-30 15:28:26 +01:00
|
|
|
if ((err = load_reg_dword(hKey, SZ_PRESHUTDOWN, &entry->preshutdown_timeout)) != 0)
|
|
|
|
return err;
|
2008-03-15 10:38:45 +01:00
|
|
|
|
2015-02-26 23:30:54 +01:00
|
|
|
if (load_reg_dword(hKey, SZ_WOW64, &value) == 0 && value == 1)
|
|
|
|
entry->is_wow64 = TRUE;
|
2019-06-20 17:58:45 +02:00
|
|
|
if (load_reg_dword(hKey, SZ_DELAYED_AUTOSTART, &value) == 0 && value == 1)
|
|
|
|
entry->delayed_autostart = TRUE;
|
2015-02-26 23:30:54 +01:00
|
|
|
|
2008-03-15 10:38:45 +01:00
|
|
|
WINE_TRACE("Image path = %s\n", wine_dbgstr_w(entry->config.lpBinaryPathName) );
|
|
|
|
WINE_TRACE("Group = %s\n", wine_dbgstr_w(entry->config.lpLoadOrderGroup) );
|
|
|
|
WINE_TRACE("Service account name = %s\n", wine_dbgstr_w(entry->config.lpServiceStartName) );
|
|
|
|
WINE_TRACE("Display name = %s\n", wine_dbgstr_w(entry->config.lpDisplayName) );
|
2008-04-07 13:01:02 +02:00
|
|
|
WINE_TRACE("Service dependencies : %s\n", entry->dependOnServices[0] ? "" : "(none)");
|
2019-05-01 10:40:06 +02:00
|
|
|
for (wptr = entry->dependOnServices; *wptr; wptr += lstrlenW(wptr) + 1)
|
2008-03-15 10:38:45 +01:00
|
|
|
WINE_TRACE(" * %s\n", wine_dbgstr_w(wptr));
|
2008-04-07 13:01:02 +02:00
|
|
|
WINE_TRACE("Group dependencies : %s\n", entry->dependOnGroups[0] ? "" : "(none)");
|
2019-05-01 10:40:06 +02:00
|
|
|
for (wptr = entry->dependOnGroups; *wptr; wptr += lstrlenW(wptr) + 1)
|
2008-03-15 10:38:45 +01:00
|
|
|
WINE_TRACE(" * %s\n", wine_dbgstr_w(wptr));
|
|
|
|
|
|
|
|
return ERROR_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2008-03-15 16:42:38 +01:00
|
|
|
static DWORD reg_set_string_value(HKEY hKey, LPCWSTR value_name, LPCWSTR string)
|
|
|
|
{
|
|
|
|
if (!string)
|
|
|
|
{
|
|
|
|
DWORD err;
|
|
|
|
err = RegDeleteValueW(hKey, value_name);
|
|
|
|
if (err != ERROR_FILE_NOT_FOUND)
|
|
|
|
return err;
|
|
|
|
|
|
|
|
return ERROR_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2019-05-01 10:40:06 +02:00
|
|
|
return RegSetValueExW(hKey, value_name, 0, REG_SZ, (const BYTE*)string, sizeof(WCHAR)*(lstrlenW(string) + 1));
|
2008-03-15 16:42:38 +01:00
|
|
|
}
|
|
|
|
|
2010-10-22 10:35:37 +02:00
|
|
|
static DWORD reg_set_multisz_value(HKEY hKey, LPCWSTR value_name, LPCWSTR string)
|
|
|
|
{
|
|
|
|
const WCHAR *ptr;
|
|
|
|
|
|
|
|
if (!string)
|
|
|
|
{
|
|
|
|
DWORD err;
|
|
|
|
err = RegDeleteValueW(hKey, value_name);
|
|
|
|
if (err != ERROR_FILE_NOT_FOUND)
|
|
|
|
return err;
|
|
|
|
|
|
|
|
return ERROR_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
ptr = string;
|
2019-05-01 10:40:06 +02:00
|
|
|
while (*ptr) ptr += lstrlenW(ptr) + 1;
|
2010-10-22 10:35:37 +02:00
|
|
|
return RegSetValueExW(hKey, value_name, 0, REG_MULTI_SZ, (const BYTE*)string, sizeof(WCHAR)*(ptr - string + 1));
|
|
|
|
}
|
|
|
|
|
2008-03-15 19:11:12 +01:00
|
|
|
DWORD save_service_config(struct service_entry *entry)
|
2008-03-15 16:42:38 +01:00
|
|
|
{
|
|
|
|
DWORD err;
|
|
|
|
HKEY hKey = NULL;
|
|
|
|
|
2008-03-28 18:08:12 +01:00
|
|
|
err = RegCreateKeyW(entry->db->root_key, entry->name, &hKey);
|
2008-03-15 16:42:38 +01:00
|
|
|
if (err != ERROR_SUCCESS)
|
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
if ((err = reg_set_string_value(hKey, SZ_DISPLAY_NAME, entry->config.lpDisplayName)) != 0)
|
|
|
|
goto cleanup;
|
|
|
|
if ((err = reg_set_string_value(hKey, SZ_IMAGE_PATH, entry->config.lpBinaryPathName)) != 0)
|
|
|
|
goto cleanup;
|
|
|
|
if ((err = reg_set_string_value(hKey, SZ_GROUP, entry->config.lpLoadOrderGroup)) != 0)
|
|
|
|
goto cleanup;
|
|
|
|
if ((err = reg_set_string_value(hKey, SZ_OBJECT_NAME, entry->config.lpServiceStartName)) != 0)
|
|
|
|
goto cleanup;
|
|
|
|
if ((err = reg_set_string_value(hKey, SZ_DESCRIPTION, entry->description)) != 0)
|
|
|
|
goto cleanup;
|
2010-10-22 10:35:37 +02:00
|
|
|
if ((err = reg_set_multisz_value(hKey, SZ_DEPEND_ON_SERVICE, entry->dependOnServices)) != 0)
|
|
|
|
goto cleanup;
|
|
|
|
if ((err = reg_set_multisz_value(hKey, SZ_DEPEND_ON_GROUP, entry->dependOnGroups)) != 0)
|
|
|
|
goto cleanup;
|
2008-03-15 16:42:38 +01:00
|
|
|
if ((err = RegSetValueExW(hKey, SZ_START, 0, REG_DWORD, (LPBYTE)&entry->config.dwStartType, sizeof(DWORD))) != 0)
|
|
|
|
goto cleanup;
|
|
|
|
if ((err = RegSetValueExW(hKey, SZ_ERROR, 0, REG_DWORD, (LPBYTE)&entry->config.dwErrorControl, sizeof(DWORD))) != 0)
|
|
|
|
goto cleanup;
|
|
|
|
if ((err = RegSetValueExW(hKey, SZ_TYPE, 0, REG_DWORD, (LPBYTE)&entry->config.dwServiceType, sizeof(DWORD))) != 0)
|
|
|
|
goto cleanup;
|
2011-11-30 15:28:26 +01:00
|
|
|
if ((err = RegSetValueExW(hKey, SZ_PRESHUTDOWN, 0, REG_DWORD, (LPBYTE)&entry->preshutdown_timeout, sizeof(DWORD))) != 0)
|
|
|
|
goto cleanup;
|
2015-02-26 23:30:54 +01:00
|
|
|
if ((err = RegSetValueExW(hKey, SZ_PRESHUTDOWN, 0, REG_DWORD, (LPBYTE)&entry->preshutdown_timeout, sizeof(DWORD))) != 0)
|
|
|
|
goto cleanup;
|
|
|
|
if (entry->is_wow64)
|
|
|
|
{
|
|
|
|
const DWORD is_wow64 = 1;
|
|
|
|
if ((err = RegSetValueExW(hKey, SZ_WOW64, 0, REG_DWORD, (LPBYTE)&is_wow64, sizeof(DWORD))) != 0)
|
|
|
|
goto cleanup;
|
|
|
|
}
|
2008-03-15 16:42:38 +01:00
|
|
|
|
|
|
|
if (entry->config.dwTagId)
|
|
|
|
err = RegSetValueExW(hKey, SZ_TAG, 0, REG_DWORD, (LPBYTE)&entry->config.dwTagId, sizeof(DWORD));
|
|
|
|
else
|
|
|
|
err = RegDeleteValueW(hKey, SZ_TAG);
|
|
|
|
|
|
|
|
if (err != 0 && err != ERROR_FILE_NOT_FOUND)
|
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
err = ERROR_SUCCESS;
|
|
|
|
cleanup:
|
|
|
|
RegCloseKey(hKey);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2016-04-20 14:51:30 +02:00
|
|
|
static void scmdatabase_add_process(struct scmdatabase *db, struct process_entry *process)
|
|
|
|
{
|
|
|
|
process->db = db;
|
|
|
|
list_add_tail(&db->processes, &process->entry);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void scmdatabase_remove_process(struct scmdatabase *db, struct process_entry *process)
|
|
|
|
{
|
|
|
|
list_remove(&process->entry);
|
|
|
|
process->entry.next = process->entry.prev = NULL;
|
|
|
|
}
|
|
|
|
|
2008-03-28 18:08:12 +01:00
|
|
|
DWORD scmdatabase_add_service(struct scmdatabase *db, struct service_entry *service)
|
2008-03-15 16:42:38 +01:00
|
|
|
{
|
|
|
|
int err;
|
2008-03-28 18:08:12 +01:00
|
|
|
service->db = db;
|
2008-03-15 16:42:38 +01:00
|
|
|
if ((err = save_service_config(service)) != ERROR_SUCCESS)
|
|
|
|
{
|
|
|
|
WINE_ERR("Couldn't store service configuration: error %u\n", err);
|
|
|
|
return ERROR_GEN_FAILURE;
|
|
|
|
}
|
|
|
|
|
2008-03-28 18:08:12 +01:00
|
|
|
list_add_tail(&db->services, &service->entry);
|
2008-03-15 16:42:38 +01:00
|
|
|
return ERROR_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2016-04-20 10:14:34 +02:00
|
|
|
static void scmdatabase_remove_service(struct scmdatabase *db, struct service_entry *service)
|
2008-03-15 16:42:38 +01:00
|
|
|
{
|
2016-04-20 10:14:34 +02:00
|
|
|
RegDeleteTreeW(db->root_key, service->name);
|
2008-03-15 16:42:38 +01:00
|
|
|
list_remove(&service->entry);
|
|
|
|
service->entry.next = service->entry.prev = NULL;
|
|
|
|
}
|
|
|
|
|
2019-05-06 10:40:48 +02:00
|
|
|
static int __cdecl compare_tags(const void *a, const void *b)
|
2016-08-18 09:31:35 +02:00
|
|
|
{
|
|
|
|
struct service_entry *service_a = *(struct service_entry **)a;
|
|
|
|
struct service_entry *service_b = *(struct service_entry **)b;
|
|
|
|
return service_a->config.dwTagId - service_b->config.dwTagId;
|
|
|
|
}
|
|
|
|
|
2019-06-20 17:58:45 +02:00
|
|
|
static PTP_CLEANUP_GROUP delayed_autostart_cleanup;
|
|
|
|
|
|
|
|
struct delayed_autostart_params
|
|
|
|
{
|
|
|
|
unsigned int count;
|
|
|
|
struct service_entry **services;
|
|
|
|
};
|
|
|
|
|
|
|
|
static void CALLBACK delayed_autostart_cancel_callback(void *object, void *userdata)
|
|
|
|
{
|
|
|
|
struct delayed_autostart_params *params = object;
|
|
|
|
while(params->count--)
|
|
|
|
release_service(params->services[params->count]);
|
|
|
|
heap_free(params->services);
|
|
|
|
heap_free(params);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void CALLBACK delayed_autostart_callback(TP_CALLBACK_INSTANCE *instance, void *context,
|
2019-09-06 21:33:58 +02:00
|
|
|
TP_TIMER *timer)
|
2019-06-20 17:58:45 +02:00
|
|
|
{
|
|
|
|
struct delayed_autostart_params *params = context;
|
|
|
|
struct service_entry *service;
|
|
|
|
unsigned int i;
|
|
|
|
DWORD err;
|
|
|
|
|
2019-09-06 21:33:58 +02:00
|
|
|
scmdatabase_lock_startup(active_database, INFINITE);
|
2019-06-20 17:58:45 +02:00
|
|
|
|
2019-09-06 21:33:58 +02:00
|
|
|
for (i = 0; i < params->count; i++)
|
|
|
|
{
|
|
|
|
service = params->services[i];
|
|
|
|
if (service->status.dwCurrentState == SERVICE_STOPPED)
|
2019-06-20 17:58:45 +02:00
|
|
|
{
|
2019-09-06 21:33:58 +02:00
|
|
|
TRACE("Starting delayed auto-start service %s\n", debugstr_w(service->name));
|
|
|
|
err = service_start(service, 0, NULL);
|
|
|
|
if (err != ERROR_SUCCESS)
|
2022-02-07 08:39:43 +01:00
|
|
|
FIXME("Delayed auto-start service %s failed to start: %ld\n",
|
2019-09-06 21:33:58 +02:00
|
|
|
wine_dbgstr_w(service->name), err);
|
2019-06-20 17:58:45 +02:00
|
|
|
}
|
2019-09-06 21:33:58 +02:00
|
|
|
release_service(service);
|
2019-06-20 17:58:45 +02:00
|
|
|
}
|
|
|
|
|
2019-09-06 21:33:58 +02:00
|
|
|
scmdatabase_unlock_startup(active_database);
|
|
|
|
|
2019-06-20 17:58:45 +02:00
|
|
|
heap_free(params->services);
|
|
|
|
heap_free(params);
|
2019-09-06 21:33:58 +02:00
|
|
|
CloseThreadpoolTimer(timer);
|
2019-06-20 17:58:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static BOOL schedule_delayed_autostart(struct service_entry **services, unsigned int count)
|
|
|
|
{
|
|
|
|
struct delayed_autostart_params *params;
|
|
|
|
TP_CALLBACK_ENVIRON environment;
|
|
|
|
LARGE_INTEGER timestamp;
|
2019-09-06 21:33:58 +02:00
|
|
|
TP_TIMER *timer;
|
2019-06-20 17:58:45 +02:00
|
|
|
FILETIME ft;
|
|
|
|
|
|
|
|
if (!(delayed_autostart_cleanup = CreateThreadpoolCleanupGroup()))
|
|
|
|
{
|
2022-02-07 08:39:43 +01:00
|
|
|
ERR("CreateThreadpoolCleanupGroup failed with error %lu\n", GetLastError());
|
2019-06-20 17:58:45 +02:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(params = heap_alloc(sizeof(*params)))) return FALSE;
|
|
|
|
params->count = count;
|
|
|
|
params->services = services;
|
|
|
|
|
|
|
|
memset(&environment, 0, sizeof(environment));
|
|
|
|
environment.Version = 1;
|
|
|
|
environment.CleanupGroup = delayed_autostart_cleanup;
|
|
|
|
environment.CleanupGroupCancelCallback = delayed_autostart_cancel_callback;
|
|
|
|
|
|
|
|
timestamp.QuadPart = (ULONGLONG)autostart_delay * -10000;
|
|
|
|
ft.dwLowDateTime = timestamp.u.LowPart;
|
|
|
|
ft.dwHighDateTime = timestamp.u.HighPart;
|
|
|
|
|
2019-09-06 21:33:58 +02:00
|
|
|
if (!(timer = CreateThreadpoolTimer(delayed_autostart_callback, params, &environment)))
|
2019-06-20 17:58:45 +02:00
|
|
|
{
|
2022-02-07 08:39:43 +01:00
|
|
|
ERR("CreateThreadpoolWait failed: %lu\n", GetLastError());
|
2019-06-20 17:58:45 +02:00
|
|
|
heap_free(params);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2019-09-06 21:33:58 +02:00
|
|
|
SetThreadpoolTimer(timer, &ft, 0, 0);
|
2019-06-20 17:58:45 +02:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2019-07-19 01:29:38 +02:00
|
|
|
static BOOL is_root_pnp_service(HDEVINFO set, const struct service_entry *service)
|
2019-06-22 17:12:34 +02:00
|
|
|
{
|
|
|
|
SP_DEVINFO_DATA device = {sizeof(device)};
|
|
|
|
WCHAR name[MAX_SERVICE_NAME];
|
|
|
|
unsigned int i;
|
|
|
|
|
|
|
|
for (i = 0; SetupDiEnumDeviceInfo(set, i, &device); ++i)
|
|
|
|
{
|
|
|
|
if (SetupDiGetDeviceRegistryPropertyW(set, &device, SPDRP_SERVICE, NULL,
|
|
|
|
(BYTE *)name, sizeof(name), NULL)
|
|
|
|
&& !wcsicmp(name, service->name))
|
|
|
|
{
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2008-04-11 12:00:11 +02:00
|
|
|
static void scmdatabase_autostart_services(struct scmdatabase *db)
|
|
|
|
{
|
2019-06-22 17:12:34 +02:00
|
|
|
static const WCHAR rootW[] = {'R','O','O','T',0};
|
2008-04-11 12:00:11 +02:00
|
|
|
struct service_entry **services_list;
|
|
|
|
unsigned int i = 0;
|
|
|
|
unsigned int size = 32;
|
2019-06-20 17:58:45 +02:00
|
|
|
unsigned int delayed_cnt = 0;
|
2008-04-11 12:00:11 +02:00
|
|
|
struct service_entry *service;
|
2019-06-22 17:12:34 +02:00
|
|
|
HDEVINFO set;
|
2008-04-11 12:00:11 +02:00
|
|
|
|
|
|
|
services_list = HeapAlloc(GetProcessHeap(), 0, size * sizeof(services_list[0]));
|
|
|
|
if (!services_list)
|
|
|
|
return;
|
|
|
|
|
2019-06-22 17:12:34 +02:00
|
|
|
if ((set = SetupDiGetClassDevsW( NULL, rootW, NULL, DIGCF_ALLCLASSES )) == INVALID_HANDLE_VALUE)
|
2022-02-07 08:39:43 +01:00
|
|
|
WINE_ERR("Failed to enumerate devices, error %#lx.\n", GetLastError());
|
2019-06-22 17:12:34 +02:00
|
|
|
|
2016-03-02 07:21:17 +01:00
|
|
|
scmdatabase_lock(db);
|
2008-04-11 12:00:11 +02:00
|
|
|
|
|
|
|
LIST_FOR_EACH_ENTRY(service, &db->services, struct service_entry, entry)
|
|
|
|
{
|
|
|
|
if (service->config.dwStartType == SERVICE_BOOT_START ||
|
|
|
|
service->config.dwStartType == SERVICE_SYSTEM_START ||
|
2019-06-22 17:12:34 +02:00
|
|
|
service->config.dwStartType == SERVICE_AUTO_START ||
|
|
|
|
(set != INVALID_HANDLE_VALUE && is_root_pnp_service(set, service)))
|
2008-04-11 12:00:11 +02:00
|
|
|
{
|
|
|
|
if (i+1 >= size)
|
|
|
|
{
|
2009-01-31 11:44:12 +01:00
|
|
|
struct service_entry **slist_new;
|
2008-04-11 12:00:11 +02:00
|
|
|
size *= 2;
|
2009-01-31 11:44:12 +01:00
|
|
|
slist_new = HeapReAlloc(GetProcessHeap(), 0, services_list, size * sizeof(services_list[0]));
|
|
|
|
if (!slist_new)
|
2008-04-11 12:00:11 +02:00
|
|
|
break;
|
2009-01-31 11:44:12 +01:00
|
|
|
services_list = slist_new;
|
2008-04-11 12:00:11 +02:00
|
|
|
}
|
2016-08-17 09:12:27 +02:00
|
|
|
services_list[i++] = grab_service(service);
|
2008-04-11 12:00:11 +02:00
|
|
|
}
|
|
|
|
}
|
2016-08-17 09:12:27 +02:00
|
|
|
size = i;
|
2008-04-11 12:00:11 +02:00
|
|
|
|
|
|
|
scmdatabase_unlock(db);
|
2016-08-18 09:31:35 +02:00
|
|
|
qsort(services_list, size, sizeof(services_list[0]), compare_tags);
|
2018-04-09 13:29:02 +02:00
|
|
|
scmdatabase_lock_startup(db, INFINITE);
|
2008-04-11 12:00:11 +02:00
|
|
|
|
|
|
|
for (i = 0; i < size; i++)
|
|
|
|
{
|
|
|
|
DWORD err;
|
|
|
|
service = services_list[i];
|
2019-06-20 17:58:45 +02:00
|
|
|
if (service->delayed_autostart)
|
|
|
|
{
|
|
|
|
TRACE("delayed starting %s\n", wine_dbgstr_w(service->name));
|
|
|
|
services_list[delayed_cnt++] = service;
|
|
|
|
continue;
|
|
|
|
}
|
2012-06-15 16:37:27 +02:00
|
|
|
err = service_start(service, 0, NULL);
|
2011-09-01 16:06:20 +02:00
|
|
|
if (err != ERROR_SUCCESS)
|
2022-02-07 08:39:43 +01:00
|
|
|
WINE_FIXME("Auto-start service %s failed to start: %ld\n",
|
2011-09-01 16:06:20 +02:00
|
|
|
wine_dbgstr_w(service->name), err);
|
2008-04-11 12:00:11 +02:00
|
|
|
release_service(service);
|
|
|
|
}
|
|
|
|
|
2016-08-18 09:30:33 +02:00
|
|
|
scmdatabase_unlock_startup(db);
|
2019-06-20 17:58:45 +02:00
|
|
|
|
|
|
|
if (!delayed_cnt || !schedule_delayed_autostart(services_list, delayed_cnt))
|
|
|
|
heap_free(services_list);
|
2019-06-22 17:12:34 +02:00
|
|
|
SetupDiDestroyDeviceInfoList(set);
|
2008-04-11 12:00:11 +02:00
|
|
|
}
|
|
|
|
|
2011-11-30 15:29:01 +01:00
|
|
|
static void scmdatabase_wait_terminate(struct scmdatabase *db)
|
|
|
|
{
|
2016-04-20 14:51:30 +02:00
|
|
|
struct list pending = LIST_INIT(pending);
|
|
|
|
void *ptr;
|
2011-11-30 15:29:01 +01:00
|
|
|
|
2016-03-02 07:21:17 +01:00
|
|
|
scmdatabase_lock(db);
|
2016-04-20 14:51:30 +02:00
|
|
|
list_move_tail(&pending, &db->processes);
|
|
|
|
while ((ptr = list_head(&pending)))
|
2011-11-30 15:29:01 +01:00
|
|
|
{
|
2016-04-20 14:51:30 +02:00
|
|
|
struct process_entry *process = grab_process(LIST_ENTRY(ptr, struct process_entry, entry));
|
|
|
|
|
2021-11-10 10:26:46 +01:00
|
|
|
process_terminate(process);
|
2016-04-20 14:51:30 +02:00
|
|
|
scmdatabase_unlock(db);
|
|
|
|
WaitForSingleObject(process->process, INFINITE);
|
|
|
|
scmdatabase_lock(db);
|
|
|
|
|
|
|
|
list_remove(&process->entry);
|
|
|
|
list_add_tail(&db->processes, &process->entry);
|
|
|
|
release_process(process);
|
2011-11-30 15:29:01 +01:00
|
|
|
}
|
|
|
|
scmdatabase_unlock(db);
|
|
|
|
}
|
|
|
|
|
2008-03-15 16:42:38 +01:00
|
|
|
BOOL validate_service_name(LPCWSTR name)
|
|
|
|
{
|
2019-05-01 10:40:06 +02:00
|
|
|
return (name && name[0] && !wcschr(name, '/') && !wcschr(name, '\\'));
|
2008-03-15 16:42:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
BOOL validate_service_config(struct service_entry *entry)
|
2008-03-15 10:38:45 +01:00
|
|
|
{
|
|
|
|
if (entry->config.dwServiceType & SERVICE_WIN32 && (entry->config.lpBinaryPathName == NULL || !entry->config.lpBinaryPathName[0]))
|
|
|
|
{
|
2008-05-08 08:21:19 +02:00
|
|
|
WINE_ERR("Service %s is Win32 but has no image path set\n", wine_dbgstr_w(entry->name));
|
2008-03-15 10:38:45 +01:00
|
|
|
return FALSE;
|
|
|
|
}
|
2008-03-15 16:42:38 +01:00
|
|
|
|
|
|
|
switch (entry->config.dwServiceType)
|
|
|
|
{
|
|
|
|
case SERVICE_KERNEL_DRIVER:
|
|
|
|
case SERVICE_FILE_SYSTEM_DRIVER:
|
|
|
|
case SERVICE_WIN32_OWN_PROCESS:
|
|
|
|
case SERVICE_WIN32_SHARE_PROCESS:
|
|
|
|
/* No problem */
|
|
|
|
break;
|
|
|
|
case SERVICE_WIN32_OWN_PROCESS | SERVICE_INTERACTIVE_PROCESS:
|
|
|
|
case SERVICE_WIN32_SHARE_PROCESS | SERVICE_INTERACTIVE_PROCESS:
|
|
|
|
/* These can be only run as LocalSystem */
|
2019-05-01 10:40:06 +02:00
|
|
|
if (entry->config.lpServiceStartName && wcsicmp(entry->config.lpServiceStartName, SZ_LOCAL_SYSTEM) != 0)
|
2008-03-15 16:42:38 +01:00
|
|
|
{
|
|
|
|
WINE_ERR("Service %s is interactive but has a start name\n", wine_dbgstr_w(entry->name));
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
2022-02-07 08:39:43 +01:00
|
|
|
WINE_ERR("Service %s has an unknown service type (0x%lx)\n", wine_dbgstr_w(entry->name), entry->config.dwServiceType);
|
2008-03-15 16:42:38 +01:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* StartType can only be a single value (if several values are mixed the result is probably not what was intended) */
|
|
|
|
if (entry->config.dwStartType > SERVICE_DISABLED)
|
|
|
|
{
|
2008-05-08 08:21:19 +02:00
|
|
|
WINE_ERR("Service %s has an unknown start type\n", wine_dbgstr_w(entry->name));
|
2008-03-15 16:42:38 +01:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2008-05-08 08:21:19 +02:00
|
|
|
/* SERVICE_BOOT_START and SERVICE_SYSTEM_START are only allowed for driver services */
|
2008-03-15 16:42:38 +01:00
|
|
|
if (((entry->config.dwStartType == SERVICE_BOOT_START) || (entry->config.dwStartType == SERVICE_SYSTEM_START)) &&
|
|
|
|
((entry->config.dwServiceType & SERVICE_WIN32_OWN_PROCESS) || (entry->config.dwServiceType & SERVICE_WIN32_SHARE_PROCESS)))
|
|
|
|
{
|
|
|
|
WINE_ERR("Service %s - SERVICE_BOOT_START and SERVICE_SYSTEM_START are only allowed for driver services\n", wine_dbgstr_w(entry->name));
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (entry->config.lpServiceStartName == NULL)
|
|
|
|
entry->config.lpServiceStartName = strdupW(SZ_LOCAL_SYSTEM);
|
|
|
|
|
2008-03-15 10:38:45 +01:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2008-03-28 18:06:43 +01:00
|
|
|
|
2008-03-28 18:08:12 +01:00
|
|
|
struct service_entry *scmdatabase_find_service(struct scmdatabase *db, LPCWSTR name)
|
2008-03-15 16:42:38 +01:00
|
|
|
{
|
|
|
|
struct service_entry *service;
|
|
|
|
|
2008-03-28 18:08:12 +01:00
|
|
|
LIST_FOR_EACH_ENTRY(service, &db->services, struct service_entry, entry)
|
2008-03-15 16:42:38 +01:00
|
|
|
{
|
2019-05-01 10:40:06 +02:00
|
|
|
if (wcsicmp(name, service->name) == 0)
|
2008-03-15 16:42:38 +01:00
|
|
|
return service;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2008-03-28 18:08:12 +01:00
|
|
|
struct service_entry *scmdatabase_find_service_by_displayname(struct scmdatabase *db, LPCWSTR name)
|
2008-03-15 16:42:38 +01:00
|
|
|
{
|
|
|
|
struct service_entry *service;
|
|
|
|
|
2008-03-28 18:08:12 +01:00
|
|
|
LIST_FOR_EACH_ENTRY(service, &db->services, struct service_entry, entry)
|
2008-03-15 16:42:38 +01:00
|
|
|
{
|
2019-05-01 10:40:06 +02:00
|
|
|
if (service->config.lpDisplayName && wcsicmp(name, service->config.lpDisplayName) == 0)
|
2008-03-15 16:42:38 +01:00
|
|
|
return service;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2016-04-18 17:08:37 +02:00
|
|
|
struct process_entry *grab_process(struct process_entry *process)
|
|
|
|
{
|
|
|
|
if (process)
|
|
|
|
InterlockedIncrement(&process->ref_count);
|
|
|
|
return process;
|
|
|
|
}
|
|
|
|
|
2016-03-16 04:31:31 +01:00
|
|
|
void release_process(struct process_entry *process)
|
|
|
|
{
|
2016-04-20 14:51:30 +02:00
|
|
|
struct scmdatabase *db = process->db;
|
|
|
|
|
|
|
|
scmdatabase_lock(db);
|
2016-03-16 04:31:31 +01:00
|
|
|
if (InterlockedDecrement(&process->ref_count) == 0)
|
2016-04-20 14:51:30 +02:00
|
|
|
{
|
|
|
|
scmdatabase_remove_process(db, process);
|
2016-03-16 04:31:31 +01:00
|
|
|
free_process_entry(process);
|
2016-04-20 14:51:30 +02:00
|
|
|
}
|
|
|
|
scmdatabase_unlock(db);
|
2016-03-16 04:31:31 +01:00
|
|
|
}
|
|
|
|
|
2016-08-17 09:12:27 +02:00
|
|
|
struct service_entry *grab_service(struct service_entry *service)
|
|
|
|
{
|
|
|
|
if (service)
|
|
|
|
InterlockedIncrement(&service->ref_count);
|
|
|
|
return service;
|
|
|
|
}
|
|
|
|
|
2008-03-15 16:42:38 +01:00
|
|
|
void release_service(struct service_entry *service)
|
|
|
|
{
|
2016-03-16 04:30:39 +01:00
|
|
|
struct scmdatabase *db = service->db;
|
|
|
|
|
|
|
|
scmdatabase_lock(db);
|
2008-03-15 16:42:38 +01:00
|
|
|
if (InterlockedDecrement(&service->ref_count) == 0 && is_marked_for_delete(service))
|
2014-06-04 21:54:17 +02:00
|
|
|
{
|
2016-03-16 04:30:39 +01:00
|
|
|
scmdatabase_remove_service(db, service);
|
2008-03-15 16:42:38 +01:00
|
|
|
free_service_entry(service);
|
2014-06-04 21:54:17 +02:00
|
|
|
}
|
2016-03-16 04:30:39 +01:00
|
|
|
scmdatabase_unlock(db);
|
2008-03-15 16:42:38 +01:00
|
|
|
}
|
|
|
|
|
2008-03-28 18:08:12 +01:00
|
|
|
static DWORD scmdatabase_create(struct scmdatabase **db)
|
2008-03-15 10:38:45 +01:00
|
|
|
{
|
|
|
|
DWORD err;
|
|
|
|
|
2008-03-28 18:08:12 +01:00
|
|
|
*db = HeapAlloc(GetProcessHeap(), 0, sizeof(**db));
|
|
|
|
if (!*db)
|
|
|
|
return ERROR_NOT_ENOUGH_SERVER_MEMORY;
|
|
|
|
|
|
|
|
(*db)->service_start_lock = FALSE;
|
2016-04-20 14:51:30 +02:00
|
|
|
list_init(&(*db)->processes);
|
2008-03-28 18:08:12 +01:00
|
|
|
list_init(&(*db)->services);
|
|
|
|
|
|
|
|
InitializeCriticalSection(&(*db)->cs);
|
2011-10-27 12:36:16 +02:00
|
|
|
(*db)->cs.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": scmdatabase");
|
2008-03-28 18:08:12 +01:00
|
|
|
|
|
|
|
err = RegCreateKeyExW(HKEY_LOCAL_MACHINE, SZ_SERVICES_KEY, 0, NULL,
|
2009-12-04 16:58:20 +01:00
|
|
|
REG_OPTION_NON_VOLATILE, MAXIMUM_ALLOWED, NULL,
|
2008-03-28 18:08:12 +01:00
|
|
|
&(*db)->root_key, NULL);
|
|
|
|
if (err != ERROR_SUCCESS)
|
|
|
|
HeapFree(GetProcessHeap(), 0, *db);
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void scmdatabase_destroy(struct scmdatabase *db)
|
|
|
|
{
|
|
|
|
RegCloseKey(db->root_key);
|
2011-10-27 12:36:16 +02:00
|
|
|
db->cs.DebugInfo->Spare[0] = 0;
|
2008-03-28 18:08:12 +01:00
|
|
|
DeleteCriticalSection(&db->cs);
|
|
|
|
HeapFree(GetProcessHeap(), 0, db);
|
|
|
|
}
|
|
|
|
|
|
|
|
static DWORD scmdatabase_load_services(struct scmdatabase *db)
|
|
|
|
{
|
|
|
|
DWORD err;
|
|
|
|
int i;
|
2008-03-15 10:38:45 +01:00
|
|
|
|
|
|
|
for (i = 0; TRUE; i++)
|
|
|
|
{
|
|
|
|
WCHAR szName[MAX_SERVICE_NAME];
|
|
|
|
struct service_entry *entry;
|
|
|
|
HKEY hServiceKey;
|
|
|
|
|
2008-03-28 18:08:12 +01:00
|
|
|
err = RegEnumKeyW(db->root_key, i, szName, MAX_SERVICE_NAME);
|
2008-03-15 10:38:45 +01:00
|
|
|
if (err == ERROR_NO_MORE_ITEMS)
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (err != 0)
|
|
|
|
{
|
2022-02-07 08:39:43 +01:00
|
|
|
WINE_ERR("Error %ld reading key %d name - skipping\n", err, i);
|
2008-03-15 10:38:45 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2008-03-28 18:08:12 +01:00
|
|
|
err = service_create(szName, &entry);
|
|
|
|
if (err != ERROR_SUCCESS)
|
|
|
|
break;
|
2008-03-15 10:38:45 +01:00
|
|
|
|
|
|
|
WINE_TRACE("Loading service %s\n", wine_dbgstr_w(szName));
|
2009-12-04 16:58:20 +01:00
|
|
|
err = RegOpenKeyExW(db->root_key, szName, 0, KEY_READ, &hServiceKey);
|
2008-03-15 10:38:45 +01:00
|
|
|
if (err == ERROR_SUCCESS)
|
|
|
|
{
|
|
|
|
err = load_service_config(hServiceKey, entry);
|
|
|
|
RegCloseKey(hServiceKey);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (err != ERROR_SUCCESS)
|
|
|
|
{
|
2022-02-07 08:39:43 +01:00
|
|
|
WINE_ERR("Error %ld reading registry key for service %s - skipping\n", err, wine_dbgstr_w(szName));
|
2008-03-15 10:38:45 +01:00
|
|
|
free_service_entry(entry);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (entry->config.dwServiceType == 0)
|
|
|
|
{
|
|
|
|
/* Maybe an application only wrote some configuration in the service key. Continue silently */
|
|
|
|
WINE_TRACE("Even the service type not set for service %s - skipping\n", wine_dbgstr_w(szName));
|
|
|
|
free_service_entry(entry);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!validate_service_config(entry))
|
|
|
|
{
|
|
|
|
WINE_ERR("Invalid configuration of service %s - skipping\n", wine_dbgstr_w(szName));
|
|
|
|
free_service_entry(entry);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
entry->status.dwServiceType = entry->config.dwServiceType;
|
2008-03-28 18:08:12 +01:00
|
|
|
entry->db = db;
|
2008-03-15 10:38:45 +01:00
|
|
|
|
2008-03-28 18:08:12 +01:00
|
|
|
list_add_tail(&db->services, &entry->entry);
|
2016-03-16 04:30:56 +01:00
|
|
|
release_service(entry);
|
2008-03-15 10:38:45 +01:00
|
|
|
}
|
|
|
|
return ERROR_SUCCESS;
|
|
|
|
}
|
2008-03-15 09:49:42 +01:00
|
|
|
|
2018-04-09 13:29:02 +02:00
|
|
|
BOOL scmdatabase_lock_startup(struct scmdatabase *db, int timeout)
|
2008-03-28 18:08:12 +01:00
|
|
|
{
|
2018-04-09 13:29:02 +02:00
|
|
|
while (InterlockedCompareExchange(&db->service_start_lock, TRUE, FALSE))
|
|
|
|
{
|
|
|
|
if (timeout != INFINITE)
|
|
|
|
{
|
|
|
|
timeout -= 10;
|
|
|
|
if (timeout <= 0) return FALSE;
|
|
|
|
}
|
|
|
|
Sleep(10);
|
|
|
|
}
|
|
|
|
return TRUE;
|
2008-03-28 18:08:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void scmdatabase_unlock_startup(struct scmdatabase *db)
|
|
|
|
{
|
|
|
|
InterlockedCompareExchange(&db->service_start_lock, FALSE, TRUE);
|
|
|
|
}
|
|
|
|
|
2016-03-02 07:21:17 +01:00
|
|
|
void scmdatabase_lock(struct scmdatabase *db)
|
2008-03-28 18:08:12 +01:00
|
|
|
{
|
|
|
|
EnterCriticalSection(&db->cs);
|
|
|
|
}
|
|
|
|
|
|
|
|
void scmdatabase_unlock(struct scmdatabase *db)
|
|
|
|
{
|
|
|
|
LeaveCriticalSection(&db->cs);
|
|
|
|
}
|
|
|
|
|
2016-03-02 07:21:17 +01:00
|
|
|
void service_lock(struct service_entry *service)
|
2008-03-28 18:08:12 +01:00
|
|
|
{
|
|
|
|
EnterCriticalSection(&service->db->cs);
|
|
|
|
}
|
|
|
|
|
|
|
|
void service_unlock(struct service_entry *service)
|
|
|
|
{
|
|
|
|
LeaveCriticalSection(&service->db->cs);
|
|
|
|
}
|
|
|
|
|
2008-04-11 12:00:11 +02:00
|
|
|
/* only one service started at a time, so there is no race on the registry
|
|
|
|
* value here */
|
|
|
|
static LPWSTR service_get_pipe_name(void)
|
|
|
|
{
|
|
|
|
static const WCHAR format[] = { '\\','\\','.','\\','p','i','p','e','\\',
|
|
|
|
'n','e','t','\\','N','t','C','o','n','t','r','o','l','P','i','p','e','%','u',0};
|
2019-05-01 10:40:06 +02:00
|
|
|
static WCHAR name[ARRAY_SIZE(format) + 10]; /* lstrlenW("4294967295") */
|
2016-04-18 17:07:06 +02:00
|
|
|
static DWORD service_current = 0;
|
|
|
|
DWORD len, value = -1;
|
2008-04-11 12:00:11 +02:00
|
|
|
LONG ret;
|
|
|
|
DWORD type;
|
|
|
|
|
2016-04-18 17:07:06 +02:00
|
|
|
len = sizeof(value);
|
2008-04-11 12:00:11 +02:00
|
|
|
ret = RegQueryValueExW(service_current_key, NULL, NULL, &type,
|
2016-04-18 17:07:06 +02:00
|
|
|
(BYTE *)&value, &len);
|
|
|
|
if (ret == ERROR_SUCCESS && type == REG_DWORD)
|
|
|
|
service_current = max(service_current, value + 1);
|
|
|
|
RegSetValueExW(service_current_key, NULL, 0, REG_DWORD,
|
|
|
|
(BYTE *)&service_current, sizeof(service_current));
|
2019-05-01 10:40:06 +02:00
|
|
|
swprintf(name, ARRAY_SIZE(name), format, service_current);
|
2016-04-18 17:07:06 +02:00
|
|
|
service_current++;
|
2008-04-11 12:00:11 +02:00
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
2015-03-01 00:24:02 +01:00
|
|
|
static DWORD get_service_binary_path(const struct service_entry *service_entry, WCHAR **path)
|
2008-04-11 12:00:11 +02:00
|
|
|
{
|
2015-03-01 00:24:02 +01:00
|
|
|
DWORD size = ExpandEnvironmentStringsW(service_entry->config.lpBinaryPathName, NULL, 0);
|
2011-11-15 12:55:58 +01:00
|
|
|
|
2015-03-01 00:24:02 +01:00
|
|
|
*path = HeapAlloc(GetProcessHeap(), 0, size*sizeof(WCHAR));
|
|
|
|
if (!*path)
|
2011-04-23 22:20:54 +02:00
|
|
|
return ERROR_NOT_ENOUGH_SERVER_MEMORY;
|
2015-03-01 00:24:02 +01:00
|
|
|
|
|
|
|
ExpandEnvironmentStringsW(service_entry->config.lpBinaryPathName, *path, size);
|
2010-06-01 13:38:21 +02:00
|
|
|
|
2015-03-01 00:24:02 +01:00
|
|
|
/* if service image is configured to systemdir, redirect it to wow64 systemdir */
|
2019-08-19 03:02:40 +02:00
|
|
|
if (service_entry->is_wow64 && !(service_entry->config.dwServiceType & (SERVICE_FILE_SYSTEM_DRIVER | SERVICE_KERNEL_DRIVER)))
|
2015-03-01 00:24:02 +01:00
|
|
|
{
|
|
|
|
WCHAR system_dir[MAX_PATH], *redirected;
|
|
|
|
DWORD len;
|
|
|
|
|
|
|
|
GetSystemDirectoryW( system_dir, MAX_PATH );
|
2019-05-01 10:40:06 +02:00
|
|
|
len = lstrlenW( system_dir );
|
2015-03-01 00:24:02 +01:00
|
|
|
|
2019-05-01 10:40:06 +02:00
|
|
|
if (wcsnicmp( system_dir, *path, len ))
|
2015-03-01 00:24:02 +01:00
|
|
|
return ERROR_SUCCESS;
|
|
|
|
|
|
|
|
GetSystemWow64DirectoryW( system_dir, MAX_PATH );
|
|
|
|
|
2019-05-01 10:40:06 +02:00
|
|
|
redirected = HeapAlloc( GetProcessHeap(), 0, (lstrlenW( *path ) + lstrlenW( system_dir ))*sizeof(WCHAR));
|
2015-03-01 00:24:02 +01:00
|
|
|
if (!redirected)
|
2011-04-23 22:20:54 +02:00
|
|
|
{
|
2015-03-01 00:24:02 +01:00
|
|
|
HeapFree( GetProcessHeap(), 0, *path );
|
2008-04-11 12:00:11 +02:00
|
|
|
return ERROR_NOT_ENOUGH_SERVER_MEMORY;
|
2011-04-23 22:20:54 +02:00
|
|
|
}
|
2015-03-01 00:24:02 +01:00
|
|
|
|
2019-05-01 10:40:06 +02:00
|
|
|
lstrcpyW( redirected, system_dir );
|
|
|
|
lstrcatW( redirected, &(*path)[len] );
|
2015-03-01 00:24:02 +01:00
|
|
|
HeapFree( GetProcessHeap(), 0, *path );
|
|
|
|
*path = redirected;
|
|
|
|
TRACE("redirected to %s\n", debugstr_w(redirected));
|
|
|
|
}
|
|
|
|
|
|
|
|
return ERROR_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2018-04-30 20:59:54 +02:00
|
|
|
static DWORD get_winedevice_binary_path(struct service_entry *service_entry, WCHAR **path, BOOL *is_wow64)
|
2016-09-01 07:39:29 +02:00
|
|
|
{
|
|
|
|
static const WCHAR winedeviceW[] = {'\\','w','i','n','e','d','e','v','i','c','e','.','e','x','e',0};
|
|
|
|
WCHAR system_dir[MAX_PATH];
|
|
|
|
DWORD type;
|
|
|
|
|
|
|
|
if (!is_win64)
|
|
|
|
*is_wow64 = FALSE;
|
|
|
|
else if (GetBinaryTypeW(*path, &type))
|
|
|
|
*is_wow64 = (type == SCS_32BIT_BINARY);
|
|
|
|
else
|
2018-04-30 20:59:54 +02:00
|
|
|
*is_wow64 = service_entry->is_wow64;
|
2016-09-01 07:39:29 +02:00
|
|
|
|
|
|
|
GetSystemDirectoryW(system_dir, MAX_PATH);
|
|
|
|
HeapFree(GetProcessHeap(), 0, *path);
|
2019-05-01 10:40:06 +02:00
|
|
|
if (!(*path = HeapAlloc(GetProcessHeap(), 0, lstrlenW(system_dir) * sizeof(WCHAR) + sizeof(winedeviceW))))
|
2016-09-01 07:39:29 +02:00
|
|
|
return ERROR_NOT_ENOUGH_SERVER_MEMORY;
|
|
|
|
|
2019-05-01 10:40:06 +02:00
|
|
|
lstrcpyW(*path, system_dir);
|
|
|
|
lstrcatW(*path, winedeviceW);
|
2016-09-01 07:39:29 +02:00
|
|
|
return ERROR_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2016-09-01 07:39:34 +02:00
|
|
|
static struct process_entry *get_winedevice_process(struct service_entry *service_entry, WCHAR *path, BOOL is_wow64)
|
|
|
|
{
|
|
|
|
struct service_entry *winedevice_entry;
|
|
|
|
|
|
|
|
if (!service_entry->config.lpLoadOrderGroup)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
LIST_FOR_EACH_ENTRY(winedevice_entry, &service_entry->db->services, struct service_entry, entry)
|
|
|
|
{
|
|
|
|
if (winedevice_entry->status.dwCurrentState != SERVICE_START_PENDING &&
|
|
|
|
winedevice_entry->status.dwCurrentState != SERVICE_RUNNING) continue;
|
|
|
|
if (!winedevice_entry->process) continue;
|
|
|
|
|
|
|
|
if (winedevice_entry->is_wow64 != is_wow64) continue;
|
2016-09-29 13:32:59 +02:00
|
|
|
if (!winedevice_entry->config.lpBinaryPathName) continue;
|
2019-05-01 10:40:06 +02:00
|
|
|
if (lstrcmpW(winedevice_entry->config.lpBinaryPathName, path)) continue;
|
2016-09-01 07:39:34 +02:00
|
|
|
|
|
|
|
if (!winedevice_entry->config.lpLoadOrderGroup) continue;
|
2019-05-01 10:40:06 +02:00
|
|
|
if (lstrcmpW(winedevice_entry->config.lpLoadOrderGroup, service_entry->config.lpLoadOrderGroup)) continue;
|
2016-09-01 07:39:34 +02:00
|
|
|
|
|
|
|
return grab_process(winedevice_entry->process);
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2016-09-01 07:39:29 +02:00
|
|
|
static DWORD add_winedevice_service(const struct service_entry *service, WCHAR *path, BOOL is_wow64,
|
|
|
|
struct service_entry **entry)
|
|
|
|
{
|
|
|
|
static const WCHAR format[] = {'W','i','n','e','d','e','v','i','c','e','%','u',0};
|
2019-05-01 10:40:06 +02:00
|
|
|
static WCHAR name[ARRAY_SIZE(format) + 10]; /* lstrlenW("4294967295") */
|
2016-09-01 07:39:29 +02:00
|
|
|
static DWORD current = 0;
|
|
|
|
struct scmdatabase *db = service->db;
|
|
|
|
DWORD err;
|
|
|
|
|
|
|
|
for (;;)
|
|
|
|
{
|
2019-05-01 10:40:06 +02:00
|
|
|
swprintf(name, ARRAY_SIZE(name), format, ++current);
|
2016-09-01 07:39:29 +02:00
|
|
|
if (!scmdatabase_find_service(db, name)) break;
|
|
|
|
}
|
|
|
|
|
|
|
|
err = service_create(name, entry);
|
|
|
|
if (err != ERROR_SUCCESS)
|
|
|
|
return err;
|
|
|
|
|
|
|
|
(*entry)->is_wow64 = is_wow64;
|
|
|
|
(*entry)->config.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
|
|
|
|
(*entry)->config.dwStartType = SERVICE_DEMAND_START;
|
|
|
|
(*entry)->status.dwServiceType = (*entry)->config.dwServiceType;
|
|
|
|
|
|
|
|
if (!((*entry)->config.lpBinaryPathName = strdupW(path)))
|
|
|
|
goto error;
|
|
|
|
if (!((*entry)->config.lpServiceStartName = strdupW(SZ_LOCAL_SYSTEM)))
|
|
|
|
goto error;
|
|
|
|
if (!((*entry)->config.lpDisplayName = strdupW(name)))
|
|
|
|
goto error;
|
|
|
|
if (service->config.lpLoadOrderGroup &&
|
|
|
|
!((*entry)->config.lpLoadOrderGroup = strdupW(service->config.lpLoadOrderGroup)))
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
(*entry)->db = db;
|
|
|
|
|
|
|
|
list_add_tail(&db->services, &(*entry)->entry);
|
|
|
|
mark_for_delete(*entry);
|
|
|
|
return ERROR_SUCCESS;
|
|
|
|
|
|
|
|
error:
|
|
|
|
free_service_entry(*entry);
|
|
|
|
return ERROR_NOT_ENOUGH_SERVER_MEMORY;
|
|
|
|
}
|
|
|
|
|
|
|
|
static DWORD service_start_process(struct service_entry *service_entry, struct process_entry **new_process,
|
|
|
|
BOOL *shared_process)
|
2015-03-01 00:24:02 +01:00
|
|
|
{
|
2016-04-20 14:51:30 +02:00
|
|
|
struct process_entry *process;
|
2015-03-01 00:24:02 +01:00
|
|
|
PROCESS_INFORMATION pi;
|
|
|
|
STARTUPINFOW si;
|
2016-09-01 07:39:29 +02:00
|
|
|
BOOL is_wow64 = FALSE;
|
2016-09-01 07:39:21 +02:00
|
|
|
HANDLE token;
|
2016-09-01 07:39:29 +02:00
|
|
|
WCHAR *path;
|
2015-03-01 00:24:02 +01:00
|
|
|
DWORD err;
|
|
|
|
BOOL r;
|
|
|
|
|
2016-03-02 07:21:17 +01:00
|
|
|
service_lock(service_entry);
|
2015-03-01 00:24:02 +01:00
|
|
|
|
2016-04-20 14:51:30 +02:00
|
|
|
if ((process = service_entry->process))
|
|
|
|
{
|
|
|
|
if (WaitForSingleObject(process->process, 0) == WAIT_TIMEOUT)
|
|
|
|
{
|
|
|
|
service_unlock(service_entry);
|
|
|
|
return ERROR_SERVICE_ALREADY_RUNNING;
|
|
|
|
}
|
|
|
|
service_entry->process = NULL;
|
2016-08-18 09:27:58 +02:00
|
|
|
process->use_count--;
|
|
|
|
release_process(process);
|
2016-04-20 14:51:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
service_entry->force_shutdown = FALSE;
|
|
|
|
|
2016-09-01 07:39:29 +02:00
|
|
|
if ((err = get_service_binary_path(service_entry, &path)))
|
2016-04-20 14:51:30 +02:00
|
|
|
{
|
|
|
|
service_unlock(service_entry);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2016-09-01 07:39:29 +02:00
|
|
|
if (service_entry->config.dwServiceType == SERVICE_KERNEL_DRIVER ||
|
|
|
|
service_entry->config.dwServiceType == SERVICE_FILE_SYSTEM_DRIVER)
|
2015-03-01 00:24:02 +01:00
|
|
|
{
|
2016-09-01 07:39:29 +02:00
|
|
|
struct service_entry *winedevice_entry;
|
|
|
|
WCHAR *group;
|
|
|
|
|
2018-04-30 20:59:54 +02:00
|
|
|
if ((err = get_winedevice_binary_path(service_entry, &path, &is_wow64)))
|
2016-09-01 07:39:29 +02:00
|
|
|
{
|
|
|
|
service_unlock(service_entry);
|
|
|
|
HeapFree(GetProcessHeap(), 0, path);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2016-09-01 07:39:34 +02:00
|
|
|
if ((process = get_winedevice_process(service_entry, path, is_wow64)))
|
|
|
|
{
|
|
|
|
HeapFree(GetProcessHeap(), 0, path);
|
|
|
|
goto found;
|
|
|
|
}
|
|
|
|
|
2016-09-01 07:39:29 +02:00
|
|
|
err = add_winedevice_service(service_entry, path, is_wow64, &winedevice_entry);
|
|
|
|
HeapFree(GetProcessHeap(), 0, path);
|
|
|
|
if (err != ERROR_SUCCESS)
|
|
|
|
{
|
|
|
|
service_unlock(service_entry);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
group = strdupW(winedevice_entry->config.lpLoadOrderGroup);
|
2015-03-01 00:24:02 +01:00
|
|
|
service_unlock(service_entry);
|
2016-09-01 07:39:29 +02:00
|
|
|
|
|
|
|
err = service_start(winedevice_entry, group != NULL, (const WCHAR **)&group);
|
|
|
|
HeapFree(GetProcessHeap(), 0, group);
|
|
|
|
if (err != ERROR_SUCCESS)
|
|
|
|
{
|
|
|
|
release_service(winedevice_entry);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
service_lock(service_entry);
|
|
|
|
process = grab_process(winedevice_entry->process);
|
|
|
|
release_service(winedevice_entry);
|
|
|
|
|
|
|
|
if (!process)
|
|
|
|
{
|
|
|
|
service_unlock(service_entry);
|
|
|
|
return ERROR_SERVICE_REQUEST_TIMEOUT;
|
|
|
|
}
|
|
|
|
|
2016-09-01 07:39:34 +02:00
|
|
|
found:
|
2016-09-01 07:39:29 +02:00
|
|
|
service_entry->status.dwCurrentState = SERVICE_START_PENDING;
|
|
|
|
service_entry->status.dwControlsAccepted = 0;
|
|
|
|
ResetEvent(service_entry->status_changed_event);
|
|
|
|
|
|
|
|
service_entry->process = grab_process(process);
|
|
|
|
service_entry->shared_process = *shared_process = TRUE;
|
|
|
|
process->use_count++;
|
|
|
|
service_unlock(service_entry);
|
|
|
|
|
|
|
|
err = WaitForSingleObject(process->control_mutex, 30000);
|
|
|
|
if (err != WAIT_OBJECT_0)
|
|
|
|
{
|
|
|
|
release_process(process);
|
|
|
|
return ERROR_SERVICE_REQUEST_TIMEOUT;
|
|
|
|
}
|
|
|
|
|
|
|
|
*new_process = process;
|
|
|
|
return ERROR_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((err = process_create(service_get_pipe_name(), &process)))
|
|
|
|
{
|
2022-02-07 08:39:43 +01:00
|
|
|
WINE_ERR("failed to create process object for %s, error = %lu\n",
|
2016-09-01 07:39:29 +02:00
|
|
|
wine_dbgstr_w(service_entry->name), err);
|
|
|
|
service_unlock(service_entry);
|
|
|
|
HeapFree(GetProcessHeap(), 0, path);
|
2015-03-01 00:24:02 +01:00
|
|
|
return err;
|
2008-04-11 12:00:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ZeroMemory(&si, sizeof(STARTUPINFOW));
|
|
|
|
si.cb = sizeof(STARTUPINFOW);
|
|
|
|
if (!(service_entry->config.dwServiceType & SERVICE_INTERACTIVE_PROCESS))
|
|
|
|
{
|
|
|
|
static WCHAR desktopW[] = {'_','_','w','i','n','e','s','e','r','v','i','c','e','_','w','i','n','s','t','a','t','i','o','n','\\','D','e','f','a','u','l','t',0};
|
|
|
|
si.lpDesktop = desktopW;
|
|
|
|
}
|
|
|
|
|
2016-09-01 07:39:21 +02:00
|
|
|
if (!environment && OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY | TOKEN_DUPLICATE, &token))
|
|
|
|
{
|
2021-04-08 12:23:09 +02:00
|
|
|
WCHAR val[16];
|
2016-09-01 07:39:21 +02:00
|
|
|
CreateEnvironmentBlock(&environment, token, FALSE);
|
2021-04-08 12:23:09 +02:00
|
|
|
if (GetEnvironmentVariableW( L"WINEBOOTSTRAPMODE", val, ARRAY_SIZE(val) ))
|
|
|
|
{
|
|
|
|
UNICODE_STRING name, value;
|
|
|
|
|
|
|
|
RtlInitUnicodeString( &name, L"WINEBOOTSTRAPMODE" );
|
|
|
|
RtlInitUnicodeString( &value, val );
|
|
|
|
RtlSetEnvironmentVariable( (WCHAR **)&environment, &name, &value );
|
|
|
|
}
|
2016-09-01 07:39:21 +02:00
|
|
|
CloseHandle(token);
|
|
|
|
}
|
|
|
|
|
2008-04-11 12:00:11 +02:00
|
|
|
service_entry->status.dwCurrentState = SERVICE_START_PENDING;
|
2016-08-19 08:01:03 +02:00
|
|
|
service_entry->status.dwControlsAccepted = 0;
|
|
|
|
ResetEvent(service_entry->status_changed_event);
|
|
|
|
|
2016-04-20 14:51:30 +02:00
|
|
|
scmdatabase_add_process(service_entry->db, process);
|
2016-08-11 07:22:55 +02:00
|
|
|
service_entry->process = grab_process(process);
|
2016-09-01 07:39:29 +02:00
|
|
|
service_entry->shared_process = *shared_process = FALSE;
|
2016-08-18 09:27:58 +02:00
|
|
|
process->use_count++;
|
2008-04-11 12:00:11 +02:00
|
|
|
service_unlock(service_entry);
|
|
|
|
|
2016-09-01 07:39:21 +02:00
|
|
|
r = CreateProcessW(NULL, path, NULL, NULL, FALSE, CREATE_UNICODE_ENVIRONMENT, environment, NULL, &si, &pi);
|
2016-09-01 07:39:29 +02:00
|
|
|
HeapFree(GetProcessHeap(), 0, path);
|
2008-04-11 12:00:11 +02:00
|
|
|
if (!r)
|
|
|
|
{
|
2016-04-20 14:51:30 +02:00
|
|
|
err = GetLastError();
|
2016-08-11 07:22:27 +02:00
|
|
|
process_terminate(process);
|
2016-08-11 07:22:55 +02:00
|
|
|
release_process(process);
|
2016-04-20 14:51:30 +02:00
|
|
|
return err;
|
2008-04-11 12:00:11 +02:00
|
|
|
}
|
|
|
|
|
2016-08-11 07:21:23 +02:00
|
|
|
process->process_id = pi.dwProcessId;
|
2016-04-20 14:51:30 +02:00
|
|
|
process->process = pi.hProcess;
|
2008-04-11 12:00:11 +02:00
|
|
|
CloseHandle( pi.hThread );
|
|
|
|
|
2016-04-20 14:51:30 +02:00
|
|
|
*new_process = process;
|
2008-04-11 12:00:11 +02:00
|
|
|
return ERROR_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2016-08-19 08:01:03 +02:00
|
|
|
static DWORD service_wait_for_startup(struct service_entry *service, struct process_entry *process)
|
2008-04-11 12:00:11 +02:00
|
|
|
{
|
2016-08-19 08:01:03 +02:00
|
|
|
HANDLE handles[2] = { service->status_changed_event, process->process };
|
|
|
|
DWORD result;
|
2014-04-07 07:25:51 +02:00
|
|
|
|
2016-08-19 08:01:03 +02:00
|
|
|
result = WaitForMultipleObjects( 2, handles, FALSE, service_pipe_timeout );
|
|
|
|
if (result != WAIT_OBJECT_0)
|
|
|
|
return ERROR_SERVICE_REQUEST_TIMEOUT;
|
2014-04-19 11:55:21 +02:00
|
|
|
|
2016-03-16 04:33:09 +01:00
|
|
|
service_lock(service);
|
2016-08-19 08:01:03 +02:00
|
|
|
result = service->status.dwCurrentState;
|
2016-03-16 04:33:09 +01:00
|
|
|
service_unlock(service);
|
|
|
|
|
2016-08-19 08:01:03 +02:00
|
|
|
return (result == SERVICE_START_PENDING || result == SERVICE_RUNNING) ?
|
2016-03-16 04:33:09 +01:00
|
|
|
ERROR_SUCCESS : ERROR_SERVICE_REQUEST_TIMEOUT;
|
2008-04-11 12:00:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************
|
2016-03-16 04:32:08 +01:00
|
|
|
* process_send_start_message
|
2008-04-11 12:00:11 +02:00
|
|
|
*/
|
2016-09-01 07:39:29 +02:00
|
|
|
static DWORD process_send_start_message(struct process_entry *process, BOOL shared_process,
|
|
|
|
const WCHAR *name, const WCHAR **argv, DWORD argc)
|
2008-04-11 12:00:11 +02:00
|
|
|
{
|
2011-05-20 12:31:27 +02:00
|
|
|
OVERLAPPED overlapped;
|
2011-05-20 11:49:02 +02:00
|
|
|
DWORD i, len, result;
|
2016-08-31 08:46:20 +02:00
|
|
|
WCHAR *str, *p;
|
2008-04-11 12:00:11 +02:00
|
|
|
|
2022-02-07 08:39:43 +01:00
|
|
|
WINE_TRACE("%p %s %p %ld\n", process, wine_dbgstr_w(name), argv, argc);
|
2008-04-11 12:00:11 +02:00
|
|
|
|
2016-03-03 06:28:16 +01:00
|
|
|
overlapped.hEvent = process->overlapped_event;
|
|
|
|
if (!ConnectNamedPipe(process->control_pipe, &overlapped))
|
2008-04-11 12:00:11 +02:00
|
|
|
{
|
2011-05-20 12:31:27 +02:00
|
|
|
if (GetLastError() == ERROR_IO_PENDING)
|
|
|
|
{
|
|
|
|
HANDLE handles[2];
|
2016-03-03 06:28:16 +01:00
|
|
|
handles[0] = process->overlapped_event;
|
2016-03-16 04:32:08 +01:00
|
|
|
handles[1] = process->process;
|
2011-05-20 12:31:27 +02:00
|
|
|
if (WaitForMultipleObjects( 2, handles, FALSE, service_pipe_timeout ) != WAIT_OBJECT_0)
|
2016-03-03 06:28:16 +01:00
|
|
|
CancelIo(process->control_pipe);
|
2014-07-25 18:49:50 +02:00
|
|
|
if (!HasOverlappedIoCompleted( &overlapped ))
|
2011-05-20 12:31:27 +02:00
|
|
|
{
|
2016-03-16 04:32:08 +01:00
|
|
|
WINE_ERR("service %s failed to start\n", wine_dbgstr_w(name));
|
2016-08-19 08:00:41 +02:00
|
|
|
return ERROR_SERVICE_REQUEST_TIMEOUT;
|
2011-05-20 12:31:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (GetLastError() != ERROR_PIPE_CONNECTED)
|
|
|
|
{
|
|
|
|
WINE_ERR("pipe connect failed\n");
|
2016-08-19 08:00:41 +02:00
|
|
|
return ERROR_SERVICE_REQUEST_TIMEOUT;
|
2011-05-20 12:31:27 +02:00
|
|
|
}
|
2008-04-11 12:00:11 +02:00
|
|
|
}
|
|
|
|
|
2019-05-01 10:40:06 +02:00
|
|
|
len = lstrlenW(name) + 1;
|
2016-08-31 08:46:20 +02:00
|
|
|
for (i = 0; i < argc; i++)
|
2019-05-01 10:40:06 +02:00
|
|
|
len += lstrlenW(argv[i])+1;
|
2016-08-10 08:30:29 +02:00
|
|
|
len = (len + 1) * sizeof(WCHAR);
|
2008-04-11 12:00:11 +02:00
|
|
|
|
2016-08-31 08:46:20 +02:00
|
|
|
if (!(str = HeapAlloc(GetProcessHeap(), 0, len)))
|
|
|
|
return ERROR_NOT_ENOUGH_SERVER_MEMORY;
|
2008-04-11 12:00:11 +02:00
|
|
|
|
2016-08-31 08:46:20 +02:00
|
|
|
p = str;
|
2019-05-01 10:40:06 +02:00
|
|
|
lstrcpyW(p, name);
|
|
|
|
p += lstrlenW(name) + 1;
|
2016-08-31 08:46:20 +02:00
|
|
|
for (i = 0; i < argc; i++)
|
2008-04-11 12:00:11 +02:00
|
|
|
{
|
2019-05-01 10:40:06 +02:00
|
|
|
lstrcpyW(p, argv[i]);
|
|
|
|
p += lstrlenW(p) + 1;
|
2008-04-11 12:00:11 +02:00
|
|
|
}
|
2016-08-31 08:46:20 +02:00
|
|
|
*p = 0;
|
2008-04-11 12:00:11 +02:00
|
|
|
|
2016-09-01 07:39:29 +02:00
|
|
|
if (!process_send_control(process, shared_process, name,
|
|
|
|
SERVICE_CONTROL_START, (const BYTE *)str, len, &result))
|
2016-08-19 08:00:41 +02:00
|
|
|
result = ERROR_SERVICE_REQUEST_TIMEOUT;
|
2008-04-11 12:00:11 +02:00
|
|
|
|
2016-08-31 08:46:20 +02:00
|
|
|
HeapFree(GetProcessHeap(), 0, str);
|
2016-08-19 08:00:41 +02:00
|
|
|
return result;
|
2008-04-11 12:00:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
DWORD service_start(struct service_entry *service, DWORD service_argc, LPCWSTR *service_argv)
|
|
|
|
{
|
2016-04-20 14:51:30 +02:00
|
|
|
struct process_entry *process = NULL;
|
2016-09-01 07:39:29 +02:00
|
|
|
BOOL shared_process;
|
2008-04-11 12:00:11 +02:00
|
|
|
DWORD err;
|
|
|
|
|
2016-09-01 07:39:29 +02:00
|
|
|
err = service_start_process(service, &process, &shared_process);
|
2016-04-20 14:51:30 +02:00
|
|
|
if (err == ERROR_SUCCESS)
|
2011-08-29 19:52:45 +02:00
|
|
|
{
|
2016-09-01 07:39:29 +02:00
|
|
|
err = process_send_start_message(process, shared_process, service->name, service_argv, service_argc);
|
2008-04-11 12:00:11 +02:00
|
|
|
|
2011-08-29 19:52:45 +02:00
|
|
|
if (err == ERROR_SUCCESS)
|
2016-08-19 08:01:03 +02:00
|
|
|
err = service_wait_for_startup(service, process);
|
2016-04-20 14:51:30 +02:00
|
|
|
|
2016-09-01 07:39:29 +02:00
|
|
|
if (err != ERROR_SUCCESS)
|
|
|
|
{
|
|
|
|
service_lock(service);
|
2019-05-27 17:36:17 +02:00
|
|
|
if (service->process)
|
|
|
|
{
|
|
|
|
service->status.dwCurrentState = SERVICE_STOPPED;
|
|
|
|
service->process = NULL;
|
|
|
|
if (!--process->use_count) process_terminate(process);
|
|
|
|
release_process(process);
|
|
|
|
}
|
2016-09-01 07:39:29 +02:00
|
|
|
service_unlock(service);
|
|
|
|
}
|
2016-08-11 07:22:55 +02:00
|
|
|
|
2016-09-01 07:39:29 +02:00
|
|
|
ReleaseMutex(process->control_mutex);
|
2016-08-11 07:22:55 +02:00
|
|
|
release_process(process);
|
2008-04-11 12:00:11 +02:00
|
|
|
}
|
|
|
|
|
2022-02-07 08:39:43 +01:00
|
|
|
WINE_TRACE("returning %ld\n", err);
|
2008-04-11 12:00:11 +02:00
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2016-04-20 14:51:30 +02:00
|
|
|
void process_terminate(struct process_entry *process)
|
|
|
|
{
|
|
|
|
struct scmdatabase *db = process->db;
|
|
|
|
struct service_entry *service;
|
|
|
|
|
|
|
|
scmdatabase_lock(db);
|
|
|
|
TerminateProcess(process->process, 0);
|
|
|
|
LIST_FOR_EACH_ENTRY(service, &db->services, struct service_entry, entry)
|
|
|
|
{
|
|
|
|
if (service->process != process) continue;
|
|
|
|
service->status.dwCurrentState = SERVICE_STOPPED;
|
|
|
|
service->process = NULL;
|
2016-08-18 09:27:58 +02:00
|
|
|
process->use_count--;
|
2016-08-11 07:21:12 +02:00
|
|
|
release_process(process);
|
2016-04-20 14:51:30 +02:00
|
|
|
}
|
|
|
|
scmdatabase_unlock(db);
|
|
|
|
}
|
|
|
|
|
2011-05-20 12:33:58 +02:00
|
|
|
static void load_registry_parameters(void)
|
|
|
|
{
|
|
|
|
static const WCHAR controlW[] =
|
|
|
|
{ 'S','y','s','t','e','m','\\',
|
|
|
|
'C','u','r','r','e','n','t','C','o','n','t','r','o','l','S','e','t','\\',
|
|
|
|
'C','o','n','t','r','o','l',0 };
|
|
|
|
static const WCHAR pipetimeoutW[] =
|
|
|
|
{'S','e','r','v','i','c','e','s','P','i','p','e','T','i','m','e','o','u','t',0};
|
|
|
|
static const WCHAR killtimeoutW[] =
|
|
|
|
{'W','a','i','t','T','o','K','i','l','l','S','e','r','v','i','c','e','T','i','m','e','o','u','t',0};
|
2019-06-20 17:58:45 +02:00
|
|
|
static const WCHAR autostartdelayW[] =
|
|
|
|
{'A','u','t','o','S','t','a','r','t','D','e','l','a','y',0};
|
2011-05-20 12:33:58 +02:00
|
|
|
HKEY key;
|
|
|
|
WCHAR buffer[64];
|
|
|
|
DWORD type, count, val;
|
|
|
|
|
|
|
|
if (RegOpenKeyW( HKEY_LOCAL_MACHINE, controlW, &key )) return;
|
|
|
|
|
|
|
|
count = sizeof(buffer);
|
|
|
|
if (!RegQueryValueExW( key, pipetimeoutW, NULL, &type, (BYTE *)buffer, &count ) &&
|
2019-05-01 10:40:06 +02:00
|
|
|
type == REG_SZ && (val = wcstol( buffer, NULL, 10 )))
|
2011-05-20 12:33:58 +02:00
|
|
|
service_pipe_timeout = val;
|
|
|
|
|
|
|
|
count = sizeof(buffer);
|
|
|
|
if (!RegQueryValueExW( key, killtimeoutW, NULL, &type, (BYTE *)buffer, &count ) &&
|
2019-05-01 10:40:06 +02:00
|
|
|
type == REG_SZ && (val = wcstol( buffer, NULL, 10 )))
|
2011-05-20 12:33:58 +02:00
|
|
|
service_kill_timeout = val;
|
|
|
|
|
2019-06-20 17:58:45 +02:00
|
|
|
count = sizeof(val);
|
|
|
|
if (!RegQueryValueExW( key, autostartdelayW, NULL, &type, (BYTE *)&val, &count ) && type == REG_DWORD)
|
|
|
|
autostart_delay = val;
|
|
|
|
|
2011-05-20 12:33:58 +02:00
|
|
|
RegCloseKey( key );
|
|
|
|
}
|
2008-04-11 12:00:11 +02:00
|
|
|
|
2019-10-17 17:05:47 +02:00
|
|
|
int __cdecl main(int argc, char *argv[])
|
2008-03-15 09:49:42 +01:00
|
|
|
{
|
2016-04-18 17:07:06 +02:00
|
|
|
static const WCHAR service_current_key_str[] = { 'S','Y','S','T','E','M','\\',
|
|
|
|
'C','u','r','r','e','n','t','C','o','n','t','r','o','l','S','e','t','\\',
|
|
|
|
'C','o','n','t','r','o','l','\\',
|
|
|
|
'S','e','r','v','i','c','e','C','u','r','r','e','n','t',0};
|
2008-03-28 17:56:00 +01:00
|
|
|
static const WCHAR svcctl_started_event[] = SVCCTL_STARTED_EVENT;
|
2016-09-01 07:39:21 +02:00
|
|
|
HANDLE started_event;
|
2008-03-15 10:38:45 +01:00
|
|
|
DWORD err;
|
2011-05-20 12:33:58 +02:00
|
|
|
|
2016-08-18 09:38:24 +02:00
|
|
|
started_event = CreateEventW(NULL, TRUE, FALSE, svcctl_started_event);
|
2016-04-15 10:03:52 +02:00
|
|
|
|
2016-04-18 17:07:06 +02:00
|
|
|
err = RegCreateKeyExW(HKEY_LOCAL_MACHINE, service_current_key_str, 0,
|
|
|
|
NULL, REG_OPTION_VOLATILE, KEY_SET_VALUE | KEY_QUERY_VALUE, NULL,
|
|
|
|
&service_current_key, NULL);
|
|
|
|
if (err != ERROR_SUCCESS)
|
|
|
|
return err;
|
|
|
|
|
2011-05-20 12:33:58 +02:00
|
|
|
load_registry_parameters();
|
2008-03-28 18:08:12 +01:00
|
|
|
err = scmdatabase_create(&active_database);
|
|
|
|
if (err != ERROR_SUCCESS)
|
2008-03-15 10:38:45 +01:00
|
|
|
return err;
|
2008-03-28 18:08:12 +01:00
|
|
|
if ((err = scmdatabase_load_services(active_database)) != ERROR_SUCCESS)
|
|
|
|
return err;
|
2008-04-11 11:59:51 +02:00
|
|
|
if ((err = RPC_Init()) == ERROR_SUCCESS)
|
2008-04-11 12:00:11 +02:00
|
|
|
{
|
|
|
|
scmdatabase_autostart_services(active_database);
|
2016-08-18 09:38:24 +02:00
|
|
|
SetEvent(started_event);
|
|
|
|
WaitForSingleObject(exit_event, INFINITE);
|
2011-11-30 15:29:01 +01:00
|
|
|
scmdatabase_wait_terminate(active_database);
|
2019-06-20 17:58:45 +02:00
|
|
|
if (delayed_autostart_cleanup)
|
|
|
|
{
|
|
|
|
CloseThreadpoolCleanupGroupMembers(delayed_autostart_cleanup, TRUE, NULL);
|
|
|
|
CloseThreadpoolCleanupGroup(delayed_autostart_cleanup);
|
|
|
|
}
|
2016-08-18 09:38:24 +02:00
|
|
|
RPC_Stop();
|
2008-04-11 12:00:11 +02:00
|
|
|
}
|
2008-03-28 18:08:12 +01:00
|
|
|
scmdatabase_destroy(active_database);
|
2016-09-01 07:39:21 +02:00
|
|
|
if (environment)
|
|
|
|
DestroyEnvironmentBlock(environment);
|
2011-11-30 15:29:01 +01:00
|
|
|
|
2022-02-07 08:39:43 +01:00
|
|
|
WINE_TRACE("services.exe exited with code %ld\n", err);
|
2008-03-28 18:08:12 +01:00
|
|
|
return err;
|
2008-03-15 09:49:42 +01:00
|
|
|
}
|