ntdll: Implement NtCreateDebugObject().
[wine.git] / dlls / ntdll / loadorder.c
blobe4aaf9dd74f54326ce8848907c39dd91bb8e5587
1 /*
2 * Dlls load order support
4 * Copyright 1999 Bertho Stultiens
5 * Copyright 2003 Alexandre Julliard
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include <stdarg.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <assert.h>
27 #include "ntstatus.h"
28 #define WIN32_NO_STATUS
29 #include "windef.h"
30 #include "winternl.h"
31 #include "ntdll_misc.h"
33 #include "wine/debug.h"
35 WINE_DEFAULT_DEBUG_CHANNEL(module);
37 #define LOADORDER_ALLOC_CLUSTER 32 /* Allocate with 32 entries at a time */
39 typedef struct module_loadorder
41 const WCHAR *modulename;
42 enum loadorder loadorder;
43 } module_loadorder_t;
45 struct loadorder_list
47 int count;
48 int alloc;
49 module_loadorder_t *order;
52 static const WCHAR separatorsW[] = L", \t";
54 static BOOL init_done;
55 static struct loadorder_list env_list;
58 /***************************************************************************
59 * cmp_sort_func (internal, static)
61 * Sorting and comparing function used in sort and search of loadorder
62 * entries.
64 static int __cdecl cmp_sort_func(const void *s1, const void *s2)
66 return wcsicmp(((const module_loadorder_t *)s1)->modulename, ((const module_loadorder_t *)s2)->modulename);
70 /***************************************************************************
71 * get_basename
73 * Return the base name of a file name (i.e. remove the path components).
75 static const WCHAR *get_basename( const WCHAR *name )
77 const WCHAR *ptr;
79 if (name[0] && name[1] == ':') name += 2; /* strip drive specification */
80 if ((ptr = wcsrchr( name, '\\' ))) name = ptr + 1;
81 if ((ptr = wcsrchr( name, '/' ))) name = ptr + 1;
82 return name;
85 /***************************************************************************
86 * remove_dll_ext
88 * Remove extension if it is ".dll".
90 static inline void remove_dll_ext( WCHAR *name )
92 WCHAR *p = wcsrchr( name, '.' );
94 if (p && !wcsicmp( p, L".dll" )) *p = 0;
98 /***************************************************************************
99 * debugstr_loadorder
101 * Return a loadorder in printable form.
103 static const char *debugstr_loadorder( enum loadorder lo )
105 switch(lo)
107 case LO_DISABLED: return "";
108 case LO_NATIVE: return "n";
109 case LO_BUILTIN: return "b";
110 case LO_NATIVE_BUILTIN: return "n,b";
111 case LO_BUILTIN_NATIVE: return "b,n";
112 case LO_DEFAULT: return "default";
113 default: return "??";
118 /***************************************************************************
119 * parse_load_order
121 * Parses the loadorder options from the configuration and puts it into
122 * a structure.
124 static enum loadorder parse_load_order( const WCHAR *order )
126 enum loadorder ret = LO_DISABLED;
128 while (*order)
130 order += wcsspn( order, separatorsW );
131 switch(*order)
133 case 'N': /* native */
134 case 'n':
135 if (ret == LO_DISABLED) ret = LO_NATIVE;
136 else if (ret == LO_BUILTIN) return LO_BUILTIN_NATIVE;
137 break;
138 case 'B': /* builtin */
139 case 'b':
140 if (ret == LO_DISABLED) ret = LO_BUILTIN;
141 else if (ret == LO_NATIVE) return LO_NATIVE_BUILTIN;
142 break;
144 order += wcscspn( order, separatorsW );
146 return ret;
150 /***************************************************************************
151 * add_load_order
153 * Adds an entry in the list of environment overrides.
155 static void add_load_order( const module_loadorder_t *plo )
157 int i;
159 for(i = 0; i < env_list.count; i++)
161 if(!cmp_sort_func(plo, &env_list.order[i] ))
163 /* replace existing option */
164 env_list.order[i].loadorder = plo->loadorder;
165 return;
169 if (i >= env_list.alloc)
171 /* No space in current array, make it larger */
172 env_list.alloc += LOADORDER_ALLOC_CLUSTER;
173 if (env_list.order)
174 env_list.order = RtlReAllocateHeap(GetProcessHeap(), 0, env_list.order,
175 env_list.alloc * sizeof(module_loadorder_t));
176 else
177 env_list.order = RtlAllocateHeap(GetProcessHeap(), 0,
178 env_list.alloc * sizeof(module_loadorder_t));
179 if(!env_list.order)
181 MESSAGE("Virtual memory exhausted\n");
182 NtTerminateProcess( GetCurrentProcess(), 1 );
185 env_list.order[i].loadorder = plo->loadorder;
186 env_list.order[i].modulename = plo->modulename;
187 env_list.count++;
191 /***************************************************************************
192 * add_load_order_set
194 * Adds a set of entries in the list of command-line overrides from the key parameter.
196 static void add_load_order_set( WCHAR *entry )
198 module_loadorder_t ldo;
199 WCHAR *end = wcschr( entry, '=' );
201 if (!end) return;
202 *end++ = 0;
203 ldo.loadorder = parse_load_order( end );
205 while (*entry)
207 entry += wcsspn( entry, separatorsW );
208 end = entry + wcscspn( entry, separatorsW );
209 if (*end) *end++ = 0;
210 if (*entry)
212 remove_dll_ext( entry );
213 ldo.modulename = entry;
214 add_load_order( &ldo );
215 entry = end;
221 /***************************************************************************
222 * init_load_order
224 static void init_load_order(void)
226 WCHAR *entry, *next, *order;
227 SIZE_T len = 1024;
228 NTSTATUS status;
230 init_done = TRUE;
232 for (;;)
234 order = RtlAllocateHeap( GetProcessHeap(), 0, len * sizeof(WCHAR) );
235 status = RtlQueryEnvironmentVariable( NULL, L"WINEDLLOVERRIDES", wcslen(L"WINEDLLOVERRIDES"),
236 order, len - 1, &len );
237 if (!status)
239 order[len] = 0;
240 break;
242 RtlFreeHeap( GetProcessHeap(), 0, order );
243 if (status != STATUS_BUFFER_TOO_SMALL) return;
246 entry = order;
247 while (*entry)
249 while (*entry == ';') entry++;
250 if (!*entry) break;
251 next = wcschr( entry, ';' );
252 if (next) *next++ = 0;
253 else next = entry + wcslen(entry);
254 add_load_order_set( entry );
255 entry = next;
258 /* sort the array for quick lookup */
259 if (env_list.count)
260 qsort(env_list.order, env_list.count, sizeof(env_list.order[0]), cmp_sort_func);
262 /* note: we don't free the string because the stored module names point inside it */
266 /***************************************************************************
267 * get_env_load_order
269 * Get the load order for a given module from the WINEDLLOVERRIDES environment variable.
271 static inline enum loadorder get_env_load_order( const WCHAR *module )
273 module_loadorder_t tmp, *res;
275 tmp.modulename = module;
276 /* some bsearch implementations (Solaris) are buggy when the number of items is 0 */
277 if (env_list.count &&
278 (res = bsearch(&tmp, env_list.order, env_list.count, sizeof(env_list.order[0]), cmp_sort_func)))
279 return res->loadorder;
280 return LO_INVALID;
284 /***************************************************************************
285 * get_standard_key
287 * Return a handle to the standard DllOverrides registry section.
289 static HANDLE get_standard_key(void)
291 static HANDLE std_key = (HANDLE)-1;
293 if (std_key == (HANDLE)-1)
295 OBJECT_ATTRIBUTES attr;
296 UNICODE_STRING nameW;
297 HANDLE root;
299 RtlOpenCurrentUser( KEY_ALL_ACCESS, &root );
300 attr.Length = sizeof(attr);
301 attr.RootDirectory = root;
302 attr.ObjectName = &nameW;
303 attr.Attributes = 0;
304 attr.SecurityDescriptor = NULL;
305 attr.SecurityQualityOfService = NULL;
306 RtlInitUnicodeString( &nameW, L"Software\\Wine\\DllOverrides" );
308 /* @@ Wine registry key: HKCU\Software\Wine\DllOverrides */
309 if (NtOpenKey( &std_key, KEY_ALL_ACCESS, &attr )) std_key = 0;
310 NtClose( root );
312 return std_key;
316 /***************************************************************************
317 * get_app_key
319 * Get the registry key for the app-specific DllOverrides list.
321 static HANDLE get_app_key( const WCHAR *app_name )
323 OBJECT_ATTRIBUTES attr;
324 UNICODE_STRING nameW;
325 HANDLE root;
326 WCHAR *str;
327 static HANDLE app_key = (HANDLE)-1;
329 if (app_key != (HANDLE)-1) return app_key;
331 str = RtlAllocateHeap( GetProcessHeap(), 0,
332 sizeof(L"Software\\Wine\\AppDefaults\\") + sizeof(L"\\DllOverrides") +
333 wcslen(app_name) * sizeof(WCHAR) );
334 if (!str) return 0;
335 wcscpy( str, L"Software\\Wine\\AppDefaults\\" );
336 wcscat( str, app_name );
337 wcscat( str, L"\\DllOverrides" );
339 RtlOpenCurrentUser( KEY_ALL_ACCESS, &root );
340 attr.Length = sizeof(attr);
341 attr.RootDirectory = root;
342 attr.ObjectName = &nameW;
343 attr.Attributes = 0;
344 attr.SecurityDescriptor = NULL;
345 attr.SecurityQualityOfService = NULL;
346 RtlInitUnicodeString( &nameW, str );
348 /* @@ Wine registry key: HKCU\Software\Wine\AppDefaults\app.exe\DllOverrides */
349 if (NtOpenKey( &app_key, KEY_ALL_ACCESS, &attr )) app_key = 0;
350 NtClose( root );
351 RtlFreeHeap( GetProcessHeap(), 0, str );
352 return app_key;
356 /***************************************************************************
357 * get_registry_value
359 * Load the registry loadorder value for a given module.
361 static enum loadorder get_registry_value( HANDLE hkey, const WCHAR *module )
363 UNICODE_STRING valueW;
364 char buffer[80];
365 DWORD count;
367 RtlInitUnicodeString( &valueW, module );
369 if (!NtQueryValueKey( hkey, &valueW, KeyValuePartialInformation,
370 buffer, sizeof(buffer), &count ))
372 WCHAR *str = (WCHAR *)((KEY_VALUE_PARTIAL_INFORMATION *)buffer)->Data;
373 return parse_load_order( str );
375 return LO_INVALID;
379 /***************************************************************************
380 * get_load_order_value
382 * Get the load order for the exact specified module string, looking in:
383 * 1. The WINEDLLOVERRIDES environment variable
384 * 2. The per-application DllOverrides key
385 * 3. The standard DllOverrides key
387 static enum loadorder get_load_order_value( HANDLE std_key, HANDLE app_key, const WCHAR *module )
389 enum loadorder ret;
391 if ((ret = get_env_load_order( module )) != LO_INVALID)
393 TRACE( "got environment %s for %s\n", debugstr_loadorder(ret), debugstr_w(module) );
394 return ret;
397 if (app_key && ((ret = get_registry_value( app_key, module )) != LO_INVALID))
399 TRACE( "got app defaults %s for %s\n", debugstr_loadorder(ret), debugstr_w(module) );
400 return ret;
403 if (std_key && ((ret = get_registry_value( std_key, module )) != LO_INVALID))
405 TRACE( "got standard key %s for %s\n", debugstr_loadorder(ret), debugstr_w(module) );
406 return ret;
409 return ret;
413 /***************************************************************************
414 * get_load_order (internal)
416 * Return the loadorder of a module.
417 * The system directory and '.dll' extension is stripped from the path.
419 enum loadorder get_load_order( const WCHAR *app_name, const UNICODE_STRING *nt_name )
421 enum loadorder ret = LO_INVALID;
422 HANDLE std_key, app_key = 0;
423 const WCHAR *path = nt_name->Buffer;
424 WCHAR *module, *basename;
425 int len;
427 if (!init_done) init_load_order();
428 std_key = get_standard_key();
429 if (app_name) app_key = get_app_key( app_name );
430 if (!wcsncmp( path, L"\\??\\", 4 )) path += 4;
432 TRACE("looking for %s\n", debugstr_w(path));
434 /* Strip path information if the module resides in the system directory
436 if (!wcsnicmp( system_dir, path, wcslen( system_dir )))
438 const WCHAR *p = path + wcslen( system_dir );
439 while (*p == '\\' || *p == '/') p++;
440 if (!wcschr( p, '\\' ) && !wcschr( p, '/' )) path = p;
443 if (!(len = wcslen(path))) return ret;
444 if (!(module = RtlAllocateHeap( GetProcessHeap(), 0, (len + 2) * sizeof(WCHAR) ))) return ret;
445 wcscpy( module+1, path ); /* reserve module[0] for the wildcard char */
446 remove_dll_ext( module + 1 );
447 basename = (WCHAR *)get_basename( module+1 );
449 /* first explicit module name */
450 if ((ret = get_load_order_value( std_key, app_key, module+1 )) != LO_INVALID)
451 goto done;
453 /* then module basename preceded by '*' */
454 basename[-1] = '*';
455 if ((ret = get_load_order_value( std_key, app_key, basename-1 )) != LO_INVALID)
456 goto done;
458 /* then module basename without '*' (only if explicit path) */
459 if (basename != module+1 && ((ret = get_load_order_value( std_key, app_key, basename )) != LO_INVALID))
460 goto done;
462 /* if loading the main exe with an explicit path, try native first */
463 if (!app_name && basename != module+1)
465 ret = LO_NATIVE_BUILTIN;
466 TRACE( "got main exe default %s for %s\n", debugstr_loadorder(ret), debugstr_w(path) );
467 goto done;
470 /* and last the hard-coded default */
471 ret = LO_DEFAULT;
472 TRACE( "got hardcoded %s for %s\n", debugstr_loadorder(ret), debugstr_w(path) );
474 done:
475 RtlFreeHeap( GetProcessHeap(), 0, module );
476 return ret;