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