* Use a Unix domain socket instead of pipes.

This commit is contained in:
Eelco Dolstra 2006-12-03 02:36:44 +00:00
parent 8c76df93e6
commit 4251f94b32
2 changed files with 18 additions and 15 deletions

View file

@ -6,6 +6,7 @@
#include <sys/types.h> #include <sys/types.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <sys/socket.h>
#include <fcntl.h> #include <fcntl.h>
#include <iostream> #include <iostream>
@ -17,8 +18,12 @@ namespace nix {
RemoteStore::RemoteStore() RemoteStore::RemoteStore()
{ {
toChild.create(); int sockets[2];
fromChild.create(); if (socketpair(AF_UNIX, SOCK_STREAM, 0, sockets) == -1)
throw SysError("cannot create sockets");
fdSelf = sockets[0];
AutoCloseFD fdChild = sockets[1];
/* Start the worker. */ /* Start the worker. */
@ -34,14 +39,15 @@ RemoteStore::RemoteStore()
case 0: case 0:
try { /* child */ try { /* child */
fromChild.readSide.close(); if (dup2(fdChild, STDOUT_FILENO) == -1)
if (dup2(fromChild.writeSide, STDOUT_FILENO) == -1)
throw SysError("dupping write side"); throw SysError("dupping write side");
toChild.writeSide.close(); if (dup2(fdChild, STDIN_FILENO) == -1)
if (dup2(toChild.readSide, STDIN_FILENO) == -1)
throw SysError("dupping read side"); throw SysError("dupping read side");
close(fdSelf);
close(fdChild);
int fdDebug = open("/tmp/worker-log", O_WRONLY | O_CREAT | O_TRUNC, 0644); int fdDebug = open("/tmp/worker-log", O_WRONLY | O_CREAT | O_TRUNC, 0644);
assert(fdDebug != -1); assert(fdDebug != -1);
if (dup2(fdDebug, STDERR_FILENO) == -1) if (dup2(fdDebug, STDERR_FILENO) == -1)
@ -59,11 +65,10 @@ RemoteStore::RemoteStore()
quickExit(1); quickExit(1);
} }
fromChild.writeSide.close(); fdChild.close();
toChild.readSide.close();
from.fd = fromChild.readSide; from.fd = fdSelf;
to.fd = toChild.writeSide; to.fd = fdSelf;
/* Send the magic greeting, check for the reply. */ /* Send the magic greeting, check for the reply. */
@ -81,8 +86,7 @@ RemoteStore::RemoteStore()
RemoteStore::~RemoteStore() RemoteStore::~RemoteStore()
{ {
try { try {
fromChild.readSide.close(); fdSelf.close();
toChild.writeSide.close();
child.wait(true); child.wait(true);
} catch (Error & e) { } catch (Error & e) {
printMsg(lvlError, format("error (ignored): %1%") % e.msg()); printMsg(lvlError, format("error (ignored): %1%") % e.msg());

View file

@ -52,8 +52,7 @@ public:
void syncWithGC(); void syncWithGC();
private: private:
Pipe toChild; AutoCloseFD fdSelf;
Pipe fromChild;
FdSink to; FdSink to;
FdSource from; FdSource from;
Pid child; Pid child;