[mapplauncherd] Put booster sockets under XDG_RUNTIME_DIR

This is a compatibility-breaking change with boosters. The socketId
field is now expected to be a socket name, not a path. Sockets are
placed under ${XDG_RUNTIME_DIR}/mapplauncherd/, defaulting to /tmp/.

Invoker currently expects sockets in that path and named 'booster-X'
where X is the single character booster type. That is expected to
change when types are refactored to use full strings.
pull/1/head
John Brooks 13 years ago
parent b6e9aa171b
commit 1a09eb0fe5

@ -24,7 +24,7 @@
#include <errno.h>
#include <string.h>
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

@ -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;

@ -23,6 +23,7 @@
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/un.h>
#include <unistd.h>
#include <cstdlib>
#include <cstring>
@ -30,45 +31,66 @@
#include <errno.h>
#include <sstream>
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;
}

@ -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

Loading…
Cancel
Save