xref: /titanic_53/usr/src/uts/common/syscall/sigqueue.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright (c) 1998-2000 by Sun Microsystems, Inc.
24*7c478bd9Sstevel@tonic-gate  * All rights reserved.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
28*7c478bd9Sstevel@tonic-gate 
29*7c478bd9Sstevel@tonic-gate 
30*7c478bd9Sstevel@tonic-gate 
31*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
32*7c478bd9Sstevel@tonic-gate 
33*7c478bd9Sstevel@tonic-gate #include <sys/param.h>
34*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
35*7c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
36*7c478bd9Sstevel@tonic-gate #include <sys/systm.h>
37*7c478bd9Sstevel@tonic-gate #include <sys/errno.h>
38*7c478bd9Sstevel@tonic-gate #include <sys/proc.h>
39*7c478bd9Sstevel@tonic-gate #include <sys/procset.h>
40*7c478bd9Sstevel@tonic-gate #include <sys/fault.h>
41*7c478bd9Sstevel@tonic-gate #include <sys/signal.h>
42*7c478bd9Sstevel@tonic-gate #include <sys/siginfo.h>
43*7c478bd9Sstevel@tonic-gate #include <sys/debug.h>
44*7c478bd9Sstevel@tonic-gate 
45*7c478bd9Sstevel@tonic-gate static int
46*7c478bd9Sstevel@tonic-gate sigqkill(pid_t pid, int signo, sigsend_t *sigsend)
47*7c478bd9Sstevel@tonic-gate {
48*7c478bd9Sstevel@tonic-gate 	register proc_t *p;
49*7c478bd9Sstevel@tonic-gate 	int error;
50*7c478bd9Sstevel@tonic-gate 
51*7c478bd9Sstevel@tonic-gate 	if (signo < 0 || signo >= NSIG)
52*7c478bd9Sstevel@tonic-gate 		return (set_errno(EINVAL));
53*7c478bd9Sstevel@tonic-gate 
54*7c478bd9Sstevel@tonic-gate 	if (pid == -1) {
55*7c478bd9Sstevel@tonic-gate 		procset_t set;
56*7c478bd9Sstevel@tonic-gate 
57*7c478bd9Sstevel@tonic-gate 		setprocset(&set, POP_AND, P_ALL, P_MYID, P_ALL, P_MYID);
58*7c478bd9Sstevel@tonic-gate 		error = sigsendset(&set, sigsend);
59*7c478bd9Sstevel@tonic-gate 	} else if (pid > 0) {
60*7c478bd9Sstevel@tonic-gate 		mutex_enter(&pidlock);
61*7c478bd9Sstevel@tonic-gate 		if ((p = prfind(pid)) == NULL || p->p_stat == SIDL)
62*7c478bd9Sstevel@tonic-gate 			error = ESRCH;
63*7c478bd9Sstevel@tonic-gate 		else {
64*7c478bd9Sstevel@tonic-gate 			error = sigsendproc(p, sigsend);
65*7c478bd9Sstevel@tonic-gate 			if (error == 0 && sigsend->perm == 0)
66*7c478bd9Sstevel@tonic-gate 				error = EPERM;
67*7c478bd9Sstevel@tonic-gate 		}
68*7c478bd9Sstevel@tonic-gate 		mutex_exit(&pidlock);
69*7c478bd9Sstevel@tonic-gate 	} else {
70*7c478bd9Sstevel@tonic-gate 		int nfound = 0;
71*7c478bd9Sstevel@tonic-gate 		pid_t pgid;
72*7c478bd9Sstevel@tonic-gate 
73*7c478bd9Sstevel@tonic-gate 		if (pid == 0)
74*7c478bd9Sstevel@tonic-gate 			pgid = ttoproc(curthread)->p_pgrp;
75*7c478bd9Sstevel@tonic-gate 		else
76*7c478bd9Sstevel@tonic-gate 			pgid = -pid;
77*7c478bd9Sstevel@tonic-gate 
78*7c478bd9Sstevel@tonic-gate 		error = 0;
79*7c478bd9Sstevel@tonic-gate 		mutex_enter(&pidlock);
80*7c478bd9Sstevel@tonic-gate 		for (p = pgfind(pgid); p && !error; p = p->p_pglink) {
81*7c478bd9Sstevel@tonic-gate 			if (p->p_stat != SIDL) {
82*7c478bd9Sstevel@tonic-gate 				nfound++;
83*7c478bd9Sstevel@tonic-gate 				error = sigsendproc(p, sigsend);
84*7c478bd9Sstevel@tonic-gate 			}
85*7c478bd9Sstevel@tonic-gate 		}
86*7c478bd9Sstevel@tonic-gate 		mutex_exit(&pidlock);
87*7c478bd9Sstevel@tonic-gate 		if (nfound == 0)
88*7c478bd9Sstevel@tonic-gate 			error = ESRCH;
89*7c478bd9Sstevel@tonic-gate 		else if (error == 0 && sigsend->perm == 0)
90*7c478bd9Sstevel@tonic-gate 			error = EPERM;
91*7c478bd9Sstevel@tonic-gate 	}
92*7c478bd9Sstevel@tonic-gate 
93*7c478bd9Sstevel@tonic-gate 	if (error)
94*7c478bd9Sstevel@tonic-gate 		return (set_errno(error));
95*7c478bd9Sstevel@tonic-gate 	return (0);
96*7c478bd9Sstevel@tonic-gate }
97*7c478bd9Sstevel@tonic-gate 
98*7c478bd9Sstevel@tonic-gate 
99*7c478bd9Sstevel@tonic-gate /*
100*7c478bd9Sstevel@tonic-gate  * for implementations that don't require binary compatibility,
101*7c478bd9Sstevel@tonic-gate  * the kill system call may be made into a library call to the
102*7c478bd9Sstevel@tonic-gate  * sigsend system call
103*7c478bd9Sstevel@tonic-gate  */
104*7c478bd9Sstevel@tonic-gate int
105*7c478bd9Sstevel@tonic-gate kill(pid_t pid, int sig)
106*7c478bd9Sstevel@tonic-gate {
107*7c478bd9Sstevel@tonic-gate 	sigsend_t v;
108*7c478bd9Sstevel@tonic-gate 
109*7c478bd9Sstevel@tonic-gate 	bzero(&v, sizeof (v));
110*7c478bd9Sstevel@tonic-gate 	v.sig = sig;
111*7c478bd9Sstevel@tonic-gate 	v.checkperm = 1;
112*7c478bd9Sstevel@tonic-gate 	v.sicode = SI_USER;
113*7c478bd9Sstevel@tonic-gate 
114*7c478bd9Sstevel@tonic-gate 	return (sigqkill(pid, sig, &v));
115*7c478bd9Sstevel@tonic-gate }
116*7c478bd9Sstevel@tonic-gate 
117*7c478bd9Sstevel@tonic-gate /*
118*7c478bd9Sstevel@tonic-gate  * The handling of small unions, like the sigval argument to sigqueue,
119*7c478bd9Sstevel@tonic-gate  * is architecture dependent.  We have adapted the convention that the
120*7c478bd9Sstevel@tonic-gate  * value itself is passed in the storage which crosses the kernel
121*7c478bd9Sstevel@tonic-gate  * protection boundary.  This procedure will accept a scalar argument,
122*7c478bd9Sstevel@tonic-gate  * and store it in the appropriate value member of the sigsend_t structure.
123*7c478bd9Sstevel@tonic-gate  */
124*7c478bd9Sstevel@tonic-gate int
125*7c478bd9Sstevel@tonic-gate sigqueue(pid_t pid, int signo, /* union sigval */ void *value, int si_code)
126*7c478bd9Sstevel@tonic-gate {
127*7c478bd9Sstevel@tonic-gate 	sigsend_t v;
128*7c478bd9Sstevel@tonic-gate 	sigqhdr_t *sqh;
129*7c478bd9Sstevel@tonic-gate 	proc_t *p = curproc;
130*7c478bd9Sstevel@tonic-gate 
131*7c478bd9Sstevel@tonic-gate 	/* The si_code value must indicate the signal will be queued */
132*7c478bd9Sstevel@tonic-gate 	if (pid <= 0 || !sigwillqueue(signo, si_code))
133*7c478bd9Sstevel@tonic-gate 		return (set_errno(EINVAL));
134*7c478bd9Sstevel@tonic-gate 
135*7c478bd9Sstevel@tonic-gate 	if (p->p_sigqhdr == NULL) {
136*7c478bd9Sstevel@tonic-gate 		/* Allocate sigqueue pool first time */
137*7c478bd9Sstevel@tonic-gate 		sqh = sigqhdralloc(sizeof (sigqueue_t), _SIGQUEUE_MAX);
138*7c478bd9Sstevel@tonic-gate 		mutex_enter(&p->p_lock);
139*7c478bd9Sstevel@tonic-gate 		if (p->p_sigqhdr == NULL) {
140*7c478bd9Sstevel@tonic-gate 			/* hang the pool head on proc */
141*7c478bd9Sstevel@tonic-gate 			p->p_sigqhdr = sqh;
142*7c478bd9Sstevel@tonic-gate 		} else {
143*7c478bd9Sstevel@tonic-gate 			/* another lwp allocated the pool, free ours */
144*7c478bd9Sstevel@tonic-gate 			sigqhdrfree(sqh);
145*7c478bd9Sstevel@tonic-gate 		}
146*7c478bd9Sstevel@tonic-gate 		mutex_exit(&p->p_lock);
147*7c478bd9Sstevel@tonic-gate 	}
148*7c478bd9Sstevel@tonic-gate 
149*7c478bd9Sstevel@tonic-gate 	bzero(&v, sizeof (v));
150*7c478bd9Sstevel@tonic-gate 	v.sig = signo;
151*7c478bd9Sstevel@tonic-gate 	v.checkperm = 1;
152*7c478bd9Sstevel@tonic-gate 	v.sicode = si_code;
153*7c478bd9Sstevel@tonic-gate 	v.value.sival_ptr = value;
154*7c478bd9Sstevel@tonic-gate 
155*7c478bd9Sstevel@tonic-gate 	return (sigqkill(pid, signo, &v));
156*7c478bd9Sstevel@tonic-gate }
157*7c478bd9Sstevel@tonic-gate 
158*7c478bd9Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
159*7c478bd9Sstevel@tonic-gate /*
160*7c478bd9Sstevel@tonic-gate  * sigqueue32 - System call entry point for 32-bit callers on LP64 kernel,
161*7c478bd9Sstevel@tonic-gate  * needed to handle the 32-bit sigvals as correctly as we can.  We always
162*7c478bd9Sstevel@tonic-gate  * assume that a 32-bit caller is passing an int. A 64-bit recipient
163*7c478bd9Sstevel@tonic-gate  * that expects an int will therefore get it correctly.  A 32-bit
164*7c478bd9Sstevel@tonic-gate  * recipient will also get it correctly since siginfo_kto32() uses
165*7c478bd9Sstevel@tonic-gate  * sival_int in the conversion.  Since a 32-bit pointer has the same
166*7c478bd9Sstevel@tonic-gate  * size and address in the sigval, it also converts correctly so that
167*7c478bd9Sstevel@tonic-gate  * two 32-bit apps can exchange a pointer value.  However, this means
168*7c478bd9Sstevel@tonic-gate  * that a pointer sent by a 32-bit caller will be seen in the upper half
169*7c478bd9Sstevel@tonic-gate  * by a 64-bit recipient, and only the upper half of a 64-bit pointer will
170*7c478bd9Sstevel@tonic-gate  * be seen by a 32-bit recipient.  This is the best solution that does
171*7c478bd9Sstevel@tonic-gate  * not require severe hacking of the sigval union.  Anyways, what it
172*7c478bd9Sstevel@tonic-gate  * means to be sending pointers between processes with dissimilar
173*7c478bd9Sstevel@tonic-gate  * models is unclear.
174*7c478bd9Sstevel@tonic-gate  */
175*7c478bd9Sstevel@tonic-gate int
176*7c478bd9Sstevel@tonic-gate sigqueue32(pid_t pid, int signo, /* union sigval32 */ caddr32_t value,
177*7c478bd9Sstevel@tonic-gate     int si_code)
178*7c478bd9Sstevel@tonic-gate {
179*7c478bd9Sstevel@tonic-gate 	union sigval sv;
180*7c478bd9Sstevel@tonic-gate 
181*7c478bd9Sstevel@tonic-gate 	bzero(&sv, sizeof (sv));
182*7c478bd9Sstevel@tonic-gate 	sv.sival_int = (int)value;
183*7c478bd9Sstevel@tonic-gate 	return (sigqueue(pid, signo, sv.sival_ptr, si_code));
184*7c478bd9Sstevel@tonic-gate }
185*7c478bd9Sstevel@tonic-gate #endif
186