diff --git a/UI/obs-app.cpp b/UI/obs-app.cpp index 48f07b59f..559f06aa5 100644 --- a/UI/obs-app.cpp +++ b/UI/obs-app.cpp @@ -1941,6 +1941,8 @@ static int run_program(fstream &logFile, int argc, char *argv[]) RunOnceMutex rom = GetRunOnceMutex(already_running); #elif defined(__APPLE__) CheckAppWithSameBundleID(already_running); +#elif defined(__linux__) + RunningInstanceCheck(already_running); #endif if (!already_running) { diff --git a/UI/platform-x11.cpp b/UI/platform-x11.cpp index 13af10e4d..f74e0f43b 100644 --- a/UI/platform-x11.cpp +++ b/UI/platform-x11.cpp @@ -27,8 +27,70 @@ #include #include "platform.hpp" + +#ifdef __linux__ +#include +#include +#include +#include +#include +#endif + using namespace std; +#ifdef __linux__ +void RunningInstanceCheck(bool &already_running) +{ + int uniq = socket(AF_LOCAL, SOCK_DGRAM | SOCK_CLOEXEC, 0); + + if (uniq == -1) { + blog(LOG_ERROR, + "Failed to check for running instance, socket: %d", errno); + already_running = 0; + return; + } + + struct sockaddr_un bindInfo; + memset(&bindInfo, 0, sizeof(sockaddr_un)); + bindInfo.sun_family = AF_LOCAL; + char *abstactSockName = NULL; + asprintf(&abstactSockName, "%s %d %s", "/com/obsproject", getpid(), + App()->GetVersionString().c_str()); + memmove(bindInfo.sun_path + 1, abstactSockName, + strlen(abstactSockName)); + free(abstactSockName); + + int bindErr = bind(uniq, (struct sockaddr *)&bindInfo, + sizeof(struct sockaddr_un)); + already_running = bindErr == 0 ? 0 : 1; + + if (already_running) { + return; + } + + FILE *fp = fopen("/proc/net/unix", "re"); + + if (fp == NULL) { + return; + } + + char *line = NULL; + size_t n = 0; + int obsCnt = 0; + while (getdelim(&line, &n, ' ', fp) != EOF) { + line[strcspn(line, "\n")] = '\0'; + if (*line == '@') { + if (strstr(line, "@/com/obsproject") != NULL) { + ++obsCnt; + } + } + } + already_running = obsCnt == 1 ? 0 : 1; + free(line); + fclose(fp); +} +#endif + static inline bool check_path(const char *data, const char *path, string &output) { diff --git a/UI/platform.hpp b/UI/platform.hpp index cf41d3c48..f8717cb43 100644 --- a/UI/platform.hpp +++ b/UI/platform.hpp @@ -70,3 +70,6 @@ void InstallNSApplicationSubclass(); void disableColorSpaceConversion(QWidget *window); void CheckAppWithSameBundleID(bool &already_running); #endif +#ifdef __linux__ +void RunningInstanceCheck(bool &already_running); +#endif