2 * Server-side directory object management
4 * Copyright (C) 2005 Vitaliy Margolen
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
23 #include "wine/port.h"
29 #include <sys/types.h>
32 #define WIN32_NO_STATUS
42 #define HASH_SIZE 7 /* default hash size */
46 struct object obj
; /* object header */
49 static void object_type_dump( struct object
*obj
, int verbose
);
50 static struct object_type
*object_type_get_type( struct object
*obj
);
52 static const struct object_ops object_type_ops
=
54 sizeof(struct object_type
), /* size */
55 object_type_dump
, /* dump */
56 object_type_get_type
, /* get_type */
57 no_add_queue
, /* add_queue */
58 NULL
, /* remove_queue */
61 no_signal
, /* signal */
62 no_get_fd
, /* get_fd */
63 no_map_access
, /* map_access */
64 default_get_sd
, /* get_sd */
65 default_set_sd
, /* set_sd */
66 no_lookup_name
, /* lookup_name */
67 no_link_name
, /* link_name */
68 NULL
, /* unlink_name */
69 no_open_file
, /* open_file */
70 no_close_handle
, /* close_handle */
71 no_destroy
/* destroy */
77 struct object obj
; /* object header */
78 struct namespace *entries
; /* directory's name space */
81 static void directory_dump( struct object
*obj
, int verbose
);
82 static struct object_type
*directory_get_type( struct object
*obj
);
83 static struct object
*directory_lookup_name( struct object
*obj
, struct unicode_str
*name
,
85 static void directory_destroy( struct object
*obj
);
87 static const struct object_ops directory_ops
=
89 sizeof(struct directory
), /* size */
90 directory_dump
, /* dump */
91 directory_get_type
, /* get_type */
92 no_add_queue
, /* add_queue */
93 NULL
, /* remove_queue */
96 no_signal
, /* signal */
97 no_get_fd
, /* get_fd */
98 default_fd_map_access
, /* map_access */
99 default_get_sd
, /* get_sd */
100 default_set_sd
, /* set_sd */
101 directory_lookup_name
, /* lookup_name */
102 no_link_name
, /* link_name */
103 NULL
, /* unlink_name */
104 no_open_file
, /* open_file */
105 no_close_handle
, /* close_handle */
106 directory_destroy
/* destroy */
109 static struct directory
*root_directory
;
110 static struct directory
*dir_objtype
;
113 static void object_type_dump( struct object
*obj
, int verbose
)
115 fputs( "Object type\n", stderr
);
118 static struct object_type
*object_type_get_type( struct object
*obj
)
120 static const WCHAR name
[] = {'O','b','j','e','c','t','T','y','p','e'};
121 static const struct unicode_str str
= { name
, sizeof(name
) };
122 return get_object_type( &str
);
125 static void directory_dump( struct object
*obj
, int verbose
)
127 fputs( "Directory\n", stderr
);
130 static struct object_type
*directory_get_type( struct object
*obj
)
132 static const WCHAR name
[] = {'D','i','r','e','c','t','o','r','y'};
133 static const struct unicode_str str
= { name
, sizeof(name
) };
134 return get_object_type( &str
);
137 static struct object
*directory_lookup_name( struct object
*obj
, struct unicode_str
*name
,
140 struct directory
*dir
= (struct directory
*)obj
;
141 struct object
*found
;
142 struct unicode_str tmp
;
145 assert( obj
->ops
== &directory_ops
);
147 if (!(p
= memchrW( name
->str
, '\\', name
->len
/ sizeof(WCHAR
) )))
148 /* Last element in the path name */
151 tmp
.len
= (p
- name
->str
) * sizeof(WCHAR
);
154 if ((found
= find_object( dir
->entries
, &tmp
, attr
)))
156 /* Skip trailing \\ */
160 tmp
.len
+= sizeof(WCHAR
);
162 /* Move to the next element*/
164 name
->len
-= tmp
.len
;
170 if (tmp
.len
== 0) /* Double backslash */
171 set_error( STATUS_OBJECT_NAME_INVALID
);
172 else if (p
) /* Path still has backslashes */
173 set_error( STATUS_OBJECT_PATH_NOT_FOUND
);
180 static void directory_destroy( struct object
*obj
)
182 struct directory
*dir
= (struct directory
*)obj
;
183 assert( obj
->ops
== &directory_ops
);
184 free( dir
->entries
);
187 static struct directory
*create_directory( struct directory
*root
, const struct unicode_str
*name
,
188 unsigned int attr
, unsigned int hash_size
,
189 const struct security_descriptor
*sd
)
191 struct directory
*dir
;
193 if ((dir
= create_named_object_dir( root
, name
, attr
, &directory_ops
)) &&
194 get_error() != STATUS_OBJECT_NAME_EXISTS
)
196 if (!(dir
->entries
= create_namespace( hash_size
)))
198 release_object( dir
);
201 if (sd
) default_set_sd( &dir
->obj
, sd
, OWNER_SECURITY_INFORMATION
| GROUP_SECURITY_INFORMATION
|
202 DACL_SECURITY_INFORMATION
| SACL_SECURITY_INFORMATION
);
207 struct directory
*get_directory_obj( struct process
*process
, obj_handle_t handle
, unsigned int access
)
209 return (struct directory
*)get_handle_obj( process
, handle
, access
, &directory_ops
);
212 /******************************************************************************
213 * Find an object by its name in a given root object
216 * root [I] directory to start search from or NULL to start from \\
217 * name [I] object name to search for
218 * attr [I] OBJECT_ATTRIBUTES.Attributes
219 * name_left [O] [optional] leftover name if object is not found
222 * NULL: If params are invalid
223 * Found: If object with exact name is found returns that object
224 * (name_left->len == 0). Object's refcount is incremented
225 * Not found: The last matched parent. (name_left->len > 0)
226 * Parent's refcount is incremented.
228 struct object
*find_object_dir( struct directory
*root
, const struct unicode_str
*name
,
229 unsigned int attr
, struct unicode_str
*name_left
)
231 struct object
*obj
, *parent
;
232 struct unicode_str name_tmp
;
234 if (name
) name_tmp
= *name
;
235 else name_tmp
.len
= 0;
238 * - Either rootdir or name have to be specified
239 * - If root is specified path shouldn't start with backslash */
242 if (name_tmp
.len
&& name_tmp
.str
[0] == '\\')
244 set_error( STATUS_OBJECT_PATH_SYNTAX_BAD
);
247 parent
= grab_object( root
);
251 if (!name_tmp
.len
|| name_tmp
.str
[0] != '\\')
253 set_error( STATUS_OBJECT_PATH_SYNTAX_BAD
);
256 parent
= grab_object( &root_directory
->obj
);
257 /* skip leading backslash */
259 name_tmp
.len
-= sizeof(WCHAR
);
262 /* Special case for opening RootDirectory */
263 if (!name_tmp
.len
) goto done
;
265 while ((obj
= parent
->ops
->lookup_name( parent
, &name_tmp
, attr
)))
267 /* move to the next element */
268 release_object ( parent
);
273 release_object( parent
);
278 if (name_left
) *name_left
= name_tmp
;
282 /* create a named (if name is present) or unnamed object. */
283 void *create_named_object_dir( struct directory
*root
, const struct unicode_str
*name
,
284 unsigned int attributes
, const struct object_ops
*ops
)
286 struct object
*obj
, *new_obj
= NULL
;
287 struct unicode_str new_name
;
289 if (!name
|| !name
->len
)
291 if ((new_obj
= alloc_object( ops
))) clear_error();
295 if (!(obj
= find_object_dir( root
, name
, attributes
, &new_name
))) return NULL
;
298 if (attributes
& OBJ_OPENIF
&& obj
->ops
== ops
)
299 set_error( STATUS_OBJECT_NAME_EXISTS
);
302 release_object( obj
);
304 if (attributes
& OBJ_OPENIF
)
305 set_error( STATUS_OBJECT_TYPE_MISMATCH
);
307 set_error( STATUS_OBJECT_NAME_COLLISION
);
312 /* ATM we can't insert objects into anything else but directories */
313 if (obj
->ops
!= &directory_ops
)
314 set_error( STATUS_OBJECT_TYPE_MISMATCH
);
317 struct directory
*dir
= (struct directory
*)obj
;
318 if ((new_obj
= create_object( dir
->entries
, ops
, &new_name
, &dir
->obj
)))
322 release_object( obj
);
326 /* open a new handle to an existing object */
327 void *open_object_dir( struct directory
*root
, const struct unicode_str
*name
,
328 unsigned int attr
, const struct object_ops
*ops
)
330 struct unicode_str name_left
;
333 if ((obj
= find_object_dir( root
, name
, attr
, &name_left
)))
335 if (name_left
.len
) /* not fully parsed */
336 set_error( STATUS_OBJECT_NAME_NOT_FOUND
);
337 else if (ops
&& obj
->ops
!= ops
)
338 set_error( STATUS_OBJECT_TYPE_MISMATCH
);
342 release_object( obj
);
347 /* retrieve an object type, creating it if needed */
348 struct object_type
*get_object_type( const struct unicode_str
*name
)
350 struct object_type
*type
;
352 if ((type
= open_object_dir( dir_objtype
, name
, 0, &object_type_ops
)))
355 if ((type
= create_named_object_dir( dir_objtype
, name
, 0, &object_type_ops
)))
358 make_object_static( &type
->obj
);
364 /* Global initialization */
366 void init_directories(void)
369 static const WCHAR dir_globalW
[] = {'\\','?','?'};
370 static const WCHAR dir_driverW
[] = {'D','r','i','v','e','r'};
371 static const WCHAR dir_deviceW
[] = {'D','e','v','i','c','e'};
372 static const WCHAR dir_nullW
[] = {'\\','D','e','v','i','c','e','\\','N','u','l','l'};
373 static const WCHAR dir_basenamedW
[] = {'\\','B','a','s','e','N','a','m','e','d','O','b','j','e','c','t','s'};
374 static const WCHAR dir_named_pipeW
[] = {'\\','D','e','v','i','c','e','\\','N','a','m','e','d','P','i','p','e'};
375 static const WCHAR dir_mailslotW
[] = {'\\','D','e','v','i','c','e','\\','M','a','i','l','S','l','o','t'};
376 static const WCHAR dir_objtypeW
[] = {'O','b','j','e','c','t','T','y','p','e','s'};
377 static const WCHAR dir_sessionsW
[] = {'S','e','s','s','i','o','n','s'};
378 static const WCHAR dir_kernelW
[] = {'K','e','r','n','e','l','O','b','j','e','c','t','s'};
379 static const WCHAR dir_windowsW
[] = {'W','i','n','d','o','w','s'};
380 static const WCHAR dir_winstationsW
[] = {'W','i','n','d','o','w','S','t','a','t','i','o','n','s'};
381 static const struct unicode_str dir_global_str
= {dir_globalW
, sizeof(dir_globalW
)};
382 static const struct unicode_str dir_driver_str
= {dir_driverW
, sizeof(dir_driverW
)};
383 static const struct unicode_str dir_device_str
= {dir_deviceW
, sizeof(dir_deviceW
)};
384 static const struct unicode_str dir_null_str
= {dir_nullW
, sizeof(dir_nullW
)};
385 static const struct unicode_str dir_basenamed_str
= {dir_basenamedW
, sizeof(dir_basenamedW
)};
386 static const struct unicode_str dir_named_pipe_str
= {dir_named_pipeW
, sizeof(dir_named_pipeW
)};
387 static const struct unicode_str dir_mailslot_str
= {dir_mailslotW
, sizeof(dir_mailslotW
)};
388 static const struct unicode_str dir_objtype_str
= {dir_objtypeW
, sizeof(dir_objtypeW
)};
389 static const struct unicode_str dir_sessions_str
= {dir_sessionsW
, sizeof(dir_sessionsW
)};
390 static const struct unicode_str dir_kernel_str
= {dir_kernelW
, sizeof(dir_kernelW
)};
391 static const struct unicode_str dir_windows_str
= {dir_windowsW
, sizeof(dir_windowsW
)};
392 static const struct unicode_str dir_winstations_str
= {dir_winstationsW
, sizeof(dir_winstationsW
)};
395 static const WCHAR link_dosdevW
[] = {'D','o','s','D','e','v','i','c','e','s'};
396 static const WCHAR link_globalW
[] = {'G','l','o','b','a','l'};
397 static const WCHAR link_localW
[] = {'L','o','c','a','l'};
398 static const WCHAR link_nulW
[] = {'N','U','L'};
399 static const WCHAR link_pipeW
[] = {'P','I','P','E'};
400 static const WCHAR link_mailslotW
[] = {'M','A','I','L','S','L','O','T'};
401 static const WCHAR link_0W
[] = {'0'};
402 static const WCHAR link_sessionW
[] = {'S','e','s','s','i','o','n'};
403 static const WCHAR link_sessionsW
[] = {'\\','S','e','s','s','i','o','n','s'};
404 static const struct unicode_str link_dosdev_str
= {link_dosdevW
, sizeof(link_dosdevW
)};
405 static const struct unicode_str link_global_str
= {link_globalW
, sizeof(link_globalW
)};
406 static const struct unicode_str link_local_str
= {link_localW
, sizeof(link_localW
)};
407 static const struct unicode_str link_nul_str
= {link_nulW
, sizeof(link_nulW
)};
408 static const struct unicode_str link_pipe_str
= {link_pipeW
, sizeof(link_pipeW
)};
409 static const struct unicode_str link_mailslot_str
= {link_mailslotW
, sizeof(link_mailslotW
)};
410 static const struct unicode_str link_0_str
= {link_0W
, sizeof(link_0W
)};
411 static const struct unicode_str link_session_str
= {link_sessionW
, sizeof(link_sessionW
)};
412 static const struct unicode_str link_sessions_str
= {link_sessionsW
, sizeof(link_sessionsW
)};
415 static const WCHAR named_pipeW
[] = {'N','a','m','e','d','P','i','p','e'};
416 static const WCHAR mailslotW
[] = {'M','a','i','l','S','l','o','t'};
417 static const WCHAR nullW
[] = {'N','u','l','l'};
418 static const struct unicode_str named_pipe_str
= {named_pipeW
, sizeof(named_pipeW
)};
419 static const struct unicode_str mailslot_str
= {mailslotW
, sizeof(mailslotW
)};
420 static const struct unicode_str null_str
= {nullW
, sizeof(nullW
)};
423 static const WCHAR event_low_memW
[] = {'L','o','w','M','e','m','o','r','y','C','o','n','d','i','t','i','o','n'};
424 static const WCHAR event_low_pagedW
[] = {'L','o','w','P','a','g','e','d','P','o','o','l','C','o','n','d','i','t','i','o','n'};
425 static const WCHAR event_low_nonpgW
[] = {'L','o','w','N','o','n','P','a','g','e','d','P','o','o','l','C','o','n','d','i','t','i','o','n'};
426 static const WCHAR event_high_memW
[] = {'H','i','g','h','M','e','m','o','r','y','C','o','n','d','i','t','i','o','n'};
427 static const WCHAR event_high_pagedW
[] = {'H','i','g','h','P','a','g','e','d','P','o','o','l','C','o','n','d','i','t','i','o','n'};
428 static const WCHAR event_high_nonpgW
[] = {'H','i','g','h','N','o','n','P','a','g','e','d','P','o','o','l','C','o','n','d','i','t','i','o','n'};
429 static const WCHAR keyed_event_crit_sectW
[] = {'C','r','i','t','S','e','c','O','u','t','O','f','M','e','m','o','r','y','E','v','e','n','t'};
430 static const struct unicode_str kernel_events
[] =
432 { event_low_memW
, sizeof(event_low_memW
) },
433 { event_low_pagedW
, sizeof(event_low_pagedW
) },
434 { event_low_nonpgW
, sizeof(event_low_nonpgW
) },
435 { event_high_memW
, sizeof(event_high_memW
) },
436 { event_high_pagedW
, sizeof(event_high_pagedW
) },
437 { event_high_nonpgW
, sizeof(event_high_nonpgW
) }
439 static const struct unicode_str keyed_event_crit_sect_str
= {keyed_event_crit_sectW
, sizeof(keyed_event_crit_sectW
)};
441 struct directory
*dir_driver
, *dir_device
, *dir_global
, *dir_basenamed
, *dir_sessions
, *dir_kernel
, *dir_windows
, *dir_winstation
;
442 struct symlink
*link_dosdev
, *link_global1
, *link_global2
, *link_local
, *link_nul
, *link_pipe
, *link_mailslot
, *link_0
, *link_session
;
443 struct keyed_event
*keyed_event
;
446 root_directory
= create_directory( NULL
, NULL
, 0, HASH_SIZE
, NULL
);
447 dir_driver
= create_directory( root_directory
, &dir_driver_str
, 0, HASH_SIZE
, NULL
);
448 dir_device
= create_directory( root_directory
, &dir_device_str
, 0, HASH_SIZE
, NULL
);
449 dir_objtype
= create_directory( root_directory
, &dir_objtype_str
, 0, HASH_SIZE
, NULL
);
450 dir_sessions
= create_directory( root_directory
, &dir_sessions_str
, 0, HASH_SIZE
, NULL
);
451 dir_kernel
= create_directory( root_directory
, &dir_kernel_str
, 0, HASH_SIZE
, NULL
);
452 dir_windows
= create_directory( root_directory
, &dir_windows_str
, 0, HASH_SIZE
, NULL
);
453 dir_winstation
= create_directory( dir_windows
, &dir_winstations_str
, 0, HASH_SIZE
, NULL
);
454 make_object_static( &root_directory
->obj
);
455 make_object_static( &dir_driver
->obj
);
456 make_object_static( &dir_objtype
->obj
);
457 make_object_static( &dir_winstation
->obj
);
459 dir_global
= create_directory( NULL
, &dir_global_str
, 0, HASH_SIZE
, NULL
);
460 /* use a larger hash table for this one since it can contain a lot of objects */
461 dir_basenamed
= create_directory( NULL
, &dir_basenamed_str
, 0, 37, NULL
);
464 create_named_pipe_device( dir_device
, &named_pipe_str
);
465 create_mailslot_device( dir_device
, &mailslot_str
);
466 create_unix_device( dir_device
, &null_str
, "/dev/null" );
469 link_dosdev
= create_symlink( root_directory
, &link_dosdev_str
, 0, &dir_global_str
, NULL
);
470 link_global1
= create_symlink( dir_global
, &link_global_str
, 0, &dir_global_str
, NULL
);
471 link_global2
= create_symlink( dir_basenamed
, &link_global_str
, 0, &dir_basenamed_str
, NULL
);
472 link_local
= create_symlink( dir_basenamed
, &link_local_str
, 0, &dir_basenamed_str
, NULL
);
473 link_nul
= create_symlink( dir_global
, &link_nul_str
, 0, &dir_null_str
, NULL
);
474 link_pipe
= create_symlink( dir_global
, &link_pipe_str
, 0, &dir_named_pipe_str
, NULL
);
475 link_mailslot
= create_symlink( dir_global
, &link_mailslot_str
, 0, &dir_mailslot_str
, NULL
);
476 link_0
= create_symlink( dir_sessions
, &link_0_str
, 0, &dir_basenamed_str
, NULL
);
477 link_session
= create_symlink( dir_basenamed
, &link_session_str
, 0, &link_sessions_str
, NULL
);
478 make_object_static( (struct object
*)link_dosdev
);
479 make_object_static( (struct object
*)link_global1
);
480 make_object_static( (struct object
*)link_global2
);
481 make_object_static( (struct object
*)link_local
);
482 make_object_static( (struct object
*)link_nul
);
483 make_object_static( (struct object
*)link_pipe
);
484 make_object_static( (struct object
*)link_mailslot
);
485 make_object_static( (struct object
*)link_0
);
486 make_object_static( (struct object
*)link_session
);
489 for (i
= 0; i
< sizeof(kernel_events
)/sizeof(kernel_events
[0]); i
++)
491 struct event
*event
= create_event( dir_kernel
, &kernel_events
[i
], 0, 1, 0, NULL
);
492 make_object_static( (struct object
*)event
);
494 keyed_event
= create_keyed_event( dir_kernel
, &keyed_event_crit_sect_str
, 0, NULL
);
495 make_object_static( (struct object
*)keyed_event
);
497 /* the objects hold references so we can release these directories */
498 release_object( dir_global
);
499 release_object( dir_device
);
500 release_object( dir_basenamed
);
501 release_object( dir_sessions
);
502 release_object( dir_kernel
);
503 release_object( dir_windows
);
506 /* create a directory object */
507 DECL_HANDLER(create_directory
)
509 struct unicode_str name
;
510 struct directory
*dir
, *root
;
511 const struct security_descriptor
*sd
;
512 const struct object_attributes
*objattr
= get_req_object_attributes( &sd
, &name
, &root
);
514 if (!objattr
) return;
516 if ((dir
= create_directory( root
, &name
, objattr
->attributes
, HASH_SIZE
, sd
)))
518 reply
->handle
= alloc_handle( current
->process
, dir
, req
->access
, objattr
->attributes
);
519 release_object( dir
);
522 if (root
) release_object( root
);
525 /* open a directory object */
526 DECL_HANDLER(open_directory
)
528 struct unicode_str name
= get_req_unicode_str();
530 reply
->handle
= open_object( current
->process
, req
->rootdir
, req
->access
,
531 &directory_ops
, &name
, req
->attributes
);
534 /* get a directory entry by index */
535 DECL_HANDLER(get_directory_entry
)
537 struct directory
*dir
= get_directory_obj( current
->process
, req
->handle
, DIRECTORY_QUERY
);
540 struct object
*obj
= find_object_index( dir
->entries
, req
->index
);
543 data_size_t name_len
, type_len
= 0;
544 const WCHAR
*type_name
= NULL
;
545 const WCHAR
*name
= get_object_name( obj
, &name_len
);
546 struct object_type
*type
= obj
->ops
->get_type( obj
);
548 if (type
) type_name
= get_object_name( &type
->obj
, &type_len
);
550 if (name_len
+ type_len
<= get_reply_max_size())
552 void *ptr
= set_reply_data_size( name_len
+ type_len
);
555 reply
->name_len
= name_len
;
556 memcpy( ptr
, name
, name_len
);
557 memcpy( (char *)ptr
+ name_len
, type_name
, type_len
);
560 else set_error( STATUS_BUFFER_OVERFLOW
);
562 if (type
) release_object( type
);
563 release_object( obj
);
565 release_object( dir
);
569 /* unlink a named object */
570 DECL_HANDLER(unlink_object
)
572 struct object
*obj
= get_handle_obj( current
->process
, req
->handle
, 0, NULL
);
576 unlink_named_object( obj
);
577 release_object( obj
);
581 /* query object type name information */
582 DECL_HANDLER(get_object_type
)
585 struct object_type
*type
;
588 if (!(obj
= get_handle_obj( current
->process
, req
->handle
, 0, NULL
))) return;
590 if ((type
= obj
->ops
->get_type( obj
)))
592 if ((name
= get_object_name( &type
->obj
, &reply
->total
)))
593 set_reply_data( name
, min( reply
->total
, get_reply_max_size() ) );
594 release_object( type
);
596 release_object( obj
);