| 59 | } |
| 60 | |
| 61 | BAN::ErrorOr<Execute::ExecuteResult> Execute::execute_command_no_wait(const InternalCommand& command) |
| 62 | { |
| 63 | ASSERT(!command.arguments.empty()); |
| 64 | |
| 65 | if (command.command.has<Builtin::BuiltinCommand>() && !command.background) |
| 66 | { |
| 67 | const auto& builtin = command.command.get<Builtin::BuiltinCommand>(); |
| 68 | if (builtin.immediate) |
| 69 | { |
| 70 | return ExecuteResult { |
| 71 | .pid = -1, |
| 72 | .exit_code = TRY(builtin.execute(*this, command.arguments, command.fd_in, command.fd_out)) |
| 73 | }; |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | const pid_t child_pid = fork(); |
| 78 | if (child_pid == -1) |
| 79 | return BAN::Error::from_errno(errno); |
| 80 | if (child_pid == 0) |
| 81 | { |
| 82 | if (command.command.has<Builtin::BuiltinCommand>()) |
| 83 | { |
| 84 | for (int sig = 1; sig <= NSIG; sig++) |
| 85 | signal(sig, SIG_DFL); |
| 86 | |
| 87 | auto builtin_ret = command.command.get<Builtin::BuiltinCommand>().execute(*this, command.arguments, command.fd_in, command.fd_out); |
| 88 | if (builtin_ret.is_error()) |
| 89 | exit(builtin_ret.error().get_error_code()); |
| 90 | exit(builtin_ret.value()); |
| 91 | } |
| 92 | |
| 93 | for (const auto& environment : command.environments) |
| 94 | setenv(environment.name.data(), environment.value.data(), true); |
| 95 | |
| 96 | BAN::Vector<const char*> exec_args; |
| 97 | TRY_OR_EXIT(exec_args.reserve(command.arguments.size() + 1)); |
| 98 | for (const auto& argument : command.arguments) |
| 99 | TRY_OR_EXIT(exec_args.push_back(argument.data())); |
| 100 | TRY_OR_EXIT(exec_args.push_back(nullptr)); |
| 101 | |
| 102 | CHECK_FD_OR_PERROR_AND_EXIT(command.fd_in, STDIN_FILENO); |
| 103 | CHECK_FD_OR_PERROR_AND_EXIT(command.fd_out, STDOUT_FILENO); |
| 104 | |
| 105 | for (const auto& redirection : command.redirections) |
| 106 | { |
| 107 | int dst_fd = -1; |
| 108 | |
| 109 | if (redirection.duplicate) |
| 110 | { |
| 111 | if (!redirection.path.empty()) |
| 112 | { |
| 113 | dst_fd = 0; |
| 114 | |
| 115 | for (char ch : redirection.path) |
| 116 | { |
| 117 | if (!isdigit(ch)) |
| 118 | { |
nothing calls this directly
no test coverage detected