[BACK]Return to rumpclient.c CVS log [TXT][DIR] Up to [cvs.NetBSD.org] / src / lib / librumpclient

Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files.

Diff for /src/lib/librumpclient/rumpclient.c between version 1.26 and 1.50

version 1.26, 2011/02/07 15:25:41 version 1.50, 2012/08/03 14:52:31
Line 29 
Line 29 
  * Client side routines for rump syscall proxy.   * Client side routines for rump syscall proxy.
  */   */
   
   #include "rumpuser_port.h"
   
   /*
    * We use kqueue on NetBSD, poll elsewhere.  Theoretically we could
    * use kqueue on other BSD's too, but I haven't tested those.  We
    * want to use kqueue because it will give us the ability to get signal
    * notifications but defer their handling to a stage where we do not
    * hold the communication lock.  Taking a signal while holding on to
    * that lock may cause a deadlock.  Therefore, block signals throughout
    * the RPC when using poll.  This unfortunately means that the normal
    * SIGINT way of stopping a process while it is undergoing rump kernel
    * RPC will not work.  If anyone know which Linux system call handles
    * the above scenario correctly, I'm all ears.
    */
   
   #ifdef __NetBSD__
   #define USE_KQUEUE
   #endif
   
 #include <sys/cdefs.h>  #include <sys/cdefs.h>
 __RCSID("$NetBSD");  __RCSID("$NetBSD$");
   
 #include <sys/param.h>  #include <sys/param.h>
 #include <sys/event.h>  
 #include <sys/mman.h>  #include <sys/mman.h>
 #include <sys/socket.h>  #include <sys/socket.h>
   #include <sys/time.h>
   
   #ifdef USE_KQUEUE
   #include <sys/event.h>
   #endif
   
 #include <arpa/inet.h>  #include <arpa/inet.h>
 #include <netinet/in.h>  #include <netinet/in.h>
Line 43  __RCSID("$NetBSD");
Line 66  __RCSID("$NetBSD");
   
 #include <assert.h>  #include <assert.h>
 #include <dlfcn.h>  #include <dlfcn.h>
   #include <err.h>
 #include <errno.h>  #include <errno.h>
 #include <fcntl.h>  #include <fcntl.h>
 #include <link.h>  #include <link.h>
