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

Annotation of src/lib/librumpclient/rumpclient.c, Revision 1.67

1.67    ! martin      1: /*      $NetBSD: rumpclient.c,v 1.66 2016/09/06 07:12:48 martin Exp $  */
1.1       pooka       2:
                      3: /*
1.11      pooka       4:  * Copyright (c) 2010, 2011 Antti Kantee.  All Rights Reserved.
1.1       pooka       5:  *
                      6:  * Redistribution and use in source and binary forms, with or without
                      7:  * modification, are permitted provided that the following conditions
                      8:  * are met:
                      9:  * 1. Redistributions of source code must retain the above copyright
                     10:  *    notice, this list of conditions and the following disclaimer.
                     11:  * 2. Redistributions in binary form must reproduce the above copyright
                     12:  *    notice, this list of conditions and the following disclaimer in the
                     13:  *    documentation and/or other materials provided with the distribution.
                     14:  *
                     15:  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
                     16:  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
                     17:  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
                     18:  * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
                     19:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     20:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
                     21:  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     22:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     23:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     24:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     25:  * SUCH DAMAGE.
                     26:  */
                     27:
                     28: /*
                     29:  * Client side routines for rump syscall proxy.
                     30:  */
                     31:
1.55      pooka      32: #include <rump/rumpuser_port.h>
1.49      pooka      33:
                     34: /*
1.64      justin     35:  * We use kqueue on the BSDs, poll elsewhere.  We
1.49      pooka      36:  * want to use kqueue because it will give us the ability to get signal
                     37:  * notifications but defer their handling to a stage where we do not
                     38:  * hold the communication lock.  Taking a signal while holding on to
                     39:  * that lock may cause a deadlock.  Therefore, block signals throughout
1.58      pooka      40:  * the RPC when using poll.  On Linux, we use signalfd in the same role
                     41:  * as kqueue on NetBSD to be able to take signals while waiting for a
                     42:  * response from the server.
1.49      pooka      43:  */
                     44:
1.64      justin     45: #if defined(__NetBSD__) || defined(__FreeBSD__) || \
                     46:     defined(__DragonFly__) || defined(__OpenBSD__)
1.49      pooka      47: #define USE_KQUEUE
                     48: #endif
1.65      justin     49: #if defined(__linux__)
1.58      pooka      50: #define USE_SIGNALFD
                     51: #endif
1.49      pooka      52:
1.67    ! martin     53: __RCSID("$NetBSD: rumpclient.c,v 1.66 2016/09/06 07:12:48 martin Exp $");
1.1       pooka      54:
1.5       pooka      55: #include <sys/param.h>
1.1       pooka      56: #include <sys/mman.h>
                     57: #include <sys/socket.h>
1.49      pooka      58: #include <sys/time.h>
                     59:
                     60: #ifdef USE_KQUEUE
                     61: #include <sys/event.h>
                     62: #endif
1.1       pooka      63:
                     64: #include <arpa/inet.h>
                     65: #include <netinet/in.h>
                     66: #include <netinet/tcp.h>
                     67:
                     68: #include <assert.h>
1.13      pooka      69: #include <dlfcn.h>
1.1       pooka      70: #include <errno.h>
                     71: #include <fcntl.h>
                     72: #include <poll.h>
                     73: #include <pthread.h>
1.11      pooka      74: #include <signal.h>
1.1       pooka      75: #include <stdarg.h>
1.18      pooka      76: #include <stdbool.h>
1.1       pooka      77: #include <stdio.h>
                     78: #include <stdlib.h>
                     79: #include <string.h>
                     80: #include <unistd.h>
                     81:
                     82: #include <rump/rumpclient.h>
                     83:
1.13      pooka      84: #define HOSTOPS
                     85: int    (*host_socket)(int, int, int);
                     86: int    (*host_close)(int);
                     87: int    (*host_connect)(int, const struct sockaddr *, socklen_t);
1.15      pooka      88: int    (*host_fcntl)(int, int, ...);
1.13      pooka      89: int    (*host_poll)(struct pollfd *, nfds_t, int);
                     90: ssize_t        (*host_read)(int, void *, size_t);
1.39      pooka      91: ssize_t (*host_sendmsg)(int, const struct msghdr *, int);
1.13      pooka      92: int    (*host_setsockopt)(int, int, int, const void *, socklen_t);
1.27      pooka      93: int    (*host_dup)(int);
1.13      pooka      94:
1.49      pooka      95: #ifdef USE_KQUEUE
1.17      pooka      96: int    (*host_kqueue)(void);
1.63      justin     97: #ifdef __NetBSD__
1.17      pooka      98: int    (*host_kevent)(int, const struct kevent *, size_t,
                     99:                       struct kevent *, size_t, const struct timespec *);
1.63      justin    100: #else
                    101: int    (*host_kevent)(int, const struct kevent *, int,
                    102:                       struct kevent *, int, const struct timespec *);
                    103: #endif
1.49      pooka     104: #endif
1.17      pooka     105:
1.58      pooka     106: #ifdef USE_SIGNALFD
                    107: #include <sys/signalfd.h>
                    108:
                    109: int    (*host_signalfd)(int, const sigset_t *, int);
                    110: #endif
                    111:
1.30      pooka     112: int    (*host_execve)(const char *, char *const[], char *const[]);
                    113:
1.1       pooka     114: #include "sp_common.c"
1.56      pooka     115: #include "rumpuser_sigtrans.c"
1.1       pooka     116:
1.11      pooka     117: static struct spclient clispc = {
                    118:        .spc_fd = -1,
                    119: };
1.1       pooka     120:
1.61      pooka     121: static int holyfd = -1;
1.15      pooka     122: static sigset_t fullset;
1.12      pooka     123:
1.44      pooka     124: static int doconnect(void);
1.29      pooka     125: static int handshake_req(struct spclient *, int, void *, int, bool);
1.18      pooka     126:
1.32      pooka     127: /*
                    128:  * Default: don't retry.  Most clients can't handle it
                    129:  * (consider e.g. fds suddenly going missing).
                    130:  */
                    131: static time_t retrytimo = 0;
1.18      pooka     132:
1.44      pooka     133: /* always defined to nothingness for now */
                    134: #define ERRLOG(a)
                    135:
