xref: /titanic_52/usr/src/uts/common/syscall/sigtimedwait.c (revision 9524a69f24734e80c50bfc33c4f1bce173d465a1)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*9524a69fSRoger A. Faulkner  * Common Development and Distribution License (the "License").
6*9524a69fSRoger A. Faulkner  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
21*9524a69fSRoger A. Faulkner 
227c478bd9Sstevel@tonic-gate /*
23*9524a69fSRoger A. Faulkner  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #include <sys/param.h>
287c478bd9Sstevel@tonic-gate #include <sys/types.h>
297c478bd9Sstevel@tonic-gate #include <sys/bitmap.h>
307c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
317c478bd9Sstevel@tonic-gate #include <sys/systm.h>
327c478bd9Sstevel@tonic-gate #include <sys/user.h>
337c478bd9Sstevel@tonic-gate #include <sys/errno.h>
347c478bd9Sstevel@tonic-gate #include <sys/proc.h>
357c478bd9Sstevel@tonic-gate #include <sys/fault.h>
367c478bd9Sstevel@tonic-gate #include <sys/procset.h>
377c478bd9Sstevel@tonic-gate #include <sys/signal.h>
387c478bd9Sstevel@tonic-gate #include <sys/siginfo.h>
397c478bd9Sstevel@tonic-gate #include <sys/time.h>
407c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
417c478bd9Sstevel@tonic-gate #include <sys/schedctl.h>
427c478bd9Sstevel@tonic-gate #include <sys/debug.h>
437c478bd9Sstevel@tonic-gate #include <sys/condvar_impl.h>
447c478bd9Sstevel@tonic-gate #include <sys/model.h>
457c478bd9Sstevel@tonic-gate #include <sys/sdt.h>
467c478bd9Sstevel@tonic-gate #include <sys/zone.h>
477c478bd9Sstevel@tonic-gate 
487c478bd9Sstevel@tonic-gate static int
497c478bd9Sstevel@tonic-gate copyout_siginfo(model_t datamodel, k_siginfo_t *ksip, void *uaddr)
507c478bd9Sstevel@tonic-gate {
517c478bd9Sstevel@tonic-gate 	zoneid_t zoneid = getzoneid();
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate 	if (datamodel == DATAMODEL_NATIVE) {
547c478bd9Sstevel@tonic-gate 		if (SI_FROMUSER(ksip) && zoneid != GLOBAL_ZONEID &&
557c478bd9Sstevel@tonic-gate 		    zoneid != ksip->si_zoneid) {
567c478bd9Sstevel@tonic-gate 			k_siginfo_t sani_sip = *ksip;
577c478bd9Sstevel@tonic-gate 			sani_sip.si_pid = curproc->p_zone->zone_zsched->p_pid;
587c478bd9Sstevel@tonic-gate 			sani_sip.si_uid = 0;
597c478bd9Sstevel@tonic-gate 			sani_sip.si_ctid = -1;
607c478bd9Sstevel@tonic-gate 			sani_sip.si_zoneid = zoneid;
617c478bd9Sstevel@tonic-gate 			if (copyout(&sani_sip, uaddr, sizeof (sani_sip)))
627c478bd9Sstevel@tonic-gate 				return (set_errno(EFAULT));
637c478bd9Sstevel@tonic-gate 		} else {
647c478bd9Sstevel@tonic-gate 			if (copyout(ksip, uaddr, sizeof (*ksip)))
657c478bd9Sstevel@tonic-gate 				return (set_errno(EFAULT));
667c478bd9Sstevel@tonic-gate 		}
677c478bd9Sstevel@tonic-gate 	}
687c478bd9Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
697c478bd9Sstevel@tonic-gate 	else {
707c478bd9Sstevel@tonic-gate 		siginfo32_t si32;
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate 		siginfo_kto32(ksip, &si32);
737c478bd9Sstevel@tonic-gate 		if (SI_FROMUSER(ksip) && zoneid != GLOBAL_ZONEID &&
747c478bd9Sstevel@tonic-gate 		    zoneid != ksip->si_zoneid) {
757c478bd9Sstevel@tonic-gate 			si32.si_pid = curproc->p_zone->zone_zsched->p_pid;
767c478bd9Sstevel@tonic-gate 			si32.si_uid = 0;
777c478bd9Sstevel@tonic-gate 			si32.si_ctid = -1;
787c478bd9Sstevel@tonic-gate 			si32.si_zoneid = zoneid;
797c478bd9Sstevel@tonic-gate 		}
807c478bd9Sstevel@tonic-gate 		if (copyout(&si32, uaddr, sizeof (si32)))
817c478bd9Sstevel@tonic-gate 			return (set_errno(EFAULT));
827c478bd9Sstevel@tonic-gate 	}
837c478bd9Sstevel@tonic-gate #endif
847c478bd9Sstevel@tonic-gate 	return (ksip->si_signo);
857c478bd9Sstevel@tonic-gate }
867c478bd9Sstevel@tonic-gate 
877c478bd9Sstevel@tonic-gate /*
887c478bd9Sstevel@tonic-gate  * Wait until a signal within a specified set is posted or until the
897c478bd9Sstevel@tonic-gate  * time interval 'timeout' if specified.  The signal is caught but
907c478bd9Sstevel@tonic-gate  * not delivered. The value of the signal is returned to the caller.
917c478bd9Sstevel@tonic-gate  */
927c478bd9Sstevel@tonic-gate int
937c478bd9Sstevel@tonic-gate sigtimedwait(sigset_t *setp, siginfo_t *siginfop, timespec_t *timeoutp)
947c478bd9Sstevel@tonic-gate {
957c478bd9Sstevel@tonic-gate 	sigset_t set;
967c478bd9Sstevel@tonic-gate 	k_sigset_t oldmask;
977c478bd9Sstevel@tonic-gate 	kthread_t *t = curthread;
987c478bd9Sstevel@tonic-gate 	klwp_t *lwp = ttolwp(t);
997c478bd9Sstevel@tonic-gate 	proc_t *p = ttoproc(t);
1007c478bd9Sstevel@tonic-gate 	timespec_t sig_timeout;
1017c478bd9Sstevel@tonic-gate 	timespec_t *rqtp = NULL;
1023348528fSdm120769 	int timecheck = 0;
1037c478bd9Sstevel@tonic-gate 	int ret;
1047c478bd9Sstevel@tonic-gate 	int error = 0;
1057c478bd9Sstevel@tonic-gate 	k_siginfo_t info, *infop;
1067c478bd9Sstevel@tonic-gate 	model_t datamodel = get_udatamodel();
1077c478bd9Sstevel@tonic-gate 
1087c478bd9Sstevel@tonic-gate 	if (timeoutp) {
1097c478bd9Sstevel@tonic-gate 		timespec_t now;
1107c478bd9Sstevel@tonic-gate 
1113348528fSdm120769 		timecheck = timechanged;
1127c478bd9Sstevel@tonic-gate 		gethrestime(&now);
1137c478bd9Sstevel@tonic-gate 		if (datamodel == DATAMODEL_NATIVE) {
1147c478bd9Sstevel@tonic-gate 			if (copyin(timeoutp, &sig_timeout,
1157c478bd9Sstevel@tonic-gate 			    sizeof (sig_timeout)))
1167c478bd9Sstevel@tonic-gate 				return (set_errno(EFAULT));
1177c478bd9Sstevel@tonic-gate 		} else {
1187c478bd9Sstevel@tonic-gate 			timespec32_t timeout32;
1197c478bd9Sstevel@tonic-gate 
1207c478bd9Sstevel@tonic-gate 			if (copyin(timeoutp, &timeout32, sizeof (timeout32)))
1217c478bd9Sstevel@tonic-gate 				return (set_errno(EFAULT));
1227c478bd9Sstevel@tonic-gate 			TIMESPEC32_TO_TIMESPEC(&sig_timeout, &timeout32)
1237c478bd9Sstevel@tonic-gate 		}
1247c478bd9Sstevel@tonic-gate 
1257c478bd9Sstevel@tonic-gate 		if (itimerspecfix(&sig_timeout))
1267c478bd9Sstevel@tonic-gate 			return (set_errno(EINVAL));
1277c478bd9Sstevel@tonic-gate 		/*
1287c478bd9Sstevel@tonic-gate 		 * Convert the timespec value into absolute time.
1297c478bd9Sstevel@tonic-gate 		 */
1307c478bd9Sstevel@tonic-gate 		timespecadd(&sig_timeout, &now);
1317c478bd9Sstevel@tonic-gate 		rqtp = &sig_timeout;
1327c478bd9Sstevel@tonic-gate 	}
1337c478bd9Sstevel@tonic-gate 	if (copyin(setp, &set, sizeof (set)))
1347c478bd9Sstevel@tonic-gate 		return (set_errno(EFAULT));
135*9524a69fSRoger A. Faulkner 	sigutok(&set, &t->t_sigwait);
136*9524a69fSRoger A. Faulkner 	if (sigisempty(&t->t_sigwait))
1377c478bd9Sstevel@tonic-gate 		return (set_errno(EINVAL));
1387c478bd9Sstevel@tonic-gate 
1397c478bd9Sstevel@tonic-gate 	mutex_enter(&p->p_lock);
1407c478bd9Sstevel@tonic-gate 	/*
1417c478bd9Sstevel@tonic-gate 	 * set the thread's signal mask to unmask
1427c478bd9Sstevel@tonic-gate 	 * those signals in the specified set.
1437c478bd9Sstevel@tonic-gate 	 */
1447c478bd9Sstevel@tonic-gate 	schedctl_finish_sigblock(t);
1457c478bd9Sstevel@tonic-gate 	oldmask = t->t_hold;
146*9524a69fSRoger A. Faulkner 	sigdiffset(&t->t_hold, &t->t_sigwait);
1477c478bd9Sstevel@tonic-gate 
1487c478bd9Sstevel@tonic-gate 	/*
1497c478bd9Sstevel@tonic-gate 	 * Wait until we take a signal or until
1507c478bd9Sstevel@tonic-gate 	 * the absolute future time is passed.
1517c478bd9Sstevel@tonic-gate 	 */
1527c478bd9Sstevel@tonic-gate 	while ((ret = cv_waituntil_sig(&t->t_delay_cv, &p->p_lock,
1533348528fSdm120769 	    rqtp, timecheck)) > 0)
1547c478bd9Sstevel@tonic-gate 		continue;
1557c478bd9Sstevel@tonic-gate 	if (ret == -1)
1567c478bd9Sstevel@tonic-gate 		error = EAGAIN;
1577c478bd9Sstevel@tonic-gate 
1587c478bd9Sstevel@tonic-gate 	/*
1597c478bd9Sstevel@tonic-gate 	 * Restore thread's signal mask to its previous value.
1607c478bd9Sstevel@tonic-gate 	 */
1617c478bd9Sstevel@tonic-gate 	t->t_hold = oldmask;
1627c478bd9Sstevel@tonic-gate 	t->t_sig_check = 1;	/* so post_syscall sees new t_hold mask */
1637c478bd9Sstevel@tonic-gate 
1647c478bd9Sstevel@tonic-gate 	if (error) {
1657c478bd9Sstevel@tonic-gate 		mutex_exit(&p->p_lock);
166*9524a69fSRoger A. Faulkner 		sigemptyset(&t->t_sigwait);
1677c478bd9Sstevel@tonic-gate 		return (set_errno(error));	/* timer expired */
1687c478bd9Sstevel@tonic-gate 	}
1697c478bd9Sstevel@tonic-gate 	/*
1707c478bd9Sstevel@tonic-gate 	 * Don't bother with signal if it is not in request set.
1717c478bd9Sstevel@tonic-gate 	 */
172*9524a69fSRoger A. Faulkner 	if (lwp->lwp_cursig == 0 ||
173*9524a69fSRoger A. Faulkner 	    !sigismember(&t->t_sigwait, lwp->lwp_cursig)) {
1747c478bd9Sstevel@tonic-gate 		mutex_exit(&p->p_lock);
1757c478bd9Sstevel@tonic-gate 		/*
1767c478bd9Sstevel@tonic-gate 		 * lwp_cursig is zero if pokelwps() awakened cv_wait_sig().
1777c478bd9Sstevel@tonic-gate 		 * This happens if some other thread in this process called
1787c478bd9Sstevel@tonic-gate 		 * forkall() or exit().
1797c478bd9Sstevel@tonic-gate 		 */
180*9524a69fSRoger A. Faulkner 		sigemptyset(&t->t_sigwait);
1817c478bd9Sstevel@tonic-gate 		return (set_errno(EINTR));
1827c478bd9Sstevel@tonic-gate 	}
1837c478bd9Sstevel@tonic-gate 
1847c478bd9Sstevel@tonic-gate 	if (lwp->lwp_curinfo)
1857c478bd9Sstevel@tonic-gate 		infop = &lwp->lwp_curinfo->sq_info;
1867c478bd9Sstevel@tonic-gate 	else {
1877c478bd9Sstevel@tonic-gate 		infop = &info;
1887c478bd9Sstevel@tonic-gate 		bzero(infop, sizeof (info));
1897c478bd9Sstevel@tonic-gate 		infop->si_signo = lwp->lwp_cursig;
1907c478bd9Sstevel@tonic-gate 		infop->si_code = SI_NOINFO;
1917c478bd9Sstevel@tonic-gate 	}
1927c478bd9Sstevel@tonic-gate 
1937c478bd9Sstevel@tonic-gate 	lwp->lwp_ru.nsignals++;
1947c478bd9Sstevel@tonic-gate 	ret = lwp->lwp_cursig;
1957c478bd9Sstevel@tonic-gate 	DTRACE_PROC2(signal__clear, int, ret, ksiginfo_t *, infop);
1967c478bd9Sstevel@tonic-gate 	lwp->lwp_cursig = 0;
1977c478bd9Sstevel@tonic-gate 	lwp->lwp_extsig = 0;
1987c478bd9Sstevel@tonic-gate 	mutex_exit(&p->p_lock);
1997c478bd9Sstevel@tonic-gate 
2007c478bd9Sstevel@tonic-gate 	if (siginfop)
2017c478bd9Sstevel@tonic-gate 		ret = copyout_siginfo(datamodel, infop, siginfop);
2027c478bd9Sstevel@tonic-gate 	if (lwp->lwp_curinfo) {
2037c478bd9Sstevel@tonic-gate 		siginfofree(lwp->lwp_curinfo);
2047c478bd9Sstevel@tonic-gate 		lwp->lwp_curinfo = NULL;
2057c478bd9Sstevel@tonic-gate 	}
206*9524a69fSRoger A. Faulkner 	sigemptyset(&t->t_sigwait);
2077c478bd9Sstevel@tonic-gate 	return (ret);
2087c478bd9Sstevel@tonic-gate }
209