fifo_vnops.c revision 1.52 1 1.52 xtraeme /* $NetBSD: fifo_vnops.c,v 1.52 2005/08/30 20:08:01 xtraeme Exp $ */
2 1.9 cgd
3 1.1 cgd /*
4 1.25 fvdl * Copyright (c) 1990, 1993, 1995
5 1.8 mycroft * The Regents of the University of California. All rights reserved.
6 1.1 cgd *
7 1.1 cgd * Redistribution and use in source and binary forms, with or without
8 1.1 cgd * modification, are permitted provided that the following conditions
9 1.1 cgd * are met:
10 1.1 cgd * 1. Redistributions of source code must retain the above copyright
11 1.1 cgd * notice, this list of conditions and the following disclaimer.
12 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 cgd * notice, this list of conditions and the following disclaimer in the
14 1.1 cgd * documentation and/or other materials provided with the distribution.
15 1.42 agc * 3. Neither the name of the University nor the names of its contributors
16 1.1 cgd * may be used to endorse or promote products derived from this software
17 1.1 cgd * without specific prior written permission.
18 1.1 cgd *
19 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 1.1 cgd * SUCH DAMAGE.
30 1.1 cgd *
31 1.25 fvdl * @(#)fifo_vnops.c 8.10 (Berkeley) 5/27/95
32 1.1 cgd */
33 1.32 lukem
34 1.32 lukem #include <sys/cdefs.h>
35 1.52 xtraeme __KERNEL_RCSID(0, "$NetBSD: fifo_vnops.c,v 1.52 2005/08/30 20:08:01 xtraeme Exp $");
36 1.1 cgd
37 1.6 mycroft #include <sys/param.h>
38 1.25 fvdl #include <sys/systm.h>
39 1.8 mycroft #include <sys/proc.h>
40 1.6 mycroft #include <sys/time.h>
41 1.6 mycroft #include <sys/namei.h>
42 1.6 mycroft #include <sys/vnode.h>
43 1.6 mycroft #include <sys/socket.h>
44 1.45 matt #include <sys/protosw.h>
45 1.6 mycroft #include <sys/socketvar.h>
46 1.6 mycroft #include <sys/stat.h>
47 1.6 mycroft #include <sys/ioctl.h>
48 1.6 mycroft #include <sys/file.h>
49 1.6 mycroft #include <sys/errno.h>
50 1.6 mycroft #include <sys/malloc.h>
51 1.17 christos #include <sys/un.h>
52 1.20 mycroft #include <sys/poll.h>
53 1.36 jdolecek #include <sys/event.h>
54 1.19 mycroft
55 1.6 mycroft #include <miscfs/fifofs/fifo.h>
56 1.19 mycroft #include <miscfs/genfs/genfs.h>
57 1.1 cgd
58 1.1 cgd /*
59 1.1 cgd * This structure is associated with the FIFO vnode and stores
60 1.1 cgd * the state associated with the FIFO.
61 1.1 cgd */
62 1.1 cgd struct fifoinfo {
63 1.1 cgd struct socket *fi_readsock;
64 1.1 cgd struct socket *fi_writesock;
65 1.1 cgd long fi_readers;
66 1.1 cgd long fi_writers;
67 1.1 cgd };
68 1.1 cgd
69 1.52 xtraeme int (**fifo_vnodeop_p)(void *);
70 1.29 jdolecek const struct vnodeopv_entry_desc fifo_vnodeop_entries[] = {
71 1.8 mycroft { &vop_default_desc, vn_default_error },
72 1.8 mycroft { &vop_lookup_desc, fifo_lookup }, /* lookup */
73 1.8 mycroft { &vop_create_desc, fifo_create }, /* create */
74 1.8 mycroft { &vop_mknod_desc, fifo_mknod }, /* mknod */
75 1.8 mycroft { &vop_open_desc, fifo_open }, /* open */
76 1.8 mycroft { &vop_close_desc, fifo_close }, /* close */
77 1.8 mycroft { &vop_access_desc, fifo_access }, /* access */
78 1.8 mycroft { &vop_getattr_desc, fifo_getattr }, /* getattr */
79 1.8 mycroft { &vop_setattr_desc, fifo_setattr }, /* setattr */
80 1.8 mycroft { &vop_read_desc, fifo_read }, /* read */
81 1.8 mycroft { &vop_write_desc, fifo_write }, /* write */
82 1.13 mycroft { &vop_lease_desc, fifo_lease_check }, /* lease */
83 1.8 mycroft { &vop_ioctl_desc, fifo_ioctl }, /* ioctl */
84 1.20 mycroft { &vop_poll_desc, fifo_poll }, /* poll */
85 1.36 jdolecek { &vop_kqfilter_desc, fifo_kqfilter }, /* kqfilter */
86 1.25 fvdl { &vop_revoke_desc, fifo_revoke }, /* revoke */
87 1.8 mycroft { &vop_mmap_desc, fifo_mmap }, /* mmap */
88 1.8 mycroft { &vop_fsync_desc, fifo_fsync }, /* fsync */
89 1.8 mycroft { &vop_seek_desc, fifo_seek }, /* seek */
90 1.8 mycroft { &vop_remove_desc, fifo_remove }, /* remove */
91 1.8 mycroft { &vop_link_desc, fifo_link }, /* link */
92 1.8 mycroft { &vop_rename_desc, fifo_rename }, /* rename */
93 1.8 mycroft { &vop_mkdir_desc, fifo_mkdir }, /* mkdir */
94 1.8 mycroft { &vop_rmdir_desc, fifo_rmdir }, /* rmdir */
95 1.8 mycroft { &vop_symlink_desc, fifo_symlink }, /* symlink */
96 1.8 mycroft { &vop_readdir_desc, fifo_readdir }, /* readdir */
97 1.8 mycroft { &vop_readlink_desc, fifo_readlink }, /* readlink */
98 1.8 mycroft { &vop_abortop_desc, fifo_abortop }, /* abortop */
99 1.8 mycroft { &vop_inactive_desc, fifo_inactive }, /* inactive */
100 1.8 mycroft { &vop_reclaim_desc, fifo_reclaim }, /* reclaim */
101 1.8 mycroft { &vop_lock_desc, fifo_lock }, /* lock */
102 1.8 mycroft { &vop_unlock_desc, fifo_unlock }, /* unlock */
103 1.8 mycroft { &vop_bmap_desc, fifo_bmap }, /* bmap */
104 1.8 mycroft { &vop_strategy_desc, fifo_strategy }, /* strategy */
105 1.8 mycroft { &vop_print_desc, fifo_print }, /* print */
106 1.8 mycroft { &vop_islocked_desc, fifo_islocked }, /* islocked */
107 1.8 mycroft { &vop_pathconf_desc, fifo_pathconf }, /* pathconf */
108 1.8 mycroft { &vop_advlock_desc, fifo_advlock }, /* advlock */
109 1.8 mycroft { &vop_blkatoff_desc, fifo_blkatoff }, /* blkatoff */
110 1.8 mycroft { &vop_valloc_desc, fifo_valloc }, /* valloc */
111 1.8 mycroft { &vop_vfree_desc, fifo_vfree }, /* vfree */
112 1.8 mycroft { &vop_truncate_desc, fifo_truncate }, /* truncate */
113 1.8 mycroft { &vop_update_desc, fifo_update }, /* update */
114 1.8 mycroft { &vop_bwrite_desc, fifo_bwrite }, /* bwrite */
115 1.31 sommerfe { &vop_putpages_desc, fifo_putpages }, /* putpages */
116 1.52 xtraeme { (struct vnodeop_desc*)NULL, (int(*)(void *))NULL }
117 1.1 cgd };
118 1.29 jdolecek const struct vnodeopv_desc fifo_vnodeop_opv_desc =
119 1.8 mycroft { &fifo_vnodeop_p, fifo_vnodeop_entries };
120 1.1 cgd
121 1.1 cgd /*
122 1.1 cgd * Trivial lookup routine that always fails.
123 1.1 cgd */
124 1.1 cgd /* ARGSUSED */
125 1.17 christos int
126 1.30 lukem fifo_lookup(void *v)
127 1.17 christos {
128 1.8 mycroft struct vop_lookup_args /* {
129 1.30 lukem struct vnode *a_dvp;
130 1.30 lukem struct vnode **a_vpp;
131 1.30 lukem struct componentname *a_cnp;
132 1.17 christos } */ *ap = v;
133 1.51 perry
134 1.8 mycroft *ap->a_vpp = NULL;
135 1.1 cgd return (ENOTDIR);
136 1.1 cgd }
137 1.1 cgd
138 1.1 cgd /*
139 1.1 cgd * Open called to set up a new instance of a fifo or
140 1.1 cgd * to find an active instance of a fifo.
141 1.1 cgd */
142 1.1 cgd /* ARGSUSED */
143 1.17 christos int
144 1.30 lukem fifo_open(void *v)
145 1.17 christos {
146 1.8 mycroft struct vop_open_args /* {
147 1.30 lukem struct vnode *a_vp;
148 1.30 lukem int a_mode;
149 1.30 lukem struct ucred *a_cred;
150 1.41 fvdl struct proc *a_p;
151 1.17 christos } */ *ap = v;
152 1.30 lukem struct vnode *vp;
153 1.30 lukem struct fifoinfo *fip;
154 1.30 lukem struct proc *p;
155 1.30 lukem struct socket *rso, *wso;
156 1.30 lukem int error;
157 1.1 cgd
158 1.30 lukem vp = ap->a_vp;
159 1.41 fvdl p = ap->a_p;
160 1.30 lukem
161 1.1 cgd if ((fip = vp->v_fifoinfo) == NULL) {
162 1.1 cgd MALLOC(fip, struct fifoinfo *, sizeof(*fip), M_VNODE, M_WAITOK);
163 1.1 cgd vp->v_fifoinfo = fip;
164 1.49 jonathan error = socreate(AF_LOCAL, &rso, SOCK_STREAM, 0, p);
165 1.49 jonathan if (error != 0) {
166 1.1 cgd free(fip, M_VNODE);
167 1.1 cgd vp->v_fifoinfo = NULL;
168 1.1 cgd return (error);
169 1.1 cgd }
170 1.1 cgd fip->fi_readsock = rso;
171 1.49 jonathan error = socreate(AF_LOCAL, &wso, SOCK_STREAM, 0, p);
172 1.49 jonathan if (error != 0) {
173 1.1 cgd (void)soclose(rso);
174 1.1 cgd free(fip, M_VNODE);
175 1.1 cgd vp->v_fifoinfo = NULL;
176 1.1 cgd return (error);
177 1.1 cgd }
178 1.1 cgd fip->fi_writesock = wso;
179 1.45 matt if ((error = unp_connect2(wso, rso, PRU_CONNECT2)) != 0) {
180 1.1 cgd (void)soclose(wso);
181 1.1 cgd (void)soclose(rso);
182 1.1 cgd free(fip, M_VNODE);
183 1.1 cgd vp->v_fifoinfo = NULL;
184 1.1 cgd return (error);
185 1.1 cgd }
186 1.8 mycroft fip->fi_readers = fip->fi_writers = 0;
187 1.1 cgd wso->so_state |= SS_CANTRCVMORE;
188 1.1 cgd rso->so_state |= SS_CANTSENDMORE;
189 1.1 cgd }
190 1.8 mycroft if (ap->a_mode & FREAD) {
191 1.16 mycroft if (fip->fi_readers++ == 0) {
192 1.1 cgd fip->fi_writesock->so_state &= ~SS_CANTSENDMORE;
193 1.1 cgd if (fip->fi_writers > 0)
194 1.47 jrf wakeup(&fip->fi_writers);
195 1.1 cgd }
196 1.16 mycroft }
197 1.16 mycroft if (ap->a_mode & FWRITE) {
198 1.16 mycroft if (fip->fi_writers++ == 0) {
199 1.16 mycroft fip->fi_readsock->so_state &= ~SS_CANTRCVMORE;
200 1.16 mycroft if (fip->fi_readers > 0)
201 1.47 jrf wakeup(&fip->fi_readers);
202 1.2 cgd }
203 1.16 mycroft }
204 1.16 mycroft if (ap->a_mode & FREAD) {
205 1.16 mycroft if (ap->a_mode & O_NONBLOCK) {
206 1.1 cgd } else {
207 1.39 martin while (!soreadable(fip->fi_readsock) && fip->fi_writers == 0) {
208 1.25 fvdl VOP_UNLOCK(vp, 0);
209 1.47 jrf error = tsleep(&fip->fi_readers,
210 1.38 jdolecek PCATCH | PSOCK, "fifor", 0);
211 1.25 fvdl vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
212 1.16 mycroft if (error)
213 1.16 mycroft goto bad;
214 1.1 cgd }
215 1.16 mycroft }
216 1.16 mycroft }
217 1.16 mycroft if (ap->a_mode & FWRITE) {
218 1.16 mycroft if (ap->a_mode & O_NONBLOCK) {
219 1.16 mycroft if (fip->fi_readers == 0) {
220 1.16 mycroft error = ENXIO;
221 1.16 mycroft goto bad;
222 1.16 mycroft }
223 1.16 mycroft } else {
224 1.2 cgd while (fip->fi_readers == 0) {
225 1.25 fvdl VOP_UNLOCK(vp, 0);
226 1.47 jrf error = tsleep(&fip->fi_writers,
227 1.38 jdolecek PCATCH | PSOCK, "fifow", 0);
228 1.25 fvdl vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
229 1.8 mycroft if (error)
230 1.16 mycroft goto bad;
231 1.2 cgd }
232 1.1 cgd }
233 1.1 cgd }
234 1.16 mycroft return (0);
235 1.30 lukem bad:
236 1.41 fvdl VOP_CLOSE(vp, ap->a_mode, ap->a_cred, p);
237 1.1 cgd return (error);
238 1.1 cgd }
239 1.1 cgd
240 1.1 cgd /*
241 1.1 cgd * Vnode op for read
242 1.1 cgd */
243 1.1 cgd /* ARGSUSED */
244 1.17 christos int
245 1.30 lukem fifo_read(void *v)
246 1.17 christos {
247 1.8 mycroft struct vop_read_args /* {
248 1.30 lukem struct vnode *a_vp;
249 1.30 lukem struct uio *a_uio;
250 1.30 lukem int a_ioflag;
251 1.30 lukem struct ucred *a_cred;
252 1.17 christos } */ *ap = v;
253 1.30 lukem struct uio *uio;
254 1.30 lukem struct socket *rso;
255 1.35 thorpej int error;
256 1.35 thorpej size_t startresid;
257 1.1 cgd
258 1.30 lukem uio = ap->a_uio;
259 1.30 lukem rso = ap->a_vp->v_fifoinfo->fi_readsock;
260 1.1 cgd #ifdef DIAGNOSTIC
261 1.1 cgd if (uio->uio_rw != UIO_READ)
262 1.1 cgd panic("fifo_read mode");
263 1.1 cgd #endif
264 1.1 cgd if (uio->uio_resid == 0)
265 1.1 cgd return (0);
266 1.8 mycroft if (ap->a_ioflag & IO_NDELAY)
267 1.1 cgd rso->so_state |= SS_NBIO;
268 1.1 cgd startresid = uio->uio_resid;
269 1.25 fvdl VOP_UNLOCK(ap->a_vp, 0);
270 1.27 matt error = (*rso->so_receive)(rso, (struct mbuf **)0, uio,
271 1.27 matt (struct mbuf **)0, (struct mbuf **)0, (int *)0);
272 1.25 fvdl vn_lock(ap->a_vp, LK_EXCLUSIVE | LK_RETRY);
273 1.1 cgd /*
274 1.1 cgd * Clear EOF indication after first such return.
275 1.1 cgd */
276 1.1 cgd if (uio->uio_resid == startresid)
277 1.1 cgd rso->so_state &= ~SS_CANTRCVMORE;
278 1.23 kleink if (ap->a_ioflag & IO_NDELAY) {
279 1.1 cgd rso->so_state &= ~SS_NBIO;
280 1.23 kleink if (error == EWOULDBLOCK &&
281 1.23 kleink ap->a_vp->v_fifoinfo->fi_writers == 0)
282 1.23 kleink error = 0;
283 1.23 kleink }
284 1.1 cgd return (error);
285 1.1 cgd }
286 1.1 cgd
287 1.1 cgd /*
288 1.1 cgd * Vnode op for write
289 1.1 cgd */
290 1.1 cgd /* ARGSUSED */
291 1.17 christos int
292 1.30 lukem fifo_write(void *v)
293 1.17 christos {
294 1.8 mycroft struct vop_write_args /* {
295 1.30 lukem struct vnode *a_vp;
296 1.30 lukem struct uio *a_uio;
297 1.30 lukem int a_ioflag;
298 1.30 lukem struct ucred *a_cred;
299 1.17 christos } */ *ap = v;
300 1.30 lukem struct socket *wso;
301 1.30 lukem int error;
302 1.1 cgd
303 1.30 lukem wso = ap->a_vp->v_fifoinfo->fi_writesock;
304 1.1 cgd #ifdef DIAGNOSTIC
305 1.8 mycroft if (ap->a_uio->uio_rw != UIO_WRITE)
306 1.1 cgd panic("fifo_write mode");
307 1.1 cgd #endif
308 1.8 mycroft if (ap->a_ioflag & IO_NDELAY)
309 1.1 cgd wso->so_state |= SS_NBIO;
310 1.25 fvdl VOP_UNLOCK(ap->a_vp, 0);
311 1.27 matt error = (*wso->so_send)(wso, (struct mbuf *)0, ap->a_uio, 0,
312 1.49 jonathan (struct mbuf *)0, 0, curproc /*XXX*/);
313 1.25 fvdl vn_lock(ap->a_vp, LK_EXCLUSIVE | LK_RETRY);
314 1.8 mycroft if (ap->a_ioflag & IO_NDELAY)
315 1.1 cgd wso->so_state &= ~SS_NBIO;
316 1.1 cgd return (error);
317 1.1 cgd }
318 1.1 cgd
319 1.1 cgd /*
320 1.1 cgd * Device ioctl operation.
321 1.1 cgd */
322 1.1 cgd /* ARGSUSED */
323 1.17 christos int
324 1.30 lukem fifo_ioctl(void *v)
325 1.17 christos {
326 1.8 mycroft struct vop_ioctl_args /* {
327 1.30 lukem struct vnode *a_vp;
328 1.30 lukem u_long a_command;
329 1.48 jrf void *a_data;
330 1.30 lukem int a_fflag;
331 1.30 lukem struct ucred *a_cred;
332 1.41 fvdl struct proc *a_p;
333 1.17 christos } */ *ap = v;
334 1.30 lukem struct file filetmp;
335 1.30 lukem int error;
336 1.1 cgd
337 1.8 mycroft if (ap->a_command == FIONBIO)
338 1.1 cgd return (0);
339 1.16 mycroft if (ap->a_fflag & FREAD) {
340 1.48 jrf filetmp.f_data = ap->a_vp->v_fifoinfo->fi_readsock;
341 1.41 fvdl error = soo_ioctl(&filetmp, ap->a_command, ap->a_data, ap->a_p);
342 1.16 mycroft if (error)
343 1.16 mycroft return (error);
344 1.16 mycroft }
345 1.16 mycroft if (ap->a_fflag & FWRITE) {
346 1.48 jrf filetmp.f_data = ap->a_vp->v_fifoinfo->fi_writesock;
347 1.41 fvdl error = soo_ioctl(&filetmp, ap->a_command, ap->a_data, ap->a_p);
348 1.16 mycroft if (error)
349 1.16 mycroft return (error);
350 1.16 mycroft }
351 1.16 mycroft return (0);
352 1.1 cgd }
353 1.1 cgd
354 1.1 cgd /* ARGSUSED */
355 1.17 christos int
356 1.30 lukem fifo_poll(void *v)
357 1.17 christos {
358 1.20 mycroft struct vop_poll_args /* {
359 1.30 lukem struct vnode *a_vp;
360 1.30 lukem int a_events;
361 1.41 fvdl struct proc *a_p;
362 1.17 christos } */ *ap = v;
363 1.30 lukem struct file filetmp;
364 1.30 lukem int revents;
365 1.1 cgd
366 1.30 lukem revents = 0;
367 1.20 mycroft if (ap->a_events & (POLLIN | POLLPRI | POLLRDNORM | POLLRDBAND)) {
368 1.48 jrf filetmp.f_data = ap->a_vp->v_fifoinfo->fi_readsock;
369 1.20 mycroft if (filetmp.f_data)
370 1.41 fvdl revents |= soo_poll(&filetmp, ap->a_events, ap->a_p);
371 1.16 mycroft }
372 1.20 mycroft if (ap->a_events & (POLLOUT | POLLWRNORM | POLLWRBAND)) {
373 1.48 jrf filetmp.f_data = ap->a_vp->v_fifoinfo->fi_writesock;
374 1.20 mycroft if (filetmp.f_data)
375 1.41 fvdl revents |= soo_poll(&filetmp, ap->a_events, ap->a_p);
376 1.16 mycroft }
377 1.20 mycroft
378 1.20 mycroft return (revents);
379 1.1 cgd }
380 1.1 cgd
381 1.25 fvdl int
382 1.30 lukem fifo_inactive(void *v)
383 1.25 fvdl {
384 1.25 fvdl struct vop_inactive_args /* {
385 1.30 lukem struct vnode *a_vp;
386 1.41 fvdl struct proc *a_p;
387 1.25 fvdl } */ *ap = v;
388 1.25 fvdl
389 1.25 fvdl VOP_UNLOCK(ap->a_vp, 0);
390 1.25 fvdl return (0);
391 1.25 fvdl }
392 1.25 fvdl
393 1.1 cgd /*
394 1.1 cgd * This is a noop, simply returning what one has been given.
395 1.1 cgd */
396 1.17 christos int
397 1.30 lukem fifo_bmap(void *v)
398 1.17 christos {
399 1.8 mycroft struct vop_bmap_args /* {
400 1.30 lukem struct vnode *a_vp;
401 1.30 lukem daddr_t a_bn;
402 1.30 lukem struct vnode **a_vpp;
403 1.30 lukem daddr_t *a_bnp;
404 1.30 lukem int *a_runp;
405 1.17 christos } */ *ap = v;
406 1.1 cgd
407 1.8 mycroft if (ap->a_vpp != NULL)
408 1.8 mycroft *ap->a_vpp = ap->a_vp;
409 1.8 mycroft if (ap->a_bnp != NULL)
410 1.8 mycroft *ap->a_bnp = ap->a_bn;
411 1.25 fvdl if (ap->a_runp != NULL)
412 1.25 fvdl *ap->a_runp = 0;
413 1.1 cgd return (0);
414 1.1 cgd }
415 1.1 cgd
416 1.1 cgd /*
417 1.1 cgd * Device close routine
418 1.1 cgd */
419 1.1 cgd /* ARGSUSED */
420 1.17 christos int
421 1.30 lukem fifo_close(void *v)
422 1.17 christos {
423 1.8 mycroft struct vop_close_args /* {
424 1.30 lukem struct vnode *a_vp;
425 1.30 lukem int a_fflag;
426 1.30 lukem struct ucred *a_cred;
427 1.41 fvdl struct proc *a_p;
428 1.17 christos } */ *ap = v;
429 1.30 lukem struct vnode *vp;
430 1.30 lukem struct fifoinfo *fip;
431 1.50 mycroft int isrevoke;
432 1.1 cgd
433 1.30 lukem vp = ap->a_vp;
434 1.30 lukem fip = vp->v_fifoinfo;
435 1.50 mycroft isrevoke = (ap->a_fflag & (FREAD | FWRITE | FNONBLOCK)) == FNONBLOCK;
436 1.50 mycroft if (isrevoke) {
437 1.50 mycroft if (fip->fi_readers != 0) {
438 1.50 mycroft fip->fi_readers = 0;
439 1.50 mycroft socantsendmore(fip->fi_writesock);
440 1.50 mycroft }
441 1.50 mycroft if (fip->fi_writers != 0) {
442 1.50 mycroft fip->fi_writers = 0;
443 1.50 mycroft socantrcvmore(fip->fi_readsock);
444 1.50 mycroft }
445 1.50 mycroft } else {
446 1.50 mycroft if ((ap->a_fflag & FREAD) && --fip->fi_readers == 0)
447 1.16 mycroft socantsendmore(fip->fi_writesock);
448 1.50 mycroft if ((ap->a_fflag & FWRITE) && --fip->fi_writers == 0)
449 1.1 cgd socantrcvmore(fip->fi_readsock);
450 1.1 cgd }
451 1.50 mycroft /* Shut down if all readers and writers are gone. */
452 1.50 mycroft if ((fip->fi_readers + fip->fi_writers) == 0) {
453 1.34 chs (void) soclose(fip->fi_readsock);
454 1.34 chs (void) soclose(fip->fi_writesock);
455 1.34 chs FREE(fip, M_VNODE);
456 1.34 chs vp->v_fifoinfo = NULL;
457 1.34 chs }
458 1.34 chs return (0);
459 1.1 cgd }
460 1.1 cgd
461 1.1 cgd /*
462 1.1 cgd * Print out the contents of a fifo vnode.
463 1.1 cgd */
464 1.17 christos int
465 1.30 lukem fifo_print(void *v)
466 1.17 christos {
467 1.8 mycroft struct vop_print_args /* {
468 1.30 lukem struct vnode *a_vp;
469 1.17 christos } */ *ap = v;
470 1.1 cgd
471 1.22 christos printf("tag VT_NON");
472 1.8 mycroft fifo_printinfo(ap->a_vp);
473 1.22 christos printf("\n");
474 1.17 christos return 0;
475 1.1 cgd }
476 1.1 cgd
477 1.1 cgd /*
478 1.1 cgd * Print out internal contents of a fifo vnode.
479 1.1 cgd */
480 1.17 christos void
481 1.30 lukem fifo_printinfo(struct vnode *vp)
482 1.1 cgd {
483 1.30 lukem struct fifoinfo *fip;
484 1.1 cgd
485 1.30 lukem fip = vp->v_fifoinfo;
486 1.22 christos printf(", fifo with %ld readers and %ld writers",
487 1.21 christos fip->fi_readers, fip->fi_writers);
488 1.1 cgd }
489 1.1 cgd
490 1.1 cgd /*
491 1.8 mycroft * Return POSIX pathconf information applicable to fifo's.
492 1.8 mycroft */
493 1.17 christos int
494 1.30 lukem fifo_pathconf(void *v)
495 1.17 christos {
496 1.8 mycroft struct vop_pathconf_args /* {
497 1.30 lukem struct vnode *a_vp;
498 1.30 lukem int a_name;
499 1.30 lukem register_t *a_retval;
500 1.17 christos } */ *ap = v;
501 1.8 mycroft
502 1.8 mycroft switch (ap->a_name) {
503 1.8 mycroft case _PC_LINK_MAX:
504 1.8 mycroft *ap->a_retval = LINK_MAX;
505 1.8 mycroft return (0);
506 1.8 mycroft case _PC_PIPE_BUF:
507 1.8 mycroft *ap->a_retval = PIPE_BUF;
508 1.8 mycroft return (0);
509 1.8 mycroft case _PC_CHOWN_RESTRICTED:
510 1.26 kleink *ap->a_retval = 1;
511 1.26 kleink return (0);
512 1.26 kleink case _PC_SYNC_IO:
513 1.8 mycroft *ap->a_retval = 1;
514 1.8 mycroft return (0);
515 1.8 mycroft default:
516 1.8 mycroft return (EINVAL);
517 1.8 mycroft }
518 1.1 cgd /* NOTREACHED */
519 1.36 jdolecek }
520 1.36 jdolecek
521 1.36 jdolecek static void
522 1.36 jdolecek filt_fifordetach(struct knote *kn)
523 1.36 jdolecek {
524 1.36 jdolecek struct socket *so;
525 1.36 jdolecek
526 1.36 jdolecek so = (struct socket *)kn->kn_hook;
527 1.37 christos SLIST_REMOVE(&so->so_rcv.sb_sel.sel_klist, kn, knote, kn_selnext);
528 1.37 christos if (SLIST_EMPTY(&so->so_rcv.sb_sel.sel_klist))
529 1.36 jdolecek so->so_rcv.sb_flags &= ~SB_KNOTE;
530 1.36 jdolecek }
531 1.36 jdolecek
532 1.36 jdolecek static int
533 1.36 jdolecek filt_fiforead(struct knote *kn, long hint)
534 1.36 jdolecek {
535 1.36 jdolecek struct socket *so;
536 1.36 jdolecek
537 1.36 jdolecek so = (struct socket *)kn->kn_hook;
538 1.36 jdolecek kn->kn_data = so->so_rcv.sb_cc;
539 1.36 jdolecek if (so->so_state & SS_CANTRCVMORE) {
540 1.36 jdolecek kn->kn_flags |= EV_EOF;
541 1.36 jdolecek return (1);
542 1.36 jdolecek }
543 1.36 jdolecek kn->kn_flags &= ~EV_EOF;
544 1.36 jdolecek return (kn->kn_data > 0);
545 1.36 jdolecek }
546 1.36 jdolecek
547 1.36 jdolecek static void
548 1.36 jdolecek filt_fifowdetach(struct knote *kn)
549 1.36 jdolecek {
550 1.36 jdolecek struct socket *so;
551 1.36 jdolecek
552 1.36 jdolecek so = (struct socket *)kn->kn_hook;
553 1.37 christos SLIST_REMOVE(&so->so_snd.sb_sel.sel_klist, kn, knote, kn_selnext);
554 1.37 christos if (SLIST_EMPTY(&so->so_snd.sb_sel.sel_klist))
555 1.36 jdolecek so->so_snd.sb_flags &= ~SB_KNOTE;
556 1.36 jdolecek }
557 1.36 jdolecek
558 1.36 jdolecek static int
559 1.36 jdolecek filt_fifowrite(struct knote *kn, long hint)
560 1.36 jdolecek {
561 1.36 jdolecek struct socket *so;
562 1.36 jdolecek
563 1.36 jdolecek so = (struct socket *)kn->kn_hook;
564 1.36 jdolecek kn->kn_data = sbspace(&so->so_snd);
565 1.36 jdolecek if (so->so_state & SS_CANTSENDMORE) {
566 1.36 jdolecek kn->kn_flags |= EV_EOF;
567 1.36 jdolecek return (1);
568 1.36 jdolecek }
569 1.36 jdolecek kn->kn_flags &= ~EV_EOF;
570 1.36 jdolecek return (kn->kn_data >= so->so_snd.sb_lowat);
571 1.36 jdolecek }
572 1.36 jdolecek
573 1.36 jdolecek static const struct filterops fiforead_filtops =
574 1.36 jdolecek { 1, NULL, filt_fifordetach, filt_fiforead };
575 1.36 jdolecek static const struct filterops fifowrite_filtops =
576 1.36 jdolecek { 1, NULL, filt_fifowdetach, filt_fifowrite };
577 1.36 jdolecek
578 1.36 jdolecek /* ARGSUSED */
579 1.36 jdolecek int
580 1.36 jdolecek fifo_kqfilter(void *v)
581 1.36 jdolecek {
582 1.36 jdolecek struct vop_kqfilter_args /* {
583 1.36 jdolecek struct vnode *a_vp;
584 1.36 jdolecek struct knote *a_kn;
585 1.36 jdolecek } */ *ap = v;
586 1.36 jdolecek struct socket *so;
587 1.36 jdolecek struct sockbuf *sb;
588 1.36 jdolecek
589 1.36 jdolecek so = (struct socket *)ap->a_vp->v_fifoinfo->fi_readsock;
590 1.36 jdolecek switch (ap->a_kn->kn_filter) {
591 1.36 jdolecek case EVFILT_READ:
592 1.36 jdolecek ap->a_kn->kn_fop = &fiforead_filtops;
593 1.36 jdolecek sb = &so->so_rcv;
594 1.36 jdolecek break;
595 1.36 jdolecek case EVFILT_WRITE:
596 1.36 jdolecek ap->a_kn->kn_fop = &fifowrite_filtops;
597 1.36 jdolecek sb = &so->so_snd;
598 1.36 jdolecek break;
599 1.36 jdolecek default:
600 1.36 jdolecek return (1);
601 1.36 jdolecek }
602 1.36 jdolecek
603 1.36 jdolecek ap->a_kn->kn_hook = so;
604 1.36 jdolecek
605 1.37 christos SLIST_INSERT_HEAD(&sb->sb_sel.sel_klist, ap->a_kn, kn_selnext);
606 1.36 jdolecek sb->sb_flags |= SB_KNOTE;
607 1.36 jdolecek return (0);
608 1.1 cgd }
609