Skip to content

Commit 08b70b5

Browse files
committed
Randomize socket filename
1 parent 513aa25 commit 08b70b5

6 files changed

Lines changed: 43 additions & 16 deletions

File tree

src/program/Context.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,9 @@ struct Context {
147147

148148
/* Indicate if at least one savestate was performed, for backtrack savestate */
149149
bool didASavestate = false;
150+
151+
/* Socket filename */
152+
std::string socket_filename;
150153
};
151154

152155
#endif

src/program/GameLoop.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -184,9 +184,9 @@ void GameLoop::init()
184184
remove_savestates(context);
185185

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

191191
/* Init savestate list */
192192
SaveStateList::init(context);
@@ -268,7 +268,7 @@ void GameLoop::init()
268268
void GameLoop::initProcessMessages()
269269
{
270270
/* Connect to the socket between the program and the game */
271-
initSocketProgram();
271+
initSocketProgram(context->socket_filename);
272272

273273
/* Receive informations from the game */
274274
int message = receiveMessage();

src/program/GameThread.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,11 @@ void GameThread::launch(Context *context)
6767
*/
6868
setenv("PWD", newdir.c_str(), 1);
6969

70+
/* Set the LIBTAS_SOCKET environment variable to context->socket_filename
71+
* so that the game knows what the socket is called
72+
*/
73+
setenv("LIBTAS_SOCKET", context->socket_filename.c_str(), 1);
74+
7075
/* Set where stderr of the game is redirected */
7176
int fd;
7277
std::string logfile = context->gamepath + ".log";

src/program/main.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,11 @@ int main(int argc, char **argv)
260260
}
261261
}
262262

263+
/* Randomize socket filename */
264+
char templ[] = "/tmp/libTAS-XXXXXX";
265+
context.socket_filename = mkdtemp(templ);
266+
context.socket_filename += "/socket";
267+
263268
/* Create the working directories */
264269
char *path = getenv("XDG_CONFIG_HOME");
265270
if (path) {

src/shared/sockethelpers.cpp

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,6 @@
3535
#include <iostream>
3636
#endif
3737

38-
#define SOCKET_FILENAME "/tmp/libTAS.socket"
39-
4038
#ifndef MSG_NOSIGNAL
4139
#define MSG_NOSIGNAL 0
4240
#endif
@@ -48,19 +46,21 @@ static int socket_fd = 0;
4846

4947
static std::mutex mutex;
5048

51-
int removeSocket(void) {
52-
int ret = unlink(SOCKET_FILENAME);
49+
int removeSocket(const std::string& socket_filename) {
50+
int ret = unlink(socket_filename.c_str());
5351
if ((ret == -1) && (errno != ENOENT))
5452
return errno;
5553
return 0;
5654
}
5755

58-
bool initSocketProgram(void)
56+
bool initSocketProgram(const std::string& socket_filename)
5957
{
6058
#ifdef __unix__
61-
const struct sockaddr_un addr = { AF_UNIX, SOCKET_FILENAME };
59+
struct sockaddr_un addr = { AF_UNIX };
60+
strncpy(addr.sun_path, socket_filename.c_str(), 108);
6261
#elif defined(__APPLE__) && defined(__MACH__)
63-
const struct sockaddr_un addr = { sizeof(struct sockaddr_un), AF_UNIX, SOCKET_FILENAME };
62+
struct sockaddr_un addr = { sizeof(struct sockaddr_un), AF_UNIX };
63+
strncpy(addr.sun_path, socket_filename.c_str(), 104);
6464
#endif
6565
socket_fd = socket(AF_UNIX, SOCK_STREAM, 0);
6666

@@ -72,7 +72,7 @@ bool initSocketProgram(void)
7272
nanosleep(&tim, NULL);
7373
while (connect(socket_fd, reinterpret_cast<const struct sockaddr*>(&addr),
7474
sizeof(struct sockaddr_un))) {
75-
std::cout << "Attempt " << retry + 1 << ": Couldn't connect to socket." << std::endl;
75+
std::cout << "Attempt " << retry + 1 << ": No connection to socket." << std::endl;
7676
retry++;
7777
if (retry < MAX_RETRIES) {
7878
nanosleep(&tim, NULL);
@@ -85,21 +85,30 @@ bool initSocketProgram(void)
8585
return true;
8686
}
8787

88+
std::string getSocketFilenameGame(void)
89+
{
90+
return getenv("LIBTAS_SOCKET");
91+
}
92+
8893
bool initSocketGame(void)
8994
{
95+
std::string socket_filename = getSocketFilenameGame();
96+
9097
/* Check if socket file already exists. If so, it is probably because
9198
* the link is already done in another process of the game.
9299
* In this case, we just return immediately.
93100
*/
94101
struct stat st;
95-
int result = stat(SOCKET_FILENAME, &st);
102+
int result = stat(socket_filename.c_str(), &st);
96103
if (result == 0)
97104
return false;
98105

99106
#ifdef __unix__
100-
const struct sockaddr_un addr = { AF_UNIX, SOCKET_FILENAME };
107+
struct sockaddr_un addr = { AF_UNIX };
108+
strncpy(addr.sun_path, socket_filename.c_str(), 108);
101109
#elif defined(__APPLE__) && defined(__MACH__)
102-
const struct sockaddr_un addr = { sizeof(struct sockaddr_un), AF_UNIX, SOCKET_FILENAME };
110+
struct sockaddr_un addr = { sizeof(struct sockaddr_un), AF_UNIX };
111+
strncpy(addr.sun_path, socket_filename.c_str(), 104);
103112
#endif
104113
const int tmp_fd = socket(AF_UNIX, SOCK_STREAM, 0);
105114
if (bind(tmp_fd, reinterpret_cast<const struct sockaddr*>(&addr), sizeof(struct sockaddr_un)))

src/shared/sockethelpers.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,15 @@
2424
#include <string>
2525

2626
/* Remove the socket file and return error */
27-
int removeSocket();
27+
int removeSocket(const std::string& socket_filename);
28+
29+
/* Returns the socket filename from environment variable (in the game, for
30+
* program use context->socket_filename)
31+
*/
32+
std::string getSocketFilenameGame(void);
2833

2934
/* Initiate a socket connection with the game */
30-
bool initSocketProgram(void);
35+
bool initSocketProgram(const std::string& socket_filename);
3136

3237
/* Initiate a socket connection with libTAS */
3338
bool initSocketGame(void);

0 commit comments

Comments
 (0)