xref: /freebsd/lib/libthr/thread/thr_kill.c (revision a9b764e218bfdbf5f746d64ff503348a2363db6e)
1a091d823SDavid Xu /*
2a091d823SDavid Xu  * Copyright (c) 1997 John Birrell <jb@cimlogic.com.au>.
3a091d823SDavid Xu  * All rights reserved.
4a091d823SDavid Xu  *
5a091d823SDavid Xu  * Redistribution and use in source and binary forms, with or without
6a091d823SDavid Xu  * modification, are permitted provided that the following conditions
7a091d823SDavid Xu  * are met:
8a091d823SDavid Xu  * 1. Redistributions of source code must retain the above copyright
9a091d823SDavid Xu  *    notice, this list of conditions and the following disclaimer.
10a091d823SDavid Xu  * 2. Redistributions in binary form must reproduce the above copyright
11a091d823SDavid Xu  *    notice, this list of conditions and the following disclaimer in the
12a091d823SDavid Xu  *    documentation and/or other materials provided with the distribution.
13fed32d75SWarner Losh  * 3. Neither the name of the author nor the names of any co-contributors
14a091d823SDavid Xu  *    may be used to endorse or promote products derived from this software
15a091d823SDavid Xu  *    without specific prior written permission.
16a091d823SDavid Xu  *
17a091d823SDavid Xu  * THIS SOFTWARE IS PROVIDED BY JOHN BIRRELL AND CONTRIBUTORS ``AS IS'' AND
18a091d823SDavid Xu  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19a091d823SDavid Xu  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20a091d823SDavid Xu  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21a091d823SDavid Xu  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22a091d823SDavid Xu  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23a091d823SDavid Xu  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24a091d823SDavid Xu  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25a091d823SDavid Xu  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26a091d823SDavid Xu  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27a091d823SDavid Xu  * SUCH DAMAGE.
28a091d823SDavid Xu  *
29a091d823SDavid Xu  * $FreeBSD$
30a091d823SDavid Xu  */
31a091d823SDavid Xu 
3237a6356bSDavid Xu #include "namespace.h"
33a091d823SDavid Xu #include <errno.h>
34a091d823SDavid Xu #include <signal.h>
35a091d823SDavid Xu #include <pthread.h>
3637a6356bSDavid Xu #include "un-namespace.h"
37a091d823SDavid Xu 
38a091d823SDavid Xu #include "thr_private.h"
39a091d823SDavid Xu 
40a091d823SDavid Xu __weak_reference(_pthread_kill, pthread_kill);
41a091d823SDavid Xu 
42a091d823SDavid Xu int
43a091d823SDavid Xu _pthread_kill(pthread_t pthread, int sig)
44a091d823SDavid Xu {
45a091d823SDavid Xu 	struct pthread *curthread = _get_curthread();
46a091d823SDavid Xu 	int ret;
47a091d823SDavid Xu 
48a091d823SDavid Xu 	/* Check for invalid signal numbers: */
49a091d823SDavid Xu 	if (sig < 0 || sig > _SIG_MAXSIG)
50a091d823SDavid Xu 		/* Invalid signal: */
51a091d823SDavid Xu 		ret = EINVAL;
52a091d823SDavid Xu 	/*
53a091d823SDavid Xu 	 * Ensure the thread is in the list of active threads, and the
54a091d823SDavid Xu 	 * signal is valid (signal 0 specifies error checking only) and
55a091d823SDavid Xu 	 * not being ignored:
56a091d823SDavid Xu 	 */
57*a9b764e2SDavid Xu 	else if (curthread == pthread) {
58*a9b764e2SDavid Xu 		if (sig > 0)
59*a9b764e2SDavid Xu 			_thr_send_sig(pthread, sig);
60*a9b764e2SDavid Xu 		ret = 0;
61*a9b764e2SDavid Xu 	} if ((ret = _thr_find_thread(curthread, pthread, /*include dead*/0))
62a091d823SDavid Xu 	    == 0) {
63a091d823SDavid Xu 		if (sig > 0)
64a091d823SDavid Xu 			_thr_send_sig(pthread, sig);
65*a9b764e2SDavid Xu 		THR_THREAD_UNLOCK(curthread, pthread);
66a091d823SDavid Xu 	}
67a091d823SDavid Xu 
68a091d823SDavid Xu 	/* Return the completion status: */
69a091d823SDavid Xu 	return (ret);
70a091d823SDavid Xu }
71