1 1.3 pgoyette /* $NetBSD: rump_cygwin_compat.c,v 1.3 2019/01/27 02:08:49 pgoyette Exp $ */ 2 1.1 pooka 3 1.1 pooka /* 4 1.1 pooka * Copyright (c) 2013 Antti Kantee. All Rights Reserved. 5 1.1 pooka * 6 1.1 pooka * Redistribution and use in source and binary forms, with or without 7 1.1 pooka * modification, are permitted provided that the following conditions 8 1.1 pooka * are met: 9 1.1 pooka * 1. Redistributions of source code must retain the above copyright 10 1.1 pooka * notice, this list of conditions and the following disclaimer. 11 1.1 pooka * 2. Redistributions in binary form must reproduce the above copyright 12 1.1 pooka * notice, this list of conditions and the following disclaimer in the 13 1.1 pooka * documentation and/or other materials provided with the distribution. 14 1.1 pooka * 15 1.1 pooka * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS 16 1.1 pooka * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 17 1.1 pooka * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 18 1.1 pooka * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19 1.1 pooka * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 1.1 pooka * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 21 1.1 pooka * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 1.1 pooka * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23 1.1 pooka * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 1.1 pooka * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 1.1 pooka * SUCH DAMAGE. 26 1.1 pooka */ 27 1.1 pooka 28 1.2 alnsn #include <sys/cdefs.h> 29 1.2 alnsn __KERNEL_RCSID(0, "$NetBSD: rump_cygwin_compat.c,v 1.3 2019/01/27 02:08:49 pgoyette Exp $"); 30 1.2 alnsn 31 1.1 pooka #include <sys/param.h> 32 1.1 pooka #include <sys/dirent.h> 33 1.1 pooka #include <sys/fcntl.h> 34 1.1 pooka #include <sys/file.h> 35 1.1 pooka #include <sys/filedesc.h> 36 1.1 pooka #include <sys/malloc.h> 37 1.1 pooka #include <sys/namei.h> 38 1.1 pooka #include <sys/stat.h> 39 1.1 pooka #include <sys/syscallargs.h> 40 1.1 pooka #include <sys/vnode.h> 41 1.1 pooka #include <sys/vfs_syscalls.h> 42 1.1 pooka 43 1.1 pooka #include <compat/sys/time_types.h> 44 1.1 pooka 45 1.1 pooka #include "rump_cygwin_syscallargs.h" 46 1.1 pooka 47 1.1 pooka struct cygwin_stat { 48 1.1 pooka int st_dev; 49 1.1 pooka int64_t st_ino; 50 1.1 pooka int st_mode; 51 1.1 pooka unsigned short st_nlink; 52 1.1 pooka int st_uid; 53 1.1 pooka int st_gid; 54 1.1 pooka int st_rdev; 55 1.1 pooka off_t st_size; 56 1.1 pooka 57 1.1 pooka struct timespec50 st_atim; 58 1.1 pooka struct timespec50 st_mtim; 59 1.1 pooka struct timespec50 st_ctim; 60 1.1 pooka 61 1.1 pooka long st_blksize; 62 1.1 pooka uint64_t st_blocks; 63 1.1 pooka 64 1.1 pooka struct timespec50 st_btim; 65 1.1 pooka }; 66 1.1 pooka 67 1.1 pooka #define PARCOPY(a) ssb->a = sb->a 68 1.1 pooka static void 69 1.1 pooka bsd_to_cygwin_stat(const struct stat *sb, struct cygwin_stat *ssb) 70 1.1 pooka { 71 1.1 pooka 72 1.1 pooka memset(ssb, 0, sizeof(*ssb)); 73 1.1 pooka PARCOPY(st_dev); 74 1.1 pooka PARCOPY(st_ino); 75 1.1 pooka PARCOPY(st_mode); 76 1.1 pooka PARCOPY(st_nlink); 77 1.1 pooka PARCOPY(st_uid); 78 1.1 pooka PARCOPY(st_gid); 79 1.1 pooka PARCOPY(st_rdev); 80 1.1 pooka PARCOPY(st_size); 81 1.1 pooka PARCOPY(st_blksize); 82 1.1 pooka PARCOPY(st_blocks); 83 1.1 pooka 84 1.1 pooka timespec_to_timespec50(&sb->st_atimespec, &ssb->st_atim); 85 1.1 pooka timespec_to_timespec50(&sb->st_mtimespec, &ssb->st_mtim); 86 1.1 pooka timespec_to_timespec50(&sb->st_ctimespec, &ssb->st_ctim); 87 1.1 pooka timespec_to_timespec50(&sb->st_birthtimespec, &ssb->st_btim); 88 1.1 pooka } 89 1.1 pooka 90 1.1 pooka int 91 1.1 pooka rump_cygwin_sys_stat(struct lwp *l, const struct rump_cygwin_sys_stat_args *uap, 92 1.1 pooka register_t *retval) 93 1.1 pooka { 94 1.1 pooka struct cygwin_stat ssb; 95 1.1 pooka struct stat sb; 96 1.1 pooka int error; 97 1.1 pooka 98 1.1 pooka error = do_sys_stat(SCARG(uap, path), FOLLOW, &sb); 99 1.1 pooka if (error) 100 1.1 pooka return error; 101 1.1 pooka 102 1.1 pooka bsd_to_cygwin_stat(&sb, &ssb); 103 1.1 pooka 104 1.1 pooka return copyout(&ssb, SCARG(uap, sp), sizeof(ssb)); 105 1.1 pooka } 106 1.1 pooka 107 1.1 pooka int 108 1.1 pooka rump_cygwin_sys_fstat(struct lwp *l, const struct rump_cygwin_sys_fstat_args *uap, 109 1.1 pooka register_t *retval) 110 1.1 pooka { 111 1.1 pooka struct cygwin_stat ssb; 112 1.1 pooka struct stat sb; 113 1.1 pooka int error; 114 1.1 pooka 115 1.1 pooka error = do_sys_fstat(SCARG(uap, fd), &sb); 116 1.1 pooka if (error) 117 1.1 pooka return error; 118 1.1 pooka 119 1.1 pooka bsd_to_cygwin_stat(&sb, &ssb); 120 1.1 pooka 121 1.1 pooka return copyout(&ssb, SCARG(uap, sp), sizeof(ssb)); 122 1.1 pooka } 123 1.1 pooka 124 1.1 pooka int 125 1.1 pooka rump_cygwin_sys_lstat(struct lwp *l, const struct rump_cygwin_sys_lstat_args *uap, 126 1.1 pooka register_t *retval) 127 1.1 pooka { 128 1.1 pooka struct cygwin_stat ssb; 129 1.1 pooka struct stat sb; 130 1.1 pooka int error; 131 1.1 pooka 132 1.1 pooka error = do_sys_stat(SCARG(uap, path), NOFOLLOW, &sb); 133 1.1 pooka if (error) 134 1.1 pooka return error; 135 1.1 pooka 136 1.1 pooka bsd_to_cygwin_stat(&sb, &ssb); 137 1.1 pooka 138 1.1 pooka return copyout(&ssb, SCARG(uap, sp), sizeof(ssb)); 139 1.1 pooka } 140 1.1 pooka 141 1.1 pooka int 142 1.1 pooka rump_cygwin_sys_open(struct lwp *l, const struct rump_cygwin_sys_open_args *uap, 143 1.1 pooka register_t *retval) 144 1.1 pooka { 145 1.1 pooka /* { 146 1.1 pooka syscallarg(const char *) path; 147 1.1 pooka syscallarg(int) flags; 148 1.1 pooka syscallarg(int) mode; 149 1.1 pooka } */ 150 1.1 pooka struct sys_open_args ua; 151 1.1 pooka int sflags, flags; 152 1.1 pooka 153 1.1 pooka sflags = SCARG(uap, flags); 154 1.1 pooka flags = sflags & (3 | O_APPEND | O_ASYNC | O_CREAT | O_TRUNC | O_EXCL); 155 1.1 pooka 156 1.1 pooka SCARG(&ua, path) = SCARG(uap, path); 157 1.1 pooka SCARG(&ua, flags) = flags; 158 1.1 pooka SCARG(&ua, mode) = SCARG(uap, mode); 159 1.1 pooka 160 1.1 pooka return sys_open(l, &ua, retval); 161 1.1 pooka } 162 1.1 pooka 163 1.1 pooka #define CYGWIN_NAME_MAX 255 164 1.1 pooka struct cygwin_dirent { 165 1.1 pooka long d_version; 166 1.1 pooka int64_t d_ino; 167 1.1 pooka unsigned char d_type; 168 1.1 pooka unsigned char d_unused[3]; 169 1.1 pooka uint32_t d_internal; 170 1.1 pooka char d_name[CYGWIN_NAME_MAX + 1]; 171 1.1 pooka } __packed; 172 1.1 pooka 173 1.1 pooka #define CYGWIN_NAMEOFF(dp) offsetof(struct cygwin_dirent,d_name) 174 1.1 pooka #define CYGWIN_RECLEN(dp, namlen) ((CYGWIN_NAMEOFF(dp) + (namlen) + 1)) 175 1.1 pooka 176 1.1 pooka int 177 1.1 pooka rump_cygwin_sys_getdents(struct lwp *l, 178 1.1 pooka const struct rump_cygwin_sys_getdents_args *uap, register_t *retval) 179 1.1 pooka { 180 1.1 pooka struct file *fp; 181 1.1 pooka struct dirent *bdp; 182 1.1 pooka struct cygwin_dirent idb; 183 1.1 pooka char *buf, *inp, *outp; 184 1.1 pooka size_t resid, buflen, nbytes; 185 1.1 pooka size_t reclen, cygwin_reclen; 186 1.1 pooka int error, done; 187 1.1 pooka 188 1.1 pooka if ((error = fd_getvnode(SCARG(uap, fd), &fp)) != 0) 189 1.1 pooka return (error); 190 1.1 pooka 191 1.1 pooka /* 192 1.1 pooka * Sneaky, but avoids having "rewind" f_offset due to the 193 1.1 pooka * conversions not fitting from our intermediate kernel buffer 194 1.1 pooka * into the user buffer 195 1.1 pooka */ 196 1.1 pooka nbytes = SCARG(uap, nbytes); 197 1.1 pooka buflen = min(MAXBSIZE, (nbytes*8)/10); 198 1.1 pooka buf = kmem_alloc(buflen, KM_SLEEP); 199 1.1 pooka 200 1.1 pooka if ((fp->f_flag & FREAD) == 0) { 201 1.1 pooka error = EBADF; 202 1.1 pooka goto out; 203 1.1 pooka } 204 1.1 pooka 205 1.1 pooka resid = nbytes; 206 1.1 pooka outp = SCARG(uap, buf); 207 1.1 pooka 208 1.1 pooka again: 209 1.1 pooka if ((error = vn_readdir(fp, buf, UIO_SYSSPACE, buflen, &done, 210 1.1 pooka l, NULL, NULL)) != 0) 211 1.1 pooka goto out; 212 1.1 pooka if (done == 0) 213 1.1 pooka goto eof; 214 1.1 pooka 215 1.1 pooka for (inp = buf; done > 0; done -= reclen) { 216 1.1 pooka bdp = (struct dirent *)inp; 217 1.1 pooka reclen = bdp->d_reclen; 218 1.1 pooka 219 1.1 pooka /* skip empty entries */ 220 1.1 pooka if (bdp->d_fileno == 0) { 221 1.1 pooka inp += reclen; 222 1.1 pooka continue; 223 1.1 pooka } 224 1.1 pooka 225 1.1 pooka cygwin_reclen = CYGWIN_RECLEN(&idb, bdp->d_namlen); 226 1.1 pooka if (resid < cygwin_reclen) { 227 1.1 pooka panic("impossible shortage of resid"); 228 1.1 pooka } 229 1.1 pooka 230 1.1 pooka memset(&idb, 0, sizeof(idb)); 231 1.1 pooka idb.d_ino = bdp->d_fileno; 232 1.1 pooka idb.d_type = bdp->d_type; 233 1.1 pooka strlcpy(idb.d_name, bdp->d_name, sizeof(idb.d_name)); 234 1.1 pooka if ((error = copyout(&idb, outp, cygwin_reclen)) != 0) 235 1.1 pooka goto out; 236 1.1 pooka 237 1.1 pooka inp += reclen; 238 1.1 pooka outp += cygwin_reclen; 239 1.1 pooka resid -= cygwin_reclen; 240 1.1 pooka } 241 1.1 pooka 242 1.1 pooka /* if we squished out the whole block, try again */ 243 1.1 pooka if (outp == SCARG(uap, buf)) { 244 1.1 pooka goto again; 245 1.1 pooka } 246 1.1 pooka 247 1.1 pooka eof: 248 1.1 pooka *retval = nbytes - resid; 249 1.1 pooka out: 250 1.1 pooka kmem_free(buf, buflen); 251 1.1 pooka fd_putfile(SCARG(uap, fd)); 252 1.1 pooka 253 1.1 pooka return (error); 254 1.1 pooka } 255