process.c 65 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992
  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->unix_pid = -1;
  598. process->exit_code = STILL_ACTIVE;
  599. process->running_threads = 0;
  600. process->priority = PROCESS_PRIOCLASS_NORMAL;
  601. process->suspend = 0;
  602. process->is_system = 0;
  603. process->debug_children = 1;
  604. process->is_terminating = 0;
  605. process->imagelen = 0;
  606. process->image = NULL;
  607. process->job = NULL;
  608. process->console = NULL;
  609. process->startup_state = STARTUP_IN_PROGRESS;
  610. process->startup_info = NULL;
  611. process->idle_event = NULL;
  612. process->peb = 0;
  613. process->ldt_copy = 0;
  614. process->dir_cache = NULL;
  615. process->winstation = 0;
  616. process->desktop = 0;
  617. process->token = NULL;
  618. process->trace_data = 0;
  619. process->rawinput_devices = NULL;
  620. process->rawinput_device_count = 0;
  621. process->rawinput_mouse = NULL;
  622. process->rawinput_kbd = NULL;
  623. process->fast_sync = NULL;
  624. memset( &process->image_info, 0, sizeof(process->image_info) );
  625. list_init( &process->kernel_object );
  626. list_init( &process->thread_list );
  627. list_init( &process->locks );
  628. list_init( &process->asyncs );
  629. list_init( &process->classes );
  630. list_init( &process->views );
  631. process->end_time = 0;
  632. if (sd && !default_set_sd( &process->obj, sd, OWNER_SECURITY_INFORMATION | GROUP_SECURITY_INFORMATION |
  633. DACL_SECURITY_INFORMATION | SACL_SECURITY_INFORMATION ))
  634. {
  635. close( fd );
  636. goto error;
  637. }
  638. if (!(process->id = process->group_id = alloc_ptid( process )))
  639. {
  640. close( fd );
  641. goto error;
  642. }
  643. if (!(process->msg_fd = create_anonymous_fd( &process_fd_ops, fd, &process->obj, 0 ))) goto error;
  644. /* create the handle table */
  645. if (!parent)
  646. {
  647. process->handles = alloc_handle_table( process, 0 );
  648. process->token = token_create_admin( TRUE, -1, TokenElevationTypeFull, default_session_id );
  649. process->affinity = ~0;
  650. }
  651. else
  652. {
  653. obj_handle_t std_handles[3];
  654. std_handles[0] = info->hstdin;
  655. std_handles[1] = info->hstdout;
  656. std_handles[2] = info->hstderr;
  657. process->parent_id = parent->id;
  658. if (flags & PROCESS_CREATE_FLAGS_INHERIT_HANDLES)
  659. process->handles = copy_handle_table( process, parent, handles, handle_count, std_handles );
  660. else
  661. process->handles = alloc_handle_table( process, 0 );
  662. /* Note: for security reasons, starting a new process does not attempt
  663. * to use the current impersonation token for the new process */
  664. process->token = token_duplicate( token ? token : parent->token, TRUE, 0, NULL, NULL, 0, NULL, 0 );
  665. process->affinity = parent->affinity;
  666. }
  667. if (!process->handles || !process->token) goto error;
  668. process->session_id = token_get_session_id( process->token );
  669. /* Assign a high security label to the token. The default would be medium
  670. * but Wine provides admin access to all applications right now so high
  671. * makes more sense for the time being. */
  672. if (!token_assign_label( process->token, &high_label_sid ))
  673. goto error;
  674. set_fd_events( process->msg_fd, POLLIN ); /* start listening to events */
  675. return process;
  676. error:
  677. if (process) release_object( process );
  678. /* if we failed to start our first process, close everything down */
  679. if (!running_processes && master_socket_timeout != TIMEOUT_INFINITE) close_master_socket( 0 );
  680. return NULL;
  681. }
  682. /* get the process data size */
  683. data_size_t get_process_startup_info_size( struct process *process )
  684. {
  685. struct startup_info *info = process->startup_info;
  686. if (!info) return 0;
  687. return info->data_size;
  688. }
  689. /* destroy a process when its refcount is 0 */
  690. static void process_destroy( struct object *obj )
  691. {
  692. struct process *process = (struct process *)obj;
  693. assert( obj->ops == &process_ops );
  694. /* we can't have a thread remaining */
  695. assert( list_empty( &process->thread_list ));
  696. assert( list_empty( &process->asyncs ));
  697. assert( !process->sigkill_timeout ); /* timeout should hold a reference to the process */
  698. close_process_handles( process );
  699. set_process_startup_state( process, STARTUP_ABORTED );
  700. if (process->job)
  701. {
  702. list_remove( &process->job_entry );
  703. release_object( process->job );
  704. }
  705. if (process->console) release_object( process->console );
  706. if (process->msg_fd) release_object( process->msg_fd );
  707. if (process->idle_event) release_object( process->idle_event );
  708. if (process->id) free_ptid( process->id );
  709. if (process->token) release_object( process->token );
  710. free( process->rawinput_devices );
  711. free( process->dir_cache );
  712. free( process->image );
  713. if (process->fast_sync) release_object( process->fast_sync );
  714. }
  715. /* dump a process on stdout for debugging purposes */
  716. static void process_dump( struct object *obj, int verbose )
  717. {
  718. struct process *process = (struct process *)obj;
  719. assert( obj->ops == &process_ops );
  720. fprintf( stderr, "Process id=%04x handles=%p\n", process->id, process->handles );
  721. }
  722. static int process_signaled( struct object *obj, struct wait_queue_entry *entry )
  723. {
  724. struct process *process = (struct process *)obj;
  725. return !process->running_threads;
  726. }
  727. static unsigned int process_map_access( struct object *obj, unsigned int access )
  728. {
  729. access = default_map_access( obj, access );
  730. if (access & PROCESS_QUERY_INFORMATION) access |= PROCESS_QUERY_LIMITED_INFORMATION;
  731. if (access & PROCESS_SET_INFORMATION) access |= PROCESS_SET_LIMITED_INFORMATION;
  732. return access;
  733. }
  734. static struct list *process_get_kernel_obj_list( struct object *obj )
  735. {
  736. struct process *process = (struct process *)obj;
  737. return &process->kernel_object;
  738. }
  739. static struct fast_sync *process_get_fast_sync( struct object *obj )
  740. {
  741. struct process *process = (struct process *)obj;
  742. if (!process->fast_sync)
  743. process->fast_sync = fast_create_event( FAST_SYNC_MANUAL_SERVER, !process->running_threads );
  744. if (process->fast_sync) grab_object( process->fast_sync );
  745. return process->fast_sync;
  746. }
  747. static struct security_descriptor *process_get_sd( struct object *obj )
  748. {
  749. static struct security_descriptor *process_default_sd;
  750. if (obj->sd) return obj->sd;
  751. if (!process_default_sd)
  752. {
  753. struct ace *ace;
  754. struct acl *dacl;
  755. struct sid *sid;
  756. size_t users_sid_len = sid_len( &domain_users_sid );
  757. size_t admins_sid_len = sid_len( &builtin_admins_sid );
  758. size_t dacl_len = sizeof(*dacl) + 2 * sizeof(*ace) + users_sid_len + admins_sid_len;
  759. process_default_sd = mem_alloc( sizeof(*process_default_sd) + admins_sid_len + users_sid_len
  760. + dacl_len );
  761. process_default_sd->control = SE_DACL_PRESENT;
  762. process_default_sd->owner_len = admins_sid_len;
  763. process_default_sd->group_len = users_sid_len;
  764. process_default_sd->sacl_len = 0;
  765. process_default_sd->dacl_len = dacl_len;
  766. sid = (struct sid *)(process_default_sd + 1);
  767. sid = copy_sid( sid, &builtin_admins_sid );
  768. sid = copy_sid( sid, &domain_users_sid );
  769. dacl = (struct acl *)((char *)(process_default_sd + 1) + admins_sid_len + users_sid_len);
  770. dacl->revision = ACL_REVISION;
  771. dacl->pad1 = 0;
  772. dacl->size = dacl_len;
  773. dacl->count = 2;
  774. dacl->pad2 = 0;
  775. ace = set_ace( ace_first( dacl ), &domain_users_sid, ACCESS_ALLOWED_ACE_TYPE,
  776. INHERIT_ONLY_ACE | CONTAINER_INHERIT_ACE, GENERIC_READ );
  777. set_ace( ace_next( ace ), &builtin_admins_sid, ACCESS_ALLOWED_ACE_TYPE, 0, PROCESS_ALL_ACCESS );
  778. }
  779. return process_default_sd;
  780. }
  781. static void process_poll_event( struct fd *fd, int event )
  782. {
  783. struct process *process = get_fd_user( fd );
  784. assert( process->obj.ops == &process_ops );
  785. if (event & (POLLERR | POLLHUP)) kill_process( process, 0 );
  786. else if (event & POLLIN) receive_fd( process );
  787. }
  788. static void startup_info_destroy( struct object *obj )
  789. {
  790. struct startup_info *info = (struct startup_info *)obj;
  791. assert( obj->ops == &startup_info_ops );
  792. free( info->data );
  793. if (info->process) release_object( info->process );
  794. }
  795. static void startup_info_dump( struct object *obj, int verbose )
  796. {
  797. struct startup_info *info = (struct startup_info *)obj;
  798. assert( obj->ops == &startup_info_ops );
  799. fputs( "Startup info", stderr );
  800. if (info->data)
  801. fprintf( stderr, " in=%04x out=%04x err=%04x",
  802. info->data->hstdin, info->data->hstdout, info->data->hstderr );
  803. fputc( '\n', stderr );
  804. }
  805. static int startup_info_signaled( struct object *obj, struct wait_queue_entry *entry )
  806. {
  807. struct startup_info *info = (struct startup_info *)obj;
  808. return info->process && info->process->startup_state != STARTUP_IN_PROGRESS;
  809. }
  810. /* get a process from an id (and increment the refcount) */
  811. struct process *get_process_from_id( process_id_t id )
  812. {
  813. struct object *obj = get_ptid_entry( id );
  814. if (obj && obj->ops == &process_ops) return (struct process *)grab_object( obj );
  815. set_error( STATUS_INVALID_CID );
  816. return NULL;
  817. }
  818. /* get a process from a handle (and increment the refcount) */
  819. struct process *get_process_from_handle( obj_handle_t handle, unsigned int access )
  820. {
  821. return (struct process *)get_handle_obj( current->process, handle,
  822. access, &process_ops );
  823. }
  824. /* terminate a process with the given exit code */
  825. static void terminate_process( struct process *process, struct thread *skip, int exit_code )
  826. {
  827. struct thread *thread;
  828. grab_object( process ); /* make sure it doesn't get freed when threads die */
  829. process->is_terminating = 1;
  830. restart:
  831. LIST_FOR_EACH_ENTRY( thread, &process->thread_list, struct thread, proc_entry )
  832. {
  833. if (exit_code) thread->exit_code = exit_code;
  834. if (thread == skip) continue;
  835. if (thread->state == TERMINATED) continue;
  836. kill_thread( thread, 1 );
  837. goto restart;
  838. }
  839. release_object( process );
  840. }
  841. /* kill all processes */
  842. static void kill_all_processes(void)
  843. {
  844. struct list *ptr;
  845. while ((ptr = list_head( &process_list )))
  846. {
  847. struct process *process = LIST_ENTRY( ptr, struct process, entry );
  848. terminate_process( process, NULL, 1 );
  849. }
  850. }
  851. /* kill all processes being attached to a console renderer */
  852. void kill_console_processes( struct thread *renderer, int exit_code )
  853. {
  854. for (;;) /* restart from the beginning of the list every time */
  855. {
  856. struct process *process;
  857. /* find the first process being attached to 'renderer' and still running */
  858. LIST_FOR_EACH_ENTRY( process, &process_list, struct process, entry )
  859. {
  860. if (process == renderer->process) continue;
  861. if (process->console && console_get_renderer( process->console ) == renderer) break;
  862. }
  863. if (&process->entry == &process_list) break; /* no process found */
  864. terminate_process( process, NULL, exit_code );
  865. }
  866. }
  867. /* a process has been killed (i.e. its last thread died) */
  868. static void process_killed( struct process *process )
  869. {
  870. assert( list_empty( &process->thread_list ));
  871. process->end_time = current_time;
  872. close_process_desktop( process );
  873. process->winstation = 0;
  874. process->desktop = 0;
  875. cancel_process_asyncs( process );
  876. close_process_handles( process );
  877. if (process->idle_event) release_object( process->idle_event );
  878. process->idle_event = NULL;
  879. assert( !process->console );
  880. destroy_process_classes( process );
  881. free_mapped_views( process );
  882. free_process_user_handles( process );
  883. remove_process_locks( process );
  884. set_process_startup_state( process, STARTUP_ABORTED );
  885. finish_process_tracing( process );
  886. release_job_process( process );
  887. start_sigkill_timer( process );
  888. wake_up( &process->obj, 0 );
  889. fast_set_event( process->fast_sync );
  890. }
  891. /* add a thread to a process running threads list */
  892. void add_process_thread( struct process *process, struct thread *thread )
  893. {
  894. list_add_tail( &process->thread_list, &thread->proc_entry );
  895. if (!process->running_threads++)
  896. {
  897. list_add_tail( &process_list, &process->entry );
  898. running_processes++;
  899. if (!process->is_system)
  900. {
  901. if (!user_processes++ && shutdown_timeout)
  902. {
  903. remove_timeout_user( shutdown_timeout );
  904. shutdown_timeout = NULL;
  905. }
  906. }
  907. }
  908. grab_object( thread );
  909. }
  910. /* remove a thread from a process running threads list */
  911. void remove_process_thread( struct process *process, struct thread *thread )
  912. {
  913. assert( process->running_threads > 0 );
  914. assert( !list_empty( &process->thread_list ));
  915. list_remove( &thread->proc_entry );
  916. if (!--process->running_threads)
  917. {
  918. /* we have removed the last running thread, exit the process */
  919. process->exit_code = thread->exit_code;
  920. generate_debug_event( thread, DbgExitProcessStateChange, process );
  921. list_remove( &process->entry );
  922. process_killed( process );
  923. }
  924. else generate_debug_event( thread, DbgExitThreadStateChange, thread );
  925. release_object( thread );
  926. }
  927. /* suspend all the threads of a process */
  928. void suspend_process( struct process *process )
  929. {
  930. if (!process->suspend++)
  931. {
  932. struct list *ptr, *next;
  933. LIST_FOR_EACH_SAFE( ptr, next, &process->thread_list )
  934. {
  935. struct thread *thread = LIST_ENTRY( ptr, struct thread, proc_entry );
  936. if (!thread->suspend) stop_thread( thread );
  937. }
  938. }
  939. }
  940. /* resume all the threads of a process */
  941. void resume_process( struct process *process )
  942. {
  943. assert (process->suspend > 0);
  944. if (!--process->suspend)
  945. {
  946. struct list *ptr, *next;
  947. LIST_FOR_EACH_SAFE( ptr, next, &process->thread_list )
  948. {
  949. struct thread *thread = LIST_ENTRY( ptr, struct thread, proc_entry );
  950. if (!thread->suspend) wake_thread( thread );
  951. }
  952. }
  953. }
  954. /* kill a process on the spot */
  955. void kill_process( struct process *process, int violent_death )
  956. {
  957. if (!violent_death && process->msg_fd) /* normal termination on pipe close */
  958. {
  959. release_object( process->msg_fd );
  960. process->msg_fd = NULL;
  961. }
  962. if (process->sigkill_timeout) return; /* already waiting for it to die */
  963. if (violent_death) terminate_process( process, NULL, 1 );
  964. else
  965. {
  966. struct list *ptr;
  967. grab_object( process ); /* make sure it doesn't get freed when threads die */
  968. while ((ptr = list_head( &process->thread_list )))
  969. {
  970. struct thread *thread = LIST_ENTRY( ptr, struct thread, proc_entry );
  971. kill_thread( thread, 0 );
  972. }
  973. release_object( process );
  974. }
  975. }
  976. /* detach all processes being debugged by a given thread */
  977. void detach_debugged_processes( struct debug_obj *debug_obj, int exit_code )
  978. {
  979. for (;;) /* restart from the beginning of the list every time */
  980. {
  981. struct process *process;
  982. /* find the first process being debugged by 'debugger' and still running */
  983. LIST_FOR_EACH_ENTRY( process, &process_list, struct process, entry )
  984. if (process->debug_obj == debug_obj) break;
  985. if (&process->entry == &process_list) break; /* no process found */
  986. if (exit_code)
  987. {
  988. process->debug_obj = NULL;
  989. terminate_process( process, NULL, exit_code );
  990. }
  991. else debugger_detach( process, debug_obj );
  992. }
  993. }
  994. void enum_processes( int (*cb)(struct process*, void*), void *user )
  995. {
  996. struct list *ptr, *next;
  997. LIST_FOR_EACH_SAFE( ptr, next, &process_list )
  998. {
  999. struct process *process = LIST_ENTRY( ptr, struct process, entry );
  1000. if ((cb)(process, user)) break;
  1001. }
  1002. }
  1003. /* set the debugged flag in the process PEB */
  1004. int set_process_debug_flag( struct process *process, int flag )
  1005. {
  1006. char data = (flag != 0);
  1007. client_ptr_t peb32 = 0;
  1008. if (!is_machine_64bit( process->machine ) && is_machine_64bit( native_machine ))
  1009. peb32 = process->peb + 0x1000;
  1010. /* BeingDebugged flag is the byte at offset 2 in the PEB */
  1011. if (peb32 && !write_process_memory( process, peb32 + 2, 1, &data )) return 0;
  1012. return write_process_memory( process, process->peb + 2, 1, &data );
  1013. }
  1014. /* create a new process */
  1015. DECL_HANDLER(new_process)
  1016. {
  1017. struct startup_info *info;
  1018. const void *info_ptr;
  1019. struct unicode_str name;
  1020. const struct security_descriptor *sd;
  1021. const struct object_attributes *objattr = get_req_object_attributes( &sd, &name, NULL );
  1022. struct process *process = NULL;
  1023. struct token *token = NULL;
  1024. struct debug_obj *debug_obj = NULL;
  1025. struct process *parent;
  1026. struct thread *parent_thread = current;
  1027. int socket_fd = thread_get_inflight_fd( current, req->socket_fd );
  1028. const obj_handle_t *handles = NULL;
  1029. const obj_handle_t *job_handles = NULL;
  1030. unsigned int i, job_handle_count;
  1031. struct job *job;
  1032. if (socket_fd == -1)
  1033. {
  1034. set_error( STATUS_INVALID_PARAMETER );
  1035. return;
  1036. }
  1037. if (!objattr)
  1038. {
  1039. set_error( STATUS_INVALID_PARAMETER );
  1040. close( socket_fd );
  1041. return;
  1042. }
  1043. if (fcntl( socket_fd, F_SETFL, O_NONBLOCK ) == -1)
  1044. {
  1045. set_error( STATUS_INVALID_HANDLE );
  1046. close( socket_fd );
  1047. return;
  1048. }
  1049. if (shutdown_stage)
  1050. {
  1051. set_error( STATUS_SHUTDOWN_IN_PROGRESS );
  1052. close( socket_fd );
  1053. return;
  1054. }
  1055. if (req->parent_process)
  1056. {
  1057. if (!(parent = get_process_from_handle( req->parent_process, PROCESS_CREATE_PROCESS)))
  1058. {
  1059. close( socket_fd );
  1060. return;
  1061. }
  1062. parent_thread = NULL;
  1063. }
  1064. else parent = (struct process *)grab_object( current->process );
  1065. /* If a job further in the job chain does not permit breakaway process creation
  1066. * succeeds and the process which is trying to breakaway is assigned to that job. */
  1067. if (parent->job && (req->flags & PROCESS_CREATE_FLAGS_BREAKAWAY) &&
  1068. !(parent->job->limit_flags & (JOB_OBJECT_LIMIT_BREAKAWAY_OK | JOB_OBJECT_LIMIT_SILENT_BREAKAWAY_OK)))
  1069. {
  1070. set_error( STATUS_ACCESS_DENIED );
  1071. close( socket_fd );
  1072. release_object( parent );
  1073. return;
  1074. }
  1075. /* build the startup info for a new process */
  1076. if (!(info = alloc_object( &startup_info_ops )))
  1077. {
  1078. close( socket_fd );
  1079. release_object( parent );
  1080. return;
  1081. }
  1082. info->process = NULL;
  1083. info->data = NULL;
  1084. info_ptr = get_req_data_after_objattr( objattr, &info->data_size );
  1085. if ((req->handles_size & 3) || req->handles_size > info->data_size)
  1086. {
  1087. set_error( STATUS_INVALID_PARAMETER );
  1088. close( socket_fd );
  1089. goto done;
  1090. }
  1091. if (req->handles_size)
  1092. {
  1093. handles = info_ptr;
  1094. info_ptr = (const char *)info_ptr + req->handles_size;
  1095. info->data_size -= req->handles_size;
  1096. }
  1097. if ((req->jobs_size & 3) || req->jobs_size > info->data_size)
  1098. {
  1099. set_error( STATUS_INVALID_PARAMETER );
  1100. close( socket_fd );
  1101. goto done;
  1102. }
  1103. if (req->jobs_size)
  1104. {
  1105. job_handles = info_ptr;
  1106. info_ptr = (const char *)info_ptr + req->jobs_size;
  1107. info->data_size -= req->jobs_size;
  1108. }
  1109. job_handle_count = req->jobs_size / sizeof(*handles);
  1110. for (i = 0; i < job_handle_count; ++i)
  1111. {
  1112. if (!(job = get_job_obj( current->process, job_handles[i], JOB_OBJECT_ASSIGN_PROCESS )))
  1113. {
  1114. close( socket_fd );
  1115. goto done;
  1116. }
  1117. release_object( job );
  1118. }
  1119. info->info_size = min( req->info_size, info->data_size );
  1120. if (req->info_size < sizeof(*info->data))
  1121. {
  1122. /* make sure we have a full startup_info_t structure */
  1123. data_size_t env_size = info->data_size - info->info_size;
  1124. data_size_t info_size = min( req->info_size, FIELD_OFFSET( startup_info_t, curdir_len ));
  1125. if (!(info->data = mem_alloc( sizeof(*info->data) + env_size )))
  1126. {
  1127. close( socket_fd );
  1128. goto done;
  1129. }
  1130. memcpy( info->data, info_ptr, info_size );
  1131. memset( (char *)info->data + info_size, 0, sizeof(*info->data) - info_size );
  1132. memcpy( info->data + 1, (const char *)info_ptr + req->info_size, env_size );
  1133. info->info_size = sizeof(startup_info_t);
  1134. info->data_size = info->info_size + env_size;
  1135. }
  1136. else
  1137. {
  1138. data_size_t pos = sizeof(*info->data);
  1139. if (!(info->data = memdup( info_ptr, info->data_size )))
  1140. {
  1141. close( socket_fd );
  1142. goto done;
  1143. }
  1144. #define FIXUP_LEN(len) do { (len) = min( (len), info->info_size - pos ); pos += (len); } while(0)
  1145. FIXUP_LEN( info->data->curdir_len );
  1146. FIXUP_LEN( info->data->dllpath_len );
  1147. FIXUP_LEN( info->data->imagepath_len );
  1148. FIXUP_LEN( info->data->cmdline_len );
  1149. FIXUP_LEN( info->data->title_len );
  1150. FIXUP_LEN( info->data->desktop_len );
  1151. FIXUP_LEN( info->data->shellinfo_len );
  1152. FIXUP_LEN( info->data->runtime_len );
  1153. #undef FIXUP_LEN
  1154. }
  1155. if (req->token && !(token = get_token_obj( current->process, req->token, TOKEN_QUERY | TOKEN_ASSIGN_PRIMARY )))
  1156. {
  1157. close( socket_fd );
  1158. goto done;
  1159. }
  1160. if (req->debug && !(debug_obj = get_debug_obj( current->process, req->debug, DEBUG_PROCESS_ASSIGN )))
  1161. {
  1162. close( socket_fd );
  1163. goto done;
  1164. }
  1165. if (!(process = create_process( socket_fd, parent, req->flags, info->data, sd,
  1166. handles, req->handles_size / sizeof(*handles), token )))
  1167. goto done;
  1168. process->machine = req->machine;
  1169. process->startup_info = (struct startup_info *)grab_object( info );
  1170. job = parent->job;
  1171. while (job)
  1172. {
  1173. if (!(job->limit_flags & JOB_OBJECT_LIMIT_SILENT_BREAKAWAY_OK)
  1174. && !(req->flags & PROCESS_CREATE_FLAGS_BREAKAWAY
  1175. && job->limit_flags & JOB_OBJECT_LIMIT_BREAKAWAY_OK))
  1176. {
  1177. add_job_process( job, process );
  1178. assert( !get_error() );
  1179. break;
  1180. }
  1181. job = job->parent;
  1182. }
  1183. for (i = 0; i < job_handle_count; ++i)
  1184. {
  1185. job = get_job_obj( current->process, job_handles[i], JOB_OBJECT_ASSIGN_PROCESS );
  1186. add_job_process( job, process );
  1187. release_object( job );
  1188. if (get_error())
  1189. {
  1190. release_job_process( process );
  1191. goto done;
  1192. }
  1193. }
  1194. /* connect to the window station */
  1195. connect_process_winstation( process, parent_thread, parent );
  1196. /* inherit the process console, but keep pseudo handles (< 0), and 0 (= not attached to a console) as is */
  1197. if ((int)info->data->console > 0)
  1198. info->data->console = duplicate_handle( parent, info->data->console, process,
  1199. 0, 0, DUPLICATE_SAME_ACCESS );
  1200. if (!(req->flags & PROCESS_CREATE_FLAGS_INHERIT_HANDLES) && info->data->console != 1)
  1201. {
  1202. info->data->hstdin = duplicate_handle( parent, info->data->hstdin, process,
  1203. 0, OBJ_INHERIT, DUPLICATE_SAME_ACCESS );
  1204. info->data->hstdout = duplicate_handle( parent, info->data->hstdout, process,
  1205. 0, OBJ_INHERIT, DUPLICATE_SAME_ACCESS );
  1206. info->data->hstderr = duplicate_handle( parent, info->data->hstderr, process,
  1207. 0, OBJ_INHERIT, DUPLICATE_SAME_ACCESS );
  1208. /* some handles above may have been invalid; this is not an error */
  1209. if (get_error() == STATUS_INVALID_HANDLE ||
  1210. get_error() == STATUS_OBJECT_TYPE_MISMATCH) clear_error();
  1211. }
  1212. /* attach to the debugger */
  1213. if (debug_obj)
  1214. {
  1215. process->debug_obj = debug_obj;
  1216. process->debug_children = !(req->flags & PROCESS_CREATE_FLAGS_NO_DEBUG_INHERIT);
  1217. }
  1218. else if (parent->debug_children)
  1219. {
  1220. process->debug_obj = parent->debug_obj;
  1221. /* debug_children is set to 1 by default */
  1222. }
  1223. if (info->data->process_group_id == parent->group_id)
  1224. process->group_id = parent->group_id;
  1225. else
  1226. info->data->process_group_id = process->group_id;
  1227. info->process = (struct process *)grab_object( process );
  1228. reply->info = alloc_handle( current->process, info, SYNCHRONIZE, 0 );
  1229. reply->pid = get_process_id( process );
  1230. reply->handle = alloc_handle_no_access_check( current->process, process, req->access, objattr->attributes );
  1231. done:
  1232. if (process) release_object( process );
  1233. if (debug_obj) release_object( debug_obj );
  1234. if (token) release_object( token );
  1235. release_object( parent );
  1236. release_object( info );
  1237. }
  1238. /* Retrieve information about a newly started process */
  1239. DECL_HANDLER(get_new_process_info)
  1240. {
  1241. struct startup_info *info;
  1242. if ((info = (struct startup_info *)get_handle_obj( current->process, req->info,
  1243. 0, &startup_info_ops )))
  1244. {
  1245. reply->success = is_process_init_done( info->process );
  1246. reply->exit_code = info->process->exit_code;
  1247. release_object( info );
  1248. }
  1249. }
  1250. /* Retrieve the new process startup info */
  1251. DECL_HANDLER(get_startup_info)
  1252. {
  1253. struct process *process = current->process;
  1254. struct startup_info *info = process->startup_info;
  1255. data_size_t size;
  1256. if (!info) return;
  1257. /* we return the data directly without making a copy so this can only be called once */
  1258. reply->machine = process->machine;
  1259. reply->info_size = info->info_size;
  1260. size = info->data_size;
  1261. if (size > get_reply_max_size()) size = get_reply_max_size();
  1262. set_reply_data_ptr( info->data, size );
  1263. info->data = NULL;
  1264. info->data_size = 0;
  1265. }
  1266. /* signal the end of the process initialization */
  1267. DECL_HANDLER(init_process_done)
  1268. {
  1269. struct process *process = current->process;
  1270. struct memory_view *view;
  1271. client_ptr_t base;
  1272. const pe_image_info_t *image_info;
  1273. if (is_process_init_done(process))
  1274. {
  1275. set_error( STATUS_INVALID_PARAMETER );
  1276. return;
  1277. }
  1278. if (!(view = get_exe_view( process )))
  1279. {
  1280. set_error( STATUS_DLL_NOT_FOUND );
  1281. return;
  1282. }
  1283. if (!(image_info = get_view_image_info( view, &base ))) return;
  1284. current->teb = req->teb;
  1285. process->peb = req->peb;
  1286. process->ldt_copy = req->ldt_copy;
  1287. process->start_time = current_time;
  1288. current->entry_point = base + image_info->entry_point;
  1289. init_process_tracing( process );
  1290. generate_startup_debug_events( process );
  1291. set_process_startup_state( process, STARTUP_DONE );
  1292. if (image_info->subsystem != IMAGE_SUBSYSTEM_WINDOWS_CUI)
  1293. process->idle_event = create_event( NULL, NULL, 0, 1, 0, NULL );
  1294. if (process->debug_obj) set_process_debug_flag( process, 1 );
  1295. reply->entry = current->entry_point;
  1296. reply->suspend = (current->suspend || process->suspend);
  1297. }
  1298. /* open a handle to a process */
  1299. DECL_HANDLER(open_process)
  1300. {
  1301. struct process *process = get_process_from_id( req->pid );
  1302. reply->handle = 0;
  1303. if (process)
  1304. {
  1305. reply->handle = alloc_handle( current->process, process, req->access, req->attributes );
  1306. release_object( process );
  1307. }
  1308. }
  1309. /* terminate a process */
  1310. DECL_HANDLER(terminate_process)
  1311. {
  1312. struct process *process;
  1313. if (req->handle)
  1314. {
  1315. process = get_process_from_handle( req->handle, PROCESS_TERMINATE );
  1316. if (!process) return;
  1317. }
  1318. else process = (struct process *)grab_object( current->process );
  1319. reply->self = (current->process == process);
  1320. terminate_process( process, current, req->exit_code );
  1321. release_object( process );
  1322. }
  1323. /* fetch information about a process */
  1324. DECL_HANDLER(get_process_info)
  1325. {
  1326. struct process *process;
  1327. if ((process = get_process_from_handle( req->handle, PROCESS_QUERY_LIMITED_INFORMATION )))
  1328. {
  1329. reply->pid = get_process_id( process );
  1330. reply->ppid = process->parent_id;
  1331. reply->exit_code = process->exit_code;
  1332. reply->priority = process->priority;
  1333. reply->affinity = process->affinity;
  1334. reply->peb = process->peb;
  1335. reply->start_time = process->start_time;
  1336. reply->end_time = process->end_time;
  1337. reply->session_id = process->session_id;
  1338. reply->machine = process->machine;
  1339. if (get_reply_max_size())
  1340. {
  1341. if (!process->running_threads) set_error( STATUS_PROCESS_IS_TERMINATING );
  1342. else set_reply_data( &process->image_info, min( sizeof(process->image_info), get_reply_max_size() ));
  1343. }
  1344. release_object( process );
  1345. }
  1346. }
  1347. /* retrieve debug information about a process */
  1348. DECL_HANDLER(get_process_debug_info)
  1349. {
  1350. struct process *process;
  1351. if (!(process = get_process_from_handle( req->handle, PROCESS_QUERY_LIMITED_INFORMATION ))) return;
  1352. reply->debug_children = process->debug_children;
  1353. if (!process->debug_obj) set_error( STATUS_PORT_NOT_SET );
  1354. else reply->debug = alloc_handle( current->process, process->debug_obj, MAXIMUM_ALLOWED, 0 );
  1355. release_object( process );
  1356. }
  1357. /* fetch the name of the process image */
  1358. DECL_HANDLER(get_process_image_name)
  1359. {
  1360. struct process *process = get_process_from_handle( req->handle, PROCESS_QUERY_LIMITED_INFORMATION );
  1361. if (!process) return;
  1362. if (process->image)
  1363. {
  1364. struct unicode_str name = { process->image, process->imagelen };
  1365. /* skip the \??\ prefix */
  1366. if (req->win32 && name.len > 6 * sizeof(WCHAR) && name.str[5] == ':')
  1367. {
  1368. name.str += 4;
  1369. name.len -= 4 * sizeof(WCHAR);
  1370. }
  1371. /* FIXME: else resolve symlinks in NT path */
  1372. reply->len = name.len;
  1373. if (name.len <= get_reply_max_size())
  1374. {
  1375. WCHAR *ptr = set_reply_data( name.str, name.len );
  1376. /* change \??\ to \\?\ */
  1377. if (req->win32 && name.len > sizeof(WCHAR) && ptr[1] == '?') ptr[1] = '\\';
  1378. }
  1379. else set_error( STATUS_BUFFER_TOO_SMALL );
  1380. }
  1381. else set_error( STATUS_INVALID_HANDLE );
  1382. release_object( process );
  1383. }
  1384. /* retrieve information about a process memory usage */
  1385. DECL_HANDLER(get_process_vm_counters)
  1386. {
  1387. struct process *process = get_process_from_handle( req->handle, PROCESS_QUERY_LIMITED_INFORMATION );
  1388. if (!process) return;
  1389. if (process->unix_pid != -1)
  1390. {
  1391. #ifdef linux
  1392. FILE *f;
  1393. char proc_path[32], line[256];
  1394. unsigned long value;
  1395. sprintf( proc_path, "/proc/%u/status", process->unix_pid );
  1396. if ((f = fopen( proc_path, "r" )))
  1397. {
  1398. while (fgets( line, sizeof(line), f ))
  1399. {
  1400. if (sscanf( line, "VmPeak: %lu", &value ))
  1401. reply->peak_virtual_size = (mem_size_t)value * 1024;
  1402. else if (sscanf( line, "VmSize: %lu", &value ))
  1403. reply->virtual_size = (mem_size_t)value * 1024;
  1404. else if (sscanf( line, "VmHWM: %lu", &value ))
  1405. reply->peak_working_set_size = (mem_size_t)value * 1024;
  1406. else if (sscanf( line, "VmRSS: %lu", &value ))
  1407. reply->working_set_size = (mem_size_t)value * 1024;
  1408. else if (sscanf( line, "RssAnon: %lu", &value ))
  1409. reply->pagefile_usage += (mem_size_t)value * 1024;
  1410. else if (sscanf( line, "VmSwap: %lu", &value ))
  1411. reply->pagefile_usage += (mem_size_t)value * 1024;
  1412. }
  1413. reply->peak_pagefile_usage = reply->pagefile_usage;
  1414. fclose( f );
  1415. }
  1416. else set_error( STATUS_ACCESS_DENIED );
  1417. #elif defined(HAVE_LIBPROCSTAT)
  1418. struct procstat *procstat;
  1419. unsigned int count;
  1420. if ((procstat = procstat_open_sysctl()))
  1421. {
  1422. struct kinfo_proc *kp = procstat_getprocs( procstat, KERN_PROC_PID, process->unix_pid, &count );
  1423. if (kp)
  1424. {
  1425. reply->virtual_size = kp->ki_size;
  1426. reply->peak_virtual_size = reply->virtual_size;
  1427. reply->working_set_size = kp->ki_rssize << PAGE_SHIFT;
  1428. reply->peak_working_set_size = kp->ki_rusage.ru_maxrss * 1024;
  1429. procstat_freeprocs( procstat, kp );
  1430. }
  1431. else set_error( STATUS_ACCESS_DENIED );
  1432. procstat_close( procstat );
  1433. }
  1434. else set_error( STATUS_ACCESS_DENIED );
  1435. #endif
  1436. }
  1437. else set_error( STATUS_ACCESS_DENIED );
  1438. release_object( process );
  1439. }
  1440. static void set_process_affinity( struct process *process, affinity_t affinity )
  1441. {
  1442. struct thread *thread;
  1443. if (!process->running_threads)
  1444. {
  1445. set_error( STATUS_PROCESS_IS_TERMINATING );
  1446. return;
  1447. }
  1448. process->affinity = affinity;
  1449. LIST_FOR_EACH_ENTRY( thread, &process->thread_list, struct thread, proc_entry )
  1450. {
  1451. set_thread_affinity( thread, affinity );
  1452. }
  1453. }
  1454. /* set information about a process */
  1455. DECL_HANDLER(set_process_info)
  1456. {
  1457. struct process *process;
  1458. if ((process = get_process_from_handle( req->handle, PROCESS_SET_INFORMATION )))
  1459. {
  1460. if (req->mask & SET_PROCESS_INFO_PRIORITY) process->priority = req->priority;
  1461. if (req->mask & SET_PROCESS_INFO_AFFINITY) set_process_affinity( process, req->affinity );
  1462. release_object( process );
  1463. }
  1464. }
  1465. /* read data from a process address space */
  1466. DECL_HANDLER(read_process_memory)
  1467. {
  1468. struct process *process;
  1469. data_size_t len = get_reply_max_size();
  1470. if (!(process = get_process_from_handle( req->handle, PROCESS_VM_READ ))) return;
  1471. if (len)
  1472. {
  1473. char *buffer = mem_alloc( len );
  1474. if (buffer)
  1475. {
  1476. if (read_process_memory( process, req->addr, len, buffer ))
  1477. set_reply_data_ptr( buffer, len );
  1478. else
  1479. free( buffer );
  1480. }
  1481. }
  1482. release_object( process );
  1483. }
  1484. /* write data to a process address space */
  1485. DECL_HANDLER(write_process_memory)
  1486. {
  1487. struct process *process;
  1488. if ((process = get_process_from_handle( req->handle, PROCESS_VM_WRITE )))
  1489. {
  1490. data_size_t len = get_req_data_size();
  1491. if (len) write_process_memory( process, req->addr, len, get_req_data() );
  1492. else set_error( STATUS_INVALID_PARAMETER );
  1493. release_object( process );
  1494. }
  1495. }
  1496. /* retrieve the process idle event */
  1497. DECL_HANDLER(get_process_idle_event)
  1498. {
  1499. struct process *process;
  1500. reply->event = 0;
  1501. if ((process = get_process_from_handle( req->handle, PROCESS_QUERY_INFORMATION )))
  1502. {
  1503. if (process->idle_event && process != current->process)
  1504. reply->event = alloc_handle( current->process, process->idle_event,
  1505. EVENT_ALL_ACCESS, 0 );
  1506. release_object( process );
  1507. }
  1508. }
  1509. /* make the current process a system process */
  1510. DECL_HANDLER(make_process_system)
  1511. {
  1512. struct process *process;
  1513. struct thread *thread;
  1514. if (!shutdown_event)
  1515. {
  1516. if (!(shutdown_event = create_event( NULL, NULL, OBJ_PERMANENT, 1, 0, NULL ))) return;
  1517. release_object( shutdown_event );
  1518. }
  1519. if (!(process = get_process_from_handle( req->handle, PROCESS_SET_INFORMATION ))) return;
  1520. if (!(reply->event = alloc_handle( current->process, shutdown_event, SYNCHRONIZE, 0 )))
  1521. {
  1522. release_object( process );
  1523. return;
  1524. }
  1525. if (!process->is_system)
  1526. {
  1527. LIST_FOR_EACH_ENTRY( thread, &process->thread_list, struct thread, proc_entry )
  1528. release_thread_desktop( thread, 0 );
  1529. process->is_system = 1;
  1530. if (!--user_processes && !shutdown_stage && master_socket_timeout != TIMEOUT_INFINITE)
  1531. shutdown_timeout = add_timeout_user( master_socket_timeout, server_shutdown_timeout, NULL );
  1532. }
  1533. release_object( process );
  1534. }
  1535. /* create a new job object */
  1536. DECL_HANDLER(create_job)
  1537. {
  1538. struct job *job;
  1539. struct unicode_str name;
  1540. struct object *root;
  1541. const struct security_descriptor *sd;
  1542. const struct object_attributes *objattr = get_req_object_attributes( &sd, &name, &root );
  1543. if (!objattr) return;
  1544. if ((job = create_job_object( root, &name, objattr->attributes, sd )))
  1545. {
  1546. if (get_error() == STATUS_OBJECT_NAME_EXISTS)
  1547. reply->handle = alloc_handle( current->process, job, req->access, objattr->attributes );
  1548. else
  1549. reply->handle = alloc_handle_no_access_check( current->process, job,
  1550. req->access, objattr->attributes );
  1551. release_object( job );
  1552. }
  1553. if (root) release_object( root );
  1554. }
  1555. /* open a job object */
  1556. DECL_HANDLER(open_job)
  1557. {
  1558. struct unicode_str name = get_req_unicode_str();
  1559. reply->handle = open_object( current->process, req->rootdir, req->access,
  1560. &job_ops, &name, req->attributes );
  1561. }
  1562. /* assign a job object to a process */
  1563. DECL_HANDLER(assign_job)
  1564. {
  1565. struct process *process;
  1566. struct job *job = get_job_obj( current->process, req->job, JOB_OBJECT_ASSIGN_PROCESS );
  1567. if (!job) return;
  1568. if ((process = get_process_from_handle( req->process, PROCESS_SET_QUOTA | PROCESS_TERMINATE )))
  1569. {
  1570. if (!process->running_threads) set_error( STATUS_PROCESS_IS_TERMINATING );
  1571. else add_job_process( job, process );
  1572. release_object( process );
  1573. }
  1574. release_object( job );
  1575. }
  1576. /* check if a process is associated with a job */
  1577. DECL_HANDLER(process_in_job)
  1578. {
  1579. struct process *process;
  1580. struct job *job;
  1581. if (!(process = get_process_from_handle( req->process, PROCESS_QUERY_INFORMATION )))
  1582. return;
  1583. if (!req->job)
  1584. {
  1585. set_error( process->job ? STATUS_PROCESS_IN_JOB : STATUS_PROCESS_NOT_IN_JOB );
  1586. }
  1587. else if ((job = get_job_obj( current->process, req->job, JOB_OBJECT_QUERY )))
  1588. {
  1589. set_error( process_in_job( job, process ) ? STATUS_PROCESS_IN_JOB : STATUS_PROCESS_NOT_IN_JOB );
  1590. release_object( job );
  1591. }
  1592. release_object( process );
  1593. }
  1594. /* retrieve information about a job */
  1595. DECL_HANDLER(get_job_info)
  1596. {
  1597. struct job *job = get_job_obj( current->process, req->handle, JOB_OBJECT_QUERY );
  1598. process_id_t *pids;
  1599. data_size_t len;
  1600. if (!job) return;
  1601. reply->total_processes = job->total_processes;
  1602. reply->active_processes = job->num_processes;
  1603. len = min( get_reply_max_size(), reply->active_processes * sizeof(*pids) );
  1604. if (len && ((pids = set_reply_data_size( len ))))
  1605. get_job_pids( job, pids, pids + len / sizeof(*pids) );
  1606. release_object( job );
  1607. }
  1608. /* terminate all processes associated with the job */
  1609. DECL_HANDLER(terminate_job)
  1610. {
  1611. struct job *job = get_job_obj( current->process, req->handle, JOB_OBJECT_TERMINATE );
  1612. if (!job) return;
  1613. terminate_job( job, req->status );
  1614. release_object( job );
  1615. }
  1616. /* update limits of the job object */
  1617. DECL_HANDLER(set_job_limits)
  1618. {
  1619. struct job *job = get_job_obj( current->process, req->handle, JOB_OBJECT_SET_ATTRIBUTES );
  1620. if (!job) return;
  1621. job->limit_flags = req->limit_flags;
  1622. release_object( job );
  1623. }
  1624. /* set the jobs completion port */
  1625. DECL_HANDLER(set_job_completion_port)
  1626. {
  1627. struct job *job = get_job_obj( current->process, req->job, JOB_OBJECT_SET_ATTRIBUTES );
  1628. if (!job) return;
  1629. if (!job->completion_port)
  1630. {
  1631. job->completion_port = get_completion_obj( current->process, req->port, IO_COMPLETION_MODIFY_STATE );
  1632. job->completion_key = req->key;
  1633. add_job_completion_existing_processes( job, job );
  1634. }
  1635. else
  1636. set_error( STATUS_INVALID_PARAMETER );
  1637. release_object( job );
  1638. }
  1639. /* Suspend a process */
  1640. DECL_HANDLER(suspend_process)
  1641. {
  1642. struct process *process;
  1643. if ((process = get_process_from_handle( req->handle, PROCESS_SUSPEND_RESUME )))
  1644. {
  1645. struct list *ptr, *next;
  1646. LIST_FOR_EACH_SAFE( ptr, next, &process->thread_list )
  1647. {
  1648. struct thread *thread = LIST_ENTRY( ptr, struct thread, proc_entry );
  1649. suspend_thread( thread );
  1650. }
  1651. release_object( process );
  1652. }
  1653. }
  1654. /* Resume a process */
  1655. DECL_HANDLER(resume_process)
  1656. {
  1657. struct process *process;
  1658. if ((process = get_process_from_handle( req->handle, PROCESS_SUSPEND_RESUME )))
  1659. {
  1660. struct list *ptr, *next;
  1661. LIST_FOR_EACH_SAFE( ptr, next, &process->thread_list )
  1662. {
  1663. struct thread *thread = LIST_ENTRY( ptr, struct thread, proc_entry );
  1664. resume_thread( thread );
  1665. }
  1666. release_object( process );
  1667. }
  1668. }
  1669. /* Get a list of processes and threads currently running */
  1670. DECL_HANDLER(list_processes)
  1671. {
  1672. struct process *process;
  1673. struct thread *thread;
  1674. unsigned int pos = 0;
  1675. char *buffer;
  1676. reply->process_count = 0;
  1677. reply->total_thread_count = 0;
  1678. reply->total_name_len = 0;
  1679. reply->info_size = 0;
  1680. LIST_FOR_EACH_ENTRY( process, &process_list, struct process, entry )
  1681. {
  1682. reply->info_size = (reply->info_size + 7) & ~7;
  1683. reply->info_size += sizeof(struct process_info) + process->imagelen;
  1684. reply->info_size = (reply->info_size + 7) & ~7;
  1685. reply->info_size += process->running_threads * sizeof(struct thread_info);
  1686. reply->process_count++;
  1687. reply->total_thread_count += process->running_threads;
  1688. reply->total_name_len += process->imagelen;
  1689. }
  1690. if (reply->info_size > get_reply_max_size())
  1691. {
  1692. set_error( STATUS_INFO_LENGTH_MISMATCH );
  1693. return;
  1694. }
  1695. if (!(buffer = set_reply_data_size( reply->info_size ))) return;
  1696. memset( buffer, 0, reply->info_size );
  1697. LIST_FOR_EACH_ENTRY( process, &process_list, struct process, entry )
  1698. {
  1699. struct process_info *process_info;
  1700. pos = (pos + 7) & ~7;
  1701. process_info = (struct process_info *)(buffer + pos);
  1702. process_info->start_time = process->start_time;
  1703. process_info->name_len = process->imagelen;
  1704. process_info->thread_count = process->running_threads;
  1705. process_info->priority = process->priority;
  1706. process_info->pid = process->id;
  1707. process_info->parent_pid = process->parent_id;
  1708. process_info->session_id = process->session_id;
  1709. process_info->handle_count = get_handle_table_count(process);
  1710. process_info->unix_pid = process->unix_pid;
  1711. pos += sizeof(*process_info);
  1712. memcpy( buffer + pos, process->image, process->imagelen );
  1713. pos += process->imagelen;
  1714. pos = (pos + 7) & ~7;
  1715. LIST_FOR_EACH_ENTRY( thread, &process->thread_list, struct thread, proc_entry )
  1716. {
  1717. struct thread_info *thread_info = (struct thread_info *)(buffer + pos);
  1718. thread_info->start_time = thread->creation_time;
  1719. thread_info->tid = thread->id;
  1720. thread_info->base_priority = thread->priority;
  1721. thread_info->current_priority = thread->priority; /* FIXME */
  1722. thread_info->unix_tid = thread->unix_tid;
  1723. thread_info->entry_point = thread->entry_point;
  1724. thread_info->teb = thread->teb;
  1725. pos += sizeof(*thread_info);
  1726. }
  1727. }
  1728. }