Home | History | Annotate | Line # | Download | only in common
linux_misc_notalpha.c revision 1.53.4.1
      1 /*	$NetBSD: linux_misc_notalpha.c,v 1.53.4.1 2000/07/13 20:16:28 thorpej Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1995, 1998 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Frank van der Linden and Eric Haszlakiewicz; by Jason R. Thorpe
      9  * of the Numerical Aerospace Simulation Facility, NASA Ames Research Center.
     10  *
     11  * Redistribution and use in source and binary forms, with or without
     12  * modification, are permitted provided that the following conditions
     13  * are met:
     14  * 1. Redistributions of source code must retain the above copyright
     15  *    notice, this list of conditions and the following disclaimer.
     16  * 2. Redistributions in binary form must reproduce the above copyright
     17  *    notice, this list of conditions and the following disclaimer in the
     18  *    documentation and/or other materials provided with the distribution.
     19  * 3. All advertising materials mentioning features or use of this software
     20  *    must display the following acknowledgement:
     21  *	This product includes software developed by the NetBSD
     22  *	Foundation, Inc. and its contributors.
     23  * 4. Neither the name of The NetBSD Foundation nor the names of its
     24  *    contributors may be used to endorse or promote products derived
     25  *    from this software without specific prior written permission.
     26  *
     27  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     28  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     29  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     30  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     31  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     32  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     33  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     34  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     35  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     36  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     37  * POSSIBILITY OF SUCH DAMAGE.
     38  */
     39 
     40 #include <sys/param.h>
     41 #include <sys/systm.h>
     42 #include <sys/kernel.h>
     43 #include <sys/mman.h>
     44 #include <sys/mount.h>
     45 #include <sys/malloc.h>
     46 #include <sys/mbuf.h>
     47 #include <sys/namei.h>
     48 #include <sys/ptrace.h>
     49 #include <sys/resource.h>
     50 #include <sys/resourcevar.h>
     51 #include <sys/wait.h>
     52 
     53 #include <sys/syscallargs.h>
     54 
     55 #include <vm/vm.h>
     56 #include <vm/vm_param.h>
     57 
     58 #include <compat/linux/common/linux_types.h>
     59 #include <compat/linux/common/linux_fcntl.h>
     60 #include <compat/linux/common/linux_misc.h>
     61 #include <compat/linux/common/linux_mmap.h>
     62 #include <compat/linux/common/linux_signal.h>
     63 #include <compat/linux/common/linux_util.h>
     64 
     65 #include <compat/linux/linux_syscallargs.h>
     66 
     67 /*
     68  * This file contains routines which are used
     69  * on every linux architechture except the Alpha.
     70  */
     71 
     72 /* Used on: arm, i386, m68k, mips, ppc, sparc, sparc64 */
     73 /* Not used on: alpha */
     74 
     75 /*
     76  * Alarm. This is a libc call which uses setitimer(2) in NetBSD.
     77  * Fiddle with the timers to make it work.
     78  */
     79 int
     80 linux_sys_alarm(p, v, retval)
     81 	struct proc *p;
     82 	void *v;
     83 	register_t *retval;
     84 {
     85 	struct linux_sys_alarm_args /* {
     86 		syscallarg(unsigned int) secs;
     87 	} */ *uap = v;
     88 	int s;
     89 	struct itimerval *itp, it;
     90 
     91 	itp = &p->p_realtimer;
     92 	s = splclock();
     93 	/*
     94 	 * Clear any pending timer alarms.
     95 	 */
     96 	callout_stop(&p->p_realit_ch);
     97 	timerclear(&itp->it_interval);
     98 	if (timerisset(&itp->it_value) &&
     99 	    timercmp(&itp->it_value, &time, >))
    100 		timersub(&itp->it_value, &time, &itp->it_value);
    101 	/*
    102 	 * Return how many seconds were left (rounded up)
    103 	 */
    104 	retval[0] = itp->it_value.tv_sec;
    105 	if (itp->it_value.tv_usec)
    106 		retval[0]++;
    107 
    108 	/*
    109 	 * alarm(0) just resets the timer.
    110 	 */
    111 	if (SCARG(uap, secs) == 0) {
    112 		timerclear(&itp->it_value);
    113 		splx(s);
    114 		return 0;
    115 	}
    116 
    117 	/*
    118 	 * Check the new alarm time for sanity, and set it.
    119 	 */
    120 	timerclear(&it.it_interval);
    121 	it.it_value.tv_sec = SCARG(uap, secs);
    122 	it.it_value.tv_usec = 0;
    123 	if (itimerfix(&it.it_value) || itimerfix(&it.it_interval)) {
    124 		splx(s);
    125 		return (EINVAL);
    126 	}
    127 
    128 	if (timerisset(&it.it_value)) {
    129 		/*
    130 		 * Don't need to check hzto() return value, here.
    131 		 * callout_reset() does it for us.
    132 		 */
    133 		timeradd(&it.it_value, &time, &it.it_value);
    134 		callout_reset(&p->p_realit_ch, hzto(&it.it_value),
    135 		    realitexpire, p);
    136 	}
    137 	p->p_realtimer = it;
    138 	splx(s);
    139 
    140 	return 0;
    141 }
    142 
    143 int
    144 linux_sys_nice(p, v, retval)
    145 	struct proc *p;
    146 	void *v;
    147 	register_t *retval;
    148 {
    149 	struct linux_sys_nice_args /* {
    150 		syscallarg(int) incr;
    151 	} */ *uap = v;
    152         struct sys_setpriority_args bsa;
    153 
    154         SCARG(&bsa, which) = PRIO_PROCESS;
    155         SCARG(&bsa, who) = 0;
    156 	SCARG(&bsa, prio) = SCARG(uap, incr);
    157         return sys_setpriority(p, &bsa, retval);
    158 }
    159 
    160 /*
    161  * The old Linux readdir was only able to read one entry at a time,
    162  * even though it had a 'count' argument. In fact, the emulation
    163  * of the old call was better than the original, because it did handle
    164  * the count arg properly. Don't bother with it anymore now, and use
    165  * it to distinguish between old and new. The difference is that the
    166  * newer one actually does multiple entries, and the reclen field
    167  * really is the reclen, not the namelength.
    168  */
    169 int
    170 linux_sys_readdir(p, v, retval)
    171 	struct proc *p;
    172 	void *v;
    173 	register_t *retval;
    174 {
    175 	struct linux_sys_readdir_args /* {
    176 		syscallarg(int) fd;
    177 		syscallarg(struct linux_dirent *) dent;
    178 		syscallarg(unsigned int) count;
    179 	} */ *uap = v;
    180 
    181 	SCARG(uap, count) = 1;
    182 	return linux_sys_getdents(p, uap, retval);
    183 }
    184 
    185 /*
    186  * I wonder why Linux has gettimeofday() _and_ time().. Still, we
    187  * need to deal with it.
    188  */
    189 int
    190 linux_sys_time(p, v, retval)
    191 	struct proc *p;
    192 	void *v;
    193 	register_t *retval;
    194 {
    195 	struct linux_sys_time_args /* {
    196 		linux_time_t *t;
    197 	} */ *uap = v;
    198 	struct timeval atv;
    199 	linux_time_t tt;
    200 	int error;
    201 
    202 	microtime(&atv);
    203 
    204 	tt = atv.tv_sec;
    205 	if (SCARG(uap, t) && (error = copyout(&tt, SCARG(uap, t), sizeof tt)))
    206 		return error;
    207 
    208 	retval[0] = tt;
    209 	return 0;
    210 }
    211 
    212 /*
    213  * utime(). Do conversion to things that utimes() understands,
    214  * and pass it on.
    215  */
    216 int
    217 linux_sys_utime(p, v, retval)
    218 	struct proc *p;
    219 	void *v;
    220 	register_t *retval;
    221 {
    222 	struct linux_sys_utime_args /* {
    223 		syscallarg(const char *) path;
    224 		syscallarg(struct linux_utimbuf *)times;
    225 	} */ *uap = v;
    226 	caddr_t sg;
    227 	int error;
    228 	struct sys_utimes_args ua;
    229 	struct timeval tv[2], *tvp;
    230 	struct linux_utimbuf lut;
    231 
    232 	sg = stackgap_init(p->p_emul);
    233 	LINUX_CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
    234 
    235 	SCARG(&ua, path) = SCARG(uap, path);
    236 
    237 	if (SCARG(uap, times) != NULL) {
    238 		if ((error = copyin(SCARG(uap, times), &lut, sizeof lut)))
    239 			return error;
    240 		tv[0].tv_usec = tv[1].tv_usec = 0;
    241 		tv[0].tv_sec = lut.l_actime;
    242 		tv[1].tv_sec = lut.l_modtime;
    243 		tvp = (struct timeval *) stackgap_alloc(&sg, sizeof(tv));
    244 		if ((error = copyout(tv, tvp, sizeof tv)))
    245 			return error;
    246 		SCARG(&ua, tptr) = tvp;
    247 	}
    248 	else
    249 		SCARG(&ua, tptr) = NULL;
    250 
    251 	return sys_utimes(p, &ua, retval);
    252 }
    253 
    254 /*
    255  * waitpid(2). Passed on to the NetBSD call, surrounded by code to
    256  * reserve some space for a NetBSD-style wait status, and converting
    257  * it to what Linux wants.
    258  */
    259 int
    260 linux_sys_waitpid(p, v, retval)
    261 	struct proc *p;
    262 	void *v;
    263 	register_t *retval;
    264 {
    265 	struct linux_sys_waitpid_args /* {
    266 		syscallarg(int) pid;
    267 		syscallarg(int *) status;
    268 		syscallarg(int) options;
    269 	} */ *uap = v;
    270 	struct sys_wait4_args w4a;
    271 	int error, *status, tstat;
    272 	caddr_t sg;
    273 
    274 	if (SCARG(uap, status) != NULL) {
    275 		sg = stackgap_init(p->p_emul);
    276 		status = (int *) stackgap_alloc(&sg, sizeof status);
    277 	} else
    278 		status = NULL;
    279 
    280 	SCARG(&w4a, pid) = SCARG(uap, pid);
    281 	SCARG(&w4a, status) = status;
    282 	SCARG(&w4a, options) = SCARG(uap, options);
    283 	SCARG(&w4a, rusage) = NULL;
    284 
    285 	if ((error = sys_wait4(p, &w4a, retval)))
    286 		return error;
    287 
    288 	sigdelset(&p->p_siglist, SIGCHLD);
    289 
    290 	if (status != NULL) {
    291 		if ((error = copyin(status, &tstat, sizeof tstat)))
    292 			return error;
    293 
    294 		bsd_to_linux_wstat(&tstat);
    295 		return copyout(&tstat, SCARG(uap, status), sizeof tstat);
    296 	}
    297 
    298 	return 0;
    299 }
    300 
    301 int
    302 linux_sys_setresgid(p, v, retval)
    303 	struct proc *p;
    304 	void *v;
    305 	register_t *retval;
    306 {
    307 	struct linux_sys_setresgid_args /* {
    308 		syscallarg(gid_t) rgid;
    309 		syscallarg(gid_t) egid;
    310 		syscallarg(gid_t) sgid;
    311 	} */ *uap = v;
    312 	struct pcred *pc = p->p_cred;
    313 	gid_t rgid, egid, sgid;
    314 	int error;
    315 
    316 	rgid = SCARG(uap, rgid);
    317 	egid = SCARG(uap, egid);
    318 	sgid = SCARG(uap, sgid);
    319 
    320 	/*
    321 	 * Note: These checks are a little different than the NetBSD
    322 	 * setregid(2) call performs.  This precisely follows the
    323 	 * behavior of the Linux kernel.
    324 	 */
    325 	if (rgid != (gid_t)-1 &&
    326 	    rgid != pc->p_rgid &&
    327 	    rgid != pc->pc_ucred->cr_gid &&
    328 	    rgid != pc->p_svgid &&
    329 	    (error = suser(pc->pc_ucred, &p->p_acflag)))
    330 		return (error);
    331 
    332 	if (egid != (gid_t)-1 &&
    333 	    egid != pc->p_rgid &&
    334 	    egid != pc->pc_ucred->cr_gid &&
    335 	    egid != pc->p_svgid &&
    336 	    (error = suser(pc->pc_ucred, &p->p_acflag)))
    337 		return (error);
    338 
    339 	if (sgid != (gid_t)-1 &&
    340 	    sgid != pc->p_rgid &&
    341 	    sgid != pc->pc_ucred->cr_gid &&
    342 	    sgid != pc->p_svgid &&
    343 	    (error = suser(pc->pc_ucred, &p->p_acflag)))
    344 		return (error);
    345 
    346 	/*
    347 	 * Now assign the real, effective, and saved GIDs.
    348 	 * Note that Linux, unlike NetBSD in setregid(2), does not
    349 	 * set the saved UID in this call unless the user specifies
    350 	 * it.
    351 	 */
    352 	if (rgid != (gid_t)-1)
    353 		pc->p_rgid = rgid;
    354 
    355 	if (egid != (gid_t)-1) {
    356 		pc->pc_ucred = crcopy(pc->pc_ucred);
    357 		pc->pc_ucred->cr_gid = egid;
    358 	}
    359 
    360 	if (sgid != (gid_t)-1)
    361 		pc->p_svgid = sgid;
    362 
    363 	if (rgid != (gid_t)-1 && egid != (gid_t)-1 && sgid != (gid_t)-1)
    364 		p->p_flag |= P_SUGID;
    365 	return (0);
    366 }
    367 
    368 int
    369 linux_sys_getresgid(p, v, retval)
    370 	struct proc *p;
    371 	void *v;
    372 	register_t *retval;
    373 {
    374 	struct linux_sys_getresgid_args /* {
    375 		syscallarg(gid_t *) rgid;
    376 		syscallarg(gid_t *) egid;
    377 		syscallarg(gid_t *) sgid;
    378 	} */ *uap = v;
    379 	struct pcred *pc = p->p_cred;
    380 	int error;
    381 
    382 	/*
    383 	 * Linux copies these values out to userspace like so:
    384 	 *
    385 	 *	1. Copy out rgid.
    386 	 *	2. If that succeeds, copy out egid.
    387 	 *	3. If both of those succeed, copy out sgid.
    388 	 */
    389 	if ((error = copyout(&pc->p_rgid, SCARG(uap, rgid),
    390 			     sizeof(gid_t))) != 0)
    391 		return (error);
    392 
    393 	if ((error = copyout(&pc->pc_ucred->cr_uid, SCARG(uap, egid),
    394 			     sizeof(gid_t))) != 0)
    395 		return (error);
    396 
    397 	return (copyout(&pc->p_svgid, SCARG(uap, sgid), sizeof(gid_t)));
    398 }
    399 
    400 /*
    401  * I wonder why Linux has settimeofday() _and_ stime().. Still, we
    402  * need to deal with it.
    403  */
    404 int
    405 linux_sys_stime(p, v, retval)
    406 	struct proc *p;
    407 	void *v;
    408 	register_t *retval;
    409 {
    410 	struct linux_sys_time_args /* {
    411 		linux_time_t *t;
    412 	} */ *uap = v;
    413 	struct timeval atv;
    414 	linux_time_t tt;
    415 	int error;
    416 
    417 	if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
    418 		return (error);
    419 
    420 	if ((error = copyin(&tt, SCARG(uap, t), sizeof tt)) != 0)
    421 		return error;
    422 
    423 	atv.tv_sec = tt;
    424 	atv.tv_usec = 0;
    425 
    426 	if ((error = settime(&atv)))
    427 		return (error);
    428 
    429 	return 0;
    430 }
    431