process.c 63 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948
  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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, 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. #ifdef HAVE_POLL_H
  35. #include <poll.h>
  36. #endif
  37. #include "ntstatus.h"
  38. #define WIN32_NO_STATUS
  39. #include "winternl.h"
  40. #include "device.h"
  41. #include "file.h"
  42. #include "handle.h"
  43. #include "process.h"
  44. #include "thread.h"
  45. #include "request.h"
  46. #include "user.h"
  47. #include "security.h"
  48. #include "esync.h"
  49. /* process structure */
  50. static struct list process_list = LIST_INIT(process_list);
  51. static int running_processes, user_processes;
  52. static struct event *shutdown_event; /* signaled when shutdown starts */
  53. static struct timeout_user *shutdown_timeout; /* timeout for server shutdown */
  54. static int shutdown_stage; /* current stage in the shutdown process */
  55. /* process operations */
  56. static void process_dump( struct object *obj, int verbose );
  57. static struct object_type *process_get_type( struct object *obj );
  58. static int process_signaled( struct object *obj, struct wait_queue_entry *entry );
  59. static unsigned int process_map_access( struct object *obj, unsigned int access );
  60. static struct security_descriptor *process_get_sd( struct object *obj );
  61. static void process_poll_event( struct fd *fd, int event );
  62. static struct list *process_get_kernel_obj_list( struct object *obj );
  63. static void process_destroy( struct object *obj );
  64. static int process_get_esync_fd( struct object *obj, enum esync_type *type );
  65. static void terminate_process( struct process *process, struct thread *skip, int exit_code );
  66. static const struct object_ops process_ops =
  67. {
  68. sizeof(struct process), /* size */
  69. process_dump, /* dump */
  70. process_get_type, /* get_type */
  71. add_queue, /* add_queue */
  72. remove_queue, /* remove_queue */
  73. process_signaled, /* signaled */
  74. process_get_esync_fd, /* get_esync_fd */
  75. no_satisfied, /* satisfied */
  76. no_signal, /* signal */
  77. no_get_fd, /* get_fd */
  78. process_map_access, /* map_access */
  79. process_get_sd, /* get_sd */
  80. default_set_sd, /* set_sd */
  81. no_lookup_name, /* lookup_name */
  82. no_link_name, /* link_name */
  83. NULL, /* unlink_name */
  84. no_open_file, /* open_file */
  85. process_get_kernel_obj_list, /* get_kernel_obj_list */
  86. no_alloc_handle, /* alloc_handle */
  87. no_close_handle, /* close_handle */
  88. process_destroy /* destroy */
  89. };
  90. static const struct fd_ops process_fd_ops =
  91. {
  92. NULL, /* get_poll_events */
  93. process_poll_event, /* poll_event */
  94. NULL, /* flush */
  95. NULL, /* get_fd_type */
  96. NULL, /* ioctl */
  97. NULL, /* queue_async */
  98. NULL, /* reselect_async */
  99. NULL /* cancel async */
  100. };
  101. /* process startup info */
  102. struct startup_info
  103. {
  104. struct object obj; /* object header */
  105. struct process *process; /* created process */
  106. data_size_t info_size; /* size of startup info */
  107. data_size_t data_size; /* size of whole startup data */
  108. startup_info_t *data; /* data for startup info */
  109. };
  110. static void startup_info_dump( struct object *obj, int verbose );
  111. static int startup_info_signaled( struct object *obj, struct wait_queue_entry *entry );
  112. static void startup_info_destroy( struct object *obj );
  113. static const struct object_ops startup_info_ops =
  114. {
  115. sizeof(struct startup_info), /* size */
  116. startup_info_dump, /* dump */
  117. no_get_type, /* get_type */
  118. add_queue, /* add_queue */
  119. remove_queue, /* remove_queue */
  120. startup_info_signaled, /* signaled */
  121. NULL, /* get_esync_fd */
  122. no_satisfied, /* satisfied */
  123. no_signal, /* signal */
  124. no_get_fd, /* get_fd */
  125. no_map_access, /* map_access */
  126. default_get_sd, /* get_sd */
  127. default_set_sd, /* set_sd */
  128. no_lookup_name, /* lookup_name */
  129. no_link_name, /* link_name */
  130. NULL, /* unlink_name */
  131. no_open_file, /* open_file */
  132. no_kernel_obj_list, /* get_kernel_obj_list */
  133. no_alloc_handle, /* alloc_handle */
  134. no_close_handle, /* close_handle */
  135. startup_info_destroy /* destroy */
  136. };
  137. /* job object */
  138. static void job_dump( struct object *obj, int verbose );
  139. static struct object_type *job_get_type( struct object *obj );
  140. static int job_signaled( struct object *obj, struct wait_queue_entry *entry );
  141. static unsigned int job_map_access( struct object *obj, unsigned int access );
  142. static int job_close_handle( struct object *obj, struct process *process, obj_handle_t handle );
  143. static void job_destroy( struct object *obj );
  144. struct job
  145. {
  146. struct object obj; /* object header */
  147. struct list process_list; /* list of all processes */
  148. int num_processes; /* count of running processes */
  149. unsigned int limit_flags; /* limit flags */
  150. int terminating; /* job is terminating */
  151. int signaled; /* job is signaled */
  152. struct completion *completion_port; /* associated completion port */
  153. apc_param_t completion_key; /* key to send with completion messages */
  154. };
  155. static const struct object_ops job_ops =
  156. {
  157. sizeof(struct job), /* size */
  158. job_dump, /* dump */
  159. job_get_type, /* get_type */
  160. add_queue, /* add_queue */
  161. remove_queue, /* remove_queue */
  162. job_signaled, /* signaled */
  163. NULL, /* get_esync_fd */
  164. no_satisfied, /* satisfied */
  165. no_signal, /* signal */
  166. no_get_fd, /* get_fd */
  167. job_map_access, /* map_access */
  168. default_get_sd, /* get_sd */
  169. default_set_sd, /* set_sd */
  170. no_lookup_name, /* lookup_name */
  171. directory_link_name, /* link_name */
  172. default_unlink_name, /* unlink_name */
  173. no_open_file, /* open_file */
  174. no_kernel_obj_list, /* get_kernel_obj_list */
  175. no_alloc_handle, /* alloc_handle */
  176. job_close_handle, /* close_handle */
  177. job_destroy /* destroy */
  178. };
  179. static struct job *create_job_object( struct object *root, const struct unicode_str *name,
  180. unsigned int attr, const struct security_descriptor *sd )
  181. {
  182. struct job *job;
  183. if ((job = create_named_object( root, &job_ops, name, attr, sd )))
  184. {
  185. if (get_error() != STATUS_OBJECT_NAME_EXISTS)
  186. {
  187. /* initialize it if it didn't already exist */
  188. list_init( &job->process_list );
  189. job->num_processes = 0;
  190. job->limit_flags = 0;
  191. job->terminating = 0;
  192. job->signaled = 0;
  193. job->completion_port = NULL;
  194. job->completion_key = 0;
  195. }
  196. }
  197. return job;
  198. }
  199. static struct job *get_job_obj( struct process *process, obj_handle_t handle, unsigned int access )
  200. {
  201. return (struct job *)get_handle_obj( process, handle, access, &job_ops );
  202. }
  203. static struct object_type *job_get_type( struct object *obj )
  204. {
  205. static const struct unicode_str str = { type_Job, sizeof(type_Job) };
  206. return get_object_type( &str );
  207. };
  208. static unsigned int job_map_access( struct object *obj, unsigned int access )
  209. {
  210. if (access & GENERIC_READ) access |= STANDARD_RIGHTS_READ;
  211. if (access & GENERIC_WRITE) access |= STANDARD_RIGHTS_WRITE;
  212. if (access & GENERIC_EXECUTE) access |= STANDARD_RIGHTS_EXECUTE;
  213. if (access & GENERIC_ALL) access |= JOB_OBJECT_ALL_ACCESS;
  214. return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
  215. }
  216. static void add_job_completion( struct job *job, apc_param_t msg, apc_param_t pid )
  217. {
  218. if (job->completion_port)
  219. add_completion( job->completion_port, job->completion_key, pid, STATUS_SUCCESS, msg );
  220. }
  221. static void add_job_process( struct job *job, struct process *process )
  222. {
  223. process->job = (struct job *)grab_object( job );
  224. list_add_tail( &job->process_list, &process->job_entry );
  225. job->num_processes++;
  226. add_job_completion( job, JOB_OBJECT_MSG_NEW_PROCESS, get_process_id(process) );
  227. }
  228. /* called when a process has terminated, allow one additional process */
  229. static void release_job_process( struct process *process )
  230. {
  231. struct job *job = process->job;
  232. if (!job) return;
  233. assert( job->num_processes );
  234. job->num_processes--;
  235. if (!job->terminating)
  236. add_job_completion( job, JOB_OBJECT_MSG_EXIT_PROCESS, get_process_id(process) );
  237. if (!job->num_processes)
  238. add_job_completion( job, JOB_OBJECT_MSG_ACTIVE_PROCESS_ZERO, 0 );
  239. }
  240. static void terminate_job( struct job *job, int exit_code )
  241. {
  242. /* don't report completion events for terminated processes */
  243. job->terminating = 1;
  244. for (;;) /* restart from the beginning of the list every time */
  245. {
  246. struct process *process;
  247. /* find the first process associated with this job and still running */
  248. LIST_FOR_EACH_ENTRY( process, &job->process_list, struct process, job_entry )
  249. {
  250. if (process->running_threads) break;
  251. }
  252. if (&process->job_entry == &job->process_list) break; /* no process found */
  253. assert( process->job == job );
  254. terminate_process( process, NULL, exit_code );
  255. }
  256. job->terminating = 0;
  257. job->signaled = 1;
  258. wake_up( &job->obj, 0 );
  259. }
  260. static int job_close_handle( struct object *obj, struct process *process, obj_handle_t handle )
  261. {
  262. struct job *job = (struct job *)obj;
  263. assert( obj->ops == &job_ops );
  264. if (obj->handle_count == 1) /* last handle */
  265. {
  266. if (job->limit_flags & JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE)
  267. terminate_job( job, 0 );
  268. }
  269. return 1;
  270. }
  271. static void job_destroy( struct object *obj )
  272. {
  273. struct job *job = (struct job *)obj;
  274. assert( obj->ops == &job_ops );
  275. assert( !job->num_processes );
  276. assert( list_empty(&job->process_list) );
  277. if (job->completion_port) release_object( job->completion_port );
  278. }
  279. static void job_dump( struct object *obj, int verbose )
  280. {
  281. struct job *job = (struct job *)obj;
  282. assert( obj->ops == &job_ops );
  283. fprintf( stderr, "Job processes=%d\n", list_count(&job->process_list) );
  284. }
  285. static int job_signaled( struct object *obj, struct wait_queue_entry *entry )
  286. {
  287. struct job *job = (struct job *)obj;
  288. return job->signaled;
  289. }
  290. struct ptid_entry
  291. {
  292. void *ptr; /* entry ptr */
  293. unsigned int next; /* next free entry */
  294. };
  295. static struct ptid_entry *ptid_entries; /* array of ptid entries */
  296. static unsigned int used_ptid_entries; /* number of entries in use */
  297. static unsigned int alloc_ptid_entries; /* number of allocated entries */
  298. static unsigned int next_free_ptid; /* next free entry */
  299. static unsigned int last_free_ptid; /* last free entry */
  300. static unsigned int num_free_ptids; /* number of free ptids */
  301. static void kill_all_processes(void);
  302. #define PTID_OFFSET 8 /* offset for first ptid value */
  303. static unsigned int index_from_ptid(unsigned int id) { return id / 4; }
  304. static unsigned int ptid_from_index(unsigned int index) { return index * 4; }
  305. /* allocate a new process or thread id */
  306. unsigned int alloc_ptid( void *ptr )
  307. {
  308. struct ptid_entry *entry;
  309. unsigned int index;
  310. if (used_ptid_entries < alloc_ptid_entries)
  311. {
  312. index = used_ptid_entries + PTID_OFFSET;
  313. entry = &ptid_entries[used_ptid_entries++];
  314. }
  315. else if (next_free_ptid && num_free_ptids >= 256)
  316. {
  317. index = next_free_ptid;
  318. entry = &ptid_entries[index - PTID_OFFSET];
  319. if (!(next_free_ptid = entry->next)) last_free_ptid = 0;
  320. num_free_ptids--;
  321. }
  322. else /* need to grow the array */
  323. {
  324. unsigned int count = alloc_ptid_entries + (alloc_ptid_entries / 2);
  325. if (!count) count = 512;
  326. if (!(entry = realloc( ptid_entries, count * sizeof(*entry) )))
  327. {
  328. set_error( STATUS_NO_MEMORY );
  329. return 0;
  330. }
  331. ptid_entries = entry;
  332. alloc_ptid_entries = count;
  333. index = used_ptid_entries + PTID_OFFSET;
  334. entry = &ptid_entries[used_ptid_entries++];
  335. }
  336. entry->ptr = ptr;
  337. return ptid_from_index( index );
  338. }
  339. /* free a process or thread id */
  340. void free_ptid( unsigned int id )
  341. {
  342. unsigned int index = index_from_ptid( id );
  343. struct ptid_entry *entry = &ptid_entries[index - PTID_OFFSET];
  344. entry->ptr = NULL;
  345. entry->next = 0;
  346. /* append to end of free list so that we don't reuse it too early */
  347. if (last_free_ptid) ptid_entries[last_free_ptid - PTID_OFFSET].next = index;
  348. else next_free_ptid = index;
  349. last_free_ptid = index;
  350. num_free_ptids++;
  351. }
  352. /* retrieve the pointer corresponding to a process or thread id */
  353. void *get_ptid_entry( unsigned int id )
  354. {
  355. unsigned int index = index_from_ptid( id );
  356. if (index < PTID_OFFSET) return NULL;
  357. if (index - PTID_OFFSET >= used_ptid_entries) return NULL;
  358. return ptid_entries[index - PTID_OFFSET].ptr;
  359. }
  360. /* return the main thread of the process */
  361. struct thread *get_process_first_thread( struct process *process )
  362. {
  363. struct list *ptr = list_head( &process->thread_list );
  364. if (!ptr) return NULL;
  365. return LIST_ENTRY( ptr, struct thread, proc_entry );
  366. }
  367. /* set the state of the process startup info */
  368. static void set_process_startup_state( struct process *process, enum startup_state state )
  369. {
  370. if (process->startup_state == STARTUP_IN_PROGRESS) process->startup_state = state;
  371. if (process->startup_info)
  372. {
  373. wake_up( &process->startup_info->obj, 0 );
  374. release_object( process->startup_info );
  375. process->startup_info = NULL;
  376. }
  377. }
  378. /* callback for server shutdown */
  379. static void server_shutdown_timeout( void *arg )
  380. {
  381. shutdown_timeout = NULL;
  382. if (!running_processes)
  383. {
  384. close_master_socket( 0 );
  385. return;
  386. }
  387. switch(++shutdown_stage)
  388. {
  389. case 1: /* signal system processes to exit */
  390. if (debug_level) fprintf( stderr, "wineserver: shutting down\n" );
  391. if (shutdown_event) set_event( shutdown_event );
  392. shutdown_timeout = add_timeout_user( 2 * -TICKS_PER_SEC, server_shutdown_timeout, NULL );
  393. close_master_socket( 4 * -TICKS_PER_SEC );
  394. break;
  395. case 2: /* now forcibly kill all processes (but still wait for SIGKILL timeouts) */
  396. kill_all_processes();
  397. break;
  398. }
  399. }
  400. /* forced shutdown, used for wineserver -k */
  401. void shutdown_master_socket(void)
  402. {
  403. kill_all_processes();
  404. shutdown_stage = 2;
  405. if (shutdown_timeout)
  406. {
  407. remove_timeout_user( shutdown_timeout );
  408. shutdown_timeout = NULL;
  409. }
  410. close_master_socket( 2 * -TICKS_PER_SEC ); /* for SIGKILL timeouts */
  411. }
  412. /* final cleanup once we are sure a process is really dead */
  413. static void process_died( struct process *process )
  414. {
  415. if (debug_level) fprintf( stderr, "%04x: *process killed*\n", process->id );
  416. if (!process->is_system)
  417. {
  418. if (!--user_processes && !shutdown_stage && master_socket_timeout != TIMEOUT_INFINITE)
  419. shutdown_timeout = add_timeout_user( master_socket_timeout, server_shutdown_timeout, NULL );
  420. }
  421. release_object( process );
  422. if (!--running_processes && shutdown_stage) close_master_socket( 0 );
  423. }
  424. /* callback for process sigkill timeout */
  425. static void process_sigkill( void *private )
  426. {
  427. struct process *process = private;
  428. process->sigkill_timeout = NULL;
  429. kill( process->unix_pid, SIGKILL );
  430. process_died( process );
  431. }
  432. /* start the sigkill timer for a process upon exit */
  433. static void start_sigkill_timer( struct process *process )
  434. {
  435. grab_object( process );
  436. if (process->unix_pid != -1 && process->msg_fd)
  437. process->sigkill_timeout = add_timeout_user( -TICKS_PER_SEC, process_sigkill, process );
  438. else
  439. process_died( process );
  440. }
  441. /* create a new process */
  442. /* if the function fails the fd is closed */
  443. struct process *create_process( int fd, struct process *parent, int inherit_all,
  444. const struct security_descriptor *sd, struct token *token )
  445. {
  446. struct process *process;
  447. krnl_cbdata_t cbdata;
  448. if (!(process = alloc_object( &process_ops )))
  449. {
  450. close( fd );
  451. goto error;
  452. }
  453. process->parent_id = 0;
  454. process->debugger = NULL;
  455. process->debug_event = NULL;
  456. process->handles = NULL;
  457. process->msg_fd = NULL;
  458. process->sigkill_timeout = NULL;
  459. process->unix_pid = -1;
  460. process->exit_code = STILL_ACTIVE;
  461. process->running_threads = 0;
  462. process->priority = PROCESS_PRIOCLASS_NORMAL;
  463. process->suspend = 0;
  464. process->is_system = 0;
  465. process->debug_children = 1;
  466. process->is_terminating = 0;
  467. process->is_kernel = 0;
  468. process->job = NULL;
  469. process->console = NULL;
  470. process->startup_state = STARTUP_IN_PROGRESS;
  471. process->startup_info = NULL;
  472. process->idle_event = NULL;
  473. process->exe_file = NULL;
  474. process->peb = 0;
  475. process->ldt_copy = 0;
  476. process->dir_cache = NULL;
  477. process->winstation = 0;
  478. process->desktop = 0;
  479. process->token = NULL;
  480. process->trace_data = 0;
  481. process->rawinput_mouse = NULL;
  482. process->rawinput_kbd = NULL;
  483. process->dev_mgr = NULL;
  484. process->callback_init_event = NULL;
  485. process->esync_fd = -1;
  486. list_init( &process->kernel_object );
  487. list_init( &process->thread_list );
  488. list_init( &process->locks );
  489. list_init( &process->asyncs );
  490. list_init( &process->classes );
  491. list_init( &process->views );
  492. list_init( &process->dlls );
  493. list_init( &process->rawinput_devices );
  494. process->end_time = 0;
  495. list_add_tail( &process_list, &process->entry );
  496. if (sd && !default_set_sd( &process->obj, sd, OWNER_SECURITY_INFORMATION | GROUP_SECURITY_INFORMATION |
  497. DACL_SECURITY_INFORMATION | SACL_SECURITY_INFORMATION ))
  498. {
  499. close( fd );
  500. goto error;
  501. }
  502. if (!(process->id = process->group_id = alloc_ptid( process )))
  503. {
  504. close( fd );
  505. goto error;
  506. }
  507. if (!(process->msg_fd = create_anonymous_fd( &process_fd_ops, fd, &process->obj, 0 ))) goto error;
  508. /* create the handle table */
  509. if (!parent)
  510. {
  511. process->handles = alloc_handle_table( process, 0 );
  512. process->token = token_create_admin();
  513. process->affinity = ~0;
  514. }
  515. else
  516. {
  517. process->parent_id = parent->id;
  518. process->handles = inherit_all ? copy_handle_table( process, parent )
  519. : alloc_handle_table( process, 0 );
  520. /* Note: for security reasons, starting a new process does not attempt
  521. * to use the current impersonation token for the new process */
  522. process->token = token_duplicate( token ? token : parent->token, TRUE, 0, NULL, NULL, 0, NULL, 0, NULL );
  523. process->affinity = parent->affinity;
  524. }
  525. if (!process->handles || !process->token) goto error;
  526. cbdata.cb_type = SERVER_CALLBACK_PROC_LIFE;
  527. cbdata.process_life.create = 1;
  528. cbdata.process_life.pid = process->id;
  529. cbdata.process_life.ppid = process->parent_id;
  530. queue_callback(&cbdata, NULL, NULL);
  531. if (do_esync())
  532. process->esync_fd = esync_create_fd( 0, 0 );
  533. set_fd_events( process->msg_fd, POLLIN ); /* start listening to events */
  534. return process;
  535. error:
  536. if (process) release_object( process );
  537. /* if we failed to start our first process, close everything down */
  538. if (!running_processes && master_socket_timeout != TIMEOUT_INFINITE) close_master_socket( 0 );
  539. return NULL;
  540. }
  541. /* initialize the current process and fill in the request */
  542. data_size_t init_process( struct thread *thread )
  543. {
  544. struct process *process = thread->process;
  545. struct startup_info *info = process->startup_info;
  546. if (!info) return 0;
  547. return info->data_size;
  548. }
  549. /* destroy a process when its refcount is 0 */
  550. static void process_destroy( struct object *obj )
  551. {
  552. struct process *process = (struct process *)obj;
  553. assert( obj->ops == &process_ops );
  554. /* we can't have a thread remaining */
  555. assert( list_empty( &process->thread_list ));
  556. assert( list_empty( &process->asyncs ));
  557. assert( !process->sigkill_timeout ); /* timeout should hold a reference to the process */
  558. close_process_handles( process );
  559. set_process_startup_state( process, STARTUP_ABORTED );
  560. if (process->job)
  561. {
  562. list_remove( &process->job_entry );
  563. release_object( process->job );
  564. }
  565. if (process->console) release_object( process->console );
  566. if (process->msg_fd) release_object( process->msg_fd );
  567. list_remove( &process->entry );
  568. if (process->idle_event) release_object( process->idle_event );
  569. if (process->exe_file) release_object( process->exe_file );
  570. if (process->id) free_ptid( process->id );
  571. if (process->token) release_object( process->token );
  572. if (process->callback_init_event) release_object( process->callback_init_event );
  573. free( process->dir_cache );
  574. if (do_esync())
  575. close( process->esync_fd );
  576. }
  577. /* dump a process on stdout for debugging purposes */
  578. static void process_dump( struct object *obj, int verbose )
  579. {
  580. struct process *process = (struct process *)obj;
  581. assert( obj->ops == &process_ops );
  582. fprintf( stderr, "Process id=%04x handles=%p\n", process->id, process->handles );
  583. }
  584. static struct object_type *process_get_type( struct object *obj )
  585. {
  586. static const struct unicode_str str = { type_Process, sizeof(type_Process) };
  587. return get_object_type( &str );
  588. }
  589. static int process_signaled( struct object *obj, struct wait_queue_entry *entry )
  590. {
  591. struct process *process = (struct process *)obj;
  592. return !process->running_threads;
  593. }
  594. static int process_get_esync_fd( struct object *obj, enum esync_type *type )
  595. {
  596. struct process *process = (struct process *)obj;
  597. *type = ESYNC_MANUAL_SERVER;
  598. return process->esync_fd;
  599. }
  600. static unsigned int process_map_access( struct object *obj, unsigned int access )
  601. {
  602. if (access & GENERIC_READ) access |= STANDARD_RIGHTS_READ | PROCESS_QUERY_INFORMATION | PROCESS_VM_READ;
  603. if (access & GENERIC_WRITE) access |= STANDARD_RIGHTS_WRITE | PROCESS_SET_QUOTA | PROCESS_SET_INFORMATION | PROCESS_SUSPEND_RESUME |
  604. PROCESS_VM_WRITE | PROCESS_DUP_HANDLE | PROCESS_CREATE_PROCESS | PROCESS_CREATE_THREAD | PROCESS_VM_OPERATION;
  605. if (access & GENERIC_EXECUTE) access |= STANDARD_RIGHTS_EXECUTE | SYNCHRONIZE | PROCESS_QUERY_LIMITED_INFORMATION | PROCESS_TERMINATE;
  606. if (access & GENERIC_ALL) access |= PROCESS_ALL_ACCESS;
  607. if (access & PROCESS_QUERY_INFORMATION) access |= PROCESS_QUERY_LIMITED_INFORMATION;
  608. if (access & PROCESS_SET_INFORMATION) access |= PROCESS_SET_LIMITED_INFORMATION;
  609. return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
  610. }
  611. static struct list *process_get_kernel_obj_list( struct object *obj )
  612. {
  613. struct process *process = (struct process *)obj;
  614. return &process->kernel_object;
  615. }
  616. static struct security_descriptor *process_get_sd( struct object *obj )
  617. {
  618. static struct security_descriptor *process_default_sd;
  619. if (obj->sd) return obj->sd;
  620. if (!process_default_sd)
  621. {
  622. size_t users_sid_len = security_sid_len( security_domain_users_sid );
  623. size_t admins_sid_len = security_sid_len( security_builtin_admins_sid );
  624. size_t dacl_len = sizeof(ACL) + 2 * offsetof( ACCESS_ALLOWED_ACE, SidStart )
  625. + users_sid_len + admins_sid_len;
  626. ACCESS_ALLOWED_ACE *aaa;
  627. ACL *dacl;
  628. process_default_sd = mem_alloc( sizeof(*process_default_sd) + admins_sid_len + users_sid_len
  629. + dacl_len );
  630. process_default_sd->control = SE_DACL_PRESENT;
  631. process_default_sd->owner_len = admins_sid_len;
  632. process_default_sd->group_len = users_sid_len;
  633. process_default_sd->sacl_len = 0;
  634. process_default_sd->dacl_len = dacl_len;
  635. memcpy( process_default_sd + 1, security_builtin_admins_sid, admins_sid_len );
  636. memcpy( (char *)(process_default_sd + 1) + admins_sid_len, security_domain_users_sid, users_sid_len );
  637. dacl = (ACL *)((char *)(process_default_sd + 1) + admins_sid_len + users_sid_len);
  638. dacl->AclRevision = ACL_REVISION;
  639. dacl->Sbz1 = 0;
  640. dacl->AclSize = dacl_len;
  641. dacl->AceCount = 2;
  642. dacl->Sbz2 = 0;
  643. aaa = (ACCESS_ALLOWED_ACE *)(dacl + 1);
  644. aaa->Header.AceType = ACCESS_ALLOWED_ACE_TYPE;
  645. aaa->Header.AceFlags = INHERIT_ONLY_ACE | CONTAINER_INHERIT_ACE;
  646. aaa->Header.AceSize = offsetof( ACCESS_ALLOWED_ACE, SidStart ) + users_sid_len;
  647. aaa->Mask = GENERIC_READ;
  648. memcpy( &aaa->SidStart, security_domain_users_sid, users_sid_len );
  649. aaa = (ACCESS_ALLOWED_ACE *)((char *)aaa + aaa->Header.AceSize);
  650. aaa->Header.AceType = ACCESS_ALLOWED_ACE_TYPE;
  651. aaa->Header.AceFlags = 0;
  652. aaa->Header.AceSize = offsetof( ACCESS_ALLOWED_ACE, SidStart ) + admins_sid_len;
  653. aaa->Mask = PROCESS_ALL_ACCESS;
  654. memcpy( &aaa->SidStart, security_builtin_admins_sid, admins_sid_len );
  655. }
  656. return process_default_sd;
  657. }
  658. static void process_poll_event( struct fd *fd, int event )
  659. {
  660. struct process *process = get_fd_user( fd );
  661. assert( process->obj.ops == &process_ops );
  662. if (event & (POLLERR | POLLHUP)) kill_process( process, 0 );
  663. else if (event & POLLIN) receive_fd( process );
  664. }
  665. static void startup_info_destroy( struct object *obj )
  666. {
  667. struct startup_info *info = (struct startup_info *)obj;
  668. assert( obj->ops == &startup_info_ops );
  669. free( info->data );
  670. if (info->process) release_object( info->process );
  671. }
  672. static void startup_info_dump( struct object *obj, int verbose )
  673. {
  674. struct startup_info *info = (struct startup_info *)obj;
  675. assert( obj->ops == &startup_info_ops );
  676. fprintf( stderr, "Startup info in=%04x out=%04x err=%04x\n",
  677. info->data->hstdin, info->data->hstdout, info->data->hstderr );
  678. }
  679. static int startup_info_signaled( struct object *obj, struct wait_queue_entry *entry )
  680. {
  681. struct startup_info *info = (struct startup_info *)obj;
  682. return info->process && info->process->startup_state != STARTUP_IN_PROGRESS;
  683. }
  684. /* get a process from an id (and increment the refcount) */
  685. struct process *get_process_from_id( process_id_t id )
  686. {
  687. struct object *obj = get_ptid_entry( id );
  688. if (obj && obj->ops == &process_ops) return (struct process *)grab_object( obj );
  689. set_error( STATUS_INVALID_PARAMETER );
  690. return NULL;
  691. }
  692. /* get a process from a handle (and increment the refcount) */
  693. struct process *get_process_from_handle( obj_handle_t handle, unsigned int access )
  694. {
  695. return (struct process *)get_handle_obj( current->process, handle,
  696. access, &process_ops );
  697. }
  698. /* find a dll from its base address */
  699. static inline struct process_dll *find_process_dll( struct process *process, mod_handle_t base )
  700. {
  701. struct process_dll *dll;
  702. LIST_FOR_EACH_ENTRY( dll, &process->dlls, struct process_dll, entry )
  703. {
  704. if (dll->base == base) return dll;
  705. }
  706. return NULL;
  707. }
  708. /* add a dll to a process list */
  709. static struct process_dll *process_load_dll( struct process *process, mod_handle_t base,
  710. const WCHAR *filename, data_size_t name_len,
  711. struct object **done_event )
  712. {
  713. struct process_dll *dll;
  714. /* make sure we don't already have one with the same base address */
  715. if (find_process_dll( process, base ))
  716. {
  717. set_error( STATUS_INVALID_PARAMETER );
  718. return NULL;
  719. }
  720. if ((dll = mem_alloc( sizeof(*dll) )))
  721. {
  722. krnl_cbdata_t cbdata;
  723. struct unicode_str filename_str = {filename, name_len};
  724. dll->base = base;
  725. dll->filename = NULL;
  726. dll->namelen = name_len;
  727. if (name_len && !(dll->filename = memdup( filename, name_len )))
  728. {
  729. free( dll );
  730. return NULL;
  731. }
  732. list_add_tail( &process->dlls, &dll->entry );
  733. cbdata.cb_type = SERVER_CALLBACK_IMAGE_LIFE;
  734. cbdata.image_life.pid = current->process->id;
  735. cbdata.image_life.base = base;
  736. cbdata.image_life.size = 0;
  737. queue_callback(&cbdata, filename ? &filename_str : NULL, done_event);
  738. }
  739. return dll;
  740. }
  741. /* remove a dll from a process list */
  742. static void process_unload_dll( struct process *process, mod_handle_t base )
  743. {
  744. struct process_dll *dll = find_process_dll( process, base );
  745. if (dll && (&dll->entry != list_head( &process->dlls ))) /* main exe can't be unloaded */
  746. {
  747. free( dll->filename );
  748. list_remove( &dll->entry );
  749. free( dll );
  750. generate_debug_event( current, UNLOAD_DLL_DEBUG_EVENT, &base );
  751. }
  752. else set_error( STATUS_INVALID_PARAMETER );
  753. }
  754. /* terminate a process with the given exit code */
  755. static void terminate_process( struct process *process, struct thread *skip, int exit_code )
  756. {
  757. struct thread *thread;
  758. grab_object( process ); /* make sure it doesn't get freed when threads die */
  759. process->is_terminating = 1;
  760. restart:
  761. LIST_FOR_EACH_ENTRY( thread, &process->thread_list, struct thread, proc_entry )
  762. {
  763. if (exit_code) thread->exit_code = exit_code;
  764. if (thread == skip) continue;
  765. if (thread->state == TERMINATED) continue;
  766. kill_thread( thread, 1 );
  767. goto restart;
  768. }
  769. release_object( process );
  770. }
  771. /* kill all processes */
  772. static void kill_all_processes(void)
  773. {
  774. for (;;)
  775. {
  776. struct process *process;
  777. LIST_FOR_EACH_ENTRY( process, &process_list, struct process, entry )
  778. {
  779. if (process->running_threads) break;
  780. }
  781. if (&process->entry == &process_list) break; /* no process found */
  782. terminate_process( process, NULL, 1 );
  783. }
  784. }
  785. /* kill all processes being attached to a console renderer */
  786. void kill_console_processes( struct thread *renderer, int exit_code )
  787. {
  788. for (;;) /* restart from the beginning of the list every time */
  789. {
  790. struct process *process;
  791. /* find the first process being attached to 'renderer' and still running */
  792. LIST_FOR_EACH_ENTRY( process, &process_list, struct process, entry )
  793. {
  794. if (process == renderer->process) continue;
  795. if (!process->running_threads) continue;
  796. if (process->console && console_get_renderer( process->console ) == renderer) break;
  797. }
  798. if (&process->entry == &process_list) break; /* no process found */
  799. terminate_process( process, NULL, exit_code );
  800. }
  801. }
  802. /* a process has been killed (i.e. its last thread died) */
  803. static void process_killed( struct process *process )
  804. {
  805. struct list *ptr;
  806. krnl_cbdata_t cbdata;
  807. queue_callback(&cbdata, NULL, NULL);
  808. assert( list_empty( &process->thread_list ));
  809. process->end_time = current_time;
  810. process->winstation = 0;
  811. process->desktop = 0;
  812. close_process_handles( process );
  813. cancel_process_asyncs( process );
  814. if (process->idle_event) release_object( process->idle_event );
  815. if (process->exe_file) release_object( process->exe_file );
  816. process->idle_event = NULL;
  817. process->exe_file = NULL;
  818. /* close the console attached to this process, if any */
  819. free_console( process );
  820. while ((ptr = list_head( &process->rawinput_devices )))
  821. {
  822. struct rawinput_device_entry *entry = LIST_ENTRY( ptr, struct rawinput_device_entry, entry );
  823. list_remove( &entry->entry );
  824. free( entry );
  825. }
  826. while ((ptr = list_head( &process->dlls )))
  827. {
  828. struct process_dll *dll = LIST_ENTRY( ptr, struct process_dll, entry );
  829. free( dll->filename );
  830. list_remove( &dll->entry );
  831. free( dll );
  832. }
  833. destroy_process_classes( process );
  834. free_mapped_views( process );
  835. free_process_user_handles( process );
  836. remove_process_locks( process );
  837. set_process_startup_state( process, STARTUP_ABORTED );
  838. finish_process_tracing( process );
  839. release_job_process( process );
  840. start_sigkill_timer( process );
  841. wake_up( &process->obj, 0 );
  842. cbdata.cb_type = SERVER_CALLBACK_PROC_LIFE;
  843. cbdata.process_life.create = 0;
  844. cbdata.process_life.pid = process->id;
  845. cbdata.process_life.ppid = process->parent_id;
  846. }
  847. /* add a thread to a process running threads list */
  848. void add_process_thread( struct process *process, struct thread *thread )
  849. {
  850. list_add_tail( &process->thread_list, &thread->proc_entry );
  851. if (!process->running_threads++)
  852. {
  853. running_processes++;
  854. if (!process->is_system)
  855. {
  856. if (!user_processes++ && shutdown_timeout)
  857. {
  858. remove_timeout_user( shutdown_timeout );
  859. shutdown_timeout = NULL;
  860. }
  861. }
  862. }
  863. grab_object( thread );
  864. }
  865. /* remove a thread from a process running threads list */
  866. void remove_process_thread( struct process *process, struct thread *thread )
  867. {
  868. assert( process->running_threads > 0 );
  869. assert( !list_empty( &process->thread_list ));
  870. list_remove( &thread->proc_entry );
  871. if (!--process->running_threads)
  872. {
  873. /* we have removed the last running thread, exit the process */
  874. process->exit_code = thread->exit_code;
  875. generate_debug_event( thread, EXIT_PROCESS_DEBUG_EVENT, process );
  876. process_killed( process );
  877. }
  878. else generate_debug_event( thread, EXIT_THREAD_DEBUG_EVENT, thread );
  879. release_object( thread );
  880. }
  881. /* suspend all the threads of a process */
  882. void suspend_process( struct process *process )
  883. {
  884. if (!process->suspend++)
  885. {
  886. struct list *ptr, *next;
  887. LIST_FOR_EACH_SAFE( ptr, next, &process->thread_list )
  888. {
  889. struct thread *thread = LIST_ENTRY( ptr, struct thread, proc_entry );
  890. if (!thread->suspend) stop_thread( thread );
  891. }
  892. }
  893. }
  894. /* resume all the threads of a process */
  895. void resume_process( struct process *process )
  896. {
  897. assert (process->suspend > 0);
  898. if (!--process->suspend)
  899. {
  900. struct list *ptr, *next;
  901. LIST_FOR_EACH_SAFE( ptr, next, &process->thread_list )
  902. {
  903. struct thread *thread = LIST_ENTRY( ptr, struct thread, proc_entry );
  904. if (!thread->suspend) wake_thread( thread );
  905. }
  906. }
  907. }
  908. /* kill a process on the spot */
  909. void kill_process( struct process *process, int violent_death )
  910. {
  911. if (!violent_death && process->msg_fd) /* normal termination on pipe close */
  912. {
  913. release_object( process->msg_fd );
  914. process->msg_fd = NULL;
  915. }
  916. if (process->sigkill_timeout) /* already waiting for it to die */
  917. {
  918. remove_timeout_user( process->sigkill_timeout );
  919. process->sigkill_timeout = NULL;
  920. process_died( process );
  921. return;
  922. }
  923. if (violent_death) terminate_process( process, NULL, 1 );
  924. else
  925. {
  926. struct list *ptr;
  927. grab_object( process ); /* make sure it doesn't get freed when threads die */
  928. while ((ptr = list_head( &process->thread_list )))
  929. {
  930. struct thread *thread = LIST_ENTRY( ptr, struct thread, proc_entry );
  931. kill_thread( thread, 0 );
  932. }
  933. release_object( process );
  934. }
  935. }
  936. /* kill all processes being debugged by a given thread */
  937. void kill_debugged_processes( struct thread *debugger, int exit_code )
  938. {
  939. for (;;) /* restart from the beginning of the list every time */
  940. {
  941. struct process *process;
  942. /* find the first process being debugged by 'debugger' and still running */
  943. LIST_FOR_EACH_ENTRY( process, &process_list, struct process, entry )
  944. {
  945. if (!process->running_threads) continue;
  946. if (process->debugger == debugger) break;
  947. }
  948. if (&process->entry == &process_list) break; /* no process found */
  949. process->debugger = NULL;
  950. terminate_process( process, NULL, exit_code );
  951. }
  952. }
  953. /* detach a debugger from all its debuggees */
  954. void detach_debugged_processes( struct thread *debugger )
  955. {
  956. struct process *process;
  957. LIST_FOR_EACH_ENTRY( process, &process_list, struct process, entry )
  958. {
  959. if (process->debugger == debugger && process->running_threads)
  960. {
  961. debugger_detach( process, debugger );
  962. }
  963. }
  964. }
  965. void enum_processes( int (*cb)(struct process*, void*), void *user )
  966. {
  967. struct list *ptr, *next;
  968. LIST_FOR_EACH_SAFE( ptr, next, &process_list )
  969. {
  970. struct process *process = LIST_ENTRY( ptr, struct process, entry );
  971. if ((cb)(process, user)) break;
  972. }
  973. }
  974. /* set the debugged flag in the process PEB */
  975. int set_process_debug_flag( struct process *process, int flag )
  976. {
  977. char data = (flag != 0);
  978. /* BeingDebugged flag is the byte at offset 2 in the PEB */
  979. return write_process_memory( process, process->peb + 2, 1, &data );
  980. }
  981. /* take a snapshot of currently running processes */
  982. struct process_snapshot *process_snap( int *count )
  983. {
  984. struct process_snapshot *snapshot, *ptr;
  985. struct process *process;
  986. if (!running_processes) return NULL;
  987. if (!(snapshot = mem_alloc( sizeof(*snapshot) * running_processes )))
  988. return NULL;
  989. ptr = snapshot;
  990. LIST_FOR_EACH_ENTRY( process, &process_list, struct process, entry )
  991. {
  992. if (!process->running_threads) continue;
  993. ptr->process = process;
  994. ptr->threads = process->running_threads;
  995. ptr->count = process->obj.refcount;
  996. ptr->priority = process->priority;
  997. ptr->handles = get_handle_table_count(process);
  998. grab_object( process );
  999. ptr++;
  1000. }
  1001. if (!(*count = ptr - snapshot))
  1002. {
  1003. free( snapshot );
  1004. snapshot = NULL;
  1005. }
  1006. return snapshot;
  1007. }
  1008. /* replace the token of a process */
  1009. void replace_process_token( struct process *process, struct token *new_token )
  1010. {
  1011. release_object(current->process->token);
  1012. current->process->token = new_token;
  1013. grab_object(new_token);
  1014. }
  1015. int is_process( struct object *obj )
  1016. {
  1017. return obj->ops == &process_ops;
  1018. }
  1019. /* create a new process */
  1020. DECL_HANDLER(new_process)
  1021. {
  1022. struct startup_info *info;
  1023. const void *info_ptr;
  1024. struct unicode_str name;
  1025. const struct security_descriptor *sd;
  1026. const struct object_attributes *objattr = get_req_object_attributes( &sd, &name, NULL );
  1027. struct process *process = NULL;
  1028. struct token *token = NULL;
  1029. struct process *parent;
  1030. struct thread *parent_thread = current;
  1031. int socket_fd = thread_get_inflight_fd( current, req->socket_fd );
  1032. struct stat winedevice_st, exe_st;
  1033. if (socket_fd == -1)
  1034. {
  1035. set_error( STATUS_INVALID_PARAMETER );
  1036. return;
  1037. }
  1038. if (!objattr)
  1039. {
  1040. set_error( STATUS_INVALID_PARAMETER );
  1041. close( socket_fd );
  1042. return;
  1043. }
  1044. if (fcntl( socket_fd, F_SETFL, O_NONBLOCK ) == -1)
  1045. {
  1046. set_error( STATUS_INVALID_HANDLE );
  1047. close( socket_fd );
  1048. return;
  1049. }
  1050. if (shutdown_stage)
  1051. {
  1052. set_error( STATUS_SHUTDOWN_IN_PROGRESS );
  1053. close( socket_fd );
  1054. return;
  1055. }
  1056. if (!is_cpu_supported( req->cpu ))
  1057. {
  1058. close( socket_fd );
  1059. return;
  1060. }
  1061. if (req->parent_process)
  1062. {
  1063. if (!(parent = get_process_from_handle( req->parent_process, PROCESS_CREATE_PROCESS)))
  1064. {
  1065. close( socket_fd );
  1066. return;
  1067. }
  1068. parent_thread = NULL;
  1069. }
  1070. else parent = (struct process *)grab_object( current->process );
  1071. if (parent->job && (req->create_flags & CREATE_BREAKAWAY_FROM_JOB) &&
  1072. !(parent->job->limit_flags & (JOB_OBJECT_LIMIT_BREAKAWAY_OK | JOB_OBJECT_LIMIT_SILENT_BREAKAWAY_OK)))
  1073. {
  1074. set_error( STATUS_ACCESS_DENIED );
  1075. close( socket_fd );
  1076. release_object( parent );
  1077. return;
  1078. }
  1079. if (req->token)
  1080. {
  1081. token = get_token_from_handle( req->token, TOKEN_QUERY | TOKEN_DUPLICATE | TOKEN_ASSIGN_PRIMARY );
  1082. if (!token)
  1083. {
  1084. close( socket_fd );
  1085. return;
  1086. }
  1087. if (!token_is_primary( token ))
  1088. {
  1089. set_error( STATUS_BAD_TOKEN_TYPE );
  1090. release_object( token );
  1091. close( socket_fd );
  1092. return;
  1093. }
  1094. }
  1095. if (!req->info_size) /* create an orphaned process */
  1096. {
  1097. if ((process = create_process( socket_fd, NULL, 0, sd, token )))
  1098. {
  1099. create_thread( -1, process, NULL );
  1100. release_object( process );
  1101. }
  1102. if (token) release_object( token );
  1103. return;
  1104. }
  1105. /* build the startup info for a new process */
  1106. if (!(info = alloc_object( &startup_info_ops )))
  1107. {
  1108. close( socket_fd );
  1109. if (token) release_object( token );
  1110. release_object( parent );
  1111. return;
  1112. }
  1113. info->process = NULL;
  1114. info->data = NULL;
  1115. info_ptr = get_req_data_after_objattr( objattr, &info->data_size );
  1116. info->info_size = min( req->info_size, info->data_size );
  1117. if (req->info_size < sizeof(*info->data))
  1118. {
  1119. /* make sure we have a full startup_info_t structure */
  1120. data_size_t env_size = info->data_size - info->info_size;
  1121. data_size_t info_size = min( req->info_size, FIELD_OFFSET( startup_info_t, curdir_len ));
  1122. if (!(info->data = mem_alloc( sizeof(*info->data) + env_size )))
  1123. {
  1124. close( socket_fd );
  1125. goto done;
  1126. }
  1127. memcpy( info->data, info_ptr, info_size );
  1128. memset( (char *)info->data + info_size, 0, sizeof(*info->data) - info_size );
  1129. memcpy( info->data + 1, (const char *)info_ptr + req->info_size, env_size );
  1130. info->info_size = sizeof(startup_info_t);
  1131. info->data_size = info->info_size + env_size;
  1132. }
  1133. else
  1134. {
  1135. data_size_t pos = sizeof(*info->data);
  1136. if (!(info->data = memdup( info_ptr, info->data_size )))
  1137. {
  1138. close( socket_fd );
  1139. goto done;
  1140. }
  1141. #define FIXUP_LEN(len) do { (len) = min( (len), info->info_size - pos ); pos += (len); } while(0)
  1142. FIXUP_LEN( info->data->curdir_len );
  1143. FIXUP_LEN( info->data->dllpath_len );
  1144. FIXUP_LEN( info->data->imagepath_len );
  1145. FIXUP_LEN( info->data->cmdline_len );
  1146. FIXUP_LEN( info->data->title_len );
  1147. FIXUP_LEN( info->data->desktop_len );
  1148. FIXUP_LEN( info->data->shellinfo_len );
  1149. FIXUP_LEN( info->data->runtime_len );
  1150. #undef FIXUP_LEN
  1151. }
  1152. if (!(process = create_process( socket_fd, parent, req->inherit_all, sd, token ))) goto done;
  1153. process->startup_info = (struct startup_info *)grab_object( info );
  1154. if (req->exe_file &&
  1155. !(process->exe_file = get_file_obj( current->process, req->exe_file, FILE_READ_DATA )))
  1156. goto done;
  1157. if (process->exe_file && !fstatat(config_dir_fd, "drive_c/windows/system32/winedevice.exe", &winedevice_st, 0))
  1158. if (!fstat( get_file_unix_fd(process->exe_file), &exe_st) && winedevice_st.st_ino == exe_st.st_ino && winedevice_st.st_dev == exe_st.st_dev)
  1159. process->is_kernel = 1;
  1160. if (parent->job
  1161. && !(req->create_flags & CREATE_BREAKAWAY_FROM_JOB)
  1162. && !(parent->job->limit_flags & JOB_OBJECT_LIMIT_SILENT_BREAKAWAY_OK))
  1163. {
  1164. add_job_process( parent->job, process );
  1165. }
  1166. /* connect to the window station */
  1167. connect_process_winstation( process, parent_thread, parent );
  1168. /* set the process console */
  1169. if (!(req->create_flags & (DETACHED_PROCESS | CREATE_NEW_CONSOLE)))
  1170. {
  1171. /* FIXME: some better error checking should be done...
  1172. * like if hConOut and hConIn are console handles, then they should be on the same
  1173. * physical console
  1174. */
  1175. inherit_console( parent_thread, parent, process, req->inherit_all ? info->data->hstdin : 0 );
  1176. }
  1177. if (!req->inherit_all && !(req->create_flags & CREATE_NEW_CONSOLE))
  1178. {
  1179. info->data->hstdin = duplicate_handle( parent, info->data->hstdin, process,
  1180. 0, OBJ_INHERIT, DUPLICATE_SAME_ACCESS );
  1181. info->data->hstdout = duplicate_handle( parent, info->data->hstdout, process,
  1182. 0, OBJ_INHERIT, DUPLICATE_SAME_ACCESS );
  1183. info->data->hstderr = duplicate_handle( parent, info->data->hstderr, process,
  1184. 0, OBJ_INHERIT, DUPLICATE_SAME_ACCESS );
  1185. /* some handles above may have been invalid; this is not an error */
  1186. if (get_error() == STATUS_INVALID_HANDLE ||
  1187. get_error() == STATUS_OBJECT_TYPE_MISMATCH) clear_error();
  1188. }
  1189. /* attach to the debugger if requested */
  1190. if (req->create_flags & (DEBUG_PROCESS | DEBUG_ONLY_THIS_PROCESS))
  1191. {
  1192. set_process_debugger( process, current );
  1193. process->debug_children = !(req->create_flags & DEBUG_ONLY_THIS_PROCESS);
  1194. }
  1195. else if (current->process->debugger && current->process->debug_children)
  1196. {
  1197. set_process_debugger( process, current->process->debugger );
  1198. /* debug_children is set to 1 by default */
  1199. }
  1200. if (!(req->create_flags & CREATE_NEW_PROCESS_GROUP))
  1201. process->group_id = parent->group_id;
  1202. info->process = (struct process *)grab_object( process );
  1203. reply->info = alloc_handle( current->process, info, SYNCHRONIZE, 0 );
  1204. reply->pid = get_process_id( process );
  1205. reply->handle = alloc_handle_no_access_check( current->process, process, req->access, objattr->attributes );
  1206. done:
  1207. if (token) release_object( token );
  1208. if (process) release_object( process );
  1209. release_object( parent );
  1210. release_object( info );
  1211. }
  1212. /* execute a new process, replacing the existing one */
  1213. DECL_HANDLER(exec_process)
  1214. {
  1215. struct process *process;
  1216. int socket_fd = thread_get_inflight_fd( current, req->socket_fd );
  1217. if (socket_fd == -1)
  1218. {
  1219. set_error( STATUS_INVALID_PARAMETER );
  1220. return;
  1221. }
  1222. if (fcntl( socket_fd, F_SETFL, O_NONBLOCK ) == -1)
  1223. {
  1224. set_error( STATUS_INVALID_HANDLE );
  1225. close( socket_fd );
  1226. return;
  1227. }
  1228. if (shutdown_stage)
  1229. {
  1230. set_error( STATUS_SHUTDOWN_IN_PROGRESS );
  1231. close( socket_fd );
  1232. return;
  1233. }
  1234. if (!is_cpu_supported( req->cpu ))
  1235. {
  1236. close( socket_fd );
  1237. return;
  1238. }
  1239. if (!(process = create_process( socket_fd, NULL, 0, NULL, NULL ))) return;
  1240. create_thread( -1, process, NULL );
  1241. release_object( process );
  1242. }
  1243. /* Retrieve information about a newly started process */
  1244. DECL_HANDLER(get_new_process_info)
  1245. {
  1246. struct startup_info *info;
  1247. if ((info = (struct startup_info *)get_handle_obj( current->process, req->info,
  1248. 0, &startup_info_ops )))
  1249. {
  1250. reply->success = is_process_init_done( info->process );
  1251. reply->exit_code = info->process->exit_code;
  1252. release_object( info );
  1253. }
  1254. }
  1255. DECL_HANDLER(wait_proc_init)
  1256. {
  1257. struct process *process;
  1258. if ((process = get_process_from_handle(req->process, 0)))
  1259. {
  1260. reply->process_state = is_process_init_done( process ) ? PROCESS_RUNNING : PROCESS_STARTING;
  1261. if (process->startup_state == STARTUP_IN_PROGRESS)
  1262. {
  1263. reply->init_event = alloc_handle(current->process, process->startup_info, SYNCHRONIZE, 0);
  1264. }
  1265. release_object(process);
  1266. }
  1267. }
  1268. /* Retrieve the new process startup info */
  1269. DECL_HANDLER(get_startup_info)
  1270. {
  1271. struct process *process = current->process;
  1272. struct startup_info *info = process->startup_info;
  1273. data_size_t size;
  1274. if (!info) return;
  1275. /* we return the data directly without making a copy so this can only be called once */
  1276. reply->info_size = info->info_size;
  1277. size = info->data_size;
  1278. if (size > get_reply_max_size()) size = get_reply_max_size();
  1279. set_reply_data_ptr( info->data, size );
  1280. info->data = NULL;
  1281. info->data_size = 0;
  1282. }
  1283. /* signal the end of the process initialization */
  1284. DECL_HANDLER(init_process_done)
  1285. {
  1286. struct process_dll *dll;
  1287. struct process *process = current->process;
  1288. if (is_process_init_done(process))
  1289. {
  1290. set_error( STATUS_INVALID_PARAMETER );
  1291. return;
  1292. }
  1293. if (!(dll = find_process_dll( process, req->module )))
  1294. {
  1295. set_error( STATUS_DLL_NOT_FOUND );
  1296. return;
  1297. }
  1298. /* main exe is the first in the dll list */
  1299. list_remove( &dll->entry );
  1300. list_add_head( &process->dlls, &dll->entry );
  1301. process->ldt_copy = req->ldt_copy;
  1302. process->start_time = current_time;
  1303. current->entry_point = req->entry;
  1304. if (process->exe_file) release_object( process->exe_file );
  1305. process->exe_file = NULL;
  1306. init_process_tracing( process );
  1307. generate_startup_debug_events( process, req->entry );
  1308. set_process_startup_state( process, STARTUP_DONE );
  1309. if (req->gui) process->idle_event = create_event( NULL, NULL, 0, 1, 0, NULL );
  1310. if (process->debugger) set_process_debug_flag( process, 1 );
  1311. reply->suspend = (current->suspend || process->suspend);
  1312. if (process->callback_init_event)
  1313. {
  1314. reply->processed_event = alloc_handle(process, process->callback_init_event, SYNCHRONIZE, 0);
  1315. release_object(process->callback_init_event);
  1316. process->callback_init_event = NULL;
  1317. }
  1318. }
  1319. /* open a handle to a process */
  1320. DECL_HANDLER(open_process)
  1321. {
  1322. struct process *process = get_process_from_id( req->pid );
  1323. reply->handle = 0;
  1324. if (process)
  1325. {
  1326. reply->handle = alloc_handle( current->process, process, req->access, req->attributes );
  1327. release_object( process );
  1328. }
  1329. }
  1330. /* terminate a process */
  1331. DECL_HANDLER(terminate_process)
  1332. {
  1333. struct process *process;
  1334. if (req->handle)
  1335. {
  1336. process = get_process_from_handle( req->handle, PROCESS_TERMINATE );
  1337. if (!process) return;
  1338. }
  1339. else process = (struct process *)grab_object( current->process );
  1340. reply->self = (current->process == process);
  1341. terminate_process( process, current, req->exit_code );
  1342. release_object( process );
  1343. }
  1344. /* fetch information about a process */
  1345. DECL_HANDLER(get_process_info)
  1346. {
  1347. struct process *process;
  1348. if ((process = get_process_from_handle( req->handle, PROCESS_QUERY_LIMITED_INFORMATION )))
  1349. {
  1350. reply->pid = get_process_id( process );
  1351. reply->ppid = process->parent_id;
  1352. reply->exit_code = process->exit_code;
  1353. reply->priority = process->priority;
  1354. reply->affinity = process->affinity;
  1355. reply->peb = process->peb;
  1356. reply->start_time = process->start_time;
  1357. reply->end_time = process->end_time;
  1358. reply->cpu = process->cpu;
  1359. reply->debugger_present = !!process->debugger;
  1360. reply->debug_children = process->debug_children;
  1361. release_object( process );
  1362. }
  1363. }
  1364. /* retrieve information about a process memory usage */
  1365. DECL_HANDLER(get_process_vm_counters)
  1366. {
  1367. struct process *process = get_process_from_handle( req->handle, PROCESS_QUERY_LIMITED_INFORMATION );
  1368. if (!process) return;
  1369. #ifdef linux
  1370. if (process->unix_pid != -1)
  1371. {
  1372. FILE *f;
  1373. char proc_path[32], line[256];
  1374. unsigned long value;
  1375. sprintf( proc_path, "/proc/%u/status", process->unix_pid );
  1376. if ((f = fopen( proc_path, "r" )))
  1377. {
  1378. while (fgets( line, sizeof(line), f ))
  1379. {
  1380. if (sscanf( line, "VmPeak: %lu", &value ))
  1381. reply->peak_virtual_size = (mem_size_t)value * 1024;
  1382. else if (sscanf( line, "VmSize: %lu", &value ))
  1383. reply->virtual_size = (mem_size_t)value * 1024;
  1384. else if (sscanf( line, "VmHWM: %lu", &value ))
  1385. reply->peak_working_set_size = (mem_size_t)value * 1024;
  1386. else if (sscanf( line, "VmRSS: %lu", &value ))
  1387. reply->working_set_size = (mem_size_t)value * 1024;
  1388. else if (sscanf( line, "RssAnon: %lu", &value ))
  1389. reply->pagefile_usage += (mem_size_t)value * 1024;
  1390. else if (sscanf( line, "VmSwap: %lu", &value ))
  1391. reply->pagefile_usage += (mem_size_t)value * 1024;
  1392. }
  1393. reply->peak_pagefile_usage = reply->pagefile_usage;
  1394. fclose( f );
  1395. }
  1396. else set_error( STATUS_ACCESS_DENIED );
  1397. }
  1398. else set_error( STATUS_ACCESS_DENIED );
  1399. #endif
  1400. release_object( process );
  1401. }
  1402. static void set_process_affinity( struct process *process, affinity_t affinity )
  1403. {
  1404. struct thread *thread;
  1405. if (!process->running_threads)
  1406. {
  1407. set_error( STATUS_PROCESS_IS_TERMINATING );
  1408. return;
  1409. }
  1410. process->affinity = affinity;
  1411. LIST_FOR_EACH_ENTRY( thread, &process->thread_list, struct thread, proc_entry )
  1412. {
  1413. set_thread_affinity( thread, affinity );
  1414. }
  1415. }
  1416. /* set information about a process */
  1417. DECL_HANDLER(set_process_info)
  1418. {
  1419. struct process *process;
  1420. if ((process = get_process_from_handle( req->handle, PROCESS_SET_INFORMATION )))
  1421. {
  1422. if (req->mask & SET_PROCESS_INFO_PRIORITY) process->priority = req->priority;
  1423. if (req->mask & SET_PROCESS_INFO_AFFINITY) set_process_affinity( process, req->affinity );
  1424. release_object( process );
  1425. }
  1426. }
  1427. /* read data from a process address space */
  1428. DECL_HANDLER(read_process_memory)
  1429. {
  1430. struct process *process;
  1431. data_size_t len = get_reply_max_size();
  1432. if (!(process = get_process_from_handle( req->handle, PROCESS_VM_READ ))) return;
  1433. if (len)
  1434. {
  1435. char *buffer = mem_alloc( len );
  1436. if (buffer)
  1437. {
  1438. if (read_process_memory( process, req->addr, len, buffer ))
  1439. set_reply_data_ptr( buffer, len );
  1440. else
  1441. free( buffer );
  1442. }
  1443. }
  1444. release_object( process );
  1445. }
  1446. /* write data to a process address space */
  1447. DECL_HANDLER(write_process_memory)
  1448. {
  1449. struct process *process;
  1450. if ((process = get_process_from_handle( req->handle, PROCESS_VM_WRITE )))
  1451. {
  1452. data_size_t len = get_req_data_size();
  1453. if (len) write_process_memory( process, req->addr, len, get_req_data() );
  1454. else set_error( STATUS_INVALID_PARAMETER );
  1455. release_object( process );
  1456. }
  1457. }
  1458. /* notify the server that a dll has been loaded */
  1459. DECL_HANDLER(load_dll)
  1460. {
  1461. struct process_dll *dll;
  1462. struct object *done_event = NULL;
  1463. if ((dll = process_load_dll( current->process, req->base, get_req_data(), get_req_data_size(), &done_event )))
  1464. {
  1465. dll->dbg_offset = req->dbg_offset;
  1466. dll->dbg_size = req->dbg_size;
  1467. dll->name = req->name;
  1468. /* only generate event if initialization is done */
  1469. if (is_process_init_done( current->process ))
  1470. {
  1471. generate_debug_event( current, LOAD_DLL_DEBUG_EVENT, dll );
  1472. if (done_event)
  1473. {
  1474. reply->processed_event = alloc_handle(current->process, done_event, SYNCHRONIZE, 0);
  1475. release_object(done_event);
  1476. }
  1477. }
  1478. else if (done_event)
  1479. {
  1480. if (current->process->callback_init_event)
  1481. release_object(current->process->callback_init_event);
  1482. current->process->callback_init_event = done_event;
  1483. }
  1484. }
  1485. }
  1486. /* notify the server that a dll is being unloaded */
  1487. DECL_HANDLER(unload_dll)
  1488. {
  1489. process_unload_dll( current->process, req->base );
  1490. }
  1491. /* retrieve information about a module in a process */
  1492. DECL_HANDLER(get_dll_info)
  1493. {
  1494. struct process *process;
  1495. if ((process = get_process_from_handle( req->handle, PROCESS_QUERY_LIMITED_INFORMATION )))
  1496. {
  1497. struct process_dll *dll;
  1498. if (req->base_address)
  1499. dll = find_process_dll( process, req->base_address );
  1500. else /* NULL means main module */
  1501. dll = list_head( &process->dlls ) ?
  1502. LIST_ENTRY(list_head( &process->dlls ), struct process_dll, entry) : NULL;
  1503. if (dll)
  1504. {
  1505. reply->entry_point = 0; /* FIXME */
  1506. reply->base_address = dll->base;
  1507. reply->filename_len = dll->namelen;
  1508. if (dll->filename)
  1509. {
  1510. if (dll->namelen <= get_reply_max_size())
  1511. set_reply_data( dll->filename, dll->namelen );
  1512. else
  1513. set_error( STATUS_BUFFER_TOO_SMALL );
  1514. }
  1515. }
  1516. else
  1517. set_error( STATUS_DLL_NOT_FOUND );
  1518. release_object( process );
  1519. }
  1520. }
  1521. /* retrieve the process idle event */
  1522. DECL_HANDLER(get_process_idle_event)
  1523. {
  1524. struct process *process;
  1525. reply->event = 0;
  1526. if ((process = get_process_from_handle( req->handle, PROCESS_QUERY_INFORMATION )))
  1527. {
  1528. if (process->idle_event && process != current->process)
  1529. reply->event = alloc_handle( current->process, process->idle_event,
  1530. EVENT_ALL_ACCESS, 0 );
  1531. release_object( process );
  1532. }
  1533. }
  1534. /* make the current process a system process */
  1535. DECL_HANDLER(make_process_system)
  1536. {
  1537. struct process *process = current->process;
  1538. if (!shutdown_event)
  1539. {
  1540. if (!(shutdown_event = create_event( NULL, NULL, 0, 1, 0, NULL ))) return;
  1541. make_object_static( (struct object *)shutdown_event );
  1542. }
  1543. if (!(reply->event = alloc_handle( current->process, shutdown_event, SYNCHRONIZE, 0 )))
  1544. return;
  1545. if (!process->is_system)
  1546. {
  1547. process->is_system = 1;
  1548. close_process_desktop( process );
  1549. if (!--user_processes && !shutdown_stage && master_socket_timeout != TIMEOUT_INFINITE)
  1550. shutdown_timeout = add_timeout_user( master_socket_timeout, server_shutdown_timeout, NULL );
  1551. }
  1552. }
  1553. /* create a new job object */
  1554. DECL_HANDLER(create_job)
  1555. {
  1556. struct job *job;
  1557. struct unicode_str name;
  1558. struct object *root;
  1559. const struct security_descriptor *sd;
  1560. const struct object_attributes *objattr = get_req_object_attributes( &sd, &name, &root );
  1561. if (!objattr) return;
  1562. if ((job = create_job_object( root, &name, objattr->attributes, sd )))
  1563. {
  1564. if (get_error() == STATUS_OBJECT_NAME_EXISTS)
  1565. reply->handle = alloc_handle( current->process, job, req->access, objattr->attributes );
  1566. else
  1567. reply->handle = alloc_handle_no_access_check( current->process, job,
  1568. req->access, objattr->attributes );
  1569. release_object( job );
  1570. }
  1571. if (root) release_object( root );
  1572. }
  1573. /* open a job object */
  1574. DECL_HANDLER(open_job)
  1575. {
  1576. struct unicode_str name = get_req_unicode_str();
  1577. reply->handle = open_object( current->process, req->rootdir, req->access,
  1578. &job_ops, &name, req->attributes );
  1579. }
  1580. /* assign a job object to a process */
  1581. DECL_HANDLER(assign_job)
  1582. {
  1583. struct process *process;
  1584. struct job *job = get_job_obj( current->process, req->job, JOB_OBJECT_ASSIGN_PROCESS );
  1585. if (!job) return;
  1586. if ((process = get_process_from_handle( req->process, PROCESS_SET_QUOTA | PROCESS_TERMINATE )))
  1587. {
  1588. if (!process->running_threads)
  1589. set_error( STATUS_PROCESS_IS_TERMINATING );
  1590. else if (process->job)
  1591. set_error( STATUS_ACCESS_DENIED );
  1592. else
  1593. add_job_process( job, process );
  1594. release_object( process );
  1595. }
  1596. release_object( job );
  1597. }
  1598. /* check if a process is associated with a job */
  1599. DECL_HANDLER(process_in_job)
  1600. {
  1601. struct process *process;
  1602. struct job *job;
  1603. if (!(process = get_process_from_handle( req->process, PROCESS_QUERY_INFORMATION )))
  1604. return;
  1605. if (!req->job)
  1606. {
  1607. set_error( process->job ? STATUS_PROCESS_IN_JOB : STATUS_PROCESS_NOT_IN_JOB );
  1608. }
  1609. else if ((job = get_job_obj( current->process, req->job, JOB_OBJECT_QUERY )))
  1610. {
  1611. set_error( process->job == job ? STATUS_PROCESS_IN_JOB : STATUS_PROCESS_NOT_IN_JOB );
  1612. release_object( job );
  1613. }
  1614. release_object( process );
  1615. }
  1616. /* terminate all processes associated with the job */
  1617. DECL_HANDLER(terminate_job)
  1618. {
  1619. struct job *job = get_job_obj( current->process, req->handle, JOB_OBJECT_TERMINATE );
  1620. if (!job) return;
  1621. terminate_job( job, req->status );
  1622. release_object( job );
  1623. }
  1624. /* update limits of the job object */
  1625. DECL_HANDLER(set_job_limits)
  1626. {
  1627. struct job *job = get_job_obj( current->process, req->handle, JOB_OBJECT_SET_ATTRIBUTES );
  1628. if (!job) return;
  1629. job->limit_flags = req->limit_flags;
  1630. release_object( job );
  1631. }
  1632. /* set the jobs completion port */
  1633. DECL_HANDLER(set_job_completion_port)
  1634. {
  1635. struct job *job = get_job_obj( current->process, req->job, JOB_OBJECT_SET_ATTRIBUTES );
  1636. if (!job) return;
  1637. if (!job->completion_port)
  1638. {
  1639. job->completion_port = get_completion_obj( current->process, req->port, IO_COMPLETION_MODIFY_STATE );
  1640. job->completion_key = req->key;
  1641. }
  1642. else
  1643. set_error( STATUS_INVALID_PARAMETER );
  1644. release_object( job );
  1645. }
  1646. /* Suspend a process */
  1647. DECL_HANDLER(suspend_process)
  1648. {
  1649. struct process *process;
  1650. if ((process = get_process_from_handle( req->handle, PROCESS_SUSPEND_RESUME )))
  1651. {
  1652. struct list *ptr, *next;
  1653. LIST_FOR_EACH_SAFE( ptr, next, &process->thread_list )
  1654. {
  1655. struct thread *thread = LIST_ENTRY( ptr, struct thread, proc_entry );
  1656. suspend_thread( thread );
  1657. }
  1658. release_object( process );
  1659. }
  1660. }
  1661. /* Resume a process */
  1662. DECL_HANDLER(resume_process)
  1663. {
  1664. struct process *process;
  1665. if ((process = get_process_from_handle( req->handle, PROCESS_SUSPEND_RESUME )))
  1666. {
  1667. struct list *ptr, *next;
  1668. LIST_FOR_EACH_SAFE( ptr, next, &process->thread_list )
  1669. {
  1670. struct thread *thread = LIST_ENTRY( ptr, struct thread, proc_entry );
  1671. resume_thread( thread );
  1672. }
  1673. release_object( process );
  1674. }
  1675. }
  1676. /* Retrieve process, thread and handle count */
  1677. DECL_HANDLER(get_system_info)
  1678. {
  1679. struct process *process;
  1680. reply->processes = 0;
  1681. reply->threads = 0;
  1682. reply->handles = 0;
  1683. LIST_FOR_EACH_ENTRY( process, &process_list, struct process, entry )
  1684. {
  1685. if (!process->running_threads) continue;
  1686. reply->processes++;
  1687. reply->threads += process->running_threads;
  1688. reply->handles += get_handle_table_count( process );
  1689. }
  1690. }