Home | History | Annotate | Line # | Download | only in common
linux_misc_notalpha.c revision 1.106.4.1
      1 /*	$NetBSD: linux_misc_notalpha.c,v 1.106.4.1 2011/03/05 20:52:49 rmind Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1995, 1998, 2008 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  *
     20  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     22  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     23  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     24  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     30  * POSSIBILITY OF SUCH DAMAGE.
     31  */
     32 
     33 #include <sys/cdefs.h>
     34 __KERNEL_RCSID(0, "$NetBSD: linux_misc_notalpha.c,v 1.106.4.1 2011/03/05 20:52:49 rmind Exp $");
     35 
     36 /*
     37  * Note that we must NOT include "opt_compat_linux32.h" here,
     38  * the maze of ifdefs below relies on COMPAT_LINUX32 only being
     39  * defined when this file is built for linux32.
     40  */
     41 
     42 #include <sys/param.h>
     43 #include <sys/systm.h>
     44 #include <sys/kernel.h>
     45 #include <sys/mman.h>
     46 #include <sys/mount.h>
     47 #include <sys/malloc.h>
     48 #include <sys/mbuf.h>
     49 #include <sys/namei.h>
     50 #include <sys/proc.h>
     51 #include <sys/prot.h>
     52 #include <sys/ptrace.h>
     53 #include <sys/resource.h>
     54 #include <sys/resourcevar.h>
     55 #include <sys/time.h>
     56 #include <sys/vfs_syscalls.h>
     57 #include <sys/wait.h>
     58 #include <sys/kauth.h>
     59 
     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 #include <compat/linux/common/linux_ipc.h>
     69 #include <compat/linux/common/linux_sem.h>
     70 #include <compat/linux/common/linux_statfs.h>
     71 
     72 #include <compat/linux/linux_syscallargs.h>
     73 
     74 /*
     75  * This file contains routines which are used
     76  * on every linux architechture except the Alpha.
     77  */
     78 
     79 /* Used on: arm, i386, m68k, mips, ppc, sparc, sparc64 */
     80 /* Not used on: alpha */
     81 
     82 #ifdef DEBUG_LINUX
     83 #define DPRINTF(a)	uprintf a
     84 #else
     85 #define DPRINTF(a)
     86 #endif
     87 
     88 #ifndef COMPAT_LINUX32
     89 
     90 /*
     91  * Alarm. This is a libc call which uses setitimer(2) in NetBSD.
     92  * Fiddle with the timers to make it work.
     93  *
     94  * XXX This shouldn't be dicking about with the ptimer stuff directly.
     95  */
     96 int
     97 linux_sys_alarm(struct lwp *l, const struct linux_sys_alarm_args *uap, register_t *retval)
     98 {
     99 	/* {
    100 		syscallarg(unsigned int) secs;
    101 	} */
    102 	struct proc *p = l->l_proc;
    103 	struct timespec now;
    104 	struct itimerspec *itp, it;
    105 	struct ptimer *ptp, *spare;
    106 	extern kmutex_t timer_lock;
    107 	struct ptimers *pts;
    108 
    109 	if ((pts = p->p_timers) == NULL)
    110 		pts = timers_alloc(p);
    111 	spare = NULL;
    112 
    113  retry:
    114 	mutex_spin_enter(&timer_lock);
    115 	if (pts && pts->pts_timers[ITIMER_REAL])
    116 		itp = &pts->pts_timers[ITIMER_REAL]->pt_time;
    117 	else
    118 		itp = NULL;
    119 	/*
    120 	 * Clear any pending timer alarms.
    121 	 */
    122 	if (itp) {
    123 		callout_stop(&pts->pts_timers[ITIMER_REAL]->pt_ch);
    124 		timespecclear(&itp->it_interval);
    125 		getnanotime(&now);
    126 		if (timespecisset(&itp->it_value) &&
    127 		    timespeccmp(&itp->it_value, &now, >))
    128 			timespecsub(&itp->it_value, &now, &itp->it_value);
    129 		/*
    130 		 * Return how many seconds were left (rounded up)
    131 		 */
    132 		retval[0] = itp->it_value.tv_sec;
    133 		if (itp->it_value.tv_nsec)
    134 			retval[0]++;
    135 	} else {
    136 		retval[0] = 0;
    137 	}
    138 
    139 	/*
    140 	 * alarm(0) just resets the timer.
    141 	 */
    142 	if (SCARG(uap, secs) == 0) {
    143 		if (itp)
    144 			timespecclear(&itp->it_value);
    145 		mutex_spin_exit(&timer_lock);
    146 		return 0;
    147 	}
    148 
    149 	/*
    150 	 * Check the new alarm time for sanity, and set it.
    151 	 */
    152 	timespecclear(&it.it_interval);
    153 	it.it_value.tv_sec = SCARG(uap, secs);
    154 	it.it_value.tv_nsec = 0;
    155 	if (itimespecfix(&it.it_value) || itimespecfix(&it.it_interval)) {
    156 		mutex_spin_exit(&timer_lock);
    157 		return (EINVAL);
    158 	}
    159 
    160 	ptp = pts->pts_timers[ITIMER_REAL];
    161 	if (ptp == NULL) {
    162 		if (spare == NULL) {
    163 			mutex_spin_exit(&timer_lock);
    164 			spare = pool_get(&ptimer_pool, PR_WAITOK);
    165 			goto retry;
    166 		}
    167 		ptp = spare;
    168 		spare = NULL;
    169 		ptp->pt_ev.sigev_notify = SIGEV_SIGNAL;
    170 		ptp->pt_ev.sigev_signo = SIGALRM;
    171 		ptp->pt_overruns = 0;
    172 		ptp->pt_proc = p;
    173 		ptp->pt_type = CLOCK_REALTIME;
    174 		ptp->pt_entry = CLOCK_REALTIME;
    175 		ptp->pt_active = 0;
    176 		ptp->pt_queued = 0;
    177 		callout_init(&ptp->pt_ch, CALLOUT_MPSAFE);
    178 		pts->pts_timers[ITIMER_REAL] = ptp;
    179 	}
    180 
    181 	if (timespecisset(&it.it_value)) {
    182 		/*
    183 		 * Don't need to check tvhzto() return value, here.
    184 		 * callout_reset() does it for us.
    185 		 */
    186 		getnanotime(&now);
    187 		timespecadd(&it.it_value, &now, &it.it_value);
    188 		callout_reset(&ptp->pt_ch, tshzto(&it.it_value),
    189 		    realtimerexpire, ptp);
    190 	}
    191 	ptp->pt_time = it;
    192 	mutex_spin_exit(&timer_lock);
    193 
    194 	return 0;
    195 }
    196 #endif /* !COMPAT_LINUX32 */
    197 
    198 #if !defined(__amd64__)
    199 int
    200 linux_sys_nice(struct lwp *l, const struct linux_sys_nice_args *uap, register_t *retval)
    201 {
    202 	/* {
    203 		syscallarg(int) incr;
    204 	} */
    205 	struct proc *p = l->l_proc;
    206 	struct sys_setpriority_args bsa;
    207 	int error;
    208 
    209 	SCARG(&bsa, which) = PRIO_PROCESS;
    210 	SCARG(&bsa, who) = 0;
    211 	SCARG(&bsa, prio) = p->p_nice - NZERO + SCARG(uap, incr);
    212 
    213 	error = sys_setpriority(l, &bsa, retval);
    214 	return (error) ? EPERM : 0;
    215 }
    216 #endif /* !__amd64__ */
    217 
    218 #ifndef COMPAT_LINUX32
    219 #ifndef __amd64__
    220 /*
    221  * The old Linux readdir was only able to read one entry at a time,
    222  * even though it had a 'count' argument. In fact, the emulation
    223  * of the old call was better than the original, because it did handle
    224  * the count arg properly. Don't bother with it anymore now, and use
    225  * it to distinguish between old and new. The difference is that the
    226  * newer one actually does multiple entries, and the reclen field
    227  * really is the reclen, not the namelength.
    228  */
    229 int
    230 linux_sys_readdir(struct lwp *l, const struct linux_sys_readdir_args *uap, register_t *retval)
    231 {
    232 	/* {
    233 		syscallarg(int) fd;
    234 		syscallarg(struct linux_dirent *) dent;
    235 		syscallarg(unsigned int) count;
    236 	} */
    237 	int error;
    238 	struct linux_sys_getdents_args da;
    239 
    240 	SCARG(&da, fd) = SCARG(uap, fd);
    241 	SCARG(&da, dent) = SCARG(uap, dent);
    242 	SCARG(&da, count) = 1;
    243 
    244 	error = linux_sys_getdents(l, &da, retval);
    245 	if (error == 0 && *retval > 1)
    246 		*retval = 1;
    247 
    248 	return error;
    249 }
    250 #endif /* !amd64 */
    251 
    252 /*
    253  * I wonder why Linux has gettimeofday() _and_ time().. Still, we
    254  * need to deal with it.
    255  */
    256 int
    257 linux_sys_time(struct lwp *l, const struct linux_sys_time_args *uap, register_t *retval)
    258 {
    259 	/* {
    260 		syscallarg(linux_time_t) *t;
    261 	} */
    262 	struct timeval atv;
    263 	linux_time_t tt;
    264 	int error;
    265 
    266 	microtime(&atv);
    267 
    268 	tt = atv.tv_sec;
    269 	if (SCARG(uap, t) && (error = copyout(&tt, SCARG(uap, t), sizeof tt)))
    270 		return error;
    271 
    272 	retval[0] = tt;
    273 	return 0;
    274 }
    275 
    276 /*
    277  * utime(). Do conversion to things that utimes() understands,
    278  * and pass it on.
    279  */
    280 int
    281 linux_sys_utime(struct lwp *l, const struct linux_sys_utime_args *uap, register_t *retval)
    282 {
    283 	/* {
    284 		syscallarg(const char *) path;
    285 		syscallarg(struct linux_utimbuf *)times;
    286 	} */
    287 	int error;
    288 	struct timeval tv[2], *tvp;
    289 	struct linux_utimbuf lut;
    290 
    291 	if (SCARG(uap, times) != NULL) {
    292 		if ((error = copyin(SCARG(uap, times), &lut, sizeof lut)))
    293 			return error;
    294 		tv[0].tv_usec = tv[1].tv_usec = 0;
    295 		tv[0].tv_sec = lut.l_actime;
    296 		tv[1].tv_sec = lut.l_modtime;
    297 		tvp = tv;
    298 	} else
    299 		tvp = NULL;
    300 
    301 	return do_sys_utimes(l, NULL, SCARG(uap, path), FOLLOW,
    302 			   tvp,  UIO_SYSSPACE);
    303 }
    304 
    305 #ifndef __amd64__
    306 /*
    307  * waitpid(2).  Just forward on to linux_sys_wait4 with a NULL rusage.
    308  */
    309 int
    310 linux_sys_waitpid(struct lwp *l, const struct linux_sys_waitpid_args *uap, register_t *retval)
    311 {
    312 	/* {
    313 		syscallarg(int) pid;
    314 		syscallarg(int *) status;
    315 		syscallarg(int) options;
    316 	} */
    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(struct lwp *l, const struct linux_sys_setresgid_args *uap, register_t *retval)
    330 {
    331 	/* {
    332 		syscallarg(gid_t) rgid;
    333 		syscallarg(gid_t) egid;
    334 		syscallarg(gid_t) sgid;
    335 	} */
    336 
    337 	/*
    338 	 * Note: These checks are a little different than the NetBSD
    339 	 * setregid(2) call performs.  This precisely follows the
    340 	 * behavior of the Linux kernel.
    341 	 */
    342 	return do_setresgid(l, SCARG(uap,rgid), SCARG(uap, egid),
    343 			    SCARG(uap, sgid),
    344 			    ID_R_EQ_R | ID_R_EQ_E | ID_R_EQ_S |
    345 			    ID_E_EQ_R | ID_E_EQ_E | ID_E_EQ_S |
    346 			    ID_S_EQ_R | ID_S_EQ_E | ID_S_EQ_S );
    347 }
    348 
    349 int
    350 linux_sys_getresgid(struct lwp *l, const struct linux_sys_getresgid_args *uap, register_t *retval)
    351 {
    352 	/* {
    353 		syscallarg(gid_t *) rgid;
    354 		syscallarg(gid_t *) egid;
    355 		syscallarg(gid_t *) sgid;
    356 	} */
    357 	kauth_cred_t pc = l->l_cred;
    358 	int error;
    359 	gid_t gid;
    360 
    361 	/*
    362 	 * Linux copies these values out to userspace like so:
    363 	 *
    364 	 *	1. Copy out rgid.
    365 	 *	2. If that succeeds, copy out egid.
    366 	 *	3. If both of those succeed, copy out sgid.
    367 	 */
    368 	gid = kauth_cred_getgid(pc);
    369 	if ((error = copyout(&gid, SCARG(uap, rgid), sizeof(gid_t))) != 0)
    370 		return (error);
    371 
    372 	gid = kauth_cred_getegid(pc);
    373 	if ((error = copyout(&gid, SCARG(uap, egid), sizeof(gid_t))) != 0)
    374 		return (error);
    375 
    376 	gid = kauth_cred_getsvgid(pc);
    377 
    378 	return (copyout(&gid, SCARG(uap, sgid), sizeof(gid_t)));
    379 }
    380 
    381 #ifndef __amd64__
    382 /*
    383  * I wonder why Linux has settimeofday() _and_ stime().. Still, we
    384  * need to deal with it.
    385  */
    386 int
    387 linux_sys_stime(struct lwp *l, const struct linux_sys_stime_args *uap, register_t *retval)
    388 {
    389 	/* {
    390 		syscallarg(linux_time_t) *t;
    391 	} */
    392 	struct timespec ats;
    393 	linux_time_t tt;
    394 	int error;
    395 
    396 	if ((error = copyin(SCARG(uap, t), &tt, sizeof tt)) != 0)
    397 		return error;
    398 
    399 	ats.tv_sec = tt;
    400 	ats.tv_nsec = 0;
    401 
    402 	if ((error = settime(l->l_proc, &ats)))
    403 		return (error);
    404 
    405 	return 0;
    406 }
    407 
    408 /*
    409  * Implement the fs stat functions. Straightforward.
    410  */
    411 int
    412 linux_sys_statfs64(struct lwp *l, const struct linux_sys_statfs64_args *uap, register_t *retval)
    413 {
    414 	/* {
    415 		syscallarg(const char *) path;
    416 		syscallarg(size_t) sz;
    417 		syscallarg(struct linux_statfs64 *) sp;
    418 	} */
    419 	struct statvfs *sb;
    420 	struct linux_statfs64 ltmp;
    421 	int error;
    422 
    423 	if (SCARG(uap, sz) != sizeof ltmp)
    424 		return (EINVAL);
    425 
    426 	sb = STATVFSBUF_GET();
    427 	error = do_sys_pstatvfs(l, SCARG(uap, path), ST_WAIT, sb);
    428 	if (error == 0) {
    429 		bsd_to_linux_statfs64(sb, &ltmp);
    430 		error = copyout(&ltmp, SCARG(uap, sp), sizeof ltmp);
    431 	}
    432 	STATVFSBUF_PUT(sb);
    433 	return error;
    434 }
    435 
    436 int
    437 linux_sys_fstatfs64(struct lwp *l, const struct linux_sys_fstatfs64_args *uap, register_t *retval)
    438 {
    439 	/* {
    440 		syscallarg(int) fd;
    441 		syscallarg(size_t) sz;
    442 		syscallarg(struct linux_statfs64 *) sp;
    443 	} */
    444 	struct statvfs *sb;
    445 	struct linux_statfs64 ltmp;
    446 	int error;
    447 
    448 	if (SCARG(uap, sz) != sizeof ltmp)
    449 		return (EINVAL);
    450 
    451 	sb = STATVFSBUF_GET();
    452 	error = do_sys_fstatvfs(l, SCARG(uap, fd), ST_WAIT, sb);
    453 	if (error == 0) {
    454 		bsd_to_linux_statfs64(sb, &ltmp);
    455 		error = copyout(&ltmp, SCARG(uap, sp), sizeof ltmp);
    456 	}
    457 	STATVFSBUF_PUT(sb);
    458 	return error;
    459 }
    460 #endif /* !__amd64__ */
    461 #endif /* !COMPAT_LINUX32 */
    462