thr_kill.c revision 331722
1185377Ssam/*
2187831Ssam * Copyright (c) 1997 John Birrell <jb@cimlogic.com.au>.
3185377Ssam * All rights reserved.
4185377Ssam *
5185377Ssam * Redistribution and use in source and binary forms, with or without
6185377Ssam * modification, are permitted provided that the following conditions
7185377Ssam * are met:
8185377Ssam * 1. Redistributions of source code must retain the above copyright
9185377Ssam *    notice, this list of conditions and the following disclaimer.
10185377Ssam * 2. Redistributions in binary form must reproduce the above copyright
11185377Ssam *    notice, this list of conditions and the following disclaimer in the
12185377Ssam *    documentation and/or other materials provided with the distribution.
13185377Ssam * 3. Neither the name of the author nor the names of any co-contributors
14185377Ssam *    may be used to endorse or promote products derived from this software
15185377Ssam *    without specific prior written permission.
16185377Ssam *
17187831Ssam * THIS SOFTWARE IS PROVIDED BY JOHN BIRRELL AND CONTRIBUTORS ``AS IS'' AND
18185377Ssam * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19185377Ssam * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20185377Ssam * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21185377Ssam * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22185377Ssam * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23185377Ssam * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24185377Ssam * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25185377Ssam * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26185377Ssam * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27185377Ssam * SUCH DAMAGE.
28185377Ssam */
29185377Ssam
30185377Ssam#include <sys/cdefs.h>
31185377Ssam__FBSDID("$FreeBSD: stable/11/lib/libthr/thread/thr_kill.c 331722 2018-03-29 02:50:57Z eadler $");
32185377Ssam
33185377Ssam#include "namespace.h"
34185377Ssam#include <errno.h>
35185377Ssam#include <signal.h>
36185377Ssam#include <pthread.h>
37185377Ssam#include "un-namespace.h"
38185377Ssam
39185377Ssam#include "thr_private.h"
40185377Ssam
41185377Ssam__weak_reference(_pthread_kill, pthread_kill);
42185377Ssam
43185377Ssamint
44185377Ssam_pthread_kill(pthread_t pthread, int sig)
45185377Ssam{
46185377Ssam	struct pthread *curthread;
47185377Ssam	int ret;
48185377Ssam
49185377Ssam	/* Check for invalid signal numbers: */
50185377Ssam	if (sig < 0 || sig > _SIG_MAXSIG)
51185377Ssam		/* Invalid signal: */
52185377Ssam		return (EINVAL);
53185377Ssam
54185377Ssam	curthread = _get_curthread();
55185377Ssam
56185377Ssam	/*
57185377Ssam	 * Ensure the thread is in the list of active threads, and the
58185377Ssam	 * signal is valid (signal 0 specifies error checking only) and
59185377Ssam	 * not being ignored:
60185377Ssam	 */
61185377Ssam	if (curthread == pthread) {
62185377Ssam		if (sig > 0)
63185377Ssam			_thr_send_sig(pthread, sig);
64185377Ssam		ret = 0;
65185377Ssam	} else if ((ret = _thr_find_thread(curthread, pthread,
66185377Ssam	    /*include dead*/0)) == 0) {
67185377Ssam		if (sig > 0)
68185377Ssam			_thr_send_sig(pthread, sig);
69185377Ssam		THR_THREAD_UNLOCK(curthread, pthread);
70185377Ssam	}
71185377Ssam
72185377Ssam	/* Return the completion status: */
73185377Ssam	return (ret);
74185377Ssam}
75185377Ssam