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 2008 Sun Microsystems, Inc. All rights reserved. 24 * Use is subject to license terms. 25 */ 26 27 #include <sys/param.h> 28 #include <sys/types.h> 29 #include <sys/sysmacros.h> 30 #include <sys/systm.h> 31 #include <sys/errno.h> 32 #include <sys/proc.h> 33 #include <sys/procset.h> 34 #include <sys/fault.h> 35 #include <sys/signal.h> 36 #include <sys/siginfo.h> 37 #include <sys/schedctl.h> 38 #include <vm/as.h> 39 #include <sys/debug.h> 40 #include <sys/contract/process_impl.h> 41 42 /*ARGSUSED*/ 43 static int 44 copyin_siginfo(model_t datamodel, void *uaddr, k_siginfo_t *ksip) 45 { 46 #ifdef _SYSCALL32_IMPL 47 int ret; 48 49 if (datamodel == DATAMODEL_NATIVE) { 50 #endif 51 return (copyin(uaddr, ksip, sizeof (k_siginfo_t))); 52 #ifdef _SYSCALL32_IMPL 53 } else { 54 siginfo32_t si32; 55 56 if (ret = copyin(uaddr, &si32, sizeof (si32))) 57 return (ret); 58 59 siginfo_32tok(&si32, ksip); 60 } 61 62 return (0); 63 #endif 64 } 65 66 /* 67 * To find secured 64 bit id for signotify() call 68 * This depends upon as_getmemid() which returns 69 * unique vnode/offset for a user virtual address. 70 */ 71 static u_longlong_t 72 get_sigid(proc_t *p, caddr_t addr) 73 { 74 u_longlong_t snid = 0; 75 memid_t memid; 76 quad_t *tquad = (quad_t *)&snid; 77 78 if (!as_getmemid(p->p_as, addr, &memid)) { 79 tquad->val[0] = (int)memid.val[0]; 80 tquad->val[1] = (int)memid.val[1]; 81 } 82 return (snid); 83 } 84 85 #define SIGN_PTR(p, n) &((signotifyq_t *)(&p->p_signhdr[1]))[n]; 86 87 int 88 signotify(int cmd, siginfo_t *siginfo, signotify_id_t *sn_id) 89 { 90 k_siginfo_t info; 91 signotify_id_t id; 92 proc_t *p; 93 proc_t *cp = curproc; 94 signotifyq_t *snqp; 95 struct cred *cr; 96 sigqueue_t *sqp; 97 sigqhdr_t *sqh; 98 u_longlong_t sid; 99 model_t datamodel = get_udatamodel(); 100 101 if (copyin(sn_id, &id, sizeof (signotify_id_t))) 102 return (set_errno(EFAULT)); 103 104 if (id.sn_index >= _SIGNOTIFY_MAX || id.sn_index < 0) 105 return (set_errno(EINVAL)); 106 107 switch (cmd) { 108 case SN_PROC: 109 /* get snid for the given user address of signotifyid_t */ 110 sid = get_sigid(cp, (caddr_t)sn_id); 111 112 if (id.sn_pid > 0) { 113 mutex_enter(&pidlock); 114 if ((p = prfind(id.sn_pid)) != NULL) { 115 mutex_enter(&p->p_lock); 116 if (p->p_signhdr != NULL) { 117 snqp = SIGN_PTR(p, id.sn_index); 118 if (snqp->sn_snid == sid) { 119 mutex_exit(&p->p_lock); 120 mutex_exit(&pidlock); 121 return (set_errno(EBUSY)); 122 } 123 } 124 mutex_exit(&p->p_lock); 125 } 126 mutex_exit(&pidlock); 127 } 128 129 if (copyin_siginfo(datamodel, siginfo, &info)) 130 return (set_errno(EFAULT)); 131 132 /* The si_code value must indicate the signal will be queued */ 133 if (!sigwillqueue(info.si_signo, info.si_code)) 134 return (set_errno(EINVAL)); 135 136 if (cp->p_signhdr == NULL) { 137 /* Allocate signotify pool first time */ 138 sqh = sigqhdralloc(sizeof (signotifyq_t), 139 _SIGNOTIFY_MAX); 140 mutex_enter(&cp->p_lock); 141 if (cp->p_signhdr == NULL) { 142 /* hang the pool head on proc */ 143 cp->p_signhdr = sqh; 144 } else { 145 /* another lwp allocated the pool, free ours */ 146 sigqhdrfree(sqh); 147 } 148 } else { 149 mutex_enter(&cp->p_lock); 150 } 151 152 sqp = sigqalloc(cp->p_signhdr); 153 if (sqp == NULL) { 154 mutex_exit(&cp->p_lock); 155 return (set_errno(EAGAIN)); 156 } 157 cr = CRED(); 158 sqp->sq_info = info; 159 sqp->sq_info.si_pid = cp->p_pid; 160 sqp->sq_info.si_ctid = PRCTID(cp); 161 sqp->sq_info.si_zoneid = getzoneid(); 162 sqp->sq_info.si_uid = crgetruid(cr); 163 164 /* fill the signotifyq_t fields */ 165 ((signotifyq_t *)sqp)->sn_snid = sid; 166 167 mutex_exit(&cp->p_lock); 168 169 /* complete the signotify_id_t fields */ 170 id.sn_index = (signotifyq_t *)sqp - SIGN_PTR(cp, 0); 171 id.sn_pid = cp->p_pid; 172 173 break; 174 175 case SN_CANCEL: 176 case SN_SEND: 177 178 sid = get_sigid(cp, (caddr_t)sn_id); 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 != sid) { 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 228 int 229 sigresend(int sig, siginfo_t *siginfo, sigset_t *mask) 230 { 231 kthread_t *t = curthread; 232 proc_t *p = ttoproc(t); 233 klwp_t *lwp = ttolwp(t); 234 sigqueue_t *sqp = kmem_zalloc(sizeof (*sqp), KM_SLEEP); 235 sigset_t set; 236 k_sigset_t kset; 237 int error; 238 239 if (sig <= 0 || sig >= NSIG || sigismember(&cantmask, sig)) { 240 error = EINVAL; 241 goto bad; 242 } 243 244 if (siginfo == NULL) { 245 sqp->sq_info.si_signo = sig; 246 sqp->sq_info.si_code = SI_NOINFO; 247 } else { 248 if (copyin_siginfo(get_udatamodel(), siginfo, &sqp->sq_info)) { 249 error = EFAULT; 250 goto bad; 251 } 252 if (sqp->sq_info.si_signo != sig) { 253 error = EINVAL; 254 goto bad; 255 } 256 } 257 258 if (copyin(mask, &set, sizeof (set))) { 259 error = EFAULT; 260 goto bad; 261 } 262 sigutok(&set, &kset); 263 264 mutex_enter(&p->p_lock); 265 if (lwp->lwp_cursig || lwp->lwp_curinfo) { 266 mutex_exit(&p->p_lock); 267 t->t_sig_check = 1; 268 error = EAGAIN; 269 goto bad; 270 } 271 lwp->lwp_cursig = sig; 272 lwp->lwp_curinfo = sqp; 273 schedctl_finish_sigblock(t); 274 t->t_hold = kset; 275 mutex_exit(&p->p_lock); 276 277 t->t_sig_check = 1; 278 return (0); 279 bad: 280 kmem_free(sqp, sizeof (*sqp)); 281 return (set_errno(error)); 282 } 283