don't allow process to attach to itself
[unix-history] / usr / src / sys / kern / uipc_socket.c
index f8aa8ef..1fe0828 100644 (file)
@@ -4,20 +4,21 @@
  *
  * %sccs.include.redist.c%
  *
  *
  * %sccs.include.redist.c%
  *
- *     @(#)uipc_socket.c       7.27.1.1 (Berkeley) %G%
+ *     @(#)uipc_socket.c       7.37 (Berkeley) %G%
  */
 
  */
 
-#include "param.h"
-#include "proc.h"
-#include "file.h"
-#include "malloc.h"
-#include "mbuf.h"
-#include "domain.h"
-#include "kernel.h"
-#include "protosw.h"
-#include "socket.h"
-#include "socketvar.h"
-#include "resourcevar.h"
+#include <sys/param.h>
+#include <sys/systm.h>
+#include <sys/proc.h>
+#include <sys/file.h>
+#include <sys/malloc.h>
+#include <sys/mbuf.h>
+#include <sys/domain.h>
+#include <sys/kernel.h>
+#include <sys/protosw.h>
+#include <sys/socket.h>
+#include <sys/socketvar.h>
+#include <sys/resourcevar.h>
 
 /*
  * Socket operation routines.
 
 /*
  * Socket operation routines.
@@ -28,6 +29,7 @@
  */
 /*ARGSUSED*/
 socreate(dom, aso, type, proto)
  */
 /*ARGSUSED*/
 socreate(dom, aso, type, proto)
+       int dom;
        struct socket **aso;
        register int type;
        int proto;
        struct socket **aso;
        register int type;
        int proto;
@@ -256,6 +258,7 @@ bad:
        return (error);
 }
 
        return (error);
 }
 
+#define        SBLOCKWAIT(f)   (((f) & MSG_DONTWAIT) ? M_NOWAIT : M_WAITOK)
 /*
  * Send on a socket.
  * If send must go all at once and message is larger than
 /*
  * Send on a socket.
  * If send must go all at once and message is larger than
@@ -301,7 +304,7 @@ sosend(so, addr, uio, top, control, flags)
 #define        snderr(errno)   { error = errno; splx(s); goto release; }
 
 restart:
 #define        snderr(errno)   { error = errno; splx(s); goto release; }
 
 restart:
-       if (error = sblock(&so->so_snd))
+       if (error = sblock(&so->so_snd, SBLOCKWAIT(flags)))
                goto out;
        do {
                s = splnet();
                goto out;
        do {
                s = splnet();
@@ -320,11 +323,11 @@ restart:
                space = sbspace(&so->so_snd);
                if (flags & MSG_OOB)
                        space += 1024;
                space = sbspace(&so->so_snd);
                if (flags & MSG_OOB)
                        space += 1024;
-               if (space < resid + clen &&
+               if (atomic && resid > so->so_snd.sb_hiwat ||
+                   clen > so->so_snd.sb_hiwat)
+                       snderr(EMSGSIZE);
+               if (space < resid + clen && uio &&
                    (atomic || space < so->so_snd.sb_lowat || space < clen)) {
                    (atomic || space < so->so_snd.sb_lowat || space < clen)) {
-                       if (atomic && resid > so->so_snd.sb_hiwat ||
-                           clen > so->so_snd.sb_hiwat)
-                               snderr(EMSGSIZE);
                        if (so->so_state & SS_NBIO)
                                snderr(EWOULDBLOCK);
                        sbunlock(&so->so_snd);
                        if (so->so_state & SS_NBIO)
                                snderr(EWOULDBLOCK);
                        sbunlock(&so->so_snd);
@@ -447,7 +450,6 @@ soreceive(so, paddr, uio, mp0, controlp, flagsp)
        struct mbuf **controlp;
        int *flagsp;
 {
        struct mbuf **controlp;
        int *flagsp;
 {
-       struct proc *p = curproc;               /* XXX */
        register struct mbuf *m, **mp;
        register int flags, len, error, s, offset;
        struct protosw *pr = so->so_proto;
        register struct mbuf *m, **mp;
        register int flags, len, error, s, offset;
        struct protosw *pr = so->so_proto;
@@ -486,7 +488,7 @@ bad:
                    (struct mbuf *)0, (struct mbuf *)0);
 
 restart:
                    (struct mbuf *)0, (struct mbuf *)0);
 
 restart:
-       if (error = sblock(&so->so_rcv))
+       if (error = sblock(&so->so_rcv, SBLOCKWAIT(flags)))
                return (error);
        s = splnet();
 
                return (error);
        s = splnet();
 
@@ -497,11 +499,13 @@ restart:
         *   1. the current count is less than the low water mark, or
         *   2. MSG_WAITALL is set, and it is possible to do the entire
         *      receive operation at once if we block (resid <= hiwat).
         *   1. the current count is less than the low water mark, or
         *   2. MSG_WAITALL is set, and it is possible to do the entire
         *      receive operation at once if we block (resid <= hiwat).
+        *   3. MSG_DONTWAIT is not set
         * If MSG_WAITALL is set but resid is larger than the receive buffer,
         * we have to do the receive in sections, and thus risk returning
         * a short count if a timeout or signal occurs after we start.
         */
         * If MSG_WAITALL is set but resid is larger than the receive buffer,
         * we have to do the receive in sections, and thus risk returning
         * a short count if a timeout or signal occurs after we start.
         */
