mirror of
https://git.rwth-aachen.de/acs/public/villas/node/
synced 2025-03-09 00:00:00 +01:00
api: create unix socket in home directory of not run as root
This commit is contained in:
parent
87ff27a566
commit
1bdd597be1
2 changed files with 34 additions and 14 deletions
|
@ -27,6 +27,8 @@
|
|||
#include <vector>
|
||||
#include <map>
|
||||
|
||||
#include <sys/socket.h>
|
||||
#include <sys/un.h>
|
||||
#include <poll.h>
|
||||
|
||||
#include <villas/common.h>
|
||||
|
@ -62,6 +64,8 @@ protected:
|
|||
void acceptNewSession();
|
||||
void closeSession(sessions::Socket *s);
|
||||
|
||||
struct sockaddr_un getSocketAddress();
|
||||
|
||||
public:
|
||||
Server(Api *a);
|
||||
~Server();
|
||||
|
|
|
@ -21,9 +21,8 @@
|
|||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*********************************************************************************/
|
||||
|
||||
#include <sys/socket.h>
|
||||
#include <sys/un.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/types.h>
|
||||
|
||||
#include <exception>
|
||||
#include <algorithm>
|
||||
|
@ -79,9 +78,36 @@ void Server::start()
|
|||
|
||||
pfds.push_back(pfd);
|
||||
|
||||
struct sockaddr_un sun = getSocketAddress();
|
||||
|
||||
ret = bind(sd, (struct sockaddr *) &sun, sizeof(struct sockaddr_un));
|
||||
if (ret)
|
||||
throw SystemError("Failed to bind API socket");
|
||||
|
||||
ret = listen(sd, 5);
|
||||
if (ret)
|
||||
throw SystemError("Failed to listen on API socket");
|
||||
|
||||
logger->info("Listening on UNIX socket: {}", sun.sun_path);
|
||||
|
||||
state = STATE_STARTED;
|
||||
}
|
||||
|
||||
struct sockaddr_un Server::getSocketAddress()
|
||||
{
|
||||
struct sockaddr_un sun = { .sun_family = AF_UNIX };
|
||||
|
||||
fs::path socketPath = PREFIX "/var/lib/villas";
|
||||
fs::path socketPath;
|
||||
|
||||
if (getuid() == 0) {
|
||||
socketPath = PREFIX "/var/lib/villas";
|
||||
}
|
||||
else {
|
||||
std::string homeDir = getenv("HOME");
|
||||
|
||||
socketPath = homeDir + "/.villas";
|
||||
}
|
||||
|
||||
if (!fs::exists(socketPath)) {
|
||||
logging.get("api")->info("Creating directory for API socket: {}", socketPath);
|
||||
fs::create_directories(socketPath);
|
||||
|
@ -96,17 +122,7 @@ void Server::start()
|
|||
|
||||
strncpy(sun.sun_path, socketPath.c_str(), sizeof(sun.sun_path) - 1);
|
||||
|
||||
ret = bind(sd, (struct sockaddr *) &sun, sizeof(struct sockaddr_un));
|
||||
if (ret)
|
||||
throw SystemError("Failed to bind API socket");
|
||||
|
||||
ret = listen(sd, 5);
|
||||
if (ret)
|
||||
throw SystemError("Failed to listen on API socket");
|
||||
|
||||
logger->info("Listening on UNIX socket: {}", socketPath);
|
||||
|
||||
state = STATE_STARTED;
|
||||
return sun;
|
||||
}
|
||||
|
||||
void Server::stop()
|
||||
|
|
Loading…
Add table
Reference in a new issue