2005-01-06 18:10:42 +00:00
|
|
|
/*-
|
2017-11-20 19:43:44 +00:00
|
|
|
* SPDX-License-Identifier: BSD-3-Clause
|
|
|
|
*
|
1997-02-10 02:22:35 +00:00
|
|
|
* Copyright (c) 1990, 1993, 1995
|
2005-09-13 17:46:48 +00:00
|
|
|
* The Regents of the University of California.
|
|
|
|
* Copyright (c) 2005 Robert N. M. Watson
|
2012-02-23 18:37:30 +00:00
|
|
|
* Copyright (c) 2012 Giovanni Trematerra
|
2005-09-13 17:46:48 +00:00
|
|
|
* All rights reserved.
|
1994-05-24 10:09:53 +00:00
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
2017-02-28 23:42:47 +00:00
|
|
|
* 3. Neither the name of the University nor the names of its contributors
|
1994-05-24 10:09:53 +00:00
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*
|
1997-02-10 02:22:35 +00:00
|
|
|
* @(#)fifo_vnops.c 8.10 (Berkeley) 5/27/95
|
1999-08-28 01:08:13 +00:00
|
|
|
* $FreeBSD$
|
1994-05-24 10:09:53 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/param.h>
|
2002-04-30 01:54:54 +00:00
|
|
|
#include <sys/event.h>
|
2004-11-15 14:51:44 +00:00
|
|
|
#include <sys/file.h>
|
|
|
|
#include <sys/filedesc.h>
|
2002-04-30 01:54:54 +00:00
|
|
|
#include <sys/filio.h>
|
|
|
|
#include <sys/fcntl.h>
|
1997-02-12 16:26:37 +00:00
|
|
|
#include <sys/kernel.h>
|
1997-12-05 19:55:52 +00:00
|
|
|
#include <sys/lock.h>
|
2002-01-13 21:37:49 +00:00
|
|
|
#include <sys/mutex.h>
|
1997-10-12 20:26:33 +00:00
|
|
|
#include <sys/malloc.h>
|
2012-02-23 18:37:30 +00:00
|
|
|
#include <sys/selinfo.h>
|
|
|
|
#include <sys/pipe.h>
|
2008-03-12 10:12:01 +00:00
|
|
|
#include <sys/proc.h>
|
2002-04-30 01:54:54 +00:00
|
|
|
#include <sys/signalvar.h>
|
|
|
|
#include <sys/sx.h>
|
|
|
|
#include <sys/systm.h>
|
1995-03-16 18:17:34 +00:00
|
|
|
#include <sys/un.h>
|
2002-04-30 01:54:54 +00:00
|
|
|
#include <sys/unistd.h>
|
|
|
|
#include <sys/vnode.h>
|
1994-05-24 10:09:53 +00:00
|
|
|
|
|
|
|
/*
|
2005-09-27 16:45:22 +00:00
|
|
|
* This structure is associated with the FIFO vnode and stores
|
|
|
|
* the state associated with the FIFO.
|
2009-11-06 22:29:46 +00:00
|
|
|
* Notes about locking:
|
2012-02-23 18:37:30 +00:00
|
|
|
* - fi_pipe is invariant since init time.
|
|
|
|
* - fi_readers and fi_writers are protected by the vnode lock.
|
1994-05-24 10:09:53 +00:00
|
|
|
*/
|
|
|
|
struct fifoinfo {
|
2012-02-23 18:37:30 +00:00
|
|
|
struct pipe *fi_pipe;
|
|
|
|
long fi_readers;
|
|
|
|
long fi_writers;
|
Ensure that when a blockable open of fifo returns success, a valid
file descriptor opened for complimentary access exists as well.
The implementation of the guarantee is done by counting the
generations of readers and writers opens. We return success and not
EINTR or ERESTART error, when the sleep for complimentary opening is
interrupted, but the generation was changed during the sleep.
Longer explanation: assume there are two threads, A doing open("fifo",
O_RDONLY) and B doing open("fifo", O_WRONLY), and no other threads
either trying to open the fifo, nor there are any file descriptors
referencing the fifo. Before the change, it was possible e.g. for for
thread A to return a valid file descriptor, while thread B returned
EINTR if a signal to B was delivered simultaneously with the wakeup
from A. After the change, in this situation both A::open() and
B::open() succeed and the signal is made "as if" it was noticed
slightly later. Note that the signal actual delivery is not changed,
it is done by ast on syscall return path, so signal handler is still
executed before first instruction after syscall.
See PR for the code demonstrating the issue.
PR: 203162
Reported by: Victor Stinner victor.stinner@gmail.com
Reviewed by: jilles
Tested by: bapt, pho
Sponsored by: The FreeBSD Foundation
MFC after: 1 week
2015-09-20 21:18:33 +00:00
|
|
|
u_int fi_rgen;
|
|
|
|
u_int fi_wgen;
|
1994-05-24 10:09:53 +00:00
|
|
|
};
|
|
|
|
|
2004-12-01 12:24:41 +00:00
|
|
|
static vop_print_t fifo_print;
|
|
|
|
static vop_open_t fifo_open;
|
|
|
|
static vop_close_t fifo_close;
|
|
|
|
static vop_advlock_t fifo_advlock;
|
1997-10-15 13:24:07 +00:00
|
|
|
|
2004-12-01 23:16:38 +00:00
|
|
|
struct vop_vector fifo_specops = {
|
|
|
|
.vop_default = &default_vnodeops,
|
2005-01-13 18:59:48 +00:00
|
|
|
|
2004-12-01 23:16:38 +00:00
|
|
|
.vop_advlock = fifo_advlock,
|
|
|
|
.vop_close = fifo_close,
|
|
|
|
.vop_create = VOP_PANIC,
|
|
|
|
.vop_getattr = VOP_EBADF,
|
2009-06-10 14:02:22 +00:00
|
|
|
.vop_ioctl = VOP_PANIC,
|
|
|
|
.vop_kqfilter = VOP_PANIC,
|
2004-12-01 23:16:38 +00:00
|
|
|
.vop_link = VOP_PANIC,
|
|
|
|
.vop_mkdir = VOP_PANIC,
|
|
|
|
.vop_mknod = VOP_PANIC,
|
|
|
|
.vop_open = fifo_open,
|
Rework pathconf handling for FIFOs.
On the one hand, FIFOs should respect other variables not supported by
the fifofs vnode operation (such as _PC_NAME_MAX, _PC_LINK_MAX, etc.).
These values are fs-specific and must come from a fs-specific method.
On the other hand, filesystems that support FIFOs are required to
support _PC_PIPE_BUF on directory vnodes that can contain FIFOs.
Given this latter requirement, once the fs-specific VOP_PATHCONF
method supports _PC_PIPE_BUF for directories, it is also suitable for
FIFOs permitting a single VOP_PATHCONF method to be used for both
FIFOs and non-FIFOs.
To that end, retire all of the FIFO-specific pathconf methods from
filesystems and change FIFO-specific vnode operation switches to use
the existing fs-specific VOP_PATHCONF method. For fifofs, set it's
VOP_PATHCONF to VOP_PANIC since it should no longer be used.
While here, move _PC_PIPE_BUF handling out of vop_stdpathconf() so that
only filesystems supporting FIFOs will report a value. In addition,
only report a valid _PC_PIPE_BUF for directories and FIFOs.
Discussed with: bde
Reviewed by: kib (part of a larger patch)
MFC after: 1 month
Sponsored by: Chelsio Communications
Differential Revision: https://reviews.freebsd.org/D12572
2017-12-19 22:39:05 +00:00
|
|
|
.vop_pathconf = VOP_PANIC,
|
2004-12-01 23:16:38 +00:00
|
|
|
.vop_print = fifo_print,
|
2004-12-13 07:07:50 +00:00
|
|
|
.vop_read = VOP_PANIC,
|
2004-12-01 23:16:38 +00:00
|
|
|
.vop_readdir = VOP_PANIC,
|
|
|
|
.vop_readlink = VOP_PANIC,
|
|
|
|
.vop_reallocblks = VOP_PANIC,
|
|
|
|
.vop_reclaim = VOP_NULL,
|
|
|
|
.vop_remove = VOP_PANIC,
|
|
|
|
.vop_rename = VOP_PANIC,
|
|
|
|
.vop_rmdir = VOP_PANIC,
|
|
|
|
.vop_setattr = VOP_EBADF,
|
|
|
|
.vop_symlink = VOP_PANIC,
|
2004-12-13 07:07:50 +00:00
|
|
|
.vop_write = VOP_PANIC,
|
1994-05-24 10:09:53 +00:00
|
|
|
};
|
1994-09-21 03:47:43 +00:00
|
|
|
|
2003-11-10 22:21:00 +00:00
|
|
|
/*
|
|
|
|
* Dispose of fifo resources.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
fifo_cleanup(struct vnode *vp)
|
|
|
|
{
|
2012-02-23 18:37:30 +00:00
|
|
|
struct fifoinfo *fip;
|
2003-11-10 22:21:00 +00:00
|
|
|
|
2009-01-28 18:10:57 +00:00
|
|
|
ASSERT_VOP_ELOCKED(vp, "fifo_cleanup");
|
2012-02-23 18:37:30 +00:00
|
|
|
fip = vp->v_fifoinfo;
|
2003-11-16 01:11:11 +00:00
|
|
|
if (fip->fi_readers == 0 && fip->fi_writers == 0) {
|
2003-11-10 22:21:00 +00:00
|
|
|
vp->v_fifoinfo = NULL;
|
2012-02-23 18:37:30 +00:00
|
|
|
pipe_dtor(fip->fi_pipe);
|
2008-10-23 15:53:51 +00:00
|
|
|
free(fip, M_VNODE);
|
2003-11-16 01:11:11 +00:00
|
|
|
}
|
2003-11-10 22:21:00 +00:00
|
|
|
}
|
|
|
|
|
1994-05-24 10:09:53 +00:00
|
|
|
/*
|
|
|
|
* Open called to set up a new instance of a fifo or
|
|
|
|
* to find an active instance of a fifo.
|
|
|
|
*/
|
|
|
|
/* ARGSUSED */
|
1997-10-15 13:24:07 +00:00
|
|
|
static int
|
1994-05-24 10:09:53 +00:00
|
|
|
fifo_open(ap)
|
|
|
|
struct vop_open_args /* {
|
|
|
|
struct vnode *a_vp;
|
|
|
|
int a_mode;
|
|
|
|
struct ucred *a_cred;
|
2001-09-12 08:38:13 +00:00
|
|
|
struct thread *a_td;
|
2009-06-10 14:09:05 +00:00
|
|
|
struct file *a_fp;
|
1994-05-24 10:09:53 +00:00
|
|
|
} */ *ap;
|
|
|
|
{
|
2012-02-23 18:37:30 +00:00
|
|
|
struct vnode *vp;
|
|
|
|
struct file *fp;
|
|
|
|
struct thread *td;
|
1997-02-10 02:22:35 +00:00
|
|
|
struct fifoinfo *fip;
|
2012-02-23 18:37:30 +00:00
|
|
|
struct pipe *fpipe;
|
Ensure that when a blockable open of fifo returns success, a valid
file descriptor opened for complimentary access exists as well.
The implementation of the guarantee is done by counting the
generations of readers and writers opens. We return success and not
EINTR or ERESTART error, when the sleep for complimentary opening is
interrupted, but the generation was changed during the sleep.
Longer explanation: assume there are two threads, A doing open("fifo",
O_RDONLY) and B doing open("fifo", O_WRONLY), and no other threads
either trying to open the fifo, nor there are any file descriptors
referencing the fifo. Before the change, it was possible e.g. for for
thread A to return a valid file descriptor, while thread B returned
EINTR if a signal to B was delivered simultaneously with the wakeup
from A. After the change, in this situation both A::open() and
B::open() succeed and the signal is made "as if" it was noticed
slightly later. Note that the signal actual delivery is not changed,
it is done by ast on syscall return path, so signal handler is still
executed before first instruction after syscall.
See PR for the code demonstrating the issue.
PR: 203162
Reported by: Victor Stinner victor.stinner@gmail.com
Reviewed by: jilles
Tested by: bapt, pho
Sponsored by: The FreeBSD Foundation
MFC after: 1 week
2015-09-20 21:18:33 +00:00
|
|
|
u_int gen;
|
2015-01-18 15:03:26 +00:00
|
|
|
int error, stops_deferred;
|
1994-05-24 10:09:53 +00:00
|
|
|
|
2012-02-23 18:37:30 +00:00
|
|
|
vp = ap->a_vp;
|
|
|
|
fp = ap->a_fp;
|
|
|
|
td = ap->a_td;
|
2007-07-26 16:58:09 +00:00
|
|
|
ASSERT_VOP_ELOCKED(vp, "fifo_open");
|
2013-12-17 17:28:02 +00:00
|
|
|
if (fp == NULL || (ap->a_mode & FEXEC) != 0)
|
2006-03-14 19:29:45 +00:00
|
|
|
return (EINVAL);
|
1994-05-24 10:09:53 +00:00
|
|
|
if ((fip = vp->v_fifoinfo) == NULL) {
|
2014-05-02 00:52:13 +00:00
|
|
|
pipe_named_ctor(&fpipe, td);
|
2012-02-23 18:37:30 +00:00
|
|
|
fip = malloc(sizeof(*fip), M_VNODE, M_WAITOK);
|
|
|
|
fip->fi_pipe = fpipe;
|
I am comparing current pipe code with the one in 8.3-STABLE r236165,
I found 8.3 is a history BSD version using socket to implement FIFO
pipe, it uses per-file seqcount to compare with writer generation
stored in per-pipe object. The concept is after all writers are gone,
the pipe enters next generation, all old readers have not closed the
pipe should get the indication that the pipe is disconnected, result
is they should get EPIPE, SIGPIPE or get POLLHUP in poll().
But newcomer should not know that previous writters were gone, it
should treat it as a fresh session.
I am trying to bring back FIFO pipe to history behavior. It is still
unclear that if single EOF flag can represent SBS_CANTSENDMORE and
SBS_CANTRCVMORE which socket-based version is using, but I have run
the poll regression test in tool directory, output is same as the one
on 8.3-STABLE now.
I think the output "not ok 18 FIFO state 6b: poll result 0 expected 1.
expected POLLHUP; got 0" might be bogus, because newcomer should not
know that old writers were gone. I got the same behavior on Linux.
Our implementation always return POLLIN for disconnected pipe even it
should return POLLHUP, but I think it is not wise to remove POLLIN for
compatible reason, this is our history behavior.
Regression test: /usr/src/tools/regression/poll
2012-07-31 05:48:35 +00:00
|
|
|
fpipe->pipe_wgen = fip->fi_readers = fip->fi_writers = 0;
|
2012-02-23 18:37:30 +00:00
|
|
|
KASSERT(vp->v_fifoinfo == NULL, ("fifo_open: v_fifoinfo race"));
|
2003-06-13 06:58:11 +00:00
|
|
|
vp->v_fifoinfo = fip;
|
1994-05-24 10:09:53 +00:00
|
|
|
}
|
2012-02-23 18:37:30 +00:00
|
|
|
fpipe = fip->fi_pipe;
|
|
|
|
KASSERT(fpipe != NULL, ("fifo_open: pipe is NULL"));
|
2003-06-01 06:24:32 +00:00
|
|
|
|
|
|
|
/*
|
2012-02-23 18:37:30 +00:00
|
|
|
* Use the pipe mutex here, in addition to the vnode lock,
|
2009-11-06 22:29:46 +00:00
|
|
|
* in order to allow vnode lock dropping before msleep() calls
|
|
|
|
* and still avoiding missed wakeups.
|
2003-06-01 06:24:32 +00:00
|
|
|
*/
|
2012-02-23 18:37:30 +00:00
|
|
|
PIPE_LOCK(fpipe);
|
1997-02-10 02:22:35 +00:00
|
|
|
if (ap->a_mode & FREAD) {
|
|
|
|
fip->fi_readers++;
|
Ensure that when a blockable open of fifo returns success, a valid
file descriptor opened for complimentary access exists as well.
The implementation of the guarantee is done by counting the
generations of readers and writers opens. We return success and not
EINTR or ERESTART error, when the sleep for complimentary opening is
interrupted, but the generation was changed during the sleep.
Longer explanation: assume there are two threads, A doing open("fifo",
O_RDONLY) and B doing open("fifo", O_WRONLY), and no other threads
either trying to open the fifo, nor there are any file descriptors
referencing the fifo. Before the change, it was possible e.g. for for
thread A to return a valid file descriptor, while thread B returned
EINTR if a signal to B was delivered simultaneously with the wakeup
from A. After the change, in this situation both A::open() and
B::open() succeed and the signal is made "as if" it was noticed
slightly later. Note that the signal actual delivery is not changed,
it is done by ast on syscall return path, so signal handler is still
executed before first instruction after syscall.
See PR for the code demonstrating the issue.
PR: 203162
Reported by: Victor Stinner victor.stinner@gmail.com
Reviewed by: jilles
Tested by: bapt, pho
Sponsored by: The FreeBSD Foundation
MFC after: 1 week
2015-09-20 21:18:33 +00:00
|
|
|
fip->fi_rgen++;
|
1997-02-10 02:22:35 +00:00
|
|
|
if (fip->fi_readers == 1) {
|
2012-02-23 18:37:30 +00:00
|
|
|
fpipe->pipe_state &= ~PIPE_EOF;
|
|
|
|
if (fip->fi_writers > 0)
|
2003-03-02 16:54:40 +00:00
|
|
|
wakeup(&fip->fi_writers);
|
1995-08-06 16:14:21 +00:00
|
|
|
}
|
2019-06-20 23:07:20 +00:00
|
|
|
fp->f_pipegen = fpipe->pipe_wgen - fip->fi_writers;
|
1997-02-10 02:22:35 +00:00
|
|
|
}
|
|
|
|
if (ap->a_mode & FWRITE) {
|
2003-06-13 06:58:11 +00:00
|
|
|
if ((ap->a_mode & O_NONBLOCK) && fip->fi_readers == 0) {
|
2012-02-23 18:37:30 +00:00
|
|
|
PIPE_UNLOCK(fpipe);
|
2009-11-06 22:29:46 +00:00
|
|
|
if (fip->fi_writers == 0)
|
|
|
|
fifo_cleanup(vp);
|
2003-06-13 06:58:11 +00:00
|
|
|
return (ENXIO);
|
|
|
|
}
|
1997-02-10 02:22:35 +00:00
|
|
|
fip->fi_writers++;
|
Ensure that when a blockable open of fifo returns success, a valid
file descriptor opened for complimentary access exists as well.
The implementation of the guarantee is done by counting the
generations of readers and writers opens. We return success and not
EINTR or ERESTART error, when the sleep for complimentary opening is
interrupted, but the generation was changed during the sleep.
Longer explanation: assume there are two threads, A doing open("fifo",
O_RDONLY) and B doing open("fifo", O_WRONLY), and no other threads
either trying to open the fifo, nor there are any file descriptors
referencing the fifo. Before the change, it was possible e.g. for for
thread A to return a valid file descriptor, while thread B returned
EINTR if a signal to B was delivered simultaneously with the wakeup
from A. After the change, in this situation both A::open() and
B::open() succeed and the signal is made "as if" it was noticed
slightly later. Note that the signal actual delivery is not changed,
it is done by ast on syscall return path, so signal handler is still
executed before first instruction after syscall.
See PR for the code demonstrating the issue.
PR: 203162
Reported by: Victor Stinner victor.stinner@gmail.com
Reviewed by: jilles
Tested by: bapt, pho
Sponsored by: The FreeBSD Foundation
MFC after: 1 week
2015-09-20 21:18:33 +00:00
|
|
|
fip->fi_wgen++;
|
1997-02-10 02:22:35 +00:00
|
|
|
if (fip->fi_writers == 1) {
|
2012-02-23 18:37:30 +00:00
|
|
|
fpipe->pipe_state &= ~PIPE_EOF;
|
|
|
|
if (fip->fi_readers > 0)
|
2003-03-02 16:54:40 +00:00
|
|
|
wakeup(&fip->fi_readers);
|
1995-08-06 16:14:21 +00:00
|
|
|
}
|
|
|
|
}
|
2003-06-13 06:58:11 +00:00
|
|
|
if ((ap->a_mode & O_NONBLOCK) == 0) {
|
|
|
|
if ((ap->a_mode & FREAD) && fip->fi_writers == 0) {
|
Ensure that when a blockable open of fifo returns success, a valid
file descriptor opened for complimentary access exists as well.
The implementation of the guarantee is done by counting the
generations of readers and writers opens. We return success and not
EINTR or ERESTART error, when the sleep for complimentary opening is
interrupted, but the generation was changed during the sleep.
Longer explanation: assume there are two threads, A doing open("fifo",
O_RDONLY) and B doing open("fifo", O_WRONLY), and no other threads
either trying to open the fifo, nor there are any file descriptors
referencing the fifo. Before the change, it was possible e.g. for for
thread A to return a valid file descriptor, while thread B returned
EINTR if a signal to B was delivered simultaneously with the wakeup
from A. After the change, in this situation both A::open() and
B::open() succeed and the signal is made "as if" it was noticed
slightly later. Note that the signal actual delivery is not changed,
it is done by ast on syscall return path, so signal handler is still
executed before first instruction after syscall.
See PR for the code demonstrating the issue.
PR: 203162
Reported by: Victor Stinner victor.stinner@gmail.com
Reviewed by: jilles
Tested by: bapt, pho
Sponsored by: The FreeBSD Foundation
MFC after: 1 week
2015-09-20 21:18:33 +00:00
|
|
|
gen = fip->fi_wgen;
|
2008-01-13 14:44:15 +00:00
|
|
|
VOP_UNLOCK(vp, 0);
|
2016-06-26 20:07:24 +00:00
|
|
|
stops_deferred = sigdeferstop(SIGDEFERSTOP_OFF);
|
2012-02-23 18:37:30 +00:00
|
|
|
error = msleep(&fip->fi_readers, PIPE_MTX(fpipe),
|
2004-05-17 20:16:40 +00:00
|
|
|
PDROP | PCATCH | PSOCK, "fifoor", 0);
|
2016-06-26 20:07:24 +00:00
|
|
|
sigallowstop(stops_deferred);
|
2008-01-10 01:10:58 +00:00
|
|
|
vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
|
Ensure that when a blockable open of fifo returns success, a valid
file descriptor opened for complimentary access exists as well.
The implementation of the guarantee is done by counting the
generations of readers and writers opens. We return success and not
EINTR or ERESTART error, when the sleep for complimentary opening is
interrupted, but the generation was changed during the sleep.
Longer explanation: assume there are two threads, A doing open("fifo",
O_RDONLY) and B doing open("fifo", O_WRONLY), and no other threads
either trying to open the fifo, nor there are any file descriptors
referencing the fifo. Before the change, it was possible e.g. for for
thread A to return a valid file descriptor, while thread B returned
EINTR if a signal to B was delivered simultaneously with the wakeup
from A. After the change, in this situation both A::open() and
B::open() succeed and the signal is made "as if" it was noticed
slightly later. Note that the signal actual delivery is not changed,
it is done by ast on syscall return path, so signal handler is still
executed before first instruction after syscall.
See PR for the code demonstrating the issue.
PR: 203162
Reported by: Victor Stinner victor.stinner@gmail.com
Reviewed by: jilles
Tested by: bapt, pho
Sponsored by: The FreeBSD Foundation
MFC after: 1 week
2015-09-20 21:18:33 +00:00
|
|
|
if (error != 0 && gen == fip->fi_wgen) {
|
2003-06-01 06:24:32 +00:00
|
|
|
fip->fi_readers--;
|
2004-05-17 20:16:40 +00:00
|
|
|
if (fip->fi_readers == 0) {
|
2012-02-23 18:37:30 +00:00
|
|
|
PIPE_LOCK(fpipe);
|
|
|
|
fpipe->pipe_state |= PIPE_EOF;
|
|
|
|
if (fpipe->pipe_state & PIPE_WANTW)
|
|
|
|
wakeup(fpipe);
|
|
|
|
PIPE_UNLOCK(fpipe);
|
2004-05-17 20:16:40 +00:00
|
|
|
fifo_cleanup(vp);
|
|
|
|
}
|
2003-06-01 06:24:32 +00:00
|
|
|
return (error);
|
|
|
|
}
|
2012-02-23 18:37:30 +00:00
|
|
|
PIPE_LOCK(fpipe);
|
2003-03-24 11:03:42 +00:00
|
|
|
/*
|
|
|
|
* We must have got woken up because we had a writer.
|
|
|
|
* That (and not still having one) is the condition
|
|
|
|
* that we must wait for.
|
|
|
|
*/
|
1994-05-24 10:09:53 +00:00
|
|
|
}
|
2003-06-13 06:58:11 +00:00
|
|
|
if ((ap->a_mode & FWRITE) && fip->fi_readers == 0) {
|
Ensure that when a blockable open of fifo returns success, a valid
file descriptor opened for complimentary access exists as well.
The implementation of the guarantee is done by counting the
generations of readers and writers opens. We return success and not
EINTR or ERESTART error, when the sleep for complimentary opening is
interrupted, but the generation was changed during the sleep.
Longer explanation: assume there are two threads, A doing open("fifo",
O_RDONLY) and B doing open("fifo", O_WRONLY), and no other threads
either trying to open the fifo, nor there are any file descriptors
referencing the fifo. Before the change, it was possible e.g. for for
thread A to return a valid file descriptor, while thread B returned
EINTR if a signal to B was delivered simultaneously with the wakeup
from A. After the change, in this situation both A::open() and
B::open() succeed and the signal is made "as if" it was noticed
slightly later. Note that the signal actual delivery is not changed,
it is done by ast on syscall return path, so signal handler is still
executed before first instruction after syscall.
See PR for the code demonstrating the issue.
PR: 203162
Reported by: Victor Stinner victor.stinner@gmail.com
Reviewed by: jilles
Tested by: bapt, pho
Sponsored by: The FreeBSD Foundation
MFC after: 1 week
2015-09-20 21:18:33 +00:00
|
|
|
gen = fip->fi_rgen;
|
2008-01-13 14:44:15 +00:00
|
|
|
VOP_UNLOCK(vp, 0);
|
2016-06-26 20:07:24 +00:00
|
|
|
stops_deferred = sigdeferstop(SIGDEFERSTOP_OFF);
|
2012-02-23 18:37:30 +00:00
|
|
|
error = msleep(&fip->fi_writers, PIPE_MTX(fpipe),
|
2004-05-17 20:16:40 +00:00
|
|
|
PDROP | PCATCH | PSOCK, "fifoow", 0);
|
2016-06-26 20:07:24 +00:00
|
|
|
sigallowstop(stops_deferred);
|
2008-01-10 01:10:58 +00:00
|
|
|
vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
|
Ensure that when a blockable open of fifo returns success, a valid
file descriptor opened for complimentary access exists as well.
The implementation of the guarantee is done by counting the
generations of readers and writers opens. We return success and not
EINTR or ERESTART error, when the sleep for complimentary opening is
interrupted, but the generation was changed during the sleep.
Longer explanation: assume there are two threads, A doing open("fifo",
O_RDONLY) and B doing open("fifo", O_WRONLY), and no other threads
either trying to open the fifo, nor there are any file descriptors
referencing the fifo. Before the change, it was possible e.g. for for
thread A to return a valid file descriptor, while thread B returned
EINTR if a signal to B was delivered simultaneously with the wakeup
from A. After the change, in this situation both A::open() and
B::open() succeed and the signal is made "as if" it was noticed
slightly later. Note that the signal actual delivery is not changed,
it is done by ast on syscall return path, so signal handler is still
executed before first instruction after syscall.
See PR for the code demonstrating the issue.
PR: 203162
Reported by: Victor Stinner victor.stinner@gmail.com
Reviewed by: jilles
Tested by: bapt, pho
Sponsored by: The FreeBSD Foundation
MFC after: 1 week
2015-09-20 21:18:33 +00:00
|
|
|
if (error != 0 && gen == fip->fi_rgen) {
|
2003-06-01 06:24:32 +00:00
|
|
|
fip->fi_writers--;
|
2004-05-17 20:16:40 +00:00
|
|
|
if (fip->fi_writers == 0) {
|
2012-02-23 18:37:30 +00:00
|
|
|
PIPE_LOCK(fpipe);
|
|
|
|
fpipe->pipe_state |= PIPE_EOF;
|
|
|
|
if (fpipe->pipe_state & PIPE_WANTR)
|
|
|
|
wakeup(fpipe);
|
I am comparing current pipe code with the one in 8.3-STABLE r236165,
I found 8.3 is a history BSD version using socket to implement FIFO
pipe, it uses per-file seqcount to compare with writer generation
stored in per-pipe object. The concept is after all writers are gone,
the pipe enters next generation, all old readers have not closed the
pipe should get the indication that the pipe is disconnected, result
is they should get EPIPE, SIGPIPE or get POLLHUP in poll().
But newcomer should not know that previous writters were gone, it
should treat it as a fresh session.
I am trying to bring back FIFO pipe to history behavior. It is still
unclear that if single EOF flag can represent SBS_CANTSENDMORE and
SBS_CANTRCVMORE which socket-based version is using, but I have run
the poll regression test in tool directory, output is same as the one
on 8.3-STABLE now.
I think the output "not ok 18 FIFO state 6b: poll result 0 expected 1.
expected POLLHUP; got 0" might be bogus, because newcomer should not
know that old writers were gone. I got the same behavior on Linux.
Our implementation always return POLLIN for disconnected pipe even it
should return POLLHUP, but I think it is not wise to remove POLLIN for
compatible reason, this is our history behavior.
Regression test: /usr/src/tools/regression/poll
2012-07-31 05:48:35 +00:00
|
|
|
fpipe->pipe_wgen++;
|
2012-02-23 18:37:30 +00:00
|
|
|
PIPE_UNLOCK(fpipe);
|
2004-05-17 20:16:40 +00:00
|
|
|
fifo_cleanup(vp);
|
|
|
|
}
|
2003-06-13 06:58:11 +00:00
|
|
|
return (error);
|
1994-05-24 10:09:53 +00:00
|
|
|
}
|
2003-06-13 06:58:11 +00:00
|
|
|
/*
|
|
|
|
* We must have got woken up because we had
|
|
|
|
* a reader. That (and not still having one)
|
|
|
|
* is the condition that we must wait for.
|
|
|
|
*/
|
2012-02-23 18:37:30 +00:00
|
|
|
PIPE_LOCK(fpipe);
|
1994-05-24 10:09:53 +00:00
|
|
|
}
|
|
|
|
}
|
2012-02-23 18:37:30 +00:00
|
|
|
PIPE_UNLOCK(fpipe);
|
2007-05-31 11:51:53 +00:00
|
|
|
KASSERT(fp != NULL, ("can't fifo/vnode bypass"));
|
2004-12-13 10:07:57 +00:00
|
|
|
KASSERT(fp->f_ops == &badfileops, ("not badfileops in fifo_open"));
|
2012-02-23 18:37:30 +00:00
|
|
|
finit(fp, fp->f_flag, DTYPE_FIFO, fpipe, &pipeops);
|
2005-09-12 19:59:12 +00:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
1994-05-24 10:09:53 +00:00
|
|
|
/*
|
|
|
|
* Device close routine
|
|
|
|
*/
|
|
|
|
/* ARGSUSED */
|
1997-10-15 13:24:07 +00:00
|
|
|
static int
|
1994-05-24 10:09:53 +00:00
|
|
|
fifo_close(ap)
|
|
|
|
struct vop_close_args /* {
|
|
|
|
struct vnode *a_vp;
|
|
|
|
int a_fflag;
|
|
|
|
struct ucred *a_cred;
|
2001-09-12 08:38:13 +00:00
|
|
|
struct thread *a_td;
|
1994-05-24 10:09:53 +00:00
|
|
|
} */ *ap;
|
|
|
|
{
|
2012-02-23 18:37:30 +00:00
|
|
|
struct vnode *vp;
|
|
|
|
struct fifoinfo *fip;
|
|
|
|
struct pipe *cpipe;
|
2003-06-01 06:24:32 +00:00
|
|
|
|
2012-02-23 18:37:30 +00:00
|
|
|
vp = ap->a_vp;
|
|
|
|
fip = vp->v_fifoinfo;
|
|
|
|
cpipe = fip->fi_pipe;
|
2009-01-28 18:10:57 +00:00
|
|
|
ASSERT_VOP_ELOCKED(vp, "fifo_close");
|
1995-08-06 16:14:21 +00:00
|
|
|
if (ap->a_fflag & FREAD) {
|
1994-05-24 10:09:53 +00:00
|
|
|
fip->fi_readers--;
|
2012-02-23 18:37:30 +00:00
|
|
|
if (fip->fi_readers == 0) {
|
|
|
|
PIPE_LOCK(cpipe);
|
|
|
|
cpipe->pipe_state |= PIPE_EOF;
|
When a thread is blocked in direct write state, it only sets PIPE_DIRECTW
flag but not PIPE_WANTW, but FIFO pipe code does not understand this internal
state, when a FIFO peer reader closes the pipe, it wants to notify the writer,
it checks PIPE_WANTW, if not set, it skips calling wakeup(), so blocked writer
never noticed the case, but in general, the writer should return from the
syscall with EPIPE error code and may get SIGPIPE signal. Setting the
PIPE_WANTW fixed problem, or you can turn off direct write, it should fix the
problem too. This bug is found by PR/170203.
Another bug in FIFO pipe code is when peer closes the pipe, another end which
is being blocked in select() or poll() is not notified, it missed to call
pipeselwakeup().
Third problem is found in poll regression test, the existing code can not
pass 6b,6c,6d tests, but FreeBSD-4 works. This commit does not fix the
problem, I still need to study more to find the cause.
PR: 170203
Tested by: Garrett Copper < yanegomi at gmail dot com >
2012-07-31 02:00:37 +00:00
|
|
|
if ((cpipe->pipe_state & PIPE_WANTW)) {
|
|
|
|
cpipe->pipe_state &= ~PIPE_WANTW;
|
2012-02-23 18:37:30 +00:00
|
|
|
wakeup(cpipe);
|
When a thread is blocked in direct write state, it only sets PIPE_DIRECTW
flag but not PIPE_WANTW, but FIFO pipe code does not understand this internal
state, when a FIFO peer reader closes the pipe, it wants to notify the writer,
it checks PIPE_WANTW, if not set, it skips calling wakeup(), so blocked writer
never noticed the case, but in general, the writer should return from the
syscall with EPIPE error code and may get SIGPIPE signal. Setting the
PIPE_WANTW fixed problem, or you can turn off direct write, it should fix the
problem too. This bug is found by PR/170203.
Another bug in FIFO pipe code is when peer closes the pipe, another end which
is being blocked in select() or poll() is not notified, it missed to call
pipeselwakeup().
Third problem is found in poll regression test, the existing code can not
pass 6b,6c,6d tests, but FreeBSD-4 works. This commit does not fix the
problem, I still need to study more to find the cause.
PR: 170203
Tested by: Garrett Copper < yanegomi at gmail dot com >
2012-07-31 02:00:37 +00:00
|
|
|
}
|
|
|
|
pipeselwakeup(cpipe);
|
2012-02-23 18:37:30 +00:00
|
|
|
PIPE_UNLOCK(cpipe);
|
|
|
|
}
|
1994-05-24 10:09:53 +00:00
|
|
|
}
|
1997-02-10 02:22:35 +00:00
|
|
|
if (ap->a_fflag & FWRITE) {
|
|
|
|
fip->fi_writers--;
|
2009-07-07 09:43:44 +00:00
|
|
|
if (fip->fi_writers == 0) {
|
2012-02-23 18:37:30 +00:00
|
|
|
PIPE_LOCK(cpipe);
|
|
|
|
cpipe->pipe_state |= PIPE_EOF;
|
When a thread is blocked in direct write state, it only sets PIPE_DIRECTW
flag but not PIPE_WANTW, but FIFO pipe code does not understand this internal
state, when a FIFO peer reader closes the pipe, it wants to notify the writer,
it checks PIPE_WANTW, if not set, it skips calling wakeup(), so blocked writer
never noticed the case, but in general, the writer should return from the
syscall with EPIPE error code and may get SIGPIPE signal. Setting the
PIPE_WANTW fixed problem, or you can turn off direct write, it should fix the
problem too. This bug is found by PR/170203.
Another bug in FIFO pipe code is when peer closes the pipe, another end which
is being blocked in select() or poll() is not notified, it missed to call
pipeselwakeup().
Third problem is found in poll regression test, the existing code can not
pass 6b,6c,6d tests, but FreeBSD-4 works. This commit does not fix the
problem, I still need to study more to find the cause.
PR: 170203
Tested by: Garrett Copper < yanegomi at gmail dot com >
2012-07-31 02:00:37 +00:00
|
|
|
if ((cpipe->pipe_state & PIPE_WANTR)) {
|
|
|
|
cpipe->pipe_state &= ~PIPE_WANTR;
|
2012-02-23 18:37:30 +00:00
|
|
|
wakeup(cpipe);
|
When a thread is blocked in direct write state, it only sets PIPE_DIRECTW
flag but not PIPE_WANTW, but FIFO pipe code does not understand this internal
state, when a FIFO peer reader closes the pipe, it wants to notify the writer,
it checks PIPE_WANTW, if not set, it skips calling wakeup(), so blocked writer
never noticed the case, but in general, the writer should return from the
syscall with EPIPE error code and may get SIGPIPE signal. Setting the
PIPE_WANTW fixed problem, or you can turn off direct write, it should fix the
problem too. This bug is found by PR/170203.
Another bug in FIFO pipe code is when peer closes the pipe, another end which
is being blocked in select() or poll() is not notified, it missed to call
pipeselwakeup().
Third problem is found in poll regression test, the existing code can not
pass 6b,6c,6d tests, but FreeBSD-4 works. This commit does not fix the
problem, I still need to study more to find the cause.
PR: 170203
Tested by: Garrett Copper < yanegomi at gmail dot com >
2012-07-31 02:00:37 +00:00
|
|
|
}
|
I am comparing current pipe code with the one in 8.3-STABLE r236165,
I found 8.3 is a history BSD version using socket to implement FIFO
pipe, it uses per-file seqcount to compare with writer generation
stored in per-pipe object. The concept is after all writers are gone,
the pipe enters next generation, all old readers have not closed the
pipe should get the indication that the pipe is disconnected, result
is they should get EPIPE, SIGPIPE or get POLLHUP in poll().
But newcomer should not know that previous writters were gone, it
should treat it as a fresh session.
I am trying to bring back FIFO pipe to history behavior. It is still
unclear that if single EOF flag can represent SBS_CANTSENDMORE and
SBS_CANTRCVMORE which socket-based version is using, but I have run
the poll regression test in tool directory, output is same as the one
on 8.3-STABLE now.
I think the output "not ok 18 FIFO state 6b: poll result 0 expected 1.
expected POLLHUP; got 0" might be bogus, because newcomer should not
know that old writers were gone. I got the same behavior on Linux.
Our implementation always return POLLIN for disconnected pipe even it
should return POLLHUP, but I think it is not wise to remove POLLIN for
compatible reason, this is our history behavior.
Regression test: /usr/src/tools/regression/poll
2012-07-31 05:48:35 +00:00
|
|
|
cpipe->pipe_wgen++;
|
When a thread is blocked in direct write state, it only sets PIPE_DIRECTW
flag but not PIPE_WANTW, but FIFO pipe code does not understand this internal
state, when a FIFO peer reader closes the pipe, it wants to notify the writer,
it checks PIPE_WANTW, if not set, it skips calling wakeup(), so blocked writer
never noticed the case, but in general, the writer should return from the
syscall with EPIPE error code and may get SIGPIPE signal. Setting the
PIPE_WANTW fixed problem, or you can turn off direct write, it should fix the
problem too. This bug is found by PR/170203.
Another bug in FIFO pipe code is when peer closes the pipe, another end which
is being blocked in select() or poll() is not notified, it missed to call
pipeselwakeup().
Third problem is found in poll regression test, the existing code can not
pass 6b,6c,6d tests, but FreeBSD-4 works. This commit does not fix the
problem, I still need to study more to find the cause.
PR: 170203
Tested by: Garrett Copper < yanegomi at gmail dot com >
2012-07-31 02:00:37 +00:00
|
|
|
pipeselwakeup(cpipe);
|
2012-02-23 18:37:30 +00:00
|
|
|
PIPE_UNLOCK(cpipe);
|
2009-07-07 09:43:44 +00:00
|
|
|
}
|
1997-02-10 02:22:35 +00:00
|
|
|
}
|
2003-11-10 22:21:00 +00:00
|
|
|
fifo_cleanup(vp);
|
2003-06-01 06:24:32 +00:00
|
|
|
return (0);
|
1994-05-24 10:09:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
1994-10-02 17:48:58 +00:00
|
|
|
* Print out internal contents of a fifo vnode.
|
1994-05-24 10:09:53 +00:00
|
|
|
*/
|
1994-05-25 09:21:21 +00:00
|
|
|
int
|
1994-10-02 17:48:58 +00:00
|
|
|
fifo_printinfo(vp)
|
|
|
|
struct vnode *vp;
|
1994-05-24 10:09:53 +00:00
|
|
|
{
|
2017-05-17 00:34:34 +00:00
|
|
|
struct fifoinfo *fip = vp->v_fifoinfo;
|
1994-05-24 10:09:53 +00:00
|
|
|
|
2007-03-02 00:10:11 +00:00
|
|
|
if (fip == NULL){
|
|
|
|
printf(", NULL v_fifoinfo");
|
|
|
|
return (0);
|
|
|
|
}
|
1994-10-02 17:48:58 +00:00
|
|
|
printf(", fifo with %ld readers and %ld writers",
|
|
|
|
fip->fi_readers, fip->fi_writers);
|
1994-05-25 09:21:21 +00:00
|
|
|
return (0);
|
1994-05-24 10:09:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
1994-10-02 17:48:58 +00:00
|
|
|
* Print out the contents of a fifo vnode.
|
1994-05-24 10:09:53 +00:00
|
|
|
*/
|
1995-12-14 09:55:16 +00:00
|
|
|
static int
|
1994-10-02 17:48:58 +00:00
|
|
|
fifo_print(ap)
|
|
|
|
struct vop_print_args /* {
|
|
|
|
struct vnode *a_vp;
|
|
|
|
} */ *ap;
|
1994-05-24 10:09:53 +00:00
|
|
|
{
|
2009-02-06 20:06:48 +00:00
|
|
|
printf(" ");
|
1994-10-02 17:48:58 +00:00
|
|
|
fifo_printinfo(ap->a_vp);
|
|
|
|
printf("\n");
|
1994-05-25 09:21:21 +00:00
|
|
|
return (0);
|
1994-05-24 10:09:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Fifo advisory byte-level locks.
|
|
|
|
*/
|
|
|
|
/* ARGSUSED */
|
1997-10-15 13:24:07 +00:00
|
|
|
static int
|
1994-05-24 10:09:53 +00:00
|
|
|
fifo_advlock(ap)
|
|
|
|
struct vop_advlock_args /* {
|
|
|
|
struct vnode *a_vp;
|
|
|
|
caddr_t a_id;
|
|
|
|
int a_op;
|
|
|
|
struct flock *a_fl;
|
|
|
|
int a_flags;
|
|
|
|
} */ *ap;
|
|
|
|
{
|
|
|
|
|
1996-12-19 18:16:33 +00:00
|
|
|
return (ap->a_flags & F_FLOCK ? EOPNOTSUPP : EINVAL);
|
1994-05-24 10:09:53 +00:00
|
|
|
}
|
2004-11-15 14:51:44 +00:00
|
|
|
|