ish/kernel/init.c
Theodore Dubois cef1841a40 Make it possible to run init
If you set the init command to /sbin/init, you'll see it fail to run openrc and then start a working getty.
2019-05-01 08:53:25 -07:00

96 lines
2.7 KiB
C

#include <signal.h>
#include <string.h>
#include <sys/stat.h>
#include "kernel/init.h"
#include "kernel/calls.h"
#include "fs/fd.h"
#include "fs/tty.h"
int mount_root(const struct fs_ops *fs, const char *source) {
char source_realpath[MAX_PATH + 1];
if (realpath(source, source_realpath) == NULL)
return errno_map();
int err = do_mount(fs, source_realpath, "");
if (err < 0)
return err;
return 0;
}
static struct tgroup *init_tgroup() {
struct tgroup *group = malloc(sizeof(struct tgroup));
if (group == NULL)
return NULL;
*group = (struct tgroup) {};
list_init(&group->threads);
lock_init(&group->lock);
cond_init(&group->child_exit);
cond_init(&group->stopped_cond);
return group;
}
void create_first_process() {
extern void sigusr1_handler(int sig);
struct sigaction sigact;
sigact.sa_handler = sigusr1_handler;
sigact.sa_flags = 0;
sigemptyset(&sigact.sa_mask);
sigaddset(&sigact.sa_mask, SIGUSR1);
sigaction(SIGUSR1, &sigact, NULL);
signal(SIGPIPE, SIG_IGN);
current = task_create_(NULL);
current->mm = mm_new();
current->mem = current->cpu.mem = &current->mm->mem;
struct tgroup *group = init_tgroup();
list_add(&group->threads, &current->group_links);
group->leader = current;
current->group = group;
current->tgid = current->pid;
struct fs_info *fs = fs_info_new();
current->fs = fs;
fs->pwd = fs->root = generic_open("/", O_RDONLY_, 0);
fs->pwd->refcount = 2;
fs->umask = 0022;
current->files = fdtable_new(3);
current->sighand = sighand_new();
for (int i = 0; i < RLIMIT_NLIMITS_; i++)
group->limits[i].cur = group->limits[i].max = RLIM_INFINITY_;
// python subprocess uses this limit as a way to close every open file
group->limits[RLIMIT_NOFILE_].cur = 1024;
current->thread = pthread_self();
sys_setsid();
}
extern int console_major;
extern int console_minor;
void set_console_device(int major, int minor) {
console_major = major;
console_minor = minor;
}
int create_stdio(const char *file, struct tty_driver *driver) {
tty_drivers[TTY_CONSOLE_MAJOR] = driver;
struct fd *fd = generic_open(file, O_RDWR_, 0);
if (fd == ERR_PTR(_ENOENT)) {
// fallback to adhoc files for stdio
fd = adhoc_fd_create(NULL);
fd->stat.rdev = dev_make(4, 1);
fd->stat.mode = S_IFCHR | S_IRUSR;
fd->flags = O_RDWR_;
int err = dev_open(4, 1, DEV_CHAR, fd);
if (err < 0)
return err;
}
if (IS_ERR(fd))
return PTR_ERR(fd);
fd->refcount = 3;
current->files->files[0] = fd;
current->files->files[1] = fd;
current->files->files[2] = fd;
return 0;
}