From 39a3775f4870fb16b49fec24aa5fd506b17a0ea9 Mon Sep 17 00:00:00 2001 From: Tim Schumacher Date: Sat, 20 Aug 2022 17:01:53 +0200 Subject: [PATCH] Userland: Rely on a single authoritative source for the default `PATH` --- Userland/Applications/Terminal/main.cpp | 3 ++- Userland/DevTools/HackStudio/main.cpp | 2 +- Userland/Libraries/LibC/unistd.cpp | 3 ++- Userland/Libraries/LibCore/File.h | 4 ++++ Userland/Libraries/LibCore/System.cpp | 4 ++-- Userland/Services/TelnetServer/main.cpp | 3 ++- Userland/Shell/Shell.cpp | 2 +- 7 files changed, 14 insertions(+), 7 deletions(-) diff --git a/Userland/Applications/Terminal/main.cpp b/Userland/Applications/Terminal/main.cpp index 7e15a469fd..1866973673 100644 --- a/Userland/Applications/Terminal/main.cpp +++ b/Userland/Applications/Terminal/main.cpp @@ -11,6 +11,7 @@ #include #include #include +#include #include #include #include @@ -162,7 +163,7 @@ static ErrorOr run_command(String command, bool keep_open) arguments.append("-c"sv); arguments.append(command); } - auto env = TRY(FixedArray::try_create({ "TERM=xterm"sv, "PAGER=more"sv, "PATH=/usr/local/sbin:/usr/local/bin:/usr/bin:/bin"sv })); + auto env = TRY(FixedArray::try_create({ "TERM=xterm"sv, "PAGER=more"sv, "PATH="sv DEFAULT_PATH_SV })); TRY(Core::System::exec(shell, arguments, Core::System::SearchInPath::No, env.span())); VERIFY_NOT_REACHED(); } diff --git a/Userland/DevTools/HackStudio/main.cpp b/Userland/DevTools/HackStudio/main.cpp index 987558ec27..14900079a8 100644 --- a/Userland/DevTools/HackStudio/main.cpp +++ b/Userland/DevTools/HackStudio/main.cpp @@ -128,7 +128,7 @@ static void update_path_environment_variable() if (path.length()) path.append(':'); - path.append("/usr/local/sbin:/usr/local/bin:/usr/bin:/bin"sv); + path.append(DEFAULT_PATH_SV); setenv("PATH", path.to_string().characters(), true); } diff --git a/Userland/Libraries/LibC/unistd.cpp b/Userland/Libraries/LibC/unistd.cpp index 78dcb42e5f..0e44c2df5a 100644 --- a/Userland/Libraries/LibC/unistd.cpp +++ b/Userland/Libraries/LibC/unistd.cpp @@ -8,6 +8,7 @@ #include #include #include +#include #include #include #include @@ -186,7 +187,7 @@ int execvpe(char const* filename, char* const argv[], char* const envp[]) ScopedValueRollback errno_rollback(errno); String path = getenv("PATH"); if (path.is_empty()) - path = "/bin:/usr/bin"; + path = DEFAULT_PATH; auto parts = path.split(':'); for (auto& part : parts) { auto candidate = String::formatted("{}/{}", part, filename); diff --git a/Userland/Libraries/LibCore/File.h b/Userland/Libraries/LibCore/File.h index 6037ff4c99..c331deaf66 100644 --- a/Userland/Libraries/LibCore/File.h +++ b/Userland/Libraries/LibCore/File.h @@ -11,6 +11,10 @@ #include #include +// FIXME: Make this a bit prettier. +#define DEFAULT_PATH "/usr/local/sbin:/usr/local/bin:/usr/bin:/bin" +#define DEFAULT_PATH_SV "/usr/local/sbin:/usr/local/bin:/usr/bin:/bin"sv + namespace Core { class File final : public IODevice { diff --git a/Userland/Libraries/LibCore/System.cpp b/Userland/Libraries/LibCore/System.cpp index e2f277cdcd..8ffcd93870 100644 --- a/Userland/Libraries/LibCore/System.cpp +++ b/Userland/Libraries/LibCore/System.cpp @@ -958,7 +958,7 @@ ErrorOr find_file_in_path(StringView filename) auto const* path_ptr = getenv("PATH"); StringView path { path_ptr, strlen(path_ptr) }; if (path.is_empty()) - path = "/bin:/usr/bin"sv; + path = DEFAULT_PATH_SV; auto parts = path.split_view(':'); for (auto& part : parts) { auto candidate = String::formatted("{}/{}", part, filename); @@ -1043,7 +1043,7 @@ ErrorOr exec(StringView filename, Span arguments, SearchInPath ScopedValueRollback errno_rollback(errno); String path = getenv("PATH"); if (path.is_empty()) - path = "/bin:/usr/bin"; + path = DEFAULT_PATH; auto parts = path.split(':'); for (auto& part : parts) { auto candidate = String::formatted("{}/{}", part, filename); diff --git a/Userland/Services/TelnetServer/main.cpp b/Userland/Services/TelnetServer/main.cpp index 33b81d304e..b210e34305 100644 --- a/Userland/Services/TelnetServer/main.cpp +++ b/Userland/Services/TelnetServer/main.cpp @@ -10,6 +10,7 @@ #include #include #include +#include #include #include #include @@ -71,7 +72,7 @@ static void run_command(int ptm_fd, String command) args[1] = "-c"; args[2] = command.characters(); } - char const* envs[] = { "TERM=xterm", "PATH=/usr/local/sbin:/usr/local/bin:/usr/bin:/bin", nullptr }; + char const* envs[] = { "TERM=xterm", "PATH=" DEFAULT_PATH, nullptr }; rc = execve("/bin/Shell", const_cast(args), const_cast(envs)); if (rc < 0) { perror("execve"); diff --git a/Userland/Shell/Shell.cpp b/Userland/Shell/Shell.cpp index b558e84b59..6a8fdc3d62 100644 --- a/Userland/Shell/Shell.cpp +++ b/Userland/Shell/Shell.cpp @@ -2189,7 +2189,7 @@ Shell::Shell() path.append({ path_env_ptr, strlen(path_env_ptr) }); if (path.length()) path.append(":"sv); - path.append("/usr/local/sbin:/usr/local/bin:/usr/bin:/bin"sv); + path.append(DEFAULT_PATH_SV); setenv("PATH", path.to_string().characters(), true); }