windowscodecs: Return correct error codes from BmpFrameEncode_GetMetadataQueryWriter().
[wine/zf.git] / server / registry.c
blob64aec1d83c9c7ac185cea2667e0204c08bd5779d
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 /* a registry key */
64 struct key
66 struct object obj; /* object header */
67 WCHAR *name; /* key name */
68 WCHAR *class; /* key class */
69 unsigned short namelen; /* length of key name */
70 unsigned short classlen; /* length of class name */
71 struct key *parent; /* parent key */
72 int last_subkey; /* last in use subkey */
73 int nb_subkeys; /* count of allocated subkeys */
74 struct key **subkeys; /* subkeys array */
75 int last_value; /* last in use value */
76 int nb_values; /* count of allocated values in array */
77 struct key_value *values; /* values array */
78 unsigned int flags; /* flags */
79 timeout_t modif; /* last modification time */
80 struct list notify_list; /* list of notifications */
83 /* key flags */
84 #define KEY_VOLATILE 0x0001 /* key is volatile (not saved to disk) */
85 #define KEY_DELETED 0x0002 /* key has been deleted */
86 #define KEY_DIRTY 0x0004 /* key has been modified */
87 #define KEY_SYMLINK 0x0008 /* key is a symbolic link */
88 #define KEY_WOW64 0x0010 /* key contains a Wow6432Node subkey */
89 #define KEY_WOWSHARE 0x0020 /* key is a Wow64 shared key (used for Software\Classes) */
91 /* a key value */
92 struct key_value
94 WCHAR *name; /* value name */
95 unsigned short namelen; /* length of value name */
96 unsigned int type; /* value type */
97 data_size_t len; /* value data length in bytes */
98 void *data; /* pointer to value data */
101 #define MIN_SUBKEYS 8 /* min. number of allocated subkeys per key */
102 #define MIN_VALUES 8 /* min. number of allocated values per key */
104 #define MAX_NAME_LEN 256 /* max. length of a key name */
105 #define MAX_VALUE_LEN 16383 /* max. length of a value name */
107 /* the root of the registry tree */
108 static struct key *root_key;
110 static const timeout_t ticks_1601_to_1970 = (timeout_t)86400 * (369 * 365 + 89) * TICKS_PER_SEC;
111 static const timeout_t save_period = 30 * -TICKS_PER_SEC; /* delay between periodic saves */
112 static struct timeout_user *save_timeout_user; /* saving timer */
113 static enum prefix_type { PREFIX_UNKNOWN, PREFIX_32BIT, PREFIX_64BIT } prefix_type;
115 static const WCHAR root_name[] = { '\\','R','e','g','i','s','t','r','y','\\' };
116 static const WCHAR wow6432node[] = {'W','o','w','6','4','3','2','N','o','d','e'};
117 static const WCHAR symlink_value[] = {'S','y','m','b','o','l','i','c','L','i','n','k','V','a','l','u','e'};
118 static const struct unicode_str symlink_str = { symlink_value, sizeof(symlink_value) };
120 static void set_periodic_save_timer(void);
121 static struct key_value *find_value( const struct key *key, const struct unicode_str *name, int *index );
123 /* information about where to save a registry branch */
124 struct save_branch_info
126 struct key *key;
127 const char *path;
130 #define MAX_SAVE_BRANCH_INFO 3
131 static int save_branch_count;
132 static struct save_branch_info save_branch_info[MAX_SAVE_BRANCH_INFO];
135 /* information about a file being loaded */
136 struct file_load_info
138 const char *filename; /* input file name */
139 FILE *file; /* input file */
140 char *buffer; /* line buffer */
141 int len; /* buffer length */
142 int line; /* current input line */
143 WCHAR *tmp; /* temp buffer to use while parsing input */
144 size_t tmplen; /* length of temp buffer */
148 static void key_dump( struct object *obj, int verbose );
149 static struct object_type *key_get_type( struct object *obj );
150 static unsigned int key_map_access( struct object *obj, unsigned int access );
151 static struct security_descriptor *key_get_sd( struct object *obj );
152 static WCHAR *key_get_full_name( struct object *obj, data_size_t *len );
153 static int key_close_handle( struct object *obj, struct process *process, obj_handle_t handle );
154 static void key_destroy( struct object *obj );
156 static const struct object_ops key_ops =
158 sizeof(struct key), /* size */
159 key_dump, /* dump */
160 key_get_type, /* get_type */
161 no_add_queue, /* add_queue */
162 NULL, /* remove_queue */
163 NULL, /* signaled */
164 NULL, /* satisfied */
165 no_signal, /* signal */
166 no_get_fd, /* get_fd */
167 key_map_access, /* map_access */
168 key_get_sd, /* get_sd */
169 default_set_sd, /* set_sd */
170 key_get_full_name, /* get_full_name */
171 no_lookup_name, /* lookup_name */
172 no_link_name, /* link_name */
173 NULL, /* unlink_name */
174 no_open_file, /* open_file */
175 no_kernel_obj_list, /* get_kernel_obj_list */
176 key_close_handle, /* close_handle */
177 key_destroy /* destroy */
181 static inline int is_wow6432node( const WCHAR *name, unsigned int len )
183 return (len == sizeof(wow6432node) && !memicmp_strW( name, wow6432node, sizeof( wow6432node )));
187 * The registry text file format v2 used by this code is similar to the one
188 * used by REGEDIT import/export functionality, with the following differences:
189 * - strings and key names can contain \x escapes for Unicode
190 * - key names use escapes too in order to support Unicode
191 * - the modification time optionally follows the key name
192 * - REG_EXPAND_SZ and REG_MULTI_SZ are saved as strings instead of hex
195 /* dump the full path of a key */
196 static void dump_path( const struct key *key, const struct key *base, FILE *f )
198 if (key->parent && key->parent != base)
200 dump_path( key->parent, base, f );
201 fprintf( f, "\\\\" );
203 dump_strW( key->name, key->namelen, f, "[]" );
206 /* dump a value to a text file */
207 static void dump_value( const struct key_value *value, FILE *f )
209 unsigned int i, dw;
210 int count;
212 if (value->namelen)
214 fputc( '\"', f );
215 count = 1 + dump_strW( value->name, value->namelen, f, "\"\"" );
216 count += fprintf( f, "\"=" );
218 else count = fprintf( f, "@=" );
220 switch(value->type)
222 case REG_SZ:
223 case REG_EXPAND_SZ:
224 case REG_MULTI_SZ:
225 /* only output properly terminated strings in string format */
226 if (value->len < sizeof(WCHAR)) break;
227 if (value->len % sizeof(WCHAR)) break;
228 if (((WCHAR *)value->data)[value->len / sizeof(WCHAR) - 1]) break;
229 if (value->type != REG_SZ) fprintf( f, "str(%x):", value->type );
230 fputc( '\"', f );
231 dump_strW( (WCHAR *)value->data, value->len, f, "\"\"" );
232 fprintf( f, "\"\n" );
233 return;
235 case REG_DWORD:
236 if (value->len != sizeof(dw)) break;
237 memcpy( &dw, value->data, sizeof(dw) );
238 fprintf( f, "dword:%08x\n", dw );
239 return;
242 if (value->type == REG_BINARY) count += fprintf( f, "hex:" );
243 else count += fprintf( f, "hex(%x):", value->type );
244 for (i = 0; i < value->len; i++)
246 count += fprintf( f, "%02x", *((unsigned char *)value->data + i) );
247 if (i < value->len-1)
249 fputc( ',', f );
250 if (++count > 76)
252 fprintf( f, "\\\n " );
253 count = 2;
257 fputc( '\n', f );
260 /* save a registry and all its subkeys to a text file */
261 static void save_subkeys( const struct key *key, const struct key *base, FILE *f )
263 int i;
265 if (key->flags & KEY_VOLATILE) return;
266 /* save key if it has either some values or no subkeys, or needs special options */
267 /* keys with no values but subkeys are saved implicitly by saving the subkeys */
268 if ((key->last_value >= 0) || (key->last_subkey == -1) || key->class || (key->flags & KEY_SYMLINK))
270 fprintf( f, "\n[" );
271 if (key != base) dump_path( key, base, f );
272 fprintf( f, "] %u\n", (unsigned int)((key->modif - ticks_1601_to_1970) / TICKS_PER_SEC) );
273 fprintf( f, "#time=%x%08x\n", (unsigned int)(key->modif >> 32), (unsigned int)key->modif );
274 if (key->class)
276 fprintf( f, "#class=\"" );
277 dump_strW( key->class, key->classlen, f, "\"\"" );
278 fprintf( f, "\"\n" );
280 if (key->flags & KEY_SYMLINK) fputs( "#link\n", f );
281 for (i = 0; i <= key->last_value; i++) dump_value( &key->values[i], f );
283 for (i = 0; i <= key->last_subkey; i++) save_subkeys( key->subkeys[i], base, f );
286 static void dump_operation( const struct key *key, const struct key_value *value, const char *op )
288 fprintf( stderr, "%s key ", op );
289 if (key) dump_path( key, NULL, stderr );
290 else fprintf( stderr, "ERROR" );
291 if (value)
293 fprintf( stderr, " value ");
294 dump_value( value, stderr );
296 else fprintf( stderr, "\n" );
299 static void key_dump( struct object *obj, int verbose )
301 struct key *key = (struct key *)obj;
302 assert( obj->ops == &key_ops );
303 fprintf( stderr, "Key flags=%x ", key->flags );
304 dump_path( key, NULL, stderr );
305 fprintf( stderr, "\n" );
308 static struct object_type *key_get_type( struct object *obj )
310 static const WCHAR name[] = {'K','e','y'};
311 static const struct unicode_str str = { name, sizeof(name) };
312 return get_object_type( &str );
315 /* notify waiter and maybe delete the notification */
316 static void do_notification( struct key *key, struct notify *notify, int del )
318 unsigned int i;
320 for (i = 0; i < notify->event_count; ++i)
322 set_event( notify->events[i] );
323 release_object( notify->events[i] );
325 free( notify->events );
326 notify->events = NULL;
327 notify->event_count = 0;
329 if (del)
331 list_remove( &notify->entry );
332 free( notify );
336 static inline struct notify *find_notify( struct key *key, struct process *process, obj_handle_t hkey )
338 struct notify *notify;
340 LIST_FOR_EACH_ENTRY( notify, &key->notify_list, struct notify, entry )
342 if (notify->process == process && notify->hkey == hkey) return notify;
344 return NULL;
347 static unsigned int key_map_access( struct object *obj, unsigned int access )
349 if (access & GENERIC_READ) access |= KEY_READ;
350 if (access & GENERIC_WRITE) access |= KEY_WRITE;
351 if (access & GENERIC_EXECUTE) access |= KEY_EXECUTE;
352 if (access & GENERIC_ALL) access |= KEY_ALL_ACCESS;
353 /* filter the WOW64 masks, as they aren't real access bits */
354 return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL |
355 KEY_WOW64_64KEY | KEY_WOW64_32KEY);
358 static struct security_descriptor *key_get_sd( struct object *obj )
360 static struct security_descriptor *key_default_sd;
362 if (obj->sd) return obj->sd;
364 if (!key_default_sd)
366 size_t users_sid_len = security_sid_len( security_builtin_users_sid );
367 size_t admins_sid_len = security_sid_len( security_builtin_admins_sid );
368 size_t dacl_len = sizeof(ACL) + 2 * offsetof( ACCESS_ALLOWED_ACE, SidStart )
369 + users_sid_len + admins_sid_len;
370 ACCESS_ALLOWED_ACE *aaa;
371 ACL *dacl;
373 key_default_sd = mem_alloc( sizeof(*key_default_sd) + 2 * admins_sid_len + dacl_len );
374 key_default_sd->control = SE_DACL_PRESENT;
375 key_default_sd->owner_len = admins_sid_len;
376 key_default_sd->group_len = admins_sid_len;
377 key_default_sd->sacl_len = 0;
378 key_default_sd->dacl_len = dacl_len;
379 memcpy( key_default_sd + 1, security_builtin_admins_sid, admins_sid_len );
380 memcpy( (char *)(key_default_sd + 1) + admins_sid_len, security_builtin_admins_sid, admins_sid_len );
382 dacl = (ACL *)((char *)(key_default_sd + 1) + 2 * admins_sid_len);
383 dacl->AclRevision = ACL_REVISION;
384 dacl->Sbz1 = 0;
385 dacl->AclSize = dacl_len;
386 dacl->AceCount = 2;
387 dacl->Sbz2 = 0;
388 aaa = (ACCESS_ALLOWED_ACE *)(dacl + 1);
389 aaa->Header.AceType = ACCESS_ALLOWED_ACE_TYPE;
390 aaa->Header.AceFlags = INHERIT_ONLY_ACE | CONTAINER_INHERIT_ACE;
391 aaa->Header.AceSize = offsetof( ACCESS_ALLOWED_ACE, SidStart ) + users_sid_len;
392 aaa->Mask = GENERIC_READ;
393 memcpy( &aaa->SidStart, security_builtin_users_sid, users_sid_len );
394 aaa = (ACCESS_ALLOWED_ACE *)((char *)aaa + aaa->Header.AceSize);
395 aaa->Header.AceType = ACCESS_ALLOWED_ACE_TYPE;
396 aaa->Header.AceFlags = 0;
397 aaa->Header.AceSize = offsetof( ACCESS_ALLOWED_ACE, SidStart ) + admins_sid_len;
398 aaa->Mask = KEY_ALL_ACCESS;
399 memcpy( &aaa->SidStart, security_builtin_admins_sid, admins_sid_len );
401 return key_default_sd;
404 static WCHAR *key_get_full_name( struct object *obj, data_size_t *ret_len )
406 static const WCHAR backslash = '\\';
407 struct key *key = (struct key *) obj;
408 data_size_t len = sizeof(root_name) - sizeof(WCHAR);
409 char *ret;
411 for (key = (struct key *)obj; key != root_key; key = key->parent) len += key->namelen + sizeof(WCHAR);
412 if (!(ret = malloc( len ))) return NULL;
414 *ret_len = len;
415 key = (struct key *)obj;
416 for (key = (struct key *)obj; key != root_key; key = key->parent)
418 memcpy( ret + len - key->namelen, key->name, key->namelen );
419 len -= key->namelen + sizeof(WCHAR);
420 memcpy( ret + len, &backslash, sizeof(WCHAR) );
422 memcpy( ret, root_name, sizeof(root_name) - sizeof(WCHAR) );
423 return (WCHAR *)ret;
426 /* close the notification associated with a handle */
427 static int key_close_handle( struct object *obj, struct process *process, obj_handle_t handle )
429 struct key * key = (struct key *) obj;
430 struct notify *notify = find_notify( key, process, handle );
431 if (notify) do_notification( key, notify, 1 );
432 return 1; /* ok to close */
435 static void key_destroy( struct object *obj )
437 int i;
438 struct list *ptr;
439 struct key *key = (struct key *)obj;
440 assert( obj->ops == &key_ops );
442 free( key->name );
443 free( key->class );
444 for (i = 0; i <= key->last_value; i++)
446 free( key->values[i].name );
447 free( key->values[i].data );
449 free( key->values );
450 for (i = 0; i <= key->last_subkey; i++)
452 key->subkeys[i]->parent = NULL;
453 release_object( key->subkeys[i] );
455 free( key->subkeys );
456 /* unconditionally notify everything waiting on this key */
457 while ((ptr = list_head( &key->notify_list )))
459 struct notify *notify = LIST_ENTRY( ptr, struct notify, entry );
460 do_notification( key, notify, 1 );
464 /* get the request vararg as registry path */
465 static inline void get_req_path( struct unicode_str *str, int skip_root )
467 str->str = get_req_data();
468 str->len = (get_req_data_size() / sizeof(WCHAR)) * sizeof(WCHAR);
470 if (skip_root && str->len >= sizeof(root_name) && !memicmp_strW( str->str, root_name, sizeof(root_name) ))
472 str->str += ARRAY_SIZE( root_name );
473 str->len -= sizeof(root_name);
477 /* return the next token in a given path */
478 /* token->str must point inside the path, or be NULL for the first call */
479 static struct unicode_str *get_path_token( const struct unicode_str *path, struct unicode_str *token )
481 data_size_t i = 0, len = path->len / sizeof(WCHAR);
483 if (!token->str) /* first time */
485 /* path cannot start with a backslash */
486 if (len && path->str[0] == '\\')
488 set_error( STATUS_OBJECT_PATH_INVALID );
489 return NULL;
492 else
494 i = token->str - path->str;
495 i += token->len / sizeof(WCHAR);
496 while (i < len && path->str[i] == '\\') i++;
498 token->str = path->str + i;
499 while (i < len && path->str[i] != '\\') i++;
500 token->len = (path->str + i - token->str) * sizeof(WCHAR);
501 return token;
504 /* allocate a key object */
505 static struct key *alloc_key( const struct unicode_str *name, timeout_t modif )
507 struct key *key;
508 if ((key = alloc_object( &key_ops )))
510 key->name = NULL;
511 key->class = NULL;
512 key->namelen = name->len;
513 key->classlen = 0;
514 key->flags = 0;
515 key->last_subkey = -1;
516 key->nb_subkeys = 0;
517 key->subkeys = NULL;
518 key->nb_values = 0;
519 key->last_value = -1;
520 key->values = NULL;
521 key->modif = modif;
522 key->parent = NULL;
523 list_init( &key->notify_list );
524 if (name->len && !(key->name = memdup( name->str, name->len )))
526 release_object( key );
527 key = NULL;
530 return key;
533 /* mark a key and all its parents as dirty (modified) */
534 static void make_dirty( struct key *key )
536 while (key)
538 if (key->flags & (KEY_DIRTY|KEY_VOLATILE)) return; /* nothing to do */
539 key->flags |= KEY_DIRTY;
540 key = key->parent;
544 /* mark a key and all its subkeys as clean (not modified) */
545 static void make_clean( struct key *key )
547 int i;
549 if (key->flags & KEY_VOLATILE) return;
550 if (!(key->flags & KEY_DIRTY)) return;
551 key->flags &= ~KEY_DIRTY;
552 for (i = 0; i <= key->last_subkey; i++) make_clean( key->subkeys[i] );
555 /* go through all the notifications and send them if necessary */
556 static void check_notify( struct key *key, unsigned int change, int not_subtree )
558 struct list *ptr, *next;
560 LIST_FOR_EACH_SAFE( ptr, next, &key->notify_list )
562 struct notify *n = LIST_ENTRY( ptr, struct notify, entry );
563 if ( ( not_subtree || n->subtree ) && ( change & n->filter ) )
564 do_notification( key, n, 0 );
568 /* update key modification time */
569 static void touch_key( struct key *key, unsigned int change )
571 struct key *k;
573 key->modif = current_time;
574 make_dirty( key );
576 /* do notifications */
577 check_notify( key, change, 1 );
578 for ( k = key->parent; k; k = k->parent )
579 check_notify( k, change, 0 );
582 /* try to grow the array of subkeys; return 1 if OK, 0 on error */
583 static int grow_subkeys( struct key *key )
585 struct key **new_subkeys;
586 int nb_subkeys;
588 if (key->nb_subkeys)
590 nb_subkeys = key->nb_subkeys + (key->nb_subkeys / 2); /* grow by 50% */
591 if (!(new_subkeys = realloc( key->subkeys, nb_subkeys * sizeof(*new_subkeys) )))
593 set_error( STATUS_NO_MEMORY );
594 return 0;
597 else
599 nb_subkeys = MIN_SUBKEYS;
600 if (!(new_subkeys = mem_alloc( nb_subkeys * sizeof(*new_subkeys) ))) return 0;
602 key->subkeys = new_subkeys;
603 key->nb_subkeys = nb_subkeys;
604 return 1;
607 /* allocate a subkey for a given key, and return its index */
608 static struct key *alloc_subkey( struct key *parent, const struct unicode_str *name,
609 int index, timeout_t modif )
611 struct key *key;
612 int i;
614 if (name->len > MAX_NAME_LEN * sizeof(WCHAR))
616 set_error( STATUS_INVALID_PARAMETER );
617 return NULL;
619 if (parent->last_subkey + 1 == parent->nb_subkeys)
621 /* need to grow the array */
622 if (!grow_subkeys( parent )) return NULL;
624 if ((key = alloc_key( name, modif )) != NULL)
626 key->parent = parent;
627 for (i = ++parent->last_subkey; i > index; i--)
628 parent->subkeys[i] = parent->subkeys[i-1];
629 parent->subkeys[index] = key;
630 if (is_wow6432node( key->name, key->namelen ) && !is_wow6432node( parent->name, parent->namelen ))
631 parent->flags |= KEY_WOW64;
633 return key;
636 /* free a subkey of a given key */
637 static void free_subkey( struct key *parent, int index )
639 struct key *key;
640 int i, nb_subkeys;
642 assert( index >= 0 );
643 assert( index <= parent->last_subkey );
645 key = parent->subkeys[index];
646 for (i = index; i < parent->last_subkey; i++) parent->subkeys[i] = parent->subkeys[i + 1];
647 parent->last_subkey--;
648 key->flags |= KEY_DELETED;
649 key->parent = NULL;
650 if (is_wow6432node( key->name, key->namelen )) parent->flags &= ~KEY_WOW64;
651 release_object( key );
653 /* try to shrink the array */
654 nb_subkeys = parent->nb_subkeys;
655 if (nb_subkeys > MIN_SUBKEYS && parent->last_subkey < nb_subkeys / 2)
657 struct key **new_subkeys;
658 nb_subkeys -= nb_subkeys / 3; /* shrink by 33% */
659 if (nb_subkeys < MIN_SUBKEYS) nb_subkeys = MIN_SUBKEYS;
660 if (!(new_subkeys = realloc( parent->subkeys, nb_subkeys * sizeof(*new_subkeys) ))) return;
661 parent->subkeys = new_subkeys;
662 parent->nb_subkeys = nb_subkeys;
666 /* find the named child of a given key and return its index */
667 static struct key *find_subkey( const struct key *key, const struct unicode_str *name, int *index )
669 int i, min, max, res;
670 data_size_t len;
672 min = 0;
673 max = key->last_subkey;
674 while (min <= max)
676 i = (min + max) / 2;
677 len = min( key->subkeys[i]->namelen, name->len );
678 res = memicmp_strW( key->subkeys[i]->name, name->str, len );
679 if (!res) res = key->subkeys[i]->namelen - name->len;
680 if (!res)
682 *index = i;
683 return key->subkeys[i];
685 if (res > 0) max = i - 1;
686 else min = i + 1;
688 *index = min; /* this is where we should insert it */
689 return NULL;
692 /* return the wow64 variant of the key, or the key itself if none */
693 static struct key *find_wow64_subkey( struct key *key, const struct unicode_str *name )
695 static const struct unicode_str wow6432node_str = { wow6432node, sizeof(wow6432node) };
696 int index;
698 if (!(key->flags & KEY_WOW64)) return key;
699 if (!is_wow6432node( name->str, name->len ))
701 key = find_subkey( key, &wow6432node_str, &index );
702 assert( key ); /* if KEY_WOW64 is set we must find it */
704 return key;
708 /* follow a symlink and return the resolved key */
709 static struct key *follow_symlink( struct key *key, int iteration )
711 struct unicode_str path, token;
712 struct key_value *value;
713 int index;
715 if (iteration > 16) return NULL;
716 if (!(key->flags & KEY_SYMLINK)) return key;
717 if (!(value = find_value( key, &symlink_str, &index ))) return NULL;
719 path.str = value->data;
720 path.len = (value->len / sizeof(WCHAR)) * sizeof(WCHAR);
721 if (path.len <= sizeof(root_name)) return NULL;
722 if (memicmp_strW( path.str, root_name, sizeof(root_name) )) return NULL;
723 path.str += ARRAY_SIZE( root_name );
724 path.len -= sizeof(root_name);
726 key = root_key;
727 token.str = NULL;
728 if (!get_path_token( &path, &token )) return NULL;
729 while (token.len)
731 if (!(key = find_subkey( key, &token, &index ))) break;
732 if (!(key = follow_symlink( key, iteration + 1 ))) break;
733 get_path_token( &path, &token );
735 return key;
738 /* open a key until we find an element that doesn't exist */
739 /* helper for open_key and create_key */
740 static struct key *open_key_prefix( struct key *key, const struct unicode_str *name,
741 unsigned int access, struct unicode_str *token, int *index )
743 token->str = NULL;
744 if (!get_path_token( name, token )) return NULL;
745 if (access & KEY_WOW64_32KEY) key = find_wow64_subkey( key, token );
746 while (token->len)
748 struct key *subkey;
749 if (!(subkey = find_subkey( key, token, index )))
751 if ((key->flags & KEY_WOWSHARE) && !(access & KEY_WOW64_64KEY))
753 /* try in the 64-bit parent */
754 key = key->parent;
755 subkey = find_subkey( key, token, index );
758 if (!subkey) break;
759 key = subkey;
760 get_path_token( name, token );
761 if (!token->len) break;
762 if (!(access & KEY_WOW64_64KEY)) key = find_wow64_subkey( key, token );
763 if (!(key = follow_symlink( key, 0 )))
765 set_error( STATUS_OBJECT_NAME_NOT_FOUND );
766 return NULL;
769 return key;
772 /* open a subkey */
773 static struct key *open_key( struct key *key, const struct unicode_str *name, unsigned int access,
774 unsigned int attributes )
776 int index;
777 struct unicode_str token;
779 if (!(key = open_key_prefix( key, name, access, &token, &index ))) return NULL;
781 if (token.len)
783 set_error( STATUS_OBJECT_NAME_NOT_FOUND );
784 return NULL;
786 if (!(access & KEY_WOW64_64KEY)) key = find_wow64_subkey( key, &token );
787 if (!(attributes & OBJ_OPENLINK) && !(key = follow_symlink( key, 0 )))
789 set_error( STATUS_OBJECT_NAME_NOT_FOUND );
790 return NULL;
792 if (debug_level > 1) dump_operation( key, NULL, "Open" );
793 grab_object( key );
794 return key;
797 /* create a subkey */
798 static struct key *create_key( struct key *key, const struct unicode_str *name,
799 const struct unicode_str *class, unsigned int options,
800 unsigned int access, unsigned int attributes,
801 const struct security_descriptor *sd, int *created )
803 int index;
804 struct unicode_str token, next;
806 *created = 0;
807 if (!(key = open_key_prefix( key, name, access, &token, &index ))) return NULL;
809 if (!token.len) /* the key already exists */
811 if (!(access & KEY_WOW64_64KEY)) key = find_wow64_subkey( key, &token );
812 if (options & REG_OPTION_CREATE_LINK)
814 set_error( STATUS_OBJECT_NAME_COLLISION );
815 return NULL;
817 if (!(attributes & OBJ_OPENLINK) && !(key = follow_symlink( key, 0 )))
819 set_error( STATUS_OBJECT_NAME_NOT_FOUND );
820 return NULL;
822 if (debug_level > 1) dump_operation( key, NULL, "Open" );
823 grab_object( key );
824 return key;
827 /* token must be the last path component at this point */
828 next = token;
829 get_path_token( name, &next );
830 if (next.len)
832 set_error( STATUS_OBJECT_NAME_NOT_FOUND );
833 return NULL;
836 if ((key->flags & KEY_VOLATILE) && !(options & REG_OPTION_VOLATILE))
838 set_error( STATUS_CHILD_MUST_BE_VOLATILE );
839 return NULL;
841 *created = 1;
842 make_dirty( key );
843 if (!(key = alloc_subkey( key, &token, index, current_time ))) return NULL;
845 if (options & REG_OPTION_CREATE_LINK) key->flags |= KEY_SYMLINK;
846 if (options & REG_OPTION_VOLATILE) key->flags |= KEY_VOLATILE;
847 else key->flags |= KEY_DIRTY;
849 if (sd) default_set_sd( &key->obj, sd, OWNER_SECURITY_INFORMATION | GROUP_SECURITY_INFORMATION |
850 DACL_SECURITY_INFORMATION | SACL_SECURITY_INFORMATION );
852 if (debug_level > 1) dump_operation( key, NULL, "Create" );
853 if (class && class->len)
855 key->classlen = class->len;
856 free(key->class);
857 if (!(key->class = memdup( class->str, key->classlen ))) key->classlen = 0;
859 touch_key( key->parent, REG_NOTIFY_CHANGE_NAME );
860 grab_object( key );
861 return key;
864 /* recursively create a subkey (for internal use only) */
865 static struct key *create_key_recursive( struct key *key, const struct unicode_str *name, timeout_t modif )
867 struct key *base;
868 int index;
869 struct unicode_str token;
871 token.str = NULL;
872 if (!get_path_token( name, &token )) return NULL;
873 while (token.len)
875 struct key *subkey;
876 if (!(subkey = find_subkey( key, &token, &index ))) break;
877 key = subkey;
878 if (!(key = follow_symlink( key, 0 )))
880 set_error( STATUS_OBJECT_NAME_NOT_FOUND );
881 return NULL;
883 get_path_token( name, &token );
886 if (token.len)
888 if (!(key = alloc_subkey( key, &token, index, modif ))) return NULL;
889 base = key;
890 for (;;)
892 get_path_token( name, &token );
893 if (!token.len) break;
894 /* we know the index is always 0 in a new key */
895 if (!(key = alloc_subkey( key, &token, 0, modif )))
897 free_subkey( base, index );
898 return NULL;
903 grab_object( key );
904 return key;
907 /* query information about a key or a subkey */
908 static void enum_key( struct key *key, int index, int info_class, struct enum_key_reply *reply )
910 int i;
911 data_size_t len, namelen, classlen;
912 data_size_t max_subkey = 0, max_class = 0;
913 data_size_t max_value = 0, max_data = 0;
914 WCHAR *fullname = NULL;
915 char *data;
917 if (index != -1) /* -1 means use the specified key directly */
919 if ((index < 0) || (index > key->last_subkey))
921 set_error( STATUS_NO_MORE_ENTRIES );
922 return;
924 key = key->subkeys[index];
927 namelen = key->namelen;
928 classlen = key->classlen;
930 switch(info_class)
932 case KeyNameInformation:
933 if (!(fullname = key->obj.ops->get_full_name( &key->obj, &namelen ))) return;
934 /* fall through */
935 case KeyBasicInformation:
936 classlen = 0; /* only return the name */
937 /* fall through */
938 case KeyNodeInformation:
939 reply->max_subkey = 0;
940 reply->max_class = 0;
941 reply->max_value = 0;
942 reply->max_data = 0;
943 break;
944 case KeyFullInformation:
945 case KeyCachedInformation:
946 for (i = 0; i <= key->last_subkey; i++)
948 if (key->subkeys[i]->namelen > max_subkey) max_subkey = key->subkeys[i]->namelen;
949 if (key->subkeys[i]->classlen > max_class) max_class = key->subkeys[i]->classlen;
951 for (i = 0; i <= key->last_value; i++)
953 if (key->values[i].namelen > max_value) max_value = key->values[i].namelen;
954 if (key->values[i].len > max_data) max_data = key->values[i].len;
956 reply->max_subkey = max_subkey;
957 reply->max_class = max_class;
958 reply->max_value = max_value;
959 reply->max_data = max_data;
960 reply->namelen = namelen;
961 if (info_class == KeyCachedInformation)
962 classlen = 0; /* don't return any data, only its size */
963 namelen = 0; /* don't return name */
964 break;
965 default:
966 set_error( STATUS_INVALID_PARAMETER );
967 return;
969 reply->subkeys = key->last_subkey + 1;
970 reply->values = key->last_value + 1;
971 reply->modif = key->modif;
972 reply->total = namelen + classlen;
974 len = min( reply->total, get_reply_max_size() );
975 if (len && (data = set_reply_data_size( len )))
977 if (len > namelen)
979 reply->namelen = namelen;
980 memcpy( data, key->name, namelen );
981 memcpy( data + namelen, key->class, len - namelen );
983 else if (info_class == KeyNameInformation)
985 reply->namelen = namelen;
986 memcpy( data, fullname, len );
988 else
990 reply->namelen = len;
991 memcpy( data, key->name, len );
994 free( fullname );
995 if (debug_level > 1) dump_operation( key, NULL, "Enum" );
998 /* delete a key and its values */
999 static int delete_key( struct key *key, int recurse )
1001 int index;
1002 struct key *parent = key->parent;
1004 /* must find parent and index */
1005 if (key == root_key)
1007 set_error( STATUS_ACCESS_DENIED );
1008 return -1;
1010 assert( parent );
1012 while (recurse && (key->last_subkey>=0))
1013 if (0 > delete_key(key->subkeys[key->last_subkey], 1))
1014 return -1;
1016 for (index = 0; index <= parent->last_subkey; index++)
1017 if (parent->subkeys[index] == key) break;
1018 assert( index <= parent->last_subkey );
1020 /* we can only delete a key that has no subkeys */
1021 if (key->last_subkey >= 0)
1023 set_error( STATUS_ACCESS_DENIED );
1024 return -1;
1027 if (debug_level > 1) dump_operation( key, NULL, "Delete" );
1028 free_subkey( parent, index );
1029 touch_key( parent, REG_NOTIFY_CHANGE_NAME );
1030 return 0;
1033 /* try to grow the array of values; return 1 if OK, 0 on error */
1034 static int grow_values( struct key *key )
1036 struct key_value *new_val;
1037 int nb_values;
1039 if (key->nb_values)
1041 nb_values = key->nb_values + (key->nb_values / 2); /* grow by 50% */
1042 if (!(new_val = realloc( key->values, nb_values * sizeof(*new_val) )))
1044 set_error( STATUS_NO_MEMORY );
1045 return 0;
1048 else
1050 nb_values = MIN_VALUES;
1051 if (!(new_val = mem_alloc( nb_values * sizeof(*new_val) ))) return 0;
1053 key->values = new_val;
1054 key->nb_values = nb_values;
1055 return 1;
1058 /* find the named value of a given key and return its index in the array */
1059 static struct key_value *find_value( const struct key *key, const struct unicode_str *name, int *index )
1061 int i, min, max, res;
1062 data_size_t len;
1064 min = 0;
1065 max = key->last_value;
1066 while (min <= max)
1068 i = (min + max) / 2;
1069 len = min( key->values[i].namelen, name->len );
1070 res = memicmp_strW( key->values[i].name, name->str, len );
1071 if (!res) res = key->values[i].namelen - name->len;
1072 if (!res)
1074 *index = i;
1075 return &key->values[i];
1077 if (res > 0) max = i - 1;
1078 else min = i + 1;
1080 *index = min; /* this is where we should insert it */
1081 return NULL;
1084 /* insert a new value; the index must have been returned by find_value */
1085 static struct key_value *insert_value( struct key *key, const struct unicode_str *name, int index )
1087 struct key_value *value;
1088 WCHAR *new_name = NULL;
1089 int i;
1091 if (name->len > MAX_VALUE_LEN * sizeof(WCHAR))
1093 set_error( STATUS_NAME_TOO_LONG );
1094 return NULL;
1096 if (key->last_value + 1 == key->nb_values)
1098 if (!grow_values( key )) return NULL;
1100 if (name->len && !(new_name = memdup( name->str, name->len ))) return NULL;
1101 for (i = ++key->last_value; i > index; i--) key->values[i] = key->values[i - 1];
1102 value = &key->values[index];
1103 value->name = new_name;
1104 value->namelen = name->len;
1105 value->len = 0;
1106 value->data = NULL;
1107 return value;
1110 /* set a key value */
1111 static void set_value( struct key *key, const struct unicode_str *name,
1112 int type, const void *data, data_size_t len )
1114 struct key_value *value;
1115 void *ptr = NULL;
1116 int index;
1118 if ((value = find_value( key, name, &index )))
1120 /* check if the new value is identical to the existing one */
1121 if (value->type == type && value->len == len &&
1122 value->data && !memcmp( value->data, data, len ))
1124 if (debug_level > 1) dump_operation( key, value, "Skip setting" );
1125 return;
1129 if (key->flags & KEY_SYMLINK)
1131 if (type != REG_LINK || name->len != symlink_str.len ||
1132 memicmp_strW( name->str, symlink_str.str, name->len ))
1134 set_error( STATUS_ACCESS_DENIED );
1135 return;
1139 if (len && !(ptr = memdup( data, len ))) return;
1141 if (!value)
1143 if (!(value = insert_value( key, name, index )))
1145 free( ptr );
1146 return;
1149 else free( value->data ); /* already existing, free previous data */
1151 value->type = type;
1152 value->len = len;
1153 value->data = ptr;
1154 touch_key( key, REG_NOTIFY_CHANGE_LAST_SET );
1155 if (debug_level > 1) dump_operation( key, value, "Set" );
1158 /* get a key value */
1159 static void get_value( struct key *key, const struct unicode_str *name, int *type, data_size_t *len )
1161 struct key_value *value;
1162 int index;
1164 if ((value = find_value( key, name, &index )))
1166 *type = value->type;
1167 *len = value->len;
1168 if (value->data) set_reply_data( value->data, min( value->len, get_reply_max_size() ));
1169 if (debug_level > 1) dump_operation( key, value, "Get" );
1171 else
1173 *type = -1;
1174 set_error( STATUS_OBJECT_NAME_NOT_FOUND );
1178 /* enumerate a key value */
1179 static void enum_value( struct key *key, int i, int info_class, struct enum_key_value_reply *reply )
1181 struct key_value *value;
1183 if (i < 0 || i > key->last_value) set_error( STATUS_NO_MORE_ENTRIES );
1184 else
1186 void *data;
1187 data_size_t namelen, maxlen;
1189 value = &key->values[i];
1190 reply->type = value->type;
1191 namelen = value->namelen;
1193 switch(info_class)
1195 case KeyValueBasicInformation:
1196 reply->total = namelen;
1197 break;
1198 case KeyValueFullInformation:
1199 reply->total = namelen + value->len;
1200 break;
1201 case KeyValuePartialInformation:
1202 reply->total = value->len;
1203 namelen = 0;
1204 break;
1205 default:
1206 set_error( STATUS_INVALID_PARAMETER );
1207 return;
1210 maxlen = min( reply->total, get_reply_max_size() );
1211 if (maxlen && ((data = set_reply_data_size( maxlen ))))
1213 if (maxlen > namelen)
1215 reply->namelen = namelen;
1216 memcpy( data, value->name, namelen );
1217 memcpy( (char *)data + namelen, value->data, maxlen - namelen );
1219 else
1221 reply->namelen = maxlen;
1222 memcpy( data, value->name, maxlen );
1225 if (debug_level > 1) dump_operation( key, value, "Enum" );
1229 /* delete a value */
1230 static void delete_value( struct key *key, const struct unicode_str *name )
1232 struct key_value *value;
1233 int i, index, nb_values;
1235 if (!(value = find_value( key, name, &index )))
1237 set_error( STATUS_OBJECT_NAME_NOT_FOUND );
1238 return;
1240 if (debug_level > 1) dump_operation( key, value, "Delete" );
1241 free( value->name );
1242 free( value->data );
1243 for (i = index; i < key->last_value; i++) key->values[i] = key->values[i + 1];
1244 key->last_value--;
1245 touch_key( key, REG_NOTIFY_CHANGE_LAST_SET );
1247 /* try to shrink the array */
1248 nb_values = key->nb_values;
1249 if (nb_values > MIN_VALUES && key->last_value < nb_values / 2)
1251 struct key_value *new_val;
1252 nb_values -= nb_values / 3; /* shrink by 33% */
1253 if (nb_values < MIN_VALUES) nb_values = MIN_VALUES;
1254 if (!(new_val = realloc( key->values, nb_values * sizeof(*new_val) ))) return;
1255 key->values = new_val;
1256 key->nb_values = nb_values;
1260 /* get the registry key corresponding to an hkey handle */
1261 static struct key *get_hkey_obj( obj_handle_t hkey, unsigned int access )
1263 struct key *key = (struct key *)get_handle_obj( current->process, hkey, access, &key_ops );
1265 if (key && key->flags & KEY_DELETED)
1267 set_error( STATUS_KEY_DELETED );
1268 release_object( key );
1269 key = NULL;
1271 return key;
1274 /* get the registry key corresponding to a parent key handle */
1275 static inline struct key *get_parent_hkey_obj( obj_handle_t hkey )
1277 if (!hkey) return (struct key *)grab_object( root_key );
1278 return get_hkey_obj( hkey, 0 );
1281 /* read a line from the input file */
1282 static int read_next_line( struct file_load_info *info )
1284 char *newbuf;
1285 int newlen, pos = 0;
1287 info->line++;
1288 for (;;)
1290 if (!fgets( info->buffer + pos, info->len - pos, info->file ))
1291 return (pos != 0); /* EOF */
1292 pos = strlen(info->buffer);
1293 if (info->buffer[pos-1] == '\n')
1295 /* got a full line */
1296 info->buffer[--pos] = 0;
1297 if (pos > 0 && info->buffer[pos-1] == '\r') info->buffer[pos-1] = 0;
1298 return 1;
1300 if (pos < info->len - 1) return 1; /* EOF but something was read */
1302 /* need to enlarge the buffer */
1303 newlen = info->len + info->len / 2;
1304 if (!(newbuf = realloc( info->buffer, newlen )))
1306 set_error( STATUS_NO_MEMORY );
1307 return -1;
1309 info->buffer = newbuf;
1310 info->len = newlen;
1314 /* make sure the temp buffer holds enough space */
1315 static int get_file_tmp_space( struct file_load_info *info, size_t size )
1317 WCHAR *tmp;
1318 if (info->tmplen >= size) return 1;
1319 if (!(tmp = realloc( info->tmp, size )))
1321 set_error( STATUS_NO_MEMORY );
1322 return 0;
1324 info->tmp = tmp;
1325 info->tmplen = size;
1326 return 1;
1329 /* report an error while loading an input file */
1330 static void file_read_error( const char *err, struct file_load_info *info )
1332 if (info->filename)
1333 fprintf( stderr, "%s:%d: %s '%s'\n", info->filename, info->line, err, info->buffer );
1334 else
1335 fprintf( stderr, "<fd>:%d: %s '%s'\n", info->line, err, info->buffer );
1338 /* convert a data type tag to a value type */
1339 static int get_data_type( const char *buffer, int *type, int *parse_type )
1341 struct data_type { const char *tag; int len; int type; int parse_type; };
1343 static const struct data_type data_types[] =
1344 { /* actual type */ /* type to assume for parsing */
1345 { "\"", 1, REG_SZ, REG_SZ },
1346 { "str:\"", 5, REG_SZ, REG_SZ },
1347 { "str(2):\"", 8, REG_EXPAND_SZ, REG_SZ },
1348 { "str(7):\"", 8, REG_MULTI_SZ, REG_SZ },
1349 { "hex:", 4, REG_BINARY, REG_BINARY },
1350 { "dword:", 6, REG_DWORD, REG_DWORD },
1351 { "hex(", 4, -1, REG_BINARY },
1352 { NULL, 0, 0, 0 }
1355 const struct data_type *ptr;
1356 char *end;
1358 for (ptr = data_types; ptr->tag; ptr++)
1360 if (strncmp( ptr->tag, buffer, ptr->len )) continue;
1361 *parse_type = ptr->parse_type;
1362 if ((*type = ptr->type) != -1) return ptr->len;
1363 /* "hex(xx):" is special */
1364 *type = (int)strtoul( buffer + 4, &end, 16 );
1365 if ((end <= buffer) || strncmp( end, "):", 2 )) return 0;
1366 return end + 2 - buffer;
1368 return 0;
1371 /* load and create a key from the input file */
1372 static struct key *load_key( struct key *base, const char *buffer, int prefix_len,
1373 struct file_load_info *info, timeout_t *modif )
1375 WCHAR *p;
1376 struct unicode_str name;
1377 int res;
1378 unsigned int mod;
1379 data_size_t len;
1381 if (!get_file_tmp_space( info, strlen(buffer) * sizeof(WCHAR) )) return NULL;
1383 len = info->tmplen;
1384 if ((res = parse_strW( info->tmp, &len, buffer, ']' )) == -1)
1386 file_read_error( "Malformed key", info );
1387 return NULL;
1389 if (sscanf( buffer + res, " %u", &mod ) == 1)
1390 *modif = (timeout_t)mod * TICKS_PER_SEC + ticks_1601_to_1970;
1391 else
1392 *modif = current_time;
1394 p = info->tmp;
1395 while (prefix_len && *p) { if (*p++ == '\\') prefix_len--; }
1397 if (!*p)
1399 if (prefix_len > 1)
1401 file_read_error( "Malformed key", info );
1402 return NULL;
1404 /* empty key name, return base key */
1405 return (struct key *)grab_object( base );
1407 name.str = p;
1408 name.len = len - (p - info->tmp + 1) * sizeof(WCHAR);
1409 return create_key_recursive( base, &name, 0 );
1412 /* update the modification time of a key (and its parents) after it has been loaded from a file */
1413 static void update_key_time( struct key *key, timeout_t modif )
1415 while (key && !key->modif)
1417 key->modif = modif;
1418 key = key->parent;
1422 /* load a global option from the input file */
1423 static int load_global_option( const char *buffer, struct file_load_info *info )
1425 const char *p;
1427 if (!strncmp( buffer, "#arch=", 6 ))
1429 enum prefix_type type;
1430 p = buffer + 6;
1431 if (!strcmp( p, "win32" )) type = PREFIX_32BIT;
1432 else if (!strcmp( p, "win64" )) type = PREFIX_64BIT;
1433 else
1435 file_read_error( "Unknown architecture", info );
1436 set_error( STATUS_NOT_REGISTRY_FILE );
1437 return 0;
1439 if (prefix_type == PREFIX_UNKNOWN) prefix_type = type;
1440 else if (type != prefix_type)
1442 file_read_error( "Mismatched architecture", info );
1443 set_error( STATUS_NOT_REGISTRY_FILE );
1444 return 0;
1447 /* ignore unknown options */
1448 return 1;
1451 /* load a key option from the input file */
1452 static int load_key_option( struct key *key, const char *buffer, struct file_load_info *info )
1454 const char *p;
1455 data_size_t len;
1457 if (!strncmp( buffer, "#time=", 6 ))
1459 timeout_t modif = 0;
1460 for (p = buffer + 6; *p; p++)
1462 if (*p >= '0' && *p <= '9') modif = (modif << 4) | (*p - '0');
1463 else if (*p >= 'A' && *p <= 'F') modif = (modif << 4) | (*p - 'A' + 10);
1464 else if (*p >= 'a' && *p <= 'f') modif = (modif << 4) | (*p - 'a' + 10);
1465 else break;
1467 update_key_time( key, modif );
1469 if (!strncmp( buffer, "#class=", 7 ))
1471 p = buffer + 7;
1472 if (*p++ != '"') return 0;
1473 if (!get_file_tmp_space( info, strlen(p) * sizeof(WCHAR) )) return 0;
1474 len = info->tmplen;
1475 if (parse_strW( info->tmp, &len, p, '\"' ) == -1) return 0;
1476 free( key->class );
1477 if (!(key->class = memdup( info->tmp, len ))) len = 0;
1478 key->classlen = len;
1480 if (!strncmp( buffer, "#link", 5 )) key->flags |= KEY_SYMLINK;
1481 /* ignore unknown options */
1482 return 1;
1485 /* parse a comma-separated list of hex digits */
1486 static int parse_hex( unsigned char *dest, data_size_t *len, const char *buffer )
1488 const char *p = buffer;
1489 data_size_t count = 0;
1490 char *end;
1492 while (isxdigit(*p))
1494 unsigned int val = strtoul( p, &end, 16 );
1495 if (end == p || val > 0xff) return -1;
1496 if (count++ >= *len) return -1; /* dest buffer overflow */
1497 *dest++ = val;
1498 p = end;
1499 while (isspace(*p)) p++;
1500 if (*p == ',') p++;
1501 while (isspace(*p)) p++;
1503 *len = count;
1504 return p - buffer;
1507 /* parse a value name and create the corresponding value */
1508 static struct key_value *parse_value_name( struct key *key, const char *buffer, data_size_t *len,
1509 struct file_load_info *info )
1511 struct key_value *value;
1512 struct unicode_str name;
1513 int index;
1515 if (!get_file_tmp_space( info, strlen(buffer) * sizeof(WCHAR) )) return NULL;
1516 name.str = info->tmp;
1517 name.len = info->tmplen;
1518 if (buffer[0] == '@')
1520 name.len = 0;
1521 *len = 1;
1523 else
1525 int r = parse_strW( info->tmp, &name.len, buffer + 1, '\"' );
1526 if (r == -1) goto error;
1527 *len = r + 1; /* for initial quote */
1528 name.len -= sizeof(WCHAR); /* terminating null */
1530 while (isspace(buffer[*len])) (*len)++;
1531 if (buffer[*len] != '=') goto error;
1532 (*len)++;
1533 while (isspace(buffer[*len])) (*len)++;
1534 if (!(value = find_value( key, &name, &index ))) value = insert_value( key, &name, index );
1535 return value;
1537 error:
1538 file_read_error( "Malformed value name", info );
1539 return NULL;
1542 /* load a value from the input file */
1543 static int load_value( struct key *key, const char *buffer, struct file_load_info *info )
1545 DWORD dw;
1546 void *ptr, *newptr;
1547 int res, type, parse_type;
1548 data_size_t maxlen, len;
1549 struct key_value *value;
1551 if (!(value = parse_value_name( key, buffer, &len, info ))) return 0;
1552 if (!(res = get_data_type( buffer + len, &type, &parse_type ))) goto error;
1553 buffer += len + res;
1555 switch(parse_type)
1557 case REG_SZ:
1558 if (!get_file_tmp_space( info, strlen(buffer) * sizeof(WCHAR) )) return 0;
1559 len = info->tmplen;
1560 if ((res = parse_strW( info->tmp, &len, buffer, '\"' )) == -1) goto error;
1561 ptr = info->tmp;
1562 break;
1563 case REG_DWORD:
1564 dw = strtoul( buffer, NULL, 16 );
1565 ptr = &dw;
1566 len = sizeof(dw);
1567 break;
1568 case REG_BINARY: /* hex digits */
1569 len = 0;
1570 for (;;)
1572 maxlen = 1 + strlen(buffer) / 2; /* at least 2 chars for one hex byte */
1573 if (!get_file_tmp_space( info, len + maxlen )) return 0;
1574 if ((res = parse_hex( (unsigned char *)info->tmp + len, &maxlen, buffer )) == -1) goto error;
1575 len += maxlen;
1576 buffer += res;
1577 while (isspace(*buffer)) buffer++;
1578 if (!*buffer) break;
1579 if (*buffer != '\\') goto error;
1580 if (read_next_line( info) != 1) goto error;
1581 buffer = info->buffer;
1582 while (isspace(*buffer)) buffer++;
1584 ptr = info->tmp;
1585 break;
1586 default:
1587 assert(0);
1588 ptr = NULL; /* keep compiler quiet */
1589 break;
1592 if (!len) newptr = NULL;
1593 else if (!(newptr = memdup( ptr, len ))) return 0;
1595 free( value->data );
1596 value->data = newptr;
1597 value->len = len;
1598 value->type = type;
1599 return 1;
1601 error:
1602 file_read_error( "Malformed value", info );
1603 free( value->data );
1604 value->data = NULL;
1605 value->len = 0;
1606 value->type = REG_NONE;
1607 return 0;
1610 /* return the length (in path elements) of name that is part of the key name */
1611 /* for instance if key is USER\foo\bar and name is foo\bar\baz, return 2 */
1612 static int get_prefix_len( struct key *key, const char *name, struct file_load_info *info )
1614 WCHAR *p;
1615 int res;
1616 data_size_t len;
1618 if (!get_file_tmp_space( info, strlen(name) * sizeof(WCHAR) )) return 0;
1620 len = info->tmplen;
1621 if ((res = parse_strW( info->tmp, &len, name, ']' )) == -1)
1623 file_read_error( "Malformed key", info );
1624 return 0;
1626 for (p = info->tmp; *p; p++) if (*p == '\\') break;
1627 len = (p - info->tmp) * sizeof(WCHAR);
1628 for (res = 1; key != root_key; res++)
1630 if (len == key->namelen && !memicmp_strW( info->tmp, key->name, len )) break;
1631 key = key->parent;
1633 if (key == root_key) res = 0; /* no matching name */
1634 return res;
1637 /* load all the keys from the input file */
1638 /* prefix_len is the number of key name prefixes to skip, or -1 for autodetection */
1639 static void load_keys( struct key *key, const char *filename, FILE *f, int prefix_len )
1641 struct key *subkey = NULL;
1642 struct file_load_info info;
1643 timeout_t modif = current_time;
1644 char *p;
1646 info.filename = filename;
1647 info.file = f;
1648 info.len = 4;
1649 info.tmplen = 4;
1650 info.line = 0;
1651 if (!(info.buffer = mem_alloc( info.len ))) return;
1652 if (!(info.tmp = mem_alloc( info.tmplen )))
1654 free( info.buffer );
1655 return;
1658 if ((read_next_line( &info ) != 1) ||
1659 strcmp( info.buffer, "WINE REGISTRY Version 2" ))
1661 set_error( STATUS_NOT_REGISTRY_FILE );
1662 goto done;
1665 while (read_next_line( &info ) == 1)
1667 p = info.buffer;
1668 while (*p && isspace(*p)) p++;
1669 switch(*p)
1671 case '[': /* new key */
1672 if (subkey)
1674 update_key_time( subkey, modif );
1675 release_object( subkey );
1677 if (prefix_len == -1) prefix_len = get_prefix_len( key, p + 1, &info );
1678 if (!(subkey = load_key( key, p + 1, prefix_len, &info, &modif )))
1679 file_read_error( "Error creating key", &info );
1680 break;
1681 case '@': /* default value */
1682 case '\"': /* value */
1683 if (subkey) load_value( subkey, p, &info );
1684 else file_read_error( "Value without key", &info );
1685 break;
1686 case '#': /* option */
1687 if (subkey) load_key_option( subkey, p, &info );
1688 else if (!load_global_option( p, &info )) goto done;
1689 break;
1690 case ';': /* comment */
1691 case 0: /* empty line */
1692 break;
1693 default:
1694 file_read_error( "Unrecognized input", &info );
1695 break;
1699 done:
1700 if (subkey)
1702 update_key_time( subkey, modif );
1703 release_object( subkey );
1705 free( info.buffer );
1706 free( info.tmp );
1709 /* load a part of the registry from a file */
1710 static void load_registry( struct key *key, obj_handle_t handle )
1712 struct file *file;
1713 int fd;
1715 if (!(file = get_file_obj( current->process, handle, FILE_READ_DATA ))) return;
1716 fd = dup( get_file_unix_fd( file ) );
1717 release_object( file );
1718 if (fd != -1)
1720 FILE *f = fdopen( fd, "r" );
1721 if (f)
1723 load_keys( key, NULL, f, -1 );
1724 fclose( f );
1726 else file_set_error();
1730 /* load one of the initial registry files */
1731 static int load_init_registry_from_file( const char *filename, struct key *key )
1733 FILE *f;
1735 if ((f = fopen( filename, "r" )))
1737 load_keys( key, filename, f, 0 );
1738 fclose( f );
1739 if (get_error() == STATUS_NOT_REGISTRY_FILE)
1741 fprintf( stderr, "%s is not a valid registry file\n", filename );
1742 return 1;
1746 assert( save_branch_count < MAX_SAVE_BRANCH_INFO );
1748 save_branch_info[save_branch_count].path = filename;
1749 save_branch_info[save_branch_count++].key = (struct key *)grab_object( key );
1750 make_object_permanent( &key->obj );
1751 return (f != NULL);
1754 static WCHAR *format_user_registry_path( const SID *sid, struct unicode_str *path )
1756 char buffer[7 + 11 + 11 + 11 * SID_MAX_SUB_AUTHORITIES], *p = buffer;
1757 unsigned int i;
1759 p += sprintf( p, "User\\S-%u-%u", sid->Revision,
1760 MAKELONG( MAKEWORD( sid->IdentifierAuthority.Value[5],
1761 sid->IdentifierAuthority.Value[4] ),
1762 MAKEWORD( sid->IdentifierAuthority.Value[3],
1763 sid->IdentifierAuthority.Value[2] )));
1764 for (i = 0; i < sid->SubAuthorityCount; i++) p += sprintf( p, "-%u", sid->SubAuthority[i] );
1765 return ascii_to_unicode_str( buffer, path );
1768 /* get the cpu architectures that can be supported in the current prefix */
1769 unsigned int get_prefix_cpu_mask(void)
1771 /* Allowed server/client/prefix combinations:
1773 * prefix
1774 * 32 64
1775 * server +------+------+ client
1776 * | ok | fail | 32
1777 * 32 +------+------+---
1778 * | fail | fail | 64
1779 * ---+------+------+---
1780 * | ok | ok | 32
1781 * 64 +------+------+---
1782 * | fail | ok | 64
1783 * ---+------+------+---
1785 switch (prefix_type)
1787 case PREFIX_64BIT:
1788 /* 64-bit prefix requires 64-bit server */
1789 return sizeof(void *) > sizeof(int) ? ~0 : 0;
1790 case PREFIX_32BIT:
1791 default:
1792 return ~CPU_64BIT_MASK; /* only 32-bit cpus supported on 32-bit prefix */
1796 /* registry initialisation */
1797 void init_registry(void)
1799 static const WCHAR HKLM[] = { 'M','a','c','h','i','n','e' };
1800 static const WCHAR HKU_default[] = { 'U','s','e','r','\\','.','D','e','f','a','u','l','t' };
1801 static const WCHAR classes[] = {'S','o','f','t','w','a','r','e','\\',
1802 'C','l','a','s','s','e','s','\\',
1803 'W','o','w','6','4','3','2','N','o','d','e'};
1804 static const struct unicode_str root_name = { NULL, 0 };
1805 static const struct unicode_str HKLM_name = { HKLM, sizeof(HKLM) };
1806 static const struct unicode_str HKU_name = { HKU_default, sizeof(HKU_default) };
1807 static const struct unicode_str classes_name = { classes, sizeof(classes) };
1809 WCHAR *current_user_path;
1810 struct unicode_str current_user_str;
1811 struct key *key, *hklm, *hkcu;
1812 char *p;
1814 /* switch to the config dir */
1816 if (fchdir( config_dir_fd ) == -1) fatal_error( "chdir to config dir: %s\n", strerror( errno ));
1818 /* create the root key */
1819 root_key = alloc_key( &root_name, current_time );
1820 assert( root_key );
1821 make_object_permanent( &root_key->obj );
1823 /* load system.reg into Registry\Machine */
1825 if (!(hklm = create_key_recursive( root_key, &HKLM_name, current_time )))
1826 fatal_error( "could not create Machine registry key\n" );
1828 if (!load_init_registry_from_file( "system.reg", hklm ))
1830 if ((p = getenv( "WINEARCH" )) && !strcmp( p, "win32" ))
1831 prefix_type = PREFIX_32BIT;
1832 else
1833 prefix_type = sizeof(void *) > sizeof(int) ? PREFIX_64BIT : PREFIX_32BIT;
1835 else if (prefix_type == PREFIX_UNKNOWN)
1836 prefix_type = PREFIX_32BIT;
1838 /* load userdef.reg into Registry\User\.Default */
1840 if (!(key = create_key_recursive( root_key, &HKU_name, current_time )))
1841 fatal_error( "could not create User\\.Default registry key\n" );
1843 load_init_registry_from_file( "userdef.reg", key );
1844 release_object( key );
1846 /* load user.reg into HKEY_CURRENT_USER */
1848 /* FIXME: match default user in token.c. should get from process token instead */
1849 current_user_path = format_user_registry_path( security_local_user_sid, &current_user_str );
1850 if (!current_user_path ||
1851 !(hkcu = create_key_recursive( root_key, &current_user_str, current_time )))
1852 fatal_error( "could not create HKEY_CURRENT_USER registry key\n" );
1853 free( current_user_path );
1854 load_init_registry_from_file( "user.reg", hkcu );
1856 /* set the shared flag on Software\Classes\Wow6432Node */
1857 if (prefix_type == PREFIX_64BIT)
1859 if ((key = create_key_recursive( hklm, &classes_name, current_time )))
1861 key->flags |= KEY_WOWSHARE;
1862 release_object( key );
1864 /* FIXME: handle HKCU too */
1867 release_object( hklm );
1868 release_object( hkcu );
1870 /* start the periodic save timer */
1871 set_periodic_save_timer();
1873 /* create windows directories */
1875 if (!mkdir( "drive_c/windows", 0777 ))
1877 mkdir( "drive_c/windows/system32", 0777 );
1878 if (prefix_type == PREFIX_64BIT) mkdir( "drive_c/windows/syswow64", 0777 );
1881 /* go back to the server dir */
1882 if (fchdir( server_dir_fd ) == -1) fatal_error( "chdir to server dir: %s\n", strerror( errno ));
1885 /* save a registry branch to a file */
1886 static void save_all_subkeys( struct key *key, FILE *f )
1888 fprintf( f, "WINE REGISTRY Version 2\n" );
1889 fprintf( f, ";; All keys relative to " );
1890 dump_path( key, NULL, f );
1891 fprintf( f, "\n" );
1892 switch (prefix_type)
1894 case PREFIX_32BIT:
1895 fprintf( f, "\n#arch=win32\n" );
1896 break;
1897 case PREFIX_64BIT:
1898 fprintf( f, "\n#arch=win64\n" );
1899 break;
1900 default:
1901 break;
1903 save_subkeys( key, key, f );
1906 /* save a registry branch to a file handle */
1907 static void save_registry( struct key *key, obj_handle_t handle )
1909 struct file *file;
1910 int fd;
1912 if (!(file = get_file_obj( current->process, handle, FILE_WRITE_DATA ))) return;
1913 fd = dup( get_file_unix_fd( file ) );
1914 release_object( file );
1915 if (fd != -1)
1917 FILE *f = fdopen( fd, "w" );
1918 if (f)
1920 save_all_subkeys( key, f );
1921 if (fclose( f )) file_set_error();
1923 else
1925 file_set_error();
1926 close( fd );
1931 /* save a registry branch to a file */
1932 static int save_branch( struct key *key, const char *path )
1934 struct stat st;
1935 char *p, *tmp = NULL;
1936 int fd, count = 0, ret = 0;
1937 FILE *f;
1939 if (!(key->flags & KEY_DIRTY))
1941 if (debug_level > 1) dump_operation( key, NULL, "Not saving clean" );
1942 return 1;
1945 /* test the file type */
1947 if ((fd = open( path, O_WRONLY )) != -1)
1949 /* if file is not a regular file or has multiple links or is accessed
1950 * via symbolic links, write directly into it; otherwise use a temp file */
1951 if (!lstat( path, &st ) && (!S_ISREG(st.st_mode) || st.st_nlink > 1))
1953 ftruncate( fd, 0 );
1954 goto save;
1956 close( fd );
1959 /* create a temp file in the same directory */
1961 if (!(tmp = malloc( strlen(path) + 20 ))) goto done;
1962 strcpy( tmp, path );
1963 if ((p = strrchr( tmp, '/' ))) p++;
1964 else p = tmp;
1965 for (;;)
1967 sprintf( p, "reg%lx%04x.tmp", (long) getpid(), count++ );
1968 if ((fd = open( tmp, O_CREAT | O_EXCL | O_WRONLY, 0666 )) != -1) break;
1969 if (errno != EEXIST) goto done;
1970 close( fd );
1973 /* now save to it */
1975 save:
1976 if (!(f = fdopen( fd, "w" )))
1978 if (tmp) unlink( tmp );
1979 close( fd );
1980 goto done;
1983 if (debug_level > 1)
1985 fprintf( stderr, "%s: ", path );
1986 dump_operation( key, NULL, "saving" );
1989 save_all_subkeys( key, f );
1990 ret = !fclose(f);
1992 if (tmp)
1994 /* if successfully written, rename to final name */
1995 if (ret) ret = !rename( tmp, path );
1996 if (!ret) unlink( tmp );
1999 done:
2000 free( tmp );
2001 if (ret) make_clean( key );
2002 return ret;
2005 /* periodic saving of the registry */
2006 static void periodic_save( void *arg )
2008 int i;
2010 if (fchdir( config_dir_fd ) == -1) return;
2011 save_timeout_user = NULL;
2012 for (i = 0; i < save_branch_count; i++)
2013 save_branch( save_branch_info[i].key, save_branch_info[i].path );
2014 if (fchdir( server_dir_fd ) == -1) fatal_error( "chdir to server dir: %s\n", strerror( errno ));
2015 set_periodic_save_timer();
2018 /* start the periodic save timer */
2019 static void set_periodic_save_timer(void)
2021 if (save_timeout_user) remove_timeout_user( save_timeout_user );
2022 save_timeout_user = add_timeout_user( save_period, periodic_save, NULL );
2025 /* save the modified registry branches to disk */
2026 void flush_registry(void)
2028 int i;
2030 if (fchdir( config_dir_fd ) == -1) return;
2031 for (i = 0; i < save_branch_count; i++)
2033 if (!save_branch( save_branch_info[i].key, save_branch_info[i].path ))
2035 fprintf( stderr, "wineserver: could not save registry branch to %s",
2036 save_branch_info[i].path );
2037 perror( " " );
2040 if (fchdir( server_dir_fd ) == -1) fatal_error( "chdir to server dir: %s\n", strerror( errno ));
2043 /* determine if the thread is wow64 (32-bit client running on 64-bit prefix) */
2044 static int is_wow64_thread( struct thread *thread )
2046 return (prefix_type == PREFIX_64BIT && !(CPU_FLAG(thread->process->cpu) & CPU_64BIT_MASK));
2050 /* create a registry key */
2051 DECL_HANDLER(create_key)
2053 struct key *key = NULL, *parent;
2054 struct unicode_str name, class;
2055 unsigned int access = req->access;
2056 const struct security_descriptor *sd;
2057 const struct object_attributes *objattr = get_req_object_attributes( &sd, &name, NULL );
2059 if (!objattr) return;
2061 if (!is_wow64_thread( current )) access = (access & ~KEY_WOW64_32KEY) | KEY_WOW64_64KEY;
2063 class.str = get_req_data_after_objattr( objattr, &class.len );
2064 class.len = (class.len / sizeof(WCHAR)) * sizeof(WCHAR);
2066 if (!objattr->rootdir && name.len >= sizeof(root_name) &&
2067 !memicmp_strW( name.str, root_name, sizeof(root_name) ))
2069 name.str += ARRAY_SIZE( root_name );
2070 name.len -= sizeof(root_name);
2073 /* NOTE: no access rights are required from the parent handle to create a key */
2074 if ((parent = get_parent_hkey_obj( objattr->rootdir )))
2076 if ((key = create_key( parent, &name, &class, req->options, access,
2077 objattr->attributes, sd, &reply->created )))
2079 reply->hkey = alloc_handle( current->process, key, access, objattr->attributes );
2080 release_object( key );
2082 release_object( parent );
2086 /* open a registry key */
2087 DECL_HANDLER(open_key)
2089 struct key *key, *parent;
2090 struct unicode_str name;
2091 unsigned int access = req->access;
2093 if (!is_wow64_thread( current )) access = (access & ~KEY_WOW64_32KEY) | KEY_WOW64_64KEY;
2095 reply->hkey = 0;
2096 /* NOTE: no access rights are required to open the parent key, only the child key */
2097 if ((parent = get_parent_hkey_obj( req->parent )))
2099 get_req_path( &name, !req->parent );
2100 if ((key = open_key( parent, &name, access, req->attributes )))
2102 reply->hkey = alloc_handle( current->process, key, access, req->attributes );
2103 release_object( key );
2105 release_object( parent );
2109 /* delete a registry key */
2110 DECL_HANDLER(delete_key)
2112 struct key *key;
2114 if ((key = get_hkey_obj( req->hkey, DELETE )))
2116 delete_key( key, 0);
2117 release_object( key );
2121 /* flush a registry key */
2122 DECL_HANDLER(flush_key)
2124 struct key *key = get_hkey_obj( req->hkey, 0 );
2125 if (key)
2127 /* we don't need to do anything here with the current implementation */
2128 release_object( key );
2132 /* enumerate registry subkeys */
2133 DECL_HANDLER(enum_key)
2135 struct key *key;
2137 if ((key = get_hkey_obj( req->hkey,
2138 req->index == -1 ? KEY_QUERY_VALUE : KEY_ENUMERATE_SUB_KEYS )))
2140 enum_key( key, req->index, req->info_class, reply );
2141 release_object( key );
2145 /* set a value of a registry key */
2146 DECL_HANDLER(set_key_value)
2148 struct key *key;
2149 struct unicode_str name;
2151 if (req->namelen > get_req_data_size())
2153 set_error( STATUS_INVALID_PARAMETER );
2154 return;
2156 name.str = get_req_data();
2157 name.len = (req->namelen / sizeof(WCHAR)) * sizeof(WCHAR);
2159 if ((key = get_hkey_obj( req->hkey, KEY_SET_VALUE )))
2161 data_size_t datalen = get_req_data_size() - req->namelen;
2162 const char *data = (const char *)get_req_data() + req->namelen;
2164 set_value( key, &name, req->type, data, datalen );
2165 release_object( key );
2169 /* retrieve the value of a registry key */
2170 DECL_HANDLER(get_key_value)
2172 struct key *key;
2173 struct unicode_str name = get_req_unicode_str();
2175 reply->total = 0;
2176 if ((key = get_hkey_obj( req->hkey, KEY_QUERY_VALUE )))
2178 get_value( key, &name, &reply->type, &reply->total );
2179 release_object( key );
2183 /* enumerate the value of a registry key */
2184 DECL_HANDLER(enum_key_value)
2186 struct key *key;
2188 if ((key = get_hkey_obj( req->hkey, KEY_QUERY_VALUE )))
2190 enum_value( key, req->index, req->info_class, reply );
2191 release_object( key );
2195 /* delete a value of a registry key */
2196 DECL_HANDLER(delete_key_value)
2198 struct key *key;
2199 struct unicode_str name = get_req_unicode_str();
2201 if ((key = get_hkey_obj( req->hkey, KEY_SET_VALUE )))
2203 delete_value( key, &name );
2204 release_object( key );
2208 /* load a registry branch from a file */
2209 DECL_HANDLER(load_registry)
2211 struct key *key, *parent;
2212 struct unicode_str name;
2213 const struct security_descriptor *sd;
2214 const struct object_attributes *objattr = get_req_object_attributes( &sd, &name, NULL );
2216 if (!objattr) return;
2218 if (!thread_single_check_privilege( current, &SeRestorePrivilege ))
2220 set_error( STATUS_PRIVILEGE_NOT_HELD );
2221 return;
2224 if (!objattr->rootdir && name.len >= sizeof(root_name) &&
2225 !memicmp_strW( name.str, root_name, sizeof(root_name) ))
2227 name.str += ARRAY_SIZE( root_name );
2228 name.len -= sizeof(root_name);
2231 if ((parent = get_parent_hkey_obj( objattr->rootdir )))
2233 int dummy;
2234 if ((key = create_key( parent, &name, NULL, 0, KEY_WOW64_64KEY, 0, sd, &dummy )))
2236 load_registry( key, req->file );
2237 release_object( key );
2239 release_object( parent );
2243 DECL_HANDLER(unload_registry)
2245 struct key *key;
2247 if (!thread_single_check_privilege( current, &SeRestorePrivilege ))
2249 set_error( STATUS_PRIVILEGE_NOT_HELD );
2250 return;
2253 if ((key = get_hkey_obj( req->hkey, 0 )))
2255 delete_key( key, 1 ); /* FIXME */
2256 release_object( key );
2260 /* save a registry branch to a file */
2261 DECL_HANDLER(save_registry)
2263 struct key *key;
2265 if (!thread_single_check_privilege( current, &SeBackupPrivilege ))
2267 set_error( STATUS_PRIVILEGE_NOT_HELD );
2268 return;
2271 if ((key = get_hkey_obj( req->hkey, 0 )))
2273 save_registry( key, req->file );
2274 release_object( key );
2278 /* add a registry key change notification */
2279 DECL_HANDLER(set_registry_notification)
2281 struct key *key;
2282 struct event *event;
2283 struct notify *notify;
2285 key = get_hkey_obj( req->hkey, KEY_NOTIFY );
2286 if (key)
2288 event = get_event_obj( current->process, req->event, SYNCHRONIZE );
2289 if (event)
2291 notify = find_notify( key, current->process, req->hkey );
2292 if (!notify)
2294 notify = mem_alloc( sizeof(*notify) );
2295 if (notify)
2297 notify->events = NULL;
2298 notify->event_count = 0;
2299 notify->subtree = req->subtree;
2300 notify->filter = req->filter;
2301 notify->hkey = req->hkey;
2302 notify->process = current->process;
2303 list_add_head( &key->notify_list, &notify->entry );
2306 if (notify)
2308 struct event **new_array;
2310 if ((new_array = realloc( notify->events, (notify->event_count + 1) * sizeof(*notify->events) )))
2312 notify->events = new_array;
2313 notify->events[notify->event_count++] = (struct event *)grab_object( event );
2314 reset_event( event );
2315 set_error( STATUS_PENDING );
2317 else set_error( STATUS_NO_MEMORY );
2319 release_object( event );
2321 release_object( key );