summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--4init/config.h2
-rwxr-xr-x4rc/4rcbin17872 -> 17872 bytes
-rw-r--r--4rc/4rc.c76
-rw-r--r--4rc/README.md6
4 files changed, 69 insertions, 15 deletions
diff --git a/4init/config.h b/4init/config.h
index 3798447..2ff1d6d 100644
--- a/4init/config.h
+++ b/4init/config.h
@@ -1,7 +1,7 @@
#ifndef CONFIG_H
#define CONFIG_H
-static const char *rc_path = "/usr/bin/4rc";
+static const char *rc_path = "/usr/local/bin/4rc";
static char *const rc_argv[] = {"4rc", NULL};
diff --git a/4rc/4rc b/4rc/4rc
index 1df6d60..250b63a 100755
--- a/4rc/4rc
+++ b/4rc/4rc
Binary files differ
diff --git a/4rc/4rc.c b/4rc/4rc.c
index 68a5d9f..35a85d3 100644
--- a/4rc/4rc.c
+++ b/4rc/4rc.c
@@ -1,36 +1,52 @@
+#define _DEFAULT_SOURCE
#define _POSIX_C_SOURCE 200809L
+#include <errno.h>
+#include <fcntl.h>
#include <stdio.h>
+#include <string.h>
#include <sys/mount.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
void create_vfs(void) {
- mkdir("/proc", 0755);
- mkdir("/sys", 0755);
- mkdir("/dev", 0755);
- mkdir("/run", 0755);
+ if (mkdir("/proc", 0755) != 0 && errno != EEXIST) {
+ perror("proc");
+ }
+ if (mkdir("/sys", 0755) != 0 && errno != EEXIST) {
+ perror("sys");
+ }
+ if (mkdir("/dev", 0755) != 0 && errno != EEXIST) {
+ perror("dev");
+ }
+ if (mkdir("/run", 0755) != 0 && errno != EEXIST) {
+ perror("run");
+ }
}
void mount_vfs(void) {
if (mount("proc", "/proc", "proc",
- MS_NOSUID | MS_NODEV | MS_NOEXEC | MS_RELATIME, NULL) != 0) {
+ MS_NOSUID | MS_NODEV | MS_NOEXEC | MS_RELATIME, NULL) != 0 &&
+ errno != EBUSY) {
perror("mount /proc");
}
if (mount("sys", "/sys", "sysfs",
- MS_NOSUID | MS_NODEV | MS_NOEXEC | MS_RELATIME, NULL) != 0) {
+ MS_NOSUID | MS_NODEV | MS_NOEXEC | MS_RELATIME, NULL) != 0 &&
+ errno != EBUSY) {
perror("mount /sys");
}
if (mount("dev", "/dev", "devtmpfs", MS_NOSUID | MS_RELATIME, "mode=755") !=
- 0) {
+ 0 &&
+ errno != EBUSY) {
perror("mount /dev");
}
if (mount("run", "/run", "tmpfs", MS_NOSUID | MS_NODEV | MS_RELATIME,
- "mode=755") != 0) {
+ "mode=755") != 0 &&
+ errno != EBUSY) {
perror("mount /run");
}
@@ -38,19 +54,59 @@ void mount_vfs(void) {
mkdir("/dev/shm", 1777);
if (mount("devpts", "/dev/pts", "devpts",
- MS_NOSUID | MS_NOEXEC | MS_RELATIME, "gid=5,mode=620") != 0) {
+ MS_NOSUID | MS_NOEXEC | MS_RELATIME, "gid=5,mode=620") != 0 &&
+ errno != EBUSY) {
perror("mount /dev/pts");
}
if (mount("shm", "/dev/shm", "tmpfs", MS_NOSUID | MS_NODEV | MS_RELATIME,
- "mode=1777") != 0) {
+ "mode=1777") != 0 &&
+ errno != EBUSY) {
perror("mount /dev/shm");
}
}
+void set_hostname(void) {
+ char buf[256];
+ int fd = open("/etc/hostname", O_RDONLY);
+
+ if (fd < 0) {
+ perror("open /etc/hostname");
+ return;
+ }
+
+ ssize_t bytes_read = read(fd, buf, sizeof(buf) - 1);
+ close(fd);
+
+ if (bytes_read > 0) {
+ buf[bytes_read] = '\0';
+ size_t len = strcspn(buf, "\r\n ");
+ buf[len] = '\0';
+
+ if (sethostname(buf, len) != 0) {
+ perror("sethostname");
+ }
+ }
+}
+
int main(void) {
create_vfs();
mount_vfs();
+ int fd = open("/dev/console", O_RDWR);
+
+ if (fd >= 0) {
+ dup2(fd, 0);
+ dup2(fd, 1);
+ dup2(fd, 2);
+ if (fd > 2) {
+ close(fd);
+ }
+ } else {
+ perror("console");
+ }
+
+ set_hostname();
+
return 0;
}
diff --git a/4rc/README.md b/4rc/README.md
index 44fbde5..9309fc9 100644
--- a/4rc/README.md
+++ b/4rc/README.md
@@ -1,5 +1,3 @@
-# 4init: minimal but functional init
+# 4rc: service manager that larps as being functional
-It's only purpose is to manage processes, it by defaults calls 4rc, but you can change that in `config.h`, it's started as PID 1, and if it crashes, the kernel will panic, read the article in Wikipedia if you want to know more.
-
-Inspired by [rofl0r](https://gist.github.com/rofl0r/6168719)'s minimal init. \ No newline at end of file
+service manager, not much else to explain other for now..