1.18      pooka     136: static int
1.39      pooka     137: send_with_recon(struct spclient *spc, struct iovec *iov, size_t iovlen)
1.18      pooka     138: {
1.20      pooka     139:        struct timeval starttime, curtime;
                    140:        time_t prevreconmsg;
                    141:        unsigned reconretries;
1.18      pooka     142:        int rv;
                    143:
1.20      pooka     144:        for (prevreconmsg = 0, reconretries = 0;;) {
1.39      pooka     145:                rv = dosend(spc, iov, iovlen);
1.18      pooka     146:                if (__predict_false(rv == ENOTCONN || rv == EBADF)) {
1.20      pooka     147:                        /* no persistent connections */
1.32      pooka     148:                        if (retrytimo == 0) {
                    149:                                rv = ENOTCONN;
1.20      pooka     150:                                break;
1.32      pooka     151:                        }
1.24      pooka     152:                        if (retrytimo == RUMPCLIENT_RETRYCONN_DIE)
1.43      pooka     153:                                _exit(1);
1.20      pooka     154:
                    155:                        if (!prevreconmsg) {
                    156:                                prevreconmsg = time(NULL);
                    157:                                gettimeofday(&starttime, NULL);
                    158:                        }
                    159:                        if (reconretries == 1) {
                    160:                                if (retrytimo == RUMPCLIENT_RETRYCONN_ONCE) {
                    161:                                        rv = ENOTCONN;
                    162:                                        break;
                    163:                                }
                    164:                                fprintf(stderr, "rump_sp: connection to "
                    165:                                    "kernel lost, trying to reconnect ...\n");
                    166:                        } else if (time(NULL) - prevreconmsg > 120) {
                    167:                                fprintf(stderr, "rump_sp: still trying to "
                    168:                                    "reconnect ...\n");
                    169:                                prevreconmsg = time(NULL);
                    170:                        }
                    171:
                    172:                        /* check that we aren't over the limit */
                    173:                        if (retrytimo > 0) {
1.54      pooka     174:                                time_t tdiff;
1.20      pooka     175:
                    176:                                gettimeofday(&curtime, NULL);
1.54      pooka     177:                                tdiff = curtime.tv_sec - starttime.tv_sec;
                    178:                                if (starttime.tv_usec > curtime.tv_usec)
                    179:                                        tdiff--;
                    180:                                if (tdiff >= retrytimo) {
1.20      pooka     181:                                        fprintf(stderr, "rump_sp: reconnect "
                    182:                                            "failed, %lld second timeout\n",
                    183:                                            (long long)retrytimo);
                    184:                                        return ENOTCONN;
                    185:                                }
                    186:                        }
                    187:
                    188:                        /* adhoc backoff timer */
                    189:                        if (reconretries < 10) {
                    190:                                usleep(100000 * reconretries);
                    191:                        } else {
                    192:                                sleep(MIN(10, reconretries-9));
                    193:                        }
                    194:                        reconretries++;
                    195:
1.44      pooka     196:                        if ((rv = doconnect()) != 0)
1.18      pooka     197:                                continue;
1.29      pooka     198:                        if ((rv = handshake_req(&clispc, HANDSHAKE_GUEST,
                    199:                            NULL, 0, true)) != 0)
1.18      pooka     200:                                continue;
1.20      pooka     201:
                    202:                        /*
                    203:                         * ok, reconnect succesful.  we need to return to
                    204:                         * the upper layer to get the entire PDU resent.
                    205:                         */
                    206:                        if (reconretries != 1)
                    207:                                fprintf(stderr, "rump_sp: reconnected!\n");
                    208:                        rv = EAGAIN;
                    209:                        break;
                    210:                } else {
                    211:                        _DIAGASSERT(errno != EAGAIN);
1.18      pooka     212:                        break;
                    213:                }
1.20      pooka     214:        }
1.18      pooka     215:
                    216:        return rv;
                    217: }
                    218:
1.12      pooka     219: static int
1.18      pooka     220: cliwaitresp(struct spclient *spc, struct respwait *rw, sigset_t *mask,
                    221:        bool keeplock)
1.12      pooka     222: {
1.18      pooka     223:        uint64_t mygen;
                    224:        bool imalive = true;
1.12      pooka     225:
1.15      pooka     226:        pthread_mutex_lock(&spc->spc_mtx);
1.18      pooka     227:        if (!keeplock)
                    228:                sendunlockl(spc);
                    229:        mygen = spc->spc_generation;
1.12      pooka     230:
                    231:        rw->rw_error = 0;
1.18      pooka     232:        while (!rw->rw_done && rw->rw_error == 0) {
                    233:                if (__predict_false(spc->spc_generation != mygen || !imalive))
                    234:                        break;
                    235:
1.12      pooka     236:                /* are we free to receive? */
                    237:                if (spc->spc_istatus == SPCSTATUS_FREE) {
1.49      pooka     238:                        int gotresp, dosig, rv;
1.15      pooka     239:
1.12      pooka     240:                        spc->spc_istatus = SPCSTATUS_BUSY;
                    241:                        pthread_mutex_unlock(&spc->spc_mtx);
                    242:
1.15      pooka     243:                        dosig = 0;
                    244:                        for (gotresp = 0; !gotresp; ) {
1.49      pooka     245: #ifdef USE_KQUEUE
                    246:                                struct kevent kev[8];
                    247:                                int i;
                    248:
1.40      pooka     249:                                /*
                    250:                                 * typically we don't have a frame waiting
                    251:                                 * when we come in here, so call kevent now
                    252:                                 */
1.58      pooka     253:                                rv = host_kevent(holyfd, NULL, 0,
1.40      pooka     254:                                    kev, __arraycount(kev), NULL);
                    255:
                    256:                                if (__predict_false(rv == -1)) {
                    257:                                        goto activity;
                    258:                                }
                    259:
                    260:                                /*
                    261:                                 * XXX: don't know how this can happen
                    262:                                 * (timeout cannot expire since there
                    263:                                 * isn't one), but it does happen.
                    264:                                 * treat it as an expectional condition
                    265:                                 * and go through tryread to determine
                    266:                                 * alive status.
                    267:                                 */
                    268:                                if (__predict_false(rv == 0))
                    269:                                        goto activity;
                    270:
                    271:                                for (i = 0; i < rv; i++) {
                    272:                                        if (kev[i].filter == EVFILT_SIGNAL)
                    273:                                                dosig++;
                    274:                                }
                    275:                                if (dosig)
                    276:                                        goto cleanup;
                    277:
                    278:                                /*
                    279:                                 * ok, activity.  try to read a frame to
                    280:                                 * determine what happens next.
                    281:                                 */
                    282:  activity:
1.58      pooka     283: #else /* !USE_KQUEUE */
                    284:                                struct pollfd pfd[2];
1.49      pooka     285:
1.58      pooka     286:                                pfd[0].fd = clispc.spc_fd;
                    287:                                pfd[0].events = POLLIN;
                    288:                                pfd[1].fd = holyfd;
                    289:                                pfd[1].events = POLLIN;
                    290:
                    291:                                rv = host_poll(pfd, 2, -1);
1.62      pooka     292:                                if (rv >= 1 && pfd[1].revents & POLLIN) {
1.58      pooka     293:                                        dosig = 1;
                    294:                                        goto cleanup;
                    295:                                }
1.49      pooka     296: #endif /* !USE_KQUEUE */
                    297:
1.15      pooka     298:                                switch (readframe(spc)) {
                    299:                                case 0:
                    300:                                        continue;
                    301:                                case -1:
1.18      pooka     302:                                        imalive = false;
1.15      pooka     303:                                        goto cleanup;
                    304:                                default:
1.40      pooka     305:                                        /* case 1 */
1.15      pooka     306:                                        break;
                    307:                                }
1.12      pooka     308:
1.15      pooka     309:                                switch (spc->spc_hdr.rsp_class) {
1.12      pooka     310:                                case RUMPSP_RESP:
                    311:                                case RUMPSP_ERROR:
                    312:                                        kickwaiter(spc);
1.15      pooka     313:                                        gotresp = spc->spc_hdr.rsp_reqno ==
                    314:                                            rw->rw_reqno;
1.12      pooka     315:                                        break;
                    316:                                case RUMPSP_REQ:
                    317:                                        handlereq(spc);
                    318:                                        break;
                    319:                                default:
                    320:                                        /* panic */
                    321:                                        break;
1.15      pooka     322:                                }
1.12      pooka     323:                        }
                    324:
1.15      pooka     325:  cleanup:
                    326:                        pthread_mutex_lock(&spc->spc_mtx);
                    327:                        if (spc->spc_istatus == SPCSTATUS_WANTED)
                    328:                                kickall(spc);
                    329:                        spc->spc_istatus = SPCSTATUS_FREE;
                    330:
                    331:                        /* take one for the team */
                    332:                        if (dosig) {
                    333:                                pthread_mutex_unlock(&spc->spc_mtx);
                    334:                                pthread_sigmask(SIG_SETMASK, mask, NULL);
                    335:                                pthread_sigmask(SIG_SETMASK, &fullset, NULL);
                    336:                                pthread_mutex_lock(&spc->spc_mtx);
                    337:                        }
1.12      pooka     338:                } else {
                    339:                        spc->spc_istatus = SPCSTATUS_WANTED;
                    340:                        pthread_cond_wait(&rw->rw_cv, &spc->spc_mtx);
                    341:                }
                    342:        }
                    343:        TAILQ_REMOVE(&spc->spc_respwait, rw, rw_entries);
                    344:        pthread_mutex_unlock(&spc->spc_mtx);
                    345:        pthread_cond_destroy(&rw->rw_cv);
                    346:
1.18      pooka     347:        if (spc->spc_generation != mygen || !imalive) {
1.12      pooka     348:                return ENOTCONN;
1.18      pooka     349:        }
1.12      pooka     350:        return rw->rw_error;
                    351: }
                    352:
