hook.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598
  1. /*
  2. * Server-side window hooks support
  3. *
  4. * Copyright (C) 2002 Alexandre Julliard
  5. * Copyright (C) 2005 Dmitry Timoshkov
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this library; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
  20. */
  21. #include "config.h"
  22. #include <assert.h>
  23. #include <stdarg.h>
  24. #include <stdio.h>
  25. #include "ntstatus.h"
  26. #define WIN32_NO_STATUS
  27. #include "windef.h"
  28. #include "winbase.h"
  29. #include "winuser.h"
  30. #include "winternl.h"
  31. #include "object.h"
  32. #include "process.h"
  33. #include "request.h"
  34. #include "user.h"
  35. struct hook_table;
  36. struct hook
  37. {
  38. struct list chain; /* hook chain entry */
  39. user_handle_t handle; /* user handle for this hook */
  40. struct process *process; /* process the hook is set to */
  41. struct thread *thread; /* thread the hook is set to */
  42. struct thread *owner; /* owner of the out of context hook */
  43. struct hook_table *table; /* hook table that contains this hook */
  44. int index; /* hook table index */
  45. int event_min;
  46. int event_max;
  47. int flags;
  48. client_ptr_t proc; /* hook function */
  49. int unicode; /* is it a unicode hook? */
  50. WCHAR *module; /* module name for global hooks */
  51. data_size_t module_size;
  52. };
  53. #define WH_WINEVENT (WH_MAXHOOK+1)
  54. #define NB_HOOKS (WH_WINEVENT-WH_MINHOOK+1)
  55. #define HOOK_ENTRY(p) LIST_ENTRY( (p), struct hook, chain )
  56. struct hook_table
  57. {
  58. struct object obj; /* object header */
  59. struct list hooks[NB_HOOKS]; /* array of hook chains */
  60. int counts[NB_HOOKS]; /* use counts for each hook chain */
  61. };
  62. static void hook_table_dump( struct object *obj, int verbose );
  63. static void hook_table_destroy( struct object *obj );
  64. static const struct object_ops hook_table_ops =
  65. {
  66. sizeof(struct hook_table), /* size */
  67. &no_type, /* type */
  68. hook_table_dump, /* dump */
  69. no_add_queue, /* add_queue */
  70. NULL, /* remove_queue */
  71. NULL, /* signaled */
  72. NULL, /* satisfied */
  73. no_signal, /* signal */
  74. no_get_fd, /* get_fd */
  75. default_map_access, /* map_access */
  76. default_get_sd, /* get_sd */
  77. default_set_sd, /* set_sd */
  78. no_get_full_name, /* get_full_name */
  79. no_lookup_name, /* lookup_name */
  80. no_link_name, /* link_name */
  81. NULL, /* unlink_name */
  82. no_open_file, /* open_file */
  83. no_kernel_obj_list, /* get_kernel_obj_list */
  84. no_close_handle, /* close_handle */
  85. hook_table_destroy /* destroy */
  86. };
  87. /* create a new hook table */
  88. static struct hook_table *alloc_hook_table(void)
  89. {
  90. struct hook_table *table;
  91. int i;
  92. if ((table = alloc_object( &hook_table_ops )))
  93. {
  94. for (i = 0; i < NB_HOOKS; i++)
  95. {
  96. list_init( &table->hooks[i] );
  97. table->counts[i] = 0;
  98. }
  99. }
  100. return table;
  101. }
  102. static struct hook_table *get_global_hooks( struct thread *thread )
  103. {
  104. struct hook_table *table;
  105. struct desktop *desktop;
  106. if (!thread->desktop) return NULL;
  107. if (!(desktop = get_thread_desktop( thread, 0 ))) return NULL;
  108. table = desktop->global_hooks;
  109. release_object( desktop );
  110. return table;
  111. }
  112. /* create a new hook and add it to the specified table */
  113. static struct hook *add_hook( struct desktop *desktop, struct thread *thread, int index, int global )
  114. {
  115. struct hook *hook;
  116. struct hook_table *table = global ? desktop->global_hooks : get_queue_hooks(thread);
  117. if (!table)
  118. {
  119. if (!(table = alloc_hook_table())) return NULL;
  120. if (global) desktop->global_hooks = table;
  121. else set_queue_hooks( thread, table );
  122. }
  123. if (!(hook = mem_alloc( sizeof(*hook) ))) return NULL;
  124. if (!(hook->handle = alloc_user_handle( hook, USER_HOOK )))
  125. {
  126. free( hook );
  127. return NULL;
  128. }
  129. hook->thread = thread ? (struct thread *)grab_object( thread ) : NULL;
  130. hook->table = table;
  131. hook->index = index;
  132. list_add_head( &table->hooks[index], &hook->chain );
  133. if (thread) thread->desktop_users++;
  134. return hook;
  135. }
  136. /* free a hook, removing it from its chain */
  137. static void free_hook( struct hook *hook )
  138. {
  139. free_user_handle( hook->handle );
  140. free( hook->module );
  141. if (hook->thread)
  142. {
  143. assert( hook->thread->desktop_users > 0 );
  144. hook->thread->desktop_users--;
  145. release_object( hook->thread );
  146. }
  147. if (hook->process) release_object( hook->process );
  148. release_object( hook->owner );
  149. list_remove( &hook->chain );
  150. free( hook );
  151. }
  152. /* find a hook from its index and proc */
  153. static struct hook *find_hook( struct thread *thread, int index, client_ptr_t proc )
  154. {
  155. struct list *p;
  156. struct hook_table *table = get_queue_hooks( thread );
  157. if (table)
  158. {
  159. LIST_FOR_EACH( p, &table->hooks[index] )
  160. {
  161. struct hook *hook = HOOK_ENTRY( p );
  162. if (hook->proc == proc) return hook;
  163. }
  164. }
  165. return NULL;
  166. }
  167. /* get the first hook in the chain */
  168. static inline struct hook *get_first_hook( struct hook_table *table, int index )
  169. {
  170. struct list *elem = list_head( &table->hooks[index] );
  171. return elem ? HOOK_ENTRY( elem ) : NULL;
  172. }
  173. /* check if a given hook should run in the owner thread instead of the current thread */
  174. static inline int run_hook_in_owner_thread( struct hook *hook )
  175. {
  176. if ((hook->index == WH_MOUSE_LL - WH_MINHOOK ||
  177. hook->index == WH_KEYBOARD_LL - WH_MINHOOK))
  178. return hook->owner != current;
  179. return 0;
  180. }
  181. /* check if a given hook should run in the current thread */
  182. static inline int run_hook_in_current_thread( struct hook *hook )
  183. {
  184. if (hook->process && hook->process != current->process) return 0;
  185. if ((hook->flags & WINEVENT_SKIPOWNPROCESS) && hook->process == current->process) return 0;
  186. if (hook->thread && hook->thread != current) return 0;
  187. if ((hook->flags & WINEVENT_SKIPOWNTHREAD) && hook->thread == current) return 0;
  188. /* don't run low-level hooks in processes suspended for debugging */
  189. if (run_hook_in_owner_thread( hook ) && hook->owner->process->suspend) return 0;
  190. return 1;
  191. }
  192. /* find the first non-deleted hook in the chain */
  193. static inline struct hook *get_first_valid_hook( struct hook_table *table, int index,
  194. int event, user_handle_t win,
  195. int object_id, int child_id )
  196. {
  197. struct hook *hook = get_first_hook( table, index );
  198. while (hook)
  199. {
  200. if (hook->proc && run_hook_in_current_thread( hook ))
  201. {
  202. if (event >= hook->event_min && event <= hook->event_max)
  203. {
  204. if (hook->flags & WINEVENT_INCONTEXT) return hook;
  205. /* only winevent hooks may be out of context */
  206. assert(hook->index + WH_MINHOOK == WH_WINEVENT);
  207. post_win_event( hook->owner, event, win, object_id, child_id,
  208. hook->proc, hook->module, hook->module_size,
  209. hook->handle );
  210. }
  211. }
  212. hook = HOOK_ENTRY( list_next( &table->hooks[index], &hook->chain ) );
  213. }
  214. return hook;
  215. }
  216. /* find the next hook in the chain, skipping the deleted ones */
  217. static struct hook *get_next_hook( struct thread *thread, struct hook *hook, int event,
  218. user_handle_t win, int object_id, int child_id )
  219. {
  220. struct hook_table *global_hooks, *table = hook->table;
  221. int index = hook->index;
  222. while ((hook = HOOK_ENTRY( list_next( &table->hooks[index], &hook->chain ) )))
  223. {
  224. if (hook->proc && run_hook_in_current_thread( hook ))
  225. {
  226. if (event >= hook->event_min && event <= hook->event_max)
  227. {
  228. if (hook->flags & WINEVENT_INCONTEXT) return hook;
  229. /* only winevent hooks may be out of context */
  230. assert(hook->index + WH_MINHOOK == WH_WINEVENT);
  231. post_win_event( hook->owner, event, win, object_id, child_id,
  232. hook->proc, hook->module, hook->module_size,
  233. hook->handle );
  234. }
  235. }
  236. }
  237. global_hooks = get_global_hooks( thread );
  238. if (global_hooks && table != global_hooks) /* now search through the global table */
  239. {
  240. hook = get_first_valid_hook( global_hooks, index, event, win, object_id, child_id );
  241. }
  242. return hook;
  243. }
  244. static void hook_table_dump( struct object *obj, int verbose )
  245. {
  246. /* struct hook_table *table = (struct hook_table *)obj; */
  247. fprintf( stderr, "Hook table\n" );
  248. }
  249. static void hook_table_destroy( struct object *obj )
  250. {
  251. int i;
  252. struct hook *hook;
  253. struct hook_table *table = (struct hook_table *)obj;
  254. for (i = 0; i < NB_HOOKS; i++)
  255. {
  256. while ((hook = get_first_hook( table, i )) != NULL) free_hook( hook );
  257. }
  258. }
  259. /* remove a hook, freeing it if the chain is not in use */
  260. static void remove_hook( struct hook *hook )
  261. {
  262. if (hook->table->counts[hook->index])
  263. hook->proc = 0; /* chain is in use, just mark it and return */
  264. else
  265. free_hook( hook );
  266. }
  267. /* release a hook chain, removing deleted hooks if the use count drops to 0 */
  268. static void release_hook_chain( struct hook_table *table, int index )
  269. {
  270. if (!table->counts[index]) /* use count shouldn't already be 0 */
  271. {
  272. set_error( STATUS_INVALID_PARAMETER );
  273. return;
  274. }
  275. if (!--table->counts[index])
  276. {
  277. struct hook *hook = get_first_hook( table, index );
  278. while (hook)
  279. {
  280. struct hook *next = HOOK_ENTRY( list_next( &table->hooks[hook->index], &hook->chain ) );
  281. if (!hook->proc) free_hook( hook );
  282. hook = next;
  283. }
  284. }
  285. }
  286. /* remove all global hooks owned by a given thread */
  287. void remove_thread_hooks( struct thread *thread )
  288. {
  289. struct hook_table *global_hooks = get_global_hooks( thread );
  290. int index;
  291. if (!global_hooks) return;
  292. /* only low-level keyboard/mouse global hooks can be owned by a thread */
  293. for (index = WH_KEYBOARD_LL - WH_MINHOOK; index <= WH_MOUSE_LL - WH_MINHOOK; index++)
  294. {
  295. struct hook *hook = get_first_hook( global_hooks, index );
  296. while (hook)
  297. {
  298. struct hook *next = HOOK_ENTRY( list_next( &global_hooks->hooks[index], &hook->chain ) );
  299. if (hook->thread == thread) remove_hook( hook );
  300. hook = next;
  301. }
  302. }
  303. }
  304. /* get a bitmap of active hooks in a hook table */
  305. static int is_hook_active( struct hook_table *table, int index )
  306. {
  307. struct hook *hook = get_first_hook( table, index );
  308. while (hook)
  309. {
  310. if (hook->proc && run_hook_in_current_thread( hook )) return 1;
  311. hook = HOOK_ENTRY( list_next( &table->hooks[index], &hook->chain ) );
  312. }
  313. return 0;
  314. }
  315. /* get a bitmap of all active hooks for the current thread */
  316. unsigned int get_active_hooks(void)
  317. {
  318. struct hook_table *table = get_queue_hooks( current );
  319. struct hook_table *global_hooks = get_global_hooks( current );
  320. unsigned int ret = 1u << 31; /* set high bit to indicate that the bitmap is valid */
  321. int id;
  322. for (id = WH_MINHOOK; id <= WH_WINEVENT; id++)
  323. {
  324. if ((table && is_hook_active( table, id - WH_MINHOOK )) ||
  325. (global_hooks && is_hook_active( global_hooks, id - WH_MINHOOK )))
  326. ret |= 1 << (id - WH_MINHOOK);
  327. }
  328. return ret;
  329. }
  330. /* return the thread that owns the first global hook */
  331. struct thread *get_first_global_hook( int id )
  332. {
  333. struct hook *hook;
  334. struct hook_table *global_hooks = get_global_hooks( current );
  335. if (!global_hooks) return NULL;
  336. if (!(hook = get_first_valid_hook( global_hooks, id - WH_MINHOOK, EVENT_MIN, 0, 0, 0 ))) return NULL;
  337. return hook->owner;
  338. }
  339. /* set a window hook */
  340. DECL_HANDLER(set_hook)
  341. {
  342. struct process *process = NULL;
  343. struct thread *thread = NULL;
  344. struct desktop *desktop;
  345. struct hook *hook;
  346. WCHAR *module;
  347. int global;
  348. data_size_t module_size = get_req_data_size();
  349. if (!req->proc || req->id < WH_MINHOOK || req->id > WH_WINEVENT)
  350. {
  351. set_error( STATUS_INVALID_PARAMETER );
  352. return;
  353. }
  354. if (!(desktop = get_thread_desktop( current, DESKTOP_HOOKCONTROL ))) return;
  355. if (req->pid && !(process = get_process_from_id( req->pid ))) goto done;
  356. if (req->tid)
  357. {
  358. if (!(thread = get_thread_from_id( req->tid ))) goto done;
  359. if (process && process != thread->process)
  360. {
  361. set_error( STATUS_INVALID_PARAMETER );
  362. goto done;
  363. }
  364. }
  365. if (req->id == WH_KEYBOARD_LL || req->id == WH_MOUSE_LL)
  366. {
  367. /* low-level hardware hooks are special: always global, but without a module */
  368. if (thread)
  369. {
  370. set_error( STATUS_INVALID_PARAMETER );
  371. goto done;
  372. }
  373. module = NULL;
  374. global = 1;
  375. }
  376. else if (!req->tid)
  377. {
  378. /* out of context hooks do not need a module handle */
  379. if (!module_size && (req->flags & WINEVENT_INCONTEXT))
  380. {
  381. set_error( STATUS_INVALID_PARAMETER );
  382. goto done;
  383. }
  384. if (!(module = memdup( get_req_data(), module_size ))) goto done;
  385. global = 1;
  386. }
  387. else
  388. {
  389. /* module is optional only if hook is in current process */
  390. if (!module_size)
  391. {
  392. module = NULL;
  393. if (thread->process != current->process)
  394. {
  395. set_error( STATUS_INVALID_PARAMETER );
  396. goto done;
  397. }
  398. }
  399. else if (!(module = memdup( get_req_data(), module_size ))) goto done;
  400. global = 0;
  401. }
  402. if ((hook = add_hook( desktop, thread, req->id - WH_MINHOOK, global )))
  403. {
  404. hook->owner = (struct thread *)grab_object( current );
  405. hook->process = process ? (struct process *)grab_object( process ) : NULL;
  406. hook->event_min = req->event_min;
  407. hook->event_max = req->event_max;
  408. hook->flags = req->flags;
  409. hook->proc = req->proc;
  410. hook->unicode = req->unicode;
  411. hook->module = module;
  412. hook->module_size = module_size;
  413. reply->handle = hook->handle;
  414. reply->active_hooks = get_active_hooks();
  415. }
  416. else free( module );
  417. done:
  418. if (process) release_object( process );
  419. if (thread) release_object( thread );
  420. release_object( desktop );
  421. }
  422. /* remove a window hook */
  423. DECL_HANDLER(remove_hook)
  424. {
  425. struct hook *hook;
  426. if (req->handle)
  427. {
  428. if (!(hook = get_user_object( req->handle, USER_HOOK )))
  429. {
  430. set_error( STATUS_INVALID_HANDLE );
  431. return;
  432. }
  433. }
  434. else
  435. {
  436. if (!req->proc || req->id < WH_MINHOOK || req->id > WH_WINEVENT)
  437. {
  438. set_error( STATUS_INVALID_PARAMETER );
  439. return;
  440. }
  441. if (!(hook = find_hook( current, req->id - WH_MINHOOK, req->proc )))
  442. {
  443. set_error( STATUS_INVALID_PARAMETER );
  444. return;
  445. }
  446. }
  447. remove_hook( hook );
  448. reply->active_hooks = get_active_hooks();
  449. }
  450. /* start calling a hook chain */
  451. DECL_HANDLER(start_hook_chain)
  452. {
  453. struct hook *hook;
  454. struct hook_table *table = get_queue_hooks( current );
  455. struct hook_table *global_table = get_global_hooks( current );
  456. if (req->id < WH_MINHOOK || req->id > WH_WINEVENT)
  457. {
  458. set_error( STATUS_INVALID_PARAMETER );
  459. return;
  460. }
  461. reply->active_hooks = get_active_hooks();
  462. if (!table || !(hook = get_first_valid_hook( table, req->id - WH_MINHOOK, req->event,
  463. req->window, req->object_id, req->child_id )))
  464. {
  465. /* try global table */
  466. if (!global_table || !(hook = get_first_valid_hook( global_table, req->id - WH_MINHOOK, req->event,
  467. req->window, req->object_id, req->child_id )))
  468. return; /* no hook set */
  469. }
  470. if (run_hook_in_owner_thread( hook ))
  471. {
  472. reply->pid = get_process_id( hook->owner->process );
  473. reply->tid = get_thread_id( hook->owner );
  474. }
  475. else
  476. {
  477. reply->pid = 0;
  478. reply->tid = 0;
  479. }
  480. reply->proc = hook->proc;
  481. reply->handle = hook->handle;
  482. reply->unicode = hook->unicode;
  483. if (table) table->counts[hook->index]++;
  484. if (global_table) global_table->counts[hook->index]++;
  485. if (hook->module) set_reply_data( hook->module, hook->module_size );
  486. }
  487. /* finished calling a hook chain */
  488. DECL_HANDLER(finish_hook_chain)
  489. {
  490. struct hook_table *table = get_queue_hooks( current );
  491. struct hook_table *global_hooks = get_global_hooks( current );
  492. int index = req->id - WH_MINHOOK;
  493. if (req->id < WH_MINHOOK || req->id > WH_WINEVENT)
  494. {
  495. set_error( STATUS_INVALID_PARAMETER );
  496. return;
  497. }
  498. if (table) release_hook_chain( table, index );
  499. if (global_hooks) release_hook_chain( global_hooks, index );
  500. }
  501. /* get the hook information */
  502. DECL_HANDLER(get_hook_info)
  503. {
  504. struct hook *hook;
  505. if (!(hook = get_user_object( req->handle, USER_HOOK ))) return;
  506. if (hook->thread && (hook->thread != current))
  507. {
  508. set_error( STATUS_INVALID_HANDLE );
  509. return;
  510. }
  511. if (req->get_next && !(hook = get_next_hook( current, hook, req->event, req->window,
  512. req->object_id, req->child_id )))
  513. return;
  514. reply->handle = hook->handle;
  515. reply->id = hook->index + WH_MINHOOK;
  516. reply->unicode = hook->unicode;
  517. if (hook->module) set_reply_data( hook->module, min(hook->module_size,get_reply_max_size()) );
  518. if (run_hook_in_owner_thread( hook ))
  519. {
  520. reply->pid = get_process_id( hook->owner->process );
  521. reply->tid = get_thread_id( hook->owner );
  522. }
  523. else
  524. {
  525. reply->pid = 0;
  526. reply->tid = 0;
  527. }
  528. reply->proc = hook->proc;
  529. }