2023-10-25 07:43:36 +03:00
|
|
|
#include "file-system.hh"
|
|
|
|
#include "unix-domain-socket.hh"
|
2024-01-16 16:23:46 +02:00
|
|
|
#include "util.hh"
|
2023-10-25 07:43:36 +03:00
|
|
|
|
2024-04-18 23:49:52 +03:00
|
|
|
#ifdef _WIN32
|
|
|
|
# include <winsock2.h>
|
|
|
|
# include <afunix.h>
|
|
|
|
#else
|
|
|
|
# include <sys/socket.h>
|
|
|
|
# include <sys/un.h>
|
|
|
|
# include "processes.hh"
|
|
|
|
#endif
|
2023-10-25 07:43:36 +03:00
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
namespace nix {
|
|
|
|
|
|
|
|
AutoCloseFD createUnixDomainSocket()
|
|
|
|
{
|
2024-04-18 23:49:52 +03:00
|
|
|
AutoCloseFD fdSocket = toDescriptor(socket(PF_UNIX, SOCK_STREAM
|
2023-10-25 07:43:36 +03:00
|
|
|
#ifdef SOCK_CLOEXEC
|
|
|
|
| SOCK_CLOEXEC
|
|
|
|
#endif
|
2024-04-18 23:49:52 +03:00
|
|
|
, 0));
|
2023-10-25 07:43:36 +03:00
|
|
|
if (!fdSocket)
|
|
|
|
throw SysError("cannot create Unix domain socket");
|
2024-04-18 23:49:52 +03:00
|
|
|
#ifndef _WIN32
|
2024-06-13 17:34:44 +03:00
|
|
|
unix::closeOnExec(fdSocket.get());
|
2024-04-18 23:49:52 +03:00
|
|
|
#endif
|
2023-10-25 07:43:36 +03:00
|
|
|
return fdSocket;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
AutoCloseFD createUnixDomainSocket(const Path & path, mode_t mode)
|
|
|
|
{
|
|
|
|
auto fdSocket = nix::createUnixDomainSocket();
|
|
|
|
|
|
|
|
bind(fdSocket.get(), path);
|
|
|
|
|
|
|
|
if (chmod(path.c_str(), mode) == -1)
|
|
|
|
throw SysError("changing permissions on '%1%'", path);
|
|
|
|
|
2024-04-18 23:49:52 +03:00
|
|
|
if (listen(toSocket(fdSocket.get()), 100) == -1)
|
2023-10-25 07:43:36 +03:00
|
|
|
throw SysError("cannot listen on socket '%1%'", path);
|
|
|
|
|
|
|
|
return fdSocket;
|
|
|
|
}
|
|
|
|
|
2024-01-16 17:35:16 +02:00
|
|
|
static void bindConnectProcHelper(
|
|
|
|
std::string_view operationName, auto && operation,
|
2024-04-18 23:49:52 +03:00
|
|
|
Socket fd, const std::string & path)
|
2023-10-25 07:43:36 +03:00
|
|
|
{
|
|
|
|
struct sockaddr_un addr;
|
|
|
|
addr.sun_family = AF_UNIX;
|
|
|
|
|
2024-01-16 17:35:16 +02:00
|
|
|
// Casting between types like these legacy C library interfaces
|
|
|
|
// require is forbidden in C++. To maintain backwards
|
|
|
|
// compatibility, the implementation of the bind/connect functions
|
|
|
|
// contains some hints to the compiler that allow for this
|
|
|
|
// special case.
|
|
|
|
auto * psaddr = reinterpret_cast<struct sockaddr *>(&addr);
|
2023-10-25 07:43:36 +03:00
|
|
|
|
|
|
|
if (path.size() + 1 >= sizeof(addr.sun_path)) {
|
2024-04-18 23:49:52 +03:00
|
|
|
#ifdef _WIN32
|
|
|
|
throw Error("cannot %s to socket at '%s': path is too long", operationName, path);
|
|
|
|
#else
|
2024-01-16 16:23:46 +02:00
|
|
|
Pipe pipe;
|
|
|
|
pipe.create();
|
2023-11-02 16:50:00 +02:00
|
|
|
Pid pid = startProcess([&] {
|
2024-01-16 16:23:46 +02:00
|
|
|
try {
|
|
|
|
pipe.readSide.close();
|
|
|
|
Path dir = dirOf(path);
|
|
|
|
if (chdir(dir.c_str()) == -1)
|
|
|
|
throw SysError("chdir to '%s' failed", dir);
|
|
|
|
std::string base(baseNameOf(path));
|
|
|
|
if (base.size() + 1 >= sizeof(addr.sun_path))
|
|
|
|
throw Error("socket path '%s' is too long", base);
|
|
|
|
memcpy(addr.sun_path, base.c_str(), base.size() + 1);
|
2024-01-16 17:35:16 +02:00
|
|
|
if (operation(fd, psaddr, sizeof(addr)) == -1)
|
|
|
|
throw SysError("cannot %s to socket at '%s'", operationName, path);
|
2024-01-16 16:23:46 +02:00
|
|
|
writeFull(pipe.writeSide.get(), "0\n");
|
|
|
|
} catch (SysError & e) {
|
|
|
|
writeFull(pipe.writeSide.get(), fmt("%d\n", e.errNo));
|
|
|
|
} catch (...) {
|
|
|
|
writeFull(pipe.writeSide.get(), "-1\n");
|
|
|
|
}
|
2023-10-25 07:43:36 +03:00
|
|
|
});
|
2024-01-16 16:23:46 +02:00
|
|
|
pipe.writeSide.close();
|
|
|
|
auto errNo = string2Int<int>(chomp(drainFD(pipe.readSide.get())));
|
|
|
|
if (!errNo || *errNo == -1)
|
2024-01-16 17:35:16 +02:00
|
|
|
throw Error("cannot %s to socket at '%s'", operationName, path);
|
2024-01-16 16:23:46 +02:00
|
|
|
else if (*errNo > 0) {
|
|
|
|
errno = *errNo;
|
2024-01-16 17:35:16 +02:00
|
|
|
throw SysError("cannot %s to socket at '%s'", operationName, path);
|
2024-01-16 16:23:46 +02:00
|
|
|
}
|
2024-04-18 23:49:52 +03:00
|
|
|
#endif
|
2023-10-25 07:43:36 +03:00
|
|
|
} else {
|
|
|
|
memcpy(addr.sun_path, path.c_str(), path.size() + 1);
|
2024-01-16 17:35:16 +02:00
|
|
|
if (operation(fd, psaddr, sizeof(addr)) == -1)
|
|
|
|
throw SysError("cannot %s to socket at '%s'", operationName, path);
|
2023-10-25 07:43:36 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-16 17:35:16 +02:00
|
|
|
|
2024-04-18 23:49:52 +03:00
|
|
|
void bind(Socket fd, const std::string & path)
|
2024-01-16 17:35:16 +02:00
|
|
|
{
|
|
|
|
unlink(path.c_str());
|
|
|
|
|
|
|
|
bindConnectProcHelper("bind", ::bind, fd, path);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-04-18 23:49:52 +03:00
|
|
|
void connect(Socket fd, const std::string & path)
|
2024-01-16 17:35:16 +02:00
|
|
|
{
|
|
|
|
bindConnectProcHelper("connect", ::connect, fd, path);
|
|
|
|
}
|
|
|
|
|
2023-10-25 07:43:36 +03:00
|
|
|
}
|