netbsd32_wait.c revision 1.2.10.1 1 /* $NetBSD: netbsd32_wait.c,v 1.2.10.1 2002/07/29 15:39:18 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.10.1 2002/07/29 15:39:18 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) && p->p_opptr != p->p_pptr){
106 t = p->p_opptr;
107 proc_reparent(p, t ? t : initproc);
108 p->p_opptr = NULL;
109 p->p_flag &= ~(P_TRACED|P_WAITED|P_FSTRACE);
110 psignal(p->p_pptr, SIGCHLD);
111 wakeup((caddr_t)p->p_pptr);
112 return (0);
113 }
114 p->p_xstat = 0;
115 ruadd(&q->p_stats->p_cru, p->p_ru);
116 pool_put(&rusage_pool, p->p_ru);
117
118 /*
119 * Finally finished with old proc entry.
120 * Unlink it from its process group and free it.
121 */
122 leavepgrp(p);
123
124 LIST_REMOVE(p, p_list); /* off zombproc */
125
126 LIST_REMOVE(p, p_sibling);
127
128 /*
129 * Decrement the count of procs running with this uid.
130 */
131 (void)chgproccnt(p->p_cred->p_ruid, -1);
132
133 /*
134 * Free up credentials.
135 */
136 if (--p->p_cred->p_refcnt == 0) {
137 crfree(p->p_cred->pc_ucred);
138 pool_put(&pcred_pool, p->p_cred);
139 }
140
141 /*
142 * Release reference to text vnode
143 */
144 if (p->p_textvp)
145 vrele(p->p_textvp);
146
147 pool_put(&proc_pool, p);
148 nprocs--;
149 return (0);
150 }
151 if (p->p_stat == SSTOP && (p->p_flag & P_WAITED) == 0 &&
152 (p->p_flag & P_TRACED || SCARG(uap, options) & WUNTRACED)) {
153 p->p_flag |= P_WAITED;
154 retval[0] = p->p_pid;
155
156 if (SCARG(uap, status)) {
157 status = W_STOPCODE(p->p_xstat);
158 error = copyout((caddr_t)&status,
159 (caddr_t)(u_long)SCARG(uap, status),
160 sizeof(status));
161 } else
162 error = 0;
163 return (error);
164 }
165 }
166 if (nfound == 0)
167 return (ECHILD);
168 if (SCARG(uap, options) & WNOHANG) {
169 retval[0] = 0;
170 return (0);
171 }
172 if ((error = tsleep((caddr_t)q, PWAIT | PCATCH, "wait", 0)) != 0)
173 return (error);
174 goto loop;
175 }
176
177 int
178 netbsd32_getrusage(p, v, retval)
179 struct proc *p;
180 void *v;
181 register_t *retval;
182 {
183 struct netbsd32_getrusage_args /* {
184 syscallarg(int) who;
185 syscallarg(netbsd32_rusagep_t) rusage;
186 } */ *uap = v;
187 struct rusage *rup;
188 struct netbsd32_rusage ru;
189
190 switch (SCARG(uap, who)) {
191
192 case RUSAGE_SELF:
193 rup = &p->p_stats->p_ru;
194 calcru(p, &rup->ru_utime, &rup->ru_stime, NULL);
195 break;
196
197 case RUSAGE_CHILDREN:
198 rup = &p->p_stats->p_cru;
199 break;
200
201 default:
202 return (EINVAL);
203 }
204 netbsd32_from_rusage(rup, &ru);
205 return (copyout(&ru, (caddr_t)(u_long)SCARG(uap, rusage), sizeof(ru)));
206 }
207