1.1       pooka     353: static int
1.26      pooka     354: syscall_req(struct spclient *spc, sigset_t *omask, int sysnum,
1.3       pooka     355:        const void *data, size_t dlen, void **resp)
1.1       pooka     356: {
                    357:        struct rsp_hdr rhdr;
1.3       pooka     358:        struct respwait rw;
1.39      pooka     359:        struct iovec iov[2];
1.3       pooka     360:        int rv;
1.1       pooka     361:
                    362:        rhdr.rsp_len = sizeof(rhdr) + dlen;
1.3       pooka     363:        rhdr.rsp_class = RUMPSP_REQ;
                    364:        rhdr.rsp_type = RUMPSP_SYSCALL;
1.1       pooka     365:        rhdr.rsp_sysnum = sysnum;
                    366:
1.39      pooka     367:        IOVPUT(iov[0], rhdr);
                    368:        IOVPUT_WITHSIZE(iov[1], __UNCONST(data), dlen);
                    369:
1.6       pooka     370:        do {
                    371:                putwait(spc, &rw, &rhdr);
1.39      pooka     372:                if ((rv = send_with_recon(spc, iov, __arraycount(iov))) != 0) {
1.6       pooka     373:                        unputwait(spc, &rw);
1.18      pooka     374:                        continue;
1.6       pooka     375:                }
                    376:
1.26      pooka     377:                rv = cliwaitresp(spc, &rw, omask, false);
1.20      pooka     378:                if (rv == ENOTCONN)
                    379:                        rv = EAGAIN;
                    380:        } while (rv == EAGAIN);
1.3       pooka     381:
                    382:        *resp = rw.rw_data;
                    383:        return rv;
1.1       pooka     384: }
                    385:
                    386: static int
1.29      pooka     387: handshake_req(struct spclient *spc, int type, void *data,
                    388:        int cancel, bool haslock)
1.10      pooka     389: {
1.11      pooka     390:        struct handshake_fork rf;
1.43      pooka     391:        const char *myprogname = NULL; /* XXXgcc */
1.10      pooka     392:        struct rsp_hdr rhdr;
                    393:        struct respwait rw;
1.12      pooka     394:        sigset_t omask;
1.21      pooka     395:        size_t bonus;
1.39      pooka     396:        struct iovec iov[2];
1.10      pooka     397:        int rv;
                    398:
1.29      pooka     399:        if (type == HANDSHAKE_FORK) {
1.21      pooka     400:                bonus = sizeof(rf);
                    401:        } else {
1.49      pooka     402: #ifdef __NetBSD__
                    403:                /* would procfs work on NetBSD too? */
1.39      pooka     404:                myprogname = getprogname();
1.49      pooka     405: #else
                    406:                int fd = open("/proc/self/comm", O_RDONLY);
                    407:                if (fd == -1) {
                    408:                        myprogname = "???";
                    409:                } else {
                    410:                        static char commname[128];
                    411:
1.50      pooka     412:                        memset(commname, 0, sizeof(commname));
1.49      pooka     413:                        if (read(fd, commname, sizeof(commname)) > 0) {
                    414:                                char *n;
                    415:
                    416:                                n = strrchr(commname, '\n');
                    417:                                if (n)
                    418:                                        *n = '\0';
                    419:                                myprogname = commname;
                    420:                        } else {
                    421:                                myprogname = "???";
                    422:                        }
                    423:                        close(fd);
                    424:                }
                    425: #endif
1.39      pooka     426:                bonus = strlen(myprogname)+1;
1.21      pooka     427:        }
                    428:
1.10      pooka     429:        /* performs server handshake */
1.21      pooka     430:        rhdr.rsp_len = sizeof(rhdr) + bonus;
1.10      pooka     431:        rhdr.rsp_class = RUMPSP_REQ;
                    432:        rhdr.rsp_type = RUMPSP_HANDSHAKE;
1.29      pooka     433:        rhdr.rsp_handshake = type;
1.10      pooka     434:
1.39      pooka     435:        IOVPUT(iov[0], rhdr);
                    436:
1.12      pooka     437:        pthread_sigmask(SIG_SETMASK, &fullset, &omask);
1.18      pooka     438:        if (haslock)
                    439:                putwait_locked(spc, &rw, &rhdr);
                    440:        else
                    441:                putwait(spc, &rw, &rhdr);
1.29      pooka     442:        if (type == HANDSHAKE_FORK) {
                    443:                memcpy(rf.rf_auth, data, sizeof(rf.rf_auth)); /* uh, why? */
1.11      pooka     444:                rf.rf_cancel = cancel;
1.39      pooka     445:                IOVPUT(iov[1], rf);
1.21      pooka     446:        } else {
1.43      pooka     447:                IOVPUT_WITHSIZE(iov[1], __UNCONST(myprogname), bonus);
1.11      pooka     448:        }
1.39      pooka     449:        rv = send_with_recon(spc, iov, __arraycount(iov));
1.18      pooka     450:        if (rv || cancel) {
                    451:                if (haslock)
                    452:                        unputwait_locked(spc, &rw);
                    453:                else
                    454:                        unputwait(spc, &rw);
                    455:                if (cancel) {
1.26      pooka     456:                        goto out;
1.18      pooka     457:                }
                    458:        } else {
                    459:                rv = cliwaitresp(spc, &rw, &omask, haslock);
1.10      pooka     460:        }
                    461:        if (rv)
1.26      pooka     462:                goto out;
1.10      pooka     463:
                    464:        rv = *(int *)rw.rw_data;
                    465:        free(rw.rw_data);
                    466:
1.26      pooka     467:  out:
                    468:        pthread_sigmask(SIG_SETMASK, &omask, NULL);
1.10      pooka     469:        return rv;
                    470: }
                    471:
                    472: static int
