diff options
author | Liav A <liavalb@gmail.com> | 2022-11-11 11:05:17 +0200 |
---|---|---|
committer | Andrew Kaster <andrewdkaster@gmail.com> | 2022-11-26 01:47:50 -0700 |
commit | 9f571e0dae0b98cd4b365a88a8bc03295645f045 (patch) | |
tree | 160a4bba22d462b3df7c62f0f7c39079d2716dca /Userland/Utilities | |
parent | 1adc3530b22cec4def8da157ad8181ee4d756408 (diff) | |
download | serenity-9f571e0dae0b98cd4b365a88a8bc03295645f045.zip |
Userland: Enhance jail-attach utility to support existing and new jails
The Core::System::create_jail function already provided the new jail
index as a result, so it was just a matter of using it when calling the
LibCore join_jail function to use the new jail.
Diffstat (limited to 'Userland/Utilities')
-rw-r--r-- | Userland/Utilities/jail-attach.cpp | 17 |
1 files changed, 14 insertions, 3 deletions
diff --git a/Userland/Utilities/jail-attach.cpp b/Userland/Utilities/jail-attach.cpp index ba09dafc49..781359fcaa 100644 --- a/Userland/Utilities/jail-attach.cpp +++ b/Userland/Utilities/jail-attach.cpp @@ -11,18 +11,29 @@ ErrorOr<int> serenity_main(Main::Arguments arguments) { - unsigned jail_index = 0; + StringView new_jail_name; Vector<StringView> command; + Optional<size_t> existing_jail_index; Core::ArgsParser args_parser; bool preserve_env = false; args_parser.set_stop_on_first_non_option(true); args_parser.add_option(preserve_env, "Preserve user environment when running command", "preserve-env", 'E'); - args_parser.add_positional_argument(jail_index, "Jail Index", "jail index"); + args_parser.add_option(new_jail_name, "Create a new jail with a name", "jail-name", 'n', "New jail name"); + args_parser.add_option(existing_jail_index, "Use an existing jail index instead of creating new jail", "jail-index", 'i', "Existing jail index"); args_parser.add_positional_argument(command, "Command to execute", "command"); args_parser.parse(arguments); TRY(Core::System::pledge("stdio rpath exec id jail tty")); - TRY(Core::System::join_jail(jail_index)); + + if (existing_jail_index.has_value() && !new_jail_name.is_null()) + return Error::from_string_view("Can't launch process in a new jail with a name and use an existing jail index."sv); + + if (existing_jail_index.has_value()) { + TRY(Core::System::join_jail(existing_jail_index.value())); + } else { + u64 new_jail_index = TRY(Core::System::create_jail(new_jail_name.is_null() ? ""sv : new_jail_name)); + TRY(Core::System::join_jail(new_jail_index)); + } TRY(Core::System::exec_command(command, preserve_env)); return 0; } |