Don't dump content of output buffers.
[wine/wine-kai.git] / server / thread.c
blob819fd9f78baedb4f57fb7a6834cb2c964990ac5c
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;
144 list_init( &thread->mutex_list );
145 list_init( &thread->system_apc );
146 list_init( &thread->user_apc );
148 for (i = 0; i < MAX_INFLIGHT_FDS; i++)
149 thread->inflight[i].server = thread->inflight[i].client = -1;
152 /* create a new thread */
153 struct thread *create_thread( int fd, struct process *process )
155 struct thread *thread;
157 if (!(thread = alloc_object( &thread_ops ))) return NULL;
159 init_thread_structure( thread );
161 thread->process = (struct process *)grab_object( process );
162 thread->desktop = process->desktop;
163 if (!current) current = thread;
165 if (!booting_thread) /* first thread ever */
167 booting_thread = thread;
168 lock_master_socket(1);
171 list_add_head( &thread_list, &thread->entry );
173 if (!(thread->id = alloc_ptid( thread )))
175 release_object( thread );
176 return NULL;
178 if (!(thread->request_fd = create_anonymous_fd( &thread_fd_ops, fd, &thread->obj )))
180 release_object( thread );
181 return NULL;
184 thread->token = (struct token *) grab_object( process->token );
186 set_fd_events( thread->request_fd, POLLIN ); /* start listening to events */
187 add_process_thread( thread->process, thread );
188 return thread;
191 /* handle a client event */
192 static void thread_poll_event( struct fd *fd, int event )
194 struct thread *thread = get_fd_user( fd );
195 assert( thread->obj.ops == &thread_ops );
197 if (event & (POLLERR | POLLHUP)) kill_thread( thread, 0 );
198 else if (event & POLLIN) read_request( thread );
199 else if (event & POLLOUT) write_reply( thread );
202 /* cleanup everything that is no longer needed by a dead thread */
203 /* used by destroy_thread and kill_thread */
204 static void cleanup_thread( struct thread *thread )
206 int i;
207 struct thread_apc *apc;
209 while ((apc = thread_dequeue_apc( thread, 0 ))) free( apc );
210 if (thread->req_data) free( thread->req_data );
211 if (thread->reply_data) free( thread->reply_data );
212 if (thread->request_fd) release_object( thread->request_fd );
213 if (thread->reply_fd) release_object( thread->reply_fd );
214 if (thread->wait_fd) release_object( thread->wait_fd );
215 free_msg_queue( thread );
216 cleanup_clipboard_thread(thread);
217 destroy_thread_windows( thread );
218 close_thread_desktop( thread );
219 for (i = 0; i < MAX_INFLIGHT_FDS; i++)
221 if (thread->inflight[i].client != -1)
223 close( thread->inflight[i].server );
224 thread->inflight[i].client = thread->inflight[i].server = -1;
227 thread->req_data = NULL;
228 thread->reply_data = NULL;
229 thread->request_fd = NULL;
230 thread->reply_fd = NULL;
231 thread->wait_fd = NULL;
232 thread->desktop = 0;
234 if (thread == booting_thread) /* killing booting thread */
236 booting_thread = NULL;
237 lock_master_socket(0);
241 /* destroy a thread when its refcount is 0 */
242 static void destroy_thread( struct object *obj )
244 struct thread_apc *apc;
245 struct thread *thread = (struct thread *)obj;
246 assert( obj->ops == &thread_ops );
248 assert( !thread->debug_ctx ); /* cannot still be debugging something */
249 list_remove( &thread->entry );
250 while ((apc = thread_dequeue_apc( thread, 0 ))) free( apc );
251 cleanup_thread( thread );
252 release_object( thread->process );
253 if (thread->id) free_ptid( thread->id );
254 if (thread->token) release_object( thread->token );
257 /* dump a thread on stdout for debugging purposes */
258 static void dump_thread( struct object *obj, int verbose )
260 struct thread *thread = (struct thread *)obj;
261 assert( obj->ops == &thread_ops );
263 fprintf( stderr, "Thread id=%04x unix pid=%d unix tid=%d teb=%p state=%d\n",
264 thread->id, thread->unix_pid, thread->unix_tid, thread->teb, thread->state );
267 static int thread_signaled( struct object *obj, struct thread *thread )
269 struct thread *mythread = (struct thread *)obj;
270 return (mythread->state == TERMINATED);
273 /* get a thread pointer from a thread id (and increment the refcount) */
274 struct thread *get_thread_from_id( thread_id_t id )
276 struct object *obj = get_ptid_entry( id );
278 if (obj && obj->ops == &thread_ops) return (struct thread *)grab_object( obj );
279 set_win32_error( ERROR_INVALID_THREAD_ID );
280 return NULL;
283 /* get a thread from a handle (and increment the refcount) */
284 struct thread *get_thread_from_handle( obj_handle_t handle, unsigned int access )
286 return (struct thread *)get_handle_obj( current->process, handle,
287 access, &thread_ops );
290 /* find a thread from a Unix pid */
291 struct thread *get_thread_from_pid( int pid )
293 struct thread *thread;
295 LIST_FOR_EACH_ENTRY( thread, &thread_list, struct thread, entry )
297 if (thread->unix_tid == pid) return thread;
299 LIST_FOR_EACH_ENTRY( thread, &thread_list, struct thread, entry )
301 if (thread->unix_pid == pid) return thread;
303 return NULL;
306 /* set all information about a thread */
307 static void set_thread_info( struct thread *thread,
308 const struct set_thread_info_request *req )
310 if (req->mask & SET_THREAD_INFO_PRIORITY)
311 thread->priority = req->priority;
312 if (req->mask & SET_THREAD_INFO_AFFINITY)
314 if (req->affinity != 1) set_error( STATUS_INVALID_PARAMETER );
315 else thread->affinity = req->affinity;
317 if (req->mask & SET_THREAD_INFO_TOKEN)
318 security_set_thread_token( thread, req->token );
321 /* stop a thread (at the Unix level) */
322 void stop_thread( struct thread *thread )
324 /* can't stop a thread while initialisation is in progress */
325 if (is_process_init_done(thread->process)) send_thread_signal( thread, SIGUSR1 );
328 /* suspend a thread */
329 static int suspend_thread( struct thread *thread )
331 int old_count = thread->suspend;
332 if (thread->suspend < MAXIMUM_SUSPEND_COUNT)
334 if (!(thread->process->suspend + thread->suspend++)) stop_thread( thread );
336 else set_error( STATUS_SUSPEND_COUNT_EXCEEDED );
337 return old_count;
340 /* resume a thread */
341 static int resume_thread( struct thread *thread )
343 int old_count = thread->suspend;
344 if (thread->suspend > 0)
346 if (!(--thread->suspend + thread->process->suspend)) wake_thread( thread );
348 return old_count;
351 /* add a thread to an object wait queue; return 1 if OK, 0 on error */
352 int add_queue( struct object *obj, struct wait_queue_entry *entry )
354 grab_object( obj );
355 entry->obj = obj;
356 list_add_tail( &obj->wait_queue, &entry->entry );
357 return 1;
360 /* remove a thread from an object wait queue */
361 void remove_queue( struct object *obj, struct wait_queue_entry *entry )
363 list_remove( &entry->entry );
364 release_object( obj );
367 /* finish waiting */
368 static void end_wait( struct thread *thread )
370 struct thread_wait *wait = thread->wait;
371 struct wait_queue_entry *entry;
372 int i;
374 assert( wait );
375 for (i = 0, entry = wait->queues; i < wait->count; i++, entry++)
376 entry->obj->ops->remove_queue( entry->obj, entry );
377 if (wait->user) remove_timeout_user( wait->user );
378 thread->wait = wait->next;
379 free( wait );
382 /* build the thread wait structure */
383 static int wait_on( int count, struct object *objects[], int flags, const abs_time_t *timeout )
385 struct thread_wait *wait;
386 struct wait_queue_entry *entry;
387 int i;
389 if (!(wait = mem_alloc( sizeof(*wait) + (count-1) * sizeof(*entry) ))) return 0;
390 wait->next = current->wait;
391 wait->thread = current;
392 wait->count = count;
393 wait->flags = flags;
394 wait->user = NULL;
395 current->wait = wait;
396 if (flags & SELECT_TIMEOUT)
398 wait->timeout.tv_sec = timeout->sec;
399 wait->timeout.tv_usec = timeout->usec;
402 for (i = 0, entry = wait->queues; i < count; i++, entry++)
404 struct object *obj = objects[i];
405 entry->thread = current;
406 if (!obj->ops->add_queue( obj, entry ))
408 wait->count = i;
409 end_wait( current );
410 return 0;
413 return 1;
416 /* check if the thread waiting condition is satisfied */
417 static int check_wait( struct thread *thread )
419 int i, signaled;
420 struct thread_wait *wait = thread->wait;
421 struct wait_queue_entry *entry = wait->queues;
423 /* Suspended threads may not acquire locks */
424 if (thread->process->suspend + thread->suspend > 0) return -1;
426 assert( wait );
427 if (wait->flags & SELECT_ALL)
429 int not_ok = 0;
430 /* Note: we must check them all anyway, as some objects may
431 * want to do something when signaled, even if others are not */
432 for (i = 0, entry = wait->queues; i < wait->count; i++, entry++)
433 not_ok |= !entry->obj->ops->signaled( entry->obj, thread );
434 if (not_ok) goto other_checks;
435 /* Wait satisfied: tell it to all objects */
436 signaled = 0;
437 for (i = 0, entry = wait->queues; i < wait->count; i++, entry++)
438 if (entry->obj->ops->satisfied( entry->obj, thread ))
439 signaled = STATUS_ABANDONED_WAIT_0;
440 return signaled;
442 else
444 for (i = 0, entry = wait->queues; i < wait->count; i++, entry++)
446 if (!entry->obj->ops->signaled( entry->obj, thread )) continue;
447 /* Wait satisfied: tell it to the object */
448 signaled = i;
449 if (entry->obj->ops->satisfied( entry->obj, thread ))
450 signaled = i + STATUS_ABANDONED_WAIT_0;
451 return signaled;
455 other_checks:
456 if ((wait->flags & SELECT_INTERRUPTIBLE) && !list_empty(&thread->system_apc)) return STATUS_USER_APC;
457 if ((wait->flags & SELECT_ALERTABLE) && !list_empty(&thread->user_apc)) return STATUS_USER_APC;
458 if (wait->flags & SELECT_TIMEOUT)
460 struct timeval now;
461 gettimeofday( &now, NULL );
462 if (!time_before( &now, &wait->timeout )) return STATUS_TIMEOUT;
464 return -1;
467 /* send the wakeup signal to a thread */
468 static int send_thread_wakeup( struct thread *thread, void *cookie, int signaled )
470 struct wake_up_reply reply;
471 int ret;
473 reply.cookie = cookie;
474 reply.signaled = signaled;
475 if ((ret = write( get_unix_fd( thread->wait_fd ), &reply, sizeof(reply) )) == sizeof(reply))
476 return 0;
477 if (ret >= 0)
478 fatal_protocol_error( thread, "partial wakeup write %d\n", ret );
479 else if (errno == EPIPE)
480 kill_thread( thread, 0 ); /* normal death */
481 else
482 fatal_protocol_perror( thread, "write" );
483 return -1;
486 /* attempt to wake up a thread */
487 /* return >0 if OK, 0 if the wait condition is still not satisfied */
488 int wake_thread( struct thread *thread )
490 int signaled, count;
491 void *cookie;
493 for (count = 0; thread->wait; count++)
495 if ((signaled = check_wait( thread )) == -1) break;
497 cookie = thread->wait->cookie;
498 if (debug_level) fprintf( stderr, "%04x: *wakeup* signaled=%d cookie=%p\n",
499 thread->id, signaled, cookie );
500 end_wait( thread );
501 if (send_thread_wakeup( thread, cookie, signaled ) == -1) /* error */
502 break;
504 return count;
507 /* thread wait timeout */
508 static void thread_timeout( void *ptr )
510 struct thread_wait *wait = ptr;
511 struct thread *thread = wait->thread;
512 void *cookie = wait->cookie;
514 wait->user = NULL;
515 if (thread->wait != wait) return; /* not the top-level wait, ignore it */
516 if (thread->suspend + thread->process->suspend > 0) return; /* suspended, ignore it */
518 if (debug_level) fprintf( stderr, "%04x: *wakeup* signaled=%d cookie=%p\n",
519 thread->id, STATUS_TIMEOUT, cookie );
520 end_wait( thread );
521 if (send_thread_wakeup( thread, cookie, STATUS_TIMEOUT ) == -1) return;
522 /* check if other objects have become signaled in the meantime */
523 wake_thread( thread );
526 /* try signaling an event flag, a semaphore or a mutex */
527 static int signal_object( obj_handle_t handle )
529 struct object *obj;
530 int ret = 0;
532 obj = get_handle_obj( current->process, handle, 0, NULL );
533 if (obj)
535 ret = obj->ops->signal( obj, get_handle_access( current->process, handle ));
536 release_object( obj );
538 return ret;
541 /* select on a list of handles */
542 static void select_on( int count, void *cookie, const obj_handle_t *handles,
543 int flags, const abs_time_t *timeout, obj_handle_t signal_obj )
545 int ret, i;
546 struct object *objects[MAXIMUM_WAIT_OBJECTS];
548 if ((count < 0) || (count > MAXIMUM_WAIT_OBJECTS))
550 set_error( STATUS_INVALID_PARAMETER );
551 return;
553 for (i = 0; i < count; i++)
555 if (!(objects[i] = get_handle_obj( current->process, handles[i], SYNCHRONIZE, NULL )))
556 break;
559 if (i < count) goto done;
560 if (!wait_on( count, objects, flags, timeout )) goto done;
562 /* signal the object */
563 if (signal_obj)
565 if (!signal_object( signal_obj ))
567 end_wait( current );
568 goto done;
570 /* check if we woke ourselves up */
571 if (!current->wait) goto done;
574 if ((ret = check_wait( current )) != -1)
576 /* condition is already satisfied */
577 end_wait( current );
578 set_error( ret );
579 goto done;
582 /* now we need to wait */
583 if (flags & SELECT_TIMEOUT)
585 if (!(current->wait->user = add_timeout_user( &current->wait->timeout,
586 thread_timeout, current->wait )))
588 end_wait( current );
589 goto done;
592 current->wait->cookie = cookie;
593 set_error( STATUS_PENDING );
595 done:
596 while (--i >= 0) release_object( objects[i] );
599 /* attempt to wake threads sleeping on the object wait queue */
600 void wake_up( struct object *obj, int max )
602 struct list *ptr, *next;
604 LIST_FOR_EACH_SAFE( ptr, next, &obj->wait_queue )
606 struct wait_queue_entry *entry = LIST_ENTRY( ptr, struct wait_queue_entry, entry );
607 if (wake_thread( entry->thread ))
609 if (max && !--max) break;
614 /* queue an async procedure call */
615 int thread_queue_apc( struct thread *thread, struct object *owner, void *func,
616 enum apc_type type, int system, void *arg1, void *arg2, void *arg3 )
618 struct thread_apc *apc;
619 struct list *queue = system ? &thread->system_apc : &thread->user_apc;
621 /* cancel a possible previous APC with the same owner */
622 if (owner) thread_cancel_apc( thread, owner, system );
623 if (thread->state == TERMINATED) return 0;
625 if (!(apc = mem_alloc( sizeof(*apc) ))) return 0;
626 apc->owner = owner;
627 apc->func = func;
628 apc->type = type;
629 apc->arg1 = arg1;
630 apc->arg2 = arg2;
631 apc->arg3 = arg3;
632 list_add_tail( queue, &apc->entry );
633 if (!list_prev( queue, &apc->entry )) /* first one */
634 wake_thread( thread );
636 return 1;
639 /* cancel the async procedure call owned by a specific object */
640 void thread_cancel_apc( struct thread *thread, struct object *owner, int system )
642 struct thread_apc *apc;
643 struct list *queue = system ? &thread->system_apc : &thread->user_apc;
644 LIST_FOR_EACH_ENTRY( apc, queue, struct thread_apc, entry )
646 if (apc->owner != owner) continue;
647 list_remove( &apc->entry );
648 free( apc );
649 return;
653 /* remove the head apc from the queue; the returned pointer must be freed by the caller */
654 static struct thread_apc *thread_dequeue_apc( struct thread *thread, int system_only )
656 struct thread_apc *apc = NULL;
657 struct list *ptr = list_head( &thread->system_apc );
659 if (!ptr && !system_only) ptr = list_head( &thread->user_apc );
660 if (ptr)
662 apc = LIST_ENTRY( ptr, struct thread_apc, entry );
663 list_remove( ptr );
665 return apc;
668 /* add an fd to the inflight list */
669 /* return list index, or -1 on error */
670 int thread_add_inflight_fd( struct thread *thread, int client, int server )
672 int i;
674 if (server == -1) return -1;
675 if (client == -1)
677 close( server );
678 return -1;
681 /* first check if we already have an entry for this fd */
682 for (i = 0; i < MAX_INFLIGHT_FDS; i++)
683 if (thread->inflight[i].client == client)
685 close( thread->inflight[i].server );
686 thread->inflight[i].server = server;
687 return i;
690 /* now find a free spot to store it */
691 for (i = 0; i < MAX_INFLIGHT_FDS; i++)
692 if (thread->inflight[i].client == -1)
694 thread->inflight[i].client = client;
695 thread->inflight[i].server = server;
696 return i;
698 return -1;
701 /* get an inflight fd and purge it from the list */
702 /* the fd must be closed when no longer used */
703 int thread_get_inflight_fd( struct thread *thread, int client )
705 int i, ret;
707 if (client == -1) return -1;
711 for (i = 0; i < MAX_INFLIGHT_FDS; i++)
713 if (thread->inflight[i].client == client)
715 ret = thread->inflight[i].server;
716 thread->inflight[i].server = thread->inflight[i].client = -1;
717 return ret;
720 } while (!receive_fd( thread->process )); /* in case it is still in the socket buffer */
721 return -1;
724 /* retrieve an LDT selector entry */
725 static void get_selector_entry( struct thread *thread, int entry,
726 unsigned int *base, unsigned int *limit,
727 unsigned char *flags )
729 if (!thread->process->ldt_copy)
731 set_error( STATUS_ACCESS_DENIED );
732 return;
734 if (entry >= 8192)
736 set_error( STATUS_INVALID_PARAMETER ); /* FIXME */
737 return;
739 if (suspend_for_ptrace( thread ))
741 unsigned char flags_buf[4];
742 int *addr = (int *)thread->process->ldt_copy + entry;
743 if (read_thread_int( thread, addr, base ) == -1) goto done;
744 if (read_thread_int( thread, addr + 8192, limit ) == -1) goto done;
745 addr = (int *)thread->process->ldt_copy + 2*8192 + (entry >> 2);
746 if (read_thread_int( thread, addr, (int *)flags_buf ) == -1) goto done;
747 *flags = flags_buf[entry & 3];
748 done:
749 resume_after_ptrace( thread );
753 /* kill a thread on the spot */
754 void kill_thread( struct thread *thread, int violent_death )
756 if (thread->state == TERMINATED) return; /* already killed */
757 thread->state = TERMINATED;
758 thread->exit_time = time(NULL);
759 if (current == thread) current = NULL;
760 if (debug_level)
761 fprintf( stderr,"%04x: *killed* exit_code=%d\n",
762 thread->id, thread->exit_code );
763 if (thread->wait)
765 while (thread->wait) end_wait( thread );
766 send_thread_wakeup( thread, NULL, STATUS_PENDING );
767 /* if it is waiting on the socket, we don't need to send a SIGTERM */
768 violent_death = 0;
770 kill_console_processes( thread, 0 );
771 debug_exit_thread( thread );
772 abandon_mutexes( thread );
773 remove_process_thread( thread->process, thread );
774 wake_up( &thread->obj, 0 );
775 detach_thread( thread, violent_death ? SIGTERM : 0 );
776 cleanup_thread( thread );
777 release_object( thread );
780 /* take a snapshot of currently running threads */
781 struct thread_snapshot *thread_snap( int *count )
783 struct thread_snapshot *snapshot, *ptr;
784 struct thread *thread;
785 int total = 0;
787 LIST_FOR_EACH_ENTRY( thread, &thread_list, struct thread, entry )
788 if (thread->state != TERMINATED) total++;
789 if (!total || !(snapshot = mem_alloc( sizeof(*snapshot) * total ))) return NULL;
790 ptr = snapshot;
791 LIST_FOR_EACH_ENTRY( thread, &thread_list, struct thread, entry )
793 if (thread->state == TERMINATED) continue;
794 ptr->thread = thread;
795 ptr->count = thread->obj.refcount;
796 ptr->priority = thread->priority;
797 grab_object( thread );
798 ptr++;
800 *count = total;
801 return snapshot;
804 /* gets the current impersonation token */
805 struct token *thread_get_impersonation_token( struct thread *thread )
807 if (thread->token)
808 return thread->token;
809 else
810 return thread->process->token;
813 /* signal that we are finished booting on the client side */
814 DECL_HANDLER(boot_done)
816 debug_level = max( debug_level, req->debug_level );
817 if (current == booting_thread)
819 booting_thread = (struct thread *)~0UL; /* make sure it doesn't match other threads */
820 lock_master_socket(0); /* allow other clients now */
824 /* create a new thread */
825 DECL_HANDLER(new_thread)
827 struct thread *thread;
828 int request_fd = thread_get_inflight_fd( current, req->request_fd );
830 if (request_fd == -1 || fcntl( request_fd, F_SETFL, O_NONBLOCK ) == -1)
832 if (request_fd != -1) close( request_fd );
833 set_error( STATUS_INVALID_HANDLE );
834 return;
837 if ((thread = create_thread( request_fd, current->process )))
839 if (req->suspend) thread->suspend++;
840 reply->tid = get_thread_id( thread );
841 if ((reply->handle = alloc_handle( current->process, thread,
842 THREAD_ALL_ACCESS, req->inherit )))
844 /* thread object will be released when the thread gets killed */
845 return;
847 kill_thread( thread, 1 );
851 /* initialize a new thread */
852 DECL_HANDLER(init_thread)
854 int reply_fd = thread_get_inflight_fd( current, req->reply_fd );
855 int wait_fd = thread_get_inflight_fd( current, req->wait_fd );
857 if (current->unix_pid != -1)
859 fatal_protocol_error( current, "init_thread: already running\n" );
860 goto error;
862 if (reply_fd == -1 || fcntl( reply_fd, F_SETFL, O_NONBLOCK ) == -1)
864 fatal_protocol_error( current, "bad reply fd\n" );
865 goto error;
867 if (wait_fd == -1)
869 fatal_protocol_error( current, "bad wait fd\n" );
870 goto error;
872 if (!(current->reply_fd = create_anonymous_fd( &thread_fd_ops, reply_fd, &current->obj )))
874 reply_fd = -1;
875 fatal_protocol_error( current, "could not allocate reply fd\n" );
876 goto error;
878 if (!(current->wait_fd = create_anonymous_fd( &thread_fd_ops, wait_fd, &current->obj )))
879 return;
881 current->unix_pid = req->unix_pid;
882 current->unix_tid = req->unix_tid;
883 current->teb = req->teb;
885 if (current->suspend + current->process->suspend > 0) stop_thread( current );
886 if (current->process->running_threads > 1)
887 generate_debug_event( current, CREATE_THREAD_DEBUG_EVENT, req->entry );
889 reply->pid = get_process_id( current->process );
890 reply->tid = get_thread_id( current );
891 reply->boot = (current == booting_thread);
892 reply->version = SERVER_PROTOCOL_VERSION;
893 return;
895 error:
896 if (reply_fd != -1) close( reply_fd );
897 if (wait_fd != -1) close( wait_fd );
900 /* terminate a thread */
901 DECL_HANDLER(terminate_thread)
903 struct thread *thread;
905 reply->self = 0;
906 reply->last = 0;
907 if ((thread = get_thread_from_handle( req->handle, THREAD_TERMINATE )))
909 thread->exit_code = req->exit_code;
910 if (thread != current) kill_thread( thread, 1 );
911 else
913 reply->self = 1;
914 reply->last = (thread->process->running_threads == 1);
916 release_object( thread );
920 /* open a handle to a thread */
921 DECL_HANDLER(open_thread)
923 struct thread *thread = get_thread_from_id( req->tid );
925 reply->handle = 0;
926 if (thread)
928 reply->handle = alloc_handle( current->process, thread, req->access, req->inherit );
929 release_object( thread );
933 /* fetch information about a thread */
934 DECL_HANDLER(get_thread_info)
936 struct thread *thread;
937 obj_handle_t handle = req->handle;
939 if (!handle) thread = get_thread_from_id( req->tid_in );
940 else thread = get_thread_from_handle( req->handle, THREAD_QUERY_INFORMATION );
942 if (thread)
944 reply->pid = get_process_id( thread->process );
945 reply->tid = get_thread_id( thread );
946 reply->teb = thread->teb;
947 reply->exit_code = (thread->state == TERMINATED) ? thread->exit_code : STILL_ACTIVE;
948 reply->priority = thread->priority;
949 reply->affinity = thread->affinity;
950 reply->creation_time = thread->creation_time;
951 reply->exit_time = thread->exit_time;
953 release_object( thread );
957 /* set information about a thread */
958 DECL_HANDLER(set_thread_info)
960 struct thread *thread;
962 if ((thread = get_thread_from_handle( req->handle, THREAD_SET_INFORMATION )))
964 set_thread_info( thread, req );
965 release_object( thread );
969 /* suspend a thread */
970 DECL_HANDLER(suspend_thread)
972 struct thread *thread;
974 if ((thread = get_thread_from_handle( req->handle, THREAD_SUSPEND_RESUME )))
976 if (thread->state == TERMINATED) set_error( STATUS_ACCESS_DENIED );
977 else reply->count = suspend_thread( thread );
978 release_object( thread );
982 /* resume a thread */
983 DECL_HANDLER(resume_thread)
985 struct thread *thread;
987 if ((thread = get_thread_from_handle( req->handle, THREAD_SUSPEND_RESUME )))
989 if (thread->state == TERMINATED) set_error( STATUS_ACCESS_DENIED );
990 else reply->count = resume_thread( thread );
991 release_object( thread );
995 /* select on a handle list */
996 DECL_HANDLER(select)
998 int count = get_req_data_size() / sizeof(int);
999 select_on( count, req->cookie, get_req_data(), req->flags, &req->timeout, req->signal );
1002 /* queue an APC for a thread */
1003 DECL_HANDLER(queue_apc)
1005 struct thread *thread;
1006 if ((thread = get_thread_from_handle( req->handle, THREAD_SET_CONTEXT )))
1008 thread_queue_apc( thread, NULL, req->func, APC_USER, !req->user,
1009 req->arg1, req->arg2, req->arg3 );
1010 release_object( thread );
1014 /* get next APC to call */
1015 DECL_HANDLER(get_apc)
1017 struct thread_apc *apc;
1019 for (;;)
1021 if (!(apc = thread_dequeue_apc( current, !req->alertable )))
1023 /* no more APCs */
1024 reply->func = NULL;
1025 reply->type = APC_NONE;
1026 return;
1028 /* Optimization: ignore APCs that have a NULL func; they are only used
1029 * to wake up a thread, but since we got here the thread woke up already.
1030 * Exception: for APC_ASYNC_IO, func == NULL is legal.
1032 if (apc->func || apc->type == APC_ASYNC_IO) break;
1033 free( apc );
1035 reply->func = apc->func;
1036 reply->type = apc->type;
1037 reply->arg1 = apc->arg1;
1038 reply->arg2 = apc->arg2;
1039 reply->arg3 = apc->arg3;
1040 free( apc );
1043 /* fetch a selector entry for a thread */
1044 DECL_HANDLER(get_selector_entry)
1046 struct thread *thread;
1047 if ((thread = get_thread_from_handle( req->handle, THREAD_QUERY_INFORMATION )))
1049 get_selector_entry( thread, req->entry, &reply->base, &reply->limit, &reply->flags );
1050 release_object( thread );