summaryrefslogtreecommitdiff
path: root/4rc/4rc.c
diff options
context:
space:
mode:
Diffstat (limited to '4rc/4rc.c')
-rw-r--r--4rc/4rc.c112
1 files changed, 0 insertions, 112 deletions
diff --git a/4rc/4rc.c b/4rc/4rc.c
deleted file mode 100644
index 35a85d3..0000000
--- a/4rc/4rc.c
+++ /dev/null
@@ -1,112 +0,0 @@
-#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) {
- 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 &&
- errno != EBUSY) {
- perror("mount /proc");
- }
-
- if (mount("sys", "/sys", "sysfs",
- 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 &&
- errno != EBUSY) {
- perror("mount /dev");
- }
-
- if (mount("run", "/run", "tmpfs", MS_NOSUID | MS_NODEV | MS_RELATIME,
- "mode=755") != 0 &&
- errno != EBUSY) {
- perror("mount /run");
- }
-
- mkdir("/dev/pts", 0755);
- mkdir("/dev/shm", 1777);
-
- if (mount("devpts", "/dev/pts", "devpts",
- 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 &&
- 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;
-}