2019-05-30 15:12:09 +02:00
|
|
|
#include "GlobalState.h"
|
|
|
|
#include "LineEditor.h"
|
|
|
|
#include "Parser.h"
|
|
|
|
#include <AK/FileSystemPath.h>
|
|
|
|
#include <LibCore/CDirIterator.h>
|
|
|
|
#include <LibCore/CElapsedTimer.h>
|
2018-11-09 10:18:04 +01:00
|
|
|
#include <errno.h>
|
2019-05-30 15:12:09 +02:00
|
|
|
#include <fcntl.h>
|
2018-11-09 10:18:04 +01:00
|
|
|
#include <pwd.h>
|
2018-11-06 10:46:40 +01:00
|
|
|
#include <signal.h>
|
2018-11-09 10:24:41 +01:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/mman.h>
|
2019-02-08 02:38:21 +01:00
|
|
|
#include <sys/stat.h>
|
2018-11-09 10:24:41 +01:00
|
|
|
#include <sys/utsname.h>
|
2019-05-30 15:12:09 +02:00
|
|
|
#include <sys/wait.h>
|
|
|
|
#include <termios.h>
|
|
|
|
#include <unistd.h>
|
2018-10-26 14:24:11 +02:00
|
|
|
|
2019-04-25 16:10:16 +02:00
|
|
|
//#define SH_DEBUG
|
|
|
|
|
2019-05-07 01:39:10 +02:00
|
|
|
GlobalState g;
|
2019-05-07 01:43:21 +02:00
|
|
|
static LineEditor editor;
|
2018-10-23 10:12:50 +02:00
|
|
|
|
|
|
|
static void prompt()
|
|
|
|
{
|
2019-05-07 01:39:10 +02:00
|
|
|
if (g.uid == 0)
|
2018-10-23 10:12:50 +02:00
|
|
|
printf("# ");
|
2019-01-25 05:51:39 +01:00
|
|
|
else {
|
2019-05-07 01:39:10 +02:00
|
|
|
printf("\033]0;%s@%s:%s\007", g.username.characters(), g.hostname, g.cwd.characters());
|
|
|
|
printf("\033[31;1m%s\033[0m@\033[37;1m%s\033[0m:\033[32;1m%s\033[0m$> ", g.username.characters(), g.hostname, g.cwd.characters());
|
2019-01-25 05:51:39 +01:00
|
|
|
}
|
2018-11-08 01:23:23 +01:00
|
|
|
fflush(stdout);
|
2018-10-23 10:12:50 +02:00
|
|
|
}
|
|
|
|
|
2019-01-23 17:03:14 +01:00
|
|
|
static int sh_pwd(int, char**)
|
2018-10-26 14:24:11 +02:00
|
|
|
{
|
2019-05-07 01:39:10 +02:00
|
|
|
printf("%s\n", g.cwd.characters());
|
2018-10-27 01:24:22 +02:00
|
|
|
return 0;
|
2018-10-26 14:24:11 +02:00
|
|
|
}
|
|
|
|
|
2018-11-07 19:03:44 +01:00
|
|
|
static volatile bool g_got_signal = false;
|
|
|
|
|
2018-11-06 10:46:40 +01:00
|
|
|
void did_receive_signal(int signum)
|
|
|
|
{
|
|
|
|
printf("\nMy word, I've received a signal with number %d\n", signum);
|
2018-11-07 19:03:44 +01:00
|
|
|
g_got_signal = true;
|
|
|
|
}
|
|
|
|
|
2018-11-09 10:18:04 +01:00
|
|
|
void handle_sigint(int)
|
2018-11-07 19:03:44 +01:00
|
|
|
{
|
2019-05-07 01:39:10 +02:00
|
|
|
g.was_interrupted = true;
|
2018-11-06 10:46:40 +01:00
|
|
|
}
|
|
|
|
|
2019-01-23 17:03:14 +01:00
|
|
|
static int sh_exit(int, char**)
|
2018-10-28 10:26:07 +01:00
|
|
|
{
|
|
|
|
printf("Good-bye!\n");
|
|
|
|
exit(0);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-04-26 03:23:27 +02:00
|
|
|
static int sh_export(int argc, char** argv)
|
|
|
|
{
|
|
|
|
if (argc == 1) {
|
|
|
|
for (int i = 0; environ[i]; ++i)
|
|
|
|
puts(environ[i]);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
auto parts = String(argv[1]).split('=');
|
|
|
|
if (parts.size() != 2) {
|
|
|
|
fprintf(stderr, "usage: export variable=value\n");
|
|
|
|
return 1;
|
|
|
|
}
|
2019-05-16 13:04:47 +02:00
|
|
|
|
2019-05-30 02:51:15 +02:00
|
|
|
return setenv(parts[0].characters(), parts[1].characters(), 1);
|
2019-04-26 03:23:27 +02:00
|
|
|
}
|
|
|
|
|
2019-05-16 14:22:12 +02:00
|
|
|
static int sh_unset(int argc, char** argv)
|
|
|
|
{
|
|
|
|
if (argc != 2) {
|
|
|
|
fprintf(stderr, "usage: unset variable\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsetenv(argv[1]);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-01-23 17:03:14 +01:00
|
|
|
static int sh_cd(int argc, char** argv)
|
2018-10-26 14:24:11 +02:00
|
|
|
{
|
2019-04-25 23:09:21 +02:00
|
|
|
char pathbuf[PATH_MAX];
|
|
|
|
|
2018-10-26 14:24:11 +02:00
|
|
|
if (argc == 1) {
|
2019-05-07 01:39:10 +02:00
|
|
|
strcpy(pathbuf, g.home.characters());
|
2019-04-25 23:09:21 +02:00
|
|
|
} else {
|
|
|
|
if (argv[1][0] == '/')
|
|
|
|
memcpy(pathbuf, argv[1], strlen(argv[1]) + 1);
|
|
|
|
else
|
2019-05-07 01:39:10 +02:00
|
|
|
sprintf(pathbuf, "%s/%s", g.cwd.characters(), argv[1]);
|
2018-10-26 14:24:11 +02:00
|
|
|
}
|
|
|
|
|
2019-01-31 17:31:23 +01:00
|
|
|
FileSystemPath canonical_path(pathbuf);
|
|
|
|
if (!canonical_path.is_valid()) {
|
2018-10-28 09:36:21 +01:00
|
|
|
printf("FileSystemPath failed to canonicalize '%s'\n", pathbuf);
|
|
|
|
return 1;
|
|
|
|
}
|
2019-01-31 17:31:23 +01:00
|
|
|
const char* path = canonical_path.string().characters();
|
2018-10-28 09:36:21 +01:00
|
|
|
|
2018-10-26 14:24:11 +02:00
|
|
|
struct stat st;
|
2018-11-10 18:16:21 +01:00
|
|
|
int rc = stat(path, &st);
|
2018-10-26 14:24:11 +02:00
|
|
|
if (rc < 0) {
|
2019-05-31 06:07:09 +02:00
|
|
|
printf("stat(%s) failed: %s\n", path, strerror(errno));
|
2018-10-26 14:24:11 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if (!S_ISDIR(st.st_mode)) {
|
2018-10-28 09:36:21 +01:00
|
|
|
printf("Not a directory: %s\n", path);
|
2018-10-26 14:24:11 +02:00
|
|
|
return 1;
|
|
|
|
}
|
2018-10-28 09:36:21 +01:00
|
|
|
rc = chdir(path);
|
2018-10-26 14:24:11 +02:00
|
|
|
if (rc < 0) {
|
2018-10-28 09:36:21 +01:00
|
|
|
printf("chdir(%s) failed: %s\n", path, strerror(errno));
|
2018-10-26 14:24:11 +02:00
|
|
|
return 1;
|
|
|
|
}
|
2019-05-07 01:39:10 +02:00
|
|
|
g.cwd = canonical_path.string();
|
2018-10-26 14:24:11 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-05-07 01:43:21 +02:00
|
|
|
static int sh_history(int, char**)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < editor.history().size(); ++i) {
|
|
|
|
printf("%6d %s\n", i, editor.history()[i].characters());
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-05-10 17:39:30 +02:00
|
|
|
static int sh_umask(int argc, char** argv)
|
|
|
|
{
|
|
|
|
if (argc == 1) {
|
|
|
|
mode_t old_mask = umask(0);
|
|
|
|
printf("%#o\n", old_mask);
|
|
|
|
umask(old_mask);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (argc == 2) {
|
|
|
|
unsigned mask;
|
|
|
|
int matches = sscanf(argv[1], "%o", &mask);
|
|
|
|
if (matches == 1) {
|
|
|
|
umask(mask);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
printf("usage: umask <octal-mask>\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-01-23 17:03:14 +01:00
|
|
|
static bool handle_builtin(int argc, char** argv, int& retval)
|
2018-10-26 14:24:11 +02:00
|
|
|
{
|
|
|
|
if (argc == 0)
|
|
|
|
return false;
|
|
|
|
if (!strcmp(argv[0], "cd")) {
|
|
|
|
retval = sh_cd(argc, argv);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (!strcmp(argv[0], "pwd")) {
|
|
|
|
retval = sh_pwd(argc, argv);
|
|
|
|
return true;
|
|
|
|
}
|
2018-10-28 10:26:07 +01:00
|
|
|
if (!strcmp(argv[0], "exit")) {
|
|
|
|
retval = sh_exit(argc, argv);
|
|
|
|
return true;
|
|
|
|
}
|
2019-04-26 03:23:27 +02:00
|
|
|
if (!strcmp(argv[0], "export")) {
|
|
|
|
retval = sh_export(argc, argv);
|
|
|
|
return true;
|
|
|
|
}
|
2019-05-16 14:22:12 +02:00
|
|
|
if (!strcmp(argv[0], "unset")) {
|
|
|
|
retval = sh_unset(argc, argv);
|
|
|
|
return true;
|
|
|
|
}
|
2019-05-07 01:43:21 +02:00
|
|
|
if (!strcmp(argv[0], "history")) {
|
|
|
|
retval = sh_history(argc, argv);
|
|
|
|
return true;
|
|
|
|
}
|
2019-05-10 17:39:30 +02:00
|
|
|
if (!strcmp(argv[0], "umask")) {
|
|
|
|
retval = sh_umask(argc, argv);
|
|
|
|
return true;
|
|
|
|
}
|
2018-10-26 14:24:11 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-04-25 16:10:16 +02:00
|
|
|
class FileDescriptorCollector {
|
|
|
|
public:
|
|
|
|
FileDescriptorCollector() { }
|
|
|
|
~FileDescriptorCollector() { collect(); }
|
|
|
|
|
|
|
|
void collect()
|
|
|
|
{
|
|
|
|
for (auto fd : m_fds)
|
|
|
|
close(fd);
|
|
|
|
m_fds.clear();
|
|
|
|
}
|
|
|
|
void add(int fd) { m_fds.append(fd); }
|
|
|
|
|
|
|
|
private:
|
|
|
|
Vector<int, 32> m_fds;
|
|
|
|
};
|
|
|
|
|
2019-05-02 02:22:28 +02:00
|
|
|
struct CommandTimer {
|
|
|
|
CommandTimer()
|
|
|
|
{
|
|
|
|
timer.start();
|
|
|
|
}
|
|
|
|
~CommandTimer()
|
|
|
|
{
|
|
|
|
dbgprintf("sh: command finished in %d ms\n", timer.elapsed());
|
|
|
|
}
|
|
|
|
|
|
|
|
CElapsedTimer timer;
|
|
|
|
};
|
|
|
|
|
2019-05-26 20:36:16 +02:00
|
|
|
static Vector<String> process_arguments(const Vector<String>& args)
|
|
|
|
{
|
|
|
|
Vector<String> argv_string;
|
|
|
|
for (auto& arg : args) {
|
|
|
|
bool is_glob = false;
|
|
|
|
for (int i = 0; i < arg.length(); i++) {
|
|
|
|
char c = arg.characters()[i];
|
|
|
|
if (c == '*' || c == '?') {
|
|
|
|
is_glob = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (is_glob == false) {
|
|
|
|
argv_string.append(arg.characters());
|
|
|
|
} else {
|
|
|
|
CDirIterator di(".", CDirIterator::NoFlags);
|
|
|
|
if (di.has_error()) {
|
|
|
|
fprintf(stderr, "CDirIterator: %s\n", di.error_string());
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (di.has_next()) {
|
|
|
|
String name = di.next_path();
|
|
|
|
|
|
|
|
// Dotfiles have to be explicitly requested
|
|
|
|
if (name[0] == '.' && arg[0] != '.')
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// And even if they are, skip . and ..
|
|
|
|
if (name == "." || name == "..")
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (name.matches(arg, String::CaseSensitivity::CaseSensitive))
|
|
|
|
argv_string.append(name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return argv_string;
|
|
|
|
}
|
|
|
|
|
2019-05-07 01:01:50 +02:00
|
|
|
static int run_command(const String& cmd)
|
2018-10-23 10:12:50 +02:00
|
|
|
{
|
2019-05-07 01:01:50 +02:00
|
|
|
if (cmd.is_empty())
|
2018-10-25 12:06:00 +02:00
|
|
|
return 0;
|
2018-10-26 11:16:56 +02:00
|
|
|
|
2019-04-25 14:33:47 +02:00
|
|
|
auto subcommands = Parser(cmd).parse();
|
2019-04-25 14:13:36 +02:00
|
|
|
|
2019-04-25 16:10:16 +02:00
|
|
|
#ifdef SH_DEBUG
|
2019-04-25 14:13:36 +02:00
|
|
|
for (int i = 0; i < subcommands.size(); ++i) {
|
|
|
|
for (int j = 0; j < i; ++j)
|
2019-05-26 00:38:11 +02:00
|
|
|
dbgprintf(" ");
|
2019-04-25 14:13:36 +02:00
|
|
|
for (auto& arg : subcommands[i].args) {
|
2019-05-26 00:38:11 +02:00
|
|
|
dbgprintf("<%s> ", arg.characters());
|
2018-10-26 11:16:56 +02:00
|
|
|
}
|
2019-05-26 00:38:11 +02:00
|
|
|
dbgprintf("\n");
|
2019-04-25 16:10:16 +02:00
|
|
|
for (auto& redirecton : subcommands[i].redirections) {
|
|
|
|
for (int j = 0; j < i; ++j)
|
2019-05-26 00:38:11 +02:00
|
|
|
dbgprintf(" ");
|
|
|
|
dbgprintf(" ");
|
2019-04-25 16:10:16 +02:00
|
|
|
switch (redirecton.type) {
|
|
|
|
case Redirection::Pipe:
|
2019-05-26 00:38:11 +02:00
|
|
|
dbgprintf("Pipe\n");
|
2019-04-25 16:10:16 +02:00
|
|
|
break;
|
|
|
|
case Redirection::FileRead:
|
2019-05-26 00:38:11 +02:00
|
|
|
dbgprintf("fd:%d = FileRead: %s\n", redirecton.fd, redirecton.path.characters());
|
2019-04-25 16:10:16 +02:00
|
|
|
break;
|
|
|
|
case Redirection::FileWrite:
|
2019-05-26 00:38:11 +02:00
|
|
|
dbgprintf("fd:%d = FileWrite: %s\n", redirecton.fd, redirecton.path.characters());
|
|
|
|
break;
|
|
|
|
case Redirection::FileWriteAppend:
|
|
|
|
dbgprintf("fd:%d = FileWriteAppend: %s\n", redirecton.fd, redirecton.path.characters());
|
2019-04-25 16:10:16 +02:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2018-10-26 11:16:56 +02:00
|
|
|
}
|
2019-04-25 14:13:36 +02:00
|
|
|
#endif
|
2018-10-26 14:24:11 +02:00
|
|
|
|
2019-04-25 14:13:36 +02:00
|
|
|
if (subcommands.is_empty())
|
2018-10-26 14:24:11 +02:00
|
|
|
return 0;
|
2019-04-25 14:13:36 +02:00
|
|
|
|
2019-04-25 16:10:16 +02:00
|
|
|
FileDescriptorCollector fds;
|
2019-04-25 14:13:36 +02:00
|
|
|
|
|
|
|
for (int i = 0; i < subcommands.size(); ++i) {
|
|
|
|
auto& subcommand = subcommands[i];
|
|
|
|
for (auto& redirection : subcommand.redirections) {
|
2019-05-26 00:38:11 +02:00
|
|
|
switch (redirection.type) {
|
|
|
|
case Redirection::Pipe: {
|
|
|
|
int pipefd[2];
|
|
|
|
int rc = pipe(pipefd);
|
|
|
|
if (rc < 0) {
|
|
|
|
perror("pipe");
|
|
|
|
return 1;
|
|
|
|
}
|
2019-06-04 20:36:08 +02:00
|
|
|
subcommand.rewirings.append({ STDOUT_FILENO, pipefd[1] });
|
2019-05-26 00:38:11 +02:00
|
|
|
auto& next_command = subcommands[i + 1];
|
2019-06-04 20:36:08 +02:00
|
|
|
next_command.rewirings.append({ STDIN_FILENO, pipefd[0] });
|
2019-05-26 00:38:11 +02:00
|
|
|
fds.add(pipefd[0]);
|
|
|
|
fds.add(pipefd[1]);
|
|
|
|
break;
|
2019-04-25 14:13:36 +02:00
|
|
|
}
|
2019-05-26 00:38:11 +02:00
|
|
|
case Redirection::FileWriteAppend: {
|
|
|
|
int fd = open(redirection.path.characters(), O_WRONLY | O_CREAT | O_APPEND, 0666);
|
|
|
|
if (fd < 0) {
|
|
|
|
perror("open");
|
|
|
|
return 1;
|
|
|
|
}
|
2019-06-04 20:36:08 +02:00
|
|
|
subcommand.rewirings.append({ redirection.fd, fd });
|
2019-05-26 00:38:11 +02:00
|
|
|
fds.add(fd);
|
|
|
|
break;
|
2019-04-25 16:10:16 +02:00
|
|
|
}
|
2019-05-26 00:38:11 +02:00
|
|
|
case Redirection::FileWrite: {
|
2019-05-26 14:57:12 +02:00
|
|
|
int fd = open(redirection.path.characters(), O_WRONLY | O_CREAT | O_TRUNC, 0666);
|
2019-05-26 00:38:11 +02:00
|
|
|
if (fd < 0) {
|
|
|
|
perror("open");
|
|
|
|
return 1;
|
|
|
|
}
|
2019-06-04 20:36:08 +02:00
|
|
|
subcommand.rewirings.append({ redirection.fd, fd });
|
2019-05-26 00:38:11 +02:00
|
|
|
fds.add(fd);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Redirection::FileRead: {
|
|
|
|
int fd = open(redirection.path.characters(), O_RDONLY);
|
|
|
|
if (fd < 0) {
|
|
|
|
perror("open");
|
|
|
|
return 1;
|
|
|
|
}
|
2019-06-04 20:36:08 +02:00
|
|
|
subcommand.rewirings.append({ redirection.fd, fd });
|
2019-05-26 00:38:11 +02:00
|
|
|
fds.add(fd);
|
|
|
|
break;
|
2019-04-25 16:10:16 +02:00
|
|
|
}
|
2019-04-25 14:13:36 +02:00
|
|
|
}
|
|
|
|
}
|
2018-10-26 14:24:11 +02:00
|
|
|
}
|
|
|
|
|
2018-12-07 01:26:07 +01:00
|
|
|
struct termios trm;
|
|
|
|
tcgetattr(0, &trm);
|
|
|
|
|
2019-06-06 10:57:51 +02:00
|
|
|
struct SpawnedProcess {
|
|
|
|
String name;
|
|
|
|
pid_t pid;
|
|
|
|
};
|
|
|
|
|
|
|
|
Vector<SpawnedProcess> children;
|
2019-04-25 14:13:36 +02:00
|
|
|
|
2019-05-02 02:22:28 +02:00
|
|
|
CommandTimer timer;
|
|
|
|
|
2019-04-25 14:13:36 +02:00
|
|
|
for (int i = 0; i < subcommands.size(); ++i) {
|
|
|
|
auto& subcommand = subcommands[i];
|
2019-05-26 20:36:16 +02:00
|
|
|
Vector<String> argv_string = process_arguments(subcommand.args);
|
2019-04-25 14:13:36 +02:00
|
|
|
Vector<const char*> argv;
|
2019-05-26 20:36:16 +02:00
|
|
|
argv.ensure_capacity(argv_string.size());
|
|
|
|
for (const auto& s : argv_string) {
|
|
|
|
argv.append(s.characters());
|
|
|
|
}
|
2019-04-25 14:13:36 +02:00
|
|
|
argv.append(nullptr);
|
|
|
|
|
2019-05-26 20:36:16 +02:00
|
|
|
#ifdef SH_DEBUG
|
|
|
|
for (auto& arg : argv) {
|
|
|
|
dbgprintf("<%s> ", arg);
|
|
|
|
}
|
|
|
|
dbgprintf("\n");
|
|
|
|
#endif
|
|
|
|
|
2019-04-25 14:13:36 +02:00
|
|
|
int retval = 0;
|
2019-04-25 16:10:16 +02:00
|
|
|
if (handle_builtin(argv.size() - 1, const_cast<char**>(argv.data()), retval))
|
2019-04-25 14:13:36 +02:00
|
|
|
return retval;
|
|
|
|
|
|
|
|
pid_t child = fork();
|
|
|
|
if (!child) {
|
|
|
|
setpgid(0, 0);
|
|
|
|
tcsetpgrp(0, getpid());
|
2019-06-04 20:36:08 +02:00
|
|
|
for (auto& rewiring : subcommand.rewirings) {
|
|
|
|
#ifdef SH_DEBUG
|
|
|
|
dbgprintf("in %s<%d>, dup2(%d, %d)\n", argv[0], getpid(), redirection.rewire_fd, redirection.fd);
|
2019-04-25 16:10:16 +02:00
|
|
|
#endif
|
2019-06-04 20:36:08 +02:00
|
|
|
int rc = dup2(rewiring.rewire_fd, rewiring.fd);
|
|
|
|
if (rc < 0) {
|
|
|
|
perror("dup2");
|
|
|
|
return 1;
|
2019-04-25 14:13:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-25 16:10:16 +02:00
|
|
|
fds.collect();
|
2019-04-25 14:13:36 +02:00
|
|
|
|
2019-04-25 16:10:16 +02:00
|
|
|
int rc = execvp(argv[0], const_cast<char* const*>(argv.data()));
|
2019-04-25 14:13:36 +02:00
|
|
|
if (rc < 0) {
|
2019-05-07 04:17:05 +02:00
|
|
|
fprintf(stderr, "execvp(%s): %s\n", argv[0], strerror(errno));
|
2019-04-25 14:13:36 +02:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
ASSERT_NOT_REACHED();
|
2018-11-03 02:04:36 +01:00
|
|
|
}
|
2019-06-06 10:57:51 +02:00
|
|
|
children.append({ argv[0], child });
|
2019-04-25 14:13:36 +02:00
|
|
|
}
|
|
|
|
|
2019-04-25 16:10:16 +02:00
|
|
|
#ifdef SH_DEBUG
|
2019-04-25 14:13:36 +02:00
|
|
|
dbgprintf("Closing fds in shell process:\n");
|
2019-04-25 16:10:16 +02:00
|
|
|
#endif
|
|
|
|
fds.collect();
|
2018-10-29 21:54:11 +01:00
|
|
|
|
2019-04-25 16:10:16 +02:00
|
|
|
#ifdef SH_DEBUG
|
2019-04-25 14:13:36 +02:00
|
|
|
dbgprintf("Now we gotta wait on children:\n");
|
|
|
|
for (auto& child : children)
|
|
|
|
dbgprintf(" %d\n", child);
|
2019-04-26 17:33:06 +02:00
|
|
|
#endif
|
|
|
|
|
2019-04-25 14:13:36 +02:00
|
|
|
|
2018-10-27 01:24:22 +02:00
|
|
|
int wstatus = 0;
|
2019-06-06 10:54:54 +02:00
|
|
|
int return_value = 0;
|
2019-04-25 14:13:36 +02:00
|
|
|
|
2019-06-06 10:54:54 +02:00
|
|
|
for (int i = 0; i < children.size(); ++i) {
|
|
|
|
auto& child = children[i];
|
2019-04-25 14:13:36 +02:00
|
|
|
do {
|
2019-06-06 10:57:51 +02:00
|
|
|
int rc = waitpid(child.pid, &wstatus, 0);
|
2019-04-25 14:13:36 +02:00
|
|
|
if (rc < 0 && errno != EINTR) {
|
2019-06-06 10:54:54 +02:00
|
|
|
if (errno != ECHILD)
|
|
|
|
perror("waitpid");
|
2019-04-25 14:13:36 +02:00
|
|
|
break;
|
|
|
|
}
|
2019-06-06 10:54:54 +02:00
|
|
|
if (WIFEXITED(wstatus)) {
|
|
|
|
if (WEXITSTATUS(wstatus) != 0)
|
2019-06-06 10:57:51 +02:00
|
|
|
printf("Shell: %s(%d) exited with status %d\n", child.name.characters(), child.pid, WEXITSTATUS(wstatus));
|
2019-06-06 10:54:54 +02:00
|
|
|
if (i == 0)
|
|
|
|
return_value = WEXITSTATUS(wstatus);
|
|
|
|
} else {
|
|
|
|
if (WIFSIGNALED(wstatus)) {
|
2019-06-06 10:57:51 +02:00
|
|
|
printf("Shell: %s(%d) exited due to signal '%s'\n", child.name.characters(), child.pid, strsignal(WTERMSIG(wstatus)));
|
2019-06-06 10:54:54 +02:00
|
|
|
} else {
|
2019-06-06 10:57:51 +02:00
|
|
|
printf("Shell: %s(%d) exited abnormally\n", child.name.characters(), child.pid);
|
2019-06-06 10:54:54 +02:00
|
|
|
}
|
|
|
|
}
|
2019-04-25 14:13:36 +02:00
|
|
|
} while(errno == EINTR);
|
|
|
|
}
|
2018-10-27 01:24:22 +02:00
|
|
|
|
2018-11-03 02:04:36 +01:00
|
|
|
// FIXME: Should I really have to tcsetpgrp() after my child has exited?
|
|
|
|
// Is the terminal controlling pgrp really still the PGID of the dead process?
|
2018-11-02 13:14:25 +01:00
|
|
|
tcsetpgrp(0, getpid());
|
2018-12-07 01:26:07 +01:00
|
|
|
tcsetattr(0, TCSANOW, &trm);
|
2019-06-06 10:54:54 +02:00
|
|
|
return return_value;
|
2018-10-23 10:12:50 +02:00
|
|
|
}
|
|
|
|
|
2019-02-03 04:32:31 +01:00
|
|
|
int main(int argc, char** argv)
|
2018-10-23 10:12:50 +02:00
|
|
|
{
|
2019-05-07 01:39:10 +02:00
|
|
|
g.uid = getuid();
|
|
|
|
g.sid = setsid();
|
2018-11-02 13:14:25 +01:00
|
|
|
tcsetpgrp(0, getpgrp());
|
2019-05-07 01:39:10 +02:00
|
|
|
tcgetattr(0, &g.termios);
|
2019-01-25 15:34:21 +01:00
|
|
|
|
2018-11-07 19:03:44 +01:00
|
|
|
{
|
|
|
|
struct sigaction sa;
|
|
|
|
sa.sa_handler = handle_sigint;
|
|
|
|
sa.sa_flags = 0;
|
|
|
|
sa.sa_mask = 0;
|
|
|
|
int rc = sigaction(SIGINT, &sa, nullptr);
|
|
|
|
assert(rc == 0);
|
|
|
|
}
|
|
|
|
|
2019-05-07 01:39:10 +02:00
|
|
|
int rc = gethostname(g.hostname, sizeof(g.hostname));
|
2018-10-28 01:08:01 +02:00
|
|
|
if (rc < 0)
|
|
|
|
perror("gethostname");
|
2019-05-07 01:39:10 +02:00
|
|
|
rc = ttyname_r(0, g.ttyname, sizeof(g.ttyname));
|
2018-10-31 01:21:56 +01:00
|
|
|
if (rc < 0)
|
|
|
|
perror("ttyname_r");
|
2019-01-30 00:49:20 +01:00
|
|
|
|
2018-10-31 21:31:56 +01:00
|
|
|
{
|
|
|
|
auto* pw = getpwuid(getuid());
|
2019-04-25 23:09:21 +02:00
|
|
|
if (pw) {
|
2019-05-07 01:39:10 +02:00
|
|
|
g.username = pw->pw_name;
|
|
|
|
g.home = pw->pw_dir;
|
2019-05-30 02:51:15 +02:00
|
|
|
setenv("HOME", pw->pw_dir, 1);
|
2019-04-25 23:09:21 +02:00
|
|
|
}
|
2018-10-31 21:31:56 +01:00
|
|
|
endpwent();
|
|
|
|
}
|
2018-10-28 01:08:01 +02:00
|
|
|
|
2019-05-13 04:52:55 +02:00
|
|
|
if (argc > 2 && !strcmp(argv[1], "-c")) {
|
|
|
|
dbgprintf("sh -c '%s'\n", argv[2]);
|
|
|
|
run_command(argv[2]);
|
|
|
|
return 0;
|
2019-02-03 04:32:31 +01:00
|
|
|
}
|
|
|
|
|
2018-10-30 00:06:31 +01:00
|
|
|
{
|
2019-05-07 01:01:50 +02:00
|
|
|
auto* cwd = getcwd(nullptr, 0);
|
2019-05-07 01:39:10 +02:00
|
|
|
g.cwd = cwd;
|
2019-05-07 01:01:50 +02:00
|
|
|
free(cwd);
|
2018-10-30 00:06:31 +01:00
|
|
|
}
|
2019-05-07 01:39:10 +02:00
|
|
|
|
2018-10-23 10:12:50 +02:00
|
|
|
for (;;) {
|
2019-05-07 01:39:10 +02:00
|
|
|
prompt();
|
|
|
|
auto line = editor.get_line();
|
|
|
|
if (line.is_empty())
|
|
|
|
continue;
|
|
|
|
run_command(line);
|
|
|
|
editor.add_to_history(line);
|
2018-10-23 10:12:50 +02:00
|
|
|
}
|
2019-05-07 01:39:10 +02:00
|
|
|
|
2018-10-23 10:12:50 +02:00
|
|
|
return 0;
|
|
|
|
}
|