1 /* $NetBSD: netbsd32_wait.c,v 1.26 2021/12/05 08:13:12 msaitoh 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 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 */ 28 29 #include <sys/cdefs.h> 30 __KERNEL_RCSID(0, "$NetBSD: netbsd32_wait.c,v 1.26 2021/12/05 08:13:12 msaitoh Exp $"); 31 32 #include <sys/param.h> 33 #include <sys/systm.h> 34 #include <sys/mount.h> 35 #include <sys/time.h> 36 #include <sys/wait.h> 37 #include <sys/ptrace.h> 38 #include <sys/resourcevar.h> 39 #include <sys/vnode.h> 40 #include <sys/pool.h> 41 #include <sys/proc.h> 42 #include <sys/dirent.h> 43 44 #include <compat/netbsd32/netbsd32.h> 45 #include <compat/netbsd32/netbsd32_syscallargs.h> 46 #include <compat/netbsd32/netbsd32_conv.h> 47 48 int 49 netbsd32___wait450(struct lwp *l, const struct netbsd32___wait450_args *uap, 50 register_t *retval) 51 { 52 /* { 53 syscallarg(int) pid; 54 syscallarg(netbsd32_intp) status; 55 syscallarg(int) options; 56 syscallarg(netbsd32_rusagep_t) rusage; 57 } */ 58 int error, status, pid = SCARG(uap, pid); 59 struct netbsd32_rusage ru32; 60 struct rusage ru; 61 62 error = do_sys_wait(&pid, &status, SCARG(uap, options), 63 SCARG_P32(uap, rusage) != NULL ? &ru : NULL); 64 65 retval[0] = pid; 66 if (pid == 0) 67 return error; 68 69 if (SCARG_P32(uap, status)) 70 error = copyout(&status, SCARG_P32(uap, status), 71 sizeof(status)); 72 73 if (SCARG_P32(uap, rusage) && error == 0) { 74 netbsd32_from_rusage(&ru, &ru32); 75 error = copyout(&ru32, SCARG_P32(uap, rusage), sizeof(ru32)); 76 } 77 78 return error; 79 } 80 81 int 82 netbsd32_wait6(struct lwp *l, const struct netbsd32_wait6_args *uap, 83 register_t *retval) 84 { 85 /* { 86 syscallarg(idtype_t) idtype; 87 syscallarg(id_t) id; 88 syscallarg(netbsd32_intp) status; 89 syscallarg(int) options; 90 syscallarg(netbsd32_wrusagep_t) wru; 91 syscallarg(netbsd32_siginfop_t) info; 92 } */ 93 idtype_t idtype = SCARG(uap, idtype); 94 id_t id = SCARG(uap, id); 95 struct wrusage wru, *wrup; 96 siginfo_t si, *sip; 97 int status; 98 int pid; 99 100 if (SCARG_P32(uap, wru) != NULL) 101 wrup = &wru; 102 else 103 wrup = NULL; 104 105 if (SCARG_P32(uap, info) != NULL) 106 sip = &si; 107 else 108 sip = NULL; 109 110 /* 111 * We expect all callers of wait6() to know about WEXITED and 112 * WTRAPPED. 113 */ 114 int error = do_sys_waitid(idtype, id, &pid, &status, 115 SCARG(uap, options), wrup, sip); 116 117 retval[0] = pid; /* tell userland who it was */ 118 119 #if 0 120 /* 121 * should we copyout if there was no process, hence no useful data? 122 * We don't for an old style wait4() (etc) but I believe 123 * FreeBSD does for wait6(), so a tossup... Go with FreeBSD for now. 124 */ 125 if (pid == 0) 126 return error; 127 #endif 128 129 130 if (error == 0 && SCARG_P32(uap, status)) 131 error = copyout(&status, SCARG_P32(uap, status), 132 sizeof(status)); 133 if (wrup != NULL && error == 0) { 134 struct netbsd32_wrusage wru32; 135 136 memset(&wru32, 0, sizeof(wru32)); 137 netbsd32_from_rusage(&wrup->wru_self, &wru32.wru_self); 138 netbsd32_from_rusage(&wrup->wru_children, &wru32.wru_children); 139 error = copyout(&wru32, SCARG_P32(uap, wru), sizeof(wru32)); 140 } 141 if (sip != NULL && error == 0) { 142 siginfo32_t si32; 143 144 netbsd32_si_to_si32(&si32, sip); 145 error = copyout(&si32, SCARG_P32(uap, info), sizeof(si32)); 146 } 147 148 return error; 149 } 150 151 int 152 netbsd32___getrusage50(struct lwp *l, 153 const struct netbsd32___getrusage50_args *uap, register_t *retval) 154 { 155 /* { 156 syscallarg(int) who; 157 syscallarg(netbsd32_rusagep_t) rusage; 158 } */ 159 int error; 160 struct proc *p = l->l_proc; 161 struct rusage ru; 162 struct netbsd32_rusage ru32; 163 164 error = getrusage1(p, SCARG(uap, who), &ru); 165 if (error != 0) 166 return error; 167 168 netbsd32_from_rusage(&ru, &ru32); 169 return copyout(&ru32, SCARG_P32(uap, rusage), sizeof(ru32)); 170 } 171