diff options
author | Eelco Dolstra <edolstra@gmail.com> | 2019-11-04 22:29:31 +0100 |
---|---|---|
committer | Eelco Dolstra <edolstra@gmail.com> | 2019-11-04 22:29:31 +0100 |
commit | b81d9d26f50784ab1884e2cc6bc470d5cba91bc9 (patch) | |
tree | bf1a03fff022f94271df102d045d6b33e677c2c6 /src/libutil | |
parent | e34b317bbf778efb4e9bee5d34fb2ceb7776de3f (diff) | |
parent | f5a46ef0b1d76d32238f76ab8253f0d2d9fc72c9 (diff) |
Merge remote-tracking branch 'origin/master' into flakes
Diffstat (limited to 'src/libutil')
-rw-r--r-- | src/libutil/archive.cc | 9 | ||||
-rw-r--r-- | src/libutil/archive.hh | 2 | ||||
-rw-r--r-- | src/libutil/util.cc | 33 | ||||
-rw-r--r-- | src/libutil/util.hh | 4 |
4 files changed, 47 insertions, 1 deletions
diff --git a/src/libutil/archive.cc b/src/libutil/archive.cc index 3aa120270..db544a212 100644 --- a/src/libutil/archive.cc +++ b/src/libutil/archive.cc @@ -375,4 +375,13 @@ void copyNAR(Source & source, Sink & sink) } +void copyPath(const Path & from, const Path & to) +{ + auto source = sinkToSource([&](Sink & sink) { + dumpPath(from, sink); + }); + restorePath(to, *source); +} + + } diff --git a/src/libutil/archive.hh b/src/libutil/archive.hh index 25be426c1..768fe2536 100644 --- a/src/libutil/archive.hh +++ b/src/libutil/archive.hh @@ -77,6 +77,8 @@ void restorePath(const Path & path, Source & source); /* Read a NAR from 'source' and write it to 'sink'. */ void copyNAR(Source & source, Sink & sink); +void copyPath(const Path & from, const Path & to); + extern const std::string narVersionMagic1; diff --git a/src/libutil/util.cc b/src/libutil/util.cc index 7c06cf166..747b31cff 100644 --- a/src/libutil/util.cc +++ b/src/libutil/util.cc @@ -21,8 +21,10 @@ #include <pwd.h> #include <sys/ioctl.h> #include <sys/types.h> +#include <sys/socket.h> #include <sys/wait.h> #include <sys/time.h> +#include <sys/un.h> #include <unistd.h> #ifdef __APPLE__ @@ -1470,7 +1472,7 @@ static Sync<std::pair<unsigned short, unsigned short>> windowSize{{0, 0}}; static void updateWindowSize() { struct winsize ws; - if (ioctl(1, TIOCGWINSZ, &ws) == 0) { + if (ioctl(2, TIOCGWINSZ, &ws) == 0) { auto windowSize_(windowSize.lock()); windowSize_->first = ws.ws_row; windowSize_->second = ws.ws_col; @@ -1567,4 +1569,33 @@ std::unique_ptr<InterruptCallback> createInterruptCallback(std::function<void()> return std::unique_ptr<InterruptCallback>(res.release()); } + +AutoCloseFD createUnixDomainSocket(const Path & path, mode_t mode) +{ + AutoCloseFD fdSocket = socket(PF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0); + if (!fdSocket) + throw SysError("cannot create Unix domain socket"); + + closeOnExec(fdSocket.get()); + + struct sockaddr_un addr; + addr.sun_family = AF_UNIX; + if (path.size() >= sizeof(addr.sun_path)) + throw Error("socket path '%1%' is too long", path); + strcpy(addr.sun_path, path.c_str()); + + unlink(path.c_str()); + + if (bind(fdSocket.get(), (struct sockaddr *) &addr, sizeof(addr)) == -1) + throw SysError("cannot bind to socket '%1%'", path); + + if (chmod(path.c_str(), mode) == -1) + throw SysError("changing permissions on '%1%'", path); + + if (listen(fdSocket.get(), 5) == -1) + throw SysError("cannot listen on socket '%1%'", path); + + return fdSocket; +} + } diff --git a/src/libutil/util.hh b/src/libutil/util.hh index 30654e315..2e5f4ca4e 100644 --- a/src/libutil/util.hh +++ b/src/libutil/util.hh @@ -577,4 +577,8 @@ typedef std::function<bool(const Path & path)> PathFilter; extern PathFilter defaultPathFilter; +/* Create a Unix domain socket in listen mode. */ +AutoCloseFD createUnixDomainSocket(const Path & path, mode_t mode); + + } |