-       while (m == 0 || so->so_rcv.sb_cc < uio->uio_resid &&
+       if (m == 0 || ((flags & MSG_DONTWAIT) == 0 &&
+           so->so_rcv.sb_cc < uio->uio_resid) &&
            (so->so_rcv.sb_cc < so->so_rcv.sb_lowat ||
            ((flags & MSG_WAITALL) && uio->uio_resid <= so->so_rcv.sb_hiwat)))
                if (m && (m->m_nextpkt || (m->m_flags & M_EOR) ||
            (so->so_rcv.sb_cc < so->so_rcv.sb_lowat ||
            ((flags & MSG_WAITALL) && uio->uio_resid <= so->so_rcv.sb_hiwat)))
                if (m && (m->m_nextpkt || (m->m_flags & M_EOR) ||
@@ -513,7 +517,7 @@ restart:
 #endif
                if (so->so_error) {
                        if (m)
 #endif
                if (so->so_error) {
                        if (m)
-                               break;
+                               goto dontblock;
                        error = so->so_error;
                        if ((flags & MSG_PEEK) == 0)
                                so->so_error = 0;
                        error = so->so_error;
                        if ((flags & MSG_PEEK) == 0)
                                so->so_error = 0;
@@ -521,7 +525,7 @@ restart:
                }
                if (so->so_state & SS_CANTRCVMORE) {
                        if (m)
                }
                if (so->so_state & SS_CANTRCVMORE) {
                        if (m)
-                               break;
+                               goto dontblock;
                        else
                                goto release;
                }
                        else
                                goto release;
                }
@@ -532,7 +536,7 @@ restart:
                }
                if (uio->uio_resid == 0)
                        goto release;
                }
                if (uio->uio_resid == 0)
                        goto release;
-               if (so->so_state & SS_NBIO) {
+               if ((so->so_state & SS_NBIO) || (flags & MSG_DONTWAIT)) {
                        error = EWOULDBLOCK;
                        goto release;
                }
                        error = EWOULDBLOCK;
                        goto release;
                }
@@ -543,7 +547,8 @@ restart:
                        return (error);
                goto restart;
        }
                        return (error);
                goto restart;
        }
-       p->p_stats->p_ru.ru_msgrcv++;
+       if (uio->uio_procp)
+               uio->uio_procp->p_stats->p_ru.ru_msgrcv++;
        nextrecord = m->m_nextpkt;
        record_eor = m->m_flags & M_EOR;
        if (pr->pr_flags & PR_ADDR) {
        nextrecord = m->m_nextpkt;
        record_eor = m->m_flags & M_EOR;
        if (pr->pr_flags & PR_ADDR) {
@@ -742,7 +747,7 @@ sorflush(so)
        struct sockbuf asb;
 
        sb->sb_flags |= SB_NOINTR;
        struct sockbuf asb;
 
        sb->sb_flags |= SB_NOINTR;
-       (void) sblock(sb);
+       (void) sblock(sb, M_WAITOK);
        s = splimp();
        socantrcvmore(so);
        sbunlock(sb);
        s = splimp();
        socantrcvmore(so);
        sbunlock(sb);
@@ -784,6 +789,7 @@ sosetopt(so, level, optname, m0)
                case SO_USELOOPBACK:
                case SO_BROADCAST:
                case SO_REUSEADDR:
                case SO_USELOOPBACK:
                case SO_BROADCAST:
                case SO_REUSEADDR:
+               case SO_REUSEPORT:
                case SO_OOBINLINE:
                        if (m == NULL || m->m_len < sizeof (int)) {
                                error = EINVAL;
                case SO_OOBINLINE:
                        if (m == NULL || m->m_len < sizeof (int)) {
                                error = EINVAL;
@@ -857,6 +863,10 @@ sosetopt(so, level, optname, m0)
                        error = ENOPROTOOPT;
                        break;
                }
                        error = ENOPROTOOPT;
                        break;
                }
+               m = 0;
+               if (error == 0 && so->so_proto && so->so_proto->pr_ctloutput)
+                       (void) ((*so->so_proto->pr_ctloutput)
+                                 (PRCO_SETOPT, so, level, optname, &m0));
        }
 bad:
        if (m)
        }
 bad:
        if (m)
@@ -895,6 +905,7 @@ sogetopt(so, level, optname, mp)
                case SO_DEBUG:
                case SO_KEEPALIVE:
                case SO_REUSEADDR:
                case SO_DEBUG:
                case SO_KEEPALIVE:
                case SO_REUSEADDR:
+               case SO_REUSEPORT:
                case SO_BROADCAST:
                case SO_OOBINLINE:
                        *mtod(m, int *) = so->so_options & optname;
                case SO_BROADCAST:
                case SO_OOBINLINE:
                        *mtod(m, int *) = so->so_options & optname;
@@ -956,9 +967,5 @@ sohasoutofband(so)
                gsignal(-so->so_pgid, SIGURG);
        else if (so->so_pgid > 0 && (p = pfind(so->so_pgid)) != 0)
                psignal(p, SIGURG);
                gsignal(-so->so_pgid, SIGURG);
        else if (so->so_pgid > 0 && (p = pfind(so->so_pgid)) != 0)
                psignal(p, SIGURG);
-       if (so->so_rcv.sb_sel) {
-               selwakeup(so->so_rcv.sb_sel, so->so_rcv.sb_flags & SB_COLL);
-               so->so_rcv.sb_sel = 0;
-               so->so_rcv.sb_flags &= ~SB_COLL;
-       }
+       selwakeup(&so->so_rcv.sb_sel);
 }
 }