-/* $OpenBSD: sys_socket.c,v 1.61 2023/04/15 13:18:28 kn Exp $ */
+/* $OpenBSD: sys_socket.c,v 1.62 2024/03/26 09:46:47 mvs Exp $ */
/* $NetBSD: sys_socket.c,v 1.13 1995/08/12 23:59:09 mycroft Exp $ */
/*
memset(ub, 0, sizeof (*ub));
ub->st_mode = S_IFSOCK;
solock(so);
+ mtx_enter(&so->so_rcv.sb_mtx);
if ((so->so_rcv.sb_state & SS_CANTRCVMORE) == 0 ||
so->so_rcv.sb_cc != 0)
ub->st_mode |= S_IRUSR | S_IRGRP | S_IROTH;
+ mtx_leave(&so->so_rcv.sb_mtx);
if ((so->so_snd.sb_state & SS_CANTSENDMORE) == 0)
ub->st_mode |= S_IWUSR | S_IWGRP | S_IWOTH;
ub->st_uid = so->so_euid;
-/* $OpenBSD: uipc_socket.c,v 1.321 2024/03/22 17:34:11 mvs Exp $ */
+/* $OpenBSD: uipc_socket.c,v 1.322 2024/03/26 09:46:47 mvs Exp $ */
/* $NetBSD: uipc_socket.c,v 1.21 1996/02/04 02:17:52 christos Exp $ */
/*
break;
}
break;
+ case AF_UNIX:
+ so->so_rcv.sb_flags |= SB_MTXLOCK;
+ break;
}
return (so);
* Dispose of any SCM_RIGHTS message that went
* through the read path rather than recv.
*/
- if (pr->pr_domain->dom_dispose)
+ if (pr->pr_domain->dom_dispose) {
+ sb_mtx_unlock(&so->so_rcv);
pr->pr_domain->dom_dispose(cm);
+ sb_mtx_lock(&so->so_rcv);
+ }
m_free(cm);
}
}
}
SBLASTRECORDCHK(&so->so_rcv, "soreceive 4");
SBLASTMBUFCHK(&so->so_rcv, "soreceive 4");
- if (pr->pr_flags & PR_WANTRCVD)
+ if (pr->pr_flags & PR_WANTRCVD) {
+ sb_mtx_unlock(&so->so_rcv);
pru_rcvd(so);
+ sb_mtx_lock(&so->so_rcv);
+ }
}
if (orig_resid == uio->uio_resid && orig_resid &&
(flags & MSG_EOR) == 0 &&
/* with SBL_WAIT and SLB_NOINTR sblock() must not fail */
KASSERT(error == 0);
socantrcvmore(so);
+ mtx_enter(&sb->sb_mtx);
m = sb->sb_mb;
memset(&sb->sb_startzero, 0,
(caddr_t)&sb->sb_endzero - (caddr_t)&sb->sb_startzero);
- mtx_enter(&sb->sb_mtx);
sb->sb_timeo_nsecs = INFSLP;
mtx_leave(&sb->sb_mtx);
sbunlock(so, sb);
void
sorwakeup(struct socket *so)
{
- soassertlocked_readonly(so);
+ if ((so->so_rcv.sb_flags & SB_MTXLOCK) == 0)
+ soassertlocked_readonly(so);
#ifdef SOCKET_SPLICE
if (so->so_rcv.sb_flags & SB_SPLICE) {
cnt = 1;
solock(so);
+ mtx_enter(&sb->sb_mtx);
+
switch (optname) {
case SO_SNDBUF:
case SO_RCVBUF:
sb->sb_hiwat : cnt;
break;
}
+
+ mtx_leave(&sb->sb_mtx);
sounlock(so);
+
break;
}
}
}
-static inline void
-sofilt_assert_locked(struct socket *so, struct sockbuf *sb)
-{
- MUTEX_ASSERT_LOCKED(&sb->sb_mtx);
- soassertlocked_readonly(so);
-}
-
int
soo_kqfilter(struct file *fp, struct knote *kn)
{
struct socket *so = kn->kn_fp->f_data;
int rv = 0;
- sofilt_assert_locked(so, &so->so_rcv);
+ MUTEX_ASSERT_LOCKED(&so->so_rcv.sb_mtx);
+ if ((so->so_rcv.sb_flags & SB_MTXLOCK) == 0)
+ soassertlocked_readonly(so);
if (so->so_options & SO_ACCEPTCONN) {
+ if (so->so_rcv.sb_flags & SB_MTXLOCK)
+ soassertlocked_readonly(so);
+
kn->kn_data = so->so_qlen;
rv = (kn->kn_data != 0);
struct socket *so = kn->kn_fp->f_data;
int rv;
- sofilt_assert_locked(so, &so->so_snd);
+ MUTEX_ASSERT_LOCKED(&so->so_snd.sb_mtx);
+ soassertlocked_readonly(so);
kn->kn_data = sbspace(so, &so->so_snd);
if (so->so_snd.sb_state & SS_CANTSENDMORE) {
struct socket *so = kn->kn_fp->f_data;
int rv = 0;
- sofilt_assert_locked(so, &so->so_rcv);
+ MUTEX_ASSERT_LOCKED(&so->so_rcv.sb_mtx);
+ if ((so->so_rcv.sb_flags & SB_MTXLOCK) == 0)
+ soassertlocked_readonly(so);
#ifdef SOCKET_SPLICE
if (isspliced(so)) {
-/* $OpenBSD: uipc_socket2.c,v 1.144 2024/02/12 22:48:27 mvs Exp $ */
+/* $OpenBSD: uipc_socket2.c,v 1.145 2024/03/26 09:46:47 mvs Exp $ */
/* $NetBSD: uipc_socket2.c,v 1.11 1996/02/04 02:17:55 christos Exp $ */
/*
soassertlocked(so);
so->so_state &= ~SS_ISCONNECTING;
so->so_state |= SS_ISDISCONNECTING;
+ mtx_enter(&so->so_rcv.sb_mtx);
so->so_rcv.sb_state |= SS_CANTRCVMORE;
+ mtx_leave(&so->so_rcv.sb_mtx);
so->so_snd.sb_state |= SS_CANTSENDMORE;
wakeup(&so->so_timeo);
sowwakeup(so);
soassertlocked(so);
so->so_state &= ~(SS_ISCONNECTING|SS_ISCONNECTED|SS_ISDISCONNECTING);
so->so_state |= SS_ISDISCONNECTED;
+ mtx_enter(&so->so_rcv.sb_mtx);
so->so_rcv.sb_state |= SS_CANTRCVMORE;
+ mtx_leave(&so->so_rcv.sb_mtx);
so->so_snd.sb_state |= SS_CANTSENDMORE;
wakeup(&so->so_timeo);
sowwakeup(so);
mtx_enter(&head->so_snd.sb_mtx);
so->so_snd.sb_timeo_nsecs = head->so_snd.sb_timeo_nsecs;
mtx_leave(&head->so_snd.sb_mtx);
+
+ mtx_enter(&head->so_rcv.sb_mtx);
so->so_rcv.sb_wat = head->so_rcv.sb_wat;
so->so_rcv.sb_lowat = head->so_rcv.sb_lowat;
- mtx_enter(&head->so_rcv.sb_mtx);
so->so_rcv.sb_timeo_nsecs = head->so_rcv.sb_timeo_nsecs;
mtx_leave(&head->so_rcv.sb_mtx);
if (sbreserve(so, &so->so_snd, sndcc))
goto bad;
- if (sbreserve(so, &so->so_rcv, rcvcc))
- goto bad2;
so->so_snd.sb_wat = sndcc;
- so->so_rcv.sb_wat = rcvcc;
- if (so->so_rcv.sb_lowat == 0)
- so->so_rcv.sb_lowat = 1;
if (so->so_snd.sb_lowat == 0)
so->so_snd.sb_lowat = MCLBYTES;
if (so->so_snd.sb_lowat > so->so_snd.sb_hiwat)
so->so_snd.sb_lowat = so->so_snd.sb_hiwat;
+
+ mtx_enter(&so->so_rcv.sb_mtx);
+ if (sbreserve(so, &so->so_rcv, rcvcc)) {
+ mtx_leave(&so->so_rcv.sb_mtx);
+ goto bad2;
+ }
+ so->so_rcv.sb_wat = rcvcc;
+ if (so->so_rcv.sb_lowat == 0)
+ so->so_rcv.sb_lowat = 1;
+ mtx_leave(&so->so_rcv.sb_mtx);
+
return (0);
bad2:
sbrelease(so, &so->so_snd);
int
sbreserve(struct socket *so, struct sockbuf *sb, u_long cc)
{
- KASSERT(sb == &so->so_rcv || sb == &so->so_snd);
- soassertlocked(so);
+ sbmtxassertlocked(so, sb);
if (cc == 0 || cc > sb_max)
return (1);
if (m == NULL)
return;
- soassertlocked(so);
+ sbmtxassertlocked(so, sb);
SBLASTRECORDCHK(sb, "sbappend 1");
if ((n = sb->sb_lastrecord) != NULL) {
{
struct mbuf *m;
- KASSERT(sb == &so->so_rcv || sb == &so->so_snd);
- soassertlocked(so);
+ sbmtxassertlocked(so, sb);
if (m0 == NULL)
return;
struct mbuf *m, *mlast, *n;
int eor = 0, space = 0;
+ sbmtxassertlocked(so, sb);
+
if (control == NULL)
panic("sbappendcontrol");
for (m = control; ; m = m->m_next) {
struct mbuf *m, *mn;
struct mbuf *next;
- KASSERT(sb == &so->so_rcv || sb == &so->so_snd);
- soassertlocked(so);
+ sbmtxassertlocked(so, sb);
next = (m = sb->sb_mb) ? m->m_nextpkt : NULL;
while (len > 0) {
-/* $OpenBSD: uipc_usrreq.c,v 1.202 2024/03/22 17:34:11 mvs Exp $ */
+/* $OpenBSD: uipc_usrreq.c,v 1.203 2024/03/26 09:46:47 mvs Exp $ */
/* $NetBSD: uipc_usrreq.c,v 1.18 1996/02/09 19:00:50 christos Exp $ */
/*
* Adjust backpressure on sender
* and wakeup any waiting to write.
*/
+ mtx_enter(&so->so_rcv.sb_mtx);
so2->so_snd.sb_mbcnt = so->so_rcv.sb_mbcnt;
so2->so_snd.sb_cc = so->so_rcv.sb_cc;
+ mtx_leave(&so->so_rcv.sb_mtx);
sowwakeup(so2);
sounlock(so2);
}
uipc_send(struct socket *so, struct mbuf *m, struct mbuf *nam,
struct mbuf *control)
{
+ struct unpcb *unp = sotounpcb(so);
struct socket *so2;
- int error = 0;
+ int error = 0, dowakeup = 0;
if (control) {
sounlock(so);
error = EPIPE;
goto dispose;
}
- if ((so2 = unp_solock_peer(so)) == NULL) {
+ if (unp->unp_conn == NULL) {
error = ENOTCONN;
goto dispose;
}
+ so2 = unp->unp_conn->unp_socket;
+
/*
* Send to paired receive port, and then raise
* send buffer counts to maintain backpressure.
* Wake up readers.
*/
+ mtx_enter(&so2->so_rcv.sb_mtx);
if (control) {
if (sbappendcontrol(so2, &so2->so_rcv, m, control)) {
control = NULL;
} else {
- sounlock(so2);
+ mtx_leave(&so2->so_rcv.sb_mtx);
error = ENOBUFS;
goto dispose;
}
so->so_snd.sb_mbcnt = so2->so_rcv.sb_mbcnt;
so->so_snd.sb_cc = so2->so_rcv.sb_cc;
if (so2->so_rcv.sb_cc > 0)
+ dowakeup = 1;
+ mtx_leave(&so2->so_rcv.sb_mtx);
+
+ if (dowakeup)
sorwakeup(so2);
- sounlock(so2);
m = NULL;
dispose:
struct unpcb *unp = sotounpcb(so);
struct socket *so2;
const struct sockaddr *from;
- int error = 0;
+ int error = 0, dowakeup = 0;
if (control) {
sounlock(so);
goto dispose;
}
- if ((so2 = unp_solock_peer(so)) == NULL) {
+ if (unp->unp_conn == NULL) {
if (nam != NULL)
error = ECONNREFUSED;
else
goto dispose;
}
+ so2 = unp->unp_conn->unp_socket;
+
if (unp->unp_addr)
from = mtod(unp->unp_addr, struct sockaddr *);
else
from = &sun_noname;
+
+ mtx_enter(&so2->so_rcv.sb_mtx);
if (sbappendaddr(so2, &so2->so_rcv, from, m, control)) {
- sorwakeup(so2);
+ dowakeup = 1;
m = NULL;
control = NULL;
} else
error = ENOBUFS;
+ mtx_leave(&so2->so_rcv.sb_mtx);
- if (so2 != so)
- sounlock(so2);
-
+ if (dowakeup)
+ sorwakeup(so2);
if (nam)
unp_disconnect(unp);
if ((unp->unp_gcflags & UNP_GCDEAD) == 0)
continue;
so = unp->unp_socket;
- solock(so);
+ mtx_enter(&so->so_rcv.sb_mtx);
unp_scan(so->so_rcv.sb_mb, unp_remove_gcrefs);
- sounlock(so);
+ mtx_leave(&so->so_rcv.sb_mtx);
}
/*
unp->unp_gcflags &= ~UNP_GCDEAD;
so = unp->unp_socket;
- solock(so);
+ mtx_enter(&so->so_rcv.sb_mtx);
unp_scan(so->so_rcv.sb_mb, unp_restore_gcrefs);
- sounlock(so);
+ mtx_leave(&so->so_rcv.sb_mtx);
KASSERT(nunref > 0);
nunref--;
-/* $OpenBSD: fifo_vnops.c,v 1.103 2024/02/03 22:50:09 mvs Exp $ */
+/* $OpenBSD: fifo_vnops.c,v 1.104 2024/03/26 09:46:47 mvs Exp $ */
/* $NetBSD: fifo_vnops.c,v 1.18 1996/03/16 23:52:42 christos Exp $ */
/*
if (fip->fi_writers == 1) {
solock(rso);
rso->so_state &= ~SS_ISDISCONNECTED;
+ mtx_enter(&rso->so_rcv.sb_mtx);
rso->so_rcv.sb_state &= ~SS_CANTRCVMORE;
+ mtx_leave(&rso->so_rcv.sb_mtx);
sounlock(rso);
if (fip->fi_readers > 0)
wakeup(&fip->fi_readers);
struct socket *so = kn->kn_hook;
int rv;
- soassertlocked(so);
MUTEX_ASSERT_LOCKED(&so->so_rcv.sb_mtx);
kn->kn_data = so->so_rcv.sb_cc;
struct socket *so = kn->kn_hook;
int rv = 0;
- soassertlocked(so);
MUTEX_ASSERT_LOCKED(&so->so_rcv.sb_mtx);
if (kn->kn_flags & __EV_POLL) {
-/* $OpenBSD: socketvar.h,v 1.125 2024/03/22 17:34:11 mvs Exp $ */
+/* $OpenBSD: socketvar.h,v 1.126 2024/03/26 09:46:47 mvs Exp $ */
/* $NetBSD: socketvar.h,v 1.18 1996/02/09 18:25:38 christos Exp $ */
/*-
static inline long
sbspace(struct socket *so, struct sockbuf *sb)
{
- soassertlocked_readonly(so);
+ if (sb->sb_flags & SB_MTXLOCK)
+ sbmtxassertlocked(so, sb);
+ else
+ soassertlocked_readonly(so);
return lmin(sb->sb_hiwat - sb->sb_cc, sb->sb_mbmax - sb->sb_mbcnt);
}