linux_misc.c revision 1.45 1 /* $NetBSD: linux_misc.c,v 1.45 1998/09/08 20:02:52 rvb Exp $ */
2
3 /*
4 * Copyright (c) 1995 Frank van der Linden
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed for the NetBSD Project
18 * by Frank van der Linden
19 * 4. The name of the author may not be used to endorse or promote products
20 * derived from this software without specific prior written permission
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 */
33
34 /*
35 * Linux compatibility module. Try to deal with various Linux system calls.
36 */
37
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/namei.h>
41 #include <sys/proc.h>
42 #include <sys/dirent.h>
43 #include <sys/file.h>
44 #include <sys/stat.h>
45 #include <sys/filedesc.h>
46 #include <sys/ioctl.h>
47 #include <sys/kernel.h>
48 #include <sys/malloc.h>
49 #include <sys/mbuf.h>
50 #include <sys/mman.h>
51 #include <sys/mount.h>
52 #include <sys/ptrace.h>
53 #include <sys/resource.h>
54 #include <sys/resourcevar.h>
55 #include <sys/signal.h>
56 #include <sys/signalvar.h>
57 #include <sys/socket.h>
58 #include <sys/time.h>
59 #include <sys/times.h>
60 #include <sys/vnode.h>
61 #include <sys/uio.h>
62 #include <sys/wait.h>
63 #include <sys/utsname.h>
64 #include <sys/unistd.h>
65
66 #include <sys/syscallargs.h>
67
68 #include <vm/vm.h>
69 #include <vm/vm_param.h>
70
71 #include <compat/linux/linux_types.h>
72 #include <compat/linux/linux_fcntl.h>
73 #include <compat/linux/linux_mmap.h>
74 #include <compat/linux/linux_signal.h>
75 #include <compat/linux/linux_syscallargs.h>
76 #include <compat/linux/linux_util.h>
77 #include <compat/linux/linux_dirent.h>
78
79 /* linux_misc.c */
80 static void bsd_to_linux_wstat __P((int *));
81 static void bsd_to_linux_statfs __P((struct statfs *, struct linux_statfs *));
82 int linux_select1 __P((struct proc *, register_t *, int, fd_set *, fd_set *,
83 fd_set *, struct timeval *));
84
85 /*
86 * The information on a terminated (or stopped) process needs
87 * to be converted in order for Linux binaries to get a valid signal
88 * number out of it.
89 */
90 static void
91 bsd_to_linux_wstat(status)
92 int *status;
93 {
94
95 if (WIFSIGNALED(*status))
96 *status = (*status & ~0177) |
97 bsd_to_linux_sig[WTERMSIG(*status)];
98 else if (WIFSTOPPED(*status))
99 *status = (*status & ~0xff00) |
100 (bsd_to_linux_sig[WSTOPSIG(*status)] << 8);
101 }
102
103 /*
104 * waitpid(2). Passed on to the NetBSD call, surrounded by code to
105 * reserve some space for a NetBSD-style wait status, and converting
106 * it to what Linux wants.
107 */
108 int
109 linux_sys_waitpid(p, v, retval)
110 struct proc *p;
111 void *v;
112 register_t *retval;
113 {
114 struct linux_sys_waitpid_args /* {
115 syscallarg(int) pid;
116 syscallarg(int *) status;
117 syscallarg(int) options;
118 } */ *uap = v;
119 struct sys_wait4_args w4a;
120 int error, *status, tstat;
121 caddr_t sg;
122
123 if (SCARG(uap, status) != NULL) {
124 sg = stackgap_init(p->p_emul);
125 status = (int *) stackgap_alloc(&sg, sizeof status);
126 } else
127 status = NULL;
128
129 SCARG(&w4a, pid) = SCARG(uap, pid);
130 SCARG(&w4a, status) = status;
131 SCARG(&w4a, options) = SCARG(uap, options);
132 SCARG(&w4a, rusage) = NULL;
133
134 if ((error = sys_wait4(p, &w4a, retval)))
135 return error;
136
137 p->p_siglist &= ~sigmask(SIGCHLD);
138
139 if (status != NULL) {
140 if ((error = copyin(status, &tstat, sizeof tstat)))
141 return error;
142
143 bsd_to_linux_wstat(&tstat);
144 return copyout(&tstat, SCARG(uap, status), sizeof tstat);
145 }
146
147 return 0;
148 }
149
150 /*
151 * This is very much the same as waitpid()
152 */
153 int
154 linux_sys_wait4(p, v, retval)
155 struct proc *p;
156 void *v;
157 register_t *retval;
158 {
159 struct linux_sys_wait4_args /* {
160 syscallarg(int) pid;
161 syscallarg(int *) status;
162 syscallarg(int) options;
163 syscallarg(struct rusage *) rusage;
164 } */ *uap = v;
165 struct sys_wait4_args w4a;
166 int error, *status, tstat;
167 caddr_t sg;
168
169 if (SCARG(uap, status) != NULL) {
170 sg = stackgap_init(p->p_emul);
171 status = (int *) stackgap_alloc(&sg, sizeof status);
172 } else
173 status = NULL;
174
175 SCARG(&w4a, pid) = SCARG(uap, pid);
176 SCARG(&w4a, status) = status;
177 SCARG(&w4a, options) = SCARG(uap, options);
178 SCARG(&w4a, rusage) = SCARG(uap, rusage);
179
180 if ((error = sys_wait4(p, &w4a, retval)))
181 return error;
182
183 p->p_siglist &= ~sigmask(SIGCHLD);
184
185 if (status != NULL) {
186 if ((error = copyin(status, &tstat, sizeof tstat)))
187 return error;
188
189 bsd_to_linux_wstat(&tstat);
190 return copyout(&tstat, SCARG(uap, status), sizeof tstat);
191 }
192
193 return 0;
194 }
195
196 /*
197 * This is the old brk(2) call. I don't think anything in the Linux
198 * world uses this anymore
199 */
200 int
201 linux_sys_break(p, v, retval)
202 struct proc *p;
203 void *v;
204 register_t *retval;
205 {
206 #if 0
207 struct linux_sys_brk_args /* {
208 syscallarg(char *) nsize;
209 } */ *uap = v;
210 #endif
211
212 return ENOSYS;
213 }
214
215 /*
216 * Linux brk(2). The check if the new address is >= the old one is
217 * done in the kernel in Linux. NetBSD does it in the library.
218 */
219 int
220 linux_sys_brk(p, v, retval)
221 struct proc *p;
222 void *v;
223 register_t *retval;
224 {
225 struct linux_sys_brk_args /* {
226 syscallarg(char *) nsize;
227 } */ *uap = v;
228 char *nbrk = SCARG(uap, nsize);
229 struct sys_obreak_args oba;
230 struct vmspace *vm = p->p_vmspace;
231 caddr_t oldbrk;
232
233 oldbrk = vm->vm_daddr + ctob(vm->vm_dsize);
234 /*
235 * XXX inconsistent.. Linux always returns at least the old
236 * brk value, but it will be page-aligned if this fails,
237 * and possibly not page aligned if it succeeds (the user
238 * supplied pointer is returned).
239 */
240 SCARG(&oba, nsize) = nbrk;
241
242 if ((caddr_t) nbrk > vm->vm_daddr && sys_obreak(p, &oba, retval) == 0)
243 retval[0] = (register_t)nbrk;
244 else
245 retval[0] = (register_t)oldbrk;
246
247 return 0;
248 }
249
250 /*
251 * I wonder why Linux has gettimeofday() _and_ time().. Still, we
252 * need to deal with it.
253 */
254 int
255 linux_sys_time(p, v, retval)
256 struct proc *p;
257 void *v;
258 register_t *retval;
259 {
260 struct linux_sys_time_args /* {
261 linux_time_t *t;
262 } */ *uap = v;
263 struct timeval atv;
264 linux_time_t tt;
265 int error;
266
267 microtime(&atv);
268
269 tt = atv.tv_sec;
270 if (SCARG(uap, t) && (error = copyout(&tt, SCARG(uap, t), sizeof tt)))
271 return error;
272
273 retval[0] = tt;
274 return 0;
275 }
276
277 /*
278 * Convert BSD statfs structure to Linux statfs structure.
279 * The Linux structure has less fields, and it also wants
280 * the length of a name in a dir entry in a field, which
281 * we fake (probably the wrong way).
282 */
283 static void
284 bsd_to_linux_statfs(bsp, lsp)
285 struct statfs *bsp;
286 struct linux_statfs *lsp;
287 {
288
289 lsp->l_ftype = bsp->f_type;
290 lsp->l_fbsize = bsp->f_bsize;
291 lsp->l_fblocks = bsp->f_blocks;
292 lsp->l_fbfree = bsp->f_bfree;
293 lsp->l_fbavail = bsp->f_bavail;
294 lsp->l_ffiles = bsp->f_files;
295 lsp->l_fffree = bsp->f_ffree;
296 lsp->l_ffsid.val[0] = bsp->f_fsid.val[0];
297 lsp->l_ffsid.val[1] = bsp->f_fsid.val[1];
298 lsp->l_fnamelen = MAXNAMLEN; /* XXX */
299 }
300
301 /*
302 * Implement the fs stat functions. Straightforward.
303 */
304 int
305 linux_sys_statfs(p, v, retval)
306 struct proc *p;
307 void *v;
308 register_t *retval;
309 {
310 struct linux_sys_statfs_args /* {
311 syscallarg(char *) path;
312 syscallarg(struct linux_statfs *) sp;
313 } */ *uap = v;
314 struct statfs btmp, *bsp;
315 struct linux_statfs ltmp;
316 struct sys_statfs_args bsa;
317 caddr_t sg;
318 int error;
319
320 sg = stackgap_init(p->p_emul);
321 bsp = (struct statfs *) stackgap_alloc(&sg, sizeof (struct statfs));
322
323 LINUX_CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
324
325 SCARG(&bsa, path) = SCARG(uap, path);
326 SCARG(&bsa, buf) = bsp;
327
328 if ((error = sys_statfs(p, &bsa, retval)))
329 return error;
330
331 if ((error = copyin((caddr_t) bsp, (caddr_t) &btmp, sizeof btmp)))
332 return error;
333
334 bsd_to_linux_statfs(&btmp, <mp);
335
336 return copyout((caddr_t) <mp, (caddr_t) SCARG(uap, sp), sizeof ltmp);
337 }
338
339 int
340 linux_sys_fstatfs(p, v, retval)
341 struct proc *p;
342 void *v;
343 register_t *retval;
344 {
345 struct linux_sys_fstatfs_args /* {
346 syscallarg(int) fd;
347 syscallarg(struct linux_statfs *) sp;
348 } */ *uap = v;
349 struct statfs btmp, *bsp;
350 struct linux_statfs ltmp;
351 struct sys_fstatfs_args bsa;
352 caddr_t sg;
353 int error;
354
355 sg = stackgap_init(p->p_emul);
356 bsp = (struct statfs *) stackgap_alloc(&sg, sizeof (struct statfs));
357
358 SCARG(&bsa, fd) = SCARG(uap, fd);
359 SCARG(&bsa, buf) = bsp;
360
361 if ((error = sys_fstatfs(p, &bsa, retval)))
362 return error;
363
364 if ((error = copyin((caddr_t) bsp, (caddr_t) &btmp, sizeof btmp)))
365 return error;
366
367 bsd_to_linux_statfs(&btmp, <mp);
368
369 return copyout((caddr_t) <mp, (caddr_t) SCARG(uap, sp), sizeof ltmp);
370 }
371
372 /*
373 * uname(). Just copy the info from the various strings stored in the
374 * kernel, and put it in the Linux utsname structure. That structure
375 * is almost the same as the NetBSD one, only it has fields 65 characters
376 * long, and an extra domainname field.
377 */
378 int
379 linux_sys_uname(p, v, retval)
380 struct proc *p;
381 void *v;
382 register_t *retval;
383 {
384 struct linux_sys_uname_args /* {
385 syscallarg(struct linux_utsname *) up;
386 } */ *uap = v;
387 extern char ostype[], hostname[], osrelease[], version[], machine[],
388 domainname[];
389 struct linux_utsname luts;
390 int len;
391 char *cp;
392
393 strncpy(luts.l_sysname, ostype, sizeof(luts.l_sysname));
394 strncpy(luts.l_nodename, hostname, sizeof(luts.l_nodename));
395 strncpy(luts.l_release, osrelease, sizeof(luts.l_release));
396 strncpy(luts.l_version, version, sizeof(luts.l_version));
397 strncpy(luts.l_machine, machine, sizeof(luts.l_machine));
398 strncpy(luts.l_domainname, domainname, sizeof(luts.l_domainname));
399
400 /* This part taken from the the uname() in libc */
401 len = sizeof(luts.l_version);
402 for (cp = luts.l_version; len--; ++cp) {
403 if (*cp == '\n' || *cp == '\t') {
404 if (len > 1)
405 *cp = ' ';
406 else
407 *cp = '\0';
408 }
409 }
410
411 return copyout(&luts, SCARG(uap, up), sizeof(luts));
412 }
413
414 int
415 linux_sys_olduname(p, v, retval)
416 struct proc *p;
417 void *v;
418 register_t *retval;
419 {
420 struct linux_sys_uname_args /* {
421 syscallarg(struct linux_oldutsname *) up;
422 } */ *uap = v;
423 extern char ostype[], hostname[], osrelease[], version[], machine[];
424 struct linux_oldutsname luts;
425 int len;
426 char *cp;
427
428 strncpy(luts.l_sysname, ostype, sizeof(luts.l_sysname));
429 strncpy(luts.l_nodename, hostname, sizeof(luts.l_nodename));
430 strncpy(luts.l_release, osrelease, sizeof(luts.l_release));
431 strncpy(luts.l_version, version, sizeof(luts.l_version));
432 strncpy(luts.l_machine, machine, sizeof(luts.l_machine));
433
434 /* This part taken from the the uname() in libc */
435 len = sizeof(luts.l_version);
436 for (cp = luts.l_version; len--; ++cp) {
437 if (*cp == '\n' || *cp == '\t') {
438 if (len > 1)
439 *cp = ' ';
440 else
441 *cp = '\0';
442 }
443 }
444
445 return copyout(&luts, SCARG(uap, up), sizeof(luts));
446 }
447
448 int
449 linux_sys_oldolduname(p, v, retval)
450 struct proc *p;
451 void *v;
452 register_t *retval;
453 {
454 struct linux_sys_uname_args /* {
455 syscallarg(struct linux_oldoldutsname *) up;
456 } */ *uap = v;
457 extern char ostype[], hostname[], osrelease[], version[], machine[];
458 struct linux_oldoldutsname luts;
459 int len;
460 char *cp;
461
462 strncpy(luts.l_sysname, ostype, sizeof(luts.l_sysname));
463 strncpy(luts.l_nodename, hostname, sizeof(luts.l_nodename));
464 strncpy(luts.l_release, osrelease, sizeof(luts.l_release));
465 strncpy(luts.l_version, version, sizeof(luts.l_version));
466 strncpy(luts.l_machine, machine, sizeof(luts.l_machine));
467
468 /* This part taken from the the uname() in libc */
469 len = sizeof(luts.l_version);
470 for (cp = luts.l_version; len--; ++cp) {
471 if (*cp == '\n' || *cp == '\t') {
472 if (len > 1)
473 *cp = ' ';
474 else
475 *cp = '\0';
476 }
477 }
478
479 return copyout(&luts, SCARG(uap, up), sizeof(luts));
480 }
481
482 /*
483 * Linux wants to pass everything to a syscall in registers. However,
484 * mmap() has 6 of them. Oops: out of register error. They just pass
485 * everything in a structure.
486 */
487 int
488 linux_sys_mmap(p, v, retval)
489 struct proc *p;
490 void *v;
491 register_t *retval;
492 {
493 struct linux_sys_mmap_args /* {
494 syscallarg(struct linux_mmap *) lmp;
495 } */ *uap = v;
496 struct linux_mmap lmap;
497 struct sys_mmap_args cma;
498 int error, flags;
499
500 if ((error = copyin(SCARG(uap, lmp), &lmap, sizeof lmap)))
501 return error;
502
503 flags = 0;
504 flags |= cvtto_bsd_mask(lmap.lm_flags, LINUX_MAP_SHARED, MAP_SHARED);
505 flags |= cvtto_bsd_mask(lmap.lm_flags, LINUX_MAP_PRIVATE, MAP_PRIVATE);
506 flags |= cvtto_bsd_mask(lmap.lm_flags, LINUX_MAP_FIXED, MAP_FIXED);
507 flags |= cvtto_bsd_mask(lmap.lm_flags, LINUX_MAP_ANON, MAP_ANON);
508
509 SCARG(&cma,addr) = lmap.lm_addr;
510 SCARG(&cma,len) = lmap.lm_len;
511 if (lmap.lm_prot & VM_PROT_WRITE) /* XXX */
512 lmap.lm_prot |= VM_PROT_READ;
513 SCARG(&cma,prot) = lmap.lm_prot;
514 SCARG(&cma,flags) = flags;
515 SCARG(&cma,fd) = lmap.lm_fd;
516 SCARG(&cma,pad) = 0;
517 SCARG(&cma,pos) = lmap.lm_pos;
518
519 return sys_mmap(p, &cma, retval);
520 }
521
522 int
523 linux_sys_mremap(p, v, retval)
524 struct proc *p;
525 void *v;
526 register_t *retval;
527 {
528 struct linux_sys_mremap_args /* {
529 syscallarg(void *) old_address;
530 syscallarg(size_t) old_size;
531 syscallarg(size_t) new_size;
532 syscallarg(u_long) flags;
533 } */ *uap = v;
534 struct sys_munmap_args mua;
535 size_t old_size, new_size;
536 int error;
537
538 old_size = round_page(SCARG(uap, old_size));
539 new_size = round_page(SCARG(uap, new_size));
540
541 /*
542 * Growing mapped region.
543 */
544 if (new_size > old_size) {
545 /*
546 * XXX Implement me. What we probably want to do is
547 * XXX dig out the guts of the old mapping, mmap that
548 * XXX object again with the new size, then munmap
549 * XXX the old mapping.
550 */
551 *retval = 0;
552 return (ENOMEM);
553 }
554
555 /*
556 * Shrinking mapped region.
557 */
558 if (new_size < old_size) {
559 SCARG(&mua, addr) = (caddr_t)SCARG(uap, old_address) +
560 new_size;
561 SCARG(&mua, len) = old_size - new_size;
562 error = sys_munmap(p, &mua, retval);
563 *retval = error ? 0 : (register_t)SCARG(uap, old_address);
564 return (error);
565 }
566
567 /*
568 * No change.
569 */
570 *retval = (register_t)SCARG(uap, old_address);
571 return (0);
572 }
573
574 int
575 linux_sys_msync(p, v, retval)
576 struct proc *p;
577 void *v;
578 register_t *retval;
579 {
580 struct linux_sys_msync_args /* {
581 syscallarg(caddr_t) addr;
582 syscallarg(int) len;
583 syscallarg(int) fl;
584 } */ *uap = v;
585
586 struct sys___msync13_args bma;
587
588 /* flags are ignored */
589 SCARG(&bma, addr) = SCARG(uap, addr);
590 SCARG(&bma, len) = SCARG(uap, len);
591 SCARG(&bma, flags) = SCARG(uap, fl);
592
593 return sys___msync13(p, &bma, retval);
594 }
595
596 /*
597 * This code is partly stolen from src/lib/libc/compat-43/times.c
598 * XXX - CLK_TCK isn't declared in /sys, just in <time.h>, done here
599 */
600
601 #define CLK_TCK 100
602 #define CONVTCK(r) (r.tv_sec * CLK_TCK + r.tv_usec / (1000000 / CLK_TCK))
603
604 int
605 linux_sys_times(p, v, retval)
606 struct proc *p;
607 void *v;
608 register_t *retval;
609 {
610 struct linux_sys_times_args /* {
611 syscallarg(struct times *) tms;
612 } */ *uap = v;
613 struct timeval t;
614 struct linux_tms ltms;
615 struct rusage ru;
616 int error, s;
617
618 calcru(p, &ru.ru_utime, &ru.ru_stime, NULL);
619 ltms.ltms_utime = CONVTCK(ru.ru_utime);
620 ltms.ltms_stime = CONVTCK(ru.ru_stime);
621
622 ltms.ltms_cutime = CONVTCK(p->p_stats->p_cru.ru_utime);
623 ltms.ltms_cstime = CONVTCK(p->p_stats->p_cru.ru_stime);
624
625 if ((error = copyout(<ms, SCARG(uap, tms), sizeof ltms)))
626 return error;
627
628 s = splclock();
629 timersub(&time, &boottime, &t);
630 splx(s);
631
632 retval[0] = ((linux_clock_t)(CONVTCK(t)));
633 return 0;
634 }
635
636 /*
637 * NetBSD passes fd[0] in retval[0], and fd[1] in retval[1].
638 * Linux directly passes the pointer.
639 */
640 int
641 linux_sys_pipe(p, v, retval)
642 struct proc *p;
643 void *v;
644 register_t *retval;
645 {
646 struct linux_sys_pipe_args /* {
647 syscallarg(int *) pfds;
648 } */ *uap = v;
649 int error;
650
651 if ((error = sys_pipe(p, 0, retval)))
652 return error;
653
654 /* Assumes register_t is an int */
655
656 if ((error = copyout(retval, SCARG(uap, pfds), 2 * sizeof (int))))
657 return error;
658
659 retval[0] = 0;
660 return 0;
661 }
662
663 /*
664 * Alarm. This is a libc call which uses setitimer(2) in NetBSD.
665 * Fiddle with the timers to make it work.
666 */
667 int
668 linux_sys_alarm(p, v, retval)
669 struct proc *p;
670 void *v;
671 register_t *retval;
672 {
673 struct linux_sys_alarm_args /* {
674 syscallarg(unsigned int) secs;
675 } */ *uap = v;
676 int s;
677 struct itimerval *itp, it;
678
679 itp = &p->p_realtimer;
680 s = splclock();
681 /*
682 * Clear any pending timer alarms.
683 */
684 untimeout(realitexpire, p);
685 timerclear(&itp->it_interval);
686 if (timerisset(&itp->it_value) &&
687 timercmp(&itp->it_value, &time, >))
688 timersub(&itp->it_value, &time, &itp->it_value);
689 /*
690 * Return how many seconds were left (rounded up)
691 */
692 retval[0] = itp->it_value.tv_sec;
693 if (itp->it_value.tv_usec)
694 retval[0]++;
695
696 /*
697 * alarm(0) just resets the timer.
698 */
699 if (SCARG(uap, secs) == 0) {
700 timerclear(&itp->it_value);
701 splx(s);
702 return 0;
703 }
704
705 /*
706 * Check the new alarm time for sanity, and set it.
707 */
708 timerclear(&it.it_interval);
709 it.it_value.tv_sec = SCARG(uap, secs);
710 it.it_value.tv_usec = 0;
711 if (itimerfix(&it.it_value) || itimerfix(&it.it_interval)) {
712 splx(s);
713 return (EINVAL);
714 }
715
716 if (timerisset(&it.it_value)) {
717 timeradd(&it.it_value, &time, &it.it_value);
718 timeout(realitexpire, p, hzto(&it.it_value));
719 }
720 p->p_realtimer = it;
721 splx(s);
722
723 return 0;
724 }
725
726 /*
727 * utime(). Do conversion to things that utimes() understands,
728 * and pass it on.
729 */
730 int
731 linux_sys_utime(p, v, retval)
732 struct proc *p;
733 void *v;
734 register_t *retval;
735 {
736 struct linux_sys_utime_args /* {
737 syscallarg(char *) path;
738 syscallarg(struct linux_utimbuf *)times;
739 } */ *uap = v;
740 caddr_t sg;
741 int error;
742 struct sys_utimes_args ua;
743 struct timeval tv[2], *tvp;
744 struct linux_utimbuf lut;
745
746 sg = stackgap_init(p->p_emul);
747 LINUX_CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
748
749 SCARG(&ua, path) = SCARG(uap, path);
750
751 if (SCARG(uap, times) != NULL) {
752 if ((error = copyin(SCARG(uap, times), &lut, sizeof lut)))
753 return error;
754 tv[0].tv_usec = tv[1].tv_usec = 0;
755 tv[0].tv_sec = lut.l_actime;
756 tv[1].tv_sec = lut.l_modtime;
757 tvp = (struct timeval *) stackgap_alloc(&sg, sizeof(tv));
758 if ((error = copyout(tv, tvp, sizeof tv)))
759 return error;
760 SCARG(&ua, tptr) = tvp;
761 }
762 else
763 SCARG(&ua, tptr) = NULL;
764
765 return sys_utimes(p, &ua, retval);
766 }
767
768 /*
769 * The old Linux readdir was only able to read one entry at a time,
770 * even though it had a 'count' argument. In fact, the emulation
771 * of the old call was better than the original, because it did handle
772 * the count arg properly. Don't bother with it anymore now, and use
773 * it to distinguish between old and new. The difference is that the
774 * newer one actually does multiple entries, and the reclen field
775 * really is the reclen, not the namelength.
776 */
777 int
778 linux_sys_readdir(p, v, retval)
779 struct proc *p;
780 void *v;
781 register_t *retval;
782 {
783 struct linux_sys_readdir_args /* {
784 syscallarg(int) fd;
785 syscallarg(struct linux_dirent *) dent;
786 syscallarg(unsigned int) count;
787 } */ *uap = v;
788
789 SCARG(uap, count) = 1;
790 return linux_sys_getdents(p, uap, retval);
791 }
792
793 /*
794 * Linux 'readdir' call. This code is mostly taken from the
795 * SunOS getdents call (see compat/sunos/sunos_misc.c), though
796 * an attempt has been made to keep it a little cleaner (failing
797 * miserably, because of the cruft needed if count 1 is passed).
798 *
799 * The d_off field should contain the offset of the next valid entry,
800 * but in Linux it has the offset of the entry itself. We emulate
801 * that bug here.
802 *
803 * Read in BSD-style entries, convert them, and copy them out.
804 *
805 * Note that this doesn't handle union-mounted filesystems.
806 */
807 int
808 linux_sys_getdents(p, v, retval)
809 struct proc *p;
810 void *v;
811 register_t *retval;
812 {
813 struct linux_sys_readdir_args /* {
814 syscallarg(int) fd;
815 syscallarg(caddr_t) dent;
816 syscallarg(unsigned int) count;
817 } */ *uap = v;
818 register struct dirent *bdp;
819 struct vnode *vp;
820 caddr_t inp, buf; /* BSD-format */
821 int len, reclen; /* BSD-format */
822 caddr_t outp; /* Linux-format */
823 int resid, linux_reclen = 0; /* Linux-format */
824 struct file *fp;
825 struct uio auio;
826 struct iovec aiov;
827 struct linux_dirent idb;
828 off_t off; /* true file offset */
829 int buflen, error, eofflag, nbytes, oldcall;
830 struct vattr va;
831 off_t *cookiebuf = NULL, *cookie;
832 int ncookies;
833
834 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
835 return (error);
836
837 if ((fp->f_flag & FREAD) == 0)
838 return (EBADF);
839
840 vp = (struct vnode *)fp->f_data;
841 if (vp->v_type != VDIR)
842 return (EINVAL);
843
844 if ((error = VOP_GETATTR(vp, &va, p->p_ucred, p)))
845 return error;
846
847 nbytes = SCARG(uap, count);
848 if (nbytes == 1) { /* emulating old, broken behaviour */
849 nbytes = sizeof (struct linux_dirent);
850 buflen = max(va.va_blocksize, nbytes);
851 oldcall = 1;
852 } else {
853 buflen = min(MAXBSIZE, nbytes);
854 if (buflen < va.va_blocksize)
855 buflen = va.va_blocksize;
856 oldcall = 0;
857 }
858 buf = malloc(buflen, M_TEMP, M_WAITOK);
859
860 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
861 off = fp->f_offset;
862 again:
863 aiov.iov_base = buf;
864 aiov.iov_len = buflen;
865 auio.uio_iov = &aiov;
866 auio.uio_iovcnt = 1;
867 auio.uio_rw = UIO_READ;
868 auio.uio_segflg = UIO_SYSSPACE;
869 auio.uio_procp = p;
870 auio.uio_resid = buflen;
871 auio.uio_offset = off;
872 /*
873 * First we read into the malloc'ed buffer, then
874 * we massage it into user space, one record at a time.
875 */
876 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, &cookiebuf,
877 &ncookies);
878 if (error)
879 goto out;
880
881 inp = buf;
882 outp = SCARG(uap, dent);
883 resid = nbytes;
884 if ((len = buflen - auio.uio_resid) == 0)
885 goto eof;
886
887 for (cookie = cookiebuf; len > 0; len -= reclen) {
888 bdp = (struct dirent *)inp;
889 reclen = bdp->d_reclen;
890 if (reclen & 3)
891 panic("linux_readdir");
892 if (bdp->d_fileno == 0) {
893 inp += reclen; /* it is a hole; squish it out */
894 off = *cookie++;
895 continue;
896 }
897 linux_reclen = LINUX_RECLEN(&idb, bdp->d_namlen);
898 if (reclen > len || resid < linux_reclen) {
899 /* entry too big for buffer, so just stop */
900 outp++;
901 break;
902 }
903 /*
904 * Massage in place to make a Linux-shaped dirent (otherwise
905 * we have to worry about touching user memory outside of
906 * the copyout() call).
907 */
908 idb.d_ino = (linux_ino_t)bdp->d_fileno;
909 /*
910 * The old readdir() call misuses the offset and reclen fields.
911 */
912 if (oldcall) {
913 idb.d_off = (linux_off_t)linux_reclen;
914 idb.d_reclen = (u_short)bdp->d_namlen;
915 } else {
916 if (sizeof (linux_off_t) < 4 && (off >> 32) != 0) {
917 compat_offseterr(vp, "linux_getdents");
918 error = EINVAL;
919 goto out;
920 }
921 idb.d_off = (linux_off_t)off;
922 idb.d_reclen = (u_short)linux_reclen;
923 }
924 strcpy(idb.d_name, bdp->d_name);
925 if ((error = copyout((caddr_t)&idb, outp, linux_reclen)))
926 goto out;
927 /* advance past this real entry */
928 inp += reclen;
929 off = *cookie++; /* each entry points to itself */
930 /* advance output past Linux-shaped entry */
931 outp += linux_reclen;
932 resid -= linux_reclen;
933 if (oldcall)
934 break;
935 }
936
937 /* if we squished out the whole block, try again */
938 if (outp == SCARG(uap, dent))
939 goto again;
940 fp->f_offset = off; /* update the vnode offset */
941
942 if (oldcall)
943 nbytes = resid + linux_reclen;
944
945 eof:
946 *retval = nbytes - resid;
947 out:
948 VOP_UNLOCK(vp, 0);
949 if (cookiebuf)
950 free(cookiebuf, M_TEMP);
951 free(buf, M_TEMP);
952 return error;
953 }
954
955 /*
956 * Not sure why the arguments to this older version of select() were put
957 * into a structure, because there are 5, and that can all be handled
958 * in registers on the i386 like Linux wants to.
959 */
960 int
961 linux_sys_oldselect(p, v, retval)
962 struct proc *p;
963 void *v;
964 register_t *retval;
965 {
966 struct linux_sys_oldselect_args /* {
967 syscallarg(struct linux_select *) lsp;
968 } */ *uap = v;
969 struct linux_select ls;
970 int error;
971
972 if ((error = copyin(SCARG(uap, lsp), &ls, sizeof(ls))))
973 return error;
974
975 return linux_select1(p, retval, ls.nfds, ls.readfds, ls.writefds,
976 ls.exceptfds, ls.timeout);
977 }
978
979 /*
980 * Even when just using registers to pass arguments to syscalls you can
981 * have 5 of them on the i386. So this newer version of select() does
982 * this.
983 */
984 int
985 linux_sys_select(p, v, retval)
986 struct proc *p;
987 void *v;
988 register_t *retval;
989 {
990 struct linux_sys_select_args /* {
991 syscallarg(int) nfds;
992 syscallarg(fd_set *) readfds;
993 syscallarg(fd_set *) writefds;
994 syscallarg(fd_set *) exceptfds;
995 syscallarg(struct timeval *) timeout;
996 } */ *uap = v;
997
998 return linux_select1(p, retval, SCARG(uap, nfds), SCARG(uap, readfds),
999 SCARG(uap, writefds), SCARG(uap, exceptfds), SCARG(uap, timeout));
1000 }
1001
1002 /*
1003 * Common code for the old and new versions of select(). A couple of
1004 * things are important:
1005 * 1) return the amount of time left in the 'timeout' parameter
1006 * 2) select never returns ERESTART on Linux, always return EINTR
1007 */
1008 int
1009 linux_select1(p, retval, nfds, readfds, writefds, exceptfds, timeout)
1010 struct proc *p;
1011 register_t *retval;
1012 int nfds;
1013 fd_set *readfds, *writefds, *exceptfds;
1014 struct timeval *timeout;
1015 {
1016 struct sys_select_args bsa;
1017 struct timeval tv0, tv1, utv, *tvp;
1018 caddr_t sg;
1019 int error;
1020
1021 SCARG(&bsa, nd) = nfds;
1022 SCARG(&bsa, in) = readfds;
1023 SCARG(&bsa, ou) = writefds;
1024 SCARG(&bsa, ex) = exceptfds;
1025 SCARG(&bsa, tv) = timeout;
1026
1027 /*
1028 * Store current time for computation of the amount of
1029 * time left.
1030 */
1031 if (timeout) {
1032 if ((error = copyin(timeout, &utv, sizeof(utv))))
1033 return error;
1034 if (itimerfix(&utv)) {
1035 /*
1036 * The timeval was invalid. Convert it to something
1037 * valid that will act as it does under Linux.
1038 */
1039 sg = stackgap_init(p->p_emul);
1040 tvp = stackgap_alloc(&sg, sizeof(utv));
1041 utv.tv_sec += utv.tv_usec / 1000000;
1042 utv.tv_usec %= 1000000;
1043 if (utv.tv_usec < 0) {
1044 utv.tv_sec -= 1;
1045 utv.tv_usec += 1000000;
1046 }
1047 if (utv.tv_sec < 0)
1048 timerclear(&utv);
1049 if ((error = copyout(&utv, tvp, sizeof(utv))))
1050 return error;
1051 SCARG(&bsa, tv) = tvp;
1052 }
1053 microtime(&tv0);
1054 }
1055
1056 error = sys_select(p, &bsa, retval);
1057 if (error) {
1058 /*
1059 * See fs/select.c in the Linux kernel. Without this,
1060 * Maelstrom doesn't work.
1061 */
1062 if (error == ERESTART)
1063 error = EINTR;
1064 return error;
1065 }
1066
1067 if (timeout) {
1068 if (*retval) {
1069 /*
1070 * Compute how much time was left of the timeout,
1071 * by subtracting the current time and the time
1072 * before we started the call, and subtracting
1073 * that result from the user-supplied value.
1074 */
1075 microtime(&tv1);
1076 timersub(&tv1, &tv0, &tv1);
1077 timersub(&utv, &tv1, &utv);
1078 if (utv.tv_sec < 0)
1079 timerclear(&utv);
1080 } else
1081 timerclear(&utv);
1082 if ((error = copyout(&utv, timeout, sizeof(utv))))
1083 return error;
1084 }
1085
1086 return 0;
1087 }
1088
1089 /*
1090 * Get the process group of a certain process. Look it up
1091 * and return the value.
1092 */
1093 int
1094 linux_sys_getpgid(p, v, retval)
1095 struct proc *p;
1096 void *v;
1097 register_t *retval;
1098 {
1099 struct linux_sys_getpgid_args /* {
1100 syscallarg(int) pid;
1101 } */ *uap = v;
1102 struct proc *targp;
1103
1104 if (SCARG(uap, pid) != 0 && SCARG(uap, pid) != p->p_pid) {
1105 if ((targp = pfind(SCARG(uap, pid))) == 0)
1106 return ESRCH;
1107 }
1108 else
1109 targp = p;
1110
1111 retval[0] = targp->p_pgid;
1112 return 0;
1113 }
1114
1115 /*
1116 * Set the 'personality' (emulation mode) for the current process. Only
1117 * accept the Linux personality here (0). This call is needed because
1118 * the Linux ELF crt0 issues it in an ugly kludge to make sure that
1119 * ELF binaries run in Linux mode, not SVR4 mode.
1120 */
1121 int
1122 linux_sys_personality(p, v, retval)
1123 struct proc *p;
1124 void *v;
1125 register_t *retval;
1126 {
1127 struct linux_sys_personality_args /* {
1128 syscallarg(int) per;
1129 } */ *uap = v;
1130
1131 if (SCARG(uap, per) != 0)
1132 return EINVAL;
1133 retval[0] = 0;
1134 return 0;
1135 }
1136
1137 /*
1138 * The calls are here because of type conversions.
1139 */
1140 int
1141 linux_sys_setreuid(p, v, retval)
1142 struct proc *p;
1143 void *v;
1144 register_t *retval;
1145 {
1146 struct linux_sys_setreuid_args /* {
1147 syscallarg(int) ruid;
1148 syscallarg(int) euid;
1149 } */ *uap = v;
1150 struct sys_setreuid_args bsa;
1151
1152 SCARG(&bsa, ruid) = ((linux_uid_t)SCARG(uap, ruid) == (linux_uid_t)-1) ?
1153 (uid_t)-1 : SCARG(uap, ruid);
1154 SCARG(&bsa, euid) = ((linux_uid_t)SCARG(uap, euid) == (linux_uid_t)-1) ?
1155 (uid_t)-1 : SCARG(uap, euid);
1156
1157 return sys_setreuid(p, &bsa, retval);
1158 }
1159
1160 int
1161 linux_sys_setregid(p, v, retval)
1162 struct proc *p;
1163 void *v;
1164 register_t *retval;
1165 {
1166 struct linux_sys_setregid_args /* {
1167 syscallarg(int) rgid;
1168 syscallarg(int) egid;
1169 } */ *uap = v;
1170 struct sys_setregid_args bsa;
1171
1172 SCARG(&bsa, rgid) = ((linux_gid_t)SCARG(uap, rgid) == (linux_gid_t)-1) ?
1173 (uid_t)-1 : SCARG(uap, rgid);
1174 SCARG(&bsa, egid) = ((linux_gid_t)SCARG(uap, egid) == (linux_gid_t)-1) ?
1175 (uid_t)-1 : SCARG(uap, egid);
1176
1177 return sys_setregid(p, &bsa, retval);
1178 }
1179
1180 int
1181 linux_sys___sysctl(p, v, retval)
1182 struct proc *p;
1183 void *v;
1184 register_t *retval;
1185 {
1186 struct linux_sys___sysctl_args /* {
1187 syscallarg(struct linux___sysctl *) lsp;
1188 } */ *uap = v;
1189 struct linux___sysctl ls;
1190 struct sys___sysctl_args bsa;
1191 int error;
1192
1193 if ((error = copyin(SCARG(uap, lsp), &ls, sizeof ls)))
1194 return error;
1195 SCARG(&bsa, name) = ls.name;
1196 SCARG(&bsa, namelen) = ls.namelen;
1197 SCARG(&bsa, old) = ls.old;
1198 SCARG(&bsa, oldlenp) = ls.oldlenp;
1199 SCARG(&bsa, new) = ls.new;
1200 SCARG(&bsa, newlen) = ls.newlen;
1201
1202 return sys___sysctl(p, &bsa, retval);
1203 }
1204
1205 int
1206 linux_sys_nice(p, v, retval)
1207 struct proc *p;
1208 void *v;
1209 register_t *retval;
1210 {
1211 struct linux_sys_nice_args /* {
1212 syscallarg(int) incr;
1213 } */ *uap = v;
1214 struct sys_setpriority_args bsa;
1215
1216 SCARG(&bsa, which) = PRIO_PROCESS;
1217 SCARG(&bsa, who) = 0;
1218 SCARG(&bsa, prio) = SCARG(uap, incr);
1219 return sys_setpriority(p, &bsa, retval);
1220 }
1221