mf/session: Handle shutdown state on GetService().
[wine/zf.git] / server / registry.c
blob74da4d1d9a41a872f20a1d60f7b0afc4b16638b5
1 /*
2 * Server-side registry management
4 * Copyright (C) 1999 Alexandre Julliard
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 /* To do:
22 * - symbolic links
25 #include "config.h"
26 #include "wine/port.h"
28 #include <assert.h>
29 #include <ctype.h>
30 #include <errno.h>
31 #include <fcntl.h>
32 #include <limits.h>
33 #include <stdio.h>
34 #include <stdarg.h>
35 #include <string.h>
36 #include <stdlib.h>
37 #include <sys/stat.h>
38 #include <unistd.h>
40 #include "ntstatus.h"
41 #define WIN32_NO_STATUS
42 #include "object.h"
43 #include "file.h"
44 #include "handle.h"
45 #include "request.h"
46 #include "process.h"
47 #include "unicode.h"
48 #include "security.h"
50 #include "winternl.h"
52 struct notify
54 struct list entry; /* entry in list of notifications */
55 struct event **events; /* events to set when changing this key */
56 unsigned int event_count; /* number of events */
57 int subtree; /* true if subtree notification */
58 unsigned int filter; /* which events to notify on */
59 obj_handle_t hkey; /* hkey associated with this notification */
60 struct process *process; /* process in which the hkey is valid */
63 static const WCHAR key_name[] = {'K','e','y'};
65 struct type_descr key_type =
67 { key_name, sizeof(key_name) }, /* name */
68 KEY_ALL_ACCESS | SYNCHRONIZE, /* valid_access */
69 { /* mapping */
70 STANDARD_RIGHTS_READ | KEY_NOTIFY | KEY_ENUMERATE_SUB_KEYS | KEY_QUERY_VALUE,
71 STANDARD_RIGHTS_WRITE | KEY_CREATE_SUB_KEY | KEY_SET_VALUE,
72 STANDARD_RIGHTS_EXECUTE | KEY_CREATE_LINK | KEY_NOTIFY | KEY_ENUMERATE_SUB_KEYS | KEY_QUERY_VALUE,
73 KEY_ALL_ACCESS
77 /* a registry key */
78 struct key
80 struct object obj; /* object header */
81 WCHAR *name; /* key name */
82 WCHAR *class; /* key class */
83 unsigned short namelen; /* length of key name */
84 unsigned short classlen; /* length of class name */
85 struct key *parent; /* parent key */
86 int last_subkey; /* last in use subkey */
87 int nb_subkeys; /* count of allocated subkeys */
88 struct key **subkeys; /* subkeys array */
89 int last_value; /* last in use value */
90 int nb_values; /* count of allocated values in array */
91 struct key_value *values; /* values array */
92 unsigned int flags; /* flags */
93 timeout_t modif; /* last modification time */
94 struct list notify_list; /* list of notifications */
97 /* key flags */
98 #define KEY_VOLATILE 0x0001 /* key is volatile (not saved to disk) */
99 #define KEY_DELETED 0x0002 /* key has been deleted */
100 #define KEY_DIRTY 0x0004 /* key has been modified */
101 #define KEY_SYMLINK 0x0008 /* key is a symbolic link */
102 #define KEY_WOW64 0x0010 /* key contains a Wow6432Node subkey */
103 #define KEY_WOWSHARE 0x0020 /* key is a Wow64 shared key (used for Software\Classes) */
105 /* a key value */
106 struct key_value
108 WCHAR *name; /* value name */
109 unsigned short namelen; /* length of value name */
110 unsigned int type; /* value type */
111 data_size_t len; /* value data length in bytes */
112 void *data; /* pointer to value data */
115 #define MIN_SUBKEYS 8 /* min. number of allocated subkeys per key */
116 #define MIN_VALUES 8 /* min. number of allocated values per key */
118 #define MAX_NAME_LEN 256 /* max. length of a key name */
119 #define MAX_VALUE_LEN 16383 /* max. length of a value name */
121 /* the root of the registry tree */
122 static struct key *root_key;
124 static const timeout_t ticks_1601_to_1970 = (timeout_t)86400 * (369 * 365 + 89) * TICKS_PER_SEC;
125 static const timeout_t save_period = 30 * -TICKS_PER_SEC; /* delay between periodic saves */
126 static struct timeout_user *save_timeout_user; /* saving timer */
127 static enum prefix_type { PREFIX_UNKNOWN, PREFIX_32BIT, PREFIX_64BIT } prefix_type;
129 static const WCHAR root_name[] = { '\\','R','e','g','i','s','t','r','y','\\' };
130 static const WCHAR wow6432node[] = {'W','o','w','6','4','3','2','N','o','d','e'};
131 static const WCHAR symlink_value[] = {'S','y','m','b','o','l','i','c','L','i','n','k','V','a','l','u','e'};
132 static const struct unicode_str symlink_str = { symlink_value, sizeof(symlink_value) };
134 static void set_periodic_save_timer(void);
135 static struct key_value *find_value( const struct key *key, const struct unicode_str *name, int *index );
137 /* information about where to save a registry branch */
138 struct save_branch_info
140 struct key *key;
141 const char *path;
144 #define MAX_SAVE_BRANCH_INFO 3
145 static int save_branch_count;
146 static struct save_branch_info save_branch_info[MAX_SAVE_BRANCH_INFO];
149 /* information about a file being loaded */
150 struct file_load_info
152 const char *filename; /* input file name */
153 FILE *file; /* input file */
154 char *buffer; /* line buffer */
155 int len; /* buffer length */
156 int line; /* current input line */
157 WCHAR *tmp; /* temp buffer to use while parsing input */
158 size_t tmplen; /* length of temp buffer */
162 static void key_dump( struct object *obj, int verbose );
163 static unsigned int key_map_access( struct object *obj, unsigned int access );
164 static struct security_descriptor *key_get_sd( struct object *obj );
165 static WCHAR *key_get_full_name( struct object *obj, data_size_t *len );
166 static int key_close_handle( struct object *obj, struct process *process, obj_handle_t handle );
167 static void key_destroy( struct object *obj );
169 static const struct object_ops key_ops =
171 sizeof(struct key), /* size */
172 &key_type, /* type */
173 key_dump, /* dump */
174 no_add_queue, /* add_queue */
175 NULL, /* remove_queue */
176 NULL, /* signaled */
177 NULL, /* satisfied */
178 no_signal, /* signal */
179 no_get_fd, /* get_fd */
180 key_map_access, /* map_access */
181 key_get_sd, /* get_sd */
182 default_set_sd, /* set_sd */
183 key_get_full_name, /* get_full_name */
184 no_lookup_name, /* lookup_name */
185 no_link_name, /* link_name */
186 NULL, /* unlink_name */
187 no_open_file, /* open_file */
188 no_kernel_obj_list, /* get_kernel_obj_list */
189 key_close_handle, /* close_handle */
190 key_destroy /* destroy */
194 static inline int is_wow6432node( const WCHAR *name, unsigned int len )
196 return (len == sizeof(wow6432node) && !memicmp_strW( name, wow6432node, sizeof( wow6432node )));
200 * The registry text file format v2 used by this code is similar to the one
201 * used by REGEDIT import/export functionality, with the following differences:
202 * - strings and key names can contain \x escapes for Unicode
203 * - key names use escapes too in order to support Unicode
204 * - the modification time optionally follows the key name
205 * - REG_EXPAND_SZ and REG_MULTI_SZ are saved as strings instead of hex
208 /* dump the full path of a key */
209 static void dump_path( const struct key *key, const struct key *base, FILE *f )
211 if (key->parent && key->parent != base)
213 dump_path( key->parent, base, f );
214 fprintf( f, "\\\\" );
216 dump_strW( key->name, key->namelen, f, "[]" );
219 /* dump a value to a text file */
220 static void dump_value( const struct key_value *value, FILE *f )
222 unsigned int i, dw;
223 int count;
225 if (value->namelen)
227 fputc( '\"', f );
228 count = 1 + dump_strW( value->name, value->namelen, f, "\"\"" );
229 count += fprintf( f, "\"=" );
231 else count = fprintf( f, "@=" );
233 switch(value->type)
235 case REG_SZ:
236 case REG_EXPAND_SZ:
237 case REG_MULTI_SZ:
238 /* only output properly terminated strings in string format */
239 if (value->len < sizeof(WCHAR)) break;
240 if (value->len % sizeof(WCHAR)) break;
241 if (((WCHAR *)value->data)[value->len / sizeof(WCHAR) - 1]) break;
242 if (value->type != REG_SZ) fprintf( f, "str(%x):", value->type );
243 fputc( '\"', f );
244 dump_strW( (WCHAR *)value->data, value->len, f, "\"\"" );
245 fprintf( f, "\"\n" );
246 return;
248 case REG_DWORD:
249 if (value->len != sizeof(dw)) break;
250 memcpy( &dw, value->data, sizeof(dw) );
251 fprintf( f, "dword:%08x\n", dw );
252 return;
255 if (value->type == REG_BINARY) count += fprintf( f, "hex:" );
256 else count += fprintf( f, "hex(%x):", value->type );
257 for (i = 0; i < value->len; i++)
259 count += fprintf( f, "%02x", *((unsigned char *)value->data + i) );
260 if (i < value->len-1)
262 fputc( ',', f );
263 if (++count > 76)
265 fprintf( f, "\\\n " );
266 count = 2;
270 fputc( '\n', f );
273 /* save a registry and all its subkeys to a text file */
274 static void save_subkeys( const struct key *key, const struct key *base, FILE *f )
276 int i;
278 if (key->flags & KEY_VOLATILE) return;
279 /* save key if it has either some values or no subkeys, or needs special options */
280 /* keys with no values but subkeys are saved implicitly by saving the subkeys */
281 if ((key->last_value >= 0) || (key->last_subkey == -1) || key->class || (key->flags & KEY_SYMLINK))
283 fprintf( f, "\n[" );
284 if (key != base) dump_path( key, base, f );
285 fprintf( f, "] %u\n", (unsigned int)((key->modif - ticks_1601_to_1970) / TICKS_PER_SEC) );
286 fprintf( f, "#time=%x%08x\n", (unsigned int)(key->modif >> 32), (unsigned int)key->modif );
287 if (key->class)
289 fprintf( f, "#class=\"" );
290 dump_strW( key->class, key->classlen, f, "\"\"" );
291 fprintf( f, "\"\n" );
293 if (key->flags & KEY_SYMLINK) fputs( "#link\n", f );
294 for (i = 0; i <= key->last_value; i++) dump_value( &key->values[i], f );
296 for (i = 0; i <= key->last_subkey; i++) save_subkeys( key->subkeys[i], base, f );
299 static void dump_operation( const struct key *key, const struct key_value *value, const char *op )
301 fprintf( stderr, "%s key ", op );
302 if (key) dump_path( key, NULL, stderr );
303 else fprintf( stderr, "ERROR" );
304 if (value)
306 fprintf( stderr, " value ");
307 dump_value( value, stderr );
309 else fprintf( stderr, "\n" );
312 static void key_dump( struct object *obj, int verbose )
314 struct key *key = (struct key *)obj;
315 assert( obj->ops == &key_ops );
316 fprintf( stderr, "Key flags=%x ", key->flags );
317 dump_path( key, NULL, stderr );
318 fprintf( stderr, "\n" );
321 /* notify waiter and maybe delete the notification */
322 static void do_notification( struct key *key, struct notify *notify, int del )
324 unsigned int i;
326 for (i = 0; i < notify->event_count; ++i)
328 set_event( notify->events[i] );
329 release_object( notify->events[i] );
331 free( notify->events );
332 notify->events = NULL;
333 notify->event_count = 0;
335 if (del)
337 list_remove( &notify->entry );
338 free( notify );
342 static inline struct notify *find_notify( struct key *key, struct process *process, obj_handle_t hkey )
344 struct notify *notify;
346 LIST_FOR_EACH_ENTRY( notify, &key->notify_list, struct notify, entry )
348 if (notify->process == process && notify->hkey == hkey) return notify;
350 return NULL;
353 static unsigned int key_map_access( struct object *obj, unsigned int access )
355 access = default_map_access( obj, access );
356 /* filter the WOW64 masks, as they aren't real access bits */
357 return access & ~(KEY_WOW64_64KEY | KEY_WOW64_32KEY);
360 static struct security_descriptor *key_get_sd( struct object *obj )
362 static struct security_descriptor *key_default_sd;
364 if (obj->sd) return obj->sd;
366 if (!key_default_sd)
368 size_t users_sid_len = security_sid_len( security_builtin_users_sid );
369 size_t admins_sid_len = security_sid_len( security_builtin_admins_sid );
370 size_t dacl_len = sizeof(ACL) + 2 * offsetof( ACCESS_ALLOWED_ACE, SidStart )
371 + users_sid_len + admins_sid_len;
372 ACCESS_ALLOWED_ACE *aaa;
373 ACL *dacl;
375 key_default_sd = mem_alloc( sizeof(*key_default_sd) + 2 * admins_sid_len + dacl_len );
376 key_default_sd->control = SE_DACL_PRESENT;
377 key_default_sd->owner_len = admins_sid_len;
378 key_default_sd->group_len = admins_sid_len;
379 key_default_sd->sacl_len = 0;
380 key_default_sd->dacl_len = dacl_len;
381 memcpy( key_default_sd + 1, security_builtin_admins_sid, admins_sid_len );
382 memcpy( (char *)(key_default_sd + 1) + admins_sid_len, security_builtin_admins_sid, admins_sid_len );
384 dacl = (ACL *)((char *)(key_default_sd + 1) + 2 * admins_sid_len);
385 dacl->AclRevision = ACL_REVISION;
386 dacl->Sbz1 = 0;
387 dacl->AclSize = dacl_len;
388 dacl->AceCount = 2;
389 dacl->Sbz2 = 0;
390 aaa = (ACCESS_ALLOWED_ACE *)(dacl + 1);
391 aaa->Header.AceType = ACCESS_ALLOWED_ACE_TYPE;
392 aaa->Header.AceFlags = INHERIT_ONLY_ACE | CONTAINER_INHERIT_ACE;
393 aaa->Header.AceSize = offsetof( ACCESS_ALLOWED_ACE, SidStart ) + users_sid_len;
394 aaa->Mask = GENERIC_READ;
395 memcpy( &aaa->SidStart, security_builtin_users_sid, users_sid_len );
396 aaa = (ACCESS_ALLOWED_ACE *)((char *)aaa + aaa->Header.AceSize);
397 aaa->Header.AceType = ACCESS_ALLOWED_ACE_TYPE;
398 aaa->Header.AceFlags = 0;
399 aaa->Header.AceSize = offsetof( ACCESS_ALLOWED_ACE, SidStart ) + admins_sid_len;
400 aaa->Mask = KEY_ALL_ACCESS;
401 memcpy( &aaa->SidStart, security_builtin_admins_sid, admins_sid_len );
403 return key_default_sd;
406 static WCHAR *key_get_full_name( struct object *obj, data_size_t *ret_len )
408 static const WCHAR backslash = '\\';
409 struct key *key = (struct key *) obj;
410 data_size_t len = sizeof(root_name) - sizeof(WCHAR);
411 char *ret;
413 for (key = (struct key *)obj; key != root_key; key = key->parent) len += key->namelen + sizeof(WCHAR);
414 if (!(ret = malloc( len ))) return NULL;
416 *ret_len = len;
417 key = (struct key *)obj;
418 for (key = (struct key *)obj; key != root_key; key = key->parent)
420 memcpy( ret + len - key->namelen, key->name, key->namelen );
421 len -= key->namelen + sizeof(WCHAR);
422 memcpy( ret + len, &backslash, sizeof(WCHAR) );
424 memcpy( ret, root_name, sizeof(root_name) - sizeof(WCHAR) );
425 return (WCHAR *)ret;
428 /* close the notification associated with a handle */
429 static int key_close_handle( struct object *obj, struct process *process, obj_handle_t handle )
431 struct key * key = (struct key *) obj;
432 struct notify *notify = find_notify( key, process, handle );
433 if (notify) do_notification( key, notify, 1 );
434 return 1; /* ok to close */
437 static void key_destroy( struct object *obj )
439 int i;
440 struct list *ptr;
441 struct key *key = (struct key *)obj;
442 assert( obj->ops == &key_ops );
444 free( key->name );
445 free( key->class );
446 for (i = 0; i <= key->last_value; i++)
448 free( key->values[i].name );
449 free( key->values[i].data );
451 free( key->values );
452 for (i = 0; i <= key->last_subkey; i++)
454 key->subkeys[i]->parent = NULL;
455 release_object( key->subkeys[i] );
457 free( key->subkeys );
458 /* unconditionally notify everything waiting on this key */
459 while ((ptr = list_head( &key->notify_list )))
461 struct notify *notify = LIST_ENTRY( ptr, struct notify, entry );
462 do_notification( key, notify, 1 );
466 /* get the request vararg as registry path */
467 static inline void get_req_path( struct unicode_str *str, int skip_root )
469 str->str = get_req_data();
470 str->len = (get_req_data_size() / sizeof(WCHAR)) * sizeof(WCHAR);
472 if (skip_root && str->len >= sizeof(root_name) && !memicmp_strW( str->str, root_name, sizeof(root_name) ))
474 str->str += ARRAY_SIZE( root_name );
475 str->len -= sizeof(root_name);
479 /* return the next token in a given path */
480 /* token->str must point inside the path, or be NULL for the first call */
481 static struct unicode_str *get_path_token( const struct unicode_str *path, struct unicode_str *token )
483 data_size_t i = 0, len = path->len / sizeof(WCHAR);
485 if (!token->str) /* first time */
487 /* path cannot start with a backslash */
488 if (len && path->str[0] == '\\')
490 set_error( STATUS_OBJECT_PATH_INVALID );
491 return NULL;
494 else
496 i = token->str - path->str;
497 i += token->len / sizeof(WCHAR);
498 while (i < len && path->str[i] == '\\') i++;
500 token->str = path->str + i;
501 while (i < len && path->str[i] != '\\') i++;
502 token->len = (path->str + i - token->str) * sizeof(WCHAR);
503 return token;
506 /* allocate a key object */
507 static struct key *alloc_key( const struct unicode_str *name, timeout_t modif )
509 struct key *key;
510 if ((key = alloc_object( &key_ops )))
512 key->name = NULL;
513 key->class = NULL;
514 key->namelen = name->len;
515 key->classlen = 0;
516 key->flags = 0;
517 key->last_subkey = -1;
518 key->nb_subkeys = 0;
519 key->subkeys = NULL;
520 key->nb_values = 0;
521 key->last_value = -1;
522 key->values = NULL;
523 key->modif = modif;
524 key->parent = NULL;
525 list_init( &key->notify_list );
526 if (name->len && !(key->name = memdup( name->str, name->len )))
528 release_object( key );
529 key = NULL;
532 return key;
535 /* mark a key and all its parents as dirty (modified) */
536 static void make_dirty( struct key *key )
538 while (key)
540 if (key->flags & (KEY_DIRTY|KEY_VOLATILE)) return; /* nothing to do */
541 key->flags |= KEY_DIRTY;
542 key = key->parent;
546 /* mark a key and all its subkeys as clean (not modified) */
547 static void make_clean( struct key *key )
549 int i;
551 if (key->flags & KEY_VOLATILE) return;
552 if (!(key->flags & KEY_DIRTY)) return;
553 key->flags &= ~KEY_DIRTY;
554 for (i = 0; i <= key->last_subkey; i++) make_clean( key->subkeys[i] );
557 /* go through all the notifications and send them if necessary */
558 static void check_notify( struct key *key, unsigned int change, int not_subtree )
560 struct list *ptr, *next;
562 LIST_FOR_EACH_SAFE( ptr, next, &key->notify_list )
564 struct notify *n = LIST_ENTRY( ptr, struct notify, entry );
565 if ( ( not_subtree || n->subtree ) && ( change & n->filter ) )
566 do_notification( key, n, 0 );
570 /* update key modification time */
571 static void touch_key( struct key *key, unsigned int change )
573 struct key *k;
575 key->modif = current_time;
576 make_dirty( key );
578 /* do notifications */
579 check_notify( key, change, 1 );
580 for ( k = key->parent; k; k = k->parent )
581 check_notify( k, change, 0 );
584 /* try to grow the array of subkeys; return 1 if OK, 0 on error */
585 static int grow_subkeys( struct key *key )
587 struct key **new_subkeys;
588 int nb_subkeys;
590 if (key->nb_subkeys)
592 nb_subkeys = key->nb_subkeys + (key->nb_subkeys / 2); /* grow by 50% */
593 if (!(new_subkeys = realloc( key->subkeys, nb_subkeys * sizeof(*new_subkeys) )))
595 set_error( STATUS_NO_MEMORY );
596 return 0;
599 else
601 nb_subkeys = MIN_SUBKEYS;
602 if (!(new_subkeys = mem_alloc( nb_subkeys * sizeof(*new_subkeys) ))) return 0;
604 key->subkeys = new_subkeys;
605 key->nb_subkeys = nb_subkeys;
606 return 1;
609 /* allocate a subkey for a given key, and return its index */
610 static struct key *alloc_subkey( struct key *parent, const struct unicode_str *name,
611 int index, timeout_t modif )
613 struct key *key;
614 int i;
616 if (name->len > MAX_NAME_LEN * sizeof(WCHAR))
618 set_error( STATUS_INVALID_PARAMETER );
619 return NULL;
621 if (parent->last_subkey + 1 == parent->nb_subkeys)
623 /* need to grow the array */
624 if (!grow_subkeys( parent )) return NULL;
626 if ((key = alloc_key( name, modif )) != NULL)
628 key->parent = parent;
629 for (i = ++parent->last_subkey; i > index; i--)
630 parent->subkeys[i] = parent->subkeys[i-1];
631 parent->subkeys[index] = key;
632 if (is_wow6432node( key->name, key->namelen ) && !is_wow6432node( parent->name, parent->namelen ))
633 parent->flags |= KEY_WOW64;
635 return key;
638 /* free a subkey of a given key */
639 static void free_subkey( struct key *parent, int index )
641 struct key *key;
642 int i, nb_subkeys;
644 assert( index >= 0 );
645 assert( index <= parent->last_subkey );
647 key = parent->subkeys[index];
648 for (i = index; i < parent->last_subkey; i++) parent->subkeys[i] = parent->subkeys[i + 1];
649 parent->last_subkey--;
650 key->flags |= KEY_DELETED;
651 key->parent = NULL;
652 if (is_wow6432node( key->name, key->namelen )) parent->flags &= ~KEY_WOW64;
653 release_object( key );
655 /* try to shrink the array */
656 nb_subkeys = parent->nb_subkeys;
657 if (nb_subkeys > MIN_SUBKEYS && parent->last_subkey < nb_subkeys / 2)
659 struct key **new_subkeys;
660 nb_subkeys -= nb_subkeys / 3; /* shrink by 33% */
661 if (nb_subkeys < MIN_SUBKEYS) nb_subkeys = MIN_SUBKEYS;
662 if (!(new_subkeys = realloc( parent->subkeys, nb_subkeys * sizeof(*new_subkeys) ))) return;
663 parent->subkeys = new_subkeys;
664 parent->nb_subkeys = nb_subkeys;
668 /* find the named child of a given key and return its index */
669 static struct key *find_subkey( const struct key *key, const struct unicode_str *name, int *index )
671 int i, min, max, res;
672 data_size_t len;
674 min = 0;
675 max = key->last_subkey;
676 while (min <= max)
678 i = (min + max) / 2;
679 len = min( key->subkeys[i]->namelen, name->len );
680 res = memicmp_strW( key->subkeys[i]->name, name->str, len );
681 if (!res) res = key->subkeys[i]->namelen - name->len;
682 if (!res)
684 *index = i;
685 return key->subkeys[i];
687 if (res > 0) max = i - 1;
688 else min = i + 1;
690 *index = min; /* this is where we should insert it */
691 return NULL;
694 /* return the wow64 variant of the key, or the key itself if none */
695 static struct key *find_wow64_subkey( struct key *key, const struct unicode_str *name )
697 static const struct unicode_str wow6432node_str = { wow6432node, sizeof(wow6432node) };
698 int index;
700 if (!(key->flags & KEY_WOW64)) return key;
701 if (!is_wow6432node( name->str, name->len ))
703 key = find_subkey( key, &wow6432node_str, &index );
704 assert( key ); /* if KEY_WOW64 is set we must find it */
706 return key;
710 /* follow a symlink and return the resolved key */
711 static struct key *follow_symlink( struct key *key, int iteration )
713 struct unicode_str path, token;
714 struct key_value *value;
715 int index;
717 if (iteration > 16) return NULL;
718 if (!(key->flags & KEY_SYMLINK)) return key;
719 if (!(value = find_value( key, &symlink_str, &index ))) return NULL;
721 path.str = value->data;
722 path.len = (value->len / sizeof(WCHAR)) * sizeof(WCHAR);
723 if (path.len <= sizeof(root_name)) return NULL;
724 if (memicmp_strW( path.str, root_name, sizeof(root_name) )) return NULL;
725 path.str += ARRAY_SIZE( root_name );
726 path.len -= sizeof(root_name);
728 key = root_key;
729 token.str = NULL;
730 if (!get_path_token( &path, &token )) return NULL;
731 while (token.len)
733 if (!(key = find_subkey( key, &token, &index ))) break;
734 if (!(key = follow_symlink( key, iteration + 1 ))) break;
735 get_path_token( &path, &token );
737 return key;
740 /* open a key until we find an element that doesn't exist */
741 /* helper for open_key and create_key */
742 static struct key *open_key_prefix( struct key *key, const struct unicode_str *name,
743 unsigned int access, struct unicode_str *token, int *index )
745 token->str = NULL;
746 if (!get_path_token( name, token )) return NULL;
747 if (access & KEY_WOW64_32KEY) key = find_wow64_subkey( key, token );
748 while (token->len)
750 struct key *subkey;
751 if (!(subkey = find_subkey( key, token, index )))
753 if ((key->flags & KEY_WOWSHARE) && !(access & KEY_WOW64_64KEY))
755 /* try in the 64-bit parent */
756 key = key->parent;
757 subkey = find_subkey( key, token, index );
760 if (!subkey) break;
761 key = subkey;
762 get_path_token( name, token );
763 if (!token->len) break;
764 if (!(access & KEY_WOW64_64KEY)) key = find_wow64_subkey( key, token );
765 if (!(key = follow_symlink( key, 0 )))
767 set_error( STATUS_OBJECT_NAME_NOT_FOUND );
768 return NULL;
771 return key;
774 /* open a subkey */
775 static struct key *open_key( struct key *key, const struct unicode_str *name, unsigned int access,
776 unsigned int attributes )
778 int index;
779 struct unicode_str token;
781 if (!(key = open_key_prefix( key, name, access, &token, &index ))) return NULL;
783 if (token.len)
785 set_error( STATUS_OBJECT_NAME_NOT_FOUND );
786 return NULL;
788 if (!(access & KEY_WOW64_64KEY)) key = find_wow64_subkey( key, &token );
789 if (!(attributes & OBJ_OPENLINK) && !(key = follow_symlink( key, 0 )))
791 set_error( STATUS_OBJECT_NAME_NOT_FOUND );
792 return NULL;
794 if (debug_level > 1) dump_operation( key, NULL, "Open" );
795 grab_object( key );
796 return key;
799 /* create a subkey */
800 static struct key *create_key( struct key *key, const struct unicode_str *name,
801 const struct unicode_str *class, unsigned int options,
802 unsigned int access, unsigned int attributes,
803 const struct security_descriptor *sd, int *created )
805 int index;
806 struct unicode_str token, next;
808 *created = 0;
809 if (!(key = open_key_prefix( key, name, access, &token, &index ))) return NULL;
811 if (!token.len) /* the key already exists */
813 if (!(access & KEY_WOW64_64KEY)) key = find_wow64_subkey( key, &token );
814 if (options & REG_OPTION_CREATE_LINK)
816 set_error( STATUS_OBJECT_NAME_COLLISION );
817 return NULL;
819 if (!(attributes & OBJ_OPENLINK) && !(key = follow_symlink( key, 0 )))
821 set_error( STATUS_OBJECT_NAME_NOT_FOUND );
822 return NULL;
824 if (debug_level > 1) dump_operation( key, NULL, "Open" );
825 grab_object( key );
826 return key;
829 /* token must be the last path component at this point */
830 next = token;
831 get_path_token( name, &next );
832 if (next.len)
834 set_error( STATUS_OBJECT_NAME_NOT_FOUND );
835 return NULL;
838 if ((key->flags & KEY_VOLATILE) && !(options & REG_OPTION_VOLATILE))
840 set_error( STATUS_CHILD_MUST_BE_VOLATILE );
841 return NULL;
843 *created = 1;
844 make_dirty( key );
845 if (!(key = alloc_subkey( key, &token, index, current_time ))) return NULL;
847 if (options & REG_OPTION_CREATE_LINK) key->flags |= KEY_SYMLINK;
848 if (options & REG_OPTION_VOLATILE) key->flags |= KEY_VOLATILE;
849 else key->flags |= KEY_DIRTY;
851 if (sd) default_set_sd( &key->obj, sd, OWNER_SECURITY_INFORMATION | GROUP_SECURITY_INFORMATION |
852 DACL_SECURITY_INFORMATION | SACL_SECURITY_INFORMATION );
854 if (debug_level > 1) dump_operation( key, NULL, "Create" );
855 if (class && class->len)
857 key->classlen = class->len;
858 free(key->class);
859 if (!(key->class = memdup( class->str, key->classlen ))) key->classlen = 0;
861 touch_key( key->parent, REG_NOTIFY_CHANGE_NAME );
862 grab_object( key );
863 return key;
866 /* recursively create a subkey (for internal use only) */
867 static struct key *create_key_recursive( struct key *key, const struct unicode_str *name, timeout_t modif )
869 struct key *base;
870 int index;
871 struct unicode_str token;
873 token.str = NULL;
874 if (!get_path_token( name, &token )) return NULL;
875 while (token.len)
877 struct key *subkey;
878 if (!(subkey = find_subkey( key, &token, &index ))) break;
879 key = subkey;
880 if (!(key = follow_symlink( key, 0 )))
882 set_error( STATUS_OBJECT_NAME_NOT_FOUND );
883 return NULL;
885 get_path_token( name, &token );
888 if (token.len)
890 if (!(key = alloc_subkey( key, &token, index, modif ))) return NULL;
891 base = key;
892 for (;;)
894 get_path_token( name, &token );
895 if (!token.len) break;
896 /* we know the index is always 0 in a new key */
897 if (!(key = alloc_subkey( key, &token, 0, modif )))
899 free_subkey( base, index );
900 return NULL;
905 grab_object( key );
906 return key;
909 /* query information about a key or a subkey */
910 static void enum_key( struct key *key, int index, int info_class, struct enum_key_reply *reply )
912 int i;
913 data_size_t len, namelen, classlen;
914 data_size_t max_subkey = 0, max_class = 0;
915 data_size_t max_value = 0, max_data = 0;
916 WCHAR *fullname = NULL;
917 char *data;
919 if (index != -1) /* -1 means use the specified key directly */
921 if ((index < 0) || (index > key->last_subkey))
923 set_error( STATUS_NO_MORE_ENTRIES );
924 return;
926 key = key->subkeys[index];
929 namelen = key->namelen;
930 classlen = key->classlen;
932 switch(info_class)
934 case KeyNameInformation:
935 if (!(fullname = key->obj.ops->get_full_name( &key->obj, &namelen ))) return;
936 /* fall through */
937 case KeyBasicInformation:
938 classlen = 0; /* only return the name */
939 /* fall through */
940 case KeyNodeInformation:
941 reply->max_subkey = 0;
942 reply->max_class = 0;
943 reply->max_value = 0;
944 reply->max_data = 0;
945 break;
946 case KeyFullInformation:
947 case KeyCachedInformation:
948 for (i = 0; i <= key->last_subkey; i++)
950 if (key->subkeys[i]->namelen > max_subkey) max_subkey = key->subkeys[i]->namelen;
951 if (key->subkeys[i]->classlen > max_class) max_class = key->subkeys[i]->classlen;
953 for (i = 0; i <= key->last_value; i++)
955 if (key->values[i].namelen > max_value) max_value = key->values[i].namelen;
956 if (key->values[i].len > max_data) max_data = key->values[i].len;
958 reply->max_subkey = max_subkey;
959 reply->max_class = max_class;
960 reply->max_value = max_value;
961 reply->max_data = max_data;
962 reply->namelen = namelen;
963 if (info_class == KeyCachedInformation)
964 classlen = 0; /* don't return any data, only its size */
965 namelen = 0; /* don't return name */
966 break;
967 default:
968 set_error( STATUS_INVALID_PARAMETER );
969 return;
971 reply->subkeys = key->last_subkey + 1;
972 reply->values = key->last_value + 1;
973 reply->modif = key->modif;
974 reply->total = namelen + classlen;
976 len = min( reply->total, get_reply_max_size() );
977 if (len && (data = set_reply_data_size( len )))
979 if (len > namelen)
981 reply->namelen = namelen;
982 memcpy( data, key->name, namelen );
983 memcpy( data + namelen, key->class, len - namelen );
985 else if (info_class == KeyNameInformation)
987 reply->namelen = namelen;
988 memcpy( data, fullname, len );
990 else
992 reply->namelen = len;
993 memcpy( data, key->name, len );
996 free( fullname );
997 if (debug_level > 1) dump_operation( key, NULL, "Enum" );
1000 /* delete a key and its values */
1001 static int delete_key( struct key *key, int recurse )
1003 int index;
1004 struct key *parent = key->parent;
1006 /* must find parent and index */
1007 if (key == root_key)
1009 set_error( STATUS_ACCESS_DENIED );
1010 return -1;
1012 assert( parent );
1014 while (recurse && (key->last_subkey>=0))
1015 if (0 > delete_key(key->subkeys[key->last_subkey], 1))
1016 return -1;
1018 for (index = 0; index <= parent->last_subkey; index++)
1019 if (parent->subkeys[index] == key) break;
1020 assert( index <= parent->last_subkey );
1022 /* we can only delete a key that has no subkeys */
1023 if (key->last_subkey >= 0)
1025 set_error( STATUS_ACCESS_DENIED );
1026 return -1;
1029 if (debug_level > 1) dump_operation( key, NULL, "Delete" );
1030 free_subkey( parent, index );
1031 touch_key( parent, REG_NOTIFY_CHANGE_NAME );
1032 return 0;
1035 /* try to grow the array of values; return 1 if OK, 0 on error */
1036 static int grow_values( struct key *key )
1038 struct key_value *new_val;
1039 int nb_values;
1041 if (key->nb_values)
1043 nb_values = key->nb_values + (key->nb_values / 2); /* grow by 50% */
1044 if (!(new_val = realloc( key->values, nb_values * sizeof(*new_val) )))
1046 set_error( STATUS_NO_MEMORY );
1047 return 0;
1050 else
1052 nb_values = MIN_VALUES;
1053 if (!(new_val = mem_alloc( nb_values * sizeof(*new_val) ))) return 0;
1055 key->values = new_val;
1056 key->nb_values = nb_values;
1057 return 1;
1060 /* find the named value of a given key and return its index in the array */
1061 static struct key_value *find_value( const struct key *key, const struct unicode_str *name, int *index )
1063 int i, min, max, res;
1064 data_size_t len;
1066 min = 0;
1067 max = key->last_value;
1068 while (min <= max)
1070 i = (min + max) / 2;
1071 len = min( key->values[i].namelen, name->len );
1072 res = memicmp_strW( key->values[i].name, name->str, len );
1073 if (!res) res = key->values[i].namelen - name->len;
1074 if (!res)
1076 *index = i;
1077 return &key->values[i];
1079 if (res > 0) max = i - 1;
1080 else min = i + 1;
1082 *index = min; /* this is where we should insert it */
1083 return NULL;
1086 /* insert a new value; the index must have been returned by find_value */
1087 static struct key_value *insert_value( struct key *key, const struct unicode_str *name, int index )
1089 struct key_value *value;
1090 WCHAR *new_name = NULL;
1091 int i;
1093 if (name->len > MAX_VALUE_LEN * sizeof(WCHAR))
1095 set_error( STATUS_NAME_TOO_LONG );
1096 return NULL;
1098 if (key->last_value + 1 == key->nb_values)
1100 if (!grow_values( key )) return NULL;
1102 if (name->len && !(new_name = memdup( name->str, name->len ))) return NULL;
1103 for (i = ++key->last_value; i > index; i--) key->values[i] = key->values[i - 1];
1104 value = &key->values[index];
1105 value->name = new_name;
1106 value->namelen = name->len;
1107 value->len = 0;
1108 value->data = NULL;
1109 return value;
1112 /* set a key value */
1113 static void set_value( struct key *key, const struct unicode_str *name,
1114 int type, const void *data, data_size_t len )
1116 struct key_value *value;
1117 void *ptr = NULL;
1118 int index;
1120 if ((value = find_value( key, name, &index )))
1122 /* check if the new value is identical to the existing one */
1123 if (value->type == type && value->len == len &&
1124 value->data && !memcmp( value->data, data, len ))
1126 if (debug_level > 1) dump_operation( key, value, "Skip setting" );
1127 return;
1131 if (key->flags & KEY_SYMLINK)
1133 if (type != REG_LINK || name->len != symlink_str.len ||
1134 memicmp_strW( name->str, symlink_str.str, name->len ))
1136 set_error( STATUS_ACCESS_DENIED );
1137 return;
1141 if (len && !(ptr = memdup( data, len ))) return;
1143 if (!value)
1145 if (!(value = insert_value( key, name, index )))
1147 free( ptr );
1148 return;
1151 else free( value->data ); /* already existing, free previous data */
1153 value->type = type;
1154 value->len = len;
1155 value->data = ptr;
1156 touch_key( key, REG_NOTIFY_CHANGE_LAST_SET );
1157 if (debug_level > 1) dump_operation( key, value, "Set" );
1160 /* get a key value */
1161 static void get_value( struct key *key, const struct unicode_str *name, int *type, data_size_t *len )
1163 struct key_value *value;
1164 int index;
1166 if ((value = find_value( key, name, &index )))
1168 *type = value->type;
1169 *len = value->len;
1170 if (value->data) set_reply_data( value->data, min( value->len, get_reply_max_size() ));
1171 if (debug_level > 1) dump_operation( key, value, "Get" );
1173 else
1175 *type = -1;
1176 set_error( STATUS_OBJECT_NAME_NOT_FOUND );
1180 /* enumerate a key value */
1181 static void enum_value( struct key *key, int i, int info_class, struct enum_key_value_reply *reply )
1183 struct key_value *value;
1185 if (i < 0 || i > key->last_value) set_error( STATUS_NO_MORE_ENTRIES );
1186 else
1188 void *data;
1189 data_size_t namelen, maxlen;
1191 value = &key->values[i];
1192 reply->type = value->type;
1193 namelen = value->namelen;
1195 switch(info_class)
1197 case KeyValueBasicInformation:
1198 reply->total = namelen;
1199 break;
1200 case KeyValueFullInformation:
1201 reply->total = namelen + value->len;
1202 break;
1203 case KeyValuePartialInformation:
1204 reply->total = value->len;
1205 namelen = 0;
1206 break;
1207 default:
1208 set_error( STATUS_INVALID_PARAMETER );
1209 return;
1212 maxlen = min( reply->total, get_reply_max_size() );
1213 if (maxlen && ((data = set_reply_data_size( maxlen ))))
1215 if (maxlen > namelen)
1217 reply->namelen = namelen;
1218 memcpy( data, value->name, namelen );
1219 memcpy( (char *)data + namelen, value->data, maxlen - namelen );
1221 else
1223 reply->namelen = maxlen;
1224 memcpy( data, value->name, maxlen );
1227 if (debug_level > 1) dump_operation( key, value, "Enum" );
1231 /* delete a value */
1232 static void delete_value( struct key *key, const struct unicode_str *name )
1234 struct key_value *value;
1235 int i, index, nb_values;
1237 if (!(value = find_value( key, name, &index )))
1239 set_error( STATUS_OBJECT_NAME_NOT_FOUND );
1240 return;
1242 if (debug_level > 1) dump_operation( key, value, "Delete" );
1243 free( value->name );
1244 free( value->data );
1245 for (i = index; i < key->last_value; i++) key->values[i] = key->values[i + 1];
1246 key->last_value--;
1247 touch_key( key, REG_NOTIFY_CHANGE_LAST_SET );
1249 /* try to shrink the array */
1250 nb_values = key->nb_values;
1251 if (nb_values > MIN_VALUES && key->last_value < nb_values / 2)
1253 struct key_value *new_val;
1254 nb_values -= nb_values / 3; /* shrink by 33% */
1255 if (nb_values < MIN_VALUES) nb_values = MIN_VALUES;
1256 if (!(new_val = realloc( key->values, nb_values * sizeof(*new_val) ))) return;
1257 key->values = new_val;
1258 key->nb_values = nb_values;
1262 /* get the registry key corresponding to an hkey handle */
1263 static struct key *get_hkey_obj( obj_handle_t hkey, unsigned int access )
1265 struct key *key = (struct key *)get_handle_obj( current->process, hkey, access, &key_ops );
1267 if (key && key->flags & KEY_DELETED)
1269 set_error( STATUS_KEY_DELETED );
1270 release_object( key );
1271 key = NULL;
1273 return key;
1276 /* get the registry key corresponding to a parent key handle */
1277 static inline struct key *get_parent_hkey_obj( obj_handle_t hkey )
1279 if (!hkey) return (struct key *)grab_object( root_key );
1280 return get_hkey_obj( hkey, 0 );
1283 /* read a line from the input file */
1284 static int read_next_line( struct file_load_info *info )
1286 char *newbuf;
1287 int newlen, pos = 0;
1289 info->line++;
1290 for (;;)
1292 if (!fgets( info->buffer + pos, info->len - pos, info->file ))
1293 return (pos != 0); /* EOF */
1294 pos = strlen(info->buffer);
1295 if (info->buffer[pos-1] == '\n')
1297 /* got a full line */
1298 info->buffer[--pos] = 0;
1299 if (pos > 0 && info->buffer[pos-1] == '\r') info->buffer[pos-1] = 0;
1300 return 1;
1302 if (pos < info->len - 1) return 1; /* EOF but something was read */
1304 /* need to enlarge the buffer */
1305 newlen = info->len + info->len / 2;
1306 if (!(newbuf = realloc( info->buffer, newlen )))
1308 set_error( STATUS_NO_MEMORY );
1309 return -1;
1311 info->buffer = newbuf;
1312 info->len = newlen;
1316 /* make sure the temp buffer holds enough space */
1317 static int get_file_tmp_space( struct file_load_info *info, size_t size )
1319 WCHAR *tmp;
1320 if (info->tmplen >= size) return 1;
1321 if (!(tmp = realloc( info->tmp, size )))
1323 set_error( STATUS_NO_MEMORY );
1324 return 0;
1326 info->tmp = tmp;
1327 info->tmplen = size;
1328 return 1;
1331 /* report an error while loading an input file */
1332 static void file_read_error( const char *err, struct file_load_info *info )
1334 if (info->filename)
1335 fprintf( stderr, "%s:%d: %s '%s'\n", info->filename, info->line, err, info->buffer );
1336 else
1337 fprintf( stderr, "<fd>:%d: %s '%s'\n", info->line, err, info->buffer );
1340 /* convert a data type tag to a value type */
1341 static int get_data_type( const char *buffer, int *type, int *parse_type )
1343 struct data_type { const char *tag; int len; int type; int parse_type; };
1345 static const struct data_type data_types[] =
1346 { /* actual type */ /* type to assume for parsing */
1347 { "\"", 1, REG_SZ, REG_SZ },
1348 { "str:\"", 5, REG_SZ, REG_SZ },
1349 { "str(2):\"", 8, REG_EXPAND_SZ, REG_SZ },
1350 { "str(7):\"", 8, REG_MULTI_SZ, REG_SZ },
1351 { "hex:", 4, REG_BINARY, REG_BINARY },
1352 { "dword:", 6, REG_DWORD, REG_DWORD },
1353 { "hex(", 4, -1, REG_BINARY },
1354 { NULL, 0, 0, 0 }
1357 const struct data_type *ptr;
1358 char *end;
1360 for (ptr = data_types; ptr->tag; ptr++)
1362 if (strncmp( ptr->tag, buffer, ptr->len )) continue;
1363 *parse_type = ptr->parse_type;
1364 if ((*type = ptr->type) != -1) return ptr->len;
1365 /* "hex(xx):" is special */
1366 *type = (int)strtoul( buffer + 4, &end, 16 );
1367 if ((end <= buffer) || strncmp( end, "):", 2 )) return 0;
1368 return end + 2 - buffer;
1370 return 0;
1373 /* load and create a key from the input file */
1374 static struct key *load_key( struct key *base, const char *buffer, int prefix_len,
1375 struct file_load_info *info, timeout_t *modif )
1377 WCHAR *p;
1378 struct unicode_str name;
1379 int res;
1380 unsigned int mod;
1381 data_size_t len;
1383 if (!get_file_tmp_space( info, strlen(buffer) * sizeof(WCHAR) )) return NULL;
1385 len = info->tmplen;
1386 if ((res = parse_strW( info->tmp, &len, buffer, ']' )) == -1)
1388 file_read_error( "Malformed key", info );
1389 return NULL;
1391 if (sscanf( buffer + res, " %u", &mod ) == 1)
1392 *modif = (timeout_t)mod * TICKS_PER_SEC + ticks_1601_to_1970;
1393 else
1394 *modif = current_time;
1396 p = info->tmp;
1397 while (prefix_len && *p) { if (*p++ == '\\') prefix_len--; }
1399 if (!*p)
1401 if (prefix_len > 1)
1403 file_read_error( "Malformed key", info );
1404 return NULL;
1406 /* empty key name, return base key */
1407 return (struct key *)grab_object( base );
1409 name.str = p;
1410 name.len = len - (p - info->tmp + 1) * sizeof(WCHAR);
1411 return create_key_recursive( base, &name, 0 );
1414 /* update the modification time of a key (and its parents) after it has been loaded from a file */
1415 static void update_key_time( struct key *key, timeout_t modif )
1417 while (key && !key->modif)
1419 key->modif = modif;
1420 key = key->parent;
1424 /* load a global option from the input file */
1425 static int load_global_option( const char *buffer, struct file_load_info *info )
1427 const char *p;
1429 if (!strncmp( buffer, "#arch=", 6 ))
1431 enum prefix_type type;
1432 p = buffer + 6;
1433 if (!strcmp( p, "win32" )) type = PREFIX_32BIT;
1434 else if (!strcmp( p, "win64" )) type = PREFIX_64BIT;
1435 else
1437 file_read_error( "Unknown architecture", info );
1438 set_error( STATUS_NOT_REGISTRY_FILE );
1439 return 0;
1441 if (prefix_type == PREFIX_UNKNOWN) prefix_type = type;
1442 else if (type != prefix_type)
1444 file_read_error( "Mismatched architecture", info );
1445 set_error( STATUS_NOT_REGISTRY_FILE );
1446 return 0;
1449 /* ignore unknown options */
1450 return 1;
1453 /* load a key option from the input file */
1454 static int load_key_option( struct key *key, const char *buffer, struct file_load_info *info )
1456 const char *p;
1457 data_size_t len;
1459 if (!strncmp( buffer, "#time=", 6 ))
1461 timeout_t modif = 0;
1462 for (p = buffer + 6; *p; p++)
1464 if (*p >= '0' && *p <= '9') modif = (modif << 4) | (*p - '0');
1465 else if (*p >= 'A' && *p <= 'F') modif = (modif << 4) | (*p - 'A' + 10);
1466 else if (*p >= 'a' && *p <= 'f') modif = (modif << 4) | (*p - 'a' + 10);
1467 else break;
1469 update_key_time( key, modif );
1471 if (!strncmp( buffer, "#class=", 7 ))
1473 p = buffer + 7;
1474 if (*p++ != '"') return 0;
1475 if (!get_file_tmp_space( info, strlen(p) * sizeof(WCHAR) )) return 0;
1476 len = info->tmplen;
1477 if (parse_strW( info->tmp, &len, p, '\"' ) == -1) return 0;
1478 free( key->class );
1479 if (!(key->class = memdup( info->tmp, len ))) len = 0;
1480 key->classlen = len;
1482 if (!strncmp( buffer, "#link", 5 )) key->flags |= KEY_SYMLINK;
1483 /* ignore unknown options */
1484 return 1;
1487 /* parse a comma-separated list of hex digits */
1488 static int parse_hex( unsigned char *dest, data_size_t *len, const char *buffer )
1490 const char *p = buffer;
1491 data_size_t count = 0;
1492 char *end;
1494 while (isxdigit(*p))
1496 unsigned int val = strtoul( p, &end, 16 );
1497 if (end == p || val > 0xff) return -1;
1498 if (count++ >= *len) return -1; /* dest buffer overflow */
1499 *dest++ = val;
1500 p = end;
1501 while (isspace(*p)) p++;
1502 if (*p == ',') p++;
1503 while (isspace(*p)) p++;
1505 *len = count;
1506 return p - buffer;
1509 /* parse a value name and create the corresponding value */
1510 static struct key_value *parse_value_name( struct key *key, const char *buffer, data_size_t *len,
1511 struct file_load_info *info )
1513 struct key_value *value;
1514 struct unicode_str name;
1515 int index;
1517 if (!get_file_tmp_space( info, strlen(buffer) * sizeof(WCHAR) )) return NULL;
1518 name.str = info->tmp;
1519 name.len = info->tmplen;
1520 if (buffer[0] == '@')
1522 name.len = 0;
1523 *len = 1;
1525 else
1527 int r = parse_strW( info->tmp, &name.len, buffer + 1, '\"' );
1528 if (r == -1) goto error;
1529 *len = r + 1; /* for initial quote */
1530 name.len -= sizeof(WCHAR); /* terminating null */
1532 while (isspace(buffer[*len])) (*len)++;
1533 if (buffer[*len] != '=') goto error;
1534 (*len)++;
1535 while (isspace(buffer[*len])) (*len)++;
1536 if (!(value = find_value( key, &name, &index ))) value = insert_value( key, &name, index );
1537 return value;
1539 error:
1540 file_read_error( "Malformed value name", info );
1541 return NULL;
1544 /* load a value from the input file */
1545 static int load_value( struct key *key, const char *buffer, struct file_load_info *info )
1547 DWORD dw;
1548 void *ptr, *newptr;
1549 int res, type, parse_type;
1550 data_size_t maxlen, len;
1551 struct key_value *value;
1553 if (!(value = parse_value_name( key, buffer, &len, info ))) return 0;
1554 if (!(res = get_data_type( buffer + len, &type, &parse_type ))) goto error;
1555 buffer += len + res;
1557 switch(parse_type)
1559 case REG_SZ:
1560 if (!get_file_tmp_space( info, strlen(buffer) * sizeof(WCHAR) )) return 0;
1561 len = info->tmplen;
1562 if ((res = parse_strW( info->tmp, &len, buffer, '\"' )) == -1) goto error;
1563 ptr = info->tmp;
1564 break;
1565 case REG_DWORD:
1566 dw = strtoul( buffer, NULL, 16 );
1567 ptr = &dw;
1568 len = sizeof(dw);
1569 break;
1570 case REG_BINARY: /* hex digits */
1571 len = 0;
1572 for (;;)
1574 maxlen = 1 + strlen(buffer) / 2; /* at least 2 chars for one hex byte */
1575 if (!get_file_tmp_space( info, len + maxlen )) return 0;
1576 if ((res = parse_hex( (unsigned char *)info->tmp + len, &maxlen, buffer )) == -1) goto error;
1577 len += maxlen;
1578 buffer += res;
1579 while (isspace(*buffer)) buffer++;
1580 if (!*buffer) break;
1581 if (*buffer != '\\') goto error;
1582 if (read_next_line( info) != 1) goto error;
1583 buffer = info->buffer;
1584 while (isspace(*buffer)) buffer++;
1586 ptr = info->tmp;
1587 break;
1588 default:
1589 assert(0);
1590 ptr = NULL; /* keep compiler quiet */
1591 break;
1594 if (!len) newptr = NULL;
1595 else if (!(newptr = memdup( ptr, len ))) return 0;
1597 free( value->data );
1598 value->data = newptr;
1599 value->len = len;
1600 value->type = type;
1601 return 1;
1603 error:
1604 file_read_error( "Malformed value", info );
1605 free( value->data );
1606 value->data = NULL;
1607 value->len = 0;
1608 value->type = REG_NONE;
1609 return 0;
1612 /* return the length (in path elements) of name that is part of the key name */
1613 /* for instance if key is USER\foo\bar and name is foo\bar\baz, return 2 */
1614 static int get_prefix_len( struct key *key, const char *name, struct file_load_info *info )
1616 WCHAR *p;
1617 int res;
1618 data_size_t len;
1620 if (!get_file_tmp_space( info, strlen(name) * sizeof(WCHAR) )) return 0;
1622 len = info->tmplen;
1623 if ((res = parse_strW( info->tmp, &len, name, ']' )) == -1)
1625 file_read_error( "Malformed key", info );
1626 return 0;
1628 for (p = info->tmp; *p; p++) if (*p == '\\') break;
1629 len = (p - info->tmp) * sizeof(WCHAR);
1630 for (res = 1; key != root_key; res++)
1632 if (len == key->namelen && !memicmp_strW( info->tmp, key->name, len )) break;
1633 key = key->parent;
1635 if (key == root_key) res = 0; /* no matching name */
1636 return res;
1639 /* load all the keys from the input file */
1640 /* prefix_len is the number of key name prefixes to skip, or -1 for autodetection */
1641 static void load_keys( struct key *key, const char *filename, FILE *f, int prefix_len )
1643 struct key *subkey = NULL;
1644 struct file_load_info info;
1645 timeout_t modif = current_time;
1646 char *p;
1648 info.filename = filename;
1649 info.file = f;
1650 info.len = 4;
1651 info.tmplen = 4;
1652 info.line = 0;
1653 if (!(info.buffer = mem_alloc( info.len ))) return;
1654 if (!(info.tmp = mem_alloc( info.tmplen )))
1656 free( info.buffer );
1657 return;
1660 if ((read_next_line( &info ) != 1) ||
1661 strcmp( info.buffer, "WINE REGISTRY Version 2" ))
1663 set_error( STATUS_NOT_REGISTRY_FILE );
1664 goto done;
1667 while (read_next_line( &info ) == 1)
1669 p = info.buffer;
1670 while (*p && isspace(*p)) p++;
1671 switch(*p)
1673 case '[': /* new key */
1674 if (subkey)
1676 update_key_time( subkey, modif );
1677 release_object( subkey );
1679 if (prefix_len == -1) prefix_len = get_prefix_len( key, p + 1, &info );
1680 if (!(subkey = load_key( key, p + 1, prefix_len, &info, &modif )))
1681 file_read_error( "Error creating key", &info );
1682 break;
1683 case '@': /* default value */
1684 case '\"': /* value */
1685 if (subkey) load_value( subkey, p, &info );
1686 else file_read_error( "Value without key", &info );
1687 break;
1688 case '#': /* option */
1689 if (subkey) load_key_option( subkey, p, &info );
1690 else if (!load_global_option( p, &info )) goto done;
1691 break;
1692 case ';': /* comment */
1693 case 0: /* empty line */
1694 break;
1695 default:
1696 file_read_error( "Unrecognized input", &info );
1697 break;
1701 done:
1702 if (subkey)
1704 update_key_time( subkey, modif );
1705 release_object( subkey );
1707 free( info.buffer );
1708 free( info.tmp );
1711 /* load a part of the registry from a file */
1712 static void load_registry( struct key *key, obj_handle_t handle )
1714 struct file *file;
1715 int fd;
1717 if (!(file = get_file_obj( current->process, handle, FILE_READ_DATA ))) return;
1718 fd = dup( get_file_unix_fd( file ) );
1719 release_object( file );
1720 if (fd != -1)
1722 FILE *f = fdopen( fd, "r" );
1723 if (f)
1725 load_keys( key, NULL, f, -1 );
1726 fclose( f );
1728 else file_set_error();
1732 /* load one of the initial registry files */
1733 static int load_init_registry_from_file( const char *filename, struct key *key )
1735 FILE *f;
1737 if ((f = fopen( filename, "r" )))
1739 load_keys( key, filename, f, 0 );
1740 fclose( f );
1741 if (get_error() == STATUS_NOT_REGISTRY_FILE)
1743 fprintf( stderr, "%s is not a valid registry file\n", filename );
1744 return 1;
1748 assert( save_branch_count < MAX_SAVE_BRANCH_INFO );
1750 save_branch_info[save_branch_count].path = filename;
1751 save_branch_info[save_branch_count++].key = (struct key *)grab_object( key );
1752 make_object_permanent( &key->obj );
1753 return (f != NULL);
1756 static WCHAR *format_user_registry_path( const SID *sid, struct unicode_str *path )
1758 char buffer[7 + 11 + 11 + 11 * SID_MAX_SUB_AUTHORITIES], *p = buffer;
1759 unsigned int i;
1761 p += sprintf( p, "User\\S-%u-%u", sid->Revision,
1762 MAKELONG( MAKEWORD( sid->IdentifierAuthority.Value[5],
1763 sid->IdentifierAuthority.Value[4] ),
1764 MAKEWORD( sid->IdentifierAuthority.Value[3],
1765 sid->IdentifierAuthority.Value[2] )));
1766 for (i = 0; i < sid->SubAuthorityCount; i++) p += sprintf( p, "-%u", sid->SubAuthority[i] );
1767 return ascii_to_unicode_str( buffer, path );
1770 /* get the cpu architectures that can be supported in the current prefix */
1771 unsigned int get_prefix_cpu_mask(void)
1773 /* Allowed server/client/prefix combinations:
1775 * prefix
1776 * 32 64
1777 * server +------+------+ client
1778 * | ok | fail | 32
1779 * 32 +------+------+---
1780 * | fail | fail | 64
1781 * ---+------+------+---
1782 * | ok | ok | 32
1783 * 64 +------+------+---
1784 * | fail | ok | 64
1785 * ---+------+------+---
1787 switch (prefix_type)
1789 case PREFIX_64BIT:
1790 /* 64-bit prefix requires 64-bit server */
1791 return sizeof(void *) > sizeof(int) ? ~0 : 0;
1792 case PREFIX_32BIT:
1793 default:
1794 return ~CPU_64BIT_MASK; /* only 32-bit cpus supported on 32-bit prefix */
1798 /* registry initialisation */
1799 void init_registry(void)
1801 static const WCHAR HKLM[] = { 'M','a','c','h','i','n','e' };
1802 static const WCHAR HKU_default[] = { 'U','s','e','r','\\','.','D','e','f','a','u','l','t' };
1803 static const WCHAR classes[] = {'S','o','f','t','w','a','r','e','\\',
1804 'C','l','a','s','s','e','s','\\',
1805 'W','o','w','6','4','3','2','N','o','d','e'};
1806 static const struct unicode_str root_name = { NULL, 0 };
1807 static const struct unicode_str HKLM_name = { HKLM, sizeof(HKLM) };
1808 static const struct unicode_str HKU_name = { HKU_default, sizeof(HKU_default) };
1809 static const struct unicode_str classes_name = { classes, sizeof(classes) };
1811 WCHAR *current_user_path;
1812 struct unicode_str current_user_str;
1813 struct key *key, *hklm, *hkcu;
1814 char *p;
1816 /* switch to the config dir */
1818 if (fchdir( config_dir_fd ) == -1) fatal_error( "chdir to config dir: %s\n", strerror( errno ));
1820 /* create the root key */
1821 root_key = alloc_key( &root_name, current_time );
1822 assert( root_key );
1823 make_object_permanent( &root_key->obj );
1825 /* load system.reg into Registry\Machine */
1827 if (!(hklm = create_key_recursive( root_key, &HKLM_name, current_time )))
1828 fatal_error( "could not create Machine registry key\n" );
1830 if (!load_init_registry_from_file( "system.reg", hklm ))
1832 if ((p = getenv( "WINEARCH" )) && !strcmp( p, "win32" ))
1833 prefix_type = PREFIX_32BIT;
1834 else
1835 prefix_type = sizeof(void *) > sizeof(int) ? PREFIX_64BIT : PREFIX_32BIT;
1837 else if (prefix_type == PREFIX_UNKNOWN)
1838 prefix_type = PREFIX_32BIT;
1840 /* load userdef.reg into Registry\User\.Default */
1842 if (!(key = create_key_recursive( root_key, &HKU_name, current_time )))
1843 fatal_error( "could not create User\\.Default registry key\n" );
1845 load_init_registry_from_file( "userdef.reg", key );
1846 release_object( key );
1848 /* load user.reg into HKEY_CURRENT_USER */
1850 /* FIXME: match default user in token.c. should get from process token instead */
1851 current_user_path = format_user_registry_path( security_local_user_sid, &current_user_str );
1852 if (!current_user_path ||
1853 !(hkcu = create_key_recursive( root_key, &current_user_str, current_time )))
1854 fatal_error( "could not create HKEY_CURRENT_USER registry key\n" );
1855 free( current_user_path );
1856 load_init_registry_from_file( "user.reg", hkcu );
1858 /* set the shared flag on Software\Classes\Wow6432Node */
1859 if (prefix_type == PREFIX_64BIT)
1861 if ((key = create_key_recursive( hklm, &classes_name, current_time )))
1863 key->flags |= KEY_WOWSHARE;
1864 release_object( key );
1866 /* FIXME: handle HKCU too */
1869 release_object( hklm );
1870 release_object( hkcu );
1872 /* start the periodic save timer */
1873 set_periodic_save_timer();
1875 /* create windows directories */
1877 if (!mkdir( "drive_c/windows", 0777 ))
1879 mkdir( "drive_c/windows/system32", 0777 );
1880 if (prefix_type == PREFIX_64BIT) mkdir( "drive_c/windows/syswow64", 0777 );
1883 /* go back to the server dir */
1884 if (fchdir( server_dir_fd ) == -1) fatal_error( "chdir to server dir: %s\n", strerror( errno ));
1887 /* save a registry branch to a file */
1888 static void save_all_subkeys( struct key *key, FILE *f )
1890 fprintf( f, "WINE REGISTRY Version 2\n" );
1891 fprintf( f, ";; All keys relative to " );
1892 dump_path( key, NULL, f );
1893 fprintf( f, "\n" );
1894 switch (prefix_type)
1896 case PREFIX_32BIT:
1897 fprintf( f, "\n#arch=win32\n" );
1898 break;
1899 case PREFIX_64BIT:
1900 fprintf( f, "\n#arch=win64\n" );
1901 break;
1902 default:
1903 break;
1905 save_subkeys( key, key, f );
1908 /* save a registry branch to a file handle */
1909 static void save_registry( struct key *key, obj_handle_t handle )
1911 struct file *file;
1912 int fd;
1914 if (!(file = get_file_obj( current->process, handle, FILE_WRITE_DATA ))) return;
1915 fd = dup( get_file_unix_fd( file ) );
1916 release_object( file );
1917 if (fd != -1)
1919 FILE *f = fdopen( fd, "w" );
1920 if (f)
1922 save_all_subkeys( key, f );
1923 if (fclose( f )) file_set_error();
1925 else
1927 file_set_error();
1928 close( fd );
1933 /* save a registry branch to a file */
1934 static int save_branch( struct key *key, const char *path )
1936 struct stat st;
1937 char *p, *tmp = NULL;
1938 int fd, count = 0, ret = 0;
1939 FILE *f;
1941 if (!(key->flags & KEY_DIRTY))
1943 if (debug_level > 1) dump_operation( key, NULL, "Not saving clean" );
1944 return 1;
1947 /* test the file type */
1949 if ((fd = open( path, O_WRONLY )) != -1)
1951 /* if file is not a regular file or has multiple links or is accessed
1952 * via symbolic links, write directly into it; otherwise use a temp file */
1953 if (!lstat( path, &st ) && (!S_ISREG(st.st_mode) || st.st_nlink > 1))
1955 ftruncate( fd, 0 );
1956 goto save;
1958 close( fd );
1961 /* create a temp file in the same directory */
1963 if (!(tmp = malloc( strlen(path) + 20 ))) goto done;
1964 strcpy( tmp, path );
1965 if ((p = strrchr( tmp, '/' ))) p++;
1966 else p = tmp;
1967 for (;;)
1969 sprintf( p, "reg%lx%04x.tmp", (long) getpid(), count++ );
1970 if ((fd = open( tmp, O_CREAT | O_EXCL | O_WRONLY, 0666 )) != -1) break;
1971 if (errno != EEXIST) goto done;
1972 close( fd );
1975 /* now save to it */
1977 save:
1978 if (!(f = fdopen( fd, "w" )))
1980 if (tmp) unlink( tmp );
1981 close( fd );
1982 goto done;
1985 if (debug_level > 1)
1987 fprintf( stderr, "%s: ", path );
1988 dump_operation( key, NULL, "saving" );
1991 save_all_subkeys( key, f );
1992 ret = !fclose(f);
1994 if (tmp)
1996 /* if successfully written, rename to final name */
1997 if (ret) ret = !rename( tmp, path );
1998 if (!ret) unlink( tmp );
2001 done:
2002 free( tmp );
2003 if (ret) make_clean( key );
2004 return ret;
2007 /* periodic saving of the registry */
2008 static void periodic_save( void *arg )
2010 int i;
2012 if (fchdir( config_dir_fd ) == -1) return;
2013 save_timeout_user = NULL;
2014 for (i = 0; i < save_branch_count; i++)
2015 save_branch( save_branch_info[i].key, save_branch_info[i].path );
2016 if (fchdir( server_dir_fd ) == -1) fatal_error( "chdir to server dir: %s\n", strerror( errno ));
2017 set_periodic_save_timer();
2020 /* start the periodic save timer */
2021 static void set_periodic_save_timer(void)
2023 if (save_timeout_user) remove_timeout_user( save_timeout_user );
2024 save_timeout_user = add_timeout_user( save_period, periodic_save, NULL );
2027 /* save the modified registry branches to disk */
2028 void flush_registry(void)
2030 int i;
2032 if (fchdir( config_dir_fd ) == -1) return;
2033 for (i = 0; i < save_branch_count; i++)
2035 if (!save_branch( save_branch_info[i].key, save_branch_info[i].path ))
2037 fprintf( stderr, "wineserver: could not save registry branch to %s",
2038 save_branch_info[i].path );
2039 perror( " " );
2042 if (fchdir( server_dir_fd ) == -1) fatal_error( "chdir to server dir: %s\n", strerror( errno ));
2045 /* determine if the thread is wow64 (32-bit client running on 64-bit prefix) */
2046 static int is_wow64_thread( struct thread *thread )
2048 return (prefix_type == PREFIX_64BIT && !(CPU_FLAG(thread->process->cpu) & CPU_64BIT_MASK));
2052 /* create a registry key */
2053 DECL_HANDLER(create_key)
2055 struct key *key = NULL, *parent;
2056 struct unicode_str name, class;
2057 unsigned int access = req->access;
2058 const struct security_descriptor *sd;
2059 const struct object_attributes *objattr = get_req_object_attributes( &sd, &name, NULL );
2061 if (!objattr) return;
2063 if (!is_wow64_thread( current )) access = (access & ~KEY_WOW64_32KEY) | KEY_WOW64_64KEY;
2065 class.str = get_req_data_after_objattr( objattr, &class.len );
2066 class.len = (class.len / sizeof(WCHAR)) * sizeof(WCHAR);
2068 if (!objattr->rootdir && name.len >= sizeof(root_name) &&
2069 !memicmp_strW( name.str, root_name, sizeof(root_name) ))
2071 name.str += ARRAY_SIZE( root_name );
2072 name.len -= sizeof(root_name);
2075 /* NOTE: no access rights are required from the parent handle to create a key */
2076 if ((parent = get_parent_hkey_obj( objattr->rootdir )))
2078 if ((key = create_key( parent, &name, &class, req->options, access,
2079 objattr->attributes, sd, &reply->created )))
2081 reply->hkey = alloc_handle( current->process, key, access, objattr->attributes );
2082 release_object( key );
2084 release_object( parent );
2088 /* open a registry key */
2089 DECL_HANDLER(open_key)
2091 struct key *key, *parent;
2092 struct unicode_str name;
2093 unsigned int access = req->access;
2095 if (!is_wow64_thread( current )) access = (access & ~KEY_WOW64_32KEY) | KEY_WOW64_64KEY;
2097 reply->hkey = 0;
2098 /* NOTE: no access rights are required to open the parent key, only the child key */
2099 if ((parent = get_parent_hkey_obj( req->parent )))
2101 get_req_path( &name, !req->parent );
2102 if ((key = open_key( parent, &name, access, req->attributes )))
2104 reply->hkey = alloc_handle( current->process, key, access, req->attributes );
2105 release_object( key );
2107 release_object( parent );
2111 /* delete a registry key */
2112 DECL_HANDLER(delete_key)
2114 struct key *key;
2116 if ((key = get_hkey_obj( req->hkey, DELETE )))
2118 delete_key( key, 0);
2119 release_object( key );
2123 /* flush a registry key */
2124 DECL_HANDLER(flush_key)
2126 struct key *key = get_hkey_obj( req->hkey, 0 );
2127 if (key)
2129 /* we don't need to do anything here with the current implementation */
2130 release_object( key );
2134 /* enumerate registry subkeys */
2135 DECL_HANDLER(enum_key)
2137 struct key *key;
2139 if ((key = get_hkey_obj( req->hkey,
2140 req->index == -1 ? KEY_QUERY_VALUE : KEY_ENUMERATE_SUB_KEYS )))
2142 enum_key( key, req->index, req->info_class, reply );
2143 release_object( key );
2147 /* set a value of a registry key */
2148 DECL_HANDLER(set_key_value)
2150 struct key *key;
2151 struct unicode_str name;
2153 if (req->namelen > get_req_data_size())
2155 set_error( STATUS_INVALID_PARAMETER );
2156 return;
2158 name.str = get_req_data();
2159 name.len = (req->namelen / sizeof(WCHAR)) * sizeof(WCHAR);
2161 if ((key = get_hkey_obj( req->hkey, KEY_SET_VALUE )))
2163 data_size_t datalen = get_req_data_size() - req->namelen;
2164 const char *data = (const char *)get_req_data() + req->namelen;
2166 set_value( key, &name, req->type, data, datalen );
2167 release_object( key );
2171 /* retrieve the value of a registry key */
2172 DECL_HANDLER(get_key_value)
2174 struct key *key;
2175 struct unicode_str name = get_req_unicode_str();
2177 reply->total = 0;
2178 if ((key = get_hkey_obj( req->hkey, KEY_QUERY_VALUE )))
2180 get_value( key, &name, &reply->type, &reply->total );
2181 release_object( key );
2185 /* enumerate the value of a registry key */
2186 DECL_HANDLER(enum_key_value)
2188 struct key *key;
2190 if ((key = get_hkey_obj( req->hkey, KEY_QUERY_VALUE )))
2192 enum_value( key, req->index, req->info_class, reply );
2193 release_object( key );
2197 /* delete a value of a registry key */
2198 DECL_HANDLER(delete_key_value)
2200 struct key *key;
2201 struct unicode_str name = get_req_unicode_str();
2203 if ((key = get_hkey_obj( req->hkey, KEY_SET_VALUE )))
2205 delete_value( key, &name );
2206 release_object( key );
2210 /* load a registry branch from a file */
2211 DECL_HANDLER(load_registry)
2213 struct key *key, *parent;
2214 struct unicode_str name;
2215 const struct security_descriptor *sd;
2216 const struct object_attributes *objattr = get_req_object_attributes( &sd, &name, NULL );
2218 if (!objattr) return;
2220 if (!thread_single_check_privilege( current, &SeRestorePrivilege ))
2222 set_error( STATUS_PRIVILEGE_NOT_HELD );
2223 return;
2226 if (!objattr->rootdir && name.len >= sizeof(root_name) &&
2227 !memicmp_strW( name.str, root_name, sizeof(root_name) ))
2229 name.str += ARRAY_SIZE( root_name );
2230 name.len -= sizeof(root_name);
2233 if ((parent = get_parent_hkey_obj( objattr->rootdir )))
2235 int dummy;
2236 if ((key = create_key( parent, &name, NULL, 0, KEY_WOW64_64KEY, 0, sd, &dummy )))
2238 load_registry( key, req->file );
2239 release_object( key );
2241 release_object( parent );
2245 DECL_HANDLER(unload_registry)
2247 struct key *key, *parent;
2248 struct unicode_str name;
2249 unsigned int access = 0;
2251 if (!thread_single_check_privilege( current, &SeRestorePrivilege ))
2253 set_error( STATUS_PRIVILEGE_NOT_HELD );
2254 return;
2257 if (!is_wow64_thread( current )) access = (access & ~KEY_WOW64_32KEY) | KEY_WOW64_64KEY;
2259 if ((parent = get_parent_hkey_obj( req->parent )))
2261 get_req_path( &name, !req->parent );
2262 if ((key = open_key( parent, &name, access, req->attributes )))
2264 if (key->obj.handle_count)
2265 set_error( STATUS_CANNOT_DELETE );
2266 else
2267 delete_key( key, 1 ); /* FIXME */
2268 release_object( key );
2270 release_object( parent );
2274 /* save a registry branch to a file */
2275 DECL_HANDLER(save_registry)
2277 struct key *key;
2279 if (!thread_single_check_privilege( current, &SeBackupPrivilege ))
2281 set_error( STATUS_PRIVILEGE_NOT_HELD );
2282 return;
2285 if ((key = get_hkey_obj( req->hkey, 0 )))
2287 save_registry( key, req->file );
2288 release_object( key );
2292 /* add a registry key change notification */
2293 DECL_HANDLER(set_registry_notification)
2295 struct key *key;
2296 struct event *event;
2297 struct notify *notify;
2299 key = get_hkey_obj( req->hkey, KEY_NOTIFY );
2300 if (key)
2302 event = get_event_obj( current->process, req->event, SYNCHRONIZE );
2303 if (event)
2305 notify = find_notify( key, current->process, req->hkey );
2306 if (!notify)
2308 notify = mem_alloc( sizeof(*notify) );
2309 if (notify)
2311 notify->events = NULL;
2312 notify->event_count = 0;
2313 notify->subtree = req->subtree;
2314 notify->filter = req->filter;
2315 notify->hkey = req->hkey;
2316 notify->process = current->process;
2317 list_add_head( &key->notify_list, &notify->entry );
2320 if (notify)
2322 struct event **new_array;
2324 if ((new_array = realloc( notify->events, (notify->event_count + 1) * sizeof(*notify->events) )))
2326 notify->events = new_array;
2327 notify->events[notify->event_count++] = (struct event *)grab_object( event );
2328 reset_event( event );
2329 set_error( STATUS_PENDING );
2331 else set_error( STATUS_NO_MEMORY );
2333 release_object( event );
2335 release_object( key );