server: Add an open_file() function to the object operations.
[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 "thread.h"
38 #include "unicode.h"
39
40
41 struct object_name
42 {
43     struct list         entry;           /* entry in the hash list */
44     struct object      *obj;             /* object owning this name */
45     struct object      *parent;          /* parent object */
46     data_size_t         len;             /* name length in bytes */
47     WCHAR               name[1];
48 };
49
50 struct namespace
51 {
52     unsigned int        hash_size;       /* size of hash table */
53     struct list         names[1];        /* array of hash entry lists */
54 };
55
56
57 #ifdef DEBUG_OBJECTS
58 static struct list object_list = LIST_INIT(object_list);
59 static struct list static_object_list = LIST_INIT(static_object_list);
60
61 void dump_objects(void)
62 {
63     struct list *p;
64
65     LIST_FOR_EACH( p, &static_object_list )
66     {
67         struct object *ptr = LIST_ENTRY( p, struct object, obj_list );
68         fprintf( stderr, "%p:%d: ", ptr, ptr->refcount );
69         ptr->ops->dump( ptr, 1 );
70     }
71     LIST_FOR_EACH( p, &object_list )
72     {
73         struct object *ptr = LIST_ENTRY( p, struct object, obj_list );
74         fprintf( stderr, "%p:%d: ", ptr, ptr->refcount );
75         ptr->ops->dump( ptr, 1 );
76     }
77 }
78
79 void close_objects(void)
80 {
81     struct list *ptr;
82
83     /* release the static objects */
84     while ((ptr = list_head( &static_object_list )))
85     {
86         struct object *obj = LIST_ENTRY( ptr, struct object, obj_list );
87         /* move it back to the standard list before freeing */
88         list_remove( &obj->obj_list );
89         list_add_head( &object_list, &obj->obj_list );
90         release_object( obj );
91     }
92
93     dump_objects();  /* dump any remaining objects */
94 }
95
96 #endif  /* DEBUG_OBJECTS */
97
98 /*****************************************************************/
99
100 /* malloc replacement */
101 void *mem_alloc( size_t size )
102 {
103     void *ptr = malloc( size );
104     if (ptr) memset( ptr, 0x55, size );
105     else set_error( STATUS_NO_MEMORY );
106     return ptr;
107 }
108
109 /* duplicate a block of memory */
110 void *memdup( const void *data, size_t len )
111 {
112     void *ptr = malloc( len );
113     if (ptr) memcpy( ptr, data, len );
114     else set_error( STATUS_NO_MEMORY );
115     return ptr;
116 }
117
118
119 /*****************************************************************/
120
121 static int get_name_hash( const struct namespace *namespace, const WCHAR *name, data_size_t len )
122 {
123     WCHAR hash = 0;
124     len /= sizeof(WCHAR);
125     while (len--) hash ^= tolowerW(*name++);
126     return hash % namespace->hash_size;
127 }
128
129 /* allocate a name for an object */
130 static struct object_name *alloc_name( const struct unicode_str *name )
131 {
132     struct object_name *ptr;
133
134     if ((ptr = mem_alloc( sizeof(*ptr) + name->len - sizeof(ptr->name) )))
135     {
136         ptr->len = name->len;
137         ptr->parent = NULL;
138         memcpy( ptr->name, name->str, name->len );
139     }
140     return ptr;
141 }
142
143 /* free the name of an object */
144 static void free_name( struct object *obj )
145 {
146     struct object_name *ptr = obj->name;
147     list_remove( &ptr->entry );
148     if (ptr->parent) release_object( ptr->parent );
149     free( ptr );
150 }
151
152 /* set the name of an existing object */
153 static void set_object_name( struct namespace *namespace,
154                              struct object *obj, struct object_name *ptr )
155 {
156     int hash = get_name_hash( namespace, ptr->name, ptr->len );
157
158     list_add_head( &namespace->names[hash], &ptr->entry );
159     ptr->obj = obj;
160     obj->name = ptr;
161 }
162
163 /* get the name of an existing object */
164 const WCHAR *get_object_name( struct object *obj, data_size_t *len )
165 {
166     struct object_name *ptr = obj->name;
167     if (!ptr) return NULL;
168     *len = ptr->len;
169     return ptr->name;
170 }
171
172 /* allocate and initialize an object */
173 void *alloc_object( const struct object_ops *ops )
174 {
175     struct object *obj = mem_alloc( ops->size );
176     if (obj)
177     {
178         obj->refcount = 1;
179         obj->ops      = ops;
180         obj->name     = NULL;
181         obj->sd       = NULL;
182         list_init( &obj->wait_queue );
183 #ifdef DEBUG_OBJECTS
184         list_add_head( &object_list, &obj->obj_list );
185 #endif
186         return obj;
187     }
188     return NULL;
189 }
190
191 void *create_object( struct namespace *namespace, const struct object_ops *ops,
192                      const struct unicode_str *name, struct object *parent )
193 {
194     struct object *obj;
195     struct object_name *name_ptr;
196
197     if (!(name_ptr = alloc_name( name ))) return NULL;
198     if ((obj = alloc_object( ops )))
199     {
200         set_object_name( namespace, obj, name_ptr );
201         if (parent) name_ptr->parent = grab_object( parent );
202     }
203     else
204         free( name_ptr );
205     return obj;
206 }
207
208 void *create_named_object( struct namespace *namespace, const struct object_ops *ops,
209                            const struct unicode_str *name, unsigned int attributes )
210 {
211     struct object *obj;
212
213     if (!name || !name->len) return alloc_object( ops );
214
215     if ((obj = find_object( namespace, name, attributes )))
216     {
217         if (attributes & OBJ_OPENIF && obj->ops == ops)
218             set_error( STATUS_OBJECT_NAME_EXISTS );
219         else
220         {
221             release_object( obj );
222             obj = NULL;
223             if (attributes & OBJ_OPENIF)
224                 set_error( STATUS_OBJECT_TYPE_MISMATCH );
225             else
226                 set_error( STATUS_OBJECT_NAME_COLLISION );
227         }
228         return obj;
229     }
230     if ((obj = create_object( namespace, ops, name, NULL ))) clear_error();
231     return obj;
232 }
233
234 /* dump the name of an object to stderr */
235 void dump_object_name( struct object *obj )
236 {
237     if (!obj->name) fprintf( stderr, "name=\"\"" );
238     else
239     {
240         fprintf( stderr, "name=L\"" );
241         dump_strW( obj->name->name, obj->name->len/sizeof(WCHAR), stderr, "\"\"" );
242         fputc( '\"', stderr );
243     }
244 }
245
246 /* unlink a named object from its namespace, without freeing the object itself */
247 void unlink_named_object( struct object *obj )
248 {
249     if (obj->name) free_name( obj );
250     obj->name = NULL;
251 }
252
253 /* mark an object as being stored statically, i.e. only released at shutdown */
254 void make_object_static( struct object *obj )
255 {
256 #ifdef DEBUG_OBJECTS
257     list_remove( &obj->obj_list );
258     list_add_head( &static_object_list, &obj->obj_list );
259 #endif
260 }
261
262 /* grab an object (i.e. increment its refcount) and return the object */
263 struct object *grab_object( void *ptr )
264 {
265     struct object *obj = (struct object *)ptr;
266     assert( obj->refcount < INT_MAX );
267     obj->refcount++;
268     return obj;
269 }
270
271 /* release an object (i.e. decrement its refcount) */
272 void release_object( void *ptr )
273 {
274     struct object *obj = (struct object *)ptr;
275     assert( obj->refcount );
276     if (!--obj->refcount)
277     {
278         /* if the refcount is 0, nobody can be in the wait queue */
279         assert( list_empty( &obj->wait_queue ));
280         obj->ops->destroy( obj );
281         if (obj->name) free_name( obj );
282         free( obj->sd );
283 #ifdef DEBUG_OBJECTS
284         list_remove( &obj->obj_list );
285         memset( obj, 0xaa, obj->ops->size );
286 #endif
287         free( obj );
288     }
289 }
290
291 /* find an object by its name; the refcount is incremented */
292 struct object *find_object( const struct namespace *namespace, const struct unicode_str *name,
293                             unsigned int attributes )
294 {
295     const struct list *list;
296     struct list *p;
297
298     if (!name || !name->len) return NULL;
299
300     list = &namespace->names[ get_name_hash( namespace, name->str, name->len ) ];
301     LIST_FOR_EACH( p, list )
302     {
303         const struct object_name *ptr = LIST_ENTRY( p, struct object_name, entry );
304         if (ptr->len != name->len) continue;
305         if (attributes & OBJ_CASE_INSENSITIVE)
306         {
307             if (!strncmpiW( ptr->name, name->str, name->len/sizeof(WCHAR) ))
308                 return grab_object( ptr->obj );
309         }
310         else
311         {
312             if (!memcmp( ptr->name, name->str, name->len ))
313                 return grab_object( ptr->obj );
314         }
315     }
316     return NULL;
317 }
318
319 /* allocate a namespace */
320 struct namespace *create_namespace( unsigned int hash_size )
321 {
322     struct namespace *namespace;
323     unsigned int i;
324
325     namespace = mem_alloc( sizeof(*namespace) + (hash_size - 1) * sizeof(namespace->names[0]) );
326     if (namespace)
327     {
328         namespace->hash_size      = hash_size;
329         for (i = 0; i < hash_size; i++) list_init( &namespace->names[i] );
330     }
331     return namespace;
332 }
333
334 /* functions for unimplemented/default object operations */
335
336 int no_add_queue( struct object *obj, struct wait_queue_entry *entry )
337 {
338     set_error( STATUS_OBJECT_TYPE_MISMATCH );
339     return 0;
340 }
341
342 int no_satisfied( struct object *obj, struct thread *thread )
343 {
344     return 0;  /* not abandoned */
345 }
346
347 int no_signal( struct object *obj, unsigned int access )
348 {
349     set_error( STATUS_OBJECT_TYPE_MISMATCH );
350     return 0;
351 }
352
353 struct fd *no_get_fd( struct object *obj )
354 {
355     set_error( STATUS_OBJECT_TYPE_MISMATCH );
356     return NULL;
357 }
358
359 unsigned int no_map_access( struct object *obj, unsigned int access )
360 {
361     if (access & GENERIC_READ)    access |= STANDARD_RIGHTS_READ;
362     if (access & GENERIC_WRITE)   access |= STANDARD_RIGHTS_WRITE;
363     if (access & GENERIC_EXECUTE) access |= STANDARD_RIGHTS_EXECUTE;
364     if (access & GENERIC_ALL)     access |= STANDARD_RIGHTS_ALL;
365     return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
366 }
367
368 struct object *no_lookup_name( struct object *obj, struct unicode_str *name,
369                                unsigned int attr )
370 {
371     return NULL;
372 }
373
374 struct object *no_open_file( struct object *obj, unsigned int access, unsigned int sharing,
375                              unsigned int options )
376 {
377     set_error( STATUS_OBJECT_TYPE_MISMATCH );
378     return NULL;
379 }
380
381 int no_close_handle( struct object *obj, struct process *process, obj_handle_t handle )
382 {
383     return 1;  /* ok to close */
384 }
385
386 void no_destroy( struct object *obj )
387 {
388 }