1 /* $NetBSD: osf1_file.c,v 1.36 2009/05/29 09:32:41 njoly Exp $ */
4 * Copyright (c) 1999 Christopher G. Demetriou. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
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. All advertising materials mentioning features or use of this software
15 * must display the following acknowledgement:
16 * This product includes software developed by Christopher G. Demetriou
17 * for the NetBSD Project.
18 * 4. The name of the author may not be used to endorse or promote products
19 * derived from this software without specific prior written permission
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 * Copyright (c) 1994, 1995 Carnegie-Mellon University.
35 * All rights reserved.
37 * Author: Chris G. Demetriou
39 * Permission to use, copy, modify and distribute this software and
40 * its documentation is hereby granted, provided that both the copyright
41 * notice and this permission notice appear in all copies of the
42 * software, derivative works or modified versions, and any portions
43 * thereof, and that both notices appear in supporting documentation.
45 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
46 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
47 * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
49 * Carnegie Mellon requests users of this software to return to
51 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
52 * School of Computer Science
53 * Carnegie Mellon University
54 * Pittsburgh PA 15213-3890
56 * any improvements or extensions that they make and grant Carnegie the
57 * rights to redistribute these changes.
60 #include <sys/cdefs.h>
61 __KERNEL_RCSID(0, "$NetBSD: osf1_file.c,v 1.36 2009/05/29 09:32:41 njoly Exp $");
63 #if defined(_KERNEL_OPT)
64 #include "opt_syscall_debug.h"
67 #include <sys/param.h>
68 #include <sys/systm.h>
69 #include <sys/namei.h>
73 #include <sys/filedesc.h>
74 #include <sys/kernel.h>
75 #include <sys/malloc.h>
77 #include <sys/mount.h>
78 #include <sys/signal.h>
79 #include <sys/signalvar.h>
80 #include <sys/reboot.h>
81 #include <sys/syscallargs.h>
83 #include <sys/vnode.h>
84 #include <sys/socketvar.h>
85 #include <sys/resource.h>
86 #include <sys/resourcevar.h>
88 #include <sys/vfs_syscalls.h>
89 #include <sys/dirent.h>
91 #include <compat/osf1/osf1.h>
92 #include <compat/osf1/osf1_syscallargs.h>
93 #include <compat/common/compat_util.h>
94 #include <compat/osf1/osf1_cvt.h>
95 #include <compat/osf1/osf1_dirent.h>
98 osf1_sys_access(struct lwp
*l
, const struct osf1_sys_access_args
*uap
, register_t
*retval
)
100 struct sys_access_args a
;
101 unsigned long leftovers
;
103 SCARG(&a
, path
) = SCARG(uap
, path
);
105 /* translate flags */
106 SCARG(&a
, flags
) = emul_flags_translate(osf1_access_flags_xtab
,
107 SCARG(uap
, flags
), &leftovers
);
111 return sys_access(l
, &a
, retval
);
115 osf1_sys_execve(struct lwp
*l
, const struct osf1_sys_execve_args
*uap
, register_t
*retval
)
117 struct sys_execve_args ap
;
119 SCARG(&ap
, path
) = SCARG(uap
, path
);
120 SCARG(&ap
, argp
) = SCARG(uap
, argp
);
121 SCARG(&ap
, envp
) = SCARG(uap
, envp
);
123 return sys_execve(l
, &ap
, retval
);
127 osf1_sys_getdirentries(struct lwp
*l
, const struct osf1_sys_getdirentries_args
*uap
, register_t
*retval
)
131 syscallarg(char *) buf;
132 syscallarg(u_int) nbytes;
133 syscallarg(long *) basep;
137 char *inp
, *buf
; /* BSD-format */
138 int len
, reclen
; /* BSD-format */
139 char *outp
; /* OSF1-format */
140 int resid
, osf1_reclen
; /* OSF1-format */
144 struct osf1_dirent idb
;
145 off_t off
, off1
; /* true file offset */
146 int buflen
, error
, eofflag
;
147 off_t
*cookiebuf
= NULL
, *cookie
;
151 if ((error
= fd_getvnode(fd
, &fp
)) != 0)
153 if ((fp
->f_flag
& FREAD
) == 0) {
158 vp
= (struct vnode
*)fp
->f_data
;
159 if (vp
->v_type
!= VDIR
) {
164 buflen
= min(MAXBSIZE
, SCARG(uap
, nbytes
));
165 buf
= malloc(buflen
, M_TEMP
, M_WAITOK
);
166 vn_lock(vp
, LK_EXCLUSIVE
| LK_RETRY
);
167 off
= off1
= fp
->f_offset
;
170 aiov
.iov_len
= buflen
;
171 auio
.uio_iov
= &aiov
;
173 auio
.uio_rw
= UIO_READ
;
174 auio
.uio_resid
= buflen
;
175 auio
.uio_offset
= off
;
176 UIO_SETUP_SYSSPACE(&auio
);
178 * First we read into the malloc'ed buffer, then
179 * we massage it into user space, one record at a time.
181 error
= VOP_READDIR(vp
, &auio
, fp
->f_cred
, &eofflag
, &cookiebuf
,
187 outp
= (char *)SCARG(uap
, buf
);
188 resid
= SCARG(uap
, nbytes
);
189 if ((len
= buflen
- auio
.uio_resid
) == 0)
192 for (cookie
= cookiebuf
; len
> 0; len
-= reclen
) {
193 bdp
= (struct dirent
*)inp
;
194 reclen
= bdp
->d_reclen
;
196 panic("osf1_sys_getdirentries: bad reclen");
198 off
= *cookie
++; /* each entry points to the next */
201 if ((off
>> 32) != 0) {
202 compat_offseterr(vp
, "osf1_sys_getdirentries");
206 if (bdp
->d_fileno
== 0) {
207 inp
+= reclen
; /* it is a hole; squish it out */
210 osf1_reclen
= OSF1_RECLEN(&idb
, bdp
->d_namlen
);
211 if (reclen
> len
|| resid
< osf1_reclen
) {
212 /* entry too big for buffer, so just stop */
217 * Massage in place to make a OSF1-shaped dirent (otherwise
218 * we have to worry about touching user memory outside of
219 * the copyout() call).
221 idb
.d_ino
= (osf1_ino_t
)bdp
->d_fileno
;
222 idb
.d_reclen
= (u_short
)osf1_reclen
;
223 idb
.d_namlen
= (u_short
)bdp
->d_namlen
;
224 strlcpy(idb
.d_name
, bdp
->d_name
, sizeof(idb
.d_name
));
225 if ((error
= copyout((void *)&idb
, outp
, osf1_reclen
)))
227 /* advance past this real entry */
229 /* advance output past OSF1-shaped entry */
231 resid
-= osf1_reclen
;
234 /* if we squished out the whole block, try again */
235 if (outp
== (char *)SCARG(uap
, buf
))
237 fp
->f_offset
= off
; /* update the vnode offset */
240 *retval
= SCARG(uap
, nbytes
) - resid
;
244 free(cookiebuf
, M_TEMP
);
246 if (SCARG(uap
, basep
) != NULL
)
247 error
= copyout(&off1
, SCARG(uap
, basep
), sizeof(long));
254 * Get file status; this version does not follow links.
258 osf1_sys_lstat(struct lwp
*l
, const struct osf1_sys_lstat_args
*uap
, register_t
*retval
)
261 struct osf1_stat osb
;
264 error
= do_sys_stat(SCARG(uap
, path
), NOFOLLOW
, &sb
);
267 osf1_cvt_stat_from_native(&sb
, &osb
);
268 error
= copyout(&osb
, SCARG(uap
, ub
), sizeof (osb
));
273 * Get file status; this version does not follow links.
277 osf1_sys_lstat2(struct lwp
*l
, const struct osf1_sys_lstat2_args
*uap
, register_t
*retval
)
280 struct osf1_stat2 osb
;
283 error
= do_sys_stat(SCARG(uap
, path
), NOFOLLOW
, &sb
);
286 osf1_cvt_stat2_from_native(&sb
, &osb
);
287 error
= copyout((void *)&osb
, (void *)SCARG(uap
, ub
), sizeof (osb
));
292 osf1_sys_mknod(struct lwp
*l
, const struct osf1_sys_mknod_args
*uap
, register_t
*retval
)
295 return do_sys_mknod(l
, SCARG(uap
, path
), SCARG(uap
, mode
),
296 osf1_cvt_dev_to_native(SCARG(uap
, dev
)), retval
, UIO_USERSPACE
);
300 osf1_sys_open(struct lwp
*l
, const struct osf1_sys_open_args
*uap
, register_t
*retval
)
302 struct sys_open_args a
;
304 unsigned long leftovers
;
309 copyinstr(SCARG(uap
, path
), pnbuf
, sizeof pnbuf
, NULL
) == 0)
310 printf("osf1_open: open: %s\n", pnbuf
);
313 /* translate flags */
314 SCARG(&a
, flags
) = emul_flags_translate(osf1_open_flags_xtab
,
315 SCARG(uap
, flags
), &leftovers
);
319 /* copy mode, no translation necessary */
320 SCARG(&a
, mode
) = SCARG(uap
, mode
);
322 /* pick appropriate path */
323 path
= SCARG(uap
, path
);
324 SCARG(&a
, path
) = path
;
326 return sys_open(l
, &a
, retval
);
330 osf1_sys_pathconf(struct lwp
*l
, const struct osf1_sys_pathconf_args
*uap
, register_t
*retval
)
332 struct sys_pathconf_args a
;
335 SCARG(&a
, path
) = SCARG(uap
, path
);
337 error
= osf1_cvt_pathconf_name_to_native(SCARG(uap
, name
),
341 error
= sys_pathconf(l
, &a
, retval
);
347 * Get file status; this version follows links.
351 osf1_sys_stat(struct lwp
*l
, const struct osf1_sys_stat_args
*uap
, register_t
*retval
)
354 struct osf1_stat osb
;
357 error
= do_sys_stat(SCARG(uap
, path
), FOLLOW
, &sb
);
360 osf1_cvt_stat_from_native(&sb
, &osb
);
361 error
= copyout((void *)&osb
, (void *)SCARG(uap
, ub
), sizeof (osb
));
366 * Get file status; this version follows links.
370 osf1_sys_stat2(struct lwp
*l
, const struct osf1_sys_stat2_args
*uap
, register_t
*retval
)
373 struct osf1_stat2 osb
;
376 error
= do_sys_stat(SCARG(uap
, path
), FOLLOW
, &sb
);
379 osf1_cvt_stat2_from_native(&sb
, &osb
);
380 error
= copyout((void *)&osb
, (void *)SCARG(uap
, ub
), sizeof (osb
));
385 osf1_sys_truncate(struct lwp
*l
, const struct osf1_sys_truncate_args
*uap
, register_t
*retval
)
387 struct sys_truncate_args a
;
389 SCARG(&a
, path
) = SCARG(uap
, path
);
391 SCARG(&a
, length
) = SCARG(uap
, length
);
393 return sys_truncate(l
, &a
, retval
);
397 osf1_sys_utimes(struct lwp
*l
, const struct osf1_sys_utimes_args
*uap
, register_t
*retval
)
399 struct osf1_timeval otv
;
400 struct timeval tv
[2], *tvp
;
403 if (SCARG(uap
, tptr
) == NULL
)
406 /* get the OSF/1 timeval argument */
407 error
= copyin(SCARG(uap
, tptr
), &otv
, sizeof otv
);
411 /* fill in and copy out the NetBSD timeval */
412 tv
[0].tv_sec
= otv
.tv_sec
;
413 tv
[0].tv_usec
= otv
.tv_usec
;
414 /* Set access and modified to the same time */
415 tv
[1].tv_sec
= otv
.tv_sec
;
416 tv
[1].tv_usec
= otv
.tv_usec
;
420 return do_sys_utimes(l
, NULL
, SCARG(uap
, path
), FOLLOW
,