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