2022-07-14 22:49:26 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2022, Andreas Kling <kling@serenityos.org>
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
*/
|
|
|
|
|
2023-05-29 23:20:05 +03:00
|
|
|
#include <AK/String.h>
|
2022-07-14 22:49:26 +03:00
|
|
|
#include <LibCore/ArgsParser.h>
|
2022-07-16 17:44:25 +03:00
|
|
|
#include <LibCore/MappedFile.h>
|
2022-07-14 22:49:26 +03:00
|
|
|
#include <LibCore/System.h>
|
2022-07-16 17:44:25 +03:00
|
|
|
#include <LibELF/Image.h>
|
2022-07-14 22:49:26 +03:00
|
|
|
#include <LibMain/Main.h>
|
|
|
|
|
2022-07-16 17:44:25 +03:00
|
|
|
static ErrorOr<bool> is_dynamically_linked_executable(StringView filename)
|
|
|
|
{
|
2023-05-29 23:20:05 +03:00
|
|
|
auto executable = TRY(Core::System::resolve_executable_from_environment(filename));
|
2023-05-13 01:10:52 +03:00
|
|
|
auto file = TRY(Core::MappedFile::map(executable));
|
2022-07-16 17:44:25 +03:00
|
|
|
ELF::Image elf_image(file->bytes());
|
|
|
|
return elf_image.is_dynamic();
|
|
|
|
}
|
|
|
|
|
2022-07-14 22:49:26 +03:00
|
|
|
ErrorOr<int> serenity_main(Main::Arguments arguments)
|
|
|
|
{
|
2022-12-04 21:02:33 +03:00
|
|
|
DeprecatedString promises;
|
2022-07-14 22:49:26 +03:00
|
|
|
Vector<StringView> command;
|
2022-07-16 17:44:25 +03:00
|
|
|
bool add_promises_for_dynamic_linker;
|
2022-07-14 22:49:26 +03:00
|
|
|
|
|
|
|
Core::ArgsParser args_parser;
|
|
|
|
args_parser.add_option(promises, "Space-separated list of pledge promises", "promises", 'p', "promises");
|
2022-07-16 17:44:25 +03:00
|
|
|
args_parser.add_option(add_promises_for_dynamic_linker, "Add temporary promises for dynamic linker", "dynamic-linker-promises", 'd');
|
2022-07-14 22:49:26 +03:00
|
|
|
args_parser.add_positional_argument(command, "Command to execute", "command");
|
|
|
|
args_parser.parse(arguments);
|
|
|
|
|
2022-07-16 17:44:25 +03:00
|
|
|
if (add_promises_for_dynamic_linker && TRY(is_dynamically_linked_executable(command[0]))) {
|
|
|
|
auto constexpr loader_promises = "stdio rpath prot_exec"sv;
|
|
|
|
MUST(Core::System::setenv("_LOADER_PLEDGE_PROMISES"sv, loader_promises, true));
|
|
|
|
MUST(Core::System::setenv("_LOADER_MAIN_PROGRAM_PLEDGE_PROMISES"sv, promises, true));
|
2022-12-04 21:02:33 +03:00
|
|
|
promises = DeprecatedString::formatted("{} {}", promises, loader_promises);
|
2022-07-16 17:44:25 +03:00
|
|
|
}
|
|
|
|
|
2022-07-14 22:49:26 +03:00
|
|
|
TRY(Core::System::pledge(StringView(), promises));
|
|
|
|
TRY(Core::System::exec(command[0], command.span(), Core::System::SearchInPath::Yes));
|
|
|
|
return 0;
|
|
|
|
}
|