From f4dc07729938b53f94cc7410b2f1c6f5741636fb Mon Sep 17 00:00:00 2001
From: dec05eba <dec05eba@protonmail.com>
Date: Sat, 4 Jan 2025 02:30:14 +0100
Subject: pidof ignore self

---
 src/Process.cpp | 9 ++++++---
 src/main.cpp    | 2 +-
 2 files changed, 7 insertions(+), 4 deletions(-)

(limited to 'src')

diff --git a/src/Process.cpp b/src/Process.cpp
index c5692f5..c5fcf0f 100644
--- a/src/Process.cpp
+++ b/src/Process.cpp
@@ -206,7 +206,7 @@ namespace gsr {
         return false;
     }
 
-    pid_t pidof(const char *process_name) {
+    pid_t pidof(const char *process_name, pid_t ignore_pid) {
         pid_t result = -1;
         DIR *dir = opendir("/proc");
         if(!dir)
@@ -222,8 +222,11 @@ namespace gsr {
 
             snprintf(cmdline_filepath, sizeof(cmdline_filepath), "/proc/%s/cmdline", entry->d_name);
             if(read_cmdline_arg0(cmdline_filepath, arg0, sizeof(arg0)) && strcmp(process_name, arg0) == 0) {
-                result = atoi(entry->d_name);
-                break;
+                const pid_t pid = atoi(entry->d_name);
+                if(pid != ignore_pid) {
+                    result = pid;
+                    break;
+                }
             }
         }
 
diff --git a/src/main.cpp b/src/main.cpp
index 1c16e6f..3cccde2 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -231,7 +231,7 @@ int main(int argc, char **argv) {
     // that uses pid sandboxing. Replace this with a better method once we no longer rely on linux global hotkeys on some platform.
     // TODO: This method doesn't work when disabling hotkeys and the method below with pidof gsr-ui doesn't work in flatpak.
     // What do? creating a pid file doesn't work in flatpak either.
-    if(is_gsr_ui_virtual_keyboard_running() || gsr::pidof("gsr-ui") != -1) {
+    if(is_gsr_ui_virtual_keyboard_running() || gsr::pidof("gsr-ui", getpid()) != -1) {
         gsr::Rpc rpc;
         if(rpc.open("gsr-ui") && rpc.write("show_ui\n", 8)) {
             fprintf(stderr, "Error: another instance of gsr-ui is already running, opening that one instead\n");
-- 
cgit v1.2.3-70-g09d2