simple-init

A set of init binaries for linux
git clone git://depsterr.com/git/simple-init
Log | Files | Refs | README | LICENSE

init.c (2498B)


      1 #include <sys/types.h>
      2 #include <sys/mount.h>
      3 #include <sys/swap.h>
      4 #include <sys/stat.h>
      5 #include <unistd.h>
      6 #include <mntent.h>
      7 #include <stdio.h>
      8 
      9 #include "util.h"
     10 
     11 int main() {
     12 
     13 	log_message("Welcome to linux");
     14 
     15 	log_message("Mounting pseudo filesystems");
     16 
     17 	mount("proc", "/proc", "proc",     MS_NOSUID | MS_NOEXEC | MS_NODEV, 0);
     18 	mount("sys",  "/sys",  "sysfs",    MS_NOSUID | MS_NOEXEC | MS_NODEV, 0);
     19 	mount("run",  "/run",  "tmpfs",    MS_NOSUID | MS_NODEV, "mode=0755");
     20 	mount("dev",  "/dev",  "devtmpfs", MS_NOSUID, "mode=0755");
     21 
     22 	mkdir("/run/user", 0755);
     23 	mkdir("/run/lock", 0755);
     24 	mkdir("/run/log",  0755);
     25 	mkdir("/dev/pts",  0755);
     26 	mkdir("/dev/shm",  0755);
     27 
     28 	mount("devpts", "/dev/pts", "devpts", MS_NOSUID | MS_NOEXEC, "mode=0620,gid=5");
     29 	mount("shm",    "/dev/shm", "tmpfs",  MS_NOSUID | MS_NODEV,  "mode=1777");
     30 
     31 	/* dest, link */
     32 	symlink("/proc/self/fd", "/dev/fd");
     33 	symlink("fd/0", "/dev/stdin");
     34 	symlink("fd/1", "/dev/stdout");
     35 	symlink("fd/2", "/dev/stderr");
     36 
     37 	/* Call modprobe stuff here */
     38 	/* char* modprobe[] = { "modprobe", "module", 0 }; */
     39 	/* spawn_process(modprobe); */
     40 
     41 	log_message("Starting device manager");
     42 
     43 	log_message("Starting udev");
     44 	char* udevcmd1[] = { "udevd", "-d", 0};
     45 	char* udevcmd2[] = { "udevadm", "trigger", "-c", "add", "-t", "subsystems", 0};
     46 	char* udevcmd3[] = { "udevadm", "trigger", "-c", "add", "-t", "devices", 0};
     47 	char* udevcmd4[] = { "udevadm", "settle", 0};
     48 	spawn_process(udevcmd1);
     49 	wait_for_children();
     50 	spawn_process(udevcmd2);
     51 	spawn_process(udevcmd3);
     52 	wait_for_children();
     53 	spawn_process(udevcmd4);
     54 
     55 	log_message("Mounting filesystems");
     56 	mount("/", "/", "ext4", MS_REMOUNT, 0);
     57 	char* mountall[] = { "mount", "-a", 0};
     58 	spawn_process(mountall);
     59 
     60 	log_message("Enabling swap");
     61 
     62 	char* swapon[] = { "swapon", "-a", 0};
     63 	spawn_process(swapon);
     64 
     65 	log_message("Seeding random");
     66 	load_random_seed();
     67 
     68 	log_message("Setting up loopback");
     69 	char* netloopcmd[] = { "ip", "link", "set", "up", "dev", "lo", 0};
     70 	spawn_process(netloopcmd);
     71 
     72 	log_message("Killing device managers");
     73 	char* udevcmd5[] = { udevcmd2[0], "control", "--exit", 0};
     74 	wait_for_children();
     75 	spawn_process(udevcmd5);
     76 
     77 	/* Startup services here */
     78 
     79 	/* char* startup[] = { "dhcpcd", 0 }; */
     80 	/* spawn_process(startup); */
     81 
     82 	wait_for_children();
     83 	log_message("Boot completed, starting ttys");
     84 	char* tty1[] = { "/bin/getty", "38400", "tty1", 0 };
     85 	char* tty2[] = { "/bin/getty", "38400", "tty2", 0 };
     86 	respawn_process(tty1);
     87 	respawn_process(tty2);
     88 }