Skip to content

Commit

Permalink
Optimize
Browse files Browse the repository at this point in the history
  • Loading branch information
DocMAX authored Dec 14, 2024
1 parent 364f8f9 commit 4d54271
Showing 1 changed file with 21 additions and 14 deletions.
35 changes: 21 additions & 14 deletions src/Apps/gamescopereaper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,16 +52,22 @@ namespace gamescope
std::getline(statFile, statLine);
statFile.close();

std::istringstream iss(statLine);
size_t openParen = statLine.find('(');
size_t closeParen = statLine.find(')', openParen);
if (openParen == std::string::npos || closeParen == std::string::npos)
continue;

// Extract the PPID (4th field, after process name with parentheses)
size_t afterName = closeParen + 2; // After ") " (space after parentheses)
std::istringstream iss(statLine.substr(afterName));
std::string token;
int i = 0;
pid_t ppid = 0;

// Extract the PPID (4th field in /proc/[pid]/stat)
while (iss >> token)
{
i++;
if (i == 4)
if (i == 3) // PPID is the 4th field, but we're offset because of the name
{
ppid = std::stoi(token);
break;
Expand Down Expand Up @@ -98,11 +104,11 @@ namespace gamescope
pthread_setname_np(pthread_self(), "gamescope-reaper");

static constexpr struct option k_ReaperOptions[] =
{
{"label", required_argument, nullptr, 0},
{"new-session-id", no_argument, nullptr, 0},
{"respawn", no_argument, nullptr, 0},
};
{
{"label", required_argument, nullptr, 0},
{"new-session-id", no_argument, nullptr, 0},
{"respawn", no_argument, nullptr, 0},
};

bool bRespawn = false;
bool bNewSession = false;
Expand Down Expand Up @@ -152,11 +158,11 @@ namespace gamescope

Process::ResetSignals();
std::array<int, 3> nExcludedFds =
{{
STDIN_FILENO,
STDOUT_FILENO,
STDERR_FILENO,
}};
{{
STDIN_FILENO,
STDOUT_FILENO,
STDERR_FILENO,
}};
Process::CloseAllFds(nExcludedFds);

if (bNewSession)
Expand Down Expand Up @@ -216,7 +222,8 @@ namespace gamescope
return 1;
}
}
}

} // namespace gamescope

int main(int argc, char **argv)
{
Expand Down

0 comments on commit 4d54271

Please sign in to comment.