1.26      pooka     473: prefork_req(struct spclient *spc, sigset_t *omask, void **resp)
1.11      pooka     474: {
                    475:        struct rsp_hdr rhdr;
                    476:        struct respwait rw;
1.39      pooka     477:        struct iovec iov[1];
1.11      pooka     478:        int rv;
                    479:
                    480:        rhdr.rsp_len = sizeof(rhdr);
                    481:        rhdr.rsp_class = RUMPSP_REQ;
                    482:        rhdr.rsp_type = RUMPSP_PREFORK;
                    483:        rhdr.rsp_error = 0;
                    484:
1.39      pooka     485:        IOVPUT(iov[0], rhdr);
                    486:
1.18      pooka     487:        do {
                    488:                putwait(spc, &rw, &rhdr);
1.39      pooka     489:                rv = send_with_recon(spc, iov, __arraycount(iov));
1.18      pooka     490:                if (rv != 0) {
                    491:                        unputwait(spc, &rw);
                    492:                        continue;
                    493:                }
1.11      pooka     494:
1.26      pooka     495:                rv = cliwaitresp(spc, &rw, omask, false);
1.20      pooka     496:                if (rv == ENOTCONN)
                    497:                        rv = EAGAIN;
                    498:        } while (rv == EAGAIN);
1.18      pooka     499:
1.11      pooka     500:        *resp = rw.rw_data;
                    501:        return rv;
                    502: }
                    503:
1.18      pooka     504: /*
                    505:  * prevent response code from deadlocking with reconnect code
                    506:  */
1.11      pooka     507: static int
1.18      pooka     508: resp_sendlock(struct spclient *spc)
                    509: {
                    510:        int rv = 0;
                    511:
                    512:        pthread_mutex_lock(&spc->spc_mtx);
                    513:        while (spc->spc_ostatus != SPCSTATUS_FREE) {
                    514:                if (__predict_false(spc->spc_reconnecting)) {
                    515:                        rv = EBUSY;
                    516:                        goto out;
                    517:                }
                    518:                spc->spc_ostatus = SPCSTATUS_WANTED;
                    519:                pthread_cond_wait(&spc->spc_cv, &spc->spc_mtx);
                    520:        }
                    521:        spc->spc_ostatus = SPCSTATUS_BUSY;
                    522:
                    523:  out:
                    524:        pthread_mutex_unlock(&spc->spc_mtx);
                    525:        return rv;
                    526: }
                    527:
                    528: static void
1.5       pooka     529: send_copyin_resp(struct spclient *spc, uint64_t reqno, void *data, size_t dlen,
                    530:        int wantstr)
1.1       pooka     531: {
                    532:        struct rsp_hdr rhdr;
1.39      pooka     533:        struct iovec iov[2];
1.1       pooka     534:
1.5       pooka     535:        if (wantstr)
                    536:                dlen = MIN(dlen, strlen(data)+1);
                    537:
1.1       pooka     538:        rhdr.rsp_len = sizeof(rhdr) + dlen;
                    539:        rhdr.rsp_reqno = reqno;
1.3       pooka     540:        rhdr.rsp_class = RUMPSP_RESP;
                    541:        rhdr.rsp_type = RUMPSP_COPYIN;
1.1       pooka     542:        rhdr.rsp_sysnum = 0;
                    543:
1.39      pooka     544:        IOVPUT(iov[0], rhdr);
                    545:        IOVPUT_WITHSIZE(iov[1], data, dlen);
                    546:
1.18      pooka     547:        if (resp_sendlock(spc) != 0)
                    548:                return;
1.39      pooka     549:        (void)SENDIOV(spc, iov);
1.3       pooka     550:        sendunlock(spc);
1.1       pooka     551: }
                    552:
1.18      pooka     553: static void
1.1       pooka     554: send_anonmmap_resp(struct spclient *spc, uint64_t reqno, void *addr)
                    555: {
                    556:        struct rsp_hdr rhdr;
1.39      pooka     557:        struct iovec iov[2];
1.1       pooka     558:
                    559:        rhdr.rsp_len = sizeof(rhdr) + sizeof(addr);
                    560:        rhdr.rsp_reqno = reqno;
1.3       pooka     561:        rhdr.rsp_class = RUMPSP_RESP;
                    562:        rhdr.rsp_type = RUMPSP_ANONMMAP;
1.1       pooka     563:        rhdr.rsp_sysnum = 0;
                    564:
1.39      pooka     565:        IOVPUT(iov[0], rhdr);
                    566:        IOVPUT(iov[1], addr);
                    567:
1.18      pooka     568:        if (resp_sendlock(spc) != 0)
                    569:                return;
1.39      pooka     570:        (void)SENDIOV(spc, iov);
1.3       pooka     571:        sendunlock(spc);
1.1       pooka     572: }
                    573:
                    574: int
                    575: rumpclient_syscall(int sysnum, const void *data, size_t dlen,
                    576:        register_t *retval)
                    577: {
                    578:        struct rsp_sysresp *resp;
1.26      pooka     579:        sigset_t omask;
1.3       pooka     580:        void *rdata;
                    581:        int rv;
                    582:
1.26      pooka     583:        pthread_sigmask(SIG_SETMASK, &fullset, &omask);
                    584:
1.3       pooka     585:        DPRINTF(("rumpsp syscall_req: syscall %d with %p/%zu\n",
                    586:            sysnum, data, dlen));
                    587:
1.26      pooka     588:        rv = syscall_req(&clispc, &omask, sysnum, data, dlen, &rdata);
1.3       pooka     589:        if (rv)
1.26      pooka     590:                goto out;
1.3       pooka     591:
                    592:        resp = rdata;
1.67    ! martin    593:        DPRINTF(("rumpsp syscall_resp: syscall %d error %d, rv: %"
        !           594:            PRIxREGISTER"/%"PRIxREGISTER"\n",
        !           595:            sysnum, rv, resp->rsys_retval[0], resp->rsys_retval[1]));
1.1       pooka     596:
1.3       pooka     597:        memcpy(retval, &resp->rsys_retval, sizeof(resp->rsys_retval));
                    598:        rv = resp->rsys_error;
                    599:        free(rdata);
1.1       pooka     600:
1.26      pooka     601:  out:
                    602:        pthread_sigmask(SIG_SETMASK, &omask, NULL);
1.3       pooka     603:        return rv;
                    604: }
