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