Userland: Rely on a single authoritative source for the default PATH

This commit is contained in:
Tim Schumacher 2022-08-20 17:01:53 +02:00 committed by Linus Groh
parent 61e18c681b
commit 39a3775f48
Notes: sideshowbarker 2024-07-17 08:59:18 +09:00
7 changed files with 14 additions and 7 deletions

View file

@ -11,6 +11,7 @@
#include <LibConfig/Listener.h>
#include <LibCore/ArgsParser.h>
#include <LibCore/DirIterator.h>
#include <LibCore/File.h>
#include <LibCore/System.h>
#include <LibDesktop/Launcher.h>
#include <LibGUI/Action.h>
@ -162,7 +163,7 @@ static ErrorOr<void> run_command(String command, bool keep_open)
arguments.append("-c"sv);
arguments.append(command);
}
auto env = TRY(FixedArray<StringView>::try_create({ "TERM=xterm"sv, "PAGER=more"sv, "PATH=/usr/local/sbin:/usr/local/bin:/usr/bin:/bin"sv }));
auto env = TRY(FixedArray<StringView>::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();
}

View file

@ -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);
}

View file

@ -8,6 +8,7 @@
#include <AK/ScopedValueRollback.h>
#include <AK/String.h>
#include <AK/Vector.h>
#include <LibCore/File.h>
#include <alloca.h>
#include <assert.h>
#include <bits/pthread_cancel.h>
@ -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);

View file

@ -11,6 +11,10 @@
#include <LibCore/IODevice.h>
#include <sys/stat.h>
// 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 {

View file

@ -958,7 +958,7 @@ ErrorOr<String> 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<void> exec(StringView filename, Span<StringView> 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);

View file

@ -10,6 +10,7 @@
#include <AK/Types.h>
#include <LibCore/ArgsParser.h>
#include <LibCore/EventLoop.h>
#include <LibCore/File.h>
#include <LibCore/TCPServer.h>
#include <LibMain/Main.h>
#include <fcntl.h>
@ -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<char**>(args), const_cast<char**>(envs));
if (rc < 0) {
perror("execve");

View file

@ -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);
}