Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions drivers/pipes/pipe_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -345,9 +345,20 @@ int pipecommon_close(FAR struct file *filep)

if (--dev->d_nwriters <= 0)
{
/* Inform poll readers that other end closed. */
/* Inform poll readers that other end closed. Match
* pipecommon_poll(), which reports POLLHUP only under
* buffer policy 0: under policy 1 the data stays valid for
* the next reader, so the loss of the last writer is not an
* end-of-file condition. A datagram socket opens and closes
* its sending FIFO for every packet, so reporting POLLHUP
* here would flag a hangup on each datagram.
*/

poll_notify(dev->d_fds, CONFIG_DEV_PIPE_NPOLLWAITERS, POLLHUP);
if (PIPE_IS_POLICY_0(dev->d_flags))
{
poll_notify(dev->d_fds, CONFIG_DEV_PIPE_NPOLLWAITERS,
POLLHUP);
}

pipecommon_wakeup(&dev->d_rdsem);
}
Expand Down
8 changes: 8 additions & 0 deletions net/local/local.h
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,14 @@ struct local_conn_s
mutex_t lc_sendlock; /* Make sending multi-thread safe */
mutex_t lc_polllock; /* Lock for net poll */

#ifdef CONFIG_NET_LOCAL_DGRAM
struct sockaddr_un lc_daddr; /* DGRAM: peer address set by connect() */
bool lc_dconnected; /* DGRAM: lc_daddr is valid. Kept apart from
* lc_state so that bind() and connect()
* do not overwrite each other.
*/
#endif

#ifdef CONFIG_NET_LOCAL_STREAM
/* SOCK_STREAM fields common to both client and server */

Expand Down
48 changes: 48 additions & 0 deletions net/local/local_bind.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ int psock_local_bind(FAR struct socket *psock,
FAR const struct sockaddr_un *unaddr =
(FAR const struct sockaddr_un *)addr;
int index;
#ifdef CONFIG_NET_LOCAL_DGRAM
int ret;
#endif

DEBUGASSERT(unaddr->sun_family == AF_LOCAL);

Expand All @@ -64,6 +67,16 @@ int psock_local_bind(FAR struct socket *psock,

conn = psock->s_conn;

/* A socket may only be bound once. Rebinding a DGRAM socket would open
* a second receiver FIFO over the first one, leaking its reader. An
* unbound socket has an empty lc_path.
*/

if (conn->lc_path[0] != '\0')
{
return -EINVAL;
}

/* Check if local address is already in use */

local_lock();
Expand Down Expand Up @@ -100,6 +113,41 @@ int psock_local_bind(FAR struct socket *psock,

strlcpy(conn->lc_path, &unaddr->sun_path[index], sizeof(conn->lc_path));

#ifdef CONFIG_NET_LOCAL_DGRAM
if (psock->s_type == SOCK_DGRAM)
{
ret = local_create_halfduplex(conn, conn->lc_path, conn->lc_rcvsize);
if (ret < 0)
{
goto errout;
}

ret = local_open_receiver(conn,
Comment thread
casaroli marked this conversation as resolved.
Resolved
Comment thread
casaroli marked this conversation as resolved.
Resolved
_SS_ISNONBLOCK(conn->lc_conn.s_flags));
if (ret < 0)
{
local_release_halfduplex(conn);
goto errout;
}
}
#endif

/* A DGRAM connect() records its default peer in lc_dconnected rather than
* in lc_state, so binding after connect() keeps the peer and the address
* stays protected by the in-use check above.
*/

conn->lc_state = LOCAL_STATE_BOUND;

return OK;

#ifdef CONFIG_NET_LOCAL_DGRAM
errout:

/* Leave the socket unbound so that it can be bound again. */

conn->lc_path[0] = '\0';
conn->lc_type = LOCAL_TYPE_UNNAMED;
return ret;
#endif
}
30 changes: 28 additions & 2 deletions net/local/local_netpoll.c
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,26 @@ int local_pollsetup(FAR struct socket *psock, FAR struct pollfd_s *fds)

if (conn->lc_proto == SOCK_DGRAM)
{
return -ENOSYS;
/* A datagram send opens its FIFO on demand and has no send buffer to
* wait for, so it is always possible to send. Report POLLOUT
* immediately rather than polling lc_infile, which is read-only and
* would never become writable.
*/

if ((fds->events & POLLOUT) != 0)
{
fds->revents |= POLLOUT;
poll_notify(&fds, 1, POLLOUT);
}

/* Only an bound socket has a receiver FIFO to watch for input. */

if ((fds->events & POLLIN) != 0 && conn->lc_infile.f_inode != NULL)
{
return file_poll(&conn->lc_infile, fds, true);
}

return OK;
}

#ifdef CONFIG_NET_LOCAL_STREAM
Expand Down Expand Up @@ -310,7 +329,14 @@ int local_pollteardown(FAR struct socket *psock, FAR struct pollfd_s *fds)

if (conn->lc_proto == SOCK_DGRAM)
{
return -ENOSYS;
/* Only a registered poll (fds->priv set by setup) needs teardown. */

if (fds->priv != NULL)
{
return file_poll(&conn->lc_infile, fds, false);
Comment thread
xiaoxiang781216 marked this conversation as resolved.
Resolved
}

return OK;
}

#ifdef CONFIG_NET_LOCAL_STREAM
Expand Down
21 changes: 21 additions & 0 deletions net/local/local_recvmsg.c
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,7 @@ psock_stream_recvfrom(FAR struct socket *psock, FAR void *buf, size_t len,
if (flags & MSG_DONTWAIT)
{
int data_len = 0;

ret = file_ioctl(&conn->lc_infile, FIONREAD, &data_len);
if (ret < 0)
{
Expand Down Expand Up @@ -411,6 +412,26 @@ psock_dgram_recvfrom(FAR struct socket *psock, FAR void *buf, size_t len,
goto errout_with_halfduplex;
}
}
else if ((flags & MSG_DONTWAIT) != 0)
{
/* bind() opened the FIFO in the blocking mode of the socket, so a
* read would wait even though the caller asked not to. Check for
* data first, as psock_stream_recvfrom() does.
*/

int data_len = 0;

ret = file_ioctl(&conn->lc_infile, FIONREAD, &data_len);
if (ret < 0)
{
return ret;
}

if (data_len == 0)
{
return -EAGAIN;
}
}

readlen = sizeof(addrlen);
ret = psock_fifo_read(psock, &addrlen, offset, &readlen, flags, false);
Expand Down
Loading
Loading