[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.60

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

CVSweb <webmaster@jp.NetBSD.org>