configurable exit callback Allow the process to provide a callback to be used to exit the applicaiton. As this runtime is lightweight, it knows nothing about the environment in which it is executing and so there is no way to exit the process 'properly'. If a process attempts to exit and there is no configured exit callback, the thread tha called exit will fault after all destructors have run. Signed-off-by: Curtis Millar <curtis@curtism.me>
diff --git a/include/sel4runtime.h b/include/sel4runtime.h index 4a3a62b..b8175a1 100644 --- a/include/sel4runtime.h +++ b/include/sel4runtime.h
@@ -122,6 +122,32 @@ ); /* + * Callback function type that can be used to exit a process. + */ +typedef void sel4runtime_exit_cb(int code); + +/* + * Set the callback used to exit a process. + * + * Returns the current exit function. + */ +sel4runtime_exit_cb *sel4runtime_set_exit(sel4runtime_exit_cb *cb); + +/* + * Callback function type that can be used to exit a process. + * + * Should return the exit code to be generated. + */ +typedef int sel4runtime_pre_exit_cb(int code); + +/* + * Set the callback called before destructors are run for a prcess exit.. + * + * Returns the current pre-exit function. + */ +sel4runtime_pre_exit_cb *sel4runtime_set_pre_exit(sel4runtime_pre_exit_cb *cb); + +/* * Exit the runtime. * * This will attempt to suspend the initial thread of the process. Any
diff --git a/src/env.c b/src/env.c index a18c6eb..9e914ef 100644 --- a/src/env.c +++ b/src/env.c
@@ -85,6 +85,10 @@ // Environment vector char const *const *envp; + + // Exit callbacks + sel4runtime_exit_cb *exit_cb; + sel4runtime_pre_exit_cb *pre_exit_cb; } env = { /* * Initialise the initial thread as referring to the global thread @@ -173,21 +177,33 @@ return env.initial_thread_tls_base; } +sel4runtime_exit_cb *sel4runtime_set_exit(sel4runtime_exit_cb *cb) +{ + sel4runtime_exit_cb *old = env.exit_cb; + env.exit_cb = cb; + return old; +} + +sel4runtime_pre_exit_cb *sel4runtime_set_pre_exit(sel4runtime_pre_exit_cb *cb) +{ + sel4runtime_pre_exit_cb *old = env.pre_exit_cb; + env.pre_exit_cb = cb; + return old; +} + void sel4runtime_exit(int code) { - if (!is_initial_thread() && env.initial_thread_tcb) { - seL4_TCB_Suspend(env.initial_thread_tcb); + if (env.pre_exit_cb != NULL) { + code = env.pre_exit_cb(code); } __sel4runtime_run_destructors(); - /* Suspend the process */ - while (true) { - if (is_initial_thread() && env.initial_thread_tcb) { - seL4_TCB_Suspend(env.initial_thread_tcb); - } - seL4_Yield(); - } + /* If the exit is never set this will try and call a NULL function + * pointer which should result in a fault. This is as good a way as + * any to exit the process if we don't know anything better about + * the environment. */ + env.exit_cb(code); } int __sel4runtime_write_tls_variable(