2 * Services - controls services keeps track of their state
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
27 #include "wine/list.h"
28 #include "wine/unicode.h"
29 #include "wine/debug.h"
33 #define MAX_SERVICE_NAME 260
35 WINE_DEFAULT_DEBUG_CHANNEL(service);
37 static struct list g_services;
39 /* Registry constants */
40 static const WCHAR SZ_SERVICES_KEY[] = { 'S','y','s','t','e','m','\\',
41 'C','u','r','r','e','n','t','C','o','n','t','r','o','l','S','e','t','\\',
42 'S','e','r','v','i','c','e','s',0 };
44 /* Service key values names */
45 static const WCHAR SZ_DISPLAY_NAME[] = {'D','i','s','p','l','a','y','N','a','m','e',0 };
46 static const WCHAR SZ_TYPE[] = {'T','y','p','e',0 };
47 static const WCHAR SZ_START[] = {'S','t','a','r','t',0 };
48 static const WCHAR SZ_ERROR[] = {'E','r','r','o','r','C','o','n','t','r','o','l',0 };
49 static const WCHAR SZ_IMAGE_PATH[] = {'I','m','a','g','e','P','a','t','h',0};
50 static const WCHAR SZ_GROUP[] = {'G','r','o','u','p',0};
51 static const WCHAR SZ_DEPEND_ON_SERVICE[] = {'D','e','p','e','n','d','O','n','S','e','r','v','i','c','e',0};
52 static const WCHAR SZ_DEPEND_ON_GROUP[] = {'D','e','p','e','n','d','O','n','G','r','o','u','p',0};
53 static const WCHAR SZ_OBJECT_NAME[] = {'O','b','j','e','c','t','N','a','m','e',0};
54 static const WCHAR SZ_TAG[] = {'T','a','g',0};
55 static const WCHAR SZ_DESCRIPTION[] = {'D','e','s','c','r','i','p','t','i','o','n',0};
58 static void free_service_entry(struct service_entry *entry)
60 HeapFree(GetProcessHeap(), 0, entry->name);
61 HeapFree(GetProcessHeap(), 0, entry->config.lpBinaryPathName);
62 HeapFree(GetProcessHeap(), 0, entry->config.lpDependencies);
63 HeapFree(GetProcessHeap(), 0, entry->config.lpLoadOrderGroup);
64 HeapFree(GetProcessHeap(), 0, entry->config.lpServiceStartName);
65 HeapFree(GetProcessHeap(), 0, entry->config.lpDisplayName);
66 HeapFree(GetProcessHeap(), 0, entry->description);
67 HeapFree(GetProcessHeap(), 0, entry);
70 static DWORD load_service_config(HKEY hKey, struct service_entry *entry)
75 if ((err = load_reg_string(hKey, SZ_IMAGE_PATH, TRUE, &entry->config.lpBinaryPathName)) != 0)
77 if ((err = load_reg_string(hKey, SZ_GROUP, 0, &entry->config.lpLoadOrderGroup)) != 0)
79 if ((err = load_reg_string(hKey, SZ_OBJECT_NAME, TRUE, &entry->config.lpServiceStartName)) != 0)
81 if ((err = load_reg_string(hKey, SZ_DISPLAY_NAME, 0, &entry->config.lpDisplayName)) != 0)
83 if ((err = load_reg_string(hKey, SZ_DESCRIPTION, 0, &entry->description)) != 0)
85 if ((err = load_reg_multisz(hKey, SZ_DEPEND_ON_SERVICE, &entry->dependOnServices)) != 0)
87 if ((err = load_reg_multisz(hKey, SZ_DEPEND_ON_GROUP, &entry->dependOnGroups)) != 0)
90 if ((err = load_reg_dword(hKey, SZ_TYPE, &entry->config.dwServiceType)) != 0)
92 if ((err = load_reg_dword(hKey, SZ_START, &entry->config.dwStartType)) != 0)
94 if ((err = load_reg_dword(hKey, SZ_ERROR, &entry->config.dwErrorControl)) != 0)
96 if ((err = load_reg_dword(hKey, SZ_TAG, &entry->config.dwTagId)) != 0)
99 WINE_TRACE("Image path = %s\n", wine_dbgstr_w(entry->config.lpBinaryPathName) );
100 WINE_TRACE("Group = %s\n", wine_dbgstr_w(entry->config.lpLoadOrderGroup) );
101 WINE_TRACE("Service account name = %s\n", wine_dbgstr_w(entry->config.lpServiceStartName) );
102 WINE_TRACE("Display name = %s\n", wine_dbgstr_w(entry->config.lpDisplayName) );
103 WINE_TRACE("Service dependancies : %s\n", entry->dependOnServices[0] ? "" : "(none)");
104 for (wptr = entry->dependOnServices; *wptr; wptr += strlenW(wptr) + 1)
105 WINE_TRACE(" * %s\n", wine_dbgstr_w(wptr));
106 WINE_TRACE("Group dependancies : %s\n", entry->dependOnGroups[0] ? "" : "(none)");
107 for (wptr = entry->dependOnGroups; *wptr; wptr += strlenW(wptr) + 1)
108 WINE_TRACE(" * %s\n", wine_dbgstr_w(wptr));
110 return ERROR_SUCCESS;
113 static BOOL validate_service_config(struct service_entry *entry)
115 if (entry->config.dwServiceType & SERVICE_WIN32 && (entry->config.lpBinaryPathName == NULL || !entry->config.lpBinaryPathName[0]))
117 WINE_ERR("Service %s is Win32 but have no image path set\n", wine_dbgstr_w(entry->name));
123 static DWORD load_services(void)
125 HKEY hServicesRootKey;
129 if ((err = RegCreateKeyExW(HKEY_LOCAL_MACHINE, SZ_SERVICES_KEY, 0, NULL,
130 REG_OPTION_NON_VOLATILE, KEY_READ, NULL,
131 &hServicesRootKey, NULL)) != ERROR_SUCCESS)
133 WINE_ERR("Couldn't open services key\n");
137 for (i = 0; TRUE; i++)
139 WCHAR szName[MAX_SERVICE_NAME];
140 struct service_entry *entry;
143 err = RegEnumKeyW(hServicesRootKey, i, szName, MAX_SERVICE_NAME);
144 if (err == ERROR_NO_MORE_ITEMS)
149 WINE_ERR("Error %d reading key %d name - skipping\n", err, i);
153 entry = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*entry));
154 entry->name = strdupW(szName);
156 WINE_TRACE("Loading service %s\n", wine_dbgstr_w(szName));
157 err = RegOpenKeyExW(hServicesRootKey, szName, 0, KEY_READ | KEY_WRITE, &hServiceKey);
158 if (err == ERROR_SUCCESS)
160 err = load_service_config(hServiceKey, entry);
161 RegCloseKey(hServiceKey);
164 if (err != ERROR_SUCCESS)
166 WINE_ERR("Error %d reading registry key for service %s - skipping\n", err, wine_dbgstr_w(szName));
167 free_service_entry(entry);
171 if (entry->config.dwServiceType == 0)
173 /* Maybe an application only wrote some configuration in the service key. Continue silently */
174 WINE_TRACE("Even the service type not set for service %s - skipping\n", wine_dbgstr_w(szName));
175 free_service_entry(entry);
179 if (!validate_service_config(entry))
181 WINE_ERR("Invalid configuration of service %s - skipping\n", wine_dbgstr_w(szName));
182 free_service_entry(entry);
186 entry->status.dwServiceType = entry->config.dwServiceType;
187 entry->status.dwCurrentState = SERVICE_STOPPED;
188 entry->status.dwWin32ExitCode = ERROR_SERVICE_NEVER_STARTED;
189 /* all other fields are zero */
191 list_add_tail(&g_services, &entry->entry);
193 RegCloseKey(hServicesRootKey);
194 return ERROR_SUCCESS;
197 int main(int argc, char *argv[])
200 list_init(&g_services);
201 if ((err = load_services()) != ERROR_SUCCESS)