linux_misc_notalpha.c revision 1.20 1 /* $NetBSD: linux_misc_notalpha.c,v 1.20 1995/09/19 22:37:33 thorpej 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/dir.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 /*
80 * The information on a terminated (or stopped) process needs
81 * to be converted in order for Linux binaries to get a valid signal
82 * number out of it.
83 */
84 static int
85 bsd_to_linux_wstat(status)
86 int *status;
87 {
88 if (WIFSIGNALED(*status))
89 *status = (*status & ~0177) |
90 bsd_to_linux_sig[WTERMSIG(*status)];
91 else if (WIFSTOPPED(*status))
92 *status = (*status & ~0xff00) |
93 (bsd_to_linux_sig[WSTOPSIG(*status)] << 8);
94 }
95
96 /*
97 * waitpid(2). Passed on to the NetBSD call, surrounded by code to
98 * reserve some space for a NetBSD-style wait status, and converting
99 * it to what Linux wants.
100 */
101 int
102 linux_waitpid(p, v, retval)
103 struct proc *p;
104 void *v;
105 register_t *retval;
106 {
107 struct linux_waitpid_args /* {
108 syscallarg(int) pid;
109 syscallarg(int *) status;
110 syscallarg(int) options;
111 } */ *uap = v;
112 struct wait4_args w4a;
113 int error, *status, tstat;
114 caddr_t sg;
115
116 if (SCARG(uap, status) != NULL) {
117 sg = stackgap_init(p->p_emul);
118 status = (int *) stackgap_alloc(&sg, sizeof status);
119 } else
120 status = NULL;
121
122 SCARG(&w4a, pid) = SCARG(uap, pid);
123 SCARG(&w4a, status) = status;
124 SCARG(&w4a, options) = SCARG(uap, options);
125 SCARG(&w4a, rusage) = NULL;
126
127 if ((error = wait4(p, &w4a, retval)))
128 return error;
129
130 p->p_siglist &= ~sigmask(SIGCHLD);
131
132 if (status != NULL) {
133 if ((error = copyin(status, &tstat, sizeof tstat)))
134 return error;
135
136 bsd_to_linux_wstat(&tstat);
137
138 return copyout(&tstat, SCARG(uap, status), sizeof tstat);
139 }
140
141 return 0;
142 }
143
144 /*
145 * This is very much the same as waitpid()
146 */
147 int
148 linux_wait4(p, v, retval)
149 struct proc *p;
150 void *v;
151 register_t *retval;
152 {
153 struct linux_wait4_args /* {
154 syscallarg(int) pid;
155 syscallarg(int *) status;
156 syscallarg(int) options;
157 syscallarg(struct rusage *) rusage;
158 } */ *uap = v;
159 struct wait4_args w4a;
160 int error, *status, tstat;
161 caddr_t sg;
162
163 if (SCARG(uap, status) != NULL) {
164 sg = stackgap_init(p->p_emul);
165 status = (int *) stackgap_alloc(&sg, sizeof status);
166 } else
167 status = NULL;
168
169 SCARG(&w4a, pid) = SCARG(uap, pid);
170 SCARG(&w4a, status) = status;
171 SCARG(&w4a, options) = SCARG(uap, options);
172 SCARG(&w4a, rusage) = SCARG(uap, rusage);
173
174 if ((error = wait4(p, &w4a, retval)))
175 return error;
176
177 p->p_siglist &= ~sigmask(SIGCHLD);
178
179 if (status != NULL) {
180 if ((error = copyin(status, &tstat, sizeof tstat)))
181 return error;
182
183 bsd_to_linux_wstat(&tstat);
184
185 return copyout(&tstat, SCARG(uap, status), sizeof tstat);
186 }
187
188 return 0;
189 }
190
191 /*
192 * This is the old brk(2) call. I don't think anything in the Linux
193 * world uses this anymore
194 */
195 int
196 linux_break(p, v, retval)
197 struct proc *p;
198 void *v;
199 register_t *retval;
200 {
201 struct linux_brk_args /* {
202 syscallarg(char *) nsize;
203 } */ *uap = v;
204
205 return ENOSYS;
206 }
207
208 /*
209 * Linux brk(2). The check if the new address is >= the old one is
210 * done in the kernel in Linux. NetBSD does it in the library.
211 */
212 int
213 linux_brk(p, v, retval)
214 struct proc *p;
215 void *v;
216 register_t *retval;
217 {
218 struct linux_brk_args /* {
219 syscallarg(char *) nsize;
220 } */ *uap = v;
221 char *nbrk = SCARG(uap, nsize);
222 struct obreak_args oba;
223 struct vmspace *vm = p->p_vmspace;
224 int error = 0;
225 caddr_t oldbrk, newbrk;
226
227 oldbrk = vm->vm_daddr + ctob(vm->vm_dsize);
228 /*
229 * XXX inconsistent.. Linux always returns at least the old
230 * brk value, but it will be page-aligned if this fails,
231 * and possibly not page aligned if it succeeds (the user
232 * supplied pointer is returned).
233 */
234 SCARG(&oba, nsize) = nbrk;
235
236 if ((caddr_t) nbrk > vm->vm_daddr && obreak(p, &oba, retval) == 0)
237 retval[0] = (register_t) nbrk;
238 else
239 retval[0] = (register_t) oldbrk;
240
241 return 0;
242 }
243
244 /*
245 * I wonder why Linux has gettimeofday() _and_ time().. Still, we
246 * need to deal with it.
247 */
248 int
249 linux_time(p, v, retval)
250 struct proc *p;
251 void *v;
252 register_t *retval;
253 {
254 struct linux_time_args /* {
255 linux_time_t *t;
256 } */ *uap = v;
257 struct timeval atv;
258 linux_time_t tt;
259 int error;
260
261 microtime(&atv);
262
263 tt = atv.tv_sec;
264 if (SCARG(uap, t) && (error = copyout(&tt, SCARG(uap, t), sizeof tt)))
265 return error;
266
267 retval[0] = tt;
268 return 0;
269 }
270
271 /*
272 * Convert BSD statfs structure to Linux statfs structure.
273 * The Linux structure has less fields, and it also wants
274 * the length of a name in a dir entry in a field, which
275 * we fake (probably the wrong way).
276 */
277 static void
278 bsd_to_linux_statfs(bsp, lsp)
279 struct statfs *bsp;
280 struct linux_statfs *lsp;
281 {
282 lsp->l_ftype = bsp->f_type;
283 lsp->l_fbsize = bsp->f_bsize;
284 lsp->l_fblocks = bsp->f_blocks;
285 lsp->l_fbfree = bsp->f_bfree;
286 lsp->l_fbavail = bsp->f_bavail;
287 lsp->l_ffiles = bsp->f_files;
288 lsp->l_fffree = bsp->f_ffree;
289 lsp->l_ffsid.val[0] = bsp->f_fsid.val[0];
290 lsp->l_ffsid.val[1] = bsp->f_fsid.val[1];
291 lsp->l_fnamelen = MAXNAMLEN; /* XXX */
292 }
293
294 /*
295 * Implement the fs stat functions. Straightforward.
296 */
297 int
298 linux_statfs(p, v, retval)
299 struct proc *p;
300 void *v;
301 register_t *retval;
302 {
303 struct linux_statfs_args /* {
304 syscallarg(char *) path;
305 syscallarg(struct linux_statfs *) sp;
306 } */ *uap = v;
307 struct statfs btmp, *bsp;
308 struct linux_statfs ltmp;
309 struct statfs_args bsa;
310 caddr_t sg;
311 int error;
312
313 sg = stackgap_init(p->p_emul);
314 bsp = (struct statfs *) stackgap_alloc(&sg, sizeof (struct statfs));
315
316 LINUX_CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
317
318 SCARG(&bsa, path) = SCARG(uap, path);
319 SCARG(&bsa, buf) = bsp;
320
321 if ((error = statfs(p, &bsa, retval)))
322 return error;
323
324 if ((error = copyin((caddr_t) bsp, (caddr_t) &btmp, sizeof btmp)))
325 return error;
326
327 bsd_to_linux_statfs(&btmp, <mp);
328
329 return copyout((caddr_t) <mp, (caddr_t) SCARG(uap, sp), sizeof ltmp);
330 }
331
332 int
333 linux_fstatfs(p, v, retval)
334 struct proc *p;
335 void *v;
336 register_t *retval;
337 {
338 struct linux_fstatfs_args /* {
339 syscallarg(int) fd;
340 syscallarg(struct linux_statfs *) sp;
341 } */ *uap = v;
342 struct statfs btmp, *bsp;
343 struct linux_statfs ltmp;
344 struct fstatfs_args bsa;
345 caddr_t sg;
346 int error;
347
348 sg = stackgap_init(p->p_emul);
349 bsp = (struct statfs *) stackgap_alloc(&sg, sizeof (struct statfs));
350
351 SCARG(&bsa, fd) = SCARG(uap, fd);
352 SCARG(&bsa, buf) = bsp;
353
354 if ((error = statfs(p, &bsa, retval)))
355 return error;
356
357 if ((error = copyin((caddr_t) bsp, (caddr_t) &btmp, sizeof btmp)))
358 return error;
359
360 bsd_to_linux_statfs(&btmp, <mp);
361
362 return copyout((caddr_t) <mp, (caddr_t) SCARG(uap, sp), sizeof ltmp);
363 }
364
365 /*
366 * uname(). Just copy the info from the various strings stored in the
367 * kernel, and put it in the Linux utsname structure. That structure
368 * is almost the same as the NetBSD one, only it has fields 65 characters
369 * long, and an extra domainname field.
370 */
371 int
372 linux_uname(p, v, retval)
373 struct proc *p;
374 void *v;
375 register_t *retval;
376 {
377 struct linux_uname_args /* {
378 syscallarg(struct linux_utsname *) up;
379 } */ *uap = v;
380 extern char ostype[], hostname[], osrelease[], version[], machine[],
381 domainname[];
382 struct linux_utsname luts;
383 int len;
384 char *cp;
385
386 strncpy(luts.l_sysname, ostype, sizeof(luts.l_sysname));
387 strncpy(luts.l_nodename, hostname, sizeof(luts.l_nodename));
388 strncpy(luts.l_release, osrelease, sizeof(luts.l_release));
389 strncpy(luts.l_version, version, sizeof(luts.l_version));
390 strncpy(luts.l_machine, machine, sizeof(luts.l_machine));
391 strncpy(luts.l_domainname, domainname, sizeof(luts.l_domainname));
392
393 /* This part taken from the the uname() in libc */
394 len = sizeof(luts.l_version);
395 for (cp = luts.l_version; len--; ++cp)
396 if (*cp == '\n' || *cp == '\t')
397 if (len > 1)
398 *cp = ' ';
399 else
400 *cp = '\0';
401
402 return copyout(&luts, SCARG(uap, up), sizeof(luts));
403 }
404
405 int
406 linux_olduname(p, v, retval)
407 struct proc *p;
408 void *v;
409 register_t *retval;
410 {
411 struct linux_uname_args /* {
412 syscallarg(struct linux_oldutsname *) up;
413 } */ *uap = v;
414 extern char ostype[], hostname[], osrelease[], version[], machine[];
415 struct linux_oldutsname luts;
416 int len;
417 char *cp;
418
419 strncpy(luts.l_sysname, ostype, sizeof(luts.l_sysname));
420 strncpy(luts.l_nodename, hostname, sizeof(luts.l_nodename));
421 strncpy(luts.l_release, osrelease, sizeof(luts.l_release));
422 strncpy(luts.l_version, version, sizeof(luts.l_version));
423 strncpy(luts.l_machine, machine, sizeof(luts.l_machine));
424
425 /* This part taken from the the uname() in libc */
426 len = sizeof(luts.l_version);
427 for (cp = luts.l_version; len--; ++cp)
428 if (*cp == '\n' || *cp == '\t')
429 if (len > 1)
430 *cp = ' ';
431 else
432 *cp = '\0';
433
434 return copyout(&luts, SCARG(uap, up), sizeof(luts));
435 }
436
437 int
438 linux_oldolduname(p, v, retval)
439 struct proc *p;
440 void *v;
441 register_t *retval;
442 {
443 struct linux_uname_args /* {
444 syscallarg(struct linux_oldoldutsname *) up;
445 } */ *uap = v;
446 extern char ostype[], hostname[], osrelease[], version[], machine[];
447 struct linux_oldoldutsname luts;
448 int len;
449 char *cp;
450
451 strncpy(luts.l_sysname, ostype, sizeof(luts.l_sysname));
452 strncpy(luts.l_nodename, hostname, sizeof(luts.l_nodename));
453 strncpy(luts.l_release, osrelease, sizeof(luts.l_release));
454 strncpy(luts.l_version, version, sizeof(luts.l_version));
455 strncpy(luts.l_machine, machine, sizeof(luts.l_machine));
456
457 /* This part taken from the the uname() in libc */
458 len = sizeof(luts.l_version);
459 for (cp = luts.l_version; len--; ++cp)
460 if (*cp == '\n' || *cp == '\t')
461 if (len > 1)
462 *cp = ' ';
463 else
464 *cp = '\0';
465
466 return copyout(&luts, SCARG(uap, up), sizeof(luts));
467 }
468
469 /*
470 * Linux wants to pass everything to a syscall in registers. However,
471 * mmap() has 6 of them. Oops: out of register error. They just pass
472 * everything in a structure.
473 */
474 int
475 linux_mmap(p, v, retval)
476 struct proc *p;
477 void *v;
478 register_t *retval;
479 {
480 struct linux_mmap_args /* {
481 syscallarg(struct linux_mmap *) lmp;
482 } */ *uap = v;
483 struct linux_mmap lmap;
484 struct mmap_args cma;
485 int error, flags;
486
487 if ((error = copyin(SCARG(uap, lmp), &lmap, sizeof lmap)))
488 return error;
489
490 flags = 0;
491 flags |= cvtto_bsd_mask(lmap.lm_flags, LINUX_MAP_SHARED, MAP_SHARED);
492 flags |= cvtto_bsd_mask(lmap.lm_flags, LINUX_MAP_PRIVATE, MAP_PRIVATE);
493 flags |= cvtto_bsd_mask(lmap.lm_flags, LINUX_MAP_FIXED, MAP_FIXED);
494 flags |= cvtto_bsd_mask(lmap.lm_flags, LINUX_MAP_ANON, MAP_ANON);
495
496 SCARG(&cma,addr) = lmap.lm_addr;
497 SCARG(&cma,len) = lmap.lm_len;
498 SCARG(&cma,prot) = lmap.lm_prot;
499 SCARG(&cma,flags) = flags;
500 SCARG(&cma,fd) = lmap.lm_fd;
501 SCARG(&cma,pad) = 0;
502 SCARG(&cma,pos) = lmap.lm_pos;
503
504 return mmap(p, &cma, retval);
505 }
506
507 /*
508 * Linux doesn't use the retval[1] value to determine whether
509 * we are the child or parent.
510 */
511 int
512 linux_fork(p, uap, retval)
513 struct proc *p;
514 void *uap;
515 register_t *retval;
516 {
517 int error;
518
519 if ((error = fork(p, uap, retval)))
520 return error;
521
522 if (retval[1] == 1)
523 retval[0] = 0;
524
525 return 0;
526 }
527
528 /*
529 * This code is partly stolen from src/lib/libc/compat-43/times.c
530 * XXX - CLK_TCK isn't declared in /sys, just in <time.h>, done here
531 */
532
533 #define CLK_TCK 100
534 #define CONVTCK(r) (r.tv_sec * CLK_TCK + r.tv_usec / (1000000 / CLK_TCK))
535
536 int
537 linux_times(p, v, retval)
538 struct proc *p;
539 void *v;
540 register_t *retval;
541 {
542 struct linux_times_args /* {
543 syscallarg(struct times *) tms;
544 } */ *uap = v;
545 struct timeval t;
546 struct linux_tms ltms;
547 struct rusage ru;
548 int error, s;
549
550 calcru(p, &ru.ru_utime, &ru.ru_stime, NULL);
551 ltms.ltms_utime = CONVTCK(ru.ru_utime);
552 ltms.ltms_stime = CONVTCK(ru.ru_stime);
553
554 ltms.ltms_cutime = CONVTCK(p->p_stats->p_cru.ru_utime);
555 ltms.ltms_cstime = CONVTCK(p->p_stats->p_cru.ru_stime);
556
557 if ((error = copyout(<ms, SCARG(uap, tms), sizeof ltms)))
558 return error;
559
560 s = splclock();
561 timersub(&time, &boottime, &t);
562 splx(s);
563
564 retval[0] = ((linux_clock_t)(CONVTCK(t)));
565 return 0;
566 }
567
568 /*
569 * NetBSD passes fd[0] in retval[0], and fd[1] in retval[1].
570 * Linux directly passes the pointer.
571 */
572 int
573 linux_pipe(p, v, retval)
574 struct proc *p;
575 void *v;
576 register_t *retval;
577 {
578 struct linux_pipe_args /* {
579 syscallarg(int *) pfds;
580 } */ *uap = v;
581 int error;
582
583 if ((error = pipe(p, 0, retval)))
584 return error;
585
586 /* Assumes register_t is an int */
587
588 if ((error = copyout(retval, SCARG(uap, pfds), 2 * sizeof (int))))
589 return error;
590
591 retval[0] = 0;
592 return 0;
593 }
594
595 /*
596 * Alarm. This is a libc call which used setitimer(2) in NetBSD.
597 * Fiddle with the timers to make it work.
598 */
599 int
600 linux_alarm(p, v, retval)
601 struct proc *p;
602 void *v;
603 register_t *retval;
604 {
605 struct linux_alarm_args /* {
606 syscallarg(unsigned int) secs;
607 } */ *uap = v;
608 int error, s;
609 struct itimerval *itp, it;
610
611 itp = &p->p_realtimer;
612 s = splclock();
613 /*
614 * Clear any pending timer alarms.
615 */
616 untimeout(realitexpire, p);
617 timerclear(&itp->it_interval);
618 if (timerisset(&itp->it_value) &&
619 timercmp(&itp->it_value, &time, >))
620 timersub(&itp->it_value, &time, &itp->it_value);
621 /*
622 * Return how many seconds were left (rounded up)
623 */
624 retval[0] = itp->it_value.tv_sec;
625 if (itp->it_value.tv_usec)
626 retval[0]++;
627
628 /*
629 * alarm(0) just resets the timer.
630 */
631 if (SCARG(uap, secs) == 0) {
632 timerclear(&itp->it_value);
633 splx(s);
634 return 0;
635 }
636
637 /*
638 * Check the new alarm time for sanity, and set it.
639 */
640 timerclear(&it.it_interval);
641 it.it_value.tv_sec = SCARG(uap, secs);
642 it.it_value.tv_usec = 0;
643 if (itimerfix(&it.it_value) || itimerfix(&it.it_interval)) {
644 splx(s);
645 return (EINVAL);
646 }
647
648 if (timerisset(&it.it_value)) {
649 timeradd(&it.it_value, &time, &it.it_value);
650 timeout(realitexpire, p, hzto(&it.it_value));
651 }
652 p->p_realtimer = it;
653 splx(s);
654
655 return 0;
656 }
657
658 /*
659 * utime(). Do conversion to things that utimes() understands,
660 * and pass it on.
661 */
662 int
663 linux_utime(p, v, retval)
664 struct proc *p;
665 void *v;
666 register_t *retval;
667 {
668 struct linux_utime_args /* {
669 syscallarg(char *) path;
670 syscallarg(struct linux_utimbuf *)times;
671 } */ *uap = v;
672 caddr_t sg;
673 int error;
674 struct utimes_args ua;
675 struct timeval tv[2], *tvp;
676 struct linux_utimbuf lut;
677
678 sg = stackgap_init(p->p_emul);
679 LINUX_CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
680
681 SCARG(&ua, path) = SCARG(uap, path);
682
683 if (SCARG(uap, times) != NULL) {
684 if ((error = copyin(SCARG(uap, times), &lut, sizeof lut)))
685 return error;
686 tv[0].tv_usec = tv[1].tv_usec = 0;
687 tv[0].tv_sec = lut.l_actime;
688 tv[1].tv_sec = lut.l_modtime;
689 tvp = (struct timeval *) stackgap_alloc(&sg, sizeof(tv));
690 if ((error = copyout(tv, tvp, sizeof tv)))
691 return error;
692 SCARG(&ua, tptr) = tvp;
693 }
694 else
695 SCARG(&ua, tptr) = NULL;
696
697 return utimes(p, uap, retval);
698 }
699
700 /*
701 * The old Linux readdir was only able to read one entry at a time,
702 * even though it had a 'count' argument. In fact, the emulation
703 * of the old call was better than the original, because it did handle
704 * the count arg properly. Don't bother with it anymore now, and use
705 * it to distinguish between old and new. The difference is that the
706 * newer one actually does multiple entries, and the reclen field
707 * really is the reclen, not the namelength.
708 */
709 int
710 linux_readdir(p, v, retval)
711 struct proc *p;
712 void *v;
713 register_t *retval;
714 {
715 struct linux_readdir_args /* {
716 syscallarg(int) fd;
717 syscallarg(struct linux_dirent *) dent;
718 syscallarg(unsigned int) count;
719 } */ *uap = v;
720
721 SCARG(uap, count) = 1;
722 return linux_getdents(p, uap, retval);
723 }
724
725 /*
726 * Linux 'readdir' call. This code is mostly taken from the
727 * SunOS getdents call (see compat/sunos/sunos_misc.c), though
728 * an attempt has been made to keep it a little cleaner (failing
729 * miserably, because of the cruft needed if count 1 is passed).
730 *
731 * The d_off field should contain the offset of the next valid entry,
732 * but in Linux it has the offset of the entry itself. We emulate
733 * that bug here.
734 *
735 * Read in BSD-style entries, convert them, and copy them out.
736 *
737 * Note that this doesn't handle union-mounted filesystems.
738 */
739 int
740 linux_getdents(p, v, retval)
741 struct proc *p;
742 void *v;
743 register_t *retval;
744 {
745 struct linux_readdir_args /* {
746 syscallarg(int) fd;
747 syscallarg(struct linux_dirent *) dent;
748 syscallarg(unsigned int) count;
749 } */ *uap = v;
750 register struct dirent *bdp;
751 struct vnode *vp;
752 caddr_t inp, buf; /* BSD-format */
753 int len, reclen; /* BSD-format */
754 caddr_t outp; /* Linux-format */
755 int resid, linuxreclen; /* Linux-format */
756 struct file *fp;
757 struct uio auio;
758 struct iovec aiov;
759 struct linux_dirent idb;
760 off_t off; /* true file offset */
761 linux_off_t soff; /* Linux file offset */
762 int buflen, error, eofflag, nbytes, oldcall;
763 struct vattr va;
764
765 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
766 return (error);
767
768 if ((fp->f_flag & FREAD) == 0)
769 return (EBADF);
770
771 vp = (struct vnode *)fp->f_data;
772
773 if (vp->v_type != VDIR) /* XXX vnode readdir op should do this */
774 return (EINVAL);
775
776 if ((error = VOP_GETATTR(vp, &va, p->p_ucred, p)))
777 return error;
778
779 nbytes = SCARG(uap, count);
780 if (nbytes == 1) { /* emulating old, broken behaviour */
781 nbytes = sizeof (struct linux_dirent);
782 buflen = max(va.va_blocksize, nbytes);
783 oldcall = 1;
784 } else {
785 buflen = min(MAXBSIZE, nbytes);
786 oldcall = 0;
787 }
788 buf = malloc(buflen, M_TEMP, M_WAITOK);
789 VOP_LOCK(vp);
790 off = fp->f_offset;
791 again:
792 aiov.iov_base = buf;
793 aiov.iov_len = buflen;
794 auio.uio_iov = &aiov;
795 auio.uio_iovcnt = 1;
796 auio.uio_rw = UIO_READ;
797 auio.uio_segflg = UIO_SYSSPACE;
798 auio.uio_procp = p;
799 auio.uio_resid = buflen;
800 auio.uio_offset = off;
801 /*
802 * First we read into the malloc'ed buffer, then
803 * we massage it into user space, one record at a time.
804 */
805 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, (u_long *)0, 0);
806 if (error)
807 goto out;
808
809 inp = buf;
810 outp = (caddr_t) SCARG(uap, dent);
811 resid = nbytes;
812 if ((len = buflen - auio.uio_resid) == 0)
813 goto eof;
814
815 for (; len > 0; len -= reclen) {
816 bdp = (struct dirent *)inp;
817 reclen = bdp->d_reclen;
818 if (reclen & 3)
819 panic("linux_readdir");
820 off += reclen;
821 if (bdp->d_fileno == 0) {
822 inp += reclen; /* it is a hole; squish it out */
823 continue;
824 }
825 linuxreclen = LINUX_RECLEN(&idb, bdp->d_namlen);
826 if (reclen > len || resid < linuxreclen) {
827 /* entry too big for buffer, so just stop */
828 outp++;
829 break;
830 }
831 /*
832 * Massage in place to make a Linux-shaped dirent (otherwise
833 * we have to worry about touching user memory outside of
834 * the copyout() call).
835 */
836 idb.d_ino = (long)bdp->d_fileno;
837 idb.d_off = off - reclen;
838 /*
839 * The old readdir() call used the reclen field as namlen.
840 */
841 idb.d_reclen = oldcall ? (u_short)bdp->d_namlen : linuxreclen;
842 strcpy(idb.d_name, bdp->d_name);
843 if ((error = copyout((caddr_t)&idb, outp, linuxreclen)))
844 goto out;
845 /* advance past this real entry */
846 inp += reclen;
847 /* advance output past Linux-shaped entry */
848 outp += linuxreclen;
849 resid -= linuxreclen;
850 if (oldcall)
851 break;
852 }
853
854 /* if we squished out the whole block, try again */
855 if (outp == (caddr_t) SCARG(uap, dent))
856 goto again;
857 fp->f_offset = off; /* update the vnode offset */
858
859 if (oldcall)
860 nbytes = resid + linuxreclen;
861
862 eof:
863 *retval = nbytes - resid;
864 out:
865 VOP_UNLOCK(vp);
866 free(buf, M_TEMP);
867 return error;
868 }
869
870 /*
871 * Not sure why the arguments to this older version of select() were put
872 * into a structure, because there are 5, and that can all be handled
873 * in registers on the i386 like Linux wants to.
874 */
875 int
876 linux_oldselect(p, v, retval)
877 struct proc *p;
878 void *v;
879 register_t *retval;
880 {
881 struct linux_oldselect_args /* {
882 syscallarg(struct linux_select *) lsp;
883 } */ *uap = v;
884 struct linux_select ls;
885 int error;
886
887 if ((error = copyin(SCARG(uap, lsp), &ls, sizeof(ls))))
888 return error;
889
890 return linux_select1(p, retval, ls.nfds, ls.readfds, ls.writefds,
891 ls.exceptfds, ls.timeout);
892 }
893
894 /*
895 * Even when just using registers to pass arguments to syscalls you can
896 * have 5 of them on the i386. So this newer version of select() does
897 * this.
898 */
899 int
900 linux_select(p, v, retval)
901 struct proc *p;
902 void *v;
903 register_t *retval;
904 {
905 struct linux_select_args /* {
906 syscallarg(int) nfds;
907 syscallarg(fd_set *) readfds;
908 syscallarg(fd_set *) writefds;
909 syscallarg(fd_set *) exceptfds;
910 syscallarg(struct timeval *) timeout;
911 } */ *uap = v;
912
913 return linux_select1(p, retval, SCARG(uap, nfds), SCARG(uap, readfds),
914 SCARG(uap, writefds), SCARG(uap, exceptfds), SCARG(uap, timeout));
915 }
916
917 /*
918 * Common code for the old and new versions of select(). A couple of
919 * things are important:
920 * 1) return the amount of time left in the 'timeout' parameter
921 * 2) select never returns ERESTART on Linux, always return EINTR
922 */
923 int
924 linux_select1(p, retval, nfds, readfds, writefds, exceptfds, timeout)
925 struct proc *p;
926 register_t *retval;
927 int nfds;
928 fd_set *readfds, *writefds, *exceptfds;
929 struct timeval *timeout;
930 {
931 struct select_args bsa;
932 struct timeval tv0, tv1, utv, *tvp;
933 caddr_t sg;
934 int error;
935
936 SCARG(&bsa, nd) = nfds;
937 SCARG(&bsa, in) = readfds;
938 SCARG(&bsa, ou) = writefds;
939 SCARG(&bsa, ex) = exceptfds;
940 SCARG(&bsa, tv) = timeout;
941
942 /*
943 * Store current time for computation of the amount of
944 * time left.
945 */
946 if (timeout) {
947 if ((error = copyin(timeout, &utv, sizeof(utv))))
948 return error;
949 if (itimerfix(&utv)) {
950 /*
951 * The timeval was invalid. Convert it to something
952 * valid that will act as it does under Linux.
953 */
954 sg = stackgap_init(p->p_emul);
955 tvp = stackgap_alloc(&sg, sizeof(utv));
956 utv.tv_sec += utv.tv_usec / 1000000;
957 utv.tv_usec %= 1000000;
958 if (utv.tv_usec < 0) {
959 utv.tv_sec -= 1;
960 utv.tv_usec += 1000000;
961 }
962 if (utv.tv_sec < 0)
963 timerclear(&utv);
964 if ((error = copyout(&utv, tvp, sizeof(utv))))
965 return error;
966 SCARG(&bsa, tv) = tvp;
967 }
968 microtime(&tv0);
969 }
970
971 error = select(p, &bsa, retval);
972 if (error) {
973 /*
974 * See fs/select.c in the Linux kernel. Without this,
975 * Maelstrom doesn't work.
976 */
977 if (error == ERESTART)
978 error = EINTR;
979 return error;
980 }
981
982 if (timeout) {
983 if (*retval) {
984 /*
985 * Compute how much time was left of the timeout,
986 * by subtracting the current time and the time
987 * before we started the call, and subtracting
988 * that result from the user-supplied value.
989 */
990 microtime(&tv1);
991 timersub(&tv1, &tv0, &tv1);
992 timersub(&utv, &tv1, &utv);
993 if (utv.tv_sec < 0)
994 timerclear(&utv);
995 } else
996 timerclear(&utv);
997 if ((error = copyout(&utv, timeout, sizeof(utv))))
998 return error;
999 }
1000
1001 return 0;
1002 }
1003
1004 /*
1005 * Get the process group of a certain process. Look it up
1006 * and return the value.
1007 */
1008 int
1009 linux_getpgid(p, v, retval)
1010 struct proc *p;
1011 void *v;
1012 register_t *retval;
1013 {
1014 struct linux_getpgid_args /* {
1015 syscallarg(int) pid;
1016 } */ *uap = v;
1017 struct proc *targp;
1018
1019 if (SCARG(uap, pid) != 0 && SCARG(uap, pid) != p->p_pid)
1020 if ((targp = pfind(SCARG(uap, pid))) == 0)
1021 return ESRCH;
1022 else
1023 targp = p;
1024
1025 retval[0] = targp->p_pgid;
1026 return 0;
1027 }
1028
1029 /*
1030 * Set the 'personality' (emulation mode) for the current process. Only
1031 * accept the Linux personality here (0). This call is needed because
1032 * the Linux ELF crt0 issues it in an ugly kludge to make sure that
1033 * ELF binaries run in Linux mode, not SVR4 mode.
1034 */
1035 int
1036 linux_personality(p, v, retval)
1037 struct proc *p;
1038 void *v;
1039 register_t *retval;
1040 {
1041 struct linux_personality_args /* {
1042 syscallarg(int) per;
1043 } */ *uap = v;
1044
1045 if (SCARG(uap, per) != 0)
1046 return EINVAL;
1047 retval[0] = 0;
1048 return 0;
1049 }
1050
1051 /*
1052 * The calls are here because of type conversions.
1053 */
1054 int
1055 linux_setreuid(p, v, retval)
1056 struct proc *p;
1057 void *v;
1058 register_t *retval;
1059 {
1060 struct linux_setreuid_args /* {
1061 syscallarg(int) ruid;
1062 syscallarg(int) euid;
1063 } */ *uap = v;
1064 struct compat_43_setreuid_args bsa;
1065
1066 SCARG(&bsa, ruid) = ((linux_uid_t)SCARG(uap, ruid) == (linux_uid_t)-1) ?
1067 (uid_t)-1 : SCARG(uap, ruid);
1068 SCARG(&bsa, euid) = ((linux_uid_t)SCARG(uap, euid) == (linux_uid_t)-1) ?
1069 (uid_t)-1 : SCARG(uap, euid);
1070
1071 return compat_43_setreuid(p, &bsa, retval);
1072 }
1073
1074 int
1075 linux_setregid(p, v, retval)
1076 struct proc *p;
1077 void *v;
1078 register_t *retval;
1079 {
1080 struct linux_setregid_args /* {
1081 syscallarg(int) rgid;
1082 syscallarg(int) egid;
1083 } */ *uap = v;
1084 struct compat_43_setregid_args bsa;
1085
1086 SCARG(&bsa, rgid) = ((linux_gid_t)SCARG(uap, rgid) == (linux_gid_t)-1) ?
1087 (uid_t)-1 : SCARG(uap, rgid);
1088 SCARG(&bsa, egid) = ((linux_gid_t)SCARG(uap, egid) == (linux_gid_t)-1) ?
1089 (uid_t)-1 : SCARG(uap, egid);
1090
1091 return compat_43_setregid(p, &bsa, retval);
1092 }
1093