xref: /titanic_44/usr/src/uts/common/syscall/signotify.c (revision 55553f719b521a0bb4deab6efc944cd30c1a56aa)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include <sys/param.h>
30 #include <sys/types.h>
31 #include <sys/sysmacros.h>
32 #include <sys/systm.h>
33 #include <sys/errno.h>
34 #include <sys/proc.h>
35 #include <sys/procset.h>
36 #include <sys/fault.h>
37 #include <sys/signal.h>
38 #include <sys/siginfo.h>
39 #include <sys/schedctl.h>
40 #include <vm/as.h>
41 #include <sys/debug.h>
42 #include <sys/contract/process_impl.h>
43 
44 /*ARGSUSED*/
45 static int
46 copyin_siginfo(model_t datamodel, void *uaddr, k_siginfo_t *ksip)
47 {
48 #ifdef _SYSCALL32_IMPL
49 	int ret;
50 
51 	if (datamodel == DATAMODEL_NATIVE) {
52 #endif
53 		return (copyin(uaddr, ksip, sizeof (k_siginfo_t)));
54 #ifdef _SYSCALL32_IMPL
55 	} else {
56 		siginfo32_t si32;
57 
58 		if (ret = copyin(uaddr, &si32, sizeof (si32)))
59 			return (ret);
60 
61 		siginfo_32tok(&si32, ksip);
62 	}
63 
64 	return (0);
65 #endif
66 }
67 
68 /*
69  * To find secured 64 bit id for signotify() call
70  * This depends upon as_getmemid() which returns
71  * unique vnode/offset for a user virtual address.
72  */
73 static u_longlong_t
74 get_sigid(proc_t *p, caddr_t addr)
75 {
76 	u_longlong_t snid = 0;
77 	memid_t memid;
78 	quad_t *tquad = (quad_t *)&snid;
79 
80 	if (!as_getmemid(p->p_as, addr, &memid)) {
81 		tquad->val[0] = (int)memid.val[0];
82 		tquad->val[1] = (int)memid.val[1];
83 	}
84 	return (snid);
85 }
86 
87 #define	SIGN_PTR(p, n)	&((signotifyq_t *)(&p->p_signhdr[1]))[n];
88 
89 int
90 signotify(int cmd, siginfo_t *siginfo, signotify_id_t *sn_id)
91 {
92 	k_siginfo_t	info;
93 	signotify_id_t	id;
94 	proc_t		*p;
95 	proc_t		*cp = curproc;
96 	signotifyq_t	*snqp;
97 	struct cred	*cr;
98 	sigqueue_t	*sqp;
99 	sigqhdr_t	*sqh;
100 	u_longlong_t	sid;
101 	model_t 	datamodel = get_udatamodel();
102 
103 	if (copyin(sn_id, &id, sizeof (signotify_id_t)))
104 		return (set_errno(EFAULT));
105 
106 	if (id.sn_index >= _SIGNOTIFY_MAX || id.sn_index < 0)
107 		return (set_errno(EINVAL));
108 
109 	switch (cmd) {
110 	case SN_PROC:
111 		/* get snid for the given user address of signotifyid_t */
112 		sid = get_sigid(cp, (caddr_t)sn_id);
113 
114 		if (id.sn_pid > 0) {
115 			mutex_enter(&pidlock);
116 			if ((p = prfind(id.sn_pid)) != NULL) {
117 				mutex_enter(&p->p_lock);
118 				if (p->p_signhdr != NULL) {
119 					snqp = SIGN_PTR(p, id.sn_index);
120 					if (snqp->sn_snid == sid) {
121 						mutex_exit(&p->p_lock);
122 						mutex_exit(&pidlock);
123 						return (set_errno(EBUSY));
124 					}
125 				}
126 				mutex_exit(&p->p_lock);
127 			}
128 			mutex_exit(&pidlock);
129 		}
130 
131 		if (copyin_siginfo(datamodel, siginfo, &info))
132 			return (set_errno(EFAULT));
133 
134 		/* The si_code value must indicate the signal will be queued */
135 		if (!sigwillqueue(info.si_signo, info.si_code))
136 			return (set_errno(EINVAL));
137 
138 		if (cp->p_signhdr == NULL) {
139 			/* Allocate signotify pool first time */
140 			sqh = sigqhdralloc(sizeof (signotifyq_t),
141 			    _SIGNOTIFY_MAX);
142 			mutex_enter(&cp->p_lock);
143 			if (cp->p_signhdr == NULL) {
144 				/* hang the pool head on proc */
145 				cp->p_signhdr = sqh;
146 			} else {
147 				/* another lwp allocated the pool, free ours */
148 				sigqhdrfree(sqh);
149 			}
150 		} else {
151 			mutex_enter(&cp->p_lock);
152 		}
153 
154 		sqp = sigqalloc(cp->p_signhdr);
155 		if (sqp == NULL) {
156 			mutex_exit(&cp->p_lock);
157 			return (set_errno(EAGAIN));
158 		}
159 		cr = CRED();
160 		sqp->sq_info = info;
161 		sqp->sq_info.si_pid = cp->p_pid;
162 		sqp->sq_info.si_ctid = PRCTID(cp);
163 		sqp->sq_info.si_zoneid = getzoneid();
164 		sqp->sq_info.si_uid = crgetruid(cr);
165 
166 		/* fill the signotifyq_t fields */
167 		((signotifyq_t *)sqp)->sn_snid = sid;
168 
169 		mutex_exit(&cp->p_lock);
170 
171 		/* complete the signotify_id_t fields */
172 		id.sn_index = (signotifyq_t *)sqp - SIGN_PTR(cp, 0);
173 		id.sn_pid = cp->p_pid;
174 
175 		break;
176 
177 	case SN_CANCEL:
178 	case SN_SEND:
179 
180 		mutex_enter(&pidlock);
181 		if ((id.sn_pid <= 0) || ((p = prfind(id.sn_pid)) == NULL)) {
182 			mutex_exit(&pidlock);
183 			return (set_errno(EINVAL));
184 		}
185 		mutex_enter(&p->p_lock);
186 		mutex_exit(&pidlock);
187 
188 		if (p->p_signhdr == NULL) {
189 			mutex_exit(&p->p_lock);
190 			return (set_errno(EINVAL));
191 		}
192 
193 		snqp = SIGN_PTR(p, id.sn_index);
194 
195 		if (snqp->sn_snid == 0) {
196 			mutex_exit(&p->p_lock);
197 			return (set_errno(EINVAL));
198 		}
199 
200 		if (snqp->sn_snid != get_sigid(cp, (caddr_t)sn_id)) {
201 			mutex_exit(&p->p_lock);
202 			return (set_errno(EINVAL));
203 		}
204 
205 		snqp->sn_snid = 0;
206 
207 		/* cmd == SN_CANCEL or signo == 0 (SIGEV_NONE) */
208 		if (((sigqueue_t *)snqp)->sq_info.si_signo <= 0)
209 			cmd = SN_CANCEL;
210 
211 		sigqsend(cmd, p, 0, (sigqueue_t *)snqp);
212 		mutex_exit(&p->p_lock);
213 
214 		id.sn_pid = 0;
215 		id.sn_index = 0;
216 
217 		break;
218 
219 	default :
220 		return (set_errno(EINVAL));
221 	}
222 
223 	if (copyout(&id, sn_id, sizeof (signotify_id_t)))
224 		return (set_errno(EFAULT));
225 
226 	return (0);
227 }
228 
229 int
230 sigresend(int sig, siginfo_t *siginfo, sigset_t *mask)
231 {
232 	kthread_t *t = curthread;
233 	proc_t *p = ttoproc(t);
234 	klwp_t *lwp = ttolwp(t);
235 	sigqueue_t *sqp = kmem_zalloc(sizeof (*sqp), KM_SLEEP);
236 	sigset_t set;
237 	k_sigset_t kset;
238 	int error;
239 
240 	if (sig <= 0 || sig >= NSIG || sigismember(&cantmask, sig)) {
241 		error = EINVAL;
242 		goto bad;
243 	}
244 
245 	if (siginfo == NULL) {
246 		sqp->sq_info.si_signo = sig;
247 		sqp->sq_info.si_code = SI_NOINFO;
248 	} else {
249 		if (copyin_siginfo(get_udatamodel(), siginfo, &sqp->sq_info)) {
250 			error = EFAULT;
251 			goto bad;
252 		}
253 		if (sqp->sq_info.si_signo != sig) {
254 			error = EINVAL;
255 			goto bad;
256 		}
257 	}
258 
259 	if (copyin(mask, &set, sizeof (set))) {
260 		error = EFAULT;
261 		goto bad;
262 	}
263 	sigutok(&set, &kset);
264 
265 	mutex_enter(&p->p_lock);
266 	if (lwp->lwp_cursig || lwp->lwp_curinfo) {
267 		mutex_exit(&p->p_lock);
268 		t->t_sig_check = 1;
269 		error = EAGAIN;
270 		goto bad;
271 	}
272 	lwp->lwp_cursig = sig;
273 	lwp->lwp_curinfo = sqp;
274 	schedctl_finish_sigblock(t);
275 	t->t_hold = kset;
276 	mutex_exit(&p->p_lock);
277 
278 	t->t_sig_check = 1;
279 	return (0);
280 bad:
281 	kmem_free(sqp, sizeof (*sqp));
282 	return (set_errno(error));
283 }
284