Home | History | Annotate | Line # | Download | only in common
linux_misc_notalpha.c revision 1.60.2.7
      1 /*	$NetBSD: linux_misc_notalpha.c,v 1.60.2.7 2002/05/29 21:32:41 nathanw 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/cdefs.h>
     41 __KERNEL_RCSID(0, "$NetBSD: linux_misc_notalpha.c,v 1.60.2.7 2002/05/29 21:32:41 nathanw Exp $");
     42 
     43 #include <sys/param.h>
     44 #include <sys/systm.h>
     45 #include <sys/kernel.h>
     46 #include <sys/mman.h>
     47 #include <sys/mount.h>
     48 #include <sys/malloc.h>
     49 #include <sys/mbuf.h>
     50 #include <sys/namei.h>
     51 #include <sys/proc.h>
     52 #include <sys/lwp.h>
     53 #include <sys/ptrace.h>
     54 #include <sys/resource.h>
     55 #include <sys/resourcevar.h>
     56 #include <sys/time.h>
     57 #include <sys/wait.h>
     58 
     59 #include <sys/sa.h>
     60 #include <sys/syscallargs.h>
     61 
     62 #include <compat/linux/common/linux_types.h>
     63 #include <compat/linux/common/linux_fcntl.h>
     64 #include <compat/linux/common/linux_misc.h>
     65 #include <compat/linux/common/linux_mmap.h>
     66 #include <compat/linux/common/linux_signal.h>
     67 #include <compat/linux/common/linux_util.h>
     68 
     69 #include <compat/linux/linux_syscallargs.h>
     70 
     71 /*
     72  * This file contains routines which are used
     73  * on every linux architechture except the Alpha.
     74  */
     75 
     76 /* Used on: arm, i386, m68k, mips, ppc, sparc, sparc64 */
     77 /* Not used on: alpha */
     78 
     79 /*
     80  * Alarm. This is a libc call which uses setitimer(2) in NetBSD.
     81  * Fiddle with the timers to make it work.
     82  */
     83 int
     84 linux_sys_alarm(l, v, retval)
     85 	struct lwp *l;
     86 	void *v;
     87 	register_t *retval;
     88 {
     89 	struct linux_sys_alarm_args /* {
     90 		syscallarg(unsigned int) secs;
     91 	} */ *uap = v;
     92 	struct proc *p = l->l_proc;
     93 	int s;
     94 	struct itimerval *itp, it;
     95 
     96 	if (p->p_timers && p->p_timers[0])
     97 		itp = &p->p_timers[0]->pt_time;
     98 	else
     99 		itp = NULL;
    100 	s = splclock();
    101 	/*
    102 	 * Clear any pending timer alarms.
    103 	 */
    104 	if (itp) {
    105 		callout_stop(&p->p_timers[0]->pt_ch);
    106 		timerclear(&itp->it_interval);
    107 		if (timerisset(&itp->it_value) &&
    108 		    timercmp(&itp->it_value, &time, >))
    109 			timersub(&itp->it_value, &time, &itp->it_value);
    110 		/*
    111 		 * Return how many seconds were left (rounded up)
    112 		 */
    113 		retval[0] = itp->it_value.tv_sec;
    114 		if (itp->it_value.tv_usec)
    115 			retval[0]++;
    116 	} else {
    117 		retval[0] = 0;
    118 	}
    119 
    120 	/*
    121 	 * alarm(0) just resets the timer.
    122 	 */
    123 	if (SCARG(uap, secs) == 0) {
    124 		if (itp)
    125 			timerclear(&itp->it_value);
    126 		splx(s);
    127 		return 0;
    128 	}
    129 
    130 	/*
    131 	 * Check the new alarm time for sanity, and set it.
    132 	 */
    133 	timerclear(&it.it_interval);
    134 	it.it_value.tv_sec = SCARG(uap, secs);
    135 	it.it_value.tv_usec = 0;
    136 	if (itimerfix(&it.it_value) || itimerfix(&it.it_interval)) {
    137 		splx(s);
    138 		return (EINVAL);
    139 	}
    140 
    141 	if (p->p_timers == NULL)
    142 		timers_alloc(p);
    143 	if (p->p_timers[0] == NULL) {
    144 		p->p_timers[0] = pool_get(&ptimer_pool, PR_WAITOK);
    145 		p->p_timers[0]->pt_ev.sigev_notify = SIGEV_SIGNAL;
    146 		p->p_timers[0]->pt_ev.sigev_signo = SIGALRM;
    147 		p->p_timers[0]->pt_type = CLOCK_REALTIME;
    148 		callout_init(&p->p_timers[0]->pt_ch);
    149 	}
    150 
    151 	if (timerisset(&it.it_value)) {
    152 		/*
    153 		 * Don't need to check hzto() return value, here.
    154 		 * callout_reset() does it for us.
    155 		 */
    156 		timeradd(&it.it_value, &time, &it.it_value);
    157 		callout_reset(&p->p_timers[0]->pt_ch, hzto(&it.it_value),
    158 		    realtimerexpire, p->p_timers[0]);
    159 	}
    160 	p->p_timers[0]->pt_time = it;
    161 	splx(s);
    162 
    163 	return 0;
    164 }
    165 
    166 int
    167 linux_sys_nice(l, v, retval)
    168 	struct lwp *l;
    169 	void *v;
    170 	register_t *retval;
    171 {
    172 	struct linux_sys_nice_args /* {
    173 		syscallarg(int) incr;
    174 	} */ *uap = v;
    175         struct sys_setpriority_args bsa;
    176 
    177         SCARG(&bsa, which) = PRIO_PROCESS;
    178         SCARG(&bsa, who) = 0;
    179 	SCARG(&bsa, prio) = SCARG(uap, incr);
    180         return sys_setpriority(l, &bsa, retval);
    181 }
    182 
    183 /*
    184  * The old Linux readdir was only able to read one entry at a time,
    185  * even though it had a 'count' argument. In fact, the emulation
    186  * of the old call was better than the original, because it did handle
    187  * the count arg properly. Don't bother with it anymore now, and use
    188  * it to distinguish between old and new. The difference is that the
    189  * newer one actually does multiple entries, and the reclen field
    190  * really is the reclen, not the namelength.
    191  */
    192 int
    193 linux_sys_readdir(l, v, retval)
    194 	struct lwp *l;
    195 	void *v;
    196 	register_t *retval;
    197 {
    198 	struct linux_sys_readdir_args /* {
    199 		syscallarg(int) fd;
    200 		syscallarg(struct linux_dirent *) dent;
    201 		syscallarg(unsigned int) count;
    202 	} */ *uap = v;
    203 
    204 	SCARG(uap, count) = 1;
    205 	return linux_sys_getdents(l, uap, retval);
    206 }
    207 
    208 /*
    209  * I wonder why Linux has gettimeofday() _and_ time().. Still, we
    210  * need to deal with it.
    211  */
    212 int
    213 linux_sys_time(l, v, retval)
    214 	struct lwp *l;
    215 	void *v;
    216 	register_t *retval;
    217 {
    218 	struct linux_sys_time_args /* {
    219 		linux_time_t *t;
    220 	} */ *uap = v;
    221 	struct timeval atv;
    222 	linux_time_t tt;
    223 	int error;
    224 
    225 	microtime(&atv);
    226 
    227 	tt = atv.tv_sec;
    228 	if (SCARG(uap, t) && (error = copyout(&tt, SCARG(uap, t), sizeof tt)))
    229 		return error;
    230 
    231 	retval[0] = tt;
    232 	return 0;
    233 }
    234 
    235 /*
    236  * utime(). Do conversion to things that utimes() understands,
    237  * and pass it on.
    238  */
    239 int
    240 linux_sys_utime(l, v, retval)
    241 	struct lwp *l;
    242 	void *v;
    243 	register_t *retval;
    244 {
    245 	struct linux_sys_utime_args /* {
    246 		syscallarg(const char *) path;
    247 		syscallarg(struct linux_utimbuf *)times;
    248 	} */ *uap = v;
    249 	struct proc *p = l->l_proc;
    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, 0);
    257 	tvp = (struct timeval *) stackgap_alloc(p, &sg, sizeof(tv));
    258 	CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
    259 
    260 	SCARG(&ua, path) = SCARG(uap, path);
    261 
    262 	if (SCARG(uap, times) != NULL) {
    263 		if ((error = copyin(SCARG(uap, times), &lut, sizeof lut)))
    264 			return error;
    265 		tv[0].tv_usec = tv[1].tv_usec = 0;
    266 		tv[0].tv_sec = lut.l_actime;
    267 		tv[1].tv_sec = lut.l_modtime;
    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(l, &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(l, v, retval)
    285 	struct lwp *l;
    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 proc *p = l->l_proc;
    295 	struct sys_wait4_args w4a;
    296 	int error, *status, tstat;
    297 	caddr_t sg;
    298 
    299 	if (SCARG(uap, status) != NULL) {
    300 		sg = stackgap_init(p, 0);
    301 		status = (int *) stackgap_alloc(p, &sg, sizeof status);
    302 	} else
    303 		status = NULL;
    304 
    305 	SCARG(&w4a, pid) = SCARG(uap, pid);
    306 	SCARG(&w4a, status) = status;
    307 	SCARG(&w4a, options) = SCARG(uap, options);
    308 	SCARG(&w4a, rusage) = NULL;
    309 
    310 	if ((error = sys_wait4(l, &w4a, retval)))
    311 		return error;
    312 
    313 	sigdelset(&p->p_sigctx.ps_siglist, SIGCHLD);
    314 
    315 	if (status != NULL) {
    316 		if ((error = copyin(status, &tstat, sizeof tstat)))
    317 			return error;
    318 
    319 		bsd_to_linux_wstat(&tstat);
    320 		return copyout(&tstat, SCARG(uap, status), sizeof tstat);
    321 	}
    322 
    323 	return 0;
    324 }
    325 
    326 int
    327 linux_sys_setresgid(l, v, retval)
    328 	struct lwp *l;
    329 	void *v;
    330 	register_t *retval;
    331 {
    332 	struct linux_sys_setresgid_args /* {
    333 		syscallarg(gid_t) rgid;
    334 		syscallarg(gid_t) egid;
    335 		syscallarg(gid_t) sgid;
    336 	} */ *uap = v;
    337 	struct proc *p = l->l_proc;
    338 	struct pcred *pc = p->p_cred;
    339 	gid_t rgid, egid, sgid;
    340 	int error;
    341 
    342 	rgid = SCARG(uap, rgid);
    343 	egid = SCARG(uap, egid);
    344 	sgid = SCARG(uap, sgid);
    345 
    346 	/*
    347 	 * Note: These checks are a little different than the NetBSD
    348 	 * setregid(2) call performs.  This precisely follows the
    349 	 * behavior of the Linux kernel.
    350 	 */
    351 	if (rgid != (gid_t)-1 &&
    352 	    rgid != pc->p_rgid &&
    353 	    rgid != pc->pc_ucred->cr_gid &&
    354 	    rgid != pc->p_svgid &&
    355 	    (error = suser(pc->pc_ucred, &p->p_acflag)))
    356 		return (error);
    357 
    358 	if (egid != (gid_t)-1 &&
    359 	    egid != pc->p_rgid &&
    360 	    egid != pc->pc_ucred->cr_gid &&
    361 	    egid != pc->p_svgid &&
    362 	    (error = suser(pc->pc_ucred, &p->p_acflag)))
    363 		return (error);
    364 
    365 	if (sgid != (gid_t)-1 &&
    366 	    sgid != pc->p_rgid &&
    367 	    sgid != pc->pc_ucred->cr_gid &&
    368 	    sgid != pc->p_svgid &&
    369 	    (error = suser(pc->pc_ucred, &p->p_acflag)))
    370 		return (error);
    371 
    372 	/*
    373 	 * Now assign the real, effective, and saved GIDs.
    374 	 * Note that Linux, unlike NetBSD in setregid(2), does not
    375 	 * set the saved UID in this call unless the user specifies
    376 	 * it.
    377 	 */
    378 	if (rgid != (gid_t)-1)
    379 		pc->p_rgid = rgid;
    380 
    381 	if (egid != (gid_t)-1) {
    382 		pc->pc_ucred = crcopy(pc->pc_ucred);
    383 		pc->pc_ucred->cr_gid = egid;
    384 	}
    385 
    386 	if (sgid != (gid_t)-1)
    387 		pc->p_svgid = sgid;
    388 
    389 	if (rgid != (gid_t)-1 && egid != (gid_t)-1 && sgid != (gid_t)-1)
    390 		p->p_flag |= P_SUGID;
    391 	return (0);
    392 }
    393 
    394 int
    395 linux_sys_getresgid(l, v, retval)
    396 	struct lwp *l;
    397 	void *v;
    398 	register_t *retval;
    399 {
    400 	struct linux_sys_getresgid_args /* {
    401 		syscallarg(gid_t *) rgid;
    402 		syscallarg(gid_t *) egid;
    403 		syscallarg(gid_t *) sgid;
    404 	} */ *uap = v;
    405 	struct proc *p = l->l_proc;
    406 	struct pcred *pc = p->p_cred;
    407 	int error;
    408 
    409 	/*
    410 	 * Linux copies these values out to userspace like so:
    411 	 *
    412 	 *	1. Copy out rgid.
    413 	 *	2. If that succeeds, copy out egid.
    414 	 *	3. If both of those succeed, copy out sgid.
    415 	 */
    416 	if ((error = copyout(&pc->p_rgid, SCARG(uap, rgid),
    417 			     sizeof(gid_t))) != 0)
    418 		return (error);
    419 
    420 	if ((error = copyout(&pc->pc_ucred->cr_gid, SCARG(uap, egid),
    421 			     sizeof(gid_t))) != 0)
    422 		return (error);
    423 
    424 	return (copyout(&pc->p_svgid, SCARG(uap, sgid), sizeof(gid_t)));
    425 }
    426 
    427 /*
    428  * I wonder why Linux has settimeofday() _and_ stime().. Still, we
    429  * need to deal with it.
    430  */
    431 int
    432 linux_sys_stime(l, v, retval)
    433 	struct lwp *l;
    434 	void *v;
    435 	register_t *retval;
    436 {
    437 	struct linux_sys_time_args /* {
    438 		linux_time_t *t;
    439 	} */ *uap = v;
    440 	struct proc *p = l->l_proc;
    441 	struct timeval atv;
    442 	linux_time_t tt;
    443 	int error;
    444 
    445 	if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
    446 		return (error);
    447 
    448 	if ((error = copyin(&tt, SCARG(uap, t), sizeof tt)) != 0)
    449 		return error;
    450 
    451 	atv.tv_sec = tt;
    452 	atv.tv_usec = 0;
    453 
    454 	if ((error = settime(&atv)))
    455 		return (error);
    456 
    457 	return 0;
    458 }
    459