Home | History | Annotate | Line # | Download | only in common
linux_misc_notalpha.c revision 1.65
      1 /*	$NetBSD: linux_misc_notalpha.c,v 1.65 2003/02/19 11:23:54 jdolecek 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.65 2003/02/19 11:23:54 jdolecek 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/ptrace.h>
     53 #include <sys/resource.h>
     54 #include <sys/resourcevar.h>
     55 #include <sys/time.h>
     56 #include <sys/wait.h>
     57 
     58 #include <sys/sa.h>
     59 #include <sys/syscallargs.h>
     60 
     61 #include <compat/linux/common/linux_types.h>
     62 #include <compat/linux/common/linux_fcntl.h>
     63 #include <compat/linux/common/linux_misc.h>
     64 #include <compat/linux/common/linux_mmap.h>
     65 #include <compat/linux/common/linux_signal.h>
     66 #include <compat/linux/common/linux_util.h>
     67 
     68 #include <compat/linux/linux_syscallargs.h>
     69 
     70 /*
     71  * This file contains routines which are used
     72  * on every linux architechture except the Alpha.
     73  */
     74 
     75 /* Used on: arm, i386, m68k, mips, ppc, sparc, sparc64 */
     76 /* Not used on: alpha */
     77 
     78 /*
     79  * Alarm. This is a libc call which uses setitimer(2) in NetBSD.
     80  * Fiddle with the timers to make it work.
     81  */
     82 int
     83 linux_sys_alarm(l, v, retval)
     84 	struct lwp *l;
     85 	void *v;
     86 	register_t *retval;
     87 {
     88 	struct linux_sys_alarm_args /* {
     89 		syscallarg(unsigned int) secs;
     90 	} */ *uap = v;
     91 	struct proc *p = l->l_proc;
     92 	int s;
     93 	struct itimerval *itp, it;
     94 	struct ptimer *ptp;
     95 
     96 	if (p->p_timers && p->p_timers->pts_timers[ITIMER_REAL])
     97 		itp = &p->p_timers->pts_timers[ITIMER_REAL]->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->pts_timers[ITIMER_REAL]->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 	ptp = p->p_timers->pts_timers[ITIMER_REAL];
    144 	if (ptp == NULL) {
    145 		ptp = pool_get(&ptimer_pool, PR_WAITOK);
    146 		ptp->pt_ev.sigev_notify = SIGEV_SIGNAL;
    147 		ptp->pt_ev.sigev_signo = SIGALRM;
    148 		ptp->pt_overruns = 0;
    149 		ptp->pt_proc = p;
    150 		ptp->pt_type = CLOCK_REALTIME;
    151 		ptp->pt_entry = CLOCK_REALTIME;
    152 		callout_init(&ptp->pt_ch);
    153 	}
    154 
    155 	if (timerisset(&it.it_value)) {
    156 		/*
    157 		 * Don't need to check hzto() return value, here.
    158 		 * callout_reset() does it for us.
    159 		 */
    160 		timeradd(&it.it_value, &time, &it.it_value);
    161 		callout_reset(&ptp->pt_ch, hzto(&it.it_value),
    162 		    realtimerexpire, ptp);
    163 	}
    164 	ptp->pt_time = it;
    165 	splx(s);
    166 
    167 	return 0;
    168 }
    169 
    170 int
    171 linux_sys_nice(l, v, retval)
    172 	struct lwp *l;
    173 	void *v;
    174 	register_t *retval;
    175 {
    176 	struct linux_sys_nice_args /* {
    177 		syscallarg(int) incr;
    178 	} */ *uap = v;
    179         struct sys_setpriority_args bsa;
    180 
    181         SCARG(&bsa, which) = PRIO_PROCESS;
    182         SCARG(&bsa, who) = 0;
    183 	SCARG(&bsa, prio) = SCARG(uap, incr);
    184         return sys_setpriority(l, &bsa, retval);
    185 }
    186 
    187 /*
    188  * The old Linux readdir was only able to read one entry at a time,
    189  * even though it had a 'count' argument. In fact, the emulation
    190  * of the old call was better than the original, because it did handle
    191  * the count arg properly. Don't bother with it anymore now, and use
    192  * it to distinguish between old and new. The difference is that the
    193  * newer one actually does multiple entries, and the reclen field
    194  * really is the reclen, not the namelength.
    195  */
    196 int
    197 linux_sys_readdir(l, v, retval)
    198 	struct lwp *l;
    199 	void *v;
    200 	register_t *retval;
    201 {
    202 	struct linux_sys_readdir_args /* {
    203 		syscallarg(int) fd;
    204 		syscallarg(struct linux_dirent *) dent;
    205 		syscallarg(unsigned int) count;
    206 	} */ *uap = v;
    207 
    208 	SCARG(uap, count) = 1;
    209 	return linux_sys_getdents(l, uap, retval);
    210 }
    211 
    212 /*
    213  * I wonder why Linux has gettimeofday() _and_ time().. Still, we
    214  * need to deal with it.
    215  */
    216 int
    217 linux_sys_time(l, v, retval)
    218 	struct lwp *l;
    219 	void *v;
    220 	register_t *retval;
    221 {
    222 	struct linux_sys_time_args /* {
    223 		linux_time_t *t;
    224 	} */ *uap = v;
    225 	struct timeval atv;
    226 	linux_time_t tt;
    227 	int error;
    228 
    229 	microtime(&atv);
    230 
    231 	tt = atv.tv_sec;
    232 	if (SCARG(uap, t) && (error = copyout(&tt, SCARG(uap, t), sizeof tt)))
    233 		return error;
    234 
    235 	retval[0] = tt;
    236 	return 0;
    237 }
    238 
    239 /*
    240  * utime(). Do conversion to things that utimes() understands,
    241  * and pass it on.
    242  */
    243 int
    244 linux_sys_utime(l, v, retval)
    245 	struct lwp *l;
    246 	void *v;
    247 	register_t *retval;
    248 {
    249 	struct linux_sys_utime_args /* {
    250 		syscallarg(const char *) path;
    251 		syscallarg(struct linux_utimbuf *)times;
    252 	} */ *uap = v;
    253 	struct proc *p = l->l_proc;
    254 	caddr_t sg;
    255 	int error;
    256 	struct sys_utimes_args ua;
    257 	struct timeval tv[2], *tvp;
    258 	struct linux_utimbuf lut;
    259 
    260 	sg = stackgap_init(p, 0);
    261 	tvp = (struct timeval *) stackgap_alloc(p, &sg, sizeof(tv));
    262 	CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
    263 
    264 	SCARG(&ua, path) = SCARG(uap, path);
    265 
    266 	if (SCARG(uap, times) != NULL) {
    267 		if ((error = copyin(SCARG(uap, times), &lut, sizeof lut)))
    268 			return error;
    269 		tv[0].tv_usec = tv[1].tv_usec = 0;
    270 		tv[0].tv_sec = lut.l_actime;
    271 		tv[1].tv_sec = lut.l_modtime;
    272 		if ((error = copyout(tv, tvp, sizeof tv)))
    273 			return error;
    274 		SCARG(&ua, tptr) = tvp;
    275 	}
    276 	else
    277 		SCARG(&ua, tptr) = NULL;
    278 
    279 	return sys_utimes(l, &ua, retval);
    280 }
    281 
    282 /*
    283  * waitpid(2). Passed on to the NetBSD call, surrounded by code to
    284  * reserve some space for a NetBSD-style wait status, and converting
    285  * it to what Linux wants.
    286  */
    287 int
    288 linux_sys_waitpid(l, v, retval)
    289 	struct lwp *l;
    290 	void *v;
    291 	register_t *retval;
    292 {
    293 	struct linux_sys_waitpid_args /* {
    294 		syscallarg(int) pid;
    295 		syscallarg(int *) status;
    296 		syscallarg(int) options;
    297 	} */ *uap = v;
    298 	struct proc *p = l->l_proc;
    299 	struct sys_wait4_args w4a;
    300 	int error, *status, tstat;
    301 	caddr_t sg;
    302 
    303 	if (SCARG(uap, status) != NULL) {
    304 		sg = stackgap_init(p, 0);
    305 		status = (int *) stackgap_alloc(p, &sg, sizeof status);
    306 	} else
    307 		status = NULL;
    308 
    309 	SCARG(&w4a, pid) = SCARG(uap, pid);
    310 	SCARG(&w4a, status) = status;
    311 	SCARG(&w4a, options) = SCARG(uap, options);
    312 	SCARG(&w4a, rusage) = NULL;
    313 
    314 	if ((error = sys_wait4(l, &w4a, retval)))
    315 		return error;
    316 
    317 	sigdelset(&p->p_sigctx.ps_siglist, SIGCHLD);
    318 
    319 	if (status != NULL) {
    320 		if ((error = copyin(status, &tstat, sizeof tstat)))
    321 			return error;
    322 
    323 		bsd_to_linux_wstat(&tstat);
    324 		return copyout(&tstat, SCARG(uap, status), sizeof tstat);
    325 	}
    326 
    327 	return 0;
    328 }
    329 
    330 int
    331 linux_sys_setresgid(l, v, retval)
    332 	struct lwp *l;
    333 	void *v;
    334 	register_t *retval;
    335 {
    336 	struct linux_sys_setresgid_args /* {
    337 		syscallarg(gid_t) rgid;
    338 		syscallarg(gid_t) egid;
    339 		syscallarg(gid_t) sgid;
    340 	} */ *uap = v;
    341 	struct proc *p = l->l_proc;
    342 	struct pcred *pc = p->p_cred;
    343 	gid_t rgid, egid, sgid;
    344 	int error;
    345 
    346 	rgid = SCARG(uap, rgid);
    347 	egid = SCARG(uap, egid);
    348 	sgid = SCARG(uap, sgid);
    349 
    350 	/*
    351 	 * Note: These checks are a little different than the NetBSD
    352 	 * setregid(2) call performs.  This precisely follows the
    353 	 * behavior of the Linux kernel.
    354 	 */
    355 	if (rgid != (gid_t)-1 &&
    356 	    rgid != pc->p_rgid &&
    357 	    rgid != pc->pc_ucred->cr_gid &&
    358 	    rgid != pc->p_svgid &&
    359 	    (error = suser(pc->pc_ucred, &p->p_acflag)))
    360 		return (error);
    361 
    362 	if (egid != (gid_t)-1 &&
    363 	    egid != pc->p_rgid &&
    364 	    egid != pc->pc_ucred->cr_gid &&
    365 	    egid != pc->p_svgid &&
    366 	    (error = suser(pc->pc_ucred, &p->p_acflag)))
    367 		return (error);
    368 
    369 	if (sgid != (gid_t)-1 &&
    370 	    sgid != pc->p_rgid &&
    371 	    sgid != pc->pc_ucred->cr_gid &&
    372 	    sgid != pc->p_svgid &&
    373 	    (error = suser(pc->pc_ucred, &p->p_acflag)))
    374 		return (error);
    375 
    376 	/*
    377 	 * Now assign the real, effective, and saved GIDs.
    378 	 * Note that Linux, unlike NetBSD in setregid(2), does not
    379 	 * set the saved UID in this call unless the user specifies
    380 	 * it.
    381 	 */
    382 	if (rgid != (gid_t)-1)
    383 		pc->p_rgid = rgid;
    384 
    385 	if (egid != (gid_t)-1) {
    386 		pc->pc_ucred = crcopy(pc->pc_ucred);
    387 		pc->pc_ucred->cr_gid = egid;
    388 	}
    389 
    390 	if (sgid != (gid_t)-1)
    391 		pc->p_svgid = sgid;
    392 
    393 	if (rgid != (gid_t)-1 && egid != (gid_t)-1 && sgid != (gid_t)-1)
    394 		p->p_flag |= P_SUGID;
    395 	return (0);
    396 }
    397 
    398 int
    399 linux_sys_getresgid(l, v, retval)
    400 	struct lwp *l;
    401 	void *v;
    402 	register_t *retval;
    403 {
    404 	struct linux_sys_getresgid_args /* {
    405 		syscallarg(gid_t *) rgid;
    406 		syscallarg(gid_t *) egid;
    407 		syscallarg(gid_t *) sgid;
    408 	} */ *uap = v;
    409 	struct proc *p = l->l_proc;
    410 	struct pcred *pc = p->p_cred;
    411 	int error;
    412 
    413 	/*
    414 	 * Linux copies these values out to userspace like so:
    415 	 *
    416 	 *	1. Copy out rgid.
    417 	 *	2. If that succeeds, copy out egid.
    418 	 *	3. If both of those succeed, copy out sgid.
    419 	 */
    420 	if ((error = copyout(&pc->p_rgid, SCARG(uap, rgid),
    421 			     sizeof(gid_t))) != 0)
    422 		return (error);
    423 
    424 	if ((error = copyout(&pc->pc_ucred->cr_gid, SCARG(uap, egid),
    425 			     sizeof(gid_t))) != 0)
    426 		return (error);
    427 
    428 	return (copyout(&pc->p_svgid, SCARG(uap, sgid), sizeof(gid_t)));
    429 }
    430 
    431 /*
    432  * I wonder why Linux has settimeofday() _and_ stime().. Still, we
    433  * need to deal with it.
    434  */
    435 int
    436 linux_sys_stime(l, v, retval)
    437 	struct lwp *l;
    438 	void *v;
    439 	register_t *retval;
    440 {
    441 	struct linux_sys_time_args /* {
    442 		linux_time_t *t;
    443 	} */ *uap = v;
    444 	struct proc *p = l->l_proc;
    445 	struct timeval atv;
    446 	linux_time_t tt;
    447 	int error;
    448 
    449 	if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
    450 		return (error);
    451 
    452 	if ((error = copyin(&tt, SCARG(uap, t), sizeof tt)) != 0)
    453 		return error;
    454 
    455 	atv.tv_sec = tt;
    456 	atv.tv_usec = 0;
    457 
    458 	if ((error = settime(&atv)))
    459 		return (error);
    460 
    461 	return 0;
    462 }
    463