Home | History | Annotate | Line # | Download | only in common
linux_misc_notalpha.c revision 1.79
      1 /*	$NetBSD: linux_misc_notalpha.c,v 1.79 2006/05/14 21:24:50 elad 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.79 2006/05/14 21:24:50 elad 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 #include <sys/kauth.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 #ifdef DEBUG_LINUX
     80 #define DPRINTF(a)	uprintf a
     81 #else
     82 #define DPRINTF(a)
     83 #endif
     84 
     85 #ifndef COMPAT_LINUX32
     86 #if !defined(__m68k__)
     87 static void bsd_to_linux_statfs64(const struct statvfs *,
     88 	struct linux_statfs64  *);
     89 #endif
     90 
     91 /*
     92  * Alarm. This is a libc call which uses setitimer(2) in NetBSD.
     93  * Fiddle with the timers to make it work.
     94  */
     95 int
     96 linux_sys_alarm(l, v, retval)
     97 	struct lwp *l;
     98 	void *v;
     99 	register_t *retval;
    100 {
    101 	struct linux_sys_alarm_args /* {
    102 		syscallarg(unsigned int) secs;
    103 	} */ *uap = v;
    104 	struct proc *p = l->l_proc;
    105 	int s;
    106 	struct itimerval *itp, it;
    107 	struct ptimer *ptp;
    108 
    109 	if (p->p_timers && p->p_timers->pts_timers[ITIMER_REAL])
    110 		itp = &p->p_timers->pts_timers[ITIMER_REAL]->pt_time;
    111 	else
    112 		itp = NULL;
    113 	s = splclock();
    114 	/*
    115 	 * Clear any pending timer alarms.
    116 	 */
    117 	if (itp) {
    118 		callout_stop(&p->p_timers->pts_timers[ITIMER_REAL]->pt_ch);
    119 		timerclear(&itp->it_interval);
    120 		if (timerisset(&itp->it_value) &&
    121 		    timercmp(&itp->it_value, &time, >))
    122 			timersub(&itp->it_value, &time, &itp->it_value);
    123 		/*
    124 		 * Return how many seconds were left (rounded up)
    125 		 */
    126 		retval[0] = itp->it_value.tv_sec;
    127 		if (itp->it_value.tv_usec)
    128 			retval[0]++;
    129 	} else {
    130 		retval[0] = 0;
    131 	}
    132 
    133 	/*
    134 	 * alarm(0) just resets the timer.
    135 	 */
    136 	if (SCARG(uap, secs) == 0) {
    137 		if (itp)
    138 			timerclear(&itp->it_value);
    139 		splx(s);
    140 		return 0;
    141 	}
    142 
    143 	/*
    144 	 * Check the new alarm time for sanity, and set it.
    145 	 */
    146 	timerclear(&it.it_interval);
    147 	it.it_value.tv_sec = SCARG(uap, secs);
    148 	it.it_value.tv_usec = 0;
    149 	if (itimerfix(&it.it_value) || itimerfix(&it.it_interval)) {
    150 		splx(s);
    151 		return (EINVAL);
    152 	}
    153 
    154 	if (p->p_timers == NULL)
    155 		timers_alloc(p);
    156 	ptp = p->p_timers->pts_timers[ITIMER_REAL];
    157 	if (ptp == NULL) {
    158 		ptp = pool_get(&ptimer_pool, PR_WAITOK);
    159 		ptp->pt_ev.sigev_notify = SIGEV_SIGNAL;
    160 		ptp->pt_ev.sigev_signo = SIGALRM;
    161 		ptp->pt_overruns = 0;
    162 		ptp->pt_proc = p;
    163 		ptp->pt_type = CLOCK_REALTIME;
    164 		ptp->pt_entry = CLOCK_REALTIME;
    165 		callout_init(&ptp->pt_ch);
    166 		p->p_timers->pts_timers[ITIMER_REAL] = ptp;
    167 	}
    168 
    169 	if (timerisset(&it.it_value)) {
    170 		/*
    171 		 * Don't need to check hzto() return value, here.
    172 		 * callout_reset() does it for us.
    173 		 */
    174 		timeradd(&it.it_value, &time, &it.it_value);
    175 		callout_reset(&ptp->pt_ch, hzto(&it.it_value),
    176 		    realtimerexpire, ptp);
    177 	}
    178 	ptp->pt_time = it;
    179 	splx(s);
    180 
    181 	return 0;
    182 }
    183 #endif /* !COMPAT_LINUX32 */
    184 
    185 #if !defined(__amd64__) || defined(COMPAT_LINUX32)
    186 int
    187 linux_sys_nice(l, v, retval)
    188 	struct lwp *l;
    189 	void *v;
    190 	register_t *retval;
    191 {
    192 	struct linux_sys_nice_args /* {
    193 		syscallarg(int) incr;
    194 	} */ *uap = v;
    195         struct sys_setpriority_args bsa;
    196 
    197         SCARG(&bsa, which) = PRIO_PROCESS;
    198         SCARG(&bsa, who) = 0;
    199 	SCARG(&bsa, prio) = SCARG(uap, incr);
    200         return sys_setpriority(l, &bsa, retval);
    201 }
    202 #endif /* !__amd64__ || COMPAT_LINUX32 */
    203 
    204 #ifndef COMPAT_LINUX32
    205 #ifndef __amd64__
    206 /*
    207  * The old Linux readdir was only able to read one entry at a time,
    208  * even though it had a 'count' argument. In fact, the emulation
    209  * of the old call was better than the original, because it did handle
    210  * the count arg properly. Don't bother with it anymore now, and use
    211  * it to distinguish between old and new. The difference is that the
    212  * newer one actually does multiple entries, and the reclen field
    213  * really is the reclen, not the namelength.
    214  */
    215 int
    216 linux_sys_readdir(l, v, retval)
    217 	struct lwp *l;
    218 	void *v;
    219 	register_t *retval;
    220 {
    221 	struct linux_sys_readdir_args /* {
    222 		syscallarg(int) fd;
    223 		syscallarg(struct linux_dirent *) dent;
    224 		syscallarg(unsigned int) count;
    225 	} */ *uap = v;
    226 
    227 	SCARG(uap, count) = 1;
    228 	return linux_sys_getdents(l, uap, retval);
    229 }
    230 #endif /* !amd64 */
    231 
    232 /*
    233  * I wonder why Linux has gettimeofday() _and_ time().. Still, we
    234  * need to deal with it.
    235  */
    236 int
    237 linux_sys_time(l, v, retval)
    238 	struct lwp *l;
    239 	void *v;
    240 	register_t *retval;
    241 {
    242 	struct linux_sys_time_args /* {
    243 		linux_time_t *t;
    244 	} */ *uap = v;
    245 	struct timeval atv;
    246 	linux_time_t tt;
    247 	int error;
    248 
    249 	microtime(&atv);
    250 
    251 	tt = atv.tv_sec;
    252 	if (SCARG(uap, t) && (error = copyout(&tt, SCARG(uap, t), sizeof tt)))
    253 		return error;
    254 
    255 	retval[0] = tt;
    256 	return 0;
    257 }
    258 
    259 /*
    260  * utime(). Do conversion to things that utimes() understands,
    261  * and pass it on.
    262  */
    263 int
    264 linux_sys_utime(l, v, retval)
    265 	struct lwp *l;
    266 	void *v;
    267 	register_t *retval;
    268 {
    269 	struct linux_sys_utime_args /* {
    270 		syscallarg(const char *) path;
    271 		syscallarg(struct linux_utimbuf *)times;
    272 	} */ *uap = v;
    273 	struct proc *p = l->l_proc;
    274 	caddr_t sg;
    275 	int error;
    276 	struct sys_utimes_args ua;
    277 	struct timeval tv[2], *tvp;
    278 	struct linux_utimbuf lut;
    279 
    280 	sg = stackgap_init(p, 0);
    281 	tvp = (struct timeval *) stackgap_alloc(p, &sg, sizeof(tv));
    282 	CHECK_ALT_EXIST(l, &sg, SCARG(uap, path));
    283 
    284 	SCARG(&ua, path) = SCARG(uap, path);
    285 
    286 	if (SCARG(uap, times) != NULL) {
    287 		if ((error = copyin(SCARG(uap, times), &lut, sizeof lut)))
    288 			return error;
    289 		tv[0].tv_usec = tv[1].tv_usec = 0;
    290 		tv[0].tv_sec = lut.l_actime;
    291 		tv[1].tv_sec = lut.l_modtime;
    292 		if ((error = copyout(tv, tvp, sizeof tv)))
    293 			return error;
    294 		SCARG(&ua, tptr) = tvp;
    295 	}
    296 	else
    297 		SCARG(&ua, tptr) = NULL;
    298 
    299 	return sys_utimes(l, &ua, retval);
    300 }
    301 
    302 #ifndef __amd64__
    303 /*
    304  * waitpid(2).  Just forward on to linux_sys_wait4 with a NULL rusage.
    305  */
    306 int
    307 linux_sys_waitpid(l, v, retval)
    308 	struct lwp *l;
    309 	void *v;
    310 	register_t *retval;
    311 {
    312 	struct linux_sys_waitpid_args /* {
    313 		syscallarg(int) pid;
    314 		syscallarg(int *) status;
    315 		syscallarg(int) options;
    316 	} */ *uap = v;
    317 	struct linux_sys_wait4_args linux_w4a;
    318 
    319 	SCARG(&linux_w4a, pid) = SCARG(uap, pid);
    320 	SCARG(&linux_w4a, status) = SCARG(uap, status);
    321 	SCARG(&linux_w4a, options) = SCARG(uap, options);
    322 	SCARG(&linux_w4a, rusage) = NULL;
    323 
    324 	return linux_sys_wait4(l, &linux_w4a, retval);
    325 }
    326 #endif /* !amd64 */
    327 
    328 int
    329 linux_sys_setresgid(l, v, retval)
    330 	struct lwp *l;
    331 	void *v;
    332 	register_t *retval;
    333 {
    334 	struct linux_sys_setresgid_args /* {
    335 		syscallarg(gid_t) rgid;
    336 		syscallarg(gid_t) egid;
    337 		syscallarg(gid_t) sgid;
    338 	} */ *uap = v;
    339 
    340 	/*
    341 	 * Note: These checks are a little different than the NetBSD
    342 	 * setregid(2) call performs.  This precisely follows the
    343 	 * behavior of the Linux kernel.
    344 	 */
    345 	return do_setresgid(l, SCARG(uap,rgid), SCARG(uap, egid),
    346 			    SCARG(uap, sgid),
    347 			    ID_R_EQ_R | ID_R_EQ_E | ID_R_EQ_S |
    348 			    ID_E_EQ_R | ID_E_EQ_E | ID_E_EQ_S |
    349 			    ID_S_EQ_R | ID_S_EQ_E | ID_S_EQ_S );
    350 }
    351 
    352 int
    353 linux_sys_getresgid(l, v, retval)
    354 	struct lwp *l;
    355 	void *v;
    356 	register_t *retval;
    357 {
    358 	struct linux_sys_getresgid_args /* {
    359 		syscallarg(gid_t *) rgid;
    360 		syscallarg(gid_t *) egid;
    361 		syscallarg(gid_t *) sgid;
    362 	} */ *uap = v;
    363 	struct proc *p = l->l_proc;
    364 	kauth_cred_t pc = p->p_cred;
    365 	int error;
    366 	gid_t gid;
    367 
    368 	/*
    369 	 * Linux copies these values out to userspace like so:
    370 	 *
    371 	 *	1. Copy out rgid.
    372 	 *	2. If that succeeds, copy out egid.
    373 	 *	3. If both of those succeed, copy out sgid.
    374 	 */
    375 	gid = kauth_cred_getgid(pc);
    376 	if ((error = copyout(&gid, SCARG(uap, rgid), sizeof(gid_t))) != 0)
    377 		return (error);
    378 
    379 	gid = kauth_cred_getegid(pc);
    380 	if ((error = copyout(&gid, SCARG(uap, egid), sizeof(gid_t))) != 0)
    381 		return (error);
    382 
    383 	gid = kauth_cred_getsvgid(pc);
    384 
    385 	return (copyout(&gid, SCARG(uap, sgid), sizeof(gid_t)));
    386 }
    387 
    388 #ifndef __amd64__
    389 /*
    390  * I wonder why Linux has settimeofday() _and_ stime().. Still, we
    391  * need to deal with it.
    392  */
    393 int
    394 linux_sys_stime(l, v, retval)
    395 	struct lwp *l;
    396 	void *v;
    397 	register_t *retval;
    398 {
    399 	struct linux_sys_time_args /* {
    400 		linux_time_t *t;
    401 	} */ *uap = v;
    402 	struct proc *p = l->l_proc;
    403 	struct timespec ats;
    404 	linux_time_t tt;
    405 	int error;
    406 
    407 	if ((error = kauth_authorize_generic(p->p_cred, KAUTH_GENERIC_ISSUSER, &p->p_acflag)) != 0)
    408 		return (error);
    409 
    410 	if ((error = copyin(&tt, SCARG(uap, t), sizeof tt)) != 0)
    411 		return error;
    412 
    413 	ats.tv_sec = tt;
    414 	ats.tv_nsec = 0;
    415 
    416 	if ((error = settime(p, &ats)))
    417 		return (error);
    418 
    419 	return 0;
    420 }
    421 #endif /* !amd64 */
    422 
    423 #if !defined(__m68k__)
    424 /*
    425  * Convert NetBSD statvfs structure to Linux statfs64 structure.
    426  * See comments in bsd_to_linux_statfs() for further background.
    427  * We can safely pass correct bsize and frsize here, since Linux glibc
    428  * statvfs() doesn't use statfs64().
    429  */
    430 static void
    431 bsd_to_linux_statfs64(bsp, lsp)
    432 	const struct statvfs *bsp;
    433 	struct linux_statfs64 *lsp;
    434 {
    435 	int i, div;
    436 
    437 	for (i = 0; i < linux_fstypes_cnt; i++) {
    438 		if (strcmp(bsp->f_fstypename, linux_fstypes[i].bsd) == 0) {
    439 			lsp->l_ftype = linux_fstypes[i].linux;
    440 			break;
    441 		}
    442 	}
    443 
    444 	if (i == linux_fstypes_cnt) {
    445 		DPRINTF(("unhandled fstype in linux emulation: %s\n",
    446 		    bsp->f_fstypename));
    447 		lsp->l_ftype = LINUX_DEFAULT_SUPER_MAGIC;
    448 	}
    449 
    450 	div = bsp->f_bsize / bsp->f_frsize;
    451 	lsp->l_fbsize = bsp->f_bsize;
    452 	lsp->l_ffrsize = bsp->f_frsize;
    453 	lsp->l_fblocks = bsp->f_blocks / div;
    454 	lsp->l_fbfree = bsp->f_bfree / div;
    455 	lsp->l_fbavail = bsp->f_bavail / div;
    456 	lsp->l_ffiles = bsp->f_files;
    457 	lsp->l_fffree = bsp->f_ffree / div;
    458 	/* Linux sets the fsid to 0..., we don't */
    459 	lsp->l_ffsid.val[0] = bsp->f_fsidx.__fsid_val[0];
    460 	lsp->l_ffsid.val[1] = bsp->f_fsidx.__fsid_val[1];
    461 	lsp->l_fnamelen = bsp->f_namemax;
    462 	(void)memset(lsp->l_fspare, 0, sizeof(lsp->l_fspare));
    463 }
    464 
    465 /*
    466  * Implement the fs stat functions. Straightforward.
    467  */
    468 int
    469 linux_sys_statfs64(l, v, retval)
    470 	struct lwp *l;
    471 	void *v;
    472 	register_t *retval;
    473 {
    474 	struct linux_sys_statfs64_args /* {
    475 		syscallarg(const char *) path;
    476 		syscallarg(size_t) sz;
    477 		syscallarg(struct linux_statfs64 *) sp;
    478 	} */ *uap = v;
    479 	struct proc *p = l->l_proc;
    480 	struct statvfs *btmp, *bsp;
    481 	struct linux_statfs64 ltmp;
    482 	struct sys_statvfs1_args bsa;
    483 	caddr_t sg;
    484 	int error;
    485 
    486 	if (SCARG(uap, sz) != sizeof ltmp)
    487 		return (EINVAL);
    488 
    489 	sg = stackgap_init(p, 0);
    490 	bsp = stackgap_alloc(p, &sg, sizeof (struct statvfs));
    491 
    492 	CHECK_ALT_EXIST(l, &sg, SCARG(uap, path));
    493 
    494 	SCARG(&bsa, path) = SCARG(uap, path);
    495 	SCARG(&bsa, buf) = bsp;
    496 	SCARG(&bsa, flags) = ST_WAIT;
    497 
    498 	if ((error = sys_statvfs1(l, &bsa, retval)))
    499 		return error;
    500 
    501 	btmp = STATVFSBUF_GET();
    502 	error = copyin(bsp, btmp, sizeof(*btmp));
    503 	if (error) {
    504 		goto out;
    505 	}
    506 	bsd_to_linux_statfs64(btmp, &ltmp);
    507 	error = copyout(&ltmp, SCARG(uap, sp), sizeof ltmp);
    508 out:
    509 	STATVFSBUF_PUT(btmp);
    510 	return error;
    511 }
    512 
    513 int
    514 linux_sys_fstatfs64(l, v, retval)
    515 	struct lwp *l;
    516 	void *v;
    517 	register_t *retval;
    518 {
    519 	struct linux_sys_fstatfs64_args /* {
    520 		syscallarg(int) fd;
    521 		syscallarg(size_t) sz;
    522 		syscallarg(struct linux_statfs64 *) sp;
    523 	} */ *uap = v;
    524 	struct proc *p = l->l_proc;
    525 	struct statvfs *btmp, *bsp;
    526 	struct linux_statfs64 ltmp;
    527 	struct sys_fstatvfs1_args bsa;
    528 	caddr_t sg;
    529 	int error;
    530 
    531 	if (SCARG(uap, sz) != sizeof ltmp)
    532 		return (EINVAL);
    533 
    534 	sg = stackgap_init(p, 0);
    535 	bsp = stackgap_alloc(p, &sg, sizeof (struct statvfs));
    536 
    537 	SCARG(&bsa, fd) = SCARG(uap, fd);
    538 	SCARG(&bsa, buf) = bsp;
    539 	SCARG(&bsa, flags) = ST_WAIT;
    540 
    541 	if ((error = sys_fstatvfs1(l, &bsa, retval)))
    542 		return error;
    543 
    544 	btmp = STATVFSBUF_GET();
    545 	error = copyin(bsp, btmp, sizeof(*btmp));
    546 	if (error) {
    547 		goto out;
    548 	}
    549 	bsd_to_linux_statfs64(btmp, &ltmp);
    550 	error = copyout(&ltmp, SCARG(uap, sp), sizeof ltmp);
    551 out:
    552 	STATVFSBUF_PUT(btmp);
    553 	return error;
    554 }
    555 #endif /* !__m68k__ */
    556 #endif /* !COMPAT_LINUX32 */
    557