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