The init() Function

Init() is located in init/main.c.

Init() completes the kernel's boot process by calling do_basic_setup() to initialize the kernel's PCI and network features. The remaining memory allocated for initialization is discarded, scheduling is enabled, the standard input, output and error streams are created, and prepare_namespace() is called to mount the root filesystem.

With the root filesystem in place, init() runs execve() to launch the program /sbin/init, if it exists. If a valid program name is provided with the init=<programname> command line option, init() will execve() that program instead. If a suitable startup program cannot be found (the kernel also tries "/bin/init" and "/bin/sh"), the kernel panics and halts.

The code for init() is shown in Figure 8.

static int init(void * unused)
{
   lock_kernel();
   do_basic_setup();

   prepare_namespace();

   free_initmem();
   unlock_kernel();

   if (open("/dev/console", O_RDWR, 0) < 0)
      printk("Warning: unable to open an initial console.\n");

   (void) dup(0);
   (void) dup(0);
	
   if (execute_command) execve(execute_command,argv_init,envp_init);
   execve("/sbin/init", argv_init, envp_init);
   execve("/bin/init",argv_init,envp_init);
   execve("/bin/sh",argv_init,envp_init);
   panic("No init found.  Try passing init= option to kernel.");
}

Figure 8. The init() function.