Home | History | Annotate | Line # | Download | only in common
linux_misc_notalpha.c revision 1.111
      1  1.111   thorpej /*	$NetBSD: linux_misc_notalpha.c,v 1.111 2020/12/04 00:26:16 thorpej Exp $	*/
      2   1.47       erh 
      3   1.47       erh /*-
      4  1.111   thorpej  * Copyright (c) 1995, 1998, 2008, 2020 The NetBSD Foundation, Inc.
      5   1.47       erh  * All rights reserved.
      6   1.47       erh  *
      7   1.47       erh  * This code is derived from software contributed to The NetBSD Foundation
      8   1.51   thorpej  * by Frank van der Linden and Eric Haszlakiewicz; by Jason R. Thorpe
      9   1.51   thorpej  * of the Numerical Aerospace Simulation Facility, NASA Ames Research Center.
     10   1.47       erh  *
     11   1.47       erh  * Redistribution and use in source and binary forms, with or without
     12   1.47       erh  * modification, are permitted provided that the following conditions
     13   1.47       erh  * are met:
     14   1.47       erh  * 1. Redistributions of source code must retain the above copyright
     15   1.47       erh  *    notice, this list of conditions and the following disclaimer.
     16   1.47       erh  * 2. Redistributions in binary form must reproduce the above copyright
     17   1.47       erh  *    notice, this list of conditions and the following disclaimer in the
     18   1.47       erh  *    documentation and/or other materials provided with the distribution.
     19   1.47       erh  *
     20   1.47       erh  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     21   1.47       erh  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     22   1.47       erh  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     23   1.47       erh  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     24   1.47       erh  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     25   1.47       erh  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     26   1.47       erh  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     27   1.47       erh  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     28   1.47       erh  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     29   1.47       erh  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     30   1.47       erh  * POSSIBILITY OF SUCH DAMAGE.
     31    1.1      fvdl  */
     32   1.61     lukem 
     33   1.61     lukem #include <sys/cdefs.h>
     34  1.111   thorpej __KERNEL_RCSID(0, "$NetBSD: linux_misc_notalpha.c,v 1.111 2020/12/04 00:26:16 thorpej Exp $");
     35  1.108       chs 
     36  1.108       chs /*
     37  1.108       chs  * Note that we must NOT include "opt_compat_linux32.h" here,
     38  1.108       chs  * the maze of ifdefs below relies on COMPAT_LINUX32 only being
     39  1.108       chs  * defined when this file is built for linux32.
     40  1.108       chs  */
     41    1.1      fvdl 
     42    1.1      fvdl #include <sys/param.h>
     43    1.1      fvdl #include <sys/systm.h>
     44    1.1      fvdl #include <sys/kernel.h>
     45   1.47       erh #include <sys/mman.h>
     46   1.47       erh #include <sys/mount.h>
     47    1.1      fvdl #include <sys/mbuf.h>
     48   1.47       erh #include <sys/namei.h>
     49   1.57     itohy #include <sys/proc.h>
     50   1.93       dsl #include <sys/prot.h>
     51    1.1      fvdl #include <sys/ptrace.h>
     52    1.1      fvdl #include <sys/resource.h>
     53    1.1      fvdl #include <sys/resourcevar.h>
     54   1.64   thorpej #include <sys/time.h>
     55   1.91       dsl #include <sys/vfs_syscalls.h>
     56    1.1      fvdl #include <sys/wait.h>
     57   1.79      elad #include <sys/kauth.h>
     58    1.1      fvdl 
     59    1.1      fvdl #include <sys/syscallargs.h>
     60    1.1      fvdl 
     61   1.48  christos #include <compat/linux/common/linux_types.h>
     62   1.48  christos #include <compat/linux/common/linux_fcntl.h>
     63   1.48  christos #include <compat/linux/common/linux_misc.h>
     64   1.48  christos #include <compat/linux/common/linux_mmap.h>
     65   1.48  christos #include <compat/linux/common/linux_signal.h>
     66   1.48  christos #include <compat/linux/common/linux_util.h>
     67   1.95     njoly #include <compat/linux/common/linux_ipc.h>
     68   1.95     njoly #include <compat/linux/common/linux_sem.h>
     69  1.108       chs #include <compat/linux/common/linux_statfs.h>
     70   1.48  christos 
     71    1.1      fvdl #include <compat/linux/linux_syscallargs.h>
     72   1.26  christos 
     73    1.1      fvdl /*
     74   1.47       erh  * This file contains routines which are used
     75   1.47       erh  * on every linux architechture except the Alpha.
     76    1.1      fvdl  */
     77   1.21   mycroft 
     78   1.47       erh /* Used on: arm, i386, m68k, mips, ppc, sparc, sparc64 */
     79   1.47       erh /* Not used on: alpha */
     80    1.1      fvdl 
     81   1.70  jdolecek #ifdef DEBUG_LINUX
     82   1.70  jdolecek #define DPRINTF(a)	uprintf a
     83   1.70  jdolecek #else
     84   1.70  jdolecek #define DPRINTF(a)
     85   1.70  jdolecek #endif
     86   1.70  jdolecek 
     87   1.77      manu #ifndef COMPAT_LINUX32
     88   1.70  jdolecek 
     89    1.1      fvdl /*
     90   1.47       erh  * Alarm. This is a libc call which uses setitimer(2) in NetBSD.
     91  1.111   thorpej  * Do the same here.
     92    1.1      fvdl  */
     93    1.1      fvdl int
     94   1.99       dsl linux_sys_alarm(struct lwp *l, const struct linux_sys_alarm_args *uap, register_t *retval)
     95   1.20   thorpej {
     96   1.99       dsl 	/* {
     97   1.47       erh 		syscallarg(unsigned int) secs;
     98   1.99       dsl 	} */
     99   1.64   thorpej 	struct proc *p = l->l_proc;
    100  1.111   thorpej 	struct itimerval itv, oitv;
    101  1.111   thorpej 	int error;
    102    1.1      fvdl 
    103  1.111   thorpej 	timerclear(&itv.it_interval);
    104  1.111   thorpej 	itv.it_value.tv_sec = SCARG(uap, secs);
    105  1.111   thorpej 	itv.it_value.tv_usec = 0;
    106  1.111   thorpej 	if (itv.it_value.tv_sec < 0) {
    107  1.111   thorpej 		return EINVAL;
    108   1.47       erh 	}
    109    1.1      fvdl 
    110  1.111   thorpej 	if ((error = dogetitimer(p, ITIMER_REAL, &oitv)) != 0) {
    111  1.111   thorpej 		return error;
    112   1.16      fvdl 	}
    113  1.111   thorpej 	if (oitv.it_value.tv_usec) {
    114  1.111   thorpej 		oitv.it_value.tv_sec++;
    115   1.64   thorpej 	}
    116   1.64   thorpej 
    117  1.111   thorpej 	if ((error = dosetitimer(p, ITIMER_REAL, &itv)) != 0) {
    118  1.111   thorpej 		return error;
    119   1.16      fvdl 	}
    120    1.1      fvdl 
    121  1.111   thorpej 	*retval = oitv.it_value.tv_sec;
    122   1.16      fvdl 	return 0;
    123    1.1      fvdl }
    124   1.77      manu #endif /* !COMPAT_LINUX32 */
    125    1.1      fvdl 
    126   1.96     njoly #if !defined(__amd64__)
    127    1.1      fvdl int
    128   1.99       dsl linux_sys_nice(struct lwp *l, const struct linux_sys_nice_args *uap, register_t *retval)
    129   1.20   thorpej {
    130   1.99       dsl 	/* {
    131   1.47       erh 		syscallarg(int) incr;
    132   1.99       dsl 	} */
    133  1.104     njoly 	struct proc *p = l->l_proc;
    134  1.106     njoly 	struct sys_setpriority_args bsa;
    135  1.106     njoly 	int error;
    136   1.20   thorpej 
    137  1.106     njoly 	SCARG(&bsa, which) = PRIO_PROCESS;
    138  1.106     njoly 	SCARG(&bsa, who) = 0;
    139  1.104     njoly 	SCARG(&bsa, prio) = p->p_nice - NZERO + SCARG(uap, incr);
    140  1.104     njoly 
    141  1.106     njoly 	error = sys_setpriority(l, &bsa, retval);
    142  1.106     njoly 	return (error) ? EPERM : 0;
    143    1.1      fvdl }
    144   1.96     njoly #endif /* !__amd64__ */
    145    1.1      fvdl 
    146   1.77      manu #ifndef COMPAT_LINUX32
    147   1.74      manu #ifndef __amd64__
    148    1.1      fvdl /*
    149   1.47       erh  * The old Linux readdir was only able to read one entry at a time,
    150   1.47       erh  * even though it had a 'count' argument. In fact, the emulation
    151   1.47       erh  * of the old call was better than the original, because it did handle
    152   1.47       erh  * the count arg properly. Don't bother with it anymore now, and use
    153   1.47       erh  * it to distinguish between old and new. The difference is that the
    154   1.47       erh  * newer one actually does multiple entries, and the reclen field
    155   1.47       erh  * really is the reclen, not the namelength.
    156    1.1      fvdl  */
    157    1.1      fvdl int
    158   1.99       dsl linux_sys_readdir(struct lwp *l, const struct linux_sys_readdir_args *uap, register_t *retval)
    159   1.20   thorpej {
    160   1.99       dsl 	/* {
    161   1.47       erh 		syscallarg(int) fd;
    162   1.47       erh 		syscallarg(struct linux_dirent *) dent;
    163   1.47       erh 		syscallarg(unsigned int) count;
    164   1.99       dsl 	} */
    165  1.100     njoly 	int error;
    166   1.99       dsl 	struct linux_sys_getdents_args da;
    167    1.1      fvdl 
    168   1.99       dsl 	SCARG(&da, fd) = SCARG(uap, fd);
    169   1.99       dsl 	SCARG(&da, dent) = SCARG(uap, dent);
    170   1.99       dsl 	SCARG(&da, count) = 1;
    171  1.100     njoly 
    172  1.100     njoly 	error = linux_sys_getdents(l, &da, retval);
    173  1.100     njoly 	if (error == 0 && *retval > 1)
    174  1.100     njoly 		*retval = 1;
    175  1.100     njoly 
    176  1.100     njoly 	return error;
    177    1.1      fvdl }
    178   1.74      manu #endif /* !amd64 */
    179    1.1      fvdl 
    180    1.1      fvdl /*
    181    1.1      fvdl  * I wonder why Linux has gettimeofday() _and_ time().. Still, we
    182    1.1      fvdl  * need to deal with it.
    183    1.1      fvdl  */
    184    1.1      fvdl int
    185   1.99       dsl linux_sys_time(struct lwp *l, const struct linux_sys_time_args *uap, register_t *retval)
    186   1.20   thorpej {
    187   1.99       dsl 	/* {
    188   1.99       dsl 		syscallarg(linux_time_t) *t;
    189   1.99       dsl 	} */
    190    1.1      fvdl 	struct timeval atv;
    191    1.1      fvdl 	linux_time_t tt;
    192    1.1      fvdl 	int error;
    193    1.1      fvdl 
    194    1.1      fvdl 	microtime(&atv);
    195    1.1      fvdl 
    196    1.1      fvdl 	tt = atv.tv_sec;
    197    1.1      fvdl 	if (SCARG(uap, t) && (error = copyout(&tt, SCARG(uap, t), sizeof tt)))
    198    1.1      fvdl 		return error;
    199    1.1      fvdl 
    200    1.1      fvdl 	retval[0] = tt;
    201    1.1      fvdl 	return 0;
    202    1.1      fvdl }
    203    1.1      fvdl 
    204    1.1      fvdl /*
    205   1.73     perry  * utime(). Do conversion to things that utimes() understands,
    206   1.47       erh  * and pass it on.
    207    1.1      fvdl  */
    208    1.1      fvdl int
    209   1.99       dsl linux_sys_utime(struct lwp *l, const struct linux_sys_utime_args *uap, register_t *retval)
    210   1.20   thorpej {
    211   1.99       dsl 	/* {
    212   1.50  christos 		syscallarg(const char *) path;
    213   1.47       erh 		syscallarg(struct linux_utimbuf *)times;
    214   1.99       dsl 	} */
    215    1.2      fvdl 	int error;
    216   1.47       erh 	struct timeval tv[2], *tvp;
    217   1.47       erh 	struct linux_utimbuf lut;
    218    1.2      fvdl 
    219   1.47       erh 	if (SCARG(uap, times) != NULL) {
    220   1.47       erh 		if ((error = copyin(SCARG(uap, times), &lut, sizeof lut)))
    221   1.47       erh 			return error;
    222   1.47       erh 		tv[0].tv_usec = tv[1].tv_usec = 0;
    223   1.47       erh 		tv[0].tv_sec = lut.l_actime;
    224   1.47       erh 		tv[1].tv_sec = lut.l_modtime;
    225   1.92       dsl 		tvp = tv;
    226   1.92       dsl 	} else
    227   1.92       dsl 		tvp = NULL;
    228    1.2      fvdl 
    229   1.92       dsl 	return do_sys_utimes(l, NULL, SCARG(uap, path), FOLLOW,
    230   1.92       dsl 			   tvp,  UIO_SYSSPACE);
    231    1.1      fvdl }
    232    1.1      fvdl 
    233   1.74      manu #ifndef __amd64__
    234   1.47       erh /*
    235   1.72       erh  * waitpid(2).  Just forward on to linux_sys_wait4 with a NULL rusage.
    236   1.47       erh  */
    237    1.1      fvdl int
    238   1.99       dsl linux_sys_waitpid(struct lwp *l, const struct linux_sys_waitpid_args *uap, register_t *retval)
    239   1.20   thorpej {
    240   1.99       dsl 	/* {
    241   1.47       erh 		syscallarg(int) pid;
    242   1.47       erh 		syscallarg(int *) status;
    243   1.47       erh 		syscallarg(int) options;
    244   1.99       dsl 	} */
    245   1.72       erh 	struct linux_sys_wait4_args linux_w4a;
    246    1.2      fvdl 
    247   1.72       erh 	SCARG(&linux_w4a, pid) = SCARG(uap, pid);
    248   1.72       erh 	SCARG(&linux_w4a, status) = SCARG(uap, status);
    249   1.72       erh 	SCARG(&linux_w4a, options) = SCARG(uap, options);
    250   1.72       erh 	SCARG(&linux_w4a, rusage) = NULL;
    251    1.2      fvdl 
    252   1.72       erh 	return linux_sys_wait4(l, &linux_w4a, retval);
    253   1.51   thorpej }
    254   1.74      manu #endif /* !amd64 */
    255   1.51   thorpej 
    256   1.51   thorpej int
    257   1.99       dsl linux_sys_setresgid(struct lwp *l, const struct linux_sys_setresgid_args *uap, register_t *retval)
    258   1.51   thorpej {
    259   1.99       dsl 	/* {
    260   1.51   thorpej 		syscallarg(gid_t) rgid;
    261   1.51   thorpej 		syscallarg(gid_t) egid;
    262   1.51   thorpej 		syscallarg(gid_t) sgid;
    263   1.99       dsl 	} */
    264   1.51   thorpej 
    265   1.51   thorpej 	/*
    266   1.51   thorpej 	 * Note: These checks are a little different than the NetBSD
    267   1.51   thorpej 	 * setregid(2) call performs.  This precisely follows the
    268   1.51   thorpej 	 * behavior of the Linux kernel.
    269   1.51   thorpej 	 */
    270   1.67       dsl 	return do_setresgid(l, SCARG(uap,rgid), SCARG(uap, egid),
    271   1.67       dsl 			    SCARG(uap, sgid),
    272   1.67       dsl 			    ID_R_EQ_R | ID_R_EQ_E | ID_R_EQ_S |
    273   1.67       dsl 			    ID_E_EQ_R | ID_E_EQ_E | ID_E_EQ_S |
    274   1.67       dsl 			    ID_S_EQ_R | ID_S_EQ_E | ID_S_EQ_S );
    275   1.51   thorpej }
    276   1.51   thorpej 
    277   1.51   thorpej int
    278   1.99       dsl linux_sys_getresgid(struct lwp *l, const struct linux_sys_getresgid_args *uap, register_t *retval)
    279   1.51   thorpej {
    280   1.99       dsl 	/* {
    281   1.51   thorpej 		syscallarg(gid_t *) rgid;
    282   1.51   thorpej 		syscallarg(gid_t *) egid;
    283   1.51   thorpej 		syscallarg(gid_t *) sgid;
    284   1.99       dsl 	} */
    285   1.81        ad 	kauth_cred_t pc = l->l_cred;
    286   1.51   thorpej 	int error;
    287   1.79      elad 	gid_t gid;
    288   1.51   thorpej 
    289   1.51   thorpej 	/*
    290   1.51   thorpej 	 * Linux copies these values out to userspace like so:
    291   1.51   thorpej 	 *
    292   1.51   thorpej 	 *	1. Copy out rgid.
    293   1.51   thorpej 	 *	2. If that succeeds, copy out egid.
    294   1.51   thorpej 	 *	3. If both of those succeed, copy out sgid.
    295   1.51   thorpej 	 */
    296   1.79      elad 	gid = kauth_cred_getgid(pc);
    297   1.79      elad 	if ((error = copyout(&gid, SCARG(uap, rgid), sizeof(gid_t))) != 0)
    298   1.51   thorpej 		return (error);
    299   1.51   thorpej 
    300   1.79      elad 	gid = kauth_cred_getegid(pc);
    301   1.79      elad 	if ((error = copyout(&gid, SCARG(uap, egid), sizeof(gid_t))) != 0)
    302   1.51   thorpej 		return (error);
    303   1.51   thorpej 
    304   1.79      elad 	gid = kauth_cred_getsvgid(pc);
    305   1.79      elad 
    306   1.79      elad 	return (copyout(&gid, SCARG(uap, sgid), sizeof(gid_t)));
    307   1.52      tron }
    308   1.52      tron 
    309   1.74      manu #ifndef __amd64__
    310   1.52      tron /*
    311   1.52      tron  * I wonder why Linux has settimeofday() _and_ stime().. Still, we
    312   1.52      tron  * need to deal with it.
    313   1.52      tron  */
    314   1.52      tron int
    315   1.99       dsl linux_sys_stime(struct lwp *l, const struct linux_sys_stime_args *uap, register_t *retval)
    316   1.52      tron {
    317   1.99       dsl 	/* {
    318   1.99       dsl 		syscallarg(linux_time_t) *t;
    319   1.99       dsl 	} */
    320   1.75  christos 	struct timespec ats;
    321   1.52      tron 	linux_time_t tt;
    322   1.52      tron 	int error;
    323   1.52      tron 
    324  1.105     njoly 	if ((error = copyin(SCARG(uap, t), &tt, sizeof tt)) != 0)
    325   1.52      tron 		return error;
    326   1.52      tron 
    327   1.75  christos 	ats.tv_sec = tt;
    328   1.75  christos 	ats.tv_nsec = 0;
    329   1.52      tron 
    330   1.81        ad 	if ((error = settime(l->l_proc, &ats)))
    331   1.52      tron 		return (error);
    332   1.52      tron 
    333   1.52      tron 	return 0;
    334    1.1      fvdl }
    335   1.70  jdolecek 
    336   1.70  jdolecek /*
    337   1.70  jdolecek  * Implement the fs stat functions. Straightforward.
    338   1.70  jdolecek  */
    339   1.70  jdolecek int
    340   1.99       dsl linux_sys_statfs64(struct lwp *l, const struct linux_sys_statfs64_args *uap, register_t *retval)
    341   1.70  jdolecek {
    342   1.99       dsl 	/* {
    343   1.70  jdolecek 		syscallarg(const char *) path;
    344   1.70  jdolecek 		syscallarg(size_t) sz;
    345   1.70  jdolecek 		syscallarg(struct linux_statfs64 *) sp;
    346   1.99       dsl 	} */
    347   1.90       dsl 	struct statvfs *sb;
    348   1.70  jdolecek 	struct linux_statfs64 ltmp;
    349   1.70  jdolecek 	int error;
    350   1.70  jdolecek 
    351   1.70  jdolecek 	if (SCARG(uap, sz) != sizeof ltmp)
    352   1.70  jdolecek 		return (EINVAL);
    353   1.70  jdolecek 
    354   1.90       dsl 	sb = STATVFSBUF_GET();
    355   1.90       dsl 	error = do_sys_pstatvfs(l, SCARG(uap, path), ST_WAIT, sb);
    356   1.90       dsl 	if (error == 0) {
    357   1.90       dsl 		bsd_to_linux_statfs64(sb, &ltmp);
    358   1.90       dsl 		error = copyout(&ltmp, SCARG(uap, sp), sizeof ltmp);
    359   1.78      yamt 	}
    360   1.90       dsl 	STATVFSBUF_PUT(sb);
    361   1.78      yamt 	return error;
    362   1.70  jdolecek }
    363   1.70  jdolecek 
    364   1.70  jdolecek int
    365   1.99       dsl linux_sys_fstatfs64(struct lwp *l, const struct linux_sys_fstatfs64_args *uap, register_t *retval)
    366   1.70  jdolecek {
    367   1.99       dsl 	/* {
    368   1.70  jdolecek 		syscallarg(int) fd;
    369   1.70  jdolecek 		syscallarg(size_t) sz;
    370   1.70  jdolecek 		syscallarg(struct linux_statfs64 *) sp;
    371   1.99       dsl 	} */
    372   1.90       dsl 	struct statvfs *sb;
    373   1.70  jdolecek 	struct linux_statfs64 ltmp;
    374   1.70  jdolecek 	int error;
    375   1.70  jdolecek 
    376   1.70  jdolecek 	if (SCARG(uap, sz) != sizeof ltmp)
    377   1.70  jdolecek 		return (EINVAL);
    378   1.70  jdolecek 
    379   1.90       dsl 	sb = STATVFSBUF_GET();
    380   1.90       dsl 	error = do_sys_fstatvfs(l, SCARG(uap, fd), ST_WAIT, sb);
    381   1.90       dsl 	if (error == 0) {
    382   1.90       dsl 		bsd_to_linux_statfs64(sb, &ltmp);
    383   1.90       dsl 		error = copyout(&ltmp, SCARG(uap, sp), sizeof ltmp);
    384   1.78      yamt 	}
    385   1.90       dsl 	STATVFSBUF_PUT(sb);
    386   1.78      yamt 	return error;
    387   1.70  jdolecek }
    388  1.108       chs #endif /* !__amd64__ */
    389   1.77      manu #endif /* !COMPAT_LINUX32 */
    390