process.c 57 KB

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