2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_OSREFERENCE_HEADER_START@
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
10 * License may not be used to create, or enable the creation or
11 * redistribution of, unlawful or unlicensed copies of an Apple operating
12 * system, or to circumvent, violate, or enable the circumvention or
13 * violation of, any terms of an Apple operating system software license
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
20 * The Original Code and all software distributed under the License are
21 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
22 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
23 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
25 * Please see the License for the specific language governing rights and
26 * limitations under the License.
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
30 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
32 * Copyright (c) 1990, 1993, 1995
33 * The Regents of the University of California. All rights reserved.
35 * Redistribution and use in source and binary forms, with or without
36 * modification, are permitted provided that the following conditions
38 * 1. Redistributions of source code must retain the above copyright
39 * notice, this list of conditions and the following disclaimer.
40 * 2. Redistributions in binary form must reproduce the above copyright
41 * notice, this list of conditions and the following disclaimer in the
42 * documentation and/or other materials provided with the distribution.
43 * 3. All advertising materials mentioning features or use of this software
44 * must display the following acknowledgement:
45 * This product includes software developed by the University of
46 * California, Berkeley and its contributors.
47 * 4. Neither the name of the University nor the names of its contributors
48 * may be used to endorse or promote products derived from this software
49 * without specific prior written permission.
51 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
52 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
53 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
54 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
55 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
56 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
57 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
58 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
59 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
60 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
63 * @(#)fifo_vnops.c 8.4 (Berkeley) 8/10/94
66 #include <sys/param.h>
69 #include <sys/namei.h>
70 #include <sys/vnode_internal.h>
71 #include <sys/socket.h>
72 #include <sys/socketvar.h>
74 #include <sys/systm.h>
75 #include <sys/ioctl.h>
76 #include <sys/file_internal.h>
77 #include <sys/errno.h>
78 #include <sys/malloc.h>
79 #include <miscfs/fifofs/fifo.h>
80 #include <vfs/vfs_support.h>
82 #define VOPFUNC int (*)(void *)
84 extern int soo_ioctl(struct fileproc
*fp
, u_long cmd
, caddr_t data
, struct proc
*p
);
85 extern int soo_select(struct fileproc
*fp
, int which
, void * wql
, struct proc
*p
);
87 int (**fifo_vnodeop_p
)(void *);
88 struct vnodeopv_entry_desc fifo_vnodeop_entries
[] = {
89 { &vnop_default_desc
, (VOPFUNC
)vn_default_error
},
90 { &vnop_lookup_desc
, (VOPFUNC
)fifo_lookup
}, /* lookup */
91 { &vnop_create_desc
, (VOPFUNC
)err_create
}, /* create */
92 { &vnop_mknod_desc
, (VOPFUNC
)err_mknod
}, /* mknod */
93 { &vnop_open_desc
, (VOPFUNC
)fifo_open
}, /* open */
94 { &vnop_close_desc
, (VOPFUNC
)fifo_close
}, /* close */
95 { &vnop_access_desc
, (VOPFUNC
)fifo_access
}, /* access */
96 { &vnop_getattr_desc
, (VOPFUNC
)fifo_getattr
}, /* getattr */
97 { &vnop_setattr_desc
, (VOPFUNC
)fifo_setattr
}, /* setattr */
98 { &vnop_read_desc
, (VOPFUNC
)fifo_read
}, /* read */
99 { &vnop_write_desc
, (VOPFUNC
)fifo_write
}, /* write */
100 { &vnop_ioctl_desc
, (VOPFUNC
)fifo_ioctl
}, /* ioctl */
101 { &vnop_select_desc
, (VOPFUNC
)fifo_select
}, /* select */
102 { &vnop_revoke_desc
, (VOPFUNC
)fifo_revoke
}, /* revoke */
103 { &vnop_mmap_desc
, (VOPFUNC
)err_mmap
}, /* mmap */
104 { &vnop_fsync_desc
, (VOPFUNC
)fifo_fsync
}, /* fsync */
105 { &vnop_remove_desc
, (VOPFUNC
)err_remove
}, /* remove */
106 { &vnop_link_desc
, (VOPFUNC
)err_link
}, /* link */
107 { &vnop_rename_desc
, (VOPFUNC
)err_rename
}, /* rename */
108 { &vnop_mkdir_desc
, (VOPFUNC
)err_mkdir
}, /* mkdir */
109 { &vnop_rmdir_desc
, (VOPFUNC
)err_rmdir
}, /* rmdir */
110 { &vnop_symlink_desc
, (VOPFUNC
)err_symlink
}, /* symlink */
111 { &vnop_readdir_desc
, (VOPFUNC
)err_readdir
}, /* readdir */
112 { &vnop_readlink_desc
, (VOPFUNC
)err_readlink
}, /* readlink */
113 { &vnop_inactive_desc
, (VOPFUNC
)fifo_inactive
}, /* inactive */
114 { &vnop_reclaim_desc
, (VOPFUNC
)fifo_reclaim
}, /* reclaim */
115 { &vnop_strategy_desc
, (VOPFUNC
)err_strategy
}, /* strategy */
116 { &vnop_pathconf_desc
, (VOPFUNC
)fifo_pathconf
}, /* pathconf */
117 { &vnop_advlock_desc
, (VOPFUNC
)fifo_advlock
}, /* advlock */
118 { &vnop_bwrite_desc
, (VOPFUNC
)fifo_bwrite
}, /* bwrite */
119 { &vnop_pagein_desc
, (VOPFUNC
)err_pagein
}, /* Pagein */
120 { &vnop_pageout_desc
, (VOPFUNC
)err_pageout
}, /* Pageout */
121 { &vnop_copyfile_desc
, (VOPFUNC
)err_copyfile
}, /* Copyfile */
122 { &vnop_blktooff_desc
, (VOPFUNC
)err_blktooff
}, /* blktooff */
123 { &vnop_offtoblk_desc
, (VOPFUNC
)err_offtoblk
}, /* offtoblk */
124 { &vnop_blockmap_desc
, (VOPFUNC
)err_blockmap
}, /* blockmap */
125 { (struct vnodeop_desc
*)NULL
, (int(*)())NULL
}
127 struct vnodeopv_desc fifo_vnodeop_opv_desc
=
128 { &fifo_vnodeop_p
, fifo_vnodeop_entries
};
131 * Trivial lookup routine that always fails.
136 struct vnop_lookup_args
/* {
137 struct vnode * a_dvp;
138 struct vnode ** a_vpp;
139 struct componentname * a_cnp;
140 vfs_context_t a_context;
149 * Open called to set up a new instance of a fifo or
150 * to find an active instance of a fifo.
155 struct vnop_open_args
/* {
158 vfs_context_t a_context;
161 struct vnode
*vp
= ap
->a_vp
;
162 struct fifoinfo
*fip
;
163 struct socket
*rso
, *wso
;
170 fip
= vp
->v_fifoinfo
;
172 if (fip
== (struct fifoinfo
*)0)
173 panic("fifo_open with no fifoinfo");
175 if ((fip
->fi_flags
& FIFO_CREATED
) == 0) {
176 if (fip
->fi_flags
& FIFO_INCREATE
) {
177 fip
->fi_flags
|= FIFO_CREATEWAIT
;
178 error
= msleep(&fip
->fi_flags
, &vp
->v_lock
, PRIBIO
| PCATCH
, "fifocreatewait", 0);
185 fip
->fi_flags
|= FIFO_INCREATE
;
187 if ( (error
= socreate(AF_LOCAL
, &rso
, SOCK_STREAM
, 0)) ) {
190 fip
->fi_readsock
= rso
;
192 if ( (error
= socreate(AF_LOCAL
, &wso
, SOCK_STREAM
, 0)) ) {
196 fip
->fi_writesock
= wso
;
198 if ( (error
= soconnect2(wso
, rso
)) ) {
203 fip
->fi_readers
= fip
->fi_writers
= 0;
206 wso
->so_state
|= SS_CANTRCVMORE
;
207 wso
->so_snd
.sb_lowat
= PIPE_BUF
;
209 /* Because all the unp is protected by single mutex
210 * doing it in two step may actually cause problems
211 * as it opens up window between the drop and acquire
213 socket_unlock(wso
, 1);
217 rso
->so_state
|= SS_CANTSENDMORE
;
218 socket_unlock(wso
, 1);
221 fip
->fi_flags
|= FIFO_CREATED
;
222 fip
->fi_flags
&= ~FIFO_INCREATE
;
224 if ((fip
->fi_flags
& FIFO_CREATEWAIT
)) {
225 fip
->fi_flags
&= ~FIFO_CREATEWAIT
;
226 wakeup(&fip
->fi_flags
);
228 /* vnode lock is held to process further */
232 /* vnode is locked at this point */
233 /* fifo in created already */
234 if (ap
->a_mode
& FREAD
) {
236 if (fip
->fi_readers
== 1) {
237 socket_lock(fip
->fi_writesock
, 1);
238 fip
->fi_writesock
->so_state
&= ~SS_CANTSENDMORE
;
239 socket_unlock(fip
->fi_writesock
, 1);
241 if (fip
->fi_writers
> 0)
242 wakeup((caddr_t
)&fip
->fi_writers
);
245 if (ap
->a_mode
& FWRITE
) {
247 if (fip
->fi_writers
== 1) {
248 socket_lock(fip
->fi_readsock
, 1);
249 fip
->fi_readsock
->so_state
&= ~SS_CANTRCVMORE
;
250 socket_unlock(fip
->fi_readsock
, 1);
252 if (fip
->fi_readers
> 0)
253 wakeup((caddr_t
)&fip
->fi_readers
);
256 if ((ap
->a_mode
& FREAD
) && (ap
->a_mode
& O_NONBLOCK
) == 0) {
257 if (fip
->fi_writers
== 0) {
258 error
= msleep((caddr_t
)&fip
->fi_readers
, &vp
->v_lock
,
259 PCATCH
| PSOCK
, "fifoor", 0);
262 if (fip
->fi_readers
== 1) {
263 if (fip
->fi_writers
> 0)
264 wakeup((caddr_t
)&fip
->fi_writers
);
268 if (ap
->a_mode
& FWRITE
) {
269 if (ap
->a_mode
& O_NONBLOCK
) {
270 if (fip
->fi_readers
== 0) {
275 if (fip
->fi_readers
== 0) {
276 error
= msleep((caddr_t
)&fip
->fi_writers
,&vp
->v_lock
,
277 PCATCH
| PSOCK
, "fifoow", 0);
280 if (fip
->fi_writers
== 1) {
281 if (fip
->fi_readers
> 0)
282 wakeup((caddr_t
)&fip
->fi_readers
);
291 fifo_close_internal(vp
, ap
->a_mode
, ap
->a_context
, 1);
298 fip
->fi_flags
&= ~FIFO_INCREATE
;
300 if ((fip
->fi_flags
& FIFO_CREATEWAIT
)) {
301 fip
->fi_flags
&= ~FIFO_CREATEWAIT
;
302 wakeup(&fip
->fi_flags
);
314 struct vnop_read_args
/* {
318 vfs_context_t a_context;
321 struct uio
*uio
= ap
->a_uio
;
322 struct socket
*rso
= ap
->a_vp
->v_fifoinfo
->fi_readsock
;
323 int error
, startresid
;
327 if (uio
->uio_rw
!= UIO_READ
)
328 panic("fifo_read mode");
330 if (uio_resid(uio
) == 0)
333 rflags
= (ap
->a_ioflag
& IO_NDELAY
) ? MSG_NBIO
: 0;
335 // LP64todo - fix this!
336 startresid
= uio_resid(uio
);
338 /* fifo conformance - if we have a reader open on the fifo but no
339 * writers then we need to make sure we do not block. We do that by
340 * checking the receive buffer and if empty set error to EWOULDBLOCK.
341 * If error is set to EWOULDBLOCK we skip the call into soreceive
344 if (ap
->a_vp
->v_fifoinfo
->fi_writers
< 1) {
346 error
= (rso
->so_rcv
.sb_cc
== 0) ? EWOULDBLOCK
: 0;
347 socket_unlock(rso
, 1);
350 /* skip soreceive to avoid blocking when we have no writers */
351 if (error
!= EWOULDBLOCK
) {
352 error
= soreceive(rso
, (struct sockaddr
**)0, uio
, (struct mbuf
**)0,
353 (struct mbuf
**)0, &rflags
);
356 /* clear EWOULDBLOCK and return EOF (zero) */
360 * Clear EOF indication after first such return.
362 if (uio_resid(uio
) == startresid
) {
364 rso
->so_state
&= ~SS_CANTRCVMORE
;
365 socket_unlock(rso
, 1);
375 struct vnop_write_args
/* {
379 vfs_context_t a_context;
382 struct socket
*wso
= ap
->a_vp
->v_fifoinfo
->fi_writesock
;
386 if (ap
->a_uio
->uio_rw
!= UIO_WRITE
)
387 panic("fifo_write mode");
389 error
= sosend(wso
, (struct sockaddr
*)0, ap
->a_uio
, 0,
390 (struct mbuf
*)0, (ap
->a_ioflag
& IO_NDELAY
) ? MSG_NBIO
: 0);
396 * Device ioctl operation.
400 struct vnop_ioctl_args
/* {
405 vfs_context_t a_context;
408 struct proc
*p
= vfs_context_proc(ap
->a_context
);
409 struct fileproc filetmp
;
410 struct fileglob filefg
;
413 if (ap
->a_command
== FIONBIO
)
415 bzero(&filetmp
, sizeof(struct fileproc
));
416 filetmp
.f_fglob
= &filefg
;
417 if (ap
->a_fflag
& FREAD
) {
418 filetmp
.f_fglob
->fg_data
= (caddr_t
)ap
->a_vp
->v_fifoinfo
->fi_readsock
;
419 error
= soo_ioctl(&filetmp
, ap
->a_command
, ap
->a_data
, p
);
423 if (ap
->a_fflag
& FWRITE
) {
424 filetmp
.f_fglob
->fg_data
= (caddr_t
)ap
->a_vp
->v_fifoinfo
->fi_writesock
;
425 error
= soo_ioctl(&filetmp
, ap
->a_command
, ap
->a_data
, p
);
434 struct vnop_select_args
/* {
439 vfs_context_t a_context;
442 struct proc
*p
= vfs_context_proc(ap
->a_context
);
443 struct fileproc filetmp
;
444 struct fileglob filefg
;
447 bzero(&filetmp
, sizeof(struct fileproc
));
448 filetmp
.f_fglob
= &filefg
;
449 if (ap
->a_which
& FREAD
) {
450 filetmp
.f_fglob
->fg_data
= (caddr_t
)ap
->a_vp
->v_fifoinfo
->fi_readsock
;
451 ready
= soo_select(&filetmp
, ap
->a_which
, ap
->a_wql
, p
);
455 if (ap
->a_which
& FWRITE
) {
456 filetmp
.f_fglob
->fg_data
= (caddr_t
)ap
->a_vp
->v_fifoinfo
->fi_writesock
;
457 ready
= soo_select(&filetmp
, ap
->a_which
, ap
->a_wql
, p
);
465 fifo_inactive(__unused
struct vnop_inactive_args
*ap
)
472 * Device close routine
476 struct vnop_close_args
/* {
479 vfs_context_t a_context;
482 return fifo_close_internal(ap
->a_vp
, ap
->a_fflag
, ap
->a_context
, 0);
486 fifo_close_internal(vnode_t vp
, int fflag
, __unused vfs_context_t context
, int locked
)
488 register struct fifoinfo
*fip
= vp
->v_fifoinfo
;
496 if ((fip
->fi_flags
& FIFO_CREATED
) == 0) {
505 if (fip
->fi_readers
== 0){
506 socket_lock(fip
->fi_writesock
, 1);
507 socantsendmore(fip
->fi_writesock
);
508 socket_unlock(fip
->fi_writesock
, 1);
512 if (fflag
& FWRITE
) {
514 if (fip
->fi_writers
== 0) {
515 socket_lock(fip
->fi_readsock
, 1);
516 socantrcvmore(fip
->fi_readsock
);
517 socket_unlock(fip
->fi_readsock
, 1);
521 if (vnode_isinuse_locked(vp
, 0, 1)) {
528 if (fip
->fi_writers
|| fip
->fi_readers
) {
534 wso
= fip
->fi_writesock
;
535 rso
= fip
->fi_readsock
;
536 fip
->fi_readsock
= 0;
537 fip
->fi_writesock
= 0;
538 fip
->fi_flags
&= ~FIFO_CREATED
;
541 error1
= soclose(rso
);
542 error2
= soclose(wso
);
551 * Print out internal contents of a fifo vnode.
557 register struct fifoinfo
*fip
= vp
->v_fifoinfo
;
559 printf(", fifo with %d readers and %d writers",
560 fip
->fi_readers
, fip
->fi_writers
);
564 * Return POSIX pathconf information applicable to fifo's.
568 struct vnop_pathconf_args
/* {
572 vfs_context_t a_context;
576 switch (ap
->a_name
) {
578 *ap
->a_retval
= LINK_MAX
;
581 *ap
->a_retval
= PIPE_BUF
;
583 case _PC_CHOWN_RESTRICTED
:
593 * Fifo failed operation
596 fifo_ebadf(__unused
void *dummy
)
603 * Fifo advisory byte-level locks.
606 fifo_advlock(__unused
struct vnop_advlock_args
*ap
)