From 78fb06fc63ec0455b5814fec37c7e1da153cb20d Mon Sep 17 00:00:00 2001 From: silentrald Date: Thu, 5 Dec 2024 08:48:04 +0800 Subject: [PATCH] [bugfix] fix issue with reading processes in os.helpers.ts in linux --- src/__tests__/unit/os.test.ts | 10 ++++++++-- src/main/helpers/os.helpers.ts | 29 ++++++++++++++++++++++------- 2 files changed, 30 insertions(+), 9 deletions(-) diff --git a/src/__tests__/unit/os.test.ts b/src/__tests__/unit/os.test.ts index 00dd25db..616b14b6 100644 --- a/src/__tests__/unit/os.test.ts +++ b/src/__tests__/unit/os.test.ts @@ -196,8 +196,14 @@ ifDescribe(IS_LINUX)("Test os.helpers isProcessRunning", () => { const running = await isProcessRunning(`bs-manager-${crypto.randomUUID()}`); expect(running).toBe(false); - // No errors received - expect(logSpy).toHaveBeenCalledTimes(0); + // Throws because grep couldn't find any process with that name + expect(logSpy).toHaveBeenCalledTimes(1); }); + + it("Empty process name", async () => { + const running = await isProcessRunning(""); + expect(running).toBe(false); + expect(logSpy).toHaveBeenCalledTimes(0); + }) }); diff --git a/src/main/helpers/os.helpers.ts b/src/main/helpers/os.helpers.ts index 1e5a9638..6f22ff4f 100644 --- a/src/main/helpers/os.helpers.ts +++ b/src/main/helpers/os.helpers.ts @@ -3,9 +3,6 @@ import log from "electron-log"; import psList from "ps-list"; import { IS_FLATPAK } from "main/constants"; -// There are 2 erroneous lines ps | grep which is both the ps and grep calls themselves -const MIN_PROCESS_COUNT_LINUX = 2; - type LinuxOptions = { // Add the prefix to the command // eg. command - "./Beat Saber.exe" --no-yeet, prefix - "path/to/proton" run @@ -101,14 +98,24 @@ export function bsmExec(command: string, options?: BsmExecOptions): Promise<{ }); } +// Transform command from "steam" to "[s]team" +// NOTE: Can add an option to isProcessRunning/getProcessId to ignore this transformation +// in the future if needed +const transformProcessNameForPS = (name: string) => `[${name.at(0)}]${name.substring(1)}`; + async function isProcessRunningLinux(name: string): Promise { + if (!name) { + return false; + } + try { - const { stdout: count } = await bsmExec(`ps awwxo args | grep -c "${name}"`, { + const processName = transformProcessNameForPS(name); + const { stdout: count } = await bsmExec(`ps awwxo args | grep -c "${processName}"`, { log: true, flatpak: { host: IS_FLATPAK }, }); - return +count.trim() > MIN_PROCESS_COUNT_LINUX; + return +count.trim() > 0; } catch(error) { log.error(error); return false; @@ -143,14 +150,22 @@ async function isProcessRunningWindows(name: string): Promise { } async function getProcessIdLinux(name: string): Promise { + if (!name) { + return null; + } + try { - const { stdout } = await bsmExec(`ps awwxo pid,args | grep "${name}"`, { + const processName = transformProcessNameForPS(name); + const { stdout } = await bsmExec(`ps awwxo pid,args | grep "${processName}"`, { log: true, flatpak: { host: IS_FLATPAK }, }); + if (!stdout) { + return null; + } + const line = stdout.split("\n") - .slice(0, -MIN_PROCESS_COUNT_LINUX) .map(line => line.trimStart()) .find(line => line.includes(name) && !line.includes("grep")); return line ? +line.split(" ").at(0) : null;