1 /*-
2 * Copyright (c) 1995 Scott Bartram
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD: releng/8.4/sys/i386/ibcs2/ibcs2_fcntl.c 199583 2009-11-20 15:27:52Z jhb $");
30
31 #include "opt_spx_hack.h"
32
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/fcntl.h>
36 #include <sys/file.h>
37 #include <sys/filedesc.h>
38 #include <sys/lock.h>
39 #include <sys/malloc.h>
40 #include <sys/mutex.h>
41 #include <sys/syscallsubr.h>
42 #include <sys/sysproto.h>
43 #include <sys/ttycom.h>
44
45 #include <i386/ibcs2/ibcs2_fcntl.h>
46 #include <i386/ibcs2/ibcs2_signal.h>
47 #include <i386/ibcs2/ibcs2_proto.h>
48 #include <i386/ibcs2/ibcs2_util.h>
49
50 static void cvt_iflock2flock(struct ibcs2_flock *, struct flock *);
51 static void cvt_flock2iflock(struct flock *, struct ibcs2_flock *);
52 static int cvt_o_flags(int);
53 static int oflags2ioflags(int);
54 static int ioflags2oflags(int);
55
56 static int
57 cvt_o_flags(flags)
58 int flags;
59 {
60 int r = 0;
61
62 /* convert mode into NetBSD mode */
63 if (flags & IBCS2_O_WRONLY) r |= O_WRONLY;
64 if (flags & IBCS2_O_RDWR) r |= O_RDWR;
65 if (flags & (IBCS2_O_NDELAY | IBCS2_O_NONBLOCK)) r |= O_NONBLOCK;
66 if (flags & IBCS2_O_APPEND) r |= O_APPEND;
67 if (flags & IBCS2_O_SYNC) r |= O_FSYNC;
68 if (flags & IBCS2_O_CREAT) r |= O_CREAT;
69 if (flags & IBCS2_O_TRUNC) r |= O_TRUNC /* | O_CREAT ??? */;
70 if (flags & IBCS2_O_EXCL) r |= O_EXCL;
71 if (flags & IBCS2_O_RDONLY) r |= O_RDONLY;
72 if (flags & IBCS2_O_PRIV) r |= O_EXLOCK;
73 if (flags & IBCS2_O_NOCTTY) r |= O_NOCTTY;
74 return r;
75 }
76
77 static void
78 cvt_flock2iflock(flp, iflp)
79 struct flock *flp;
80 struct ibcs2_flock *iflp;
81 {
82 switch (flp->l_type) {
83 case F_RDLCK:
84 iflp->l_type = IBCS2_F_RDLCK;
85 break;
86 case F_WRLCK:
87 iflp->l_type = IBCS2_F_WRLCK;
88 break;
89 case F_UNLCK:
90 iflp->l_type = IBCS2_F_UNLCK;
91 break;
92 }
93 iflp->l_whence = (short)flp->l_whence;
94 iflp->l_start = (ibcs2_off_t)flp->l_start;
95 iflp->l_len = (ibcs2_off_t)flp->l_len;
96 iflp->l_sysid = flp->l_sysid;
97 iflp->l_pid = (ibcs2_pid_t)flp->l_pid;
98 }
99
100 #ifdef DEBUG_IBCS2
101 static void
102 print_flock(struct flock *flp)
103 {
104 printf("flock: start=%x len=%x pid=%d type=%d whence=%d\n",
105 (int)flp->l_start, (int)flp->l_len, (int)flp->l_pid,
106 flp->l_type, flp->l_whence);
107 }
108 #endif
109
110 static void
111 cvt_iflock2flock(iflp, flp)
112 struct ibcs2_flock *iflp;
113 struct flock *flp;
114 {
115 flp->l_start = (off_t)iflp->l_start;
116 flp->l_len = (off_t)iflp->l_len;
117 flp->l_pid = (pid_t)iflp->l_pid;
118 switch (iflp->l_type) {
119 case IBCS2_F_RDLCK:
120 flp->l_type = F_RDLCK;
121 break;
122 case IBCS2_F_WRLCK:
123 flp->l_type = F_WRLCK;
124 break;
125 case IBCS2_F_UNLCK:
126 flp->l_type = F_UNLCK;
127 break;
128 }
129 flp->l_whence = iflp->l_whence;
130 flp->l_sysid = iflp->l_sysid;
131 }
132
133 /* convert iBCS2 mode into NetBSD mode */
134 static int
135 ioflags2oflags(flags)
136 int flags;
137 {
138 int r = 0;
139
140 if (flags & IBCS2_O_RDONLY) r |= O_RDONLY;
141 if (flags & IBCS2_O_WRONLY) r |= O_WRONLY;
142 if (flags & IBCS2_O_RDWR) r |= O_RDWR;
143 if (flags & IBCS2_O_NDELAY) r |= O_NONBLOCK;
144 if (flags & IBCS2_O_APPEND) r |= O_APPEND;
145 if (flags & IBCS2_O_SYNC) r |= O_FSYNC;
146 if (flags & IBCS2_O_NONBLOCK) r |= O_NONBLOCK;
147 if (flags & IBCS2_O_CREAT) r |= O_CREAT;
148 if (flags & IBCS2_O_TRUNC) r |= O_TRUNC;
149 if (flags & IBCS2_O_EXCL) r |= O_EXCL;
150 if (flags & IBCS2_O_NOCTTY) r |= O_NOCTTY;
151 return r;
152 }
153
154 /* convert NetBSD mode into iBCS2 mode */
155 static int
156 oflags2ioflags(flags)
157 int flags;
158 {
159 int r = 0;
160
161 if (flags & O_RDONLY) r |= IBCS2_O_RDONLY;
162 if (flags & O_WRONLY) r |= IBCS2_O_WRONLY;
163 if (flags & O_RDWR) r |= IBCS2_O_RDWR;
164 if (flags & O_NDELAY) r |= IBCS2_O_NONBLOCK;
165 if (flags & O_APPEND) r |= IBCS2_O_APPEND;
166 if (flags & O_FSYNC) r |= IBCS2_O_SYNC;
167 if (flags & O_NONBLOCK) r |= IBCS2_O_NONBLOCK;
168 if (flags & O_CREAT) r |= IBCS2_O_CREAT;
169 if (flags & O_TRUNC) r |= IBCS2_O_TRUNC;
170 if (flags & O_EXCL) r |= IBCS2_O_EXCL;
171 if (flags & O_NOCTTY) r |= IBCS2_O_NOCTTY;
172 return r;
173 }
174
175 int
176 ibcs2_open(td, uap)
177 struct thread *td;
178 struct ibcs2_open_args *uap;
179 {
180 struct proc *p;
181 char *path;
182 int flags, noctty, ret;
183
184 p = td->td_proc;
185 noctty = uap->flags & IBCS2_O_NOCTTY;
186 flags = cvt_o_flags(uap->flags);
187 if (uap->flags & O_CREAT)
188 CHECKALTCREAT(td, uap->path, &path);
189 else
190 CHECKALTEXIST(td, uap->path, &path);
191 ret = kern_open(td, path, UIO_SYSSPACE, flags, uap->mode);
192
193 #ifdef SPX_HACK
194 if (ret == ENXIO) {
195 if (!strcmp(path, "/compat/ibcs2/dev/spx"))
196 ret = spx_open(td);
197 free(path, M_TEMP);
198 } else
199 #endif /* SPX_HACK */
200 free(path, M_TEMP);
201 PROC_LOCK(p);
202 if (!ret && !noctty && SESS_LEADER(p) && !(p->p_flag & P_CONTROLT)) {
203 struct file *fp;
204 int error;
205
206 error = fget(td, td->td_retval[0], &fp);
207 PROC_UNLOCK(p);
208 if (error)
209 return (EBADF);
210
211 /* ignore any error, just give it a try */
212 if (fp->f_type == DTYPE_VNODE)
213 fo_ioctl(fp, TIOCSCTTY, (caddr_t) 0, td->td_ucred,
214 td);
215 fdrop(fp, td);
216 } else
217 PROC_UNLOCK(p);
218 return ret;
219 }
220
221 int
222 ibcs2_creat(td, uap)
223 struct thread *td;
224 struct ibcs2_creat_args *uap;
225 {
226 char *path;
227 int error;
228
229 CHECKALTCREAT(td, uap->path, &path);
230 error = kern_open(td, path, UIO_SYSSPACE, O_WRONLY | O_CREAT | O_TRUNC,
231 uap->mode);
232 free(path, M_TEMP);
233 return (error);
234 }
235
236 int
237 ibcs2_access(td, uap)
238 struct thread *td;
239 struct ibcs2_access_args *uap;
240 {
241 char *path;
242 int error;
243
244 CHECKALTEXIST(td, uap->path, &path);
245 error = kern_access(td, path, UIO_SYSSPACE, uap->flags);
246 free(path, M_TEMP);
247 return (error);
248 }
249
250 int
251 ibcs2_fcntl(td, uap)
252 struct thread *td;
253 struct ibcs2_fcntl_args *uap;
254 {
255 intptr_t arg;
256 int error;
257 struct flock fl;
258 struct ibcs2_flock ifl;
259
260 arg = (intptr_t)uap->arg;
261 switch(uap->cmd) {
262 case IBCS2_F_DUPFD:
263 return (kern_fcntl(td, uap->fd, F_DUPFD, arg));
264 case IBCS2_F_GETFD:
265 return (kern_fcntl(td, uap->fd, F_GETFD, arg));
266 case IBCS2_F_SETFD:
267 return (kern_fcntl(td, uap->fd, F_SETFD, arg));
268 case IBCS2_F_GETFL:
269 error = kern_fcntl(td, uap->fd, F_GETFL, arg);
270 if (error)
271 return error;
272 td->td_retval[0] = oflags2ioflags(td->td_retval[0]);
273 return error;
274 case IBCS2_F_SETFL:
275 return (kern_fcntl(td, uap->fd, F_SETFL,
276 ioflags2oflags(arg)));
277
278 case IBCS2_F_GETLK:
279 {
280 error = copyin((caddr_t)uap->arg, (caddr_t)&ifl,
281 ibcs2_flock_len);
282 if (error)
283 return error;
284 cvt_iflock2flock(&ifl, &fl);
285 error = kern_fcntl(td, uap->fd, F_GETLK, (intptr_t)&fl);
286 if (error)
287 return error;
288 cvt_flock2iflock(&fl, &ifl);
289 return copyout((caddr_t)&ifl, (caddr_t)uap->arg,
290 ibcs2_flock_len);
291 }
292
293 case IBCS2_F_SETLK:
294 {
295 error = copyin((caddr_t)uap->arg, (caddr_t)&ifl,
296 ibcs2_flock_len);
297 if (error)
298 return error;
299 cvt_iflock2flock(&ifl, &fl);
300 return (kern_fcntl(td, uap->fd, F_SETLK, (intptr_t)&fl));
301 }
302
303 case IBCS2_F_SETLKW:
304 {
305 error = copyin((caddr_t)uap->arg, (caddr_t)&ifl,
306 ibcs2_flock_len);
307 if (error)
308 return error;
309 cvt_iflock2flock(&ifl, &fl);
310 return (kern_fcntl(td, uap->fd, F_SETLKW, (intptr_t)&fl));
311 }
312 }
313 return ENOSYS;
314 }
Cache object: 5da82d46fd99666a39554750ee712c71
|