netbsd32_wait.c revision 1.5 1 /* $NetBSD: netbsd32_wait.c,v 1.5 2003/01/18 08:28:26 thorpej 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.5 2003/01/18 08:28:26 thorpej 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(l, v, retval)
51 struct lwp *l;
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 proc *q = l->l_proc;
62 struct netbsd32_rusage ru32;
63 int nfound;
64 struct proc *p, *t;
65 int status, error;
66
67 if (SCARG(uap, pid) == 0)
68 SCARG(uap, pid) = -q->p_pgid;
69 if (SCARG(uap, options) &~ (WUNTRACED|WNOHANG))
70 return (EINVAL);
71
72 loop:
73 nfound = 0;
74 for (p = q->p_children.lh_first; p != 0; p = p->p_sibling.le_next) {
75 if (SCARG(uap, pid) != WAIT_ANY &&
76 p->p_pid != SCARG(uap, pid) &&
77 p->p_pgid != -SCARG(uap, pid))
78 continue;
79 nfound++;
80 if (p->p_stat == SZOMB) {
81 retval[0] = p->p_pid;
82
83 if (SCARG(uap, status)) {
84 status = p->p_xstat; /* convert to int */
85 error = copyout((caddr_t)&status,
86 (caddr_t)NETBSD32PTR64(SCARG(uap, status)),
87 sizeof(status));
88 if (error)
89 return (error);
90 }
91 if (SCARG(uap, rusage)) {
92 netbsd32_from_rusage(p->p_ru, &ru32);
93 if ((error = copyout((caddr_t)&ru32,
94 (caddr_t)NETBSD32PTR64(SCARG(uap, rusage)),
95 sizeof(struct netbsd32_rusage))))
96 return (error);
97 }
98 /*
99 * If we got the child via ptrace(2) or procfs, and
100 * the parent is different (meaning the process was
101 * attached, rather than run as a child), then we need
102 * to give it back to the old parent, and send the
103 * parent a SIGCHLD. The rest of the cleanup will be
104 * done when the old parent waits on the child.
105 */
106 if ((p->p_flag & P_TRACED) && p->p_opptr != p->p_pptr){
107 t = p->p_opptr;
108 proc_reparent(p, t ? t : initproc);
109 p->p_opptr = NULL;
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)NETBSD32PTR64(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(l, v, retval)
180 struct lwp *l;
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 proc *p = l->l_proc;
189 struct rusage *rup;
190 struct netbsd32_rusage ru;
191
192 switch (SCARG(uap, who)) {
193
194 case RUSAGE_SELF:
195 rup = &p->p_stats->p_ru;
196 calcru(p, &rup->ru_utime, &rup->ru_stime, NULL);
197 break;
198
199 case RUSAGE_CHILDREN:
200 rup = &p->p_stats->p_cru;
201 break;
202
203 default:
204 return (EINVAL);
205 }
206 netbsd32_from_rusage(rup, &ru);
207 return (copyout(&ru, (caddr_t)NETBSD32PTR64(SCARG(uap, rusage)),
208 sizeof(ru)));
209 }
210