handle.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558
  1. /*
  2. * Server-side handle management
  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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. */
  20. #include "config.h"
  21. #include "wine/port.h"
  22. #include <assert.h>
  23. #include <limits.h>
  24. #include <string.h>
  25. #include <stdarg.h>
  26. #include <stdio.h>
  27. #include <stdlib.h>
  28. #include "windef.h"
  29. #include "winbase.h"
  30. #include "handle.h"
  31. #include "process.h"
  32. #include "thread.h"
  33. #include "request.h"
  34. struct handle_entry
  35. {
  36. struct object *ptr; /* object */
  37. unsigned int access; /* access rights */
  38. int fd; /* file descriptor (in client process) */
  39. };
  40. struct handle_table
  41. {
  42. struct object obj; /* object header */
  43. struct process *process; /* process owning this table */
  44. int count; /* number of allocated entries */
  45. int last; /* last used entry */
  46. int free; /* first entry that may be free */
  47. struct handle_entry *entries; /* handle entries */
  48. };
  49. static struct handle_table *global_table;
  50. /* reserved handle access rights */
  51. #define RESERVED_SHIFT 25
  52. #define RESERVED_INHERIT (HANDLE_FLAG_INHERIT << RESERVED_SHIFT)
  53. #define RESERVED_CLOSE_PROTECT (HANDLE_FLAG_PROTECT_FROM_CLOSE << RESERVED_SHIFT)
  54. #define RESERVED_ALL (RESERVED_INHERIT | RESERVED_CLOSE_PROTECT)
  55. #define MIN_HANDLE_ENTRIES 32
  56. /* handle to table index conversion */
  57. /* handles are a multiple of 4 under NT; handle 0 is not used */
  58. inline static obj_handle_t index_to_handle( int index )
  59. {
  60. return (obj_handle_t)((index + 1) << 2);
  61. }
  62. inline static int handle_to_index( obj_handle_t handle )
  63. {
  64. return ((unsigned int)handle >> 2) - 1;
  65. }
  66. /* global handle conversion */
  67. #define HANDLE_OBFUSCATOR 0x544a4def
  68. inline static int handle_is_global( obj_handle_t handle)
  69. {
  70. return ((unsigned long)handle ^ HANDLE_OBFUSCATOR) < 0x10000;
  71. }
  72. inline static obj_handle_t handle_local_to_global( obj_handle_t handle )
  73. {
  74. if (!handle) return 0;
  75. return (obj_handle_t)((unsigned long)handle ^ HANDLE_OBFUSCATOR);
  76. }
  77. inline static obj_handle_t handle_global_to_local( obj_handle_t handle )
  78. {
  79. return (obj_handle_t)((unsigned long)handle ^ HANDLE_OBFUSCATOR);
  80. }
  81. static void handle_table_dump( struct object *obj, int verbose );
  82. static void handle_table_destroy( struct object *obj );
  83. static const struct object_ops handle_table_ops =
  84. {
  85. sizeof(struct handle_table), /* size */
  86. handle_table_dump, /* dump */
  87. no_add_queue, /* add_queue */
  88. NULL, /* remove_queue */
  89. NULL, /* signaled */
  90. NULL, /* satisfied */
  91. no_get_fd, /* get_fd */
  92. handle_table_destroy /* destroy */
  93. };
  94. /* dump a handle table */
  95. static void handle_table_dump( struct object *obj, int verbose )
  96. {
  97. int i;
  98. struct handle_table *table = (struct handle_table *)obj;
  99. struct handle_entry *entry = table->entries;
  100. assert( obj->ops == &handle_table_ops );
  101. fprintf( stderr, "Handle table last=%d count=%d process=%p\n",
  102. table->last, table->count, table->process );
  103. if (!verbose) return;
  104. entry = table->entries;
  105. for (i = 0; i <= table->last; i++, entry++)
  106. {
  107. if (!entry->ptr) continue;
  108. fprintf( stderr, "%9u: %p %08x ",
  109. (unsigned int)index_to_handle(i), entry->ptr, entry->access );
  110. entry->ptr->ops->dump( entry->ptr, 0 );
  111. }
  112. }
  113. /* destroy a handle table */
  114. static void handle_table_destroy( struct object *obj )
  115. {
  116. int i;
  117. struct handle_table *table = (struct handle_table *)obj;
  118. struct handle_entry *entry = table->entries;
  119. assert( obj->ops == &handle_table_ops );
  120. for (i = 0; i <= table->last; i++, entry++)
  121. {
  122. struct object *obj = entry->ptr;
  123. entry->ptr = NULL;
  124. if (obj) release_object( obj );
  125. }
  126. free( table->entries );
  127. }
  128. /* allocate a new handle table */
  129. struct handle_table *alloc_handle_table( struct process *process, int count )
  130. {
  131. struct handle_table *table;
  132. if (count < MIN_HANDLE_ENTRIES) count = MIN_HANDLE_ENTRIES;
  133. if (!(table = alloc_object( &handle_table_ops )))
  134. return NULL;
  135. table->process = process;
  136. table->count = count;
  137. table->last = -1;
  138. table->free = 0;
  139. if ((table->entries = mem_alloc( count * sizeof(*table->entries) ))) return table;
  140. release_object( table );
  141. return NULL;
  142. }
  143. /* grow a handle table */
  144. static int grow_handle_table( struct handle_table *table )
  145. {
  146. struct handle_entry *new_entries;
  147. int count = table->count;
  148. if (count >= INT_MAX / 2) return 0;
  149. count *= 2;
  150. if (!(new_entries = realloc( table->entries, count * sizeof(struct handle_entry) )))
  151. {
  152. set_error( STATUS_NO_MEMORY );
  153. return 0;
  154. }
  155. table->entries = new_entries;
  156. table->count = count;
  157. return 1;
  158. }
  159. /* allocate the first free entry in the handle table */
  160. static obj_handle_t alloc_entry( struct handle_table *table, void *obj, unsigned int access )
  161. {
  162. struct handle_entry *entry = table->entries + table->free;
  163. int i;
  164. for (i = table->free; i <= table->last; i++, entry++) if (!entry->ptr) goto found;
  165. if (i >= table->count)
  166. {
  167. if (!grow_handle_table( table )) return 0;
  168. entry = table->entries + i; /* the entries may have moved */
  169. }
  170. table->last = i;
  171. found:
  172. table->free = i + 1;
  173. entry->ptr = grab_object( obj );
  174. entry->access = access;
  175. entry->fd = -1;
  176. return index_to_handle(i);
  177. }
  178. /* allocate a handle for an object, incrementing its refcount */
  179. /* return the handle, or 0 on error */
  180. obj_handle_t alloc_handle( struct process *process, void *obj, unsigned int access, int inherit )
  181. {
  182. struct handle_table *table = process->handles;
  183. assert( table );
  184. assert( !(access & RESERVED_ALL) );
  185. if (inherit) access |= RESERVED_INHERIT;
  186. return alloc_entry( table, obj, access );
  187. }
  188. /* allocate a global handle for an object, incrementing its refcount */
  189. /* return the handle, or 0 on error */
  190. static obj_handle_t alloc_global_handle( void *obj, unsigned int access )
  191. {
  192. if (!global_table)
  193. {
  194. if (!(global_table = (struct handle_table *)alloc_handle_table( NULL, 0 )))
  195. return 0;
  196. }
  197. return handle_local_to_global( alloc_entry( global_table, obj, access ));
  198. }
  199. /* return a handle entry, or NULL if the handle is invalid */
  200. static struct handle_entry *get_handle( struct process *process, obj_handle_t handle )
  201. {
  202. struct handle_table *table = process->handles;
  203. struct handle_entry *entry;
  204. int index;
  205. if (handle_is_global(handle))
  206. {
  207. handle = handle_global_to_local(handle);
  208. table = global_table;
  209. }
  210. if (!table) goto error;
  211. index = handle_to_index( handle );
  212. if (index < 0) goto error;
  213. if (index > table->last) goto error;
  214. entry = table->entries + index;
  215. if (!entry->ptr) goto error;
  216. return entry;
  217. error:
  218. set_error( STATUS_INVALID_HANDLE );
  219. return NULL;
  220. }
  221. /* attempt to shrink a table */
  222. static void shrink_handle_table( struct handle_table *table )
  223. {
  224. struct handle_entry *entry = table->entries + table->last;
  225. struct handle_entry *new_entries;
  226. int count = table->count;
  227. while (table->last >= 0)
  228. {
  229. if (entry->ptr) break;
  230. table->last--;
  231. entry--;
  232. }
  233. if (table->last >= count / 4) return; /* no need to shrink */
  234. if (count < MIN_HANDLE_ENTRIES * 2) return; /* too small to shrink */
  235. count /= 2;
  236. if (!(new_entries = realloc( table->entries, count * sizeof(*new_entries) ))) return;
  237. table->count = count;
  238. table->entries = new_entries;
  239. }
  240. /* copy the handle table of the parent process */
  241. /* return 1 if OK, 0 on error */
  242. struct handle_table *copy_handle_table( struct process *process, struct process *parent )
  243. {
  244. struct handle_table *parent_table = parent->handles;
  245. struct handle_table *table;
  246. int i;
  247. assert( parent_table );
  248. assert( parent_table->obj.ops == &handle_table_ops );
  249. if (!(table = (struct handle_table *)alloc_handle_table( process, parent_table->count )))
  250. return NULL;
  251. if ((table->last = parent_table->last) >= 0)
  252. {
  253. struct handle_entry *ptr = table->entries;
  254. memcpy( ptr, parent_table->entries, (table->last + 1) * sizeof(struct handle_entry) );
  255. for (i = 0; i <= table->last; i++, ptr++)
  256. {
  257. if (!ptr->ptr) continue;
  258. ptr->fd = -1;
  259. if (ptr->access & RESERVED_INHERIT) grab_object( ptr->ptr );
  260. else ptr->ptr = NULL; /* don't inherit this entry */
  261. }
  262. }
  263. /* attempt to shrink the table */
  264. shrink_handle_table( table );
  265. return table;
  266. }
  267. /* close a handle and decrement the refcount of the associated object */
  268. /* return 1 if OK, 0 on error */
  269. int close_handle( struct process *process, obj_handle_t handle, int *fd )
  270. {
  271. struct handle_table *table;
  272. struct handle_entry *entry;
  273. struct object *obj;
  274. if (!(entry = get_handle( process, handle ))) return 0;
  275. if (entry->access & RESERVED_CLOSE_PROTECT)
  276. {
  277. set_error( STATUS_INVALID_HANDLE );
  278. return 0;
  279. }
  280. obj = entry->ptr;
  281. entry->ptr = NULL;
  282. if (fd) *fd = entry->fd;
  283. else if (entry->fd != -1) return 1; /* silently ignore close attempt if we cannot close the fd */
  284. entry->fd = -1;
  285. table = handle_is_global(handle) ? global_table : process->handles;
  286. if (entry < table->entries + table->free) table->free = entry - table->entries;
  287. if (entry == table->entries + table->last) shrink_handle_table( table );
  288. /* hack: windows seems to treat registry handles differently */
  289. registry_close_handle( obj, handle );
  290. release_object( obj );
  291. return 1;
  292. }
  293. /* close all the global handles */
  294. void close_global_handles(void)
  295. {
  296. if (global_table)
  297. {
  298. release_object( global_table );
  299. global_table = NULL;
  300. }
  301. }
  302. /* retrieve the object corresponding to one of the magic pseudo-handles */
  303. static inline struct object *get_magic_handle( obj_handle_t handle )
  304. {
  305. switch((unsigned long)handle)
  306. {
  307. case 0xfffffffe: /* current thread pseudo-handle */
  308. return &current->obj;
  309. case 0x7fffffff: /* current process pseudo-handle */
  310. case 0xffffffff: /* current process pseudo-handle */
  311. return (struct object *)current->process;
  312. default:
  313. return NULL;
  314. }
  315. }
  316. /* retrieve the object corresponding to a handle, incrementing its refcount */
  317. struct object *get_handle_obj( struct process *process, obj_handle_t handle,
  318. unsigned int access, const struct object_ops *ops )
  319. {
  320. struct handle_entry *entry;
  321. struct object *obj;
  322. if (!(obj = get_magic_handle( handle )))
  323. {
  324. if (!(entry = get_handle( process, handle ))) return NULL;
  325. if ((entry->access & access) != access)
  326. {
  327. set_error( STATUS_ACCESS_DENIED );
  328. return NULL;
  329. }
  330. obj = entry->ptr;
  331. }
  332. if (ops && (obj->ops != ops))
  333. {
  334. set_error( STATUS_OBJECT_TYPE_MISMATCH ); /* not the right type */
  335. return NULL;
  336. }
  337. return grab_object( obj );
  338. }
  339. /* retrieve the cached fd for a given handle */
  340. int get_handle_unix_fd( struct process *process, obj_handle_t handle, unsigned int access )
  341. {
  342. struct handle_entry *entry;
  343. if (!(entry = get_handle( process, handle ))) return -1;
  344. if ((entry->access & access) != access)
  345. {
  346. set_error( STATUS_ACCESS_DENIED );
  347. return -1;
  348. }
  349. return entry->fd;
  350. }
  351. /* remove the cached fd and return it */
  352. int flush_cached_fd( struct process *process, obj_handle_t handle )
  353. {
  354. struct handle_entry *entry = get_handle( process, handle );
  355. int fd = -1;
  356. if (entry)
  357. {
  358. fd = entry->fd;
  359. entry->fd = -1;
  360. }
  361. return fd;
  362. }
  363. /* find the first inherited handle of the given type */
  364. /* this is needed for window stations and desktops (don't ask...) */
  365. obj_handle_t find_inherited_handle( struct process *process, const struct object_ops *ops )
  366. {
  367. struct handle_table *table = process->handles;
  368. struct handle_entry *ptr;
  369. int i;
  370. if (!table) return 0;
  371. for (i = 0, ptr = table->entries; i <= table->last; i++, ptr++)
  372. {
  373. if (!ptr->ptr) continue;
  374. if (ptr->ptr->ops != ops) continue;
  375. if (ptr->access & RESERVED_INHERIT) return index_to_handle(i);
  376. }
  377. return 0;
  378. }
  379. /* get/set the handle reserved flags */
  380. /* return the old flags (or -1 on error) */
  381. int set_handle_info( struct process *process, obj_handle_t handle, int mask, int flags, int *fd )
  382. {
  383. struct handle_entry *entry;
  384. unsigned int old_access;
  385. if (get_magic_handle( handle ))
  386. {
  387. /* we can retrieve but not set info for magic handles */
  388. if (mask) set_error( STATUS_ACCESS_DENIED );
  389. return 0;
  390. }
  391. if (!(entry = get_handle( process, handle ))) return -1;
  392. old_access = entry->access;
  393. mask = (mask << RESERVED_SHIFT) & RESERVED_ALL;
  394. flags = (flags << RESERVED_SHIFT) & mask;
  395. entry->access = (entry->access & ~mask) | flags;
  396. /* if no current fd set it, otherwise return current fd */
  397. if (entry->fd == -1) entry->fd = *fd;
  398. *fd = entry->fd;
  399. return (old_access & RESERVED_ALL) >> RESERVED_SHIFT;
  400. }
  401. /* duplicate a handle */
  402. obj_handle_t duplicate_handle( struct process *src, obj_handle_t src_handle, struct process *dst,
  403. unsigned int access, int inherit, int options )
  404. {
  405. obj_handle_t res;
  406. struct object *obj = get_handle_obj( src, src_handle, 0, NULL );
  407. if (!obj) return 0;
  408. if (options & DUP_HANDLE_SAME_ACCESS)
  409. {
  410. struct handle_entry *entry = get_handle( src, src_handle );
  411. if (entry)
  412. access = entry->access;
  413. else /* pseudo-handle, give it full access */
  414. {
  415. access = STANDARD_RIGHTS_ALL | SPECIFIC_RIGHTS_ALL;
  416. clear_error();
  417. }
  418. }
  419. access &= ~RESERVED_ALL;
  420. if (options & DUP_HANDLE_MAKE_GLOBAL)
  421. res = alloc_global_handle( obj, access );
  422. else
  423. res = alloc_handle( dst, obj, access, inherit );
  424. release_object( obj );
  425. return res;
  426. }
  427. /* open a new handle to an existing object */
  428. obj_handle_t open_object( const struct namespace *namespace, const WCHAR *name, size_t len,
  429. const struct object_ops *ops, unsigned int access, int inherit )
  430. {
  431. obj_handle_t handle = 0;
  432. struct object *obj = find_object( namespace, name, len );
  433. if (obj)
  434. {
  435. if (ops && obj->ops != ops)
  436. set_error( STATUS_OBJECT_TYPE_MISMATCH );
  437. else
  438. handle = alloc_handle( current->process, obj, access, inherit );
  439. release_object( obj );
  440. }
  441. else
  442. set_error( STATUS_OBJECT_NAME_NOT_FOUND );
  443. return handle;
  444. }
  445. /* return the size of the handle table of a given process */
  446. unsigned int get_handle_table_count( struct process *process )
  447. {
  448. return process->handles->count;
  449. }
  450. /* close a handle */
  451. DECL_HANDLER(close_handle)
  452. {
  453. close_handle( current->process, req->handle, &reply->fd );
  454. }
  455. /* set a handle information */
  456. DECL_HANDLER(set_handle_info)
  457. {
  458. int fd = req->fd;
  459. if (handle_is_global(req->handle)) fd = -1; /* no fd cache for global handles */
  460. reply->old_flags = set_handle_info( current->process, req->handle,
  461. req->mask, req->flags, &fd );
  462. reply->cur_fd = fd;
  463. }
  464. /* duplicate a handle */
  465. DECL_HANDLER(dup_handle)
  466. {
  467. struct process *src, *dst;
  468. reply->handle = 0;
  469. reply->fd = -1;
  470. if ((src = get_process_from_handle( req->src_process, PROCESS_DUP_HANDLE )))
  471. {
  472. if (req->options & DUP_HANDLE_MAKE_GLOBAL)
  473. {
  474. reply->handle = duplicate_handle( src, req->src_handle, NULL,
  475. req->access, req->inherit, req->options );
  476. }
  477. else if ((dst = get_process_from_handle( req->dst_process, PROCESS_DUP_HANDLE )))
  478. {
  479. reply->handle = duplicate_handle( src, req->src_handle, dst,
  480. req->access, req->inherit, req->options );
  481. release_object( dst );
  482. }
  483. /* close the handle no matter what happened */
  484. if (req->options & DUP_HANDLE_CLOSE_SOURCE)
  485. {
  486. if (src == current->process) close_handle( src, req->src_handle, &reply->fd );
  487. else close_handle( src, req->src_handle, NULL );
  488. }
  489. release_object( src );
  490. }
  491. }