Home | History | Annotate | Line # | Download | only in common
linux_misc_notalpha.c revision 1.48
      1  1.48  christos /*	$NetBSD: linux_misc_notalpha.c,v 1.48 1998/10/03 20:17:42 christos Exp $	*/
      2  1.47       erh 
      3  1.47       erh /*-
      4  1.47       erh  * Copyright (c) 1998 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.47       erh  * by Eric Haszlakiewicz.
      9  1.47       erh  *
     10  1.47       erh  * Redistribution and use in source and binary forms, with or without
     11  1.47       erh  * modification, are permitted provided that the following conditions
     12  1.47       erh  * are met:
     13  1.47       erh  * 1. Redistributions of source code must retain the above copyright
     14  1.47       erh  *    notice, this list of conditions and the following disclaimer.
     15  1.47       erh  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.47       erh  *    notice, this list of conditions and the following disclaimer in the
     17  1.47       erh  *    documentation and/or other materials provided with the distribution.
     18  1.47       erh  * 3. All advertising materials mentioning features or use of this software
     19  1.47       erh  *    must display the following acknowledgement:
     20  1.47       erh  *	This product includes software developed by the NetBSD
     21  1.47       erh  *	Foundation, Inc. and its contributors.
     22  1.47       erh  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  1.47       erh  *    contributors may be used to endorse or promote products derived
     24  1.47       erh  *    from this software without specific prior written permission.
     25  1.47       erh  *
     26  1.47       erh  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  1.47       erh  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  1.47       erh  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  1.47       erh  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  1.47       erh  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  1.47       erh  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  1.47       erh  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  1.47       erh  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  1.47       erh  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  1.47       erh  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  1.47       erh  * POSSIBILITY OF SUCH DAMAGE.
     37  1.47       erh  */
     38   1.1      fvdl 
     39   1.1      fvdl /*
     40   1.1      fvdl  * Copyright (c) 1995 Frank van der Linden
     41   1.1      fvdl  * All rights reserved.
     42   1.1      fvdl  *
     43   1.1      fvdl  * Redistribution and use in source and binary forms, with or without
     44   1.1      fvdl  * modification, are permitted provided that the following conditions
     45   1.1      fvdl  * are met:
     46   1.1      fvdl  * 1. Redistributions of source code must retain the above copyright
     47   1.1      fvdl  *    notice, this list of conditions and the following disclaimer.
     48   1.1      fvdl  * 2. Redistributions in binary form must reproduce the above copyright
     49   1.1      fvdl  *    notice, this list of conditions and the following disclaimer in the
     50   1.1      fvdl  *    documentation and/or other materials provided with the distribution.
     51   1.1      fvdl  * 3. All advertising materials mentioning features or use of this software
     52   1.1      fvdl  *    must display the following acknowledgement:
     53   1.1      fvdl  *      This product includes software developed for the NetBSD Project
     54   1.1      fvdl  *      by Frank van der Linden
     55   1.1      fvdl  * 4. The name of the author may not be used to endorse or promote products
     56   1.1      fvdl  *    derived from this software without specific prior written permission
     57   1.1      fvdl  *
     58   1.1      fvdl  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     59   1.1      fvdl  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     60   1.1      fvdl  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     61   1.1      fvdl  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     62   1.1      fvdl  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     63   1.1      fvdl  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     64   1.1      fvdl  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     65   1.1      fvdl  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     66   1.1      fvdl  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     67   1.1      fvdl  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     68   1.1      fvdl  */
     69   1.1      fvdl 
     70   1.1      fvdl #include <sys/param.h>
     71   1.1      fvdl #include <sys/systm.h>
     72   1.1      fvdl #include <sys/kernel.h>
     73  1.47       erh #include <sys/mman.h>
     74  1.47       erh #include <sys/mount.h>
     75   1.1      fvdl #include <sys/malloc.h>
     76   1.1      fvdl #include <sys/mbuf.h>
     77  1.47       erh #include <sys/namei.h>
     78   1.1      fvdl #include <sys/ptrace.h>
     79   1.1      fvdl #include <sys/resource.h>
     80   1.1      fvdl #include <sys/resourcevar.h>
     81   1.1      fvdl #include <sys/wait.h>
     82   1.1      fvdl 
     83   1.1      fvdl #include <sys/syscallargs.h>
     84   1.1      fvdl 
     85   1.1      fvdl #include <vm/vm.h>
     86   1.1      fvdl #include <vm/vm_param.h>
     87   1.1      fvdl 
     88  1.48  christos #include <compat/linux/common/linux_types.h>
     89  1.48  christos #include <compat/linux/common/linux_fcntl.h>
     90  1.48  christos #include <compat/linux/common/linux_misc.h>
     91  1.48  christos #include <compat/linux/common/linux_mmap.h>
     92  1.48  christos #include <compat/linux/common/linux_signal.h>
     93  1.48  christos #include <compat/linux/common/linux_util.h>
     94  1.48  christos 
     95   1.1      fvdl #include <compat/linux/linux_syscallargs.h>
     96  1.26  christos 
     97   1.1      fvdl /*
     98  1.47       erh  * This file contains routines which are used
     99  1.47       erh  * on every linux architechture except the Alpha.
    100   1.1      fvdl  */
    101  1.21   mycroft 
    102  1.47       erh /* Used on: arm, i386, m68k, mips, ppc, sparc, sparc64 */
    103  1.47       erh /* Not used on: alpha */
    104   1.1      fvdl 
    105   1.1      fvdl /*
    106  1.47       erh  * Alarm. This is a libc call which uses setitimer(2) in NetBSD.
    107  1.47       erh  * Fiddle with the timers to make it work.
    108   1.1      fvdl  */
    109   1.1      fvdl int
    110  1.47       erh linux_sys_alarm(p, v, retval)
    111   1.1      fvdl 	struct proc *p;
    112  1.20   thorpej 	void *v;
    113  1.20   thorpej 	register_t *retval;
    114  1.20   thorpej {
    115  1.47       erh 	struct linux_sys_alarm_args /* {
    116  1.47       erh 		syscallarg(unsigned int) secs;
    117  1.20   thorpej 	} */ *uap = v;
    118  1.47       erh 	int s;
    119  1.47       erh 	struct itimerval *itp, it;
    120   1.1      fvdl 
    121  1.47       erh 	itp = &p->p_realtimer;
    122  1.47       erh 	s = splclock();
    123  1.47       erh 	/*
    124  1.47       erh 	 * Clear any pending timer alarms.
    125  1.47       erh 	 */
    126  1.47       erh 	untimeout(realitexpire, p);
    127  1.47       erh 	timerclear(&itp->it_interval);
    128  1.47       erh 	if (timerisset(&itp->it_value) &&
    129  1.47       erh 	    timercmp(&itp->it_value, &time, >))
    130  1.47       erh 		timersub(&itp->it_value, &time, &itp->it_value);
    131  1.47       erh 	/*
    132  1.47       erh 	 * Return how many seconds were left (rounded up)
    133  1.47       erh 	 */
    134  1.47       erh 	retval[0] = itp->it_value.tv_sec;
    135  1.47       erh 	if (itp->it_value.tv_usec)
    136  1.47       erh 		retval[0]++;
    137   1.1      fvdl 
    138  1.47       erh 	/*
    139  1.47       erh 	 * alarm(0) just resets the timer.
    140  1.47       erh 	 */
    141  1.47       erh 	if (SCARG(uap, secs) == 0) {
    142  1.47       erh 		timerclear(&itp->it_value);
    143  1.47       erh 		splx(s);
    144  1.47       erh 		return 0;
    145  1.47       erh 	}
    146   1.1      fvdl 
    147  1.47       erh 	/*
    148  1.47       erh 	 * Check the new alarm time for sanity, and set it.
    149  1.47       erh 	 */
    150  1.47       erh 	timerclear(&it.it_interval);
    151  1.47       erh 	it.it_value.tv_sec = SCARG(uap, secs);
    152  1.47       erh 	it.it_value.tv_usec = 0;
    153  1.47       erh 	if (itimerfix(&it.it_value) || itimerfix(&it.it_interval)) {
    154  1.47       erh 		splx(s);
    155  1.47       erh 		return (EINVAL);
    156  1.16      fvdl 	}
    157   1.1      fvdl 
    158  1.47       erh 	if (timerisset(&it.it_value)) {
    159  1.47       erh 		timeradd(&it.it_value, &time, &it.it_value);
    160  1.47       erh 		timeout(realitexpire, p, hzto(&it.it_value));
    161  1.16      fvdl 	}
    162  1.47       erh 	p->p_realtimer = it;
    163  1.47       erh 	splx(s);
    164   1.1      fvdl 
    165  1.16      fvdl 	return 0;
    166   1.1      fvdl }
    167   1.1      fvdl 
    168   1.1      fvdl int
    169  1.47       erh linux_sys_nice(p, v, retval)
    170   1.1      fvdl 	struct proc *p;
    171  1.20   thorpej 	void *v;
    172  1.20   thorpej 	register_t *retval;
    173  1.20   thorpej {
    174  1.47       erh 	struct linux_sys_nice_args /* {
    175  1.47       erh 		syscallarg(int) incr;
    176  1.20   thorpej 	} */ *uap = v;
    177  1.47       erh         struct sys_setpriority_args bsa;
    178  1.20   thorpej 
    179  1.47       erh         SCARG(&bsa, which) = PRIO_PROCESS;
    180  1.47       erh         SCARG(&bsa, who) = 0;
    181  1.47       erh 	SCARG(&bsa, prio) = SCARG(uap, incr);
    182  1.47       erh         return sys_setpriority(p, &bsa, retval);
    183   1.1      fvdl }
    184   1.1      fvdl 
    185   1.1      fvdl /*
    186  1.47       erh  * The old Linux readdir was only able to read one entry at a time,
    187  1.47       erh  * even though it had a 'count' argument. In fact, the emulation
    188  1.47       erh  * of the old call was better than the original, because it did handle
    189  1.47       erh  * the count arg properly. Don't bother with it anymore now, and use
    190  1.47       erh  * it to distinguish between old and new. The difference is that the
    191  1.47       erh  * newer one actually does multiple entries, and the reclen field
    192  1.47       erh  * really is the reclen, not the namelength.
    193   1.1      fvdl  */
    194   1.1      fvdl int
    195  1.47       erh linux_sys_readdir(p, v, retval)
    196   1.1      fvdl 	struct proc *p;
    197  1.20   thorpej 	void *v;
    198  1.20   thorpej 	register_t *retval;
    199  1.20   thorpej {
    200  1.47       erh 	struct linux_sys_readdir_args /* {
    201  1.47       erh 		syscallarg(int) fd;
    202  1.47       erh 		syscallarg(struct linux_dirent *) dent;
    203  1.47       erh 		syscallarg(unsigned int) count;
    204  1.20   thorpej 	} */ *uap = v;
    205   1.1      fvdl 
    206  1.47       erh 	SCARG(uap, count) = 1;
    207  1.47       erh 	return linux_sys_getdents(p, uap, retval);
    208   1.1      fvdl }
    209   1.1      fvdl 
    210   1.1      fvdl /*
    211   1.1      fvdl  * I wonder why Linux has gettimeofday() _and_ time().. Still, we
    212   1.1      fvdl  * need to deal with it.
    213   1.1      fvdl  */
    214   1.1      fvdl int
    215  1.21   mycroft linux_sys_time(p, v, retval)
    216   1.1      fvdl 	struct proc *p;
    217  1.20   thorpej 	void *v;
    218  1.20   thorpej 	register_t *retval;
    219  1.20   thorpej {
    220  1.21   mycroft 	struct linux_sys_time_args /* {
    221   1.1      fvdl 		linux_time_t *t;
    222  1.20   thorpej 	} */ *uap = v;
    223   1.1      fvdl 	struct timeval atv;
    224   1.1      fvdl 	linux_time_t tt;
    225   1.1      fvdl 	int error;
    226   1.1      fvdl 
    227   1.1      fvdl 	microtime(&atv);
    228   1.1      fvdl 
    229   1.1      fvdl 	tt = atv.tv_sec;
    230   1.1      fvdl 	if (SCARG(uap, t) && (error = copyout(&tt, SCARG(uap, t), sizeof tt)))
    231   1.1      fvdl 		return error;
    232   1.1      fvdl 
    233   1.1      fvdl 	retval[0] = tt;
    234   1.1      fvdl 	return 0;
    235   1.1      fvdl }
    236   1.1      fvdl 
    237   1.1      fvdl /*
    238  1.47       erh  * utime(). Do conversion to things that utimes() understands,
    239  1.47       erh  * and pass it on.
    240   1.1      fvdl  */
    241   1.1      fvdl int
    242  1.47       erh linux_sys_utime(p, v, retval)
    243   1.1      fvdl 	struct proc *p;
    244  1.20   thorpej 	void *v;
    245  1.20   thorpej 	register_t *retval;
    246  1.20   thorpej {
    247  1.47       erh 	struct linux_sys_utime_args /* {
    248   1.1      fvdl 		syscallarg(char *) path;
    249  1.47       erh 		syscallarg(struct linux_utimbuf *)times;
    250  1.20   thorpej 	} */ *uap = v;
    251   1.2      fvdl 	caddr_t sg;
    252   1.2      fvdl 	int error;
    253  1.47       erh 	struct sys_utimes_args ua;
    254  1.47       erh 	struct timeval tv[2], *tvp;
    255  1.47       erh 	struct linux_utimbuf lut;
    256   1.2      fvdl 
    257   1.9  christos 	sg = stackgap_init(p->p_emul);
    258   1.9  christos 	LINUX_CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
    259   1.2      fvdl 
    260  1.47       erh 	SCARG(&ua, path) = SCARG(uap, path);
    261   1.2      fvdl 
    262  1.47       erh 	if (SCARG(uap, times) != NULL) {
    263  1.47       erh 		if ((error = copyin(SCARG(uap, times), &lut, sizeof lut)))
    264  1.47       erh 			return error;
    265  1.47       erh 		tv[0].tv_usec = tv[1].tv_usec = 0;
    266  1.47       erh 		tv[0].tv_sec = lut.l_actime;
    267  1.47       erh 		tv[1].tv_sec = lut.l_modtime;
    268  1.47       erh 		tvp = (struct timeval *) stackgap_alloc(&sg, sizeof(tv));
    269  1.47       erh 		if ((error = copyout(tv, tvp, sizeof tv)))
    270  1.47       erh 			return error;
    271  1.47       erh 		SCARG(&ua, tptr) = tvp;
    272  1.47       erh 	}
    273  1.47       erh 	else
    274  1.47       erh 		SCARG(&ua, tptr) = NULL;
    275   1.2      fvdl 
    276  1.47       erh 	return sys_utimes(p, &ua, retval);
    277   1.1      fvdl }
    278   1.1      fvdl 
    279  1.47       erh /*
    280  1.47       erh  * waitpid(2). Passed on to the NetBSD call, surrounded by code to
    281  1.47       erh  * reserve some space for a NetBSD-style wait status, and converting
    282  1.47       erh  * it to what Linux wants.
    283  1.47       erh  */
    284   1.1      fvdl int
    285  1.47       erh linux_sys_waitpid(p, v, retval)
    286   1.1      fvdl 	struct proc *p;
    287  1.20   thorpej 	void *v;
    288  1.20   thorpej 	register_t *retval;
    289  1.20   thorpej {
    290  1.47       erh 	struct linux_sys_waitpid_args /* {
    291  1.47       erh 		syscallarg(int) pid;
    292  1.47       erh 		syscallarg(int *) status;
    293  1.47       erh 		syscallarg(int) options;
    294  1.20   thorpej 	} */ *uap = v;
    295  1.47       erh 	struct sys_wait4_args w4a;
    296  1.47       erh 	int error, *status, tstat;
    297   1.2      fvdl 	caddr_t sg;
    298   1.2      fvdl 
    299  1.47       erh 	if (SCARG(uap, status) != NULL) {
    300  1.47       erh 		sg = stackgap_init(p->p_emul);
    301  1.47       erh 		status = (int *) stackgap_alloc(&sg, sizeof status);
    302  1.47       erh 	} else
    303  1.47       erh 		status = NULL;
    304   1.2      fvdl 
    305  1.47       erh 	SCARG(&w4a, pid) = SCARG(uap, pid);
    306  1.47       erh 	SCARG(&w4a, status) = status;
    307  1.47       erh 	SCARG(&w4a, options) = SCARG(uap, options);
    308  1.47       erh 	SCARG(&w4a, rusage) = NULL;
    309   1.2      fvdl 
    310  1.47       erh 	if ((error = sys_wait4(p, &w4a, retval)))
    311   1.2      fvdl 		return error;
    312   1.2      fvdl 
    313  1.47       erh 	sigdelset(&p->p_siglist, SIGCHLD);
    314   1.2      fvdl 
    315  1.47       erh 	if (status != NULL) {
    316  1.47       erh 		if ((error = copyin(status, &tstat, sizeof tstat)))
    317  1.47       erh 			return error;
    318   1.2      fvdl 
    319  1.47       erh 		bsd_to_linux_wstat(&tstat);
    320  1.47       erh 		return copyout(&tstat, SCARG(uap, status), sizeof tstat);
    321  1.44   thorpej 	}
    322   1.1      fvdl 
    323  1.47       erh 	return 0;
    324   1.1      fvdl }
    325