Home | History | Annotate | Line # | Download | only in common
      1 /*	$NetBSD: linux32_stat.c,v 1.18 2021/11/25 02:27:08 ryo Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2006 Emmanuel Dreyfus, all rights reserved.
      5  *
      6  * Redistribution and use in source and binary forms, with or without
      7  * modification, are permitted provided that the following conditions
      8  * are met:
      9  * 1. Redistributions of source code must retain the above copyright
     10  *    notice, this list of conditions and the following disclaimer.
     11  * 2. Redistributions in binary form must reproduce the above copyright
     12  *    notice, this list of conditions and the following disclaimer in the
     13  *    documentation and/or other materials provided with the distribution.
     14  * 3. All advertising materials mentioning features or use of this software
     15  *    must display the following acknowledgement:
     16  *	This product includes software developed by Emmanuel Dreyfus
     17  * 4. The name of the author may not be used to endorse or promote
     18  *    products derived from this software without specific prior written
     19  *    permission.
     20  *
     21  * THIS SOFTWARE IS PROVIDED BY THE THE AUTHOR AND CONTRIBUTORS ``AS IS''
     22  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
     23  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     24  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS
     25  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     26  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     27  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     31  * POSSIBILITY OF SUCH DAMAGE.
     32  */
     33 
     34 #include <sys/cdefs.h>
     35 
     36 __KERNEL_RCSID(0, "$NetBSD: linux32_stat.c,v 1.18 2021/11/25 02:27:08 ryo Exp $");
     37 
     38 #include <sys/types.h>
     39 #include <sys/param.h>
     40 #include <sys/fstypes.h>
     41 #include <sys/signal.h>
     42 #include <sys/dirent.h>
     43 #include <sys/kernel.h>
     44 #include <sys/namei.h>
     45 #include <sys/fcntl.h>
     46 #include <sys/filedesc.h>
     47 #include <sys/select.h>
     48 #include <sys/proc.h>
     49 #include <sys/ucred.h>
     50 #include <sys/swap.h>
     51 #include <sys/vfs_syscalls.h>
     52 
     53 #include <machine/types.h>
     54 
     55 #include <sys/syscallargs.h>
     56 
     57 #include <compat/netbsd32/netbsd32.h>
     58 #include <compat/netbsd32/netbsd32_conv.h>
     59 #include <compat/netbsd32/netbsd32_syscallargs.h>
     60 
     61 #include <compat/linux/common/linux_types.h>
     62 #include <compat/linux/common/linux_signal.h>
     63 #include <compat/linux/common/linux_machdep.h>
     64 #include <compat/linux/common/linux_misc.h>
     65 #include <compat/linux/common/linux_oldolduname.h>
     66 #include <compat/linux/common/linux_ipc.h>
     67 #include <compat/linux/common/linux_sem.h>
     68 #include <compat/linux/common/linux_fcntl.h>
     69 #include <compat/linux/linux_syscallargs.h>
     70 
     71 #include <compat/linux32/common/linux32_types.h>
     72 #include <compat/linux32/common/linux32_signal.h>
     73 #include <compat/linux32/common/linux32_machdep.h>
     74 #include <compat/linux32/common/linux32_sysctl.h>
     75 #include <compat/linux32/common/linux32_socketcall.h>
     76 #include <compat/linux32/linux32_syscall.h>
     77 #include <compat/linux32/linux32_syscallargs.h>
     78 
     79 static inline void bsd_to_linux32_stat(struct stat *, struct linux32_stat *);
     80 static inline void bsd_to_linux32_stat64(struct stat *, struct linux32_stat64 *);
     81 
     82 #define linux_fakedev(x,y) (x)
     83 
     84 static inline void
     85 bsd_to_linux32_stat(struct stat *st, struct linux32_stat *st32)
     86 {
     87 	memset(st32, 0, sizeof(*st32));
     88 	st32->lst_dev = linux_fakedev(st->st_dev, 0);
     89 	st32->lst_ino = st->st_ino;
     90 	st32->lst_mode = st->st_mode;
     91 	if (st->st_nlink >= (1 << 15))
     92 		st32->lst_nlink = (1 << 15) - 1;
     93 	else
     94 		st32->lst_nlink = st->st_nlink;
     95 	st32->lst_uid = st->st_uid;
     96 	st32->lst_gid = st->st_gid;
     97 	st32->lst_rdev = linux_fakedev(st->st_rdev, 0);
     98 	st32->lst_size = st->st_size;
     99 	st32->lst_blksize = st->st_blksize;
    100 	st32->lst_blocks = st->st_blocks;
    101 	st32->lst_atime = st->st_atime;
    102 	st32->lst_mtime = st->st_mtime;
    103 	st32->lst_ctime = st->st_ctime;
    104 #ifdef LINUX32_STAT_HAS_NSEC
    105 	st32->lst_atime_nsec = st->st_atimensec;
    106 	st32->lst_mtime_nsec = st->st_mtimensec;
    107 	st32->lst_ctime_nsec = st->st_ctimensec;
    108 #endif
    109 }
    110 
    111 static inline void
    112 bsd_to_linux32_stat64(struct stat *st, struct linux32_stat64 *st32)
    113 {
    114 	memset(st32, 0, sizeof(*st32));
    115 	st32->lst_dev = linux_fakedev(st->st_dev, 0);
    116 	st32->lst_ino = st->st_ino;
    117 	st32->lst_mode = st->st_mode;
    118 	if (st->st_nlink >= (1 << 15))
    119 		st32->lst_nlink = (1 << 15) - 1;
    120 	else
    121 		st32->lst_nlink = st->st_nlink;
    122 	st32->lst_uid = st->st_uid;
    123 	st32->lst_gid = st->st_gid;
    124 	st32->lst_rdev = linux_fakedev(st->st_rdev, 0);
    125 	st32->lst_size = st->st_size;
    126 	st32->lst_blksize = st->st_blksize;
    127 	st32->lst_blocks = st->st_blocks;
    128 	st32->lst_atime = st->st_atime;
    129 	st32->lst_mtime = st->st_mtime;
    130 	st32->lst_ctime = st->st_ctime;
    131 #ifdef LINUX32_STAT64_HAS_NSEC
    132 	st32->lst_atime_nsec = st->st_atimensec;
    133 	st32->lst_mtime_nsec = st->st_mtimensec;
    134 	st32->lst_ctime_nsec = st->st_ctimensec;
    135 #endif
    136 #ifdef LINUX32_STAT64_HAS_BROKEN_ST_INO
    137 	st32->__lst_ino = st->st_ino;
    138 #endif
    139 }
    140 
    141 int
    142 linux32_sys_stat(struct lwp *l, const struct linux32_sys_stat_args *uap, register_t *retval)
    143 {
    144 	/* {
    145 	        syscallarg(netbsd32_charp) path;
    146 	        syscallarg(linux32_statp) sp;
    147 	} */
    148 	int error;
    149 	struct stat st;
    150 	struct linux32_stat st32;
    151 
    152 	error = do_sys_stat(SCARG_P32(uap, path), FOLLOW, &st);
    153 	if (error != 0)
    154 		return error;
    155 
    156 	bsd_to_linux32_stat(&st, &st32);
    157 	return copyout(&st32, SCARG_P32(uap, sp), sizeof(st32));
    158 }
    159 
    160 int
    161 linux32_sys_lstat(struct lwp *l, const struct linux32_sys_lstat_args *uap, register_t *retval)
    162 {
    163 	/* {
    164 	        syscallarg(netbsd32_charp) path;
    165 	        syscallarg(linux32_statp) sp;
    166 	} */
    167 	int error;
    168 	struct stat st;
    169 	struct linux32_stat st32;
    170 
    171 	error = do_sys_stat(SCARG_P32(uap, path), NOFOLLOW, &st);
    172 	if (error != 0)
    173 		return error;
    174 
    175 	bsd_to_linux32_stat(&st, &st32);
    176 	return copyout(&st32, SCARG_P32(uap, sp), sizeof(st32));
    177 }
    178 
    179 int
    180 linux32_sys_fstat(struct lwp *l, const struct linux32_sys_fstat_args *uap, register_t *retval)
    181 {
    182 	/* {
    183 	        syscallarg(int) fd;
    184 	        syscallarg(linux32_statp) sp;
    185 	} */
    186 	int error;
    187 	struct stat st;
    188 	struct linux32_stat st32;
    189 
    190 	error = do_sys_fstat(SCARG(uap, fd), &st);
    191 	if (error != 0)
    192 		return error;
    193 
    194 	bsd_to_linux32_stat(&st, &st32);
    195 	return copyout(&st32, SCARG_P32(uap, sp), sizeof(st32));
    196 }
    197 
    198 int
    199 linux32_sys_stat64(struct lwp *l, const struct linux32_sys_stat64_args *uap, register_t *retval)
    200 {
    201 	/* {
    202 	        syscallarg(netbsd32_charp) path;
    203 	        syscallarg(linux32_stat64p) sp;
    204 	} */
    205 	int error;
    206 	struct stat st;
    207 	struct linux32_stat64 st32;
    208 
    209 	error = do_sys_stat(SCARG_P32(uap, path), FOLLOW, &st);
    210 	if (error != 0)
    211 		return error;
    212 
    213 	bsd_to_linux32_stat64(&st, &st32);
    214 	return copyout(&st32, SCARG_P32(uap, sp), sizeof(st32));
    215 }
    216 
    217 int
    218 linux32_sys_lstat64(struct lwp *l, const struct linux32_sys_lstat64_args *uap, register_t *retval)
    219 {
    220 	/* {
    221 	        syscallarg(netbsd32_charp) path;
    222 	        syscallarg(linux32_stat64p) sp;
    223 	} */
    224 	int error;
    225 	struct stat st;
    226 	struct linux32_stat64 st32;
    227 
    228 	error = do_sys_stat(SCARG_P32(uap, path), NOFOLLOW, &st);
    229 	if (error != 0)
    230 		return error;
    231 
    232 	bsd_to_linux32_stat64(&st, &st32);
    233 	return copyout(&st32, SCARG_P32(uap, sp), sizeof(st32));
    234 }
    235 
    236 int
    237 linux32_sys_fstat64(struct lwp *l, const struct linux32_sys_fstat64_args *uap, register_t *retval)
    238 {
    239 	/* {
    240 	        syscallarg(int) fd;
    241 	        syscallarg(linux32_stat64p) sp;
    242 	} */
    243 	int error;
    244 	struct stat st;
    245 	struct linux32_stat64 st32;
    246 
    247 	error = do_sys_fstat(SCARG(uap, fd), &st);
    248 	if (error != 0)
    249 		return error;
    250 
    251 	bsd_to_linux32_stat64(&st, &st32);
    252 	return copyout(&st32, SCARG_P32(uap, sp), sizeof(st32));
    253 }
    254 
    255 int
    256 linux32_sys_fstatat64(struct lwp *l,
    257     const struct linux32_sys_fstatat64_args *uap, register_t *retval)
    258 {
    259 	/* {
    260 		syscallarg(int) fd;
    261 		syscallarg(netbsd32_charp) path;
    262 		syscallarg(linux32_stat64p) sp;
    263 		syscallarg(int) flag;
    264 	} */
    265 	struct linux32_stat64 st32;
    266 	struct stat st;
    267 	int error;
    268 
    269 	error = linux_statat(l, SCARG(uap, fd), SCARG_P32(uap, path),
    270 	    SCARG(uap, flag), &st);
    271 	if (error != 0)
    272 		return error;
    273 
    274 	bsd_to_linux32_stat64(&st, &st32);
    275 
    276 	return copyout(&st32, SCARG_P32(uap, sp), sizeof st32);
    277 }
    278 
    279 #ifdef LINUX32_SYS_statx
    280 int
    281 linux32_sys_statx(struct lwp *l, const struct linux32_sys_statx_args *uap,
    282     register_t *retval)
    283 {
    284 	/* {
    285 		syscallarg(int) fd;
    286 		syscallarg(netbsd32_charp) path;
    287 		syscallarg(int) flag;
    288 		syscallarg(unsigned int) mask;
    289 		syscallarg(linux32_statxp) sp;
    290 	} */
    291 	struct linux_statx stx;
    292 	struct stat st;
    293 	int error;
    294 
    295 	error = linux_statat(l, SCARG(uap, fd), SCARG_P32(uap, path),
    296 	    SCARG(uap, flag), &st);
    297 	if (error != 0)
    298 		return error;
    299 
    300 	/* struct statx has binary compatibilities between 32bit and 64bit */
    301 	error = bsd_to_linux_statx(&st, &stx, SCARG(uap, mask));
    302 	if (error != 0)
    303 		return error;
    304 
    305 	return copyout(&stx, SCARG_P32(uap, sp), sizeof stx);
    306 }
    307 #endif /* LINUX32_SYS_statx */
    308