linux_file.c revision 1.86 1 1.86 pooka /* $NetBSD: linux_file.c,v 1.86 2007/11/26 19:01:31 pooka Exp $ */
2 1.23 erh
3 1.23 erh /*-
4 1.25 fvdl * Copyright (c) 1995, 1998 The NetBSD Foundation, Inc.
5 1.23 erh * All rights reserved.
6 1.23 erh *
7 1.23 erh * This code is derived from software contributed to The NetBSD Foundation
8 1.25 fvdl * by Frank van der Linden and Eric Haszlakiewicz.
9 1.23 erh *
10 1.23 erh * Redistribution and use in source and binary forms, with or without
11 1.23 erh * modification, are permitted provided that the following conditions
12 1.23 erh * are met:
13 1.23 erh * 1. Redistributions of source code must retain the above copyright
14 1.23 erh * notice, this list of conditions and the following disclaimer.
15 1.23 erh * 2. Redistributions in binary form must reproduce the above copyright
16 1.23 erh * notice, this list of conditions and the following disclaimer in the
17 1.23 erh * documentation and/or other materials provided with the distribution.
18 1.23 erh * 3. All advertising materials mentioning features or use of this software
19 1.23 erh * must display the following acknowledgement:
20 1.23 erh * This product includes software developed by the NetBSD
21 1.23 erh * Foundation, Inc. and its contributors.
22 1.23 erh * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.23 erh * contributors may be used to endorse or promote products derived
24 1.23 erh * from this software without specific prior written permission.
25 1.23 erh *
26 1.23 erh * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.23 erh * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.23 erh * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.23 erh * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.23 erh * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.23 erh * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.23 erh * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.23 erh * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.23 erh * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.23 erh * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.23 erh * POSSIBILITY OF SUCH DAMAGE.
37 1.1 fvdl */
38 1.1 fvdl
39 1.23 erh /*
40 1.23 erh * Functions in multiarch:
41 1.23 erh * linux_sys_llseek : linux_llseek.c
42 1.23 erh */
43 1.43 lukem
44 1.43 lukem #include <sys/cdefs.h>
45 1.86 pooka __KERNEL_RCSID(0, "$NetBSD: linux_file.c,v 1.86 2007/11/26 19:01:31 pooka Exp $");
46 1.23 erh
47 1.1 fvdl #include <sys/param.h>
48 1.1 fvdl #include <sys/systm.h>
49 1.1 fvdl #include <sys/namei.h>
50 1.1 fvdl #include <sys/proc.h>
51 1.1 fvdl #include <sys/file.h>
52 1.1 fvdl #include <sys/stat.h>
53 1.1 fvdl #include <sys/filedesc.h>
54 1.1 fvdl #include <sys/ioctl.h>
55 1.1 fvdl #include <sys/kernel.h>
56 1.1 fvdl #include <sys/mount.h>
57 1.1 fvdl #include <sys/malloc.h>
58 1.81 dsl #include <sys/namei.h>
59 1.13 fvdl #include <sys/vnode.h>
60 1.13 fvdl #include <sys/tty.h>
61 1.39 manu #include <sys/socketvar.h>
62 1.13 fvdl #include <sys/conf.h>
63 1.41 jdolecek #include <sys/pipe.h>
64 1.1 fvdl
65 1.1 fvdl #include <sys/syscallargs.h>
66 1.81 dsl #include <sys/vfs_syscalls.h>
67 1.1 fvdl
68 1.24 christos #include <compat/linux/common/linux_types.h>
69 1.24 christos #include <compat/linux/common/linux_signal.h>
70 1.24 christos #include <compat/linux/common/linux_fcntl.h>
71 1.24 christos #include <compat/linux/common/linux_util.h>
72 1.24 christos #include <compat/linux/common/linux_machdep.h>
73 1.84 njoly #include <compat/linux/common/linux_ipc.h>
74 1.84 njoly #include <compat/linux/common/linux_sem.h>
75 1.24 christos
76 1.1 fvdl #include <compat/linux/linux_syscallargs.h>
77 1.14 christos
78 1.14 christos static int linux_to_bsd_ioflags __P((int));
79 1.14 christos static int bsd_to_linux_ioflags __P((int));
80 1.14 christos static void bsd_to_linux_flock __P((struct flock *, struct linux_flock *));
81 1.14 christos static void linux_to_bsd_flock __P((struct linux_flock *, struct flock *));
82 1.67 manu #ifndef __amd64__
83 1.14 christos static void bsd_to_linux_stat __P((struct stat *, struct linux_stat *));
84 1.56 thorpej static int linux_stat1 __P((struct lwp *, void *, register_t *, int));
85 1.67 manu #endif
86 1.14 christos
87 1.1 fvdl /*
88 1.1 fvdl * Some file-related calls are handled here. The usual flag conversion
89 1.1 fvdl * an structure conversion is done, and alternate emul path searching.
90 1.1 fvdl */
91 1.1 fvdl
92 1.1 fvdl /*
93 1.1 fvdl * The next two functions convert between the Linux and NetBSD values
94 1.1 fvdl * of the flags used in open(2) and fcntl(2).
95 1.1 fvdl */
96 1.1 fvdl static int
97 1.14 christos linux_to_bsd_ioflags(lflags)
98 1.14 christos int lflags;
99 1.1 fvdl {
100 1.1 fvdl int res = 0;
101 1.1 fvdl
102 1.1 fvdl res |= cvtto_bsd_mask(lflags, LINUX_O_WRONLY, O_WRONLY);
103 1.1 fvdl res |= cvtto_bsd_mask(lflags, LINUX_O_RDONLY, O_RDONLY);
104 1.1 fvdl res |= cvtto_bsd_mask(lflags, LINUX_O_RDWR, O_RDWR);
105 1.1 fvdl res |= cvtto_bsd_mask(lflags, LINUX_O_CREAT, O_CREAT);
106 1.1 fvdl res |= cvtto_bsd_mask(lflags, LINUX_O_EXCL, O_EXCL);
107 1.1 fvdl res |= cvtto_bsd_mask(lflags, LINUX_O_NOCTTY, O_NOCTTY);
108 1.1 fvdl res |= cvtto_bsd_mask(lflags, LINUX_O_TRUNC, O_TRUNC);
109 1.1 fvdl res |= cvtto_bsd_mask(lflags, LINUX_O_NDELAY, O_NDELAY);
110 1.1 fvdl res |= cvtto_bsd_mask(lflags, LINUX_O_SYNC, O_FSYNC);
111 1.1 fvdl res |= cvtto_bsd_mask(lflags, LINUX_FASYNC, O_ASYNC);
112 1.1 fvdl res |= cvtto_bsd_mask(lflags, LINUX_O_APPEND, O_APPEND);
113 1.1 fvdl
114 1.1 fvdl return res;
115 1.1 fvdl }
116 1.1 fvdl
117 1.1 fvdl static int
118 1.14 christos bsd_to_linux_ioflags(bflags)
119 1.14 christos int bflags;
120 1.1 fvdl {
121 1.1 fvdl int res = 0;
122 1.1 fvdl
123 1.1 fvdl res |= cvtto_linux_mask(bflags, O_WRONLY, LINUX_O_WRONLY);
124 1.1 fvdl res |= cvtto_linux_mask(bflags, O_RDONLY, LINUX_O_RDONLY);
125 1.1 fvdl res |= cvtto_linux_mask(bflags, O_RDWR, LINUX_O_RDWR);
126 1.1 fvdl res |= cvtto_linux_mask(bflags, O_CREAT, LINUX_O_CREAT);
127 1.1 fvdl res |= cvtto_linux_mask(bflags, O_EXCL, LINUX_O_EXCL);
128 1.1 fvdl res |= cvtto_linux_mask(bflags, O_NOCTTY, LINUX_O_NOCTTY);
129 1.1 fvdl res |= cvtto_linux_mask(bflags, O_TRUNC, LINUX_O_TRUNC);
130 1.1 fvdl res |= cvtto_linux_mask(bflags, O_NDELAY, LINUX_O_NDELAY);
131 1.1 fvdl res |= cvtto_linux_mask(bflags, O_FSYNC, LINUX_O_SYNC);
132 1.1 fvdl res |= cvtto_linux_mask(bflags, O_ASYNC, LINUX_FASYNC);
133 1.1 fvdl res |= cvtto_linux_mask(bflags, O_APPEND, LINUX_O_APPEND);
134 1.1 fvdl
135 1.1 fvdl return res;
136 1.1 fvdl }
137 1.1 fvdl
138 1.1 fvdl /*
139 1.1 fvdl * creat(2) is an obsolete function, but it's present as a Linux
140 1.1 fvdl * system call, so let's deal with it.
141 1.1 fvdl *
142 1.23 erh * Note: On the Alpha this doesn't really exist in Linux, but it's defined
143 1.23 erh * in syscalls.master anyway so this doesn't have to be special cased.
144 1.23 erh *
145 1.1 fvdl * Just call open(2) with the TRUNC, CREAT and WRONLY flags.
146 1.1 fvdl */
147 1.1 fvdl int
148 1.56 thorpej linux_sys_creat(l, v, retval)
149 1.56 thorpej struct lwp *l;
150 1.11 thorpej void *v;
151 1.11 thorpej register_t *retval;
152 1.11 thorpej {
153 1.12 mycroft struct linux_sys_creat_args /* {
154 1.27 christos syscallarg(const char *) path;
155 1.1 fvdl syscallarg(int) mode;
156 1.11 thorpej } */ *uap = v;
157 1.12 mycroft struct sys_open_args oa;
158 1.1 fvdl
159 1.1 fvdl SCARG(&oa, path) = SCARG(uap, path);
160 1.1 fvdl SCARG(&oa, flags) = O_CREAT | O_TRUNC | O_WRONLY;
161 1.1 fvdl SCARG(&oa, mode) = SCARG(uap, mode);
162 1.12 mycroft
163 1.56 thorpej return sys_open(l, &oa, retval);
164 1.1 fvdl }
165 1.1 fvdl
166 1.1 fvdl /*
167 1.1 fvdl * open(2). Take care of the different flag values, and let the
168 1.1 fvdl * NetBSD syscall do the real work. See if this operation
169 1.1 fvdl * gives the current process a controlling terminal.
170 1.1 fvdl * (XXX is this necessary?)
171 1.1 fvdl */
172 1.1 fvdl int
173 1.56 thorpej linux_sys_open(l, v, retval)
174 1.56 thorpej struct lwp *l;
175 1.11 thorpej void *v;
176 1.11 thorpej register_t *retval;
177 1.11 thorpej {
178 1.12 mycroft struct linux_sys_open_args /* {
179 1.27 christos syscallarg(const char *) path;
180 1.1 fvdl syscallarg(int) flags;
181 1.1 fvdl syscallarg(int) mode;
182 1.11 thorpej } */ *uap = v;
183 1.56 thorpej struct proc *p = l->l_proc;
184 1.1 fvdl int error, fl;
185 1.12 mycroft struct sys_open_args boa;
186 1.1 fvdl
187 1.2 fvdl fl = linux_to_bsd_ioflags(SCARG(uap, flags));
188 1.1 fvdl
189 1.1 fvdl SCARG(&boa, path) = SCARG(uap, path);
190 1.1 fvdl SCARG(&boa, flags) = fl;
191 1.1 fvdl SCARG(&boa, mode) = SCARG(uap, mode);
192 1.2 fvdl
193 1.56 thorpej if ((error = sys_open(l, &boa, retval)))
194 1.1 fvdl return error;
195 1.1 fvdl
196 1.1 fvdl /*
197 1.1 fvdl * this bit from sunos_misc.c (and svr4_fcntl.c).
198 1.1 fvdl * If we are a session leader, and we don't have a controlling
199 1.1 fvdl * terminal yet, and the O_NOCTTY flag is not set, try to make
200 1.1 fvdl * this the controlling terminal.
201 1.65 perry */
202 1.77 ad if (!(fl & O_NOCTTY) && SESS_LEADER(p) && !(p->p_lflag & PL_CONTROLT)) {
203 1.1 fvdl struct filedesc *fdp = p->p_fd;
204 1.38 thorpej struct file *fp;
205 1.38 thorpej
206 1.38 thorpej fp = fd_getfile(fdp, *retval);
207 1.1 fvdl
208 1.1 fvdl /* ignore any error, just give it a try */
209 1.57 yamt if (fp != NULL) {
210 1.57 yamt FILE_USE(fp);
211 1.57 yamt if (fp->f_type == DTYPE_VNODE) {
212 1.57 yamt (fp->f_ops->fo_ioctl) (fp, TIOCSCTTY,
213 1.79 christos (void *) 0, l);
214 1.57 yamt }
215 1.71 christos FILE_UNUSE(fp, l);
216 1.57 yamt }
217 1.1 fvdl }
218 1.1 fvdl return 0;
219 1.1 fvdl }
220 1.1 fvdl
221 1.1 fvdl /*
222 1.1 fvdl * The next two functions take care of converting the flock
223 1.1 fvdl * structure back and forth between Linux and NetBSD format.
224 1.1 fvdl * The only difference in the structures is the order of
225 1.1 fvdl * the fields, and the 'whence' value.
226 1.1 fvdl */
227 1.1 fvdl static void
228 1.1 fvdl bsd_to_linux_flock(bfp, lfp)
229 1.1 fvdl struct flock *bfp;
230 1.1 fvdl struct linux_flock *lfp;
231 1.1 fvdl {
232 1.12 mycroft
233 1.1 fvdl lfp->l_start = bfp->l_start;
234 1.1 fvdl lfp->l_len = bfp->l_len;
235 1.1 fvdl lfp->l_pid = bfp->l_pid;
236 1.3 mycroft lfp->l_whence = bfp->l_whence;
237 1.3 mycroft switch (bfp->l_type) {
238 1.1 fvdl case F_RDLCK:
239 1.3 mycroft lfp->l_type = LINUX_F_RDLCK;
240 1.1 fvdl break;
241 1.1 fvdl case F_UNLCK:
242 1.3 mycroft lfp->l_type = LINUX_F_UNLCK;
243 1.1 fvdl break;
244 1.1 fvdl case F_WRLCK:
245 1.3 mycroft lfp->l_type = LINUX_F_WRLCK;
246 1.1 fvdl break;
247 1.1 fvdl }
248 1.1 fvdl }
249 1.1 fvdl
250 1.1 fvdl static void
251 1.1 fvdl linux_to_bsd_flock(lfp, bfp)
252 1.1 fvdl struct linux_flock *lfp;
253 1.1 fvdl struct flock *bfp;
254 1.1 fvdl {
255 1.12 mycroft
256 1.1 fvdl bfp->l_start = lfp->l_start;
257 1.1 fvdl bfp->l_len = lfp->l_len;
258 1.1 fvdl bfp->l_pid = lfp->l_pid;
259 1.3 mycroft bfp->l_whence = lfp->l_whence;
260 1.3 mycroft switch (lfp->l_type) {
261 1.1 fvdl case LINUX_F_RDLCK:
262 1.3 mycroft bfp->l_type = F_RDLCK;
263 1.1 fvdl break;
264 1.1 fvdl case LINUX_F_UNLCK:
265 1.3 mycroft bfp->l_type = F_UNLCK;
266 1.1 fvdl break;
267 1.1 fvdl case LINUX_F_WRLCK:
268 1.3 mycroft bfp->l_type = F_WRLCK;
269 1.1 fvdl break;
270 1.1 fvdl }
271 1.1 fvdl }
272 1.1 fvdl
273 1.1 fvdl /*
274 1.1 fvdl * Most actions in the fcntl() call are straightforward; simply
275 1.1 fvdl * pass control to the NetBSD system call. A few commands need
276 1.1 fvdl * conversions after the actual system call has done its work,
277 1.1 fvdl * because the flag values and lock structure are different.
278 1.1 fvdl */
279 1.1 fvdl int
280 1.56 thorpej linux_sys_fcntl(l, v, retval)
281 1.56 thorpej struct lwp *l;
282 1.11 thorpej void *v;
283 1.11 thorpej register_t *retval;
284 1.11 thorpej {
285 1.12 mycroft struct linux_sys_fcntl_args /* {
286 1.1 fvdl syscallarg(int) fd;
287 1.1 fvdl syscallarg(int) cmd;
288 1.1 fvdl syscallarg(void *) arg;
289 1.11 thorpej } */ *uap = v;
290 1.56 thorpej struct proc *p = l->l_proc;
291 1.23 erh int fd, cmd, error;
292 1.23 erh u_long val;
293 1.83 dsl void *arg;
294 1.1 fvdl struct linux_flock lfl;
295 1.83 dsl struct flock bfl;
296 1.12 mycroft struct sys_fcntl_args fca;
297 1.13 fvdl struct filedesc *fdp;
298 1.13 fvdl struct file *fp;
299 1.13 fvdl struct vnode *vp;
300 1.13 fvdl struct vattr va;
301 1.53 gehenna const struct cdevsw *cdev;
302 1.13 fvdl long pgid;
303 1.13 fvdl struct pgrp *pgrp;
304 1.13 fvdl struct tty *tp, *(*d_tty) __P((dev_t));
305 1.1 fvdl
306 1.1 fvdl fd = SCARG(uap, fd);
307 1.1 fvdl cmd = SCARG(uap, cmd);
308 1.79 christos arg = (void *) SCARG(uap, arg);
309 1.1 fvdl
310 1.1 fvdl switch (cmd) {
311 1.1 fvdl case LINUX_F_DUPFD:
312 1.1 fvdl cmd = F_DUPFD;
313 1.1 fvdl break;
314 1.1 fvdl case LINUX_F_GETFD:
315 1.1 fvdl cmd = F_GETFD;
316 1.1 fvdl break;
317 1.1 fvdl case LINUX_F_SETFD:
318 1.1 fvdl cmd = F_SETFD;
319 1.1 fvdl break;
320 1.1 fvdl case LINUX_F_GETFL:
321 1.1 fvdl SCARG(&fca, fd) = fd;
322 1.1 fvdl SCARG(&fca, cmd) = F_GETFL;
323 1.1 fvdl SCARG(&fca, arg) = arg;
324 1.56 thorpej if ((error = sys_fcntl(l, &fca, retval)))
325 1.1 fvdl return error;
326 1.1 fvdl retval[0] = bsd_to_linux_ioflags(retval[0]);
327 1.1 fvdl return 0;
328 1.41 jdolecek case LINUX_F_SETFL: {
329 1.69 christos struct file *fp1 = NULL;
330 1.41 jdolecek
331 1.23 erh val = linux_to_bsd_ioflags((unsigned long)SCARG(uap, arg));
332 1.39 manu /*
333 1.41 jdolecek * Linux seems to have same semantics for sending SIGIO to the
334 1.64 abs * read side of socket, but slightly different semantics
335 1.41 jdolecek * for SIGIO to the write side. Rather than sending the SIGIO
336 1.41 jdolecek * every time it's possible to write (directly) more data, it
337 1.41 jdolecek * only sends SIGIO if last write(2) failed due to insufficient
338 1.41 jdolecek * memory to hold the data. This is compatible enough
339 1.41 jdolecek * with NetBSD semantics to not do anything about the
340 1.41 jdolecek * difference.
341 1.65 perry *
342 1.41 jdolecek * Linux does NOT send SIGIO for pipes. Deal with socketpair
343 1.41 jdolecek * ones and DTYPE_PIPE ones. For these, we don't set
344 1.41 jdolecek * the underlying flags (we don't pass O_ASYNC flag down
345 1.41 jdolecek * to sys_fcntl()), but set the FASYNC flag for file descriptor,
346 1.41 jdolecek * so that F_GETFL would report the ASYNC i/o is on.
347 1.39 manu */
348 1.41 jdolecek if (val & O_ASYNC) {
349 1.69 christos if (((fp1 = fd_getfile(p->p_fd, fd)) == NULL))
350 1.39 manu return (EBADF);
351 1.39 manu
352 1.69 christos FILE_USE(fp1);
353 1.39 manu
354 1.69 christos if (((fp1->f_type == DTYPE_SOCKET) && fp1->f_data
355 1.69 christos && ((struct socket *)fp1->f_data)->so_state & SS_ISAPIPE)
356 1.69 christos || (fp1->f_type == DTYPE_PIPE))
357 1.41 jdolecek val &= ~O_ASYNC;
358 1.41 jdolecek else {
359 1.41 jdolecek /* not a pipe, do not modify anything */
360 1.71 christos FILE_UNUSE(fp1, l);
361 1.69 christos fp1 = NULL;
362 1.39 manu }
363 1.41 jdolecek }
364 1.41 jdolecek
365 1.41 jdolecek SCARG(&fca, fd) = fd;
366 1.41 jdolecek SCARG(&fca, cmd) = F_SETFL;
367 1.79 christos SCARG(&fca, arg) = (void *) val;
368 1.39 manu
369 1.56 thorpej error = sys_fcntl(l, &fca, retval);
370 1.41 jdolecek
371 1.41 jdolecek /* Now set the FASYNC flag for pipes */
372 1.69 christos if (fp1) {
373 1.41 jdolecek if (!error)
374 1.69 christos fp1->f_flag |= FASYNC;
375 1.71 christos FILE_UNUSE(fp1, l);
376 1.39 manu }
377 1.41 jdolecek
378 1.41 jdolecek return (error);
379 1.41 jdolecek }
380 1.1 fvdl case LINUX_F_GETLK:
381 1.18 kleink if ((error = copyin(arg, &lfl, sizeof lfl)))
382 1.18 kleink return error;
383 1.18 kleink linux_to_bsd_flock(&lfl, &bfl);
384 1.83 dsl error = do_fcntl_lock(l, fd, F_GETLK, &bfl);
385 1.83 dsl if (error)
386 1.1 fvdl return error;
387 1.1 fvdl bsd_to_linux_flock(&bfl, &lfl);
388 1.1 fvdl return copyout(&lfl, arg, sizeof lfl);
389 1.47 christos
390 1.1 fvdl case LINUX_F_SETLK:
391 1.1 fvdl case LINUX_F_SETLKW:
392 1.1 fvdl cmd = (cmd == LINUX_F_SETLK ? F_SETLK : F_SETLKW);
393 1.1 fvdl if ((error = copyin(arg, &lfl, sizeof lfl)))
394 1.1 fvdl return error;
395 1.1 fvdl linux_to_bsd_flock(&lfl, &bfl);
396 1.83 dsl return do_fcntl_lock(l, fd, cmd, &bfl);
397 1.47 christos
398 1.1 fvdl case LINUX_F_SETOWN:
399 1.65 perry case LINUX_F_GETOWN:
400 1.13 fvdl /*
401 1.49 jdolecek * We need to route fcntl() for tty descriptors around normal
402 1.49 jdolecek * fcntl(), since NetBSD tty TIOC{G,S}PGRP semantics is too
403 1.49 jdolecek * restrictive for Linux F_{G,S}ETOWN. For non-tty descriptors,
404 1.49 jdolecek * this is not a problem.
405 1.13 fvdl */
406 1.13 fvdl fdp = p->p_fd;
407 1.38 thorpej if ((fp = fd_getfile(fdp, fd)) == NULL)
408 1.13 fvdl return EBADF;
409 1.60 jdolecek FILE_USE(fp);
410 1.60 jdolecek
411 1.60 jdolecek /* Check it's a character device vnode */
412 1.60 jdolecek if (fp->f_type != DTYPE_VNODE
413 1.60 jdolecek || (vp = (struct vnode *)fp->f_data) == NULL
414 1.60 jdolecek || vp->v_type != VCHR) {
415 1.71 christos FILE_UNUSE(fp, l);
416 1.60 jdolecek
417 1.53 gehenna not_tty:
418 1.49 jdolecek /* Not a tty, proceed with common fcntl() */
419 1.13 fvdl cmd = cmd == LINUX_F_SETOWN ? F_SETOWN : F_GETOWN;
420 1.49 jdolecek break;
421 1.49 jdolecek }
422 1.47 christos
423 1.86 pooka error = VOP_GETATTR(vp, &va, l->l_cred);
424 1.60 jdolecek
425 1.71 christos FILE_UNUSE(fp, l);
426 1.60 jdolecek
427 1.60 jdolecek if (error)
428 1.49 jdolecek return error;
429 1.60 jdolecek
430 1.53 gehenna cdev = cdevsw_lookup(va.va_rdev);
431 1.53 gehenna if (cdev == NULL)
432 1.53 gehenna return (ENXIO);
433 1.53 gehenna d_tty = cdev->d_tty;
434 1.49 jdolecek if (!d_tty || (!(tp = (*d_tty)(va.va_rdev))))
435 1.53 gehenna goto not_tty;
436 1.49 jdolecek
437 1.49 jdolecek /* set tty pg_id appropriately */
438 1.49 jdolecek if (cmd == LINUX_F_GETOWN) {
439 1.58 dsl retval[0] = tp->t_pgrp ? tp->t_pgrp->pg_id : NO_PGID;
440 1.49 jdolecek return 0;
441 1.49 jdolecek }
442 1.80 ad mutex_enter(&proclist_lock);
443 1.49 jdolecek if ((long)arg <= 0) {
444 1.49 jdolecek pgid = -(long)arg;
445 1.49 jdolecek } else {
446 1.78 dsl struct proc *p1 = p_find((long)arg, PFIND_LOCKED | PFIND_UNLOCK_FAIL);
447 1.49 jdolecek if (p1 == NULL)
448 1.49 jdolecek return (ESRCH);
449 1.49 jdolecek pgid = (long)p1->p_pgrp->pg_id;
450 1.13 fvdl }
451 1.78 dsl pgrp = pg_find(pgid, PFIND_LOCKED);
452 1.78 dsl if (pgrp == NULL || pgrp->pg_session != p->p_session) {
453 1.80 ad mutex_exit(&proclist_lock);
454 1.49 jdolecek return EPERM;
455 1.78 dsl }
456 1.49 jdolecek tp->t_pgrp = pgrp;
457 1.80 ad mutex_exit(&proclist_lock);
458 1.49 jdolecek return 0;
459 1.47 christos
460 1.1 fvdl default:
461 1.1 fvdl return EOPNOTSUPP;
462 1.1 fvdl }
463 1.1 fvdl
464 1.1 fvdl SCARG(&fca, fd) = fd;
465 1.1 fvdl SCARG(&fca, cmd) = cmd;
466 1.1 fvdl SCARG(&fca, arg) = arg;
467 1.12 mycroft
468 1.56 thorpej return sys_fcntl(l, &fca, retval);
469 1.1 fvdl }
470 1.1 fvdl
471 1.67 manu #if !defined(__amd64__)
472 1.1 fvdl /*
473 1.1 fvdl * Convert a NetBSD stat structure to a Linux stat structure.
474 1.1 fvdl * Only the order of the fields and the padding in the structure
475 1.9 fvdl * is different. linux_fakedev is a machine-dependent function
476 1.9 fvdl * which optionally converts device driver major/minor numbers
477 1.9 fvdl * (XXX horrible, but what can you do against code that compares
478 1.9 fvdl * things against constant major device numbers? sigh)
479 1.1 fvdl */
480 1.1 fvdl static void
481 1.1 fvdl bsd_to_linux_stat(bsp, lsp)
482 1.1 fvdl struct stat *bsp;
483 1.1 fvdl struct linux_stat *lsp;
484 1.1 fvdl {
485 1.12 mycroft
486 1.45 christos lsp->lst_dev = linux_fakedev(bsp->st_dev, 0);
487 1.1 fvdl lsp->lst_ino = bsp->st_ino;
488 1.19 christos lsp->lst_mode = (linux_mode_t)bsp->st_mode;
489 1.19 christos if (bsp->st_nlink >= (1 << 15))
490 1.19 christos lsp->lst_nlink = (1 << 15) - 1;
491 1.19 christos else
492 1.19 christos lsp->lst_nlink = (linux_nlink_t)bsp->st_nlink;
493 1.1 fvdl lsp->lst_uid = bsp->st_uid;
494 1.1 fvdl lsp->lst_gid = bsp->st_gid;
495 1.45 christos lsp->lst_rdev = linux_fakedev(bsp->st_rdev, 1);
496 1.1 fvdl lsp->lst_size = bsp->st_size;
497 1.1 fvdl lsp->lst_blksize = bsp->st_blksize;
498 1.1 fvdl lsp->lst_blocks = bsp->st_blocks;
499 1.1 fvdl lsp->lst_atime = bsp->st_atime;
500 1.1 fvdl lsp->lst_mtime = bsp->st_mtime;
501 1.1 fvdl lsp->lst_ctime = bsp->st_ctime;
502 1.66 christos #ifdef LINUX_STAT_HAS_NSEC
503 1.66 christos lsp->lst_atime_nsec = bsp->st_atimensec;
504 1.66 christos lsp->lst_mtime_nsec = bsp->st_mtimensec;
505 1.66 christos lsp->lst_ctime_nsec = bsp->st_ctimensec;
506 1.66 christos #endif
507 1.1 fvdl }
508 1.1 fvdl
509 1.1 fvdl /*
510 1.1 fvdl * The stat functions below are plain sailing. stat and lstat are handled
511 1.1 fvdl * by one function to avoid code duplication.
512 1.1 fvdl */
513 1.1 fvdl int
514 1.56 thorpej linux_sys_fstat(l, v, retval)
515 1.56 thorpej struct lwp *l;
516 1.11 thorpej void *v;
517 1.11 thorpej register_t *retval;
518 1.11 thorpej {
519 1.12 mycroft struct linux_sys_fstat_args /* {
520 1.1 fvdl syscallarg(int) fd;
521 1.1 fvdl syscallarg(linux_stat *) sp;
522 1.11 thorpej } */ *uap = v;
523 1.1 fvdl struct linux_stat tmplst;
524 1.81 dsl struct stat tmpst;
525 1.1 fvdl int error;
526 1.1 fvdl
527 1.81 dsl error = do_sys_fstat(l, SCARG(uap, fd), &tmpst);
528 1.81 dsl if (error != 0)
529 1.1 fvdl return error;
530 1.1 fvdl bsd_to_linux_stat(&tmpst, &tmplst);
531 1.1 fvdl
532 1.81 dsl return copyout(&tmplst, SCARG(uap, sp), sizeof tmplst);
533 1.1 fvdl }
534 1.1 fvdl
535 1.1 fvdl static int
536 1.81 dsl linux_stat1(l, v, retval, flags)
537 1.56 thorpej struct lwp *l;
538 1.14 christos void *v;
539 1.1 fvdl register_t *retval;
540 1.81 dsl int flags;
541 1.1 fvdl {
542 1.1 fvdl struct linux_stat tmplst;
543 1.81 dsl struct stat tmpst;
544 1.1 fvdl int error;
545 1.14 christos struct linux_sys_stat_args *uap = v;
546 1.1 fvdl
547 1.81 dsl error = do_sys_stat(l, SCARG(uap, path), flags, &tmpst);
548 1.81 dsl if (error != 0)
549 1.1 fvdl return error;
550 1.1 fvdl
551 1.1 fvdl bsd_to_linux_stat(&tmpst, &tmplst);
552 1.1 fvdl
553 1.81 dsl return copyout(&tmplst, SCARG(uap, sp), sizeof tmplst);
554 1.1 fvdl }
555 1.1 fvdl
556 1.1 fvdl int
557 1.56 thorpej linux_sys_stat(l, v, retval)
558 1.56 thorpej struct lwp *l;
559 1.11 thorpej void *v;
560 1.11 thorpej register_t *retval;
561 1.11 thorpej {
562 1.12 mycroft struct linux_sys_stat_args /* {
563 1.27 christos syscallarg(const char *) path;
564 1.1 fvdl syscallarg(struct linux_stat *) sp;
565 1.11 thorpej } */ *uap = v;
566 1.11 thorpej
567 1.81 dsl return linux_stat1(l, uap, retval, FOLLOW);
568 1.1 fvdl }
569 1.1 fvdl
570 1.23 erh /* Note: this is "newlstat" in the Linux sources */
571 1.23 erh /* (we don't bother with the old lstat currently) */
572 1.1 fvdl int
573 1.56 thorpej linux_sys_lstat(l, v, retval)
574 1.56 thorpej struct lwp *l;
575 1.11 thorpej void *v;
576 1.11 thorpej register_t *retval;
577 1.11 thorpej {
578 1.12 mycroft struct linux_sys_lstat_args /* {
579 1.27 christos syscallarg(const char *) path;
580 1.1 fvdl syscallarg(struct linux_stat *) sp;
581 1.11 thorpej } */ *uap = v;
582 1.11 thorpej
583 1.81 dsl return linux_stat1(l, uap, retval, NOFOLLOW);
584 1.1 fvdl }
585 1.67 manu #endif /* !__amd64__ */
586 1.1 fvdl
587 1.1 fvdl /*
588 1.10 fvdl * The following syscalls are mostly here because of the alternate path check.
589 1.1 fvdl */
590 1.1 fvdl int
591 1.56 thorpej linux_sys_unlink(l, v, retval)
592 1.56 thorpej struct lwp *l;
593 1.11 thorpej void *v;
594 1.2 fvdl register_t *retval;
595 1.2 fvdl
596 1.2 fvdl {
597 1.12 mycroft struct linux_sys_unlink_args /* {
598 1.27 christos syscallarg(const char *) path;
599 1.11 thorpej } */ *uap = v;
600 1.63 jdolecek int error;
601 1.63 jdolecek struct nameidata nd;
602 1.2 fvdl
603 1.63 jdolecek error = sys_unlink(l, uap, retval);
604 1.63 jdolecek if (error != EPERM)
605 1.63 jdolecek return (error);
606 1.63 jdolecek
607 1.63 jdolecek /*
608 1.63 jdolecek * Linux returns EISDIR if unlink(2) is called on a directory.
609 1.63 jdolecek * We return EPERM in such cases. To emulate correct behaviour,
610 1.63 jdolecek * check if the path points to directory and return EISDIR if this
611 1.63 jdolecek * is the case.
612 1.63 jdolecek */
613 1.82 dsl NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | TRYEMULROOT, UIO_USERSPACE,
614 1.71 christos SCARG(uap, path), l);
615 1.63 jdolecek if (namei(&nd) == 0) {
616 1.63 jdolecek struct stat sb;
617 1.63 jdolecek
618 1.71 christos if (vn_stat(nd.ni_vp, &sb, l) == 0
619 1.63 jdolecek && S_ISDIR(sb.st_mode))
620 1.63 jdolecek error = EISDIR;
621 1.63 jdolecek
622 1.63 jdolecek vput(nd.ni_vp);
623 1.63 jdolecek }
624 1.63 jdolecek
625 1.63 jdolecek return (error);
626 1.2 fvdl }
627 1.2 fvdl
628 1.10 fvdl int
629 1.56 thorpej linux_sys_mknod(l, v, retval)
630 1.56 thorpej struct lwp *l;
631 1.11 thorpej void *v;
632 1.11 thorpej register_t *retval;
633 1.11 thorpej {
634 1.12 mycroft struct linux_sys_mknod_args /* {
635 1.27 christos syscallarg(const char *) path;
636 1.2 fvdl syscallarg(int) mode;
637 1.2 fvdl syscallarg(int) dev;
638 1.11 thorpej } */ *uap = v;
639 1.2 fvdl
640 1.10 fvdl /*
641 1.40 wiz * BSD handles FIFOs separately
642 1.10 fvdl */
643 1.74 pavel if (S_ISFIFO(SCARG(uap, mode))) {
644 1.54 jdolecek struct sys_mkfifo_args bma;
645 1.54 jdolecek
646 1.21 thorpej SCARG(&bma, path) = SCARG(uap, path);
647 1.21 thorpej SCARG(&bma, mode) = SCARG(uap, mode);
648 1.56 thorpej return sys_mkfifo(l, &bma, retval);
649 1.54 jdolecek } else {
650 1.54 jdolecek struct sys_mknod_args bma;
651 1.54 jdolecek
652 1.54 jdolecek SCARG(&bma, path) = SCARG(uap, path);
653 1.54 jdolecek SCARG(&bma, mode) = SCARG(uap, mode);
654 1.54 jdolecek /*
655 1.54 jdolecek * Linux device numbers uses 8 bits for minor and 8 bits
656 1.54 jdolecek * for major. Due to how we map our major and minor,
657 1.82 dsl * this just fits into our dev_t. Just mask off the
658 1.54 jdolecek * upper 16bit to remove any random junk.
659 1.54 jdolecek */
660 1.54 jdolecek SCARG(&bma, dev) = SCARG(uap, dev) & 0xffff;
661 1.56 thorpej return sys_mknod(l, &bma, retval);
662 1.54 jdolecek }
663 1.2 fvdl }
664 1.2 fvdl
665 1.67 manu #if defined(__i386__) || defined(__m68k__) || \
666 1.67 manu defined(__arm__)
667 1.2 fvdl int
668 1.56 thorpej linux_sys_chown16(l, v, retval)
669 1.56 thorpej struct lwp *l;
670 1.11 thorpej void *v;
671 1.11 thorpej register_t *retval;
672 1.11 thorpej {
673 1.31 fvdl struct linux_sys_chown16_args /* {
674 1.27 christos syscallarg(const char *) path;
675 1.2 fvdl syscallarg(int) uid;
676 1.2 fvdl syscallarg(int) gid;
677 1.11 thorpej } */ *uap = v;
678 1.22 kleink struct sys___posix_chown_args bca;
679 1.2 fvdl
680 1.10 fvdl SCARG(&bca, path) = SCARG(uap, path);
681 1.10 fvdl SCARG(&bca, uid) = ((linux_uid_t)SCARG(uap, uid) == (linux_uid_t)-1) ?
682 1.10 fvdl (uid_t)-1 : SCARG(uap, uid);
683 1.10 fvdl SCARG(&bca, gid) = ((linux_gid_t)SCARG(uap, gid) == (linux_gid_t)-1) ?
684 1.10 fvdl (gid_t)-1 : SCARG(uap, gid);
685 1.65 perry
686 1.56 thorpej return sys___posix_chown(l, &bca, retval);
687 1.10 fvdl }
688 1.10 fvdl
689 1.10 fvdl int
690 1.56 thorpej linux_sys_fchown16(l, v, retval)
691 1.56 thorpej struct lwp *l;
692 1.11 thorpej void *v;
693 1.11 thorpej register_t *retval;
694 1.11 thorpej {
695 1.31 fvdl struct linux_sys_fchown16_args /* {
696 1.10 fvdl syscallarg(int) fd;
697 1.10 fvdl syscallarg(int) uid;
698 1.10 fvdl syscallarg(int) gid;
699 1.11 thorpej } */ *uap = v;
700 1.22 kleink struct sys___posix_fchown_args bfa;
701 1.10 fvdl
702 1.10 fvdl SCARG(&bfa, fd) = SCARG(uap, fd);
703 1.10 fvdl SCARG(&bfa, uid) = ((linux_uid_t)SCARG(uap, uid) == (linux_uid_t)-1) ?
704 1.10 fvdl (uid_t)-1 : SCARG(uap, uid);
705 1.10 fvdl SCARG(&bfa, gid) = ((linux_gid_t)SCARG(uap, gid) == (linux_gid_t)-1) ?
706 1.10 fvdl (gid_t)-1 : SCARG(uap, gid);
707 1.65 perry
708 1.56 thorpej return sys___posix_fchown(l, &bfa, retval);
709 1.2 fvdl }
710 1.2 fvdl
711 1.2 fvdl int
712 1.56 thorpej linux_sys_lchown16(l, v, retval)
713 1.56 thorpej struct lwp *l;
714 1.23 erh void *v;
715 1.23 erh register_t *retval;
716 1.23 erh {
717 1.31 fvdl struct linux_sys_lchown16_args /* {
718 1.23 erh syscallarg(char *) path;
719 1.23 erh syscallarg(int) uid;
720 1.23 erh syscallarg(int) gid;
721 1.23 erh } */ *uap = v;
722 1.23 erh struct sys___posix_lchown_args bla;
723 1.23 erh
724 1.23 erh SCARG(&bla, path) = SCARG(uap, path);
725 1.23 erh SCARG(&bla, uid) = ((linux_uid_t)SCARG(uap, uid) == (linux_uid_t)-1) ?
726 1.23 erh (uid_t)-1 : SCARG(uap, uid);
727 1.23 erh SCARG(&bla, gid) = ((linux_gid_t)SCARG(uap, gid) == (linux_gid_t)-1) ?
728 1.23 erh (gid_t)-1 : SCARG(uap, gid);
729 1.23 erh
730 1.56 thorpej return sys___posix_lchown(l, &bla, retval);
731 1.33 fvdl }
732 1.67 manu #endif /* __i386__ || __m68k__ || __arm__ || __amd64__ */
733 1.15 fvdl
734 1.15 fvdl /*
735 1.15 fvdl * This is just fsync() for now (just as it is in the Linux kernel)
736 1.23 erh * Note: this is not implemented under Linux on Alpha and Arm
737 1.23 erh * but should still be defined in our syscalls.master.
738 1.23 erh * (syscall #148 on the arm)
739 1.15 fvdl */
740 1.15 fvdl int
741 1.56 thorpej linux_sys_fdatasync(l, v, retval)
742 1.56 thorpej struct lwp *l;
743 1.15 fvdl void *v;
744 1.15 fvdl register_t *retval;
745 1.15 fvdl {
746 1.16 christos #ifdef notdef
747 1.15 fvdl struct linux_sys_fdatasync_args /* {
748 1.15 fvdl syscallarg(int) fd;
749 1.15 fvdl } */ *uap = v;
750 1.16 christos #endif
751 1.56 thorpej return sys_fsync(l, v, retval);
752 1.28 tron }
753 1.28 tron
754 1.28 tron /*
755 1.28 tron * pread(2).
756 1.28 tron */
757 1.28 tron int
758 1.56 thorpej linux_sys_pread(l, v, retval)
759 1.56 thorpej struct lwp *l;
760 1.28 tron void *v;
761 1.28 tron register_t *retval;
762 1.28 tron {
763 1.28 tron struct linux_sys_pread_args /* {
764 1.28 tron syscallarg(int) fd;
765 1.28 tron syscallarg(void *) buf;
766 1.28 tron syscallarg(size_t) nbyte;
767 1.28 tron syscallarg(linux_off_t) offset;
768 1.28 tron } */ *uap = v;
769 1.28 tron struct sys_pread_args pra;
770 1.28 tron
771 1.28 tron SCARG(&pra, fd) = SCARG(uap, fd);
772 1.28 tron SCARG(&pra, buf) = SCARG(uap, buf);
773 1.28 tron SCARG(&pra, nbyte) = SCARG(uap, nbyte);
774 1.28 tron SCARG(&pra, offset) = SCARG(uap, offset);
775 1.28 tron
776 1.62 jdolecek return sys_pread(l, &pra, retval);
777 1.28 tron }
778 1.28 tron
779 1.28 tron /*
780 1.28 tron * pwrite(2).
781 1.28 tron */
782 1.28 tron int
783 1.56 thorpej linux_sys_pwrite(l, v, retval)
784 1.56 thorpej struct lwp *l;
785 1.28 tron void *v;
786 1.28 tron register_t *retval;
787 1.28 tron {
788 1.28 tron struct linux_sys_pwrite_args /* {
789 1.28 tron syscallarg(int) fd;
790 1.28 tron syscallarg(void *) buf;
791 1.28 tron syscallarg(size_t) nbyte;
792 1.28 tron syscallarg(linux_off_t) offset;
793 1.28 tron } */ *uap = v;
794 1.28 tron struct sys_pwrite_args pra;
795 1.28 tron
796 1.28 tron SCARG(&pra, fd) = SCARG(uap, fd);
797 1.28 tron SCARG(&pra, buf) = SCARG(uap, buf);
798 1.28 tron SCARG(&pra, nbyte) = SCARG(uap, nbyte);
799 1.28 tron SCARG(&pra, offset) = SCARG(uap, offset);
800 1.28 tron
801 1.62 jdolecek return sys_pwrite(l, &pra, retval);
802 1.1 fvdl }
803 1.68 fvdl
804 1.75 christos #define LINUX_NOT_SUPPORTED(fun) \
805 1.75 christos int \
806 1.76 christos fun(struct lwp *l, void *v, register_t *retval) \
807 1.75 christos { \
808 1.75 christos return EOPNOTSUPP; \
809 1.75 christos }
810 1.75 christos
811 1.75 christos LINUX_NOT_SUPPORTED(linux_sys_setxattr)
812 1.75 christos LINUX_NOT_SUPPORTED(linux_sys_lsetxattr)
813 1.75 christos LINUX_NOT_SUPPORTED(linux_sys_fsetxattr)
814 1.75 christos
815 1.75 christos LINUX_NOT_SUPPORTED(linux_sys_getxattr)
816 1.75 christos LINUX_NOT_SUPPORTED(linux_sys_lgetxattr)
817 1.75 christos LINUX_NOT_SUPPORTED(linux_sys_fgetxattr)
818 1.75 christos
819 1.75 christos LINUX_NOT_SUPPORTED(linux_sys_listxattr)
820 1.75 christos LINUX_NOT_SUPPORTED(linux_sys_llistxattr)
821 1.75 christos LINUX_NOT_SUPPORTED(linux_sys_flistxattr)
822 1.75 christos
823 1.75 christos LINUX_NOT_SUPPORTED(linux_sys_removexattr)
824 1.75 christos LINUX_NOT_SUPPORTED(linux_sys_lremovexattr)
825 1.75 christos LINUX_NOT_SUPPORTED(linux_sys_fremovexattr)
826