svchost: Added base implementation of svchost (1/26)
Roy Shea
roy at cs.hmc.edu
Mon Nov 19 18:20:07 CST 2007
---
programs/svchost/Makefile.in | 15 ++
programs/svchost/svchost.c | 369 ++++++++++++++++++++++++++++++++++++++++++
2 files changed, 384 insertions(+), 0 deletions(-)
create mode 100644 programs/svchost/Makefile.in
create mode 100644 programs/svchost/svchost.c
diff --git a/programs/svchost/Makefile.in b/programs/svchost/Makefile.in
new file mode 100644
index 0000000..8d2ceef
--- /dev/null
+++ b/programs/svchost/Makefile.in
@@ -0,0 +1,15 @@
+TOPSRCDIR = @top_srcdir@
+TOPOBJDIR = ../..
+SRCDIR = @srcdir@
+VPATH = @srcdir@
+MODULE = svchost.exe
+APPMODE =
+IMPORTS = advapi32 kernel32
+
+C_SRCS = \
+ svchost.c
+
+ at MAKE_PROG_RULES@
+
+ at DEPENDENCIES@ # everything below this line is overwritten by make depend
+
diff --git a/programs/svchost/svchost.c b/programs/svchost/svchost.c
new file mode 100644
index 0000000..90485fc
--- /dev/null
+++ b/programs/svchost/svchost.c
@@ -0,0 +1,369 @@
+/*
+ * Implementation of svchost.exe
+ *
+ * Copyright 2007 Google (Roy Shea)
+ *
+ * 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
+ */
+
+/* Usage:
+ * Starting a service group:
+ *
+ * svchost /k service_group_name
+ */
+
+#include <stdarg.h>
+
+#include "windef.h"
+#include "winbase.h"
+#include "winreg.h"
+#include "winsvc.h"
+#include "wine/debug.h"
+
+WINE_DEFAULT_DEBUG_CHANNEL(svchost);
+
+/* Static strings used throughout svchost */
+static const CHAR kd[] = "-k";
+
+static const CHAR ks[] = "/k";
+
+static const CHAR reg_seperator[] = "\\";
+
+static const CHAR service_reg_path[] = "System\\CurrentControlSet\\Services";
+
+static const CHAR parameters[] = "Parameters";
+
+static const CHAR service_dll[] = "ServiceDll";
+
+static const CHAR svchost_path[] =
+ "Software\\Microsoft\\Windows NT\\CurrentVersion\\Svchost";
+
+static const CHAR service_main[] = "ServiceMain";
+
+/* Allocate and initialize a STR containing the queried value */
+static LPSTR GetRegValue(HKEY service_key, const CHAR *value_name)
+{
+ DWORD type;
+ DWORD size;
+ LONG ret;
+ LPSTR value;
+ LPSTR tmp_value;
+
+ WINE_TRACE("\n");
+
+ ret = RegQueryValueEx(service_key, value_name, NULL, &type, NULL, &size);
+ if (ret != ERROR_SUCCESS)
+ {
+ return NULL;
+ }
+ value = HeapAlloc(GetProcessHeap(), 0, size);
+ ret = RegQueryValueEx(service_key, value_name, NULL, &type,
+ (LPBYTE)value, &size);
+ if (ret != ERROR_SUCCESS)
+ {
+ HeapFree(GetProcessHeap(), 0, value);
+ return NULL;
+ }
+
+ /* Verify proper NULL termination for string types */
+ switch (type)
+ {
+ case REG_SZ: /* Fall through */
+ case REG_EXPAND_SZ:
+ if (value[size-1] != '\0')
+ {
+ WINE_TRACE("NULL terminating registry entry: %s\n", value);
+ tmp_value = HeapReAlloc(GetProcessHeap(),
+ HEAP_ZERO_MEMORY, value, size+1);
+ if (!tmp_value)
+ {
+ HeapFree(GetProcessHeap(), 0, value);
+ return NULL;
+ }
+ value = tmp_value;
+ }
+ break;
+ case REG_MULTI_SZ:
+ if (value[size-1] != '\0' || value[size-2] != '\0')
+ {
+ WINE_TRACE("NULL terminating registry entry: %s\n", value);
+ tmp_value = HeapReAlloc(GetProcessHeap(),
+ HEAP_ZERO_MEMORY, value, size+2);
+ if (!tmp_value)
+ {
+ HeapFree(GetProcessHeap(), 0, value);
+ return NULL;
+ }
+ value = tmp_value;
+ }
+ break;
+ default:
+ /* No need to check other types */
+ break;
+ }
+ return value;
+}
+
+/* Allocate and initialize a STR containing the expanded string */
+static LPSTR ExpandEnv(LPSTR string)
+{
+ DWORD size;
+ LPSTR expanded_string;
+
+ WINE_TRACE("\n");
+
+ size = 0;
+ size = ExpandEnvironmentStrings(string, NULL, size);
+ if (size == 0)
+ {
+ WINE_ERR("cannot expand env vars in %s: %u\n", string, GetLastError());
+ return NULL;
+ }
+ expanded_string = HeapAlloc(GetProcessHeap(), 0,
+ (size + 1) * sizeof(CHAR));
+ if (ExpandEnvironmentStrings(string, expanded_string, size) == 0)
+ {
+ WINE_ERR("cannot expand env vars in %s: %u\n",
+ string, GetLastError());
+ HeapFree(GetProcessHeap(), 0, expanded_string);
+ return NULL;
+ }
+ return expanded_string;
+}
+
+/* Fill in service table entry for a specified service */
+static BOOL AddServiceElem(LPSTR service_name,
+ SERVICE_TABLE_ENTRY *service_table_entry)
+{
+ HKEY service_hkey = NULL;
+ LONG ret;
+ LPSTR service_param_key = NULL;
+ LPSTR dll_name_short = NULL;
+ LPSTR dll_name_long = NULL;
+ LPSTR dll_service_main = NULL;
+ HMODULE library = NULL;
+ LPSERVICE_MAIN_FUNCTION service_main_func = NULL;
+
+ WINE_TRACE("Adding element for %s\n", service_name);
+
+ /* Construct registry path to the service's parameters key */
+ service_param_key = HeapAlloc( GetProcessHeap(), 0,
+ (lstrlen(service_reg_path) +
+ lstrlen(reg_seperator) +
+ lstrlen(service_name) +
+ lstrlen(reg_seperator) +
+ lstrlen(parameters) +
+ 1) * sizeof(CHAR));
+ lstrcpy(service_param_key, service_reg_path);
+ lstrcat(service_param_key, reg_seperator);
+ lstrcat(service_param_key, service_name);
+ lstrcat(service_param_key, reg_seperator);
+ lstrcat(service_param_key, parameters);
+ ret = RegOpenKeyEx(HKEY_LOCAL_MACHINE, service_param_key, 0,
+ KEY_READ, &service_hkey);
+ if (ret != ERROR_SUCCESS)
+ {
+ WINE_ERR("cannot open key %s, err=%d\n", service_param_key, ret);
+ HeapFree(GetProcessHeap(), 0, service_param_key);
+ return FALSE;
+ }
+
+ /* Find DLL associate with service from key */
+ dll_name_short = GetRegValue(service_hkey, service_dll);
+ if (!dll_name_short)
+ {
+ WINE_ERR("cannot find registry value %s for service %s\n",
+ service_dll, service_name);
+ HeapFree(GetProcessHeap(), 0, service_param_key);
+ RegCloseKey(service_hkey);
+ return FALSE;
+ }
+
+ /* Expand environment variables in ServiceDll name*/
+ dll_name_long = ExpandEnv(dll_name_short);
+ if (!dll_name_long)
+ {
+ WINE_ERR("failed to expand string %s\n", dll_name_short);
+ HeapFree(GetProcessHeap(), 0, service_param_key);
+ HeapFree(GetProcessHeap(), 0, dll_name_short);
+ RegCloseKey(service_hkey);
+ return FALSE;
+ }
+ HeapFree(GetProcessHeap(), 0, service_param_key);
+ HeapFree(GetProcessHeap(), 0, dll_name_short);
+
+ /* Load the DLL */
+ library = LoadLibrary(dll_name_long);
+ if (!library)
+ {
+ WINE_ERR("failed to load library %s, err=%u\n",
+ dll_name_long, GetLastError());
+ HeapFree(GetProcessHeap(), 0, dll_name_long);
+ return FALSE;
+ }
+ HeapFree(GetProcessHeap(), 0, dll_name_long);
+
+ /* Look for alternate to default ServiceMain entry point and obtain a
+ * pointer to ServiceMain entry point */
+ dll_service_main = GetRegValue(service_hkey, service_main);
+ RegCloseKey(service_hkey);
+ if (!dll_service_main)
+ {
+ service_main_func = (LPSERVICE_MAIN_FUNCTION)
+ GetProcAddress(library, service_main);
+ }
+ else
+ {
+ service_main_func = (LPSERVICE_MAIN_FUNCTION)
+ GetProcAddress(library, dll_service_main);
+ HeapFree(GetProcessHeap(), 0, dll_service_main);
+ }
+ if (!service_main_func)
+ {
+ WINE_ERR("cannot locate ServiceMain procedure in DLL for %s\n",
+ service_name);
+ FreeLibrary(library);
+ return FALSE;
+ }
+
+ /* Fill in the service table entry */
+ service_table_entry->lpServiceName = service_name;
+ service_table_entry->lpServiceProc = service_main_func;
+ return TRUE;
+}
+
+/* Initialize the service table for a list (REG_MULTI_SZ) of services */
+static BOOL StartGroupServices(LPSTR services)
+{
+ LPSTR service_name = NULL;
+ SERVICE_TABLE_ENTRY *service_table = NULL;
+ DWORD service_count;
+
+ /* Count the services to load */
+ service_count = 0;
+ service_name = services;
+ while (*service_name != '\0')
+ {
+ ++service_count;
+ service_name = service_name + lstrlen(service_name);
+ ++service_name;
+ }
+ WINE_TRACE("Service group %s contains %d services\n",
+ services, service_count);
+
+ /* Populate the service table */
+ service_table = HeapAlloc(GetProcessHeap(), 0,
+ (service_count + 1) * sizeof(SERVICE_TABLE_ENTRY));
+ service_name = services;
+ service_count = 0;
+ while (*service_name != '\0')
+ {
+ if (!AddServiceElem(service_name, &service_table[service_count]))
+ {
+ HeapFree(GetProcessHeap(), 0, service_table);
+ return FALSE;
+ }
+ ++service_count;
+ service_name = service_name + lstrlen(service_name);
+ ++service_name;
+ }
+ service_table[service_count].lpServiceName = NULL;
+ service_table[service_count].lpServiceProc = NULL;
+
+ /* Start the services */
+ if (!StartServiceCtrlDispatcher(service_table))
+ {
+ WINE_ERR("StartServiceCtrlDispatcherW failed to start %s: %u\n",
+ services, GetLastError());
+ HeapFree(GetProcessHeap(), 0, service_table);
+ return FALSE;
+ }
+ HeapFree(GetProcessHeap(), 0, service_table);
+ return TRUE;
+}
+
+/* Find the list of services associated with a group name and start those
+ * services */
+static BOOL LoadGroup(PCHAR group_name)
+{
+ HKEY group_hkey = NULL;
+ LPSTR services = NULL;
+ LONG ret;
+
+ WINE_TRACE("Loading service group for %s\n", group_name);
+
+ /* Lookup group_name value of svchost registry entry */
+ ret = RegOpenKeyEx(HKEY_LOCAL_MACHINE, svchost_path, 0,
+ KEY_READ, &group_hkey);
+ if (ret != ERROR_SUCCESS)
+ {
+ WINE_ERR("cannot open key %s, err=%d\n", svchost_path, ret);
+ return FALSE;
+ }
+ services = GetRegValue(group_hkey, group_name);
+ RegCloseKey(group_hkey);
+ if (!services)
+ {
+ WINE_ERR("cannot find registry value %s in %s\n",
+ group_name, svchost_path);
+ return FALSE;
+ }
+
+ /* Start services */
+ if (StartGroupServices(services) == FALSE)
+ {
+ WINE_TRACE("Failed to start service group\n");
+ HeapFree(GetProcessHeap(), 0, services);
+ return FALSE;
+ }
+ HeapFree(GetProcessHeap(), 0, services);
+ return TRUE;
+}
+
+
+int main(int argc, CHAR *argv[])
+{
+ int option_index;
+
+ WINE_TRACE("\n");
+
+ for (option_index = 1; option_index < argc; option_index++)
+ {
+ if (lstrcmpi(argv[option_index], ks) == 0 ||
+ lstrcmpi(argv[option_index], kd) == 0)
+ {
+ ++option_index;
+ if (option_index >= argc)
+ {
+ WINE_ERR("Must specify group to initialize\n");
+ return 0;
+ }
+ if (!LoadGroup(argv[option_index]))
+ {
+ WINE_ERR("Failed to load requested group: %s\n",
+ argv[option_index]);
+ return 0;
+ }
+ }
+ else
+ {
+ WINE_FIXME("Unrecognized option: %s\n", argv[option_index]);
+ return 0;
+ }
+ }
+
+ return 0;
+}
+
--
1.5.3.1
More information about the wine-patches
mailing list