xref: /illumos-gate/usr/src/uts/common/syscall/signotify.c (revision 88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98)
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, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2004 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 <vm/as.h>
40 #include <sys/debug.h>
41 #include <sys/contract/process_impl.h>
42 
43 /*ARGSUSED*/
44 static int
45 copyin_siginfo(model_t datamodel, void *uaddr, k_siginfo_t *ksip)
46 {
47 #ifdef _SYSCALL32_IMPL
48 	int ret;
49 
50 	if (datamodel == DATAMODEL_NATIVE) {
51 #endif
52 		return (copyin(uaddr, ksip, sizeof (k_siginfo_t)));
53 #ifdef _SYSCALL32_IMPL
54 	} else {
55 		siginfo32_t si32;
56 
57 		if (ret = copyin(uaddr, &si32, sizeof (si32)))
58 			return (ret);
59 
60 		siginfo_32tok(&si32, ksip);
61 	}
62 
63 	return (0);
64 #endif
65 }
66 
67 /*
68  * To find secured 64 bit id for signotify() call
69  * This depends upon as_getmemid() which returns
70  * unique vnode/offset for a user virtual address.
71  */
72 static u_longlong_t
73 get_sigid(proc_t *p, caddr_t addr)
74 {
75 	u_longlong_t snid = 0;
76 	memid_t memid;
77 	quad_t *tquad = (quad_t *)&snid;
78 
79 	if (!as_getmemid(p->p_as, addr, &memid)) {
80 		tquad->val[0] = (int)memid.val[0];
81 		tquad->val[1] = (int)memid.val[1];
82 	}
83 	return (snid);
84 }
85 
86 #define	SIGN_PTR(p, n)	&((signotifyq_t *)(&p->p_signhdr[1]))[n];
87 
88 int
89 signotify(int cmd, siginfo_t *siginfo, signotify_id_t *sn_id)
90 {
91 	k_siginfo_t	info;
92 	signotify_id_t	id;
93 	proc_t		*p;
94 	proc_t		*cp = curproc;
95 	signotifyq_t	*snqp;
96 	struct cred	*cr;
97 	sigqueue_t	*sqp;
98 	sigqhdr_t	*sqh;
99 	u_longlong_t	sid;
100 	model_t 	datamodel = get_udatamodel();
101 
102 	if (copyin(sn_id, &id, sizeof (signotify_id_t)))
103 		return (set_errno(EFAULT));
104 
105 	if (id.sn_index >= _SIGNOTIFY_MAX || id.sn_index < 0)
106 		return (set_errno(EINVAL));
107 
108 	switch (cmd) {
109 	case SN_PROC:
110 		/* get snid for the given user address of signotifyid_t */
111 		sid = get_sigid(cp, (caddr_t)sn_id);
112 
113 		if (id.sn_pid > 0) {
114 			mutex_enter(&pidlock);
115 			if ((p = prfind(id.sn_pid)) != NULL) {
116 				mutex_enter(&p->p_lock);
117 				if (p->p_signhdr != NULL) {
118 					snqp = SIGN_PTR(p, id.sn_index);
119 					if (snqp->sn_snid == sid) {
120 						mutex_exit(&p->p_lock);
121 						mutex_exit(&pidlock);
122 						return (set_errno(EBUSY));
123 					}
124 				}
125 				mutex_exit(&p->p_lock);
126 			}
127 			mutex_exit(&pidlock);
128 		}
129 
130 		if (copyin_siginfo(datamodel, siginfo, &info))
131 			return (set_errno(EFAULT));
132 
133 		/* The si_code value must indicate the signal will be queued */
134 		if (!sigwillqueue(info.si_signo, info.si_code))
135 			return (set_errno(EINVAL));
136 
137 		if (cp->p_signhdr == NULL) {
138 			/* Allocate signotify pool first time */
139 			sqh = sigqhdralloc(sizeof (signotifyq_t),
140 			    _SIGNOTIFY_MAX);
141 			mutex_enter(&cp->p_lock);
142 			if (cp->p_signhdr == NULL) {
143 				/* hang the pool head on proc */
144 				cp->p_signhdr = sqh;
145 			} else {
146 				/* another lwp allocated the pool, free ours */
147 				sigqhdrfree(sqh);
148 			}
149 		} else {
150 			mutex_enter(&cp->p_lock);
151 		}
152 
153 		sqp = sigqalloc(cp->p_signhdr);
154 		if (sqp == NULL) {
155 			mutex_exit(&cp->p_lock);
156 			return (set_errno(EAGAIN));
157 		}
158 		cr = CRED();
159 		sqp->sq_info = info;
160 		sqp->sq_info.si_pid = cp->p_pid;
161 		sqp->sq_info.si_ctid = PRCTID(cp);
162 		sqp->sq_info.si_zoneid = getzoneid();
163 		sqp->sq_info.si_uid = crgetruid(cr);
164 
165 		/* fill the signotifyq_t fields */
166 		((signotifyq_t *)sqp)->sn_snid = sid;
167 
168 		mutex_exit(&cp->p_lock);
169 
170 		/* complete the signotify_id_t fields */
171 		id.sn_index = (signotifyq_t *)sqp - SIGN_PTR(cp, 0);
172 		id.sn_pid = cp->p_pid;
173 
174 		break;
175 
176 	case SN_CANCEL:
177 	case SN_SEND:
178 
179 		mutex_enter(&pidlock);
180 		if ((id.sn_pid <= 0) || ((p = prfind(id.sn_pid)) == NULL)) {
181 			mutex_exit(&pidlock);
182 			return (set_errno(EINVAL));
183 		}
184 		mutex_enter(&p->p_lock);
185 		mutex_exit(&pidlock);
186 
187 		if (p->p_signhdr == NULL) {
188 			mutex_exit(&p->p_lock);
189 			return (set_errno(EINVAL));
190 		}
191 
192 		snqp = SIGN_PTR(p, id.sn_index);
193 
194 		if (snqp->sn_snid == 0) {
195 			mutex_exit(&p->p_lock);
196 			return (set_errno(EINVAL));
197 		}
198 
199 		if (snqp->sn_snid != get_sigid(cp, (caddr_t)sn_id)) {
200 			mutex_exit(&p->p_lock);
201 			return (set_errno(EINVAL));
202 		}
203 
204 		snqp->sn_snid = 0;
205 
206 		/* cmd == SN_CANCEL or signo == 0 (SIGEV_NONE) */
207 		if (((sigqueue_t *)snqp)->sq_info.si_signo <= 0)
208 			cmd = SN_CANCEL;
209 
210 		sigqsend(cmd, p, 0, (sigqueue_t *)snqp);
211 		mutex_exit(&p->p_lock);
212 
213 		id.sn_pid = 0;
214 		id.sn_index = 0;
215 
216 		break;
217 
218 	default :
219 		return (set_errno(EINVAL));
220 	}
221 
222 	if (copyout(&id, sn_id, sizeof (signotify_id_t)))
223 		return (set_errno(EFAULT));
224 
225 	return (0);
226 }
227