services.exe: Load list of services from registry at startup.
[wine] / programs / services / services.c
1 /*
2  * Services - controls services keeps track of their state
3  *
4  * Copyright 2007 Google (Mikolaj Zalewski)
5  *
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.
10  *
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.
15  *
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
19  */
20
21 #define WIN32_LEAN_AND_MEAN
22
23 #include <stdarg.h>
24 #include <windows.h>
25 #include <winsvc.h>
26
27 #include "wine/list.h"
28 #include "wine/unicode.h"
29 #include "wine/debug.h"
30
31 #include "services.h"
32
33 #define MAX_SERVICE_NAME 260
34
35 WINE_DEFAULT_DEBUG_CHANNEL(service);
36
37 static struct list g_services;
38
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 };
43
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};
56
57
58 static void free_service_entry(struct service_entry *entry)
59 {
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);
68 }
69
70 static DWORD load_service_config(HKEY hKey, struct service_entry *entry)
71 {
72     DWORD err;
73     WCHAR *wptr;
74
75     if ((err = load_reg_string(hKey, SZ_IMAGE_PATH,   TRUE, &entry->config.lpBinaryPathName)) != 0)
76         return err;
77     if ((err = load_reg_string(hKey, SZ_GROUP,        0,    &entry->config.lpLoadOrderGroup)) != 0)
78         return err;
79     if ((err = load_reg_string(hKey, SZ_OBJECT_NAME,  TRUE, &entry->config.lpServiceStartName)) != 0)
80         return err;
81     if ((err = load_reg_string(hKey, SZ_DISPLAY_NAME, 0,    &entry->config.lpDisplayName)) != 0)
82         return err;
83     if ((err = load_reg_string(hKey, SZ_DESCRIPTION,  0,    &entry->description)) != 0)
84         return err;
85     if ((err = load_reg_multisz(hKey, SZ_DEPEND_ON_SERVICE, &entry->dependOnServices)) != 0)
86         return err;
87     if ((err = load_reg_multisz(hKey, SZ_DEPEND_ON_GROUP,   &entry->dependOnGroups)) != 0)
88         return err;
89
90     if ((err = load_reg_dword(hKey, SZ_TYPE,  &entry->config.dwServiceType)) != 0)
91         return err;
92     if ((err = load_reg_dword(hKey, SZ_START, &entry->config.dwStartType)) != 0)
93         return err;
94     if ((err = load_reg_dword(hKey, SZ_ERROR, &entry->config.dwErrorControl)) != 0)
95         return err;
96     if ((err = load_reg_dword(hKey, SZ_TAG,   &entry->config.dwTagId)) != 0)
97         return err;
98
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));
109
110     return ERROR_SUCCESS;
111 }
112
113 static BOOL validate_service_config(struct service_entry *entry)
114 {
115     if (entry->config.dwServiceType & SERVICE_WIN32 && (entry->config.lpBinaryPathName == NULL || !entry->config.lpBinaryPathName[0]))
116     {
117         WINE_ERR("Service %s is Win32 but have no image path set\n", wine_dbgstr_w(entry->name));
118         return FALSE;
119     }
120     return TRUE;
121 }
122
123 static DWORD load_services(void)
124 {
125     HKEY hServicesRootKey;
126     DWORD err;
127     int i;
128
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)
132     {
133         WINE_ERR("Couldn't open services key\n");
134         return err;
135     }
136
137     for (i = 0; TRUE; i++)
138     {
139         WCHAR szName[MAX_SERVICE_NAME];
140         struct service_entry *entry;
141         HKEY hServiceKey;
142
143         err = RegEnumKeyW(hServicesRootKey, i, szName, MAX_SERVICE_NAME);
144         if (err == ERROR_NO_MORE_ITEMS)
145             break;
146
147         if (err != 0)
148         {
149             WINE_ERR("Error %d reading key %d name - skipping\n", err, i);
150             continue;
151         }
152
153         entry = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*entry));
154         entry->name = strdupW(szName);
155
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)
159         {
160             err = load_service_config(hServiceKey, entry);
161             RegCloseKey(hServiceKey);
162         }
163
164         if (err != ERROR_SUCCESS)
165         {
166             WINE_ERR("Error %d reading registry key for service %s - skipping\n", err, wine_dbgstr_w(szName));
167             free_service_entry(entry);
168             continue;
169         }
170
171         if (entry->config.dwServiceType == 0)
172         {
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);
176             continue;
177         }
178
179         if (!validate_service_config(entry))
180         {
181             WINE_ERR("Invalid configuration of service %s - skipping\n", wine_dbgstr_w(szName));
182             free_service_entry(entry);
183             continue;
184         }
185
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 */
190
191         list_add_tail(&g_services, &entry->entry);
192     }
193     RegCloseKey(hServicesRootKey);
194     return ERROR_SUCCESS;
195 }
196
197 int main(int argc, char *argv[])
198 {
199     DWORD err;
200     list_init(&g_services);
201     if ((err = load_services()) != ERROR_SUCCESS)
202         return err;
203     return 0;
204 }