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"
31 #ifdef HAVE_VALGRIND_MEMCHECK_H
32 #include <valgrind/memcheck.h>
36 #define WIN32_NO_STATUS
48 unsigned int hash_size
; /* size of hash table */
49 struct list names
[1]; /* array of hash entry lists */
54 static struct list object_list
= LIST_INIT(object_list
);
56 void dump_objects(void)
60 LIST_FOR_EACH_ENTRY( ptr
, &object_list
, struct object
, obj_list
)
62 fprintf( stderr
, "%p:%d: ", ptr
, ptr
->refcount
);
63 dump_object_name( ptr
);
64 ptr
->ops
->dump( ptr
, 1 );
68 void close_objects(void)
70 /* release the permanent objects */
76 LIST_FOR_EACH_ENTRY( obj
, &object_list
, struct object
, obj_list
)
78 if (!(found
= obj
->is_permanent
)) continue;
79 obj
->is_permanent
= 0;
80 release_object( obj
);
86 dump_objects(); /* dump any remaining objects */
89 #endif /* DEBUG_OBJECTS */
91 /*****************************************************************/
93 /* mark a block of memory as uninitialized for debugging purposes */
94 static inline void mark_block_uninitialized( void *ptr
, size_t size
)
96 memset( ptr
, 0x55, size
);
97 #if defined(VALGRIND_MAKE_MEM_UNDEFINED)
98 VALGRIND_DISCARD( VALGRIND_MAKE_MEM_UNDEFINED( ptr
, size
));
99 #elif defined(VALGRIND_MAKE_WRITABLE)
100 VALGRIND_DISCARD( VALGRIND_MAKE_WRITABLE( ptr
, size
));
104 /* malloc replacement */
105 void *mem_alloc( size_t size
)
107 void *ptr
= malloc( size
);
108 if (ptr
) mark_block_uninitialized( ptr
, size
);
109 else set_error( STATUS_NO_MEMORY
);
113 /* duplicate a block of memory */
114 void *memdup( const void *data
, size_t len
)
116 void *ptr
= malloc( len
);
117 if (ptr
) memcpy( ptr
, data
, len
);
118 else set_error( STATUS_NO_MEMORY
);
123 /*****************************************************************/
125 void namespace_add( struct namespace *namespace, struct object_name
*ptr
)
127 unsigned int hash
= hash_strW( ptr
->name
, ptr
->len
, namespace->hash_size
);
129 list_add_head( &namespace->names
[hash
], &ptr
->entry
);
132 /* allocate a name for an object */
133 static struct object_name
*alloc_name( const struct unicode_str
*name
)
135 struct object_name
*ptr
;
137 if ((ptr
= mem_alloc( sizeof(*ptr
) + name
->len
- sizeof(ptr
->name
) )))
139 ptr
->len
= name
->len
;
141 memcpy( ptr
->name
, name
->str
, name
->len
);
146 /* get the name of an existing object */
147 const WCHAR
*get_object_name( struct object
*obj
, data_size_t
*len
)
149 struct object_name
*ptr
= obj
->name
;
150 if (!ptr
) return NULL
;
155 /* get the full path name of an existing object */
156 WCHAR
*default_get_full_name( struct object
*obj
, data_size_t
*ret_len
)
158 static const WCHAR backslash
= '\\';
159 struct object
*ptr
= obj
;
163 while (ptr
&& ptr
->name
)
165 struct object_name
*name
= ptr
->name
;
166 len
+= name
->len
+ sizeof(WCHAR
);
169 if (!len
) return NULL
;
170 if (!(ret
= malloc( len
))) return NULL
;
173 while (obj
&& obj
->name
)
175 struct object_name
*name
= obj
->name
;
176 memcpy( ret
+ len
- name
->len
, name
->name
, name
->len
);
177 len
-= name
->len
+ sizeof(WCHAR
);
178 memcpy( ret
+ len
, &backslash
, sizeof(WCHAR
) );
184 /* allocate and initialize an object */
185 void *alloc_object( const struct object_ops
*ops
)
187 struct object
*obj
= mem_alloc( ops
->size
);
191 obj
->handle_count
= 0;
192 obj
->is_permanent
= 0;
196 list_init( &obj
->wait_queue
);
198 list_add_head( &object_list
, &obj
->obj_list
);
205 /* free an object once it has been destroyed */
206 static void free_object( struct object
*obj
)
210 list_remove( &obj
->obj_list
);
211 memset( obj
, 0xaa, obj
->ops
->size
);
216 /* find an object by name starting from the specified root */
217 /* if it doesn't exist, its parent is returned, and name_left contains the remaining name */
218 struct object
*lookup_named_object( struct object
*root
, const struct unicode_str
*name
,
219 unsigned int attr
, struct unicode_str
*name_left
)
221 struct object
*obj
, *parent
;
222 struct unicode_str name_tmp
= *name
, *ptr
= &name_tmp
;
226 /* if root is specified path shouldn't start with backslash */
227 if (name_tmp
.len
&& name_tmp
.str
[0] == '\\')
229 set_error( STATUS_OBJECT_PATH_SYNTAX_BAD
);
232 parent
= grab_object( root
);
236 if (!name_tmp
.len
|| name_tmp
.str
[0] != '\\')
238 set_error( STATUS_OBJECT_PATH_SYNTAX_BAD
);
241 /* skip leading backslash */
243 name_tmp
.len
-= sizeof(WCHAR
);
244 parent
= get_root_directory();
247 if (!name_tmp
.len
) ptr
= NULL
; /* special case for empty path */
251 while ((obj
= parent
->ops
->lookup_name( parent
, ptr
, attr
)))
253 /* move to the next element */
254 release_object ( parent
);
259 release_object( parent
);
263 if (name_left
) *name_left
= name_tmp
;
267 /* return length of first path element in name */
268 data_size_t
get_path_element( const WCHAR
*name
, data_size_t len
)
272 for (i
= 0; i
< len
/ sizeof(WCHAR
); i
++) if (name
[i
] == '\\') break;
273 return i
* sizeof(WCHAR
);
276 static struct object
*create_object( struct object
*parent
, const struct object_ops
*ops
,
277 const struct unicode_str
*name
, unsigned int attributes
,
278 const struct security_descriptor
*sd
)
281 struct object_name
*name_ptr
;
283 if (!(name_ptr
= alloc_name( name
))) return NULL
;
284 if (!(obj
= alloc_object( ops
))) goto failed
;
285 if (sd
&& !default_set_sd( obj
, sd
, OWNER_SECURITY_INFORMATION
| GROUP_SECURITY_INFORMATION
|
286 DACL_SECURITY_INFORMATION
| SACL_SECURITY_INFORMATION
))
288 if (!obj
->ops
->link_name( obj
, name_ptr
, parent
)) goto failed
;
291 obj
->name
= name_ptr
;
295 if (obj
) free_object( obj
);
300 /* create an object as named child under the specified parent */
301 void *create_named_object( struct object
*parent
, const struct object_ops
*ops
,
302 const struct unicode_str
*name
, unsigned int attributes
,
303 const struct security_descriptor
*sd
)
305 struct object
*obj
, *new_obj
;
306 struct unicode_str new_name
;
310 if (!name
|| !name
->len
)
312 if (!(new_obj
= alloc_object( ops
))) return NULL
;
313 if (sd
&& !default_set_sd( new_obj
, sd
, OWNER_SECURITY_INFORMATION
| GROUP_SECURITY_INFORMATION
|
314 DACL_SECURITY_INFORMATION
| SACL_SECURITY_INFORMATION
))
316 free_object( new_obj
);
322 if (!(obj
= lookup_named_object( parent
, name
, attributes
, &new_name
))) return NULL
;
326 if (attributes
& OBJ_OPENIF
&& obj
->ops
== ops
)
327 set_error( STATUS_OBJECT_NAME_EXISTS
);
330 release_object( obj
);
332 if (attributes
& OBJ_OPENIF
)
333 set_error( STATUS_OBJECT_TYPE_MISMATCH
);
335 set_error( STATUS_OBJECT_NAME_COLLISION
);
340 new_obj
= create_object( obj
, ops
, &new_name
, attributes
, sd
);
341 release_object( obj
);
344 if (attributes
& OBJ_PERMANENT
)
346 make_object_permanent( new_obj
);
347 grab_object( new_obj
);
352 /* open a object by name under the specified parent */
353 void *open_named_object( struct object
*parent
, const struct object_ops
*ops
,
354 const struct unicode_str
*name
, unsigned int attributes
)
356 struct unicode_str name_left
;
359 if ((obj
= lookup_named_object( parent
, name
, attributes
, &name_left
)))
361 if (name_left
.len
) /* not fully parsed */
362 set_error( STATUS_OBJECT_NAME_NOT_FOUND
);
363 else if (ops
&& obj
->ops
!= ops
)
364 set_error( STATUS_OBJECT_TYPE_MISMATCH
);
368 release_object( obj
);
373 /* recursive helper for dump_object_name */
374 static void dump_name( struct object
*obj
)
376 struct object_name
*name
= obj
->name
;
379 if (name
->parent
) dump_name( name
->parent
);
380 fputs( "\\\\", stderr
);
381 dump_strW( name
->name
, name
->len
, stderr
, "[]" );
384 /* dump the name of an object to stderr */
385 void dump_object_name( struct object
*obj
)
387 if (!obj
->name
) return;
388 fputc( '[', stderr
);
390 fputs( "] ", stderr
);
393 /* unlink a named object from its namespace, without freeing the object itself */
394 void unlink_named_object( struct object
*obj
)
396 struct object_name
*name_ptr
= obj
->name
;
398 if (!name_ptr
) return;
400 obj
->ops
->unlink_name( obj
, name_ptr
);
401 if (name_ptr
->parent
) release_object( name_ptr
->parent
);
405 /* grab an object (i.e. increment its refcount) and return the object */
406 struct object
*grab_object( void *ptr
)
408 struct object
*obj
= (struct object
*)ptr
;
409 assert( obj
->refcount
< INT_MAX
);
414 /* release an object (i.e. decrement its refcount) */
415 void release_object( void *ptr
)
417 struct object
*obj
= (struct object
*)ptr
;
418 assert( obj
->refcount
);
419 if (!--obj
->refcount
)
421 assert( !obj
->handle_count
);
422 /* if the refcount is 0, nobody can be in the wait queue */
423 assert( list_empty( &obj
->wait_queue
));
424 free_kernel_objects( obj
);
425 unlink_named_object( obj
);
426 obj
->ops
->destroy( obj
);
431 /* find an object by its name; the refcount is incremented */
432 struct object
*find_object( const struct namespace *namespace, const struct unicode_str
*name
,
433 unsigned int attributes
)
435 const struct list
*list
;
438 if (!name
|| !name
->len
) return NULL
;
440 list
= &namespace->names
[ hash_strW( name
->str
, name
->len
, namespace->hash_size
) ];
441 LIST_FOR_EACH( p
, list
)
443 const struct object_name
*ptr
= LIST_ENTRY( p
, struct object_name
, entry
);
444 if (ptr
->len
!= name
->len
) continue;
445 if (attributes
& OBJ_CASE_INSENSITIVE
)
447 if (!memicmp_strW( ptr
->name
, name
->str
, name
->len
))
448 return grab_object( ptr
->obj
);
452 if (!memcmp( ptr
->name
, name
->str
, name
->len
))
453 return grab_object( ptr
->obj
);
459 /* find an object by its index; the refcount is incremented */
460 struct object
*find_object_index( const struct namespace *namespace, unsigned int index
)
464 /* FIXME: not efficient at all */
465 for (i
= 0; i
< namespace->hash_size
; i
++)
467 const struct object_name
*ptr
;
468 LIST_FOR_EACH_ENTRY( ptr
, &namespace->names
[i
], const struct object_name
, entry
)
470 if (!index
--) return grab_object( ptr
->obj
);
473 set_error( STATUS_NO_MORE_ENTRIES
);
477 /* allocate a namespace */
478 struct namespace *create_namespace( unsigned int hash_size
)
480 struct namespace *namespace;
483 namespace = mem_alloc( sizeof(*namespace) + (hash_size
- 1) * sizeof(namespace->names
[0]) );
486 namespace->hash_size
= hash_size
;
487 for (i
= 0; i
< hash_size
; i
++) list_init( &namespace->names
[i
] );
492 /* functions for unimplemented/default object operations */
494 struct object_type
*no_get_type( struct object
*obj
)
499 int no_add_queue( struct object
*obj
, struct wait_queue_entry
*entry
)
501 set_error( STATUS_OBJECT_TYPE_MISMATCH
);
505 void no_satisfied( struct object
*obj
, struct wait_queue_entry
*entry
)
509 int no_signal( struct object
*obj
, unsigned int access
)
511 set_error( STATUS_OBJECT_TYPE_MISMATCH
);
515 struct fd
*no_get_fd( struct object
*obj
)
517 set_error( STATUS_OBJECT_TYPE_MISMATCH
);
521 unsigned int no_map_access( struct object
*obj
, unsigned int access
)
523 if (access
& GENERIC_READ
) access
|= STANDARD_RIGHTS_READ
;
524 if (access
& GENERIC_WRITE
) access
|= STANDARD_RIGHTS_WRITE
;
525 if (access
& GENERIC_EXECUTE
) access
|= STANDARD_RIGHTS_EXECUTE
;
526 if (access
& GENERIC_ALL
) access
|= STANDARD_RIGHTS_ALL
;
527 return access
& ~(GENERIC_READ
| GENERIC_WRITE
| GENERIC_EXECUTE
| GENERIC_ALL
);
530 struct security_descriptor
*default_get_sd( struct object
*obj
)
535 int set_sd_defaults_from_token( struct object
*obj
, const struct security_descriptor
*sd
,
536 unsigned int set_info
, struct token
*token
)
538 struct security_descriptor new_sd
, *new_sd_ptr
;
540 const SID
*owner
= NULL
, *group
= NULL
;
541 const ACL
*sacl
, *dacl
;
542 ACL
*replaced_sacl
= NULL
;
545 if (!set_info
) return 1;
547 new_sd
.control
= sd
->control
& ~SE_SELF_RELATIVE
;
549 if (set_info
& OWNER_SECURITY_INFORMATION
&& sd
->owner_len
)
551 owner
= sd_get_owner( sd
);
552 new_sd
.owner_len
= sd
->owner_len
;
554 else if (obj
->sd
&& obj
->sd
->owner_len
)
556 owner
= sd_get_owner( obj
->sd
);
557 new_sd
.owner_len
= obj
->sd
->owner_len
;
561 owner
= token_get_user( token
);
562 new_sd
.owner_len
= security_sid_len( owner
);
564 else new_sd
.owner_len
= 0;
566 if (set_info
& GROUP_SECURITY_INFORMATION
&& sd
->group_len
)
568 group
= sd_get_group( sd
);
569 new_sd
.group_len
= sd
->group_len
;
571 else if (obj
->sd
&& obj
->sd
->group_len
)
573 group
= sd_get_group( obj
->sd
);
574 new_sd
.group_len
= obj
->sd
->group_len
;
578 group
= token_get_primary_group( token
);
579 new_sd
.group_len
= security_sid_len( group
);
581 else new_sd
.group_len
= 0;
583 sacl
= sd_get_sacl( sd
, &present
);
584 if (set_info
& SACL_SECURITY_INFORMATION
&& present
)
586 new_sd
.control
|= SE_SACL_PRESENT
;
587 new_sd
.sacl_len
= sd
->sacl_len
;
589 else if (set_info
& LABEL_SECURITY_INFORMATION
&& present
)
591 const ACL
*old_sacl
= NULL
;
592 if (obj
->sd
&& obj
->sd
->control
& SE_SACL_PRESENT
) old_sacl
= sd_get_sacl( obj
->sd
, &present
);
593 if (!(replaced_sacl
= replace_security_labels( old_sacl
, sacl
))) return 0;
594 new_sd
.control
|= SE_SACL_PRESENT
;
595 new_sd
.sacl_len
= replaced_sacl
->AclSize
;
596 sacl
= replaced_sacl
;
600 if (obj
->sd
) sacl
= sd_get_sacl( obj
->sd
, &present
);
602 if (obj
->sd
&& present
)
604 new_sd
.control
|= SE_SACL_PRESENT
;
605 new_sd
.sacl_len
= obj
->sd
->sacl_len
;
611 dacl
= sd_get_dacl( sd
, &present
);
612 if (set_info
& DACL_SECURITY_INFORMATION
&& present
)
614 new_sd
.control
|= SE_DACL_PRESENT
;
615 new_sd
.dacl_len
= sd
->dacl_len
;
619 if (obj
->sd
) dacl
= sd_get_dacl( obj
->sd
, &present
);
621 if (obj
->sd
&& present
)
623 new_sd
.control
|= SE_DACL_PRESENT
;
624 new_sd
.dacl_len
= obj
->sd
->dacl_len
;
628 dacl
= token_get_default_dacl( token
);
629 new_sd
.control
|= SE_DACL_PRESENT
;
630 new_sd
.dacl_len
= dacl
->AclSize
;
632 else new_sd
.dacl_len
= 0;
635 ptr
= mem_alloc( sizeof(new_sd
) + new_sd
.owner_len
+ new_sd
.group_len
+
636 new_sd
.sacl_len
+ new_sd
.dacl_len
);
639 free( replaced_sacl
);
642 new_sd_ptr
= (struct security_descriptor
*)ptr
;
644 memcpy( ptr
, &new_sd
, sizeof(new_sd
) );
645 ptr
+= sizeof(new_sd
);
646 memcpy( ptr
, owner
, new_sd
.owner_len
);
647 ptr
+= new_sd
.owner_len
;
648 memcpy( ptr
, group
, new_sd
.group_len
);
649 ptr
+= new_sd
.group_len
;
650 memcpy( ptr
, sacl
, new_sd
.sacl_len
);
651 ptr
+= new_sd
.sacl_len
;
652 memcpy( ptr
, dacl
, new_sd
.dacl_len
);
654 free( replaced_sacl
);
656 obj
->sd
= new_sd_ptr
;
660 /** Set the security descriptor using the current primary token for defaults. */
661 int default_set_sd( struct object
*obj
, const struct security_descriptor
*sd
,
662 unsigned int set_info
)
664 return set_sd_defaults_from_token( obj
, sd
, set_info
, current
->process
->token
);
667 WCHAR
*no_get_full_name( struct object
*obj
, data_size_t
*ret_len
)
672 struct object
*no_lookup_name( struct object
*obj
, struct unicode_str
*name
,
675 if (!name
) set_error( STATUS_OBJECT_TYPE_MISMATCH
);
679 int no_link_name( struct object
*obj
, struct object_name
*name
, struct object
*parent
)
681 set_error( STATUS_OBJECT_TYPE_MISMATCH
);
685 void default_unlink_name( struct object
*obj
, struct object_name
*name
)
687 list_remove( &name
->entry
);
690 struct object
*no_open_file( struct object
*obj
, unsigned int access
, unsigned int sharing
,
691 unsigned int options
)
693 set_error( STATUS_OBJECT_TYPE_MISMATCH
);
697 int no_close_handle( struct object
*obj
, struct process
*process
, obj_handle_t handle
)
699 return 1; /* ok to close */
702 void no_destroy( struct object
*obj
)