amstream: Increase parent IAMMediaStream refcount in IDirectDrawMediaStream::CreateSa...
[wine.git] / server / object.c
blob8ec6609f69d57fa9ff3664f88f381e2b97531345
1 /*
2 * Server-side objects
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
21 #include "config.h"
22 #include "wine/port.h"
24 #include <assert.h>
25 #include <limits.h>
26 #include <stdlib.h>
27 #include <stdio.h>
28 #include <string.h>
29 #include <unistd.h>
30 #include <stdarg.h>
31 #ifdef HAVE_VALGRIND_MEMCHECK_H
32 #include <valgrind/memcheck.h>
33 #endif
35 #include "ntstatus.h"
36 #define WIN32_NO_STATUS
37 #include "winternl.h"
39 #include "file.h"
40 #include "process.h"
41 #include "thread.h"
42 #include "unicode.h"
43 #include "security.h"
46 struct namespace
48 unsigned int hash_size; /* size of hash table */
49 struct list names[1]; /* array of hash entry lists */
53 #ifdef DEBUG_OBJECTS
54 static struct list object_list = LIST_INIT(object_list);
55 static struct list static_object_list = LIST_INIT(static_object_list);
57 void dump_objects(void)
59 struct list *p;
61 LIST_FOR_EACH( p, &static_object_list )
63 struct object *ptr = LIST_ENTRY( p, struct object, obj_list );
64 fprintf( stderr, "%p:%d: ", ptr, ptr->refcount );
65 dump_object_name( ptr );
66 ptr->ops->dump( ptr, 1 );
68 LIST_FOR_EACH( p, &object_list )
70 struct object *ptr = LIST_ENTRY( p, struct object, obj_list );
71 fprintf( stderr, "%p:%d: ", ptr, ptr->refcount );
72 dump_object_name( ptr );
73 ptr->ops->dump( ptr, 1 );
77 void close_objects(void)
79 struct list *ptr;
81 /* release the static objects */
82 while ((ptr = list_head( &static_object_list )))
84 struct object *obj = LIST_ENTRY( ptr, struct object, obj_list );
85 /* move it back to the standard list before freeing */
86 list_remove( &obj->obj_list );
87 list_add_head( &object_list, &obj->obj_list );
88 release_object( obj );
91 dump_objects(); /* dump any remaining objects */
94 #endif /* DEBUG_OBJECTS */
96 /*****************************************************************/
98 /* mark a block of memory as uninitialized for debugging purposes */
99 static inline void mark_block_uninitialized( void *ptr, size_t size )
101 memset( ptr, 0x55, size );
102 #if defined(VALGRIND_MAKE_MEM_UNDEFINED)
103 VALGRIND_DISCARD( VALGRIND_MAKE_MEM_UNDEFINED( ptr, size ));
104 #elif defined(VALGRIND_MAKE_WRITABLE)
105 VALGRIND_DISCARD( VALGRIND_MAKE_WRITABLE( ptr, size ));
106 #endif
109 /* malloc replacement */
110 void *mem_alloc( size_t size )
112 void *ptr = malloc( size );
113 if (ptr) mark_block_uninitialized( ptr, size );
114 else set_error( STATUS_NO_MEMORY );
115 return ptr;
118 /* duplicate a block of memory */
119 void *memdup( const void *data, size_t len )
121 void *ptr = malloc( len );
122 if (ptr) memcpy( ptr, data, len );
123 else set_error( STATUS_NO_MEMORY );
124 return ptr;
128 /*****************************************************************/
130 void namespace_add( struct namespace *namespace, struct object_name *ptr )
132 unsigned int hash = hash_strW( ptr->name, ptr->len, namespace->hash_size );
134 list_add_head( &namespace->names[hash], &ptr->entry );
137 /* allocate a name for an object */
138 static struct object_name *alloc_name( const struct unicode_str *name )
140 struct object_name *ptr;
142 if ((ptr = mem_alloc( sizeof(*ptr) + name->len - sizeof(ptr->name) )))
144 ptr->len = name->len;
145 ptr->parent = NULL;
146 memcpy( ptr->name, name->str, name->len );
148 return ptr;
151 /* get the name of an existing object */
152 const WCHAR *get_object_name( struct object *obj, data_size_t *len )
154 struct object_name *ptr = obj->name;
155 if (!ptr) return NULL;
156 *len = ptr->len;
157 return ptr->name;
160 /* get the full path name of an existing object */
161 WCHAR *get_object_full_name( struct object *obj, data_size_t *ret_len )
163 static const WCHAR backslash = '\\';
164 struct object *ptr = obj;
165 data_size_t len = 0;
166 char *ret;
168 while (ptr && ptr->name)
170 struct object_name *name = ptr->name;
171 len += name->len + sizeof(WCHAR);
172 ptr = name->parent;
174 if (!len) return NULL;
175 if (!(ret = malloc( len ))) return NULL;
177 *ret_len = len;
178 while (obj && obj->name)
180 struct object_name *name = obj->name;
181 memcpy( ret + len - name->len, name->name, name->len );
182 len -= name->len + sizeof(WCHAR);
183 memcpy( ret + len, &backslash, sizeof(WCHAR) );
184 obj = name->parent;
186 return (WCHAR *)ret;
189 /* allocate and initialize an object */
190 void *alloc_object( const struct object_ops *ops )
192 struct object *obj = mem_alloc( ops->size );
193 if (obj)
195 obj->refcount = 1;
196 obj->handle_count = 0;
197 obj->ops = ops;
198 obj->name = NULL;
199 obj->sd = NULL;
200 list_init( &obj->wait_queue );
201 #ifdef DEBUG_OBJECTS
202 list_add_head( &object_list, &obj->obj_list );
203 #endif
204 return obj;
206 return NULL;
209 /* free an object once it has been destroyed */
210 static void free_object( struct object *obj )
212 free( obj->sd );
213 #ifdef DEBUG_OBJECTS
214 list_remove( &obj->obj_list );
215 memset( obj, 0xaa, obj->ops->size );
216 #endif
217 free( obj );
220 /* find an object by name starting from the specified root */
221 /* if it doesn't exist, its parent is returned, and name_left contains the remaining name */
222 struct object *lookup_named_object( struct object *root, const struct unicode_str *name,
223 unsigned int attr, struct unicode_str *name_left )
225 struct object *obj, *parent;
226 struct unicode_str name_tmp = *name, *ptr = &name_tmp;
228 if (root)
230 /* if root is specified path shouldn't start with backslash */
231 if (name_tmp.len && name_tmp.str[0] == '\\')
233 set_error( STATUS_OBJECT_PATH_SYNTAX_BAD );
234 return NULL;
236 parent = grab_object( root );
238 else
240 if (!name_tmp.len || name_tmp.str[0] != '\\')
242 set_error( STATUS_OBJECT_PATH_SYNTAX_BAD );
243 return NULL;
245 /* skip leading backslash */
246 name_tmp.str++;
247 name_tmp.len -= sizeof(WCHAR);
248 parent = get_root_directory();
251 if (!name_tmp.len) ptr = NULL; /* special case for empty path */
253 clear_error();
255 while ((obj = parent->ops->lookup_name( parent, ptr, attr )))
257 /* move to the next element */
258 release_object ( parent );
259 parent = obj;
261 if (get_error())
263 release_object( parent );
264 return NULL;
267 if (name_left) *name_left = name_tmp;
268 return parent;
271 /* return length of first path element in name */
272 data_size_t get_path_element( const WCHAR *name, data_size_t len )
274 data_size_t i;
276 for (i = 0; i < len / sizeof(WCHAR); i++) if (name[i] == '\\') break;
277 return i * sizeof(WCHAR);
280 static struct object *create_object( struct object *parent, const struct object_ops *ops,
281 const struct unicode_str *name, unsigned int attributes,
282 const struct security_descriptor *sd )
284 struct object *obj;
285 struct object_name *name_ptr;
287 if (!(name_ptr = alloc_name( name ))) return NULL;
288 if (!(obj = alloc_object( ops ))) goto failed;
289 if (sd && !default_set_sd( obj, sd, OWNER_SECURITY_INFORMATION | GROUP_SECURITY_INFORMATION |
290 DACL_SECURITY_INFORMATION | SACL_SECURITY_INFORMATION ))
291 goto failed;
292 if (!obj->ops->link_name( obj, name_ptr, parent )) goto failed;
294 name_ptr->obj = obj;
295 obj->name = name_ptr;
296 if (attributes & OBJ_PERMANENT)
298 make_object_static( obj );
299 grab_object( obj );
301 return obj;
303 failed:
304 if (obj) free_object( obj );
305 free( name_ptr );
306 return NULL;
309 /* create an object as named child under the specified parent */
310 void *create_named_object( struct object *parent, const struct object_ops *ops,
311 const struct unicode_str *name, unsigned int attributes,
312 const struct security_descriptor *sd )
314 struct object *obj, *new_obj;
315 struct unicode_str new_name;
317 clear_error();
319 if (!name || !name->len)
321 if (!(new_obj = alloc_object( ops ))) return NULL;
322 if (sd && !default_set_sd( new_obj, sd, OWNER_SECURITY_INFORMATION | GROUP_SECURITY_INFORMATION |
323 DACL_SECURITY_INFORMATION | SACL_SECURITY_INFORMATION ))
325 free_object( new_obj );
326 return NULL;
328 return new_obj;
331 if (!(obj = lookup_named_object( parent, name, attributes, &new_name ))) return NULL;
333 if (!new_name.len)
335 if (attributes & OBJ_OPENIF && obj->ops == ops)
336 set_error( STATUS_OBJECT_NAME_EXISTS );
337 else
339 release_object( obj );
340 obj = NULL;
341 if (attributes & OBJ_OPENIF)
342 set_error( STATUS_OBJECT_TYPE_MISMATCH );
343 else
344 set_error( STATUS_OBJECT_NAME_COLLISION );
346 return obj;
349 new_obj = create_object( obj, ops, &new_name, attributes, sd );
350 release_object( obj );
351 return new_obj;
354 /* open a object by name under the specified parent */
355 void *open_named_object( struct object *parent, const struct object_ops *ops,
356 const struct unicode_str *name, unsigned int attributes )
358 struct unicode_str name_left;
359 struct object *obj;
361 if ((obj = lookup_named_object( parent, name, attributes, &name_left )))
363 if (name_left.len) /* not fully parsed */
364 set_error( STATUS_OBJECT_NAME_NOT_FOUND );
365 else if (ops && obj->ops != ops)
366 set_error( STATUS_OBJECT_TYPE_MISMATCH );
367 else
368 return obj;
370 release_object( obj );
372 return NULL;
375 /* recursive helper for dump_object_name */
376 static void dump_name( struct object *obj )
378 struct object_name *name = obj->name;
380 if (!name) return;
381 if (name->parent) dump_name( name->parent );
382 fputs( "\\\\", stderr );
383 dump_strW( name->name, name->len, stderr, "[]" );
386 /* dump the name of an object to stderr */
387 void dump_object_name( struct object *obj )
389 if (!obj->name) return;
390 fputc( '[', stderr );
391 dump_name( obj );
392 fputs( "] ", stderr );
395 /* unlink a named object from its namespace, without freeing the object itself */
396 void unlink_named_object( struct object *obj )
398 struct object_name *name_ptr = obj->name;
400 if (!name_ptr) return;
401 obj->name = NULL;
402 obj->ops->unlink_name( obj, name_ptr );
403 if (name_ptr->parent) release_object( name_ptr->parent );
404 free( name_ptr );
407 /* mark an object as being stored statically, i.e. only released at shutdown */
408 void make_object_static( struct object *obj )
410 obj->is_permanent = 1;
411 #ifdef DEBUG_OBJECTS
412 list_remove( &obj->obj_list );
413 list_add_head( &static_object_list, &obj->obj_list );
414 #endif
417 /* mark an object as no longer static */
418 void make_object_temporary( struct object *obj )
420 obj->is_permanent = 0;
421 #ifdef DEBUG_OBJECTS
422 list_remove( &obj->obj_list );
423 list_add_head( &object_list, &obj->obj_list );
424 #endif
427 /* grab an object (i.e. increment its refcount) and return the object */
428 struct object *grab_object( void *ptr )
430 struct object *obj = (struct object *)ptr;
431 assert( obj->refcount < INT_MAX );
432 obj->refcount++;
433 return obj;
436 /* release an object (i.e. decrement its refcount) */
437 void release_object( void *ptr )
439 struct object *obj = (struct object *)ptr;
440 assert( obj->refcount );
441 if (!--obj->refcount)
443 assert( !obj->handle_count );
444 /* if the refcount is 0, nobody can be in the wait queue */
445 assert( list_empty( &obj->wait_queue ));
446 free_kernel_objects( obj );
447 unlink_named_object( obj );
448 obj->ops->destroy( obj );
449 free_object( obj );
453 /* find an object by its name; the refcount is incremented */
454 struct object *find_object( const struct namespace *namespace, const struct unicode_str *name,
455 unsigned int attributes )
457 const struct list *list;
458 struct list *p;
460 if (!name || !name->len) return NULL;
462 list = &namespace->names[ hash_strW( name->str, name->len, namespace->hash_size ) ];
463 LIST_FOR_EACH( p, list )
465 const struct object_name *ptr = LIST_ENTRY( p, struct object_name, entry );
466 if (ptr->len != name->len) continue;
467 if (attributes & OBJ_CASE_INSENSITIVE)
469 if (!memicmp_strW( ptr->name, name->str, name->len ))
470 return grab_object( ptr->obj );
472 else
474 if (!memcmp( ptr->name, name->str, name->len ))
475 return grab_object( ptr->obj );
478 return NULL;
481 /* find an object by its index; the refcount is incremented */
482 struct object *find_object_index( const struct namespace *namespace, unsigned int index )
484 unsigned int i;
486 /* FIXME: not efficient at all */
487 for (i = 0; i < namespace->hash_size; i++)
489 const struct object_name *ptr;
490 LIST_FOR_EACH_ENTRY( ptr, &namespace->names[i], const struct object_name, entry )
492 if (!index--) return grab_object( ptr->obj );
495 set_error( STATUS_NO_MORE_ENTRIES );
496 return NULL;
499 /* allocate a namespace */
500 struct namespace *create_namespace( unsigned int hash_size )
502 struct namespace *namespace;
503 unsigned int i;
505 namespace = mem_alloc( sizeof(*namespace) + (hash_size - 1) * sizeof(namespace->names[0]) );
506 if (namespace)
508 namespace->hash_size = hash_size;
509 for (i = 0; i < hash_size; i++) list_init( &namespace->names[i] );
511 return namespace;
514 /* functions for unimplemented/default object operations */
516 struct object_type *no_get_type( struct object *obj )
518 return NULL;
521 int no_add_queue( struct object *obj, struct wait_queue_entry *entry )
523 set_error( STATUS_OBJECT_TYPE_MISMATCH );
524 return 0;
527 void no_satisfied( struct object *obj, struct wait_queue_entry *entry )
531 int no_signal( struct object *obj, unsigned int access )
533 set_error( STATUS_OBJECT_TYPE_MISMATCH );
534 return 0;
537 struct fd *no_get_fd( struct object *obj )
539 set_error( STATUS_OBJECT_TYPE_MISMATCH );
540 return NULL;
543 unsigned int no_map_access( struct object *obj, unsigned int access )
545 if (access & GENERIC_READ) access |= STANDARD_RIGHTS_READ;
546 if (access & GENERIC_WRITE) access |= STANDARD_RIGHTS_WRITE;
547 if (access & GENERIC_EXECUTE) access |= STANDARD_RIGHTS_EXECUTE;
548 if (access & GENERIC_ALL) access |= STANDARD_RIGHTS_ALL;
549 return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
552 struct security_descriptor *default_get_sd( struct object *obj )
554 return obj->sd;
557 int set_sd_defaults_from_token( struct object *obj, const struct security_descriptor *sd,
558 unsigned int set_info, struct token *token )
560 struct security_descriptor new_sd, *new_sd_ptr;
561 int present;
562 const SID *owner = NULL, *group = NULL;
563 const ACL *sacl, *dacl;
564 ACL *replaced_sacl = NULL;
565 char *ptr;
567 if (!set_info) return 1;
569 new_sd.control = sd->control & ~SE_SELF_RELATIVE;
571 if (set_info & OWNER_SECURITY_INFORMATION && sd->owner_len)
573 owner = sd_get_owner( sd );
574 new_sd.owner_len = sd->owner_len;
576 else if (obj->sd && obj->sd->owner_len)
578 owner = sd_get_owner( obj->sd );
579 new_sd.owner_len = obj->sd->owner_len;
581 else if (token)
583 owner = token_get_user( token );
584 new_sd.owner_len = security_sid_len( owner );
586 else new_sd.owner_len = 0;
588 if (set_info & GROUP_SECURITY_INFORMATION && sd->group_len)
590 group = sd_get_group( sd );
591 new_sd.group_len = sd->group_len;
593 else if (obj->sd && obj->sd->group_len)
595 group = sd_get_group( obj->sd );
596 new_sd.group_len = obj->sd->group_len;
598 else if (token)
600 group = token_get_primary_group( token );
601 new_sd.group_len = security_sid_len( group );
603 else new_sd.group_len = 0;
605 sacl = sd_get_sacl( sd, &present );
606 if (set_info & SACL_SECURITY_INFORMATION && present)
608 new_sd.control |= SE_SACL_PRESENT;
609 new_sd.sacl_len = sd->sacl_len;
611 else if (set_info & LABEL_SECURITY_INFORMATION && present)
613 const ACL *old_sacl = NULL;
614 if (obj->sd && obj->sd->control & SE_SACL_PRESENT) old_sacl = sd_get_sacl( obj->sd, &present );
615 if (!(replaced_sacl = replace_security_labels( old_sacl, sacl ))) return 0;
616 new_sd.control |= SE_SACL_PRESENT;
617 new_sd.sacl_len = replaced_sacl->AclSize;
618 sacl = replaced_sacl;
620 else
622 if (obj->sd) sacl = sd_get_sacl( obj->sd, &present );
624 if (obj->sd && present)
626 new_sd.control |= SE_SACL_PRESENT;
627 new_sd.sacl_len = obj->sd->sacl_len;
629 else
630 new_sd.sacl_len = 0;
633 dacl = sd_get_dacl( sd, &present );
634 if (set_info & DACL_SECURITY_INFORMATION && present)
636 new_sd.control |= SE_DACL_PRESENT;
637 new_sd.dacl_len = sd->dacl_len;
639 else
641 if (obj->sd) dacl = sd_get_dacl( obj->sd, &present );
643 if (obj->sd && present)
645 new_sd.control |= SE_DACL_PRESENT;
646 new_sd.dacl_len = obj->sd->dacl_len;
648 else if (token)
650 dacl = token_get_default_dacl( token );
651 new_sd.control |= SE_DACL_PRESENT;
652 new_sd.dacl_len = dacl->AclSize;
654 else new_sd.dacl_len = 0;
657 ptr = mem_alloc( sizeof(new_sd) + new_sd.owner_len + new_sd.group_len +
658 new_sd.sacl_len + new_sd.dacl_len );
659 if (!ptr)
661 free( replaced_sacl );
662 return 0;
664 new_sd_ptr = (struct security_descriptor*)ptr;
666 memcpy( ptr, &new_sd, sizeof(new_sd) );
667 ptr += sizeof(new_sd);
668 memcpy( ptr, owner, new_sd.owner_len );
669 ptr += new_sd.owner_len;
670 memcpy( ptr, group, new_sd.group_len );
671 ptr += new_sd.group_len;
672 memcpy( ptr, sacl, new_sd.sacl_len );
673 ptr += new_sd.sacl_len;
674 memcpy( ptr, dacl, new_sd.dacl_len );
676 free( replaced_sacl );
677 free( obj->sd );
678 obj->sd = new_sd_ptr;
679 return 1;
682 /** Set the security descriptor using the current primary token for defaults. */
683 int default_set_sd( struct object *obj, const struct security_descriptor *sd,
684 unsigned int set_info )
686 return set_sd_defaults_from_token( obj, sd, set_info, current->process->token );
689 struct object *no_lookup_name( struct object *obj, struct unicode_str *name,
690 unsigned int attr )
692 if (!name) set_error( STATUS_OBJECT_TYPE_MISMATCH );
693 return NULL;
696 int no_link_name( struct object *obj, struct object_name *name, struct object *parent )
698 set_error( STATUS_OBJECT_TYPE_MISMATCH );
699 return 0;
702 void default_unlink_name( struct object *obj, struct object_name *name )
704 list_remove( &name->entry );
707 struct object *no_open_file( struct object *obj, unsigned int access, unsigned int sharing,
708 unsigned int options )
710 set_error( STATUS_OBJECT_TYPE_MISMATCH );
711 return NULL;
714 int no_close_handle( struct object *obj, struct process *process, obj_handle_t handle )
716 return 1; /* ok to close */
719 void no_destroy( struct object *obj )