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