xref: /freebsd/lib/libthr/thread/thr_sigqueue.c (revision 7fdf597e96a02165cfe22ff357b857d5fa15ed8a)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1997 John Birrell <jb@cimlogic.com.au>.
5  * All rights reserved.
6  *
7  * Copyright 2024 The FreeBSD Foundation
8  *
9  * Portions of this software were developed by Konstantin Belousov
10  * <kib@FreeBSD.org> under sponsorship from the FreeBSD Foundation.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  * 3. Neither the name of the author nor the names of any co-contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY JOHN BIRRELL AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  */
36 
37 #include "namespace.h"
38 #include <errno.h>
39 #include <signal.h>
40 #include <pthread.h>
41 #include "un-namespace.h"
42 
43 #include "thr_private.h"
44 
45 __weak_reference(_Tthr_sigqueue, _pthread_sigqueue);
46 __weak_reference(_Tthr_sigqueue, pthread_sigqueue);
47 
48 int
49 _Tthr_sigqueue(pthread_t pthread, int sig, const union sigval value)
50 {
51 	struct pthread *curthread;
52 	int e, ret;
53 
54 	if (sig < 0 || sig > _SIG_MAXSIG)
55 		return (EINVAL);
56 
57 	curthread = _get_curthread();
58 	ret = 0;
59 
60 	if (curthread == pthread) {
61 		if (sig > 0) {
62 			e = sigqueue(pthread->tid, sig | __SIGQUEUE_TID,
63 			    value);
64 			if (e == -1)
65 				ret = errno;
66 		}
67 	} else if ((ret = _thr_find_thread(curthread, pthread,
68 	    0)) == 0) {
69 		if (sig > 0) {
70 			e = sigqueue(pthread->tid, sig | __SIGQUEUE_TID,
71 			    value);
72 			if (e == -1)
73 				ret = errno;
74 		}
75 		THR_THREAD_UNLOCK(curthread, pthread);
76 	}
77 
78 	return (ret);
79 }
80