Skip to content

Commit

Permalink
if stdin is not a tty, inherit stdin/stdout/stderr
Browse files Browse the repository at this point in the history
  • Loading branch information
MatthewMerrill committed May 28, 2019
1 parent d189103 commit 249790e
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
23 changes: 23 additions & 0 deletions kernel/init.c
Original file line number Diff line number Diff line change
Expand Up @@ -138,3 +138,26 @@ int create_stdio(const char *file) {
current->files->files[2] = fd_retain(fd);
return 0;
}

static struct fd *open_fd_from_actual_fd(int actual_fd) {
int fd_no = actual_fd;
if (fd_no < 0)
return ERR_PTR(errno_map());
struct fd *fd = fd_create(&realfs_fdops);
fd->real_fd = actual_fd;
fd->dir = NULL;
return fd;
}

int create_piped_stdio() {
struct fd *si = open_fd_from_actual_fd(STDIN_FILENO);
struct fd *so = open_fd_from_actual_fd(STDOUT_FILENO);
struct fd *se = open_fd_from_actual_fd(STDERR_FILENO);
si->refcount = 0;
so->refcount = 0;
se->refcount = 0;
current->files->files[0] = fd_retain(si);
current->files->files[1] = fd_retain(so);
current->files->files[2] = fd_retain(se);
return 0;
}
11 changes: 8 additions & 3 deletions xX_main_Xx.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,14 @@ static inline int xX_main_Xx(int argc, char *const argv[], const char *envp) {
if (err < 0)
return err;
tty_drivers[TTY_CONSOLE_MAJOR] = &real_tty_driver;
err = create_stdio(console);
if (err < 0)
return err;
if (isatty(fileno(stdin))) {
err = create_stdio(console);
if (err < 0)
return err;
}
else {
create_piped_stdio();
}
exit_hook = exit_handler;
return 0;
}

0 comments on commit 249790e

Please sign in to comment.