* Refactoring.

This commit is contained in:
Eelco Dolstra 2006-12-04 13:28:14 +00:00
parent 052b6fb149
commit f5f0cf423f
3 changed files with 33 additions and 17 deletions

View file

@ -18,6 +18,35 @@ namespace nix {
RemoteStore::RemoteStore() RemoteStore::RemoteStore()
{
string remoteMode = getEnv("NIX_REMOTE");
if (remoteMode == "slave")
/* Fork off a setuid worker to do the privileged work. */
forkSlave();
else if (remoteMode == "daemon")
/* Connect to a daemon that does the privileged work for
us. */
;
else
throw Error(format("invalid setting for NIX_REMOTE, `%1%'")
% remoteMode);
/* Send the magic greeting, check for the reply. */
try {
processStderr();
writeInt(WORKER_MAGIC_1, to);
unsigned int magic = readInt(from);
if (magic != WORKER_MAGIC_2) throw Error("protocol mismatch");
} catch (Error & e) {
throw Error(format("cannot start worker (%1%)")
% e.msg());
}
}
void RemoteStore::forkSlave()
{ {
int sockets[2]; int sockets[2];
if (socketpair(AF_UNIX, SOCK_STREAM, 0, sockets) == -1) if (socketpair(AF_UNIX, SOCK_STREAM, 0, sockets) == -1)
@ -26,7 +55,6 @@ RemoteStore::RemoteStore()
fdSelf = sockets[0]; fdSelf = sockets[0];
AutoCloseFD fdChild = sockets[1]; AutoCloseFD fdChild = sockets[1];
/* Start the worker. */ /* Start the worker. */
Path worker = getEnv("NIX_WORKER"); Path worker = getEnv("NIX_WORKER");
if (worker == "") if (worker == "")
@ -78,18 +106,6 @@ RemoteStore::RemoteStore()
from.fd = fdSelf; from.fd = fdSelf;
to.fd = fdSelf; to.fd = fdSelf;
/* Send the magic greeting, check for the reply. */
try {
processStderr();
writeInt(WORKER_MAGIC_1, to);
unsigned int magic = readInt(from);
if (magic != WORKER_MAGIC_2) throw Error("protocol mismatch");
} catch (Error & e) {
throw Error(format("cannot start worker process `%1%' (%2%)")
% worker % e.msg());
}
} }

View file

@ -58,6 +58,8 @@ private:
Pid child; Pid child;
void processStderr(); void processStderr();
void forkSlave();
}; };

View file

@ -137,12 +137,10 @@ boost::shared_ptr<StoreAPI> store;
boost::shared_ptr<StoreAPI> openStore(bool reserveSpace) boost::shared_ptr<StoreAPI> openStore(bool reserveSpace)
{ {
string mode = getEnv("NIX_REMOTE"); if (getEnv("NIX_REMOTE") == "")
if (mode == "")
return boost::shared_ptr<StoreAPI>(new LocalStore(reserveSpace)); return boost::shared_ptr<StoreAPI>(new LocalStore(reserveSpace));
else if (mode == "slave") else
return boost::shared_ptr<StoreAPI>(new RemoteStore()); return boost::shared_ptr<StoreAPI>(new RemoteStore());
else throw Error(format("invalid setting for NIX_REMOTE, `%1%'") % mode);
} }