Debian Codegraph @

src/helper/pmon/procpidsolver.cpp

raw symbols SCIP
1 // SPDX-License-Identifier: GPL-3.0-or-later
2 // Copyright 2019 Juan Palacios <[email protected]>
3
4 #include "procpidsolver.h"
5
6 #include "common/fileutils.h"
7 #include <algorithm>
8 #include <cctype>
9 #include <cstddef>
10 #include <system_error>
11
12 std::string ProcPIDSolver::app(int PID)
13 {
14 auto const procPIDPath = procPath_ / std::to_string(PID);
15 auto exeName = procExeFileName(procPIDPath);
16
17 // Handle wine apps.
18 if (wineExecutables_.find(exeName) != wineExecutables_.cend()) {
19 auto const entries = Utils::File::readFileLines(procPIDPath / "cmdline",
20 '\0');
21 auto const wineExeName = wineAppName(entries);
22 if (!wineExeName.empty())
23 exeName = wineExeName;
24 }
25
26 return exeName;
27 }
28
29 std::string
30 ProcPIDSolver::procExeFileName(std::filesystem::path const &procPIDPath) const
31 {
32 std::error_code ec;
33 return std::filesystem::read_symlink(procPIDPath / "exe", ec).filename();
34 }
35
36 std::string ProcPIDSolver::lastComponent(std::string const &path) const
37 {
38 size_t const pos = path.find_last_of("\\/");
39 if (pos == std::string::npos)
40 return path;
41
42 return path.substr(pos + 1);
43 }
44
45 std::string ProcPIDSolver::wineAppName(std::vector<std::string> const &cmdline) const
46 {
47 for (auto const &entry : cmdline) {
48
49 // skip wine executable files
50 std::filesystem::path const entryPath(entry);
51 if (entryPath.is_absolute() &&
52 wineExecutables_.find(entryPath.filename()) != wineExecutables_.cend())
53 continue;
54
55 // look for .exe extension
56 std::string extension = entryPath.extension();
57 std::transform(extension.cbegin(), extension.cend(), extension.begin(),
58 ::tolower);
59 if (extension != ".exe")
60 break;
61
62 return lastComponent(entry);
63 }
64
65 return "";
66 }