xref: /freebsd/sys/fs/fifofs/fifo_vnops.c (revision 3642298923e528d795e3a30ec165d2b469e28b40)
1 /*-
2  * Copyright (c) 1990, 1993, 1995
3  *	The Regents of the University of California.
4  * Copyright (c) 2005 Robert N. M. Watson
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 4. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  *
31  *	@(#)fifo_vnops.c	8.10 (Berkeley) 5/27/95
32  * $FreeBSD$
33  */
34 
35 #include <sys/param.h>
36 #include <sys/event.h>
37 #include <sys/file.h>
38 #include <sys/filedesc.h>
39 #include <sys/filio.h>
40 #include <sys/fcntl.h>
41 #include <sys/kernel.h>
42 #include <sys/lock.h>
43 #include <sys/mutex.h>
44 #include <sys/malloc.h>
45 #include <sys/poll.h>
46 #include <sys/proc.h> /* XXXKSE */
47 #include <sys/signalvar.h>
48 #include <sys/socket.h>
49 #include <sys/socketvar.h>
50 #include <sys/sx.h>
51 #include <sys/systm.h>
52 #include <sys/un.h>
53 #include <sys/unistd.h>
54 #include <sys/vnode.h>
55 #include <fs/fifofs/fifo.h>
56 
57 static fo_rdwr_t        fifo_read_f;
58 static fo_rdwr_t        fifo_write_f;
59 static fo_ioctl_t       fifo_ioctl_f;
60 static fo_poll_t        fifo_poll_f;
61 static fo_kqfilter_t    fifo_kqfilter_f;
62 static fo_stat_t        fifo_stat_f;
63 static fo_close_t       fifo_close_f;
64 
65 struct fileops fifo_ops_f = {
66 	.fo_read =      fifo_read_f,
67 	.fo_write =     fifo_write_f,
68 	.fo_ioctl =     fifo_ioctl_f,
69 	.fo_poll =      fifo_poll_f,
70 	.fo_kqfilter =  fifo_kqfilter_f,
71 	.fo_stat =      fifo_stat_f,
72 	.fo_close =     fifo_close_f,
73 	.fo_flags =     DFLAG_PASSABLE
74 };
75 
76 /*
77  * This structure is associated with the FIFO vnode and stores
78  * the state associated with the FIFO.
79  */
80 struct fifoinfo {
81 	struct socket	*fi_readsock;
82 	struct socket	*fi_writesock;
83 	long		fi_readers;
84 	long		fi_writers;
85 };
86 
87 static vop_print_t	fifo_print;
88 static vop_open_t	fifo_open;
89 static vop_close_t	fifo_close;
90 static vop_ioctl_t	fifo_ioctl;
91 static vop_kqfilter_t	fifo_kqfilter;
92 static vop_pathconf_t	fifo_pathconf;
93 static vop_advlock_t	fifo_advlock;
94 
95 static void	filt_fifordetach(struct knote *kn);
96 static int	filt_fiforead(struct knote *kn, long hint);
97 static void	filt_fifowdetach(struct knote *kn);
98 static int	filt_fifowrite(struct knote *kn, long hint);
99 static void	filt_fifodetach_notsup(struct knote *kn);
100 static int	filt_fifo_notsup(struct knote *kn, long hint);
101 
102 static struct filterops fiforead_filtops =
103 	{ 1, NULL, filt_fifordetach, filt_fiforead };
104 static struct filterops fifowrite_filtops =
105 	{ 1, NULL, filt_fifowdetach, filt_fifowrite };
106 static struct filterops fifo_notsup_filtops =
107 	{ 1, NULL, filt_fifodetach_notsup, filt_fifo_notsup };
108 
109 struct vop_vector fifo_specops = {
110 	.vop_default =		&default_vnodeops,
111 
112 	.vop_access =		VOP_EBADF,
113 	.vop_advlock =		fifo_advlock,
114 	.vop_close =		fifo_close,
115 	.vop_create =		VOP_PANIC,
116 	.vop_getattr =		VOP_EBADF,
117 	.vop_ioctl =		fifo_ioctl,
118 	.vop_kqfilter =		fifo_kqfilter,
119 	.vop_lease =		VOP_NULL,
120 	.vop_link =		VOP_PANIC,
121 	.vop_mkdir =		VOP_PANIC,
122 	.vop_mknod =		VOP_PANIC,
123 	.vop_open =		fifo_open,
124 	.vop_pathconf =		fifo_pathconf,
125 	.vop_print =		fifo_print,
126 	.vop_read =		VOP_PANIC,
127 	.vop_readdir =		VOP_PANIC,
128 	.vop_readlink =		VOP_PANIC,
129 	.vop_reallocblks =	VOP_PANIC,
130 	.vop_reclaim =		VOP_NULL,
131 	.vop_remove =		VOP_PANIC,
132 	.vop_rename =		VOP_PANIC,
133 	.vop_rmdir =		VOP_PANIC,
134 	.vop_setattr =		VOP_EBADF,
135 	.vop_symlink =		VOP_PANIC,
136 	.vop_write =		VOP_PANIC,
137 };
138 
139 struct mtx fifo_mtx;
140 MTX_SYSINIT(fifo, &fifo_mtx, "fifo mutex", MTX_DEF);
141 
142 /*
143  * Dispose of fifo resources.
144  */
145 static void
146 fifo_cleanup(struct vnode *vp)
147 {
148 	struct fifoinfo *fip = vp->v_fifoinfo;
149 
150 	ASSERT_VOP_LOCKED(vp, "fifo_cleanup");
151 	if (fip->fi_readers == 0 && fip->fi_writers == 0) {
152 		vp->v_fifoinfo = NULL;
153 		(void)soclose(fip->fi_readsock);
154 		(void)soclose(fip->fi_writesock);
155 		FREE(fip, M_VNODE);
156 	}
157 }
158 
159 /*
160  * Open called to set up a new instance of a fifo or
161  * to find an active instance of a fifo.
162  */
163 /* ARGSUSED */
164 static int
165 fifo_open(ap)
166 	struct vop_open_args /* {
167 		struct vnode *a_vp;
168 		int  a_mode;
169 		struct ucred *a_cred;
170 		struct thread *a_td;
171 	} */ *ap;
172 {
173 	struct vnode *vp = ap->a_vp;
174 	struct fifoinfo *fip;
175 	struct thread *td = ap->a_td;
176 	struct ucred *cred = ap->a_cred;
177 	struct socket *rso, *wso;
178 	struct file *fp;
179 	int error;
180 
181 	if ((fip = vp->v_fifoinfo) == NULL) {
182 		MALLOC(fip, struct fifoinfo *, sizeof(*fip), M_VNODE, M_WAITOK);
183 		error = socreate(AF_LOCAL, &rso, SOCK_STREAM, 0, cred, td);
184 		if (error)
185 			goto fail1;
186 		fip->fi_readsock = rso;
187 		error = socreate(AF_LOCAL, &wso, SOCK_STREAM, 0, cred, td);
188 		if (error)
189 			goto fail2;
190 		fip->fi_writesock = wso;
191 		error = soconnect2(wso, rso);
192 		if (error) {
193 			(void)soclose(wso);
194 fail2:
195 			(void)soclose(rso);
196 fail1:
197 			free(fip, M_VNODE);
198 			return (error);
199 		}
200 		fip->fi_readers = fip->fi_writers = 0;
201 		wso->so_snd.sb_lowat = PIPE_BUF;
202 		SOCKBUF_LOCK(&rso->so_rcv);
203 		rso->so_rcv.sb_state |= SBS_CANTRCVMORE;
204 		SOCKBUF_UNLOCK(&rso->so_rcv);
205 		KASSERT(vp->v_fifoinfo == NULL,
206 		    ("fifo_open: v_fifoinfo race"));
207 		vp->v_fifoinfo = fip;
208 	}
209 
210 	/*
211 	 * General access to fi_readers and fi_writers is protected using
212 	 * the vnode lock.
213 	 *
214 	 * Protect the increment of fi_readers and fi_writers and the
215 	 * associated calls to wakeup() with the fifo mutex in addition
216 	 * to the vnode lock.  This allows the vnode lock to be dropped
217 	 * for the msleep() calls below, and using the fifo mutex with
218 	 * msleep() prevents the wakeup from being missed.
219 	 */
220 	mtx_lock(&fifo_mtx);
221 	if (ap->a_mode & FREAD) {
222 		fip->fi_readers++;
223 		if (fip->fi_readers == 1) {
224 			SOCKBUF_LOCK(&fip->fi_writesock->so_snd);
225 			fip->fi_writesock->so_snd.sb_state &= ~SBS_CANTSENDMORE;
226 			SOCKBUF_UNLOCK(&fip->fi_writesock->so_snd);
227 			if (fip->fi_writers > 0) {
228 				wakeup(&fip->fi_writers);
229 				sowwakeup(fip->fi_writesock);
230 			}
231 		}
232 	}
233 	if (ap->a_mode & FWRITE) {
234 		if ((ap->a_mode & O_NONBLOCK) && fip->fi_readers == 0) {
235 			mtx_unlock(&fifo_mtx);
236 			return (ENXIO);
237 		}
238 		fip->fi_writers++;
239 		if (fip->fi_writers == 1) {
240 			SOCKBUF_LOCK(&fip->fi_writesock->so_rcv);
241 			fip->fi_readsock->so_rcv.sb_state &= ~SBS_CANTRCVMORE;
242 			SOCKBUF_UNLOCK(&fip->fi_writesock->so_rcv);
243 			if (fip->fi_readers > 0) {
244 				wakeup(&fip->fi_readers);
245 				sorwakeup(fip->fi_readsock);
246 			}
247 		}
248 	}
249 	if ((ap->a_mode & O_NONBLOCK) == 0) {
250 		if ((ap->a_mode & FREAD) && fip->fi_writers == 0) {
251 			VOP_UNLOCK(vp, 0, td);
252 			error = msleep(&fip->fi_readers, &fifo_mtx,
253 			    PDROP | PCATCH | PSOCK, "fifoor", 0);
254 			vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
255 			if (error) {
256 				fip->fi_readers--;
257 				if (fip->fi_readers == 0) {
258 					socantsendmore(fip->fi_writesock);
259 					fifo_cleanup(vp);
260 				}
261 				return (error);
262 			}
263 			mtx_lock(&fifo_mtx);
264 			/*
265 			 * We must have got woken up because we had a writer.
266 			 * That (and not still having one) is the condition
267 			 * that we must wait for.
268 			 */
269 		}
270 		if ((ap->a_mode & FWRITE) && fip->fi_readers == 0) {
271 			VOP_UNLOCK(vp, 0, td);
272 			error = msleep(&fip->fi_writers, &fifo_mtx,
273 			    PDROP | PCATCH | PSOCK, "fifoow", 0);
274 			vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
275 			if (error) {
276 				fip->fi_writers--;
277 				if (fip->fi_writers == 0) {
278 					socantrcvmore(fip->fi_readsock);
279 					fifo_cleanup(vp);
280 				}
281 				return (error);
282 			}
283 			/*
284 			 * We must have got woken up because we had
285 			 * a reader.  That (and not still having one)
286 			 * is the condition that we must wait for.
287 			 */
288 			mtx_lock(&fifo_mtx);
289 		}
290 	}
291 	mtx_unlock(&fifo_mtx);
292 	KASSERT(ap->a_fdidx >= 0, ("can't fifo/vnode bypass %d", ap->a_fdidx));
293 	fp = ap->a_td->td_proc->p_fd->fd_ofiles[ap->a_fdidx];
294 	KASSERT(fp->f_ops == &badfileops, ("not badfileops in fifo_open"));
295 	fp->f_ops = &fifo_ops_f;
296 	fp->f_data = fip;
297 	return (0);
298 }
299 
300 /*
301  * Now unused vnode ioctl routine.
302  */
303 /* ARGSUSED */
304 static int
305 fifo_ioctl(ap)
306 	struct vop_ioctl_args /* {
307 		struct vnode *a_vp;
308 		u_long  a_command;
309 		caddr_t  a_data;
310 		int  a_fflag;
311 		struct ucred *a_cred;
312 		struct thread *a_td;
313 	} */ *ap;
314 {
315 
316 	printf("WARNING: fifo_ioctl called unexpectedly\n");
317 	return (ENOTTY);
318 }
319 
320 /*
321  * Now unused vnode kqfilter routine.
322  */
323 /* ARGSUSED */
324 static int
325 fifo_kqfilter(ap)
326 	struct vop_kqfilter_args /* {
327 		struct vnode *a_vp;
328 		struct knote *a_kn;
329 	} */ *ap;
330 {
331 
332 	printf("WARNING: fifo_kqfilter called unexpectedly\n");
333 	return (EINVAL);
334 }
335 
336 static void
337 filt_fifordetach(struct knote *kn)
338 {
339 	struct socket *so = (struct socket *)kn->kn_hook;
340 
341 	SOCKBUF_LOCK(&so->so_rcv);
342 	knlist_remove(&so->so_rcv.sb_sel.si_note, kn, 1);
343 	if (knlist_empty(&so->so_rcv.sb_sel.si_note))
344 		so->so_rcv.sb_flags &= ~SB_KNOTE;
345 	SOCKBUF_UNLOCK(&so->so_rcv);
346 }
347 
348 static int
349 filt_fiforead(struct knote *kn, long hint)
350 {
351 	struct socket *so = (struct socket *)kn->kn_hook;
352 
353 	SOCKBUF_LOCK_ASSERT(&so->so_rcv);
354 	kn->kn_data = so->so_rcv.sb_cc;
355 	if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
356 		kn->kn_flags |= EV_EOF;
357 		return (1);
358 	} else {
359 		kn->kn_flags &= ~EV_EOF;
360 		return (kn->kn_data > 0);
361 	}
362 }
363 
364 static void
365 filt_fifowdetach(struct knote *kn)
366 {
367 	struct socket *so = (struct socket *)kn->kn_hook;
368 
369 	SOCKBUF_LOCK(&so->so_snd);
370 	knlist_remove(&so->so_snd.sb_sel.si_note, kn, 1);
371 	if (knlist_empty(&so->so_snd.sb_sel.si_note))
372 		so->so_snd.sb_flags &= ~SB_KNOTE;
373 	SOCKBUF_UNLOCK(&so->so_snd);
374 }
375 
376 static int
377 filt_fifowrite(struct knote *kn, long hint)
378 {
379 	struct socket *so = (struct socket *)kn->kn_hook;
380 
381 	SOCKBUF_LOCK_ASSERT(&so->so_snd);
382 	kn->kn_data = sbspace(&so->so_snd);
383 	if (so->so_snd.sb_state & SBS_CANTSENDMORE) {
384 		kn->kn_flags |= EV_EOF;
385 		return (1);
386 	} else {
387 		kn->kn_flags &= ~EV_EOF;
388 	        return (kn->kn_data >= so->so_snd.sb_lowat);
389 	}
390 }
391 
392 static void
393 filt_fifodetach_notsup(struct knote *kn)
394 {
395 
396 }
397 
398 static int
399 filt_fifo_notsup(struct knote *kn, long hint)
400 {
401 
402 	return (0);
403 }
404 
405 /*
406  * Device close routine
407  */
408 /* ARGSUSED */
409 static int
410 fifo_close(ap)
411 	struct vop_close_args /* {
412 		struct vnode *a_vp;
413 		int  a_fflag;
414 		struct ucred *a_cred;
415 		struct thread *a_td;
416 	} */ *ap;
417 {
418 	struct vnode *vp = ap->a_vp;
419 	struct fifoinfo *fip = vp->v_fifoinfo;
420 
421 	ASSERT_VOP_LOCKED(vp, "fifo_close");
422 	if (ap->a_fflag & FREAD) {
423 		fip->fi_readers--;
424 		if (fip->fi_readers == 0)
425 			socantsendmore(fip->fi_writesock);
426 	}
427 	if (ap->a_fflag & FWRITE) {
428 		fip->fi_writers--;
429 		if (fip->fi_writers == 0)
430 			socantrcvmore(fip->fi_readsock);
431 	}
432 	fifo_cleanup(vp);
433 	return (0);
434 }
435 
436 /*
437  * Print out internal contents of a fifo vnode.
438  */
439 int
440 fifo_printinfo(vp)
441 	struct vnode *vp;
442 {
443 	register struct fifoinfo *fip = vp->v_fifoinfo;
444 
445 	printf(", fifo with %ld readers and %ld writers",
446 		fip->fi_readers, fip->fi_writers);
447 	return (0);
448 }
449 
450 /*
451  * Print out the contents of a fifo vnode.
452  */
453 static int
454 fifo_print(ap)
455 	struct vop_print_args /* {
456 		struct vnode *a_vp;
457 	} */ *ap;
458 {
459 	fifo_printinfo(ap->a_vp);
460 	printf("\n");
461 	return (0);
462 }
463 
464 /*
465  * Return POSIX pathconf information applicable to fifo's.
466  */
467 static int
468 fifo_pathconf(ap)
469 	struct vop_pathconf_args /* {
470 		struct vnode *a_vp;
471 		int a_name;
472 		int *a_retval;
473 	} */ *ap;
474 {
475 
476 	switch (ap->a_name) {
477 	case _PC_LINK_MAX:
478 		*ap->a_retval = LINK_MAX;
479 		return (0);
480 	case _PC_PIPE_BUF:
481 		*ap->a_retval = PIPE_BUF;
482 		return (0);
483 	case _PC_CHOWN_RESTRICTED:
484 		*ap->a_retval = 1;
485 		return (0);
486 	default:
487 		return (EINVAL);
488 	}
489 	/* NOTREACHED */
490 }
491 
492 /*
493  * Fifo advisory byte-level locks.
494  */
495 /* ARGSUSED */
496 static int
497 fifo_advlock(ap)
498 	struct vop_advlock_args /* {
499 		struct vnode *a_vp;
500 		caddr_t  a_id;
501 		int  a_op;
502 		struct flock *a_fl;
503 		int  a_flags;
504 	} */ *ap;
505 {
506 
507 	return (ap->a_flags & F_FLOCK ? EOPNOTSUPP : EINVAL);
508 }
509 
510 static int
511 fifo_close_f(struct file *fp, struct thread *td)
512 {
513 
514 	return (vnops.fo_close(fp, td));
515 }
516 
517 /*
518  * The implementation of ioctl() for named fifos is complicated by the fact
519  * that we permit O_RDWR fifo file descriptors, meaning that the actions of
520  * ioctls may have to be applied to both the underlying sockets rather than
521  * just one.  The original implementation simply forward the ioctl to one
522  * or both sockets based on fp->f_flag.  We now consider each ioctl
523  * separately, as the composition effect requires careful ordering.
524  *
525  * We do not blindly pass all ioctls through to the socket in order to avoid
526  * providing unnecessary ioctls that might be improperly depended on by
527  * applications (such as socket-specific, routing, and interface ioctls).
528  *
529  * Unlike sys_pipe.c, fifos do not implement the deprecated TIOCSPGRP and
530  * TIOCGPGRP ioctls.  Earlier implementations of fifos did forward SIOCSPGRP
531  * and SIOCGPGRP ioctls, so we might need to re-add those here.
532  */
533 static int
534 fifo_ioctl_f(struct file *fp, u_long com, void *data, struct ucred *cred,
535     struct thread *td)
536 {
537 	struct fifoinfo *fi;
538 	struct file filetmp;	/* Local, so need not be locked. */
539 	int error;
540 
541 	error = ENOTTY;
542 	fi = fp->f_data;
543 
544 	switch (com) {
545 	case FIONBIO:
546 		/*
547 		 * Non-blocking I/O is implemented at the fifo layer using
548 		 * MSG_NBIO, so does not need to be forwarded down the stack.
549 		 */
550 		return (0);
551 
552 	case FIOASYNC:
553 	case FIOSETOWN:
554 	case FIOGETOWN:
555 		/*
556 		 * These socket ioctls don't have any ordering requirements,
557 		 * so are called in an arbitrary order, and only on the
558 		 * sockets indicated by the file descriptor rights.
559 		 *
560 		 * XXXRW: If O_RDWR and the read socket accepts an ioctl but
561 		 * the write socket doesn't, the socketpair is left in an
562 		 * inconsistent state.
563 		 */
564 		if (fp->f_flag & FREAD) {
565 			filetmp.f_data = fi->fi_readsock;
566 			filetmp.f_cred = cred;
567 			error = soo_ioctl(&filetmp, com, data, cred, td);
568 			if (error)
569 				return (error);
570 		}
571 		if (fp->f_flag & FWRITE) {
572 			filetmp.f_data = fi->fi_writesock;
573 			filetmp.f_cred = cred;
574 			error = soo_ioctl(&filetmp, com, data, cred, td);
575 		}
576 		return (error);
577 
578 	case FIONREAD:
579 		/*
580 		 * FIONREAD will return 0 for non-readable descriptors, and
581 		 * the results of FIONREAD on the read socket for readable
582 		 * descriptors.
583 		 */
584 		if (!(fp->f_flag & FREAD)) {
585 			*(int *)data = 0;
586 			return (0);
587 		}
588 		filetmp.f_data = fi->fi_readsock;
589 		filetmp.f_cred = cred;
590 		return (soo_ioctl(&filetmp, com, data, cred, td));
591 
592 	default:
593 		return (ENOTTY);
594 	}
595 }
596 
597 /*
598  * Because fifos are now a file descriptor layer object, EVFILT_VNODE is not
599  * implemented.  Likely, fifo_kqfilter() should be removed, and
600  * fifo_kqfilter_f() should know how to forward the request to the underling
601  * vnode using f_vnode in the file descriptor here.
602  */
603 static int
604 fifo_kqfilter_f(struct file *fp, struct knote *kn)
605 {
606 	struct fifoinfo *fi;
607 	struct socket *so;
608 	struct sockbuf *sb;
609 
610 	fi = fp->f_data;
611 
612 	/*
613 	 * If a filter is requested that is not supported by this file
614 	 * descriptor, don't return an error, but also don't ever generate an
615 	 * event.
616 	 */
617 	if ((kn->kn_filter == EVFILT_READ) && !(fp->f_flag & FREAD)) {
618 		kn->kn_fop = &fifo_notsup_filtops;
619 		return (0);
620 	}
621 
622 	if ((kn->kn_filter == EVFILT_WRITE) && !(fp->f_flag & FWRITE)) {
623 		kn->kn_fop = &fifo_notsup_filtops;
624 		return (0);
625 	}
626 
627 	switch (kn->kn_filter) {
628 	case EVFILT_READ:
629 		kn->kn_fop = &fiforead_filtops;
630 		so = fi->fi_readsock;
631 		sb = &so->so_rcv;
632 		break;
633 	case EVFILT_WRITE:
634 		kn->kn_fop = &fifowrite_filtops;
635 		so = fi->fi_writesock;
636 		sb = &so->so_snd;
637 		break;
638 	default:
639 		return (EINVAL);
640 	}
641 
642 	kn->kn_hook = (caddr_t)so;
643 
644 	SOCKBUF_LOCK(sb);
645 	knlist_add(&sb->sb_sel.si_note, kn, 1);
646 	sb->sb_flags |= SB_KNOTE;
647 	SOCKBUF_UNLOCK(sb);
648 
649 	return (0);
650 }
651 
652 static int
653 fifo_poll_f(struct file *fp, int events, struct ucred *cred, struct thread *td)
654 {
655 	struct fifoinfo *fip;
656 	struct file filetmp;
657 	int levents, revents = 0;
658 
659 	fip = fp->f_data;
660 	levents = events &
661 	    (POLLIN | POLLINIGNEOF | POLLPRI | POLLRDNORM | POLLRDBAND);
662 	if ((fp->f_flag & FREAD) && levents) {
663 		/*
664 		 * If POLLIN or POLLRDNORM is requested and POLLINIGNEOF is
665 		 * not, then convert the first two to the last one.  This
666 		 * tells the socket poll function to ignore EOF so that we
667 		 * block if there is no writer (and no data).  Callers can
668 		 * set POLLINIGNEOF to get non-blocking behavior.
669 		 */
670 		if (levents & (POLLIN | POLLRDNORM) &&
671 		    !(levents & POLLINIGNEOF)) {
672 			levents &= ~(POLLIN | POLLRDNORM);
673 			levents |= POLLINIGNEOF;
674 		}
675 
676 		filetmp.f_data = fip->fi_readsock;
677 		filetmp.f_cred = cred;
678 		revents |= soo_poll(&filetmp, levents, cred, td);
679 
680 		/* Reverse the above conversion. */
681 		if ((revents & POLLINIGNEOF) && !(events & POLLINIGNEOF)) {
682 			revents |= (events & (POLLIN | POLLRDNORM));
683 			revents &= ~POLLINIGNEOF;
684 		}
685 	}
686 	levents = events & (POLLOUT | POLLWRNORM | POLLWRBAND);
687 	if ((fp->f_flag & FWRITE) && levents) {
688 		filetmp.f_data = fip->fi_writesock;
689 		filetmp.f_cred = cred;
690 		revents |= soo_poll(&filetmp, levents, cred, td);
691 	}
692 	return (revents);
693 }
694 
695 static int
696 fifo_read_f(struct file *fp, struct uio *uio, struct ucred *cred, int flags, struct thread *td)
697 {
698 	struct fifoinfo *fip;
699 	int error, sflags;
700 
701 	fip = fp->f_data;
702 	KASSERT(uio->uio_rw == UIO_READ,("fifo_read mode"));
703 	if (uio->uio_resid == 0)
704 		return (0);
705 	sflags = (fp->f_flag & FNONBLOCK) ? MSG_NBIO : 0;
706 	error = soreceive(fip->fi_readsock, NULL, uio, NULL, NULL, &sflags);
707 	return (error);
708 }
709 
710 static int
711 fifo_stat_f(struct file *fp, struct stat *sb, struct ucred *cred, struct thread *td)
712 {
713 
714 	return (vnops.fo_stat(fp, sb, cred, td));
715 }
716 
717 static int
718 fifo_write_f(struct file *fp, struct uio *uio, struct ucred *cred, int flags, struct thread *td)
719 {
720 	struct fifoinfo *fip;
721 	int error, sflags;
722 
723 	fip = fp->f_data;
724 	KASSERT(uio->uio_rw == UIO_WRITE,("fifo_write mode"));
725 	sflags = (fp->f_flag & FNONBLOCK) ? MSG_NBIO : 0;
726 	error = sosend(fip->fi_writesock, NULL, uio, 0, NULL, sflags, td);
727 	return (error);
728 }
729