Skip to content

Commit

Permalink
Make the whole boot system thing happen
Browse files Browse the repository at this point in the history
  • Loading branch information
tbodt committed May 5, 2019
1 parent cad09c7 commit 894c3d2
Show file tree
Hide file tree
Showing 11 changed files with 226 additions and 95 deletions.
90 changes: 61 additions & 29 deletions app/AppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,9 @@ - (int)startThings {
return err;

// need to do this first so that we can have a valid current for the generic_mknod calls
create_first_process();
err = become_first_process();
if (err < 0)
return err;

// create some device nodes
// this will do nothing if they already exist
Expand All @@ -78,34 +80,14 @@ - (int)startThings {
generic_mknod("/dev/tty4", S_IFCHR|0666, dev_make(4, 4));
generic_mknod("/dev/tty5", S_IFCHR|0666, dev_make(4, 5));
generic_mknod("/dev/tty6", S_IFCHR|0666, dev_make(4, 6));
generic_mknod("/dev/tty7", S_IFCHR|0666, dev_make(4, 7));
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));

NSArray<NSString *> *command = UserPreferences.shared.launchCommand;
char argv[4096];
char *p = argv;
for (NSString *cmd in command) {
const char *c = cmd.UTF8String;
// Save space for the final NUL byte in argv
while (p < argv + sizeof(argv) - 1 && (*p++ = *c++));
// If we reach the end of the buffer, the last string still needs to be
// NUL terminated
*p = '\0';
}
// Add the final NUL byte to argv
*++p = '\0';
const char *envp = "TERM=xterm-256color\0";
err = sys_execve(argv, argv, envp);
if (err < 0)
return err;
set_console_device(4, 1);
err = create_stdio("/dev/console", &ios_tty_driver);
if (err < 0)
return err;
exit_hook = ios_handle_exit;
die_handler = ios_handle_die;
do_mount(&procfs, "proc", "/proc", 0);
do_mount(&devptsfs, "devpts", "/dev/pts", 0);

