mirror of
https://github.com/johndoe6345789/SparkOS.git
synced 2026-04-24 13:34:56 +00:00
Implement SparkOS MVP: init system, build infrastructure, and rootfs
Co-authored-by: johndoe6345789 <224850594+johndoe6345789@users.noreply.github.com>
This commit is contained in:
99
src/init.c
Normal file
99
src/init.c
Normal file
@@ -0,0 +1,99 @@
|
||||
/*
|
||||
* SparkOS Init - Minimal init system for SparkOS
|
||||
* This is the first process that runs after the kernel boots
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/wait.h>
|
||||
#include <sys/reboot.h>
|
||||
#include <signal.h>
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
|
||||
#define INIT_PATH "/sbin/init"
|
||||
|
||||
static void signal_handler(int sig) {
|
||||
if (sig == SIGCHLD) {
|
||||
// Reap zombie processes
|
||||
while (waitpid(-1, NULL, WNOHANG) > 0);
|
||||
}
|
||||
}
|
||||
|
||||
static void spawn_shell() {
|
||||
pid_t pid = fork();
|
||||
|
||||
if (pid < 0) {
|
||||
perror("fork failed");
|
||||
return;
|
||||
}
|
||||
|
||||
if (pid == 0) {
|
||||
// Child process - exec shell
|
||||
char *argv[] = {"/bin/bash", "--login", NULL};
|
||||
char *envp[] = {
|
||||
"HOME=/root",
|
||||
"PATH=/bin:/sbin:/usr/bin:/usr/sbin",
|
||||
"TERM=linux",
|
||||
"PS1=SparkOS# ",
|
||||
NULL
|
||||
};
|
||||
|
||||
execve("/bin/bash", argv, envp);
|
||||
|
||||
// If bash fails, try sh
|
||||
char *sh_argv[] = {"/bin/sh", NULL};
|
||||
execve("/bin/sh", sh_argv, envp);
|
||||
|
||||
perror("failed to exec shell");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
// Parent process - wait for shell to exit
|
||||
int status;
|
||||
waitpid(pid, &status, 0);
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
printf("SparkOS Init System Starting...\n");
|
||||
|
||||
// Make sure we're PID 1
|
||||
if (getpid() != 1) {
|
||||
fprintf(stderr, "init must be run as PID 1\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Set up signal handlers
|
||||
signal(SIGCHLD, signal_handler);
|
||||
|
||||
// Mount essential filesystems
|
||||
printf("Mounting essential filesystems...\n");
|
||||
if (system("mount -t proc proc /proc 2>/dev/null") != 0) {
|
||||
fprintf(stderr, "Warning: Failed to mount /proc\n");
|
||||
}
|
||||
if (system("mount -t sysfs sys /sys 2>/dev/null") != 0) {
|
||||
fprintf(stderr, "Warning: Failed to mount /sys\n");
|
||||
}
|
||||
if (system("mount -t devtmpfs dev /dev 2>/dev/null") != 0) {
|
||||
fprintf(stderr, "Warning: Failed to mount /dev\n");
|
||||
}
|
||||
if (system("mount -t tmpfs tmpfs /tmp 2>/dev/null") != 0) {
|
||||
fprintf(stderr, "Warning: Failed to mount /tmp\n");
|
||||
}
|
||||
|
||||
printf("Starting shell...\n");
|
||||
printf("Welcome to SparkOS!\n");
|
||||
printf("===================\n\n");
|
||||
|
||||
// Main loop - keep respawning shell
|
||||
while (1) {
|
||||
spawn_shell();
|
||||
|
||||
// If shell exits, ask if user wants to reboot
|
||||
printf("\nShell exited. Press Ctrl+Alt+Del to reboot or wait for new shell...\n");
|
||||
sleep(2);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user