mirror of
https://github.com/RGBCube/serenity
synced 2025-05-20 13:35:07 +00:00
Shell: Implement a very basic exec builtin
Other shells also support a number of other options with exec and some have special behaviour when calling exec with no arguments except redirections. This PR only supports the basic case of replacing the Shell process (or LibShell host process) with the provided command.
This commit is contained in:
parent
50d24e4f98
commit
96cd04f2ba
3 changed files with 59 additions and 36 deletions
|
@ -789,42 +789,7 @@ RefPtr<Job> Shell::run_command(const AST::Command& command)
|
|||
// We no longer need the jobs here.
|
||||
jobs.clear();
|
||||
|
||||
int rc = execvp(argv[0], const_cast<char* const*>(argv.data()));
|
||||
if (rc < 0) {
|
||||
int saved_errno = errno;
|
||||
struct stat st;
|
||||
if (stat(argv[0], &st)) {
|
||||
fprintf(stderr, "stat(%s): %s\n", argv[0], strerror(errno));
|
||||
_exit(126);
|
||||
}
|
||||
if (!(st.st_mode & S_IXUSR)) {
|
||||
fprintf(stderr, "%s: Not executable\n", argv[0]);
|
||||
_exit(126);
|
||||
}
|
||||
if (saved_errno == ENOENT) {
|
||||
int shebang_fd = open(argv[0], O_RDONLY);
|
||||
auto close_argv = ScopeGuard([shebang_fd]() { if (shebang_fd >= 0) close(shebang_fd); });
|
||||
char shebang[256] {};
|
||||
ssize_t num_read = -1;
|
||||
if ((shebang_fd >= 0) && ((num_read = read(shebang_fd, shebang, sizeof(shebang))) >= 2) && (StringView(shebang).starts_with("#!"))) {
|
||||
StringView shebang_path_view(&shebang[2], num_read - 2);
|
||||
Optional<size_t> newline_pos = shebang_path_view.find_first_of("\n\r");
|
||||
shebang[newline_pos.has_value() ? (newline_pos.value() + 2) : num_read] = '\0';
|
||||
argv[0] = shebang;
|
||||
int rc = execvp(argv[0], const_cast<char* const*>(argv.data()));
|
||||
if (rc < 0)
|
||||
fprintf(stderr, "%s: Invalid interpreter \"%s\": %s\n", argv[0], &shebang[2], strerror(errno));
|
||||
} else
|
||||
fprintf(stderr, "%s: Command not found.\n", argv[0]);
|
||||
} else {
|
||||
if (S_ISDIR(st.st_mode)) {
|
||||
fprintf(stderr, "Shell: %s: Is a directory\n", argv[0]);
|
||||
_exit(126);
|
||||
}
|
||||
fprintf(stderr, "execvp(%s): %s\n", argv[0], strerror(saved_errno));
|
||||
}
|
||||
_exit(126);
|
||||
}
|
||||
execute_process(move(argv));
|
||||
ASSERT_NOT_REACHED();
|
||||
}
|
||||
|
||||
|
@ -893,6 +858,47 @@ RefPtr<Job> Shell::run_command(const AST::Command& command)
|
|||
return *job;
|
||||
}
|
||||
|
||||
void Shell::execute_process(Vector<const char*>&& argv)
|
||||
{
|
||||
int rc = execvp(argv[0], const_cast<char* const*>(argv.data()));
|
||||
if (rc < 0) {
|
||||
int saved_errno = errno;
|
||||
struct stat st;
|
||||
if (stat(argv[0], &st)) {
|
||||
fprintf(stderr, "stat(%s): %s\n", argv[0], strerror(errno));
|
||||
_exit(126);
|
||||
}
|
||||
if (!(st.st_mode & S_IXUSR)) {
|
||||
fprintf(stderr, "%s: Not executable\n", argv[0]);
|
||||
_exit(126);
|
||||
}
|
||||
if (saved_errno == ENOENT) {
|
||||
int shebang_fd = open(argv[0], O_RDONLY);
|
||||
auto close_argv = ScopeGuard([shebang_fd]() { if (shebang_fd >= 0) close(shebang_fd); });
|
||||
char shebang[256] {};
|
||||
ssize_t num_read = -1;
|
||||
if ((shebang_fd >= 0) && ((num_read = read(shebang_fd, shebang, sizeof(shebang))) >= 2) && (StringView(shebang).starts_with("#!"))) {
|
||||
StringView shebang_path_view(&shebang[2], num_read - 2);
|
||||
Optional<size_t> newline_pos = shebang_path_view.find_first_of("\n\r");
|
||||
shebang[newline_pos.has_value() ? (newline_pos.value() + 2) : num_read] = '\0';
|
||||
argv[0] = shebang;
|
||||
int rc = execvp(argv[0], const_cast<char* const*>(argv.data()));
|
||||
if (rc < 0)
|
||||
fprintf(stderr, "%s: Invalid interpreter \"%s\": %s\n", argv[0], &shebang[2], strerror(errno));
|
||||
} else
|
||||
fprintf(stderr, "%s: Command not found.\n", argv[0]);
|
||||
} else {
|
||||
if (S_ISDIR(st.st_mode)) {
|
||||
fprintf(stderr, "Shell: %s: Is a directory\n", argv[0]);
|
||||
_exit(126);
|
||||
}
|
||||
fprintf(stderr, "execvp(%s): %s\n", argv[0], strerror(saved_errno));
|
||||
}
|
||||
_exit(126);
|
||||
}
|
||||
ASSERT_NOT_REACHED();
|
||||
}
|
||||
|
||||
void Shell::run_tail(const AST::Command& invoking_command, const AST::NodeWithAction& next_in_chain, int head_exit_code)
|
||||
{
|
||||
if (m_error != ShellError::None) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue