12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147 |
- /*
- * Server-side thread management
- *
- * Copyright (C) 1998 Alexandre Julliard
- *
- * This library is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 2.1 of the License, or (at your option) any later version.
- *
- * This library is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this library; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
- */
- #include "config.h"
- #include "wine/port.h"
- #include <assert.h>
- #include <errno.h>
- #include <fcntl.h>
- #include <signal.h>
- #include <stdarg.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <sys/types.h>
- #include <unistd.h>
- #include <time.h>
- #ifdef HAVE_POLL_H
- #include <poll.h>
- #endif
- #ifdef HAVE_SCHED_H
- #include <sched.h>
- #endif
- #include "ntstatus.h"
- #define WIN32_NO_STATUS
- #include "windef.h"
- #include "winternl.h"
- #include "device.h"
- #include "file.h"
- #include "handle.h"
- #include "process.h"
- #include "thread.h"
- #include "request.h"
- #include "user.h"
- #include "security.h"
- #include "esync.h"
- #ifdef __i386__
- static const unsigned int supported_cpus = CPU_FLAG(CPU_x86);
- #elif defined(__x86_64__)
- static const unsigned int supported_cpus = CPU_FLAG(CPU_x86_64) | CPU_FLAG(CPU_x86);
- #elif defined(__powerpc__)
- static const unsigned int supported_cpus = CPU_FLAG(CPU_POWERPC);
- #elif defined(__arm__)
- static const unsigned int supported_cpus = CPU_FLAG(CPU_ARM);
- #elif defined(__aarch64__)
- static const unsigned int supported_cpus = CPU_FLAG(CPU_ARM64) | CPU_FLAG(CPU_ARM);
- #else
- #error Unsupported CPU
- #endif
- /* thread queues */
- struct thread_wait
- {
- struct thread_wait *next; /* next wait structure for this thread */
- struct thread *thread; /* owner thread */
- int count; /* count of objects */
- int flags;
- int in_kernel; /* we can't timeout when winedevice is working on the thread's behalf */
- int abandoned;
- enum select_op select;
- client_ptr_t key; /* wait key for keyed events */
- client_ptr_t cookie; /* magic cookie to return to client */
- timeout_t timeout;
- struct timeout_user *user;
- struct wait_queue_entry queues[1];
- };
- /* asynchronous procedure calls */
- struct thread_apc
- {
- struct object obj; /* object header */
- struct list entry; /* queue linked list */
- struct thread *callee; /* thread this apc is queued on */
- struct thread *caller; /* thread that queued this apc */
- struct object *owner; /* object that queued this apc */
- int executed; /* has it been executed by the client? */
- apc_call_t call; /* call arguments */
- apc_result_t result; /* call results once executed */
- };
- static void dump_thread_apc( struct object *obj, int verbose );
- static int thread_apc_signaled( struct object *obj, struct wait_queue_entry *entry );
- static void thread_apc_destroy( struct object *obj );
- static void clear_apc_queue( struct list *queue );
- static const struct object_ops thread_apc_ops =
- {
- sizeof(struct thread_apc), /* size */
- dump_thread_apc, /* dump */
- no_get_type, /* get_type */
- add_queue, /* add_queue */
- remove_queue, /* remove_queue */
- thread_apc_signaled, /* signaled */
- NULL, /* get_esync_fd */
- no_satisfied, /* satisfied */
- no_signal, /* signal */
- no_get_fd, /* get_fd */
- no_map_access, /* map_access */
- default_get_sd, /* get_sd */
- default_set_sd, /* set_sd */
- no_lookup_name, /* lookup_name */
- no_link_name, /* link_name */
- NULL, /* unlink_name */
- no_open_file, /* open_file */
- no_kernel_obj_list, /* get_kernel_obj_list */
- no_alloc_handle, /* alloc_handle */
- no_close_handle, /* close_handle */
- thread_apc_destroy /* destroy */
- };
- /* thread operations */
- static void dump_thread( struct object *obj, int verbose );
- static struct object_type *thread_get_type( struct object *obj );
- static int thread_signaled( struct object *obj, struct wait_queue_entry *entry );
- static int thread_get_esync_fd( struct object *obj, enum esync_type *type );
- static unsigned int thread_map_access( struct object *obj, unsigned int access );
- static void thread_poll_event( struct fd *fd, int event );
- static struct list *thread_get_kernel_obj_list( struct object *obj );
- static void destroy_thread( struct object *obj );
- static const struct object_ops thread_ops =
- {
- sizeof(struct thread), /* size */
- dump_thread, /* dump */
- thread_get_type, /* get_type */
- add_queue, /* add_queue */
- remove_queue, /* remove_queue */
- thread_signaled, /* signaled */
- thread_get_esync_fd, /* get_esync_fd */
- no_satisfied, /* satisfied */
- no_signal, /* signal */
- no_get_fd, /* get_fd */
- thread_map_access, /* map_access */
- default_get_sd, /* get_sd */
- default_set_sd, /* set_sd */
- no_lookup_name, /* lookup_name */
- no_link_name, /* link_name */
- NULL, /* unlink_name */
- no_open_file, /* open_file */
- thread_get_kernel_obj_list, /* get_kernel_obj_list */
- no_alloc_handle, /* alloc_handle */
- no_close_handle, /* close_handle */
- destroy_thread /* destroy */
- };
- static const struct fd_ops thread_fd_ops =
- {
- NULL, /* get_poll_events */
- thread_poll_event, /* poll_event */
- NULL, /* flush */
- NULL, /* get_fd_type */
- NULL, /* ioctl */
- NULL, /* queue_async */
- NULL /* reselect_async */
- };
- /* thread startup info, right now only used for waiting */
- struct startup_info
- {
- struct object obj;
- struct thread *thread;
- };
- static void startup_info_dump( struct object *obj, int verbose );
- static int startup_info_signaled( struct object *obj, struct wait_queue_entry *entry );
- static void startup_info_destroy( struct object *obj );
- static const struct object_ops startup_info_ops =
- {
- sizeof(struct startup_info), /* size */
- startup_info_dump, /* dump */
- no_get_type, /* get_type */
- add_queue, /* add_queue */
- remove_queue, /* remove_queue */
- startup_info_signaled, /* signaled */
- NULL, /* get_esync_fd */
- no_satisfied, /* satisfied */
- no_signal, /* signal */
- no_get_fd, /* get_fd */
- no_map_access, /* map_access */
- default_get_sd, /* get_sd */
- default_set_sd, /* set_sd */
- no_lookup_name, /* lookup_name */
- no_link_name, /* link_name */
- NULL, /* unlink_name */
- no_open_file, /* open_file */
- no_kernel_obj_list, /* get_kernel_obj_list */
- no_alloc_handle, /* alloc_handle */
- no_close_handle, /* close_handle */
- startup_info_destroy /* destroy */
- };
- static struct list thread_list = LIST_INIT(thread_list);
- /* initialize the structure for a newly allocated thread */
- static inline void init_thread_structure( struct thread *thread )
- {
- int i;
- thread->unix_pid = -1; /* not known yet */
- thread->unix_tid = -1; /* not known yet */
- thread->context = NULL;
- thread->suspend_context = NULL;
- thread->teb = 0;
- thread->entry_point = 0;
- thread->esync_fd = -1;
- thread->esync_apc_fd = -1;
- thread->debug_ctx = NULL;
- thread->system_regs = 0;
- thread->queue = NULL;
- thread->wait = NULL;
- thread->error = 0;
- thread->req_data = NULL;
- thread->req_toread = 0;
- thread->reply_data = NULL;
- thread->reply_towrite = 0;
- thread->request_fd = NULL;
- thread->reply_fd = NULL;
- thread->wait_fd = NULL;
- thread->state = RUNNING;
- thread->exit_code = 0;
- thread->priority = 0;
- thread->suspend = 0;
- thread->desktop_users = 0;
- thread->token = NULL;
- thread->desc = NULL;
- thread->desc_len = 0;
- thread->exit_poll = NULL;
- thread->attached_process = NULL;
- thread->callback_init_event = NULL;
- thread->startup_info = NULL;
- thread->shm_fd = -1;
- thread->shm = NULL;
- thread->creation_time = current_time;
- thread->exit_time = 0;
- list_init( &thread->mutex_list );
- list_init( &thread->system_apc );
- list_init( &thread->kernel_apc);
- list_init( &thread->user_apc );
- list_init( &thread->kernel_object );
- for (i = 0; i < MAX_INFLIGHT_FDS; i++)
- thread->inflight[i].server = thread->inflight[i].client = -1;
- }
- /* check if address looks valid for a client-side data structure (TEB etc.) */
- static inline int is_valid_address( client_ptr_t addr )
- {
- return addr && !(addr % sizeof(int));
- }
- /* create a new thread */
- struct thread *create_thread( int fd, struct process *process, const struct security_descriptor *sd )
- {
- struct thread *thread;
- int request_pipe[2];
- krnl_cbdata_t cbdata;
- struct object *done_event = NULL;
- if (fd == -1)
- {
- if (pipe( request_pipe ) == -1)
- {
- file_set_error();
- return NULL;
- }
- if (send_client_fd( process, request_pipe[1], SERVER_PROTOCOL_VERSION ) == -1)
- {
- close( request_pipe[0] );
- close( request_pipe[1] );
- return NULL;
- }
- close( request_pipe[1] );
- fd = request_pipe[0];
- }
- if (process->is_terminating)
- {
- close( fd );
- set_error( STATUS_PROCESS_IS_TERMINATING );
- return NULL;
- }
- if (!(thread = alloc_object( &thread_ops )))
- {
- close( fd );
- return NULL;
- }
- init_thread_structure( thread );
- thread->process = (struct process *)grab_object( process );
- thread->desktop = process->desktop;
- thread->affinity = process->affinity;
- if (!current) current = thread;
- list_add_head( &thread_list, &thread->entry );
- if (sd && !set_sd_defaults_from_token( &thread->obj, sd,
- OWNER_SECURITY_INFORMATION | GROUP_SECURITY_INFORMATION |
- DACL_SECURITY_INFORMATION | SACL_SECURITY_INFORMATION,
- process->token ))
- {
- close( fd );
- release_object( thread );
- return NULL;
- }
- if (!(thread->id = alloc_ptid( thread )))
- {
- close( fd );
- release_object( thread );
- return NULL;
- }
- if (!(thread->startup_info = alloc_object( &startup_info_ops )))
- {
- release_object(thread);
- return NULL;
- }
- thread->startup_info->thread = (struct thread *)grab_object(thread);
- if (!(thread->request_fd = create_anonymous_fd( &thread_fd_ops, fd, &thread->obj, 0 )))
- {
- release_object(thread->startup_info);
- release_object( thread );
- return NULL;
- }
- if (do_esync())
- {
- thread->esync_fd = esync_create_fd( 0, 0 );
- thread->esync_apc_fd = esync_create_fd( 0, 0 );
- }
- set_fd_events( thread->request_fd, POLLIN ); /* start listening to events */
- add_process_thread( thread->process, thread );
- cbdata.cb_type = SERVER_CALLBACK_THRD_LIFE;
- cbdata.thread_life.create = 1;
- cbdata.thread_life.pid = thread->process->id;
- cbdata.thread_life.tid = thread->id;
- queue_callback(&cbdata, NULL, &done_event);
- if (done_event)
- {
- if (is_process_init_done(thread->process))
- thread->callback_init_event = done_event;
- else
- release_object(done_event);
- }
- return thread;
- }
- static void startup_info_destroy( struct object *obj )
- {
- struct startup_info *info = (struct startup_info *)obj;
- assert( obj->ops == &startup_info_ops );
- if (info->thread) release_object( info->thread );
- }
- static void startup_info_dump( struct object *obj, int verbose )
- {
- assert( obj->ops == &startup_info_ops );
- fprintf( stderr, "Startup info, thread\n");
- }
- static int startup_info_signaled( struct object *obj, struct wait_queue_entry *entry )
- {
- struct startup_info *info = (struct startup_info *)obj;
- return info->thread && info->thread->unix_tid != -1;
- }
- /* handle a client event */
- static void thread_poll_event( struct fd *fd, int event )
- {
- struct thread *thread = get_fd_user( fd );
- assert( thread->obj.ops == &thread_ops );
- grab_object( thread );
- if (event & (POLLERR | POLLHUP)) kill_thread( thread, 0 );
- else if (event & POLLIN) read_request( thread );
- else if (event & POLLOUT) write_reply( thread );
- release_object( thread );
- }
- static struct list *thread_get_kernel_obj_list( struct object *obj )
- {
- struct thread *thread = (struct thread *)obj;
- return &thread->kernel_object;
- }
- /* cleanup everything that is no longer needed by a dead thread */
- /* used by destroy_thread and kill_thread */
- static void cleanup_thread( struct thread *thread )
- {
- int i;
- clear_apc_queue( &thread->system_apc );
- clear_apc_queue( &thread->kernel_apc );
- clear_apc_queue( &thread->user_apc );
- free( thread->req_data );
- free( thread->reply_data );
- if (thread->request_fd) release_object( thread->request_fd );
- if (thread->reply_fd) release_object( thread->reply_fd );
- if (thread->wait_fd) release_object( thread->wait_fd );
- if (thread->attached_process) release_object ( thread->attached_process);
- if (thread->callback_init_event) release_object( thread->callback_init_event );
- free( thread->suspend_context );
- cleanup_clipboard_thread(thread);
- destroy_thread_windows( thread );
- free_msg_queue( thread );
- close_thread_desktop( thread );
- for (i = 0; i < MAX_INFLIGHT_FDS; i++)
- {
- if (thread->inflight[i].client != -1)
- {
- close( thread->inflight[i].server );
- thread->inflight[i].client = thread->inflight[i].server = -1;
- }
- }
- free( thread->desc );
- release_shared_memory( thread->shm_fd, thread->shm, sizeof(*thread->shm) );
- thread->req_data = NULL;
- thread->reply_data = NULL;
- thread->request_fd = NULL;
- thread->reply_fd = NULL;
- thread->wait_fd = NULL;
- thread->context = NULL;
- thread->suspend_context = NULL;
- thread->desktop = 0;
- thread->desc = NULL;
- thread->desc_len = 0;
- thread->attached_process = NULL;
- thread->shm_fd = -1;
- thread->shm = NULL;
- }
- /* destroy a thread when its refcount is 0 */
- static void destroy_thread( struct object *obj )
- {
- struct thread *thread = (struct thread *)obj;
- assert( obj->ops == &thread_ops );
- assert( !thread->debug_ctx ); /* cannot still be debugging something */
- list_remove( &thread->entry );
- cleanup_thread( thread );
- release_object( thread->process );
- if (thread->exit_poll) remove_timeout_user( thread->exit_poll );
- if (thread->id) free_ptid( thread->id );
- if (thread->token) release_object( thread->token );
- if (do_esync())
- close( thread->esync_fd );
- }
- /* dump a thread on stdout for debugging purposes */
- static void dump_thread( struct object *obj, int verbose )
- {
- struct thread *thread = (struct thread *)obj;
- assert( obj->ops == &thread_ops );
- fprintf( stderr, "Thread id=%04x unix pid=%d unix tid=%d state=%d\n",
- thread->id, thread->unix_pid, thread->unix_tid, thread->state );
- }
- static struct object_type *thread_get_type( struct object *obj )
- {
- static const struct unicode_str str = { type_Thread, sizeof(type_Thread) };
- return get_object_type( &str );
- }
- static int thread_signaled( struct object *obj, struct wait_queue_entry *entry )
- {
- struct thread *mythread = (struct thread *)obj;
- return mythread->state == TERMINATED && !mythread->exit_poll;
- }
- static int thread_get_esync_fd( struct object *obj, enum esync_type *type )
- {
- struct thread *thread = (struct thread *)obj;
- *type = ESYNC_MANUAL_SERVER;
- return thread->esync_fd;
- }
- static unsigned int thread_map_access( struct object *obj, unsigned int access )
- {
- if (access & GENERIC_READ) access |= STANDARD_RIGHTS_READ | THREAD_QUERY_INFORMATION | THREAD_GET_CONTEXT;
- if (access & GENERIC_WRITE) access |= STANDARD_RIGHTS_WRITE | THREAD_SET_INFORMATION | THREAD_SET_CONTEXT |
- THREAD_TERMINATE | THREAD_SUSPEND_RESUME;
- if (access & GENERIC_EXECUTE) access |= STANDARD_RIGHTS_EXECUTE | SYNCHRONIZE | THREAD_QUERY_LIMITED_INFORMATION;
- if (access & GENERIC_ALL) access |= THREAD_ALL_ACCESS;
- if (access & THREAD_QUERY_INFORMATION) access |= THREAD_QUERY_LIMITED_INFORMATION;
- if (access & THREAD_SET_INFORMATION) access |= THREAD_SET_LIMITED_INFORMATION;
- return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
- }
- static void dump_thread_apc( struct object *obj, int verbose )
- {
- struct thread_apc *apc = (struct thread_apc *)obj;
- assert( obj->ops == &thread_apc_ops );
- fprintf( stderr, "APC owner=%p type=%u\n", apc->owner, apc->call.type );
- }
- static int thread_apc_signaled( struct object *obj, struct wait_queue_entry *entry )
- {
- struct thread_apc *apc = (struct thread_apc *)obj;
- return apc->executed;
- }
- static void thread_apc_destroy( struct object *obj )
- {
- struct thread_apc *apc = (struct thread_apc *)obj;
- if (apc->callee) release_object( apc->callee);
- if (apc->caller) release_object( apc->caller );
- if (apc->owner) release_object( apc->owner );
- }
- /* queue an async procedure call */
- static struct thread_apc *create_apc( struct object *owner, const apc_call_t *call_data )
- {
- struct thread_apc *apc;
- if ((apc = alloc_object( &thread_apc_ops )))
- {
- apc->call = *call_data;
- apc->caller = NULL;
- apc->callee = NULL;
- apc->owner = owner;
- apc->executed = 0;
- apc->result.type = APC_NONE;
- if (owner) grab_object( owner );
- }
- return apc;
- }
- /* get a thread pointer from a thread id (and increment the refcount) */
- struct thread *get_thread_from_id( thread_id_t id )
- {
- struct object *obj = get_ptid_entry( id );
- if (obj && obj->ops == &thread_ops) return (struct thread *)grab_object( obj );
- set_error( STATUS_INVALID_CID );
- return NULL;
- }
- /* get a thread from a handle (and increment the refcount) */
- struct thread *get_thread_from_handle( obj_handle_t handle, unsigned int access )
- {
- return (struct thread *)get_handle_obj( current->process, handle,
- access, &thread_ops );
- }
- /* find a thread from a Unix tid */
- struct thread *get_thread_from_tid( int tid )
- {
- struct thread *thread;
- LIST_FOR_EACH_ENTRY( thread, &thread_list, struct thread, entry )
- {
- if (thread->unix_tid == tid) return thread;
- }
- return NULL;
- }
- /* find a thread from a Unix pid */
- struct thread *get_thread_from_pid( int pid )
- {
- struct thread *thread;
- LIST_FOR_EACH_ENTRY( thread, &thread_list, struct thread, entry )
- {
- if (thread->unix_pid == pid) return thread;
- }
- return NULL;
- }
- int set_thread_affinity( struct thread *thread, affinity_t affinity )
- {
- int ret = 0;
- #ifdef HAVE_SCHED_SETAFFINITY
- if (thread->unix_tid != -1)
- {
- cpu_set_t set;
- int i;
- affinity_t mask;
- CPU_ZERO( &set );
- for (i = 0, mask = 1; mask; i++, mask <<= 1)
- if (affinity & mask) CPU_SET( i, &set );
- ret = sched_setaffinity( thread->unix_tid, sizeof(set), &set );
- }
- #endif
- if (!ret) thread->affinity = affinity;
- return ret;
- }
- affinity_t get_thread_affinity( struct thread *thread )
- {
- affinity_t mask = 0;
- #ifdef HAVE_SCHED_SETAFFINITY
- if (thread->unix_tid != -1)
- {
- cpu_set_t set;
- unsigned int i;
- if (!sched_getaffinity( thread->unix_tid, sizeof(set), &set ))
- for (i = 0; i < 8 * sizeof(mask); i++)
- if (CPU_ISSET( i, &set )) mask |= (affinity_t)1 << i;
- }
- #endif
- if (!mask) mask = ~(affinity_t)0;
- return mask;
- }
- #define THREAD_PRIORITY_REALTIME_HIGHEST 6
- #define THREAD_PRIORITY_REALTIME_LOWEST -7
- /* set all information about a thread */
- static void set_thread_info( struct thread *thread,
- const struct set_thread_info_request *req )
- {
- if (req->mask & SET_THREAD_INFO_PRIORITY)
- {
- int max = THREAD_PRIORITY_HIGHEST;
- int min = THREAD_PRIORITY_LOWEST;
- if (thread->process->priority == PROCESS_PRIOCLASS_REALTIME)
- {
- max = THREAD_PRIORITY_REALTIME_HIGHEST;
- min = THREAD_PRIORITY_REALTIME_LOWEST;
- }
- if ((req->priority >= min && req->priority <= max) ||
- req->priority == THREAD_PRIORITY_IDLE ||
- req->priority == THREAD_PRIORITY_TIME_CRITICAL)
- {
- thread->priority = req->priority;
- set_scheduler_priority( thread );
- }
- else
- set_error( STATUS_INVALID_PARAMETER );
- }
- if (req->mask & SET_THREAD_INFO_AFFINITY)
- {
- if ((req->affinity & thread->process->affinity) != req->affinity)
- set_error( STATUS_INVALID_PARAMETER );
- else if (thread->state == TERMINATED)
- set_error( STATUS_THREAD_IS_TERMINATING );
- else if (set_thread_affinity( thread, req->affinity ))
- file_set_error();
- }
- if (req->mask & SET_THREAD_INFO_TOKEN)
- security_set_thread_token( thread, req->token );
- if (req->mask & SET_THREAD_INFO_ENTRYPOINT)
- thread->entry_point = req->entry_point;
- if (req->mask & SET_THREAD_INFO_DESCRIPTION)
- {
- WCHAR *desc;
- data_size_t desc_len = get_req_data_size();
- if (desc_len)
- {
- if ((desc = mem_alloc( desc_len )))
- {
- memcpy( desc, get_req_data(), desc_len );
- free( thread->desc );
- thread->desc = desc;
- thread->desc_len = desc_len;
- }
- }
- else
- {
- free( thread->desc );
- thread->desc = NULL;
- thread->desc_len = 0;
- }
- }
- }
- /* stop a thread (at the Unix level) */
- void stop_thread( struct thread *thread )
- {
- if (thread->context) return; /* already inside a debug event, no need for a signal */
- /* can't stop a thread while initialisation is in progress */
- if (is_process_init_done(thread->process)) send_thread_signal( thread, SIGUSR1 );
- }
- /* stop a thread if it's supposed to be suspended */
- void stop_thread_if_suspended( struct thread *thread )
- {
- if (thread->suspend + thread->process->suspend > 0) stop_thread( thread );
- }
- /* suspend a thread */
- int suspend_thread( struct thread *thread )
- {
- int old_count = thread->suspend;
- if (thread->suspend < MAXIMUM_SUSPEND_COUNT)
- {
- if (!(thread->process->suspend + thread->suspend++)) stop_thread( thread );
- }
- else set_error( STATUS_SUSPEND_COUNT_EXCEEDED );
- return old_count;
- }
- /* resume a thread */
- int resume_thread( struct thread *thread )
- {
- int old_count = thread->suspend;
- if (thread->suspend > 0)
- {
- if (!(--thread->suspend)) resume_delayed_debug_events( thread );
- if (!(thread->suspend + thread->process->suspend)) wake_thread( thread );
- }
- return old_count;
- }
- int is_thread( struct object *obj )
- {
- return obj->ops == &thread_ops;
- }
- /* add a thread to an object wait queue; return 1 if OK, 0 on error */
- int add_queue( struct object *obj, struct wait_queue_entry *entry )
- {
- grab_object( obj );
- entry->obj = obj;
- list_add_tail( &obj->wait_queue, &entry->entry );
- return 1;
- }
- /* remove a thread from an object wait queue */
- void remove_queue( struct object *obj, struct wait_queue_entry *entry )
- {
- list_remove( &entry->entry );
- release_object( obj );
- }
- struct thread *get_wait_queue_thread( struct wait_queue_entry *entry )
- {
- return entry->wait->thread;
- }
- enum select_op get_wait_queue_select_op( struct wait_queue_entry *entry )
- {
- return entry->wait->select;
- }
- client_ptr_t get_wait_queue_key( struct wait_queue_entry *entry )
- {
- return entry->wait->key;
- }
- void make_wait_abandoned( struct wait_queue_entry *entry )
- {
- entry->wait->abandoned = 1;
- }
- /* finish waiting */
- static unsigned int end_wait( struct thread *thread, unsigned int status )
- {
- struct thread_wait *wait = thread->wait;
- struct wait_queue_entry *entry;
- int i;
- assert( wait );
- thread->wait = wait->next;
- if (status < wait->count) /* wait satisfied, tell it to the objects */
- {
- if (wait->select == SELECT_WAIT_ALL)
- {
- for (i = 0, entry = wait->queues; i < wait->count; i++, entry++)
- entry->obj->ops->satisfied( entry->obj, entry );
- }
- else
- {
- entry = wait->queues + status;
- entry->obj->ops->satisfied( entry->obj, entry );
- }
- if (wait->abandoned) status += STATUS_ABANDONED_WAIT_0;
- }
- for (i = 0, entry = wait->queues; i < wait->count; i++, entry++)
- entry->obj->ops->remove_queue( entry->obj, entry );
- if (wait->user) remove_timeout_user( wait->user );
- free( wait );
- return status;
- }
- /* build the thread wait structure */
- static int wait_on( const select_op_t *select_op, unsigned int count, struct object *objects[],
- int flags, timeout_t timeout )
- {
- struct thread_wait *wait;
- struct wait_queue_entry *entry;
- unsigned int i;
- if (!(wait = mem_alloc( FIELD_OFFSET(struct thread_wait, queues[count]) ))) return 0;
- wait->next = current->wait;
- wait->thread = current;
- wait->count = count;
- wait->flags = flags;
- wait->select = select_op->op;
- wait->cookie = 0;
- wait->user = NULL;
- wait->timeout = timeout;
- wait->abandoned = 0;
- wait->in_kernel = 0;
- current->wait = wait;
- for (i = 0, entry = wait->queues; i < count; i++, entry++)
- {
- struct object *obj = objects[i];
- entry->wait = wait;
- if (!obj->ops->add_queue( obj, entry ))
- {
- wait->count = i;
- end_wait( current, get_error() );
- return 0;
- }
- }
- return 1;
- }
- static int wait_on_handles( const select_op_t *select_op, unsigned int count, const obj_handle_t *handles,
- int flags, timeout_t timeout )
- {
- struct object *objects[MAXIMUM_WAIT_OBJECTS];
- unsigned int i;
- int ret = 0;
- assert( count <= MAXIMUM_WAIT_OBJECTS );
- for (i = 0; i < count; i++)
- if (!(objects[i] = get_handle_obj( current->process, handles[i], SYNCHRONIZE, NULL )))
- break;
- if (i == count) ret = wait_on( select_op, count, objects, flags, timeout );
- while (i > 0) release_object( objects[--i] );
- return ret;
- }
- /* check if the thread waiting condition is satisfied */
- static int check_wait( struct thread *thread )
- {
- int i;
- struct thread_wait *wait = thread->wait;
- struct wait_queue_entry *entry;
- assert( wait );
- if ((wait->flags & SELECT_INTERRUPTIBLE) && !list_empty( &thread->system_apc ))
- return STATUS_KERNEL_APC;
- if ((wait->flags & SELECT_INTERRUPTIBLE && !list_empty( &thread->kernel_apc )))
- {
- struct thread_apc *apc = LIST_ENTRY( list_head(&thread->kernel_apc), struct thread_apc, entry );
- assert(apc->call.type == APC_REAL_KERNEL);
- thread->wait->in_kernel = 1;
- apc->executed = 1;
- wake_up( &apc->obj, 0 );
- return -1;
- }
- /* Suspended threads may not acquire locks, but they can run system APCs */
- if (thread->process->suspend + thread->suspend > 0) return -1;
- if (wait->select == SELECT_WAIT_ALL)
- {
- int not_ok = 0;
- /* Note: we must check them all anyway, as some objects may
- * want to do something when signaled, even if others are not */
- for (i = 0, entry = wait->queues; i < wait->count; i++, entry++)
- not_ok |= !entry->obj->ops->signaled( entry->obj, entry );
- if (!not_ok) return STATUS_WAIT_0;
- }
- else
- {
- for (i = 0, entry = wait->queues; i < wait->count; i++, entry++)
- if (entry->obj->ops->signaled( entry->obj, entry )) return i;
- }
- if ((wait->flags & SELECT_ALERTABLE) && !list_empty(&thread->user_apc))
- {
- struct thread_apc *apc = LIST_ENTRY( list_head(&thread->user_apc), struct thread_apc, entry );
- if (apc->call.type != APC_REAL_USER)
- return STATUS_USER_APC;
- else
- {
- thread->wait->in_kernel = 1;
- apc->executed = 1;
- wake_up( &apc->obj, 0 );
- return -1;
- }
- }
- if (wait->timeout <= current_time) return STATUS_TIMEOUT;
- return -1;
- }
- /* send the wakeup signal to a thread */
- static int send_thread_wakeup( struct thread *thread, client_ptr_t cookie, int signaled )
- {
- struct wake_up_reply reply;
- int ret;
- memset( &reply, 0, sizeof(reply) );
- reply.cookie = cookie;
- reply.signaled = signaled;
- if ((ret = write( get_unix_fd( thread->wait_fd ), &reply, sizeof(reply) )) == sizeof(reply))
- return 0;
- if (ret >= 0)
- fatal_protocol_error( thread, "partial wakeup write %d\n", ret );
- else if (errno == EPIPE)
- kill_thread( thread, 0 ); /* normal death */
- else
- fatal_protocol_error( thread, "write: %s\n", strerror( errno ));
- return -1;
- }
- /* attempt to wake up a thread */
- /* return >0 if OK, 0 if the wait condition is still not satisfied and -1 on error */
- int wake_thread( struct thread *thread )
- {
- int signaled, count;
- client_ptr_t cookie;
- for (count = 0; thread->wait; count++)
- {
- if ((signaled = check_wait( thread )) == -1) break;
- cookie = thread->wait->cookie;
- signaled = end_wait( thread, signaled );
- if (debug_level) fprintf( stderr, "%04x: *wakeup* signaled=%d\n", thread->id, signaled );
- if (cookie && send_thread_wakeup( thread, cookie, signaled ) == -1) /* error */
- {
- if (!count) count = -1;
- break;
- }
- }
- return count;
- }
- /* attempt to wake up a thread from a wait queue entry, assuming that it is signaled */
- int wake_thread_queue_entry( struct wait_queue_entry *entry )
- {
- struct thread_wait *wait = entry->wait;
- struct thread *thread = wait->thread;
- int signaled;
- client_ptr_t cookie;
- if (thread->wait != wait) return 0; /* not the current wait */
- if (thread->process->suspend + thread->suspend > 0) return 0; /* cannot acquire locks */
- assert( wait->select != SELECT_WAIT_ALL );
- cookie = wait->cookie;
- signaled = end_wait( thread, entry - wait->queues );
- if (debug_level) fprintf( stderr, "%04x: *wakeup* signaled=%d\n", thread->id, signaled );
- if (!cookie || send_thread_wakeup( thread, cookie, signaled ) != -1)
- wake_thread( thread ); /* check other waits too */
- return 1;
- }
- /* thread wait timeout */
- static void thread_timeout( void *ptr )
- {
- struct thread_wait *wait = ptr;
- struct thread *thread = wait->thread;
- client_ptr_t cookie = wait->cookie;
- wait->user = NULL;
- if (thread->wait != wait) return; /* not the top-level wait, ignore it */
- if (thread->suspend + thread->process->suspend > 0) return; /* suspended, ignore it */
- if (debug_level) fprintf( stderr, "%04x: *wakeup* signaled=TIMEOUT\n", thread->id );
- end_wait( thread, STATUS_TIMEOUT );
- assert( cookie );
- if (send_thread_wakeup( thread, cookie, STATUS_TIMEOUT ) == -1) return;
- /* check if other objects have become signaled in the meantime */
- wake_thread( thread );
- }
- /* try signaling an event flag, a semaphore or a mutex */
- static int signal_object( obj_handle_t handle )
- {
- struct object *obj;
- int ret = 0;
- obj = get_handle_obj( current->process, handle, 0, NULL );
- if (obj)
- {
- ret = obj->ops->signal( obj, get_handle_access( current->process, handle ));
- release_object( obj );
- }
- return ret;
- }
- /* select on a list of handles */
- static timeout_t select_on( const select_op_t *select_op, data_size_t op_size, client_ptr_t cookie,
- int flags, timeout_t timeout )
- {
- int ret;
- unsigned int count;
- struct object *object;
- if (timeout <= 0) timeout = current_time - timeout;
- switch (select_op->op)
- {
- case SELECT_NONE:
- if (!wait_on( select_op, 0, NULL, flags, timeout )) return timeout;
- break;
- case SELECT_WAIT:
- case SELECT_WAIT_ALL:
- count = (op_size - offsetof( select_op_t, wait.handles )) / sizeof(select_op->wait.handles[0]);
- if (op_size < offsetof( select_op_t, wait.handles ) || count > MAXIMUM_WAIT_OBJECTS)
- {
- set_error( STATUS_INVALID_PARAMETER );
- return 0;
- }
- if (!wait_on_handles( select_op, count, select_op->wait.handles, flags, timeout ))
- return timeout;
- break;
- case SELECT_SIGNAL_AND_WAIT:
- if (!wait_on_handles( select_op, 1, &select_op->signal_and_wait.wait, flags, timeout ))
- return timeout;
- if (select_op->signal_and_wait.signal)
- {
- if (!signal_object( select_op->signal_and_wait.signal ))
- {
- end_wait( current, get_error() );
- return timeout;
- }
- /* check if we woke ourselves up */
- if (!current->wait) return timeout;
- }
- break;
- case SELECT_KEYED_EVENT_WAIT:
- case SELECT_KEYED_EVENT_RELEASE:
- object = (struct object *)get_keyed_event_obj( current->process, select_op->keyed_event.handle,
- select_op->op == SELECT_KEYED_EVENT_WAIT ? KEYEDEVENT_WAIT : KEYEDEVENT_WAKE );
- if (!object) return timeout;
- ret = wait_on( select_op, 1, &object, flags, timeout );
- release_object( object );
- if (!ret) return timeout;
- current->wait->key = select_op->keyed_event.key;
- break;
- default:
- set_error( STATUS_INVALID_PARAMETER );
- return 0;
- }
- if ((ret = check_wait( current )) != -1)
- {
- /* condition is already satisfied */
- set_error( end_wait( current, ret ));
- return timeout;
- }
- /* now we need to wait */
- if (current->wait->timeout != TIMEOUT_INFINITE && !current->wait->in_kernel)
- {
- if (!(current->wait->user = add_timeout_user( current->wait->timeout,
- thread_timeout, current->wait )))
- {
- end_wait( current, get_error() );
- return timeout;
- }
- }
- current->wait->cookie = cookie;
- set_error( STATUS_PENDING );
- return timeout;
- }
- /* attempt to wake threads sleeping on the object wait queue */
- void wake_up( struct object *obj, int max )
- {
- struct list *ptr;
- int ret;
- if (do_esync())
- esync_wake_up( obj );
- LIST_FOR_EACH( ptr, &obj->wait_queue )
- {
- struct wait_queue_entry *entry = LIST_ENTRY( ptr, struct wait_queue_entry, entry );
- if (!(ret = wake_thread( get_wait_queue_thread( entry )))) continue;
- if (ret > 0 && max && !--max) break;
- /* restart at the head of the list since a wake up can change the object wait queue */
- ptr = &obj->wait_queue;
- }
- }
- /* return the apc queue to use for a given apc type */
- static inline struct list *get_apc_queue( struct thread *thread, enum apc_type type )
- {
- switch(type)
- {
- case APC_NONE:
- case APC_USER:
- case APC_TIMER:
- case APC_REAL_USER:
- return &thread->user_apc;
- case APC_REAL_KERNEL:
- return &thread->kernel_apc;
- default:
- return &thread->system_apc;
- }
- }
- /* check if thread is currently waiting for a (system) apc */
- static inline int is_in_apc_wait( struct thread *thread )
- {
- return (thread->process->suspend || thread->suspend ||
- (thread->wait && (thread->wait->flags & SELECT_INTERRUPTIBLE)));
- }
- /* queue an existing APC to a given thread */
- static int queue_apc( struct process *process, struct thread *thread, struct thread_apc *apc )
- {
- struct list *queue;
- if (thread && thread->state == TERMINATED && process)
- thread = NULL;
- if (!thread) /* find a suitable thread inside the process */
- {
- struct thread *candidate;
- /* first try to find a waiting thread */
- LIST_FOR_EACH_ENTRY( candidate, &process->thread_list, struct thread, proc_entry )
- {
- if (candidate->state == TERMINATED) continue;
- if (is_in_apc_wait( candidate ))
- {
- thread = candidate;
- break;
- }
- }
- if (!thread)
- {
- /* then use the first one that accepts a signal */
- LIST_FOR_EACH_ENTRY( candidate, &process->thread_list, struct thread, proc_entry )
- {
- if (send_thread_signal( candidate, SIGUSR1 ))
- {
- thread = candidate;
- break;
- }
- }
- }
- if (!thread) return 0; /* nothing found */
- queue = get_apc_queue( thread, apc->call.type );
- }
- else
- {
- if (thread->state == TERMINATED) return 0;
- queue = get_apc_queue( thread, apc->call.type );
- /* send signal for system APCs if needed */
- if ((queue == &thread->system_apc || queue == &thread->kernel_apc) && list_empty( queue ) && !is_in_apc_wait( thread ))
- {
- if (!send_thread_signal( thread, SIGUSR1 )) return 0;
- }
- /* cancel a possible previous APC with the same owner */
- if (apc->owner) thread_cancel_apc( thread, apc->owner, apc->call.type );
- }
- apc->callee = (struct thread *) grab_object( thread );
- grab_object( apc );
- list_add_tail( queue, &apc->entry );
- if (!list_prev( queue, &apc->entry )) /* first one */
- {
- wake_thread( thread );
- if (do_esync() && queue == &thread->user_apc)
- esync_wake_fd( thread->esync_apc_fd );
- }
- return 1;
- }
- /* queue an async procedure call */
- int thread_queue_apc( struct process *process, struct thread *thread, struct object *owner, const apc_call_t *call_data )
- {
- struct thread_apc *apc;
- int ret = 0;
- if ((apc = create_apc( owner, call_data )))
- {
- ret = queue_apc( process, thread, apc );
- release_object( apc );
- }
- return ret;
- }
- /* cancel the async procedure call owned by a specific object */
- void thread_cancel_apc( struct thread *thread, struct object *owner, enum apc_type type )
- {
- struct thread_apc *apc;
- struct list *queue = get_apc_queue( thread, type );
- LIST_FOR_EACH_ENTRY( apc, queue, struct thread_apc, entry )
- {
- if (apc->owner != owner) continue;
- list_remove( &apc->entry );
- apc->executed = 1;
- wake_up( &apc->obj, 0 );
- release_object( apc );
- return;
- }
- }
- /* remove the head apc from the queue; the returned object must be released by the caller */
- static struct thread_apc *thread_dequeue_apc( struct thread *thread, int system )
- {
- struct thread_apc *apc = NULL;
- struct list *ptr = list_head( system ? &thread->system_apc : &thread->user_apc );
- if (ptr)
- {
- apc = LIST_ENTRY( ptr, struct thread_apc, entry );
- list_remove( ptr );
- }
- if (do_esync() && list_empty( &thread->system_apc ) && list_empty( &thread->user_apc ))
- esync_clear( thread->esync_apc_fd );
- return apc;
- }
- /* clear an APC queue, cancelling all the APCs on it */
- static void clear_apc_queue( struct list *queue )
- {
- struct list *ptr;
- while ((ptr = list_head( queue )))
- {
- struct thread_apc *apc = LIST_ENTRY( ptr, struct thread_apc, entry );
- list_remove( &apc->entry );
- apc->executed = 1;
- wake_up( &apc->obj, 0 );
- release_object( apc );
- }
- }
- /* add an fd to the inflight list */
- /* return list index, or -1 on error */
- int thread_add_inflight_fd( struct thread *thread, int client, int server )
- {
- int i;
- if (server == -1) return -1;
- if (client == -1)
- {
- close( server );
- return -1;
- }
- /* first check if we already have an entry for this fd */
- for (i = 0; i < MAX_INFLIGHT_FDS; i++)
- if (thread->inflight[i].client == client)
- {
- close( thread->inflight[i].server );
- thread->inflight[i].server = server;
- return i;
- }
- /* now find a free spot to store it */
- for (i = 0; i < MAX_INFLIGHT_FDS; i++)
- if (thread->inflight[i].client == -1)
- {
- thread->inflight[i].client = client;
- thread->inflight[i].server = server;
- return i;
- }
- close( server );
- return -1;
- }
- /* get an inflight fd and purge it from the list */
- /* the fd must be closed when no longer used */
- int thread_get_inflight_fd( struct thread *thread, int client )
- {
- int i, ret;
- if (client == -1) return -1;
- do
- {
- for (i = 0; i < MAX_INFLIGHT_FDS; i++)
- {
- if (thread->inflight[i].client == client)
- {
- ret = thread->inflight[i].server;
- thread->inflight[i].server = thread->inflight[i].client = -1;
- return ret;
- }
- }
- } while (!receive_fd( thread->process )); /* in case it is still in the socket buffer */
- return -1;
- }
- static void check_terminated( void *arg )
- {
- struct thread *thread = arg;
- assert( thread->obj.ops == &thread_ops );
- assert( thread->state == TERMINATED );
- /* don't wake up until the thread is really dead, to avoid race conditions */
- if (thread->unix_tid != -1 && !kill( thread->unix_tid, 0 ))
- {
- thread->exit_poll = add_timeout_user( -TICKS_PER_SEC / 1000, check_terminated, thread );
- return;
- }
- /* grab reference since object can be destroyed while trying to wake up */
- grab_object( &thread->obj );
- thread->exit_poll = NULL;
- wake_up( &thread->obj, 0 );
- release_object( &thread->obj );
- }
- /* kill a thread on the spot */
- void kill_thread( struct thread *thread, int violent_death )
- {
- if (thread->state == TERMINATED) return; /* already killed */
- thread->state = TERMINATED;
- thread->exit_time = current_time;
- if (current == thread) current = NULL;
- if (debug_level)
- fprintf( stderr,"%04x: *killed* exit_code=%d\n",
- thread->id, thread->exit_code );
- if (thread->wait)
- {
- while (thread->wait) end_wait( thread, STATUS_THREAD_IS_TERMINATING );
- send_thread_wakeup( thread, 0, thread->exit_code );
- /* if it is waiting on the socket, we don't need to send a SIGQUIT */
- violent_death = 0;
- }
- kill_console_processes( thread, 0 );
- debug_exit_thread( thread );
- abandon_mutexes( thread );
- if (do_esync())
- esync_abandon_mutexes( thread );
- if (violent_death)
- {
- send_thread_signal( thread, SIGQUIT );
- check_terminated( thread );
- }
- else wake_up( &thread->obj, 0 );
- cleanup_thread( thread );
- remove_process_thread( thread->process, thread );
- release_object( thread );
- }
- /* copy parts of a context structure */
- static void copy_context( context_t *to, const context_t *from, unsigned int flags )
- {
- assert( to->cpu == from->cpu );
- to->flags |= flags;
- if (flags & SERVER_CTX_CONTROL) to->ctl = from->ctl;
- if (flags & SERVER_CTX_INTEGER) to->integer = from->integer;
- if (flags & SERVER_CTX_SEGMENTS) to->seg = from->seg;
- if (flags & SERVER_CTX_FLOATING_POINT) to->fp = from->fp;
- if (flags & SERVER_CTX_DEBUG_REGISTERS) to->debug = from->debug;
- if (flags & SERVER_CTX_EXTENDED_REGISTERS) to->ext = from->ext;
- }
- /* return the context flags that correspond to system regs */
- /* (system regs are the ones we can't access on the client side) */
- static unsigned int get_context_system_regs( enum cpu_type cpu )
- {
- switch (cpu)
- {
- case CPU_x86: return SERVER_CTX_DEBUG_REGISTERS;
- case CPU_x86_64: return SERVER_CTX_DEBUG_REGISTERS;
- case CPU_POWERPC: return 0;
- case CPU_ARM: return SERVER_CTX_DEBUG_REGISTERS;
- case CPU_ARM64: return SERVER_CTX_DEBUG_REGISTERS;
- }
- return 0;
- }
- /* take a snapshot of currently running threads */
- struct thread_snapshot *thread_snap( int *count )
- {
- struct thread_snapshot *snapshot, *ptr;
- struct thread *thread;
- int total = 0;
- LIST_FOR_EACH_ENTRY( thread, &thread_list, struct thread, entry )
- if (thread->state != TERMINATED) total++;
- if (!total || !(snapshot = mem_alloc( sizeof(*snapshot) * total ))) return NULL;
- ptr = snapshot;
- LIST_FOR_EACH_ENTRY( thread, &thread_list, struct thread, entry )
- {
- if (thread->state == TERMINATED) continue;
- ptr->thread = thread;
- ptr->count = thread->obj.refcount;
- ptr->priority = thread->priority;
- grab_object( thread );
- ptr++;
- }
- *count = total;
- return snapshot;
- }
- /* gets the current impersonation token */
- struct token *thread_get_impersonation_token( struct thread *thread )
- {
- if (thread->token)
- return thread->token;
- else
- return thread->process->token;
- }
- /* check if a cpu type can be supported on this server */
- int is_cpu_supported( enum cpu_type cpu )
- {
- unsigned int prefix_cpu_mask = get_prefix_cpu_mask();
- if (supported_cpus & prefix_cpu_mask & CPU_FLAG(cpu)) return 1;
- if (!(supported_cpus & prefix_cpu_mask))
- set_error( STATUS_NOT_SUPPORTED );
- else if (supported_cpus & CPU_FLAG(cpu))
- set_error( STATUS_INVALID_IMAGE_WIN_64 ); /* server supports it but not the prefix */
- else
- set_error( STATUS_INVALID_IMAGE_FORMAT );
- return 0;
- }
- /* return the cpu mask for supported cpus */
- unsigned int get_supported_cpu_mask(void)
- {
- return supported_cpus & get_prefix_cpu_mask();
- }
- /* create a new thread */
- DECL_HANDLER(new_thread)
- {
- struct thread *thread;
- struct process *process;
- struct unicode_str name;
- const struct security_descriptor *sd;
- const struct object_attributes *objattr = get_req_object_attributes( &sd, &name, NULL );
- int request_fd = thread_get_inflight_fd( current, req->request_fd );
- if (!(process = get_process_from_handle( req->process, PROCESS_CREATE_THREAD )))
- {
- if (request_fd != -1) close( request_fd );
- return;
- }
- if (process != current->process)
- {
- if (request_fd != -1) /* can't create a request fd in a different process */
- {
- close( request_fd );
- set_error( STATUS_INVALID_PARAMETER );
- goto done;
- }
- if (process->running_threads) /* only the initial thread can be created in another process */
- {
- set_error( STATUS_ACCESS_DENIED );
- goto done;
- }
- }
- else if (request_fd == -1 || fcntl( request_fd, F_SETFL, O_NONBLOCK ) == -1)
- {
- if (request_fd != -1) close( request_fd );
- set_error( STATUS_INVALID_HANDLE );
- goto done;
- }
- if ((thread = create_thread( request_fd, process, sd )))
- {
- thread->system_regs = current->system_regs;
- if (req->suspend) thread->suspend++;
- reply->tid = get_thread_id( thread );
- if ((reply->handle = alloc_handle_no_access_check( current->process, thread,
- req->access, objattr->attributes )))
- {
- /* thread object will be released when the thread gets killed */
- goto done;
- }
- kill_thread( thread, 1 );
- }
- done:
- release_object( process );
- }
- /* initialize a new thread */
- DECL_HANDLER(init_thread)
- {
- struct process *process = current->process;
- int wait_fd, reply_fd;
- if ((reply_fd = thread_get_inflight_fd( current, req->reply_fd )) == -1)
- {
- set_error( STATUS_TOO_MANY_OPENED_FILES );
- return;
- }
- if ((wait_fd = thread_get_inflight_fd( current, req->wait_fd )) == -1)
- {
- set_error( STATUS_TOO_MANY_OPENED_FILES );
- goto error;
- }
- if (current->reply_fd) /* already initialised */
- {
- set_error( STATUS_INVALID_PARAMETER );
- goto error;
- }
- if (fcntl( reply_fd, F_SETFL, O_NONBLOCK ) == -1) goto error;
- current->reply_fd = create_anonymous_fd( &thread_fd_ops, reply_fd, ¤t->obj, 0 );
- current->wait_fd = create_anonymous_fd( &thread_fd_ops, wait_fd, ¤t->obj, 0 );
- if (!current->reply_fd || !current->wait_fd) return;
- if (!is_valid_address(req->teb))
- {
- set_error( STATUS_INVALID_PARAMETER );
- return;
- }
- current->unix_pid = req->unix_pid;
- current->unix_tid = req->unix_tid;
- current->teb = req->teb;
- current->entry_point = process->peb ? req->entry : 0;
- if (!process->peb) /* first thread, initialize the process too */
- {
- if (!is_cpu_supported( req->cpu )) return;
- process->unix_pid = current->unix_pid;
- process->peb = req->entry;
- process->cpu = req->cpu;
- reply->info_size = init_process( current );
- if (!process->parent_id)
- process->affinity = current->affinity = get_thread_affinity( current );
- else
- set_thread_affinity( current, current->affinity );
- }
- else
- {
- if (req->cpu != process->cpu)
- {
- set_error( STATUS_INVALID_PARAMETER );
- return;
- }
- if (process->unix_pid != current->unix_pid)
- process->unix_pid = -1; /* can happen with linuxthreads */
- init_thread_context( current );
- generate_debug_event( current, CREATE_THREAD_DEBUG_EVENT, &req->entry );
- set_thread_affinity( current, current->affinity );
- }
- debug_level = max( debug_level, req->debug_level );
- if (current->startup_info)
- {
- wake_up( ¤t->startup_info->obj, 0 );
- release_object( current->startup_info );
- current->startup_info = NULL;
- }
- if (current->callback_init_event)
- {
- reply->processed_event = alloc_handle(current->process, current->callback_init_event, SYNCHRONIZE, 0);
- release_object(current->callback_init_event);
- current->callback_init_event = NULL;
- }
- reply->pid = get_process_id( process );
- reply->tid = get_thread_id( current );
- reply->version = SERVER_PROTOCOL_VERSION;
- reply->server_start = server_start_time;
- reply->all_cpus = supported_cpus & get_prefix_cpu_mask();
- reply->suspend = (current->suspend || process->suspend);
- return;
- error:
- if (reply_fd != -1) close( reply_fd );
- if (wait_fd != -1) close( wait_fd );
- }
- /* terminate a thread */
- DECL_HANDLER(terminate_thread)
- {
- struct thread *thread;
- reply->self = 0;
- reply->last = 0;
- if ((thread = get_thread_from_handle( req->handle, THREAD_TERMINATE )))
- {
- thread->exit_code = req->exit_code;
- if (thread != current) kill_thread( thread, 1 );
- else
- {
- reply->self = 1;
- reply->last = (thread->process->running_threads == 1);
- }
- release_object( thread );
- }
- }
- /* open a handle to a thread */
- DECL_HANDLER(open_thread)
- {
- struct thread *thread = get_thread_from_id( req->tid );
- reply->handle = 0;
- if (thread)
- {
- reply->handle = alloc_handle( current->process, thread, req->access, req->attributes );
- release_object( thread );
- }
- }
- DECL_HANDLER(wait_thread_init)
- {
- struct thread *thread;
- if ((thread = get_thread_from_handle(req->thread, 0)))
- {
- reply->thread_state = thread->unix_pid == -1 ? THREAD_STARTING : THREAD_RUNNING;
- if (reply->thread_state == THREAD_STARTING)
- {
- reply->init_event = alloc_handle(current->process, thread->startup_info, SYNCHRONIZE, 0);
- }
- release_object(thread);
- }
- }
- /* fetch information about a thread */
- DECL_HANDLER(get_thread_info)
- {
- struct thread *thread;
- obj_handle_t handle = req->handle;
- if (!handle) thread = get_thread_from_id( req->tid_in );
- else thread = get_thread_from_handle( req->handle, THREAD_QUERY_LIMITED_INFORMATION );
- if (thread)
- {
- reply->pid = get_process_id( thread->process );
- reply->tid = get_thread_id( thread );
- reply->teb = thread->teb;
- reply->entry_point = thread->entry_point;
- reply->exit_code = (thread->state == TERMINATED) ? thread->exit_code : STATUS_PENDING;
- reply->priority = thread->priority;
- reply->affinity = thread->affinity;
- reply->last = thread->process->running_threads == 1;
- reply->suspend_count = thread->suspend;
- reply->desc_len = thread->desc_len;
- if (thread->desc && get_reply_max_size())
- {
- if (thread->desc_len <= get_reply_max_size())
- set_reply_data( thread->desc, thread->desc_len );
- else
- set_error( STATUS_BUFFER_TOO_SMALL );
- }
- release_object( thread );
- }
- }
- /* fetch information about thread times */
- DECL_HANDLER(get_thread_times)
- {
- struct thread *thread;
- if ((thread = get_thread_from_handle( req->handle, THREAD_QUERY_INFORMATION )))
- {
- reply->creation_time = thread->creation_time;
- reply->exit_time = thread->exit_time;
- reply->unix_pid = thread->unix_pid;
- reply->unix_tid = thread->unix_tid;
- release_object( thread );
- }
- }
- /* set information about a thread */
- DECL_HANDLER(set_thread_info)
- {
- struct thread *thread;
- if ((thread = get_thread_from_handle( req->handle, THREAD_SET_INFORMATION )))
- {
- set_thread_info( thread, req );
- release_object( thread );
- }
- }
- /* suspend a thread */
- DECL_HANDLER(suspend_thread)
- {
- struct thread *thread;
- if ((thread = get_thread_from_handle( req->handle, THREAD_SUSPEND_RESUME )))
- {
- if (thread->state == TERMINATED) set_error( STATUS_ACCESS_DENIED );
- else reply->count = suspend_thread( thread );
- release_object( thread );
- }
- }
- /* resume a thread */
- DECL_HANDLER(resume_thread)
- {
- struct thread *thread;
- if ((thread = get_thread_from_handle( req->handle, THREAD_SUSPEND_RESUME )))
- {
- reply->count = resume_thread( thread );
- release_object( thread );
- }
- }
- /* select on a handle list */
- DECL_HANDLER(select)
- {
- select_op_t select_op;
- data_size_t op_size;
- struct thread_apc *apc;
- const apc_result_t *result = get_req_data();
- if (get_req_data_size() < sizeof(*result))
- {
- set_error( STATUS_INVALID_PARAMETER );
- return;
- }
- if (!req->cookie)
- {
- set_error( STATUS_INVALID_PARAMETER );
- return;
- }
- op_size = min( get_req_data_size() - sizeof(*result), sizeof(select_op) );
- memset( &select_op, 0, sizeof(select_op) );
- memcpy( &select_op, result + 1, op_size );
- /* first store results of previous apc */
- if (req->prev_apc)
- {
- if (!(apc = (struct thread_apc *)get_handle_obj( current->process, req->prev_apc,
- 0, &thread_apc_ops ))) return;
- apc->result = *result;
- apc->executed = 1;
- if (apc->result.type == APC_CREATE_THREAD) /* transfer the handle to the caller process */
- {
- obj_handle_t handle = duplicate_handle( current->process, apc->result.create_thread.handle,
- apc->caller->process, 0, 0, DUP_HANDLE_SAME_ACCESS );
- close_handle( current->process, apc->result.create_thread.handle );
- apc->result.create_thread.handle = handle;
- clear_error(); /* ignore errors from the above calls */
- }
- else if (apc->result.type == APC_ASYNC_IO)
- {
- if (apc->owner)
- async_set_result( apc->owner, apc->result.async_io.status, apc->result.async_io.total );
- }
- wake_up( &apc->obj, 0 );
- close_handle( current->process, req->prev_apc );
- release_object( apc );
- }
- reply->timeout = select_on( &select_op, op_size, req->cookie, req->flags, req->timeout );
- while (get_error() == STATUS_USER_APC)
- {
- if (!(apc = thread_dequeue_apc( current, 0 )))
- break;
- /* Optimization: ignore APC_NONE calls, they are only used to
- * wake up a thread, but since we got here the thread woke up already.
- */
- if (apc->call.type != APC_NONE &&
- (reply->apc_handle = alloc_handle( current->process, apc, SYNCHRONIZE, 0 )))
- {
- reply->call = apc->call;
- release_object( apc );
- break;
- }
- apc->executed = 1;
- wake_up( &apc->obj, 0 );
- release_object( apc );
- }
- if (get_error() == STATUS_KERNEL_APC)
- {
- apc = thread_dequeue_apc( current, 1 );
- if ((reply->apc_handle = alloc_handle( current->process, apc, SYNCHRONIZE, 0 )))
- reply->call = apc->call;
- else
- {
- apc->executed = 1;
- wake_up( &apc->obj, 0 );
- }
- release_object( apc );
- }
- }
- /* queue an APC for a thread or process */
- DECL_HANDLER(queue_apc)
- {
- struct thread *thread = NULL;
- struct process *process = NULL;
- struct thread_apc *apc;
- obj_handle_t apc_handle = 0;
- if (!(apc = create_apc( NULL, &req->call ))) return;
- switch (apc->call.type)
- {
- case APC_NONE:
- case APC_USER:
- case APC_REAL_USER:
- case APC_REAL_KERNEL:
- thread = get_thread_from_handle( req->handle, THREAD_SET_CONTEXT );
- break;
- case APC_VIRTUAL_ALLOC:
- case APC_VIRTUAL_FREE:
- case APC_VIRTUAL_PROTECT:
- case APC_VIRTUAL_FLUSH:
- case APC_VIRTUAL_LOCK:
- case APC_VIRTUAL_UNLOCK:
- case APC_UNMAP_VIEW:
- process = get_process_from_handle( req->handle, PROCESS_VM_OPERATION );
- break;
- case APC_VIRTUAL_QUERY:
- process = get_process_from_handle( req->handle, PROCESS_QUERY_INFORMATION );
- break;
- case APC_MAP_VIEW:
- process = get_process_from_handle( req->handle, PROCESS_VM_OPERATION );
- if (process && process != current->process)
- {
- /* duplicate the handle into the target process */
- obj_handle_t handle = duplicate_handle( current->process, apc->call.map_view.handle,
- process, 0, 0, DUP_HANDLE_SAME_ACCESS );
- if (handle) apc->call.map_view.handle = handle;
- else
- {
- release_object( process );
- process = NULL;
- }
- }
- break;
- case APC_CREATE_THREAD:
- case APC_BREAK_PROCESS:
- process = get_process_from_handle( req->handle, PROCESS_CREATE_THREAD );
- break;
- default:
- set_error( STATUS_INVALID_PARAMETER );
- break;
- }
- if (!thread && !process)
- {
- release_object(apc);
- return;
- }
- apc_handle = alloc_handle( current->process, apc, SYNCHRONIZE, 0 );
- if (apc_handle)
- {
- if (process)
- reply->self = (process == current->process);
- if (queue_apc( process, thread, apc ))
- {
- apc->caller = (struct thread *)grab_object( current );
- reply->handle = apc_handle;
- }
- else
- {
- close_handle( current->process, apc_handle );
- set_error( thread ? STATUS_THREAD_IS_TERMINATING : STATUS_PROCESS_IS_TERMINATING );
- }
- release_object( thread ? (void*) thread : (void*) process );
- }
- /* Optimization: APC_USER and APC_NONE don't use the handle */
- if (apc->call.type == APC_USER || apc->call.type == APC_NONE)
- close_handle(current->process, apc_handle);
- release_object( apc );
- }
- /* transform the APC object into the type for usermode */
- DECL_HANDLER(finalize_apc)
- {
- struct thread_apc *apc;
- struct list *queue;
- if (!(apc = (struct thread_apc *)get_handle_obj(current->process, req->handle,
- 0, &thread_apc_ops ))) return;
- if (apc->executed != 1)
- {
- set_error(STATUS_INVALID_PARAMETER);
- goto done;
- }
- if (apc->call.type != APC_REAL_USER && apc->call.type != APC_REAL_KERNEL)
- {
- set_error(STATUS_INVALID_PARAMETER);
- goto done;
- }
- queue = get_apc_queue(apc->callee, apc->call.type);
- if (list_head(queue) != &apc->entry)
- {
- set_error(STATUS_INVALID_PARAMETER);
- goto done;
- }
- apc->executed = 0;
- if (apc->call.type == APC_REAL_KERNEL)
- {
- /* dequeue */
- list_remove(&apc->entry);
- release_object(apc);
- if (req->call.type != APC_NONE)
- set_error(STATUS_INVALID_PARAMETER);
- }
- else
- apc->call = req->call;
- if (apc->callee->wait)
- apc->callee->wait->in_kernel = 0;
- fprintf(stderr, "finalize APC wakeup %u\n", wake_thread( apc->callee ));
- if (do_esync() && queue == &apc->callee->user_apc)
- esync_wake_fd( apc->callee->esync_apc_fd );
- done:
- if (get_error() != 0)
- fprintf(stderr, "APC error %x\n", get_error());
- release_object( apc );
- }
- /* Get the result of an APC call */
- DECL_HANDLER(get_apc_result)
- {
- struct thread_apc *apc;
- if (!(apc = (struct thread_apc *)get_handle_obj( current->process, req->handle,
- 0, &thread_apc_ops ))) return;
- if (apc->executed) reply->result = apc->result;
- else set_error( STATUS_PENDING );
- /* close the handle directly to avoid an extra round-trip */
- close_handle( current->process, req->handle );
- release_object( apc );
- }
- /* retrieve the current context of a thread */
- DECL_HANDLER(get_thread_context)
- {
- struct thread *thread;
- context_t *context;
- if (get_reply_max_size() < sizeof(context_t))
- {
- set_error( STATUS_INVALID_PARAMETER );
- return;
- }
- if (!(thread = get_thread_from_handle( req->handle, THREAD_GET_CONTEXT ))) return;
- reply->self = (thread == current);
- if (thread != current && !thread->context)
- {
- /* thread is not suspended, retry (if it's still running) */
- if (thread->state == RUNNING)
- {
- set_error( STATUS_PENDING );
- if (req->suspend)
- {
- release_object( thread );
- /* make sure we have suspend access */
- if (!(thread = get_thread_from_handle( req->handle, THREAD_SUSPEND_RESUME ))) return;
- suspend_thread( thread );
- }
- }
- else set_error( STATUS_UNSUCCESSFUL );
- }
- else if ((context = set_reply_data_size( sizeof(context_t) )))
- {
- unsigned int flags = get_context_system_regs( thread->process->cpu );
- memset( context, 0, sizeof(context_t) );
- context->cpu = thread->process->cpu;
- if (thread->context) copy_context( context, thread->context, req->flags & ~flags );
- if (req->flags & flags) get_thread_context( thread, context, req->flags & flags );
- }
- release_object( thread );
- }
- /* set the current context of a thread */
- DECL_HANDLER(set_thread_context)
- {
- struct thread *thread;
- const context_t *context = get_req_data();
- if (get_req_data_size() < sizeof(context_t))
- {
- set_error( STATUS_INVALID_PARAMETER );
- return;
- }
- if (!(thread = get_thread_from_handle( req->handle, THREAD_SET_CONTEXT ))) return;
- reply->self = (thread == current);
- if (thread != current && !thread->context)
- {
- /* thread is not suspended, retry (if it's still running) */
- if (thread->state == RUNNING)
- {
- set_error( STATUS_PENDING );
- if (req->suspend)
- {
- release_object( thread );
- /* make sure we have suspend access */
- if (!(thread = get_thread_from_handle( req->handle, THREAD_SUSPEND_RESUME ))) return;
- suspend_thread( thread );
- }
- }
- else set_error( STATUS_UNSUCCESSFUL );
- }
- else if (context->cpu == thread->process->cpu)
- {
- unsigned int system_flags = get_context_system_regs(context->cpu) & context->flags;
- unsigned int client_flags = context->flags & ~system_flags;
- if (system_flags) set_thread_context( thread, context, system_flags );
- if (thread->context && !get_error()) copy_context( thread->context, context, client_flags );
- }
- else set_error( STATUS_INVALID_PARAMETER );
- release_object( thread );
- }
- /* retrieve the suspended context of a thread */
- DECL_HANDLER(get_suspend_context)
- {
- if (get_reply_max_size() < sizeof(context_t))
- {
- set_error( STATUS_INVALID_PARAMETER );
- return;
- }
- if (current->suspend_context)
- {
- if (current->suspend_context->flags)
- set_reply_data_ptr( current->suspend_context, sizeof(context_t) );
- else
- free( current->suspend_context );
- if (current->context == current->suspend_context)
- {
- current->context = NULL;
- stop_thread_if_suspended( current );
- }
- current->suspend_context = NULL;
- }
- else set_error( STATUS_INVALID_PARAMETER ); /* not suspended, shouldn't happen */
- }
- /* store the suspended context of a thread */
- DECL_HANDLER(set_suspend_context)
- {
- const context_t *context = get_req_data();
- if (get_req_data_size() < sizeof(context_t))
- {
- set_error( STATUS_INVALID_PARAMETER );
- return;
- }
- if (current->context || context->cpu != current->process->cpu)
- {
- /* nested suspend or exception, shouldn't happen */
- set_error( STATUS_INVALID_PARAMETER );
- }
- else if ((current->suspend_context = mem_alloc( sizeof(context_t) )))
- {
- memcpy( current->suspend_context, get_req_data(), sizeof(context_t) );
- current->suspend_context->flags = 0; /* to keep track of what is modified */
- current->context = current->suspend_context;
- }
- }
- /* fetch a selector entry for a thread */
- DECL_HANDLER(get_selector_entry)
- {
- struct thread *thread;
- if ((thread = get_thread_from_handle( req->handle, THREAD_QUERY_INFORMATION )))
- {
- get_selector_entry( thread, req->entry, &reply->base, &reply->limit, &reply->flags );
- release_object( thread );
- }
- }
|