Files
MicroPythonOS/draft_code/main.c_with_chroot
T
Thomas Farstrike f2ac71d6f2 Add draft_code
2025-06-13 09:55:17 +02:00

82 lines
2.6 KiB
Plaintext

diff --git a/ports/unix/main.c b/ports/unix/main.c
index 58fa3ff..c022c20 100644
--- a/ports/unix/main.c
+++ b/ports/unix/main.c
@@ -53,6 +53,8 @@
#include "extmod/vfs_posix.h"
#include "genhdr/mpversion.h"
#include "input.h"
+#include "machine_sdl.h"
+#include "machine_timer.h"
// Command line options, with their defaults
static bool compile_only = false;
@@ -61,7 +63,7 @@ static uint emit_opt = MP_EMIT_OPT_NONE;
#if MICROPY_ENABLE_GC
// Heap size of GC heap (if enabled)
// Make it larger on a 64 bit machine, because pointers are larger.
-long heap_size = 1024 * 1024 * (sizeof(mp_uint_t) / 4);
+long heap_size = 8388608;
#endif
// Number of heaps to assign by default if MICROPY_GC_SPLIT_HEAP=1
@@ -192,6 +194,12 @@ static char *strjoin(const char *s1, int sep_char, const char *s2) {
}
#endif
+char *mp_repl_get_ps3(void)
+{
+ return "";
+}
+
+
static int do_repl(void) {
mp_hal_stdout_tx_str(MICROPY_BANNER_NAME_AND_VERSION);
mp_hal_stdout_tx_str("; " MICROPY_BANNER_MACHINE);
@@ -282,8 +290,16 @@ static int do_repl(void) {
for (;;) {
char *line = prompt((char *)mp_repl_get_ps1());
if (line == NULL) {
- // EOF
- return 0;
+ if (errno != EWOULDBLOCK) {
+ return 0;
+ } else {
+ while (line == NULL && errno == EWOULDBLOCK) {
+ mp_handle_pending(true);
+ usleep(1000);
+ line = prompt(mp_repl_get_ps3());
+ }
+ if (line == NULL) return 0;
+ }
}
while (mp_repl_continue_with_input(line)) {
char *line2 = prompt((char *)mp_repl_get_ps2());
@@ -470,6 +486,17 @@ static void sys_set_excecutable(char *argv0) {
MP_NOINLINE int main_(int argc, char **argv);
int main(int argc, char **argv) {
+ // Parse command-line arguments for a custom root
+ const char *fs_root = "/home/user/sources/PiggyOS/internal_filesystem"; // Hardcode or parse from argv
+ if (chroot(fs_root) != 0) {
+ perror("chroot failed");
+ return 1;
+ }
+ if (chdir("/") != 0) {
+ perror("chdir failed");
+ return 1;
+ }
+
#if MICROPY_PY_THREAD
mp_thread_init();
#endif
@@ -752,6 +779,8 @@ MP_NOINLINE int main_(int argc, char **argv) {
MP_STATE_THREAD(prof_trace_callback) = MP_OBJ_NULL;
#endif
+ machine_timer_deinit_all();
+ deinit_sdl();
#if MICROPY_PY_SYS_ATEXIT
// Beware, the sys.settrace callback should be disabled before running sys.atexit.
if (mp_obj_is_callable(MP_STATE_VM(sys_exitfunc))) {