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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include "wine/port.h"
32 #include <sys/types.h>
40 #define WIN32_NO_STATUS
57 struct thread_wait
*next
; /* next wait structure for this thread */
58 struct thread
*thread
; /* owner thread */
59 int count
; /* count of objects */
61 void *cookie
; /* magic cookie to return to client */
62 struct timeval timeout
;
63 struct timeout_user
*user
;
64 struct wait_queue_entry queues
[1];
67 /* asynchronous procedure calls */
71 struct object obj
; /* object header */
72 struct list entry
; /* queue linked list */
73 struct object
*owner
; /* object that queued this apc */
74 int executed
; /* has it been executed by the client? */
78 static void dump_thread_apc( struct object
*obj
, int verbose
);
79 static int thread_apc_signaled( struct object
*obj
, struct thread
*thread
);
81 static const struct object_ops thread_apc_ops
=
83 sizeof(struct thread_apc
), /* size */
84 dump_thread_apc
, /* dump */
85 add_queue
, /* add_queue */
86 remove_queue
, /* remove_queue */
87 thread_apc_signaled
, /* signaled */
88 no_satisfied
, /* satisfied */
89 no_signal
, /* signal */
90 no_get_fd
, /* get_fd */
91 no_map_access
, /* map_access */
92 no_lookup_name
, /* lookup_name */
93 no_close_handle
, /* close_handle */
94 no_destroy
/* destroy */
98 /* thread operations */
100 static void dump_thread( struct object
*obj
, int verbose
);
101 static int thread_signaled( struct object
*obj
, struct thread
*thread
);
102 static unsigned int thread_map_access( struct object
*obj
, unsigned int access
);
103 static void thread_poll_event( struct fd
*fd
, int event
);
104 static void destroy_thread( struct object
*obj
);
105 static struct thread_apc
*thread_dequeue_apc( struct thread
*thread
, int system_only
);
107 static const struct object_ops thread_ops
=
109 sizeof(struct thread
), /* size */
110 dump_thread
, /* dump */
111 add_queue
, /* add_queue */
112 remove_queue
, /* remove_queue */
113 thread_signaled
, /* signaled */
114 no_satisfied
, /* satisfied */
115 no_signal
, /* signal */
116 no_get_fd
, /* get_fd */
117 thread_map_access
, /* map_access */
118 no_lookup_name
, /* lookup_name */
119 no_close_handle
, /* close_handle */
120 destroy_thread
/* destroy */
123 static const struct fd_ops thread_fd_ops
=
125 NULL
, /* get_poll_events */
126 thread_poll_event
, /* poll_event */
127 no_flush
, /* flush */
128 no_get_file_info
, /* get_file_info */
129 no_queue_async
, /* queue_async */
130 no_cancel_async
/* cancel_async */
133 static struct list thread_list
= LIST_INIT(thread_list
);
135 /* initialize the structure for a newly allocated thread */
136 inline static void init_thread_structure( struct thread
*thread
)
140 thread
->unix_pid
= -1; /* not known yet */
141 thread
->unix_tid
= -1; /* not known yet */
142 thread
->context
= NULL
;
143 thread
->suspend_context
= NULL
;
145 thread
->debug_ctx
= NULL
;
146 thread
->debug_event
= NULL
;
147 thread
->debug_break
= 0;
148 thread
->queue
= NULL
;
151 thread
->req_data
= NULL
;
152 thread
->req_toread
= 0;
153 thread
->reply_data
= NULL
;
154 thread
->reply_towrite
= 0;
155 thread
->request_fd
= NULL
;
156 thread
->reply_fd
= NULL
;
157 thread
->wait_fd
= NULL
;
158 thread
->state
= RUNNING
;
159 thread
->exit_code
= 0;
160 thread
->priority
= 0;
161 thread
->affinity
= 1;
163 thread
->desktop_users
= 0;
164 thread
->token
= NULL
;
166 thread
->creation_time
= current_time
;
167 thread
->exit_time
.tv_sec
= thread
->exit_time
.tv_usec
= 0;
169 list_init( &thread
->mutex_list
);
170 list_init( &thread
->system_apc
);
171 list_init( &thread
->user_apc
);
173 for (i
= 0; i
< MAX_INFLIGHT_FDS
; i
++)
174 thread
->inflight
[i
].server
= thread
->inflight
[i
].client
= -1;
177 /* check if address looks valid for a client-side data structure (TEB etc.) */
178 static inline int is_valid_address( void *addr
)
180 return addr
&& !((unsigned long)addr
% sizeof(int));
183 /* create a new thread */
184 struct thread
*create_thread( int fd
, struct process
*process
)
186 struct thread
*thread
;
188 if (!(thread
= alloc_object( &thread_ops
))) return NULL
;
190 init_thread_structure( thread
);
192 thread
->process
= (struct process
*)grab_object( process
);
193 thread
->desktop
= process
->desktop
;
194 if (!current
) current
= thread
;
196 list_add_head( &thread_list
, &thread
->entry
);
198 if (!(thread
->id
= alloc_ptid( thread
)))
200 release_object( thread
);
203 if (!(thread
->request_fd
= create_anonymous_fd( &thread_fd_ops
, fd
, &thread
->obj
)))
205 release_object( thread
);
209 set_fd_events( thread
->request_fd
, POLLIN
); /* start listening to events */
210 add_process_thread( thread
->process
, thread
);
214 /* handle a client event */
215 static void thread_poll_event( struct fd
*fd
, int event
)
217 struct thread
*thread
= get_fd_user( fd
);
218 assert( thread
->obj
.ops
== &thread_ops
);
220 if (event
& (POLLERR
| POLLHUP
)) kill_thread( thread
, 0 );
221 else if (event
& POLLIN
) read_request( thread
);
222 else if (event
& POLLOUT
) write_reply( thread
);
225 /* cleanup everything that is no longer needed by a dead thread */
226 /* used by destroy_thread and kill_thread */
227 static void cleanup_thread( struct thread
*thread
)
230 struct thread_apc
*apc
;
232 while ((apc
= thread_dequeue_apc( thread
, 0 ))) release_object( apc
);
233 free( thread
->req_data
);
234 free( thread
->reply_data
);
235 if (thread
->request_fd
) release_object( thread
->request_fd
);
236 if (thread
->reply_fd
) release_object( thread
->reply_fd
);
237 if (thread
->wait_fd
) release_object( thread
->wait_fd
);
238 free( thread
->suspend_context
);
239 free_msg_queue( thread
);
240 cleanup_clipboard_thread(thread
);
241 destroy_thread_windows( thread
);
242 close_thread_desktop( thread
);
243 for (i
= 0; i
< MAX_INFLIGHT_FDS
; i
++)
245 if (thread
->inflight
[i
].client
!= -1)
247 close( thread
->inflight
[i
].server
);
248 thread
->inflight
[i
].client
= thread
->inflight
[i
].server
= -1;
251 thread
->req_data
= NULL
;
252 thread
->reply_data
= NULL
;
253 thread
->request_fd
= NULL
;
254 thread
->reply_fd
= NULL
;
255 thread
->wait_fd
= NULL
;
256 thread
->context
= NULL
;
257 thread
->suspend_context
= NULL
;
261 /* destroy a thread when its refcount is 0 */
262 static void destroy_thread( struct object
*obj
)
264 struct thread
*thread
= (struct thread
*)obj
;
265 assert( obj
->ops
== &thread_ops
);
267 assert( !thread
->debug_ctx
); /* cannot still be debugging something */
268 list_remove( &thread
->entry
);
269 cleanup_thread( thread
);
270 release_object( thread
->process
);
271 if (thread
->id
) free_ptid( thread
->id
);
272 if (thread
->token
) release_object( thread
->token
);
275 /* dump a thread on stdout for debugging purposes */
276 static void dump_thread( struct object
*obj
, int verbose
)
278 struct thread
*thread
= (struct thread
*)obj
;
279 assert( obj
->ops
== &thread_ops
);
281 fprintf( stderr
, "Thread id=%04x unix pid=%d unix tid=%d teb=%p state=%d\n",
282 thread
->id
, thread
->unix_pid
, thread
->unix_tid
, thread
->teb
, thread
->state
);
285 static int thread_signaled( struct object
*obj
, struct thread
*thread
)
287 struct thread
*mythread
= (struct thread
*)obj
;
288 return (mythread
->state
== TERMINATED
);
291 static unsigned int thread_map_access( struct object
*obj
, unsigned int access
)
293 if (access
& GENERIC_READ
) access
|= STANDARD_RIGHTS_READ
| SYNCHRONIZE
;
294 if (access
& GENERIC_WRITE
) access
|= STANDARD_RIGHTS_WRITE
| SYNCHRONIZE
;
295 if (access
& GENERIC_EXECUTE
) access
|= STANDARD_RIGHTS_EXECUTE
;
296 if (access
& GENERIC_ALL
) access
|= THREAD_ALL_ACCESS
;
297 return access
& ~(GENERIC_READ
| GENERIC_WRITE
| GENERIC_EXECUTE
| GENERIC_ALL
);
300 static void dump_thread_apc( struct object
*obj
, int verbose
)
302 struct thread_apc
*apc
= (struct thread_apc
*)obj
;
303 assert( obj
->ops
== &thread_apc_ops
);
305 fprintf( stderr
, "APC owner=%p type=%u\n", apc
->owner
, apc
->call
.type
);
308 static int thread_apc_signaled( struct object
*obj
, struct thread
*thread
)
310 struct thread_apc
*apc
= (struct thread_apc
*)obj
;
311 return apc
->executed
;
314 /* get a thread pointer from a thread id (and increment the refcount) */
315 struct thread
*get_thread_from_id( thread_id_t id
)
317 struct object
*obj
= get_ptid_entry( id
);
319 if (obj
&& obj
->ops
== &thread_ops
) return (struct thread
*)grab_object( obj
);
320 set_error( STATUS_INVALID_CID
);
324 /* get a thread from a handle (and increment the refcount) */
325 struct thread
*get_thread_from_handle( obj_handle_t handle
, unsigned int access
)
327 return (struct thread
*)get_handle_obj( current
->process
, handle
,
328 access
, &thread_ops
);
331 /* find a thread from a Unix tid */
332 struct thread
*get_thread_from_tid( int tid
)
334 struct thread
*thread
;
336 LIST_FOR_EACH_ENTRY( thread
, &thread_list
, struct thread
, entry
)
338 if (thread
->unix_tid
== tid
) return thread
;
343 /* find a thread from a Unix pid */
344 struct thread
*get_thread_from_pid( int pid
)
346 struct thread
*thread
;
348 LIST_FOR_EACH_ENTRY( thread
, &thread_list
, struct thread
, entry
)
350 if (thread
->unix_pid
== pid
) return thread
;
355 /* set all information about a thread */
356 static void set_thread_info( struct thread
*thread
,
357 const struct set_thread_info_request
*req
)
359 if (req
->mask
& SET_THREAD_INFO_PRIORITY
)
360 thread
->priority
= req
->priority
;
361 if (req
->mask
& SET_THREAD_INFO_AFFINITY
)
363 if (req
->affinity
!= 1) set_error( STATUS_INVALID_PARAMETER
);
364 else thread
->affinity
= req
->affinity
;
366 if (req
->mask
& SET_THREAD_INFO_TOKEN
)
367 security_set_thread_token( thread
, req
->token
);
370 /* stop a thread (at the Unix level) */
371 void stop_thread( struct thread
*thread
)
373 if (thread
->context
) return; /* already inside a debug event, no need for a signal */
374 /* can't stop a thread while initialisation is in progress */
375 if (is_process_init_done(thread
->process
)) send_thread_signal( thread
, SIGUSR1
);
378 /* suspend a thread */
379 static int suspend_thread( struct thread
*thread
)
381 int old_count
= thread
->suspend
;
382 if (thread
->suspend
< MAXIMUM_SUSPEND_COUNT
)
384 if (!(thread
->process
->suspend
+ thread
->suspend
++)) stop_thread( thread
);
386 else set_error( STATUS_SUSPEND_COUNT_EXCEEDED
);
390 /* resume a thread */
391 static int resume_thread( struct thread
*thread
)
393 int old_count
= thread
->suspend
;
394 if (thread
->suspend
> 0)
396 if (!(--thread
->suspend
+ thread
->process
->suspend
)) wake_thread( thread
);
401 /* add a thread to an object wait queue; return 1 if OK, 0 on error */
402 int add_queue( struct object
*obj
, struct wait_queue_entry
*entry
)
406 list_add_tail( &obj
->wait_queue
, &entry
->entry
);
410 /* remove a thread from an object wait queue */
411 void remove_queue( struct object
*obj
, struct wait_queue_entry
*entry
)
413 list_remove( &entry
->entry
);
414 release_object( obj
);
418 static void end_wait( struct thread
*thread
)
420 struct thread_wait
*wait
= thread
->wait
;
421 struct wait_queue_entry
*entry
;
425 for (i
= 0, entry
= wait
->queues
; i
< wait
->count
; i
++, entry
++)
426 entry
->obj
->ops
->remove_queue( entry
->obj
, entry
);
427 if (wait
->user
) remove_timeout_user( wait
->user
);
428 thread
->wait
= wait
->next
;
432 /* build the thread wait structure */
433 static int wait_on( int count
, struct object
*objects
[], int flags
, const abs_time_t
*timeout
)
435 struct thread_wait
*wait
;
436 struct wait_queue_entry
*entry
;
439 if (!(wait
= mem_alloc( sizeof(*wait
) + (count
-1) * sizeof(*entry
) ))) return 0;
440 wait
->next
= current
->wait
;
441 wait
->thread
= current
;
445 current
->wait
= wait
;
446 if (flags
& SELECT_TIMEOUT
)
448 wait
->timeout
.tv_sec
= timeout
->sec
;
449 wait
->timeout
.tv_usec
= timeout
->usec
;
452 for (i
= 0, entry
= wait
->queues
; i
< count
; i
++, entry
++)
454 struct object
*obj
= objects
[i
];
455 entry
->thread
= current
;
456 if (!obj
->ops
->add_queue( obj
, entry
))
466 /* check if the thread waiting condition is satisfied */
467 static int check_wait( struct thread
*thread
)
470 struct thread_wait
*wait
= thread
->wait
;
471 struct wait_queue_entry
*entry
= wait
->queues
;
473 /* Suspended threads may not acquire locks */
474 if (thread
->process
->suspend
+ thread
->suspend
> 0) return -1;
477 if (wait
->flags
& SELECT_ALL
)
480 /* Note: we must check them all anyway, as some objects may
481 * want to do something when signaled, even if others are not */
482 for (i
= 0, entry
= wait
->queues
; i
< wait
->count
; i
++, entry
++)
483 not_ok
|= !entry
->obj
->ops
->signaled( entry
->obj
, thread
);
484 if (not_ok
) goto other_checks
;
485 /* Wait satisfied: tell it to all objects */
487 for (i
= 0, entry
= wait
->queues
; i
< wait
->count
; i
++, entry
++)
488 if (entry
->obj
->ops
->satisfied( entry
->obj
, thread
))
489 signaled
= STATUS_ABANDONED_WAIT_0
;
494 for (i
= 0, entry
= wait
->queues
; i
< wait
->count
; i
++, entry
++)
496 if (!entry
->obj
->ops
->signaled( entry
->obj
, thread
)) continue;
497 /* Wait satisfied: tell it to the object */
499 if (entry
->obj
->ops
->satisfied( entry
->obj
, thread
))
500 signaled
= i
+ STATUS_ABANDONED_WAIT_0
;
506 if ((wait
->flags
& SELECT_INTERRUPTIBLE
) && !list_empty(&thread
->system_apc
)) return STATUS_USER_APC
;
507 if ((wait
->flags
& SELECT_ALERTABLE
) && !list_empty(&thread
->user_apc
)) return STATUS_USER_APC
;
508 if (wait
->flags
& SELECT_TIMEOUT
)
510 if (!time_before( ¤t_time
, &wait
->timeout
)) return STATUS_TIMEOUT
;
515 /* send the wakeup signal to a thread */
516 static int send_thread_wakeup( struct thread
*thread
, void *cookie
, int signaled
)
518 struct wake_up_reply reply
;
521 reply
.cookie
= cookie
;
522 reply
.signaled
= signaled
;
523 if ((ret
= write( get_unix_fd( thread
->wait_fd
), &reply
, sizeof(reply
) )) == sizeof(reply
))
526 fatal_protocol_error( thread
, "partial wakeup write %d\n", ret
);
527 else if (errno
== EPIPE
)
528 kill_thread( thread
, 0 ); /* normal death */
530 fatal_protocol_perror( thread
, "write" );
534 /* attempt to wake up a thread */
535 /* return >0 if OK, 0 if the wait condition is still not satisfied */
536 int wake_thread( struct thread
*thread
)
541 for (count
= 0; thread
->wait
; count
++)
543 if ((signaled
= check_wait( thread
)) == -1) break;
545 cookie
= thread
->wait
->cookie
;
546 if (debug_level
) fprintf( stderr
, "%04x: *wakeup* signaled=%d cookie=%p\n",
547 thread
->id
, signaled
, cookie
);
549 if (send_thread_wakeup( thread
, cookie
, signaled
) == -1) /* error */
555 /* thread wait timeout */
556 static void thread_timeout( void *ptr
)
558 struct thread_wait
*wait
= ptr
;
559 struct thread
*thread
= wait
->thread
;
560 void *cookie
= wait
->cookie
;
563 if (thread
->wait
!= wait
) return; /* not the top-level wait, ignore it */
564 if (thread
->suspend
+ thread
->process
->suspend
> 0) return; /* suspended, ignore it */
566 if (debug_level
) fprintf( stderr
, "%04x: *wakeup* signaled=%d cookie=%p\n",
567 thread
->id
, (int)STATUS_TIMEOUT
, cookie
);
569 if (send_thread_wakeup( thread
, cookie
, STATUS_TIMEOUT
) == -1) return;
570 /* check if other objects have become signaled in the meantime */
571 wake_thread( thread
);
574 /* try signaling an event flag, a semaphore or a mutex */
575 static int signal_object( obj_handle_t handle
)
580 obj
= get_handle_obj( current
->process
, handle
, 0, NULL
);
583 ret
= obj
->ops
->signal( obj
, get_handle_access( current
->process
, handle
));
584 release_object( obj
);
589 /* select on a list of handles */
590 static void select_on( int count
, void *cookie
, const obj_handle_t
*handles
,
591 int flags
, const abs_time_t
*timeout
, obj_handle_t signal_obj
)
594 struct object
*objects
[MAXIMUM_WAIT_OBJECTS
];
596 if ((count
< 0) || (count
> MAXIMUM_WAIT_OBJECTS
))
598 set_error( STATUS_INVALID_PARAMETER
);
601 for (i
= 0; i
< count
; i
++)
603 if (!(objects
[i
] = get_handle_obj( current
->process
, handles
[i
], SYNCHRONIZE
, NULL
)))
607 if (i
< count
) goto done
;
608 if (!wait_on( count
, objects
, flags
, timeout
)) goto done
;
610 /* signal the object */
613 if (!signal_object( signal_obj
))
618 /* check if we woke ourselves up */
619 if (!current
->wait
) goto done
;
622 if ((ret
= check_wait( current
)) != -1)
624 /* condition is already satisfied */
630 /* now we need to wait */
631 if (flags
& SELECT_TIMEOUT
)
633 if (!(current
->wait
->user
= add_timeout_user( ¤t
->wait
->timeout
,
634 thread_timeout
, current
->wait
)))
640 current
->wait
->cookie
= cookie
;
641 set_error( STATUS_PENDING
);
644 while (--i
>= 0) release_object( objects
[i
] );
647 /* attempt to wake threads sleeping on the object wait queue */
648 void wake_up( struct object
*obj
, int max
)
650 struct list
*ptr
, *next
;
652 LIST_FOR_EACH_SAFE( ptr
, next
, &obj
->wait_queue
)
654 struct wait_queue_entry
*entry
= LIST_ENTRY( ptr
, struct wait_queue_entry
, entry
);
655 if (wake_thread( entry
->thread
))
657 if (max
&& !--max
) break;
662 /* return the apc queue to use for a given apc type */
663 static inline struct list
*get_apc_queue( struct thread
*thread
, enum apc_type type
)
670 return &thread
->user_apc
;
672 return &thread
->system_apc
;
676 /* queue an async procedure call */
677 int thread_queue_apc( struct thread
*thread
, struct object
*owner
, const apc_call_t
*call_data
)
679 struct thread_apc
*apc
;
680 struct list
*queue
= get_apc_queue( thread
, call_data
->type
);
682 /* cancel a possible previous APC with the same owner */
683 if (owner
) thread_cancel_apc( thread
, owner
, call_data
->type
);
684 if (thread
->state
== TERMINATED
) return 0;
686 if (!(apc
= alloc_object( &thread_apc_ops
))) return 0;
687 apc
->call
= *call_data
;
690 list_add_tail( queue
, &apc
->entry
);
691 if (!list_prev( queue
, &apc
->entry
)) /* first one */
692 wake_thread( thread
);
697 /* cancel the async procedure call owned by a specific object */
698 void thread_cancel_apc( struct thread
*thread
, struct object
*owner
, enum apc_type type
)
700 struct thread_apc
*apc
;
701 struct list
*queue
= get_apc_queue( thread
, type
);
703 LIST_FOR_EACH_ENTRY( apc
, queue
, struct thread_apc
, entry
)
705 if (apc
->owner
!= owner
) continue;
706 list_remove( &apc
->entry
);
707 release_object( apc
);
712 /* remove the head apc from the queue; the returned pointer must be freed by the caller */
713 static struct thread_apc
*thread_dequeue_apc( struct thread
*thread
, int system_only
)
715 struct thread_apc
*apc
= NULL
;
716 struct list
*ptr
= list_head( &thread
->system_apc
);
718 if (!ptr
&& !system_only
) ptr
= list_head( &thread
->user_apc
);
721 apc
= LIST_ENTRY( ptr
, struct thread_apc
, entry
);
727 /* add an fd to the inflight list */
728 /* return list index, or -1 on error */
729 int thread_add_inflight_fd( struct thread
*thread
, int client
, int server
)
733 if (server
== -1) return -1;
740 /* first check if we already have an entry for this fd */
741 for (i
= 0; i
< MAX_INFLIGHT_FDS
; i
++)
742 if (thread
->inflight
[i
].client
== client
)
744 close( thread
->inflight
[i
].server
);
745 thread
->inflight
[i
].server
= server
;
749 /* now find a free spot to store it */
750 for (i
= 0; i
< MAX_INFLIGHT_FDS
; i
++)
751 if (thread
->inflight
[i
].client
== -1)
753 thread
->inflight
[i
].client
= client
;
754 thread
->inflight
[i
].server
= server
;
760 /* get an inflight fd and purge it from the list */
761 /* the fd must be closed when no longer used */
762 int thread_get_inflight_fd( struct thread
*thread
, int client
)
766 if (client
== -1) return -1;
770 for (i
= 0; i
< MAX_INFLIGHT_FDS
; i
++)
772 if (thread
->inflight
[i
].client
== client
)
774 ret
= thread
->inflight
[i
].server
;
775 thread
->inflight
[i
].server
= thread
->inflight
[i
].client
= -1;
779 } while (!receive_fd( thread
->process
)); /* in case it is still in the socket buffer */
783 /* kill a thread on the spot */
784 void kill_thread( struct thread
*thread
, int violent_death
)
786 if (thread
->state
== TERMINATED
) return; /* already killed */
787 thread
->state
= TERMINATED
;
788 thread
->exit_time
= current_time
;
789 if (current
== thread
) current
= NULL
;
791 fprintf( stderr
,"%04x: *killed* exit_code=%d\n",
792 thread
->id
, thread
->exit_code
);
795 while (thread
->wait
) end_wait( thread
);
796 send_thread_wakeup( thread
, NULL
, STATUS_PENDING
);
797 /* if it is waiting on the socket, we don't need to send a SIGTERM */
800 kill_console_processes( thread
, 0 );
801 debug_exit_thread( thread
);
802 abandon_mutexes( thread
);
803 wake_up( &thread
->obj
, 0 );
804 if (violent_death
) send_thread_signal( thread
, SIGTERM
);
805 cleanup_thread( thread
);
806 remove_process_thread( thread
->process
, thread
);
807 release_object( thread
);
810 /* trigger a breakpoint event in a given thread */
811 void break_thread( struct thread
*thread
)
813 struct debug_event_exception data
;
815 assert( thread
->context
);
817 data
.record
.ExceptionCode
= STATUS_BREAKPOINT
;
818 data
.record
.ExceptionFlags
= EXCEPTION_CONTINUABLE
;
819 data
.record
.ExceptionRecord
= NULL
;
820 data
.record
.ExceptionAddress
= get_context_ip( thread
->context
);
821 data
.record
.NumberParameters
= 0;
823 generate_debug_event( thread
, EXCEPTION_DEBUG_EVENT
, &data
);
824 thread
->debug_break
= 0;
827 /* take a snapshot of currently running threads */
828 struct thread_snapshot
*thread_snap( int *count
)
830 struct thread_snapshot
*snapshot
, *ptr
;
831 struct thread
*thread
;
834 LIST_FOR_EACH_ENTRY( thread
, &thread_list
, struct thread
, entry
)
835 if (thread
->state
!= TERMINATED
) total
++;
836 if (!total
|| !(snapshot
= mem_alloc( sizeof(*snapshot
) * total
))) return NULL
;
838 LIST_FOR_EACH_ENTRY( thread
, &thread_list
, struct thread
, entry
)
840 if (thread
->state
== TERMINATED
) continue;
841 ptr
->thread
= thread
;
842 ptr
->count
= thread
->obj
.refcount
;
843 ptr
->priority
= thread
->priority
;
844 grab_object( thread
);
851 /* gets the current impersonation token */
852 struct token
*thread_get_impersonation_token( struct thread
*thread
)
855 return thread
->token
;
857 return thread
->process
->token
;
860 /* create a new thread */
861 DECL_HANDLER(new_thread
)
863 struct thread
*thread
;
864 int request_fd
= thread_get_inflight_fd( current
, req
->request_fd
);
866 if (request_fd
== -1 || fcntl( request_fd
, F_SETFL
, O_NONBLOCK
) == -1)
868 if (request_fd
!= -1) close( request_fd
);
869 set_error( STATUS_INVALID_HANDLE
);
873 if ((thread
= create_thread( request_fd
, current
->process
)))
875 if (req
->suspend
) thread
->suspend
++;
876 reply
->tid
= get_thread_id( thread
);
877 if ((reply
->handle
= alloc_handle( current
->process
, thread
, req
->access
, req
->attributes
)))
879 /* thread object will be released when the thread gets killed */
882 kill_thread( thread
, 1 );
886 /* initialize a new thread */
887 DECL_HANDLER(init_thread
)
889 struct process
*process
= current
->process
;
890 int reply_fd
= thread_get_inflight_fd( current
, req
->reply_fd
);
891 int wait_fd
= thread_get_inflight_fd( current
, req
->wait_fd
);
893 if (current
->reply_fd
) /* already initialised */
895 set_error( STATUS_INVALID_PARAMETER
);
899 if (reply_fd
== -1 || fcntl( reply_fd
, F_SETFL
, O_NONBLOCK
) == -1) goto error
;
901 current
->reply_fd
= create_anonymous_fd( &thread_fd_ops
, reply_fd
, ¤t
->obj
);
903 if (!current
->reply_fd
) goto error
;
907 set_error( STATUS_TOO_MANY_OPENED_FILES
); /* most likely reason */
910 if (!(current
->wait_fd
= create_anonymous_fd( &thread_fd_ops
, wait_fd
, ¤t
->obj
)))
913 if (!is_valid_address(req
->teb
) || !is_valid_address(req
->peb
) || !is_valid_address(req
->ldt_copy
))
915 set_error( STATUS_INVALID_PARAMETER
);
919 current
->unix_pid
= req
->unix_pid
;
920 current
->unix_tid
= req
->unix_tid
;
921 current
->teb
= req
->teb
;
923 if (!process
->peb
) /* first thread, initialize the process too */
925 process
->unix_pid
= current
->unix_pid
;
926 process
->peb
= req
->peb
;
927 process
->ldt_copy
= req
->ldt_copy
;
928 reply
->info_size
= init_process( current
);
932 if (process
->unix_pid
!= current
->unix_pid
)
933 process
->unix_pid
= -1; /* can happen with linuxthreads */
934 if (current
->suspend
+ process
->suspend
> 0) stop_thread( current
);
935 generate_debug_event( current
, CREATE_THREAD_DEBUG_EVENT
, req
->entry
);
937 debug_level
= max( debug_level
, req
->debug_level
);
939 reply
->pid
= get_process_id( process
);
940 reply
->tid
= get_thread_id( current
);
941 reply
->version
= SERVER_PROTOCOL_VERSION
;
942 reply
->server_start
.sec
= server_start_time
.tv_sec
;
943 reply
->server_start
.usec
= server_start_time
.tv_usec
;
947 if (reply_fd
!= -1) close( reply_fd
);
948 if (wait_fd
!= -1) close( wait_fd
);
951 /* terminate a thread */
952 DECL_HANDLER(terminate_thread
)
954 struct thread
*thread
;
958 if ((thread
= get_thread_from_handle( req
->handle
, THREAD_TERMINATE
)))
960 thread
->exit_code
= req
->exit_code
;
961 if (thread
!= current
) kill_thread( thread
, 1 );
965 reply
->last
= (thread
->process
->running_threads
== 1);
967 release_object( thread
);
971 /* open a handle to a thread */
972 DECL_HANDLER(open_thread
)
974 struct thread
*thread
= get_thread_from_id( req
->tid
);
979 reply
->handle
= alloc_handle( current
->process
, thread
, req
->access
, req
->attributes
);
980 release_object( thread
);
984 /* fetch information about a thread */
985 DECL_HANDLER(get_thread_info
)
987 struct thread
*thread
;
988 obj_handle_t handle
= req
->handle
;
990 if (!handle
) thread
= get_thread_from_id( req
->tid_in
);
991 else thread
= get_thread_from_handle( req
->handle
, THREAD_QUERY_INFORMATION
);
995 reply
->pid
= get_process_id( thread
->process
);
996 reply
->tid
= get_thread_id( thread
);
997 reply
->teb
= thread
->teb
;
998 reply
->exit_code
= (thread
->state
== TERMINATED
) ? thread
->exit_code
: STATUS_PENDING
;
999 reply
->priority
= thread
->priority
;
1000 reply
->affinity
= thread
->affinity
;
1001 reply
->creation_time
.sec
= thread
->creation_time
.tv_sec
;
1002 reply
->creation_time
.usec
= thread
->creation_time
.tv_usec
;
1003 reply
->exit_time
.sec
= thread
->exit_time
.tv_sec
;
1004 reply
->exit_time
.usec
= thread
->exit_time
.tv_usec
;
1005 reply
->last
= thread
->process
->running_threads
== 1;
1007 release_object( thread
);
1011 /* set information about a thread */
1012 DECL_HANDLER(set_thread_info
)
1014 struct thread
*thread
;
1016 if ((thread
= get_thread_from_handle( req
->handle
, THREAD_SET_INFORMATION
)))
1018 set_thread_info( thread
, req
);
1019 release_object( thread
);
1023 /* suspend a thread */
1024 DECL_HANDLER(suspend_thread
)
1026 struct thread
*thread
;
1028 if ((thread
= get_thread_from_handle( req
->handle
, THREAD_SUSPEND_RESUME
)))
1030 if (thread
->state
== TERMINATED
) set_error( STATUS_ACCESS_DENIED
);
1031 else reply
->count
= suspend_thread( thread
);
1032 release_object( thread
);
1036 /* resume a thread */
1037 DECL_HANDLER(resume_thread
)
1039 struct thread
*thread
;
1041 if ((thread
= get_thread_from_handle( req
->handle
, THREAD_SUSPEND_RESUME
)))
1043 if (thread
->state
== TERMINATED
) set_error( STATUS_ACCESS_DENIED
);
1044 else reply
->count
= resume_thread( thread
);
1045 release_object( thread
);
1049 /* select on a handle list */
1050 DECL_HANDLER(select
)
1052 int count
= get_req_data_size() / sizeof(obj_handle_t
);
1053 select_on( count
, req
->cookie
, get_req_data(), req
->flags
, &req
->timeout
, req
->signal
);
1056 /* queue an APC for a thread */
1057 DECL_HANDLER(queue_apc
)
1059 struct thread
*thread
;
1060 if ((thread
= get_thread_from_handle( req
->handle
, THREAD_SET_CONTEXT
)))
1062 switch( req
->call
.type
)
1066 thread_queue_apc( thread
, NULL
, &req
->call
);
1069 set_error( STATUS_INVALID_PARAMETER
);
1072 release_object( thread
);
1076 /* get next APC to call */
1077 DECL_HANDLER(get_apc
)
1079 struct thread_apc
*apc
;
1083 if (!(apc
= (struct thread_apc
*)get_handle_obj( current
->process
, req
->prev
,
1084 0, &thread_apc_ops
))) return;
1086 wake_up( &apc
->obj
, 0 );
1087 close_handle( current
->process
, req
->prev
);
1088 release_object( apc
);
1093 if (!(apc
= thread_dequeue_apc( current
, !req
->alertable
)))
1096 set_error( STATUS_PENDING
);
1099 /* Optimization: ignore APC_NONE calls, they are only used to
1100 * wake up a thread, but since we got here the thread woke up already.
1102 if (apc
->call
.type
!= APC_NONE
) break;
1103 release_object( apc
);
1106 if ((reply
->handle
= alloc_handle( current
->process
, apc
, SYNCHRONIZE
, 0 )))
1107 reply
->call
= apc
->call
;
1108 release_object( apc
);
1111 /* retrieve the current context of a thread */
1112 DECL_HANDLER(get_thread_context
)
1114 struct thread
*thread
;
1117 if (get_reply_max_size() < sizeof(CONTEXT
))
1119 set_error( STATUS_INVALID_PARAMETER
);
1122 if (!(thread
= get_thread_from_handle( req
->handle
, THREAD_GET_CONTEXT
))) return;
1126 if (thread
!= current
|| !thread
->suspend_context
)
1128 /* not suspended, shouldn't happen */
1129 set_error( STATUS_INVALID_PARAMETER
);
1133 if (thread
->context
== thread
->suspend_context
) thread
->context
= NULL
;
1134 set_reply_data_ptr( thread
->suspend_context
, sizeof(CONTEXT
) );
1135 thread
->suspend_context
= NULL
;
1138 else if (thread
!= current
&& !thread
->context
)
1140 /* thread is not suspended, retry (if it's still running) */
1141 if (thread
->state
!= RUNNING
) set_error( STATUS_ACCESS_DENIED
);
1142 else set_error( STATUS_PENDING
);
1144 else if ((context
= set_reply_data_size( sizeof(CONTEXT
) )))
1146 unsigned int flags
= get_context_system_regs( req
->flags
);
1148 memset( context
, 0, sizeof(CONTEXT
) );
1149 context
->ContextFlags
= get_context_cpu_flag();
1150 if (thread
->context
) copy_context( context
, thread
->context
, req
->flags
& ~flags
);
1151 if (flags
) get_thread_context( thread
, context
, flags
);
1153 reply
->self
= (thread
== current
);
1154 release_object( thread
);
1157 /* set the current context of a thread */
1158 DECL_HANDLER(set_thread_context
)
1160 struct thread
*thread
;
1162 if (get_req_data_size() < sizeof(CONTEXT
))
1164 set_error( STATUS_INVALID_PARAMETER
);
1167 if (!(thread
= get_thread_from_handle( req
->handle
, THREAD_SET_CONTEXT
))) return;
1171 if (thread
!= current
|| thread
->context
)
1173 /* nested suspend or exception, shouldn't happen */
1174 set_error( STATUS_INVALID_PARAMETER
);
1176 else if ((thread
->suspend_context
= mem_alloc( sizeof(CONTEXT
) )))
1178 memcpy( thread
->suspend_context
, get_req_data(), sizeof(CONTEXT
) );
1179 thread
->context
= thread
->suspend_context
;
1180 if (thread
->debug_break
) break_thread( thread
);
1183 else if (thread
!= current
&& !thread
->context
)
1185 /* thread is not suspended, retry (if it's still running) */
1186 if (thread
->state
!= RUNNING
) set_error( STATUS_ACCESS_DENIED
);
1187 else set_error( STATUS_PENDING
);
1191 const CONTEXT
*context
= get_req_data();
1192 unsigned int flags
= get_context_system_regs( req
->flags
);
1194 if (flags
) set_thread_context( thread
, context
, flags
);
1195 if (thread
->context
&& !get_error())
1196 copy_context( thread
->context
, context
, req
->flags
& ~flags
);
1198 reply
->self
= (thread
== current
);
1199 release_object( thread
);
1202 /* fetch a selector entry for a thread */
1203 DECL_HANDLER(get_selector_entry
)
1205 struct thread
*thread
;
1206 if ((thread
= get_thread_from_handle( req
->handle
, THREAD_QUERY_INFORMATION
)))
1208 get_selector_entry( thread
, req
->entry
, &reply
->base
, &reply
->limit
, &reply
->flags
);
1209 release_object( thread
);