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