4 * Copyright (C) 1998 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
22 #include "wine/port.h"
33 #define WIN32_NO_STATUS
45 struct list entry; /* entry in the hash list */
46 struct object *obj; /* object owning this name */
47 struct object *parent; /* parent object */
48 data_size_t len; /* name length in bytes */
54 unsigned int hash_size; /* size of hash table */
55 struct list names[1]; /* array of hash entry lists */
60 static struct list object_list = LIST_INIT(object_list);
61 static struct list static_object_list = LIST_INIT(static_object_list);
63 void dump_objects(void)
67 LIST_FOR_EACH( p, &static_object_list )
69 struct object *ptr = LIST_ENTRY( p, struct object, obj_list );
70 fprintf( stderr, "%p:%d: ", ptr, ptr->refcount );
71 ptr->ops->dump( ptr, 1 );
73 LIST_FOR_EACH( p, &object_list )
75 struct object *ptr = LIST_ENTRY( p, struct object, obj_list );
76 fprintf( stderr, "%p:%d: ", ptr, ptr->refcount );
77 ptr->ops->dump( ptr, 1 );
81 void close_objects(void)
85 /* release the static objects */
86 while ((ptr = list_head( &static_object_list )))
88 struct object *obj = LIST_ENTRY( ptr, struct object, obj_list );
89 /* move it back to the standard list before freeing */
90 list_remove( &obj->obj_list );
91 list_add_head( &object_list, &obj->obj_list );
92 release_object( obj );
95 dump_objects(); /* dump any remaining objects */
98 #endif /* DEBUG_OBJECTS */
100 /*****************************************************************/
102 /* malloc replacement */
103 void *mem_alloc( size_t size )
105 void *ptr = malloc( size );
106 if (ptr) memset( ptr, 0x55, size );
107 else set_error( STATUS_NO_MEMORY );
111 /* duplicate a block of memory */
112 void *memdup( const void *data, size_t len )
114 void *ptr = malloc( len );
115 if (ptr) memcpy( ptr, data, len );
116 else set_error( STATUS_NO_MEMORY );
121 /*****************************************************************/
123 static int get_name_hash( const struct namespace *namespace, const WCHAR *name, data_size_t len )
126 len /= sizeof(WCHAR);
127 while (len--) hash ^= tolowerW(*name++);
128 return hash % namespace->hash_size;
131 /* allocate a name for an object */
132 static struct object_name *alloc_name( const struct unicode_str *name )
134 struct object_name *ptr;
136 if ((ptr = mem_alloc( sizeof(*ptr) + name->len - sizeof(ptr->name) )))
138 ptr->len = name->len;
140 memcpy( ptr->name, name->str, name->len );
145 /* free the name of an object */
146 static void free_name( struct object *obj )
148 struct object_name *ptr = obj->name;
149 list_remove( &ptr->entry );
150 if (ptr->parent) release_object( ptr->parent );
154 /* set the name of an existing object */
155 static void set_object_name( struct namespace *namespace,
156 struct object *obj, struct object_name *ptr )
158 int hash = get_name_hash( namespace, ptr->name, ptr->len );
160 list_add_head( &namespace->names[hash], &ptr->entry );
165 /* get the name of an existing object */
166 const WCHAR *get_object_name( struct object *obj, data_size_t *len )
168 struct object_name *ptr = obj->name;
169 if (!ptr) return NULL;
174 /* allocate and initialize an object */
175 void *alloc_object( const struct object_ops *ops )
177 struct object *obj = mem_alloc( ops->size );
184 list_init( &obj->wait_queue );
186 list_add_head( &object_list, &obj->obj_list );
193 void *create_object( struct namespace *namespace, const struct object_ops *ops,
194 const struct unicode_str *name, struct object *parent )
197 struct object_name *name_ptr;
199 if (!(name_ptr = alloc_name( name ))) return NULL;
200 if ((obj = alloc_object( ops )))
202 set_object_name( namespace, obj, name_ptr );
203 if (parent) name_ptr->parent = grab_object( parent );
210 void *create_named_object( struct namespace *namespace, const struct object_ops *ops,
211 const struct unicode_str *name, unsigned int attributes )
215 if (!name || !name->len) return alloc_object( ops );
217 if ((obj = find_object( namespace, name, attributes )))
219 if (attributes & OBJ_OPENIF && obj->ops == ops)
220 set_error( STATUS_OBJECT_NAME_EXISTS );
223 release_object( obj );
225 if (attributes & OBJ_OPENIF)
226 set_error( STATUS_OBJECT_TYPE_MISMATCH );
228 set_error( STATUS_OBJECT_NAME_COLLISION );
232 if ((obj = create_object( namespace, ops, name, NULL ))) clear_error();
236 /* dump the name of an object to stderr */
237 void dump_object_name( struct object *obj )
239 if (!obj->name) fprintf( stderr, "name=\"\"" );
242 fprintf( stderr, "name=L\"" );
243 dump_strW( obj->name->name, obj->name->len/sizeof(WCHAR), stderr, "\"\"" );
244 fputc( '\"', stderr );
248 /* unlink a named object from its namespace, without freeing the object itself */
249 void unlink_named_object( struct object *obj )
251 if (obj->name) free_name( obj );
255 /* mark an object as being stored statically, i.e. only released at shutdown */
256 void make_object_static( struct object *obj )
259 list_remove( &obj->obj_list );
260 list_add_head( &static_object_list, &obj->obj_list );
264 /* grab an object (i.e. increment its refcount) and return the object */
265 struct object *grab_object( void *ptr )
267 struct object *obj = (struct object *)ptr;
268 assert( obj->refcount < INT_MAX );
273 /* release an object (i.e. decrement its refcount) */
274 void release_object( void *ptr )
276 struct object *obj = (struct object *)ptr;
277 assert( obj->refcount );
278 if (!--obj->refcount)
280 /* if the refcount is 0, nobody can be in the wait queue */
281 assert( list_empty( &obj->wait_queue ));
282 obj->ops->destroy( obj );
283 if (obj->name) free_name( obj );
286 list_remove( &obj->obj_list );
287 memset( obj, 0xaa, obj->ops->size );
293 /* find an object by its name; the refcount is incremented */
294 struct object *find_object( const struct namespace *namespace, const struct unicode_str *name,
295 unsigned int attributes )
297 const struct list *list;
300 if (!name || !name->len) return NULL;
302 list = &namespace->names[ get_name_hash( namespace, name->str, name->len ) ];
303 LIST_FOR_EACH( p, list )
305 const struct object_name *ptr = LIST_ENTRY( p, struct object_name, entry );
306 if (ptr->len != name->len) continue;
307 if (attributes & OBJ_CASE_INSENSITIVE)
309 if (!strncmpiW( ptr->name, name->str, name->len/sizeof(WCHAR) ))
310 return grab_object( ptr->obj );
314 if (!memcmp( ptr->name, name->str, name->len ))
315 return grab_object( ptr->obj );
321 /* find an object by its index; the refcount is incremented */
322 struct object *find_object_index( const struct namespace *namespace, unsigned int index )
326 /* FIXME: not efficient at all */
327 for (i = 0; i < namespace->hash_size; i++)
329 const struct object_name *ptr;
330 LIST_FOR_EACH_ENTRY( ptr, &namespace->names[i], const struct object_name, entry )
332 if (!index--) return grab_object( ptr->obj );
335 set_error( STATUS_NO_MORE_ENTRIES );
339 /* allocate a namespace */
340 struct namespace *create_namespace( unsigned int hash_size )
342 struct namespace *namespace;
345 namespace = mem_alloc( sizeof(*namespace) + (hash_size - 1) * sizeof(namespace->names[0]) );
348 namespace->hash_size = hash_size;
349 for (i = 0; i < hash_size; i++) list_init( &namespace->names[i] );
354 /* functions for unimplemented/default object operations */
356 struct object_type *no_get_type( struct object *obj )
361 int no_add_queue( struct object *obj, struct wait_queue_entry *entry )
363 set_error( STATUS_OBJECT_TYPE_MISMATCH );
367 int no_satisfied( struct object *obj, struct thread *thread )
369 return 0; /* not abandoned */
372 int no_signal( struct object *obj, unsigned int access )
374 set_error( STATUS_OBJECT_TYPE_MISMATCH );
378 struct fd *no_get_fd( struct object *obj )
380 set_error( STATUS_OBJECT_TYPE_MISMATCH );
384 unsigned int no_map_access( struct object *obj, unsigned int access )
386 if (access & GENERIC_READ) access |= STANDARD_RIGHTS_READ;
387 if (access & GENERIC_WRITE) access |= STANDARD_RIGHTS_WRITE;
388 if (access & GENERIC_EXECUTE) access |= STANDARD_RIGHTS_EXECUTE;
389 if (access & GENERIC_ALL) access |= STANDARD_RIGHTS_ALL;
390 return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
393 struct security_descriptor *default_get_sd( struct object *obj )
398 int default_set_sd( struct object *obj, const struct security_descriptor *sd,
399 unsigned int set_info )
401 struct security_descriptor new_sd, *new_sd_ptr;
403 const SID *owner, *group;
404 const ACL *sacl, *dacl;
407 if (!set_info) return 1;
409 new_sd.control = sd->control & ~SE_SELF_RELATIVE;
411 owner = sd_get_owner( sd );
412 if (set_info & OWNER_SECURITY_INFORMATION && owner)
413 new_sd.owner_len = sd->owner_len;
416 owner = token_get_user( current->process->token );
417 new_sd.owner_len = FIELD_OFFSET(SID, SubAuthority[owner->SubAuthorityCount]);
418 new_sd.control |= SE_OWNER_DEFAULTED;
421 group = sd_get_group( sd );
422 if (set_info & GROUP_SECURITY_INFORMATION && group)
423 new_sd.group_len = sd->group_len;
426 group = token_get_primary_group( current->process->token );
427 new_sd.group_len = FIELD_OFFSET(SID, SubAuthority[group->SubAuthorityCount]);
428 new_sd.control |= SE_GROUP_DEFAULTED;
431 new_sd.control |= SE_SACL_PRESENT;
432 sacl = sd_get_sacl( sd, &present );
433 if (set_info & SACL_SECURITY_INFORMATION && present)
434 new_sd.sacl_len = sd->sacl_len;
437 if (obj->sd) sacl = sd_get_sacl( obj->sd, &present );
439 if (obj->sd && present)
440 new_sd.sacl_len = obj->sd->sacl_len;
444 new_sd.control |= SE_SACL_DEFAULTED;
448 new_sd.control |= SE_DACL_PRESENT;
449 dacl = sd_get_dacl( sd, &present );
450 if (set_info & DACL_SECURITY_INFORMATION && present)
451 new_sd.dacl_len = sd->dacl_len;
454 if (obj->sd) dacl = sd_get_dacl( obj->sd, &present );
456 if (obj->sd && present)
457 new_sd.dacl_len = obj->sd->dacl_len;
460 dacl = token_get_default_dacl( current->process->token );
461 new_sd.dacl_len = dacl->AclSize;
462 new_sd.control |= SE_DACL_DEFAULTED;
466 ptr = mem_alloc( sizeof(new_sd) + new_sd.owner_len + new_sd.group_len +
467 new_sd.sacl_len + new_sd.dacl_len );
469 new_sd_ptr = (struct security_descriptor*)ptr;
471 memcpy( ptr, &new_sd, sizeof(new_sd) );
472 ptr += sizeof(new_sd);
473 memcpy( ptr, owner, new_sd.owner_len );
474 ptr += new_sd.owner_len;
475 memcpy( ptr, group, new_sd.group_len );
476 ptr += new_sd.group_len;
477 memcpy( ptr, sacl, new_sd.sacl_len );
478 ptr += new_sd.sacl_len;
479 memcpy( ptr, dacl, new_sd.dacl_len );
482 obj->sd = new_sd_ptr;
486 struct object *no_lookup_name( struct object *obj, struct unicode_str *name,
492 struct object *no_open_file( struct object *obj, unsigned int access, unsigned int sharing,
493 unsigned int options )
495 set_error( STATUS_OBJECT_TYPE_MISMATCH );
499 int no_close_handle( struct object *obj, struct process *process, obj_handle_t handle )
501 return 1; /* ok to close */
504 void no_destroy( struct object *obj )