int main(int argc, char *argv[]) { if (getpid() == 1) { setenv("PATH", "/bin:/usr/local/bin:/usr/bin:/mnt/bin", 1); setenv("HOME", "/root", 1); struct sigaction sa; sa.sa_sigaction = on_signal; sa.sa_flags = SA_SIGINFO; if (sigaction(SIGINT, &sa, NULL) != 0) { perror("sigaction SIGINT"); return 1; }; if (sigaction(SIGCHLD, &sa, NULL) != 0) { perror("sigaction SIGCHLD"); return 1; }; if (sigaction(SIGTERM, &sa, NULL) != 0) { perror("sigaction SIGTERM"); return 1; }; if (sigaction(SIGHUP, &sa, NULL) != 0) // SIGHUP is sent when the power button is pressed { perror("sigaction SIGHUP"); return 1; }; loadmods(); printf("init: initializing partitions...\n"); init_parts(); printf("init: loading configuration file /initrd/startup.conf...\n"); if (load_config("/initrd/startup.conf") == -1) { printf("init: attempting startup with fallback configuration file /initrd/startup-fallback.conf...\n"); if (load_config("/initrd/startup-fallback.conf") == -1) { printf("init: failed to start up\n"); }; }; /* if (_glidix_mount("isofs", "/dev/sdb", "/mnt/", 0) != 0) { perror("init: mount failed"); }; close(open("/dev/sda", O_RDONLY)); if (_glidix_mount("gxfs", "/dev/sda0", "/", 0) != 0) { perror("init: mount gxfs on /"); }; */ /* if (fork() == 0) { setenv("PATH", "/bin:/usr/local/bin:/usr/bin:/mnt/bin", 1); setenv("HOME", "/root", 1); if (execl("/mnt/bin/sh", "sh", NULL) != 0) { perror("exec"); exit(1); }; }; */ while (1) { pause(); if (shouldHalt) { sa.sa_handler = SIG_DFL; if (sigaction(SIGCHLD, &sa, NULL) != 0) { perror("sigaction SIGCHLD"); return 1; }; int fd = open("/etc/down-action", O_RDONLY); char downAction[256]; memset(downAction, 0, 256); read(fd, downAction, 16); close(fd); int action = _GLIDIX_DOWN_HALT; if (strcmp(downAction, "poweroff") == 0) { action = _GLIDIX_DOWN_POWEROFF; } else if (strcmp(downAction, "reboot") == 0) { action = _GLIDIX_DOWN_REBOOT; }; _glidix_down(action); } else if ((shouldRunPoweroff) && (!ranPoweroff)) { ranPoweroff = 1; if (fork() == 0) { if (execl("/usr/bin/halt", "poweroff", NULL) == -1) { perror("exec poweroff"); fprintf(stderr, "forcing shutdown\n"); kill(1, SIGTERM); exit(1); }; }; }; }; } else { fprintf(stderr, "%s: not allowed to execute with pid other than 1!\n", argv[0]); return 1; }; return 0; };
int main(int argc, char *argv[]) { if (getpid() == 1) { if (open("/dev/tty0", O_RDWR) != 0) return 1; if (dup(0) != 1) return 1; if (dup(1) != 2) return 1; setenv("PATH", "/usr/local/bin:/usr/bin:/bin", 1); setenv("HOME", "/root", 1); setenv("LD_LIBRARY_PATH", "/usr/local/lib:/usr/lib:/lib", 1); struct sigaction sa; memset(&sa, 0, sizeof(struct sigaction)); sa.sa_sigaction = on_signal; sigemptyset(&sa.sa_mask); sa.sa_flags = SA_SIGINFO; if (sigaction(SIGINT, &sa, NULL) != 0) { perror("sigaction SIGINT"); return 1; }; if (sigaction(SIGCHLD, &sa, NULL) != 0) { perror("sigaction SIGCHLD"); return 1; }; if (sigaction(SIGTERM, &sa, NULL) != 0) { perror("sigaction SIGTERM"); return 1; }; if (sigaction(SIGHUP, &sa, NULL) != 0) // SIGHUP is sent when the power button is pressed { perror("sigaction SIGHUP"); return 1; }; if (mkdir("/sem", 01777) != 0) { perror("mkdir /run/sem"); return 1; }; loadmods(); printf("init: initializing partitions...\n"); init_parts(); printf("init: looking for root filesystem...\n"); if (try_mount_root() != 0) { printf("init: failed to find the root filesystem!\n"); return 1; }; printf("init: setting up second-level filesystem...\n"); if (mount("bind", "/dev", "/rootfs/dev", 0, NULL, 0) != 0) { perror("init: bind /dev"); return 1; }; if (mount("bind", "/proc", "/rootfs/proc", 0, NULL, 0) != 0) { perror("init: bind /proc"); return 1; }; if (mount("bind", "/initrd", "/rootfs/initrd", 0, NULL, 0) != 0) { perror("init: bind /initrd"); return 1; }; if (mount("bind", "/run", "/rootfs/run", 0, NULL, 0) != 0) { perror("init: bind /run"); return 1; }; if (mount("bind", "/run", "/rootfs/var/run", 0, NULL, 0) != 0) { perror("init: bind /var/run"); return 1; }; printf("init: setting up fsinfo...\n"); int fd = open("/run/fsinfo", O_WRONLY | O_CREAT | O_EXCL, 0644); if (fd == -1) { perror("init: open /run/fsinfo"); return 1; }; struct __fsinfo_record record; memset(&record, 0, sizeof(struct __fsinfo_record)); strcpy(record.__image, rootImage); strcpy(record.__mntpoint, "/"); write(fd, &record, sizeof(struct __fsinfo_record)); strcpy(record.__image, "none"); strcpy(record.__mntpoint, "/dev"); write(fd, &record, sizeof(struct __fsinfo_record)); strcpy(record.__mntpoint, "/proc"); write(fd, &record, sizeof(struct __fsinfo_record)); strcpy(record.__mntpoint, "/initrd"); write(fd, &record, sizeof(struct __fsinfo_record)); strcpy(record.__mntpoint, "/run"); write(fd, &record, sizeof(struct __fsinfo_record)); strcpy(record.__mntpoint, "/var/run"); write(fd, &record, sizeof(struct __fsinfo_record)); close(fd); printf("init: executing startup script...\n"); if (fork() == 0) { if (chdir("/rootfs") != 0) { fprintf(stderr, "init: cannot switch to /rootfs: %s\n", strerror(errno)); _exit(1); }; if (chroot("/rootfs") != 0) { fprintf(stderr, "init: failed to set root directory to /rootfs: %s\n", strerror(errno)); _exit(1); }; execl("/bin/sh", "/bin/sh", "/etc/init/startup.sh", NULL); perror("init: exec"); _exit(1); }; while (1) { pause(); if (shouldHalt) { tcsetpgrp(0, getpgrp()); printf("init: received shutdown request\n"); int fd = open("/run/down-action", O_RDONLY); char downAction[256]; memset(downAction, 0, 256); downAction[read(fd, downAction, 16)] = 0; close(fd); int action = _GLIDIX_DOWN_HALT; if (strcmp(downAction, "poweroff") == 0) { action = _GLIDIX_DOWN_POWEROFF; } else if (strcmp(downAction, "reboot") == 0) { action = _GLIDIX_DOWN_REBOOT; }; shutdownSystem(action); } else if ((shouldRunPoweroff) && (!ranPoweroff)) { ranPoweroff = 1; if (fork() == 0) { if (execl("/usr/bin/halt", "poweroff", NULL) == -1) { perror("exec poweroff"); fprintf(stderr, "forcing shutdown\n"); kill(1, SIGTERM); exit(1); }; }; }; }; } else { fprintf(stderr, "%s: not allowed to execute with pid other than 1!\n", argv[0]); return 1; }; return 0; };