msvcp110: Add tr2_sys__Remove_dir_wchar implementation and test.
[wine/multimedia.git] / server / object.c
blob965c11c0486323796a7deb76a164bb85878f9e4e
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>
32 #include "ntstatus.h"
33 #define WIN32_NO_STATUS
34 #include "winternl.h"
36 #include "file.h"
37 #include "process.h"
38 #include "thread.h"
39 #include "unicode.h"
40 #include "security.h"
43 struct object_name
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 */
49 WCHAR name[1];
52 struct namespace
54 unsigned int hash_size; /* size of hash table */
55 struct list names[1]; /* array of hash entry lists */
59 #ifdef DEBUG_OBJECTS
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)
65 struct list *p;
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)
83 struct list *ptr;
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 );
108 return ptr;
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 );
117 return ptr;
121 /*****************************************************************/
123 static int get_name_hash( const struct namespace *namespace, const WCHAR *name, data_size_t len )
125 WCHAR hash = 0;
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;
139 ptr->parent = NULL;
140 memcpy( ptr->name, name->str, name->len );
142 return ptr;
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 );
151 free( ptr );
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 );
161 ptr->obj = obj;
162 obj->name = ptr;
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;
170 *len = ptr->len;
171 return ptr->name;
174 /* get the full path name of an existing object */
175 WCHAR *get_object_full_name( struct object *obj, data_size_t *ret_len )
177 static const WCHAR backslash = '\\';
178 struct object *ptr = obj;
179 data_size_t len = 0;
180 char *ret;
182 while (ptr && ptr->name)
184 struct object_name *name = ptr->name;
185 len += name->len + sizeof(WCHAR);
186 ptr = name->parent;
188 if (!len) return NULL;
189 if (!(ret = malloc( len ))) return NULL;
191 *ret_len = len;
192 while (obj && obj->name)
194 struct object_name *name = obj->name;
195 memcpy( ret + len - name->len, name->name, name->len );
196 len -= name->len + sizeof(WCHAR);
197 memcpy( ret + len, &backslash, sizeof(WCHAR) );
198 obj = name->parent;
200 return (WCHAR *)ret;
203 /* allocate and initialize an object */
204 void *alloc_object( const struct object_ops *ops )
206 struct object *obj = mem_alloc( ops->size );
207 if (obj)
209 obj->refcount = 1;
210 obj->handle_count = 0;
211 obj->ops = ops;
212 obj->name = NULL;
213 obj->sd = NULL;
214 list_init( &obj->wait_queue );
215 #ifdef DEBUG_OBJECTS
216 list_add_head( &object_list, &obj->obj_list );
217 #endif
218 return obj;
220 return NULL;
223 void *create_object( struct namespace *namespace, const struct object_ops *ops,
224 const struct unicode_str *name, struct object *parent )
226 struct object *obj;
227 struct object_name *name_ptr;
229 if (!(name_ptr = alloc_name( name ))) return NULL;
230 if ((obj = alloc_object( ops )))
232 set_object_name( namespace, obj, name_ptr );
233 if (parent) name_ptr->parent = grab_object( parent );
235 else
236 free( name_ptr );
237 return obj;
240 void *create_named_object( struct namespace *namespace, const struct object_ops *ops,
241 const struct unicode_str *name, unsigned int attributes )
243 struct object *obj;
245 if (!name || !name->len) return alloc_object( ops );
247 if ((obj = find_object( namespace, name, attributes )))
249 if (attributes & OBJ_OPENIF && obj->ops == ops)
250 set_error( STATUS_OBJECT_NAME_EXISTS );
251 else
253 release_object( obj );
254 obj = NULL;
255 if (attributes & OBJ_OPENIF)
256 set_error( STATUS_OBJECT_TYPE_MISMATCH );
257 else
258 set_error( STATUS_OBJECT_NAME_COLLISION );
260 return obj;
262 if ((obj = create_object( namespace, ops, name, NULL ))) clear_error();
263 return obj;
266 /* dump the name of an object to stderr */
267 void dump_object_name( struct object *obj )
269 if (!obj->name) fprintf( stderr, "name=\"\"" );
270 else
272 fprintf( stderr, "name=L\"" );
273 dump_strW( obj->name->name, obj->name->len/sizeof(WCHAR), stderr, "\"\"" );
274 fputc( '\"', stderr );
278 /* unlink a named object from its namespace, without freeing the object itself */
279 void unlink_named_object( struct object *obj )
281 if (obj->name) free_name( obj );
282 obj->name = NULL;
285 /* mark an object as being stored statically, i.e. only released at shutdown */
286 void make_object_static( struct object *obj )
288 #ifdef DEBUG_OBJECTS
289 list_remove( &obj->obj_list );
290 list_add_head( &static_object_list, &obj->obj_list );
291 #endif
294 /* grab an object (i.e. increment its refcount) and return the object */
295 struct object *grab_object( void *ptr )
297 struct object *obj = (struct object *)ptr;
298 assert( obj->refcount < INT_MAX );
299 obj->refcount++;
300 return obj;
303 /* release an object (i.e. decrement its refcount) */
304 void release_object( void *ptr )
306 struct object *obj = (struct object *)ptr;
307 assert( obj->refcount );
308 if (!--obj->refcount)
310 assert( !obj->handle_count );
311 /* if the refcount is 0, nobody can be in the wait queue */
312 assert( list_empty( &obj->wait_queue ));
313 obj->ops->destroy( obj );
314 if (obj->name) free_name( obj );
315 free( obj->sd );
316 #ifdef DEBUG_OBJECTS
317 list_remove( &obj->obj_list );
318 memset( obj, 0xaa, obj->ops->size );
319 #endif
320 free( obj );
324 /* find an object by its name; the refcount is incremented */
325 struct object *find_object( const struct namespace *namespace, const struct unicode_str *name,
326 unsigned int attributes )
328 const struct list *list;
329 struct list *p;
331 if (!name || !name->len) return NULL;
333 list = &namespace->names[ get_name_hash( namespace, name->str, name->len ) ];
334 LIST_FOR_EACH( p, list )
336 const struct object_name *ptr = LIST_ENTRY( p, struct object_name, entry );
337 if (ptr->len != name->len) continue;
338 if (attributes & OBJ_CASE_INSENSITIVE)
340 if (!strncmpiW( ptr->name, name->str, name->len/sizeof(WCHAR) ))
341 return grab_object( ptr->obj );
343 else
345 if (!memcmp( ptr->name, name->str, name->len ))
346 return grab_object( ptr->obj );
349 return NULL;
352 /* find an object by its index; the refcount is incremented */
353 struct object *find_object_index( const struct namespace *namespace, unsigned int index )
355 unsigned int i;
357 /* FIXME: not efficient at all */
358 for (i = 0; i < namespace->hash_size; i++)
360 const struct object_name *ptr;
361 LIST_FOR_EACH_ENTRY( ptr, &namespace->names[i], const struct object_name, entry )
363 if (!index--) return grab_object( ptr->obj );
366 set_error( STATUS_NO_MORE_ENTRIES );
367 return NULL;
370 /* allocate a namespace */
371 struct namespace *create_namespace( unsigned int hash_size )
373 struct namespace *namespace;
374 unsigned int i;
376 namespace = mem_alloc( sizeof(*namespace) + (hash_size - 1) * sizeof(namespace->names[0]) );
377 if (namespace)
379 namespace->hash_size = hash_size;
380 for (i = 0; i < hash_size; i++) list_init( &namespace->names[i] );
382 return namespace;
385 /* functions for unimplemented/default object operations */
387 struct object_type *no_get_type( struct object *obj )
389 return NULL;
392 int no_add_queue( struct object *obj, struct wait_queue_entry *entry )
394 set_error( STATUS_OBJECT_TYPE_MISMATCH );
395 return 0;
398 void no_satisfied( struct object *obj, struct wait_queue_entry *entry )
402 int no_signal( struct object *obj, unsigned int access )
404 set_error( STATUS_OBJECT_TYPE_MISMATCH );
405 return 0;
408 struct fd *no_get_fd( struct object *obj )
410 set_error( STATUS_OBJECT_TYPE_MISMATCH );
411 return NULL;
414 unsigned int no_map_access( struct object *obj, unsigned int access )
416 if (access & GENERIC_READ) access |= STANDARD_RIGHTS_READ;
417 if (access & GENERIC_WRITE) access |= STANDARD_RIGHTS_WRITE;
418 if (access & GENERIC_EXECUTE) access |= STANDARD_RIGHTS_EXECUTE;
419 if (access & GENERIC_ALL) access |= STANDARD_RIGHTS_ALL;
420 return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
423 struct security_descriptor *default_get_sd( struct object *obj )
425 return obj->sd;
428 int set_sd_defaults_from_token( struct object *obj, const struct security_descriptor *sd,
429 unsigned int set_info, struct token *token )
431 struct security_descriptor new_sd, *new_sd_ptr;
432 int present;
433 const SID *owner = NULL, *group = NULL;
434 const ACL *sacl, *dacl;
435 char *ptr;
437 if (!set_info) return 1;
439 new_sd.control = sd->control & ~SE_SELF_RELATIVE;
441 if (set_info & OWNER_SECURITY_INFORMATION && sd->owner_len)
443 owner = sd_get_owner( sd );
444 new_sd.owner_len = sd->owner_len;
446 else if (obj->sd && obj->sd->owner_len)
448 owner = sd_get_owner( obj->sd );
449 new_sd.owner_len = obj->sd->owner_len;
451 else if (token)
453 owner = token_get_user( token );
454 new_sd.owner_len = security_sid_len( owner );
456 else new_sd.owner_len = 0;
458 if (set_info & GROUP_SECURITY_INFORMATION && sd->group_len)
460 group = sd_get_group( sd );
461 new_sd.group_len = sd->group_len;
463 else if (obj->sd && obj->sd->group_len)
465 group = sd_get_group( obj->sd );
466 new_sd.group_len = obj->sd->group_len;
468 else if (token)
470 group = token_get_primary_group( token );
471 new_sd.group_len = security_sid_len( group );
473 else new_sd.group_len = 0;
475 new_sd.control |= SE_SACL_PRESENT;
476 sacl = sd_get_sacl( sd, &present );
477 if (set_info & SACL_SECURITY_INFORMATION && present)
478 new_sd.sacl_len = sd->sacl_len;
479 else
481 if (obj->sd) sacl = sd_get_sacl( obj->sd, &present );
483 if (obj->sd && present)
484 new_sd.sacl_len = obj->sd->sacl_len;
485 else
486 new_sd.sacl_len = 0;
489 new_sd.control |= SE_DACL_PRESENT;
490 dacl = sd_get_dacl( sd, &present );
491 if (set_info & DACL_SECURITY_INFORMATION && present)
492 new_sd.dacl_len = sd->dacl_len;
493 else
495 if (obj->sd) dacl = sd_get_dacl( obj->sd, &present );
497 if (obj->sd && present)
498 new_sd.dacl_len = obj->sd->dacl_len;
499 else if (token)
501 dacl = token_get_default_dacl( token );
502 new_sd.dacl_len = dacl->AclSize;
504 else new_sd.dacl_len = 0;
507 ptr = mem_alloc( sizeof(new_sd) + new_sd.owner_len + new_sd.group_len +
508 new_sd.sacl_len + new_sd.dacl_len );
509 if (!ptr) return 0;
510 new_sd_ptr = (struct security_descriptor*)ptr;
512 memcpy( ptr, &new_sd, sizeof(new_sd) );
513 ptr += sizeof(new_sd);
514 memcpy( ptr, owner, new_sd.owner_len );
515 ptr += new_sd.owner_len;
516 memcpy( ptr, group, new_sd.group_len );
517 ptr += new_sd.group_len;
518 memcpy( ptr, sacl, new_sd.sacl_len );
519 ptr += new_sd.sacl_len;
520 memcpy( ptr, dacl, new_sd.dacl_len );
522 free( obj->sd );
523 obj->sd = new_sd_ptr;
524 return 1;
527 /** Set the security descriptor using the current primary token for defaults. */
528 int default_set_sd( struct object *obj, const struct security_descriptor *sd,
529 unsigned int set_info )
531 return set_sd_defaults_from_token( obj, sd, set_info, current->process->token );
534 struct object *no_lookup_name( struct object *obj, struct unicode_str *name,
535 unsigned int attr )
537 return NULL;
540 struct object *no_open_file( struct object *obj, unsigned int access, unsigned int sharing,
541 unsigned int options )
543 set_error( STATUS_OBJECT_TYPE_MISMATCH );
544 return NULL;
547 int no_close_handle( struct object *obj, struct process *process, obj_handle_t handle )
549 return 1; /* ok to close */
552 void no_destroy( struct object *obj )