xref: /netbsd-src/sys/compat/linux/common/linux_misc_notalpha.c (revision da9817918ec7e88db2912a2882967c7570a83f47)
1 /*	$NetBSD: linux_misc_notalpha.c,v 1.106 2009/06/02 13:00:23 njoly 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 2009/06/02 13:00:23 njoly Exp $");
35 
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/kernel.h>
39 #include <sys/mman.h>
40 #include <sys/mount.h>
41 #include <sys/malloc.h>
42 #include <sys/mbuf.h>
43 #include <sys/namei.h>
44 #include <sys/proc.h>
45 #include <sys/prot.h>
46 #include <sys/ptrace.h>
47 #include <sys/resource.h>
48 #include <sys/resourcevar.h>
49 #include <sys/time.h>
50 #include <sys/vfs_syscalls.h>
51 #include <sys/wait.h>
52 #include <sys/kauth.h>
53 
54 #include <sys/syscallargs.h>
55 
56 #include <compat/linux/common/linux_types.h>
57 #include <compat/linux/common/linux_fcntl.h>
58 #include <compat/linux/common/linux_misc.h>
59 #include <compat/linux/common/linux_mmap.h>
60 #include <compat/linux/common/linux_signal.h>
61 #include <compat/linux/common/linux_util.h>
62 #include <compat/linux/common/linux_ipc.h>
63 #include <compat/linux/common/linux_sem.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 #ifdef DEBUG_LINUX
76 #define DPRINTF(a)	uprintf a
77 #else
78 #define DPRINTF(a)
79 #endif
80 
81 #ifndef COMPAT_LINUX32
82 #if !defined(__m68k__) && !defined(__amd64__)
83 static void bsd_to_linux_statfs64(const struct statvfs *,
84 	struct linux_statfs64  *);
85 #endif
86 
87 /*
88  * Alarm. This is a libc call which uses setitimer(2) in NetBSD.
89  * Fiddle with the timers to make it work.
90  *
91  * XXX This shouldn't be dicking about with the ptimer stuff directly.
92  */
93 int
94 linux_sys_alarm(struct lwp *l, const struct linux_sys_alarm_args *uap, register_t *retval)
95 {
96 	/* {
97 		syscallarg(unsigned int) secs;
98 	} */
99 	struct proc *p = l->l_proc;
100 	struct timespec now;
101 	struct itimerspec *itp, it;
102 	struct ptimer *ptp, *spare;
103 	extern kmutex_t timer_lock;
104 	struct ptimers *pts;
105 
106 	if ((pts = p->p_timers) == NULL)
107 		pts = timers_alloc(p);
108 	spare = NULL;
109 
110  retry:
111 	mutex_spin_enter(&timer_lock);
112 	if (pts && pts->pts_timers[ITIMER_REAL])
113 		itp = &pts->pts_timers[ITIMER_REAL]->pt_time;
114 	else
115 		itp = NULL;
116 	/*
117 	 * Clear any pending timer alarms.
118 	 */
119 	if (itp) {
120 		callout_stop(&pts->pts_timers[ITIMER_REAL]->pt_ch);
121 		timespecclear(&itp->it_interval);
122 		getnanotime(&now);
123 		if (timespecisset(&itp->it_value) &&
124 		    timespeccmp(&itp->it_value, &now, >))
125 			timespecsub(&itp->it_value, &now, &itp->it_value);
126 		/*
127 		 * Return how many seconds were left (rounded up)
128 		 */
129 		retval[0] = itp->it_value.tv_sec;
130 		if (itp->it_value.tv_nsec)
131 			retval[0]++;
132 	} else {
133 		retval[0] = 0;
134 	}
135 
136 	/*
137 	 * alarm(0) just resets the timer.
138 	 */
139 	if (SCARG(uap, secs) == 0) {
140 		if (itp)
141 			timespecclear(&itp->it_value);
142 		mutex_spin_exit(&timer_lock);
143 		return 0;
144 	}
145 
146 	/*
147 	 * Check the new alarm time for sanity, and set it.
148 	 */
149 	timespecclear(&it.it_interval);
150 	it.it_value.tv_sec = SCARG(uap, secs);
151 	it.it_value.tv_nsec = 0;
152 	if (itimespecfix(&it.it_value) || itimespecfix(&it.it_interval)) {
153 		mutex_spin_exit(&timer_lock);
154 		return (EINVAL);
155 	}
156 
157 	ptp = pts->pts_timers[ITIMER_REAL];
158 	if (ptp == NULL) {
159 		if (spare == NULL) {
160 			mutex_spin_exit(&timer_lock);
161 			spare = pool_get(&ptimer_pool, PR_WAITOK);
162 			goto retry;
163 		}
164 		ptp = spare;
165 		spare = NULL;
166 		ptp->pt_ev.sigev_notify = SIGEV_SIGNAL;
167 		ptp->pt_ev.sigev_signo = SIGALRM;
168 		ptp->pt_overruns = 0;
169 		ptp->pt_proc = p;
170 		ptp->pt_type = CLOCK_REALTIME;
171 		ptp->pt_entry = CLOCK_REALTIME;
172 		ptp->pt_active = 0;
173 		ptp->pt_queued = 0;
174 		callout_init(&ptp->pt_ch, CALLOUT_MPSAFE);
175 		pts->pts_timers[ITIMER_REAL] = ptp;
176 	}
177 
178 	if (timespecisset(&it.it_value)) {
179 		/*
180 		 * Don't need to check tvhzto() return value, here.
181 		 * callout_reset() does it for us.
182 		 */
183 		getnanotime(&now);
184 		timespecadd(&it.it_value, &now, &it.it_value);
185 		callout_reset(&ptp->pt_ch, tshzto(&it.it_value),
186 		    realtimerexpire, ptp);
187 	}
188 	ptp->pt_time = it;
189 	mutex_spin_exit(&timer_lock);
190 
191 	return 0;
192 }
193 #endif /* !COMPAT_LINUX32 */
194 
195 #if !defined(__amd64__)
196 int
197 linux_sys_nice(struct lwp *l, const struct linux_sys_nice_args *uap, register_t *retval)
198 {
199 	/* {
200 		syscallarg(int) incr;
201 	} */
202 	struct proc *p = l->l_proc;
203 	struct sys_setpriority_args bsa;
204 	int error;
205 
206 	SCARG(&bsa, which) = PRIO_PROCESS;
207 	SCARG(&bsa, who) = 0;
208 	SCARG(&bsa, prio) = p->p_nice - NZERO + SCARG(uap, incr);
209 
210 	error = sys_setpriority(l, &bsa, retval);
211 	return (error) ? EPERM : 0;
212 }
213 #endif /* !__amd64__ */
214 
215 #ifndef COMPAT_LINUX32
216 #ifndef __amd64__
217 /*
218  * The old Linux readdir was only able to read one entry at a time,
219  * even though it had a 'count' argument. In fact, the emulation
220  * of the old call was better than the original, because it did handle
221  * the count arg properly. Don't bother with it anymore now, and use
222  * it to distinguish between old and new. The difference is that the
223  * newer one actually does multiple entries, and the reclen field
224  * really is the reclen, not the namelength.
225  */
226 int
227 linux_sys_readdir(struct lwp *l, const struct linux_sys_readdir_args *uap, register_t *retval)
228 {
229 	/* {
230 		syscallarg(int) fd;
231 		syscallarg(struct linux_dirent *) dent;
232 		syscallarg(unsigned int) count;
233 	} */
234 	int error;
235 	struct linux_sys_getdents_args da;
236 
237 	SCARG(&da, fd) = SCARG(uap, fd);
238 	SCARG(&da, dent) = SCARG(uap, dent);
239 	SCARG(&da, count) = 1;
240 
241 	error = linux_sys_getdents(l, &da, retval);
242 	if (error == 0 && *retval > 1)
243 		*retval = 1;
244 
245 	return error;
246 }
247 #endif /* !amd64 */
248 
249 /*
250  * I wonder why Linux has gettimeofday() _and_ time().. Still, we
251  * need to deal with it.
252  */
253 int
254 linux_sys_time(struct lwp *l, const struct linux_sys_time_args *uap, register_t *retval)
255 {
256 	/* {
257 		syscallarg(linux_time_t) *t;
258 	} */
259 	struct timeval atv;
260 	linux_time_t tt;
261 	int error;
262 
263 	microtime(&atv);
264 
265 	tt = atv.tv_sec;
266 	if (SCARG(uap, t) && (error = copyout(&tt, SCARG(uap, t), sizeof tt)))
267 		return error;
268 
269 	retval[0] = tt;
270 	return 0;
271 }
272 
273 /*
274  * utime(). Do conversion to things that utimes() understands,
275  * and pass it on.
276  */
277 int
278 linux_sys_utime(struct lwp *l, const struct linux_sys_utime_args *uap, register_t *retval)
279 {
280 	/* {
281 		syscallarg(const char *) path;
282 		syscallarg(struct linux_utimbuf *)times;
283 	} */
284 	int error;
285 	struct timeval tv[2], *tvp;
286 	struct linux_utimbuf lut;
287 
288 	if (SCARG(uap, times) != NULL) {
289 		if ((error = copyin(SCARG(uap, times), &lut, sizeof lut)))
290 			return error;
291 		tv[0].tv_usec = tv[1].tv_usec = 0;
292 		tv[0].tv_sec = lut.l_actime;
293 		tv[1].tv_sec = lut.l_modtime;
294 		tvp = tv;
295 	} else
296 		tvp = NULL;
297 
298 	return do_sys_utimes(l, NULL, SCARG(uap, path), FOLLOW,
299 			   tvp,  UIO_SYSSPACE);
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(struct lwp *l, const struct linux_sys_waitpid_args *uap, register_t *retval)
308 {
309 	/* {
310 		syscallarg(int) pid;
311 		syscallarg(int *) status;
312 		syscallarg(int) options;
313 	} */
314 	struct linux_sys_wait4_args linux_w4a;
315 
316 	SCARG(&linux_w4a, pid) = SCARG(uap, pid);
317 	SCARG(&linux_w4a, status) = SCARG(uap, status);
318 	SCARG(&linux_w4a, options) = SCARG(uap, options);
319 	SCARG(&linux_w4a, rusage) = NULL;
320 
321 	return linux_sys_wait4(l, &linux_w4a, retval);
322 }
323 #endif /* !amd64 */
324 
325 int
326 linux_sys_setresgid(struct lwp *l, const struct linux_sys_setresgid_args *uap, register_t *retval)
327 {
328 	/* {
329 		syscallarg(gid_t) rgid;
330 		syscallarg(gid_t) egid;
331 		syscallarg(gid_t) sgid;
332 	} */
333 
334 	/*
335 	 * Note: These checks are a little different than the NetBSD
336 	 * setregid(2) call performs.  This precisely follows the
337 	 * behavior of the Linux kernel.
338 	 */
339 	return do_setresgid(l, SCARG(uap,rgid), SCARG(uap, egid),
340 			    SCARG(uap, sgid),
341 			    ID_R_EQ_R | ID_R_EQ_E | ID_R_EQ_S |
342 			    ID_E_EQ_R | ID_E_EQ_E | ID_E_EQ_S |
343 			    ID_S_EQ_R | ID_S_EQ_E | ID_S_EQ_S );
344 }
345 
346 int
347 linux_sys_getresgid(struct lwp *l, const struct linux_sys_getresgid_args *uap, register_t *retval)
348 {
349 	/* {
350 		syscallarg(gid_t *) rgid;
351 		syscallarg(gid_t *) egid;
352 		syscallarg(gid_t *) sgid;
353 	} */
354 	kauth_cred_t pc = l->l_cred;
355 	int error;
356 	gid_t gid;
357 
358 	/*
359 	 * Linux copies these values out to userspace like so:
360 	 *
361 	 *	1. Copy out rgid.
362 	 *	2. If that succeeds, copy out egid.
363 	 *	3. If both of those succeed, copy out sgid.
364 	 */
365 	gid = kauth_cred_getgid(pc);
366 	if ((error = copyout(&gid, SCARG(uap, rgid), sizeof(gid_t))) != 0)
367 		return (error);
368 
369 	gid = kauth_cred_getegid(pc);
370 	if ((error = copyout(&gid, SCARG(uap, egid), sizeof(gid_t))) != 0)
371 		return (error);
372 
373 	gid = kauth_cred_getsvgid(pc);
374 
375 	return (copyout(&gid, SCARG(uap, sgid), sizeof(gid_t)));
376 }
377 
378 #ifndef __amd64__
379 /*
380  * I wonder why Linux has settimeofday() _and_ stime().. Still, we
381  * need to deal with it.
382  */
383 int
384 linux_sys_stime(struct lwp *l, const struct linux_sys_stime_args *uap, register_t *retval)
385 {
386 	/* {
387 		syscallarg(linux_time_t) *t;
388 	} */
389 	struct timespec ats;
390 	linux_time_t tt;
391 	int error;
392 
393 	if ((error = copyin(SCARG(uap, t), &tt, sizeof tt)) != 0)
394 		return error;
395 
396 	ats.tv_sec = tt;
397 	ats.tv_nsec = 0;
398 
399 	if ((error = settime(l->l_proc, &ats)))
400 		return (error);
401 
402 	return 0;
403 }
404 #endif /* !amd64 */
405 
406 #if !defined(__m68k__) && !defined(__amd64__)
407 /*
408  * Convert NetBSD statvfs structure to Linux statfs64 structure.
409  * See comments in bsd_to_linux_statfs() for further background.
410  * We can safely pass correct bsize and frsize here, since Linux glibc
411  * statvfs() doesn't use statfs64().
412  */
413 static void
414 bsd_to_linux_statfs64(const struct statvfs *bsp, struct linux_statfs64 *lsp)
415 {
416 	int i, div;
417 
418 	for (i = 0; i < linux_fstypes_cnt; i++) {
419 		if (strcmp(bsp->f_fstypename, linux_fstypes[i].bsd) == 0) {
420 			lsp->l_ftype = linux_fstypes[i].linux;
421 			break;
422 		}
423 	}
424 
425 	if (i == linux_fstypes_cnt) {
426 		DPRINTF(("unhandled fstype in linux emulation: %s\n",
427 		    bsp->f_fstypename));
428 		lsp->l_ftype = LINUX_DEFAULT_SUPER_MAGIC;
429 	}
430 
431 	div = bsp->f_frsize ? (bsp->f_bsize / bsp->f_frsize) : 1;
432 	if (div == 0)
433 		div = 1;
434 	lsp->l_fbsize = bsp->f_bsize;
435 	lsp->l_ffrsize = bsp->f_frsize;
436 	lsp->l_fblocks = bsp->f_blocks / div;
437 	lsp->l_fbfree = bsp->f_bfree / div;
438 	lsp->l_fbavail = bsp->f_bavail / div;
439 	lsp->l_ffiles = bsp->f_files;
440 	lsp->l_fffree = bsp->f_ffree / div;
441 	/* Linux sets the fsid to 0..., we don't */
442 	lsp->l_ffsid.val[0] = bsp->f_fsidx.__fsid_val[0];
443 	lsp->l_ffsid.val[1] = bsp->f_fsidx.__fsid_val[1];
444 	lsp->l_fnamelen = bsp->f_namemax;
445 	(void)memset(lsp->l_fspare, 0, sizeof(lsp->l_fspare));
446 }
447 
448 /*
449  * Implement the fs stat functions. Straightforward.
450  */
451 int
452 linux_sys_statfs64(struct lwp *l, const struct linux_sys_statfs64_args *uap, register_t *retval)
453 {
454 	/* {
455 		syscallarg(const char *) path;
456 		syscallarg(size_t) sz;
457 		syscallarg(struct linux_statfs64 *) sp;
458 	} */
459 	struct statvfs *sb;
460 	struct linux_statfs64 ltmp;
461 	int error;
462 
463 	if (SCARG(uap, sz) != sizeof ltmp)
464 		return (EINVAL);
465 
466 	sb = STATVFSBUF_GET();
467 	error = do_sys_pstatvfs(l, SCARG(uap, path), ST_WAIT, sb);
468 	if (error == 0) {
469 		bsd_to_linux_statfs64(sb, &ltmp);
470 		error = copyout(&ltmp, SCARG(uap, sp), sizeof ltmp);
471 	}
472 	STATVFSBUF_PUT(sb);
473 	return error;
474 }
475 
476 int
477 linux_sys_fstatfs64(struct lwp *l, const struct linux_sys_fstatfs64_args *uap, register_t *retval)
478 {
479 	/* {
480 		syscallarg(int) fd;
481 		syscallarg(size_t) sz;
482 		syscallarg(struct linux_statfs64 *) sp;
483 	} */
484 	struct statvfs *sb;
485 	struct linux_statfs64 ltmp;
486 	int error;
487 
488 	if (SCARG(uap, sz) != sizeof ltmp)
489 		return (EINVAL);
490 
491 	sb = STATVFSBUF_GET();
492 	error = do_sys_fstatvfs(l, SCARG(uap, fd), ST_WAIT, sb);
493 	if (error == 0) {
494 		bsd_to_linux_statfs64(sb, &ltmp);
495 		error = copyout(&ltmp, SCARG(uap, sp), sizeof ltmp);
496 	}
497 	STATVFSBUF_PUT(sb);
498 	return error;
499 }
500 #endif /* !__m68k__ && !__amd64__ */
501 #endif /* !COMPAT_LINUX32 */
502