Skip to content

Commit

Permalink
tidy up stdio piping code and add error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
MatthewMerrill committed May 28, 2019
1 parent 033e0de commit 82721f3
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 16 deletions.
25 changes: 13 additions & 12 deletions kernel/init.c
Original file line number Diff line number Diff line change
Expand Up @@ -140,23 +140,24 @@ int create_stdio(const char *file) {
}

static struct fd *open_fd_from_actual_fd(int fd_no) {
if (fd_no < 0)
return ERR_PTR(errno_map());
struct fd *fd = fd_create(&realfs_fdops);
struct fd *fd = adhoc_fd_create(&realfs_fdops);
if (fd == NULL) {
return NULL;
}
fd->real_fd = fd_no;
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);
if (!(current->files->files[0] = open_fd_from_actual_fd(STDIN_FILENO))) {
return -1;
}
if (!(current->files->files[1] = open_fd_from_actual_fd(STDOUT_FILENO))) {
return -1;
}
if (!(current->files->files[2] = open_fd_from_actual_fd(STDERR_FILENO))) {
return -1;
}
return 0;
}
9 changes: 5 additions & 4 deletions xX_main_Xx.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,13 +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;
if (isatty(fileno(stdin))) {
if (isatty(STDIN_FILENO)) {
err = create_stdio(console);
if (err < 0)
return err;
}
else {
create_piped_stdio();
} else {
err = create_piped_stdio();
if (err < 0)
return err;
}
exit_hook = exit_handler;
return 0;
Expand Down

0 comments on commit 82721f3

Please sign in to comment.