NetBSD-Bugs archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

Re: kern/39108 (Proposal for unix(4) LOCAL_PROC/SCM_PROC)



The following reply was made to PR kern/39108; it has been noted by GNATS.

From: mmondor%pulsar-zone.net@localhost
To: gnats-bugs%gnats.NetBSD.org@localhost
Cc: 
Subject: Re: kern/39108 (Proposal for unix(4) LOCAL_PROC/SCM_PROC)
Date: Sun, 2 Nov 2008 22:03:10 -0500

 Updated diff for 5.99.01:
 
 
 Index: sys/sys/socket.h
 ===================================================================
 RCS file: /nfs/ginseng/home/data/cvsup/netbsd/src/sys/sys/socket.h,v
 retrieving revision 1.91
 diff -u -p -r1.91 socket.h
 --- sys/sys/socket.h   4 Aug 2008 03:55:47 -0000       1.91
 +++ sys/sys/socket.h   31 Oct 2008 02:17:58 -0000
 @@ -331,6 +331,22 @@ struct sockcred {
        (sizeof(struct sockcred) + (sizeof(gid_t) * ((ngrps) - 1)))
  #endif /* _NETBSD_SOURCE */
  
 +#if defined(_NETBSD_SOURCE)
 +
 +#ifndef pid_t
 +typedef __pid_t               pid_t;          /* process id */
 +#define pid_t         __pid_t
 +#endif
 +
 +/*
 + * Like sockcred but to obtain PID/PGID
 + */
 +struct sockproc {
 +      pid_t   sp_pid;                 /* process id */
 +      pid_t   sp_pgid;                /* process group id */
 +};
 +
 +#endif /* _NETBSD_SOURCE */
  
  #if defined(_NETBSD_SOURCE)
  /*
 @@ -542,6 +558,7 @@ struct cmsghdr {
  #if defined(_NETBSD_SOURCE)
  #define       SCM_TIMESTAMP   0x02            /* timestamp (struct timeval) */
  #define       SCM_CREDS       0x04            /* credentials (struct 
sockcred) */
 +#define SCM_PROC      0x08            /* PID/PGID (struct sockproc) */
  #endif
  
  /*
 Index: sys/sys/un.h
 ===================================================================
 RCS file: /nfs/ginseng/home/data/cvsup/netbsd/src/sys/sys/un.h,v
 retrieving revision 1.44
 diff -u -p -r1.44 un.h
 --- sys/sys/un.h       6 Aug 2008 15:01:24 -0000       1.44
 +++ sys/sys/un.h       31 Oct 2008 02:18:00 -0000
 @@ -59,6 +59,7 @@ struct       sockaddr_un {
  #define       LOCAL_CREDS     0x0001          /* pass credentials to receiver 
*/
  #define       LOCAL_CONNWAIT  0x0002          /* connects block until 