// configure dns
struct __res_state res;
Expand All @@ -122,7 +104,7 @@ - (int)startThings {
for (int i = 0; i < serversFound; i ++) {
union res_sockaddr_union s = servers[i];
if (s.sin.sin_len == 0)
continue;
continue;
getnameinfo((struct sockaddr *) &s.sin, s.sin.sin_len,
address, sizeof(address),
NULL, 0, NI_NUMERICHOST);
Expand All @@ -133,14 +115,64 @@ - (int)startThings {
fd->ops->write(fd, resolvConf.UTF8String, [resolvConf lengthOfBytesUsingEncoding:NSUTF8StringEncoding]);
fd_close(fd);
}

do_mount(&procfs, "proc", "/proc", 0);
do_mount(&devptsfs, "devpts", "/dev/pts", 0);


exit_hook = ios_handle_exit;
die_handler = ios_handle_die;

tty_drivers[TTY_CONSOLE_MAJOR] = &ios_tty_driver;
set_console_device(TTY_CONSOLE_MAJOR, 1);
err = create_stdio("/dev/console");
if (err < 0)
return err;

NSArray<NSString *> *command;
if (UserPreferences.shared.bootEnabled) {
command = UserPreferences.shared.bootCommand;
} else {
command = UserPreferences.shared.launchCommand;
}
NSLog(@"%@", command);
char argv[4096];
[self convertCommand:command toArgs:argv limitSize:sizeof(argv)];
const char *envp = "TERM=xterm-256color\0";
err = sys_execve(argv, argv, envp);
if (err < 0)
return err;
task_start(current);

if (UserPreferences.shared.bootEnabled) {
err = become_new_init_child();
if (err < 0)
return err;
err = create_stdio("/dev/tty7");
if (err < 0)
return err;
char argv[4096];
[self convertCommand:UserPreferences.shared.launchCommand toArgs:argv limitSize:sizeof(argv)];
const char *envp = "TERM=xterm-256color\0";
err = sys_execve(argv, argv, envp);
if (err < 0)
return err;
task_start(current);
}

return 0;
}

- (void)convertCommand:(NSArray<NSString *> *)command toArgs:(char *)argv limitSize:(size_t)maxSize {
char *p = argv;
for (NSString *cmd in command) {
const char *c = cmd.UTF8String;
// Save space for the final NUL byte in argv
while (p < argv + maxSize - 1 && (*p++ = *c++));
// If we reach the end of the buffer, the last string still needs to be
// NUL terminated
*p = '\0';
}
// Add the final NUL byte to argv
*++p = '\0';
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// get the network permissions popup to appear on chinese devices
[[NSURLSession.sharedSession dataTaskWithURL:[NSURL URLWithString:@"http://captive.apple.com"]] resume];
Expand Down
2 changes: 1 addition & 1 deletion app/TerminalViewController.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

@interface TerminalViewController : UIViewController

@property (readonly) Terminal *terminal;
@property (nonatomic) Terminal *terminal;

@end

Expand Down
32 changes: 29 additions & 3 deletions app/TerminalViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@

@interface TerminalViewController () <UIGestureRecognizerDelegate>

@property Terminal *terminal;
@property UITapGestureRecognizer *tapRecognizer;
@property (weak, nonatomic) IBOutlet TerminalView *termView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *bottomConstraint;
Expand All @@ -39,8 +38,11 @@ @implementation TerminalViewController

- (void)viewDidLoad {
[super viewDidLoad];
self.terminal = [Terminal terminalWithType:4 number:1];
self.termView.terminal = self.terminal;
if (UserPreferences.shared.bootEnabled) {
self.terminal = [Terminal terminalWithType:4 number:7];
} else {
self.terminal = [Terminal terminalWithType:4 number:1];
}
[self.termView becomeFirstResponder];

NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
Expand Down Expand Up @@ -223,6 +225,30 @@ - (IBAction)pressArrow:(ArrowBarButton *)sender {
}
}

- (void)switchTerminal:(UIKeyCommand *)sender {
unsigned i = (unsigned) sender.input.integerValue;
self.terminal = [Terminal terminalWithType:4 number:i];
}

- (NSArray<UIKeyCommand *> *)keyCommands {
static NSMutableArray<UIKeyCommand *> *commands = nil;
if (commands == nil) {
commands = [NSMutableArray new];
for (unsigned i = 1; i <= 7; i++) {
[commands addObject:
[UIKeyCommand keyCommandWithInput:[NSString stringWithFormat:@"%d", i]
modifierFlags:UIKeyModifierCommand|UIKeyModifierAlternate|UIKeyModifierShift
action:@selector(switchTerminal:)]];
}
}
return commands;
}

- (void)setTerminal:(Terminal *)terminal {
_terminal = terminal;
self.termView.terminal = self.terminal;
}

@end

@interface BarView : UIInputView
Expand Down
3 changes: 1 addition & 2 deletions kernel/exec.c
Original file line number Diff line number Diff line change
Expand Up @@ -181,8 +181,7 @@ static int elf_exec(struct fd *fd, const char *file, const char *argv, const cha
// killed before it even starts. please don't be too sad about it, it's
// just a process.
mm_release(current->mm);
current->mm = mm_new();
current->mem = current->cpu.mem = &current->mm->mem;
task_set_mm(current, mm_new());
write_wrlock(&current->mem->lock);

current->mm->exefile = fd_retain(fd);
Expand Down
26 changes: 16 additions & 10 deletions kernel/fork.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,7 @@ static int copy_task(struct task *task, dword_t flags, addr_t stack, addr_t ptid
if (flags & CLONE_VM_) {
mm_retain(mm);
} else {
task->mm = mm_copy(mm);
task->mem = task->cpu.mem = &task->mm->mem;
task_set_mm(task, mm_copy(mm));
}

if (flags & CLONE_FILES_) {
Expand Down Expand Up @@ -133,6 +132,18 @@ static int copy_task(struct task *task, dword_t flags, addr_t stack, addr_t ptid
return err;
}

struct task *fork_task(struct task *parent) {
struct task *task = task_create_(parent);
if (task == NULL)
return ERR_PTR(_ENOMEM);
int err = copy_task(task, SIGCHLD_, 0, 0, 0, 0);
if (err < 0) {
task_destroy(task);
return ERR_PTR(err);
}
return task;
}

dword_t sys_clone(dword_t flags, addr_t stack, addr_t ptid, addr_t tls, addr_t ctid) {
STRACE("clone(0x%x, 0x%x, 0x%x, 0x%x, 0x%x)", flags, stack, ptid, tls, ctid);
if (flags & ~CSIGNAL_ & ~IMPLEMENTED_FLAGS) {
Expand All @@ -144,14 +155,9 @@ dword_t sys_clone(dword_t flags, addr_t stack, addr_t ptid, addr_t tls, addr_t c
if (flags & CLONE_THREAD_ && !(flags & CLONE_SIGHAND_))
return _EINVAL;

struct task *task = task_create_(current);
if (task == NULL)
return _ENOMEM;
int err = copy_task(task, flags, stack, ptid, tls, ctid);
if (err < 0) {
task_destroy(task);
return err;
}
struct task *task = fork_task(current);
if (IS_ERR(task))
return PTR_ERR(task);
task->cpu.eax = 0;

struct vfork_info vfork;
Expand Down
12 changes: 9 additions & 3 deletions kernel/group.c
Original file line number Diff line number Diff line change
Expand Up @@ -69,16 +69,16 @@ pid_t_ sys_getpgrp() {
return sys_getpgid(0);
}

dword_t sys_setsid() {
pid_t_ task_setsid(struct task *task) {
lock(&pids_lock);
struct tgroup *group = current->group;
struct tgroup *group = task->group;
pid_t_ new_sid = group->leader->pid;
if (group->pgid == new_sid || group->sid == new_sid) {
unlock(&pids_lock);
return _EPERM;
}

struct pid *pid = pid_get(current->pid);
struct pid *pid = pid_get(task->pid);
list_remove_safe(&group->session);
list_add(&pid->session, &group->session);
group->sid = new_sid;
Expand All @@ -94,7 +94,13 @@ dword_t sys_setsid() {
return new_sid;
}

dword_t sys_setsid() {
STRACE("setsid()");
return task_setsid(current);
}

dword_t sys_getsid() {
STRACE("getsid()");
lock(&pids_lock);
pid_t_ sid = current->group->sid;
unlock(&pids_lock);
Expand Down
Loading

0 comments on commit 894c3d2

Please sign in to comment.