diff --git a/Base/usr/share/man/man1/jail-attach.md b/Base/usr/share/man/man1/jail-attach.md index 7c8cf5b826..0d23feb790 100644 --- a/Base/usr/share/man/man1/jail-attach.md +++ b/Base/usr/share/man/man1/jail-attach.md @@ -13,9 +13,19 @@ $ jail-attach `jail-attach` attaches a new process by specifying a command, to an existing jail, with a specified jail index. +## Options + +* `-i`, `--jail-index`: Use an already existing jail with its index +* `-n`, `--jail-name`: Create a new jail with a provided name + ## Examples ```sh -# Attach the command "ps -ef" to a jail with the index 0 -$ jail-attach 0 ps -ef +# Attach the command "ps -ef" to an already existing jail with the index 0 +$ jail-attach -i 0 ps -ef +``` + +```sh +# Attach the command "/bin/Shell" to a new jail with the name "test jail" +$ jail-attach -n "test jail" /bin/Shell ``` 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 serenity_main(Main::Arguments arguments) { - unsigned jail_index = 0; + StringView new_jail_name; Vector command; + Optional 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; }