vfs_syscalls_12.c revision 1.2 1 /* $NetBSD: vfs_syscalls_12.c,v 1.2 1997/10/16 23:50:36 christos Exp $ */
2
3 /*
4 * Copyright (c) 1989, 1993
5 * The Regents of the University of California. All rights reserved.
6 * (c) UNIX System Laboratories, Inc.
7 * All or some portions of this file are derived from material licensed
8 * to the University of California by American Telephone and Telegraph
9 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
10 * the permission of UNIX System Laboratories, Inc.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. All advertising materials mentioning features or use of this software
21 * must display the following acknowledgement:
22 * This product includes software developed by the University of
23 * California, Berkeley and its contributors.
24 * 4. Neither the name of the University nor the names of its contributors
25 * may be used to endorse or promote products derived from this software
26 * without specific prior written permission.
27 *
28 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 * SUCH DAMAGE.
39 *
40 * From: @(#)vfs_syscalls.c 8.28 (Berkeley) 12/10/94
41 */
42
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/namei.h>
46 #include <sys/filedesc.h>
47 #include <sys/kernel.h>
48 #include <sys/file.h>
49 #include <sys/stat.h>
50 #include <sys/socketvar.h>
51 #include <sys/vnode.h>
52 #include <sys/mount.h>
53 #include <sys/proc.h>
54 #include <sys/uio.h>
55 #include <sys/malloc.h>
56 #include <sys/dirent.h>
57
58 #include <sys/syscallargs.h>
59
60 static void cvtstat __P((struct stat *, struct stat12 *));
61
62 /*
63 * Convert from an old to a new stat structure.
64 */
65 static void
66 cvtstat(st, ost)
67 struct stat *st;
68 struct stat12 *ost;
69 {
70
71 ost->st_dev = st->st_dev;
72 ost->st_ino = st->st_ino;
73 ost->st_mode = st->st_mode;
74 ost->st_nlink = st->st_nlink;
75 if (st->st_nlink >= (1 << 15))
76 ost->st_nlink = (1 << 15) - 1;
77 else
78 ost->st_nlink = st->st_nlink;
79 ost->st_uid = st->st_uid;
80 ost->st_gid = st->st_gid;
81 ost->st_rdev = st->st_rdev;
82 ost->st_size = st->st_size;
83 ost->st_atime = st->st_atime;
84 ost->st_mtime = st->st_mtime;
85 ost->st_ctime = st->st_ctime;
86 ost->st_blksize = st->st_blksize;
87 ost->st_blocks = st->st_blocks;
88 ost->st_flags = st->st_flags;
89 ost->st_gen = st->st_gen;
90 }
91 /*
92 * Read a block of directory entries in a file system independent format.
93 */
94 int
95 compat_12_sys_getdirentries(p, v, retval)
96 struct proc *p;
97 void *v;
98 register_t *retval;
99 {
100 register struct compat_12_sys_getdirentries_args /* {
101 syscallarg(int) fd;
102 syscallarg(char *) buf;
103 syscallarg(u_int) count;
104 syscallarg(long *) basep;
105 } */ *uap = v;
106 struct file *fp;
107 int error, done;
108 long loff;
109
110 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
111 return error;
112 if ((fp->f_flag & FREAD) == 0)
113 return (EBADF);
114
115 loff = fp->f_offset;
116
117 error = vn_readdir(fp, SCARG(uap, buf), UIO_USERSPACE,
118 SCARG(uap, count), &done, p, 0, 0);
119
120 error = copyout(&loff, SCARG(uap, basep), sizeof(long));
121 *retval = done;
122 return error;
123 }
124
125 /*
126 * Get file status; this version follows links.
127 */
128 /* ARGSUSED */
129 int
130 compat_12_sys_stat(p, v, retval)
131 struct proc *p;
132 void *v;
133 register_t *retval;
134 {
135 register struct compat_12_sys_stat_args /* {
136 syscallarg(char *) path;
137 syscallarg(struct stat12 *) ub;
138 } */ *uap = v;
139 struct stat sb;
140 struct stat12 osb;
141 int error;
142 struct nameidata nd;
143
144 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
145 SCARG(uap, path), p);
146 if ((error = namei(&nd)) != 0)
147 return (error);
148 error = vn_stat(nd.ni_vp, &sb, p);
149 vput(nd.ni_vp);
150 if (error)
151 return (error);
152 cvtstat(&sb, &osb);
153 error = copyout((caddr_t)&osb, (caddr_t)SCARG(uap, ub), sizeof (osb));
154 return (error);
155 }
156
157
158 /*
159 * Get file status; this version does not follow links.
160 */
161 /* ARGSUSED */
162 int
163 compat_12_sys_lstat(p, v, retval)
164 struct proc *p;
165 void *v;
166 register_t *retval;
167 {
168 register struct compat_12_sys_lstat_args /* {
169 syscallarg(char *) path;
170 syscallarg(struct stat12 *) ub;
171 } */ *uap = v;
172 struct stat sb;
173 struct stat12 osb;
174 int error;
175 struct nameidata nd;
176
177 NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF, UIO_USERSPACE,
178 SCARG(uap, path), p);
179 if ((error = namei(&nd)) != 0)
180 return (error);
181 error = vn_stat(nd.ni_vp, &sb, p);
182 vput(nd.ni_vp);
183 if (error)
184 return (error);
185 cvtstat(&sb, &osb);
186 error = copyout(&osb, SCARG(uap, ub), sizeof (osb));
187 return (error);
188 }
189
190
191 /*
192 * Return status information about a file descriptor.
193 */
194 /* ARGSUSED */
195 int
196 compat_12_sys_fstat(p, v, retval)
197 struct proc *p;
198 void *v;
199 register_t *retval;
200 {
201 register struct compat_12_sys_fstat_args /* {
202 syscallarg(int) fd;
203 syscallarg(struct stat12 *) sb;
204 } */ *uap = v;
205 int fd = SCARG(uap, fd);
206 register struct filedesc *fdp = p->p_fd;
207 register struct file *fp;
208 struct stat ub;
209 struct stat12 oub;
210 int error;
211
212 if ((u_int)fd >= fdp->fd_nfiles ||
213 (fp = fdp->fd_ofiles[fd]) == NULL)
214 return (EBADF);
215 switch (fp->f_type) {
216
217 case DTYPE_VNODE:
218 error = vn_stat((struct vnode *)fp->f_data, &ub, p);
219 break;
220
221 case DTYPE_SOCKET:
222 error = soo_stat((struct socket *)fp->f_data, &ub);
223 break;
224
225 default:
226 panic("compat_12_sys_fstat");
227 /*NOTREACHED*/
228 }
229 cvtstat(&ub, &oub);
230 if (error == 0)
231 error = copyout((caddr_t)&oub, (caddr_t)SCARG(uap, sb),
232 sizeof (oub));
233 return (error);
234 }
235