linux_misc_notalpha.c revision 1.21 1 /* $NetBSD: linux_misc_notalpha.c,v 1.21 1995/10/07 06:27:10 mycroft 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
89 if (WIFSIGNALED(*status))
90 *status = (*status & ~0177) |
91 bsd_to_linux_sig[WTERMSIG(*status)];
92 else if (WIFSTOPPED(*status))
93 *status = (*status & ~0xff00) |
94 (bsd_to_linux_sig[WSTOPSIG(*status)] << 8);
95 }
96
97 /*
98 * waitpid(2). Passed on to the NetBSD call, surrounded by code to
99 * reserve some space for a NetBSD-style wait status, and converting
100 * it to what Linux wants.
101 */
102 int
103 linux_sys_waitpid(p, v, retval)
104 struct proc *p;
105 void *v;
106 register_t *retval;
107 {
108 struct linux_sys_waitpid_args /* {
109 syscallarg(int) pid;
110 syscallarg(int *) status;
111 syscallarg(int) options;
112 } */ *uap = v;
113 struct sys_wait4_args w4a;
114 int error, *status, tstat;
115 caddr_t sg;
116
117 if (SCARG(uap, status) != NULL) {
118 sg = stackgap_init(p->p_emul);
119 status = (int *) stackgap_alloc(&sg, sizeof status);
120 } else
121 status = NULL;
122
123 SCARG(&w4a, pid) = SCARG(uap, pid);
124 SCARG(&w4a, status) = status;
125 SCARG(&w4a, options) = SCARG(uap, options);
126 SCARG(&w4a, rusage) = NULL;
127
128 if ((error = sys_wait4(p, &w4a, retval)))
129 return error;
130
131 p->p_siglist &= ~sigmask(SIGCHLD);
132
133 if (status != NULL) {
134 if ((error = copyin(status, &tstat, sizeof tstat)))
135 return error;
136
137 bsd_to_linux_wstat(&tstat);
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_sys_wait4(p, v, retval)
149 struct proc *p;
150 void *v;
151 register_t *retval;
152 {
153 struct linux_sys_wait4_args /* {
154 syscallarg(int) pid;
155 syscallarg(int *) status;
156 syscallarg(int) options;
157 syscallarg(struct rusage *) rusage;
158 } */ *uap = v;
159 struct sys_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 = sys_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_sys_break(p, v, retval)
197 struct proc *p;
198 void *v;
199 register_t *retval;
200 {
201 struct linux_sys_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_sys_brk(p, v, retval)
214 struct proc *p;
215 void *v;
216 register_t *retval;
217 {
218 struct linux_sys_brk_args /* {
219 syscallarg(char *) nsize;
220 } */ *uap = v;
221 char *nbrk = SCARG(uap, nsize);
222 struct sys_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 && sys_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_sys_time(p, v, retval)
250 struct proc *p;
251 void *v;
252 register_t *retval;
253 {
254 struct linux_sys_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
283 lsp->l_ftype = bsp->f_type;
284 lsp->l_fbsize = bsp->f_bsize;
285 lsp->l_fblocks = bsp->f_blocks;
286 lsp->l_fbfree = bsp->f_bfree;
287 lsp->l_fbavail = bsp->f_bavail;
288 lsp->l_ffiles = bsp->f_files;
289 lsp->l_fffree = bsp->f_ffree;
290 lsp->l_ffsid.val[0] = bsp->f_fsid.val[0];
291 lsp->l_ffsid.val[1] = bsp->f_fsid.val[1];
292 lsp->l_fnamelen = MAXNAMLEN; /* XXX */
293 }
294
295 /*
296 * Implement the fs stat functions. Straightforward.
297 */
298 int
299 linux_sys_statfs(p, v, retval)
300 struct proc *p;
301 void *v;
302 register_t *retval;
303 {
304 struct linux_sys_statfs_args /* {
305 syscallarg(char *) path;
306 syscallarg(struct linux_statfs *) sp;
307 } */ *uap = v;
308 struct statfs btmp, *bsp;
309 struct linux_statfs ltmp;
310 struct sys_statfs_args bsa;
311 caddr_t sg;
312 int error;
313
314 sg = stackgap_init(p->p_emul);
315 bsp = (struct statfs *) stackgap_alloc(&sg, sizeof (struct statfs));
316
317 LINUX_CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
318
319 SCARG(&bsa, path) = SCARG(uap, path);
320 SCARG(&bsa, buf) = bsp;
321
322 if ((error = sys_statfs(p, &bsa, retval)))
323 return error;
324
325 if ((error = copyin((caddr_t) bsp, (caddr_t) &btmp, sizeof btmp)))
326 return error;
327
328 bsd_to_linux_statfs(&btmp, <mp);
329
330 return copyout((caddr_t) <mp, (caddr_t) SCARG(uap, sp), sizeof ltmp);
331 }
332
333 int
334 linux_sys_fstatfs(p, v, retval)
335 struct proc *p;
336 void *v;
337 register_t *retval;
338 {
339 struct linux_sys_fstatfs_args /* {
340 syscallarg(int) fd;
341 syscallarg(struct linux_statfs *) sp;
342 } */ *uap = v;
343 struct statfs btmp, *bsp;
344 struct linux_statfs ltmp;
345 struct sys_fstatfs_args bsa;
346 caddr_t sg;
347 int error;
348
349 sg = stackgap_init(p->p_emul);
350 bsp = (struct statfs *) stackgap_alloc(&sg, sizeof (struct statfs));
351
352 SCARG(&bsa, fd) = SCARG(uap, fd);
353 SCARG(&bsa, buf) = bsp;
354
355 if ((error = sys_fstatfs(p, &bsa, retval)))
356 return error;
357
358 if ((error = copyin((caddr_t) bsp, (caddr_t) &btmp, sizeof btmp)))
359 return error;
360
361 bsd_to_linux_statfs(&btmp, <mp);
362
363 return copyout((caddr_t) <mp, (caddr_t) SCARG(uap, sp), sizeof ltmp);
364 }
365
366 /*
367 * uname(). Just copy the info from the various strings stored in the
368 * kernel, and put it in the Linux utsname structure. That structure
369 * is almost the same as the NetBSD one, only it has fields 65 characters
370 * long, and an extra domainname field.
371 */
372 int
373 linux_sys_uname(p, v, retval)
374 struct proc *p;
375 void *v;
376 register_t *retval;
377 {
378 struct linux_sys_uname_args /* {
379 syscallarg(struct linux_utsname *) up;
380 } */ *uap = v;
381 extern char ostype[], hostname[], osrelease[], version[], machine[],
382 domainname[];
383 struct linux_utsname luts;
384 int len;
385 char *cp;
386
387 strncpy(luts.l_sysname, ostype, sizeof(luts.l_sysname));
388 strncpy(luts.l_nodename, hostname, sizeof(luts.l_nodename));
389 strncpy(luts.l_release, osrelease, sizeof(luts.l_release));
390 strncpy(luts.l_version, version, sizeof(luts.l_version));
391 strncpy(luts.l_machine, machine, sizeof(luts.l_machine));
392 strncpy(luts.l_domainname, domainname, sizeof(luts.l_domainname));
393
394 /* This part taken from the the uname() in libc */
395 len = sizeof(luts.l_version);
396 for (cp = luts.l_version; len--; ++cp)
397 if (*cp == '\n' || *cp == '\t')
398 if (len > 1)
399 *cp = ' ';
400 else
401 *cp = '\0';
402
403 return copyout(&luts, SCARG(uap, up), sizeof(luts));
404 }
405
406 int
407 linux_sys_olduname(p, v, retval)
408 struct proc *p;
409 void *v;
410 register_t *retval;
411 {
412 struct linux_sys_uname_args /* {
413 syscallarg(struct linux_oldutsname *) up;
414 } */ *uap = v;
415 extern char ostype[], hostname[], osrelease[], version[], machine[];
416 struct linux_oldutsname luts;
417 int len;
418 char *cp;
419
420 strncpy(luts.l_sysname, ostype, sizeof(luts.l_sysname));
421 strncpy(luts.l_nodename, hostname, sizeof(luts.l_nodename));
422 strncpy(luts.l_release, osrelease, sizeof(luts.l_release));
423 strncpy(luts.l_version, version, sizeof(luts.l_version));
424 strncpy(luts.l_machine, machine, sizeof(luts.l_machine));
425
426 /* This part taken from the the uname() in libc */
427 len = sizeof(luts.l_version);
428 for (cp = luts.l_version; len--; ++cp)
429 if (*cp == '\n' || *cp == '\t')
430 if (len > 1)
431 *cp = ' ';
432 else
433 *cp = '\0';
434
435 return copyout(&luts, SCARG(uap, up), sizeof(luts));
436 }
437
438 int
439 linux_sys_oldolduname(p, v, retval)
440 struct proc *p;
441 void *v;
442 register_t *retval;
443 {
444 struct linux_sys_uname_args /* {
445 syscallarg(struct linux_oldoldutsname *) up;
446 } */ *uap = v;
447 extern char ostype[], hostname[], osrelease[], version[], machine[];
448 struct linux_oldoldutsname luts;
449 int len;
450 char *cp;
451
452 strncpy(luts.l_sysname, ostype, sizeof(luts.l_sysname));
453 strncpy(luts.l_nodename, hostname, sizeof(luts.l_nodename));
454 strncpy(luts.l_release, osrelease, sizeof(luts.l_release));
455 strncpy(luts.l_version, version, sizeof(luts.l_version));
456 strncpy(luts.l_machine, machine, sizeof(luts.l_machine));
457
458 /* This part taken from the the uname() in libc */
459 len = sizeof(luts.l_version);
460 for (cp = luts.l_version; len--; ++cp)
461 if (*cp == '\n' || *cp == '\t')
462 if (len > 1)
463 *cp = ' ';
464 else
465 *cp = '\0';
466
467 return copyout(&luts, SCARG(uap, up), sizeof(luts));
468 }
469
470 /*
471 * Linux wants to pass everything to a syscall in registers. However,
472 * mmap() has 6 of them. Oops: out of register error. They just pass
473 * everything in a structure.
474 */
475 int
476 linux_sys_mmap(p, v, retval)
477 struct proc *p;
478 void *v;
479 register_t *retval;
480 {
481 struct linux_sys_mmap_args /* {
482 syscallarg(struct linux_mmap *) lmp;
483 } */ *uap = v;
484 struct linux_mmap lmap;
485 struct sys_mmap_args cma;
486 int error, flags;
487
488 if ((error = copyin(SCARG(uap, lmp), &lmap, sizeof lmap)))
489 return error;
490
491 flags = 0;
492 flags |= cvtto_bsd_mask(lmap.lm_flags, LINUX_MAP_SHARED, MAP_SHARED);
493 flags |= cvtto_bsd_mask(lmap.lm_flags, LINUX_MAP_PRIVATE, MAP_PRIVATE);
494 flags |= cvtto_bsd_mask(lmap.lm_flags, LINUX_MAP_FIXED, MAP_FIXED);
495 flags |= cvtto_bsd_mask(lmap.lm_flags, LINUX_MAP_ANON, MAP_ANON);
496
497 SCARG(&cma,addr) = lmap.lm_addr;
498 SCARG(&cma,len) = lmap.lm_len;
499 SCARG(&cma,prot) = lmap.lm_prot;
500 SCARG(&cma,flags) = flags;
501 SCARG(&cma,fd) = lmap.lm_fd;
502 SCARG(&cma,pad) = 0;
503 SCARG(&cma,pos) = lmap.lm_pos;
504
505 return sys_mmap(p, &cma, retval);
506 }
507
508 /*
509 * Linux doesn't use the retval[1] value to determine whether
510 * we are the child or parent.
511 */
512 int
513 linux_sys_fork(p, v, retval)
514 struct proc *p;
515 void *v;
516 register_t *retval;
517 {
518 int error;
519
520 if ((error = sys_fork(p, v, retval)))
521 return error;
522
523 if (retval[1] == 1)
524 retval[0] = 0;
525
526 return 0;
527 }
528
529 /*
530 * This code is partly stolen from src/lib/libc/compat-43/times.c
531 * XXX - CLK_TCK isn't declared in /sys, just in <time.h>, done here
532 */
533
534 #define CLK_TCK 100
535 #define CONVTCK(r) (r.tv_sec * CLK_TCK + r.tv_usec / (1000000 / CLK_TCK))
536
537 int
538 linux_sys_times(p, v, retval)
539 struct proc *p;
540 void *v;
541 register_t *retval;
542 {
543 struct linux_sys_times_args /* {
544 syscallarg(struct times *) tms;
545 } */ *uap = v;
546 struct timeval t;
547 struct linux_tms ltms;
548 struct rusage ru;
549 int error, s;
550
551 calcru(p, &ru.ru_utime, &ru.ru_stime, NULL);
552 ltms.ltms_utime = CONVTCK(ru.ru_utime);
553 ltms.ltms_stime = CONVTCK(ru.ru_stime);
554
555 ltms.ltms_cutime = CONVTCK(p->p_stats->p_cru.ru_utime);
556 ltms.ltms_cstime = CONVTCK(p->p_stats->p_cru.ru_stime);
557
558 if ((error = copyout(<ms, SCARG(uap, tms), sizeof ltms)))
559 return error;
560
561 s = splclock();
562 timersub(&time, &boottime, &t);
563 splx(s);
564
565 retval[0] = ((linux_clock_t)(CONVTCK(t)));
566 return 0;
567 }
568
569 /*
570 * NetBSD passes fd[0] in retval[0], and fd[1] in retval[1].
571 * Linux directly passes the pointer.
572 */
573 int
574 linux_sys_pipe(p, v, retval)
575 struct proc *p;
576 void *v;
577 register_t *retval;
578 {
579 struct linux_sys_pipe_args /* {
580 syscallarg(int *) pfds;
581 } */ *uap = v;
582 int error;
583
584 if ((error = sys_pipe(p, 0, retval)))
585 return error;
586
587 /* Assumes register_t is an int */
588
589 if ((error = copyout(retval, SCARG(uap, pfds), 2 * sizeof (int))))
590 return error;
591
592 retval[0] = 0;
593 return 0;
594 }
595
596 /*
597 * Alarm. This is a libc call which uses setitimer(2) in NetBSD.
598 * Fiddle with the timers to make it work.
599 */
600 int
601 linux_sys_alarm(p, v, retval)
602 struct proc *p;
603 void *v;
604 register_t *retval;
605 {
606 struct linux_sys_alarm_args /* {
607 syscallarg(unsigned int) secs;
608 } */ *uap = v;
609 int error, s;
610 struct itimerval *itp, it;
611
612 itp = &p->p_realtimer;
613 s = splclock();
614 /*
615 * Clear any pending timer alarms.
616 */
617 untimeout(realitexpire, p);
618 timerclear(&itp->it_interval);
619 if (timerisset(&itp->it_value) &&
620 timercmp(&itp->it_value, &time, >))
621 timersub(&itp->it_value, &time, &itp->it_value);
622 /*
623 * Return how many seconds were left (rounded up)
624 */
625 retval[0] = itp->it_value.tv_sec;
626 if (itp->it_value.tv_usec)
627 retval[0]++;
628
629 /*
630 * alarm(0) just resets the timer.
631 */
632 if (SCARG(uap, secs) == 0) {
633 timerclear(&itp->it_value);
634 splx(s);
635 return 0;
636 }
637
638 /*
639 * Check the new alarm time for sanity, and set it.
640 */
641 timerclear(&it.it_interval);
642 it.it_value.tv_sec = SCARG(uap, secs);
643 it.it_value.tv_usec = 0;
644 if (itimerfix(&it.it_value) || itimerfix(&it.it_interval)) {
645 splx(s);
646 return (EINVAL);
647 }
648
649 if (timerisset(&it.it_value)) {
650 timeradd(&it.it_value, &time, &it.it_value);
651 timeout(realitexpire, p, hzto(&it.it_value));
652 }
653 p->p_realtimer = it;
654 splx(s);
655
656 return 0;
657 }
658
659 /*
660 * utime(). Do conversion to things that utimes() understands,
661 * and pass it on.
662 */
663 int
664 linux_sys_utime(p, v, retval)
665 struct proc *p;
666 void *v;
667 register_t *retval;
668 {
669 struct linux_sys_utime_args /* {
670 syscallarg(char *) path;
671 syscallarg(struct linux_utimbuf *)times;
672 } */ *uap = v;
673 caddr_t sg;
674 int error;
675 struct sys_utimes_args ua;
676 struct timeval tv[2], *tvp;
677 struct linux_utimbuf lut;
678
679 sg = stackgap_init(p->p_emul);
680 LINUX_CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
681
682 SCARG(&ua, path) = SCARG(uap, path);
683
684 if (SCARG(uap, times) != NULL) {
685 if ((error = copyin(SCARG(uap, times), &lut, sizeof lut)))
686 return error;
687 tv[0].tv_usec = tv[1].tv_usec = 0;
688 tv[0].tv_sec = lut.l_actime;
689 tv[1].tv_sec = lut.l_modtime;
690 tvp = (struct timeval *) stackgap_alloc(&sg, sizeof(tv));
691 if ((error = copyout(tv, tvp, sizeof tv)))
692 return error;
693 SCARG(&ua, tptr) = tvp;
694 }
695 else
696 SCARG(&ua, tptr) = NULL;
697
698 return sys_utimes(p, uap, retval);
699 }
700
701 /*
702 * The old Linux readdir was only able to read one entry at a time,
703 * even though it had a 'count' argument. In fact, the emulation
704 * of the old call was better than the original, because it did handle
705 * the count arg properly. Don't bother with it anymore now, and use
706 * it to distinguish between old and new. The difference is that the
707 * newer one actually does multiple entries, and the reclen field
708 * really is the reclen, not the namelength.
709 */
710 int
711 linux_sys_readdir(p, v, retval)
712 struct proc *p;
713 void *v;
714 register_t *retval;
715 {
716 struct linux_sys_readdir_args /* {
717 syscallarg(int) fd;
718 syscallarg(struct linux_dirent *) dent;
719 syscallarg(unsigned int) count;
720 } */ *uap = v;
721
722 SCARG(uap, count) = 1;
723 return linux_sys_getdents(p, uap, retval);
724 }
725
726 /*
727 * Linux 'readdir' call. This code is mostly taken from the
728 * SunOS getdents call (see compat/sunos/sunos_misc.c), though
729 * an attempt has been made to keep it a little cleaner (failing
730 * miserably, because of the cruft needed if count 1 is passed).
731 *
732 * The d_off field should contain the offset of the next valid entry,
733 * but in Linux it has the offset of the entry itself. We emulate
734 * that bug here.
735 *
736 * Read in BSD-style entries, convert them, and copy them out.
737 *
738 * Note that this doesn't handle union-mounted filesystems.
739 */
740 int
741 linux_sys_getdents(p, v, retval)
742 struct proc *p;
743 void *v;
744 register_t *retval;
745 {
746 struct linux_sys_readdir_args /* {
747 syscallarg(int) fd;
748 syscallarg(caddr_t) dent;
749 syscallarg(unsigned int) count;
750 } */ *uap = v;
751 register struct dirent *bdp;
752 struct vnode *vp;
753 caddr_t inp, buf; /* BSD-format */
754 int len, reclen; /* BSD-format */
755 caddr_t outp; /* Linux-format */
756 int resid, linux_reclen;/* Linux-format */
757 struct file *fp;
758 struct uio auio;
759 struct iovec aiov;
760 struct linux_dirent idb;
761 off_t off; /* true 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 = 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 linux_reclen = LINUX_RECLEN(&idb, bdp->d_namlen);
826 if (reclen > len || resid < linux_reclen) {
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 misuses the offset and reclen fields.
840 */
841 idb.d_reclen = oldcall ? (u_short)bdp->d_namlen : linux_reclen;
842 strcpy(idb.d_name, bdp->d_name);
843 if ((error = copyout((caddr_t)&idb, outp, linux_reclen)))
844 goto out;
845 /* advance past this real entry */
846 inp += reclen;
847 /* advance output past Linux-shaped entry */
848 outp += linux_reclen;
849 resid -= linux_reclen;
850 if (oldcall)
851 break;
852 }
853
854 /* if we squished out the whole block, try again */
855 if (outp == SCARG(uap, dent))
856 goto again;
857 fp->f_offset = off; /* update the vnode offset */
858
859 if (oldcall)
860 nbytes = resid + linux_reclen;
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_sys_oldselect(p, v, retval)
877 struct proc *p;
878 void *v;
879 register_t *retval;
880 {
881 struct linux_sys_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_sys_select(p, v, retval)
901 struct proc *p;
902 void *v;
903 register_t *retval;
904 {
905 struct linux_sys_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 sys_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 = sys_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_sys_getpgid(p, v, retval)
1010 struct proc *p;
1011 void *v;
1012 register_t *retval;
1013 {
1014 struct linux_sys_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_sys_personality(p, v, retval)
1037 struct proc *p;
1038 void *v;
1039 register_t *retval;
1040 {
1041 struct linux_sys_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_sys_setreuid(p, v, retval)
1056 struct proc *p;
1057 void *v;
1058 register_t *retval;
1059 {
1060 struct linux_sys_setreuid_args /* {
1061 syscallarg(int) ruid;
1062 syscallarg(int) euid;
1063 } */ *uap = v;
1064 struct compat_43_sys_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_sys_setreuid(p, &bsa, retval);
1072 }
1073
1074 int
1075 linux_sys_setregid(p, v, retval)
1076 struct proc *p;
1077 void *v;
1078 register_t *retval;
1079 {
1080 struct linux_sys_setregid_args /* {
1081 syscallarg(int) rgid;
1082 syscallarg(int) egid;
1083 } */ *uap = v;
1084 struct compat_43_sys_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_sys_setregid(p, &bsa, retval);
1092 }
1093