Fixes handle_indirs issue
[akaros.git] / kern / src / init.c
index 1a76999..774181d 100644 (file)
@@ -52,6 +52,7 @@
 int booting = 1;
 struct sysinfo_t sysinfo;
 static void run_linker_funcs(void);
+static int run_init_script(void);
 
 void kernel_init(multiboot_info_t *mboot_info)
 {
@@ -80,11 +81,11 @@ void kernel_init(multiboot_info_t *mboot_info)
        vmr_init();
        file_init();
        page_check();
-       vfs_init();
-       devfs_init();
        idt_init();
        kernel_msg_init();
        timer_init();
+       vfs_init();
+       devfs_init();
        train_timing();
        kb_buf_init(&cons_buf);
        arch_init();
@@ -106,8 +107,64 @@ void kernel_init(multiboot_info_t *mboot_info)
        // zra: let's Ivy know we're done booting
        booting = 0;
 
+#ifdef CONFIG_RUN_INIT_SCRIPT
+       if (run_init_script()) {
+               printk("Configured to run init script, but no script specified!\n");
+               manager();
+       }
+#else
        manager();
+#endif
+}
+
+#ifdef CONFIG_RUN_INIT_SCRIPT
+static int run_init_script(void)
+{
+       /* If we have an init script path specified */
+       if (strlen(CONFIG_INIT_SCRIPT_PATH_AND_ARGS) != 0) {
+               int vargs = 0;
+               char *sptr = &CONFIG_INIT_SCRIPT_PATH_AND_ARGS[0];
+
+               /* Figure out how many arguments there are, by finding the spaces */
+               /* TODO: consider rewriting this stuff with parsecmd */
+               while (*sptr != '\0') {
+                       if (*(sptr++) != ' ') {
+                               vargs++;
+                               while ((*sptr != ' ') && (*sptr != '\0'))
+                                       sptr++;
+                       }
+               }
+
+               /* Initialize l_argv with its first three arguments, but allocate space
+                * for all arguments as calculated above */
+               int static_args = 3;
+               int total_args = vargs + static_args;
+               char *l_argv[total_args];
+               l_argv[0] = "";
+               l_argv[1] = "busybox";
+               l_argv[2] = "ash";
+
+               /* Initialize l_argv with the rest of the arguments */
+               int i = static_args;
+               sptr = &CONFIG_INIT_SCRIPT_PATH_AND_ARGS[0];
+               while (*sptr != '\0') {
+                       if (*sptr != ' ') {
+                               l_argv[i++] = sptr;
+                               while ((*sptr != ' ') && (*sptr != '\0'))
+                                       sptr++;
+                               if (*sptr == '\0')
+                                       break;
+                               *sptr = '\0';
+                       }
+                       sptr++;
+               }
+
+               /* Run the script with its arguments */
+               mon_bin_run(total_args, l_argv, NULL);
+       }
+       return -1;
 }
+#endif
 
 /*
  * Panic is called on unresolvable fatal errors.