process.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189
  1. /*
  2. * Server-side process 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 <signal.h>
  25. #include <string.h>
  26. #include <stdarg.h>
  27. #include <stdio.h>
  28. #include <stdlib.h>
  29. #include <sys/time.h>
  30. #ifdef HAVE_SYS_SOCKET_H
  31. # include <sys/socket.h>
  32. #endif
  33. #include <unistd.h>
  34. #include "windef.h"
  35. #include "winbase.h"
  36. #include "winnt.h"
  37. #include "file.h"
  38. #include "handle.h"
  39. #include "process.h"
  40. #include "thread.h"
  41. #include "request.h"
  42. #include "console.h"
  43. #include "user.h"
  44. /* process structure */
  45. static struct process *first_process;
  46. static int running_processes;
  47. /* process operations */
  48. static void process_dump( struct object *obj, int verbose );
  49. static int process_signaled( struct object *obj, struct thread *thread );
  50. static void process_poll_event( struct fd *fd, int event );
  51. static void process_destroy( struct object *obj );
  52. static const struct object_ops process_ops =
  53. {
  54. sizeof(struct process), /* size */
  55. process_dump, /* dump */
  56. add_queue, /* add_queue */
  57. remove_queue, /* remove_queue */
  58. process_signaled, /* signaled */
  59. no_satisfied, /* satisfied */
  60. no_get_fd, /* get_fd */
  61. process_destroy /* destroy */
  62. };
  63. static const struct fd_ops process_fd_ops =
  64. {
  65. NULL, /* get_poll_events */
  66. process_poll_event, /* poll_event */
  67. no_flush, /* flush */
  68. no_get_file_info, /* get_file_info */
  69. no_queue_async, /* queue_async */
  70. no_cancel_async /* cancel async */
  71. };
  72. /* process startup info */
  73. struct startup_info
  74. {
  75. struct object obj; /* object header */
  76. struct list entry; /* entry in list of startup infos */
  77. int inherit_all; /* inherit all handles from parent */
  78. int create_flags; /* creation flags */
  79. int unix_pid; /* Unix pid of new process */
  80. obj_handle_t hstdin; /* handle for stdin */
  81. obj_handle_t hstdout; /* handle for stdout */
  82. obj_handle_t hstderr; /* handle for stderr */
  83. struct file *exe_file; /* file handle for main exe */
  84. struct thread *owner; /* owner thread (the one that created the new process) */
  85. struct process *process; /* created process */
  86. struct thread *thread; /* created thread */
  87. size_t data_size; /* size of startup data */
  88. void *data; /* data for startup info */
  89. };
  90. static void startup_info_dump( struct object *obj, int verbose );
  91. static int startup_info_signaled( struct object *obj, struct thread *thread );
  92. static void startup_info_destroy( struct object *obj );
  93. static const struct object_ops startup_info_ops =
  94. {
  95. sizeof(struct startup_info), /* size */
  96. startup_info_dump, /* dump */
  97. add_queue, /* add_queue */
  98. remove_queue, /* remove_queue */
  99. startup_info_signaled, /* signaled */
  100. no_satisfied, /* satisfied */
  101. no_get_fd, /* get_fd */
  102. startup_info_destroy /* destroy */
  103. };
  104. static struct list startup_info_list = LIST_INIT(startup_info_list);
  105. struct ptid_entry
  106. {
  107. void *ptr; /* entry ptr */
  108. unsigned int next; /* next free entry */
  109. };
  110. static struct ptid_entry *ptid_entries; /* array of ptid entries */
  111. static unsigned int used_ptid_entries; /* number of entries in use */
  112. static unsigned int alloc_ptid_entries; /* number of allocated entries */
  113. static unsigned int next_free_ptid; /* next free entry */
  114. static unsigned int last_free_ptid; /* last free entry */
  115. #define PTID_OFFSET 8 /* offset for first ptid value */
  116. /* allocate a new process or thread id */
  117. unsigned int alloc_ptid( void *ptr )
  118. {
  119. struct ptid_entry *entry;
  120. unsigned int id;
  121. if (used_ptid_entries < alloc_ptid_entries)
  122. {
  123. id = used_ptid_entries + PTID_OFFSET;
  124. entry = &ptid_entries[used_ptid_entries++];
  125. }
  126. else if (next_free_ptid)
  127. {
  128. id = next_free_ptid;
  129. entry = &ptid_entries[id - PTID_OFFSET];
  130. if (!(next_free_ptid = entry->next)) last_free_ptid = 0;
  131. }
  132. else /* need to grow the array */
  133. {
  134. unsigned int count = alloc_ptid_entries + (alloc_ptid_entries / 2);
  135. if (!count) count = 64;
  136. if (!(entry = realloc( ptid_entries, count * sizeof(*entry) )))
  137. {
  138. set_error( STATUS_NO_MEMORY );
  139. return 0;
  140. }
  141. ptid_entries = entry;
  142. alloc_ptid_entries = count;
  143. id = used_ptid_entries + PTID_OFFSET;
  144. entry = &ptid_entries[used_ptid_entries++];
  145. }
  146. entry->ptr = ptr;
  147. return id;
  148. }
  149. /* free a process or thread id */
  150. void free_ptid( unsigned int id )
  151. {
  152. struct ptid_entry *entry = &ptid_entries[id - PTID_OFFSET];
  153. entry->ptr = NULL;
  154. entry->next = 0;
  155. /* append to end of free list so that we don't reuse it too early */
  156. if (last_free_ptid) ptid_entries[last_free_ptid - PTID_OFFSET].next = id;
  157. else next_free_ptid = id;
  158. last_free_ptid = id;
  159. }
  160. /* retrieve the pointer corresponding to a process or thread id */
  161. void *get_ptid_entry( unsigned int id )
  162. {
  163. if (id < PTID_OFFSET) return NULL;
  164. if (id - PTID_OFFSET >= used_ptid_entries) return NULL;
  165. return ptid_entries[id - PTID_OFFSET].ptr;
  166. }
  167. /* set the state of the process startup info */
  168. static void set_process_startup_state( struct process *process, enum startup_state state )
  169. {
  170. if (process->startup_state == STARTUP_IN_PROGRESS) process->startup_state = state;
  171. if (process->startup_info)
  172. {
  173. wake_up( &process->startup_info->obj, 0 );
  174. release_object( process->startup_info );
  175. process->startup_info = NULL;
  176. }
  177. }
  178. /* set the console and stdio handles for a newly created process */
  179. static int set_process_console( struct process *process, struct thread *parent_thread,
  180. struct startup_info *info, struct init_process_reply *reply )
  181. {
  182. if (info)
  183. {
  184. if (!(process->create_flags & (DETACHED_PROCESS | CREATE_NEW_CONSOLE)))
  185. {
  186. /* FIXME: some better error checking should be done...
  187. * like if hConOut and hConIn are console handles, then they should be on the same
  188. * physical console
  189. */
  190. inherit_console( parent_thread, process, info->inherit_all ? info->hstdin : 0 );
  191. }
  192. if (!info->inherit_all && !(process->create_flags & CREATE_NEW_CONSOLE))
  193. {
  194. reply->hstdin = duplicate_handle( parent_thread->process, info->hstdin, process,
  195. 0, TRUE, DUPLICATE_SAME_ACCESS );
  196. reply->hstdout = duplicate_handle( parent_thread->process, info->hstdout, process,
  197. 0, TRUE, DUPLICATE_SAME_ACCESS );
  198. reply->hstderr = duplicate_handle( parent_thread->process, info->hstderr, process,
  199. 0, TRUE, DUPLICATE_SAME_ACCESS );
  200. }
  201. else
  202. {
  203. reply->hstdin = info->hstdin;
  204. reply->hstdout = info->hstdout;
  205. reply->hstderr = info->hstderr;
  206. }
  207. }
  208. else reply->hstdin = reply->hstdout = reply->hstderr = 0;
  209. /* some handles above may have been invalid; this is not an error */
  210. if (get_error() == STATUS_INVALID_HANDLE ||
  211. get_error() == STATUS_OBJECT_TYPE_MISMATCH) clear_error();
  212. return 1;
  213. }
  214. /* create a new process and its main thread */
  215. struct thread *create_process( int fd )
  216. {
  217. struct process *process;
  218. struct thread *thread = NULL;
  219. int request_pipe[2];
  220. if (!(process = alloc_object( &process_ops ))) goto error;
  221. process->next = NULL;
  222. process->prev = NULL;
  223. process->parent = NULL;
  224. process->thread_list = NULL;
  225. process->debugger = NULL;
  226. process->handles = NULL;
  227. process->msg_fd = NULL;
  228. process->exit_code = STILL_ACTIVE;
  229. process->running_threads = 0;
  230. process->priority = NORMAL_PRIORITY_CLASS;
  231. process->affinity = 1;
  232. process->suspend = 0;
  233. process->create_flags = 0;
  234. process->console = NULL;
  235. process->startup_state = STARTUP_IN_PROGRESS;
  236. process->startup_info = NULL;
  237. process->idle_event = NULL;
  238. process->queue = NULL;
  239. process->atom_table = NULL;
  240. process->peb = NULL;
  241. process->ldt_copy = NULL;
  242. process->exe.next = NULL;
  243. process->exe.prev = NULL;
  244. process->exe.file = NULL;
  245. process->exe.dbg_offset = 0;
  246. process->exe.dbg_size = 0;
  247. process->exe.namelen = 0;
  248. process->exe.filename = NULL;
  249. process->group_id = 0;
  250. process->token = create_admin_token();
  251. list_init( &process->locks );
  252. list_init( &process->classes );
  253. gettimeofday( &process->start_time, NULL );
  254. if ((process->next = first_process) != NULL) process->next->prev = process;
  255. first_process = process;
  256. if (!(process->id = alloc_ptid( process ))) goto error;
  257. if (!(process->msg_fd = create_anonymous_fd( &process_fd_ops, fd, &process->obj ))) goto error;
  258. /* create the main thread */
  259. if (pipe( request_pipe ) == -1)
  260. {
  261. file_set_error();
  262. goto error;
  263. }
  264. if (send_client_fd( process, request_pipe[1], 0 ) == -1)
  265. {
  266. close( request_pipe[0] );
  267. close( request_pipe[1] );
  268. goto error;
  269. }
  270. close( request_pipe[1] );
  271. if (!(thread = create_thread( request_pipe[0], process ))) goto error;
  272. set_fd_events( process->msg_fd, POLLIN ); /* start listening to events */
  273. release_object( process );
  274. return thread;
  275. error:
  276. if (process) release_object( process );
  277. /* if we failed to start our first process, close everything down */
  278. if (!running_processes) close_master_socket();
  279. return NULL;
  280. }
  281. /* find the startup info for a given Unix process */
  282. inline static struct startup_info *find_startup_info( int unix_pid )
  283. {
  284. struct list *ptr;
  285. LIST_FOR_EACH( ptr, &startup_info_list )
  286. {
  287. struct startup_info *info = LIST_ENTRY( ptr, struct startup_info, entry );
  288. if (info->unix_pid == unix_pid) return info;
  289. }
  290. return NULL;
  291. }
  292. /* initialize the current process and fill in the request */
  293. static struct startup_info *init_process( struct init_process_reply *reply )
  294. {
  295. struct process *process = current->process;
  296. struct thread *parent_thread = NULL;
  297. struct process *parent = NULL;
  298. struct startup_info *info = find_startup_info( current->unix_pid );
  299. if (info)
  300. {
  301. if (info->thread)
  302. {
  303. fatal_protocol_error( current, "init_process: called twice?\n" );
  304. return NULL;
  305. }
  306. parent_thread = info->owner;
  307. parent = parent_thread->process;
  308. process->parent = (struct process *)grab_object( parent );
  309. }
  310. /* set the process flags */
  311. process->create_flags = info ? info->create_flags : 0;
  312. /* create the handle table */
  313. if (info && info->inherit_all)
  314. process->handles = copy_handle_table( process, parent );
  315. else
  316. process->handles = alloc_handle_table( process, 0 );
  317. if (!process->handles) return NULL;
  318. /* retrieve the main exe file */
  319. reply->exe_file = 0;
  320. if (info && info->exe_file)
  321. {
  322. process->exe.file = (struct file *)grab_object( info->exe_file );
  323. if (!(reply->exe_file = alloc_handle( process, process->exe.file, GENERIC_READ, 0 )))
  324. return NULL;
  325. }
  326. /* set the process console */
  327. if (!set_process_console( process, parent_thread, info, reply )) return NULL;
  328. process->group_id = get_process_id( process );
  329. if (parent)
  330. {
  331. /* attach to the debugger if requested */
  332. if (process->create_flags & (DEBUG_PROCESS | DEBUG_ONLY_THIS_PROCESS))
  333. set_process_debugger( process, parent_thread );
  334. else if (parent->debugger && !(parent->create_flags & DEBUG_ONLY_THIS_PROCESS))
  335. set_process_debugger( process, parent->debugger );
  336. if (!(process->create_flags & CREATE_NEW_PROCESS_GROUP))
  337. process->group_id = parent->group_id;
  338. }
  339. /* thread will be actually suspended in init_done */
  340. if (process->create_flags & CREATE_SUSPENDED) current->suspend++;
  341. if (info)
  342. {
  343. reply->info_size = info->data_size;
  344. info->process = (struct process *)grab_object( process );
  345. info->thread = (struct thread *)grab_object( current );
  346. }
  347. reply->create_flags = process->create_flags;
  348. reply->server_start = server_start_ticks;
  349. return info ? (struct startup_info *)grab_object( info ) : NULL;
  350. }
  351. /* destroy a process when its refcount is 0 */
  352. static void process_destroy( struct object *obj )
  353. {
  354. struct process *process = (struct process *)obj;
  355. assert( obj->ops == &process_ops );
  356. /* we can't have a thread remaining */
  357. assert( !process->thread_list );
  358. set_process_startup_state( process, STARTUP_ABORTED );
  359. if (process->console) release_object( process->console );
  360. if (process->parent) release_object( process->parent );
  361. if (process->msg_fd) release_object( process->msg_fd );
  362. if (process->next) process->next->prev = process->prev;
  363. if (process->prev) process->prev->next = process->next;
  364. else first_process = process->next;
  365. if (process->idle_event) release_object( process->idle_event );
  366. if (process->queue) release_object( process->queue );
  367. if (process->atom_table) release_object( process->atom_table );
  368. if (process->exe.file) release_object( process->exe.file );
  369. if (process->exe.filename) free( process->exe.filename );
  370. if (process->id) free_ptid( process->id );
  371. if (process->token) release_object( process->token );
  372. }
  373. /* dump a process on stdout for debugging purposes */
  374. static void process_dump( struct object *obj, int verbose )
  375. {
  376. struct process *process = (struct process *)obj;
  377. assert( obj->ops == &process_ops );
  378. fprintf( stderr, "Process id=%04x next=%p prev=%p handles=%p\n",
  379. process->id, process->next, process->prev, process->handles );
  380. }
  381. static int process_signaled( struct object *obj, struct thread *thread )
  382. {
  383. struct process *process = (struct process *)obj;
  384. return !process->running_threads;
  385. }
  386. static void process_poll_event( struct fd *fd, int event )
  387. {
  388. struct process *process = get_fd_user( fd );
  389. assert( process->obj.ops == &process_ops );
  390. if (event & (POLLERR | POLLHUP)) set_fd_events( fd, -1 );
  391. else if (event & POLLIN) receive_fd( process );
  392. }
  393. static void startup_info_destroy( struct object *obj )
  394. {
  395. struct startup_info *info = (struct startup_info *)obj;
  396. assert( obj->ops == &startup_info_ops );
  397. list_remove( &info->entry );
  398. if (info->data) free( info->data );
  399. if (info->exe_file) release_object( info->exe_file );
  400. if (info->process) release_object( info->process );
  401. if (info->thread) release_object( info->thread );
  402. if (info->owner) release_object( info->owner );
  403. }
  404. static void startup_info_dump( struct object *obj, int verbose )
  405. {
  406. struct startup_info *info = (struct startup_info *)obj;
  407. assert( obj->ops == &startup_info_ops );
  408. fprintf( stderr, "Startup info flags=%x in=%p out=%p err=%p\n",
  409. info->create_flags, info->hstdin, info->hstdout, info->hstderr );
  410. }
  411. static int startup_info_signaled( struct object *obj, struct thread *thread )
  412. {
  413. struct startup_info *info = (struct startup_info *)obj;
  414. return info->process && is_process_init_done(info->process);
  415. }
  416. /* get a process from an id (and increment the refcount) */
  417. struct process *get_process_from_id( process_id_t id )
  418. {
  419. struct object *obj = get_ptid_entry( id );
  420. if (obj && obj->ops == &process_ops) return (struct process *)grab_object( obj );
  421. set_error( STATUS_INVALID_PARAMETER );
  422. return NULL;
  423. }
  424. /* get a process from a handle (and increment the refcount) */
  425. struct process *get_process_from_handle( obj_handle_t handle, unsigned int access )
  426. {
  427. return (struct process *)get_handle_obj( current->process, handle,
  428. access, &process_ops );
  429. }
  430. /* add a dll to a process list */
  431. static struct process_dll *process_load_dll( struct process *process, struct file *file,
  432. void *base, const WCHAR *filename, size_t name_len )
  433. {
  434. struct process_dll *dll;
  435. /* make sure we don't already have one with the same base address */
  436. for (dll = process->exe.next; dll; dll = dll->next) if (dll->base == base)
  437. {
  438. set_error( STATUS_INVALID_PARAMETER );
  439. return NULL;
  440. }
  441. if ((dll = mem_alloc( sizeof(*dll) )))
  442. {
  443. dll->prev = &process->exe;
  444. dll->file = NULL;
  445. dll->base = base;
  446. dll->filename = NULL;
  447. dll->namelen = name_len;
  448. if (name_len && !(dll->filename = memdup( filename, name_len )))
  449. {
  450. free( dll );
  451. return NULL;
  452. }
  453. if (file) dll->file = (struct file *)grab_object( file );
  454. if ((dll->next = process->exe.next)) dll->next->prev = dll;
  455. process->exe.next = dll;
  456. }
  457. return dll;
  458. }
  459. /* remove a dll from a process list */
  460. static void process_unload_dll( struct process *process, void *base )
  461. {
  462. struct process_dll *dll;
  463. for (dll = process->exe.next; dll; dll = dll->next)
  464. {
  465. if (dll->base == base)
  466. {
  467. if (dll->file) release_object( dll->file );
  468. if (dll->next) dll->next->prev = dll->prev;
  469. if (dll->prev) dll->prev->next = dll->next;
  470. if (dll->filename) free( dll->filename );
  471. free( dll );
  472. generate_debug_event( current, UNLOAD_DLL_DEBUG_EVENT, base );
  473. return;
  474. }
  475. }
  476. set_error( STATUS_INVALID_PARAMETER );
  477. }
  478. /* kill all processes */
  479. void kill_all_processes( struct process *skip, int exit_code )
  480. {
  481. for (;;)
  482. {
  483. struct process *process = first_process;
  484. while (process && (!process->running_threads || process == skip))
  485. process = process->next;
  486. if (!process) break;
  487. kill_process( process, NULL, exit_code );
  488. }
  489. }
  490. /* kill all processes being attached to a console renderer */
  491. void kill_console_processes( struct thread *renderer, int exit_code )
  492. {
  493. for (;;) /* restart from the beginning of the list every time */
  494. {
  495. struct process *process = first_process;
  496. /* find the first process being attached to 'renderer' and still running */
  497. while (process &&
  498. (process == renderer->process || !process->console ||
  499. process->console->renderer != renderer || !process->running_threads))
  500. {
  501. process = process->next;
  502. }
  503. if (!process) break;
  504. kill_process( process, NULL, exit_code );
  505. }
  506. }
  507. /* a process has been killed (i.e. its last thread died) */
  508. static void process_killed( struct process *process )
  509. {
  510. assert( !process->thread_list );
  511. gettimeofday( &process->end_time, NULL );
  512. if (process->handles) release_object( process->handles );
  513. process->handles = NULL;
  514. /* close the console attached to this process, if any */
  515. free_console( process );
  516. while (process->exe.next)
  517. {
  518. struct process_dll *dll = process->exe.next;
  519. process->exe.next = dll->next;
  520. if (dll->file) release_object( dll->file );
  521. if (dll->filename) free( dll->filename );
  522. free( dll );
  523. }
  524. destroy_process_classes( process );
  525. set_process_startup_state( process, STARTUP_ABORTED );
  526. if (process->exe.file) release_object( process->exe.file );
  527. process->exe.file = NULL;
  528. wake_up( &process->obj, 0 );
  529. if (!--running_processes) close_master_socket();
  530. }
  531. /* add a thread to a process running threads list */
  532. void add_process_thread( struct process *process, struct thread *thread )
  533. {
  534. thread->proc_next = process->thread_list;
  535. thread->proc_prev = NULL;
  536. if (thread->proc_next) thread->proc_next->proc_prev = thread;
  537. process->thread_list = thread;
  538. if (!process->running_threads++) running_processes++;
  539. grab_object( thread );
  540. }
  541. /* remove a thread from a process running threads list */
  542. void remove_process_thread( struct process *process, struct thread *thread )
  543. {
  544. assert( process->running_threads > 0 );
  545. assert( process->thread_list );
  546. if (thread->proc_next) thread->proc_next->proc_prev = thread->proc_prev;
  547. if (thread->proc_prev) thread->proc_prev->proc_next = thread->proc_next;
  548. else process->thread_list = thread->proc_next;
  549. if (!--process->running_threads)
  550. {
  551. /* we have removed the last running thread, exit the process */
  552. process->exit_code = thread->exit_code;
  553. generate_debug_event( thread, EXIT_PROCESS_DEBUG_EVENT, process );
  554. process_killed( process );
  555. }
  556. else generate_debug_event( thread, EXIT_THREAD_DEBUG_EVENT, thread );
  557. release_object( thread );
  558. }
  559. /* suspend all the threads of a process */
  560. void suspend_process( struct process *process )
  561. {
  562. if (!process->suspend++)
  563. {
  564. struct thread *thread = process->thread_list;
  565. while (thread)
  566. {
  567. struct thread *next = thread->proc_next;
  568. if (!thread->suspend) stop_thread( thread );
  569. thread = next;
  570. }
  571. }
  572. }
  573. /* resume all the threads of a process */
  574. void resume_process( struct process *process )
  575. {
  576. assert (process->suspend > 0);
  577. if (!--process->suspend)
  578. {
  579. struct thread *thread = process->thread_list;
  580. while (thread)
  581. {
  582. struct thread *next = thread->proc_next;
  583. if (!thread->suspend) wake_thread( thread );
  584. thread = next;
  585. }
  586. }
  587. }
  588. /* kill a process on the spot */
  589. void kill_process( struct process *process, struct thread *skip, int exit_code )
  590. {
  591. struct thread *thread = process->thread_list;
  592. while (thread)
  593. {
  594. struct thread *next = thread->proc_next;
  595. thread->exit_code = exit_code;
  596. if (thread != skip) kill_thread( thread, 1 );
  597. thread = next;
  598. }
  599. }
  600. /* kill all processes being debugged by a given thread */
  601. void kill_debugged_processes( struct thread *debugger, int exit_code )
  602. {
  603. for (;;) /* restart from the beginning of the list every time */
  604. {
  605. struct process *process = first_process;
  606. /* find the first process being debugged by 'debugger' and still running */
  607. while (process && (process->debugger != debugger || !process->running_threads))
  608. process = process->next;
  609. if (!process) return;
  610. process->debugger = NULL;
  611. kill_process( process, NULL, exit_code );
  612. }
  613. }
  614. /* detach a debugger from all its debuggees */
  615. void detach_debugged_processes( struct thread *debugger )
  616. {
  617. struct process *process;
  618. for (process = first_process; process; process = process->next)
  619. {
  620. if (process->debugger == debugger && process->running_threads)
  621. {
  622. debugger_detach( process, debugger );
  623. }
  624. }
  625. }
  626. void enum_processes( int (*cb)(struct process*, void*), void *user )
  627. {
  628. struct process *process;
  629. for (process = first_process; process; process = process->next)
  630. {
  631. if ((cb)(process, user)) break;
  632. }
  633. }
  634. /* read data from a process memory space */
  635. /* len is the total size (in ints) */
  636. static int read_process_memory( struct process *process, const int *addr, size_t len, int *dest )
  637. {
  638. struct thread *thread = process->thread_list;
  639. assert( !((unsigned int)addr % sizeof(int)) ); /* address must be aligned */
  640. if (!thread) /* process is dead */
  641. {
  642. set_error( STATUS_ACCESS_DENIED );
  643. return 0;
  644. }
  645. if (suspend_for_ptrace( thread ))
  646. {
  647. while (len > 0)
  648. {
  649. if (read_thread_int( thread, addr++, dest++ ) == -1) break;
  650. len--;
  651. }
  652. resume_after_ptrace( thread );
  653. }
  654. return !len;
  655. }
  656. /* make sure we can write to the whole address range */
  657. /* len is the total size (in ints) */
  658. static int check_process_write_access( struct thread *thread, int *addr, size_t len )
  659. {
  660. int page = get_page_size() / sizeof(int);
  661. for (;;)
  662. {
  663. if (write_thread_int( thread, addr, 0, 0 ) == -1) return 0;
  664. if (len <= page) break;
  665. addr += page;
  666. len -= page;
  667. }
  668. return (write_thread_int( thread, addr + len - 1, 0, 0 ) != -1);
  669. }
  670. /* write data to a process memory space */
  671. /* len is the total size (in ints), max is the size we can actually read from the input buffer */
  672. /* we check the total size for write permissions */
  673. static int write_process_memory( struct process *process, int *addr, size_t len,
  674. unsigned int first_mask, unsigned int last_mask, const int *src )
  675. {
  676. struct thread *thread = process->thread_list;
  677. int ret = 0;
  678. assert( !((unsigned int)addr % sizeof(int) )); /* address must be aligned */
  679. if (!thread) /* process is dead */
  680. {
  681. set_error( STATUS_ACCESS_DENIED );
  682. return 0;
  683. }
  684. if (suspend_for_ptrace( thread ))
  685. {
  686. if (!check_process_write_access( thread, addr, len ))
  687. {
  688. set_error( STATUS_ACCESS_DENIED );
  689. goto done;
  690. }
  691. /* first word is special */
  692. if (len > 1)
  693. {
  694. if (write_thread_int( thread, addr++, *src++, first_mask ) == -1) goto done;
  695. len--;
  696. }
  697. else last_mask &= first_mask;
  698. while (len > 1)
  699. {
  700. if (write_thread_int( thread, addr++, *src++, ~0 ) == -1) goto done;
  701. len--;
  702. }
  703. /* last word is special too */
  704. if (write_thread_int( thread, addr, *src, last_mask ) == -1) goto done;
  705. ret = 1;
  706. done:
  707. resume_after_ptrace( thread );
  708. }
  709. return ret;
  710. }
  711. /* set the debugged flag in the process PEB */
  712. int set_process_debug_flag( struct process *process, int flag )
  713. {
  714. int mask = 0, data = 0;
  715. /* BeingDebugged flag is the byte at offset 2 in the PEB */
  716. memset( (char *)&mask + 2, 0xff, 1 );
  717. memset( (char *)&data + 2, flag, 1 );
  718. return write_process_memory( process, process->peb, 1, mask, mask, &data );
  719. }
  720. /* take a snapshot of currently running processes */
  721. struct process_snapshot *process_snap( int *count )
  722. {
  723. struct process_snapshot *snapshot, *ptr;
  724. struct process *process;
  725. if (!running_processes) return NULL;
  726. if (!(snapshot = mem_alloc( sizeof(*snapshot) * running_processes )))
  727. return NULL;
  728. ptr = snapshot;
  729. for (process = first_process; process; process = process->next)
  730. {
  731. if (!process->running_threads) continue;
  732. ptr->process = process;
  733. ptr->threads = process->running_threads;
  734. ptr->count = process->obj.refcount;
  735. ptr->priority = process->priority;
  736. ptr->handles = get_handle_table_count(process);
  737. grab_object( process );
  738. ptr++;
  739. }
  740. *count = running_processes;
  741. return snapshot;
  742. }
  743. /* take a snapshot of the modules of a process */
  744. struct module_snapshot *module_snap( struct process *process, int *count )
  745. {
  746. struct module_snapshot *snapshot, *ptr;
  747. struct process_dll *dll;
  748. int total = 0;
  749. for (dll = &process->exe; dll; dll = dll->next) total++;
  750. if (!(snapshot = mem_alloc( sizeof(*snapshot) * total ))) return NULL;
  751. for (ptr = snapshot, dll = &process->exe; dll; dll = dll->next, ptr++)
  752. {
  753. ptr->base = dll->base;
  754. ptr->size = dll->size;
  755. ptr->namelen = dll->namelen;
  756. ptr->filename = memdup( dll->filename, dll->namelen );
  757. }
  758. *count = total;
  759. return snapshot;
  760. }
  761. /* create a new process */
  762. DECL_HANDLER(new_process)
  763. {
  764. struct startup_info *info;
  765. /* build the startup info for a new process */
  766. if (!(info = alloc_object( &startup_info_ops ))) return;
  767. list_add_head( &startup_info_list, &info->entry );
  768. info->inherit_all = req->inherit_all;
  769. info->create_flags = req->create_flags;
  770. info->unix_pid = req->unix_pid;
  771. info->hstdin = req->hstdin;
  772. info->hstdout = req->hstdout;
  773. info->hstderr = req->hstderr;
  774. info->exe_file = NULL;
  775. info->owner = (struct thread *)grab_object( current );
  776. info->process = NULL;
  777. info->thread = NULL;
  778. info->data_size = get_req_data_size();
  779. info->data = NULL;
  780. if (req->exe_file &&
  781. !(info->exe_file = get_file_obj( current->process, req->exe_file, GENERIC_READ )))
  782. goto done;
  783. if (!(info->data = memdup( get_req_data(), info->data_size ))) goto done;
  784. reply->info = alloc_handle( current->process, info, SYNCHRONIZE, FALSE );
  785. done:
  786. release_object( info );
  787. }
  788. /* Retrieve information about a newly started process */
  789. DECL_HANDLER(get_new_process_info)
  790. {
  791. struct startup_info *info;
  792. if ((info = (struct startup_info *)get_handle_obj( current->process, req->info,
  793. 0, &startup_info_ops )))
  794. {
  795. reply->pid = get_process_id( info->process );
  796. reply->tid = get_thread_id( info->thread );
  797. reply->phandle = alloc_handle( current->process, info->process,
  798. PROCESS_ALL_ACCESS, req->pinherit );
  799. reply->thandle = alloc_handle( current->process, info->thread,
  800. THREAD_ALL_ACCESS, req->tinherit );
  801. reply->success = is_process_init_done( info->process );
  802. release_object( info );
  803. }
  804. else
  805. {
  806. reply->pid = 0;
  807. reply->tid = 0;
  808. reply->phandle = 0;
  809. reply->thandle = 0;
  810. reply->success = 0;
  811. }
  812. }
  813. /* Retrieve the new process startup info */
  814. DECL_HANDLER(get_startup_info)
  815. {
  816. struct startup_info *info;
  817. if ((info = current->process->startup_info))
  818. {
  819. size_t size = info->data_size;
  820. if (size > get_reply_max_size()) size = get_reply_max_size();
  821. /* we return the data directly without making a copy so this can only be called once */
  822. set_reply_data_ptr( info->data, size );
  823. info->data = NULL;
  824. info->data_size = 0;
  825. }
  826. }
  827. /* initialize a new process */
  828. DECL_HANDLER(init_process)
  829. {
  830. if (current->unix_pid == -1)
  831. {
  832. fatal_protocol_error( current, "init_process: init_thread not called yet\n" );
  833. return;
  834. }
  835. if (current->process->startup_info)
  836. {
  837. fatal_protocol_error( current, "init_process: called twice\n" );
  838. return;
  839. }
  840. if (!req->peb || (unsigned int)req->peb % sizeof(int))
  841. {
  842. fatal_protocol_error( current, "init_process: bad peb address\n" );
  843. return;
  844. }
  845. if (!req->ldt_copy || (unsigned int)req->ldt_copy % sizeof(int))
  846. {
  847. fatal_protocol_error( current, "init_process: bad ldt_copy address\n" );
  848. return;
  849. }
  850. reply->info_size = 0;
  851. current->process->peb = req->peb;
  852. current->process->ldt_copy = req->ldt_copy;
  853. current->process->startup_info = init_process( reply );
  854. }
  855. /* signal the end of the process initialization */
  856. DECL_HANDLER(init_process_done)
  857. {
  858. struct file *file = NULL;
  859. struct process *process = current->process;
  860. if (is_process_init_done(process))
  861. {
  862. fatal_protocol_error( current, "init_process_done: called twice\n" );
  863. return;
  864. }
  865. if (!req->module)
  866. {
  867. fatal_protocol_error( current, "init_process_done: module base address cannot be 0\n" );
  868. return;
  869. }
  870. process->exe.base = req->module;
  871. process->exe.size = req->module_size;
  872. process->exe.name = req->name;
  873. if (req->exe_file) file = get_file_obj( process, req->exe_file, GENERIC_READ );
  874. if (process->exe.file) release_object( process->exe.file );
  875. process->exe.file = file;
  876. if ((process->exe.namelen = get_req_data_size()))
  877. process->exe.filename = memdup( get_req_data(), process->exe.namelen );
  878. generate_startup_debug_events( process, req->entry );
  879. set_process_startup_state( process, STARTUP_DONE );
  880. if (req->gui) process->idle_event = create_event( NULL, 0, 1, 0 );
  881. if (current->suspend + process->suspend > 0) stop_thread( current );
  882. if (process->debugger) set_process_debug_flag( process, 1 );
  883. }
  884. /* open a handle to a process */
  885. DECL_HANDLER(open_process)
  886. {
  887. struct process *process = get_process_from_id( req->pid );
  888. reply->handle = 0;
  889. if (process)
  890. {
  891. reply->handle = alloc_handle( current->process, process, req->access, req->inherit );
  892. release_object( process );
  893. }
  894. }
  895. /* terminate a process */
  896. DECL_HANDLER(terminate_process)
  897. {
  898. struct process *process;
  899. if ((process = get_process_from_handle( req->handle, PROCESS_TERMINATE )))
  900. {
  901. reply->self = (current->process == process);
  902. kill_process( process, current, req->exit_code );
  903. release_object( process );
  904. }
  905. }
  906. /* fetch information about a process */
  907. DECL_HANDLER(get_process_info)
  908. {
  909. struct process *process;
  910. if ((process = get_process_from_handle( req->handle, PROCESS_QUERY_INFORMATION )))
  911. {
  912. reply->pid = get_process_id( process );
  913. reply->ppid = process->parent ? get_process_id( process->parent ) : 0;
  914. reply->exit_code = process->exit_code;
  915. reply->priority = process->priority;
  916. reply->process_affinity = process->affinity;
  917. reply->system_affinity = 1;
  918. reply->peb = process->peb;
  919. release_object( process );
  920. }
  921. }
  922. /* set information about a process */
  923. DECL_HANDLER(set_process_info)
  924. {
  925. struct process *process;
  926. if ((process = get_process_from_handle( req->handle, PROCESS_SET_INFORMATION )))
  927. {
  928. if (req->mask & SET_PROCESS_INFO_PRIORITY) process->priority = req->priority;
  929. if (req->mask & SET_PROCESS_INFO_AFFINITY)
  930. {
  931. if (req->affinity != 1) set_error( STATUS_INVALID_PARAMETER );
  932. else process->affinity = req->affinity;
  933. }
  934. release_object( process );
  935. }
  936. }
  937. /* read data from a process address space */
  938. DECL_HANDLER(read_process_memory)
  939. {
  940. struct process *process;
  941. size_t len = get_reply_max_size();
  942. if (!(process = get_process_from_handle( req->handle, PROCESS_VM_READ ))) return;
  943. if (len)
  944. {
  945. unsigned int start_offset = (unsigned int)req->addr % sizeof(int);
  946. unsigned int nb_ints = (len + start_offset + sizeof(int) - 1) / sizeof(int);
  947. const int *start = (int *)((char *)req->addr - start_offset);
  948. int *buffer = mem_alloc( nb_ints * sizeof(int) );
  949. if (buffer)
  950. {
  951. if (read_process_memory( process, start, nb_ints, buffer ))
  952. {
  953. /* move start of requested data to start of buffer */
  954. if (start_offset) memmove( buffer, (char *)buffer + start_offset, len );
  955. set_reply_data_ptr( buffer, len );
  956. }
  957. else len = 0;
  958. }
  959. }
  960. release_object( process );
  961. }
  962. /* write data to a process address space */
  963. DECL_HANDLER(write_process_memory)
  964. {
  965. struct process *process;
  966. if ((process = get_process_from_handle( req->handle, PROCESS_VM_WRITE )))
  967. {
  968. size_t len = get_req_data_size();
  969. if ((len % sizeof(int)) || ((unsigned int)req->addr % sizeof(int)))
  970. set_error( STATUS_INVALID_PARAMETER );
  971. else
  972. {
  973. if (len) write_process_memory( process, req->addr, len / sizeof(int),
  974. req->first_mask, req->last_mask, get_req_data() );
  975. }
  976. release_object( process );
  977. }
  978. }
  979. /* notify the server that a dll has been loaded */
  980. DECL_HANDLER(load_dll)
  981. {
  982. struct process_dll *dll;
  983. struct file *file = NULL;
  984. if (req->handle && !(file = get_file_obj( current->process, req->handle, GENERIC_READ )))
  985. return;
  986. if ((dll = process_load_dll( current->process, file, req->base,
  987. get_req_data(), get_req_data_size() )))
  988. {
  989. dll->size = req->size;
  990. dll->dbg_offset = req->dbg_offset;
  991. dll->dbg_size = req->dbg_size;
  992. dll->name = req->name;
  993. /* only generate event if initialization is done */
  994. if (is_process_init_done( current->process ))
  995. generate_debug_event( current, LOAD_DLL_DEBUG_EVENT, dll );
  996. }
  997. if (file) release_object( file );
  998. }
  999. /* notify the server that a dll is being unloaded */
  1000. DECL_HANDLER(unload_dll)
  1001. {
  1002. process_unload_dll( current->process, req->base );
  1003. }
  1004. /* retrieve information about a module in a process */
  1005. DECL_HANDLER(get_dll_info)
  1006. {
  1007. struct process *process;
  1008. if ((process = get_process_from_handle( req->handle, PROCESS_QUERY_INFORMATION )))
  1009. {
  1010. struct process_dll *dll;
  1011. for (dll = &process->exe; dll; dll = dll->next)
  1012. {
  1013. if (dll->base == req->base_address)
  1014. {
  1015. reply->size = dll->size;
  1016. reply->entry_point = 0; /* FIXME */
  1017. if (dll->filename)
  1018. {
  1019. size_t len = min( dll->namelen, get_reply_max_size() );
  1020. set_reply_data( dll->filename, len );
  1021. }
  1022. break;
  1023. }
  1024. }
  1025. if (!dll)
  1026. set_error(STATUS_DLL_NOT_FOUND);
  1027. release_object( process );
  1028. }
  1029. }
  1030. /* wait for a process to start waiting on input */
  1031. /* FIXME: only returns event for now, wait is done in the client */
  1032. DECL_HANDLER(wait_input_idle)
  1033. {
  1034. struct process *process;
  1035. reply->event = 0;
  1036. if ((process = get_process_from_handle( req->handle, PROCESS_QUERY_INFORMATION )))
  1037. {
  1038. if (process->idle_event && process != current->process && process->queue != current->queue)
  1039. reply->event = alloc_handle( current->process, process->idle_event,
  1040. EVENT_ALL_ACCESS, 0 );
  1041. release_object( process );
  1042. }
  1043. }