]> CyberLeo.Net >> Repos - FreeBSD/releng/9.2.git/blob - sys/i386/ibcs2/ibcs2_fcntl.c
- Copy stable/9 to releng/9.2 as part of the 9.2-RELEASE cycle.
[FreeBSD/releng/9.2.git] / sys / i386 / ibcs2 / ibcs2_fcntl.c
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$");
30
31 #include "opt_spx_hack.h"
32
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/capability.h>
36 #include <sys/fcntl.h>
37 #include <sys/file.h>
38 #include <sys/filedesc.h>
39 #include <sys/lock.h>
40 #include <sys/malloc.h>
41 #include <sys/mutex.h>
42 #include <sys/syscallsubr.h>
43 #include <sys/sysproto.h>
44 #include <sys/ttycom.h>
45
46 #include <i386/ibcs2/ibcs2_fcntl.h>
47 #include <i386/ibcs2/ibcs2_signal.h>
48 #include <i386/ibcs2/ibcs2_proto.h>
49 #include <i386/ibcs2/ibcs2_util.h>
50
51 static void cvt_iflock2flock(struct ibcs2_flock *, struct flock *);
52 static void cvt_flock2iflock(struct flock *, struct ibcs2_flock *);
53 static int  cvt_o_flags(int);
54 static int  oflags2ioflags(int);
55 static int  ioflags2oflags(int);
56
57 static int
58 cvt_o_flags(flags)
59         int flags;
60 {
61         int r = 0;
62
63         /* convert mode into NetBSD mode */
64         if (flags & IBCS2_O_WRONLY) r |= O_WRONLY;
65         if (flags & IBCS2_O_RDWR)   r |= O_RDWR;
66         if (flags & (IBCS2_O_NDELAY | IBCS2_O_NONBLOCK)) r |= O_NONBLOCK;
67         if (flags & IBCS2_O_APPEND) r |= O_APPEND;
68         if (flags & IBCS2_O_SYNC)   r |= O_FSYNC;
69         if (flags & IBCS2_O_CREAT)  r |= O_CREAT;
70         if (flags & IBCS2_O_TRUNC)  r |= O_TRUNC /* | O_CREAT ??? */;
71         if (flags & IBCS2_O_EXCL)   r |= O_EXCL;
72         if (flags & IBCS2_O_RDONLY) r |= O_RDONLY;
73         if (flags & IBCS2_O_PRIV)   r |= O_EXLOCK;
74         if (flags & IBCS2_O_NOCTTY) r |= O_NOCTTY;
75         return r;
76 }
77
78 static void
79 cvt_flock2iflock(flp, iflp)
80         struct flock *flp;
81         struct ibcs2_flock *iflp;
82 {
83         switch (flp->l_type) {
84         case F_RDLCK:
85                 iflp->l_type = IBCS2_F_RDLCK;
86                 break;
87         case F_WRLCK:
88                 iflp->l_type = IBCS2_F_WRLCK;
89                 break;
90         case F_UNLCK:
91                 iflp->l_type = IBCS2_F_UNLCK;
92                 break;
93         }
94         iflp->l_whence = (short)flp->l_whence;
95         iflp->l_start = (ibcs2_off_t)flp->l_start;
96         iflp->l_len = (ibcs2_off_t)flp->l_len;
97         iflp->l_sysid = flp->l_sysid;
98         iflp->l_pid = (ibcs2_pid_t)flp->l_pid;
99 }
100
101 #ifdef DEBUG_IBCS2
102 static void
103 print_flock(struct flock *flp)
104 {
105   printf("flock: start=%x len=%x pid=%d type=%d whence=%d\n",
106          (int)flp->l_start, (int)flp->l_len, (int)flp->l_pid,
107          flp->l_type, flp->l_whence);
108 }
109 #endif
110
111 static void
112 cvt_iflock2flock(iflp, flp)
113         struct ibcs2_flock *iflp;
114         struct flock *flp;
115 {
116         flp->l_start = (off_t)iflp->l_start;
117         flp->l_len = (off_t)iflp->l_len;
118         flp->l_pid = (pid_t)iflp->l_pid;
119         switch (iflp->l_type) {
120         case IBCS2_F_RDLCK:
121                 flp->l_type = F_RDLCK;
122                 break;
123         case IBCS2_F_WRLCK:
124                 flp->l_type = F_WRLCK;
125                 break;
126         case IBCS2_F_UNLCK:
127                 flp->l_type = F_UNLCK;
128                 break;
129         }
130         flp->l_whence = iflp->l_whence;
131         flp->l_sysid = iflp->l_sysid;
132 }
133
134 /* convert iBCS2 mode into NetBSD mode */
135 static int
136 ioflags2oflags(flags)
137         int flags;
138 {
139         int r = 0;
140         
141         if (flags & IBCS2_O_RDONLY) r |= O_RDONLY;
142         if (flags & IBCS2_O_WRONLY) r |= O_WRONLY;
143         if (flags & IBCS2_O_RDWR) r |= O_RDWR;
144         if (flags & IBCS2_O_NDELAY) r |= O_NONBLOCK;
145         if (flags & IBCS2_O_APPEND) r |= O_APPEND;
146         if (flags & IBCS2_O_SYNC) r |= O_FSYNC;
147         if (flags & IBCS2_O_NONBLOCK) r |= O_NONBLOCK;
148         if (flags & IBCS2_O_CREAT) r |= O_CREAT;
149         if (flags & IBCS2_O_TRUNC) r |= O_TRUNC;
150         if (flags & IBCS2_O_EXCL) r |= O_EXCL;
151         if (flags & IBCS2_O_NOCTTY) r |= O_NOCTTY;
152         return r;
153 }
154
155 /* convert NetBSD mode into iBCS2 mode */
156 static int
157 oflags2ioflags(flags)
158         int flags;
159 {
160         int r = 0;
161         
162         if (flags & O_RDONLY) r |= IBCS2_O_RDONLY;
163         if (flags & O_WRONLY) r |= IBCS2_O_WRONLY;
164         if (flags & O_RDWR) r |= IBCS2_O_RDWR;
165         if (flags & O_NDELAY) r |= IBCS2_O_NONBLOCK;
166         if (flags & O_APPEND) r |= IBCS2_O_APPEND;
167         if (flags & O_FSYNC) r |= IBCS2_O_SYNC;
168         if (flags & O_NONBLOCK) r |= IBCS2_O_NONBLOCK;
169         if (flags & O_CREAT) r |= IBCS2_O_CREAT;
170         if (flags & O_TRUNC) r |= IBCS2_O_TRUNC;
171         if (flags & O_EXCL) r |= IBCS2_O_EXCL;
172         if (flags & O_NOCTTY) r |= IBCS2_O_NOCTTY;
173         return r;
174 }
175
176 int
177 ibcs2_open(td, uap)
178         struct thread *td;
179         struct ibcs2_open_args *uap;
180 {
181         struct proc *p;
182         char *path;
183         int flags, noctty, ret;
184
185         p = td->td_proc;
186         noctty = uap->flags & IBCS2_O_NOCTTY;
187         flags = cvt_o_flags(uap->flags);
188         if (uap->flags & O_CREAT)
189                 CHECKALTCREAT(td, uap->path, &path);
190         else
191                 CHECKALTEXIST(td, uap->path, &path);
192         ret = kern_open(td, path, UIO_SYSSPACE, flags, uap->mode);
193
194 #ifdef SPX_HACK
195         if (ret == ENXIO) {
196                 if (!strcmp(path, "/compat/ibcs2/dev/spx"))
197                         ret = spx_open(td);
198                 free(path, M_TEMP);
199         } else
200 #endif /* SPX_HACK */
201         free(path, M_TEMP);
202         PROC_LOCK(p);
203         if (!ret && !noctty && SESS_LEADER(p) && !(p->p_flag & P_CONTROLT)) {
204                 struct file *fp;
205                 int error;
206
207                 error = fget(td, td->td_retval[0], CAP_IOCTL, &fp);
208                 PROC_UNLOCK(p);
209                 if (error)
210                         return (EBADF);
211
212                 /* ignore any error, just give it a try */
213                 if (fp->f_type == DTYPE_VNODE)
214                         fo_ioctl(fp, TIOCSCTTY, (caddr_t) 0, td->td_ucred,
215                             td);
216                 fdrop(fp, td);
217         } else
218                 PROC_UNLOCK(p);
219         return ret;
220 }
221
222 int
223 ibcs2_creat(td, uap)
224         struct thread *td;  
225         struct ibcs2_creat_args *uap;
226 {
227         char *path;
228         int error;
229
230         CHECKALTCREAT(td, uap->path, &path);
231         error = kern_open(td, path, UIO_SYSSPACE, O_WRONLY | O_CREAT | O_TRUNC,
232             uap->mode);
233         free(path, M_TEMP);
234         return (error);
235 }       
236
237 int
238 ibcs2_access(td, uap)
239         struct thread *td;
240         struct ibcs2_access_args *uap;
241 {
242         char *path;
243         int error;
244
245         CHECKALTEXIST(td, uap->path, &path);
246         error = kern_access(td, path, UIO_SYSSPACE, uap->flags);
247         free(path, M_TEMP);
248         return (error);
249 }
250
251 int
252 ibcs2_fcntl(td, uap)
253         struct thread *td;
254         struct ibcs2_fcntl_args *uap;
255 {
256         intptr_t arg;
257         int error;
258         struct flock fl;
259         struct ibcs2_flock ifl;
260
261         arg = (intptr_t)uap->arg;
262         switch(uap->cmd) {
263         case IBCS2_F_DUPFD:
264                 return (kern_fcntl(td, uap->fd, F_DUPFD, arg));
265         case IBCS2_F_GETFD:
266                 return (kern_fcntl(td, uap->fd, F_GETFD, arg));
267         case IBCS2_F_SETFD:
268                 return (kern_fcntl(td, uap->fd, F_SETFD, arg));
269         case IBCS2_F_GETFL:
270                 error = kern_fcntl(td, uap->fd, F_GETFL, arg);
271                 if (error)
272                         return error;
273                 td->td_retval[0] = oflags2ioflags(td->td_retval[0]);
274                 return error;
275         case IBCS2_F_SETFL:
276                 return (kern_fcntl(td, uap->fd, F_SETFL,
277                     ioflags2oflags(arg)));
278
279         case IBCS2_F_GETLK:
280             {
281                 error = copyin((caddr_t)uap->arg, (caddr_t)&ifl,
282                                ibcs2_flock_len);
283                 if (error)
284                         return error;
285                 cvt_iflock2flock(&ifl, &fl);
286                 error = kern_fcntl(td, uap->fd, F_GETLK, (intptr_t)&fl);
287                 if (error)
288                         return error;
289                 cvt_flock2iflock(&fl, &ifl);
290                 return copyout((caddr_t)&ifl, (caddr_t)uap->arg,
291                                ibcs2_flock_len);
292             }
293
294         case IBCS2_F_SETLK:
295             {
296                 error = copyin((caddr_t)uap->arg, (caddr_t)&ifl,
297                                ibcs2_flock_len);
298                 if (error)
299                         return error;
300                 cvt_iflock2flock(&ifl, &fl);
301                 return (kern_fcntl(td, uap->fd, F_SETLK, (intptr_t)&fl));
302             }
303
304         case IBCS2_F_SETLKW:
305             {
306                 error = copyin((caddr_t)uap->arg, (caddr_t)&ifl,
307                                ibcs2_flock_len);
308                 if (error)
309                         return error;
310                 cvt_iflock2flock(&ifl, &fl);
311                 return (kern_fcntl(td, uap->fd, F_SETLKW, (intptr_t)&fl));
312             }
313         }
314         return ENOSYS;
315 }