]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/fs/fifofs/fifo_vnops.c
If fifo_open() is called with a negative file descriptor, return EINVAL
[FreeBSD/FreeBSD.git] / sys / fs / fifofs / fifo_vnops.c
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         ASSERT_VOP_LOCKED(vp, "fifo_open");
182         if (ap->a_fdidx < 0)
183                 return (EINVAL);
184         if ((fip = vp->v_fifoinfo) == NULL) {
185                 MALLOC(fip, struct fifoinfo *, sizeof(*fip), M_VNODE, M_WAITOK);
186                 error = socreate(AF_LOCAL, &rso, SOCK_STREAM, 0, cred, td);
187                 if (error)
188                         goto fail1;
189                 fip->fi_readsock = rso;
190                 error = socreate(AF_LOCAL, &wso, SOCK_STREAM, 0, cred, td);
191                 if (error)
192                         goto fail2;
193                 fip->fi_writesock = wso;
194                 error = soconnect2(wso, rso);
195                 if (error) {
196                         (void)soclose(wso);
197 fail2:
198                         (void)soclose(rso);
199 fail1:
200                         free(fip, M_VNODE);
201                         return (error);
202                 }
203                 fip->fi_readers = fip->fi_writers = 0;
204                 wso->so_snd.sb_lowat = PIPE_BUF;
205                 SOCKBUF_LOCK(&rso->so_rcv);
206                 rso->so_rcv.sb_state |= SBS_CANTRCVMORE;
207                 SOCKBUF_UNLOCK(&rso->so_rcv);
208                 KASSERT(vp->v_fifoinfo == NULL,
209                     ("fifo_open: v_fifoinfo race"));
210                 vp->v_fifoinfo = fip;
211         }
212
213         /*
214          * General access to fi_readers and fi_writers is protected using
215          * the vnode lock.
216          *
217          * Protect the increment of fi_readers and fi_writers and the
218          * associated calls to wakeup() with the fifo mutex in addition
219          * to the vnode lock.  This allows the vnode lock to be dropped
220          * for the msleep() calls below, and using the fifo mutex with
221          * msleep() prevents the wakeup from being missed.
222          */
223         mtx_lock(&fifo_mtx);
224         if (ap->a_mode & FREAD) {
225                 fip->fi_readers++;
226                 if (fip->fi_readers == 1) {
227                         SOCKBUF_LOCK(&fip->fi_writesock->so_snd);
228                         fip->fi_writesock->so_snd.sb_state &= ~SBS_CANTSENDMORE;
229                         SOCKBUF_UNLOCK(&fip->fi_writesock->so_snd);
230                         if (fip->fi_writers > 0) {
231                                 wakeup(&fip->fi_writers);
232                                 sowwakeup(fip->fi_writesock);
233                         }
234                 }
235         }
236         if (ap->a_mode & FWRITE) {
237                 if ((ap->a_mode & O_NONBLOCK) && fip->fi_readers == 0) {
238                         mtx_unlock(&fifo_mtx);
239                         return (ENXIO);
240                 }
241                 fip->fi_writers++;
242                 if (fip->fi_writers == 1) {
243                         SOCKBUF_LOCK(&fip->fi_readsock->so_rcv);
244                         fip->fi_readsock->so_rcv.sb_state &= ~SBS_CANTRCVMORE;
245                         SOCKBUF_UNLOCK(&fip->fi_readsock->so_rcv);
246                         if (fip->fi_readers > 0) {
247                                 wakeup(&fip->fi_readers);
248                                 sorwakeup(fip->fi_readsock);
249                         }
250                 }
251         }
252         if ((ap->a_mode & O_NONBLOCK) == 0) {
253                 if ((ap->a_mode & FREAD) && fip->fi_writers == 0) {
254                         VOP_UNLOCK(vp, 0, td);
255                         error = msleep(&fip->fi_readers, &fifo_mtx,
256                             PDROP | PCATCH | PSOCK, "fifoor", 0);
257                         vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
258                         if (error) {
259                                 fip->fi_readers--;
260                                 if (fip->fi_readers == 0) {
261                                         socantsendmore(fip->fi_writesock);
262                                         fifo_cleanup(vp);
263                                 }
264                                 return (error);
265                         }
266                         mtx_lock(&fifo_mtx);
267                         /*
268                          * We must have got woken up because we had a writer.
269                          * That (and not still having one) is the condition
270                          * that we must wait for.
271                          */
272                 }
273                 if ((ap->a_mode & FWRITE) && fip->fi_readers == 0) {
274                         VOP_UNLOCK(vp, 0, td);
275                         error = msleep(&fip->fi_writers, &fifo_mtx,
276                             PDROP | PCATCH | PSOCK, "fifoow", 0);
277                         vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
278                         if (error) {
279                                 fip->fi_writers--;
280                                 if (fip->fi_writers == 0) {
281                                         socantrcvmore(fip->fi_readsock);
282                                         fifo_cleanup(vp);
283                                 }
284                                 return (error);
285                         }
286                         /*
287                          * We must have got woken up because we had
288                          * a reader.  That (and not still having one)
289                          * is the condition that we must wait for.
290                          */
291                         mtx_lock(&fifo_mtx);
292                 }
293         }
294         mtx_unlock(&fifo_mtx);
295         KASSERT(ap->a_fdidx >= 0, ("can't fifo/vnode bypass %d", ap->a_fdidx));
296         fp = ap->a_td->td_proc->p_fd->fd_ofiles[ap->a_fdidx];
297         KASSERT(fp->f_ops == &badfileops, ("not badfileops in fifo_open"));
298         fp->f_ops = &fifo_ops_f;
299         fp->f_data = fip;
300         return (0);
301 }
302
303 /*
304  * Now unused vnode ioctl routine.
305  */
306 /* ARGSUSED */
307 static int
308 fifo_ioctl(ap)
309         struct vop_ioctl_args /* {
310                 struct vnode *a_vp;
311                 u_long  a_command;
312                 caddr_t  a_data;
313                 int  a_fflag;
314                 struct ucred *a_cred;
315                 struct thread *a_td;
316         } */ *ap;
317 {
318
319         printf("WARNING: fifo_ioctl called unexpectedly\n");
320         return (ENOTTY);
321 }
322
323 /*
324  * Now unused vnode kqfilter routine.
325  */
326 /* ARGSUSED */
327 static int
328 fifo_kqfilter(ap)
329         struct vop_kqfilter_args /* {
330                 struct vnode *a_vp;
331                 struct knote *a_kn;
332         } */ *ap;
333 {
334
335         printf("WARNING: fifo_kqfilter called unexpectedly\n");
336         return (EINVAL);
337 }
338
339 static void
340 filt_fifordetach(struct knote *kn)
341 {
342         struct socket *so = (struct socket *)kn->kn_hook;
343
344         SOCKBUF_LOCK(&so->so_rcv);
345         knlist_remove(&so->so_rcv.sb_sel.si_note, kn, 1);
346         if (knlist_empty(&so->so_rcv.sb_sel.si_note))
347                 so->so_rcv.sb_flags &= ~SB_KNOTE;
348         SOCKBUF_UNLOCK(&so->so_rcv);
349 }
350
351 static int
352 filt_fiforead(struct knote *kn, long hint)
353 {
354         struct socket *so = (struct socket *)kn->kn_hook;
355
356         SOCKBUF_LOCK_ASSERT(&so->so_rcv);
357         kn->kn_data = so->so_rcv.sb_cc;
358         if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
359                 kn->kn_flags |= EV_EOF;
360                 return (1);
361         } else {
362                 kn->kn_flags &= ~EV_EOF;
363                 return (kn->kn_data > 0);
364         }
365 }
366
367 static void
368 filt_fifowdetach(struct knote *kn)
369 {
370         struct socket *so = (struct socket *)kn->kn_hook;
371
372         SOCKBUF_LOCK(&so->so_snd);
373         knlist_remove(&so->so_snd.sb_sel.si_note, kn, 1);
374         if (knlist_empty(&so->so_snd.sb_sel.si_note))
375                 so->so_snd.sb_flags &= ~SB_KNOTE;
376         SOCKBUF_UNLOCK(&so->so_snd);
377 }
378
379 static int
380 filt_fifowrite(struct knote *kn, long hint)
381 {
382         struct socket *so = (struct socket *)kn->kn_hook;
383
384         SOCKBUF_LOCK_ASSERT(&so->so_snd);
385         kn->kn_data = sbspace(&so->so_snd);
386         if (so->so_snd.sb_state & SBS_CANTSENDMORE) {
387                 kn->kn_flags |= EV_EOF;
388                 return (1);
389         } else {
390                 kn->kn_flags &= ~EV_EOF;
391                 return (kn->kn_data >= so->so_snd.sb_lowat);
392         }
393 }
394
395 static void
396 filt_fifodetach_notsup(struct knote *kn)
397 {
398
399 }
400
401 static int
402 filt_fifo_notsup(struct knote *kn, long hint)
403 {
404
405         return (0);
406 }
407
408 /*
409  * Device close routine
410  */
411 /* ARGSUSED */
412 static int
413 fifo_close(ap)
414         struct vop_close_args /* {
415                 struct vnode *a_vp;
416                 int  a_fflag;
417                 struct ucred *a_cred;
418                 struct thread *a_td;
419         } */ *ap;
420 {
421         struct vnode *vp = ap->a_vp;
422         struct fifoinfo *fip = vp->v_fifoinfo;
423
424         ASSERT_VOP_LOCKED(vp, "fifo_close");
425         KASSERT(fip != NULL, ("fifo_close: no v_fifoinfo"));
426         if (ap->a_fflag & FREAD) {
427                 fip->fi_readers--;
428                 if (fip->fi_readers == 0)
429                         socantsendmore(fip->fi_writesock);
430         }
431         if (ap->a_fflag & FWRITE) {
432                 fip->fi_writers--;
433                 if (fip->fi_writers == 0)
434                         socantrcvmore(fip->fi_readsock);
435         }
436         fifo_cleanup(vp);
437         return (0);
438 }
439
440 /*
441  * Print out internal contents of a fifo vnode.
442  */
443 int
444 fifo_printinfo(vp)
445         struct vnode *vp;
446 {
447         register struct fifoinfo *fip = vp->v_fifoinfo;
448
449         printf(", fifo with %ld readers and %ld writers",
450                 fip->fi_readers, fip->fi_writers);
451         return (0);
452 }
453
454 /*
455  * Print out the contents of a fifo vnode.
456  */
457 static int
458 fifo_print(ap)
459         struct vop_print_args /* {
460                 struct vnode *a_vp;
461         } */ *ap;
462 {
463         fifo_printinfo(ap->a_vp);
464         printf("\n");
465         return (0);
466 }
467
468 /*
469  * Return POSIX pathconf information applicable to fifo's.
470  */
471 static int
472 fifo_pathconf(ap)
473         struct vop_pathconf_args /* {
474                 struct vnode *a_vp;
475                 int a_name;
476                 int *a_retval;
477         } */ *ap;
478 {
479
480         switch (ap->a_name) {
481         case _PC_LINK_MAX:
482                 *ap->a_retval = LINK_MAX;
483                 return (0);
484         case _PC_PIPE_BUF:
485                 *ap->a_retval = PIPE_BUF;
486                 return (0);
487         case _PC_CHOWN_RESTRICTED:
488                 *ap->a_retval = 1;
489                 return (0);
490         default:
491                 return (EINVAL);
492         }
493         /* NOTREACHED */
494 }
495
496 /*
497  * Fifo advisory byte-level locks.
498  */
499 /* ARGSUSED */
500 static int
501 fifo_advlock(ap)
502         struct vop_advlock_args /* {
503                 struct vnode *a_vp;
504                 caddr_t  a_id;
505                 int  a_op;
506                 struct flock *a_fl;
507                 int  a_flags;
508         } */ *ap;
509 {
510
511         return (ap->a_flags & F_FLOCK ? EOPNOTSUPP : EINVAL);
512 }
513
514 static int
515 fifo_close_f(struct file *fp, struct thread *td)
516 {
517
518         return (vnops.fo_close(fp, td));
519 }
520
521 /*
522  * The implementation of ioctl() for named fifos is complicated by the fact
523  * that we permit O_RDWR fifo file descriptors, meaning that the actions of
524  * ioctls may have to be applied to both the underlying sockets rather than
525  * just one.  The original implementation simply forward the ioctl to one
526  * or both sockets based on fp->f_flag.  We now consider each ioctl
527  * separately, as the composition effect requires careful ordering.
528  *
529  * We do not blindly pass all ioctls through to the socket in order to avoid
530  * providing unnecessary ioctls that might be improperly depended on by
531  * applications (such as socket-specific, routing, and interface ioctls).
532  *
533  * Unlike sys_pipe.c, fifos do not implement the deprecated TIOCSPGRP and
534  * TIOCGPGRP ioctls.  Earlier implementations of fifos did forward SIOCSPGRP
535  * and SIOCGPGRP ioctls, so we might need to re-add those here.
536  */
537 static int
538 fifo_ioctl_f(struct file *fp, u_long com, void *data, struct ucred *cred,
539     struct thread *td)
540 {
541         struct fifoinfo *fi;
542         struct file filetmp;    /* Local, so need not be locked. */
543         int error;
544
545         error = ENOTTY;
546         fi = fp->f_data;
547
548         switch (com) {
549         case FIONBIO:
550                 /*
551                  * Non-blocking I/O is implemented at the fifo layer using
552                  * MSG_NBIO, so does not need to be forwarded down the stack.
553                  */
554                 return (0);
555
556         case FIOASYNC:
557         case FIOSETOWN:
558         case FIOGETOWN:
559                 /*
560                  * These socket ioctls don't have any ordering requirements,
561                  * so are called in an arbitrary order, and only on the
562                  * sockets indicated by the file descriptor rights.
563                  *
564                  * XXXRW: If O_RDWR and the read socket accepts an ioctl but
565                  * the write socket doesn't, the socketpair is left in an
566                  * inconsistent state.
567                  */
568                 if (fp->f_flag & FREAD) {
569                         filetmp.f_data = fi->fi_readsock;
570                         filetmp.f_cred = cred;
571                         error = soo_ioctl(&filetmp, com, data, cred, td);
572                         if (error)
573                                 return (error);
574                 }
575                 if (fp->f_flag & FWRITE) {
576                         filetmp.f_data = fi->fi_writesock;
577                         filetmp.f_cred = cred;
578                         error = soo_ioctl(&filetmp, com, data, cred, td);
579                 }
580                 return (error);
581
582         case FIONREAD:
583                 /*
584                  * FIONREAD will return 0 for non-readable descriptors, and
585                  * the results of FIONREAD on the read socket for readable
586                  * descriptors.
587                  */
588                 if (!(fp->f_flag & FREAD)) {
589                         *(int *)data = 0;
590                         return (0);
591                 }
592                 filetmp.f_data = fi->fi_readsock;
593                 filetmp.f_cred = cred;
594                 return (soo_ioctl(&filetmp, com, data, cred, td));
595
596         default:
597                 return (ENOTTY);
598         }
599 }
600
601 /*
602  * Because fifos are now a file descriptor layer object, EVFILT_VNODE is not
603  * implemented.  Likely, fifo_kqfilter() should be removed, and
604  * fifo_kqfilter_f() should know how to forward the request to the underling
605  * vnode using f_vnode in the file descriptor here.
606  */
607 static int
608 fifo_kqfilter_f(struct file *fp, struct knote *kn)
609 {
610         struct fifoinfo *fi;
611         struct socket *so;
612         struct sockbuf *sb;
613
614         fi = fp->f_data;
615
616         /*
617          * If a filter is requested that is not supported by this file
618          * descriptor, don't return an error, but also don't ever generate an
619          * event.
620          */
621         if ((kn->kn_filter == EVFILT_READ) && !(fp->f_flag & FREAD)) {
622                 kn->kn_fop = &fifo_notsup_filtops;
623                 return (0);
624         }
625
626         if ((kn->kn_filter == EVFILT_WRITE) && !(fp->f_flag & FWRITE)) {
627                 kn->kn_fop = &fifo_notsup_filtops;
628                 return (0);
629         }
630
631         switch (kn->kn_filter) {
632         case EVFILT_READ:
633                 kn->kn_fop = &fiforead_filtops;
634                 so = fi->fi_readsock;
635                 sb = &so->so_rcv;
636                 break;
637         case EVFILT_WRITE:
638                 kn->kn_fop = &fifowrite_filtops;
639                 so = fi->fi_writesock;
640                 sb = &so->so_snd;
641                 break;
642         default:
643                 return (EINVAL);
644         }
645
646         kn->kn_hook = (caddr_t)so;
647
648         SOCKBUF_LOCK(sb);
649         knlist_add(&sb->sb_sel.si_note, kn, 1);
650         sb->sb_flags |= SB_KNOTE;
651         SOCKBUF_UNLOCK(sb);
652
653         return (0);
654 }
655
656 static int
657 fifo_poll_f(struct file *fp, int events, struct ucred *cred, struct thread *td)
658 {
659         struct fifoinfo *fip;
660         struct file filetmp;
661         int levents, revents = 0;
662
663         fip = fp->f_data;
664         levents = events &
665             (POLLIN | POLLINIGNEOF | POLLPRI | POLLRDNORM | POLLRDBAND);
666         if ((fp->f_flag & FREAD) && levents) {
667                 /*
668                  * If POLLIN or POLLRDNORM is requested and POLLINIGNEOF is
669                  * not, then convert the first two to the last one.  This
670                  * tells the socket poll function to ignore EOF so that we
671                  * block if there is no writer (and no data).  Callers can
672                  * set POLLINIGNEOF to get non-blocking behavior.
673                  */
674                 if (levents & (POLLIN | POLLRDNORM) &&
675                     !(levents & POLLINIGNEOF)) {
676                         levents &= ~(POLLIN | POLLRDNORM);
677                         levents |= POLLINIGNEOF;
678                 }
679
680                 filetmp.f_data = fip->fi_readsock;
681                 filetmp.f_cred = cred;
682                 revents |= soo_poll(&filetmp, levents, cred, td);
683
684                 /* Reverse the above conversion. */
685                 if ((revents & POLLINIGNEOF) && !(events & POLLINIGNEOF)) {
686                         revents |= (events & (POLLIN | POLLRDNORM));
687                         revents &= ~POLLINIGNEOF;
688                 }
689         }
690         levents = events & (POLLOUT | POLLWRNORM | POLLWRBAND);
691         if ((fp->f_flag & FWRITE) && levents) {
692                 filetmp.f_data = fip->fi_writesock;
693                 filetmp.f_cred = cred;
694                 revents |= soo_poll(&filetmp, levents, cred, td);
695         }
696         return (revents);
697 }
698
699 static int
700 fifo_read_f(struct file *fp, struct uio *uio, struct ucred *cred, int flags, struct thread *td)
701 {
702         struct fifoinfo *fip;
703         int error, sflags;
704
705         fip = fp->f_data;
706         KASSERT(uio->uio_rw == UIO_READ,("fifo_read mode"));
707         if (uio->uio_resid == 0)
708                 return (0);
709         sflags = (fp->f_flag & FNONBLOCK) ? MSG_NBIO : 0;
710         mtx_lock(&Giant);
711         error = soreceive(fip->fi_readsock, NULL, uio, NULL, NULL, &sflags);
712         mtx_unlock(&Giant);
713         return (error);
714 }
715
716 static int
717 fifo_stat_f(struct file *fp, struct stat *sb, struct ucred *cred, struct thread *td)
718 {
719
720         return (vnops.fo_stat(fp, sb, cred, td));
721 }
722
723 static int
724 fifo_write_f(struct file *fp, struct uio *uio, struct ucred *cred, int flags, struct thread *td)
725 {
726         struct fifoinfo *fip;
727         int error, sflags;
728
729         fip = fp->f_data;
730         KASSERT(uio->uio_rw == UIO_WRITE,("fifo_write mode"));
731         sflags = (fp->f_flag & FNONBLOCK) ? MSG_NBIO : 0;
732         mtx_lock(&Giant);
733         error = sosend(fip->fi_writesock, NULL, uio, 0, NULL, sflags, td);
734         mtx_unlock(&Giant);
735         return (error);
736 }