]> CyberLeo.Net >> Repos - FreeBSD/releng/10.2.git/blob - sys/i386/ibcs2/ibcs2_fcntl.c
- Copy stable/10@285827 to releng/10.2 in preparation for 10.2-RC1
[FreeBSD/releng/10.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/capsicum.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                 cap_rights_t rights;
205                 struct file *fp;
206                 int error;
207
208                 error = fget(td, td->td_retval[0],
209                     cap_rights_init(&rights, CAP_IOCTL), &fp);
210                 PROC_UNLOCK(p);
211                 if (error)
212                         return (EBADF);
213
214                 /* ignore any error, just give it a try */
215                 if (fp->f_type == DTYPE_VNODE)
216                         fo_ioctl(fp, TIOCSCTTY, (caddr_t) 0, td->td_ucred,
217                             td);
218                 fdrop(fp, td);
219         } else
220                 PROC_UNLOCK(p);
221         return ret;
222 }
223
224 int
225 ibcs2_creat(td, uap)
226         struct thread *td;  
227         struct ibcs2_creat_args *uap;
228 {
229         char *path;
230         int error;
231
232         CHECKALTCREAT(td, uap->path, &path);
233         error = kern_open(td, path, UIO_SYSSPACE, O_WRONLY | O_CREAT | O_TRUNC,
234             uap->mode);
235         free(path, M_TEMP);
236         return (error);
237 }       
238
239 int
240 ibcs2_access(td, uap)
241         struct thread *td;
242         struct ibcs2_access_args *uap;
243 {
244         char *path;
245         int error;
246
247         CHECKALTEXIST(td, uap->path, &path);
248         error = kern_access(td, path, UIO_SYSSPACE, uap->amode);
249         free(path, M_TEMP);
250         return (error);
251 }
252
253 int
254 ibcs2_fcntl(td, uap)
255         struct thread *td;
256         struct ibcs2_fcntl_args *uap;
257 {
258         intptr_t arg;
259         int error;
260         struct flock fl;
261         struct ibcs2_flock ifl;
262
263         arg = (intptr_t)uap->arg;
264         switch(uap->cmd) {
265         case IBCS2_F_DUPFD:
266                 return (kern_fcntl(td, uap->fd, F_DUPFD, arg));
267         case IBCS2_F_GETFD:
268                 return (kern_fcntl(td, uap->fd, F_GETFD, arg));
269         case IBCS2_F_SETFD:
270                 return (kern_fcntl(td, uap->fd, F_SETFD, arg));
271         case IBCS2_F_GETFL:
272                 error = kern_fcntl(td, uap->fd, F_GETFL, arg);
273                 if (error)
274                         return error;
275                 td->td_retval[0] = oflags2ioflags(td->td_retval[0]);
276                 return error;
277         case IBCS2_F_SETFL:
278                 return (kern_fcntl(td, uap->fd, F_SETFL,
279                     ioflags2oflags(arg)));
280
281         case IBCS2_F_GETLK:
282             {
283                 error = copyin((caddr_t)uap->arg, (caddr_t)&ifl,
284                                ibcs2_flock_len);
285                 if (error)
286                         return error;
287                 cvt_iflock2flock(&ifl, &fl);
288                 error = kern_fcntl(td, uap->fd, F_GETLK, (intptr_t)&fl);
289                 if (error)
290                         return error;
291                 cvt_flock2iflock(&fl, &ifl);
292                 return copyout((caddr_t)&ifl, (caddr_t)uap->arg,
293                                ibcs2_flock_len);
294             }
295
296         case IBCS2_F_SETLK:
297             {
298                 error = copyin((caddr_t)uap->arg, (caddr_t)&ifl,
299                                ibcs2_flock_len);
300                 if (error)
301                         return error;
302                 cvt_iflock2flock(&ifl, &fl);
303                 return (kern_fcntl(td, uap->fd, F_SETLK, (intptr_t)&fl));
304             }
305
306         case IBCS2_F_SETLKW:
307             {
308                 error = copyin((caddr_t)uap->arg, (caddr_t)&ifl,
309                                ibcs2_flock_len);
310                 if (error)
311                         return error;
312                 cvt_iflock2flock(&ifl, &fl);
313                 return (kern_fcntl(td, uap->fd, F_SETLKW, (intptr_t)&fl));
314             }
315         }
316         return ENOSYS;
317 }