Hermes
0.7.0
Linux game streaming host
Toggle main menu visibility
Loading...
Searching...
No Matches
desktop_steam.h
Go to the documentation of this file.
1
5
#pragma once
6
7
#include <algorithm>
8
#include <cctype>
9
#include <filesystem>
10
#include <fstream>
11
#include <optional>
12
#include <string>
13
#include <string_view>
14
#include <sys/types.h>
15
16
namespace
platf::desktop_steam {
17
19
inline
constexpr
std::string_view
flatpak_id
=
"com.valvesoftware.Steam"
;
20
21
struct
client_t
{
22
pid_t pid;
24
bool
flatpak
;
25
};
26
27
namespace
detail {
28
inline
std::string first_line(
const
std::filesystem::path &file) {
29
std::ifstream stream {file};
30
std::string line;
31
std::getline(stream, line);
32
return
line;
33
}
34
35
inline
bool
owned_by(
const
std::filesystem::path &entry, uid_t uid) {
36
std::ifstream stream {entry /
"status"
};
37
for
(std::string line; std::getline(stream, line);) {
38
if
(line.starts_with(
"Uid:"
)) {
39
// Real, effective, saved and filesystem IDs: the real one is first.
40
try
{
41
return
std::stoul(line.substr(4)) == uid;
42
}
catch
(...) {
43
return
false
;
44
}
45
}
46
}
47
return
false
;
48
}
49
54
inline
bool
is_client
(
const
std::filesystem::path &entry) {
55
if
(first_line(entry /
"comm"
) !=
"steam"
) {
56
return
false
;
57
}
58
std::error_code ec;
59
const
auto
exe = std::filesystem::read_symlink(entry /
"exe"
, ec);
60
return
!ec && exe.filename() ==
"steam"
;
61
}
62
63
inline
bool
in_flatpak(
const
std::filesystem::path &entry) {
64
// systemd puts a Flatpak in app-flatpak-<id>-<n>.scope; .flatpak-info
65
// is in the root of every Flatpak sandbox, should the scope be missing.
66
std::error_code ec;
67
return
first_line(entry /
"cgroup"
).find(
"app-flatpak-"
+ std::string {
flatpak_id
}) != std::string::npos ||
68
std::filesystem::exists(entry /
"root"
/
".flatpak-info"
, ec);
69
}
70
}
// namespace detail
71
86
inline
std::optional<client_t>
find
(
const
std::filesystem::path &proc, uid_t uid,
const
std::filesystem::path &pid_file, std::string_view own_cgroup) {
87
const
auto
candidate = [&](pid_t pid) -> std::optional<client_t> {
88
const
auto
entry = proc / std::to_string(pid);
89
if
(pid <= 0 || detail::first_line(entry /
"comm"
) !=
"steam"
|| !detail::owned_by(entry, uid)) {
90
return
std::nullopt;
91
}
92
if
(own_cgroup.ends_with(
".service"
) && detail::first_line(entry /
"cgroup"
).starts_with(own_cgroup)) {
93
return
std::nullopt;
94
}
95
return
client_t
{pid, detail::in_flatpak(entry)};
96
};
97
98
if
(!pid_file.empty()) {
99
pid_t pid = 0;
100
// The pid file outlives the client, so the process is checked too.
101
std::ifstream {pid_file} >> pid;
102
if
(
const
auto
client = candidate(pid)) {
103
return
client;
104
}
105
}
106
107
std::optional<client_t> found;
108
std::error_code ec;
109
for
(
const
auto
&entry : std::filesystem::directory_iterator {proc, ec}) {
110
const
auto
name = entry.path().filename().string();
111
if
(name.empty() || !std::ranges::all_of(name, [](
unsigned
char
c) {
112
return std::isdigit(c);
113
})) {
114
continue
;
115
}
116
const
pid_t pid = std::stoi(name);
117
if
(found && found->pid < pid) {
118
continue
;
119
}
120
if
(!
detail::is_client
(entry.path())) {
121
continue
;
122
}
123
if
(
const
auto
client = candidate(pid)) {
124
found = client;
125
}
126
}
127
return
found;
128
}
129
130
}
// namespace platf::desktop_steam
platf::desktop_steam::find
std::optional< client_t > find(const std::filesystem::path &proc, uid_t uid, const std::filesystem::path &pid_file, std::string_view own_cgroup)
The user's running Steam client.
Definition
desktop_steam.h:86
platf::desktop_steam::detail::is_client
bool is_client(const std::filesystem::path &entry)
Definition
desktop_steam.h:54
platf::desktop_steam::flatpak_id
constexpr std::string_view flatpak_id
The Flatpak's application ID.
Definition
desktop_steam.h:19
platf::desktop_steam::client_t
Definition
desktop_steam.h:21
platf::desktop_steam::client_t::flatpak
bool flatpak
Runs in the Flatpak, so flatpak run starts it again.
Definition
desktop_steam.h:24
src
platform
linux
desktop_steam.h
Generated by
1.18.0