2016-09-06 10:45:41 +03:00
|
|
|
/* $NetBSD: rumpclient.c,v 1.67 2016/09/06 07:45:41 martin Exp $ */
|
2010-11-05 00:01:28 +03:00
|
|
|
|
|
|
|
/*
|
2011-01-05 20:14:50 +03:00
|
|
|
* Copyright (c) 2010, 2011 Antti Kantee. All Rights Reserved.
|
2010-11-05 00:01:28 +03:00
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
|
|
|
|
* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
|
|
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
* DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Client side routines for rump syscall proxy.
|
|
|
|
*/
|
|
|
|
|
2013-09-10 20:53:06 +04:00
|
|
|
#include <rump/rumpuser_port.h>
|
2012-08-03 15:31:33 +04:00
|
|
|
|
|
|
|
/*
|
2014-12-13 20:18:55 +03:00
|
|
|
* We use kqueue on the BSDs, poll elsewhere. We
|
2012-08-03 15:31:33 +04:00
|
|
|
* want to use kqueue because it will give us the ability to get signal
|
|
|
|
* notifications but defer their handling to a stage where we do not
|
|
|
|
* hold the communication lock. Taking a signal while holding on to
|
|
|
|
* that lock may cause a deadlock. Therefore, block signals throughout
|
2014-04-02 18:48:03 +04:00
|
|
|
* the RPC when using poll. On Linux, we use signalfd in the same role
|
|
|
|
* as kqueue on NetBSD to be able to take signals while waiting for a
|
|
|
|
* response from the server.
|
2012-08-03 15:31:33 +04:00
|
|
|
*/
|
|
|
|
|
2014-12-13 20:18:55 +03:00
|
|
|
#if defined(__NetBSD__) || defined(__FreeBSD__) || \
|
|
|
|
defined(__DragonFly__) || defined(__OpenBSD__)
|
2012-08-03 15:31:33 +04:00
|
|
|
#define USE_KQUEUE
|
|
|
|
#endif
|
2015-01-17 22:34:50 +03:00
|
|
|
#if defined(__linux__)
|
2014-04-02 18:48:03 +04:00
|
|
|
#define USE_SIGNALFD
|
|
|
|
#endif
|
2012-08-03 15:31:33 +04:00
|
|
|
|
2016-09-06 10:45:41 +03:00
|
|
|
__RCSID("$NetBSD: rumpclient.c,v 1.67 2016/09/06 07:45:41 martin Exp $");
|
2010-11-05 00:01:28 +03:00
|
|
|
|
2010-11-25 20:59:02 +03:00
|
|
|
#include <sys/param.h>
|
2010-11-05 00:01:28 +03:00
|
|
|
#include <sys/mman.h>
|
|
|
|
#include <sys/socket.h>
|
2012-08-03 15:31:33 +04:00
|
|
|
#include <sys/time.h>
|
|
|
|
|
|
|
|
#ifdef USE_KQUEUE
|
|
|
|
#include <sys/event.h>
|
|
|
|
#endif
|
2010-11-05 00:01:28 +03:00
|
|
|
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <netinet/tcp.h>
|
|
|
|
|
|
|
|
#include <assert.h>
|
2011-01-07 22:37:51 +03:00
|
|
|
#include <dlfcn.h>
|
2010-11-05 00:01:28 +03:00
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <poll.h>
|
|
|
|
#include <pthread.h>
|
2011-01-05 20:14:50 +03:00
|
|
|
#include <signal.h>
|
2010-11-05 00:01:28 +03:00
|
|
|
#include <stdarg.h>
|
2011-01-24 20:47:51 +03:00
|
|
|
#include <stdbool.h>
|
2010-11-05 00:01:28 +03:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include <rump/rumpclient.h>
|
|
|
|
|
2011-01-07 22:37:51 +03:00
|
|
|
#define HOSTOPS
|
|
|
|
int (*host_socket)(int, int, int);
|
|
|
|
int (*host_close)(int);
|
|
|
|
int (*host_connect)(int, const struct sockaddr *, socklen_t);
|
2011-01-10 22:49:43 +03:00
|
|
|
int (*host_fcntl)(int, int, ...);
|
2011-01-07 22:37:51 +03:00
|
|
|
int (*host_poll)(struct pollfd *, nfds_t, int);
|
|
|
|
ssize_t (*host_read)(int, void *, size_t);
|
2011-03-08 18:34:37 +03:00
|
|
|
ssize_t (*host_sendmsg)(int, const struct msghdr *, int);
|
2011-01-07 22:37:51 +03:00
|
|
|
int (*host_setsockopt)(int, int, int, const void *, socklen_t);
|
2011-02-09 17:29:58 +03:00
|
|
|
int (*host_dup)(int);
|
2011-01-07 22:37:51 +03:00
|
|
|
|
2012-08-03 15:31:33 +04:00
|
|
|
#ifdef USE_KQUEUE
|
2011-01-21 13:43:33 +03:00
|
|
|
int (*host_kqueue)(void);
|
2014-12-08 04:10:07 +03:00
|
|
|
#ifdef __NetBSD__
|
2011-01-21 13:43:33 +03:00
|
|
|
int (*host_kevent)(int, const struct kevent *, size_t,
|
|
|
|
struct kevent *, size_t, const struct timespec *);
|
2014-12-08 04:10:07 +03:00
|
|
|
#else
|
|
|
|
int (*host_kevent)(int, const struct kevent *, int,
|
|
|
|
struct kevent *, int, const struct timespec *);
|
|
|
|
#endif
|
2012-08-03 15:31:33 +04:00
|
|
|
#endif
|
2011-01-21 13:43:33 +03:00
|
|
|
|
2014-04-02 18:48:03 +04:00
|
|
|
#ifdef USE_SIGNALFD
|
|
|
|
#include <sys/signalfd.h>
|
|
|
|
|
|
|
|
int (*host_signalfd)(int, const sigset_t *, int);
|
|
|
|
#endif
|
|
|
|
|
2011-02-16 18:33:46 +03:00
|
|
|
int (*host_execve)(const char *, char *const[], char *const[]);
|
|
|
|
|
2010-11-05 00:01:28 +03:00
|
|
|
#include "sp_common.c"
|
2014-02-20 04:42:27 +04:00
|
|
|
#include "rumpuser_sigtrans.c"
|
2010-11-05 00:01:28 +03:00
|
|
|
|
2011-01-05 20:14:50 +03:00
|
|
|
static struct spclient clispc = {
|
|
|
|
.spc_fd = -1,
|
|
|
|
};
|
2010-11-05 00:01:28 +03:00
|
|
|
|
2014-04-03 21:11:35 +04:00
|
|
|
static int holyfd = -1;
|
2011-01-06 09:57:14 +03:00
|
|
|
static sigset_t fullset;
|
2011-01-10 22:49:43 +03:00
|
|
|
|
2011-03-15 12:35:05 +03:00
|
|
|
static int doconnect(void);
|
2011-02-15 13:37:07 +03:00
|
|
|
static int handshake_req(struct spclient *, int, void *, int, bool);
|
2011-01-24 20:47:51 +03:00
|
|
|
|
2011-02-16 22:26:58 +03:00
|
|
|
/*
|
|
|
|
* Default: don't retry. Most clients can't handle it
|
|
|
|
* (consider e.g. fds suddenly going missing).
|
|
|
|
*/
|
|
|
|
static time_t retrytimo = 0;
|
2011-01-24 20:47:51 +03:00
|
|
|
|
2011-03-15 12:35:05 +03:00
|
|
|
/* always defined to nothingness for now */
|
|
|
|
#define ERRLOG(a)
|
|
|
|
|
2011-01-24 20:47:51 +03:00
|
|
|
static int
|
2011-03-08 18:34:37 +03:00
|
|
|
send_with_recon(struct spclient *spc, struct iovec *iov, size_t iovlen)
|
2011-01-24 20:47:51 +03:00
|
|
|
{
|
2011-01-27 21:04:05 +03:00
|
|
|
struct timeval starttime, curtime;
|
|
|
|
time_t prevreconmsg;
|
|
|
|
unsigned reconretries;
|
2011-01-24 20:47:51 +03:00
|
|
|
int rv;
|
|
|
|
|
2011-01-27 21:04:05 +03:00
|
|
|
for (prevreconmsg = 0, reconretries = 0;;) {
|
2011-03-08 18:34:37 +03:00
|
|
|
rv = dosend(spc, iov, iovlen);
|
2011-01-24 20:47:51 +03:00
|
|
|
if (__predict_false(rv == ENOTCONN || rv == EBADF)) {
|
2011-01-27 21:04:05 +03:00
|
|
|
/* no persistent connections */
|
2011-02-16 22:26:58 +03:00
|
|
|
if (retrytimo == 0) {
|
|
|
|
rv = ENOTCONN;
|
2011-01-27 21:04:05 +03:00
|
|
|
break;
|
2011-02-16 22:26:58 +03:00
|
|
|
}
|
2011-02-06 18:43:20 +03:00
|
|
|
if (retrytimo == RUMPCLIENT_RETRYCONN_DIE)
|
2011-03-09 18:09:21 +03:00
|
|
|
_exit(1);
|
2011-01-27 21:04:05 +03:00
|
|
|
|
|
|
|
if (!prevreconmsg) {
|
|
|
|
prevreconmsg = time(NULL);
|
|
|
|
gettimeofday(&starttime, NULL);
|
|
|
|
}
|
|
|
|
if (reconretries == 1) {
|
|
|
|
if (retrytimo == RUMPCLIENT_RETRYCONN_ONCE) {
|
|
|
|
rv = ENOTCONN;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
fprintf(stderr, "rump_sp: connection to "
|
|
|
|
"kernel lost, trying to reconnect ...\n");
|
|
|
|
} else if (time(NULL) - prevreconmsg > 120) {
|
|
|
|
fprintf(stderr, "rump_sp: still trying to "
|
|
|
|
"reconnect ...\n");
|
|
|
|
prevreconmsg = time(NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* check that we aren't over the limit */
|
|
|
|
if (retrytimo > 0) {
|
2013-01-18 00:47:44 +04:00
|
|
|
time_t tdiff;
|
2011-01-27 21:04:05 +03:00
|
|
|
|
|
|
|
gettimeofday(&curtime, NULL);
|
2013-01-18 00:47:44 +04:00
|
|
|
tdiff = curtime.tv_sec - starttime.tv_sec;
|
|
|
|
if (starttime.tv_usec > curtime.tv_usec)
|
|
|
|
tdiff--;
|
|
|
|
if (tdiff >= retrytimo) {
|
2011-01-27 21:04:05 +03:00
|
|
|
fprintf(stderr, "rump_sp: reconnect "
|
|
|
|
"failed, %lld second timeout\n",
|
|
|
|
(long long)retrytimo);
|
|
|
|
return ENOTCONN;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* adhoc backoff timer */
|
|
|
|
if (reconretries < 10) {
|
|
|
|
usleep(100000 * reconretries);
|
|
|
|
} else {
|
|
|
|
sleep(MIN(10, reconretries-9));
|
|
|
|
}
|
|
|
|
reconretries++;
|
|
|
|
|
2011-03-15 12:35:05 +03:00
|
|
|
if ((rv = doconnect()) != 0)
|
2011-01-24 20:47:51 +03:00
|
|
|
continue;
|
2011-02-15 13:37:07 +03:00
|
|
|
if ((rv = handshake_req(&clispc, HANDSHAKE_GUEST,
|
|
|
|
NULL, 0, true)) != 0)
|
2011-01-24 20:47:51 +03:00
|
|
|
continue;
|
2011-01-27 21:04:05 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* ok, reconnect succesful. we need to return to
|
|
|
|
* the upper layer to get the entire PDU resent.
|
|
|
|
*/
|
|
|
|
if (reconretries != 1)
|
|
|
|
fprintf(stderr, "rump_sp: reconnected!\n");
|
|
|
|
rv = EAGAIN;
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
_DIAGASSERT(errno != EAGAIN);
|
2011-01-24 20:47:51 +03:00
|
|
|
break;
|
|
|
|
}
|
2011-01-27 21:04:05 +03:00
|
|
|
}
|
2011-01-24 20:47:51 +03:00
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2011-01-06 09:57:14 +03:00
|
|
|
static int
|
2011-01-24 20:47:51 +03:00
|
|
|
cliwaitresp(struct spclient *spc, struct respwait *rw, sigset_t *mask,
|
|
|
|
bool keeplock)
|
2011-01-06 09:57:14 +03:00
|
|
|
{
|
2011-01-24 20:47:51 +03:00
|
|
|
uint64_t mygen;
|
|
|
|
bool imalive = true;
|
2011-01-06 09:57:14 +03:00
|
|
|
|
2011-01-10 22:49:43 +03:00
|
|
|
pthread_mutex_lock(&spc->spc_mtx);
|
2011-01-24 20:47:51 +03:00
|
|
|
if (!keeplock)
|
|
|
|
sendunlockl(spc);
|
|
|
|
mygen = spc->spc_generation;
|
2011-01-06 09:57:14 +03:00
|
|
|
|
|
|
|
rw->rw_error = 0;
|
2011-01-24 20:47:51 +03:00
|
|
|
while (!rw->rw_done && rw->rw_error == 0) {
|
|
|
|
if (__predict_false(spc->spc_generation != mygen || !imalive))
|
|
|
|
break;
|
|
|
|
|
2011-01-06 09:57:14 +03:00
|
|
|
/* are we free to receive? */
|
|
|
|
if (spc->spc_istatus == SPCSTATUS_FREE) {
|
2012-08-03 15:31:33 +04:00
|
|
|
int gotresp, dosig, rv;
|
2011-01-10 22:49:43 +03:00
|
|
|
|
2011-01-06 09:57:14 +03:00
|
|
|
spc->spc_istatus = SPCSTATUS_BUSY;
|
|
|
|
pthread_mutex_unlock(&spc->spc_mtx);
|
|
|
|
|
2011-01-10 22:49:43 +03:00
|
|
|
dosig = 0;
|
|
|
|
for (gotresp = 0; !gotresp; ) {
|
2012-08-03 15:31:33 +04:00
|
|
|
#ifdef USE_KQUEUE
|
|
|
|
struct kevent kev[8];
|
|
|
|
int i;
|
|
|
|
|
2011-03-08 18:55:12 +03:00
|
|
|
/*
|
|
|
|
* typically we don't have a frame waiting
|
|
|
|
* when we come in here, so call kevent now
|
|
|
|
*/
|
2014-04-02 18:48:03 +04:00
|
|
|
rv = host_kevent(holyfd, NULL, 0,
|
2011-03-08 18:55:12 +03:00
|
|
|
kev, __arraycount(kev), NULL);
|
|
|
|
|
|
|
|
if (__predict_false(rv == -1)) {
|
|
|
|
goto activity;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* XXX: don't know how this can happen
|
|
|
|
* (timeout cannot expire since there
|
|
|
|
* isn't one), but it does happen.
|
|
|
|
* treat it as an expectional condition
|
|
|
|
* and go through tryread to determine
|
|
|
|
* alive status.
|
|
|
|
*/
|
|
|
|
if (__predict_false(rv == 0))
|
|
|
|
goto activity;
|
|
|
|
|
|
|
|
for (i = 0; i < rv; i++) {
|
|
|
|
if (kev[i].filter == EVFILT_SIGNAL)
|
|
|
|
dosig++;
|
|
|
|
}
|
|
|
|
if (dosig)
|
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* ok, activity. try to read a frame to
|
|
|
|
* determine what happens next.
|
|
|
|
*/
|
|
|
|
activity:
|
2014-04-02 18:48:03 +04:00
|
|
|
#else /* !USE_KQUEUE */
|
|
|
|
struct pollfd pfd[2];
|
2012-08-03 15:31:33 +04:00
|
|
|
|
2014-04-02 18:48:03 +04:00
|
|
|
pfd[0].fd = clispc.spc_fd;
|
|
|
|
pfd[0].events = POLLIN;
|
|
|
|
pfd[1].fd = holyfd;
|
|
|
|
pfd[1].events = POLLIN;
|
2012-08-03 15:31:33 +04:00
|
|
|
|
2014-04-02 18:48:03 +04:00
|
|
|
rv = host_poll(pfd, 2, -1);
|
2014-04-25 16:20:12 +04:00
|
|
|
if (rv >= 1 && pfd[1].revents & POLLIN) {
|
2014-04-02 18:48:03 +04:00
|
|
|
dosig = 1;
|
|
|
|
goto cleanup;
|
|
|
|
}
|
2012-08-03 15:31:33 +04:00
|
|
|
#endif /* !USE_KQUEUE */
|
|
|
|
|
2011-01-10 22:49:43 +03:00
|
|
|
switch (readframe(spc)) {
|
|
|
|
case 0:
|
|
|
|
continue;
|
|
|
|
case -1:
|
2011-01-24 20:47:51 +03:00
|
|
|
imalive = false;
|
2011-01-10 22:49:43 +03:00
|
|
|
goto cleanup;
|
|
|
|
default:
|
2011-03-08 18:55:12 +03:00
|
|
|
/* case 1 */
|
2011-01-10 22:49:43 +03:00
|
|
|
break;
|
|
|
|
}
|
2011-01-06 09:57:14 +03:00
|
|
|
|
2011-01-10 22:49:43 +03:00
|
|
|
switch (spc->spc_hdr.rsp_class) {
|
2011-01-06 09:57:14 +03:00
|
|
|
case RUMPSP_RESP:
|
|
|
|
case RUMPSP_ERROR:
|
|
|
|
kickwaiter(spc);
|
2011-01-10 22:49:43 +03:00
|
|
|
gotresp = spc->spc_hdr.rsp_reqno ==
|
|
|
|
rw->rw_reqno;
|
2011-01-06 09:57:14 +03:00
|
|
|
break;
|
|
|
|
case RUMPSP_REQ:
|
|
|
|
handlereq(spc);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
/* panic */
|
|
|
|
break;
|
2011-01-10 22:49:43 +03:00
|
|
|
}
|
2011-01-06 09:57:14 +03:00
|
|
|
}
|
|
|
|
|
2011-01-10 22:49:43 +03:00
|
|
|
cleanup:
|
|
|
|
pthread_mutex_lock(&spc->spc_mtx);
|
|
|
|
if (spc->spc_istatus == SPCSTATUS_WANTED)
|
|
|
|
kickall(spc);
|
|
|
|
spc->spc_istatus = SPCSTATUS_FREE;
|
|
|
|
|
|
|
|
/* take one for the team */
|
|
|
|
if (dosig) {
|
|
|
|
pthread_mutex_unlock(&spc->spc_mtx);
|
|
|
|
pthread_sigmask(SIG_SETMASK, mask, NULL);
|
|
|
|
pthread_sigmask(SIG_SETMASK, &fullset, NULL);
|
|
|
|
pthread_mutex_lock(&spc->spc_mtx);
|
|
|
|
}
|
2011-01-06 09:57:14 +03:00
|
|
|
} else {
|
|
|
|
spc->spc_istatus = SPCSTATUS_WANTED;
|
|
|
|
pthread_cond_wait(&rw->rw_cv, &spc->spc_mtx);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
TAILQ_REMOVE(&spc->spc_respwait, rw, rw_entries);
|
|
|
|
pthread_mutex_unlock(&spc->spc_mtx);
|
|
|
|
pthread_cond_destroy(&rw->rw_cv);
|
|
|
|
|
2011-01-24 20:47:51 +03:00
|
|
|
if (spc->spc_generation != mygen || !imalive) {
|
2011-01-06 09:57:14 +03:00
|
|
|
return ENOTCONN;
|
2011-01-24 20:47:51 +03:00
|
|
|
}
|
2011-01-06 09:57:14 +03:00
|
|
|
return rw->rw_error;
|
|
|
|
}
|
|
|
|
|
2010-11-05 00:01:28 +03:00
|
|
|
static int
|
2011-02-07 18:25:41 +03:00
|
|
|
syscall_req(struct spclient *spc, sigset_t *omask, int sysnum,
|
2010-11-19 18:25:49 +03:00
|
|
|
const void *data, size_t dlen, void **resp)
|
2010-11-05 00:01:28 +03:00
|
|
|
{
|
|
|
|
struct rsp_hdr rhdr;
|
2010-11-19 18:25:49 +03:00
|
|
|
struct respwait rw;
|
2011-03-08 18:34:37 +03:00
|
|
|
struct iovec iov[2];
|
2010-11-19 18:25:49 +03:00
|
|
|
int rv;
|
2010-11-05 00:01:28 +03:00
|
|
|
|
|
|
|
rhdr.rsp_len = sizeof(rhdr) + dlen;
|
2010-11-19 18:25:49 +03:00
|
|
|
rhdr.rsp_class = RUMPSP_REQ;
|
|
|
|
rhdr.rsp_type = RUMPSP_SYSCALL;
|
2010-11-05 00:01:28 +03:00
|
|
|
rhdr.rsp_sysnum = sysnum;
|
|
|
|
|
2011-03-08 18:34:37 +03:00
|
|
|
IOVPUT(iov[0], rhdr);
|
|
|
|
IOVPUT_WITHSIZE(iov[1], __UNCONST(data), dlen);
|
|
|
|
|
2010-11-29 19:08:03 +03:00
|
|
|
do {
|
|
|
|
putwait(spc, &rw, &rhdr);
|
2011-03-08 18:34:37 +03:00
|
|
|
if ((rv = send_with_recon(spc, iov, __arraycount(iov))) != 0) {
|
2011-01-24 20:47:51 +03:00
|
|
|
unputwait(spc, &rw);
|
|
|
|
continue;
|
2010-11-29 19:08:03 +03:00
|
|
|
}
|
|
|
|
|
2011-02-07 18:25:41 +03:00
|
|
|
rv = cliwaitresp(spc, &rw, omask, false);
|
2011-01-27 21:04:05 +03:00
|
|
|
if (rv == ENOTCONN)
|
|
|
|
rv = EAGAIN;
|
|
|
|
} while (rv == EAGAIN);
|
2010-11-19 18:25:49 +03:00
|
|
|
|
|
|
|
*resp = rw.rw_data;
|
|
|
|
return rv;
|
2010-11-05 00:01:28 +03:00
|
|
|
}
|
|
|
|
|
2010-12-16 20:05:44 +03:00
|
|
|
static int
|
2011-02-15 13:37:07 +03:00
|
|
|
handshake_req(struct spclient *spc, int type, void *data,
|
|
|
|
int cancel, bool haslock)
|
2010-12-16 20:05:44 +03:00
|
|
|
{
|
2011-01-05 20:14:50 +03:00
|
|
|
struct handshake_fork rf;
|
2011-03-09 18:09:21 +03:00
|
|
|
const char *myprogname = NULL; /* XXXgcc */
|
2010-12-16 20:05:44 +03:00
|
|
|
struct rsp_hdr rhdr;
|
|
|
|
struct respwait rw;
|
2011-01-06 09:57:14 +03:00
|
|
|
sigset_t omask;
|
2011-01-28 22:21:28 +03:00
|
|
|
size_t bonus;
|
2011-03-08 18:34:37 +03:00
|
|
|
struct iovec iov[2];
|
2010-12-16 20:05:44 +03:00
|
|
|
int rv;
|
|
|
|
|
2011-02-15 13:37:07 +03:00
|
|
|
if (type == HANDSHAKE_FORK) {
|
2011-01-28 22:21:28 +03:00
|
|
|
bonus = sizeof(rf);
|
|
|
|
} else {
|
2012-08-03 15:31:33 +04:00
|
|
|
#ifdef __NetBSD__
|
|
|
|
/* would procfs work on NetBSD too? */
|
2011-03-08 18:34:37 +03:00
|
|
|
myprogname = getprogname();
|
2012-08-03 15:31:33 +04:00
|
|
|
#else
|
|
|
|
int fd = open("/proc/self/comm", O_RDONLY);
|
|
|
|
if (fd == -1) {
|
|
|
|
myprogname = "???";
|
|
|
|
} else {
|
|
|
|
static char commname[128];
|
|
|
|
|
2012-08-03 18:52:31 +04:00
|
|
|
memset(commname, 0, sizeof(commname));
|
2012-08-03 15:31:33 +04:00
|
|
|
if (read(fd, commname, sizeof(commname)) > 0) {
|
|
|
|
char *n;
|
|
|
|
|
|
|
|
n = strrchr(commname, '\n');
|
|
|
|
if (n)
|
|
|
|
*n = '\0';
|
|
|
|
myprogname = commname;
|
|
|
|
} else {
|
|
|
|
myprogname = "???";
|
|
|
|
}
|
|
|
|
close(fd);
|
|
|
|
}
|
|
|
|
#endif
|
2011-03-08 18:34:37 +03:00
|
|
|
bonus = strlen(myprogname)+1;
|
2011-01-28 22:21:28 +03:00
|
|
|
}
|
|
|
|
|
2010-12-16 20:05:44 +03:00
|
|
|
/* performs server handshake */
|
2011-01-28 22:21:28 +03:00
|
|
|
rhdr.rsp_len = sizeof(rhdr) + bonus;
|
2010-12-16 20:05:44 +03:00
|
|
|
rhdr.rsp_class = RUMPSP_REQ;
|
|
|
|
rhdr.rsp_type = RUMPSP_HANDSHAKE;
|
2011-02-15 13:37:07 +03:00
|
|
|
rhdr.rsp_handshake = type;
|
2010-12-16 20:05:44 +03:00
|
|
|
|
2011-03-08 18:34:37 +03:00
|
|
|
IOVPUT(iov[0], rhdr);
|
|
|
|
|
2011-01-06 09:57:14 +03:00
|
|
|
pthread_sigmask(SIG_SETMASK, &fullset, &omask);
|
2011-01-24 20:47:51 +03:00
|
|
|
if (haslock)
|
|
|
|
putwait_locked(spc, &rw, &rhdr);
|
|
|
|
else
|
|
|
|
putwait(spc, &rw, &rhdr);
|
2011-02-15 13:37:07 +03:00
|
|
|
if (type == HANDSHAKE_FORK) {
|
|
|
|
memcpy(rf.rf_auth, data, sizeof(rf.rf_auth)); /* uh, why? */
|
2011-01-05 20:14:50 +03:00
|
|
|
rf.rf_cancel = cancel;
|
2011-03-08 18:34:37 +03:00
|
|
|
IOVPUT(iov[1], rf);
|
2011-01-28 22:21:28 +03:00
|
|
|
} else {
|
2011-03-09 18:09:21 +03:00
|
|
|
IOVPUT_WITHSIZE(iov[1], __UNCONST(myprogname), bonus);
|
2011-01-05 20:14:50 +03:00
|
|
|
}
|
2011-03-08 18:34:37 +03:00
|
|
|
rv = send_with_recon(spc, iov, __arraycount(iov));
|
2011-01-24 20:47:51 +03:00
|
|
|
if (rv || cancel) {
|
|
|
|
if (haslock)
|
|
|
|
unputwait_locked(spc, &rw);
|
|
|
|
else
|
|
|
|
unputwait(spc, &rw);
|
|
|
|
if (cancel) {
|
2011-02-07 18:25:41 +03:00
|
|
|
goto out;
|
2011-01-24 20:47:51 +03:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
rv = cliwaitresp(spc, &rw, &omask, haslock);
|
2010-12-16 20:05:44 +03:00
|
|
|
}
|
|
|
|
if (rv)
|
2011-02-07 18:25:41 +03:00
|
|
|
goto out;
|
2010-12-16 20:05:44 +03:00
|
|
|
|
|
|
|
rv = *(int *)rw.rw_data;
|
|
|
|
free(rw.rw_data);
|
|
|
|
|
2011-02-07 18:25:41 +03:00
|
|
|
out:
|
|
|
|
pthread_sigmask(SIG_SETMASK, &omask, NULL);
|
2010-12-16 20:05:44 +03:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2011-01-05 20:14:50 +03:00
|
|
|
static int
|
2011-02-07 18:25:41 +03:00
|
|
|
prefork_req(struct spclient *spc, sigset_t *omask, void **resp)
|
2011-01-05 20:14:50 +03:00
|
|
|
{
|
|
|
|
struct rsp_hdr rhdr;
|
|
|
|
struct respwait rw;
|
2011-03-08 18:34:37 +03:00
|
|
|
struct iovec iov[1];
|
2011-01-05 20:14:50 +03:00
|
|
|
int rv;
|
|
|
|
|
|
|
|
rhdr.rsp_len = sizeof(rhdr);
|
|
|
|
rhdr.rsp_class = RUMPSP_REQ;
|
|
|
|
rhdr.rsp_type = RUMPSP_PREFORK;
|
|
|
|
rhdr.rsp_error = 0;
|
|
|
|
|
2011-03-08 18:34:37 +03:00
|
|
|
IOVPUT(iov[0], rhdr);
|
|
|
|
|
2011-01-24 20:47:51 +03:00
|
|
|
do {
|
|
|
|
putwait(spc, &rw, &rhdr);
|
2011-03-08 18:34:37 +03:00
|
|
|
rv = send_with_recon(spc, iov, __arraycount(iov));
|
2011-01-24 20:47:51 +03:00
|
|
|
if (rv != 0) {
|
|
|
|
unputwait(spc, &rw);
|
|
|
|
continue;
|
|
|
|
}
|
2011-01-05 20:14:50 +03:00
|
|
|
|
2011-02-07 18:25:41 +03:00
|
|
|
rv = cliwaitresp(spc, &rw, omask, false);
|
2011-01-27 21:04:05 +03:00
|
|
|
if (rv == ENOTCONN)
|
|
|
|
rv = EAGAIN;
|
|
|
|
} while (rv == EAGAIN);
|
2011-01-24 20:47:51 +03:00
|
|
|
|
2011-01-05 20:14:50 +03:00
|
|
|
*resp = rw.rw_data;
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2011-01-24 20:47:51 +03:00
|
|
|
/*
|
|
|
|
* prevent response code from deadlocking with reconnect code
|
|
|
|
*/
|
2010-11-05 00:01:28 +03:00
|
|
|
static int
|
2011-01-24 20:47:51 +03:00
|
|
|
resp_sendlock(struct spclient *spc)
|
|
|
|
{
|
|
|
|
int rv = 0;
|
|
|
|
|
|
|
|
pthread_mutex_lock(&spc->spc_mtx);
|
|
|
|
while (spc->spc_ostatus != SPCSTATUS_FREE) {
|
|
|
|
if (__predict_false(spc->spc_reconnecting)) {
|
|
|
|
rv = EBUSY;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
spc->spc_ostatus = SPCSTATUS_WANTED;
|
|
|
|
pthread_cond_wait(&spc->spc_cv, &spc->spc_mtx);
|
|
|
|
}
|
|
|
|
spc->spc_ostatus = SPCSTATUS_BUSY;
|
|
|
|
|
|
|
|
out:
|
|
|
|
pthread_mutex_unlock(&spc->spc_mtx);
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2010-11-25 20:59:02 +03:00
|
|
|
send_copyin_resp(struct spclient *spc, uint64_t reqno, void *data, size_t dlen,
|
|
|
|
int wantstr)
|
2010-11-05 00:01:28 +03:00
|
|
|
{
|
|
|
|
struct rsp_hdr rhdr;
|
2011-03-08 18:34:37 +03:00
|
|
|
struct iovec iov[2];
|
2010-11-05 00:01:28 +03:00
|
|
|
|
2010-11-25 20:59:02 +03:00
|
|
|
if (wantstr)
|
|
|
|
dlen = MIN(dlen, strlen(data)+1);
|
|
|
|
|
2010-11-05 00:01:28 +03:00
|
|
|
rhdr.rsp_len = sizeof(rhdr) + dlen;
|
|
|
|
rhdr.rsp_reqno = reqno;
|
2010-11-19 18:25:49 +03:00
|
|
|
rhdr.rsp_class = RUMPSP_RESP;
|
|
|
|
rhdr.rsp_type = RUMPSP_COPYIN;
|
2010-11-05 00:01:28 +03:00
|
|
|
rhdr.rsp_sysnum = 0;
|
|
|
|
|
2011-03-08 18:34:37 +03:00
|
|
|
IOVPUT(iov[0], rhdr);
|
|
|
|
IOVPUT_WITHSIZE(iov[1], data, dlen);
|
|
|
|
|
2011-01-24 20:47:51 +03:00
|
|
|
if (resp_sendlock(spc) != 0)
|
|
|
|
return;
|
2011-03-08 18:34:37 +03:00
|
|
|
(void)SENDIOV(spc, iov);
|
2010-11-19 18:25:49 +03:00
|
|
|
sendunlock(spc);
|
2010-11-05 00:01:28 +03:00
|
|
|
}
|
|
|
|
|
2011-01-24 20:47:51 +03:00
|
|
|
static void
|
2010-11-05 00:01:28 +03:00
|
|
|
send_anonmmap_resp(struct spclient *spc, uint64_t reqno, void *addr)
|
|
|
|
{
|
|
|
|
struct rsp_hdr rhdr;
|
2011-03-08 18:34:37 +03:00
|
|
|
struct iovec iov[2];
|
2010-11-05 00:01:28 +03:00
|
|
|
|
|
|
|
rhdr.rsp_len = sizeof(rhdr) + sizeof(addr);
|
|
|
|
rhdr.rsp_reqno = reqno;
|
2010-11-19 18:25:49 +03:00
|
|
|
rhdr.rsp_class = RUMPSP_RESP;
|
|
|
|
rhdr.rsp_type = RUMPSP_ANONMMAP;
|
2010-11-05 00:01:28 +03:00
|
|
|
rhdr.rsp_sysnum = 0;
|
|
|
|
|
2011-03-08 18:34:37 +03:00
|
|
|
IOVPUT(iov[0], rhdr);
|
|
|
|
IOVPUT(iov[1], addr);
|
|
|
|
|
2011-01-24 20:47:51 +03:00
|
|
|
if (resp_sendlock(spc) != 0)
|
|
|
|
return;
|
2011-03-08 18:34:37 +03:00
|
|
|
(void)SENDIOV(spc, iov);
|
2010-11-19 18:25:49 +03:00
|
|
|
sendunlock(spc);
|
2010-11-05 00:01:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
rumpclient_syscall(int sysnum, const void *data, size_t dlen,
|
|
|
|
register_t *retval)
|
|
|
|
{
|
|
|
|
struct rsp_sysresp *resp;
|
2011-02-07 18:25:41 +03:00
|
|
|
sigset_t omask;
|
2010-11-19 18:25:49 +03:00
|
|
|
void *rdata;
|
|
|
|
int rv;
|
|
|
|
|
2011-02-07 18:25:41 +03:00
|
|
|
pthread_sigmask(SIG_SETMASK, &fullset, &omask);
|
|
|
|
|
2010-11-19 18:25:49 +03:00
|
|
|
DPRINTF(("rumpsp syscall_req: syscall %d with %p/%zu\n",
|
|
|
|
sysnum, data, dlen));
|
|
|
|
|
2011-02-07 18:25:41 +03:00
|
|
|
rv = syscall_req(&clispc, &omask, sysnum, data, dlen, &rdata);
|
2010-11-19 18:25:49 +03:00
|
|
|
if (rv)
|
2011-02-07 18:25:41 +03:00
|
|
|
goto out;
|
2010-11-19 18:25:49 +03:00
|
|
|
|
|
|
|
resp = rdata;
|
2016-09-06 10:45:41 +03:00
|
|
|
DPRINTF(("rumpsp syscall_resp: syscall %d error %d, rv: %"
|
|
|
|
PRIxREGISTER"/%"PRIxREGISTER"\n",
|
|
|
|
sysnum, rv, resp->rsys_retval[0], resp->rsys_retval[1]));
|
2010-11-19 18:25:49 +03:00
|
|
|
|
|
|
|
memcpy(retval, &resp->rsys_retval, sizeof(resp->rsys_retval));
|
|
|
|
rv = resp->rsys_error;
|
|
|
|
free(rdata);
|
|
|
|
|
2011-02-07 18:25:41 +03:00
|
|
|
out:
|
|
|
|
pthread_sigmask(SIG_SETMASK, &omask, NULL);
|
2010-11-19 18:25:49 +03:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
handlereq(struct spclient *spc)
|
|
|
|
{
|
2010-11-05 00:01:28 +03:00
|
|
|
struct rsp_copydata *copydata;
|
2011-01-14 16:12:14 +03:00
|
|
|
struct rsp_hdr *rhdr = &spc->spc_hdr;
|
2010-11-05 00:01:28 +03:00
|
|
|
void *mapaddr;
|
2010-11-19 18:25:49 +03:00
|
|
|
size_t maplen;
|
2010-11-25 20:59:02 +03:00
|
|
|
int reqtype = spc->spc_hdr.rsp_type;
|
2014-02-20 04:42:27 +04:00
|
|
|
int sig;
|
2010-11-05 00:01:28 +03:00
|
|
|
|
2010-11-25 20:59:02 +03:00
|
|
|
switch (reqtype) {
|
2010-11-19 18:25:49 +03:00
|
|
|
case RUMPSP_COPYIN:
|
2010-11-25 20:59:02 +03:00
|
|
|
case RUMPSP_COPYINSTR:
|
2010-11-19 18:25:49 +03:00
|
|
|
/*LINTED*/
|
|
|
|
copydata = (struct rsp_copydata *)spc->spc_buf;
|
|
|
|
DPRINTF(("rump_sp handlereq: copyin request: %p/%zu\n",
|
|
|
|
copydata->rcp_addr, copydata->rcp_len));
|
|
|
|
send_copyin_resp(spc, spc->spc_hdr.rsp_reqno,
|
2010-11-25 20:59:02 +03:00
|
|
|
copydata->rcp_addr, copydata->rcp_len,
|
|
|
|
reqtype == RUMPSP_COPYINSTR);
|
2010-11-19 18:25:49 +03:00
|
|
|
break;
|
|
|
|
case RUMPSP_COPYOUT:
|
2010-11-25 20:59:02 +03:00
|
|
|
case RUMPSP_COPYOUTSTR:
|
2010-11-19 18:25:49 +03:00
|
|
|
/*LINTED*/
|
|
|
|
copydata = (struct rsp_copydata *)spc->spc_buf;
|
|
|
|
DPRINTF(("rump_sp handlereq: copyout request: %p/%zu\n",
|
|
|
|
copydata->rcp_addr, copydata->rcp_len));
|
|
|
|
/*LINTED*/
|
|
|
|
memcpy(copydata->rcp_addr, copydata->rcp_data,
|
|
|
|
copydata->rcp_len);
|
|
|
|
break;
|
|
|
|
case RUMPSP_ANONMMAP:
|
|
|
|
/*LINTED*/
|
|
|
|
maplen = *(size_t *)spc->spc_buf;
|
|
|
|
mapaddr = mmap(NULL, maplen, PROT_READ|PROT_WRITE,
|
2014-02-26 06:03:40 +04:00
|
|
|
MAP_ANON|MAP_PRIVATE, -1, 0);
|
2010-11-19 18:25:49 +03:00
|
|
|
if (mapaddr == MAP_FAILED)
|
|
|
|
mapaddr = NULL;
|
|
|
|
DPRINTF(("rump_sp handlereq: anonmmap: %p\n", mapaddr));
|
|
|
|
send_anonmmap_resp(spc, spc->spc_hdr.rsp_reqno, mapaddr);
|
|
|
|
break;
|
2011-01-14 16:12:14 +03:00
|
|
|
case RUMPSP_RAISE:
|
2014-02-20 04:42:27 +04:00
|
|
|
sig = rumpuser__sig_rump2host(rhdr->rsp_signo);
|
|
|
|
DPRINTF(("rump_sp handlereq: raise sig %d\n", sig));
|
|
|
|
raise(sig);
|
2011-01-14 16:12:14 +03:00
|
|
|
/*
|
|
|
|
* We most likely have signals blocked, but the signal
|
|
|
|
* will be handled soon enough when we return.
|
|
|
|
*/
|
|
|
|
break;
|
2010-11-19 18:25:49 +03:00
|
|
|
default:
|
2011-01-06 09:57:14 +03:00
|
|
|
printf("PANIC: INVALID TYPE %d\n", reqtype);
|
2010-11-19 18:25:49 +03:00
|
|
|
abort();
|
|
|
|
break;
|
|
|
|
}
|
2010-11-05 00:01:28 +03:00
|
|
|
|
2010-11-29 19:08:03 +03:00
|
|
|
spcfreebuf(spc);
|
2010-11-05 00:01:28 +03:00
|
|
|
}
|
|
|
|
|
2011-01-05 20:14:50 +03:00
|
|
|
static unsigned ptab_idx;
|
|
|
|
static struct sockaddr *serv_sa;
|
|
|
|
|
2011-02-09 17:29:58 +03:00
|
|
|
/* dup until we get a "good" fd which does not collide with stdio */
|
|
|
|
static int
|
2011-02-14 17:56:23 +03:00
|
|
|
dupgood(int myfd, int mustchange)
|
2011-02-09 17:29:58 +03:00
|
|
|
{
|
2011-02-14 17:56:23 +03:00
|
|
|
int ofds[4];
|
2011-04-30 16:25:05 +04:00
|
|
|
int sverrno;
|
2012-03-21 14:10:36 +04:00
|
|
|
unsigned int i;
|
2011-02-09 17:29:58 +03:00
|
|
|
|
2011-02-14 17:56:23 +03:00
|
|
|
for (i = 0; (myfd <= 2 || mustchange) && myfd != -1; i++) {
|
2011-02-09 17:29:58 +03:00
|
|
|
assert(i < __arraycount(ofds));
|
|
|
|
ofds[i] = myfd;
|
|
|
|
myfd = host_dup(myfd);
|
2011-02-14 17:56:23 +03:00
|
|
|
if (mustchange) {
|
|
|
|
i--; /* prevent closing old fd */
|
|
|
|
mustchange = 0;
|
|
|
|
}
|
2011-02-09 17:29:58 +03:00
|
|
|
}
|
|
|
|
|
2011-04-30 16:25:05 +04:00
|
|
|
sverrno = 0;
|
|
|
|
if (myfd == -1 && i > 0)
|
|
|
|
sverrno = errno;
|
|
|
|
|
2012-03-21 14:10:36 +04:00
|
|
|
while (i-- > 0) {
|
2011-02-09 17:29:58 +03:00
|
|
|
host_close(ofds[i]);
|
|
|
|
}
|
|
|
|
|
2011-04-30 16:25:05 +04:00
|
|
|
if (sverrno)
|
|
|
|
errno = sverrno;
|
|
|
|
|
2011-02-09 17:29:58 +03:00
|
|
|
return myfd;
|
|
|
|
}
|
|
|
|
|
2014-04-02 18:48:03 +04:00
|
|
|
#if defined(USE_KQUEUE)
|
|
|
|
|
|
|
|
static int
|
|
|
|
makeholyfd(void)
|
|
|
|
{
|
|
|
|
struct kevent kev[NSIG+1];
|
|
|
|
int i, fd;
|
|
|
|
|
|
|
|
/* setup kqueue, we want all signals and the fd */
|
|
|
|
if ((fd = dupgood(host_kqueue(), 0)) == -1) {
|
|
|
|
ERRLOG(("rump_sp: cannot setup kqueue"));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < NSIG; i++) {
|
|
|
|
EV_SET(&kev[i], i+1, EVFILT_SIGNAL, EV_ADD|EV_ENABLE, 0, 0, 0);
|
|
|
|
}
|
|
|
|
EV_SET(&kev[NSIG], clispc.spc_fd,
|
|
|
|
EVFILT_READ, EV_ADD|EV_ENABLE, 0, 0, 0);
|
|
|
|
if (host_kevent(fd, kev, NSIG+1, NULL, 0, NULL) == -1) {
|
|
|
|
ERRLOG(("rump_sp: kevent() failed"));
|
2014-04-02 19:04:19 +04:00
|
|
|
host_close(fd);
|
2014-04-02 18:48:03 +04:00
|
|
|
return -1;
|
2014-04-02 19:04:19 +04:00
|
|
|
}
|
2014-04-02 18:48:03 +04:00
|
|
|
|
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
|
|
|
|
#elif defined(USE_SIGNALFD) /* !USE_KQUEUE */
|
|
|
|
|
|
|
|
static int
|
|
|
|
makeholyfd(void)
|
|
|
|
{
|
|
|
|
|
|
|
|
return host_signalfd(-1, &fullset, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
#else /* !USE_KQUEUE && !USE_SIGNALFD */
|
|
|
|
|
|
|
|
static int
|
|
|
|
makeholyfd(void)
|
|
|
|
{
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2011-01-05 20:14:50 +03:00
|
|
|
static int
|
2011-03-15 12:35:05 +03:00
|
|
|
doconnect(void)
|
2010-11-05 00:01:28 +03:00
|
|
|
{
|
2011-01-24 20:47:51 +03:00
|
|
|
struct respwait rw;
|
|
|
|
struct rsp_hdr rhdr;
|
2010-12-16 15:38:20 +03:00
|
|
|
char banner[MAXBANNER];
|
2012-08-03 15:31:33 +04:00
|
|
|
int s, error, flags;
|
2010-12-16 15:38:20 +03:00
|
|
|
ssize_t n;
|
2010-11-05 00:01:28 +03:00
|
|
|
|
2014-04-02 18:48:03 +04:00
|
|
|
if (holyfd != -1)
|
|
|
|
host_close(holyfd);
|
|
|
|
holyfd = -1;
|
2011-01-26 17:42:41 +03:00
|
|
|
s = -1;
|
2011-01-24 20:47:51 +03:00
|
|
|
|
|
|
|
if (clispc.spc_fd != -1)
|
|
|
|
host_close(clispc.spc_fd);
|
|
|
|
clispc.spc_fd = -1;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* for reconnect, gate everyone out of the receiver code
|
|
|
|
*/
|
|
|
|
putwait_locked(&clispc, &rw, &rhdr);
|
|
|
|
|
|
|
|
pthread_mutex_lock(&clispc.spc_mtx);
|
|
|
|
clispc.spc_reconnecting = 1;
|
|
|
|
pthread_cond_broadcast(&clispc.spc_cv);
|
|
|
|
clispc.spc_generation++;
|
|
|
|
while (clispc.spc_istatus != SPCSTATUS_FREE) {
|
|
|
|
clispc.spc_istatus = SPCSTATUS_WANTED;
|
|
|
|
pthread_cond_wait(&rw.rw_cv, &clispc.spc_mtx);
|
|
|
|
}
|
|
|
|
kickall(&clispc);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* we can release it already since we hold the
|
|
|
|
* send lock during reconnect
|
|
|
|
* XXX: assert it
|
|
|
|
*/
|
|
|
|
clispc.spc_istatus = SPCSTATUS_FREE;
|
|
|
|
pthread_mutex_unlock(&clispc.spc_mtx);
|
|
|
|
unputwait_locked(&clispc, &rw);
|
|
|
|
|
|
|
|
free(clispc.spc_buf);
|
|
|
|
clispc.spc_off = 0;
|
|
|
|
|
2011-02-14 17:56:23 +03:00
|
|
|
s = dupgood(host_socket(parsetab[ptab_idx].domain, SOCK_STREAM, 0), 0);
|
2011-01-05 20:14:50 +03:00
|
|
|
if (s == -1)
|
2010-11-05 16:50:48 +03:00
|
|
|
return -1;
|
2010-11-05 00:01:28 +03:00
|
|
|
|
2012-08-03 15:31:33 +04:00
|
|
|
while (host_connect(s, serv_sa, parsetab[ptab_idx].slen) == -1) {
|
2011-01-24 20:47:51 +03:00
|
|
|
if (errno == EINTR)
|
|
|
|
continue;
|
2011-03-15 12:35:05 +03:00
|
|
|
ERRLOG(("rump_sp: client connect failed: %s\n",
|
|
|
|
strerror(errno)));
|
2011-01-27 21:04:05 +03:00
|
|
|
return -1;
|
2010-11-05 16:50:48 +03:00
|
|
|
}
|
2010-11-05 00:01:28 +03:00
|
|
|
|
2011-01-05 20:14:50 +03:00
|
|
|
if ((error = parsetab[ptab_idx].connhook(s)) != 0) {
|
2011-03-15 12:35:05 +03:00
|
|
|
ERRLOG(("rump_sp: connect hook failed\n"));
|
2010-11-05 16:50:48 +03:00
|
|
|
return -1;
|
2010-11-05 00:01:28 +03:00
|
|
|
}
|
2010-11-24 20:03:39 +03:00
|
|
|
|
2011-04-30 16:25:05 +04:00
|
|
|
if ((n = host_read(s, banner, sizeof(banner)-1)) <= 0) {
|
2011-03-15 12:35:05 +03:00
|
|
|
ERRLOG(("rump_sp: failed to read banner\n"));
|
2010-12-16 15:38:20 +03:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (banner[n-1] != '\n') {
|
2011-03-15 12:35:05 +03:00
|
|
|
ERRLOG(("rump_sp: invalid banner\n"));
|
2010-12-16 15:38:20 +03:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
banner[n] = '\0';
|
2011-04-30 16:25:05 +04:00
|
|
|
/* XXX parse the banner some day */
|
2010-12-16 15:38:20 +03:00
|
|
|
|
2011-01-10 22:49:43 +03:00
|
|
|
flags = host_fcntl(s, F_GETFL, 0);
|
|
|
|
if (host_fcntl(s, F_SETFL, flags | O_NONBLOCK) == -1) {
|
2011-03-15 12:35:05 +03:00
|
|
|
ERRLOG(("rump_sp: socket fd NONBLOCK: %s\n", strerror(errno)));
|
2011-01-10 22:49:43 +03:00
|
|
|
return -1;
|
|
|
|
}
|
2011-01-24 20:47:51 +03:00
|
|
|
clispc.spc_fd = s;
|
|
|
|
clispc.spc_state = SPCSTATE_RUNNING;
|
|
|
|
clispc.spc_reconnecting = 0;
|
2014-04-02 18:48:03 +04:00
|
|
|
holyfd = makeholyfd();
|
2011-01-10 22:49:43 +03:00
|
|
|
|
2011-01-24 20:47:51 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
doinit(void)
|
|
|
|
{
|
|
|
|
|
2011-01-05 20:14:50 +03:00
|
|
|
TAILQ_INIT(&clispc.spc_respwait);
|
|
|
|
pthread_mutex_init(&clispc.spc_mtx, NULL);
|
|
|
|
pthread_cond_init(&clispc.spc_cv, NULL);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-01-17 20:29:44 +04:00
|
|
|
#ifdef RTLD_NEXT
|
2011-02-24 15:25:44 +03:00
|
|
|
void *rumpclient__dlsym(void *, const char *);
|
|
|
|
void *
|
|
|
|
rumpclient__dlsym(void *handle, const char *symbol)
|
|
|
|
{
|
|
|
|
|
|
|
|
return dlsym(handle, symbol);
|
|
|
|
}
|
2012-08-03 15:31:33 +04:00
|
|
|
void *rumphijack_dlsym(void *, const char *)
|
|
|
|
__attribute__((__weak__, alias("rumpclient__dlsym")));
|
2013-01-17 20:29:44 +04:00
|
|
|
#endif
|
2011-02-24 15:25:44 +03:00
|
|
|
|
2011-02-27 15:58:29 +03:00
|
|
|
static pid_t init_done = 0;
|
2011-01-07 22:37:51 +03:00
|
|
|
|
2011-01-05 20:14:50 +03:00
|
|
|
int
|
2011-12-12 19:21:32 +04:00
|
|
|
rumpclient_init(void)
|
2011-01-05 20:14:50 +03:00
|
|
|
{
|
|
|
|
char *p;
|
|
|
|
int error;
|
2011-02-15 13:37:07 +03:00
|
|
|
int rv = -1;
|
|
|
|
int hstype;
|
2011-02-27 15:58:29 +03:00
|
|
|
pid_t mypid;
|
2011-02-15 13:37:07 +03:00
|
|
|
|
2011-02-27 15:58:29 +03:00
|
|
|
/*
|
|
|
|
* Make sure we're not riding the context of a previous
|
|
|
|
* host fork. Note: it's *possible* that after n>1 forks
|
|
|
|
* we have the same pid as one of our exited parents, but
|
|
|
|
* I'm pretty sure there are 0 practical implications, since
|
|
|
|
* it means generations would have to skip rumpclient init.
|
|
|
|
*/
|
|
|
|
if (init_done == (mypid = getpid()))
|
2011-02-15 13:37:07 +03:00
|
|
|
return 0;
|
2011-02-27 15:58:29 +03:00
|
|
|
|
2014-04-02 18:48:03 +04:00
|
|
|
#ifdef USE_KQUEUE
|
2014-04-03 21:11:35 +04:00
|
|
|
/* kq does not traverse fork() */
|
|
|
|
holyfd = -1;
|
2014-04-02 18:48:03 +04:00
|
|
|
#endif
|
2011-02-27 15:58:29 +03:00
|
|
|
init_done = mypid;
|
2011-01-05 20:14:50 +03:00
|
|
|
|
2011-02-07 17:49:53 +03:00
|
|
|
sigfillset(&fullset);
|
|
|
|
|
2011-01-07 22:37:51 +03:00
|
|
|
/*
|
2012-08-03 15:31:33 +04:00
|
|
|
* sag mir, wo die symbols sind. zogen fort, der krieg beginnt.
|
2011-01-07 22:37:51 +03:00
|
|
|
* wann wird man je verstehen? wann wird man je verstehen?
|
|
|
|
*/
|
2013-01-17 20:29:44 +04:00
|
|
|
#ifdef RTLD_NEXT
|
2011-01-07 22:37:51 +03:00
|
|
|
#define FINDSYM2(_name_,_syscall_) \
|
2011-02-24 15:25:44 +03:00
|
|
|
if ((host_##_name_ = rumphijack_dlsym(RTLD_NEXT, \
|
2011-02-24 12:52:34 +03:00
|
|
|
#_syscall_)) == NULL) { \
|
2011-02-24 17:22:04 +03:00
|
|
|
if (rumphijack_dlsym == rumpclient__dlsym) \
|
2011-02-24 12:52:34 +03:00
|
|
|
host_##_name_ = _name_; /* static fallback */ \
|
2013-01-18 00:47:44 +04:00
|
|
|
if (host_##_name_ == NULL) { \
|
|
|
|
fprintf(stderr,"cannot find %s: %s", #_syscall_,\
|
2011-02-24 12:52:34 +03:00
|
|
|
dlerror()); \
|
2013-01-18 00:47:44 +04:00
|
|
|
exit(1); \
|
|
|
|
} \
|
2011-02-24 12:52:34 +03:00
|
|
|
}
|
2013-01-17 20:29:44 +04:00
|
|
|
#else
|
|
|
|
#define FINDSYM2(_name_,_syscall) \
|
|
|
|
host_##_name_ = _name_;
|
|
|
|
#endif
|
2011-01-07 22:37:51 +03:00
|
|
|
#define FINDSYM(_name_) FINDSYM2(_name_,_name_)
|
2012-08-03 15:31:33 +04:00
|
|
|
#ifdef __NetBSD__
|
2011-02-24 12:52:34 +03:00
|
|
|
FINDSYM2(socket,__socket30)
|
2012-08-03 15:31:33 +04:00
|
|
|
#else
|
|
|
|
FINDSYM(socket)
|
|
|
|
#endif
|
|
|
|
|
2011-02-24 12:52:34 +03:00
|
|
|
FINDSYM(close)
|
|
|
|
FINDSYM(connect)
|
|
|
|
FINDSYM(fcntl)
|
|
|
|
FINDSYM(poll)
|
|
|
|
FINDSYM(read)
|
2011-03-08 18:34:37 +03:00
|
|
|
FINDSYM(sendmsg)
|
2011-02-24 12:52:34 +03:00
|
|
|
FINDSYM(setsockopt)
|
|
|
|
FINDSYM(dup)
|
|
|
|
FINDSYM(execve)
|
2012-08-03 15:31:33 +04:00
|
|
|
|
|
|
|
#ifdef USE_KQUEUE
|
|
|
|
FINDSYM(kqueue)
|
2014-12-08 04:10:07 +03:00
|
|
|
#ifdef __NetBSD__
|
2011-02-05 15:38:19 +03:00
|
|
|
#if !__NetBSD_Prereq__(5,99,7)
|
2011-02-24 12:52:34 +03:00
|
|
|
FINDSYM(kevent)
|
2011-02-05 15:38:19 +03:00
|
|
|
#else
|
2011-02-24 12:52:34 +03:00
|
|
|
FINDSYM2(kevent,_sys___kevent50)
|
2011-02-05 15:38:19 +03:00
|
|
|
#endif
|
2014-12-08 04:10:07 +03:00
|
|
|
#else
|
|
|
|
FINDSYM(kevent)
|
|
|
|
#endif
|
2012-08-03 15:31:33 +04:00
|
|
|
#endif /* USE_KQUEUE */
|
|
|
|
|
2014-04-02 18:48:03 +04:00
|
|
|
#ifdef USE_SIGNALFD
|
|
|
|
FINDSYM(signalfd)
|
|
|
|
#endif
|
|
|
|
|
2011-01-07 22:37:51 +03:00
|
|
|
#undef FINDSYM
|
|
|
|
#undef FINDSY2
|
|
|
|
|
2011-02-14 17:56:23 +03:00
|
|
|
if ((p = getenv("RUMP__PARSEDSERVER")) == NULL) {
|
|
|
|
if ((p = getenv("RUMP_SERVER")) == NULL) {
|
2012-09-12 16:38:16 +04:00
|
|
|
fprintf(stderr, "error: RUMP_SERVER not set\n");
|
2011-02-14 17:56:23 +03:00
|
|
|
errno = ENOENT;
|
2011-02-15 13:37:07 +03:00
|
|
|
goto out;
|
2011-02-14 17:56:23 +03:00
|
|
|
}
|
2011-01-05 20:14:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if ((error = parseurl(p, &serv_sa, &ptab_idx, 0)) != 0) {
|
2010-12-16 20:05:44 +03:00
|
|
|
errno = error;
|
2011-02-15 13:37:07 +03:00
|
|
|
goto out;
|
2010-12-16 20:05:44 +03:00
|
|
|
}
|
|
|
|
|
2011-01-24 20:47:51 +03:00
|
|
|
if (doinit() == -1)
|
2011-02-15 13:37:07 +03:00
|
|
|
goto out;
|
2011-02-14 17:56:23 +03:00
|
|
|
|
|
|
|
if ((p = getenv("RUMPCLIENT__EXECFD")) != NULL) {
|
2014-04-02 18:48:03 +04:00
|
|
|
sscanf(p, "%d,%d", &clispc.spc_fd, &holyfd);
|
2011-02-14 17:56:23 +03:00
|
|
|
unsetenv("RUMPCLIENT__EXECFD");
|
2011-02-15 13:37:07 +03:00
|
|
|
hstype = HANDSHAKE_EXEC;
|
|
|
|
} else {
|
2011-03-15 12:35:05 +03:00
|
|
|
if (doconnect() == -1)
|
2011-02-15 13:37:07 +03:00
|
|
|
goto out;
|
|
|
|
hstype = HANDSHAKE_GUEST;
|
2011-02-14 17:56:23 +03:00
|
|
|
}
|
|
|
|
|
2011-02-15 13:37:07 +03:00
|
|
|
error = handshake_req(&clispc, hstype, NULL, 0, false);
|
2011-01-05 20:14:50 +03:00
|
|
|
if (error) {
|
|
|
|
pthread_mutex_destroy(&clispc.spc_mtx);
|
|
|
|
pthread_cond_destroy(&clispc.spc_cv);
|
2011-01-24 20:47:51 +03:00
|
|
|
if (clispc.spc_fd != -1)
|
|
|
|
host_close(clispc.spc_fd);
|
2011-01-05 20:14:50 +03:00
|
|
|
errno = error;
|
2011-02-15 13:37:07 +03:00
|
|
|
goto out;
|
2011-01-05 20:14:50 +03:00
|
|
|
}
|
2011-02-15 13:37:07 +03:00
|
|
|
rv = 0;
|
2011-01-05 20:14:50 +03:00
|
|
|
|
2011-02-15 13:37:07 +03:00
|
|
|
out:
|
|
|
|
if (rv == -1)
|
|
|
|
init_done = 0;
|
|
|
|
return rv;
|
2011-01-05 20:14:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
struct rumpclient_fork {
|
|
|
|
uint32_t fork_auth[AUTHLEN];
|
2011-02-16 20:56:46 +03:00
|
|
|
struct spclient fork_spc;
|
2014-04-02 18:48:03 +04:00
|
|
|
int fork_holyfd;
|
2011-01-05 20:14:50 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
struct rumpclient_fork *
|
|
|
|
rumpclient_prefork(void)
|
|
|
|
{
|
|
|
|
struct rumpclient_fork *rpf;
|
2011-02-07 18:25:41 +03:00
|
|
|
sigset_t omask;
|
2011-01-05 20:14:50 +03:00
|
|
|
void *resp;
|
|
|
|
int rv;
|
|
|
|
|
2011-02-07 18:25:41 +03:00
|
|
|
pthread_sigmask(SIG_SETMASK, &fullset, &omask);
|
2011-01-05 20:14:50 +03:00
|
|
|
rpf = malloc(sizeof(*rpf));
|
|
|
|
if (rpf == NULL)
|
2011-02-16 18:33:46 +03:00
|
|
|
goto out;
|
2011-01-05 20:14:50 +03:00
|
|
|
|
2011-02-07 18:25:41 +03:00
|
|
|
if ((rv = prefork_req(&clispc, &omask, &resp)) != 0) {
|
2011-01-05 20:14:50 +03:00
|
|
|
free(rpf);
|
|
|
|
errno = rv;
|
2011-02-07 18:25:41 +03:00
|
|
|
rpf = NULL;
|
|
|
|
goto out;
|
2011-01-05 20:14:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(rpf->fork_auth, resp, sizeof(rpf->fork_auth));
|
|
|
|
free(resp);
|
2010-12-16 20:05:44 +03:00
|
|
|
|
2011-02-16 20:56:46 +03:00
|
|
|
rpf->fork_spc = clispc;
|
2014-04-02 18:48:03 +04:00
|
|
|
rpf->fork_holyfd = holyfd;
|
2011-02-16 20:56:46 +03:00
|
|
|
|
2011-02-07 18:25:41 +03:00
|
|
|
out:
|
|
|
|
pthread_sigmask(SIG_SETMASK, &omask, NULL);
|
2011-01-05 20:14:50 +03:00
|
|
|
return rpf;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
rumpclient_fork_init(struct rumpclient_fork *rpf)
|
|
|
|
{
|
|
|
|
int error;
|
2011-02-06 18:41:37 +03:00
|
|
|
int osock;
|
2011-01-05 20:14:50 +03:00
|
|
|
|
2011-02-06 18:41:37 +03:00
|
|
|
osock = clispc.spc_fd;
|
2011-01-05 20:14:50 +03:00
|
|
|
memset(&clispc, 0, sizeof(clispc));
|
2011-02-06 18:41:37 +03:00
|
|
|
clispc.spc_fd = osock;
|
|
|
|
|
2014-04-02 18:48:03 +04:00
|
|
|
#ifdef USE_KQUEUE
|
|
|
|
holyfd = -1; /* kqueue descriptor is not copied over fork() */
|
|
|
|
#else
|
|
|
|
if (holyfd != -1) {
|
|
|
|
host_close(holyfd);
|
|
|
|
holyfd = -1;
|
|
|
|
}
|
|
|
|
#endif
|
2011-01-05 20:14:50 +03:00
|
|
|
|
2011-01-24 20:47:51 +03:00
|
|
|
if (doinit() == -1)
|
|
|
|
return -1;
|
2011-03-15 12:35:05 +03:00
|
|
|
if (doconnect() == -1)
|
2011-01-05 20:14:50 +03:00
|
|
|
return -1;
|
|
|
|
|
2011-02-15 13:37:07 +03:00
|
|
|
error = handshake_req(&clispc, HANDSHAKE_FORK, rpf->fork_auth,
|
|
|
|
0, false);
|
2010-12-16 20:05:44 +03:00
|
|
|
if (error) {
|
|
|
|
pthread_mutex_destroy(&clispc.spc_mtx);
|
|
|
|
pthread_cond_destroy(&clispc.spc_cv);
|
2011-01-05 20:14:50 +03:00
|
|
|
errno = error;
|
|
|
|
return -1;
|
2010-12-16 20:05:44 +03:00
|
|
|
}
|
2011-01-05 20:14:50 +03:00
|
|
|
|
|
|
|
return 0;
|
2010-11-05 00:01:28 +03:00
|
|
|
}
|
2011-01-27 21:04:05 +03:00
|
|
|
|
2011-03-08 21:28:01 +03:00
|
|
|
/*ARGSUSED*/
|
2011-02-16 20:56:46 +03:00
|
|
|
void
|
|
|
|
rumpclient_fork_cancel(struct rumpclient_fork *rpf)
|
|
|
|
{
|
|
|
|
|
|
|
|
/* EUNIMPL */
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
rumpclient_fork_vparent(struct rumpclient_fork *rpf)
|
|
|
|
{
|
|
|
|
|
|
|
|
clispc = rpf->fork_spc;
|
2014-04-02 18:48:03 +04:00
|
|
|
holyfd = rpf->fork_holyfd;
|
2011-02-16 20:56:46 +03:00
|
|
|
}
|
|
|
|
|
2011-01-27 21:04:05 +03:00
|
|
|
void
|
|
|
|
rumpclient_setconnretry(time_t timeout)
|
|
|
|
{
|
|
|
|
|
2011-02-06 18:43:20 +03:00
|
|
|
if (timeout < RUMPCLIENT_RETRYCONN_DIE)
|
2011-01-27 21:04:05 +03:00
|
|
|
return; /* gigo */
|
|
|
|
|
|
|
|
retrytimo = timeout;
|
|
|
|
}
|
2011-02-14 17:56:23 +03:00
|
|
|
|
|
|
|
int
|
|
|
|
rumpclient__closenotify(int *fdp, enum rumpclient_closevariant variant)
|
|
|
|
{
|
|
|
|
int fd = *fdp;
|
|
|
|
int untilfd, rv;
|
|
|
|
int newfd;
|
|
|
|
|
|
|
|
switch (variant) {
|
|
|
|
case RUMPCLIENT_CLOSE_FCLOSEM:
|
2014-04-02 18:48:03 +04:00
|
|
|
untilfd = MAX(clispc.spc_fd, holyfd);
|
2011-02-14 17:56:23 +03:00
|
|
|
for (; fd <= untilfd; fd++) {
|
2014-04-02 18:48:03 +04:00
|
|
|
if (fd == clispc.spc_fd || fd == holyfd)
|
2011-02-14 17:56:23 +03:00
|
|
|
continue;
|
|
|
|
rv = host_close(fd);
|
|
|
|
if (rv == -1)
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
*fdp = fd;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case RUMPCLIENT_CLOSE_CLOSE:
|
|
|
|
case RUMPCLIENT_CLOSE_DUP2:
|
|
|
|
if (fd == clispc.spc_fd) {
|
|
|
|
newfd = dupgood(clispc.spc_fd, 1);
|
|
|
|
if (newfd == -1)
|
|
|
|
return -1;
|
2012-08-03 15:31:33 +04:00
|
|
|
|
|
|
|
#ifdef USE_KQUEUE
|
|
|
|
{
|
|
|
|
struct kevent kev[2];
|
|
|
|
|
2011-02-14 17:56:23 +03:00
|
|
|
/*
|
|
|
|
* now, we have a new socket number, so change
|
|
|
|
* the file descriptor that kqueue is
|
|
|
|
* monitoring. remove old and add new.
|
|
|
|
*/
|
|
|
|
EV_SET(&kev[0], clispc.spc_fd,
|
|
|
|
EVFILT_READ, EV_DELETE, 0, 0, 0);
|
|
|
|
EV_SET(&kev[1], newfd,
|
|
|
|
EVFILT_READ, EV_ADD|EV_ENABLE, 0, 0, 0);
|
2014-04-02 18:48:03 +04:00
|
|
|
if (host_kevent(holyfd, kev, 2, NULL, 0, NULL) == -1) {
|
2011-02-14 17:56:23 +03:00
|
|
|
int sverrno = errno;
|
|
|
|
host_close(newfd);
|
|
|
|
errno = sverrno;
|
|
|
|
return -1;
|
2014-04-02 18:48:03 +04:00
|
|
|
}}
|
|
|
|
#endif /* !USE_KQUEUE */
|
2011-02-14 17:56:23 +03:00
|
|
|
clispc.spc_fd = newfd;
|
|
|
|
}
|
2014-04-02 18:48:03 +04:00
|
|
|
if (holyfd != -1 && fd == holyfd) {
|
|
|
|
newfd = dupgood(holyfd, 1);
|
2011-02-14 17:56:23 +03:00
|
|
|
if (newfd == -1)
|
|
|
|
return -1;
|
2014-04-02 18:48:03 +04:00
|
|
|
holyfd = newfd;
|
2011-02-14 17:56:23 +03:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-02-16 18:33:46 +03:00
|
|
|
pid_t
|
2011-12-12 19:21:32 +04:00
|
|
|
rumpclient_fork(void)
|
2011-02-16 18:33:46 +03:00
|
|
|
{
|
|
|
|
|
2011-02-16 20:56:46 +03:00
|
|
|
return rumpclient__dofork(fork);
|
2011-02-16 18:33:46 +03:00
|
|
|
}
|
|
|
|
|
2011-02-14 17:56:23 +03:00
|
|
|
/*
|
|
|
|
* Process is about to exec. Save info about our existing connection
|
|
|
|
* in the env. rumpclient will check for this info in init().
|
|
|
|
* This is mostly for the benefit of rumphijack, but regular applications
|
|
|
|
* may use it as well.
|
|
|
|
*/
|
|
|
|
int
|
2011-02-16 18:33:46 +03:00
|
|
|
rumpclient_exec(const char *path, char *const argv[], char *const envp[])
|
2011-02-14 17:56:23 +03:00
|
|
|
{
|
|
|
|
char buf[4096];
|
|
|
|
char **newenv;
|
|
|
|
char *envstr, *envstr2;
|
2011-02-16 18:33:46 +03:00
|
|
|
size_t nelem;
|
|
|
|
int rv, sverrno;
|
2011-02-14 17:56:23 +03:00
|
|
|
|
|
|
|
snprintf(buf, sizeof(buf), "RUMPCLIENT__EXECFD=%d,%d",
|
2014-04-02 18:48:03 +04:00
|
|
|
clispc.spc_fd, holyfd);
|
2011-02-14 17:56:23 +03:00
|
|
|
envstr = malloc(strlen(buf)+1);
|
|
|
|
if (envstr == NULL) {
|
|
|
|
return ENOMEM;
|
|
|
|
}
|
|
|
|
strcpy(envstr, buf);
|
|
|
|
|
|
|
|
/* do we have a fully parsed url we want to forward in the env? */
|
|
|
|
if (*parsedurl != '\0') {
|
|
|
|
snprintf(buf, sizeof(buf),
|
|
|
|
"RUMP__PARSEDSERVER=%s", parsedurl);
|
|
|
|
envstr2 = malloc(strlen(buf)+1);
|
|
|
|
if (envstr2 == NULL) {
|
|
|
|
free(envstr);
|
|
|
|
return ENOMEM;
|
|
|
|
}
|
|
|
|
strcpy(envstr2, buf);
|
|
|
|
} else {
|
|
|
|
envstr2 = NULL;
|
|
|
|
}
|
|
|
|
|
2011-02-16 18:33:46 +03:00
|
|
|
for (nelem = 0; envp && envp[nelem]; nelem++)
|
|
|
|
continue;
|
2011-02-14 17:56:23 +03:00
|
|
|
|
2011-02-18 19:22:10 +03:00
|
|
|
newenv = malloc(sizeof(*newenv) * (nelem+3));
|
2011-02-14 17:56:23 +03:00
|
|
|
if (newenv == NULL) {
|
|
|
|
free(envstr2);
|
|
|
|
free(envstr);
|
|
|
|
return ENOMEM;
|
|
|
|
}
|
2011-02-16 18:33:46 +03:00
|
|
|
memcpy(&newenv[0], envp, nelem*sizeof(*envp));
|
2011-02-14 17:56:23 +03:00
|
|
|
|
2011-02-16 18:33:46 +03:00
|
|
|
newenv[nelem] = envstr;
|
|
|
|
newenv[nelem+1] = envstr2;
|
|
|
|
newenv[nelem+2] = NULL;
|
2011-02-14 17:56:23 +03:00
|
|
|
|
2011-02-16 18:33:46 +03:00
|
|
|
rv = host_execve(path, argv, newenv);
|
2011-02-14 17:56:23 +03:00
|
|
|
|
2011-02-16 18:33:46 +03:00
|
|
|
_DIAGASSERT(rv != 0);
|
|
|
|
sverrno = errno;
|
|
|
|
free(envstr2);
|
|
|
|
free(envstr);
|
|
|
|
free(newenv);
|
|
|
|
errno = sverrno;
|
|
|
|
return rv;
|
2011-02-14 17:56:23 +03:00
|
|
|
}
|
2011-02-16 20:56:46 +03:00
|
|
|
|
2013-01-18 00:47:44 +04:00
|
|
|
/*
|
|
|
|
* daemon() is handwritten for the benefit of platforms which
|
|
|
|
* do not support daemon().
|
|
|
|
*/
|
2011-02-16 20:56:46 +03:00
|
|
|
int
|
|
|
|
rumpclient_daemon(int nochdir, int noclose)
|
|
|
|
{
|
|
|
|
struct rumpclient_fork *rf;
|
|
|
|
int sverrno;
|
|
|
|
|
|
|
|
if ((rf = rumpclient_prefork()) == NULL)
|
|
|
|
return -1;
|
|
|
|
|
2013-01-18 00:47:44 +04:00
|
|
|
switch (fork()) {
|
|
|
|
case 0:
|
|
|
|
break;
|
|
|
|
case -1:
|
|
|
|
goto daemonerr;
|
|
|
|
default:
|
|
|
|
_exit(0);
|
2011-02-16 20:56:46 +03:00
|
|
|
}
|
|
|
|
|
2013-01-18 00:47:44 +04:00
|
|
|
if (setsid() == -1)
|
|
|
|
goto daemonerr;
|
|
|
|
if (!nochdir && chdir("/") == -1)
|
|
|
|
goto daemonerr;
|
|
|
|
if (!noclose) {
|
|
|
|
int fd = open("/dev/null", O_RDWR);
|
|
|
|
dup2(fd, 0);
|
|
|
|
dup2(fd, 1);
|
|
|
|
dup2(fd, 2);
|
|
|
|
if (fd > 2)
|
|
|
|
close(fd);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* note: fork is either completed or cancelled by the call */
|
2011-02-16 20:56:46 +03:00
|
|
|
if (rumpclient_fork_init(rf) == -1)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
return 0;
|
2013-01-18 00:47:44 +04:00
|
|
|
|
|
|
|
daemonerr:
|
|
|
|
sverrno = errno;
|
|
|
|
rumpclient_fork_cancel(rf);
|
|
|
|
errno = sverrno;
|
|
|
|
return -1;
|
2011-02-16 20:56:46 +03:00
|
|
|
}
|