Skip to content
Permalink
Browse files

Make the session automatically restart

  • Loading branch information...
tbodt committed May 5, 2019
1 parent fd3308f commit 7a45420ac0bd67852f2a898a6f0d565f67d8cbf9
Showing with 57 additions and 27 deletions.
  1. +1 −1 app/AppDelegate.h
  2. +46 −14 app/AppDelegate.m
  3. +0 −5 app/TerminalViewController.m
  4. +6 −5 kernel/exit.c
  5. +1 −1 kernel/task.h
  6. +3 −1 xX_main_Xx.h
@@ -14,4 +14,4 @@

@end

extern NSString *const ISHExitedNotification;
extern NSString *const ProcessExitedNotification;
@@ -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
@@ -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) {
@@ -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"];
@@ -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];
@@ -217,4 +249,4 @@ - (void)applicationDidEnterBackground:(UIApplication *)application {

@end

NSString *const ISHExitedNotification = @"ISHExitedNotification";
NSString *const ProcessExitedNotification = @"ProcessExitedNotification";
@@ -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];

@@ -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;
@@ -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);
@@ -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++) {
@@ -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) {
@@ -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)

@@ -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

0 comments on commit 7a45420

Please sign in to comment.
You can’t perform that action at this time.