process.c 64 KB

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