Skip to content

Commit

Permalink
Randomize socket filename
Browse files Browse the repository at this point in the history
  • Loading branch information
jakobrs committed Jun 14, 2021
1 parent 513aa25 commit 5607e57
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 15 deletions.
3 changes: 3 additions & 0 deletions src/program/Context.h
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,9 @@ struct Context {

/* Indicate if at least one savestate was performed, for backtrack savestate */
bool didASavestate = false;

/* Socket filename */
std::string socket_filename;
};

#endif
6 changes: 3 additions & 3 deletions src/program/GameLoop.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -184,9 +184,9 @@ void GameLoop::init()
remove_savestates(context);

/* Remove the file socket */
int err = removeSocket();
int err = removeSocket(context->socket_filename);
if (err != 0)
emit alertToShow(QString("Could not remove socket file /tmp/libTAS.socket: %2").arg(strerror(err)));
emit alertToShow(QString("Could not remove socket file %1: %2").arg(context->socket_filename.c_str(), strerror(err)));

/* Init savestate list */
SaveStateList::init(context);
Expand Down Expand Up @@ -268,7 +268,7 @@ void GameLoop::init()
void GameLoop::initProcessMessages()
{
/* Connect to the socket between the program and the game */
initSocketProgram();
initSocketProgram(context->socket_filename);

/* Receive informations from the game */
int message = receiveMessage();
Expand Down
5 changes: 5 additions & 0 deletions src/program/GameThread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@ void GameThread::launch(Context *context)
*/
setenv("PWD", newdir.c_str(), 1);

/* Set the LIBTAS_SOCKET environment variable to context->socket_filename
* so that the game knows what the socket is called
*/
setenv("LIBTAS_SOCKET", context->socket_filename.c_str(), 1);

/* Set where stderr of the game is redirected */
int fd;
std::string logfile = context->gamepath + ".log";
Expand Down
5 changes: 5 additions & 0 deletions src/program/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,11 @@ int main(int argc, char **argv)
}
}

/* Randomize socket filename */
char templ[] = "/tmp/libTAS-XXXXXX";
context.socket_filename = mkdtemp(templ);
context.socket_filename += "/socket";

/* Create the working directories */
char *path = getenv("XDG_CONFIG_HOME");
if (path) {
Expand Down
29 changes: 19 additions & 10 deletions src/shared/sockethelpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@
#include <iostream>
#endif

#define SOCKET_FILENAME "/tmp/libTAS.socket"

#ifndef MSG_NOSIGNAL
#define MSG_NOSIGNAL 0
#endif
Expand All @@ -48,19 +46,21 @@ static int socket_fd = 0;

static std::mutex mutex;

int removeSocket(void) {
int ret = unlink(SOCKET_FILENAME);
int removeSocket(const std::string& socket_filename) {
int ret = unlink(socket_filename.c_str());
if ((ret == -1) && (errno != ENOENT))
return errno;
return 0;
}

bool initSocketProgram(void)
bool initSocketProgram(const std::string& socket_filename)
{
#ifdef __unix__
const struct sockaddr_un addr = { AF_UNIX, SOCKET_FILENAME };
struct sockaddr_un addr = { AF_UNIX };
strncpy(addr.sun_path, socket_filename.c_str(), 108);
#elif defined(__APPLE__) && defined(__MACH__)
const struct sockaddr_un addr = { sizeof(struct sockaddr_un), AF_UNIX, SOCKET_FILENAME };
struct sockaddr_un addr = { sizeof(struct sockaddr_un), AF_UNIX };
strncpy(addr.sun_path, socket_filename.c_str(), 104);
#endif
socket_fd = socket(AF_UNIX, SOCK_STREAM, 0);

Expand All @@ -85,21 +85,30 @@ bool initSocketProgram(void)
return true;
}

std::string getSocketFilenameGame(void)
{
return getenv("LIBTAS_SOCKET");
}

bool initSocketGame(void)
{
std::string socket_filename = getSocketFilenameGame();

/* Check if socket file already exists. If so, it is probably because
* the link is already done in another process of the game.
* In this case, we just return immediately.
*/
struct stat st;
int result = stat(SOCKET_FILENAME, &st);
int result = stat(socket_filename.c_str(), &st);
if (result == 0)
return false;

#ifdef __unix__
const struct sockaddr_un addr = { AF_UNIX, SOCKET_FILENAME };
struct sockaddr_un addr = { AF_UNIX };
strncpy(addr.sun_path, socket_filename.c_str(), 108);
#elif defined(__APPLE__) && defined(__MACH__)
const struct sockaddr_un addr = { sizeof(struct sockaddr_un), AF_UNIX, SOCKET_FILENAME };
struct sockaddr_un addr = { sizeof(struct sockaddr_un), AF_UNIX };
strncpy(addr.sun_path, socket_filename.c_str(), 104);
#endif
const int tmp_fd = socket(AF_UNIX, SOCK_STREAM, 0);
if (bind(tmp_fd, reinterpret_cast<const struct sockaddr*>(&addr), sizeof(struct sockaddr_un)))
Expand Down
9 changes: 7 additions & 2 deletions src/shared/sockethelpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,15 @@
#include <string>

/* Remove the socket file and return error */
int removeSocket();
int removeSocket(const std::string& socket_filename);

/* Returns the socket filename from environment variable (in the game, for
* program use context->socket_filename)
*/
std::string getSocketFilenameGame(void);

/* Initiate a socket connection with the game */
bool initSocketProgram(void);
bool initSocketProgram(const std::string& socket_filename);

/* Initiate a socket connection with libTAS */
bool initSocketGame(void);
Expand Down

0 comments on commit 5607e57

Please sign in to comment.