1.1       pooka     605:
1.3       pooka     606: static void
                    607: handlereq(struct spclient *spc)
                    608: {
                    609:        struct rsp_copydata *copydata;
1.16      pooka     610:        struct rsp_hdr *rhdr = &spc->spc_hdr;
1.3       pooka     611:        void *mapaddr;
                    612:        size_t maplen;
1.5       pooka     613:        int reqtype = spc->spc_hdr.rsp_type;
1.56      pooka     614:        int sig;
1.1       pooka     615:
1.5       pooka     616:        switch (reqtype) {
1.3       pooka     617:        case RUMPSP_COPYIN:
1.5       pooka     618:        case RUMPSP_COPYINSTR:
1.3       pooka     619:                /*LINTED*/
                    620:                copydata = (struct rsp_copydata *)spc->spc_buf;
                    621:                DPRINTF(("rump_sp handlereq: copyin request: %p/%zu\n",
                    622:                    copydata->rcp_addr, copydata->rcp_len));
                    623:                send_copyin_resp(spc, spc->spc_hdr.rsp_reqno,
1.5       pooka     624:                    copydata->rcp_addr, copydata->rcp_len,
                    625:                    reqtype == RUMPSP_COPYINSTR);
1.3       pooka     626:                break;
                    627:        case RUMPSP_COPYOUT:
1.5       pooka     628:        case RUMPSP_COPYOUTSTR:
1.3       pooka     629:                /*LINTED*/
                    630:                copydata = (struct rsp_copydata *)spc->spc_buf;
                    631:                DPRINTF(("rump_sp handlereq: copyout request: %p/%zu\n",
                    632:                    copydata->rcp_addr, copydata->rcp_len));
                    633:                /*LINTED*/
                    634:                memcpy(copydata->rcp_addr, copydata->rcp_data,
                    635:                    copydata->rcp_len);
                    636:                break;
                    637:        case RUMPSP_ANONMMAP:
                    638:                /*LINTED*/
                    639:                maplen = *(size_t *)spc->spc_buf;
                    640:                mapaddr = mmap(NULL, maplen, PROT_READ|PROT_WRITE,
1.57      pooka     641:                    MAP_ANON|MAP_PRIVATE, -1, 0);
1.3       pooka     642:                if (mapaddr == MAP_FAILED)
                    643:                        mapaddr = NULL;
                    644:                DPRINTF(("rump_sp handlereq: anonmmap: %p\n", mapaddr));
                    645:                send_anonmmap_resp(spc, spc->spc_hdr.rsp_reqno, mapaddr);
                    646:                break;
1.16      pooka     647:        case RUMPSP_RAISE:
1.56      pooka     648:                sig = rumpuser__sig_rump2host(rhdr->rsp_signo);
                    649:                DPRINTF(("rump_sp handlereq: raise sig %d\n", sig));
                    650:                raise(sig);
1.16      pooka     651:                /*
                    652:                 * We most likely have signals blocked, but the signal
                    653:                 * will be handled soon enough when we return.
                    654:                 */
                    655:                break;
1.3       pooka     656:        default:
1.12      pooka     657:                printf("PANIC: INVALID TYPE %d\n", reqtype);
1.3       pooka     658:                abort();
                    659:                break;
1.1       pooka     660:        }
                    661:
1.6       pooka     662:        spcfreebuf(spc);
1.1       pooka     663: }
                    664:
1.11      pooka     665: static unsigned ptab_idx;
                    666: static struct sockaddr *serv_sa;
                    667:
1.27      pooka     668: /* dup until we get a "good" fd which does not collide with stdio */
                    669: static int
1.28      pooka     670: dupgood(int myfd, int mustchange)
1.27      pooka     671: {
1.28      pooka     672:        int ofds[4];
1.45      alnsn     673:        int sverrno;
1.48      matt      674:        unsigned int i;
1.27      pooka     675:
1.28      pooka     676:        for (i = 0; (myfd <= 2 || mustchange) && myfd != -1; i++) {
1.27      pooka     677:                assert(i < __arraycount(ofds));
                    678:                ofds[i] = myfd;
                    679:                myfd = host_dup(myfd);
1.28      pooka     680:                if (mustchange) {
                    681:                        i--; /* prevent closing old fd */
                    682:                        mustchange = 0;
                    683:                }
1.27      pooka     684:        }
                    685:
1.45      alnsn     686:        sverrno = 0;
                    687:        if (myfd == -1 && i > 0)
                    688:                sverrno = errno;
                    689:
1.48      matt      690:        while (i-- > 0) {
1.27      pooka     691:                host_close(ofds[i]);
                    692:        }
                    693:
1.45      alnsn     694:        if (sverrno)
                    695:                errno = sverrno;
                    696:
1.27      pooka     697:        return myfd;
                    698: }
                    699:
1.58      pooka     700: #if defined(USE_KQUEUE)
                    701:
                    702: static int
                    703: makeholyfd(void)
                    704: {
                    705:        struct kevent kev[NSIG+1];
                    706:        int i, fd;
                    707:
                    708:        /* setup kqueue, we want all signals and the fd */
                    709:        if ((fd = dupgood(host_kqueue(), 0)) == -1) {
                    710:                ERRLOG(("rump_sp: cannot setup kqueue"));
                    711:                return -1;
                    712:        }
                    713:
                    714:        for (i = 0; i < NSIG; i++) {
                    715:                EV_SET(&kev[i], i+1, EVFILT_SIGNAL, EV_ADD|EV_ENABLE, 0, 0, 0);
                    716:        }
                    717:        EV_SET(&kev[NSIG], clispc.spc_fd,
                    718:            EVFILT_READ, EV_ADD|EV_ENABLE, 0, 0, 0);
                    719:        if (host_kevent(fd, kev, NSIG+1, NULL, 0, NULL) == -1) {
                    720:                ERRLOG(("rump_sp: kevent() failed"));
1.59      pooka     721:                host_close(fd);
1.58      pooka     722:                return -1;
1.59      pooka     723:        }
1.58      pooka     724:
                    725:        return fd;
                    726: }
                    727:
                    728: #elif defined(USE_SIGNALFD) /* !USE_KQUEUE */
                    729:
                    730: static int
                    731: makeholyfd(void)
                    732: {
                    733:
                    734:        return host_signalfd(-1, &fullset, 0);
                    735: }
                    736:
                    737: #else /* !USE_KQUEUE && !USE_SIGNALFD */
                    738:
                    739: static int
                    740: makeholyfd(void)
                    741: {
                    742:
                    743:        return -1;
                    744: }
                    745:
                    746: #endif
                    747:
