From 0c9eeeea431316d32636d492e07b4c846ad1ac7a Mon Sep 17 00:00:00 2001 From: Jeff Becker Date: Tue, 8 Sep 2020 13:15:11 -0400 Subject: [PATCH] allow an IPC socket to set which group it is owned by using SOCKET_GID similar to how STARTUP_UMASK is done. --- lokimq/lokimq.h | 4 ++++ lokimq/proxy.cpp | 14 ++++++++++++++ 2 files changed, 18 insertions(+) diff --git a/lokimq/lokimq.h b/lokimq/lokimq.h index 530fc7f..1de0445 100644 --- a/lokimq/lokimq.h +++ b/lokimq/lokimq.h @@ -266,6 +266,10 @@ public: */ int STARTUP_UMASK = -1; + /** The gid that owns any sockets when constructed (same as umask) + */ + int SOCKET_GID = -1; + /// A special TaggedThreadID value that always refers to the proxy thread; the main use of this is /// to direct very simple batch completion jobs to be executed directly in the proxy thread. inline static constexpr TaggedThreadID run_in_proxy{-1}; diff --git a/lokimq/proxy.cpp b/lokimq/proxy.cpp index 034a9e9..620e13c 100644 --- a/lokimq/proxy.cpp +++ b/lokimq/proxy.cpp @@ -12,6 +12,8 @@ extern "C" { #ifndef _WIN32 extern "C" { #include +#include +#include } #endif @@ -384,6 +386,18 @@ void LokiMQ::proxy_loop() { #ifndef _WIN32 if (saved_umask != -1) umask(saved_umask); + + // set socket gid if it is provided + if (SOCKET_GID != -1) { + for(size_t i = 0; i < bind.size(); i++) { + const address addr(bind[i].first); + if(addr.ipc()) { + if(chown(addr.socket.c_str(), -1, SOCKET_GID) == -1) { + throw std::runtime_error("cannot set group on " + addr.socket + ": " + strerror(errno)); + } + } + } + } #endif pollitems_stale = true;