server: New scheme for cleaning up objects on server exit.
[wine] / server / directory.c
1 /*
2  * Server-side directory object management
3  *
4  * Copyright (C) 2005 Vitaliy Margolen
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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  */
21
22 #include "config.h"
23 #include "wine/port.h"
24
25 #include <assert.h>
26 #include <stdarg.h>
27 #include <stdlib.h>
28 #include <stdio.h>
29 #include <sys/types.h>
30
31 #include "ntstatus.h"
32 #define WIN32_NO_STATUS
33 #include "winternl.h"
34 #include "ddk/wdm.h"
35
36 #include "handle.h"
37 #include "request.h"
38 #include "process.h"
39 #include "object.h"
40 #include "unicode.h"
41
42 #define HASH_SIZE 7  /* default hash size */
43
44 struct directory
45 {
46     struct object     obj;        /* object header */
47     struct namespace *entries;    /* directory's name space */
48 };
49
50 static void directory_dump( struct object *obj, int verbose );
51 static unsigned int directory_map_access( struct object *obj, unsigned int access );
52 static struct object *directory_lookup_name( struct object *obj, struct unicode_str *name,
53                                              unsigned int attr );
54 static void directory_destroy( struct object *obj );
55
56 static const struct object_ops directory_ops =
57 {
58     sizeof(struct directory),     /* size */
59     directory_dump,               /* dump */
60     no_add_queue,                 /* add_queue */
61     NULL,                         /* remove_queue */
62     NULL,                         /* signaled */
63     NULL,                         /* satisfied */
64     no_signal,                    /* signal */
65     no_get_fd,                    /* get_fd */
66     directory_map_access,         /* map_access */
67     directory_lookup_name,        /* lookup_name */
68     no_close_handle,              /* close_handle */
69     directory_destroy             /* destroy */
70 };
71
72 static struct directory *root_directory;
73
74
75 static void directory_dump( struct object *obj, int verbose )
76 {
77     assert( obj->ops == &directory_ops );
78
79     fputs( "Directory ", stderr );
80     dump_object_name( obj );
81     fputc( '\n', stderr );
82 }
83
84 static struct object *directory_lookup_name( struct object *obj, struct unicode_str *name,
85                                              unsigned int attr )
86 {
87     struct directory *dir = (struct directory *)obj;
88     struct object *found;
89     struct unicode_str tmp;
90     const WCHAR *p;
91
92     assert( obj->ops == &directory_ops );
93
94     if (!(p = memchrW( name->str, '\\', name->len / sizeof(WCHAR) )))
95         /* Last element in the path name */
96         tmp.len = name->len;
97     else
98         tmp.len = (p - name->str) * sizeof(WCHAR);
99
100     tmp.str = name->str;
101     if ((found = find_object( dir->entries, &tmp, attr )))
102     {
103         /* Skip trailing \\ */
104         if (p)
105         {
106             p++;
107             tmp.len += sizeof(WCHAR);
108         }
109         /* Move to the next element*/
110         name->str = p;
111         name->len -= tmp.len;
112         return found;
113     }
114
115     if (name->str)
116     {
117         if (tmp.len == 0) /* Double backslash */
118             set_error( STATUS_OBJECT_NAME_INVALID );
119         else if (p)  /* Path still has backslashes */
120             set_error( STATUS_OBJECT_PATH_NOT_FOUND );
121     }
122     return NULL;
123 }
124
125 static unsigned int directory_map_access( struct object *obj, unsigned int access )
126 {
127     if (access & GENERIC_READ)    access |= FILE_GENERIC_READ;
128     if (access & GENERIC_WRITE)   access |= FILE_GENERIC_WRITE;
129     if (access & GENERIC_EXECUTE) access |= FILE_GENERIC_EXECUTE;
130     if (access & GENERIC_ALL)     access |= FILE_ALL_ACCESS;
131     return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
132 }
133
134 static void directory_destroy( struct object *obj )
135 {
136     struct directory *dir = (struct directory *)obj;
137     assert( obj->ops == &directory_ops );
138     free( dir->entries );
139 }
140
141 static struct directory *create_directory( struct directory *root, const struct unicode_str *name,
142                                            unsigned int attr, unsigned int hash_size )
143 {
144     struct directory *dir;
145
146     if ((dir = create_named_object_dir( root, name, attr, &directory_ops )) &&
147         get_error() != STATUS_OBJECT_NAME_EXISTS)
148     {
149         if (!(dir->entries = create_namespace( hash_size )))
150         {
151             release_object( dir );
152             dir = NULL;
153         }
154     }
155     return dir;
156 }
157
158 struct directory *get_directory_obj( struct process *process, obj_handle_t handle, unsigned int access )
159 {
160     return (struct directory *)get_handle_obj( process, handle, access, &directory_ops );
161 }
162
163 /******************************************************************************
164  * Find an object by its name in a given root object
165  *
166  * PARAMS
167  *  root      [I] directory to start search from or NULL to start from \\
168  *  name      [I] object name to search for
169  *  attr      [I] OBJECT_ATTRIBUTES.Attributes
170  *  name_left [O] [optional] leftover name if object is not found
171  *
172  * RETURNS
173  *  NULL:      If params are invalid
174  *  Found:     If object with exact name is found returns that object
175  *             (name_left->len == 0). Object's refcount is incremented
176  *  Not found: The last matched parent. (name_left->len > 0)
177  *             Parent's refcount is incremented.
178  */
179 struct object *find_object_dir( struct directory *root, const struct unicode_str *name,
180                                 unsigned int attr, struct unicode_str *name_left )
181 {
182     struct object *obj, *parent;
183     struct unicode_str name_tmp;
184
185     if (name) name_tmp = *name;
186     else name_tmp.len = 0;
187
188     /* Arguments check:
189      * - Either rootdir or name have to be specified
190      * - If root is specified path shouldn't start with backslash */
191     if (root)
192     {
193         if (name_tmp.len && name_tmp.str[0] == '\\')
194         {
195             set_error( STATUS_OBJECT_PATH_SYNTAX_BAD );
196             return NULL;
197         }
198         parent = grab_object( root );
199     }
200     else
201     {
202         if (!name_tmp.len || name_tmp.str[0] != '\\')
203         {
204             set_error( STATUS_OBJECT_PATH_SYNTAX_BAD );
205             return NULL;
206         }
207         parent = grab_object( &root_directory->obj );
208         /* skip leading backslash */
209         name_tmp.str++;
210         name_tmp.len -= sizeof(WCHAR);
211     }
212
213     /* Special case for opening RootDirectory */
214     if (!name_tmp.len) goto done;
215
216     while ((obj = parent->ops->lookup_name( parent, &name_tmp, attr )))
217     {
218         /* move to the next element */
219         release_object ( parent );
220         parent = obj;
221     }
222     if (get_error())
223     {
224         release_object( parent );
225         return NULL;
226     }
227
228     done:
229     if (name_left) *name_left = name_tmp;
230     return parent;
231 }
232
233 /* create a named (if name is present) or unnamed object. */
234 void *create_named_object_dir( struct directory *root, const struct unicode_str *name,
235                                unsigned int attributes, const struct object_ops *ops )
236 {
237     struct object *obj, *new_obj = NULL;
238     struct unicode_str new_name;
239
240     if (!name || !name->len) return alloc_object( ops );
241
242     if (!(obj = find_object_dir( root, name, attributes, &new_name ))) return NULL;
243     if (!new_name.len)
244     {
245         if (attributes & OBJ_OPENIF && obj->ops == ops)
246             set_error( STATUS_OBJECT_NAME_EXISTS );
247         else
248         {
249             release_object( obj );
250             obj = NULL;
251             if (attributes & OBJ_OPENIF)
252                 set_error( STATUS_OBJECT_TYPE_MISMATCH );
253             else
254                 set_error( STATUS_OBJECT_NAME_COLLISION );
255         }
256         return obj;
257     }
258
259     /* ATM we can't insert objects into anything else but directories */
260     if (obj->ops != &directory_ops)
261         set_error( STATUS_OBJECT_TYPE_MISMATCH );
262     else
263     {
264         struct directory *dir = (struct directory *)obj;
265         if ((new_obj = create_object( dir->entries, ops, &new_name, &dir->obj )))
266             clear_error();
267     }
268
269     release_object( obj );
270     return new_obj;
271 }
272
273 /* open a new handle to an existing object */
274 void *open_object_dir( struct directory *root, const struct unicode_str *name,
275                        unsigned int attr, const struct object_ops *ops )
276 {
277     struct unicode_str name_left;
278     struct object *obj;
279
280     if ((obj = find_object_dir( root, name, attr, &name_left )))
281     {
282         if (name_left.len) /* not fully parsed */
283             set_error( STATUS_OBJECT_NAME_NOT_FOUND );
284         else if (ops && obj->ops != ops)
285             set_error( STATUS_OBJECT_TYPE_MISMATCH );
286         else
287             return obj;
288
289         release_object( obj );
290     }
291     return NULL;
292 }
293
294
295 /* Global initialization */
296
297 void init_directories(void)
298 {
299     /* Directories */
300     static const WCHAR dir_globalW[] = {'\\','?','?'};
301     static const WCHAR dir_driverW[] = {'D','r','i','v','e','r'};
302     static const WCHAR dir_deviceW[] = {'D','e','v','i','c','e'};
303     static const WCHAR dir_basenamedW[] = {'\\','B','a','s','e','N','a','m','e','d','O','b','j','e','c','t','s'};
304     static const struct unicode_str dir_global_str = {dir_globalW, sizeof(dir_globalW)};
305     static const struct unicode_str dir_driver_str = {dir_driverW, sizeof(dir_driverW)};
306     static const struct unicode_str dir_device_str = {dir_deviceW, sizeof(dir_deviceW)};
307     static const struct unicode_str dir_basenamed_str = {dir_basenamedW, sizeof(dir_basenamedW)};
308
309     /* symlinks */
310     static const WCHAR link_dosdevW[] = {'D','o','s','D','e','v','i','c','e','s'};
311     static const WCHAR link_globalW[] = {'G','l','o','b','a','l'};
312     static const WCHAR link_localW[]  = {'L','o','c','a','l'};
313     static const struct unicode_str link_dosdev_str = {link_dosdevW, sizeof(link_dosdevW)};
314     static const struct unicode_str link_global_str = {link_globalW, sizeof(link_globalW)};
315     static const struct unicode_str link_local_str  = {link_localW, sizeof(link_localW)};
316
317     /* devices */
318     static const WCHAR pipeW[] = {'P','I','P','E'};
319     static const WCHAR mailslotW[] = {'M','A','I','L','S','L','O','T'};
320     static const struct unicode_str pipe_str = {pipeW, sizeof(pipeW)};
321     static const struct unicode_str mailslot_str = {mailslotW, sizeof(mailslotW)};
322
323     struct directory *dir_driver, *dir_device, *dir_global, *dir_basenamed;
324     struct symlink *link_dosdev, *link_global1, *link_global2, *link_local;
325
326     root_directory = create_directory( NULL, NULL, 0, HASH_SIZE );
327     dir_driver     = create_directory( root_directory, &dir_driver_str, 0, HASH_SIZE );
328     dir_device     = create_directory( root_directory, &dir_device_str, 0, HASH_SIZE );
329     make_object_static( &root_directory->obj );
330     make_object_static( &dir_driver->obj );
331     make_object_static( &dir_device->obj );
332
333     dir_global     = create_directory( NULL, &dir_global_str, 0, HASH_SIZE );
334     /* use a larger hash table for this one since it can contain a lot of objects */
335     dir_basenamed  = create_directory( NULL, &dir_basenamed_str, 0, 37 );
336
337     /* symlinks */
338     link_dosdev    = create_symlink( root_directory, &link_dosdev_str, 0, &dir_global_str );
339     link_global1   = create_symlink( dir_global, &link_global_str, 0, &dir_global_str );
340     link_global2   = create_symlink( dir_basenamed, &link_global_str, 0, &dir_basenamed_str );
341     link_local     = create_symlink( dir_basenamed, &link_local_str, 0, &dir_basenamed_str );
342     make_object_static( (struct object *)link_dosdev );
343     make_object_static( (struct object *)link_global1 );
344     make_object_static( (struct object *)link_global2 );
345     make_object_static( (struct object *)link_local );
346
347     /* devices */
348     create_named_pipe_device( dir_global, &pipe_str );
349     create_mailslot_device( dir_global, &mailslot_str );
350
351     /* the symlinks or devices hold references so we can release these */
352     release_object( dir_global );
353     release_object( dir_basenamed );
354 }
355
356 /* create a directory object */
357 DECL_HANDLER(create_directory)
358 {
359     struct unicode_str name;
360     struct directory *dir, *root = NULL;
361
362     reply->handle = 0;
363     get_req_unicode_str( &name );
364     if (req->rootdir && !(root = get_directory_obj( current->process, req->rootdir, 0 )))
365         return;
366
367     if ((dir = create_directory( root, &name, req->attributes, HASH_SIZE )))
368     {
369         reply->handle = alloc_handle( current->process, dir, req->access, req->attributes );
370         release_object( dir );
371     }
372
373     if (root) release_object( root );
374 }
375
376 /* open a directory object */
377 DECL_HANDLER(open_directory)
378 {
379     struct unicode_str name;
380     struct directory *dir, *root = NULL;
381
382     get_req_unicode_str( &name );
383     if (req->rootdir && !(root = get_directory_obj( current->process, req->rootdir, 0 )))
384         return;
385
386     if ((dir = open_object_dir( root, &name, req->attributes, &directory_ops )))
387     {
388         reply->handle = alloc_handle( current->process, &dir->obj, req->access, req->attributes );
389         release_object( dir );
390     }
391
392     if (root) release_object( root );
393 }