accepted */
  #define       LOCAL_PEEREID   0x0003          /* get peer identification */
 +#define LOCAL_PROC    0x0004          /* pass PID/PGID to receiver */
  #endif
  
  /*
 Index: sys/kern/uipc_usrreq.c
 ===================================================================
 RCS file: /nfs/ginseng/home/data/cvsup/netbsd/src/sys/kern/uipc_usrreq.c,v
 retrieving revision 1.119
 diff -u -p -r1.119 uipc_usrreq.c
 --- sys/kern/uipc_usrreq.c     11 Oct 2008 13:40:57 -0000      1.119
 +++ sys/kern/uipc_usrreq.c     3 Nov 2008 01:44:54 -0000
 @@ -168,7 +168,10 @@ const struct sockaddr_un sun_noname = {
  };
  ino_t unp_ino;                        /* prototype for fake inode numbers */
  
 +inline struct mbuf *unp_addsockalloc(struct mbuf *, int, int);
 +inline struct mbuf *unp_addsockappend(struct mbuf *, struct mbuf *);
  struct mbuf *unp_addsockcred(struct lwp *, struct mbuf *);
 +struct mbuf *unp_addsockproc(struct lwp *, struct mbuf *);
  static kmutex_t *uipc_lock;
  
  /*
 @@ -287,6 +290,8 @@ unp_output(struct mbuf *m, struct mbuf *
                sun = &sun_noname;
        if (unp->unp_conn->unp_flags & UNP_WANTCRED)
                control = unp_addsockcred(l, control);
 +      if (unp->unp_conn->unp_flags & UNP_WANTPROC)
 +              control = unp_addsockproc(l, control);
        if (sbappendaddr(&so2->so_rcv, (const struct sockaddr *)sun, m,
            control) == 0) {
                so2->so_rcv.sb_overflowed++;
 @@ -482,7 +487,7 @@ uipc_usrreq(struct socket *so, int req, 
                 * Note: unp_internalize() rejects any control message
                 * other than SCM_RIGHTS, and only allows one.  This
                 * has the side-effect of preventing a caller from
 -               * forging SCM_CREDS.
 +               * forging SCM_CREDS or SCM_PROC.
                 */
                if (control) {
                        sounlock(so);
 @@ -549,6 +554,14 @@ uipc_usrreq(struct socket *so, int req, 
                                unp->unp_conn->unp_flags &= ~UNP_WANTCRED;
                                control = unp_addsockcred(l, control);
                        }
 +                      if (unp->unp_conn->unp_flags & UNP_WANTPROC) {
 +                              /*
 +                               * PID/PGID are passed only once on
 +                               * SOCK_STREAM.
 +                               */
 +                              unp->unp_conn->unp_flags &= ~UNP_WANTPROC;
 +                              control = unp_addsockproc(l, control);
 +                      }
                        /*
                         * Send to paired receive port, and then reduce
                         * send buffer hiwater marks to maintain backpressure.
 @@ -654,6 +667,7 @@ uipc_ctloutput(int op, struct socket *so
        case PRCO_SETOPT:
                switch (sopt->sopt_name) {
                case LOCAL_CREDS:
 +              case LOCAL_PROC:
                case LOCAL_CONNWAIT:
                        error = sockopt_getint(sopt, &optval);
                        if (error)
 @@ -668,6 +682,9 @@ uipc_ctloutput(int op, struct socket *so
                        case LOCAL_CREDS:
                                OPTSET(UNP_WANTCRED);
                                break;
 +                      case LOCAL_PROC:
 +                              OPTSET(UNP_WANTPROC);
 +                              break;
                        case LOCAL_CONNWAIT:
                                OPTSET(UNP_CONNWAIT);
                                break;
 @@ -681,6 +698,7 @@ uipc_ctloutput(int op, struct socket *so
                }
                break;
  
 +#define       OPTBIT(bit)     (unp->unp_flags & (bit) ? 1 : 0)
        case PRCO_GETOPT:
                sounlock(so);
                switch (sopt->sopt_name) {
 @@ -693,11 +711,13 @@ uipc_ctloutput(int op, struct socket *so
                        }
                        break;
                case LOCAL_CREDS:
 -#define       OPTBIT(bit)     (unp->unp_flags & (bit) ? 1 : 0)
 -
                        optval = OPTBIT(UNP_WANTCRED);
                        error = sockopt_setint(sopt, optval);
                        break;
 +              case LOCAL_PROC:
 +                      optval = OPTBIT(UNP_WANTPROC);
 +                      error = sockopt_setint(sopt, optval);
 +                      break;
  #undef OPTBIT
  
                default:
 @@ -1353,16 +1373,10 @@ unp_internalize(struct mbuf **controlp)
        return error;
  }
  
 -struct mbuf *
 -unp_addsockcred(struct lwp *l, struct mbuf *control)
 +inline struct mbuf *
 +unp_addsockalloc(struct mbuf *control, int len, int space)
  {
 -      struct cmsghdr *cmp;
 -      struct sockcred *sc;
 -      struct mbuf *m, *n;
 -      int len, space, i;
 -
 -      len = CMSG_LEN(SOCKCREDSIZE(kauth_cred_ngroups(l->l_cred)));
 -      space = CMSG_SPACE(SOCKCREDSIZE(kauth_cred_ngroups(l->l_cred)));
 +      struct mbuf *m;
  
        m = m_get(M_WAIT, MT_CONTROL);
        if (space > MLEN) {
 @@ -1376,6 +1390,36 @@ unp_addsockcred(struct lwp *l, struct mb
                }
        }
  
 +      return m;
 +}
 +
 +inline struct mbuf *
 +unp_addsockappend(struct mbuf *control, struct mbuf *m)
 +{
 +      struct mbuf *n;
 +
 +      if (control != NULL) {
 +              for (n = control; n->m_next != NULL; n = n->m_next) ;
 +              n->m_next = m;
 +              return control;
 +      }
 +
 +      return m;
 +}
 +
 +struct mbuf *
 +unp_addsockcred(struct lwp *l, struct mbuf *control)
 +{
 +      struct cmsghdr *cmp;
 +      struct sockcred *sc;
 +      struct mbuf *m;
 +      int len, space, i;
 +
 +      len = CMSG_LEN(SOCKCREDSIZE(kauth_cred_ngroups(l->l_cred)));
 +      space = CMSG_SPACE(SOCKCREDSIZE(kauth_cred_ngroups(l->l_cred)));
 +
 +      m = unp_addsockalloc(control, len, space);
 +
        m->m_len = space;
        m->m_next = NULL;
        cmp = mtod(m, struct cmsghdr *);
 @@ -1391,17 +1435,33 @@ unp_addsockcred(struct lwp *l, struct mb
        for (i = 0; i < sc->sc_ngroups; i++)
                sc->sc_groups[i] = kauth_cred_group(l->l_cred, i);
  
 -      /*
 -       * If a control message already exists, append us to the end.
 -       */
 -      if (control != NULL) {
 -              for (n = control; n->m_next != NULL; n = n->m_next)
 -                      ;
 -              n->m_next = m;
 -      } else
 -              control = m;
 +      return unp_addsockappend(m, control);
 +}
 +
 +struct mbuf *
 +unp_addsockproc(struct lwp *l, struct mbuf *control)
 +{
 +      struct cmsghdr *cmp;
 +      struct sockproc *sp;
 +      struct mbuf *m;
 +      int len, space;
 +
 +      len = CMSG_LEN(sizeof(struct sockproc));
 +      space = CMSG_SPACE(sizeof(struct sockproc));
 +
 +      m = unp_addsockalloc(control, len, space);
 +
 +      m->m_len = space;
 +      m->m_next = NULL;
 +      cmp = mtod(m, struct cmsghdr *);
 +      sp = (struct sockproc *)CMSG_DATA(cmp);
 +      cmp->cmsg_len = len;
 +      cmp->cmsg_level = SOL_SOCKET;
 +      cmp->cmsg_type = SCM_PROC;
 +      sp->sp_pid = l->l_proc->p_pid;
 +      sp->sp_pgid = l->l_proc->p_pgid;
  
 -      return (control);
 +      return unp_addsockappend(m, control);
  }
  
  int   unp_defer, unp_gcing;
 Index: share/man/man4/unix.4
 ===================================================================
 RCS file: /nfs/ginseng/home/data/cvsup/netbsd/src/share/man/man4/unix.4,v
 retrieving revision 1.19
 diff -u -p -r1.19 unix.4
 --- share/man/man4/unix.4      9 Aug 2007 15:23:03 -0000       1.19
 +++ share/man/man4/unix.4      5 Jul 2008 20:33:38 -0000
 @@ -168,7 +168,7 @@ Descriptors that are awaiting delivery, 
  purposely not received, are automatically closed by the system
  when the destination socket is closed.
  .Pp
 -There are two
 +There are three
  .Tn socket-level
  .Xr setsockopt 2 / Ns Xr getsockopt 2
  option available in the
 @@ -177,15 +177,17 @@ domain:
  .Pp
  The
  .Dv LOCAL_CREDS
 +or
 +.Dv LOCAL_PROC
  option may be enabled on a
  .Dv SOCK_DGRAM
  or a
  .Dv SOCK_STREAM
  socket.
  This option provides a mechanism for the receiver to
 -receive the credentials of the process as a
 +receive the credentials or identity of the process as a
  .Xr recvmsg 2
 -control message.
 +control message with ancillary data.
  The msg_control field in the msghdr structure points
  to a buffer that contains a cmsghdr structure followed by a variable
  length sockcred structure, defined in
 @@ -202,6 +204,14 @@ struct sockcred {
  };
  .Ed
  .Pp
 +or a sockproc structure, defined as follows:
 +.Bd -literal
 +struct sockproc {
 +      pid_t   sp_pid;                 /* PID of process */
 +      pid_t   sp_pgid;                /* PGID of process */
 +};
 +.Ed
 +.Pp
  The
  .Dv LOCAL_PEEREID
  option may be used with
 
 
 


Home | Main Index | Thread Index | Old Index