Add support for impersonating a token.
[wine/multimedia.git] / server / thread.c
blobc11c18ab54e4a9e0f5a7fb32e58f9be73223d53b
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"
40 #include "winbase.h"
42 #include "file.h"
43 #include "handle.h"
44 #include "process.h"
45 #include "thread.h"
46 #include "request.h"
47 #include "user.h"
48 #include "security.h"
51 /* thread queues */
53 struct thread_wait
55 struct thread_wait *next; /* next wait structure for this thread */
56 struct thread *thread; /* owner thread */
57 int count; /* count of objects */
58 int flags;
59 void *cookie; /* magic cookie to return to client */
60 struct timeval timeout;
61 struct timeout_user *user;
62 struct wait_queue_entry queues[1];
65 /* asynchronous procedure calls */
67 struct thread_apc
69 struct list entry; /* queue linked list */
70 struct object *owner; /* object that queued this apc */
71 void *func; /* function to call in client */
72 enum apc_type type; /* type of apc function */
73 int nb_args; /* number of arguments */
74 void *arg1; /* function arguments */
75 void *arg2;
76 void *arg3;
80 /* thread operations */
82 static void dump_thread( struct object *obj, int verbose );
83 static int thread_signaled( struct object *obj, struct thread *thread );
84 static void thread_poll_event( struct fd *fd, int event );
85 static void destroy_thread( struct object *obj );
86 static struct thread_apc *thread_dequeue_apc( struct thread *thread, int system_only );
88 static const struct object_ops thread_ops =
90 sizeof(struct thread), /* size */
91 dump_thread, /* dump */
92 add_queue, /* add_queue */
93 remove_queue, /* remove_queue */
94 thread_signaled, /* signaled */
95 no_satisfied, /* satisfied */
96 no_signal, /* signal */
97 no_get_fd, /* get_fd */
98 no_close_handle, /* close_handle */
99 destroy_thread /* destroy */
102 static const struct fd_ops thread_fd_ops =
104 NULL, /* get_poll_events */
105 thread_poll_event, /* poll_event */
106 no_flush, /* flush */
107 no_get_file_info, /* get_file_info */
108 no_queue_async, /* queue_async */
109 no_cancel_async /* cancel_async */
112 static struct list thread_list = LIST_INIT(thread_list);
113 static struct thread *booting_thread;
115 /* initialize the structure for a newly allocated thread */
116 inline static void init_thread_structure( struct thread *thread )
118 int i;
120 thread->unix_pid = -1; /* not known yet */
121 thread->unix_tid = -1; /* not known yet */
122 thread->context = NULL;
123 thread->teb = NULL;
124 thread->debug_ctx = NULL;
125 thread->debug_event = NULL;
126 thread->queue = NULL;
127 thread->wait = NULL;
128 thread->error = 0;
129 thread->req_data = NULL;
130 thread->req_toread = 0;
131 thread->reply_data = NULL;
132 thread->reply_towrite = 0;
133 thread->request_fd = NULL;
134 thread->reply_fd = NULL;
135 thread->wait_fd = NULL;
136 thread->state = RUNNING;
137 thread->attached = 0;
138 thread->exit_code = 0;
139 thread->priority = THREAD_PRIORITY_NORMAL;
140 thread->affinity = 1;
141 thread->suspend = 0;
142 thread->creation_time = time(NULL);
143 thread->exit_time = 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 /* create a new thread */
154 struct thread *create_thread( int fd, struct process *process )
156 struct thread *thread;
158 if (!(thread = alloc_object( &thread_ops ))) return NULL;
160 init_thread_structure( thread );
162 thread->process = (struct process *)grab_object( process );
163 thread->desktop = process->desktop;
164 if (!current) current = thread;
166 if (!booting_thread) /* first thread ever */
168 booting_thread = thread;
169 lock_master_socket(1);
172 list_add_head( &thread_list, &thread->entry );
174 if (!(thread->id = alloc_ptid( thread )))
176 release_object( thread );
177 return NULL;
179 if (!(thread->request_fd = create_anonymous_fd( &thread_fd_ops, fd, &thread->obj )))
181 release_object( thread );
182 return NULL;
185 thread->token = (struct token *) grab_object( process->token );
187 set_fd_events( thread->request_fd, POLLIN ); /* start listening to events */
188 add_process_thread( thread->process, thread );
189 return thread;
192 /* handle a client event */
193 static void thread_poll_event( struct fd *fd, int event )
195 struct thread *thread = get_fd_user( fd );
196 assert( thread->obj.ops == &thread_ops );
198 if (event & (POLLERR | POLLHUP)) kill_thread( thread, 0 );
199 else if (event & POLLIN) read_request( thread );
200 else if (event & POLLOUT) write_reply( thread );
203 /* cleanup everything that is no longer needed by a dead thread */
204 /* used by destroy_thread and kill_thread */
205 static void cleanup_thread( struct thread *thread )
207 int i;
208 struct thread_apc *apc;
210 while ((apc = thread_dequeue_apc( thread, 0 ))) free( apc );
211 if (thread->req_data) free( thread->req_data );
212 if (thread->reply_data) free( thread->reply_data );
213 if (thread->request_fd) release_object( thread->request_fd );
214 if (thread->reply_fd) release_object( thread->reply_fd );
215 if (thread->wait_fd) release_object( thread->wait_fd );
216 free_msg_queue( thread );
217 cleanup_clipboard_thread(thread);
218 destroy_thread_windows( thread );
219 close_thread_desktop( thread );
220 for (i = 0; i < MAX_INFLIGHT_FDS; i++)
222 if (thread->inflight[i].client != -1)
224 close( thread->inflight[i].server );
225 thread->inflight[i].client = thread->inflight[i].server = -1;
228 thread->req_data = NULL;
229 thread->reply_data = NULL;
230 thread->request_fd = NULL;
231 thread->reply_fd = NULL;
232 thread->wait_fd = NULL;
233 thread->desktop = 0;
235 if (thread == booting_thread) /* killing booting thread */
237 booting_thread = NULL;
238 lock_master_socket(0);
242 /* destroy a thread when its refcount is 0 */
243 static void destroy_thread( struct object *obj )
245 struct thread_apc *apc;
246 struct thread *thread = (struct thread *)obj;
247 assert( obj->ops == &thread_ops );
249 assert( !thread->debug_ctx ); /* cannot still be debugging something */
250 list_remove( &thread->entry );
251 while ((apc = thread_dequeue_apc( thread, 0 ))) free( apc );
252 cleanup_thread( thread );
253 release_object( thread->process );
254 if (thread->id) free_ptid( thread->id );
255 if (thread->token) release_object( thread->token );
258 /* dump a thread on stdout for debugging purposes */
259 static void dump_thread( struct object *obj, int verbose )
261 struct thread *thread = (struct thread *)obj;
262 assert( obj->ops == &thread_ops );
264 fprintf( stderr, "Thread id=%04x unix pid=%d unix tid=%d teb=%p state=%d\n",
265 thread->id, thread->unix_pid, thread->unix_tid, thread->teb, thread->state );
268 static int thread_signaled( struct object *obj, struct thread *thread )
270 struct thread *mythread = (struct thread *)obj;
271 return (mythread->state == TERMINATED);
274 /* get a thread pointer from a thread id (and increment the refcount) */
275 struct thread *get_thread_from_id( thread_id_t id )
277 struct object *obj = get_ptid_entry( id );
279 if (obj && obj->ops == &thread_ops) return (struct thread *)grab_object( obj );
280 set_win32_error( ERROR_INVALID_THREAD_ID );
281 return NULL;
284 /* get a thread from a handle (and increment the refcount) */
285 struct thread *get_thread_from_handle( obj_handle_t handle, unsigned int access )
287 return (struct thread *)get_handle_obj( current->process, handle,
288 access, &thread_ops );
291 /* find a thread from a Unix pid */
292 struct thread *get_thread_from_pid( int pid )
294 struct thread *thread;
296 LIST_FOR_EACH_ENTRY( thread, &thread_list, struct thread, entry )
298 if (thread->unix_tid == pid) return thread;
300 LIST_FOR_EACH_ENTRY( thread, &thread_list, struct thread, entry )
302 if (thread->unix_pid == pid) return thread;
304 return NULL;
307 /* set all information about a thread */
308 static void set_thread_info( struct thread *thread,
309 const struct set_thread_info_request *req )
311 if (req->mask & SET_THREAD_INFO_PRIORITY)
312 thread->priority = req->priority;
313 if (req->mask & SET_THREAD_INFO_AFFINITY)
315 if (req->affinity != 1) set_error( STATUS_INVALID_PARAMETER );
316 else thread->affinity = req->affinity;
318 if (req->mask & SET_THREAD_INFO_TOKEN)
319 security_set_thread_token( thread, req->token );
322 /* stop a thread (at the Unix level) */
323 void stop_thread( struct thread *thread )
325 /* can't stop a thread while initialisation is in progress */
326 if (is_process_init_done(thread->process)) send_thread_signal( thread, SIGUSR1 );
329 /* suspend a thread */
330 static int suspend_thread( struct thread *thread )
332 int old_count = thread->suspend;
333 if (thread->suspend < MAXIMUM_SUSPEND_COUNT)
335 if (!(thread->process->suspend + thread->suspend++)) stop_thread( thread );
337 else set_error( STATUS_SUSPEND_COUNT_EXCEEDED );
338 return old_count;
341 /* resume a thread */
342 static int resume_thread( struct thread *thread )
344 int old_count = thread->suspend;
345 if (thread->suspend > 0)
347 if (!(--thread->suspend + thread->process->suspend)) wake_thread( thread );
349 return old_count;
352 /* add a thread to an object wait queue; return 1 if OK, 0 on error */
353 int add_queue( struct object *obj, struct wait_queue_entry *entry )
355 grab_object( obj );
356 entry->obj = obj;
357 list_add_tail( &obj->wait_queue, &entry->entry );
358 return 1;
361 /* remove a thread from an object wait queue */
362 void remove_queue( struct object *obj, struct wait_queue_entry *entry )
364 list_remove( &entry->entry );
365 release_object( obj );
368 /* finish waiting */
369 static void end_wait( struct thread *thread )
371 struct thread_wait *wait = thread->wait;
372 struct wait_queue_entry *entry;
373 int i;
375 assert( wait );
376 for (i = 0, entry = wait->queues; i < wait->count; i++, entry++)
377 entry->obj->ops->remove_queue( entry->obj, entry );
378 if (wait->user) remove_timeout_user( wait->user );
379 thread->wait = wait->next;
380 free( wait );
383 /* build the thread wait structure */
384 static int wait_on( int count, struct object *objects[], int flags, const abs_time_t *timeout )
386 struct thread_wait *wait;
387 struct wait_queue_entry *entry;
388 int i;
390 if (!(wait = mem_alloc( sizeof(*wait) + (count-1) * sizeof(*entry) ))) return 0;
391 wait->next = current->wait;
392 wait->thread = current;
393 wait->count = count;
394 wait->flags = flags;
395 wait->user = NULL;
396 current->wait = wait;
397 if (flags & SELECT_TIMEOUT)
399 wait->timeout.tv_sec = timeout->sec;
400 wait->timeout.tv_usec = timeout->usec;
403 for (i = 0, entry = wait->queues; i < count; i++, entry++)
405 struct object *obj = objects[i];
406 entry->thread = current;
407 if (!obj->ops->add_queue( obj, entry ))
409 wait->count = i;
410 end_wait( current );
411 return 0;
414 return 1;
417 /* check if the thread waiting condition is satisfied */
418 static int check_wait( struct thread *thread )
420 int i, signaled;
421 struct thread_wait *wait = thread->wait;
422 struct wait_queue_entry *entry = wait->queues;
424 /* Suspended threads may not acquire locks */
425 if (thread->process->suspend + thread->suspend > 0) return -1;
427 assert( wait );
428 if (wait->flags & SELECT_ALL)
430 int not_ok = 0;
431 /* Note: we must check them all anyway, as some objects may
432 * want to do something when signaled, even if others are not */
433 for (i = 0, entry = wait->queues; i < wait->count; i++, entry++)
434 not_ok |= !entry->obj->ops->signaled( entry->obj, thread );
435 if (not_ok) goto other_checks;
436 /* Wait satisfied: tell it to all objects */
437 signaled = 0;
438 for (i = 0, entry = wait->queues; i < wait->count; i++, entry++)
439 if (entry->obj->ops->satisfied( entry->obj, thread ))
440 signaled = STATUS_ABANDONED_WAIT_0;
441 return signaled;
443 else
445 for (i = 0, entry = wait->queues; i < wait->count; i++, entry++)
447 if (!entry->obj->ops->signaled( entry->obj, thread )) continue;
448 /* Wait satisfied: tell it to the object */
449 signaled = i;
450 if (entry->obj->ops->satisfied( entry->obj, thread ))
451 signaled = i + STATUS_ABANDONED_WAIT_0;
452 return signaled;
456 other_checks:
457 if ((wait->flags & SELECT_INTERRUPTIBLE) && !list_empty(&thread->system_apc)) return STATUS_USER_APC;
458 if ((wait->flags & SELECT_ALERTABLE) && !list_empty(&thread->user_apc)) return STATUS_USER_APC;
459 if (wait->flags & SELECT_TIMEOUT)
461 struct timeval now;
462 gettimeofday( &now, NULL );
463 if (!time_before( &now, &wait->timeout )) return STATUS_TIMEOUT;
465 return -1;
468 /* send the wakeup signal to a thread */
469 static int send_thread_wakeup( struct thread *thread, void *cookie, int signaled )
471 struct wake_up_reply reply;
472 int ret;
474 reply.cookie = cookie;
475 reply.signaled = signaled;
476 if ((ret = write( get_unix_fd( thread->wait_fd ), &reply, sizeof(reply) )) == sizeof(reply))
477 return 0;
478 if (ret >= 0)
479 fatal_protocol_error( thread, "partial wakeup write %d\n", ret );
480 else if (errno == EPIPE)
481 kill_thread( thread, 0 ); /* normal death */
482 else
483 fatal_protocol_perror( thread, "write" );
484 return -1;
487 /* attempt to wake up a thread */
488 /* return >0 if OK, 0 if the wait condition is still not satisfied */
489 int wake_thread( struct thread *thread )
491 int signaled, count;
492 void *cookie;
494 for (count = 0; thread->wait; count++)
496 if ((signaled = check_wait( thread )) == -1) break;
498 cookie = thread->wait->cookie;
499 if (debug_level) fprintf( stderr, "%04x: *wakeup* signaled=%d cookie=%p\n",
500 thread->id, signaled, cookie );
501 end_wait( thread );
502 if (send_thread_wakeup( thread, cookie, signaled ) == -1) /* error */
503 break;
505 return count;
508 /* thread wait timeout */
509 static void thread_timeout( void *ptr )
511 struct thread_wait *wait = ptr;
512 struct thread *thread = wait->thread;
513 void *cookie = wait->cookie;
515 wait->user = NULL;
516 if (thread->wait != wait) return; /* not the top-level wait, ignore it */
517 if (thread->suspend + thread->process->suspend > 0) return; /* suspended, ignore it */
519 if (debug_level) fprintf( stderr, "%04x: *wakeup* signaled=%d cookie=%p\n",
520 thread->id, STATUS_TIMEOUT, cookie );
521 end_wait( thread );
522 if (send_thread_wakeup( thread, cookie, STATUS_TIMEOUT ) == -1) return;
523 /* check if other objects have become signaled in the meantime */
524 wake_thread( thread );
527 /* try signaling an event flag, a semaphore or a mutex */
528 static int signal_object( obj_handle_t handle )
530 struct object *obj;
531 int ret = 0;
533 obj = get_handle_obj( current->process, handle, 0, NULL );
534 if (obj)
536 ret = obj->ops->signal( obj, get_handle_access( current->process, handle ));
537 release_object( obj );
539 return ret;
542 /* select on a list of handles */
543 static void select_on( int count, void *cookie, const obj_handle_t *handles,
544 int flags, const abs_time_t *timeout, obj_handle_t signal_obj )
546 int ret, i;
547 struct object *objects[MAXIMUM_WAIT_OBJECTS];
549 if ((count < 0) || (count > MAXIMUM_WAIT_OBJECTS))
551 set_error( STATUS_INVALID_PARAMETER );
552 return;
554 for (i = 0; i < count; i++)
556 if (!(objects[i] = get_handle_obj( current->process, handles[i], SYNCHRONIZE, NULL )))
557 break;
560 if (i < count) goto done;
561 if (!wait_on( count, objects, flags, timeout )) goto done;
563 /* signal the object */
564 if (signal_obj)
566 if (!signal_object( signal_obj ))
568 end_wait( current );
569 goto done;
571 /* check if we woke ourselves up */
572 if (!current->wait) goto done;
575 if ((ret = check_wait( current )) != -1)
577 /* condition is already satisfied */
578 end_wait( current );
579 set_error( ret );
580 goto done;
583 /* now we need to wait */
584 if (flags & SELECT_TIMEOUT)
586 if (!(current->wait->user = add_timeout_user( &current->wait->timeout,
587 thread_timeout, current->wait )))
589 end_wait( current );
590 goto done;
593 current->wait->cookie = cookie;
594 set_error( STATUS_PENDING );
596 done:
597 while (--i >= 0) release_object( objects[i] );
600 /* attempt to wake threads sleeping on the object wait queue */
601 void wake_up( struct object *obj, int max )
603 struct list *ptr, *next;
605 LIST_FOR_EACH_SAFE( ptr, next, &obj->wait_queue )
607 struct wait_queue_entry *entry = LIST_ENTRY( ptr, struct wait_queue_entry, entry );
608 if (wake_thread( entry->thread ))
610 if (max && !--max) break;
615 /* queue an async procedure call */
616 int thread_queue_apc( struct thread *thread, struct object *owner, void *func,
617 enum apc_type type, int system, void *arg1, void *arg2, void *arg3 )
619 struct thread_apc *apc;
620 struct list *queue = system ? &thread->system_apc : &thread->user_apc;
622 /* cancel a possible previous APC with the same owner */
623 if (owner) thread_cancel_apc( thread, owner, system );
624 if (thread->state == TERMINATED) return 0;
626 if (!(apc = mem_alloc( sizeof(*apc) ))) return 0;
627 apc->owner = owner;
628 apc->func = func;
629 apc->type = type;
630 apc->arg1 = arg1;
631 apc->arg2 = arg2;
632 apc->arg3 = arg3;
633 list_add_tail( queue, &apc->entry );
634 if (!list_prev( queue, &apc->entry )) /* first one */
635 wake_thread( thread );
637 return 1;
640 /* cancel the async procedure call owned by a specific object */
641 void thread_cancel_apc( struct thread *thread, struct object *owner, int system )
643 struct thread_apc *apc;
644 struct list *queue = system ? &thread->system_apc : &thread->user_apc;
645 LIST_FOR_EACH_ENTRY( apc, queue, struct thread_apc, entry )
647 if (apc->owner != owner) continue;
648 list_remove( &apc->entry );
649 free( apc );
650 return;
654 /* remove the head apc from the queue; the returned pointer must be freed by the caller */
655 static struct thread_apc *thread_dequeue_apc( struct thread *thread, int system_only )
657 struct thread_apc *apc = NULL;
658 struct list *ptr = list_head( &thread->system_apc );
660 if (!ptr && !system_only) ptr = list_head( &thread->user_apc );
661 if (ptr)
663 apc = LIST_ENTRY( ptr, struct thread_apc, entry );
664 list_remove( ptr );
666 return apc;
669 /* add an fd to the inflight list */
670 /* return list index, or -1 on error */
671 int thread_add_inflight_fd( struct thread *thread, int client, int server )
673 int i;
675 if (server == -1) return -1;
676 if (client == -1)
678 close( server );
679 return -1;
682 /* first check if we already have an entry for this fd */
683 for (i = 0; i < MAX_INFLIGHT_FDS; i++)
684 if (thread->inflight[i].client == client)
686 close( thread->inflight[i].server );
687 thread->inflight[i].server = server;
688 return i;
691 /* now find a free spot to store it */
692 for (i = 0; i < MAX_INFLIGHT_FDS; i++)
693 if (thread->inflight[i].client == -1)
695 thread->inflight[i].client = client;
696 thread->inflight[i].server = server;
697 return i;
699 return -1;
702 /* get an inflight fd and purge it from the list */
703 /* the fd must be closed when no longer used */
704 int thread_get_inflight_fd( struct thread *thread, int client )
706 int i, ret;
708 if (client == -1) return -1;
712 for (i = 0; i < MAX_INFLIGHT_FDS; i++)
714 if (thread->inflight[i].client == client)
716 ret = thread->inflight[i].server;
717 thread->inflight[i].server = thread->inflight[i].client = -1;
718 return ret;
721 } while (!receive_fd( thread->process )); /* in case it is still in the socket buffer */
722 return -1;
725 /* retrieve an LDT selector entry */
726 static void get_selector_entry( struct thread *thread, int entry,
727 unsigned int *base, unsigned int *limit,
728 unsigned char *flags )
730 if (!thread->process->ldt_copy)
732 set_error( STATUS_ACCESS_DENIED );
733 return;
735 if (entry >= 8192)
737 set_error( STATUS_INVALID_PARAMETER ); /* FIXME */
738 return;
740 if (suspend_for_ptrace( thread ))
742 unsigned char flags_buf[4];
743 int *addr = (int *)thread->process->ldt_copy + entry;
744 if (read_thread_int( thread, addr, base ) == -1) goto done;
745 if (read_thread_int( thread, addr + 8192, limit ) == -1) goto done;
746 addr = (int *)thread->process->ldt_copy + 2*8192 + (entry >> 2);
747 if (read_thread_int( thread, addr, (int *)flags_buf ) == -1) goto done;
748 *flags = flags_buf[entry & 3];
749 done:
750 resume_after_ptrace( thread );
754 /* kill a thread on the spot */
755 void kill_thread( struct thread *thread, int violent_death )
757 if (thread->state == TERMINATED) return; /* already killed */
758 thread->state = TERMINATED;
759 thread->exit_time = time(NULL);
760 if (current == thread) current = NULL;
761 if (debug_level)
762 fprintf( stderr,"%04x: *killed* exit_code=%d\n",
763 thread->id, thread->exit_code );
764 if (thread->wait)
766 while (thread->wait) end_wait( thread );
767 send_thread_wakeup( thread, NULL, STATUS_PENDING );
768 /* if it is waiting on the socket, we don't need to send a SIGTERM */
769 violent_death = 0;
771 kill_console_processes( thread, 0 );
772 debug_exit_thread( thread );
773 abandon_mutexes( thread );
774 remove_process_thread( thread->process, thread );
775 wake_up( &thread->obj, 0 );
776 detach_thread( thread, violent_death ? SIGTERM : 0 );
777 cleanup_thread( thread );
778 release_object( thread );
781 /* take a snapshot of currently running threads */
782 struct thread_snapshot *thread_snap( int *count )
784 struct thread_snapshot *snapshot, *ptr;
785 struct thread *thread;
786 int total = 0;
788 LIST_FOR_EACH_ENTRY( thread, &thread_list, struct thread, entry )
789 if (thread->state != TERMINATED) total++;
790 if (!total || !(snapshot = mem_alloc( sizeof(*snapshot) * total ))) return NULL;
791 ptr = snapshot;
792 LIST_FOR_EACH_ENTRY( thread, &thread_list, struct thread, entry )
794 if (thread->state == TERMINATED) continue;
795 ptr->thread = thread;
796 ptr->count = thread->obj.refcount;
797 ptr->priority = thread->priority;
798 grab_object( thread );
799 ptr++;
801 *count = total;
802 return snapshot;
805 /* gets the current impersonation token */
806 struct token *thread_get_impersonation_token( struct thread *thread )
808 if (thread->token)
809 return thread->token;
810 else
811 return thread->process->token;
814 /* signal that we are finished booting on the client side */
815 DECL_HANDLER(boot_done)
817 debug_level = max( debug_level, req->debug_level );
818 if (current == booting_thread)
820 booting_thread = (struct thread *)~0UL; /* make sure it doesn't match other threads */
821 lock_master_socket(0); /* allow other clients now */
825 /* create a new thread */
826 DECL_HANDLER(new_thread)
828 struct thread *thread;
829 int request_fd = thread_get_inflight_fd( current, req->request_fd );
831 if (request_fd == -1 || fcntl( request_fd, F_SETFL, O_NONBLOCK ) == -1)
833 if (request_fd != -1) close( request_fd );
834 set_error( STATUS_INVALID_HANDLE );
835 return;
838 if ((thread = create_thread( request_fd, current->process )))
840 if (req->suspend) thread->suspend++;
841 reply->tid = get_thread_id( thread );
842 if ((reply->handle = alloc_handle( current->process, thread,
843 THREAD_ALL_ACCESS, req->inherit )))
845 /* thread object will be released when the thread gets killed */
846 return;
848 kill_thread( thread, 1 );
852 /* initialize a new thread */
853 DECL_HANDLER(init_thread)
855 int reply_fd = thread_get_inflight_fd( current, req->reply_fd );
856 int wait_fd = thread_get_inflight_fd( current, req->wait_fd );
858 if (current->unix_pid != -1)
860 fatal_protocol_error( current, "init_thread: already running\n" );
861 goto error;
863 if (reply_fd == -1 || fcntl( reply_fd, F_SETFL, O_NONBLOCK ) == -1)
865 fatal_protocol_error( current, "bad reply fd\n" );
866 goto error;
868 if (wait_fd == -1)
870 fatal_protocol_error( current, "bad wait fd\n" );
871 goto error;
873 if (!(current->reply_fd = create_anonymous_fd( &thread_fd_ops, reply_fd, &current->obj )))
875 reply_fd = -1;
876 fatal_protocol_error( current, "could not allocate reply fd\n" );
877 goto error;
879 if (!(current->wait_fd = create_anonymous_fd( &thread_fd_ops, wait_fd, &current->obj )))
880 return;
882 current->unix_pid = req->unix_pid;
883 current->unix_tid = req->unix_tid;
884 current->teb = req->teb;
886 if (current->suspend + current->process->suspend > 0) stop_thread( current );
887 if (current->process->running_threads > 1)
888 generate_debug_event( current, CREATE_THREAD_DEBUG_EVENT, req->entry );
890 reply->pid = get_process_id( current->process );
891 reply->tid = get_thread_id( current );
892 reply->boot = (current == booting_thread);
893 reply->version = SERVER_PROTOCOL_VERSION;
894 return;
896 error:
897 if (reply_fd != -1) close( reply_fd );
898 if (wait_fd != -1) close( wait_fd );
901 /* terminate a thread */
902 DECL_HANDLER(terminate_thread)
904 struct thread *thread;
906 reply->self = 0;
907 reply->last = 0;
908 if ((thread = get_thread_from_handle( req->handle, THREAD_TERMINATE )))
910 thread->exit_code = req->exit_code;
911 if (thread != current) kill_thread( thread, 1 );
912 else
914 reply->self = 1;
915 reply->last = (thread->process->running_threads == 1);
917 release_object( thread );
921 /* open a handle to a thread */
922 DECL_HANDLER(open_thread)
924 struct thread *thread = get_thread_from_id( req->tid );
926 reply->handle = 0;
927 if (thread)
929 reply->handle = alloc_handle( current->process, thread, req->access, req->inherit );
930 release_object( thread );
934 /* fetch information about a thread */
935 DECL_HANDLER(get_thread_info)
937 struct thread *thread;
938 obj_handle_t handle = req->handle;
940 if (!handle) thread = get_thread_from_id( req->tid_in );
941 else thread = get_thread_from_handle( req->handle, THREAD_QUERY_INFORMATION );
943 if (thread)
945 reply->pid = get_process_id( thread->process );
946 reply->tid = get_thread_id( thread );
947 reply->teb = thread->teb;
948 reply->exit_code = (thread->state == TERMINATED) ? thread->exit_code : STILL_ACTIVE;
949 reply->priority = thread->priority;
950 reply->affinity = thread->affinity;
951 reply->creation_time = thread->creation_time;
952 reply->exit_time = thread->exit_time;
954 release_object( thread );
958 /* set information about a thread */
959 DECL_HANDLER(set_thread_info)
961 struct thread *thread;
963 if ((thread = get_thread_from_handle( req->handle, THREAD_SET_INFORMATION )))
965 set_thread_info( thread, req );
966 release_object( thread );
970 /* suspend a thread */
971 DECL_HANDLER(suspend_thread)
973 struct thread *thread;
975 if ((thread = get_thread_from_handle( req->handle, THREAD_SUSPEND_RESUME )))
977 if (thread->state == TERMINATED) set_error( STATUS_ACCESS_DENIED );
978 else reply->count = suspend_thread( thread );
979 release_object( thread );
983 /* resume a thread */
984 DECL_HANDLER(resume_thread)
986 struct thread *thread;
988 if ((thread = get_thread_from_handle( req->handle, THREAD_SUSPEND_RESUME )))
990 if (thread->state == TERMINATED) set_error( STATUS_ACCESS_DENIED );
991 else reply->count = resume_thread( thread );
992 release_object( thread );
996 /* select on a handle list */
997 DECL_HANDLER(select)
999 int count = get_req_data_size() / sizeof(int);
1000 select_on( count, req->cookie, get_req_data(), req->flags, &req->timeout, req->signal );
1003 /* queue an APC for a thread */
1004 DECL_HANDLER(queue_apc)
1006 struct thread *thread;
1007 if ((thread = get_thread_from_handle( req->handle, THREAD_SET_CONTEXT )))
1009 thread_queue_apc( thread, NULL, req->func, APC_USER, !req->user,
1010 req->arg1, req->arg2, req->arg3 );
1011 release_object( thread );
1015 /* get next APC to call */
1016 DECL_HANDLER(get_apc)
1018 struct thread_apc *apc;
1020 for (;;)
1022 if (!(apc = thread_dequeue_apc( current, !req->alertable )))
1024 /* no more APCs */
1025 reply->func = NULL;
1026 reply->type = APC_NONE;
1027 return;
1029 /* Optimization: ignore APCs that have a NULL func; they are only used
1030 * to wake up a thread, but since we got here the thread woke up already.
1031 * Exception: for APC_ASYNC_IO, func == NULL is legal.
1033 if (apc->func || apc->type == APC_ASYNC_IO) break;
1034 free( apc );
1036 reply->func = apc->func;
1037 reply->type = apc->type;
1038 reply->arg1 = apc->arg1;
1039 reply->arg2 = apc->arg2;
1040 reply->arg3 = apc->arg3;
1041 free( apc );
1044 /* fetch a selector entry for a thread */
1045 DECL_HANDLER(get_selector_entry)
1047 struct thread *thread;
1048 if ((thread = get_thread_from_handle( req->handle, THREAD_QUERY_INFORMATION )))
1050 get_selector_entry( thread, req->entry, &reply->base, &reply->limit, &reply->flags );
1051 release_object( thread );