1.11      pooka     748: static int
1.44      pooka     749: doconnect(void)
1.1       pooka     750: {
1.18      pooka     751:        struct respwait rw;
                    752:        struct rsp_hdr rhdr;
1.9       pooka     753:        char banner[MAXBANNER];
1.49      pooka     754:        int s, error, flags;
1.9       pooka     755:        ssize_t n;
1.1       pooka     756:
1.58      pooka     757:        if (holyfd != -1)
                    758:                host_close(holyfd);
                    759:        holyfd = -1;
1.19      pooka     760:        s = -1;
1.18      pooka     761:
                    762:        if (clispc.spc_fd != -1)
                    763:                host_close(clispc.spc_fd);
                    764:        clispc.spc_fd = -1;
                    765:
                    766:        /*
                    767:         * for reconnect, gate everyone out of the receiver code
                    768:         */
                    769:        putwait_locked(&clispc, &rw, &rhdr);
                    770:
                    771:        pthread_mutex_lock(&clispc.spc_mtx);
                    772:        clispc.spc_reconnecting = 1;
                    773:        pthread_cond_broadcast(&clispc.spc_cv);
                    774:        clispc.spc_generation++;
                    775:        while (clispc.spc_istatus != SPCSTATUS_FREE) {
                    776:                clispc.spc_istatus = SPCSTATUS_WANTED;
                    777:                pthread_cond_wait(&rw.rw_cv, &clispc.spc_mtx);
                    778:        }
                    779:        kickall(&clispc);
                    780:
                    781:        /*
                    782:         * we can release it already since we hold the
                    783:         * send lock during reconnect
                    784:         * XXX: assert it
                    785:         */
                    786:        clispc.spc_istatus = SPCSTATUS_FREE;
                    787:        pthread_mutex_unlock(&clispc.spc_mtx);
                    788:        unputwait_locked(&clispc, &rw);
                    789:
                    790:        free(clispc.spc_buf);
                    791:        clispc.spc_off = 0;
                    792:
1.28      pooka     793:        s = dupgood(host_socket(parsetab[ptab_idx].domain, SOCK_STREAM, 0), 0);
1.11      pooka     794:        if (s == -1)
1.2       pooka     795:                return -1;
1.1       pooka     796:
1.49      pooka     797:        while (host_connect(s, serv_sa, parsetab[ptab_idx].slen) == -1) {
1.18      pooka     798:                if (errno == EINTR)
                    799:                        continue;
1.44      pooka     800:                ERRLOG(("rump_sp: client connect failed: %s\n",
                    801:                    strerror(errno)));
1.20      pooka     802:                return -1;
1.2       pooka     803:        }
1.1       pooka     804:
1.11      pooka     805:        if ((error = parsetab[ptab_idx].connhook(s)) != 0) {
1.44      pooka     806:                ERRLOG(("rump_sp: connect hook failed\n"));
1.2       pooka     807:                return -1;
1.1       pooka     808:        }
1.4       pooka     809:
1.45      alnsn     810:        if ((n = host_read(s, banner, sizeof(banner)-1)) <= 0) {
1.44      pooka     811:                ERRLOG(("rump_sp: failed to read banner\n"));
1.2       pooka     812:                return -1;
1.1       pooka     813:        }
1.9       pooka     814:
                    815:        if (banner[n-1] != '\n') {
1.44      pooka     816:                ERRLOG(("rump_sp: invalid banner\n"));
1.9       pooka     817:                return -1;
                    818:        }
                    819:        banner[n] = '\0';
1.45      alnsn     820:        /* XXX parse the banner some day */
1.9       pooka     821:
1.15      pooka     822:        flags = host_fcntl(s, F_GETFL, 0);
                    823:        if (host_fcntl(s, F_SETFL, flags | O_NONBLOCK) == -1) {
1.44      pooka     824:                ERRLOG(("rump_sp: socket fd NONBLOCK: %s\n", strerror(errno)));
1.15      pooka     825:                return -1;
                    826:        }
1.18      pooka     827:        clispc.spc_fd = s;
                    828:        clispc.spc_state = SPCSTATE_RUNNING;
                    829:        clispc.spc_reconnecting = 0;
1.58      pooka     830:        holyfd = makeholyfd();
1.15      pooka     831:
1.18      pooka     832:        return 0;
                    833: }
                    834:
                    835: static int
                    836: doinit(void)
                    837: {
                    838:
1.11      pooka     839:        TAILQ_INIT(&clispc.spc_respwait);
                    840:        pthread_mutex_init(&clispc.spc_mtx, NULL);
                    841:        pthread_cond_init(&clispc.spc_cv, NULL);
                    842:
                    843:        return 0;
                    844: }
                    845:
1.53      pooka     846: #ifdef RTLD_NEXT
1.35      pooka     847: void *rumpclient__dlsym(void *, const char *);
                    848: void *
                    849: rumpclient__dlsym(void *handle, const char *symbol)
                    850: {
                    851:
                    852:        return dlsym(handle, symbol);
                    853: }
1.49      pooka     854: void *rumphijack_dlsym(void *, const char *)
                    855:     __attribute__((__weak__, alias("rumpclient__dlsym")));
