winebus.sys: Fix ball value reporting.
[wine.git] / server / object.c
blob048da504ad2c5b46855356b9ec1b23f8491b8916
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 static int get_name_hash( const struct namespace *namespace, const WCHAR *name, data_size_t len )
132 WCHAR hash = 0;
133 len /= sizeof(WCHAR);
134 while (len--) hash ^= tolowerW(*name++);
135 return hash % namespace->hash_size;
138 void namespace_add( struct namespace *namespace, struct object_name *ptr )
140 int hash = get_name_hash( namespace, ptr->name, ptr->len );
142 list_add_head( &namespace->names[hash], &ptr->entry );
145 /* allocate a name for an object */
146 static struct object_name *alloc_name( const struct unicode_str *name )
148 struct object_name *ptr;
150 if ((ptr = mem_alloc( sizeof(*ptr) + name->len - sizeof(ptr->name) )))
152 ptr->len = name->len;
153 ptr->parent = NULL;
154 memcpy( ptr->name, name->str, name->len );
156 return ptr;
159 /* get the name of an existing object */
160 const WCHAR *get_object_name( struct object *obj, data_size_t *len )
162 struct object_name *ptr = obj->name;
163 if (!ptr) return NULL;
164 *len = ptr->len;
165 return ptr->name;
168 /* get the full path name of an existing object */
169 WCHAR *get_object_full_name( struct object *obj, data_size_t *ret_len )
171 static const WCHAR backslash = '\\';
172 struct object *ptr = obj;
173 data_size_t len = 0;
174 char *ret;
176 while (ptr && ptr->name)
178 struct object_name *name = ptr->name;
179 len += name->len + sizeof(WCHAR);
180 ptr = name->parent;
182 if (!len) return NULL;
183 if (!(ret = malloc( len ))) return NULL;
185 *ret_len = len;
186 while (obj && obj->name)
188 struct object_name *name = obj->name;
189 memcpy( ret + len - name->len, name->name, name->len );
190 len -= name->len + sizeof(WCHAR);
191 memcpy( ret + len, &backslash, sizeof(WCHAR) );
192 obj = name->parent;
194 return (WCHAR *)ret;
197 /* allocate and initialize an object */
198 void *alloc_object( const struct object_ops *ops )
200 struct object *obj = mem_alloc( ops->size );
201 if (obj)
203 obj->refcount = 1;
204 obj->handle_count = 0;
205 obj->ops = ops;
206 obj->name = NULL;
207 obj->sd = NULL;
208 list_init( &obj->wait_queue );
209 #ifdef DEBUG_OBJECTS
210 list_add_head( &object_list, &obj->obj_list );
211 #endif
212 return obj;
214 return NULL;
217 /* free an object once it has been destroyed */
218 void free_object( struct object *obj )
220 free( obj->sd );
221 #ifdef DEBUG_OBJECTS
222 list_remove( &obj->obj_list );
223 memset( obj, 0xaa, obj->ops->size );
224 #endif
225 free( obj );
228 /* find an object by name starting from the specified root */
229 /* if it doesn't exist, its parent is returned, and name_left contains the remaining name */
230 struct object *lookup_named_object( struct object *root, const struct unicode_str *name,
231 unsigned int attr, struct unicode_str *name_left )
233 struct object *obj, *parent;
234 struct unicode_str name_tmp = *name, *ptr = &name_tmp;
236 if (root)
238 /* if root is specified path shouldn't start with backslash */
239 if (name_tmp.len && name_tmp.str[0] == '\\')
241 set_error( STATUS_OBJECT_PATH_SYNTAX_BAD );
242 return NULL;
244 parent = grab_object( root );
246 else
248 if (!name_tmp.len || name_tmp.str[0] != '\\')
250 set_error( STATUS_OBJECT_PATH_SYNTAX_BAD );
251 return NULL;
253 /* skip leading backslash */
254 name_tmp.str++;
255 name_tmp.len -= sizeof(WCHAR);
256 parent = get_root_directory();
259 if (!name_tmp.len) ptr = NULL; /* special case for empty path */
261 clear_error();
263 while ((obj = parent->ops->lookup_name( parent, ptr, attr )))
265 /* move to the next element */
266 release_object ( parent );
267 parent = obj;
269 if (get_error())
271 release_object( parent );
272 return NULL;
275 if (name_left) *name_left = name_tmp;
276 return parent;
279 static struct object *create_object( struct object *parent, const struct object_ops *ops,
280 const struct unicode_str *name, const struct security_descriptor *sd )
282 struct object *obj;
283 struct object_name *name_ptr;
285 if (!(name_ptr = alloc_name( name ))) return NULL;
286 if (!(obj = alloc_object( ops ))) goto failed;
287 if (sd && !default_set_sd( obj, sd, OWNER_SECURITY_INFORMATION | GROUP_SECURITY_INFORMATION |
288 DACL_SECURITY_INFORMATION | SACL_SECURITY_INFORMATION ))
289 goto failed;
290 if (!obj->ops->link_name( obj, name_ptr, parent )) goto failed;
292 name_ptr->obj = obj;
293 obj->name = name_ptr;
294 return obj;
296 failed:
297 if (obj) free_object( obj );
298 free( name_ptr );
299 return NULL;
302 /* create an object as named child under the specified parent */
303 void *create_named_object( struct object *parent, const struct object_ops *ops,
304 const struct unicode_str *name, unsigned int attributes,
305 const struct security_descriptor *sd )
307 struct object *obj, *new_obj;
308 struct unicode_str new_name;
310 clear_error();
312 if (!name || !name->len)
314 if (!(new_obj = alloc_object( ops ))) return NULL;
315 if (sd && !default_set_sd( new_obj, sd, OWNER_SECURITY_INFORMATION | GROUP_SECURITY_INFORMATION |
316 DACL_SECURITY_INFORMATION | SACL_SECURITY_INFORMATION ))
318 free_object( new_obj );
319 return NULL;
321 return new_obj;
324 if (!(obj = lookup_named_object( parent, name, attributes, &new_name ))) return NULL;
326 if (!new_name.len)
328 if (attributes & OBJ_OPENIF && obj->ops == ops)
329 set_error( STATUS_OBJECT_NAME_EXISTS );
330 else
332 release_object( obj );
333 obj = NULL;
334 if (attributes & OBJ_OPENIF)
335 set_error( STATUS_OBJECT_TYPE_MISMATCH );
336 else
337 set_error( STATUS_OBJECT_NAME_COLLISION );
339 return obj;
342 new_obj = create_object( obj, ops, &new_name, sd );
343 release_object( obj );
344 return new_obj;
347 /* open a object by name under the specified parent */
348 void *open_named_object( struct object *parent, const struct object_ops *ops,
349 const struct unicode_str *name, unsigned int attributes )
351 struct unicode_str name_left;
352 struct object *obj;
354 if ((obj = lookup_named_object( parent, name, attributes, &name_left )))
356 if (name_left.len) /* not fully parsed */
357 set_error( STATUS_OBJECT_NAME_NOT_FOUND );
358 else if (ops && obj->ops != ops)
359 set_error( STATUS_OBJECT_TYPE_MISMATCH );
360 else
361 return obj;
363 release_object( obj );
365 return NULL;
368 /* recursive helper for dump_object_name */
369 static void dump_name( struct object *obj )
371 struct object_name *name = obj->name;
373 if (!name) return;
374 if (name->parent) dump_name( name->parent );
375 fputs( "\\\\", stderr );
376 dump_strW( name->name, name->len / sizeof(WCHAR), stderr, "[]" );
379 /* dump the name of an object to stderr */
380 void dump_object_name( struct object *obj )
382 if (!obj->name) return;
383 fputc( '[', stderr );
384 dump_name( obj );
385 fputs( "] ", stderr );
388 /* unlink a named object from its namespace, without freeing the object itself */
389 void unlink_named_object( struct object *obj )
391 struct object_name *name_ptr = obj->name;
393 if (!name_ptr) return;
394 obj->name = NULL;
395 obj->ops->unlink_name( obj, name_ptr );
396 if (name_ptr->parent) release_object( name_ptr->parent );
397 free( name_ptr );
400 /* mark an object as being stored statically, i.e. only released at shutdown */
401 void make_object_static( struct object *obj )
403 #ifdef DEBUG_OBJECTS
404 list_remove( &obj->obj_list );
405 list_add_head( &static_object_list, &obj->obj_list );
406 #endif
409 /* grab an object (i.e. increment its refcount) and return the object */
410 struct object *grab_object( void *ptr )
412 struct object *obj = (struct object *)ptr;
413 assert( obj->refcount < INT_MAX );
414 obj->refcount++;
415 return obj;
418 /* release an object (i.e. decrement its refcount) */
419 void release_object( void *ptr )
421 struct object *obj = (struct object *)ptr;
422 assert( obj->refcount );
423 if (!--obj->refcount)
425 assert( !obj->handle_count );
426 /* if the refcount is 0, nobody can be in the wait queue */
427 assert( list_empty( &obj->wait_queue ));
428 free_kernel_objects( obj );
429 unlink_named_object( obj );
430 obj->ops->destroy( obj );
431 free_object( obj );
435 /* find an object by its name; the refcount is incremented */
436 struct object *find_object( const struct namespace *namespace, const struct unicode_str *name,
437 unsigned int attributes )
439 const struct list *list;
440 struct list *p;
442 if (!name || !name->len) return NULL;
444 list = &namespace->names[ get_name_hash( namespace, name->str, name->len ) ];
445 LIST_FOR_EACH( p, list )
447 const struct object_name *ptr = LIST_ENTRY( p, struct object_name, entry );
448 if (ptr->len != name->len) continue;
449 if (attributes & OBJ_CASE_INSENSITIVE)
451 if (!strncmpiW( ptr->name, name->str, name->len/sizeof(WCHAR) ))
452 return grab_object( ptr->obj );
454 else
456 if (!memcmp( ptr->name, name->str, name->len ))
457 return grab_object( ptr->obj );
460 return NULL;
463 /* find an object by its index; the refcount is incremented */
464 struct object *find_object_index( const struct namespace *namespace, unsigned int index )
466 unsigned int i;
468 /* FIXME: not efficient at all */
469 for (i = 0; i < namespace->hash_size; i++)
471 const struct object_name *ptr;
472 LIST_FOR_EACH_ENTRY( ptr, &namespace->names[i], const struct object_name, entry )
474 if (!index--) return grab_object( ptr->obj );
477 set_error( STATUS_NO_MORE_ENTRIES );
478 return NULL;
481 /* allocate a namespace */
482 struct namespace *create_namespace( unsigned int hash_size )
484 struct namespace *namespace;
485 unsigned int i;
487 namespace = mem_alloc( sizeof(*namespace) + (hash_size - 1) * sizeof(namespace->names[0]) );
488 if (namespace)
490 namespace->hash_size = hash_size;
491 for (i = 0; i < hash_size; i++) list_init( &namespace->names[i] );
493 return namespace;
496 /* functions for unimplemented/default object operations */
498 struct object_type *no_get_type( struct object *obj )
500 return NULL;
503 int no_add_queue( struct object *obj, struct wait_queue_entry *entry )
505 set_error( STATUS_OBJECT_TYPE_MISMATCH );
506 return 0;
509 void no_satisfied( struct object *obj, struct wait_queue_entry *entry )
513 int no_signal( struct object *obj, unsigned int access )
515 set_error( STATUS_OBJECT_TYPE_MISMATCH );
516 return 0;
519 struct fd *no_get_fd( struct object *obj )
521 set_error( STATUS_OBJECT_TYPE_MISMATCH );
522 return NULL;
525 unsigned int no_map_access( struct object *obj, unsigned int access )
527 if (access & GENERIC_READ) access |= STANDARD_RIGHTS_READ;
528 if (access & GENERIC_WRITE) access |= STANDARD_RIGHTS_WRITE;
529 if (access & GENERIC_EXECUTE) access |= STANDARD_RIGHTS_EXECUTE;
530 if (access & GENERIC_ALL) access |= STANDARD_RIGHTS_ALL;
531 return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
534 struct security_descriptor *default_get_sd( struct object *obj )
536 return obj->sd;
539 int set_sd_defaults_from_token( struct object *obj, const struct security_descriptor *sd,
540 unsigned int set_info, struct token *token )
542 struct security_descriptor new_sd, *new_sd_ptr;
543 int present;
544 const SID *owner = NULL, *group = NULL;
545 const ACL *sacl, *dacl;
546 ACL *replaced_sacl = NULL;
547 char *ptr;
549 if (!set_info) return 1;
551 new_sd.control = sd->control & ~SE_SELF_RELATIVE;
553 if (set_info & OWNER_SECURITY_INFORMATION && sd->owner_len)
555 owner = sd_get_owner( sd );
556 new_sd.owner_len = sd->owner_len;
558 else if (obj->sd && obj->sd->owner_len)
560 owner = sd_get_owner( obj->sd );
561 new_sd.owner_len = obj->sd->owner_len;
563 else if (token)
565 owner = token_get_user( token );
566 new_sd.owner_len = security_sid_len( owner );
568 else new_sd.owner_len = 0;
570 if (set_info & GROUP_SECURITY_INFORMATION && sd->group_len)
572 group = sd_get_group( sd );
573 new_sd.group_len = sd->group_len;
575 else if (obj->sd && obj->sd->group_len)
577 group = sd_get_group( obj->sd );
578 new_sd.group_len = obj->sd->group_len;
580 else if (token)
582 group = token_get_primary_group( token );
583 new_sd.group_len = security_sid_len( group );
585 else new_sd.group_len = 0;
587 sacl = sd_get_sacl( sd, &present );
588 if (set_info & SACL_SECURITY_INFORMATION && present)
590 new_sd.control |= SE_SACL_PRESENT;
591 new_sd.sacl_len = sd->sacl_len;
593 else if (set_info & LABEL_SECURITY_INFORMATION && present)
595 const ACL *old_sacl = NULL;
596 if (obj->sd && obj->sd->control & SE_SACL_PRESENT) old_sacl = sd_get_sacl( obj->sd, &present );
597 if (!(replaced_sacl = replace_security_labels( old_sacl, sacl ))) return 0;
598 new_sd.control |= SE_SACL_PRESENT;
599 new_sd.sacl_len = replaced_sacl->AclSize;
600 sacl = replaced_sacl;
602 else
604 if (obj->sd) sacl = sd_get_sacl( obj->sd, &present );
606 if (obj->sd && present)
608 new_sd.control |= SE_SACL_PRESENT;
609 new_sd.sacl_len = obj->sd->sacl_len;
611 else
612 new_sd.sacl_len = 0;
615 dacl = sd_get_dacl( sd, &present );
616 if (set_info & DACL_SECURITY_INFORMATION && present)
618 new_sd.control |= SE_DACL_PRESENT;
619 new_sd.dacl_len = sd->dacl_len;
621 else
623 if (obj->sd) dacl = sd_get_dacl( obj->sd, &present );
625 if (obj->sd && present)
627 new_sd.control |= SE_DACL_PRESENT;
628 new_sd.dacl_len = obj->sd->dacl_len;
630 else if (token)
632 dacl = token_get_default_dacl( token );
633 new_sd.control |= SE_DACL_PRESENT;
634 new_sd.dacl_len = dacl->AclSize;
636 else new_sd.dacl_len = 0;
639 ptr = mem_alloc( sizeof(new_sd) + new_sd.owner_len + new_sd.group_len +
640 new_sd.sacl_len + new_sd.dacl_len );
641 if (!ptr)
643 free( replaced_sacl );
644 return 0;
646 new_sd_ptr = (struct security_descriptor*)ptr;
648 memcpy( ptr, &new_sd, sizeof(new_sd) );
649 ptr += sizeof(new_sd);
650 memcpy( ptr, owner, new_sd.owner_len );
651 ptr += new_sd.owner_len;
652 memcpy( ptr, group, new_sd.group_len );
653 ptr += new_sd.group_len;
654 memcpy( ptr, sacl, new_sd.sacl_len );
655 ptr += new_sd.sacl_len;
656 memcpy( ptr, dacl, new_sd.dacl_len );
658 free( replaced_sacl );
659 free( obj->sd );
660 obj->sd = new_sd_ptr;
661 return 1;
664 /** Set the security descriptor using the current primary token for defaults. */
665 int default_set_sd( struct object *obj, const struct security_descriptor *sd,
666 unsigned int set_info )
668 return set_sd_defaults_from_token( obj, sd, set_info, current->process->token );
671 struct object *no_lookup_name( struct object *obj, struct unicode_str *name,
672 unsigned int attr )
674 if (!name) set_error( STATUS_OBJECT_TYPE_MISMATCH );
675 return NULL;
678 int no_link_name( struct object *obj, struct object_name *name, struct object *parent )
680 set_error( STATUS_OBJECT_TYPE_MISMATCH );
681 return 0;
684 void default_unlink_name( struct object *obj, struct object_name *name )
686 list_remove( &name->entry );
689 struct object *no_open_file( struct object *obj, unsigned int access, unsigned int sharing,
690 unsigned int options )
692 set_error( STATUS_OBJECT_TYPE_MISMATCH );
693 return NULL;
696 int no_close_handle( struct object *obj, struct process *process, obj_handle_t handle )
698 return 1; /* ok to close */
701 void no_destroy( struct object *obj )