Already initialize the process in the first init_thread request
[wine.git] / server / thread.c
blob4eedc918e35234de56c46d7edbad9112a8b71957
1 /*
2 * Server-side thread management
4 * Copyright (C) 1998 Alexandre Julliard
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.
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.
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include "config.h"
22 #include "wine/port.h"
24 #include <assert.h>
25 #include <errno.h>
26 #include <fcntl.h>
27 #include <signal.h>
28 #include <stdarg.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <sys/types.h>
33 #include <unistd.h>
34 #include <time.h>
35 #ifdef HAVE_POLL_H
36 #include <poll.h>
37 #endif
39 #include "windef.h"
41 #include "file.h"
42 #include "handle.h"
43 #include "process.h"
44 #include "thread.h"
45 #include "request.h"
46 #include "user.h"
47 #include "security.h"
50 /* thread queues */
52 struct thread_wait
54 struct thread_wait *next; /* next wait structure for this thread */
55 struct thread *thread; /* owner thread */
56 int count; /* count of objects */
57 int flags;
58 void *cookie; /* magic cookie to return to client */
59 struct timeval timeout;
60 struct timeout_user *user;
61 struct wait_queue_entry queues[1];
64 /* asynchronous procedure calls */
66 struct thread_apc
68 struct list entry; /* queue linked list */
69 struct object *owner; /* object that queued this apc */
70 void *func; /* function to call in client */
71 enum apc_type type; /* type of apc function */
72 int nb_args; /* number of arguments */
73 void *arg1; /* function arguments */
74 void *arg2;
75 void *arg3;
79 /* thread operations */
81 static void dump_thread( struct object *obj, int verbose );
82 static int thread_signaled( struct object *obj, struct thread *thread );
83 static void thread_poll_event( struct fd *fd, int event );
84 static void destroy_thread( struct object *obj );
85 static struct thread_apc *thread_dequeue_apc( struct thread *thread, int system_only );
87 static const struct object_ops thread_ops =
89 sizeof(struct thread), /* size */
90 dump_thread, /* dump */
91 add_queue, /* add_queue */
92 remove_queue, /* remove_queue */
93 thread_signaled, /* signaled */
94 no_satisfied, /* satisfied */
95 no_signal, /* signal */
96 no_get_fd, /* get_fd */
97 no_close_handle, /* close_handle */
98 destroy_thread /* destroy */
101 static const struct fd_ops thread_fd_ops =
103 NULL, /* get_poll_events */
104 thread_poll_event, /* poll_event */
105 no_flush, /* flush */
106 no_get_file_info, /* get_file_info */
107 no_queue_async, /* queue_async */
108 no_cancel_async /* cancel_async */
111 static struct list thread_list = LIST_INIT(thread_list);
112 static struct thread *booting_thread;
114 /* initialize the structure for a newly allocated thread */
115 inline static void init_thread_structure( struct thread *thread )
117 int i;
119 thread->unix_pid = -1; /* not known yet */
120 thread->unix_tid = -1; /* not known yet */
121 thread->context = NULL;
122 thread->teb = NULL;
123 thread->debug_ctx = NULL;
124 thread->debug_event = NULL;
125 thread->queue = NULL;
126 thread->wait = NULL;
127 thread->error = 0;
128 thread->req_data = NULL;
129 thread->req_toread = 0;
130 thread->reply_data = NULL;
131 thread->reply_towrite = 0;
132 thread->request_fd = NULL;
133 thread->reply_fd = NULL;
134 thread->wait_fd = NULL;
135 thread->state = RUNNING;
136 thread->attached = 0;
137 thread->exit_code = 0;
138 thread->priority = THREAD_PRIORITY_NORMAL;
139 thread->affinity = 1;
140 thread->suspend = 0;
141 thread->creation_time = time(NULL);
142 thread->exit_time = 0;
143 thread->desktop_users = 0;
145 list_init( &thread->mutex_list );
146 list_init( &thread->system_apc );
147 list_init( &thread->user_apc );
149 for (i = 0; i < MAX_INFLIGHT_FDS; i++)
150 thread->inflight[i].server = thread->inflight[i].client = -1;
153 /* check if address looks valid for a client-side data structure (TEB etc.) */
154 static inline int is_valid_address( void *addr )
156 return addr && !((unsigned int)addr % sizeof(int));
159 /* create a new thread */
160 struct thread *create_thread( int fd, struct process *process )
162 struct thread *thread;
164 if (!(thread = alloc_object( &thread_ops ))) return NULL;
166 init_thread_structure( thread );
168 thread->process = (struct process *)grab_object( process );
169 thread->desktop = process->desktop;
170 if (!current) current = thread;
172 if (!booting_thread) /* first thread ever */
174 booting_thread = thread;
175 lock_master_socket(1);
178 list_add_head( &thread_list, &thread->entry );
180 if (!(thread->id = alloc_ptid( thread )))
182 release_object( thread );
183 return NULL;
185 if (!(thread->request_fd = create_anonymous_fd( &thread_fd_ops, fd, &thread->obj )))
187 release_object( thread );
188 return NULL;
191 thread->token = (struct token *) grab_object( process->token );
193 set_fd_events( thread->request_fd, POLLIN ); /* start listening to events */
194 add_process_thread( thread->process, thread );
195 return thread;
198 /* handle a client event */
199 static void thread_poll_event( struct fd *fd, int event )
201 struct thread *thread = get_fd_user( fd );
202 assert( thread->obj.ops == &thread_ops );
204 if (event & (POLLERR | POLLHUP)) kill_thread( thread, 0 );
205 else if (event & POLLIN) read_request( thread );
206 else if (event & POLLOUT) write_reply( thread );
209 /* cleanup everything that is no longer needed by a dead thread */
210 /* used by destroy_thread and kill_thread */
211 static void cleanup_thread( struct thread *thread )
213 int i;
214 struct thread_apc *apc;
216 while ((apc = thread_dequeue_apc( thread, 0 ))) free( apc );
217 if (thread->req_data) free( thread->req_data );
218 if (thread->reply_data) free( thread->reply_data );
219 if (thread->request_fd) release_object( thread->request_fd );
220 if (thread->reply_fd) release_object( thread->reply_fd );
221 if (thread->wait_fd) release_object( thread->wait_fd );
222 free_msg_queue( thread );
223 cleanup_clipboard_thread(thread);
224 destroy_thread_windows( thread );
225 close_thread_desktop( thread );
226 for (i = 0; i < MAX_INFLIGHT_FDS; i++)
228 if (thread->inflight[i].client != -1)
230 close( thread->inflight[i].server );
231 thread->inflight[i].client = thread->inflight[i].server = -1;
234 thread->req_data = NULL;
235 thread->reply_data = NULL;
236 thread->request_fd = NULL;
237 thread->reply_fd = NULL;
238 thread->wait_fd = NULL;
239 thread->desktop = 0;
241 if (thread == booting_thread) /* killing booting thread */
243 booting_thread = NULL;
244 lock_master_socket(0);
248 /* destroy a thread when its refcount is 0 */
249 static void destroy_thread( struct object *obj )
251 struct thread_apc *apc;
252 struct thread *thread = (struct thread *)obj;
253 assert( obj->ops == &thread_ops );
255 assert( !thread->debug_ctx ); /* cannot still be debugging something */
256 list_remove( &thread->entry );
257 while ((apc = thread_dequeue_apc( thread, 0 ))) free( apc );
258 cleanup_thread( thread );
259 release_object( thread->process );
260 if (thread->id) free_ptid( thread->id );
261 if (thread->token) release_object( thread->token );
264 /* dump a thread on stdout for debugging purposes */
265 static void dump_thread( struct object *obj, int verbose )
267 struct thread *thread = (struct thread *)obj;
268 assert( obj->ops == &thread_ops );
270 fprintf( stderr, "Thread id=%04x unix pid=%d unix tid=%d teb=%p state=%d\n",
271 thread->id, thread->unix_pid, thread->unix_tid, thread->teb, thread->state );
274 static int thread_signaled( struct object *obj, struct thread *thread )
276 struct thread *mythread = (struct thread *)obj;
277 return (mythread->state == TERMINATED);
280 /* get a thread pointer from a thread id (and increment the refcount) */
281 struct thread *get_thread_from_id( thread_id_t id )
283 struct object *obj = get_ptid_entry( id );
285 if (obj && obj->ops == &thread_ops) return (struct thread *)grab_object( obj );
286 set_win32_error( ERROR_INVALID_THREAD_ID );
287 return NULL;
290 /* get a thread from a handle (and increment the refcount) */
291 struct thread *get_thread_from_handle( obj_handle_t handle, unsigned int access )
293 return (struct thread *)get_handle_obj( current->process, handle,
294 access, &thread_ops );
297 /* find a thread from a Unix pid */
298 struct thread *get_thread_from_pid( int pid )
300 struct thread *thread;
302 LIST_FOR_EACH_ENTRY( thread, &thread_list, struct thread, entry )
304 if (thread->unix_tid == pid) return thread;
306 LIST_FOR_EACH_ENTRY( thread, &thread_list, struct thread, entry )
308 if (thread->unix_pid == pid) return thread;
310 return NULL;
313 /* set all information about a thread */
314 static void set_thread_info( struct thread *thread,
315 const struct set_thread_info_request *req )
317 if (req->mask & SET_THREAD_INFO_PRIORITY)
318 thread->priority = req->priority;
319 if (req->mask & SET_THREAD_INFO_AFFINITY)
321 if (req->affinity != 1) set_error( STATUS_INVALID_PARAMETER );
322 else thread->affinity = req->affinity;
324 if (req->mask & SET_THREAD_INFO_TOKEN)
325 security_set_thread_token( thread, req->token );
328 /* stop a thread (at the Unix level) */
329 void stop_thread( struct thread *thread )
331 /* can't stop a thread while initialisation is in progress */
332 if (is_process_init_done(thread->process)) send_thread_signal( thread, SIGUSR1 );
335 /* suspend a thread */
336 static int suspend_thread( struct thread *thread )
338 int old_count = thread->suspend;
339 if (thread->suspend < MAXIMUM_SUSPEND_COUNT)
341 if (!(thread->process->suspend + thread->suspend++)) stop_thread( thread );
343 else set_error( STATUS_SUSPEND_COUNT_EXCEEDED );
344 return old_count;
347 /* resume a thread */
348 static int resume_thread( struct thread *thread )
350 int old_count = thread->suspend;
351 if (thread->suspend > 0)
353 if (!(--thread->suspend + thread->process->suspend)) wake_thread( thread );
355 return old_count;
358 /* add a thread to an object wait queue; return 1 if OK, 0 on error */
359 int add_queue( struct object *obj, struct wait_queue_entry *entry )
361 grab_object( obj );
362 entry->obj = obj;
363 list_add_tail( &obj->wait_queue, &entry->entry );
364 return 1;
367 /* remove a thread from an object wait queue */
368 void remove_queue( struct object *obj, struct wait_queue_entry *entry )
370 list_remove( &entry->entry );
371 release_object( obj );
374 /* finish waiting */
375 static void end_wait( struct thread *thread )
377 struct thread_wait *wait = thread->wait;
378 struct wait_queue_entry *entry;
379 int i;
381 assert( wait );
382 for (i = 0, entry = wait->queues; i < wait->count; i++, entry++)
383 entry->obj->ops->remove_queue( entry->obj, entry );
384 if (wait->user) remove_timeout_user( wait->user );
385 thread->wait = wait->next;
386 free( wait );
389 /* build the thread wait structure */
390 static int wait_on( int count, struct object *objects[], int flags, const abs_time_t *timeout )
392 struct thread_wait *wait;
393 struct wait_queue_entry *entry;
394 int i;
396 if (!(wait = mem_alloc( sizeof(*wait) + (count-1) * sizeof(*entry) ))) return 0;
397 wait->next = current->wait;
398 wait->thread = current;
399 wait->count = count;
400 wait->flags = flags;
401 wait->user = NULL;
402 current->wait = wait;
403 if (flags & SELECT_TIMEOUT)
405 wait->timeout.tv_sec = timeout->sec;
406 wait->timeout.tv_usec = timeout->usec;
409 for (i = 0, entry = wait->queues; i < count; i++, entry++)
411 struct object *obj = objects[i];
412 entry->thread = current;
413 if (!obj->ops->add_queue( obj, entry ))
415 wait->count = i;
416 end_wait( current );
417 return 0;
420 return 1;
423 /* check if the thread waiting condition is satisfied */
424 static int check_wait( struct thread *thread )
426 int i, signaled;
427 struct thread_wait *wait = thread->wait;
428 struct wait_queue_entry *entry = wait->queues;
430 /* Suspended threads may not acquire locks */
431 if (thread->process->suspend + thread->suspend > 0) return -1;
433 assert( wait );
434 if (wait->flags & SELECT_ALL)
436 int not_ok = 0;
437 /* Note: we must check them all anyway, as some objects may
438 * want to do something when signaled, even if others are not */
439 for (i = 0, entry = wait->queues; i < wait->count; i++, entry++)
440 not_ok |= !entry->obj->ops->signaled( entry->obj, thread );
441 if (not_ok) goto other_checks;
442 /* Wait satisfied: tell it to all objects */
443 signaled = 0;
444 for (i = 0, entry = wait->queues; i < wait->count; i++, entry++)
445 if (entry->obj->ops->satisfied( entry->obj, thread ))
446 signaled = STATUS_ABANDONED_WAIT_0;
447 return signaled;
449 else
451 for (i = 0, entry = wait->queues; i < wait->count; i++, entry++)
453 if (!entry->obj->ops->signaled( entry->obj, thread )) continue;
454 /* Wait satisfied: tell it to the object */
455 signaled = i;
456 if (entry->obj->ops->satisfied( entry->obj, thread ))
457 signaled = i + STATUS_ABANDONED_WAIT_0;
458 return signaled;
462 other_checks:
463 if ((wait->flags & SELECT_INTERRUPTIBLE) && !list_empty(&thread->system_apc)) return STATUS_USER_APC;
464 if ((wait->flags & SELECT_ALERTABLE) && !list_empty(&thread->user_apc)) return STATUS_USER_APC;
465 if (wait->flags & SELECT_TIMEOUT)
467 struct timeval now;
468 gettimeofday( &now, NULL );
469 if (!time_before( &now, &wait->timeout )) return STATUS_TIMEOUT;
471 return -1;
474 /* send the wakeup signal to a thread */
475 static int send_thread_wakeup( struct thread *thread, void *cookie, int signaled )
477 struct wake_up_reply reply;
478 int ret;
480 reply.cookie = cookie;
481 reply.signaled = signaled;
482 if ((ret = write( get_unix_fd( thread->wait_fd ), &reply, sizeof(reply) )) == sizeof(reply))
483 return 0;
484 if (ret >= 0)
485 fatal_protocol_error( thread, "partial wakeup write %d\n", ret );
486 else if (errno == EPIPE)
487 kill_thread( thread, 0 ); /* normal death */
488 else
489 fatal_protocol_perror( thread, "write" );
490 return -1;
493 /* attempt to wake up a thread */
494 /* return >0 if OK, 0 if the wait condition is still not satisfied */
495 int wake_thread( struct thread *thread )
497 int signaled, count;
498 void *cookie;
500 for (count = 0; thread->wait; count++)
502 if ((signaled = check_wait( thread )) == -1) break;
504 cookie = thread->wait->cookie;
505 if (debug_level) fprintf( stderr, "%04x: *wakeup* signaled=%d cookie=%p\n",
506 thread->id, signaled, cookie );
507 end_wait( thread );
508 if (send_thread_wakeup( thread, cookie, signaled ) == -1) /* error */
509 break;
511 return count;
514 /* thread wait timeout */
515 static void thread_timeout( void *ptr )
517 struct thread_wait *wait = ptr;
518 struct thread *thread = wait->thread;
519 void *cookie = wait->cookie;
521 wait->user = NULL;
522 if (thread->wait != wait) return; /* not the top-level wait, ignore it */
523 if (thread->suspend + thread->process->suspend > 0) return; /* suspended, ignore it */
525 if (debug_level) fprintf( stderr, "%04x: *wakeup* signaled=%d cookie=%p\n",
526 thread->id, STATUS_TIMEOUT, cookie );
527 end_wait( thread );
528 if (send_thread_wakeup( thread, cookie, STATUS_TIMEOUT ) == -1) return;
529 /* check if other objects have become signaled in the meantime */
530 wake_thread( thread );
533 /* try signaling an event flag, a semaphore or a mutex */
534 static int signal_object( obj_handle_t handle )
536 struct object *obj;
537 int ret = 0;
539 obj = get_handle_obj( current->process, handle, 0, NULL );
540 if (obj)
542 ret = obj->ops->signal( obj, get_handle_access( current->process, handle ));
543 release_object( obj );
545 return ret;
548 /* select on a list of handles */
549 static void select_on( int count, void *cookie, const obj_handle_t *handles,
550 int flags, const abs_time_t *timeout, obj_handle_t signal_obj )
552 int ret, i;
553 struct object *objects[MAXIMUM_WAIT_OBJECTS];
555 if ((count < 0) || (count > MAXIMUM_WAIT_OBJECTS))
557 set_error( STATUS_INVALID_PARAMETER );
558 return;
560 for (i = 0; i < count; i++)
562 if (!(objects[i] = get_handle_obj( current->process, handles[i], SYNCHRONIZE, NULL )))
563 break;
566 if (i < count) goto done;
567 if (!wait_on( count, objects, flags, timeout )) goto done;
569 /* signal the object */
570 if (signal_obj)
572 if (!signal_object( signal_obj ))
574 end_wait( current );
575 goto done;
577 /* check if we woke ourselves up */
578 if (!current->wait) goto done;
581 if ((ret = check_wait( current )) != -1)
583 /* condition is already satisfied */
584 end_wait( current );
585 set_error( ret );
586 goto done;
589 /* now we need to wait */
590 if (flags & SELECT_TIMEOUT)
592 if (!(current->wait->user = add_timeout_user( &current->wait->timeout,
593 thread_timeout, current->wait )))
595 end_wait( current );
596 goto done;
599 current->wait->cookie = cookie;
600 set_error( STATUS_PENDING );
602 done:
603 while (--i >= 0) release_object( objects[i] );
606 /* attempt to wake threads sleeping on the object wait queue */
607 void wake_up( struct object *obj, int max )
609 struct list *ptr, *next;
611 LIST_FOR_EACH_SAFE( ptr, next, &obj->wait_queue )
613 struct wait_queue_entry *entry = LIST_ENTRY( ptr, struct wait_queue_entry, entry );
614 if (wake_thread( entry->thread ))
616 if (max && !--max) break;
621 /* queue an async procedure call */
622 int thread_queue_apc( struct thread *thread, struct object *owner, void *func,
623 enum apc_type type, int system, void *arg1, void *arg2, void *arg3 )
625 struct thread_apc *apc;
626 struct list *queue = system ? &thread->system_apc : &thread->user_apc;
628 /* cancel a possible previous APC with the same owner */
629 if (owner) thread_cancel_apc( thread, owner, system );
630 if (thread->state == TERMINATED) return 0;
632 if (!(apc = mem_alloc( sizeof(*apc) ))) return 0;
633 apc->owner = owner;
634 apc->func = func;
635 apc->type = type;
636 apc->arg1 = arg1;
637 apc->arg2 = arg2;
638 apc->arg3 = arg3;
639 list_add_tail( queue, &apc->entry );
640 if (!list_prev( queue, &apc->entry )) /* first one */
641 wake_thread( thread );
643 return 1;
646 /* cancel the async procedure call owned by a specific object */
647 void thread_cancel_apc( struct thread *thread, struct object *owner, int system )
649 struct thread_apc *apc;
650 struct list *queue = system ? &thread->system_apc : &thread->user_apc;
651 LIST_FOR_EACH_ENTRY( apc, queue, struct thread_apc, entry )
653 if (apc->owner != owner) continue;
654 list_remove( &apc->entry );
655 free( apc );
656 return;
660 /* remove the head apc from the queue; the returned pointer must be freed by the caller */
661 static struct thread_apc *thread_dequeue_apc( struct thread *thread, int system_only )
663 struct thread_apc *apc = NULL;
664 struct list *ptr = list_head( &thread->system_apc );
666 if (!ptr && !system_only) ptr = list_head( &thread->user_apc );
667 if (ptr)
669 apc = LIST_ENTRY( ptr, struct thread_apc, entry );
670 list_remove( ptr );
672 return apc;
675 /* add an fd to the inflight list */
676 /* return list index, or -1 on error */
677 int thread_add_inflight_fd( struct thread *thread, int client, int server )
679 int i;
681 if (server == -1) return -1;
682 if (client == -1)
684 close( server );
685 return -1;
688 /* first check if we already have an entry for this fd */
689 for (i = 0; i < MAX_INFLIGHT_FDS; i++)
690 if (thread->inflight[i].client == client)
692 close( thread->inflight[i].server );
693 thread->inflight[i].server = server;
694 return i;
697 /* now find a free spot to store it */
698 for (i = 0; i < MAX_INFLIGHT_FDS; i++)
699 if (thread->inflight[i].client == -1)
701 thread->inflight[i].client = client;
702 thread->inflight[i].server = server;
703 return i;
705 return -1;
708 /* get an inflight fd and purge it from the list */
709 /* the fd must be closed when no longer used */
710 int thread_get_inflight_fd( struct thread *thread, int client )
712 int i, ret;
714 if (client == -1) return -1;
718 for (i = 0; i < MAX_INFLIGHT_FDS; i++)
720 if (thread->inflight[i].client == client)
722 ret = thread->inflight[i].server;
723 thread->inflight[i].server = thread->inflight[i].client = -1;
724 return ret;
727 } while (!receive_fd( thread->process )); /* in case it is still in the socket buffer */
728 return -1;
731 /* retrieve an LDT selector entry */
732 static void get_selector_entry( struct thread *thread, int entry,
733 unsigned int *base, unsigned int *limit,
734 unsigned char *flags )
736 if (!thread->process->ldt_copy)
738 set_error( STATUS_ACCESS_DENIED );
739 return;
741 if (entry >= 8192)
743 set_error( STATUS_INVALID_PARAMETER ); /* FIXME */
744 return;
746 if (suspend_for_ptrace( thread ))
748 unsigned char flags_buf[4];
749 int *addr = (int *)thread->process->ldt_copy + entry;
750 if (read_thread_int( thread, addr, base ) == -1) goto done;
751 if (read_thread_int( thread, addr + 8192, limit ) == -1) goto done;
752 addr = (int *)thread->process->ldt_copy + 2*8192 + (entry >> 2);
753 if (read_thread_int( thread, addr, (int *)flags_buf ) == -1) goto done;
754 *flags = flags_buf[entry & 3];
755 done:
756 resume_after_ptrace( thread );
760 /* kill a thread on the spot */
761 void kill_thread( struct thread *thread, int violent_death )
763 if (thread->state == TERMINATED) return; /* already killed */
764 thread->state = TERMINATED;
765 thread->exit_time = time(NULL);
766 if (current == thread) current = NULL;
767 if (debug_level)
768 fprintf( stderr,"%04x: *killed* exit_code=%d\n",
769 thread->id, thread->exit_code );
770 if (thread->wait)
772 while (thread->wait) end_wait( thread );
773 send_thread_wakeup( thread, NULL, STATUS_PENDING );
774 /* if it is waiting on the socket, we don't need to send a SIGTERM */
775 violent_death = 0;
777 kill_console_processes( thread, 0 );
778 debug_exit_thread( thread );
779 abandon_mutexes( thread );
780 remove_process_thread( thread->process, thread );
781 wake_up( &thread->obj, 0 );
782 detach_thread( thread, violent_death ? SIGTERM : 0 );
783 cleanup_thread( thread );
784 release_object( thread );
787 /* take a snapshot of currently running threads */
788 struct thread_snapshot *thread_snap( int *count )
790 struct thread_snapshot *snapshot, *ptr;
791 struct thread *thread;
792 int total = 0;
794 LIST_FOR_EACH_ENTRY( thread, &thread_list, struct thread, entry )
795 if (thread->state != TERMINATED) total++;
796 if (!total || !(snapshot = mem_alloc( sizeof(*snapshot) * total ))) return NULL;
797 ptr = snapshot;
798 LIST_FOR_EACH_ENTRY( thread, &thread_list, struct thread, entry )
800 if (thread->state == TERMINATED) continue;
801 ptr->thread = thread;
802 ptr->count = thread->obj.refcount;
803 ptr->priority = thread->priority;
804 grab_object( thread );
805 ptr++;
807 *count = total;
808 return snapshot;
811 /* gets the current impersonation token */
812 struct token *thread_get_impersonation_token( struct thread *thread )
814 if (thread->token)
815 return thread->token;
816 else
817 return thread->process->token;
820 /* signal that we are finished booting on the client side */
821 DECL_HANDLER(boot_done)
823 if (current == booting_thread)
825 booting_thread = (struct thread *)~0UL; /* make sure it doesn't match other threads */
826 lock_master_socket(0); /* allow other clients now */
830 /* create a new thread */
831 DECL_HANDLER(new_thread)
833 struct thread *thread;
834 int request_fd = thread_get_inflight_fd( current, req->request_fd );
836 if (request_fd == -1 || fcntl( request_fd, F_SETFL, O_NONBLOCK ) == -1)
838 if (request_fd != -1) close( request_fd );
839 set_error( STATUS_INVALID_HANDLE );
840 return;
843 if ((thread = create_thread( request_fd, current->process )))
845 if (req->suspend) thread->suspend++;
846 reply->tid = get_thread_id( thread );
847 if ((reply->handle = alloc_handle( current->process, thread,
848 THREAD_ALL_ACCESS, req->inherit )))
850 /* thread object will be released when the thread gets killed */
851 return;
853 kill_thread( thread, 1 );
857 /* initialize a new thread */
858 DECL_HANDLER(init_thread)
860 struct process *process = current->process;
861 int reply_fd = thread_get_inflight_fd( current, req->reply_fd );
862 int wait_fd = thread_get_inflight_fd( current, req->wait_fd );
864 if (current->unix_pid != -1)
866 fatal_protocol_error( current, "init_thread: already running\n" );
867 goto error;
869 if (reply_fd == -1 || fcntl( reply_fd, F_SETFL, O_NONBLOCK ) == -1)
871 fatal_protocol_error( current, "bad reply fd\n" );
872 goto error;
874 if (wait_fd == -1)
876 fatal_protocol_error( current, "bad wait fd\n" );
877 goto error;
879 if (!(current->reply_fd = create_anonymous_fd( &thread_fd_ops, reply_fd, &current->obj )))
881 reply_fd = -1;
882 fatal_protocol_error( current, "could not allocate reply fd\n" );
883 goto error;
885 if (!(current->wait_fd = create_anonymous_fd( &thread_fd_ops, wait_fd, &current->obj )))
886 return;
888 if (!is_valid_address(req->teb) || !is_valid_address(req->peb) || !is_valid_address(req->ldt_copy))
890 set_error( STATUS_INVALID_PARAMETER );
891 return;
894 current->unix_pid = req->unix_pid;
895 current->unix_tid = req->unix_tid;
896 current->teb = req->teb;
898 if (!process->peb) /* first thread, initialize the process too */
900 process->peb = req->peb;
901 process->ldt_copy = req->ldt_copy;
902 init_process( current );
904 else
906 if (current->suspend + process->suspend > 0) stop_thread( current );
907 generate_debug_event( current, CREATE_THREAD_DEBUG_EVENT, req->entry );
909 debug_level = max( debug_level, req->debug_level );
911 reply->pid = get_process_id( process );
912 reply->tid = get_thread_id( current );
913 reply->version = SERVER_PROTOCOL_VERSION;
914 return;
916 error:
917 if (reply_fd != -1) close( reply_fd );
918 if (wait_fd != -1) close( wait_fd );
921 /* terminate a thread */
922 DECL_HANDLER(terminate_thread)
924 struct thread *thread;
926 reply->self = 0;
927 reply->last = 0;
928 if ((thread = get_thread_from_handle( req->handle, THREAD_TERMINATE )))
930 thread->exit_code = req->exit_code;
931 if (thread != current) kill_thread( thread, 1 );
932 else
934 reply->self = 1;
935 reply->last = (thread->process->running_threads == 1);
937 release_object( thread );
941 /* open a handle to a thread */
942 DECL_HANDLER(open_thread)
944 struct thread *thread = get_thread_from_id( req->tid );
946 reply->handle = 0;
947 if (thread)
949 reply->handle = alloc_handle( current->process, thread, req->access, req->inherit );
950 release_object( thread );
954 /* fetch information about a thread */
955 DECL_HANDLER(get_thread_info)
957 struct thread *thread;
958 obj_handle_t handle = req->handle;
960 if (!handle) thread = get_thread_from_id( req->tid_in );
961 else thread = get_thread_from_handle( req->handle, THREAD_QUERY_INFORMATION );
963 if (thread)
965 reply->pid = get_process_id( thread->process );
966 reply->tid = get_thread_id( thread );
967 reply->teb = thread->teb;
968 reply->exit_code = (thread->state == TERMINATED) ? thread->exit_code : STILL_ACTIVE;
969 reply->priority = thread->priority;
970 reply->affinity = thread->affinity;
971 reply->creation_time = thread->creation_time;
972 reply->exit_time = thread->exit_time;
974 release_object( thread );
978 /* set information about a thread */
979 DECL_HANDLER(set_thread_info)
981 struct thread *thread;
983 if ((thread = get_thread_from_handle( req->handle, THREAD_SET_INFORMATION )))
985 set_thread_info( thread, req );
986 release_object( thread );
990 /* suspend a thread */
991 DECL_HANDLER(suspend_thread)
993 struct thread *thread;
995 if ((thread = get_thread_from_handle( req->handle, THREAD_SUSPEND_RESUME )))
997 if (thread->state == TERMINATED) set_error( STATUS_ACCESS_DENIED );
998 else reply->count = suspend_thread( thread );
999 release_object( thread );
1003 /* resume a thread */
1004 DECL_HANDLER(resume_thread)
1006 struct thread *thread;
1008 if ((thread = get_thread_from_handle( req->handle, THREAD_SUSPEND_RESUME )))
1010 if (thread->state == TERMINATED) set_error( STATUS_ACCESS_DENIED );
1011 else reply->count = resume_thread( thread );
1012 release_object( thread );
1016 /* select on a handle list */
1017 DECL_HANDLER(select)
1019 int count = get_req_data_size() / sizeof(int);
1020 select_on( count, req->cookie, get_req_data(), req->flags, &req->timeout, req->signal );
1023 /* queue an APC for a thread */
1024 DECL_HANDLER(queue_apc)
1026 struct thread *thread;
1027 if ((thread = get_thread_from_handle( req->handle, THREAD_SET_CONTEXT )))
1029 thread_queue_apc( thread, NULL, req->func, APC_USER, !req->user,
1030 req->arg1, req->arg2, req->arg3 );
1031 release_object( thread );
1035 /* get next APC to call */
1036 DECL_HANDLER(get_apc)
1038 struct thread_apc *apc;
1040 for (;;)
1042 if (!(apc = thread_dequeue_apc( current, !req->alertable )))
1044 /* no more APCs */
1045 reply->func = NULL;
1046 reply->type = APC_NONE;
1047 return;
1049 /* Optimization: ignore APCs that have a NULL func; they are only used
1050 * to wake up a thread, but since we got here the thread woke up already.
1051 * Exception: for APC_ASYNC_IO, func == NULL is legal.
1053 if (apc->func || apc->type == APC_ASYNC_IO) break;
1054 free( apc );
1056 reply->func = apc->func;
1057 reply->type = apc->type;
1058 reply->arg1 = apc->arg1;
1059 reply->arg2 = apc->arg2;
1060 reply->arg3 = apc->arg3;
1061 free( apc );
1064 /* fetch a selector entry for a thread */
1065 DECL_HANDLER(get_selector_entry)
1067 struct thread *thread;
1068 if ((thread = get_thread_from_handle( req->handle, THREAD_QUERY_INFORMATION )))
1070 get_selector_entry( thread, req->entry, &reply->base, &reply->limit, &reply->flags );
1071 release_object( thread );