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