Skip to content

Commit c432057

Browse files
committed
refactor(linux): Move environment variable access to wrapper functions
1 parent a682ab0 commit c432057

6 files changed

Lines changed: 56 additions & 45 deletions

File tree

src/platform/common.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -632,6 +632,14 @@ namespace platf {
632632

633633
void restart();
634634

635+
/**
636+
* @brief Get an environment variable.
637+
* @param name The name of the environment variable.
638+
* @param value Reference to write the env variable value into.
639+
* @return true if parameter value was updated to the environment variable, false if the value didn't exist
640+
*/
641+
bool get_env(const std::string &name, std::string &value);
642+
635643
/**
636644
* @brief Set an environment variable.
637645
* @param name The name of the environment variable.
@@ -640,6 +648,20 @@ namespace platf {
640648
*/
641649
int set_env(const std::string &name, const std::string &value);
642650

651+
/**
652+
* @brief Append string to an environment variable if it does not already contain it.
653+
* @param name The name of the environment variable.
654+
* @param value The value to set the environment variable to.
655+
* @param separator Optional separator for the new value if it is not the first one (default: "")
656+
* @return 0 on success, non-zero on failure.
657+
*/
658+
inline int append_env(const std::string &name, const std::string &value, const std::string &separator = "") {
659+
if (std::string old_value; get_env(name, old_value) && !old_value.contains(value)) {
660+
return set_env(name, old_value.empty() ? value : old_value + separator + value);
661+
}
662+
return 0;
663+
}
664+
643665
/**
644666
* @brief Unset an environment variable.
645667
* @param name The name of the environment variable.

src/platform/linux/input/inputtino_seat.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,14 @@
77

88
// local includes
99
#include "inputtino_seat.h"
10+
#include "src/platform/common.h"
1011

1112
namespace platf::inputtino_seat {
1213

1314
std::string get_target_seat() {
14-
if (const char *seat = std::getenv("XDG_SEAT")) {
15-
if (seat[0] != '\0') {
16-
return seat;
17-
}
15+
if (std::string seat; platf::get_env("XDG_SEAT", seat) && !seat.empty()) {
16+
return seat;
1817
}
19-
2018
return {};
2119
}
2220

src/platform/linux/kwingrab.cpp

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ namespace kwin {
5050
class screencast_permission_helper_t {
5151
public:
5252
static bool is_permission_system_deactivated() {
53-
return getenvstr("KWIN_WAYLAND_NO_PERMISSION_CHECKS") == "1";
53+
std::string v;
54+
return platf::get_env("KWIN_WAYLAND_NO_PERMISSION_CHECKS", v) && v == "1";
5455
}
5556

5657
static void setup() {
@@ -147,17 +148,9 @@ namespace kwin {
147148
static inline bool initialized = false;
148149
static inline bool create_file = true;
149150

150-
static std::string getenvstr(std::string const &key) {
151-
char const *val = std::getenv(key.c_str());
152-
if (!val) {
153-
return "";
154-
}
155-
return val;
156-
}
157-
158151
static std::filesystem::path get_home_dir() {
159152
// Check HOME environment variable
160-
if (std::string homedir = getenvstr("HOME"); !homedir.empty()) {
153+
if (std::string homedir; platf::get_env("HOME", homedir) && !homedir.empty()) {
161154
return homedir;
162155
}
163156
// Fall back to home directory from NSS passwd
@@ -169,7 +162,7 @@ namespace kwin {
169162
// Follow the XDG base directory specification for user data home:
170163
// https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html
171164
std::filesystem::path xdg_data_home;
172-
if (std::string dir = getenvstr("XDG_DATA_HOME"); !dir.empty()) {
165+
if (std::string dir; platf::get_env("XDG_DATA_HOME", dir) && !dir.empty()) {
173166
xdg_data_home = std::filesystem::path(dir);
174167
} else {
175168
const auto homedir = get_home_dir();
@@ -209,7 +202,7 @@ namespace kwin {
209202
static bool check_kwin_system_permissions(const std::string_view &filenameprefix, const std::string_view &executablepath) {
210203
// Find data dirs to check from XDG_DATA_DIRS
211204
std::vector<std::string> xdg_data_dirs;
212-
if (const std::string e = getenvstr("XDG_DATA_DIRS"); !e.empty()) {
205+
if (std::string e; platf::get_env("XDG_DATA_DIRS", e) && !e.empty()) {
213206
std::stringstream ss(e);
214207
std::string item;
215208

@@ -303,13 +296,13 @@ namespace kwin {
303296
screencast_permission_helper_t::setup();
304297
}
305298

306-
const char *wl_name = std::getenv("WAYLAND_DISPLAY");
307-
if (!wl_name) {
299+
std::string wl_name;
300+
if (!platf::get_env("WAYLAND_DISPLAY", wl_name)) {
308301
BOOST_LOG(error) << "[kwingrab] WAYLAND_DISPLAY not set"sv;
309302
return -1;
310303
}
311304

312-
wl_display = wl_display_connect(wl_name);
305+
wl_display = wl_display_connect(wl_name.c_str());
313306
if (!wl_display) {
314307
BOOST_LOG(error) << "[kwingrab] cannot connect to Wayland display: "sv << wl_name;
315308
return -1;

src/platform/linux/misc.cpp

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -145,24 +145,24 @@ namespace platf {
145145
std::call_once(migration_flag, []() {
146146
bool found = false;
147147
bool migrate_config = true;
148-
const char *dir;
149-
const char *homedir;
150-
const char *migrate_envvar;
148+
std::string dir;
149+
std::string homedir;
150+
std::string migrate_envvar;
151151

152152
// Get the home directory
153-
if ((homedir = getenv("HOME")) == nullptr || strlen(homedir) == 0) {
153+
if (!get_env("HOME", homedir) || homedir.empty()) {
154154
// If HOME is empty or not set, use the current user's home directory
155155
homedir = getpwuid(geteuid())->pw_dir;
156156
}
157157

158158
// May be set if running under a systemd service with the ConfigurationDirectory= option set.
159-
if ((dir = getenv("CONFIGURATION_DIRECTORY")) != nullptr && strlen(dir) > 0) {
159+
if (get_env("CONFIGURATION_DIRECTORY", dir) && !dir.empty()) {
160160
found = true;
161161
config_path = fs::path(dir) / "sunshine"sv;
162162
}
163163
// Otherwise, follow the XDG base directory specification:
164164
// https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html
165-
if (!found && (dir = getenv("XDG_CONFIG_HOME")) != nullptr && strlen(dir) > 0) {
165+
if (!found && get_env("XDG_CONFIG_HOME", dir) && !dir.empty()) {
166166
found = true;
167167
config_path = fs::path(dir) / "sunshine"sv;
168168
}
@@ -173,8 +173,7 @@ namespace platf {
173173
}
174174

175175
// migrate from the old config location if necessary
176-
migrate_envvar = getenv("SUNSHINE_MIGRATE_CONFIG");
177-
if (migrate_config && found && migrate_envvar && strcmp(migrate_envvar, "1") == 0) {
176+
if (migrate_config && found && get_env("SUNSHINE_MIGRATE_CONFIG", migrate_envvar) && (migrate_envvar == "1")) {
178177
std::error_code ec;
179178
fs::path old_config_path = fs::path(homedir) / ".config/sunshine"sv;
180179
if (old_config_path != config_path && fs::exists(old_config_path, ec)) {
@@ -322,7 +321,9 @@ namespace platf {
322321
*/
323322
void open_url(const std::string &url) {
324323
// set working dir to user home directory
325-
auto working_dir = boost::filesystem::path(std::getenv("HOME"));
324+
std::string homedir;
325+
get_env("HOME", homedir);
326+
auto working_dir = boost::filesystem::path(homedir);
326327
std::string cmd = R"(xdg-open ")" + url + R"(")";
327328

328329
boost::process::v1::environment _env = boost::this_process::environment();
@@ -445,24 +446,19 @@ namespace platf {
445446
lifetime::exit_sunshine(0, true);
446447
}
447448

448-
std::string get_env(const std::string &name) {
449-
if (const auto value = getenv(name.c_str()); value != nullptr) {
450-
return value;
449+
bool get_env(const std::string &name, std::string &value) {
450+
const auto value_ = getenv(name.c_str()); // NOSONAR(cpp:S1874) - _dupenv_s is only available on WIN32
451+
if (value_ == nullptr) {
452+
return false;
451453
}
452-
return "";
454+
value = std::string(value_);
455+
return true;
453456
}
454457

455458
int set_env(const std::string &name, const std::string &value) {
456459
return setenv(name.c_str(), value.c_str(), 1);
457460
}
458461

459-
int append_env(const std::string &name, const std::string &value, const std::string &separator) {
460-
if (const std::string old_value = get_env(name); !old_value.contains(value)) {
461-
return set_env(name, old_value.empty() ? value : old_value + separator + value);
462-
}
463-
return 0;
464-
}
465-
466462
int unset_env(const std::string &name) {
467463
return unsetenv(name.c_str());
468464
}
@@ -1152,13 +1148,13 @@ namespace platf {
11521148

11531149
window_system = window_system_e::NONE;
11541150
#ifdef SUNSHINE_BUILD_WAYLAND
1155-
if (std::getenv("WAYLAND_DISPLAY")) {
1151+
if (std::string v; get_env("WAYLAND_DISPLAY", v)) {
11561152
window_system = window_system_e::WAYLAND;
11571153
}
11581154
#endif
11591155
#if defined(SUNSHINE_BUILD_X11) || defined(SUNSHINE_BUILD_CUDA)
1160-
if (std::getenv("DISPLAY") && window_system != window_system_e::WAYLAND) {
1161-
if (std::getenv("WAYLAND_DISPLAY")) {
1156+
if (std::string v; get_env("DISPLAY", v) && window_system != window_system_e::WAYLAND) {
1157+
if (get_env("WAYLAND_DISPLAY", v)) {
11621158
BOOST_LOG(warning) << "Wayland detected, yet sunshine will use X11 for screencasting, screencasting will only work on XWayland applications"sv;
11631159
}
11641160

src/platform/linux/wayland.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,9 @@ namespace wl {
5050

5151
int display_t::init(const char *display_name) {
5252
if (!display_name) {
53-
display_name = std::getenv("WAYLAND_DISPLAY");
53+
if (std::string v; platf::get_env("WAYLAND_DISPLAY", v)) {
54+
display_name = v.c_str();
55+
}
5456
}
5557

5658
if (!display_name) {

src/platform/linux/wayland.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ namespace wl {
175175
public:
176176
/**
177177
* @brief Initialize display.
178-
* If display_name == nullptr -> display_name = std::getenv("WAYLAND_DISPLAY")
178+
* If display_name == nullptr -> Set display_name to value of environment variable "WAYLAND_DISPLAY"
179179
* @param display_name The name of the display.
180180
* @return 0 on success, -1 on failure.
181181
*/

0 commit comments

Comments
 (0)