Skip to content

Commit

Permalink
Exit handling on iOS
Browse files Browse the repository at this point in the history
  • Loading branch information
tbodt committed Oct 25, 2017
1 parent 9ba95aa commit afb6b5d
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 9 deletions.
2 changes: 1 addition & 1 deletion app/AppDelegate.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@

@property (strong, nonatomic) UIWindow *window;


@end

extern NSString *const ISHExitedNotification;
7 changes: 7 additions & 0 deletions app/AppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ @interface AppDelegate ()

@end

static void ios_handle_exit(int code) {
[[NSNotificationCenter defaultCenter] postNotificationName:ISHExitedNotification object:nil];
}

@implementation AppDelegate

- (int)startThings {
Expand All @@ -31,6 +35,7 @@ - (int)startThings {
err = create_stdio(ios_tty_driver);
if (err < 0)
return err;
exit_hook = ios_handle_exit;
start_thread(current);
return 0;
}
Expand Down Expand Up @@ -72,3 +77,5 @@ - (void)applicationWillTerminate:(UIApplication *)application {


@end

NSString *const ISHExitedNotification = @"ISHExitedNotification";
8 changes: 8 additions & 0 deletions app/TerminalViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
//

#import "TerminalViewController.h"
#import "AppDelegate.h"

@interface TerminalViewController ()

Expand All @@ -30,8 +31,15 @@ - (void)viewDidLoad {
forKeyPath:@"content"
options:NSKeyValueObservingOptionInitial
context:NULL];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(ishExited)
name:ISHExitedNotification
object:nil];
}

- (void)ishExited:(NSNotification *)notification {
NSLog(@"exit");
}

- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
Expand Down
19 changes: 16 additions & 3 deletions kernel/exit.c
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
#include <pthread.h>
#include "kernel/calls.h"

void (*exit_hook)(int code) = NULL;

noreturn void do_exit(int status) {
if (current->pid == 1) {
exit(status >> 8);
}
lock(current->parent);
current->exit_code = status;
current->zombie = true;
Expand All @@ -15,6 +14,20 @@ noreturn void do_exit(int status) {
notify(current, vfork_done);
unlock(current);

if (current->pid == 1) {
// brutally murder everything
// which will leave everything in an inconsistent state. I will solve this problem later.
big_lock(pids);
for (int i = 2; i < MAX_PID; i++) {
struct process *proc = pid_get_proc(i);
if (proc != NULL)
pthread_kill(proc->thread, SIGKILL);
}
big_unlock(pids);

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

Expand Down
1 change: 1 addition & 0 deletions kernel/init.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ int create_init_process(const char *program, char *const argv[], char *const env

current = process_create();
current->cpu.mem = mem_new();
current->parent = current;
current->ppid = 1;
current->uid = current->gid = 0;
current->root = generic_open("/", O_RDONLY_, 0);
Expand Down
7 changes: 3 additions & 4 deletions kernel/process.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

__thread struct process *current;

#define MAX_PID (1 << 10) // oughta be enough
static struct pid pids[MAX_PID + 1] = {};
pthread_mutex_t pids_lock = PTHREAD_MUTEX_INITIALIZER;

Expand Down Expand Up @@ -70,12 +69,12 @@ void process_destroy(struct process *proc) {
free(proc);
}

void (*process_run_func)() = NULL;
void (*process_run_hook)() = NULL;

static void *process_run(void *proc) {
current = proc;
if (process_run_func)
process_run_func();
if (process_run_hook)
process_run_hook();
else
cpu_run(&current->cpu);
assert(false);
Expand Down
6 changes: 5 additions & 1 deletion kernel/process.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,13 @@ struct pid *pid_get(dword_t pid);
struct process *pid_get_proc(dword_t pid);
extern pthread_mutex_t pids_lock;

#define MAX_PID (1 << 10) // oughta be enough

// When a thread is created to run a new process, this function is used.
extern void (*run_process_func)(void);
extern void (*process_run_hook)(void);
// TODO document
void start_thread(struct process *proc);

extern void (*exit_hook)(int code);

#endif
5 changes: 5 additions & 0 deletions xX_main_Xx.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
#include "kernel/init.h"
#include "kernel/fs.h"

static void exit_handler(int code) {
exit(code >> 8);
}

// this function parses command line arguments and initializes global
// data structures. thanks programming discussions discord server for the name.
// https://discord.gg/9zT7NHP
Expand Down Expand Up @@ -33,5 +37,6 @@ static inline int xX_main_Xx(int argc, char *const argv[]) {
err = create_stdio(real_tty_driver);
if (err < 0)
return err;
exit_hook = exit_handler;
return 0;
}

0 comments on commit afb6b5d

Please sign in to comment.