1 /*-
2 * Copyright (c) 1994-1995 Søren Schmidt
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer
10 * in this position and unchanged.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. The name of the author may not be used to endorse or promote products
15 * derived from this software withough specific prior written permission
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 *
28 * $FreeBSD$
29 */
30
31 #include "opt_compat.h"
32
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/sysproto.h>
36 #include <sys/fcntl.h>
37 #include <sys/file.h>
38 #include <sys/filedesc.h>
39 #include <sys/lock.h>
40 #include <sys/proc.h>
41 #include <sys/vnode.h>
42 #include <sys/malloc.h>
43 #include <sys/dirent.h>
44 #include <sys/conf.h>
45 #include <sys/tty.h>
46
47 #include <i386/linux/linux.h>
48 #include <i386/linux/linux_proto.h>
49 #include <i386/linux/linux_util.h>
50
51 int
52 linux_creat(struct proc *p, struct linux_creat_args *args)
53 {
54 struct open_args /* {
55 char *path;
56 int flags;
57 int mode;
58 } */ bsd_open_args;
59 caddr_t sg;
60
61 sg = stackgap_init();
62 CHECKALTCREAT(p, &sg, args->path);
63
64 #ifdef DEBUG
65 printf("Linux-emul(%d): creat(%s, %d)\n",
66 p->p_pid, args->path, args->mode);
67 #endif
68 bsd_open_args.path = args->path;
69 bsd_open_args.mode = args->mode;
70 bsd_open_args.flags = O_WRONLY | O_CREAT | O_TRUNC;
71 return open(p, &bsd_open_args);
72 }
73
74 int
75 linux_open(struct proc *p, struct linux_open_args *args)
76 {
77 struct open_args /* {
78 char *path;
79 int flags;
80 int mode;
81 } */ bsd_open_args;
82 int error;
83 caddr_t sg;
84
85 sg = stackgap_init();
86
87 if (args->flags & LINUX_O_CREAT)
88 CHECKALTCREAT(p, &sg, args->path);
89 else
90 CHECKALTEXIST(p, &sg, args->path);
91
92 #ifdef DEBUG
93 printf("Linux-emul(%d): open(%s, 0x%x, 0x%x)\n",
94 p->p_pid, args->path, args->flags, args->mode);
95 #endif
96 bsd_open_args.flags = 0;
97 if (args->flags & LINUX_O_RDONLY)
98 bsd_open_args.flags |= O_RDONLY;
99 if (args->flags & LINUX_O_WRONLY)
100 bsd_open_args.flags |= O_WRONLY;
101 if (args->flags & LINUX_O_RDWR)
102 bsd_open_args.flags |= O_RDWR;
103 if (args->flags & LINUX_O_NDELAY)
104 bsd_open_args.flags |= O_NONBLOCK;
105 if (args->flags & LINUX_O_APPEND)
106 bsd_open_args.flags |= O_APPEND;
107 if (args->flags & LINUX_O_SYNC)
108 bsd_open_args.flags |= O_FSYNC;
109 if (args->flags & LINUX_O_NONBLOCK)
110 bsd_open_args.flags |= O_NONBLOCK;
111 if (args->flags & LINUX_FASYNC)
112 bsd_open_args.flags |= O_ASYNC;
113 if (args->flags & LINUX_O_CREAT)
114 bsd_open_args.flags |= O_CREAT;
115 if (args->flags & LINUX_O_TRUNC)
116 bsd_open_args.flags |= O_TRUNC;
117 if (args->flags & LINUX_O_EXCL)
118 bsd_open_args.flags |= O_EXCL;
119 if (args->flags & LINUX_O_NOCTTY)
120 bsd_open_args.flags |= O_NOCTTY;
121 bsd_open_args.path = args->path;
122 bsd_open_args.mode = args->mode;
123
124 error = open(p, &bsd_open_args);
125 if (!error && !(bsd_open_args.flags & O_NOCTTY) &&
126 SESS_LEADER(p) && !(p->p_flag & P_CONTROLT)) {
127 struct filedesc *fdp = p->p_fd;
128 struct file *fp = fdp->fd_ofiles[p->p_retval[0]];
129
130 if (fp->f_type == DTYPE_VNODE)
131 (fp->f_ops->fo_ioctl)(fp, TIOCSCTTY, (caddr_t) 0, p);
132 }
133 #ifdef DEBUG
134 printf("Linux-emul(%d): open returns error %d\n",
135 p->p_pid, error);
136 #endif
137 return error;
138 }
139
140 struct linux_flock {
141 short l_type;
142 short l_whence;
143 linux_off_t l_start;
144 linux_off_t l_len;
145 linux_pid_t l_pid;
146 };
147
148 static void
149 linux_to_bsd_flock(struct linux_flock *linux_flock, struct flock *bsd_flock)
150 {
151 switch (linux_flock->l_type) {
152 case LINUX_F_RDLCK:
153 bsd_flock->l_type = F_RDLCK;
154 break;
155 case LINUX_F_WRLCK:
156 bsd_flock->l_type = F_WRLCK;
157 break;
158 case LINUX_F_UNLCK:
159 bsd_flock->l_type = F_UNLCK;
160 break;
161 }
162 bsd_flock->l_whence = linux_flock->l_whence;
163 bsd_flock->l_start = (off_t)linux_flock->l_start;
164 bsd_flock->l_len = (off_t)linux_flock->l_len;
165 bsd_flock->l_pid = (pid_t)linux_flock->l_pid;
166 }
167
168 static void
169 bsd_to_linux_flock(struct flock *bsd_flock, struct linux_flock *linux_flock)
170 {
171 switch (bsd_flock->l_type) {
172 case F_RDLCK:
173 linux_flock->l_type = LINUX_F_RDLCK;
174 break;
175 case F_WRLCK:
176 linux_flock->l_type = LINUX_F_WRLCK;
177 break;
178 case F_UNLCK:
179 linux_flock->l_type = LINUX_F_UNLCK;
180 break;
181 }
182 linux_flock->l_whence = bsd_flock->l_whence;
183 linux_flock->l_start = (linux_off_t)bsd_flock->l_start;
184 linux_flock->l_len = (linux_off_t)bsd_flock->l_len;
185 linux_flock->l_pid = (linux_pid_t)bsd_flock->l_pid;
186 }
187
188 int
189 linux_fcntl(struct proc *p, struct linux_fcntl_args *args)
190 {
191 int error, result;
192 struct fcntl_args /* {
193 int fd;
194 int cmd;
195 int arg;
196 } */ fcntl_args;
197 struct linux_flock linux_flock;
198 struct flock *bsd_flock;
199 struct filedesc *fdp;
200 struct file *fp;
201 struct vnode *vp;
202 struct vattr va;
203 long pgid;
204 struct pgrp *pgrp;
205 struct tty *tp, *(*d_tty) __P((dev_t));
206 caddr_t sg;
207
208 sg = stackgap_init();
209 bsd_flock = (struct flock *)stackgap_alloc(&sg, sizeof(struct flock));
210 d_tty = NULL;
211
212 #ifdef DEBUG
213 printf("Linux-emul(%d): fcntl(%d, %08x, *)\n",
214 p->p_pid, args->fd, args->cmd);
215 #endif
216 fcntl_args.fd = args->fd;
217
218 switch (args->cmd) {
219 case LINUX_F_DUPFD:
220 fcntl_args.cmd = F_DUPFD;
221 fcntl_args.arg = args->arg;
222 return fcntl(p, &fcntl_args);
223
224 case LINUX_F_GETFD:
225 fcntl_args.cmd = F_GETFD;
226 return fcntl(p, &fcntl_args);
227
228 case LINUX_F_SETFD:
229 fcntl_args.cmd = F_SETFD;
230 fcntl_args.arg = args->arg;
231 return fcntl(p, &fcntl_args);
232
233 case LINUX_F_GETFL:
234 fcntl_args.cmd = F_GETFL;
235 error = fcntl(p, &fcntl_args);
236 result = p->p_retval[0];
237 p->p_retval[0] = 0;
238 if (result & O_RDONLY) p->p_retval[0] |= LINUX_O_RDONLY;
239 if (result & O_WRONLY) p->p_retval[0] |= LINUX_O_WRONLY;
240 if (result & O_RDWR) p->p_retval[0] |= LINUX_O_RDWR;
241 if (result & O_NDELAY) p->p_retval[0] |= LINUX_O_NONBLOCK;
242 if (result & O_APPEND) p->p_retval[0] |= LINUX_O_APPEND;
243 if (result & O_FSYNC) p->p_retval[0] |= LINUX_O_SYNC;
244 if (result & O_ASYNC) p->p_retval[0] |= LINUX_FASYNC;
245 return error;
246
247 case LINUX_F_SETFL:
248 fcntl_args.arg = 0;
249 if (args->arg & LINUX_O_NDELAY) fcntl_args.arg |= O_NONBLOCK;
250 if (args->arg & LINUX_O_APPEND) fcntl_args.arg |= O_APPEND;
251 if (args->arg & LINUX_O_SYNC) fcntl_args.arg |= O_FSYNC;
252 if (args->arg & LINUX_FASYNC) fcntl_args.arg |= O_ASYNC;
253 fcntl_args.cmd = F_SETFL;
254 return fcntl(p, &fcntl_args);
255
256 case LINUX_F_GETLK:
257 if ((error = copyin((caddr_t)args->arg, (caddr_t)&linux_flock,
258 sizeof(struct linux_flock))))
259 return error;
260 linux_to_bsd_flock(&linux_flock, bsd_flock);
261 fcntl_args.cmd = F_GETLK;
262 fcntl_args.arg = (int)bsd_flock;
263 if (error = fcntl(p, &fcntl_args))
264 return error;
265 bsd_to_linux_flock(bsd_flock, &linux_flock);
266 return copyout((caddr_t)&linux_flock, (caddr_t)args->arg,
267 sizeof(struct linux_flock));
268
269 case LINUX_F_SETLK:
270 if ((error = copyin((caddr_t)args->arg, (caddr_t)&linux_flock,
271 sizeof(struct linux_flock))))
272 return error;
273 linux_to_bsd_flock(&linux_flock, bsd_flock);
274 fcntl_args.cmd = F_SETLK;
275 fcntl_args.arg = (int)bsd_flock;
276 return fcntl(p, &fcntl_args);
277
278 case LINUX_F_SETLKW:
279 if ((error = copyin((caddr_t)args->arg, (caddr_t)&linux_flock,
280 sizeof(struct linux_flock))))
281 return error;
282 linux_to_bsd_flock(&linux_flock, bsd_flock);
283 fcntl_args.cmd = F_SETLKW;
284 fcntl_args.arg = (int)bsd_flock;
285 return fcntl(p, &fcntl_args);
286
287 case LINUX_F_SETOWN:
288 case LINUX_F_GETOWN:
289 /*
290 * We need to route around the normal fcntl() for these calls,
291 * since it uses TIOC{G,S}PGRP, which is too restrictive for
292 * Linux F_{G,S}ETOWN semantics. For sockets, this problem
293 * does not exist.
294 */
295 fdp = p->p_fd;
296 if ((u_int)args->fd >= fdp->fd_nfiles ||
297 (fp = fdp->fd_ofiles[args->fd]) == NULL)
298 return EBADF;
299 if (fp->f_type == DTYPE_SOCKET) {
300 fcntl_args.cmd = args->cmd == LINUX_F_SETOWN ? F_SETOWN : F_GETOWN;
301 fcntl_args.arg = args->arg;
302 return fcntl(p, &fcntl_args);
303 }
304 vp = (struct vnode *)fp->f_data;
305 if (vp->v_type != VCHR)
306 return EINVAL;
307 if ((error = VOP_GETATTR(vp, &va, p->p_ucred, p)))
308 return error;
309
310 d_tty = cdevsw[major(va.va_rdev)]->d_devtotty;
311 if (!d_tty || (!(tp = (*d_tty)(va.va_rdev))))
312 return EINVAL;
313 if (args->cmd == LINUX_F_GETOWN) {
314 p->p_retval[0] = tp->t_pgrp ? tp->t_pgrp->pg_id : NO_PID;
315 return 0;
316 }
317 if ((long)args->arg <= 0) {
318 pgid = -(long)args->arg;
319 } else {
320 struct proc *p1 = pfind((long)args->arg);
321 if (p1 == 0)
322 return (ESRCH);
323 pgid = (long)p1->p_pgrp->pg_id;
324 }
325 pgrp = pgfind(pgid);
326 if (pgrp == NULL || pgrp->pg_session != p->p_session)
327 return EPERM;
328 tp->t_pgrp = pgrp;
329 return 0;
330 }
331 return EINVAL;
332 }
333
334 int
335 linux_lseek(struct proc *p, struct linux_lseek_args *args)
336 {
337
338 struct lseek_args /* {
339 int fd;
340 int pad;
341 off_t offset;
342 int whence;
343 } */ tmp_args;
344 int error;
345
346 #ifdef DEBUG
347 printf("Linux-emul(%ld): lseek(%d, %ld, %d)\n",
348 (long)p->p_pid, args->fdes, args->off, args->whence);
349 #endif
350 tmp_args.fd = args->fdes;
351 tmp_args.offset = (off_t)args->off;
352 tmp_args.whence = args->whence;
353 error = lseek(p, &tmp_args);
354 return error;
355 }
356
357 int
358 linux_llseek(struct proc *p, struct linux_llseek_args *args)
359 {
360 struct lseek_args bsd_args;
361 int error;
362 off_t off;
363
364 #ifdef DEBUG
365 printf("Linux-emul(%d): llseek(%d, %d:%d, %d)\n",
366 p->p_pid, args->fd, args->ohigh, args->olow, args->whence);
367 #endif
368 off = (args->olow) | (((off_t) args->ohigh) << 32);
369
370 bsd_args.fd = args->fd;
371 bsd_args.offset = off;
372 bsd_args.whence = args->whence;
373
374 if ((error = lseek(p, &bsd_args)))
375 return error;
376
377 if ((error = copyout(p->p_retval, (caddr_t)args->res, sizeof (off_t))))
378 return error;
379
380 p->p_retval[0] = 0;
381 return 0;
382 }
383
384
385 struct linux_dirent {
386 long dino;
387 linux_off_t doff;
388 unsigned short dreclen;
389 char dname[LINUX_NAME_MAX + 1];
390 };
391
392 #define LINUX_RECLEN(de,namlen) \
393 ALIGN((((char *)&(de)->dname - (char *)de) + (namlen) + 1))
394
395 int
396 linux_readdir(struct proc *p, struct linux_readdir_args *args)
397 {
398 struct linux_getdents_args lda;
399
400 lda.fd = args->fd;
401 lda.dent = args->dent;
402 lda.count = 1;
403 return linux_getdents(p, &lda);
404 }
405
406 int
407 linux_getdents(struct proc *p, struct linux_getdents_args *args)
408 {
409 register struct dirent *bdp;
410 struct vnode *vp;
411 caddr_t inp, buf; /* BSD-format */
412 int len, reclen; /* BSD-format */
413 caddr_t outp; /* Linux-format */
414 int resid, linuxreclen=0; /* Linux-format */
415 struct file *fp;
416 struct uio auio;
417 struct iovec aiov;
418 struct vattr va;
419 off_t off;
420 struct linux_dirent linux_dirent;
421 int buflen, error, eofflag, nbytes, justone;
422 u_long *cookies = NULL, *cookiep;
423 int ncookies;
424
425 #ifdef DEBUG
426 printf("Linux-emul(%d): getdents(%d, *, %d)\n",
427 p->p_pid, args->fd, args->count);
428 #endif
429 if ((error = getvnode(p->p_fd, args->fd, &fp)) != 0) {
430 return (error);
431 }
432
433 if ((fp->f_flag & FREAD) == 0)
434 return (EBADF);
435
436 vp = (struct vnode *) fp->f_data;
437
438 if (vp->v_type != VDIR)
439 return (EINVAL);
440
441 if ((error = VOP_GETATTR(vp, &va, p->p_ucred, p))) {
442 return error;
443 }
444
445 nbytes = args->count;
446 if (nbytes == 1) {
447 nbytes = sizeof (struct linux_dirent);
448 justone = 1;
449 }
450 else
451 justone = 0;
452
453 off = fp->f_offset;
454 #define DIRBLKSIZ 512 /* XXX we used to use ufs's DIRBLKSIZ */
455 buflen = max(DIRBLKSIZ, nbytes);
456 buflen = min(buflen, MAXBSIZE);
457 buf = malloc(buflen, M_TEMP, M_WAITOK);
458 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
459 again:
460 aiov.iov_base = buf;
461 aiov.iov_len = buflen;
462 auio.uio_iov = &aiov;
463 auio.uio_iovcnt = 1;
464 auio.uio_rw = UIO_READ;
465 auio.uio_segflg = UIO_SYSSPACE;
466 auio.uio_procp = p;
467 auio.uio_resid = buflen;
468 auio.uio_offset = off;
469
470 if (cookies) {
471 free(cookies, M_TEMP);
472 cookies = NULL;
473 }
474
475 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, &ncookies, &cookies);
476 if (error) {
477 goto out;
478 }
479
480 inp = buf;
481 outp = (caddr_t) args->dent;
482 resid = nbytes;
483 if ((len = buflen - auio.uio_resid) <= 0) {
484 goto eof;
485 }
486
487 cookiep = cookies;
488
489 if (cookies) {
490 /*
491 * When using cookies, the vfs has the option of reading from
492 * a different offset than that supplied (UFS truncates the
493 * offset to a block boundary to make sure that it never reads
494 * partway through a directory entry, even if the directory
495 * has been compacted).
496 */
497 while (len > 0 && ncookies > 0 && *cookiep <= off) {
498 bdp = (struct dirent *) inp;
499 len -= bdp->d_reclen;
500 inp += bdp->d_reclen;
501 cookiep++;
502 ncookies--;
503 }
504 }
505
506 while (len > 0) {
507 if (cookiep && ncookies == 0)
508 break;
509 bdp = (struct dirent *) inp;
510 reclen = bdp->d_reclen;
511 if (reclen & 3) {
512 printf("linux_readdir: reclen=%d\n", reclen);
513 error = EFAULT;
514 goto out;
515 }
516
517 if (bdp->d_fileno == 0) {
518 inp += reclen;
519 if (cookiep) {
520 off = *cookiep++;
521 ncookies--;
522 } else
523 off += reclen;
524 len -= reclen;
525 continue;
526 }
527 linuxreclen = LINUX_RECLEN(&linux_dirent, bdp->d_namlen);
528 if (reclen > len || resid < linuxreclen) {
529 outp++;
530 break;
531 }
532 linux_dirent.dino = (long) bdp->d_fileno;
533 if (justone) {
534 /*
535 * old linux-style readdir usage.
536 */
537 linux_dirent.doff = (linux_off_t) linuxreclen;
538 linux_dirent.dreclen = (u_short) bdp->d_namlen;
539 } else {
540 linux_dirent.doff = (linux_off_t)(off + reclen);
541 linux_dirent.dreclen = (u_short) linuxreclen;
542 }
543 strcpy(linux_dirent.dname, bdp->d_name);
544 if ((error = copyout((caddr_t)&linux_dirent, outp, linuxreclen))) {
545 goto out;
546 }
547 inp += reclen;
548 if (cookiep) {
549 off = *cookiep++;
550 ncookies--;
551 } else
552 off += reclen;
553 outp += linuxreclen;
554 resid -= linuxreclen;
555 len -= reclen;
556 if (justone)
557 break;
558 }
559
560 if (outp == (caddr_t) args->dent)
561 goto again;
562 fp->f_offset = off;
563
564 if (justone)
565 nbytes = resid + linuxreclen;
566
567 eof:
568 p->p_retval[0] = nbytes - resid;
569 out:
570 if (cookies)
571 free(cookies, M_TEMP);
572 VOP_UNLOCK(vp, 0, p);
573 free(buf, M_TEMP);
574 return error;
575 }
576
577 /*
578 * These exist mainly for hooks for doing /compat/linux translation.
579 */
580
581 int
582 linux_access(struct proc *p, struct linux_access_args *args)
583 {
584 struct access_args bsd;
585 caddr_t sg;
586
587 sg = stackgap_init();
588 CHECKALTEXIST(p, &sg, args->path);
589
590 #ifdef DEBUG
591 printf("Linux-emul(%d): access(%s, %d)\n",
592 p->p_pid, args->path, args->flags);
593 #endif
594 bsd.path = args->path;
595 bsd.flags = args->flags;
596
597 return access(p, &bsd);
598 }
599
600 int
601 linux_unlink(struct proc *p, struct linux_unlink_args *args)
602 {
603 struct unlink_args bsd;
604 caddr_t sg;
605
606 sg = stackgap_init();
607 CHECKALTEXIST(p, &sg, args->path);
608
609 #ifdef DEBUG
610 printf("Linux-emul(%d): unlink(%s)\n",
611 p->p_pid, args->path);
612 #endif
613 bsd.path = args->path;
614
615 return unlink(p, &bsd);
616 }
617
618 int
619 linux_chdir(struct proc *p, struct linux_chdir_args *args)
620 {
621 struct chdir_args bsd;
622 caddr_t sg;
623
624 sg = stackgap_init();
625 CHECKALTEXIST(p, &sg, args->path);
626
627 #ifdef DEBUG
628 printf("Linux-emul(%d): chdir(%s)\n",
629 p->p_pid, args->path);
630 #endif
631 bsd.path = args->path;
632
633 return chdir(p, &bsd);
634 }
635
636 int
637 linux_chmod(struct proc *p, struct linux_chmod_args *args)
638 {
639 struct chmod_args bsd;
640 caddr_t sg;
641
642 sg = stackgap_init();
643 CHECKALTEXIST(p, &sg, args->path);
644
645 #ifdef DEBUG
646 printf("Linux-emul(%d): chmod(%s, %d)\n",
647 p->p_pid, args->path, args->mode);
648 #endif
649 bsd.path = args->path;
650 bsd.mode = args->mode;
651
652 return chmod(p, &bsd);
653 }
654
655 int
656 linux_chown(struct proc *p, struct linux_chown_args *args)
657 {
658 struct chown_args bsd;
659 caddr_t sg;
660
661 sg = stackgap_init();
662 CHECKALTEXIST(p, &sg, args->path);
663
664 #ifdef DEBUG
665 printf("Linux-emul(%d): chown(%s, %d, %d)\n",
666 p->p_pid, args->path, args->uid, args->gid);
667 #endif
668 bsd.path = args->path;
669 /* XXX size casts here */
670 bsd.uid = args->uid;
671 bsd.gid = args->gid;
672
673 return chown(p, &bsd);
674 }
675
676 int
677 linux_lchown(struct proc *p, struct linux_lchown_args *args)
678 {
679 struct lchown_args bsd;
680 caddr_t sg;
681
682 sg = stackgap_init();
683 CHECKALTEXIST(p, &sg, args->path);
684
685 #ifdef DEBUG
686 printf("Linux-emul(%d): lchown(%s, %d, %d)\n",
687 p->p_pid, args->path, args->uid, args->gid);
688 #endif
689 bsd.path = args->path;
690 /* XXX size casts here */
691 bsd.uid = args->uid;
692 bsd.gid = args->gid;
693
694 return lchown(p, &bsd);
695 }
696
697 int
698 linux_mkdir(struct proc *p, struct linux_mkdir_args *args)
699 {
700 struct mkdir_args bsd;
701 caddr_t sg;
702
703 sg = stackgap_init();
704 CHECKALTCREAT(p, &sg, args->path);
705
706 #ifdef DEBUG
707 printf("Linux-emul(%d): mkdir(%s, %d)\n",
708 p->p_pid, args->path, args->mode);
709 #endif
710 bsd.path = args->path;
711 bsd.mode = args->mode;
712
713 return mkdir(p, &bsd);
714 }
715
716 int
717 linux_rmdir(struct proc *p, struct linux_rmdir_args *args)
718 {
719 struct rmdir_args bsd;
720 caddr_t sg;
721
722 sg = stackgap_init();
723 CHECKALTEXIST(p, &sg, args->path);
724
725 #ifdef DEBUG
726 printf("Linux-emul(%d): rmdir(%s)\n",
727 p->p_pid, args->path);
728 #endif
729 bsd.path = args->path;
730
731 return rmdir(p, &bsd);
732 }
733
734 int
735 linux_rename(struct proc *p, struct linux_rename_args *args)
736 {
737 struct rename_args bsd;
738 caddr_t sg;
739
740 sg = stackgap_init();
741 CHECKALTEXIST(p, &sg, args->from);
742 CHECKALTCREAT(p, &sg, args->to);
743
744 #ifdef DEBUG
745 printf("Linux-emul(%d): rename(%s, %s)\n",
746 p->p_pid, args->from, args->to);
747 #endif
748 bsd.from = args->from;
749 bsd.to = args->to;
750
751 return rename(p, &bsd);
752 }
753
754 int
755 linux_symlink(struct proc *p, struct linux_symlink_args *args)
756 {
757 struct symlink_args bsd;
758 caddr_t sg;
759
760 sg = stackgap_init();
761 CHECKALTEXIST(p, &sg, args->path);
762 CHECKALTCREAT(p, &sg, args->to);
763
764 #ifdef DEBUG
765 printf("Linux-emul(%d): symlink(%s, %s)\n",
766 p->p_pid, args->path, args->to);
767 #endif
768 bsd.path = args->path;
769 bsd.link = args->to;
770
771 return symlink(p, &bsd);
772 }
773
774 int
775 linux_execve(struct proc *p, struct linux_execve_args *args)
776 {
777 struct execve_args bsd;
778 caddr_t sg;
779
780 sg = stackgap_init();
781 CHECKALTEXIST(p, &sg, args->path);
782
783 #ifdef DEBUG
784 printf("Linux-emul(%d): execve(%s)\n",
785 p->p_pid, args->path);
786 #endif
787 bsd.fname = args->path;
788 bsd.argv = args->argp;
789 bsd.envv = args->envp;
790
791 return execve(p, &bsd);
792 }
793
794 int
795 linux_readlink(struct proc *p, struct linux_readlink_args *args)
796 {
797 struct readlink_args bsd;
798 caddr_t sg;
799
800 sg = stackgap_init();
801 CHECKALTEXIST(p, &sg, args->name);
802
803 #ifdef DEBUG
804 printf("Linux-emul(%ld): readlink(%s, %p, %d)\n",
805 (long)p->p_pid, args->name, (void *)args->buf, args->count);
806 #endif
807 bsd.path = args->name;
808 bsd.buf = args->buf;
809 bsd.count = args->count;
810
811 return readlink(p, &bsd);
812 }
813
814 int
815 linux_truncate(struct proc *p, struct linux_truncate_args *args)
816 {
817 struct truncate_args bsd;
818 caddr_t sg;
819
820 sg = stackgap_init();
821 CHECKALTEXIST(p, &sg, args->path);
822
823 #ifdef DEBUG
824 printf("Linux-emul(%d): truncate(%s, %ld)\n",
825 p->p_pid, args->path, args->length);
826 #endif
827 bsd.path = args->path;
828 bsd.length = args->length;
829
830 return truncate(p, &bsd);
831 }
832
833 int
834 linux_link(struct proc *p, struct linux_link_args *args)
835 {
836 struct link_args bsd;
837 caddr_t sg;
838
839 sg = stackgap_init();
840 CHECKALTEXIST(p, &sg, args->path);
841 CHECKALTCREAT(p, &sg, args->to);
842
843 #ifdef DEBUG
844 printf("Linux-emul(%ld): link(%s, %s)\n", (long)p->p_pid,
845 args->path, args->to);
846 #endif
847
848 bsd.path = args->path;
849 bsd.link = args->to;
850
851 return link(p, &bsd);
852 }
853
854 int
855 linux_getcwd(p, args)
856 struct proc *p;
857 struct linux_getcwd_args *args;
858 {
859 struct __getcwd_args bsd;
860 caddr_t sg;
861 int error, len;
862
863 #ifdef DEBUG
864 printf("Linux-emul(%ld): getcwd(%p, %ld)\n", (long)p->p_pid,
865 args->buf, args->bufsize);
866 #endif
867
868 sg = stackgap_init();
869 bsd.buf = stackgap_alloc(&sg, SPARE_USRSPACE);
870 bsd.buflen = SPARE_USRSPACE;
871 error = __getcwd(p, &bsd);
872 if (!error) {
873 len = strlen(bsd.buf) + 1;
874 if (len <= args->bufsize) {
875 p->p_retval[0] = len;
876 error = copyout(bsd.buf, args->buf, len);
877 }
878 else
879 error = ERANGE;
880 }
881 return (error);
882 }
883
884 int
885 linux_fdatasync(p, uap)
886 struct proc *p;
887 struct linux_fdatasync_args *uap;
888 {
889 struct fsync_args bsd;
890
891 bsd.fd = uap->fd;
892 return fsync(p, &bsd);
893 }
894
Cache object: 215496ea02d05a0d6fc816422410f6d4
|