fifo_vnops.c revision 1.87 1 1.87 thorpej /* $NetBSD: fifo_vnops.c,v 1.87 2021/10/02 02:07:41 thorpej Exp $ */
2 1.64 ad
3 1.64 ad /*-
4 1.64 ad * Copyright (c) 2008 The NetBSD Foundation, Inc.
5 1.64 ad * All rights reserved.
6 1.64 ad *
7 1.64 ad * Redistribution and use in source and binary forms, with or without
8 1.64 ad * modification, are permitted provided that the following conditions
9 1.64 ad * are met:
10 1.64 ad * 1. Redistributions of source code must retain the above copyright
11 1.64 ad * notice, this list of conditions and the following disclaimer.
12 1.64 ad * 2. Redistributions in binary form must reproduce the above copyright
13 1.64 ad * notice, this list of conditions and the following disclaimer in the
14 1.64 ad * documentation and/or other materials provided with the distribution.
15 1.64 ad *
16 1.64 ad * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 1.64 ad * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 1.64 ad * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 1.64 ad * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 1.64 ad * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 1.64 ad * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 1.64 ad * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 1.64 ad * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 1.64 ad * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 1.64 ad * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 1.64 ad * POSSIBILITY OF SUCH DAMAGE.
27 1.64 ad */
28 1.9 cgd
29 1.1 cgd /*
30 1.25 fvdl * Copyright (c) 1990, 1993, 1995
31 1.8 mycroft * The Regents of the University of California. All rights reserved.
32 1.1 cgd *
33 1.1 cgd * Redistribution and use in source and binary forms, with or without
34 1.1 cgd * modification, are permitted provided that the following conditions
35 1.1 cgd * are met:
36 1.1 cgd * 1. Redistributions of source code must retain the above copyright
37 1.1 cgd * notice, this list of conditions and the following disclaimer.
38 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
39 1.1 cgd * notice, this list of conditions and the following disclaimer in the
40 1.1 cgd * documentation and/or other materials provided with the distribution.
41 1.42 agc * 3. Neither the name of the University nor the names of its contributors
42 1.1 cgd * may be used to endorse or promote products derived from this software
43 1.1 cgd * without specific prior written permission.
44 1.1 cgd *
45 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
46 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
47 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
48 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
49 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
50 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
51 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
52 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
53 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
54 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
55 1.1 cgd * SUCH DAMAGE.
56 1.1 cgd *
57 1.25 fvdl * @(#)fifo_vnops.c 8.10 (Berkeley) 5/27/95
58 1.1 cgd */
59 1.32 lukem
60 1.32 lukem #include <sys/cdefs.h>
61 1.87 thorpej __KERNEL_RCSID(0, "$NetBSD: fifo_vnops.c,v 1.87 2021/10/02 02:07:41 thorpej Exp $");
62 1.1 cgd
63 1.6 mycroft #include <sys/param.h>
64 1.25 fvdl #include <sys/systm.h>
65 1.8 mycroft #include <sys/proc.h>
66 1.6 mycroft #include <sys/time.h>
67 1.6 mycroft #include <sys/namei.h>
68 1.6 mycroft #include <sys/vnode.h>
69 1.6 mycroft #include <sys/socket.h>
70 1.45 matt #include <sys/protosw.h>
71 1.6 mycroft #include <sys/socketvar.h>
72 1.6 mycroft #include <sys/stat.h>
73 1.6 mycroft #include <sys/ioctl.h>
74 1.6 mycroft #include <sys/file.h>
75 1.6 mycroft #include <sys/errno.h>
76 1.64 ad #include <sys/kmem.h>
77 1.17 christos #include <sys/un.h>
78 1.20 mycroft #include <sys/poll.h>
79 1.36 jdolecek #include <sys/event.h>
80 1.64 ad #include <sys/condvar.h>
81 1.19 mycroft
82 1.6 mycroft #include <miscfs/fifofs/fifo.h>
83 1.19 mycroft #include <miscfs/genfs/genfs.h>
84 1.1 cgd
85 1.1 cgd /*
86 1.1 cgd * This structure is associated with the FIFO vnode and stores
87 1.1 cgd * the state associated with the FIFO.
88 1.1 cgd */
89 1.1 cgd struct fifoinfo {
90 1.1 cgd struct socket *fi_readsock;
91 1.1 cgd struct socket *fi_writesock;
92 1.64 ad kcondvar_t fi_rcv;
93 1.64 ad int fi_readers;
94 1.64 ad kcondvar_t fi_wcv;
95 1.64 ad int fi_writers;
96 1.1 cgd };
97 1.1 cgd
98 1.1 cgd /*
99 1.1 cgd * Trivial lookup routine that always fails.
100 1.1 cgd */
101 1.1 cgd /* ARGSUSED */
102 1.68 pooka static int
103 1.30 lukem fifo_lookup(void *v)
104 1.17 christos {
105 1.74 hannken struct vop_lookup_v2_args /* {
106 1.30 lukem struct vnode *a_dvp;
107 1.30 lukem struct vnode **a_vpp;
108 1.30 lukem struct componentname *a_cnp;
109 1.17 christos } */ *ap = v;
110 1.51 perry
111 1.8 mycroft *ap->a_vpp = NULL;
112 1.1 cgd return (ENOTDIR);
113 1.1 cgd }
114 1.1 cgd
115 1.1 cgd /*
116 1.1 cgd * Open called to set up a new instance of a fifo or
117 1.1 cgd * to find an active instance of a fifo.
118 1.1 cgd */
119 1.68 pooka static int
120 1.30 lukem fifo_open(void *v)
121 1.17 christos {
122 1.8 mycroft struct vop_open_args /* {
123 1.30 lukem struct vnode *a_vp;
124 1.30 lukem int a_mode;
125 1.55 elad kauth_cred_t a_cred;
126 1.17 christos } */ *ap = v;
127 1.58 pooka struct lwp *l = curlwp;
128 1.30 lukem struct vnode *vp;
129 1.30 lukem struct fifoinfo *fip;
130 1.30 lukem struct socket *rso, *wso;
131 1.30 lukem int error;
132 1.1 cgd
133 1.30 lukem vp = ap->a_vp;
134 1.75 rmind KASSERT(VOP_ISLOCKED(vp));
135 1.30 lukem
136 1.1 cgd if ((fip = vp->v_fifoinfo) == NULL) {
137 1.64 ad fip = kmem_alloc(sizeof(*fip), KM_SLEEP);
138 1.64 ad error = socreate(AF_LOCAL, &rso, SOCK_STREAM, 0, l, NULL);
139 1.49 jonathan if (error != 0) {
140 1.64 ad kmem_free(fip, sizeof(*fip));
141 1.64 ad return (error);
142 1.1 cgd }
143 1.1 cgd fip->fi_readsock = rso;
144 1.64 ad error = socreate(AF_LOCAL, &wso, SOCK_STREAM, 0, l, rso);
145 1.49 jonathan if (error != 0) {
146 1.1 cgd (void)soclose(rso);
147 1.64 ad kmem_free(fip, sizeof(*fip));
148 1.64 ad return (error);
149 1.1 cgd }
150 1.1 cgd fip->fi_writesock = wso;
151 1.64 ad solock(wso);
152 1.77 rtr if ((error = unp_connect2(wso, rso)) != 0) {
153 1.64 ad sounlock(wso);
154 1.1 cgd (void)soclose(wso);
155 1.1 cgd (void)soclose(rso);
156 1.64 ad kmem_free(fip, sizeof(*fip));
157 1.64 ad return (error);
158 1.1 cgd }
159 1.64 ad fip->fi_readers = 0;
160 1.64 ad fip->fi_writers = 0;
161 1.1 cgd wso->so_state |= SS_CANTRCVMORE;
162 1.1 cgd rso->so_state |= SS_CANTSENDMORE;
163 1.64 ad cv_init(&fip->fi_rcv, "fiford");
164 1.64 ad cv_init(&fip->fi_wcv, "fifowr");
165 1.75 rmind vp->v_fifoinfo = fip;
166 1.64 ad } else {
167 1.64 ad wso = fip->fi_writesock;
168 1.64 ad rso = fip->fi_readsock;
169 1.64 ad solock(wso);
170 1.1 cgd }
171 1.64 ad
172 1.8 mycroft if (ap->a_mode & FREAD) {
173 1.16 mycroft if (fip->fi_readers++ == 0) {
174 1.64 ad wso->so_state &= ~SS_CANTSENDMORE;
175 1.64 ad cv_broadcast(&fip->fi_wcv);
176 1.1 cgd }
177 1.16 mycroft }
178 1.16 mycroft if (ap->a_mode & FWRITE) {
179 1.16 mycroft if (fip->fi_writers++ == 0) {
180 1.64 ad rso->so_state &= ~SS_CANTRCVMORE;
181 1.64 ad cv_broadcast(&fip->fi_rcv);
182 1.2 cgd }
183 1.16 mycroft }
184 1.16 mycroft if (ap->a_mode & FREAD) {
185 1.16 mycroft if (ap->a_mode & O_NONBLOCK) {
186 1.1 cgd } else {
187 1.64 ad while (!soreadable(rso) && fip->fi_writers == 0) {
188 1.69 hannken VOP_UNLOCK(vp);
189 1.64 ad error = cv_wait_sig(&fip->fi_rcv,
190 1.64 ad wso->so_lock);
191 1.64 ad sounlock(wso);
192 1.25 fvdl vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
193 1.16 mycroft if (error)
194 1.16 mycroft goto bad;
195 1.64 ad solock(wso);
196 1.1 cgd }
197 1.16 mycroft }
198 1.16 mycroft }
199 1.16 mycroft if (ap->a_mode & FWRITE) {
200 1.16 mycroft if (ap->a_mode & O_NONBLOCK) {
201 1.16 mycroft if (fip->fi_readers == 0) {
202 1.16 mycroft error = ENXIO;
203 1.64 ad sounlock(wso);
204 1.16 mycroft goto bad;
205 1.16 mycroft }
206 1.16 mycroft } else {
207 1.2 cgd while (fip->fi_readers == 0) {
208 1.69 hannken VOP_UNLOCK(vp);
209 1.64 ad error = cv_wait_sig(&fip->fi_wcv,
210 1.64 ad wso->so_lock);
211 1.64 ad sounlock(wso);
212 1.25 fvdl vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
213 1.8 mycroft if (error)
214 1.16 mycroft goto bad;
215 1.64 ad solock(wso);
216 1.2 cgd }
217 1.1 cgd }
218 1.1 cgd }
219 1.64 ad sounlock(wso);
220 1.16 mycroft return (0);
221 1.30 lukem bad:
222 1.58 pooka VOP_CLOSE(vp, ap->a_mode, ap->a_cred);
223 1.1 cgd return (error);
224 1.1 cgd }
225 1.1 cgd
226 1.1 cgd /*
227 1.1 cgd * Vnode op for read
228 1.1 cgd */
229 1.1 cgd /* ARGSUSED */
230 1.68 pooka static int
231 1.30 lukem fifo_read(void *v)
232 1.17 christos {
233 1.8 mycroft struct vop_read_args /* {
234 1.30 lukem struct vnode *a_vp;
235 1.30 lukem struct uio *a_uio;
236 1.30 lukem int a_ioflag;
237 1.55 elad kauth_cred_t a_cred;
238 1.17 christos } */ *ap = v;
239 1.30 lukem struct uio *uio;
240 1.30 lukem struct socket *rso;
241 1.71 christos int error, sflags;
242 1.35 thorpej size_t startresid;
243 1.1 cgd
244 1.30 lukem uio = ap->a_uio;
245 1.30 lukem rso = ap->a_vp->v_fifoinfo->fi_readsock;
246 1.1 cgd #ifdef DIAGNOSTIC
247 1.1 cgd if (uio->uio_rw != UIO_READ)
248 1.1 cgd panic("fifo_read mode");
249 1.1 cgd #endif
250 1.1 cgd if (uio->uio_resid == 0)
251 1.1 cgd return (0);
252 1.1 cgd startresid = uio->uio_resid;
253 1.69 hannken VOP_UNLOCK(ap->a_vp);
254 1.71 christos sflags = (ap->a_ioflag & IO_NDELAY) ? MSG_NBIO : 0;
255 1.71 christos error = (*rso->so_receive)(rso, NULL, uio, NULL, NULL, &sflags);
256 1.1 cgd /*
257 1.1 cgd * Clear EOF indication after first such return.
258 1.1 cgd */
259 1.72 christos if (error == 0 && uio->uio_resid == startresid)
260 1.1 cgd rso->so_state &= ~SS_CANTRCVMORE;
261 1.23 kleink if (ap->a_ioflag & IO_NDELAY) {
262 1.23 kleink if (error == EWOULDBLOCK &&
263 1.23 kleink ap->a_vp->v_fifoinfo->fi_writers == 0)
264 1.23 kleink error = 0;
265 1.23 kleink }
266 1.64 ad vn_lock(ap->a_vp, LK_EXCLUSIVE | LK_RETRY);
267 1.1 cgd return (error);
268 1.1 cgd }
269 1.1 cgd
270 1.1 cgd /*
271 1.1 cgd * Vnode op for write
272 1.1 cgd */
273 1.1 cgd /* ARGSUSED */
274 1.68 pooka static int
275 1.30 lukem fifo_write(void *v)
276 1.17 christos {
277 1.8 mycroft struct vop_write_args /* {
278 1.30 lukem struct vnode *a_vp;
279 1.30 lukem struct uio *a_uio;
280 1.30 lukem int a_ioflag;
281 1.55 elad kauth_cred_t a_cred;
282 1.17 christos } */ *ap = v;
283 1.30 lukem struct socket *wso;
284 1.71 christos int error, sflags;
285 1.1 cgd
286 1.30 lukem wso = ap->a_vp->v_fifoinfo->fi_writesock;
287 1.1 cgd #ifdef DIAGNOSTIC
288 1.8 mycroft if (ap->a_uio->uio_rw != UIO_WRITE)
289 1.1 cgd panic("fifo_write mode");
290 1.1 cgd #endif
291 1.69 hannken VOP_UNLOCK(ap->a_vp);
292 1.71 christos sflags = (ap->a_ioflag & IO_NDELAY) ? MSG_NBIO : 0;
293 1.71 christos error = (*wso->so_send)(wso, NULL, ap->a_uio, 0, NULL, sflags, curlwp);
294 1.64 ad vn_lock(ap->a_vp, LK_EXCLUSIVE | LK_RETRY);
295 1.1 cgd return (error);
296 1.1 cgd }
297 1.1 cgd
298 1.1 cgd /*
299 1.1 cgd * Device ioctl operation.
300 1.1 cgd */
301 1.1 cgd /* ARGSUSED */
302 1.68 pooka static int
303 1.30 lukem fifo_ioctl(void *v)
304 1.17 christos {
305 1.8 mycroft struct vop_ioctl_args /* {
306 1.30 lukem struct vnode *a_vp;
307 1.30 lukem u_long a_command;
308 1.48 jrf void *a_data;
309 1.30 lukem int a_fflag;
310 1.55 elad kauth_cred_t a_cred;
311 1.54 christos struct lwp *a_l;
312 1.17 christos } */ *ap = v;
313 1.30 lukem struct file filetmp;
314 1.30 lukem int error;
315 1.1 cgd
316 1.8 mycroft if (ap->a_command == FIONBIO)
317 1.1 cgd return (0);
318 1.16 mycroft if (ap->a_fflag & FREAD) {
319 1.48 jrf filetmp.f_data = ap->a_vp->v_fifoinfo->fi_readsock;
320 1.63 ad error = soo_ioctl(&filetmp, ap->a_command, ap->a_data);
321 1.16 mycroft if (error)
322 1.16 mycroft return (error);
323 1.16 mycroft }
324 1.16 mycroft if (ap->a_fflag & FWRITE) {
325 1.48 jrf filetmp.f_data = ap->a_vp->v_fifoinfo->fi_writesock;
326 1.63 ad error = soo_ioctl(&filetmp, ap->a_command, ap->a_data);
327 1.16 mycroft if (error)
328 1.16 mycroft return (error);
329 1.16 mycroft }
330 1.16 mycroft return (0);
331 1.1 cgd }
332 1.1 cgd
333 1.1 cgd /* ARGSUSED */
334 1.68 pooka static int
335 1.30 lukem fifo_poll(void *v)
336 1.17 christos {
337 1.20 mycroft struct vop_poll_args /* {
338 1.30 lukem struct vnode *a_vp;
339 1.30 lukem int a_events;
340 1.17 christos } */ *ap = v;
341 1.87 thorpej struct socket *rso = ap->a_vp->v_fifoinfo->fi_readsock;
342 1.87 thorpej struct socket *wso = ap->a_vp->v_fifoinfo->fi_writesock;
343 1.87 thorpej struct socket *lso = NULL;
344 1.87 thorpej int events;
345 1.87 thorpej
346 1.87 thorpej /*
347 1.87 thorpej * N.B. We're using a slightly different naming convention
348 1.87 thorpej * for these variables that most poll handlers.
349 1.87 thorpej */
350 1.87 thorpej int revents = 0;
351 1.87 thorpej int wevents = 0;
352 1.87 thorpej
353 1.87 thorpej if (rso != NULL) {
354 1.87 thorpej lso = rso;
355 1.87 thorpej } else if (wso != NULL) {
356 1.87 thorpej lso = wso;
357 1.87 thorpej }
358 1.87 thorpej
359 1.87 thorpej if (lso == NULL) {
360 1.87 thorpej /* No associated sockets -> no events to report. */
361 1.87 thorpej return 0;
362 1.87 thorpej }
363 1.87 thorpej
364 1.87 thorpej KASSERT(rso == NULL || lso->so_lock == rso->so_lock);
365 1.87 thorpej KASSERT(wso == NULL || lso->so_lock == wso->so_lock);
366 1.87 thorpej
367 1.87 thorpej solock(lso);
368 1.1 cgd
369 1.87 thorpej if (rso != NULL) {
370 1.87 thorpej events = ap->a_events & (POLLIN | POLLRDNORM);
371 1.87 thorpej if (events != 0 && soreadable(rso)) {
372 1.87 thorpej revents |= events;
373 1.87 thorpej }
374 1.87 thorpej if (rso->so_state & SS_CANTRCVMORE) {
375 1.87 thorpej revents |= POLLHUP;
376 1.87 thorpej }
377 1.87 thorpej /*
378 1.87 thorpej * We always selrecord the read side here regardless
379 1.87 thorpej * of the caller's read interest because we need to
380 1.87 thorpej * action POLLHUP.
381 1.87 thorpej */
382 1.87 thorpej if (revents == 0) {
383 1.87 thorpej selrecord(curlwp, &rso->so_rcv.sb_sel);
384 1.87 thorpej rso->so_rcv.sb_flags |= SB_NOTIFY;
385 1.87 thorpej }
386 1.16 mycroft }
387 1.87 thorpej
388 1.87 thorpej /* POSIX sez: POLLHUP and POLLOUT are mutually-exclusive. */
389 1.87 thorpej if (wso != NULL && (revents & POLLHUP) == 0) {
390 1.87 thorpej events = ap->a_events & (POLLOUT | POLLWRNORM);
391 1.87 thorpej if (events != 0 && sowritable(wso)) {
392 1.87 thorpej wevents |= events;
393 1.87 thorpej }
394 1.87 thorpej if (wevents == 0 && events != 0) {
395 1.87 thorpej selrecord(curlwp, &wso->so_snd.sb_sel);
396 1.87 thorpej wso->so_snd.sb_flags |= SB_NOTIFY;
397 1.87 thorpej }
398 1.16 mycroft }
399 1.20 mycroft
400 1.87 thorpej sounlock(lso);
401 1.87 thorpej
402 1.87 thorpej return (revents | wevents);
403 1.1 cgd }
404 1.1 cgd
405 1.68 pooka static int
406 1.30 lukem fifo_inactive(void *v)
407 1.25 fvdl {
408 1.78 riastrad struct vop_inactive_v2_args /* {
409 1.30 lukem struct vnode *a_vp;
410 1.54 christos struct lwp *a_l;
411 1.78 riastrad } */ *ap __unused = v;
412 1.25 fvdl
413 1.25 fvdl return (0);
414 1.25 fvdl }
415 1.25 fvdl
416 1.1 cgd /*
417 1.1 cgd * This is a noop, simply returning what one has been given.
418 1.1 cgd */
419 1.68 pooka static int
420 1.30 lukem fifo_bmap(void *v)
421 1.17 christos {
422 1.8 mycroft struct vop_bmap_args /* {
423 1.30 lukem struct vnode *a_vp;
424 1.30 lukem daddr_t a_bn;
425 1.30 lukem struct vnode **a_vpp;
426 1.30 lukem daddr_t *a_bnp;
427 1.30 lukem int *a_runp;
428 1.17 christos } */ *ap = v;
429 1.1 cgd
430 1.8 mycroft if (ap->a_vpp != NULL)
431 1.8 mycroft *ap->a_vpp = ap->a_vp;
432 1.8 mycroft if (ap->a_bnp != NULL)
433 1.8 mycroft *ap->a_bnp = ap->a_bn;
434 1.25 fvdl if (ap->a_runp != NULL)
435 1.25 fvdl *ap->a_runp = 0;
436 1.1 cgd return (0);
437 1.1 cgd }
438 1.1 cgd
439 1.1 cgd /*
440 1.87 thorpej * This is like socantrcvmore(), but we send the POLL_HUP code.
441 1.87 thorpej */
442 1.87 thorpej static void
443 1.87 thorpej fifo_socantrcvmore(struct socket *so)
444 1.87 thorpej {
445 1.87 thorpej KASSERT(solocked(so));
446 1.87 thorpej
447 1.87 thorpej so->so_state |= SS_CANTRCVMORE;
448 1.87 thorpej if (sb_notify(&so->so_rcv)) {
449 1.87 thorpej sowakeup(so, &so->so_rcv, POLL_HUP);
450 1.87 thorpej }
451 1.87 thorpej }
452 1.87 thorpej
453 1.87 thorpej /*
454 1.1 cgd * Device close routine
455 1.1 cgd */
456 1.1 cgd /* ARGSUSED */
457 1.68 pooka static int
458 1.30 lukem fifo_close(void *v)
459 1.17 christos {
460 1.8 mycroft struct vop_close_args /* {
461 1.30 lukem struct vnode *a_vp;
462 1.30 lukem int a_fflag;
463 1.55 elad kauth_cred_t a_cred;
464 1.54 christos struct lwp *a_l;
465 1.17 christos } */ *ap = v;
466 1.30 lukem struct vnode *vp;
467 1.30 lukem struct fifoinfo *fip;
468 1.64 ad struct socket *wso, *rso;
469 1.50 mycroft int isrevoke;
470 1.1 cgd
471 1.30 lukem vp = ap->a_vp;
472 1.30 lukem fip = vp->v_fifoinfo;
473 1.50 mycroft isrevoke = (ap->a_fflag & (FREAD | FWRITE | FNONBLOCK)) == FNONBLOCK;
474 1.64 ad wso = fip->fi_writesock;
475 1.64 ad rso = fip->fi_readsock;
476 1.64 ad solock(wso);
477 1.50 mycroft if (isrevoke) {
478 1.50 mycroft if (fip->fi_readers != 0) {
479 1.50 mycroft fip->fi_readers = 0;
480 1.64 ad socantsendmore(wso);
481 1.50 mycroft }
482 1.50 mycroft if (fip->fi_writers != 0) {
483 1.50 mycroft fip->fi_writers = 0;
484 1.87 thorpej fifo_socantrcvmore(rso);
485 1.50 mycroft }
486 1.50 mycroft } else {
487 1.50 mycroft if ((ap->a_fflag & FREAD) && --fip->fi_readers == 0)
488 1.64 ad socantsendmore(wso);
489 1.50 mycroft if ((ap->a_fflag & FWRITE) && --fip->fi_writers == 0)
490 1.87 thorpej fifo_socantrcvmore(rso);
491 1.1 cgd }
492 1.50 mycroft if ((fip->fi_readers + fip->fi_writers) == 0) {
493 1.64 ad sounlock(wso);
494 1.64 ad (void) soclose(rso);
495 1.64 ad (void) soclose(wso);
496 1.64 ad cv_destroy(&fip->fi_rcv);
497 1.64 ad cv_destroy(&fip->fi_wcv);
498 1.64 ad kmem_free(fip, sizeof(*fip));
499 1.34 chs vp->v_fifoinfo = NULL;
500 1.64 ad } else
501 1.64 ad sounlock(wso);
502 1.34 chs return (0);
503 1.1 cgd }
504 1.1 cgd
505 1.1 cgd /*
506 1.68 pooka * Print out internal contents of a fifo vnode.
507 1.68 pooka */
508 1.68 pooka static void
509 1.68 pooka fifo_printinfo(struct vnode *vp)
510 1.68 pooka {
511 1.68 pooka struct fifoinfo *fip;
512 1.68 pooka
513 1.68 pooka fip = vp->v_fifoinfo;
514 1.68 pooka printf(", fifo with %d readers and %d writers",
515 1.68 pooka fip->fi_readers, fip->fi_writers);
516 1.68 pooka }
517 1.68 pooka
518 1.68 pooka /*
519 1.1 cgd * Print out the contents of a fifo vnode.
520 1.1 cgd */
521 1.68 pooka static int
522 1.30 lukem fifo_print(void *v)
523 1.17 christos {
524 1.8 mycroft struct vop_print_args /* {
525 1.30 lukem struct vnode *a_vp;
526 1.17 christos } */ *ap = v;
527 1.1 cgd
528 1.68 pooka /*
529 1.68 pooka * We are most likely being called with the vnode belonging
530 1.68 pooka * to some file system and this is not printed.
531 1.68 pooka */
532 1.68 pooka if (ap->a_vp->v_tag == VT_NON)
533 1.68 pooka printf("tag VT_NON");
534 1.68 pooka
535 1.8 mycroft fifo_printinfo(ap->a_vp);
536 1.22 christos printf("\n");
537 1.17 christos return 0;
538 1.1 cgd }
539 1.1 cgd
540 1.1 cgd /*
541 1.8 mycroft * Return POSIX pathconf information applicable to fifo's.
542 1.8 mycroft */
543 1.68 pooka static int
544 1.30 lukem fifo_pathconf(void *v)
545 1.17 christos {
546 1.8 mycroft struct vop_pathconf_args /* {
547 1.30 lukem struct vnode *a_vp;
548 1.30 lukem int a_name;
549 1.30 lukem register_t *a_retval;
550 1.17 christos } */ *ap = v;
551 1.8 mycroft
552 1.8 mycroft switch (ap->a_name) {
553 1.8 mycroft case _PC_LINK_MAX:
554 1.8 mycroft *ap->a_retval = LINK_MAX;
555 1.8 mycroft return (0);
556 1.8 mycroft case _PC_PIPE_BUF:
557 1.8 mycroft *ap->a_retval = PIPE_BUF;
558 1.8 mycroft return (0);
559 1.8 mycroft case _PC_CHOWN_RESTRICTED:
560 1.26 kleink *ap->a_retval = 1;
561 1.26 kleink return (0);
562 1.26 kleink case _PC_SYNC_IO:
563 1.8 mycroft *ap->a_retval = 1;
564 1.8 mycroft return (0);
565 1.8 mycroft default:
566 1.81 christos return genfs_pathconf(ap);
567 1.8 mycroft }
568 1.1 cgd /* NOTREACHED */
569 1.36 jdolecek }
570 1.36 jdolecek
571 1.36 jdolecek static void
572 1.36 jdolecek filt_fifordetach(struct knote *kn)
573 1.36 jdolecek {
574 1.36 jdolecek struct socket *so;
575 1.36 jdolecek
576 1.36 jdolecek so = (struct socket *)kn->kn_hook;
577 1.64 ad solock(so);
578 1.85 thorpej if (selremove_knote(&so->so_rcv.sb_sel, kn))
579 1.85 thorpej so->so_rcv.sb_flags &= ~SB_KNOTE;
580 1.64 ad sounlock(so);
581 1.36 jdolecek }
582 1.36 jdolecek
583 1.36 jdolecek static int
584 1.57 christos filt_fiforead(struct knote *kn, long hint)
585 1.36 jdolecek {
586 1.36 jdolecek struct socket *so;
587 1.64 ad int rv;
588 1.36 jdolecek
589 1.36 jdolecek so = (struct socket *)kn->kn_hook;
590 1.64 ad if (hint != NOTE_SUBMIT)
591 1.64 ad solock(so);
592 1.36 jdolecek kn->kn_data = so->so_rcv.sb_cc;
593 1.36 jdolecek if (so->so_state & SS_CANTRCVMORE) {
594 1.36 jdolecek kn->kn_flags |= EV_EOF;
595 1.65 ad rv = 1;
596 1.65 ad } else {
597 1.65 ad kn->kn_flags &= ~EV_EOF;
598 1.65 ad rv = (kn->kn_data > 0);
599 1.36 jdolecek }
600 1.64 ad if (hint != NOTE_SUBMIT)
601 1.64 ad sounlock(so);
602 1.64 ad return rv;
603 1.36 jdolecek }
604 1.36 jdolecek
605 1.36 jdolecek static void
606 1.36 jdolecek filt_fifowdetach(struct knote *kn)
607 1.36 jdolecek {
608 1.36 jdolecek struct socket *so;
609 1.36 jdolecek
610 1.36 jdolecek so = (struct socket *)kn->kn_hook;
611 1.64 ad solock(so);
612 1.85 thorpej if (selremove_knote(&so->so_snd.sb_sel, kn))
613 1.85 thorpej so->so_snd.sb_flags &= ~SB_KNOTE;
614 1.64 ad sounlock(so);
615 1.36 jdolecek }
616 1.36 jdolecek
617 1.36 jdolecek static int
618 1.57 christos filt_fifowrite(struct knote *kn, long hint)
619 1.36 jdolecek {
620 1.36 jdolecek struct socket *so;
621 1.64 ad int rv;
622 1.36 jdolecek
623 1.36 jdolecek so = (struct socket *)kn->kn_hook;
624 1.64 ad if (hint != NOTE_SUBMIT)
625 1.65 ad solock(so);
626 1.36 jdolecek kn->kn_data = sbspace(&so->so_snd);
627 1.36 jdolecek if (so->so_state & SS_CANTSENDMORE) {
628 1.36 jdolecek kn->kn_flags |= EV_EOF;
629 1.65 ad rv = 1;
630 1.65 ad } else {
631 1.65 ad kn->kn_flags &= ~EV_EOF;
632 1.65 ad rv = (kn->kn_data >= so->so_snd.sb_lowat);
633 1.36 jdolecek }
634 1.64 ad if (hint != NOTE_SUBMIT)
635 1.64 ad sounlock(so);
636 1.64 ad return rv;
637 1.36 jdolecek }
638 1.36 jdolecek
639 1.79 maya static const struct filterops fiforead_filtops = {
640 1.86 thorpej .f_flags = FILTEROP_ISFD | FILTEROP_MPSAFE,
641 1.79 maya .f_attach = NULL,
642 1.79 maya .f_detach = filt_fifordetach,
643 1.79 maya .f_event = filt_fiforead,
644 1.79 maya };
645 1.79 maya
646 1.79 maya static const struct filterops fifowrite_filtops = {
647 1.86 thorpej .f_flags = FILTEROP_ISFD | FILTEROP_MPSAFE,
648 1.79 maya .f_attach = NULL,
649 1.79 maya .f_detach = filt_fifowdetach,
650 1.79 maya .f_event = filt_fifowrite,
651 1.79 maya };
652 1.36 jdolecek
653 1.36 jdolecek /* ARGSUSED */
654 1.68 pooka static int
655 1.36 jdolecek fifo_kqfilter(void *v)
656 1.36 jdolecek {
657 1.36 jdolecek struct vop_kqfilter_args /* {
658 1.36 jdolecek struct vnode *a_vp;
659 1.36 jdolecek struct knote *a_kn;
660 1.36 jdolecek } */ *ap = v;
661 1.36 jdolecek struct socket *so;
662 1.36 jdolecek struct sockbuf *sb;
663 1.36 jdolecek
664 1.36 jdolecek so = (struct socket *)ap->a_vp->v_fifoinfo->fi_readsock;
665 1.36 jdolecek switch (ap->a_kn->kn_filter) {
666 1.36 jdolecek case EVFILT_READ:
667 1.36 jdolecek ap->a_kn->kn_fop = &fiforead_filtops;
668 1.36 jdolecek sb = &so->so_rcv;
669 1.36 jdolecek break;
670 1.36 jdolecek case EVFILT_WRITE:
671 1.36 jdolecek ap->a_kn->kn_fop = &fifowrite_filtops;
672 1.36 jdolecek sb = &so->so_snd;
673 1.36 jdolecek break;
674 1.36 jdolecek default:
675 1.59 pooka return (EINVAL);
676 1.36 jdolecek }
677 1.36 jdolecek
678 1.36 jdolecek ap->a_kn->kn_hook = so;
679 1.36 jdolecek
680 1.64 ad solock(so);
681 1.82 thorpej selrecord_knote(&sb->sb_sel, ap->a_kn);
682 1.36 jdolecek sb->sb_flags |= SB_KNOTE;
683 1.64 ad sounlock(so);
684 1.64 ad
685 1.36 jdolecek return (0);
686 1.1 cgd }
687 1.68 pooka
688 1.68 pooka int (**fifo_vnodeop_p)(void *);
689 1.68 pooka const struct vnodeopv_entry_desc fifo_vnodeop_entries[] = {
690 1.68 pooka { &vop_default_desc, vn_default_error },
691 1.83 dholland { &vop_parsepath_desc, genfs_parsepath }, /* parsepath */
692 1.68 pooka { &vop_lookup_desc, fifo_lookup }, /* lookup */
693 1.68 pooka { &vop_create_desc, genfs_badop }, /* create */
694 1.68 pooka { &vop_mknod_desc, genfs_badop }, /* mknod */
695 1.68 pooka { &vop_open_desc, fifo_open }, /* open */
696 1.68 pooka { &vop_close_desc, fifo_close }, /* close */
697 1.68 pooka { &vop_access_desc, genfs_ebadf }, /* access */
698 1.80 christos { &vop_accessx_desc, genfs_accessx }, /* accessx */
699 1.68 pooka { &vop_getattr_desc, genfs_ebadf }, /* getattr */
700 1.68 pooka { &vop_setattr_desc, genfs_ebadf }, /* setattr */
701 1.68 pooka { &vop_read_desc, fifo_read }, /* read */
702 1.68 pooka { &vop_write_desc, fifo_write }, /* write */
703 1.76 dholland { &vop_fallocate_desc, genfs_eopnotsupp }, /* fallocate */
704 1.76 dholland { &vop_fdiscard_desc, genfs_eopnotsupp }, /* fdiscard */
705 1.68 pooka { &vop_ioctl_desc, fifo_ioctl }, /* ioctl */
706 1.68 pooka { &vop_poll_desc, fifo_poll }, /* poll */
707 1.68 pooka { &vop_kqfilter_desc, fifo_kqfilter }, /* kqfilter */
708 1.68 pooka { &vop_revoke_desc, genfs_revoke }, /* revoke */
709 1.68 pooka { &vop_mmap_desc, genfs_badop }, /* mmap */
710 1.68 pooka { &vop_fsync_desc, genfs_nullop }, /* fsync */
711 1.68 pooka { &vop_seek_desc, genfs_badop }, /* seek */
712 1.68 pooka { &vop_remove_desc, genfs_badop }, /* remove */
713 1.68 pooka { &vop_link_desc, genfs_badop }, /* link */
714 1.68 pooka { &vop_rename_desc, genfs_badop }, /* rename */
715 1.68 pooka { &vop_mkdir_desc, genfs_badop }, /* mkdir */
716 1.68 pooka { &vop_rmdir_desc, genfs_badop }, /* rmdir */
717 1.68 pooka { &vop_symlink_desc, genfs_badop }, /* symlink */
718 1.68 pooka { &vop_readdir_desc, genfs_badop }, /* readdir */
719 1.68 pooka { &vop_readlink_desc, genfs_badop }, /* readlink */
720 1.68 pooka { &vop_abortop_desc, genfs_badop }, /* abortop */
721 1.68 pooka { &vop_inactive_desc, fifo_inactive }, /* inactive */
722 1.68 pooka { &vop_reclaim_desc, genfs_nullop }, /* reclaim */
723 1.68 pooka { &vop_lock_desc, genfs_lock }, /* lock */
724 1.68 pooka { &vop_unlock_desc, genfs_unlock }, /* unlock */
725 1.68 pooka { &vop_bmap_desc, fifo_bmap }, /* bmap */
726 1.68 pooka { &vop_strategy_desc, genfs_badop }, /* strategy */
727 1.68 pooka { &vop_print_desc, fifo_print }, /* print */
728 1.68 pooka { &vop_islocked_desc, genfs_islocked }, /* islocked */
729 1.68 pooka { &vop_pathconf_desc, fifo_pathconf }, /* pathconf */
730 1.68 pooka { &vop_advlock_desc, genfs_einval }, /* advlock */
731 1.68 pooka { &vop_bwrite_desc, genfs_nullop }, /* bwrite */
732 1.68 pooka { &vop_putpages_desc, genfs_null_putpages }, /* putpages */
733 1.70 plunky { NULL, NULL }
734 1.68 pooka };
735 1.68 pooka const struct vnodeopv_desc fifo_vnodeop_opv_desc =
736 1.68 pooka { &fifo_vnodeop_p, fifo_vnodeop_entries };
737