summaryrefslogtreecommitdiff
path: root/4rc/4rc.c
diff options
context:
space:
mode:
Diffstat (limited to '4rc/4rc.c')
-rw-r--r--4rc/4rc.c76
1 files changed, 66 insertions, 10 deletions
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;
}