1 /* $NetBSD: netbsd32_wait.c,v 1.2 2001/11/13 02:09:10 lukem Exp $ */ 2 3 /* 4 * Copyright (c) 1998, 2001 Matthew R. Green 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. The name of the author may not be used to endorse or promote products 16 * derived from this software without specific prior written permission. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 23 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 24 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 25 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 26 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28 * SUCH DAMAGE. 29 */ 30 31 #include <sys/cdefs.h> 32 __KERNEL_RCSID(0, "$NetBSD: netbsd32_wait.c,v 1.2 2001/11/13 02:09:10 lukem Exp $"); 33 34 #include <sys/param.h> 35 #include <sys/systm.h> 36 #include <sys/mount.h> 37 #include <sys/time.h> 38 #include <sys/wait.h> 39 #include <sys/ptrace.h> 40 #include <sys/resourcevar.h> 41 #include <sys/vnode.h> 42 #include <sys/pool.h> 43 #include <sys/proc.h> 44 45 #include <compat/netbsd32/netbsd32.h> 46 #include <compat/netbsd32/netbsd32_syscallargs.h> 47 #include <compat/netbsd32/netbsd32_conv.h> 48 49 int 50 netbsd32_wait4(q, v, retval) 51 struct proc *q; 52 void *v; 53 register_t *retval; 54 { 55 struct netbsd32_wait4_args /* { 56 syscallarg(int) pid; 57 syscallarg(netbsd32_intp) status; 58 syscallarg(int) options; 59 syscallarg(netbsd32_rusagep_t) rusage; 60 } */ *uap = v; 61 struct netbsd32_rusage ru32; 62 int nfound; 63 struct proc *p, *t; 64 int status, error; 65 66 if (SCARG(uap, pid) == 0) 67 SCARG(uap, pid) = -q->p_pgid; 68 if (SCARG(uap, options) &~ (WUNTRACED|WNOHANG)) 69 return (EINVAL); 70 71 loop: 72 nfound = 0; 73 for (p = q->p_children.lh_first; p != 0; p = p->p_sibling.le_next) { 74 if (SCARG(uap, pid) != WAIT_ANY && 75 p->p_pid != SCARG(uap, pid) && 76 p->p_pgid != -SCARG(uap, pid)) 77 continue; 78 nfound++; 79 if (p->p_stat == SZOMB) { 80 retval[0] = p->p_pid; 81 82 if (SCARG(uap, status)) { 83 status = p->p_xstat; /* convert to int */ 84 error = copyout((caddr_t)&status, 85 (caddr_t)(u_long)SCARG(uap, status), 86 sizeof(status)); 87 if (error) 88 return (error); 89 } 90 if (SCARG(uap, rusage)) { 91 netbsd32_from_rusage(p->p_ru, &ru32); 92 if ((error = copyout((caddr_t)&ru32, 93 (caddr_t)(u_long)SCARG(uap, rusage), 94 sizeof(struct netbsd32_rusage)))) 95 return (error); 96 } 97 /* 98 * If we got the child via ptrace(2) or procfs, and 99 * the parent is different (meaning the process was 100 * attached, rather than run as a child), then we need 101 * to give it back to the old parent, and send the 102 * parent a SIGCHLD. The rest of the cleanup will be 103 * done when the old parent waits on the child. 104 */ 105 if ((p->p_flag & P_TRACED) && 106 p->p_oppid != p->p_pptr->p_pid) { 107 t = pfind(p->p_oppid); 108 proc_reparent(p, t ? t : initproc); 109 p->p_oppid = 0; 110 p->p_flag &= ~(P_TRACED|P_WAITED|P_FSTRACE); 111 psignal(p->p_pptr, SIGCHLD); 112 wakeup((caddr_t)p->p_pptr); 113 return (0); 114 } 115 p->p_xstat = 0; 116 ruadd(&q->p_stats->p_cru, p->p_ru); 117 pool_put(&rusage_pool, p->p_ru); 118 119 /* 120 * Finally finished with old proc entry. 121 * Unlink it from its process group and free it. 122 */ 123 leavepgrp(p); 124 125 LIST_REMOVE(p, p_list); /* off zombproc */ 126 127 LIST_REMOVE(p, p_sibling); 128 129 /* 130 * Decrement the count of procs running with this uid. 131 */ 132 (void)chgproccnt(p->p_cred->p_ruid, -1); 133 134 /* 135 * Free up credentials. 136 */ 137 if (--p->p_cred->p_refcnt == 0) { 138 crfree(p->p_cred->pc_ucred); 139 pool_put(&pcred_pool, p->p_cred); 140 } 141 142 /* 143 * Release reference to text vnode 144 */ 145 if (p->p_textvp) 146 vrele(p->p_textvp); 147 148 pool_put(&proc_pool, p); 149 nprocs--; 150 return (0); 151 } 152 if (p->p_stat == SSTOP && (p->p_flag & P_WAITED) == 0 && 153 (p->p_flag & P_TRACED || SCARG(uap, options) & WUNTRACED)) { 154 p->p_flag |= P_WAITED; 155 retval[0] = p->p_pid; 156 157 if (SCARG(uap, status)) { 158 status = W_STOPCODE(p->p_xstat); 159 error = copyout((caddr_t)&status, 160 (caddr_t)(u_long)SCARG(uap, status), 161 sizeof(status)); 162 } else 163 error = 0; 164 return (error); 165 } 166 } 167 if (nfound == 0) 168 return (ECHILD); 169 if (SCARG(uap, options) & WNOHANG) { 170 retval[0] = 0; 171 return (0); 172 } 173 if ((error = tsleep((caddr_t)q, PWAIT | PCATCH, "wait", 0)) != 0) 174 return (error); 175 goto loop; 176 } 177 178 int 179 netbsd32_getrusage(p, v, retval) 180 struct proc *p; 181 void *v; 182 register_t *retval; 183 { 184 struct netbsd32_getrusage_args /* { 185 syscallarg(int) who; 186 syscallarg(netbsd32_rusagep_t) rusage; 187 } */ *uap = v; 188 struct rusage *rup; 189 struct netbsd32_rusage ru; 190 191 switch (SCARG(uap, who)) { 192 193 case RUSAGE_SELF: 194 rup = &p->p_stats->p_ru; 195 calcru(p, &rup->ru_utime, &rup->ru_stime, NULL); 196 break; 197 198 case RUSAGE_CHILDREN: 199 rup = &p->p_stats->p_cru; 200 break; 201 202 default: 203 return (EINVAL); 204 } 205 netbsd32_from_rusage(rup, &ru); 206 return (copyout(&ru, (caddr_t)(u_long)SCARG(uap, rusage), sizeof(ru))); 207 } 208