Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
27 changes: 17 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,20 +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 };
#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 };
#endif
strncpy(addr.sun_path, socket_filename.c_str(), sizeof(addr.sun_path));
socket_fd = socket(AF_UNIX, SOCK_STREAM, 0);

struct timespec tim = {0, 500L*1000L*1000L};
Expand All @@ -85,22 +84,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 };
#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 };
#endif
strncpy(addr.sun_path, socket_filename.c_str(), sizeof(addr.sun_path));
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);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of a const std::string&, an std::string_view could be used instead.


/* 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);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above.


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