Fix stat(2) on a listening socket.

This commit is contained in:
Gleb Smirnoff 2017-06-09 15:54:48 +00:00
parent 52453d22f2
commit 438902edbd
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=319754

View File

@ -285,7 +285,6 @@ soo_stat(struct file *fp, struct stat *ub, struct ucred *active_cred,
struct thread *td)
{
struct socket *so = fp->f_data;
struct sockbuf *sb;
#ifdef MAC
int error;
#endif
@ -297,22 +296,26 @@ soo_stat(struct file *fp, struct stat *ub, struct ucred *active_cred,
if (error)
return (error);
#endif
/*
* If SBS_CANTRCVMORE is set, but there's still data left in the
* receive buffer, the socket is still readable.
*/
sb = &so->so_rcv;
SOCKBUF_LOCK(sb);
if ((sb->sb_state & SBS_CANTRCVMORE) == 0 || sbavail(sb))
ub->st_mode |= S_IRUSR | S_IRGRP | S_IROTH;
ub->st_size = sbavail(sb) - sb->sb_ctl;
SOCKBUF_UNLOCK(sb);
if (!SOLISTENING(so)) {
struct sockbuf *sb;
sb = &so->so_snd;
SOCKBUF_LOCK(sb);
if ((sb->sb_state & SBS_CANTSENDMORE) == 0)
ub->st_mode |= S_IWUSR | S_IWGRP | S_IWOTH;
SOCKBUF_UNLOCK(sb);
/*
* If SBS_CANTRCVMORE is set, but there's still data left
* in the receive buffer, the socket is still readable.
*/
sb = &so->so_rcv;
SOCKBUF_LOCK(sb);
if ((sb->sb_state & SBS_CANTRCVMORE) == 0 || sbavail(sb))
ub->st_mode |= S_IRUSR | S_IRGRP | S_IROTH;
ub->st_size = sbavail(sb) - sb->sb_ctl;
SOCKBUF_UNLOCK(sb);
sb = &so->so_snd;
SOCKBUF_LOCK(sb);
if ((sb->sb_state & SBS_CANTSENDMORE) == 0)
ub->st_mode |= S_IWUSR | S_IWGRP | S_IWOTH;
SOCKBUF_UNLOCK(sb);
}
ub->st_uid = so->so_cred->cr_uid;
ub->st_gid = so->so_cred->cr_gid;
return (*so->so_proto->pr_usrreqs->pru_sense)(so, ub);