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