process.c 65 KB

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