1.53      pooka     856: #endif
1.35      pooka     857:
1.38      pooka     858: static pid_t init_done = 0;
1.13      pooka     859:
1.11      pooka     860: int
1.46      joerg     861: rumpclient_init(void)
1.11      pooka     862: {
                    863:        char *p;
                    864:        int error;
1.29      pooka     865:        int rv = -1;
                    866:        int hstype;
1.38      pooka     867:        pid_t mypid;
1.29      pooka     868:
1.38      pooka     869:        /*
                    870:         * Make sure we're not riding the context of a previous
                    871:         * host fork.  Note: it's *possible* that after n>1 forks
                    872:         * we have the same pid as one of our exited parents, but
                    873:         * I'm pretty sure there are 0 practical implications, since
                    874:         * it means generations would have to skip rumpclient init.
                    875:         */
                    876:        if (init_done == (mypid = getpid()))
1.29      pooka     877:                return 0;
1.38      pooka     878:
1.61      pooka     879: #ifdef USE_KQUEUE
1.38      pooka     880:        /* kq does not traverse fork() */
1.61      pooka     881:        holyfd = -1;
1.58      pooka     882: #endif
1.38      pooka     883:        init_done = mypid;
1.11      pooka     884:
1.25      pooka     885:        sigfillset(&fullset);
                    886:
1.13      pooka     887:        /*
1.49      pooka     888:         * sag mir, wo die symbols sind.  zogen fort, der krieg beginnt.
1.13      pooka     889:         * wann wird man je verstehen?  wann wird man je verstehen?
                    890:         */
1.53      pooka     891: #ifdef RTLD_NEXT
1.13      pooka     892: #define FINDSYM2(_name_,_syscall_)                                     \
1.35      pooka     893:        if ((host_##_name_ = rumphijack_dlsym(RTLD_NEXT,                \
1.34      pooka     894:            #_syscall_)) == NULL) {                                     \
1.36      pooka     895:                if (rumphijack_dlsym == rumpclient__dlsym)              \
1.34      pooka     896:                        host_##_name_ = _name_; /* static fallback */   \
1.54      pooka     897:                if (host_##_name_ == NULL) {                            \
                    898:                        fprintf(stderr,"cannot find %s: %s", #_syscall_,\
1.34      pooka     899:                            dlerror());                                 \
1.54      pooka     900:                        exit(1);                                        \
                    901:                }                                                       \
1.34      pooka     902:        }
1.53      pooka     903: #else
                    904: #define FINDSYM2(_name_,_syscall)                                      \
                    905:        host_##_name_ = _name_;
                    906: #endif
1.13      pooka     907: #define FINDSYM(_name_) FINDSYM2(_name_,_name_)
1.49      pooka     908: #ifdef __NetBSD__
1.34      pooka     909:        FINDSYM2(socket,__socket30)
1.49      pooka     910: #else
                    911:        FINDSYM(socket)
                    912: #endif
                    913:
1.34      pooka     914:        FINDSYM(close)
                    915:        FINDSYM(connect)
                    916:        FINDSYM(fcntl)
                    917:        FINDSYM(poll)
                    918:        FINDSYM(read)
1.39      pooka     919:        FINDSYM(sendmsg)
1.34      pooka     920:        FINDSYM(setsockopt)
                    921:        FINDSYM(dup)
1.49      pooka     922:        FINDSYM(execve)
                    923:
                    924: #ifdef USE_KQUEUE
1.34      pooka     925:        FINDSYM(kqueue)
1.63      justin    926: #ifdef __NetBSD__
1.22      pooka     927: #if !__NetBSD_Prereq__(5,99,7)
1.34      pooka     928:        FINDSYM(kevent)
1.22      pooka     929: #else
1.34      pooka     930:        FINDSYM2(kevent,_sys___kevent50)
1.22      pooka     931: #endif
1.63      justin    932: #else
                    933:        FINDSYM(kevent)
                    934: #endif
1.49      pooka     935: #endif /* USE_KQUEUE */
                    936:
1.58      pooka     937: #ifdef USE_SIGNALFD
                    938:        FINDSYM(signalfd)
                    939: #endif
                    940:
1.13      pooka     941: #undef FINDSYM
                    942: #undef FINDSY2
                    943:
1.28      pooka     944:        if ((p = getenv("RUMP__PARSEDSERVER")) == NULL) {
                    945:                if ((p = getenv("RUMP_SERVER")) == NULL) {
1.52      pooka     946:                        fprintf(stderr, "error: RUMP_SERVER not set\n");
1.28      pooka     947:                        errno = ENOENT;
1.29      pooka     948:                        goto out;
1.28      pooka     949:                }
1.11      pooka     950:        }
                    951:
                    952:        if ((error = parseurl(p, &serv_sa, &ptab_idx, 0)) != 0) {
                    953:                errno = error;
1.29      pooka     954:                goto out;
1.11      pooka     955:        }
                    956:
1.18      pooka     957:        if (doinit() == -1)
1.29      pooka     958:                goto out;
1.28      pooka     959:
                    960:        if ((p = getenv("RUMPCLIENT__EXECFD")) != NULL) {
1.58      pooka     961:                sscanf(p, "%d,%d", &clispc.spc_fd, &holyfd);
1.28      pooka     962:                unsetenv("RUMPCLIENT__EXECFD");
1.29      pooka     963:                hstype = HANDSHAKE_EXEC;
                    964:        } else {
1.44      pooka     965:                if (doconnect() == -1)
1.29      pooka     966:                        goto out;
                    967:                hstype = HANDSHAKE_GUEST;
1.28      pooka     968:        }
                    969:
1.29      pooka     970:        error = handshake_req(&clispc, hstype, NULL, 0, false);
1.11      pooka     971:        if (error) {
                    972:                pthread_mutex_destroy(&clispc.spc_mtx);
                    973:                pthread_cond_destroy(&clispc.spc_cv);
1.18      pooka     974:                if (clispc.spc_fd != -1)
                    975:                        host_close(clispc.spc_fd);
1.10      pooka     976:                errno = error;
1.29      pooka     977:                goto out;
1.10      pooka     978:        }
1.29      pooka     979:        rv = 0;
1.10      pooka     980:
1.29      pooka     981:  out:
                    982:        if (rv == -1)
                    983:                init_done = 0;
                    984:        return rv;
1.11      pooka     985: }
                    986:
                    987: struct rumpclient_fork {
                    988:        uint32_t fork_auth[AUTHLEN];
1.31      pooka     989:        struct spclient fork_spc;
1.58      pooka     990:        int fork_holyfd;
1.11      pooka     991: };
                    992:
                    993: struct rumpclient_fork *
                    994: rumpclient_prefork(void)
                    995: {
                    996:        struct rumpclient_fork *rpf;
1.26      pooka     997:        sigset_t omask;
1.11      pooka     998:        void *resp;
                    999:        int rv;
                   1000:
1.26      pooka    1001:        pthread_sigmask(SIG_SETMASK, &fullset, &omask);
1.11      pooka    1002:        rpf = malloc(sizeof(*rpf));
                   1003:        if (rpf == NULL)
1.30      pooka    1004:                goto out;
1.11      pooka    1005:
1.26      pooka    1006:        if ((rv = prefork_req(&clispc, &omask, &resp)) != 0) {
1.11      pooka    1007:                free(rpf);
                   1008:                errno = rv;
1.26      pooka    1009:                rpf = NULL;
                   1010:                goto out;
1.11      pooka    1011:        }
                   1012:
                   1013:        memcpy(rpf->fork_auth, resp, sizeof(rpf->fork_auth));
                   1014:        free(resp);
                   1015:
1.31      pooka    1016:        rpf->fork_spc = clispc;
1.58      pooka    1017:        rpf->fork_holyfd = holyfd;
1.31      pooka    1018:
1.26      pooka    1019:  out:
                   1020:        pthread_sigmask(SIG_SETMASK, &omask, NULL);
1.11      pooka    1021:        return rpf;
                   1022: }
                   1023:
                   1024: int
                   1025: rumpclient_fork_init(struct rumpclient_fork *rpf)
                   1026: {
                   1027:        int error;
1.23      pooka    1028:        int osock;
1.11      pooka    1029:
1.23      pooka    1030:        osock = clispc.spc_fd;
1.11      pooka    1031:        memset(&clispc, 0, sizeof(clispc));
1.23      pooka    1032:        clispc.spc_fd = osock;
                   1033:
1.58      pooka    1034: #ifdef USE_KQUEUE
                   1035:        holyfd = -1; /* kqueue descriptor is not copied over fork() */
                   1036: #else
                   1037:        if (holyfd != -1) {
                   1038:                host_close(holyfd);
                   1039:                holyfd = -1;
                   1040:        }
                   1041: #endif
1.11      pooka    1042:
1.18      pooka    1043:        if (doinit() == -1)
                   1044:                return -1;
1.44      pooka    1045:        if (doconnect() == -1)
1.11      pooka    1046:                return -1;
1.10      pooka    1047:
1.29      pooka    1048:        error = handshake_req(&clispc, HANDSHAKE_FORK, rpf->fork_auth,
                   1049:            0, false);
1.10      pooka    1050:        if (error) {
                   1051:                pthread_mutex_destroy(&clispc.spc_mtx);
                   1052:                pthread_cond_destroy(&clispc.spc_cv);
1.11      pooka    1053:                errno = error;
                   1054:                return -1;
1.10      pooka    1055:        }
1.11      pooka    1056:
                   1057:        return 0;
1.1       pooka    1058: }
1.20      pooka    1059:
1.42      pooka    1060: /*ARGSUSED*/
1.20      pooka    1061: void
1.31      pooka    1062: rumpclient_fork_cancel(struct rumpclient_fork *rpf)
                   1063: {
                   1064:
                   1065:        /* EUNIMPL */
                   1066: }
                   1067:
                   1068: void
                   1069: rumpclient_fork_vparent(struct rumpclient_fork *rpf)
                   1070: {
                   1071:
                   1072:        clispc = rpf->fork_spc;
1.58      pooka    1073:        holyfd = rpf->fork_holyfd;
1.31      pooka    1074: }
                   1075:
                   1076: void
1.20      pooka    1077: rumpclient_setconnretry(time_t timeout)
                   1078: {
                   1079:
1.24      pooka    1080:        if (timeout < RUMPCLIENT_RETRYCONN_DIE)
1.20      pooka    1081:                return; /* gigo */
                   1082:
                   1083:        retrytimo = timeout;
                   1084: }
1.28      pooka    1085:
                   1086: int
                   1087: rumpclient__closenotify(int *fdp, enum rumpclient_closevariant variant)
                   1088: {
                   1089:        int fd = *fdp;
                   1090:        int untilfd, rv;
                   1091:        int newfd;
                   1092:
                   1093:        switch (variant) {
                   1094:        case RUMPCLIENT_CLOSE_FCLOSEM:
1.58      pooka    1095:                untilfd = MAX(clispc.spc_fd, holyfd);
1.28      pooka    1096:                for (; fd <= untilfd; fd++) {
1.58      pooka    1097:                        if (fd == clispc.spc_fd || fd == holyfd)
1.28      pooka    1098:                                continue;
                   1099:                        rv = host_close(fd);
                   1100:                        if (rv == -1)
                   1101:                                return -1;
                   1102:                }
                   1103:                *fdp = fd;
                   1104:                break;
                   1105:
                   1106:        case RUMPCLIENT_CLOSE_CLOSE:
                   1107:        case RUMPCLIENT_CLOSE_DUP2:
                   1108:                if (fd == clispc.spc_fd) {
                   1109:                        newfd = dupgood(clispc.spc_fd, 1);
                   1110:                        if (newfd == -1)
                   1111:                                return -1;
1.49      pooka    1112:
                   1113: #ifdef USE_KQUEUE
                   1114:                        {
                   1115:                        struct kevent kev[2];
                   1116:
1.28      pooka    1117:                        /*
                   1118:                         * now, we have a new socket number, so change
                   1119:                         * the file descriptor that kqueue is
                   1120:                         * monitoring.  remove old and add new.
                   1121:                         */
                   1122:                        EV_SET(&kev[0], clispc.spc_fd,
                   1123:                            EVFILT_READ, EV_DELETE, 0, 0, 0);
                   1124:                        EV_SET(&kev[1], newfd,
                   1125:                            EVFILT_READ, EV_ADD|EV_ENABLE, 0, 0, 0);
1.58      pooka    1126:                        if (host_kevent(holyfd, kev, 2, NULL, 0, NULL) == -1) {
1.28      pooka    1127:                                int sverrno = errno;
                   1128:                                host_close(newfd);
                   1129:                                errno = sverrno;
                   1130:                                return -1;
1.58      pooka    1131:                        }}
                   1132: #endif /* !USE_KQUEUE */
1.28      pooka    1133:                        clispc.spc_fd = newfd;
                   1134:                }
1.58      pooka    1135:                if (holyfd != -1 && fd == holyfd) {
                   1136:                        newfd = dupgood(holyfd, 1);
1.28      pooka    1137:                        if (newfd == -1)
                   1138:                                return -1;
1.58      pooka    1139:                        holyfd = newfd;
1.28      pooka    1140:                }
                   1141:                break;
                   1142:        }
                   1143:
                   1144:        return 0;
                   1145: }
                   1146:
1.30      pooka    1147: pid_t
1.46      joerg    1148: rumpclient_fork(void)
1.30      pooka    1149: {
                   1150:
1.31      pooka    1151:        return rumpclient__dofork(fork);
1.30      pooka    1152: }
                   1153:
1.28      pooka    1154: /*
                   1155:  * Process is about to exec.  Save info about our existing connection
                   1156:  * in the env.  rumpclient will check for this info in init().
                   1157:  * This is mostly for the benefit of rumphijack, but regular applications
                   1158:  * may use it as well.
                   1159:  */
                   1160: int
1.30      pooka    1161: rumpclient_exec(const char *path, char *const argv[], char *const envp[])
1.28      pooka    1162: {
                   1163:        char buf[4096];
                   1164:        char **newenv;
                   1165:        char *envstr, *envstr2;
1.30      pooka    1166:        size_t nelem;
                   1167:        int rv, sverrno;
1.28      pooka    1168:
                   1169:        snprintf(buf, sizeof(buf), "RUMPCLIENT__EXECFD=%d,%d",
1.58      pooka    1170:            clispc.spc_fd, holyfd);
1.28      pooka    1171:        envstr = malloc(strlen(buf)+1);
                   1172:        if (envstr == NULL) {
                   1173:                return ENOMEM;
                   1174:        }
                   1175:        strcpy(envstr, buf);
                   1176:
                   1177:        /* do we have a fully parsed url we want to forward in the env? */
                   1178:        if (*parsedurl != '\0') {
                   1179:                snprintf(buf, sizeof(buf),
                   1180:                    "RUMP__PARSEDSERVER=%s", parsedurl);
                   1181:                envstr2 = malloc(strlen(buf)+1);
                   1182:                if (envstr2 == NULL) {
                   1183:                        free(envstr);
                   1184:                        return ENOMEM;
                   1185:                }
                   1186:                strcpy(envstr2, buf);
                   1187:        } else {
                   1188:                envstr2 = NULL;
                   1189:        }
                   1190:
1.30      pooka    1191:        for (nelem = 0; envp && envp[nelem]; nelem++)
                   1192:                continue;
1.28      pooka    1193:
1.33      pooka    1194:        newenv = malloc(sizeof(*newenv) * (nelem+3));
1.28      pooka    1195:        if (newenv == NULL) {
                   1196:                free(envstr2);
                   1197:                free(envstr);
                   1198:                return ENOMEM;
                   1199:        }
1.30      pooka    1200:        memcpy(&newenv[0], envp, nelem*sizeof(*envp));
1.28      pooka    1201:
1.30      pooka    1202:        newenv[nelem] = envstr;
                   1203:        newenv[nelem+1] = envstr2;
                   1204:        newenv[nelem+2] = NULL;
                   1205:
                   1206:        rv = host_execve(path, argv, newenv);
                   1207:
                   1208:        _DIAGASSERT(rv != 0);
                   1209:        sverrno = errno;
                   1210:        free(envstr2);
                   1211:        free(envstr);
                   1212:        free(newenv);
                   1213:        errno = sverrno;
                   1214:        return rv;
1.28      pooka    1215: }
1.31      pooka    1216:
1.54      pooka    1217: /*
                   1218:  * daemon() is handwritten for the benefit of platforms which
                   1219:  * do not support daemon().
                   1220:  */
1.31      pooka    1221: int
                   1222: rumpclient_daemon(int nochdir, int noclose)
                   1223: {
                   1224:        struct rumpclient_fork *rf;
                   1225:        int sverrno;
                   1226:
                   1227:        if ((rf = rumpclient_prefork()) == NULL)
                   1228:                return -1;
                   1229:
1.54      pooka    1230:        switch (fork()) {
                   1231:        case 0:
                   1232:                break;
                   1233:        case -1:
                   1234:                goto daemonerr;
                   1235:        default:
                   1236:                _exit(0);
                   1237:        }
                   1238:
                   1239:        if (setsid() == -1)
                   1240:                goto daemonerr;
                   1241:        if (!nochdir && chdir("/") == -1)
                   1242:                goto daemonerr;
                   1243:        if (!noclose) {
                   1244:                int fd = open("/dev/null", O_RDWR);
                   1245:                dup2(fd, 0);
                   1246:                dup2(fd, 1);
                   1247:                dup2(fd, 2);
                   1248:                if (fd > 2)
                   1249:                        close(fd);
1.31      pooka    1250:        }
                   1251:
1.54      pooka    1252:        /* note: fork is either completed or cancelled by the call */
1.31      pooka    1253:        if (rumpclient_fork_init(rf) == -1)
                   1254:                return -1;
                   1255:
                   1256:        return 0;
1.54      pooka    1257:
                   1258:  daemonerr:
                   1259:        sverrno = errno;
                   1260:        rumpclient_fork_cancel(rf);
                   1261:        errno = sverrno;
                   1262:        return -1;
1.31      pooka    1263: }

CVSweb <webmaster@jp.NetBSD.org>