Skip to content

Commit

Permalink
Make stdio connect to a real file, /dev/tty1 by default
Browse files Browse the repository at this point in the history
I expect we'll eventually have more than 1.
  • Loading branch information
tbodt committed Apr 8, 2019
1 parent 8c13e11 commit 8b7bde4
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 25 deletions.
17 changes: 9 additions & 8 deletions app/AppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,14 @@ - (int)startThings {
if (err < 0)
return err;

// create some device nodes
// this will do nothing if they already exist
generic_mknod("/dev/tty1", S_IFCHR|0666, dev_make(4, 1));
generic_mknod("/dev/tty", S_IFCHR|0666, dev_make(5, 0));
generic_mknod("/dev/ptmx", S_IFCHR|0666, dev_make(5, 2));
generic_mknod("/dev/random", S_IFCHR|0666, dev_make(1, 8));
generic_mknod("/dev/urandom", S_IFCHR|0666, dev_make(1, 9));

create_first_process();
NSArray<NSString *> *command = UserPreferences.shared.launchCommand;
char argv[4096];
Expand All @@ -84,7 +92,7 @@ - (int)startThings {
err = sys_execve(argv, argv, envp);
if (err < 0)
return err;
err = create_stdio(&ios_tty_driver);
err = create_stdio("/dev/tty1", &ios_tty_driver);
if (err < 0)
return err;
exit_hook = ios_handle_exit;
Expand Down Expand Up @@ -116,13 +124,6 @@ - (int)startThings {
fd->ops->write(fd, resolvConf.UTF8String, [resolvConf lengthOfBytesUsingEncoding:NSUTF8StringEncoding]);
fd_close(fd);
}

// create some device nodes
// this will do nothing if they already exist
generic_mknod("/dev/tty", S_IFCHR|0666, dev_make(5, 0));
generic_mknod("/dev/ptmx", S_IFCHR|0666, dev_make(5, 2));
generic_mknod("/dev/random", S_IFCHR|0666, dev_make(1, 8));
generic_mknod("/dev/urandom", S_IFCHR|0666, dev_make(1, 9));

do_mount(&procfs, "proc", "/proc");
do_mount(&devptsfs, "devpts", "/dev/pts");
Expand Down
2 changes: 1 addition & 1 deletion app/Terminal.m
Original file line number Diff line number Diff line change
Expand Up @@ -273,4 +273,4 @@ static void ios_tty_cleanup(struct tty *tty) {
.write = ios_tty_write,
.cleanup = ios_tty_cleanup,
};
DEFINE_TTY_DRIVER(ios_tty_driver, &ios_tty_ops, 1);
DEFINE_TTY_DRIVER(ios_tty_driver, &ios_tty_ops, 2);
2 changes: 1 addition & 1 deletion app/TerminalViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ @implementation TerminalViewController

- (void)viewDidLoad {
[super viewDidLoad];
self.terminal = [Terminal terminalWithType:0 number:0];
self.terminal = [Terminal terminalWithType:0 number:1];
self.termView.terminal = self.terminal;
[self.termView becomeFirstResponder];

Expand Down
2 changes: 1 addition & 1 deletion fs/tty-real.c
Original file line number Diff line number Diff line change
Expand Up @@ -120,4 +120,4 @@ struct tty_driver_ops real_tty_ops = {
.write = real_tty_write,
.cleanup = real_tty_cleanup,
};
DEFINE_TTY_DRIVER(real_tty_driver, &real_tty_ops, 1);
DEFINE_TTY_DRIVER(real_tty_driver, &real_tty_ops, 2);
24 changes: 14 additions & 10 deletions kernel/init.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,18 +62,22 @@ void create_first_process() {
sys_setsid();
}

int create_stdio(struct tty_driver *driver) {
// I can't wait for when init and udev works and I don't need to do this
int create_stdio(const char *file, struct tty_driver *driver) {
tty_drivers[TTY_CONSOLE_MAJOR] = driver;

// FIXME use generic_open (or something) to avoid this mess
struct fd *fd = adhoc_fd_create(NULL);
fd->stat.rdev = dev_make(4, 0);
fd->stat.mode = S_IFCHR | S_IRUSR;
fd->flags = O_RDWR_;
int err = dev_open(4, 0, DEV_CHAR, fd);
if (err < 0)
return err;
struct fd *fd = generic_open(file, O_RDWR_, 0);
if (fd == ERR_PTR(_ENOENT)) {
// fallback to adhoc files for stdio
fd = adhoc_fd_create(NULL);
fd->stat.rdev = dev_make(4, 0);
fd->stat.mode = S_IFCHR | S_IRUSR;
fd->flags = O_RDWR_;
int err = dev_open(4, 0, DEV_CHAR, fd);
if (err < 0)
return err;
}
if (IS_ERR(fd))
return PTR_ERR(fd);

fd->refcount = 3;
current->files->files[0] = fd;
Expand Down
2 changes: 1 addition & 1 deletion kernel/init.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@

int mount_root(const struct fs_ops *fs, const char *source);
void create_first_process(void);
int create_stdio(struct tty_driver *driver);
int create_stdio(const char *file, struct tty_driver *driver);

#endif
2 changes: 1 addition & 1 deletion kernel/task.h
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,6 @@ void task_start(struct task *task);

extern void (*exit_hook)(int code);

#define superuser() (current->euid == 0)
#define superuser() (current != NULL && current->euid == 0)

#endif
8 changes: 6 additions & 2 deletions xX_main_Xx.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ static inline int xX_main_Xx(int argc, char *const argv[], const char *envp) {
const char *root = "";
bool has_root = false;
const struct fs_ops *fs = &realfs;
while ((opt = getopt(argc, argv, "+r:f:")) != -1) {
const char *console = "/dev/tty1";
while ((opt = getopt(argc, argv, "+r:f:c:")) != -1) {
switch (opt) {
case 'r':
case 'f':
Expand All @@ -29,6 +30,9 @@ static inline int xX_main_Xx(int argc, char *const argv[], const char *envp) {
if (opt == 'f')
fs = &fakefs;
break;
case 'c':
console = optarg;
break;
}
}

Expand Down Expand Up @@ -63,7 +67,7 @@ static inline int xX_main_Xx(int argc, char *const argv[], const char *envp) {
err = sys_execve(argv[optind], argv_copy, envp == NULL ? "\0" : envp);
if (err < 0)
return err;
err = create_stdio(&real_tty_driver);
err = create_stdio(console, &real_tty_driver);
if (err < 0)
return err;
exit_hook = exit_handler;
Expand Down

0 comments on commit 8b7bde4

Please sign in to comment.