diff --git a/src/ebooster/ebooster.cpp b/src/ebooster/ebooster.cpp index ba0cdcb..f8bc80c 100644 --- a/src/ebooster/ebooster.cpp +++ b/src/ebooster/ebooster.cpp @@ -24,7 +24,7 @@ #include #include -const string EBooster::m_socketId = "/tmp/booste"; +const string EBooster::m_socketId = "booster-e"; const string EBooster::m_temporaryProcessName = "booster-e"; const string & EBooster::socketId() const diff --git a/src/invoker/invoker.c b/src/invoker/invoker.c index a3d1e22..c748954 100644 --- a/src/invoker/invoker.c +++ b/src/invoker/invoker.c @@ -190,12 +190,23 @@ static int invoker_init(char app_type) return -1; } - sun.sun_family = AF_UNIX; //AF_FILE; - + sun.sun_family = AF_UNIX; const int maxSize = sizeof(sun.sun_path) - 1; + + const char *runtimeDir = getenv("XDG_RUNTIME_DIR"); + const char *subpath = "/mapplauncherd/booster-"; + const int subpathLen = strlen(subpath) + 1; + + if (runtimeDir && *runtimeDir) + strncpy(sun.sun_path, runtimeDir, maxSize - subpathLen); + else + strncpy(sun.sun_path, "/tmp", maxSize - subpathLen); + + sun.sun_path[maxSize - subpathLen] = 0; + strcat(sun.sun_path, subpath); + if (app_type >= 'a' && app_type <= 'z') { - strncpy(sun.sun_path, "/tmp/boost", maxSize - 1); int len = strlen(sun.sun_path); sun.sun_path[len++] = app_type; sun.sun_path[len] = 0; diff --git a/src/launcherlib/socketmanager.cpp b/src/launcherlib/socketmanager.cpp index 783b3b8..a6124eb 100644 --- a/src/launcherlib/socketmanager.cpp +++ b/src/launcherlib/socketmanager.cpp @@ -23,6 +23,7 @@ #include #include #include +#include #include #include #include @@ -30,45 +31,66 @@ #include #include +SocketManager::SocketManager() +{ + const char *runtimeDir = getenv("XDG_RUNTIME_DIR"); + if (!runtimeDir || !*runtimeDir) + runtimeDir = "/tmp/"; + + m_socketRootPath = runtimeDir; + m_socketRootPath += "/mapplauncherd"; + + if (mkdir(m_socketRootPath.c_str(), S_IRUSR | S_IWUSR | S_IXUSR) != 0) { + if (errno != EEXIST) { + Logger::logError("Daemon: Cannot create socket root directory %s: %s\n", + m_socketRootPath.c_str(), strerror(errno)); + } + } + + m_socketRootPath += '/'; +} + void SocketManager::initSocket(const string & socketId) { + string socketPath = m_socketRootPath + socketId; + // Initialize a socket at socketId if one already doesn't // exist for that id / path. if (m_socketHash.find(socketId) == m_socketHash.end()) { - Logger::logDebug("SocketManager: Initing socket at '%s'..", socketId.c_str()); + Logger::logDebug("SocketManager: Initing socket at '%s'..", socketPath.c_str()); // Create a new local socket int socketFd = socket(PF_UNIX, SOCK_STREAM, 0); if (socketFd < 0) throw std::runtime_error("SocketManager: Failed to open socket\n"); - // TODO: Error if socketId >= maxLen. Also unlink() here may + // TODO: Error if socketPath >= maxLen. Also unlink() here may // try to remove a different file than is passed to sun.sa_data. // Remove the previous socket file struct stat sb; - stat(socketId.c_str(), &sb); + stat(socketPath.c_str(), &sb); if (S_ISSOCK(sb.st_mode)) { // coverity[toctou] - if (unlink(socketId.c_str()) == -1) + if (unlink(socketPath.c_str()) == -1) { std::string msg("SocketManager: Failed to unlink existing socket file '"); - msg += socketId + "': " + strerror(errno); + msg += socketPath + "': " + strerror(errno); Logger::logWarning(msg.c_str()); } } // Initialize the socket struct - struct sockaddr sun; - sun.sa_family = AF_UNIX; - int maxLen = sizeof(sun.sa_data) - 1; - strncpy(sun.sa_data, socketId.c_str(), maxLen); - sun.sa_data[maxLen] = '\0'; + struct sockaddr_un sun; + sun.sun_family = AF_UNIX; + int maxLen = sizeof(sun.sun_path) - 1; + strncpy(sun.sun_path, socketPath.c_str(), maxLen); + sun.sun_path[maxLen] = '\0'; // Bind the socket - if (bind(socketFd, &sun, sizeof(sun)) < 0) + if (bind(socketFd, (struct sockaddr*) &sun, sizeof(sun)) < 0) { std::string msg("SocketManager: Failed to bind to socket (fd="); std::stringstream ss; @@ -88,9 +110,7 @@ void SocketManager::initSocket(const string & socketId) } // Set permissions - chmod(socketId.c_str(), S_IRUSR | S_IWUSR | S_IXUSR | - S_IRGRP | S_IWGRP | S_IXGRP | - S_IROTH | S_IWOTH | S_IXOTH); + chmod(socketPath.c_str(), S_IRUSR | S_IWUSR | S_IXUSR); // Store path <-> file descriptor mapping m_socketHash[socketId] = socketFd; @@ -142,3 +162,9 @@ void SocketManager::addMapping(const string & socketId, int fd) { m_socketHash[socketId] = fd; } + +string SocketManager::socketRootPath() const +{ + return m_socketRootPath; +} + diff --git a/src/launcherlib/socketmanager.h b/src/launcherlib/socketmanager.h index 5dbea31..58e3c5d 100644 --- a/src/launcherlib/socketmanager.h +++ b/src/launcherlib/socketmanager.h @@ -36,6 +36,7 @@ using std::string; class DECL_EXPORT SocketManager { public: + SocketManager(); /*! \brief Initialize a file socket. * \param socketId Path to the socket file. @@ -68,9 +69,18 @@ public: //! Add mapping of socketId to fd void addMapping(const string & socketId, int fd); + /*! + * Root path for booster sockets + */ + string socketRootPath() const; + private: SocketHash m_socketHash; + + //! Root path for booster sockets + string m_socketRootPath; + }; #endif // SOCKETMANAGER_H