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 directory_link_name
, /* link_name */
68 default_unlink_name
, /* 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 directory_link_name
, /* link_name */
103 default_unlink_name
, /* 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 (!name
) return NULL
; /* open the directory itself */
149 if (!(p
= memchrW( name
->str
, '\\', name
->len
/ sizeof(WCHAR
) )))
150 /* Last element in the path name */
153 tmp
.len
= (p
- name
->str
) * sizeof(WCHAR
);
156 if ((found
= find_object( dir
->entries
, &tmp
, attr
)))
158 /* Skip trailing \\ */
162 tmp
.len
+= sizeof(WCHAR
);
164 /* Move to the next element*/
166 name
->len
-= tmp
.len
;
170 if (name
->str
) /* not the last element */
172 if (tmp
.len
== 0) /* Double backslash */
173 set_error( STATUS_OBJECT_NAME_INVALID
);
174 else if (p
) /* Path still has backslashes */
175 set_error( STATUS_OBJECT_PATH_NOT_FOUND
);
180 int directory_link_name( struct object
*obj
, struct object_name
*name
, struct object
*parent
)
182 struct directory
*dir
= (struct directory
*)parent
;
184 if (parent
->ops
!= &directory_ops
)
186 set_error( STATUS_OBJECT_TYPE_MISMATCH
);
189 namespace_add( dir
->entries
, name
);
190 name
->parent
= grab_object( parent
);
194 static void directory_destroy( struct object
*obj
)
196 struct directory
*dir
= (struct directory
*)obj
;
197 assert( obj
->ops
== &directory_ops
);
198 free( dir
->entries
);
201 static struct directory
*create_directory( struct directory
*root
, const struct unicode_str
*name
,
202 unsigned int attr
, unsigned int hash_size
,
203 const struct security_descriptor
*sd
)
205 struct directory
*dir
;
207 if ((dir
= create_named_object_dir( root
, name
, attr
, &directory_ops
)) &&
208 get_error() != STATUS_OBJECT_NAME_EXISTS
)
210 if (!(dir
->entries
= create_namespace( hash_size
)))
212 release_object( dir
);
215 if (sd
) default_set_sd( &dir
->obj
, sd
, OWNER_SECURITY_INFORMATION
| GROUP_SECURITY_INFORMATION
|
216 DACL_SECURITY_INFORMATION
| SACL_SECURITY_INFORMATION
);
221 struct object
*get_root_directory(void)
223 return grab_object( root_directory
);
226 struct directory
*get_directory_obj( struct process
*process
, obj_handle_t handle
, unsigned int access
)
228 return (struct directory
*)get_handle_obj( process
, handle
, access
, &directory_ops
);
231 /******************************************************************************
232 * Find an object by its name in a given root object
235 * root [I] directory to start search from or NULL to start from \\
236 * name [I] object name to search for
237 * attr [I] OBJECT_ATTRIBUTES.Attributes
238 * name_left [O] [optional] leftover name if object is not found
241 * NULL: If params are invalid
242 * Found: If object with exact name is found returns that object
243 * (name_left->len == 0). Object's refcount is incremented
244 * Not found: The last matched parent. (name_left->len > 0)
245 * Parent's refcount is incremented.
247 struct object
*find_object_dir( struct directory
*root
, const struct unicode_str
*name
,
248 unsigned int attr
, struct unicode_str
*name_left
)
250 return lookup_named_object( &root
->obj
, name
, attr
, name_left
);
253 /* create a named (if name is present) or unnamed object. */
254 void *create_named_object_dir( struct directory
*root
, const struct unicode_str
*name
,
255 unsigned int attributes
, const struct object_ops
*ops
)
257 return create_named_object( &root
->obj
, ops
, name
, attributes
);
260 /* open a new handle to an existing object */
261 void *open_object_dir( struct directory
*root
, const struct unicode_str
*name
,
262 unsigned int attr
, const struct object_ops
*ops
)
264 return open_named_object( &root
->obj
, ops
, name
, attr
);
267 /* retrieve an object type, creating it if needed */
268 struct object_type
*get_object_type( const struct unicode_str
*name
)
270 struct object_type
*type
;
272 if ((type
= create_named_object_dir( dir_objtype
, name
, OBJ_OPENIF
, &object_type_ops
)))
274 if (get_error() != STATUS_OBJECT_NAME_EXISTS
)
277 make_object_static( &type
->obj
);
284 /* Global initialization */
286 void init_directories(void)
289 static const WCHAR dir_globalW
[] = {'\\','?','?'};
290 static const WCHAR dir_driverW
[] = {'D','r','i','v','e','r'};
291 static const WCHAR dir_deviceW
[] = {'D','e','v','i','c','e'};
292 static const WCHAR dir_nullW
[] = {'\\','D','e','v','i','c','e','\\','N','u','l','l'};
293 static const WCHAR dir_basenamedW
[] = {'\\','B','a','s','e','N','a','m','e','d','O','b','j','e','c','t','s'};
294 static const WCHAR dir_named_pipeW
[] = {'\\','D','e','v','i','c','e','\\','N','a','m','e','d','P','i','p','e'};
295 static const WCHAR dir_mailslotW
[] = {'\\','D','e','v','i','c','e','\\','M','a','i','l','S','l','o','t'};
296 static const WCHAR dir_objtypeW
[] = {'O','b','j','e','c','t','T','y','p','e','s'};
297 static const WCHAR dir_sessionsW
[] = {'S','e','s','s','i','o','n','s'};
298 static const WCHAR dir_kernelW
[] = {'K','e','r','n','e','l','O','b','j','e','c','t','s'};
299 static const WCHAR dir_windowsW
[] = {'W','i','n','d','o','w','s'};
300 static const WCHAR dir_winstationsW
[] = {'W','i','n','d','o','w','S','t','a','t','i','o','n','s'};
301 static const struct unicode_str dir_global_str
= {dir_globalW
, sizeof(dir_globalW
)};
302 static const struct unicode_str dir_driver_str
= {dir_driverW
, sizeof(dir_driverW
)};
303 static const struct unicode_str dir_device_str
= {dir_deviceW
, sizeof(dir_deviceW
)};
304 static const struct unicode_str dir_null_str
= {dir_nullW
, sizeof(dir_nullW
)};
305 static const struct unicode_str dir_basenamed_str
= {dir_basenamedW
, sizeof(dir_basenamedW
)};
306 static const struct unicode_str dir_named_pipe_str
= {dir_named_pipeW
, sizeof(dir_named_pipeW
)};
307 static const struct unicode_str dir_mailslot_str
= {dir_mailslotW
, sizeof(dir_mailslotW
)};
308 static const struct unicode_str dir_objtype_str
= {dir_objtypeW
, sizeof(dir_objtypeW
)};
309 static const struct unicode_str dir_sessions_str
= {dir_sessionsW
, sizeof(dir_sessionsW
)};
310 static const struct unicode_str dir_kernel_str
= {dir_kernelW
, sizeof(dir_kernelW
)};
311 static const struct unicode_str dir_windows_str
= {dir_windowsW
, sizeof(dir_windowsW
)};
312 static const struct unicode_str dir_winstations_str
= {dir_winstationsW
, sizeof(dir_winstationsW
)};
315 static const WCHAR link_dosdevW
[] = {'D','o','s','D','e','v','i','c','e','s'};
316 static const WCHAR link_globalW
[] = {'G','l','o','b','a','l'};
317 static const WCHAR link_localW
[] = {'L','o','c','a','l'};
318 static const WCHAR link_nulW
[] = {'N','U','L'};
319 static const WCHAR link_pipeW
[] = {'P','I','P','E'};
320 static const WCHAR link_mailslotW
[] = {'M','A','I','L','S','L','O','T'};
321 static const WCHAR link_0W
[] = {'0'};
322 static const WCHAR link_sessionW
[] = {'S','e','s','s','i','o','n'};
323 static const WCHAR link_sessionsW
[] = {'\\','S','e','s','s','i','o','n','s'};
324 static const struct unicode_str link_dosdev_str
= {link_dosdevW
, sizeof(link_dosdevW
)};
325 static const struct unicode_str link_global_str
= {link_globalW
, sizeof(link_globalW
)};
326 static const struct unicode_str link_local_str
= {link_localW
, sizeof(link_localW
)};
327 static const struct unicode_str link_nul_str
= {link_nulW
, sizeof(link_nulW
)};
328 static const struct unicode_str link_pipe_str
= {link_pipeW
, sizeof(link_pipeW
)};
329 static const struct unicode_str link_mailslot_str
= {link_mailslotW
, sizeof(link_mailslotW
)};
330 static const struct unicode_str link_0_str
= {link_0W
, sizeof(link_0W
)};
331 static const struct unicode_str link_session_str
= {link_sessionW
, sizeof(link_sessionW
)};
332 static const struct unicode_str link_sessions_str
= {link_sessionsW
, sizeof(link_sessionsW
)};
335 static const WCHAR named_pipeW
[] = {'N','a','m','e','d','P','i','p','e'};
336 static const WCHAR mailslotW
[] = {'M','a','i','l','S','l','o','t'};
337 static const WCHAR nullW
[] = {'N','u','l','l'};
338 static const struct unicode_str named_pipe_str
= {named_pipeW
, sizeof(named_pipeW
)};
339 static const struct unicode_str mailslot_str
= {mailslotW
, sizeof(mailslotW
)};
340 static const struct unicode_str null_str
= {nullW
, sizeof(nullW
)};
343 static const WCHAR event_low_memW
[] = {'L','o','w','M','e','m','o','r','y','C','o','n','d','i','t','i','o','n'};
344 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'};
345 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'};
346 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'};
347 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'};
348 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'};
349 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'};
350 static const struct unicode_str kernel_events
[] =
352 { event_low_memW
, sizeof(event_low_memW
) },
353 { event_low_pagedW
, sizeof(event_low_pagedW
) },
354 { event_low_nonpgW
, sizeof(event_low_nonpgW
) },
355 { event_high_memW
, sizeof(event_high_memW
) },
356 { event_high_pagedW
, sizeof(event_high_pagedW
) },
357 { event_high_nonpgW
, sizeof(event_high_nonpgW
) }
359 static const struct unicode_str keyed_event_crit_sect_str
= {keyed_event_crit_sectW
, sizeof(keyed_event_crit_sectW
)};
361 struct directory
*dir_driver
, *dir_device
, *dir_global
, *dir_basenamed
, *dir_sessions
, *dir_kernel
, *dir_windows
, *dir_winstation
;
362 struct symlink
*link_dosdev
, *link_global1
, *link_global2
, *link_local
, *link_nul
, *link_pipe
, *link_mailslot
, *link_0
, *link_session
;
363 struct keyed_event
*keyed_event
;
366 root_directory
= create_directory( NULL
, NULL
, 0, HASH_SIZE
, NULL
);
367 dir_driver
= create_directory( root_directory
, &dir_driver_str
, 0, HASH_SIZE
, NULL
);
368 dir_device
= create_directory( root_directory
, &dir_device_str
, 0, HASH_SIZE
, NULL
);
369 dir_objtype
= create_directory( root_directory
, &dir_objtype_str
, 0, HASH_SIZE
, NULL
);
370 dir_sessions
= create_directory( root_directory
, &dir_sessions_str
, 0, HASH_SIZE
, NULL
);
371 dir_kernel
= create_directory( root_directory
, &dir_kernel_str
, 0, HASH_SIZE
, NULL
);
372 dir_windows
= create_directory( root_directory
, &dir_windows_str
, 0, HASH_SIZE
, NULL
);
373 dir_winstation
= create_directory( dir_windows
, &dir_winstations_str
, 0, HASH_SIZE
, NULL
);
374 make_object_static( &root_directory
->obj
);
375 make_object_static( &dir_driver
->obj
);
376 make_object_static( &dir_objtype
->obj
);
377 make_object_static( &dir_winstation
->obj
);
379 dir_global
= create_directory( NULL
, &dir_global_str
, 0, HASH_SIZE
, NULL
);
380 /* use a larger hash table for this one since it can contain a lot of objects */
381 dir_basenamed
= create_directory( NULL
, &dir_basenamed_str
, 0, 37, NULL
);
384 create_named_pipe_device( dir_device
, &named_pipe_str
);
385 create_mailslot_device( dir_device
, &mailslot_str
);
386 create_unix_device( dir_device
, &null_str
, "/dev/null" );
389 link_dosdev
= create_symlink( root_directory
, &link_dosdev_str
, 0, &dir_global_str
, NULL
);
390 link_global1
= create_symlink( dir_global
, &link_global_str
, 0, &dir_global_str
, NULL
);
391 link_global2
= create_symlink( dir_basenamed
, &link_global_str
, 0, &dir_basenamed_str
, NULL
);
392 link_local
= create_symlink( dir_basenamed
, &link_local_str
, 0, &dir_basenamed_str
, NULL
);
393 link_nul
= create_symlink( dir_global
, &link_nul_str
, 0, &dir_null_str
, NULL
);
394 link_pipe
= create_symlink( dir_global
, &link_pipe_str
, 0, &dir_named_pipe_str
, NULL
);
395 link_mailslot
= create_symlink( dir_global
, &link_mailslot_str
, 0, &dir_mailslot_str
, NULL
);
396 link_0
= create_symlink( dir_sessions
, &link_0_str
, 0, &dir_basenamed_str
, NULL
);
397 link_session
= create_symlink( dir_basenamed
, &link_session_str
, 0, &link_sessions_str
, NULL
);
398 make_object_static( (struct object
*)link_dosdev
);
399 make_object_static( (struct object
*)link_global1
);
400 make_object_static( (struct object
*)link_global2
);
401 make_object_static( (struct object
*)link_local
);
402 make_object_static( (struct object
*)link_nul
);
403 make_object_static( (struct object
*)link_pipe
);
404 make_object_static( (struct object
*)link_mailslot
);
405 make_object_static( (struct object
*)link_0
);
406 make_object_static( (struct object
*)link_session
);
409 for (i
= 0; i
< sizeof(kernel_events
)/sizeof(kernel_events
[0]); i
++)
411 struct event
*event
= create_event( dir_kernel
, &kernel_events
[i
], 0, 1, 0, NULL
);
412 make_object_static( (struct object
*)event
);
414 keyed_event
= create_keyed_event( dir_kernel
, &keyed_event_crit_sect_str
, 0, NULL
);
415 make_object_static( (struct object
*)keyed_event
);
417 /* the objects hold references so we can release these directories */
418 release_object( dir_global
);
419 release_object( dir_device
);
420 release_object( dir_basenamed
);
421 release_object( dir_sessions
);
422 release_object( dir_kernel
);
423 release_object( dir_windows
);
426 /* create a directory object */
427 DECL_HANDLER(create_directory
)
429 struct unicode_str name
;
430 struct directory
*dir
, *root
;
431 const struct security_descriptor
*sd
;
432 const struct object_attributes
*objattr
= get_req_object_attributes( &sd
, &name
, &root
);
434 if (!objattr
) return;
436 if ((dir
= create_directory( root
, &name
, objattr
->attributes
, HASH_SIZE
, sd
)))
438 reply
->handle
= alloc_handle( current
->process
, dir
, req
->access
, objattr
->attributes
);
439 release_object( dir
);
442 if (root
) release_object( root
);
445 /* open a directory object */
446 DECL_HANDLER(open_directory
)
448 struct unicode_str name
= get_req_unicode_str();
450 reply
->handle
= open_object( current
->process
, req
->rootdir
, req
->access
,
451 &directory_ops
, &name
, req
->attributes
);
454 /* get a directory entry by index */
455 DECL_HANDLER(get_directory_entry
)
457 struct directory
*dir
= get_directory_obj( current
->process
, req
->handle
, DIRECTORY_QUERY
);
460 struct object
*obj
= find_object_index( dir
->entries
, req
->index
);
463 data_size_t name_len
, type_len
= 0;
464 const WCHAR
*type_name
= NULL
;
465 const WCHAR
*name
= get_object_name( obj
, &name_len
);
466 struct object_type
*type
= obj
->ops
->get_type( obj
);
468 if (type
) type_name
= get_object_name( &type
->obj
, &type_len
);
470 if (name_len
+ type_len
<= get_reply_max_size())
472 void *ptr
= set_reply_data_size( name_len
+ type_len
);
475 reply
->name_len
= name_len
;
476 memcpy( ptr
, name
, name_len
);
477 memcpy( (char *)ptr
+ name_len
, type_name
, type_len
);
480 else set_error( STATUS_BUFFER_OVERFLOW
);
482 if (type
) release_object( type
);
483 release_object( obj
);
485 release_object( dir
);
489 /* unlink a named object */
490 DECL_HANDLER(unlink_object
)
492 struct object
*obj
= get_handle_obj( current
->process
, req
->handle
, 0, NULL
);
496 unlink_named_object( obj
);
497 release_object( obj
);
501 /* query object type name information */
502 DECL_HANDLER(get_object_type
)
505 struct object_type
*type
;
508 if (!(obj
= get_handle_obj( current
->process
, req
->handle
, 0, NULL
))) return;
510 if ((type
= obj
->ops
->get_type( obj
)))
512 if ((name
= get_object_name( &type
->obj
, &reply
->total
)))
513 set_reply_data( name
, min( reply
->total
, get_reply_max_size() ) );
514 release_object( type
);
516 release_object( obj
);