linux_file.c revision 1.60 1 /* $NetBSD: linux_file.c,v 1.60 2003/06/29 14:51:28 jdolecek Exp $ */
2
3 /*-
4 * Copyright (c) 1995, 1998 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Frank van der Linden and Eric Haszlakiewicz.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 /*
40 * Functions in multiarch:
41 * linux_sys_llseek : linux_llseek.c
42 */
43
44 #include <sys/cdefs.h>
45 __KERNEL_RCSID(0, "$NetBSD: linux_file.c,v 1.60 2003/06/29 14:51:28 jdolecek Exp $");
46
47 #include <sys/param.h>
48 #include <sys/systm.h>
49 #include <sys/namei.h>
50 #include <sys/proc.h>
51 #include <sys/file.h>
52 #include <sys/stat.h>
53 #include <sys/filedesc.h>
54 #include <sys/ioctl.h>
55 #include <sys/kernel.h>
56 #include <sys/mount.h>
57 #include <sys/malloc.h>
58 #include <sys/vnode.h>
59 #include <sys/tty.h>
60 #include <sys/socketvar.h>
61 #include <sys/conf.h>
62 #include <sys/pipe.h>
63
64 #include <sys/sa.h>
65 #include <sys/syscallargs.h>
66
67 #include <compat/linux/common/linux_types.h>
68 #include <compat/linux/common/linux_signal.h>
69 #include <compat/linux/common/linux_fcntl.h>
70 #include <compat/linux/common/linux_util.h>
71 #include <compat/linux/common/linux_machdep.h>
72
73 #include <compat/linux/linux_syscallargs.h>
74
75 static int linux_to_bsd_ioflags __P((int));
76 static int bsd_to_linux_ioflags __P((int));
77 static void bsd_to_linux_flock __P((struct flock *, struct linux_flock *));
78 static void linux_to_bsd_flock __P((struct linux_flock *, struct flock *));
79 static void bsd_to_linux_stat __P((struct stat *, struct linux_stat *));
80 static int linux_stat1 __P((struct lwp *, void *, register_t *, int));
81
82 /*
83 * Some file-related calls are handled here. The usual flag conversion
84 * an structure conversion is done, and alternate emul path searching.
85 */
86
87 /*
88 * The next two functions convert between the Linux and NetBSD values
89 * of the flags used in open(2) and fcntl(2).
90 */
91 static int
92 linux_to_bsd_ioflags(lflags)
93 int lflags;
94 {
95 int res = 0;
96
97 res |= cvtto_bsd_mask(lflags, LINUX_O_WRONLY, O_WRONLY);
98 res |= cvtto_bsd_mask(lflags, LINUX_O_RDONLY, O_RDONLY);
99 res |= cvtto_bsd_mask(lflags, LINUX_O_RDWR, O_RDWR);
100 res |= cvtto_bsd_mask(lflags, LINUX_O_CREAT, O_CREAT);
101 res |= cvtto_bsd_mask(lflags, LINUX_O_EXCL, O_EXCL);
102 res |= cvtto_bsd_mask(lflags, LINUX_O_NOCTTY, O_NOCTTY);
103 res |= cvtto_bsd_mask(lflags, LINUX_O_TRUNC, O_TRUNC);
104 res |= cvtto_bsd_mask(lflags, LINUX_O_NDELAY, O_NDELAY);
105 res |= cvtto_bsd_mask(lflags, LINUX_O_SYNC, O_FSYNC);
106 res |= cvtto_bsd_mask(lflags, LINUX_FASYNC, O_ASYNC);
107 res |= cvtto_bsd_mask(lflags, LINUX_O_APPEND, O_APPEND);
108
109 return res;
110 }
111
112 static int
113 bsd_to_linux_ioflags(bflags)
114 int bflags;
115 {
116 int res = 0;
117
118 res |= cvtto_linux_mask(bflags, O_WRONLY, LINUX_O_WRONLY);
119 res |= cvtto_linux_mask(bflags, O_RDONLY, LINUX_O_RDONLY);
120 res |= cvtto_linux_mask(bflags, O_RDWR, LINUX_O_RDWR);
121 res |= cvtto_linux_mask(bflags, O_CREAT, LINUX_O_CREAT);
122 res |= cvtto_linux_mask(bflags, O_EXCL, LINUX_O_EXCL);
123 res |= cvtto_linux_mask(bflags, O_NOCTTY, LINUX_O_NOCTTY);
124 res |= cvtto_linux_mask(bflags, O_TRUNC, LINUX_O_TRUNC);
125 res |= cvtto_linux_mask(bflags, O_NDELAY, LINUX_O_NDELAY);
126 res |= cvtto_linux_mask(bflags, O_FSYNC, LINUX_O_SYNC);
127 res |= cvtto_linux_mask(bflags, O_ASYNC, LINUX_FASYNC);
128 res |= cvtto_linux_mask(bflags, O_APPEND, LINUX_O_APPEND);
129
130 return res;
131 }
132
133 /*
134 * creat(2) is an obsolete function, but it's present as a Linux
135 * system call, so let's deal with it.
136 *
137 * Note: On the Alpha this doesn't really exist in Linux, but it's defined
138 * in syscalls.master anyway so this doesn't have to be special cased.
139 *
140 * Just call open(2) with the TRUNC, CREAT and WRONLY flags.
141 */
142 int
143 linux_sys_creat(l, v, retval)
144 struct lwp *l;
145 void *v;
146 register_t *retval;
147 {
148 struct linux_sys_creat_args /* {
149 syscallarg(const char *) path;
150 syscallarg(int) mode;
151 } */ *uap = v;
152 struct proc *p = l->l_proc;
153 struct sys_open_args oa;
154 caddr_t sg;
155
156 sg = stackgap_init(p, 0);
157 CHECK_ALT_CREAT(l, &sg, SCARG(uap, path));
158
159 SCARG(&oa, path) = SCARG(uap, path);
160 SCARG(&oa, flags) = O_CREAT | O_TRUNC | O_WRONLY;
161 SCARG(&oa, mode) = SCARG(uap, mode);
162
163 return sys_open(l, &oa, retval);
164 }
165
166 /*
167 * open(2). Take care of the different flag values, and let the
168 * NetBSD syscall do the real work. See if this operation
169 * gives the current process a controlling terminal.
170 * (XXX is this necessary?)
171 */
172 int
173 linux_sys_open(l, v, retval)
174 struct lwp *l;
175 void *v;
176 register_t *retval;
177 {
178 struct linux_sys_open_args /* {
179 syscallarg(const char *) path;
180 syscallarg(int) flags;
181 syscallarg(int) mode;
182 } */ *uap = v;
183 struct proc *p = l->l_proc;
184 int error, fl;
185 struct sys_open_args boa;
186 caddr_t sg;
187
188 sg = stackgap_init(p, 0);
189
190 fl = linux_to_bsd_ioflags(SCARG(uap, flags));
191
192 if (fl & O_CREAT)
193 CHECK_ALT_CREAT(l, &sg, SCARG(uap, path));
194 else
195 CHECK_ALT_EXIST(l, &sg, SCARG(uap, path));
196
197 SCARG(&boa, path) = SCARG(uap, path);
198 SCARG(&boa, flags) = fl;
199 SCARG(&boa, mode) = SCARG(uap, mode);
200
201 if ((error = sys_open(l, &boa, retval)))
202 return error;
203
204 /*
205 * this bit from sunos_misc.c (and svr4_fcntl.c).
206 * If we are a session leader, and we don't have a controlling
207 * terminal yet, and the O_NOCTTY flag is not set, try to make
208 * this the controlling terminal.
209 */
210 if (!(fl & O_NOCTTY) && SESS_LEADER(p) && !(p->p_flag & P_CONTROLT)) {
211 struct filedesc *fdp = p->p_fd;
212 struct file *fp;
213
214 fp = fd_getfile(fdp, *retval);
215
216 /* ignore any error, just give it a try */
217 if (fp != NULL) {
218 FILE_USE(fp);
219 if (fp->f_type == DTYPE_VNODE) {
220 (fp->f_ops->fo_ioctl) (fp, TIOCSCTTY,
221 (caddr_t) 0, l);
222 }
223 FILE_UNUSE(fp, l);
224 }
225 }
226 return 0;
227 }
228
229 /*
230 * The next two functions take care of converting the flock
231 * structure back and forth between Linux and NetBSD format.
232 * The only difference in the structures is the order of
233 * the fields, and the 'whence' value.
234 */
235 static void
236 bsd_to_linux_flock(bfp, lfp)
237 struct flock *bfp;
238 struct linux_flock *lfp;
239 {
240
241 lfp->l_start = bfp->l_start;
242 lfp->l_len = bfp->l_len;
243 lfp->l_pid = bfp->l_pid;
244 lfp->l_whence = bfp->l_whence;
245 switch (bfp->l_type) {
246 case F_RDLCK:
247 lfp->l_type = LINUX_F_RDLCK;
248 break;
249 case F_UNLCK:
250 lfp->l_type = LINUX_F_UNLCK;
251 break;
252 case F_WRLCK:
253 lfp->l_type = LINUX_F_WRLCK;
254 break;
255 }
256 }
257
258 static void
259 linux_to_bsd_flock(lfp, bfp)
260 struct linux_flock *lfp;
261 struct flock *bfp;
262 {
263
264 bfp->l_start = lfp->l_start;
265 bfp->l_len = lfp->l_len;
266 bfp->l_pid = lfp->l_pid;
267 bfp->l_whence = lfp->l_whence;
268 switch (lfp->l_type) {
269 case LINUX_F_RDLCK:
270 bfp->l_type = F_RDLCK;
271 break;
272 case LINUX_F_UNLCK:
273 bfp->l_type = F_UNLCK;
274 break;
275 case LINUX_F_WRLCK:
276 bfp->l_type = F_WRLCK;
277 break;
278 }
279 }
280
281 /*
282 * Most actions in the fcntl() call are straightforward; simply
283 * pass control to the NetBSD system call. A few commands need
284 * conversions after the actual system call has done its work,
285 * because the flag values and lock structure are different.
286 */
287 int
288 linux_sys_fcntl(l, v, retval)
289 struct lwp *l;
290 void *v;
291 register_t *retval;
292 {
293 struct linux_sys_fcntl_args /* {
294 syscallarg(int) fd;
295 syscallarg(int) cmd;
296 syscallarg(void *) arg;
297 } */ *uap = v;
298 struct proc *p = l->l_proc;
299 int fd, cmd, error;
300 u_long val;
301 caddr_t arg, sg;
302 struct linux_flock lfl;
303 struct flock *bfp, bfl;
304 struct sys_fcntl_args fca;
305 struct filedesc *fdp;
306 struct file *fp;
307 struct vnode *vp;
308 struct vattr va;
309 const struct cdevsw *cdev;
310 long pgid;
311 struct pgrp *pgrp;
312 struct tty *tp, *(*d_tty) __P((dev_t));
313
314 fd = SCARG(uap, fd);
315 cmd = SCARG(uap, cmd);
316 arg = (caddr_t) SCARG(uap, arg);
317
318 switch (cmd) {
319 case LINUX_F_DUPFD:
320 cmd = F_DUPFD;
321 break;
322 case LINUX_F_GETFD:
323 cmd = F_GETFD;
324 break;
325 case LINUX_F_SETFD:
326 cmd = F_SETFD;
327 break;
328 case LINUX_F_GETFL:
329 SCARG(&fca, fd) = fd;
330 SCARG(&fca, cmd) = F_GETFL;
331 SCARG(&fca, arg) = arg;
332 if ((error = sys_fcntl(l, &fca, retval)))
333 return error;
334 retval[0] = bsd_to_linux_ioflags(retval[0]);
335 return 0;
336 case LINUX_F_SETFL: {
337 struct file *fp = NULL;
338
339 val = linux_to_bsd_ioflags((unsigned long)SCARG(uap, arg));
340 /*
341 * Linux seems to have same semantics for sending SIGIO to the
342 * read side of socket, but slighly different semantics
343 * for SIGIO to the write side. Rather than sending the SIGIO
344 * every time it's possible to write (directly) more data, it
345 * only sends SIGIO if last write(2) failed due to insufficient
346 * memory to hold the data. This is compatible enough
347 * with NetBSD semantics to not do anything about the
348 * difference.
349 *
350 * Linux does NOT send SIGIO for pipes. Deal with socketpair
351 * ones and DTYPE_PIPE ones. For these, we don't set
352 * the underlying flags (we don't pass O_ASYNC flag down
353 * to sys_fcntl()), but set the FASYNC flag for file descriptor,
354 * so that F_GETFL would report the ASYNC i/o is on.
355 */
356 if (val & O_ASYNC) {
357 if (((fp = fd_getfile(p->p_fd, fd)) == NULL))
358 return (EBADF);
359
360 FILE_USE(fp);
361
362 if (((fp->f_type == DTYPE_SOCKET) && fp->f_data
363 && ((struct socket *)fp->f_data)->so_state & SS_ISAPIPE)
364 || (fp->f_type == DTYPE_PIPE))
365 val &= ~O_ASYNC;
366 else {
367 /* not a pipe, do not modify anything */
368 FILE_UNUSE(fp, l);
369 fp = NULL;
370 }
371 }
372
373 SCARG(&fca, fd) = fd;
374 SCARG(&fca, cmd) = F_SETFL;
375 SCARG(&fca, arg) = (caddr_t) val;
376
377 error = sys_fcntl(l, &fca, retval);
378
379 /* Now set the FASYNC flag for pipes */
380 if (fp) {
381 if (!error)
382 fp->f_flag |= FASYNC;
383 FILE_UNUSE(fp, l);
384 }
385
386 return (error);
387 }
388 case LINUX_F_GETLK:
389 sg = stackgap_init(p, 0);
390 bfp = (struct flock *) stackgap_alloc(p, &sg, sizeof *bfp);
391 if ((error = copyin(arg, &lfl, sizeof lfl)))
392 return error;
393 linux_to_bsd_flock(&lfl, &bfl);
394 if ((error = copyout(&bfl, bfp, sizeof bfl)))
395 return error;
396 SCARG(&fca, fd) = fd;
397 SCARG(&fca, cmd) = F_GETLK;
398 SCARG(&fca, arg) = bfp;
399 if ((error = sys_fcntl(l, &fca, retval)))
400 return error;
401 if ((error = copyin(bfp, &bfl, sizeof bfl)))
402 return error;
403 bsd_to_linux_flock(&bfl, &lfl);
404 return copyout(&lfl, arg, sizeof lfl);
405
406 case LINUX_F_SETLK:
407 case LINUX_F_SETLKW:
408 cmd = (cmd == LINUX_F_SETLK ? F_SETLK : F_SETLKW);
409 if ((error = copyin(arg, &lfl, sizeof lfl)))
410 return error;
411 linux_to_bsd_flock(&lfl, &bfl);
412 sg = stackgap_init(p, 0);
413 bfp = (struct flock *) stackgap_alloc(p, &sg, sizeof *bfp);
414 if ((error = copyout(&bfl, bfp, sizeof bfl)))
415 return error;
416 arg = (caddr_t)bfp;
417 break;
418
419 case LINUX_F_SETOWN:
420 case LINUX_F_GETOWN:
421 /*
422 * We need to route fcntl() for tty descriptors around normal
423 * fcntl(), since NetBSD tty TIOC{G,S}PGRP semantics is too
424 * restrictive for Linux F_{G,S}ETOWN. For non-tty descriptors,
425 * this is not a problem.
426 */
427 fdp = p->p_fd;
428 if ((fp = fd_getfile(fdp, fd)) == NULL)
429 return EBADF;
430 FILE_USE(fp);
431
432 /* Check it's a character device vnode */
433 if (fp->f_type != DTYPE_VNODE
434 || (vp = (struct vnode *)fp->f_data) == NULL
435 || vp->v_type != VCHR) {
436 FILE_UNUSE(fp, l);
437
438 not_tty:
439 /* Not a tty, proceed with common fcntl() */
440 cmd = cmd == LINUX_F_SETOWN ? F_SETOWN : F_GETOWN;
441 break;
442 }
443
444 /* check that the vnode is a tty */
445 error = VOP_GETATTR(vp, &va, p->p_ucred, l);
446
447 FILE_UNUSE(fp, l); /* Don't need anymore */
448
449 if (error)
450 return error;
451
452 cdev = cdevsw_lookup(va.va_rdev);
453 if (cdev == NULL)
454 return (ENXIO);
455 d_tty = cdev->d_tty;
456 if (!d_tty || (!(tp = (*d_tty)(va.va_rdev))))
457 goto not_tty;
458
459 /* set tty pg_id appropriately */
460 if (cmd == LINUX_F_GETOWN) {
461 retval[0] = tp->t_pgrp ? tp->t_pgrp->pg_id : NO_PGID;
462 return 0;
463 }
464 if ((long)arg <= 0) {
465 pgid = -(long)arg;
466 } else {
467 struct proc *p1 = pfind((long)arg);
468 if (p1 == NULL)
469 return (ESRCH);
470 pgid = (long)p1->p_pgrp->pg_id;
471 }
472 pgrp = pgfind(pgid);
473 if (pgrp == NULL || pgrp->pg_session != p->p_session)
474 return EPERM;
475 tp->t_pgrp = pgrp;
476 return 0;
477
478 default:
479 return EOPNOTSUPP;
480 }
481
482 SCARG(&fca, fd) = fd;
483 SCARG(&fca, cmd) = cmd;
484 SCARG(&fca, arg) = arg;
485
486 return sys_fcntl(l, &fca, retval);
487 }
488
489 /*
490 * Convert a NetBSD stat structure to a Linux stat structure.
491 * Only the order of the fields and the padding in the structure
492 * is different. linux_fakedev is a machine-dependent function
493 * which optionally converts device driver major/minor numbers
494 * (XXX horrible, but what can you do against code that compares
495 * things against constant major device numbers? sigh)
496 */
497 static void
498 bsd_to_linux_stat(bsp, lsp)
499 struct stat *bsp;
500 struct linux_stat *lsp;
501 {
502
503 lsp->lst_dev = linux_fakedev(bsp->st_dev, 0);
504 lsp->lst_ino = bsp->st_ino;
505 lsp->lst_mode = (linux_mode_t)bsp->st_mode;
506 if (bsp->st_nlink >= (1 << 15))
507 lsp->lst_nlink = (1 << 15) - 1;
508 else
509 lsp->lst_nlink = (linux_nlink_t)bsp->st_nlink;
510 lsp->lst_uid = bsp->st_uid;
511 lsp->lst_gid = bsp->st_gid;
512 lsp->lst_rdev = linux_fakedev(bsp->st_rdev, 1);
513 lsp->lst_size = bsp->st_size;
514 lsp->lst_blksize = bsp->st_blksize;
515 lsp->lst_blocks = bsp->st_blocks;
516 lsp->lst_atime = bsp->st_atime;
517 lsp->lst_mtime = bsp->st_mtime;
518 lsp->lst_ctime = bsp->st_ctime;
519 }
520
521 /*
522 * The stat functions below are plain sailing. stat and lstat are handled
523 * by one function to avoid code duplication.
524 */
525 int
526 linux_sys_fstat(l, v, retval)
527 struct lwp *l;
528 void *v;
529 register_t *retval;
530 {
531 struct linux_sys_fstat_args /* {
532 syscallarg(int) fd;
533 syscallarg(linux_stat *) sp;
534 } */ *uap = v;
535 struct proc *p = l->l_proc;
536 struct sys___fstat13_args fsa;
537 struct linux_stat tmplst;
538 struct stat *st,tmpst;
539 caddr_t sg;
540 int error;
541
542 sg = stackgap_init(p, 0);
543
544 st = stackgap_alloc(p, &sg, sizeof (struct stat));
545
546 SCARG(&fsa, fd) = SCARG(uap, fd);
547 SCARG(&fsa, sb) = st;
548
549 if ((error = sys___fstat13(l, &fsa, retval)))
550 return error;
551
552 if ((error = copyin(st, &tmpst, sizeof tmpst)))
553 return error;
554
555 bsd_to_linux_stat(&tmpst, &tmplst);
556
557 if ((error = copyout(&tmplst, SCARG(uap, sp), sizeof tmplst)))
558 return error;
559
560 return 0;
561 }
562
563 static int
564 linux_stat1(l, v, retval, dolstat)
565 struct lwp *l;
566 void *v;
567 register_t *retval;
568 int dolstat;
569 {
570 struct sys___stat13_args sa;
571 struct linux_stat tmplst;
572 struct stat *st, tmpst;
573 struct proc *p = l->l_proc;
574 caddr_t sg;
575 int error;
576 struct linux_sys_stat_args *uap = v;
577
578 sg = stackgap_init(p, 0);
579 st = stackgap_alloc(p, &sg, sizeof (struct stat));
580 if (dolstat)
581 CHECK_ALT_SYMLINK(l, &sg, SCARG(uap, path));
582 else
583 CHECK_ALT_EXIST(l, &sg, SCARG(uap, path));
584
585 SCARG(&sa, ub) = st;
586 SCARG(&sa, path) = SCARG(uap, path);
587
588 if ((error = (dolstat ? sys___lstat13(l, &sa, retval) :
589 sys___stat13(l, &sa, retval))))
590 return error;
591
592 if ((error = copyin(st, &tmpst, sizeof tmpst)))
593 return error;
594
595 bsd_to_linux_stat(&tmpst, &tmplst);
596
597 if ((error = copyout(&tmplst, SCARG(uap, sp), sizeof tmplst)))
598 return error;
599
600 return 0;
601 }
602
603 int
604 linux_sys_stat(l, v, retval)
605 struct lwp *l;
606 void *v;
607 register_t *retval;
608 {
609 struct linux_sys_stat_args /* {
610 syscallarg(const char *) path;
611 syscallarg(struct linux_stat *) sp;
612 } */ *uap = v;
613
614 return linux_stat1(l, uap, retval, 0);
615 }
616
617 /* Note: this is "newlstat" in the Linux sources */
618 /* (we don't bother with the old lstat currently) */
619 int
620 linux_sys_lstat(l, v, retval)
621 struct lwp *l;
622 void *v;
623 register_t *retval;
624 {
625 struct linux_sys_lstat_args /* {
626 syscallarg(const char *) path;
627 syscallarg(struct linux_stat *) sp;
628 } */ *uap = v;
629
630 return linux_stat1(l, uap, retval, 1);
631 }
632
633 /*
634 * The following syscalls are mostly here because of the alternate path check.
635 */
636 int
637 linux_sys_access(l, v, retval)
638 struct lwp *l;
639 void *v;
640 register_t *retval;
641 {
642 struct linux_sys_access_args /* {
643 syscallarg(const char *) path;
644 syscallarg(int) flags;
645 } */ *uap = v;
646 struct proc *p = l->l_proc;
647 caddr_t sg = stackgap_init(p, 0);
648
649 CHECK_ALT_EXIST(l, &sg, SCARG(uap, path));
650
651 return sys_access(l, uap, retval);
652 }
653
654 int
655 linux_sys_unlink(l, v, retval)
656 struct lwp *l;
657 void *v;
658 register_t *retval;
659
660 {
661 struct linux_sys_unlink_args /* {
662 syscallarg(const char *) path;
663 } */ *uap = v;
664 struct proc *p = l->l_proc;
665 caddr_t sg = stackgap_init(p, 0);
666
667 CHECK_ALT_EXIST(l, &sg, SCARG(uap, path));
668
669 return sys_unlink(l, uap, retval);
670 }
671
672 int
673 linux_sys_chdir(l, v, retval)
674 struct lwp *l;
675 void *v;
676 register_t *retval;
677 {
678 struct linux_sys_chdir_args /* {
679 syscallarg(const char *) path;
680 } */ *uap = v;
681 struct proc *p = l->l_proc;
682 caddr_t sg = stackgap_init(p, 0);
683
684 CHECK_ALT_EXIST(l, &sg, SCARG(uap, path));
685
686 return sys_chdir(l, uap, retval);
687 }
688
689 int
690 linux_sys_mknod(l, v, retval)
691 struct lwp *l;
692 void *v;
693 register_t *retval;
694 {
695 struct linux_sys_mknod_args /* {
696 syscallarg(const char *) path;
697 syscallarg(int) mode;
698 syscallarg(int) dev;
699 } */ *uap = v;
700 struct proc *p = l->l_proc;
701 caddr_t sg = stackgap_init(p, 0);
702
703 CHECK_ALT_CREAT(l, &sg, SCARG(uap, path));
704
705 /*
706 * BSD handles FIFOs separately
707 */
708 if (SCARG(uap, mode) & S_IFIFO) {
709 struct sys_mkfifo_args bma;
710
711 SCARG(&bma, path) = SCARG(uap, path);
712 SCARG(&bma, mode) = SCARG(uap, mode);
713 return sys_mkfifo(l, &bma, retval);
714 } else {
715 struct sys_mknod_args bma;
716
717 SCARG(&bma, path) = SCARG(uap, path);
718 SCARG(&bma, mode) = SCARG(uap, mode);
719 /*
720 * Linux device numbers uses 8 bits for minor and 8 bits
721 * for major. Due to how we map our major and minor,
722 * this just fints into our dev_t. Just mask off the
723 * upper 16bit to remove any random junk.
724 */
725 SCARG(&bma, dev) = SCARG(uap, dev) & 0xffff;
726 return sys_mknod(l, &bma, retval);
727 }
728 }
729
730 int
731 linux_sys_chmod(l, v, retval)
732 struct lwp *l;
733 void *v;
734 register_t *retval;
735 {
736 struct linux_sys_chmod_args /* {
737 syscallarg(const char *) path;
738 syscallarg(int) mode;
739 } */ *uap = v;
740 struct proc *p = l->l_proc;
741 caddr_t sg = stackgap_init(p, 0);
742
743 CHECK_ALT_EXIST(l, &sg, SCARG(uap, path));
744
745 return sys_chmod(l, uap, retval);
746 }
747
748 #if defined(__i386__) || defined(__m68k__) || defined(__arm__)
749 int
750 linux_sys_chown16(l, v, retval)
751 struct lwp *l;
752 void *v;
753 register_t *retval;
754 {
755 struct linux_sys_chown16_args /* {
756 syscallarg(const char *) path;
757 syscallarg(int) uid;
758 syscallarg(int) gid;
759 } */ *uap = v;
760 struct proc *p = l->l_proc;
761 struct sys___posix_chown_args bca;
762 caddr_t sg = stackgap_init(p, 0);
763
764 CHECK_ALT_EXIST(l, &sg, SCARG(uap, path));
765
766 SCARG(&bca, path) = SCARG(uap, path);
767 SCARG(&bca, uid) = ((linux_uid_t)SCARG(uap, uid) == (linux_uid_t)-1) ?
768 (uid_t)-1 : SCARG(uap, uid);
769 SCARG(&bca, gid) = ((linux_gid_t)SCARG(uap, gid) == (linux_gid_t)-1) ?
770 (gid_t)-1 : SCARG(uap, gid);
771
772 return sys___posix_chown(l, &bca, retval);
773 }
774
775 int
776 linux_sys_fchown16(l, v, retval)
777 struct lwp *l;
778 void *v;
779 register_t *retval;
780 {
781 struct linux_sys_fchown16_args /* {
782 syscallarg(int) fd;
783 syscallarg(int) uid;
784 syscallarg(int) gid;
785 } */ *uap = v;
786 struct sys___posix_fchown_args bfa;
787
788 SCARG(&bfa, fd) = SCARG(uap, fd);
789 SCARG(&bfa, uid) = ((linux_uid_t)SCARG(uap, uid) == (linux_uid_t)-1) ?
790 (uid_t)-1 : SCARG(uap, uid);
791 SCARG(&bfa, gid) = ((linux_gid_t)SCARG(uap, gid) == (linux_gid_t)-1) ?
792 (gid_t)-1 : SCARG(uap, gid);
793
794 return sys___posix_fchown(l, &bfa, retval);
795 }
796
797 int
798 linux_sys_lchown16(l, v, retval)
799 struct lwp *l;
800 void *v;
801 register_t *retval;
802 {
803 struct linux_sys_lchown16_args /* {
804 syscallarg(char *) path;
805 syscallarg(int) uid;
806 syscallarg(int) gid;
807 } */ *uap = v;
808 struct proc *p = l->l_proc;
809 struct sys___posix_lchown_args bla;
810 caddr_t sg = stackgap_init(p, 0);
811
812 CHECK_ALT_SYMLINK(l, &sg, SCARG(uap, path));
813
814 SCARG(&bla, path) = SCARG(uap, path);
815 SCARG(&bla, uid) = ((linux_uid_t)SCARG(uap, uid) == (linux_uid_t)-1) ?
816 (uid_t)-1 : SCARG(uap, uid);
817 SCARG(&bla, gid) = ((linux_gid_t)SCARG(uap, gid) == (linux_gid_t)-1) ?
818 (gid_t)-1 : SCARG(uap, gid);
819
820 return sys___posix_lchown(l, &bla, retval);
821 }
822 #endif /* __i386__ || __m68k__ || __arm__ */
823 #if defined (__i386__) || defined (__m68k__) || \
824 defined (__powerpc__) || defined (__mips__) || defined(__arm__)
825 int
826 linux_sys_chown(l, v, retval)
827 struct lwp *l;
828 void *v;
829 register_t *retval;
830 {
831 struct linux_sys_chown_args /* {
832 syscallarg(char *) path;
833 syscallarg(int) uid;
834 syscallarg(int) gid;
835 } */ *uap = v;
836 struct proc *p = l->l_proc;
837 caddr_t sg = stackgap_init(p, 0);
838
839 CHECK_ALT_EXIST(l, &sg, SCARG(uap, path));
840
841 return sys___posix_chown(l, uap, retval);
842 }
843
844 int
845 linux_sys_lchown(l, v, retval)
846 struct lwp *l;
847 void *v;
848 register_t *retval;
849 {
850 struct linux_sys_lchown_args /* {
851 syscallarg(char *) path;
852 syscallarg(int) uid;
853 syscallarg(int) gid;
854 } */ *uap = v;
855 struct proc *p = l->l_proc;
856 caddr_t sg = stackgap_init(p, 0);
857
858 CHECK_ALT_SYMLINK(l, &sg, SCARG(uap, path));
859
860 return sys___posix_lchown(l, uap, retval);
861 }
862 #endif /* __i386__ || __m68k__ || __powerpc__ || __mips__ || __arm__ */
863
864 int
865 linux_sys_rename(l, v, retval)
866 struct lwp *l;
867 void *v;
868 register_t *retval;
869 {
870 struct linux_sys_rename_args /* {
871 syscallarg(const char *) from;
872 syscallarg(const char *) to;
873 } */ *uap = v;
874 struct proc *p = l->l_proc;
875 caddr_t sg = stackgap_init(p, 0);
876
877 CHECK_ALT_EXIST(l, &sg, SCARG(uap, from));
878 CHECK_ALT_CREAT(l, &sg, SCARG(uap, to));
879
880 return sys___posix_rename(l, uap, retval);
881 }
882
883 int
884 linux_sys_mkdir(l, v, retval)
885 struct lwp *l;
886 void *v;
887 register_t *retval;
888 {
889 struct linux_sys_mkdir_args /* {
890 syscallarg(const char *) path;
891 syscallarg(int) mode;
892 } */ *uap = v;
893 struct proc *p = l->l_proc;
894 caddr_t sg = stackgap_init(p, 0);
895
896 CHECK_ALT_CREAT(l, &sg, SCARG(uap, path));
897
898 return sys_mkdir(l, uap, retval);
899 }
900
901 int
902 linux_sys_rmdir(l, v, retval)
903 struct lwp *l;
904 void *v;
905 register_t *retval;
906 {
907 struct linux_sys_rmdir_args /* {
908 syscallarg(const char *) path;
909 } */ *uap = v;
910 struct proc *p = l->l_proc;
911 caddr_t sg = stackgap_init(p, 0);
912
913 CHECK_ALT_EXIST(l, &sg, SCARG(uap, path));
914
915 return sys_rmdir(l, uap, retval);
916 }
917
918 int
919 linux_sys_symlink(l, v, retval)
920 struct lwp *l;
921 void *v;
922 register_t *retval;
923 {
924 struct linux_sys_symlink_args /* {
925 syscallarg(const char *) path;
926 syscallarg(const char *) to;
927 } */ *uap = v;
928 struct proc *p = l->l_proc;
929 caddr_t sg = stackgap_init(p, 0);
930
931 CHECK_ALT_EXIST(l, &sg, SCARG(uap, path));
932 CHECK_ALT_CREAT(l, &sg, SCARG(uap, to));
933
934 return sys_symlink(l, uap, retval);
935 }
936
937 int
938 linux_sys_link(l, v, retval)
939 struct lwp *l;
940 void *v;
941 register_t *retval;
942 {
943 struct linux_sys_link_args /* {
944 syscallarg(const char *) path;
945 syscallarg(const char *) link;
946 } */ *uap = v;
947 struct proc *p = l->l_proc;
948 caddr_t sg = stackgap_init(p, 0);
949
950 CHECK_ALT_EXIST(l, &sg, SCARG(uap, path));
951 CHECK_ALT_CREAT(l, &sg, SCARG(uap, link));
952
953 return sys_link(l, uap, retval);
954 }
955
956 int
957 linux_sys_readlink(l, v, retval)
958 struct lwp *l;
959 void *v;
960 register_t *retval;
961 {
962 struct linux_sys_readlink_args /* {
963 syscallarg(const char *) name;
964 syscallarg(char *) buf;
965 syscallarg(int) count;
966 } */ *uap = v;
967 struct proc *p = l->l_proc;
968 caddr_t sg = stackgap_init(p, 0);
969
970 CHECK_ALT_SYMLINK(l, &sg, SCARG(uap, name));
971
972 return sys_readlink(l, uap, retval);
973 }
974
975 int
976 linux_sys_truncate(l, v, retval)
977 struct lwp *l;
978 void *v;
979 register_t *retval;
980 {
981 struct linux_sys_truncate_args /* {
982 syscallarg(const char *) path;
983 syscallarg(long) length;
984 } */ *uap = v;
985 struct proc *p = l->l_proc;
986 caddr_t sg = stackgap_init(p, 0);
987
988 CHECK_ALT_EXIST(l, &sg, SCARG(uap, path));
989
990 return compat_43_sys_truncate(l, uap, retval);
991 }
992
993 /*
994 * This is just fsync() for now (just as it is in the Linux kernel)
995 * Note: this is not implemented under Linux on Alpha and Arm
996 * but should still be defined in our syscalls.master.
997 * (syscall #148 on the arm)
998 */
999 int
1000 linux_sys_fdatasync(l, v, retval)
1001 struct lwp *l;
1002 void *v;
1003 register_t *retval;
1004 {
1005 #ifdef notdef
1006 struct linux_sys_fdatasync_args /* {
1007 syscallarg(int) fd;
1008 } */ *uap = v;
1009 #endif
1010 return sys_fsync(l, v, retval);
1011 }
1012
1013 /*
1014 * pread(2).
1015 */
1016 int
1017 linux_sys_pread(l, v, retval)
1018 struct lwp *l;
1019 void *v;
1020 register_t *retval;
1021 {
1022 struct linux_sys_pread_args /* {
1023 syscallarg(int) fd;
1024 syscallarg(void *) buf;
1025 syscallarg(size_t) nbyte;
1026 syscallarg(linux_off_t) offset;
1027 } */ *uap = v;
1028 struct sys_pread_args pra;
1029
1030 SCARG(&pra, fd) = SCARG(uap, fd);
1031 SCARG(&pra, buf) = SCARG(uap, buf);
1032 SCARG(&pra, nbyte) = SCARG(uap, nbyte);
1033 SCARG(&pra, offset) = SCARG(uap, offset);
1034
1035 return sys_read(l, &pra, retval);
1036 }
1037
1038 /*
1039 * pwrite(2).
1040 */
1041 int
1042 linux_sys_pwrite(l, v, retval)
1043 struct lwp *l;
1044 void *v;
1045 register_t *retval;
1046 {
1047 struct linux_sys_pwrite_args /* {
1048 syscallarg(int) fd;
1049 syscallarg(void *) buf;
1050 syscallarg(size_t) nbyte;
1051 syscallarg(linux_off_t) offset;
1052 } */ *uap = v;
1053 struct sys_pwrite_args pra;
1054
1055 SCARG(&pra, fd) = SCARG(uap, fd);
1056 SCARG(&pra, buf) = SCARG(uap, buf);
1057 SCARG(&pra, nbyte) = SCARG(uap, nbyte);
1058 SCARG(&pra, offset) = SCARG(uap, offset);
1059
1060 return sys_write(l, &pra, retval);
1061 }
1062