netbsd32_wait.c revision 1.1 1 1.1 mrg /* $NetBSD: netbsd32_wait.c,v 1.1 2001/02/08 13:19:34 mrg 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.1 mrg
31 1.1 mrg #include <sys/param.h>
32 1.1 mrg #include <sys/systm.h>
33 1.1 mrg #include <sys/mount.h>
34 1.1 mrg #include <sys/time.h>
35 1.1 mrg #include <sys/wait.h>
36 1.1 mrg #include <sys/ptrace.h>
37 1.1 mrg #include <sys/resourcevar.h>
38 1.1 mrg #include <sys/vnode.h>
39 1.1 mrg #include <sys/pool.h>
40 1.1 mrg #include <sys/proc.h>
41 1.1 mrg
42 1.1 mrg #include <compat/netbsd32/netbsd32.h>
43 1.1 mrg #include <compat/netbsd32/netbsd32_syscallargs.h>
44 1.1 mrg #include <compat/netbsd32/netbsd32_conv.h>
45 1.1 mrg
46 1.1 mrg int
47 1.1 mrg netbsd32_wait4(q, v, retval)
48 1.1 mrg struct proc *q;
49 1.1 mrg void *v;
50 1.1 mrg register_t *retval;
51 1.1 mrg {
52 1.1 mrg struct netbsd32_wait4_args /* {
53 1.1 mrg syscallarg(int) pid;
54 1.1 mrg syscallarg(netbsd32_intp) status;
55 1.1 mrg syscallarg(int) options;
56 1.1 mrg syscallarg(netbsd32_rusagep_t) rusage;
57 1.1 mrg } */ *uap = v;
58 1.1 mrg struct netbsd32_rusage ru32;
59 1.1 mrg int nfound;
60 1.1 mrg struct proc *p, *t;
61 1.1 mrg int status, error;
62 1.1 mrg
63 1.1 mrg if (SCARG(uap, pid) == 0)
64 1.1 mrg SCARG(uap, pid) = -q->p_pgid;
65 1.1 mrg if (SCARG(uap, options) &~ (WUNTRACED|WNOHANG))
66 1.1 mrg return (EINVAL);
67 1.1 mrg
68 1.1 mrg loop:
69 1.1 mrg nfound = 0;
70 1.1 mrg for (p = q->p_children.lh_first; p != 0; p = p->p_sibling.le_next) {
71 1.1 mrg if (SCARG(uap, pid) != WAIT_ANY &&
72 1.1 mrg p->p_pid != SCARG(uap, pid) &&
73 1.1 mrg p->p_pgid != -SCARG(uap, pid))
74 1.1 mrg continue;
75 1.1 mrg nfound++;
76 1.1 mrg if (p->p_stat == SZOMB) {
77 1.1 mrg retval[0] = p->p_pid;
78 1.1 mrg
79 1.1 mrg if (SCARG(uap, status)) {
80 1.1 mrg status = p->p_xstat; /* convert to int */
81 1.1 mrg error = copyout((caddr_t)&status,
82 1.1 mrg (caddr_t)(u_long)SCARG(uap, status),
83 1.1 mrg sizeof(status));
84 1.1 mrg if (error)
85 1.1 mrg return (error);
86 1.1 mrg }
87 1.1 mrg if (SCARG(uap, rusage)) {
88 1.1 mrg netbsd32_from_rusage(p->p_ru, &ru32);
89 1.1 mrg if ((error = copyout((caddr_t)&ru32,
90 1.1 mrg (caddr_t)(u_long)SCARG(uap, rusage),
91 1.1 mrg sizeof(struct netbsd32_rusage))))
92 1.1 mrg return (error);
93 1.1 mrg }
94 1.1 mrg /*
95 1.1 mrg * If we got the child via ptrace(2) or procfs, and
96 1.1 mrg * the parent is different (meaning the process was
97 1.1 mrg * attached, rather than run as a child), then we need
98 1.1 mrg * to give it back to the old parent, and send the
99 1.1 mrg * parent a SIGCHLD. The rest of the cleanup will be
100 1.1 mrg * done when the old parent waits on the child.
101 1.1 mrg */
102 1.1 mrg if ((p->p_flag & P_TRACED) &&
103 1.1 mrg p->p_oppid != p->p_pptr->p_pid) {
104 1.1 mrg t = pfind(p->p_oppid);
105 1.1 mrg proc_reparent(p, t ? t : initproc);
106 1.1 mrg p->p_oppid = 0;
107 1.1 mrg p->p_flag &= ~(P_TRACED|P_WAITED|P_FSTRACE);
108 1.1 mrg psignal(p->p_pptr, SIGCHLD);
109 1.1 mrg wakeup((caddr_t)p->p_pptr);
110 1.1 mrg return (0);
111 1.1 mrg }
112 1.1 mrg p->p_xstat = 0;
113 1.1 mrg ruadd(&q->p_stats->p_cru, p->p_ru);
114 1.1 mrg pool_put(&rusage_pool, p->p_ru);
115 1.1 mrg
116 1.1 mrg /*
117 1.1 mrg * Finally finished with old proc entry.
118 1.1 mrg * Unlink it from its process group and free it.
119 1.1 mrg */
120 1.1 mrg leavepgrp(p);
121 1.1 mrg
122 1.1 mrg LIST_REMOVE(p, p_list); /* off zombproc */
123 1.1 mrg
124 1.1 mrg LIST_REMOVE(p, p_sibling);
125 1.1 mrg
126 1.1 mrg /*
127 1.1 mrg * Decrement the count of procs running with this uid.
128 1.1 mrg */
129 1.1 mrg (void)chgproccnt(p->p_cred->p_ruid, -1);
130 1.1 mrg
131 1.1 mrg /*
132 1.1 mrg * Free up credentials.
133 1.1 mrg */
134 1.1 mrg if (--p->p_cred->p_refcnt == 0) {
135 1.1 mrg crfree(p->p_cred->pc_ucred);
136 1.1 mrg pool_put(&pcred_pool, p->p_cred);
137 1.1 mrg }
138 1.1 mrg
139 1.1 mrg /*
140 1.1 mrg * Release reference to text vnode
141 1.1 mrg */
142 1.1 mrg if (p->p_textvp)
143 1.1 mrg vrele(p->p_textvp);
144 1.1 mrg
145 1.1 mrg pool_put(&proc_pool, p);
146 1.1 mrg nprocs--;
147 1.1 mrg return (0);
148 1.1 mrg }
149 1.1 mrg if (p->p_stat == SSTOP && (p->p_flag & P_WAITED) == 0 &&
150 1.1 mrg (p->p_flag & P_TRACED || SCARG(uap, options) & WUNTRACED)) {
151 1.1 mrg p->p_flag |= P_WAITED;
152 1.1 mrg retval[0] = p->p_pid;
153 1.1 mrg
154 1.1 mrg if (SCARG(uap, status)) {
155 1.1 mrg status = W_STOPCODE(p->p_xstat);
156 1.1 mrg error = copyout((caddr_t)&status,
157 1.1 mrg (caddr_t)(u_long)SCARG(uap, status),
158 1.1 mrg sizeof(status));
159 1.1 mrg } else
160 1.1 mrg error = 0;
161 1.1 mrg return (error);
162 1.1 mrg }
163 1.1 mrg }
164 1.1 mrg if (nfound == 0)
165 1.1 mrg return (ECHILD);
166 1.1 mrg if (SCARG(uap, options) & WNOHANG) {
167 1.1 mrg retval[0] = 0;
168 1.1 mrg return (0);
169 1.1 mrg }
170 1.1 mrg if ((error = tsleep((caddr_t)q, PWAIT | PCATCH, "wait", 0)) != 0)
171 1.1 mrg return (error);
172 1.1 mrg goto loop;
173 1.1 mrg }
174 1.1 mrg
175 1.1 mrg int
176 1.1 mrg netbsd32_getrusage(p, v, retval)
177 1.1 mrg struct proc *p;
178 1.1 mrg void *v;
179 1.1 mrg register_t *retval;
180 1.1 mrg {
181 1.1 mrg struct netbsd32_getrusage_args /* {
182 1.1 mrg syscallarg(int) who;
183 1.1 mrg syscallarg(netbsd32_rusagep_t) rusage;
184 1.1 mrg } */ *uap = v;
185 1.1 mrg struct rusage *rup;
186 1.1 mrg struct netbsd32_rusage ru;
187 1.1 mrg
188 1.1 mrg switch (SCARG(uap, who)) {
189 1.1 mrg
190 1.1 mrg case RUSAGE_SELF:
191 1.1 mrg rup = &p->p_stats->p_ru;
192 1.1 mrg calcru(p, &rup->ru_utime, &rup->ru_stime, NULL);
193 1.1 mrg break;
194 1.1 mrg
195 1.1 mrg case RUSAGE_CHILDREN:
196 1.1 mrg rup = &p->p_stats->p_cru;
197 1.1 mrg break;
198 1.1 mrg
199 1.1 mrg default:
200 1.1 mrg return (EINVAL);
201 1.1 mrg }
202 1.1 mrg netbsd32_from_rusage(rup, &ru);
203 1.1 mrg return (copyout(&ru, (caddr_t)(u_long)SCARG(uap, rusage), sizeof(ru)));
204 1.1 mrg }
205