]>
Commit | Line | Data |
---|---|---|
1c79356b | 1 | /* |
2d21ac55 | 2 | * Copyright (c) 2000-2006 Apple Computer, Inc. All rights reserved. |
5d5c5d0d | 3 | * |
2d21ac55 | 4 | * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ |
1c79356b | 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. | |
8f6c56a5 | 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. | |
17 | * | |
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. | |
8f6c56a5 | 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 | ||
82 | int (**fifo_vnodeop_p)(void *); | |
83 | struct vnodeopv_entry_desc fifo_vnodeop_entries[] = { | |
91447636 A |
84 | { &vnop_default_desc, (VOPFUNC)vn_default_error }, |
85 | { &vnop_lookup_desc, (VOPFUNC)fifo_lookup }, /* lookup */ | |
86 | { &vnop_create_desc, (VOPFUNC)err_create }, /* create */ | |
87 | { &vnop_mknod_desc, (VOPFUNC)err_mknod }, /* mknod */ | |
88 | { &vnop_open_desc, (VOPFUNC)fifo_open }, /* open */ | |
89 | { &vnop_close_desc, (VOPFUNC)fifo_close }, /* close */ | |
90 | { &vnop_access_desc, (VOPFUNC)fifo_access }, /* access */ | |
91 | { &vnop_getattr_desc, (VOPFUNC)fifo_getattr }, /* getattr */ | |
92 | { &vnop_setattr_desc, (VOPFUNC)fifo_setattr }, /* setattr */ | |
93 | { &vnop_read_desc, (VOPFUNC)fifo_read }, /* read */ | |
94 | { &vnop_write_desc, (VOPFUNC)fifo_write }, /* write */ | |
95 | { &vnop_ioctl_desc, (VOPFUNC)fifo_ioctl }, /* ioctl */ | |
96 | { &vnop_select_desc, (VOPFUNC)fifo_select }, /* select */ | |
97 | { &vnop_revoke_desc, (VOPFUNC)fifo_revoke }, /* revoke */ | |
98 | { &vnop_mmap_desc, (VOPFUNC)err_mmap }, /* mmap */ | |
99 | { &vnop_fsync_desc, (VOPFUNC)fifo_fsync }, /* fsync */ | |
100 | { &vnop_remove_desc, (VOPFUNC)err_remove }, /* remove */ | |
101 | { &vnop_link_desc, (VOPFUNC)err_link }, /* link */ | |
102 | { &vnop_rename_desc, (VOPFUNC)err_rename }, /* rename */ | |
103 | { &vnop_mkdir_desc, (VOPFUNC)err_mkdir }, /* mkdir */ | |
104 | { &vnop_rmdir_desc, (VOPFUNC)err_rmdir }, /* rmdir */ | |
105 | { &vnop_symlink_desc, (VOPFUNC)err_symlink }, /* symlink */ | |
106 | { &vnop_readdir_desc, (VOPFUNC)err_readdir }, /* readdir */ | |
107 | { &vnop_readlink_desc, (VOPFUNC)err_readlink }, /* readlink */ | |
108 | { &vnop_inactive_desc, (VOPFUNC)fifo_inactive }, /* inactive */ | |
109 | { &vnop_reclaim_desc, (VOPFUNC)fifo_reclaim }, /* reclaim */ | |
110 | { &vnop_strategy_desc, (VOPFUNC)err_strategy }, /* strategy */ | |
111 | { &vnop_pathconf_desc, (VOPFUNC)fifo_pathconf }, /* pathconf */ | |
112 | { &vnop_advlock_desc, (VOPFUNC)fifo_advlock }, /* advlock */ | |
113 | { &vnop_bwrite_desc, (VOPFUNC)fifo_bwrite }, /* bwrite */ | |
114 | { &vnop_pagein_desc, (VOPFUNC)err_pagein }, /* Pagein */ | |
115 | { &vnop_pageout_desc, (VOPFUNC)err_pageout }, /* Pageout */ | |
116 | { &vnop_copyfile_desc, (VOPFUNC)err_copyfile }, /* Copyfile */ | |
117 | { &vnop_blktooff_desc, (VOPFUNC)err_blktooff }, /* blktooff */ | |
118 | { &vnop_offtoblk_desc, (VOPFUNC)err_offtoblk }, /* offtoblk */ | |
119 | { &vnop_blockmap_desc, (VOPFUNC)err_blockmap }, /* blockmap */ | |
5ba3f43e | 120 | { (struct vnodeop_desc*)NULL, (int(*)(void *))NULL } |
1c79356b A |
121 | }; |
122 | struct vnodeopv_desc fifo_vnodeop_opv_desc = | |
123 | { &fifo_vnodeop_p, fifo_vnodeop_entries }; | |
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 A |
131 | { |
132 | ||
133 | *ap->a_vpp = NULL; | |
134 | return (ENOTDIR); | |
135 | } | |
136 | ||
137 | /* | |
138 | * Open called to set up a new instance of a fifo or | |
139 | * to find an active instance of a fifo. | |
140 | */ | |
141 | /* ARGSUSED */ | |
91447636 | 142 | int |
2d21ac55 | 143 | fifo_open(struct vnop_open_args *ap) |
1c79356b A |
144 | { |
145 | struct vnode *vp = ap->a_vp; | |
146 | struct fifoinfo *fip; | |
1c79356b A |
147 | struct socket *rso, *wso; |
148 | int error; | |
149 | ||
91447636 A |
150 | vnode_lock(vp); |
151 | ||
152 | retry: | |
153 | ||
154 | fip = vp->v_fifoinfo; | |
155 | ||
156 | if (fip == (struct fifoinfo *)0) | |
157 | panic("fifo_open with no fifoinfo"); | |
158 | ||
159 | if ((fip->fi_flags & FIFO_CREATED) == 0) { | |
160 | if (fip->fi_flags & FIFO_INCREATE) { | |
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); | |
165 | return(error); | |
166 | } | |
167 | goto retry; | |
168 | } else { | |
169 | fip->fi_flags |= FIFO_INCREATE; | |
170 | vnode_unlock(vp); | |
171 | if ( (error = socreate(AF_LOCAL, &rso, SOCK_STREAM, 0)) ) { | |
172 | goto bad1; | |
173 | } | |
91447636 A |
174 | |
175 | if ( (error = socreate(AF_LOCAL, &wso, SOCK_STREAM, 0)) ) { | |
176 | (void)soclose(rso); | |
177 | goto bad1; | |
178 | } | |
91447636 A |
179 | |
180 | if ( (error = soconnect2(wso, rso)) ) { | |
181 | (void)soclose(wso); | |
182 | (void)soclose(rso); | |
183 | goto bad1; | |
184 | } | |
185 | fip->fi_readers = fip->fi_writers = 0; | |
186 | ||
b0d623f7 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; | |
b0d623f7 | 193 | socket_unlock(wso, 1); |
91447636 | 194 | |
b0d623f7 | 195 | socket_lock(rso, 1); |
91447636 | 196 | rso->so_state |= SS_CANTSENDMORE; |
b0d623f7 | 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; | |
205 | ||
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 | ||
1c79356b A |
223 | if (fip->fi_writers > 0) |
224 | wakeup((caddr_t)&fip->fi_writers); | |
225 | } | |
226 | } | |
227 | if (ap->a_mode & FWRITE) { | |
228 | fip->fi_writers++; | |
229 | if (fip->fi_writers == 1) { | |
91447636 | 230 | socket_lock(fip->fi_readsock, 1); |
1c79356b | 231 | fip->fi_readsock->so_state &= ~SS_CANTRCVMORE; |
91447636 A |
232 | socket_unlock(fip->fi_readsock, 1); |
233 | ||
1c79356b A |
234 | if (fip->fi_readers > 0) |
235 | wakeup((caddr_t)&fip->fi_readers); | |
236 | } | |
237 | } | |
238 | if ((ap->a_mode & FREAD) && (ap->a_mode & O_NONBLOCK) == 0) { | |
239 | if (fip->fi_writers == 0) { | |
91447636 | 240 | error = msleep((caddr_t)&fip->fi_readers, &vp->v_lock, |
2d21ac55 | 241 | PCATCH | PSOCK, "fifoor", NULL); |
1c79356b A |
242 | if (error) |
243 | goto bad; | |
244 | if (fip->fi_readers == 1) { | |
245 | if (fip->fi_writers > 0) | |
246 | wakeup((caddr_t)&fip->fi_writers); | |
247 | } | |
248 | } | |
249 | } | |
250 | if (ap->a_mode & FWRITE) { | |
251 | if (ap->a_mode & O_NONBLOCK) { | |
252 | if (fip->fi_readers == 0) { | |
91447636 A |
253 | error = ENXIO; |
254 | goto bad; | |
1c79356b A |
255 | } |
256 | } else { | |
257 | if (fip->fi_readers == 0) { | |
91447636 | 258 | error = msleep((caddr_t)&fip->fi_writers,&vp->v_lock, |
2d21ac55 | 259 | PCATCH | PSOCK, "fifoow", NULL); |
1c79356b A |
260 | if (error) |
261 | goto bad; | |
262 | if (fip->fi_writers == 1) { | |
263 | if (fip->fi_readers > 0) | |
264 | wakeup((caddr_t)&fip->fi_readers); | |
265 | } | |
266 | } | |
267 | } | |
268 | } | |
91447636 A |
269 | |
270 | vnode_unlock(vp); | |
1c79356b A |
271 | return (0); |
272 | bad: | |
91447636 A |
273 | fifo_close_internal(vp, ap->a_mode, ap->a_context, 1); |
274 | ||
275 | vnode_unlock(vp); | |
276 | return (error); | |
277 | bad1: | |
278 | vnode_lock(vp); | |
279 | ||
280 | fip->fi_flags &= ~FIFO_INCREATE; | |
281 | ||
282 | if ((fip->fi_flags & FIFO_CREATEWAIT)) { | |
283 | fip->fi_flags &= ~FIFO_CREATEWAIT; | |
284 | wakeup(&fip->fi_flags); | |
285 | } | |
286 | vnode_unlock(vp); | |
287 | ||
1c79356b A |
288 | return (error); |
289 | } | |
290 | ||
291 | /* | |
292 | * Vnode op for read | |
293 | */ | |
91447636 | 294 | int |
2d21ac55 | 295 | fifo_read(struct vnop_read_args *ap) |
1c79356b A |
296 | { |
297 | struct uio *uio = ap->a_uio; | |
298 | struct socket *rso = ap->a_vp->v_fifoinfo->fi_readsock; | |
b0d623f7 A |
299 | user_ssize_t startresid; |
300 | int error; | |
91447636 | 301 | int rflags; |
1c79356b A |
302 | |
303 | #if DIAGNOSTIC | |
304 | if (uio->uio_rw != UIO_READ) | |
305 | panic("fifo_read mode"); | |
306 | #endif | |
91447636 | 307 | if (uio_resid(uio) == 0) |
1c79356b | 308 | return (0); |
91447636 A |
309 | |
310 | rflags = (ap->a_ioflag & IO_NDELAY) ? MSG_NBIO : 0; | |
311 | ||
91447636 A |
312 | startresid = uio_resid(uio); |
313 | ||
5353443c A |
314 | /* fifo conformance - if we have a reader open on the fifo but no |
315 | * writers then we need to make sure we do not block. We do that by | |
316 | * checking the receive buffer and if empty set error to EWOULDBLOCK. | |
317 | * If error is set to EWOULDBLOCK we skip the call into soreceive | |
318 | */ | |
319 | error = 0; | |
320 | if (ap->a_vp->v_fifoinfo->fi_writers < 1) { | |
91447636 A |
321 | socket_lock(rso, 1); |
322 | error = (rso->so_rcv.sb_cc == 0) ? EWOULDBLOCK : 0; | |
323 | socket_unlock(rso, 1); | |
5353443c A |
324 | } |
325 | ||
326 | /* skip soreceive to avoid blocking when we have no writers */ | |
327 | if (error != EWOULDBLOCK) { | |
5353443c | 328 | error = soreceive(rso, (struct sockaddr **)0, uio, (struct mbuf **)0, |
39037602 A |
329 | (struct mbuf **)0, &rflags); |
330 | if (error == 0) | |
331 | lock_vnode_and_post(ap->a_vp, 0); | |
5353443c A |
332 | } |
333 | else { | |
334 | /* clear EWOULDBLOCK and return EOF (zero) */ | |
335 | error = 0; | |
336 | } | |
1c79356b A |
337 | /* |
338 | * Clear EOF indication after first such return. | |
339 | */ | |
91447636 A |
340 | if (uio_resid(uio) == startresid) { |
341 | socket_lock(rso, 1); | |
1c79356b | 342 | rso->so_state &= ~SS_CANTRCVMORE; |
91447636 A |
343 | socket_unlock(rso, 1); |
344 | } | |
1c79356b A |
345 | return (error); |
346 | } | |
347 | ||
348 | /* | |
349 | * Vnode op for write | |
350 | */ | |
91447636 | 351 | int |
2d21ac55 | 352 | fifo_write(struct vnop_write_args *ap) |
1c79356b A |
353 | { |
354 | struct socket *wso = ap->a_vp->v_fifoinfo->fi_writesock; | |
1c79356b A |
355 | int error; |
356 | ||
357 | #if DIAGNOSTIC | |
358 | if (ap->a_uio->uio_rw != UIO_WRITE) | |
359 | panic("fifo_write mode"); | |
360 | #endif | |
2d21ac55 | 361 | error = sosend(wso, (struct sockaddr *)0, ap->a_uio, NULL, |
91447636 | 362 | (struct mbuf *)0, (ap->a_ioflag & IO_NDELAY) ? MSG_NBIO : 0); |
39037602 A |
363 | if (error == 0) |
364 | lock_vnode_and_post(ap->a_vp, 0); | |
91447636 | 365 | |
1c79356b A |
366 | return (error); |
367 | } | |
368 | ||
369 | /* | |
370 | * Device ioctl operation. | |
371 | */ | |
91447636 | 372 | int |
2d21ac55 | 373 | fifo_ioctl(struct vnop_ioctl_args *ap) |
1c79356b | 374 | { |
91447636 A |
375 | struct fileproc filetmp; |
376 | struct fileglob filefg; | |
1c79356b A |
377 | int error; |
378 | ||
379 | if (ap->a_command == FIONBIO) | |
380 | return (0); | |
91447636 A |
381 | bzero(&filetmp, sizeof(struct fileproc)); |
382 | filetmp.f_fglob = &filefg; | |
1c79356b | 383 | if (ap->a_fflag & FREAD) { |
91447636 | 384 | filetmp.f_fglob->fg_data = (caddr_t)ap->a_vp->v_fifoinfo->fi_readsock; |
2d21ac55 | 385 | error = soo_ioctl(&filetmp, ap->a_command, ap->a_data, ap->a_context); |
1c79356b A |
386 | if (error) |
387 | return (error); | |
388 | } | |
389 | if (ap->a_fflag & FWRITE) { | |
91447636 | 390 | filetmp.f_fglob->fg_data = (caddr_t)ap->a_vp->v_fifoinfo->fi_writesock; |
2d21ac55 | 391 | error = soo_ioctl(&filetmp, ap->a_command, ap->a_data, ap->a_context); |
1c79356b A |
392 | if (error) |
393 | return (error); | |
394 | } | |
395 | return (0); | |
396 | } | |
397 | ||
91447636 | 398 | int |
2d21ac55 | 399 | fifo_select(struct vnop_select_args *ap) |
1c79356b | 400 | { |
91447636 A |
401 | struct fileproc filetmp; |
402 | struct fileglob filefg; | |
1c79356b A |
403 | int ready; |
404 | ||
91447636 A |
405 | bzero(&filetmp, sizeof(struct fileproc)); |
406 | filetmp.f_fglob = &filefg; | |
9bccf70c | 407 | if (ap->a_which & FREAD) { |
91447636 | 408 | filetmp.f_fglob->fg_data = (caddr_t)ap->a_vp->v_fifoinfo->fi_readsock; |
2d21ac55 | 409 | ready = soo_select(&filetmp, ap->a_which, ap->a_wql, ap->a_context); |
1c79356b A |
410 | if (ready) |
411 | return (ready); | |
412 | } | |
9bccf70c | 413 | if (ap->a_which & FWRITE) { |
91447636 | 414 | filetmp.f_fglob->fg_data = (caddr_t)ap->a_vp->v_fifoinfo->fi_writesock; |
2d21ac55 | 415 | ready = soo_select(&filetmp, ap->a_which, ap->a_wql, ap->a_context); |
1c79356b A |
416 | if (ready) |
417 | return (ready); | |
418 | } | |
419 | return (0); | |
420 | } | |
421 | ||
422 | int | |
91447636 | 423 | fifo_inactive(__unused struct vnop_inactive_args *ap) |
1c79356b | 424 | { |
1c79356b A |
425 | return (0); |
426 | } | |
427 | ||
1c79356b A |
428 | |
429 | /* | |
430 | * Device close routine | |
431 | */ | |
91447636 | 432 | int |
2d21ac55 | 433 | fifo_close(struct vnop_close_args *ap) |
1c79356b | 434 | { |
91447636 A |
435 | return fifo_close_internal(ap->a_vp, ap->a_fflag, ap->a_context, 0); |
436 | } | |
437 | ||
438 | int | |
439 | fifo_close_internal(vnode_t vp, int fflag, __unused vfs_context_t context, int locked) | |
440 | { | |
2d21ac55 | 441 | struct fifoinfo *fip = vp->v_fifoinfo; |
1c79356b | 442 | int error1, error2; |
91447636 A |
443 | struct socket *rso; |
444 | struct socket *wso; | |
1c79356b | 445 | |
91447636 A |
446 | if (!locked) |
447 | vnode_lock(vp); | |
448 | ||
449 | if ((fip->fi_flags & FIFO_CREATED) == 0) { | |
450 | if (!locked) | |
451 | vnode_unlock(vp); | |
452 | return(0); | |
453 | ||
454 | } | |
455 | ||
456 | if (fflag & FREAD) { | |
1c79356b A |
457 | fip->fi_readers--; |
458 | if (fip->fi_readers == 0){ | |
91447636 | 459 | socket_lock(fip->fi_writesock, 1); |
1c79356b | 460 | socantsendmore(fip->fi_writesock); |
91447636 | 461 | socket_unlock(fip->fi_writesock, 1); |
1c79356b A |
462 | } |
463 | } | |
91447636 A |
464 | |
465 | if (fflag & FWRITE) { | |
1c79356b A |
466 | fip->fi_writers--; |
467 | if (fip->fi_writers == 0) { | |
91447636 | 468 | socket_lock(fip->fi_readsock, 1); |
1c79356b | 469 | socantrcvmore(fip->fi_readsock); |
91447636 | 470 | socket_unlock(fip->fi_readsock, 1); |
1c79356b A |
471 | } |
472 | } | |
91447636 A |
473 | #if 0 |
474 | if (vnode_isinuse_locked(vp, 0, 1)) { | |
475 | if (!locked) | |
476 | vnode_unlock(vp); | |
477 | return (0); | |
478 | } | |
479 | #endif | |
480 | ||
481 | if (fip->fi_writers || fip->fi_readers) { | |
482 | if (!locked) | |
483 | vnode_unlock(vp); | |
1c79356b | 484 | return (0); |
91447636 A |
485 | } |
486 | ||
487 | wso = fip->fi_writesock; | |
488 | rso = fip->fi_readsock; | |
2d21ac55 A |
489 | fip->fi_readsock = NULL; |
490 | fip->fi_writesock = NULL; | |
91447636 A |
491 | fip->fi_flags &= ~FIFO_CREATED; |
492 | if (!locked) | |
493 | vnode_unlock(vp); | |
494 | error1 = soclose(rso); | |
495 | error2 = soclose(wso); | |
496 | ||
1c79356b A |
497 | if (error1) |
498 | return (error1); | |
499 | return (error2); | |
500 | } | |
501 | ||
1c79356b A |
502 | /* |
503 | * Print out internal contents of a fifo vnode. | |
504 | */ | |
91447636 | 505 | void |
2d21ac55 | 506 | fifo_printinfo(struct vnode *vp) |
1c79356b | 507 | { |
2d21ac55 | 508 | struct fifoinfo *fip = vp->v_fifoinfo; |
1c79356b | 509 | |
2d21ac55 | 510 | printf(", fifo with %ld readers and %ld writers", |
1c79356b A |
511 | fip->fi_readers, fip->fi_writers); |
512 | } | |
513 | ||
514 | /* | |
515 | * Return POSIX pathconf information applicable to fifo's. | |
516 | */ | |
91447636 | 517 | int |
2d21ac55 | 518 | fifo_pathconf(struct vnop_pathconf_args *ap) |
1c79356b | 519 | { |
1c79356b A |
520 | switch (ap->a_name) { |
521 | case _PC_LINK_MAX: | |
522 | *ap->a_retval = LINK_MAX; | |
523 | return (0); | |
524 | case _PC_PIPE_BUF: | |
525 | *ap->a_retval = PIPE_BUF; | |
526 | return (0); | |
527 | case _PC_CHOWN_RESTRICTED: | |
2d21ac55 | 528 | *ap->a_retval = 200112; /* _POSIX_CHOWN_RESTRICTED */ |
1c79356b A |
529 | return (0); |
530 | default: | |
531 | return (EINVAL); | |
532 | } | |
533 | /* NOTREACHED */ | |
534 | } | |
535 | ||
536 | /* | |
537 | * Fifo failed operation | |
538 | */ | |
91447636 A |
539 | int |
540 | fifo_ebadf(__unused void *dummy) | |
1c79356b A |
541 | { |
542 | ||
543 | return (EBADF); | |
544 | } | |
545 | ||
546 | /* | |
547 | * Fifo advisory byte-level locks. | |
548 | */ | |
91447636 A |
549 | int |
550 | fifo_advlock(__unused struct vnop_advlock_args *ap) | |
1c79356b A |
551 | { |
552 | ||
91447636 | 553 | return (ENOTSUP); |
1c79356b A |
554 | } |
555 | ||
b0d623f7 A |
556 | |
557 | /* You'd certainly better have an iocount on the vnode! */ | |
558 | int | |
559 | fifo_freespace(struct vnode *vp, long *count) | |
560 | { | |
561 | struct socket *rsock; | |
562 | rsock = vp->v_fifoinfo->fi_readsock; | |
563 | socket_lock(rsock, 1); | |
564 | *count = sbspace(&rsock->so_rcv); | |
565 | socket_unlock(rsock, 1); | |
566 | return 0; | |
567 | } | |
568 | ||
569 | int | |
570 | fifo_charcount(struct vnode *vp, int *count) | |
571 | { | |
572 | int mcount; | |
573 | int err = sock_ioctl(vp->v_fifoinfo->fi_readsock, FIONREAD, (void*)&mcount); | |
574 | if (err == 0) { | |
575 | *count = mcount; | |
576 | } | |
577 | return err; | |
578 | } |