xref: /titanic_50/usr/src/uts/common/syscall/signotify.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 2004 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*7c478bd9Sstevel@tonic-gate 
29*7c478bd9Sstevel@tonic-gate #include <sys/param.h>
30*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
31*7c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
32*7c478bd9Sstevel@tonic-gate #include <sys/systm.h>
33*7c478bd9Sstevel@tonic-gate #include <sys/errno.h>
34*7c478bd9Sstevel@tonic-gate #include <sys/proc.h>
35*7c478bd9Sstevel@tonic-gate #include <sys/procset.h>
36*7c478bd9Sstevel@tonic-gate #include <sys/fault.h>
37*7c478bd9Sstevel@tonic-gate #include <sys/signal.h>
38*7c478bd9Sstevel@tonic-gate #include <sys/siginfo.h>
39*7c478bd9Sstevel@tonic-gate #include <vm/as.h>
40*7c478bd9Sstevel@tonic-gate #include <sys/debug.h>
41*7c478bd9Sstevel@tonic-gate #include <sys/contract/process_impl.h>
42*7c478bd9Sstevel@tonic-gate 
43*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
44*7c478bd9Sstevel@tonic-gate static int
45*7c478bd9Sstevel@tonic-gate copyin_siginfo(model_t datamodel, void *uaddr, k_siginfo_t *ksip)
46*7c478bd9Sstevel@tonic-gate {
47*7c478bd9Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
48*7c478bd9Sstevel@tonic-gate 	int ret;
49*7c478bd9Sstevel@tonic-gate 
50*7c478bd9Sstevel@tonic-gate 	if (datamodel == DATAMODEL_NATIVE) {
51*7c478bd9Sstevel@tonic-gate #endif
52*7c478bd9Sstevel@tonic-gate 		return (copyin(uaddr, ksip, sizeof (k_siginfo_t)));
53*7c478bd9Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
54*7c478bd9Sstevel@tonic-gate 	} else {
55*7c478bd9Sstevel@tonic-gate 		siginfo32_t si32;
56*7c478bd9Sstevel@tonic-gate 
57*7c478bd9Sstevel@tonic-gate 		if (ret = copyin(uaddr, &si32, sizeof (si32)))
58*7c478bd9Sstevel@tonic-gate 			return (ret);
59*7c478bd9Sstevel@tonic-gate 
60*7c478bd9Sstevel@tonic-gate 		siginfo_32tok(&si32, ksip);
61*7c478bd9Sstevel@tonic-gate 	}
62*7c478bd9Sstevel@tonic-gate 
63*7c478bd9Sstevel@tonic-gate 	return (0);
64*7c478bd9Sstevel@tonic-gate #endif
65*7c478bd9Sstevel@tonic-gate }
66*7c478bd9Sstevel@tonic-gate 
67*7c478bd9Sstevel@tonic-gate /*
68*7c478bd9Sstevel@tonic-gate  * To find secured 64 bit id for signotify() call
69*7c478bd9Sstevel@tonic-gate  * This depends upon as_getmemid() which returns
70*7c478bd9Sstevel@tonic-gate  * unique vnode/offset for a user virtual address.
71*7c478bd9Sstevel@tonic-gate  */
72*7c478bd9Sstevel@tonic-gate static u_longlong_t
73*7c478bd9Sstevel@tonic-gate get_sigid(proc_t *p, caddr_t addr)
74*7c478bd9Sstevel@tonic-gate {
75*7c478bd9Sstevel@tonic-gate 	u_longlong_t snid = 0;
76*7c478bd9Sstevel@tonic-gate 	memid_t memid;
77*7c478bd9Sstevel@tonic-gate 	quad_t *tquad = (quad_t *)&snid;
78*7c478bd9Sstevel@tonic-gate 
79*7c478bd9Sstevel@tonic-gate 	if (!as_getmemid(p->p_as, addr, &memid)) {
80*7c478bd9Sstevel@tonic-gate 		tquad->val[0] = (int)memid.val[0];
81*7c478bd9Sstevel@tonic-gate 		tquad->val[1] = (int)memid.val[1];
82*7c478bd9Sstevel@tonic-gate 	}
83*7c478bd9Sstevel@tonic-gate 	return (snid);
84*7c478bd9Sstevel@tonic-gate }
85*7c478bd9Sstevel@tonic-gate 
86*7c478bd9Sstevel@tonic-gate #define	SIGN_PTR(p, n)	&((signotifyq_t *)(&p->p_signhdr[1]))[n];
87*7c478bd9Sstevel@tonic-gate 
88*7c478bd9Sstevel@tonic-gate int
89*7c478bd9Sstevel@tonic-gate signotify(int cmd, siginfo_t *siginfo, signotify_id_t *sn_id)
90*7c478bd9Sstevel@tonic-gate {
91*7c478bd9Sstevel@tonic-gate 	k_siginfo_t	info;
92*7c478bd9Sstevel@tonic-gate 	signotify_id_t	id;
93*7c478bd9Sstevel@tonic-gate 	proc_t		*p;
94*7c478bd9Sstevel@tonic-gate 	proc_t		*cp = curproc;
95*7c478bd9Sstevel@tonic-gate 	signotifyq_t	*snqp;
96*7c478bd9Sstevel@tonic-gate 	struct cred	*cr;
97*7c478bd9Sstevel@tonic-gate 	sigqueue_t	*sqp;
98*7c478bd9Sstevel@tonic-gate 	sigqhdr_t	*sqh;
99*7c478bd9Sstevel@tonic-gate 	u_longlong_t	sid;
100*7c478bd9Sstevel@tonic-gate 	model_t 	datamodel = get_udatamodel();
101*7c478bd9Sstevel@tonic-gate 
102*7c478bd9Sstevel@tonic-gate 	if (copyin(sn_id, &id, sizeof (signotify_id_t)))
103*7c478bd9Sstevel@tonic-gate 		return (set_errno(EFAULT));
104*7c478bd9Sstevel@tonic-gate 
105*7c478bd9Sstevel@tonic-gate 	if (id.sn_index >= _SIGNOTIFY_MAX || id.sn_index < 0)
106*7c478bd9Sstevel@tonic-gate 		return (set_errno(EINVAL));
107*7c478bd9Sstevel@tonic-gate 
108*7c478bd9Sstevel@tonic-gate 	switch (cmd) {
109*7c478bd9Sstevel@tonic-gate 	case SN_PROC:
110*7c478bd9Sstevel@tonic-gate 		/* get snid for the given user address of signotifyid_t */
111*7c478bd9Sstevel@tonic-gate 		sid = get_sigid(cp, (caddr_t)sn_id);
112*7c478bd9Sstevel@tonic-gate 
113*7c478bd9Sstevel@tonic-gate 		if (id.sn_pid > 0) {
114*7c478bd9Sstevel@tonic-gate 			mutex_enter(&pidlock);
115*7c478bd9Sstevel@tonic-gate 			if ((p = prfind(id.sn_pid)) != NULL) {
116*7c478bd9Sstevel@tonic-gate 				mutex_enter(&p->p_lock);
117*7c478bd9Sstevel@tonic-gate 				if (p->p_signhdr != NULL) {
118*7c478bd9Sstevel@tonic-gate 					snqp = SIGN_PTR(p, id.sn_index);
119*7c478bd9Sstevel@tonic-gate 					if (snqp->sn_snid == sid) {
120*7c478bd9Sstevel@tonic-gate 						mutex_exit(&p->p_lock);
121*7c478bd9Sstevel@tonic-gate 						mutex_exit(&pidlock);
122*7c478bd9Sstevel@tonic-gate 						return (set_errno(EBUSY));
123*7c478bd9Sstevel@tonic-gate 					}
124*7c478bd9Sstevel@tonic-gate 				}
125*7c478bd9Sstevel@tonic-gate 				mutex_exit(&p->p_lock);
126*7c478bd9Sstevel@tonic-gate 			}
127*7c478bd9Sstevel@tonic-gate 			mutex_exit(&pidlock);
128*7c478bd9Sstevel@tonic-gate 		}
129*7c478bd9Sstevel@tonic-gate 
130*7c478bd9Sstevel@tonic-gate 		if (copyin_siginfo(datamodel, siginfo, &info))
131*7c478bd9Sstevel@tonic-gate 			return (set_errno(EFAULT));
132*7c478bd9Sstevel@tonic-gate 
133*7c478bd9Sstevel@tonic-gate 		/* The si_code value must indicate the signal will be queued */
134*7c478bd9Sstevel@tonic-gate 		if (!sigwillqueue(info.si_signo, info.si_code))
135*7c478bd9Sstevel@tonic-gate 			return (set_errno(EINVAL));
136*7c478bd9Sstevel@tonic-gate 
137*7c478bd9Sstevel@tonic-gate 		if (cp->p_signhdr == NULL) {
138*7c478bd9Sstevel@tonic-gate 			/* Allocate signotify pool first time */
139*7c478bd9Sstevel@tonic-gate 			sqh = sigqhdralloc(sizeof (signotifyq_t),
140*7c478bd9Sstevel@tonic-gate 			    _SIGNOTIFY_MAX);
141*7c478bd9Sstevel@tonic-gate 			mutex_enter(&cp->p_lock);
142*7c478bd9Sstevel@tonic-gate 			if (cp->p_signhdr == NULL) {
143*7c478bd9Sstevel@tonic-gate 				/* hang the pool head on proc */
144*7c478bd9Sstevel@tonic-gate 				cp->p_signhdr = sqh;
145*7c478bd9Sstevel@tonic-gate 			} else {
146*7c478bd9Sstevel@tonic-gate 				/* another lwp allocated the pool, free ours */
147*7c478bd9Sstevel@tonic-gate 				sigqhdrfree(sqh);
148*7c478bd9Sstevel@tonic-gate 			}
149*7c478bd9Sstevel@tonic-gate 		} else {
150*7c478bd9Sstevel@tonic-gate 			mutex_enter(&cp->p_lock);
151*7c478bd9Sstevel@tonic-gate 		}
152*7c478bd9Sstevel@tonic-gate 
153*7c478bd9Sstevel@tonic-gate 		sqp = sigqalloc(cp->p_signhdr);
154*7c478bd9Sstevel@tonic-gate 		if (sqp == NULL) {
155*7c478bd9Sstevel@tonic-gate 			mutex_exit(&cp->p_lock);
156*7c478bd9Sstevel@tonic-gate 			return (set_errno(EAGAIN));
157*7c478bd9Sstevel@tonic-gate 		}
158*7c478bd9Sstevel@tonic-gate 		cr = CRED();
159*7c478bd9Sstevel@tonic-gate 		sqp->sq_info = info;
160*7c478bd9Sstevel@tonic-gate 		sqp->sq_info.si_pid = cp->p_pid;
161*7c478bd9Sstevel@tonic-gate 		sqp->sq_info.si_ctid = PRCTID(cp);
162*7c478bd9Sstevel@tonic-gate 		sqp->sq_info.si_zoneid = getzoneid();
163*7c478bd9Sstevel@tonic-gate 		sqp->sq_info.si_uid = crgetruid(cr);
164*7c478bd9Sstevel@tonic-gate 
165*7c478bd9Sstevel@tonic-gate 		/* fill the signotifyq_t fields */
166*7c478bd9Sstevel@tonic-gate 		((signotifyq_t *)sqp)->sn_snid = sid;
167*7c478bd9Sstevel@tonic-gate 
168*7c478bd9Sstevel@tonic-gate 		mutex_exit(&cp->p_lock);
169*7c478bd9Sstevel@tonic-gate 
170*7c478bd9Sstevel@tonic-gate 		/* complete the signotify_id_t fields */
171*7c478bd9Sstevel@tonic-gate 		id.sn_index = (signotifyq_t *)sqp - SIGN_PTR(cp, 0);
172*7c478bd9Sstevel@tonic-gate 		id.sn_pid = cp->p_pid;
173*7c478bd9Sstevel@tonic-gate 
174*7c478bd9Sstevel@tonic-gate 		break;
175*7c478bd9Sstevel@tonic-gate 
176*7c478bd9Sstevel@tonic-gate 	case SN_CANCEL:
177*7c478bd9Sstevel@tonic-gate 	case SN_SEND:
178*7c478bd9Sstevel@tonic-gate 
179*7c478bd9Sstevel@tonic-gate 		mutex_enter(&pidlock);
180*7c478bd9Sstevel@tonic-gate 		if ((id.sn_pid <= 0) || ((p = prfind(id.sn_pid)) == NULL)) {
181*7c478bd9Sstevel@tonic-gate 			mutex_exit(&pidlock);
182*7c478bd9Sstevel@tonic-gate 			return (set_errno(EINVAL));
183*7c478bd9Sstevel@tonic-gate 		}
184*7c478bd9Sstevel@tonic-gate 		mutex_enter(&p->p_lock);
185*7c478bd9Sstevel@tonic-gate 		mutex_exit(&pidlock);
186*7c478bd9Sstevel@tonic-gate 
187*7c478bd9Sstevel@tonic-gate 		if (p->p_signhdr == NULL) {
188*7c478bd9Sstevel@tonic-gate 			mutex_exit(&p->p_lock);
189*7c478bd9Sstevel@tonic-gate 			return (set_errno(EINVAL));
190*7c478bd9Sstevel@tonic-gate 		}
191*7c478bd9Sstevel@tonic-gate 
192*7c478bd9Sstevel@tonic-gate 		snqp = SIGN_PTR(p, id.sn_index);
193*7c478bd9Sstevel@tonic-gate 
194*7c478bd9Sstevel@tonic-gate 		if (snqp->sn_snid == 0) {
195*7c478bd9Sstevel@tonic-gate 			mutex_exit(&p->p_lock);
196*7c478bd9Sstevel@tonic-gate 			return (set_errno(EINVAL));
197*7c478bd9Sstevel@tonic-gate 		}
198*7c478bd9Sstevel@tonic-gate 
199*7c478bd9Sstevel@tonic-gate 		if (snqp->sn_snid != get_sigid(cp, (caddr_t)sn_id)) {
200*7c478bd9Sstevel@tonic-gate 			mutex_exit(&p->p_lock);
201*7c478bd9Sstevel@tonic-gate 			return (set_errno(EINVAL));
202*7c478bd9Sstevel@tonic-gate 		}
203*7c478bd9Sstevel@tonic-gate 
204*7c478bd9Sstevel@tonic-gate 		snqp->sn_snid = 0;
205*7c478bd9Sstevel@tonic-gate 
206*7c478bd9Sstevel@tonic-gate 		/* cmd == SN_CANCEL or signo == 0 (SIGEV_NONE) */
207*7c478bd9Sstevel@tonic-gate 		if (((sigqueue_t *)snqp)->sq_info.si_signo <= 0)
208*7c478bd9Sstevel@tonic-gate 			cmd = SN_CANCEL;
209*7c478bd9Sstevel@tonic-gate 
210*7c478bd9Sstevel@tonic-gate 		sigqsend(cmd, p, 0, (sigqueue_t *)snqp);
211*7c478bd9Sstevel@tonic-gate 		mutex_exit(&p->p_lock);
212*7c478bd9Sstevel@tonic-gate 
213*7c478bd9Sstevel@tonic-gate 		id.sn_pid = 0;
214*7c478bd9Sstevel@tonic-gate 		id.sn_index = 0;
215*7c478bd9Sstevel@tonic-gate 
216*7c478bd9Sstevel@tonic-gate 		break;
217*7c478bd9Sstevel@tonic-gate 
218*7c478bd9Sstevel@tonic-gate 	default :
219*7c478bd9Sstevel@tonic-gate 		return (set_errno(EINVAL));
220*7c478bd9Sstevel@tonic-gate 	}
221*7c478bd9Sstevel@tonic-gate 
222*7c478bd9Sstevel@tonic-gate 	if (copyout(&id, sn_id, sizeof (signotify_id_t)))
223*7c478bd9Sstevel@tonic-gate 		return (set_errno(EFAULT));
224*7c478bd9Sstevel@tonic-gate 
225*7c478bd9Sstevel@tonic-gate 	return (0);
226*7c478bd9Sstevel@tonic-gate }
227