process.c 64 KB

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