wined3d: Remove an unneeded check.
[wine] / server / object.c
1 /*
2  * Server-side objects
3  *
4  * Copyright (C) 1998 Alexandre Julliard
5  *
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.
10  *
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.
15  *
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
19  */
20
21 #include "config.h"
22 #include "wine/port.h"
23
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
32 #include "ntstatus.h"
33 #define WIN32_NO_STATUS
34 #include "winternl.h"
35
36 #include "file.h"
37 #include "process.h"
38 #include "thread.h"
39 #include "unicode.h"
40 #include "security.h"
41
42
43 struct object_name
44 {
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];
50 };
51
52 struct namespace
53 {
54     unsigned int        hash_size;       /* size of hash table */
55     struct list         names[1];        /* array of hash entry lists */
56 };
57
58
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);
62
63 void dump_objects(void)
64 {
65     struct list *p;
66
67     LIST_FOR_EACH( p, &static_object_list )
68     {
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 );
72     }
73     LIST_FOR_EACH( p, &object_list )
74     {
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 );
78     }
79 }
80
81 void close_objects(void)
82 {
83     struct list *ptr;
84
85     /* release the static objects */
86     while ((ptr = list_head( &static_object_list )))
87     {
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 );
93     }
94
95     dump_objects();  /* dump any remaining objects */
96 }
97
98 #endif  /* DEBUG_OBJECTS */
99
100 /*****************************************************************/
101
102 /* malloc replacement */
103 void *mem_alloc( size_t size )
104 {
105     void *ptr = malloc( size );
106     if (ptr) memset( ptr, 0x55, size );
107     else set_error( STATUS_NO_MEMORY );
108     return ptr;
109 }
110
111 /* duplicate a block of memory */
112 void *memdup( const void *data, size_t len )
113 {
114     void *ptr = malloc( len );
115     if (ptr) memcpy( ptr, data, len );
116     else set_error( STATUS_NO_MEMORY );
117     return ptr;
118 }
119
120
121 /*****************************************************************/
122
123 static int get_name_hash( const struct namespace *namespace, const WCHAR *name, data_size_t len )
124 {
125     WCHAR hash = 0;
126     len /= sizeof(WCHAR);
127     while (len--) hash ^= tolowerW(*name++);
128     return hash % namespace->hash_size;
129 }
130
131 /* allocate a name for an object */
132 static struct object_name *alloc_name( const struct unicode_str *name )
133 {
134     struct object_name *ptr;
135
136     if ((ptr = mem_alloc( sizeof(*ptr) + name->len - sizeof(ptr->name) )))
137     {
138         ptr->len = name->len;
139         ptr->parent = NULL;
140         memcpy( ptr->name, name->str, name->len );
141     }
142     return ptr;
143 }
144
145 /* free the name of an object */
146 static void free_name( struct object *obj )
147 {
148     struct object_name *ptr = obj->name;
149     list_remove( &ptr->entry );
150     if (ptr->parent) release_object( ptr->parent );
151     free( ptr );
152 }
153
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 )
157 {
158     int hash = get_name_hash( namespace, ptr->name, ptr->len );
159
160     list_add_head( &namespace->names[hash], &ptr->entry );
161     ptr->obj = obj;
162     obj->name = ptr;
163 }
164
165 /* get the name of an existing object */
166 const WCHAR *get_object_name( struct object *obj, data_size_t *len )
167 {
168     struct object_name *ptr = obj->name;
169     if (!ptr) return NULL;
170     *len = ptr->len;
171     return ptr->name;
172 }
173
174 /* allocate and initialize an object */
175 void *alloc_object( const struct object_ops *ops )
176 {
177     struct object *obj = mem_alloc( ops->size );
178     if (obj)
179     {
180         obj->refcount = 1;
181         obj->ops      = ops;
182         obj->name     = NULL;
183         obj->sd       = NULL;
184         list_init( &obj->wait_queue );
185 #ifdef DEBUG_OBJECTS
186         list_add_head( &object_list, &obj->obj_list );
187 #endif
188         return obj;
189     }
190     return NULL;
191 }
192
193 void *create_object( struct namespace *namespace, const struct object_ops *ops,
194                      const struct unicode_str *name, struct object *parent )
195 {
196     struct object *obj;
197     struct object_name *name_ptr;
198
199     if (!(name_ptr = alloc_name( name ))) return NULL;
200     if ((obj = alloc_object( ops )))
201     {
202         set_object_name( namespace, obj, name_ptr );
203         if (parent) name_ptr->parent = grab_object( parent );
204     }
205     else
206         free( name_ptr );
207     return obj;
208 }
209
210 void *create_named_object( struct namespace *namespace, const struct object_ops *ops,
211                            const struct unicode_str *name, unsigned int attributes )
212 {
213     struct object *obj;
214
215     if (!name || !name->len) return alloc_object( ops );
216
217     if ((obj = find_object( namespace, name, attributes )))
218     {
219         if (attributes & OBJ_OPENIF && obj->ops == ops)
220             set_error( STATUS_OBJECT_NAME_EXISTS );
221         else
222         {
223             release_object( obj );
224             obj = NULL;
225             if (attributes & OBJ_OPENIF)
226                 set_error( STATUS_OBJECT_TYPE_MISMATCH );
227             else
228                 set_error( STATUS_OBJECT_NAME_COLLISION );
229         }
230         return obj;
231     }
232     if ((obj = create_object( namespace, ops, name, NULL ))) clear_error();
233     return obj;
234 }
235
236 /* dump the name of an object to stderr */
237 void dump_object_name( struct object *obj )
238 {
239     if (!obj->name) fprintf( stderr, "name=\"\"" );
240     else
241     {
242         fprintf( stderr, "name=L\"" );
243         dump_strW( obj->name->name, obj->name->len/sizeof(WCHAR), stderr, "\"\"" );
244         fputc( '\"', stderr );
245     }
246 }
247
248 /* unlink a named object from its namespace, without freeing the object itself */
249 void unlink_named_object( struct object *obj )
250 {
251     if (obj->name) free_name( obj );
252     obj->name = NULL;
253 }
254
255 /* mark an object as being stored statically, i.e. only released at shutdown */
256 void make_object_static( struct object *obj )
257 {
258 #ifdef DEBUG_OBJECTS
259     list_remove( &obj->obj_list );
260     list_add_head( &static_object_list, &obj->obj_list );
261 #endif
262 }
263
264 /* grab an object (i.e. increment its refcount) and return the object */
265 struct object *grab_object( void *ptr )
266 {
267     struct object *obj = (struct object *)ptr;
268     assert( obj->refcount < INT_MAX );
269     obj->refcount++;
270     return obj;
271 }
272
273 /* release an object (i.e. decrement its refcount) */
274 void release_object( void *ptr )
275 {
276     struct object *obj = (struct object *)ptr;
277     assert( obj->refcount );
278     if (!--obj->refcount)
279     {
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 );
284         free( obj->sd );
285 #ifdef DEBUG_OBJECTS
286         list_remove( &obj->obj_list );
287         memset( obj, 0xaa, obj->ops->size );
288 #endif
289         free( obj );
290     }
291 }
292
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 )
296 {
297     const struct list *list;
298     struct list *p;
299
300     if (!name || !name->len) return NULL;
301
302     list = &namespace->names[ get_name_hash( namespace, name->str, name->len ) ];
303     LIST_FOR_EACH( p, list )
304     {
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)
308         {
309             if (!strncmpiW( ptr->name, name->str, name->len/sizeof(WCHAR) ))
310                 return grab_object( ptr->obj );
311         }
312         else
313         {
314             if (!memcmp( ptr->name, name->str, name->len ))
315                 return grab_object( ptr->obj );
316         }
317     }
318     return NULL;
319 }
320
321 /* allocate a namespace */
322 struct namespace *create_namespace( unsigned int hash_size )
323 {
324     struct namespace *namespace;
325     unsigned int i;
326
327     namespace = mem_alloc( sizeof(*namespace) + (hash_size - 1) * sizeof(namespace->names[0]) );
328     if (namespace)
329     {
330         namespace->hash_size      = hash_size;
331         for (i = 0; i < hash_size; i++) list_init( &namespace->names[i] );
332     }
333     return namespace;
334 }
335
336 /* functions for unimplemented/default object operations */
337
338 int no_add_queue( struct object *obj, struct wait_queue_entry *entry )
339 {
340     set_error( STATUS_OBJECT_TYPE_MISMATCH );
341     return 0;
342 }
343
344 int no_satisfied( struct object *obj, struct thread *thread )
345 {
346     return 0;  /* not abandoned */
347 }
348
349 int no_signal( struct object *obj, unsigned int access )
350 {
351     set_error( STATUS_OBJECT_TYPE_MISMATCH );
352     return 0;
353 }
354
355 struct fd *no_get_fd( struct object *obj )
356 {
357     set_error( STATUS_OBJECT_TYPE_MISMATCH );
358     return NULL;
359 }
360
361 unsigned int no_map_access( struct object *obj, unsigned int access )
362 {
363     if (access & GENERIC_READ)    access |= STANDARD_RIGHTS_READ;
364     if (access & GENERIC_WRITE)   access |= STANDARD_RIGHTS_WRITE;
365     if (access & GENERIC_EXECUTE) access |= STANDARD_RIGHTS_EXECUTE;
366     if (access & GENERIC_ALL)     access |= STANDARD_RIGHTS_ALL;
367     return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
368 }
369
370 struct security_descriptor *default_get_sd( struct object *obj )
371 {
372     return obj->sd;
373 }
374
375 int default_set_sd( struct object *obj, const struct security_descriptor *sd,
376                     unsigned int set_info )
377 {
378     struct security_descriptor new_sd, *new_sd_ptr;
379     int present;
380     const SID *owner, *group;
381     const ACL *sacl, *dacl;
382     char *ptr;
383
384     if (!set_info) return 1;
385
386     new_sd.control = sd->control & ~SE_SELF_RELATIVE;
387
388     owner = sd_get_owner( sd );
389     if (set_info & OWNER_SECURITY_INFORMATION && owner)
390         new_sd.owner_len = sd->owner_len;
391     else
392     {
393         owner = token_get_user( current->process->token );
394         new_sd.owner_len = FIELD_OFFSET(SID, SubAuthority[owner->SubAuthorityCount]);
395         new_sd.control |= SE_OWNER_DEFAULTED;
396     }
397
398     group = sd_get_group( sd );
399     if (set_info & GROUP_SECURITY_INFORMATION && group)
400         new_sd.group_len = sd->group_len;
401     else
402     {
403         group = token_get_primary_group( current->process->token );
404         new_sd.group_len = FIELD_OFFSET(SID, SubAuthority[group->SubAuthorityCount]);
405         new_sd.control |= SE_GROUP_DEFAULTED;
406     }
407
408     new_sd.control |= SE_SACL_PRESENT;
409     sacl = sd_get_sacl( sd, &present );
410     if (set_info & SACL_SECURITY_INFORMATION && present)
411         new_sd.sacl_len = sd->sacl_len;
412     else
413     {
414         if (obj->sd) sacl = sd_get_sacl( obj->sd, &present );
415
416         if (obj->sd && present)
417             new_sd.sacl_len = obj->sd->sacl_len;
418         else
419         {
420             new_sd.sacl_len = 0;
421             new_sd.control |= SE_SACL_DEFAULTED;
422         }
423     }
424
425     new_sd.control |= SE_DACL_PRESENT;
426     dacl = sd_get_dacl( sd, &present );
427     if (set_info & DACL_SECURITY_INFORMATION && present)
428         new_sd.dacl_len = sd->dacl_len;
429     else
430     {
431         if (obj->sd) dacl = sd_get_dacl( obj->sd, &present );
432
433         if (obj->sd && present)
434             new_sd.dacl_len = obj->sd->dacl_len;
435         else
436         {
437             dacl = token_get_default_dacl( current->process->token );
438             new_sd.dacl_len = dacl->AclSize;
439             new_sd.control |= SE_DACL_DEFAULTED;
440         }
441     }
442
443     ptr = mem_alloc( sizeof(new_sd) + new_sd.owner_len + new_sd.group_len +
444                      new_sd.sacl_len + new_sd.dacl_len );
445     if (!ptr) return 0;
446     new_sd_ptr = (struct security_descriptor*)ptr;
447
448     memcpy( ptr, &new_sd, sizeof(new_sd) );
449     ptr += sizeof(new_sd);
450     memcpy( ptr, owner, new_sd.owner_len );
451     ptr += new_sd.owner_len;
452     memcpy( ptr, group, new_sd.group_len );
453     ptr += new_sd.group_len;
454     memcpy( ptr, sacl, new_sd.sacl_len );
455     ptr += new_sd.sacl_len;
456     memcpy( ptr, dacl, new_sd.dacl_len );
457
458     free( obj->sd );
459     obj->sd = new_sd_ptr;
460     return 1;
461 }
462
463 struct object *no_lookup_name( struct object *obj, struct unicode_str *name,
464                                unsigned int attr )
465 {
466     return NULL;
467 }
468
469 struct object *no_open_file( struct object *obj, unsigned int access, unsigned int sharing,
470                              unsigned int options )
471 {
472     set_error( STATUS_OBJECT_TYPE_MISMATCH );
473     return NULL;
474 }
475
476 int no_close_handle( struct object *obj, struct process *process, obj_handle_t handle )
477 {
478     return 1;  /* ok to close */
479 }
480
481 void no_destroy( struct object *obj )
482 {
483 }