Line 65  int (*host_connect)(int, const struct so
Line 89  int (*host_connect)(int, const struct so
 int     (*host_fcntl)(int, int, ...);  int     (*host_fcntl)(int, int, ...);
 int     (*host_poll)(struct pollfd *, nfds_t, int);  int     (*host_poll)(struct pollfd *, nfds_t, int);
 ssize_t (*host_read)(int, void *, size_t);  ssize_t (*host_read)(int, void *, size_t);
 ssize_t (*host_sendto)(int, const void *, size_t, int,  ssize_t (*host_sendmsg)(int, const struct msghdr *, int);
                        const struct sockaddr *, socklen_t);  
 int     (*host_setsockopt)(int, int, int, const void *, socklen_t);  int     (*host_setsockopt)(int, int, int, const void *, socklen_t);
   int     (*host_dup)(int);
   
   #ifdef USE_KQUEUE
 int     (*host_kqueue)(void);  int     (*host_kqueue)(void);
 int     (*host_kevent)(int, const struct kevent *, size_t,  int     (*host_kevent)(int, const struct kevent *, size_t,
                        struct kevent *, size_t, const struct timespec *);                         struct kevent *, size_t, const struct timespec *);
   #endif
   
   int     (*host_execve)(const char *, char *const[], char *const[]);
   
 #include "sp_common.c"  #include "sp_common.c"
   
Line 82  static struct spclient clispc = {
Line 110  static struct spclient clispc = {
 static int kq = -1;  static int kq = -1;
 static sigset_t fullset;  static sigset_t fullset;
   
 static int doconnect(bool);  static int doconnect(void);
 static int handshake_req(struct spclient *, uint32_t *, int, bool);  static int handshake_req(struct spclient *, int, void *, int, bool);
   
   /*
    * Default: don't retry.  Most clients can't handle it
    * (consider e.g. fds suddenly going missing).
    */
   static time_t retrytimo = 0;
   
 time_t retrytimo = RUMPCLIENT_RETRYCONN_ONCE;  /* always defined to nothingness for now */
   #define ERRLOG(a)
   
 static int  static int
 send_with_recon(struct spclient *spc, const void *data, size_t dlen)  send_with_recon(struct spclient *spc, struct iovec *iov, size_t iovlen)
 {  {
         struct timeval starttime, curtime;          struct timeval starttime, curtime;
         time_t prevreconmsg;          time_t prevreconmsg;
Line 96  send_with_recon(struct spclient *spc, co
Line 131  send_with_recon(struct spclient *spc, co
         int rv;          int rv;
   
         for (prevreconmsg = 0, reconretries = 0;;) {          for (prevreconmsg = 0, reconretries = 0;;) {
                 rv = dosend(spc, data, dlen);                  rv = dosend(spc, iov, iovlen);
                 if (__predict_false(rv == ENOTCONN || rv == EBADF)) {                  if (__predict_false(rv == ENOTCONN || rv == EBADF)) {
                         /* no persistent connections */                          /* no persistent connections */
                         if (retrytimo == 0)                          if (retrytimo == 0) {
                                   rv = ENOTCONN;
                                 break;                                  break;
                           }
                         if (retrytimo == RUMPCLIENT_RETRYCONN_DIE)                          if (retrytimo == RUMPCLIENT_RETRYCONN_DIE)
                                 exit(1);                                  _exit(1);
   
                         if (!prevreconmsg) {                          if (!prevreconmsg) {
                                 prevreconmsg = time(NULL);                                  prevreconmsg = time(NULL);
Line 143  send_with_recon(struct spclient *spc, co
Line 180  send_with_recon(struct spclient *spc, co
                         }                          }
                         reconretries++;                          reconretries++;
   
                         if ((rv = doconnect(false)) != 0)                          if ((rv = doconnect()) != 0)
                                 continue;                                  continue;
                         if ((rv = handshake_req(&clispc, NULL, 0, true)) != 0)                          if ((rv = handshake_req(&clispc, HANDSHAKE_GUEST,
                               NULL, 0, true)) != 0)
                                 continue;                                  continue;
   
                         /*                          /*
Line 184  cliwaitresp(struct spclient *spc, struct
Line 222  cliwaitresp(struct spclient *spc, struct
   
                 /* are we free to receive? */                  /* are we free to receive? */
                 if (spc->spc_istatus == SPCSTATUS_FREE) {                  if (spc->spc_istatus == SPCSTATUS_FREE) {
                         struct kevent kev[8];                          int gotresp, dosig, rv;
                         int gotresp, dosig, rv, i;  
   
                         spc->spc_istatus = SPCSTATUS_BUSY;                          spc->spc_istatus = SPCSTATUS_BUSY;
                         pthread_mutex_unlock(&spc->spc_mtx);                          pthread_mutex_unlock(&spc->spc_mtx);
   
                         dosig = 0;                          dosig = 0;
                         for (gotresp = 0; !gotresp; ) {                          for (gotresp = 0; !gotresp; ) {
                                 switch (readframe(spc)) {  #ifdef USE_KQUEUE
                                 case 0:                                  struct kevent kev[8];
                                         rv = host_kevent(kq, NULL, 0,                                  int i;
                                             kev, __arraycount(kev), NULL);  
                                   /*
                                    * typically we don't have a frame waiting
                                    * when we come in here, so call kevent now
                                    */
                                   rv = host_kevent(kq, NULL, 0,
                                       kev, __arraycount(kev), NULL);
   
                                   if (__predict_false(rv == -1)) {
                                           goto activity;
                                   }
   
                                         /*                                  /*
                                          * XXX: don't know how this can                                   * XXX: don't know how this can happen
                                          * happen (timeout cannot expire                                   * (timeout cannot expire since there
                                          * since there isn't one), but                                   * isn't one), but it does happen.
                                          * it does happen                                   * treat it as an expectional condition
                                          */                                   * and go through tryread to determine
                                         if (__predict_false(rv == 0))                                   * alive status.
                                                 continue;                                   */
                                   if (__predict_false(rv == 0))
                                         for (i = 0; i < rv; i++) {                                          goto activity;
                                                 if (kev[i].filter  
                                                     == EVFILT_SIGNAL)                                  for (i = 0; i < rv; i++) {
                                                         dosig++;                                          if (kev[i].filter == EVFILT_SIGNAL)
                                         }                                                  dosig++;
                                         if (dosig)                                  }
                                                 goto cleanup;                                  if (dosig)
                                           goto cleanup;
   
                                   /*
                                    * ok, activity.  try to read a frame to
                                    * determine what happens next.
                                    */
    activity:
   #else /* USE_KQUEUE */
                                   struct pollfd pfd;
   
                                   pfd.fd = clispc.spc_fd;
                                   pfd.events = POLLIN;
   
                                   rv = host_poll(&pfd, 1, -1);
   #endif /* !USE_KQUEUE */
   
                                   switch (readframe(spc)) {
                                   case 0:
                                         continue;                                          continue;
                                 case -1:                                  case -1:
                                         imalive = false;                                          imalive = false;
                                         goto cleanup;                                          goto cleanup;
                                 default:                                  default:
                                           /* case 1 */
                                         break;                                          break;
                                 }                                  }
   
Line 272  syscall_req(struct spclient *spc, sigset
Line 337  syscall_req(struct spclient *spc, sigset
 {  {
         struct rsp_hdr rhdr;          struct rsp_hdr rhdr;
         struct respwait rw;          struct respwait rw;
           struct iovec iov[2];
         int rv;          int rv;
   
         rhdr.rsp_len = sizeof(rhdr) + dlen;          rhdr.rsp_len = sizeof(rhdr) + dlen;
Line 279  syscall_req(struct spclient *spc, sigset
Line 345  syscall_req(struct spclient *spc, sigset
         rhdr.rsp_type = RUMPSP_SYSCALL;          rhdr.rsp_type = RUMPSP_SYSCALL;
         rhdr.rsp_sysnum = sysnum;          rhdr.rsp_sysnum = sysnum;
   
           IOVPUT(iov[0], rhdr);
           IOVPUT_WITHSIZE(iov[1], __UNCONST(data), dlen);
   
         do {          do {
                 putwait(spc, &rw, &rhdr);                  putwait(spc, &rw, &rhdr);
                 if ((rv = send_with_recon(spc, &rhdr, sizeof(rhdr))) != 0) {                  if ((rv = send_with_recon(spc, iov, __arraycount(iov))) != 0) {
                         unputwait(spc, &rw);  
                         continue;  
                 }  
                 if ((rv = send_with_recon(spc, data, dlen)) != 0) {  
                         unputwait(spc, &rw);                          unputwait(spc, &rw);
                         continue;                          continue;
                 }                  }
Line 300  syscall_req(struct spclient *spc, sigset
Line 365  syscall_req(struct spclient *spc, sigset
 }  }
   
 static int  static int
 handshake_req(struct spclient *spc, uint32_t *auth, int cancel, bool haslock)  handshake_req(struct spclient *spc, int type, void *data,
           int cancel, bool haslock)
 {  {
         struct handshake_fork rf;          struct handshake_fork rf;
           const char *myprogname = NULL; /* XXXgcc */
         struct rsp_hdr rhdr;          struct rsp_hdr rhdr;
         struct respwait rw;          struct respwait rw;
         sigset_t omask;          sigset_t omask;
         size_t bonus;          size_t bonus;
           struct iovec iov[2];
         int rv;          int rv;
   
         if (auth) {          if (type == HANDSHAKE_FORK) {
                 bonus = sizeof(rf);                  bonus = sizeof(rf);
         } else {          } else {
                 bonus = strlen(getprogname())+1;  #ifdef __NetBSD__
                   /* would procfs work on NetBSD too? */
                   myprogname = getprogname();
   #else
                   int fd = open("/proc/self/comm", O_RDONLY);
                   if (fd == -1) {
                           myprogname = "???";
                   } else {
                           static char commname[128];
   
                           memset(commname, 0, sizeof(commname));
                           if (read(fd, commname, sizeof(commname)) > 0) {
                                   char *n;
   
                                   n = strrchr(commname, '\n');
                                   if (n)
                                           *n = '\0';
                                   myprogname = commname;
                           } else {
                                   myprogname = "???";
                           }
                           close(fd);
                   }
   #endif
                   bonus = strlen(myprogname)+1;
         }          }
   
         /* performs server handshake */          /* performs server handshake */
         rhdr.rsp_len = sizeof(rhdr) + bonus;          rhdr.rsp_len = sizeof(rhdr) + bonus;
         rhdr.rsp_class = RUMPSP_REQ;          rhdr.rsp_class = RUMPSP_REQ;
         rhdr.rsp_type = RUMPSP_HANDSHAKE;          rhdr.rsp_type = RUMPSP_HANDSHAKE;
         if (auth)          rhdr.rsp_handshake = type;
                 rhdr.rsp_handshake = HANDSHAKE_FORK;  
         else          IOVPUT(iov[0], rhdr);
                 rhdr.rsp_handshake = HANDSHAKE_GUEST;  
   
         pthread_sigmask(SIG_SETMASK, &fullset, &omask);          pthread_sigmask(SIG_SETMASK, &fullset, &omask);
         if (haslock)          if (haslock)
                 putwait_locked(spc, &rw, &rhdr);                  putwait_locked(spc, &rw, &rhdr);
         else          else
                 putwait(spc, &rw, &rhdr);                  putwait(spc, &rw, &rhdr);
         rv = dosend(spc, &rhdr, sizeof(rhdr));          if (type == HANDSHAKE_FORK) {
         if (auth) {                  memcpy(rf.rf_auth, data, sizeof(rf.rf_auth)); /* uh, why? */
                 memcpy(rf.rf_auth, auth, AUTHLEN*sizeof(*auth));  
                 rf.rf_cancel = cancel;                  rf.rf_cancel = cancel;
                 rv = send_with_recon(spc, &rf, sizeof(rf));                  IOVPUT(iov[1], rf);
         } else {          } else {
                 rv = dosend(spc, getprogname(), strlen(getprogname())+1);                  IOVPUT_WITHSIZE(iov[1], __UNCONST(myprogname), bonus);
         }          }
           rv = send_with_recon(spc, iov, __arraycount(iov));
         if (rv || cancel) {          if (rv || cancel) {
                 if (haslock)                  if (haslock)
                         unputwait_locked(spc, &rw);                          unputwait_locked(spc, &rw);
Line 364  prefork_req(struct spclient *spc, sigset
Line 455  prefork_req(struct spclient *spc, sigset
 {  {
         struct rsp_hdr rhdr;          struct rsp_hdr rhdr;
         struct respwait rw;          struct respwait rw;
           struct iovec iov[1];
         int rv;          int rv;
   
         rhdr.rsp_len = sizeof(rhdr);          rhdr.rsp_len = sizeof(rhdr);
Line 371  prefork_req(struct spclient *spc, sigset
Line 463  prefork_req(struct spclient *spc, sigset
         rhdr.rsp_type = RUMPSP_PREFORK;          rhdr.rsp_type = RUMPSP_PREFORK;
         rhdr.rsp_error = 0;          rhdr.rsp_error = 0;
   
           IOVPUT(iov[0], rhdr);
   
         do {          do {
                 putwait(spc, &rw, &rhdr);                  putwait(spc, &rw, &rhdr);
                 rv = send_with_recon(spc, &rhdr, sizeof(rhdr));                  rv = send_with_recon(spc, iov, __arraycount(iov));
                 if (rv != 0) {                  if (rv != 0) {
                         unputwait(spc, &rw);                          unputwait(spc, &rw);
                         continue;                          continue;
Line 417  send_copyin_resp(struct spclient *spc, u
Line 511  send_copyin_resp(struct spclient *spc, u
         int wantstr)          int wantstr)
 {  {
         struct rsp_hdr rhdr;          struct rsp_hdr rhdr;
           struct iovec iov[2];
   
         if (wantstr)          if (wantstr)
                 dlen = MIN(dlen, strlen(data)+1);                  dlen = MIN(dlen, strlen(data)+1);
Line 427  send_copyin_resp(struct spclient *spc, u
Line 522  send_copyin_resp(struct spclient *spc, u
         rhdr.rsp_type = RUMPSP_COPYIN;          rhdr.rsp_type = RUMPSP_COPYIN;
         rhdr.rsp_sysnum = 0;          rhdr.rsp_sysnum = 0;
   
           IOVPUT(iov[0], rhdr);
           IOVPUT_WITHSIZE(iov[1], data, dlen);
   
         if (resp_sendlock(spc) != 0)          if (resp_sendlock(spc) != 0)
                 return;                  return;
         (void)dosend(spc, &rhdr, sizeof(rhdr));          (void)SENDIOV(spc, iov);
         (void)dosend(spc, data, dlen);  
         sendunlock(spc);          sendunlock(spc);
 }  }
   
Line 438  static void
Line 535  static void
 send_anonmmap_resp(struct spclient *spc, uint64_t reqno, void *addr)  send_anonmmap_resp(struct spclient *spc, uint64_t reqno, void *addr)
 {  {
         struct rsp_hdr rhdr;          struct rsp_hdr rhdr;
           struct iovec iov[2];
   
         rhdr.rsp_len = sizeof(rhdr) + sizeof(addr);          rhdr.rsp_len = sizeof(rhdr) + sizeof(addr);
         rhdr.rsp_reqno = reqno;          rhdr.rsp_reqno = reqno;
Line 445  send_anonmmap_resp(struct spclient *spc,
Line 543  send_anonmmap_resp(struct spclient *spc,
         rhdr.rsp_type = RUMPSP_ANONMMAP;          rhdr.rsp_type = RUMPSP_ANONMMAP;
         rhdr.rsp_sysnum = 0;          rhdr.rsp_sysnum = 0;
   
           IOVPUT(iov[0], rhdr);
           IOVPUT(iov[1], addr);
   
         if (resp_sendlock(spc) != 0)          if (resp_sendlock(spc) != 0)
                 return;                  return;
         (void)dosend(spc, &rhdr, sizeof(rhdr));          (void)SENDIOV(spc, iov);
         (void)dosend(spc, &addr, sizeof(addr));  
         sendunlock(spc);          sendunlock(spc);
 }  }
   
Line 543  handlereq(struct spclient *spc)
Line 643  handlereq(struct spclient *spc)
 static unsigned ptab_idx;  static unsigned ptab_idx;
 static struct sockaddr *serv_sa;  static struct sockaddr *serv_sa;
   
   /* dup until we get a "good" fd which does not collide with stdio */
   static int
   dupgood(int myfd, int mustchange)
   {
           int ofds[4];
           int sverrno;
           unsigned int i;
   
           for (i = 0; (myfd <= 2 || mustchange) && myfd != -1; i++) {
                   assert(i < __arraycount(ofds));
                   ofds[i] = myfd;
                   myfd = host_dup(myfd);
                   if (mustchange) {
                           i--; /* prevent closing old fd */
                           mustchange = 0;
                   }
           }
   
           sverrno = 0;
           if (myfd == -1 && i > 0)
                   sverrno = errno;
   
           while (i-- > 0) {
                   host_close(ofds[i]);
           }
   
           if (sverrno)
                   errno = sverrno;
   
           return myfd;
   }
   
 static int  static int
 doconnect(bool noisy)  doconnect(void)
 {  {
         struct respwait rw;          struct respwait rw;
         struct rsp_hdr rhdr;          struct rsp_hdr rhdr;
         struct kevent kev[NSIG+1];  
         char banner[MAXBANNER];          char banner[MAXBANNER];
         struct pollfd pfd;          struct pollfd pfd;
         int s, error, flags, i;          int s, error, flags;
         ssize_t n;          ssize_t n;
   
         if (kq != -1)          if (kq != -1)
Line 590  doconnect(bool noisy)
Line 721  doconnect(bool noisy)
         free(clispc.spc_buf);          free(clispc.spc_buf);
         clispc.spc_off = 0;          clispc.spc_off = 0;
   
         s = host_socket(parsetab[ptab_idx].domain, SOCK_STREAM, 0);          s = dupgood(host_socket(parsetab[ptab_idx].domain, SOCK_STREAM, 0), 0);
         if (s == -1)          if (s == -1)
                 return -1;                  return -1;
   
         pfd.fd = s;          pfd.fd = s;
         pfd.events = POLLIN;          pfd.events = POLLIN;
         while (host_connect(s, serv_sa, (socklen_t)serv_sa->sa_len) == -1) {          while (host_connect(s, serv_sa, parsetab[ptab_idx].slen) == -1) {
                 if (errno == EINTR)                  if (errno == EINTR)
                         continue;                          continue;
                 error = errno;                  ERRLOG(("rump_sp: client connect failed: %s\n",
                 if (noisy)                      strerror(errno)));
                         fprintf(stderr, "rump_sp: client connect failed: %s\n",  
                             strerror(errno));  
                 errno = error;  
                 return -1;                  return -1;
         }          }
   
         if ((error = parsetab[ptab_idx].connhook(s)) != 0) {          if ((error = parsetab[ptab_idx].connhook(s)) != 0) {
                 error = errno;                  ERRLOG(("rump_sp: connect hook failed\n"));
                 if (noisy)  
                         fprintf(stderr, "rump_sp: connect hook failed\n");  
                 errno = error;  
                 return -1;                  return -1;
         }          }
   
         if ((n = host_read(s, banner, sizeof(banner)-1)) < 0) {          if ((n = host_read(s, banner, sizeof(banner)-1)) <= 0) {
                 error = errno;                  ERRLOG(("rump_sp: failed to read banner\n"));
                 if (noisy)  
                         fprintf(stderr, "rump_sp: failed to read banner\n");  
                 errno = error;  
                 return -1;                  return -1;
         }          }
   
         if (banner[n-1] != '\n') {          if (banner[n-1] != '\n') {
                 if (noisy)                  ERRLOG(("rump_sp: invalid banner\n"));
                         fprintf(stderr, "rump_sp: invalid banner\n");  
                 errno = EINVAL;  
                 return -1;                  return -1;
         }          }
         banner[n] = '\0';          banner[n] = '\0';
         /* parse the banner some day */          /* XXX parse the banner some day */
   
         flags = host_fcntl(s, F_GETFL, 0);          flags = host_fcntl(s, F_GETFL, 0);
         if (host_fcntl(s, F_SETFL, flags | O_NONBLOCK) == -1) {          if (host_fcntl(s, F_SETFL, flags | O_NONBLOCK) == -1) {
                 if (noisy)                  ERRLOG(("rump_sp: socket fd NONBLOCK: %s\n", strerror(errno)));
                         fprintf(stderr, "rump_sp: socket fd NONBLOCK: %s\n",  
                             strerror(errno));  
                 errno = EINVAL;  
                 return -1;                  return -1;
         }          }
         clispc.spc_fd = s;          clispc.spc_fd = s;
         clispc.spc_state = SPCSTATE_RUNNING;          clispc.spc_state = SPCSTATE_RUNNING;
         clispc.spc_reconnecting = 0;          clispc.spc_reconnecting = 0;
   
   #ifdef USE_KQUEUE
   {
           struct kevent kev[NSIG+1];
           int i;
   
         /* setup kqueue, we want all signals and the fd */          /* setup kqueue, we want all signals and the fd */
         if ((kq = host_kqueue()) == -1) {          if ((kq = dupgood(host_kqueue(), 0)) == -1) {
                 error = errno;                  ERRLOG(("rump_sp: cannot setup kqueue"));
                 if (noisy)  
                         fprintf(stderr, "rump_sp: cannot setup kqueue");  
                 errno = error;  
                 return -1;                  return -1;
         }          }
   
Line 659  doconnect(bool noisy)
Line 778  doconnect(bool noisy)
         EV_SET(&kev[NSIG], clispc.spc_fd,          EV_SET(&kev[NSIG], clispc.spc_fd,
             EVFILT_READ, EV_ADD|EV_ENABLE, 0, 0, 0);              EVFILT_READ, EV_ADD|EV_ENABLE, 0, 0, 0);
         if (host_kevent(kq, kev, NSIG+1, NULL, 0, NULL) == -1) {          if (host_kevent(kq, kev, NSIG+1, NULL, 0, NULL) == -1) {
                 error = errno;                  ERRLOG(("rump_sp: kevent() failed"));
                 if (noisy)  
                         fprintf(stderr, "rump_sp: kevent() failed");  
                 errno = error;  
                 return -1;                  return -1;
         }          }
   }
   #endif /* USE_KQUEUE */
   
         return 0;          return 0;
 }  }
Line 680  doinit(void)
Line 798  doinit(void)
         return 0;          return 0;
 }  }
   
 void *(*rumpclient_dlsym)(void *, const char *);  void *rumpclient__dlsym(void *, const char *);
   void *
   rumpclient__dlsym(void *handle, const char *symbol)
   {
   
           return dlsym(handle, symbol);
   }
   void *rumphijack_dlsym(void *, const char *)
       __attribute__((__weak__, alias("rumpclient__dlsym")));
   
   static pid_t init_done = 0;
   
 int  int
 rumpclient_init()  rumpclient_init(void)
 {  {
         char *p;          char *p;
         int error;          int error;
           int rv = -1;
           int hstype;
           pid_t mypid;
   
         sigfillset(&fullset);          /*
            * Make sure we're not riding the context of a previous
            * host fork.  Note: it's *possible* that after n>1 forks
            * we have the same pid as one of our exited parents, but
            * I'm pretty sure there are 0 practical implications, since
            * it means generations would have to skip rumpclient init.
            */
           if (init_done == (mypid = getpid()))
                   return 0;
   
           /* kq does not traverse fork() */
           if (init_done != 0)
                   kq = -1;
           init_done = mypid;
   
         /* dlsym overrided by rumphijack? */          sigfillset(&fullset);
         if (!rumpclient_dlsym)  
                 rumpclient_dlsym = dlsym;  
   
         /*          /*
          * sag mir, wo die symbol sind.  zogen fort, der krieg beginnt.           * sag mir, wo die symbols sind.  zogen fort, der krieg beginnt.
          * wann wird man je verstehen?  wann wird man je verstehen?           * wann wird man je verstehen?  wann wird man je verstehen?
          */           */
 #define FINDSYM2(_name_,_syscall_)                                      \  #define FINDSYM2(_name_,_syscall_)                                      \
         if ((host_##_name_ = rumpclient_dlsym(RTLD_NEXT,                \          if ((host_##_name_ = rumphijack_dlsym(RTLD_NEXT,                \
             #_syscall_)) == NULL)                                       \              #_syscall_)) == NULL) {                                     \
                 /* host_##_name_ = _syscall_ */;                  if (rumphijack_dlsym == rumpclient__dlsym)              \
                           host_##_name_ = _name_; /* static fallback */   \
                   if (host_##_name_ == NULL)                              \
                           errx(1, "cannot find %s: %s", #_syscall_,       \
                               dlerror());                                 \
           }
 #define FINDSYM(_name_) FINDSYM2(_name_,_name_)  #define FINDSYM(_name_) FINDSYM2(_name_,_name_)
         FINDSYM2(socket,__socket30);  #ifdef __NetBSD__
         FINDSYM(close);          FINDSYM2(socket,__socket30)
         FINDSYM(connect);  #else
         FINDSYM(fcntl);          FINDSYM(socket)
         FINDSYM(poll);  #endif
         FINDSYM(read);  
         FINDSYM(sendto);          FINDSYM(close)
         FINDSYM(setsockopt);          FINDSYM(connect)
         FINDSYM(kqueue);          FINDSYM(fcntl)
           FINDSYM(poll)
           FINDSYM(read)
           FINDSYM(sendmsg)
           FINDSYM(setsockopt)
           FINDSYM(dup)
           FINDSYM(execve)
   
   #ifdef USE_KQUEUE
           FINDSYM(kqueue)
 #if !__NetBSD_Prereq__(5,99,7)  #if !__NetBSD_Prereq__(5,99,7)
         FINDSYM(kevent);          FINDSYM(kevent)
 #else  #else
         FINDSYM2(kevent,_sys___kevent50);          FINDSYM2(kevent,_sys___kevent50)
 #endif  #endif
   #endif /* USE_KQUEUE */
   
 #undef  FINDSYM  #undef  FINDSYM
 #undef  FINDSY2  #undef  FINDSY2
   
         if ((p = getenv("RUMP_SERVER")) == NULL) {          if ((p = getenv("RUMP__PARSEDSERVER")) == NULL) {
                 errno = ENOENT;                  if ((p = getenv("RUMP_SERVER")) == NULL) {
                 return -1;                          errno = ENOENT;
                           goto out;
                   }
         }          }
   
         if ((error = parseurl(p, &serv_sa, &ptab_idx, 0)) != 0) {          if ((error = parseurl(p, &serv_sa, &ptab_idx, 0)) != 0) {
                 errno = error;                  errno = error;
                 return -1;                  goto out;
         }          }
   
         if (doinit() == -1)          if (doinit() == -1)
                 return -1;                  goto out;
         if (doconnect(true) == -1)  
                 return -1;          if ((p = getenv("RUMPCLIENT__EXECFD")) != NULL) {
                   sscanf(p, "%d,%d", &clispc.spc_fd, &kq);
                   unsetenv("RUMPCLIENT__EXECFD");
                   hstype = HANDSHAKE_EXEC;
           } else {
                   if (doconnect() == -1)
                           goto out;
                   hstype = HANDSHAKE_GUEST;
           }
   
         error = handshake_req(&clispc, NULL, 0, false);          error = handshake_req(&clispc, hstype, NULL, 0, false);
         if (error) {          if (error) {
                 pthread_mutex_destroy(&clispc.spc_mtx);                  pthread_mutex_destroy(&clispc.spc_mtx);
                 pthread_cond_destroy(&clispc.spc_cv);                  pthread_cond_destroy(&clispc.spc_cv);
                 if (clispc.spc_fd != -1)                  if (clispc.spc_fd != -1)
                         host_close(clispc.spc_fd);                          host_close(clispc.spc_fd);
                 errno = error;                  errno = error;
                 return -1;                  goto out;
         }          }
           rv = 0;
   
         return 0;   out:
           if (rv == -1)
                   init_done = 0;
           return rv;
 }  }
   
 struct rumpclient_fork {  struct rumpclient_fork {
         uint32_t fork_auth[AUTHLEN];          uint32_t fork_auth[AUTHLEN];
           struct spclient fork_spc;
           int fork_kq;
 };  };
   
 struct rumpclient_fork *  struct rumpclient_fork *
Line 763  rumpclient_prefork(void)
Line 937  rumpclient_prefork(void)
         pthread_sigmask(SIG_SETMASK, &fullset, &omask);          pthread_sigmask(SIG_SETMASK, &fullset, &omask);
         rpf = malloc(sizeof(*rpf));          rpf = malloc(sizeof(*rpf));
         if (rpf == NULL)          if (rpf == NULL)
                 return NULL;                  goto out;
   
         if ((rv = prefork_req(&clispc, &omask, &resp)) != 0) {          if ((rv = prefork_req(&clispc, &omask, &resp)) != 0) {
                 free(rpf);                  free(rpf);
Line 775  rumpclient_prefork(void)
Line 949  rumpclient_prefork(void)
         memcpy(rpf->fork_auth, resp, sizeof(rpf->fork_auth));          memcpy(rpf->fork_auth, resp, sizeof(rpf->fork_auth));
         free(resp);          free(resp);
   
           rpf->fork_spc = clispc;
           rpf->fork_kq = kq;
   
  out:   out:
         pthread_sigmask(SIG_SETMASK, &omask, NULL);          pthread_sigmask(SIG_SETMASK, &omask, NULL);
         return rpf;          return rpf;
Line 794  rumpclient_fork_init(struct rumpclient_f
Line 971  rumpclient_fork_init(struct rumpclient_f
   
         if (doinit() == -1)          if (doinit() == -1)
                 return -1;                  return -1;
         if (doconnect(false) == -1)          if (doconnect() == -1)
                 return -1;                  return -1;
   
         error = handshake_req(&clispc, rpf->fork_auth, 0, false);          error = handshake_req(&clispc, HANDSHAKE_FORK, rpf->fork_auth,
               0, false);
         if (error) {          if (error) {
                 pthread_mutex_destroy(&clispc.spc_mtx);                  pthread_mutex_destroy(&clispc.spc_mtx);
                 pthread_cond_destroy(&clispc.spc_cv);                  pthread_cond_destroy(&clispc.spc_cv);
Line 808  rumpclient_fork_init(struct rumpclient_f
Line 986  rumpclient_fork_init(struct rumpclient_f
         return 0;          return 0;
 }  }
   
   /*ARGSUSED*/
   void
   rumpclient_fork_cancel(struct rumpclient_fork *rpf)
   {
   
           /* EUNIMPL */
   }
   
   void
   rumpclient_fork_vparent(struct rumpclient_fork *rpf)
   {
   
           clispc = rpf->fork_spc;
           kq = rpf->fork_kq;
   }
   
 void  void
 rumpclient_setconnretry(time_t timeout)  rumpclient_setconnretry(time_t timeout)
 {  {
Line 817  rumpclient_setconnretry(time_t timeout)
Line 1011  rumpclient_setconnretry(time_t timeout)
   
         retrytimo = timeout;          retrytimo = timeout;
 }  }
   
   int
   rumpclient__closenotify(int *fdp, enum rumpclient_closevariant variant)
   {
           int fd = *fdp;
           int untilfd, rv;
           int newfd;
   
           switch (variant) {
           case RUMPCLIENT_CLOSE_FCLOSEM:
                   untilfd = MAX(clispc.spc_fd, kq);
                   for (; fd <= untilfd; fd++) {
                           if (fd == clispc.spc_fd || fd == kq)
                                   continue;
                           rv = host_close(fd);
                           if (rv == -1)
                                   return -1;
                   }
                   *fdp = fd;
                   break;
   
           case RUMPCLIENT_CLOSE_CLOSE:
           case RUMPCLIENT_CLOSE_DUP2:
                   if (fd == clispc.spc_fd) {
                           newfd = dupgood(clispc.spc_fd, 1);
                           if (newfd == -1)
                                   return -1;
   
   #ifdef USE_KQUEUE
                           {
                           struct kevent kev[2];
   
                           /*
                            * now, we have a new socket number, so change
                            * the file descriptor that kqueue is
                            * monitoring.  remove old and add new.
                            */
                           EV_SET(&kev[0], clispc.spc_fd,
                               EVFILT_READ, EV_DELETE, 0, 0, 0);
                           EV_SET(&kev[1], newfd,
                               EVFILT_READ, EV_ADD|EV_ENABLE, 0, 0, 0);
                           if (host_kevent(kq, kev, 2, NULL, 0, NULL) == -1) {
                                   int sverrno = errno;
                                   host_close(newfd);
                                   errno = sverrno;
                                   return -1;
                           }
                           clispc.spc_fd = newfd;
                           }
                   }
                   if (fd == kq) {
                           newfd = dupgood(kq, 1);
                           if (newfd == -1)
                                   return -1;
                           kq = newfd;
   #else /* USE_KQUEUE */
                           clispc.spc_fd = newfd;
   #endif /* !USE_KQUEUE */
                   }
                   break;
           }
   
           return 0;
   }
   
   pid_t
   rumpclient_fork(void)
   {
   
           return rumpclient__dofork(fork);
   }
   
   /*
    * Process is about to exec.  Save info about our existing connection
    * in the env.  rumpclient will check for this info in init().
    * This is mostly for the benefit of rumphijack, but regular applications
    * may use it as well.
    */
   int
   rumpclient_exec(const char *path, char *const argv[], char *const envp[])
   {
           char buf[4096];
           char **newenv;
           char *envstr, *envstr2;
           size_t nelem;
           int rv, sverrno;
   
           snprintf(buf, sizeof(buf), "RUMPCLIENT__EXECFD=%d,%d",
               clispc.spc_fd, kq);
           envstr = malloc(strlen(buf)+1);
           if (envstr == NULL) {
                   return ENOMEM;
           }
           strcpy(envstr, buf);
   
           /* do we have a fully parsed url we want to forward in the env? */
           if (*parsedurl != '\0') {
                   snprintf(buf, sizeof(buf),
                       "RUMP__PARSEDSERVER=%s", parsedurl);
                   envstr2 = malloc(strlen(buf)+1);
                   if (envstr2 == NULL) {
                           free(envstr);
                           return ENOMEM;
                   }
                   strcpy(envstr2, buf);
           } else {
                   envstr2 = NULL;
           }
   
           for (nelem = 0; envp && envp[nelem]; nelem++)
                   continue;
   
           newenv = malloc(sizeof(*newenv) * (nelem+3));
           if (newenv == NULL) {
                   free(envstr2);
                   free(envstr);
                   return ENOMEM;
           }
           memcpy(&newenv[0], envp, nelem*sizeof(*envp));
   
           newenv[nelem] = envstr;
           newenv[nelem+1] = envstr2;
           newenv[nelem+2] = NULL;
   
           rv = host_execve(path, argv, newenv);
   
           _DIAGASSERT(rv != 0);
           sverrno = errno;
           free(envstr2);
           free(envstr);
           free(newenv);
           errno = sverrno;
           return rv;
   }
   
   int
   rumpclient_daemon(int nochdir, int noclose)
   {
           struct rumpclient_fork *rf;
           int sverrno;
   
           if ((rf = rumpclient_prefork()) == NULL)
                   return -1;
   
           if (daemon(nochdir, noclose) == -1) {
                   sverrno = errno;
                   rumpclient_fork_cancel(rf);
                   errno = sverrno;
                   return -1;
           }
   
           if (rumpclient_fork_init(rf) == -1)
                   return -1;
   
           return 0;
   }

Legend:
Removed from v.1.26  
changed lines
  Added in v.1.50

CVSweb <webmaster@jp.NetBSD.org>