Skip to content

Commit

Permalink
Make the session automatically restart
Browse files Browse the repository at this point in the history
  • Loading branch information
tbodt committed May 5, 2019
1 parent fd3308f commit 7a45420
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 27 deletions.
2 changes: 1 addition & 1 deletion app/AppDelegate.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@

@end

extern NSString *const ISHExitedNotification;
extern NSString *const ProcessExitedNotification;
60 changes: 46 additions & 14 deletions app/AppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,23 @@

@interface AppDelegate ()

@property int sessionPid;

@property BOOL exiting;

@end

static void ios_handle_exit(int code) {
[[NSNotificationCenter defaultCenter] postNotificationName:ISHExitedNotification object:nil];
static void ios_handle_exit(struct task *task, int code) {
// we are interested in init and in children of init
// this is called with pids_lock as an implementation side effect, please do not cite as an example of good API design
if (task->parent != NULL && task->parent->parent != NULL)
return;
dispatch_async(dispatch_get_main_queue(), ^{
[[NSNotificationCenter defaultCenter] postNotificationName:ProcessExitedNotification
object:nil
userInfo:@{@"pid": @(task->pid),
@"code": @(code)}];
});
}

// Put the abort message in the thread name so it gets included in the crash dump
Expand Down Expand Up @@ -141,24 +152,32 @@ - (int)startThings {
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);
err = [self startSession];
if (err < 0)
return err;
task_start(current);
}

return 0;
}

- (int)startSession {
int 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;
self.sessionPid = current->pid;
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) {
Expand All @@ -178,6 +197,12 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(
[[NSURLSession.sharedSession dataTaskWithURL:[NSURL URLWithString:@"http://captive.apple.com"]] resume];

[UserPreferences.shared addObserver:self forKeyPath:@"shouldDisableDimming" options:NSKeyValueObservingOptionInitial context:nil];

[NSNotificationCenter.defaultCenter addObserver:self
selector:@selector(processExited:)
name:ProcessExitedNotification
object:nil];

int err = [self startThings];
if (err < 0) {
NSString *message = [NSString stringWithFormat:@"could not initialize"];
Expand All @@ -194,6 +219,13 @@ - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(N
UIApplication.sharedApplication.idleTimerDisabled = UserPreferences.shared.shouldDisableDimming;
}

- (void)processExited:(NSNotification *)notif {
int pid = [notif.userInfo[@"pid"] intValue];
if (pid == self.sessionPid) {
[self startSession];
}
}

- (void)showMessage:(NSString *)message subtitle:(NSString *)subtitle fatal:(BOOL)fatal {
dispatch_async(dispatch_get_main_queue(), ^{
UIAlertController *alert = [UIAlertController alertControllerWithTitle:message message:subtitle preferredStyle:UIAlertControllerStyleAlert];
Expand All @@ -217,4 +249,4 @@ - (void)applicationDidEnterBackground:(UIApplication *)application {

@end

NSString *const ISHExitedNotification = @"ISHExitedNotification";
NSString *const ProcessExitedNotification = @"ProcessExitedNotification";
5 changes: 0 additions & 5 deletions app/TerminalViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,6 @@ - (void)viewDidLoad {
name:UIKeyboardWillHideNotification
object:nil];

[center addObserver:self
selector:@selector(ishExited:)
name:ISHExitedNotification
object:nil];

[self _updateStyleFromPreferences:NO];
[[UserPreferences shared] addObserver:self forKeyPath:@"theme" options:NSKeyValueObservingOptionNew context:nil];

Expand Down
11 changes: 6 additions & 5 deletions kernel/exit.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ static bool exit_tgroup(struct task *task) {
return group_dead;
}

void (*exit_hook)(struct task *task, int code) = NULL;

noreturn void do_exit(int status) {
// has to happen before mm_release
addr_t clear_tid = current->clear_tid;
Expand Down Expand Up @@ -79,6 +81,9 @@ noreturn void do_exit(int status) {
notify(&parent->group->child_exit);
send_signal(parent, leader->exit_signal);
}

if (exit_hook != NULL)
exit_hook(current, status);
}

vfork_notify(current);
Expand Down Expand Up @@ -111,9 +116,8 @@ noreturn void do_exit_group(int status) {
do_exit(status);
}

void (*exit_hook)(int code) = NULL;
// always called from init process
static void halt_system(int status) {
static void halt_system() {
// brutally murder everything
// which will leave everything in an inconsistent state. I will solve this problem later.
for (int i = 2; i < MAX_PID; i++) {
Expand All @@ -129,9 +133,6 @@ static void halt_system(int status) {
mount_remove(mount);
}
unlock(&mounts_lock);

if (exit_hook != NULL)
exit_hook(status);
}

dword_t sys_exit(dword_t status) {
Expand Down
2 changes: 1 addition & 1 deletion kernel/task.h
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ extern void (*task_run_hook)(void);
// TODO document
void task_start(struct task *task);

extern void (*exit_hook)(int code);
extern void (*exit_hook)(struct task *task, int code);

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

Expand Down
4 changes: 3 additions & 1 deletion xX_main_Xx.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
#define IOPOL_VFS_HFS_CASE_SENSITIVITY_FORCE_CASE_SENSITIVE 1
#endif

static void exit_handler(int code) {
static void exit_handler(struct task *task, int code) {
if (task->parent != NULL)
return;
if (code & 0xff)
raise(code & 0xff);
else
Expand Down

0 comments on commit 7a45420

Please sign in to comment.