]>
Commit | Line | Data |
---|---|---|
1c79356b | 1 | /* |
cb323159 | 2 | * Copyright (c) 2000-2019 Apple Inc. All rights reserved. |
5d5c5d0d | 3 | * |
2d21ac55 | 4 | * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ |
0a7de745 | 5 | * |
2d21ac55 A |
6 | * This file contains Original Code and/or Modifications of Original Code |
7 | * as defined in and that are subject to the Apple Public Source License | |
8 | * Version 2.0 (the 'License'). You may not use this file except in | |
9 | * compliance with the License. The rights granted to you under the License | |
10 | * may not be used to create, or enable the creation or redistribution of, | |
11 | * unlawful or unlicensed copies of an Apple operating system, or to | |
12 | * circumvent, violate, or enable the circumvention or violation of, any | |
13 | * terms of an Apple operating system software license agreement. | |
0a7de745 | 14 | * |
2d21ac55 A |
15 | * Please obtain a copy of the License at |
16 | * http://www.opensource.apple.com/apsl/ and read it before using this file. | |
0a7de745 | 17 | * |
2d21ac55 A |
18 | * The Original Code and all software distributed under the License are |
19 | * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER | |
8f6c56a5 A |
20 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, |
21 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
2d21ac55 A |
22 | * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. |
23 | * Please see the License for the specific language governing rights and | |
24 | * limitations under the License. | |
0a7de745 | 25 | * |
2d21ac55 | 26 | * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ |
1c79356b A |
27 | */ |
28 | /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */ | |
29 | /* | |
30 | * Copyright (c) 1990, 1993, 1995 | |
31 | * The Regents of the University of California. All rights reserved. | |
32 | * | |
33 | * Redistribution and use in source and binary forms, with or without | |
34 | * modification, are permitted provided that the following conditions | |
35 | * are met: | |
36 | * 1. Redistributions of source code must retain the above copyright | |
37 | * notice, this list of conditions and the following disclaimer. | |
38 | * 2. Redistributions in binary form must reproduce the above copyright | |
39 | * notice, this list of conditions and the following disclaimer in the | |
40 | * documentation and/or other materials provided with the distribution. | |
41 | * 3. All advertising materials mentioning features or use of this software | |
42 | * must display the following acknowledgement: | |
43 | * This product includes software developed by the University of | |
44 | * California, Berkeley and its contributors. | |
45 | * 4. Neither the name of the University nor the names of its contributors | |
46 | * may be used to endorse or promote products derived from this software | |
47 | * without specific prior written permission. | |
48 | * | |
49 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | |
50 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
51 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
52 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
53 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
54 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
55 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
56 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
57 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
58 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
59 | * SUCH DAMAGE. | |
60 | * | |
61 | * @(#)fifo_vnops.c 8.4 (Berkeley) 8/10/94 | |
62 | */ | |
63 | ||
64 | #include <sys/param.h> | |
65 | #include <sys/proc.h> | |
66 | #include <sys/time.h> | |
67 | #include <sys/namei.h> | |
91447636 | 68 | #include <sys/vnode_internal.h> |
1c79356b A |
69 | #include <sys/socket.h> |
70 | #include <sys/socketvar.h> | |
71 | #include <sys/stat.h> | |
72 | #include <sys/systm.h> | |
73 | #include <sys/ioctl.h> | |
91447636 | 74 | #include <sys/file_internal.h> |
1c79356b A |
75 | #include <sys/errno.h> |
76 | #include <sys/malloc.h> | |
77 | #include <miscfs/fifofs/fifo.h> | |
78 | #include <vfs/vfs_support.h> | |
79 | ||
1c79356b A |
80 | #define VOPFUNC int (*)(void *) |
81 | ||
0a7de745 | 82 | int(**fifo_vnodeop_p)(void *); |
cb323159 A |
83 | const struct vnodeopv_entry_desc fifo_vnodeop_entries[] = { |
84 | { .opve_op = &vnop_default_desc, .opve_impl = (VOPFUNC)vn_default_error }, | |
85 | { .opve_op = &vnop_lookup_desc, .opve_impl = (VOPFUNC)fifo_lookup }, /* lookup */ | |
86 | { .opve_op = &vnop_create_desc, .opve_impl = (VOPFUNC)err_create }, /* create */ | |
87 | { .opve_op = &vnop_mknod_desc, .opve_impl = (VOPFUNC)err_mknod }, /* mknod */ | |
88 | { .opve_op = &vnop_open_desc, .opve_impl = (VOPFUNC)fifo_open }, /* open */ | |
89 | { .opve_op = &vnop_close_desc, .opve_impl = (VOPFUNC)fifo_close }, /* close */ | |
90 | { .opve_op = &vnop_access_desc, .opve_impl = (VOPFUNC)fifo_access }, /* access */ | |
91 | { .opve_op = &vnop_getattr_desc, .opve_impl = (VOPFUNC)fifo_getattr }, /* getattr */ | |
92 | { .opve_op = &vnop_setattr_desc, .opve_impl = (VOPFUNC)fifo_setattr }, /* setattr */ | |
93 | { .opve_op = &vnop_read_desc, .opve_impl = (VOPFUNC)fifo_read }, /* read */ | |
94 | { .opve_op = &vnop_write_desc, .opve_impl = (VOPFUNC)fifo_write }, /* write */ | |
95 | { .opve_op = &vnop_ioctl_desc, .opve_impl = (VOPFUNC)fifo_ioctl }, /* ioctl */ | |
96 | { .opve_op = &vnop_select_desc, .opve_impl = (VOPFUNC)fifo_select }, /* select */ | |
97 | { .opve_op = &vnop_revoke_desc, .opve_impl = (VOPFUNC)fifo_revoke }, /* revoke */ | |
98 | { .opve_op = &vnop_mmap_desc, .opve_impl = (VOPFUNC)err_mmap }, /* mmap */ | |
99 | { .opve_op = &vnop_fsync_desc, .opve_impl = (VOPFUNC)fifo_fsync }, /* fsync */ | |
100 | { .opve_op = &vnop_remove_desc, .opve_impl = (VOPFUNC)err_remove }, /* remove */ | |
101 | { .opve_op = &vnop_link_desc, .opve_impl = (VOPFUNC)err_link }, /* link */ | |
102 | { .opve_op = &vnop_rename_desc, .opve_impl = (VOPFUNC)err_rename }, /* rename */ | |
103 | { .opve_op = &vnop_mkdir_desc, .opve_impl = (VOPFUNC)err_mkdir }, /* mkdir */ | |
104 | { .opve_op = &vnop_rmdir_desc, .opve_impl = (VOPFUNC)err_rmdir }, /* rmdir */ | |
105 | { .opve_op = &vnop_symlink_desc, .opve_impl = (VOPFUNC)err_symlink }, /* symlink */ | |
106 | { .opve_op = &vnop_readdir_desc, .opve_impl = (VOPFUNC)err_readdir }, /* readdir */ | |
107 | { .opve_op = &vnop_readlink_desc, .opve_impl = (VOPFUNC)err_readlink }, /* readlink */ | |
108 | { .opve_op = &vnop_inactive_desc, .opve_impl = (VOPFUNC)fifo_inactive }, /* inactive */ | |
109 | { .opve_op = &vnop_reclaim_desc, .opve_impl = (VOPFUNC)fifo_reclaim }, /* reclaim */ | |
110 | { .opve_op = &vnop_strategy_desc, .opve_impl = (VOPFUNC)err_strategy }, /* strategy */ | |
111 | { .opve_op = &vnop_pathconf_desc, .opve_impl = (VOPFUNC)fifo_pathconf }, /* pathconf */ | |
112 | { .opve_op = &vnop_advlock_desc, .opve_impl = (VOPFUNC)fifo_advlock }, /* advlock */ | |
113 | { .opve_op = &vnop_bwrite_desc, .opve_impl = (VOPFUNC)fifo_bwrite }, /* bwrite */ | |
114 | { .opve_op = &vnop_pagein_desc, .opve_impl = (VOPFUNC)err_pagein }, /* Pagein */ | |
115 | { .opve_op = &vnop_pageout_desc, .opve_impl = (VOPFUNC)err_pageout }, /* Pageout */ | |
116 | { .opve_op = &vnop_copyfile_desc, .opve_impl = (VOPFUNC)err_copyfile }, /* Copyfile */ | |
117 | { .opve_op = &vnop_blktooff_desc, .opve_impl = (VOPFUNC)err_blktooff }, /* blktooff */ | |
118 | { .opve_op = &vnop_offtoblk_desc, .opve_impl = (VOPFUNC)err_offtoblk }, /* offtoblk */ | |
119 | { .opve_op = &vnop_blockmap_desc, .opve_impl = (VOPFUNC)err_blockmap }, /* blockmap */ | |
120 | { .opve_op = (struct vnodeop_desc*)NULL, .opve_impl = (int (*)(void *))NULL } | |
1c79356b | 121 | }; |
cb323159 A |
122 | const struct vnodeopv_desc fifo_vnodeop_opv_desc = |
123 | { .opv_desc_vector_p = &fifo_vnodeop_p, .opv_desc_ops = fifo_vnodeop_entries }; | |
1c79356b A |
124 | |
125 | /* | |
126 | * Trivial lookup routine that always fails. | |
127 | */ | |
128 | /* ARGSUSED */ | |
91447636 | 129 | int |
2d21ac55 | 130 | fifo_lookup(struct vnop_lookup_args *ap) |
1c79356b | 131 | { |
1c79356b | 132 | *ap->a_vpp = NULL; |
0a7de745 | 133 | return ENOTDIR; |
1c79356b A |
134 | } |
135 | ||
136 | /* | |
137 | * Open called to set up a new instance of a fifo or | |
138 | * to find an active instance of a fifo. | |
139 | */ | |
140 | /* ARGSUSED */ | |
91447636 | 141 | int |
2d21ac55 | 142 | fifo_open(struct vnop_open_args *ap) |
1c79356b A |
143 | { |
144 | struct vnode *vp = ap->a_vp; | |
145 | struct fifoinfo *fip; | |
1c79356b A |
146 | struct socket *rso, *wso; |
147 | int error; | |
148 | ||
91447636 A |
149 | vnode_lock(vp); |
150 | ||
151 | retry: | |
152 | ||
153 | fip = vp->v_fifoinfo; | |
154 | ||
0a7de745 | 155 | if (fip == (struct fifoinfo *)0) { |
91447636 | 156 | panic("fifo_open with no fifoinfo"); |
0a7de745 | 157 | } |
91447636 A |
158 | |
159 | if ((fip->fi_flags & FIFO_CREATED) == 0) { | |
160 | if (fip->fi_flags & FIFO_INCREATE) { | |
0a7de745 | 161 | fip->fi_flags |= FIFO_CREATEWAIT; |
2d21ac55 | 162 | error = msleep(&fip->fi_flags, &vp->v_lock, PRIBIO | PCATCH, "fifocreatewait", NULL); |
91447636 A |
163 | if (error) { |
164 | vnode_unlock(vp); | |
0a7de745 | 165 | return error; |
91447636 A |
166 | } |
167 | goto retry; | |
168 | } else { | |
0a7de745 | 169 | fip->fi_flags |= FIFO_INCREATE; |
91447636 | 170 | vnode_unlock(vp); |
0a7de745 A |
171 | if ((error = socreate(AF_LOCAL, &rso, SOCK_STREAM, 0))) { |
172 | goto bad1; | |
91447636 | 173 | } |
91447636 | 174 | |
0a7de745 | 175 | if ((error = socreate(AF_LOCAL, &wso, SOCK_STREAM, 0))) { |
91447636 | 176 | (void)soclose(rso); |
0a7de745 | 177 | goto bad1; |
91447636 | 178 | } |
91447636 | 179 | |
0a7de745 | 180 | if ((error = soconnect2(wso, rso))) { |
91447636 A |
181 | (void)soclose(wso); |
182 | (void)soclose(rso); | |
0a7de745 | 183 | goto bad1; |
91447636 A |
184 | } |
185 | fip->fi_readers = fip->fi_writers = 0; | |
186 | ||
0a7de745 A |
187 | /* Lock ordering between wso and rso does not matter here |
188 | * because they are just created and no one has a reference to them | |
189 | */ | |
190 | socket_lock(wso, 1); | |
91447636 A |
191 | wso->so_state |= SS_CANTRCVMORE; |
192 | wso->so_snd.sb_lowat = PIPE_BUF; | |
0a7de745 | 193 | socket_unlock(wso, 1); |
91447636 | 194 | |
0a7de745 | 195 | socket_lock(rso, 1); |
91447636 | 196 | rso->so_state |= SS_CANTSENDMORE; |
0a7de745 | 197 | socket_unlock(rso, 1); |
91447636 A |
198 | |
199 | vnode_lock(vp); | |
b0d623f7 A |
200 | fip->fi_readsock = rso; |
201 | fip->fi_writesock = wso; | |
202 | ||
91447636 A |
203 | fip->fi_flags |= FIFO_CREATED; |
204 | fip->fi_flags &= ~FIFO_INCREATE; | |
0a7de745 | 205 | |
91447636 A |
206 | if ((fip->fi_flags & FIFO_CREATEWAIT)) { |
207 | fip->fi_flags &= ~FIFO_CREATEWAIT; | |
208 | wakeup(&fip->fi_flags); | |
209 | } | |
210 | /* vnode lock is held to process further */ | |
1c79356b | 211 | } |
1c79356b | 212 | } |
91447636 A |
213 | |
214 | /* vnode is locked at this point */ | |
215 | /* fifo in created already */ | |
1c79356b A |
216 | if (ap->a_mode & FREAD) { |
217 | fip->fi_readers++; | |
218 | if (fip->fi_readers == 1) { | |
91447636 | 219 | socket_lock(fip->fi_writesock, 1); |
1c79356b | 220 | fip->fi_writesock->so_state &= ~SS_CANTSENDMORE; |
91447636 A |
221 | socket_unlock(fip->fi_writesock, 1); |
222 | ||
0a7de745 | 223 | if (fip->fi_writers > 0) { |
1c79356b | 224 | wakeup((caddr_t)&fip->fi_writers); |
0a7de745 | 225 | } |
1c79356b A |
226 | } |
227 | } | |
228 | if (ap->a_mode & FWRITE) { | |
229 | fip->fi_writers++; | |
230 | if (fip->fi_writers == 1) { | |
91447636 | 231 | socket_lock(fip->fi_readsock, 1); |
1c79356b | 232 | fip->fi_readsock->so_state &= ~SS_CANTRCVMORE; |
91447636 | 233 | socket_unlock(fip->fi_readsock, 1); |
0a7de745 A |
234 | |
235 | if (fip->fi_readers > 0) { | |
1c79356b | 236 | wakeup((caddr_t)&fip->fi_readers); |
0a7de745 | 237 | } |
1c79356b A |
238 | } |
239 | } | |
240 | if ((ap->a_mode & FREAD) && (ap->a_mode & O_NONBLOCK) == 0) { | |
241 | if (fip->fi_writers == 0) { | |
91447636 | 242 | error = msleep((caddr_t)&fip->fi_readers, &vp->v_lock, |
0a7de745 A |
243 | PCATCH | PSOCK, "fifoor", NULL); |
244 | if (error) { | |
1c79356b | 245 | goto bad; |
0a7de745 | 246 | } |
1c79356b | 247 | if (fip->fi_readers == 1) { |
0a7de745 | 248 | if (fip->fi_writers > 0) { |
1c79356b | 249 | wakeup((caddr_t)&fip->fi_writers); |
0a7de745 | 250 | } |
1c79356b A |
251 | } |
252 | } | |
253 | } | |
254 | if (ap->a_mode & FWRITE) { | |
255 | if (ap->a_mode & O_NONBLOCK) { | |
256 | if (fip->fi_readers == 0) { | |
0a7de745 A |
257 | error = ENXIO; |
258 | goto bad; | |
1c79356b A |
259 | } |
260 | } else { | |
261 | if (fip->fi_readers == 0) { | |
0a7de745 A |
262 | error = msleep((caddr_t)&fip->fi_writers, &vp->v_lock, |
263 | PCATCH | PSOCK, "fifoow", NULL); | |
264 | if (error) { | |
1c79356b | 265 | goto bad; |
0a7de745 | 266 | } |
1c79356b | 267 | if (fip->fi_writers == 1) { |
0a7de745 | 268 | if (fip->fi_readers > 0) { |
1c79356b | 269 | wakeup((caddr_t)&fip->fi_readers); |
0a7de745 | 270 | } |
1c79356b A |
271 | } |
272 | } | |
273 | } | |
274 | } | |
91447636 A |
275 | |
276 | vnode_unlock(vp); | |
0a7de745 | 277 | return 0; |
1c79356b | 278 | bad: |
91447636 A |
279 | fifo_close_internal(vp, ap->a_mode, ap->a_context, 1); |
280 | ||
281 | vnode_unlock(vp); | |
0a7de745 | 282 | return error; |
91447636 A |
283 | bad1: |
284 | vnode_lock(vp); | |
285 | ||
286 | fip->fi_flags &= ~FIFO_INCREATE; | |
0a7de745 | 287 | |
91447636 A |
288 | if ((fip->fi_flags & FIFO_CREATEWAIT)) { |
289 | fip->fi_flags &= ~FIFO_CREATEWAIT; | |
290 | wakeup(&fip->fi_flags); | |
291 | } | |
292 | vnode_unlock(vp); | |
293 | ||
0a7de745 | 294 | return error; |
1c79356b A |
295 | } |
296 | ||
297 | /* | |
298 | * Vnode op for read | |
299 | */ | |
91447636 | 300 | int |
2d21ac55 | 301 | fifo_read(struct vnop_read_args *ap) |
1c79356b A |
302 | { |
303 | struct uio *uio = ap->a_uio; | |
304 | struct socket *rso = ap->a_vp->v_fifoinfo->fi_readsock; | |
b0d623f7 A |
305 | user_ssize_t startresid; |
306 | int error; | |
91447636 | 307 | int rflags; |
1c79356b A |
308 | |
309 | #if DIAGNOSTIC | |
0a7de745 | 310 | if (uio->uio_rw != UIO_READ) { |
1c79356b | 311 | panic("fifo_read mode"); |
0a7de745 | 312 | } |
1c79356b | 313 | #endif |
0a7de745 A |
314 | if (uio_resid(uio) == 0) { |
315 | return 0; | |
316 | } | |
91447636 A |
317 | |
318 | rflags = (ap->a_ioflag & IO_NDELAY) ? MSG_NBIO : 0; | |
319 | ||
91447636 A |
320 | startresid = uio_resid(uio); |
321 | ||
0a7de745 A |
322 | /* fifo conformance - if we have a reader open on the fifo but no |
323 | * writers then we need to make sure we do not block. We do that by | |
5353443c A |
324 | * checking the receive buffer and if empty set error to EWOULDBLOCK. |
325 | * If error is set to EWOULDBLOCK we skip the call into soreceive | |
326 | */ | |
327 | error = 0; | |
328 | if (ap->a_vp->v_fifoinfo->fi_writers < 1) { | |
91447636 A |
329 | socket_lock(rso, 1); |
330 | error = (rso->so_rcv.sb_cc == 0) ? EWOULDBLOCK : 0; | |
331 | socket_unlock(rso, 1); | |
5353443c A |
332 | } |
333 | ||
334 | /* skip soreceive to avoid blocking when we have no writers */ | |
335 | if (error != EWOULDBLOCK) { | |
5353443c | 336 | error = soreceive(rso, (struct sockaddr **)0, uio, (struct mbuf **)0, |
0a7de745 A |
337 | (struct mbuf **)0, &rflags); |
338 | if (error == 0) { | |
39037602 | 339 | lock_vnode_and_post(ap->a_vp, 0); |
0a7de745 A |
340 | } |
341 | } else { | |
5353443c A |
342 | /* clear EWOULDBLOCK and return EOF (zero) */ |
343 | error = 0; | |
344 | } | |
1c79356b A |
345 | /* |
346 | * Clear EOF indication after first such return. | |
347 | */ | |
91447636 A |
348 | if (uio_resid(uio) == startresid) { |
349 | socket_lock(rso, 1); | |
1c79356b | 350 | rso->so_state &= ~SS_CANTRCVMORE; |
91447636 A |
351 | socket_unlock(rso, 1); |
352 | } | |
0a7de745 | 353 | return error; |
1c79356b A |
354 | } |
355 | ||
356 | /* | |
357 | * Vnode op for write | |
358 | */ | |
91447636 | 359 | int |
2d21ac55 | 360 | fifo_write(struct vnop_write_args *ap) |
1c79356b A |
361 | { |
362 | struct socket *wso = ap->a_vp->v_fifoinfo->fi_writesock; | |
1c79356b A |
363 | int error; |
364 | ||
365 | #if DIAGNOSTIC | |
0a7de745 | 366 | if (ap->a_uio->uio_rw != UIO_WRITE) { |
1c79356b | 367 | panic("fifo_write mode"); |
0a7de745 | 368 | } |
1c79356b | 369 | #endif |
2d21ac55 | 370 | error = sosend(wso, (struct sockaddr *)0, ap->a_uio, NULL, |
0a7de745 A |
371 | (struct mbuf *)0, (ap->a_ioflag & IO_NDELAY) ? MSG_NBIO : 0); |
372 | if (error == 0) { | |
39037602 | 373 | lock_vnode_and_post(ap->a_vp, 0); |
0a7de745 | 374 | } |
91447636 | 375 | |
0a7de745 | 376 | return error; |
1c79356b A |
377 | } |
378 | ||
379 | /* | |
380 | * Device ioctl operation. | |
381 | */ | |
91447636 | 382 | int |
2d21ac55 | 383 | fifo_ioctl(struct vnop_ioctl_args *ap) |
1c79356b | 384 | { |
91447636 A |
385 | struct fileproc filetmp; |
386 | struct fileglob filefg; | |
1c79356b A |
387 | int error; |
388 | ||
0a7de745 A |
389 | if (ap->a_command == FIONBIO) { |
390 | return 0; | |
391 | } | |
91447636 A |
392 | bzero(&filetmp, sizeof(struct fileproc)); |
393 | filetmp.f_fglob = &filefg; | |
1c79356b | 394 | if (ap->a_fflag & FREAD) { |
91447636 | 395 | filetmp.f_fglob->fg_data = (caddr_t)ap->a_vp->v_fifoinfo->fi_readsock; |
2d21ac55 | 396 | error = soo_ioctl(&filetmp, ap->a_command, ap->a_data, ap->a_context); |
0a7de745 A |
397 | if (error) { |
398 | return error; | |
399 | } | |
1c79356b A |
400 | } |
401 | if (ap->a_fflag & FWRITE) { | |
91447636 | 402 | filetmp.f_fglob->fg_data = (caddr_t)ap->a_vp->v_fifoinfo->fi_writesock; |
2d21ac55 | 403 | error = soo_ioctl(&filetmp, ap->a_command, ap->a_data, ap->a_context); |
0a7de745 A |
404 | if (error) { |
405 | return error; | |
406 | } | |
1c79356b | 407 | } |
0a7de745 | 408 | return 0; |
1c79356b A |
409 | } |
410 | ||
91447636 | 411 | int |
2d21ac55 | 412 | fifo_select(struct vnop_select_args *ap) |
1c79356b | 413 | { |
91447636 A |
414 | struct fileproc filetmp; |
415 | struct fileglob filefg; | |
1c79356b A |
416 | int ready; |
417 | ||
91447636 A |
418 | bzero(&filetmp, sizeof(struct fileproc)); |
419 | filetmp.f_fglob = &filefg; | |
9bccf70c | 420 | if (ap->a_which & FREAD) { |
91447636 | 421 | filetmp.f_fglob->fg_data = (caddr_t)ap->a_vp->v_fifoinfo->fi_readsock; |
2d21ac55 | 422 | ready = soo_select(&filetmp, ap->a_which, ap->a_wql, ap->a_context); |
0a7de745 A |
423 | if (ready) { |
424 | return ready; | |
425 | } | |
1c79356b | 426 | } |
9bccf70c | 427 | if (ap->a_which & FWRITE) { |
91447636 | 428 | filetmp.f_fglob->fg_data = (caddr_t)ap->a_vp->v_fifoinfo->fi_writesock; |
2d21ac55 | 429 | ready = soo_select(&filetmp, ap->a_which, ap->a_wql, ap->a_context); |
0a7de745 A |
430 | if (ready) { |
431 | return ready; | |
432 | } | |
1c79356b | 433 | } |
0a7de745 | 434 | return 0; |
1c79356b A |
435 | } |
436 | ||
437 | int | |
91447636 | 438 | fifo_inactive(__unused struct vnop_inactive_args *ap) |
1c79356b | 439 | { |
0a7de745 | 440 | return 0; |
1c79356b A |
441 | } |
442 | ||
1c79356b A |
443 | |
444 | /* | |
445 | * Device close routine | |
446 | */ | |
91447636 | 447 | int |
2d21ac55 | 448 | fifo_close(struct vnop_close_args *ap) |
1c79356b | 449 | { |
91447636 A |
450 | return fifo_close_internal(ap->a_vp, ap->a_fflag, ap->a_context, 0); |
451 | } | |
452 | ||
453 | int | |
454 | fifo_close_internal(vnode_t vp, int fflag, __unused vfs_context_t context, int locked) | |
455 | { | |
2d21ac55 | 456 | struct fifoinfo *fip = vp->v_fifoinfo; |
1c79356b | 457 | int error1, error2; |
91447636 A |
458 | struct socket *rso; |
459 | struct socket *wso; | |
1c79356b | 460 | |
0a7de745 | 461 | if (!locked) { |
91447636 | 462 | vnode_lock(vp); |
0a7de745 | 463 | } |
91447636 A |
464 | |
465 | if ((fip->fi_flags & FIFO_CREATED) == 0) { | |
0a7de745 | 466 | if (!locked) { |
91447636 | 467 | vnode_unlock(vp); |
0a7de745 A |
468 | } |
469 | return 0; | |
91447636 | 470 | } |
0a7de745 | 471 | |
91447636 | 472 | if (fflag & FREAD) { |
1c79356b | 473 | fip->fi_readers--; |
0a7de745 | 474 | if (fip->fi_readers == 0) { |
91447636 | 475 | socket_lock(fip->fi_writesock, 1); |
1c79356b | 476 | socantsendmore(fip->fi_writesock); |
91447636 | 477 | socket_unlock(fip->fi_writesock, 1); |
1c79356b A |
478 | } |
479 | } | |
91447636 A |
480 | |
481 | if (fflag & FWRITE) { | |
1c79356b A |
482 | fip->fi_writers--; |
483 | if (fip->fi_writers == 0) { | |
91447636 | 484 | socket_lock(fip->fi_readsock, 1); |
1c79356b | 485 | socantrcvmore(fip->fi_readsock); |
91447636 | 486 | socket_unlock(fip->fi_readsock, 1); |
1c79356b A |
487 | } |
488 | } | |
91447636 A |
489 | #if 0 |
490 | if (vnode_isinuse_locked(vp, 0, 1)) { | |
0a7de745 | 491 | if (!locked) { |
91447636 | 492 | vnode_unlock(vp); |
0a7de745 A |
493 | } |
494 | return 0; | |
91447636 A |
495 | } |
496 | #endif | |
497 | ||
498 | if (fip->fi_writers || fip->fi_readers) { | |
0a7de745 | 499 | if (!locked) { |
91447636 | 500 | vnode_unlock(vp); |
0a7de745 A |
501 | } |
502 | return 0; | |
91447636 A |
503 | } |
504 | ||
505 | wso = fip->fi_writesock; | |
506 | rso = fip->fi_readsock; | |
2d21ac55 A |
507 | fip->fi_readsock = NULL; |
508 | fip->fi_writesock = NULL; | |
91447636 | 509 | fip->fi_flags &= ~FIFO_CREATED; |
0a7de745 | 510 | if (!locked) { |
91447636 | 511 | vnode_unlock(vp); |
0a7de745 | 512 | } |
91447636 A |
513 | error1 = soclose(rso); |
514 | error2 = soclose(wso); | |
515 | ||
0a7de745 A |
516 | if (error1) { |
517 | return error1; | |
518 | } | |
519 | return error2; | |
1c79356b A |
520 | } |
521 | ||
1c79356b A |
522 | /* |
523 | * Print out internal contents of a fifo vnode. | |
524 | */ | |
91447636 | 525 | void |
2d21ac55 | 526 | fifo_printinfo(struct vnode *vp) |
1c79356b | 527 | { |
2d21ac55 | 528 | struct fifoinfo *fip = vp->v_fifoinfo; |
1c79356b | 529 | |
2d21ac55 | 530 | printf(", fifo with %ld readers and %ld writers", |
0a7de745 | 531 | fip->fi_readers, fip->fi_writers); |
1c79356b A |
532 | } |
533 | ||
534 | /* | |
535 | * Return POSIX pathconf information applicable to fifo's. | |
536 | */ | |
91447636 | 537 | int |
2d21ac55 | 538 | fifo_pathconf(struct vnop_pathconf_args *ap) |
1c79356b | 539 | { |
1c79356b A |
540 | switch (ap->a_name) { |
541 | case _PC_LINK_MAX: | |
542 | *ap->a_retval = LINK_MAX; | |
0a7de745 | 543 | return 0; |
1c79356b A |
544 | case _PC_PIPE_BUF: |
545 | *ap->a_retval = PIPE_BUF; | |
0a7de745 | 546 | return 0; |
1c79356b | 547 | case _PC_CHOWN_RESTRICTED: |
0a7de745 A |
548 | *ap->a_retval = 200112; /* _POSIX_CHOWN_RESTRICTED */ |
549 | return 0; | |
1c79356b | 550 | default: |
0a7de745 | 551 | return EINVAL; |
1c79356b A |
552 | } |
553 | /* NOTREACHED */ | |
554 | } | |
555 | ||
556 | /* | |
557 | * Fifo failed operation | |
558 | */ | |
91447636 A |
559 | int |
560 | fifo_ebadf(__unused void *dummy) | |
1c79356b | 561 | { |
0a7de745 | 562 | return EBADF; |
1c79356b A |
563 | } |
564 | ||
565 | /* | |
566 | * Fifo advisory byte-level locks. | |
567 | */ | |
91447636 A |
568 | int |
569 | fifo_advlock(__unused struct vnop_advlock_args *ap) | |
1c79356b | 570 | { |
0a7de745 | 571 | return ENOTSUP; |
1c79356b A |
572 | } |
573 | ||
b0d623f7 A |
574 | |
575 | /* You'd certainly better have an iocount on the vnode! */ | |
576 | int | |
0a7de745 | 577 | fifo_freespace(struct vnode *vp, long *count) |
b0d623f7 A |
578 | { |
579 | struct socket *rsock; | |
580 | rsock = vp->v_fifoinfo->fi_readsock; | |
581 | socket_lock(rsock, 1); | |
582 | *count = sbspace(&rsock->so_rcv); | |
583 | socket_unlock(rsock, 1); | |
584 | return 0; | |
585 | } | |
586 | ||
587 | int | |
0a7de745 | 588 | fifo_charcount(struct vnode *vp, int *count) |
b0d623f7 A |
589 | { |
590 | int mcount; | |
591 | int err = sock_ioctl(vp->v_fifoinfo->fi_readsock, FIONREAD, (void*)&mcount); | |
592 | if (err == 0) { | |
593 | *count = mcount; | |
594 | } | |
595 | return err; | |
596 | } |