summary refs log tree commit diff
path: root/sbin
diff options
context:
space:
mode:
Diffstat (limited to 'sbin')
-rw-r--r--sbin/fdmaker/fdmaker.c42
-rw-r--r--sbin/init/init.c26
-rw-r--r--sbin/init/sinit.c96
3 files changed, 164 insertions, 0 deletions
diff --git a/sbin/fdmaker/fdmaker.c b/sbin/fdmaker/fdmaker.c
new file mode 100644
index 0000000..c504181
--- /dev/null
+++ b/sbin/fdmaker/fdmaker.c
@@ -0,0 +1,42 @@
+#include <stdlib.h>
+#include <stdio.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <sys/ioctl.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <sys/sysmacros.h>
+#include <string.h>
+
+int main() {
+    // make process be sigma (why did i think of this)
+    if (setsid() < 0) {
+        perror("setsid");
+        exit(1);
+    }
+
+    // >open /dev/tty1
+    // >look inside
+    // >Failed to open /dev/tty1
+    int fd = open("/dev/tty1", O_RDWR);
+    if (fd == -1) {
+        write(2, "Failed to open /dev/tty1\n", 26);
+        exit(1);
+    }
+    // make tty1 be the displayed tty
+    if (ioctl(fd, TIOCSCTTY, 0) < 0) {
+        write(2, "ioctl TIOCSCTTY", 16);
+        exit(1);
+    }
+    char path[128] = {0};
+    write(fd, "\nEnter path to shell (nothing for /bin/shel): ", 47);
+    int len = read(0, path, sizeof(path));
+    path[len - 1] = '\0';
+    if (len == 1) {
+        strcpy(path, "/bin/shel");
+    }
+    // write(1, path, strlen(path));
+    write(fd, "\e[2J\e[0;0Hwelcome to nyaOS - a distro made by an idiot with no C experience that still somehow works\n\n", 103);
+    
+    execve(path, 0, 0);
+}
\ No newline at end of file
diff --git a/sbin/init/init.c b/sbin/init/init.c
new file mode 100644
index 0000000..168d011
--- /dev/null
+++ b/sbin/init/init.c
@@ -0,0 +1,26 @@
+#include <unistd.h>
+#include <signal.h>
+
+int main() {
+	sigset_t set;
+	int status;
+    write(1, "nyaOS init process started\n", 28);
+	sigfillset(&set);
+	sigprocmask(SIG_BLOCK, &set, 0);
+    if (fork()) pause();
+    //TODO - eventually add device scan
+    if (!fork()) execve("/sbin/devscan", 0, 0);
+
+    sigprocmask(SIG_UNBLOCK, &set, 0);
+
+    // setsid();
+    setpgid(0, 0);
+    
+    write(1, "press RETURN to\n\n", 17);
+    char a[2] = {0};
+    read(0, a, 1);
+    // make tty1
+    execve("/sbin/fdmaker", 0, 0);
+    // execve("/sbin/devscan", 0, 0);
+    // execve("/sbin/fdmaker", 0, 0);
+}
\ No newline at end of file
diff --git a/sbin/init/sinit.c b/sbin/init/sinit.c
new file mode 100644
index 0000000..b29f880
--- /dev/null
+++ b/sbin/init/sinit.c
@@ -0,0 +1,96 @@
+/* See LICENSE file for copyright and license details. */
+#include <sys/types.h>
+#include <sys/wait.h>
+
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+#define LEN(x)	(sizeof (x) / sizeof *(x))
+#define TIMEO	30
+
+static void sigpoweroff(void);
+static void sigreap(void);
+static void sigreboot(void);
+static void spawn(char *const []);
+
+static struct {
+	int sig;
+	void (*handler)(void);
+} sigmap[] = {
+	{ SIGUSR1, sigpoweroff },
+	{ SIGCHLD, sigreap     },
+	{ SIGALRM, sigreap     },
+	{ SIGINT,  sigreboot   },
+};
+
+/* See LICENSE file for copyright and license details. */
+
+static char *const rcinitcmd[]     = { "/bin/zsh", NULL };
+static char *const rcrebootcmd[]   = { "/bin/rc.shutdown", "reboot", NULL };
+static char *const rcpoweroffcmd[] = { "/bin/rc.shutdown", "poweroff", NULL };
+
+
+static sigset_t set;
+
+int
+main(void)
+{
+	int sig;
+	size_t i;
+
+	if (getpid() != 1)
+		return 1;
+	chdir("/");
+	sigfillset(&set);
+	sigprocmask(SIG_BLOCK, &set, NULL);
+	spawn(rcinitcmd);
+	while (1) {
+		alarm(TIMEO);
+		sigwait(&set, &sig);
+		for (i = 0; i < LEN(sigmap); i++) {
+			if (sigmap[i].sig == sig) {
+				sigmap[i].handler();
+				break;
+			}
+		}
+	}
+	/* not reachable */
+	return 0;
+}
+
+static void
+sigpoweroff(void)
+{
+	spawn(rcpoweroffcmd);
+}
+
+static void
+sigreap(void)
+{
+	while (waitpid(-1, NULL, WNOHANG) > 0)
+		;
+	alarm(TIMEO);
+}
+
+static void
+sigreboot(void)
+{
+	spawn(rcrebootcmd);
+}
+
+static void
+spawn(char *const argv[])
+{
+	switch (fork()) {
+	case 0:
+		sigprocmask(SIG_UNBLOCK, &set, NULL);
+		setsid();
+		execvp(argv[0], argv);
+		perror("execvp");
+		_exit(1);
+	case -1:
+		perror("fork");
+	}
+}