xref: /titanic_50/usr/src/uts/common/syscall/sigtimedwait.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 2005 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/bitmap.h>
32*7c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
33*7c478bd9Sstevel@tonic-gate #include <sys/systm.h>
34*7c478bd9Sstevel@tonic-gate #include <sys/user.h>
35*7c478bd9Sstevel@tonic-gate #include <sys/errno.h>
36*7c478bd9Sstevel@tonic-gate #include <sys/proc.h>
37*7c478bd9Sstevel@tonic-gate #include <sys/fault.h>
38*7c478bd9Sstevel@tonic-gate #include <sys/procset.h>
39*7c478bd9Sstevel@tonic-gate #include <sys/signal.h>
40*7c478bd9Sstevel@tonic-gate #include <sys/siginfo.h>
41*7c478bd9Sstevel@tonic-gate #include <sys/time.h>
42*7c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
43*7c478bd9Sstevel@tonic-gate #include <sys/schedctl.h>
44*7c478bd9Sstevel@tonic-gate #include <sys/debug.h>
45*7c478bd9Sstevel@tonic-gate #include <sys/condvar_impl.h>
46*7c478bd9Sstevel@tonic-gate #include <sys/model.h>
47*7c478bd9Sstevel@tonic-gate #include <sys/sdt.h>
48*7c478bd9Sstevel@tonic-gate #include <sys/zone.h>
49*7c478bd9Sstevel@tonic-gate 
50*7c478bd9Sstevel@tonic-gate static int
51*7c478bd9Sstevel@tonic-gate copyout_siginfo(model_t datamodel, k_siginfo_t *ksip, void *uaddr)
52*7c478bd9Sstevel@tonic-gate {
53*7c478bd9Sstevel@tonic-gate 	zoneid_t zoneid = getzoneid();
54*7c478bd9Sstevel@tonic-gate 
55*7c478bd9Sstevel@tonic-gate 	if (datamodel == DATAMODEL_NATIVE) {
56*7c478bd9Sstevel@tonic-gate 		if (SI_FROMUSER(ksip) && zoneid != GLOBAL_ZONEID &&
57*7c478bd9Sstevel@tonic-gate 		    zoneid != ksip->si_zoneid) {
58*7c478bd9Sstevel@tonic-gate 			k_siginfo_t sani_sip = *ksip;
59*7c478bd9Sstevel@tonic-gate 			sani_sip.si_pid = curproc->p_zone->zone_zsched->p_pid;
60*7c478bd9Sstevel@tonic-gate 			sani_sip.si_uid = 0;
61*7c478bd9Sstevel@tonic-gate 			sani_sip.si_ctid = -1;
62*7c478bd9Sstevel@tonic-gate 			sani_sip.si_zoneid = zoneid;
63*7c478bd9Sstevel@tonic-gate 			if (copyout(&sani_sip, uaddr, sizeof (sani_sip)))
64*7c478bd9Sstevel@tonic-gate 				return (set_errno(EFAULT));
65*7c478bd9Sstevel@tonic-gate 		} else {
66*7c478bd9Sstevel@tonic-gate 			if (copyout(ksip, uaddr, sizeof (*ksip)))
67*7c478bd9Sstevel@tonic-gate 				return (set_errno(EFAULT));
68*7c478bd9Sstevel@tonic-gate 		}
69*7c478bd9Sstevel@tonic-gate 	}
70*7c478bd9Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
71*7c478bd9Sstevel@tonic-gate 	else {
72*7c478bd9Sstevel@tonic-gate 		siginfo32_t si32;
73*7c478bd9Sstevel@tonic-gate 
74*7c478bd9Sstevel@tonic-gate 		siginfo_kto32(ksip, &si32);
75*7c478bd9Sstevel@tonic-gate 		if (SI_FROMUSER(ksip) && zoneid != GLOBAL_ZONEID &&
76*7c478bd9Sstevel@tonic-gate 		    zoneid != ksip->si_zoneid) {
77*7c478bd9Sstevel@tonic-gate 			si32.si_pid = curproc->p_zone->zone_zsched->p_pid;
78*7c478bd9Sstevel@tonic-gate 			si32.si_uid = 0;
79*7c478bd9Sstevel@tonic-gate 			si32.si_ctid = -1;
80*7c478bd9Sstevel@tonic-gate 			si32.si_zoneid = zoneid;
81*7c478bd9Sstevel@tonic-gate 		}
82*7c478bd9Sstevel@tonic-gate 		if (copyout(&si32, uaddr, sizeof (si32)))
83*7c478bd9Sstevel@tonic-gate 			return (set_errno(EFAULT));
84*7c478bd9Sstevel@tonic-gate 	}
85*7c478bd9Sstevel@tonic-gate #endif
86*7c478bd9Sstevel@tonic-gate 	return (ksip->si_signo);
87*7c478bd9Sstevel@tonic-gate }
88*7c478bd9Sstevel@tonic-gate 
89*7c478bd9Sstevel@tonic-gate /*
90*7c478bd9Sstevel@tonic-gate  * Wait until a signal within a specified set is posted or until the
91*7c478bd9Sstevel@tonic-gate  * time interval 'timeout' if specified.  The signal is caught but
92*7c478bd9Sstevel@tonic-gate  * not delivered. The value of the signal is returned to the caller.
93*7c478bd9Sstevel@tonic-gate  */
94*7c478bd9Sstevel@tonic-gate int
95*7c478bd9Sstevel@tonic-gate sigtimedwait(sigset_t *setp, siginfo_t *siginfop, timespec_t *timeoutp)
96*7c478bd9Sstevel@tonic-gate {
97*7c478bd9Sstevel@tonic-gate 	sigset_t set;
98*7c478bd9Sstevel@tonic-gate 	k_sigset_t kset;
99*7c478bd9Sstevel@tonic-gate 	k_sigset_t oldmask;
100*7c478bd9Sstevel@tonic-gate 	kthread_t *t = curthread;
101*7c478bd9Sstevel@tonic-gate 	klwp_t *lwp = ttolwp(t);
102*7c478bd9Sstevel@tonic-gate 	proc_t *p = ttoproc(t);
103*7c478bd9Sstevel@tonic-gate 	timespec_t sig_timeout;
104*7c478bd9Sstevel@tonic-gate 	timespec_t *rqtp = NULL;
105*7c478bd9Sstevel@tonic-gate 	int timecheck = 0;
106*7c478bd9Sstevel@tonic-gate 	int ret;
107*7c478bd9Sstevel@tonic-gate 	int error = 0;
108*7c478bd9Sstevel@tonic-gate 	k_siginfo_t info, *infop;
109*7c478bd9Sstevel@tonic-gate 	model_t datamodel = get_udatamodel();
110*7c478bd9Sstevel@tonic-gate 
111*7c478bd9Sstevel@tonic-gate 	if (timeoutp) {
112*7c478bd9Sstevel@tonic-gate 		timespec_t now;
113*7c478bd9Sstevel@tonic-gate 
114*7c478bd9Sstevel@tonic-gate 		timecheck = timechanged;
115*7c478bd9Sstevel@tonic-gate 		gethrestime(&now);
116*7c478bd9Sstevel@tonic-gate 		if (datamodel == DATAMODEL_NATIVE) {
117*7c478bd9Sstevel@tonic-gate 			if (copyin(timeoutp, &sig_timeout,
118*7c478bd9Sstevel@tonic-gate 			    sizeof (sig_timeout)))
119*7c478bd9Sstevel@tonic-gate 				return (set_errno(EFAULT));
120*7c478bd9Sstevel@tonic-gate 		} else {
121*7c478bd9Sstevel@tonic-gate 			timespec32_t timeout32;
122*7c478bd9Sstevel@tonic-gate 
123*7c478bd9Sstevel@tonic-gate 			if (copyin(timeoutp, &timeout32, sizeof (timeout32)))
124*7c478bd9Sstevel@tonic-gate 				return (set_errno(EFAULT));
125*7c478bd9Sstevel@tonic-gate 			TIMESPEC32_TO_TIMESPEC(&sig_timeout, &timeout32)
126*7c478bd9Sstevel@tonic-gate 		}
127*7c478bd9Sstevel@tonic-gate 
128*7c478bd9Sstevel@tonic-gate 		if (itimerspecfix(&sig_timeout))
129*7c478bd9Sstevel@tonic-gate 			return (set_errno(EINVAL));
130*7c478bd9Sstevel@tonic-gate 		/*
131*7c478bd9Sstevel@tonic-gate 		 * Convert the timespec value into absolute time.
132*7c478bd9Sstevel@tonic-gate 		 */
133*7c478bd9Sstevel@tonic-gate 		timespecadd(&sig_timeout, &now);
134*7c478bd9Sstevel@tonic-gate 		rqtp = &sig_timeout;
135*7c478bd9Sstevel@tonic-gate 	}
136*7c478bd9Sstevel@tonic-gate 	if (copyin(setp, &set, sizeof (set)))
137*7c478bd9Sstevel@tonic-gate 		return (set_errno(EFAULT));
138*7c478bd9Sstevel@tonic-gate 	sigutok(&set, &kset);
139*7c478bd9Sstevel@tonic-gate 	if (sigisempty(&kset))
140*7c478bd9Sstevel@tonic-gate 		return (set_errno(EINVAL));
141*7c478bd9Sstevel@tonic-gate 
142*7c478bd9Sstevel@tonic-gate 	mutex_enter(&p->p_lock);
143*7c478bd9Sstevel@tonic-gate 	/*
144*7c478bd9Sstevel@tonic-gate 	 * set the thread's signal mask to unmask
145*7c478bd9Sstevel@tonic-gate 	 * those signals in the specified set.
146*7c478bd9Sstevel@tonic-gate 	 */
147*7c478bd9Sstevel@tonic-gate 	schedctl_finish_sigblock(t);
148*7c478bd9Sstevel@tonic-gate 	oldmask = t->t_hold;
149*7c478bd9Sstevel@tonic-gate 	sigdiffset(&t->t_hold, &kset);
150*7c478bd9Sstevel@tonic-gate 
151*7c478bd9Sstevel@tonic-gate 	/*
152*7c478bd9Sstevel@tonic-gate 	 * Wait until we take a signal or until
153*7c478bd9Sstevel@tonic-gate 	 * the absolute future time is passed.
154*7c478bd9Sstevel@tonic-gate 	 */
155*7c478bd9Sstevel@tonic-gate 	while ((ret = cv_waituntil_sig(&t->t_delay_cv, &p->p_lock,
156*7c478bd9Sstevel@tonic-gate 	    rqtp, timecheck)) > 0)
157*7c478bd9Sstevel@tonic-gate 		continue;
158*7c478bd9Sstevel@tonic-gate 	if (ret == -1)
159*7c478bd9Sstevel@tonic-gate 		error = EAGAIN;
160*7c478bd9Sstevel@tonic-gate 
161*7c478bd9Sstevel@tonic-gate 	/*
162*7c478bd9Sstevel@tonic-gate 	 * Restore thread's signal mask to its previous value.
163*7c478bd9Sstevel@tonic-gate 	 */
164*7c478bd9Sstevel@tonic-gate 	t->t_hold = oldmask;
165*7c478bd9Sstevel@tonic-gate 	t->t_sig_check = 1;	/* so post_syscall sees new t_hold mask */
166*7c478bd9Sstevel@tonic-gate 
167*7c478bd9Sstevel@tonic-gate 	if (error) {
168*7c478bd9Sstevel@tonic-gate 		mutex_exit(&p->p_lock);
169*7c478bd9Sstevel@tonic-gate 		return (set_errno(error));	/* timer expired */
170*7c478bd9Sstevel@tonic-gate 	}
171*7c478bd9Sstevel@tonic-gate 	/*
172*7c478bd9Sstevel@tonic-gate 	 * Don't bother with signal if it is not in request set.
173*7c478bd9Sstevel@tonic-gate 	 */
174*7c478bd9Sstevel@tonic-gate 	if (lwp->lwp_cursig == 0 || !sigismember(&kset, lwp->lwp_cursig)) {
175*7c478bd9Sstevel@tonic-gate 		mutex_exit(&p->p_lock);
176*7c478bd9Sstevel@tonic-gate 		/*
177*7c478bd9Sstevel@tonic-gate 		 * lwp_cursig is zero if pokelwps() awakened cv_wait_sig().
178*7c478bd9Sstevel@tonic-gate 		 * This happens if some other thread in this process called
179*7c478bd9Sstevel@tonic-gate 		 * forkall() or exit().
180*7c478bd9Sstevel@tonic-gate 		 */
181*7c478bd9Sstevel@tonic-gate 		return (set_errno(EINTR));
182*7c478bd9Sstevel@tonic-gate 	}
183*7c478bd9Sstevel@tonic-gate 
184*7c478bd9Sstevel@tonic-gate 	if (lwp->lwp_curinfo)
185*7c478bd9Sstevel@tonic-gate 		infop = &lwp->lwp_curinfo->sq_info;
186*7c478bd9Sstevel@tonic-gate 	else {
187*7c478bd9Sstevel@tonic-gate 		infop = &info;
188*7c478bd9Sstevel@tonic-gate 		bzero(infop, sizeof (info));
189*7c478bd9Sstevel@tonic-gate 		infop->si_signo = lwp->lwp_cursig;
190*7c478bd9Sstevel@tonic-gate 		infop->si_code = SI_NOINFO;
191*7c478bd9Sstevel@tonic-gate 	}
192*7c478bd9Sstevel@tonic-gate 
193*7c478bd9Sstevel@tonic-gate 	lwp->lwp_ru.nsignals++;
194*7c478bd9Sstevel@tonic-gate 	ret = lwp->lwp_cursig;
195*7c478bd9Sstevel@tonic-gate 	DTRACE_PROC2(signal__clear, int, ret, ksiginfo_t *, infop);
196*7c478bd9Sstevel@tonic-gate 	lwp->lwp_cursig = 0;
197*7c478bd9Sstevel@tonic-gate 	lwp->lwp_extsig = 0;
198*7c478bd9Sstevel@tonic-gate 	mutex_exit(&p->p_lock);
199*7c478bd9Sstevel@tonic-gate 
200*7c478bd9Sstevel@tonic-gate 	if (siginfop)
201*7c478bd9Sstevel@tonic-gate 		ret = copyout_siginfo(datamodel, infop, siginfop);
202*7c478bd9Sstevel@tonic-gate 	if (lwp->lwp_curinfo) {
203*7c478bd9Sstevel@tonic-gate 		siginfofree(lwp->lwp_curinfo);
204*7c478bd9Sstevel@tonic-gate 		lwp->lwp_curinfo = NULL;
205*7c478bd9Sstevel@tonic-gate 	}
206*7c478bd9Sstevel@tonic-gate 	return (ret);
207*7c478bd9Sstevel@tonic-gate }
208