Skip to content

Commit

Permalink
Split out some things from the xX_main_Xx function
Browse files Browse the repository at this point in the history
  • Loading branch information
tbodt committed Oct 18, 2017
1 parent 60bf7e5 commit 93c254d
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 49 deletions.
52 changes: 52 additions & 0 deletions kernel/init.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#include <string.h>
#include <sys/stat.h>
#include "kernel/init.h"
#include "kernel/calls.h"
#include "fs/tty.h"

void mount_root(const char *source) {
mounts = malloc(sizeof(struct mount));
mounts->point = "";
mounts->source = strdup(source);
mounts->fs = &realfs;
mounts->next = NULL;
}

static void nop_handler() {}

// this function parses command line arguments and initializes global
// data structures. thanks programming discussions discord server for the name.
// https://discord.gg/9zT7NHP
int create_init_process(const char *program, char *const argv[], char *const envp[]) {
signal(SIGUSR1, nop_handler);

current = process_create();
current->cpu.mem = mem_new();
current->ppid = 1;
current->uid = current->gid = 0;
current->root = generic_open("/", O_RDONLY_, 0);
current->pwd = generic_dup(current->root);
current->umask = 0022;
current->thread = pthread_self();
sys_setsid();

return sys_execve(program, argv, envp);
}

int create_stdio(struct tty_driver driver) {
// I can't wait for when init and udev works and I don't need to do this
tty_drivers[TTY_VIRTUAL] = driver;

// FIXME use generic_open (or something) to avoid this mess
struct fd *fd = adhoc_fd_create();
fd->stat->rdev = dev_make(4, 0);
fd->stat->mode = S_IFCHR | S_IRUSR;
int err = dev_open(4, 0, DEV_CHAR, fd);
if (err < 0)
return err;
current->files[0] = fd;
current->files[1] = generic_dup(fd);
current->files[2] = generic_dup(fd);
return 0;
}

10 changes: 10 additions & 0 deletions kernel/init.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#ifndef KERNEL_INIT_H
#define KERNEL_INIT_H

#include "fs/tty.h"

void mount_root(const char *source);
int create_init_process(const char *program, char *const argv[], char *const envp[]);
int create_stdio(struct tty_driver driver);

#endif
2 changes: 2 additions & 0 deletions meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ cified_vdso = custom_target('cify-vdso',
command: [cify, 'vdso_data', vdso.full_path(), '@OUTPUT@'])

src = [
'kernel/init.c',

'kernel/calls.c',
'kernel/user.c',
'kernel/vdso.c',
Expand Down
56 changes: 7 additions & 49 deletions xX_main_Xx.h
Original file line number Diff line number Diff line change
@@ -1,21 +1,6 @@
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <sys/stat.h>
#include "kernel/calls.h"
#include "fs/tty.h"

static void mount_root(const char *source) {
mounts = malloc(sizeof(struct mount));
mounts->point = "";
mounts->source = strdup(source);
mounts->fs = &realfs;
mounts->next = NULL;
}

static void nop_handler() {}
#include "kernel/init.h"
#include "kernel/fs.h"

// this function parses command line arguments and initializes global
// data structures. thanks programming discussions discord server for the name.
Expand All @@ -36,43 +21,16 @@ static inline int xX_main_Xx(int argc, char *const argv[]) {

char root_realpath[MAX_PATH + 1] = "/";
if (has_root && realpath(root, root_realpath) == NULL) {
perror(root); exit(1);
perror(root);
exit(1);
}
mount_root(root_realpath);

signal(SIGUSR1, nop_handler);

// make a process
current = process_create();
current->cpu.mem = mem_new();
current->ppid = 1;
current->uid = current->gid = 0;
current->root = generic_open("/", O_RDONLY_, 0);
if (has_root)
current->pwd = generic_dup(current->root);
else
current->pwd = generic_open(getcwd(NULL, 0), O_RDONLY_, 0);
current->umask = 0022;
current->thread = pthread_self();
sys_setsid();

// I can't wait for when init and udev works and I don't need to do this
tty_drivers[TTY_VIRTUAL] = real_tty_driver;

// FIXME use generic_open (or something) to avoid this mess
struct fd *fd = adhoc_fd_create();
fd->stat->rdev = dev_make(4, 0);
fd->stat->mode = S_IFCHR | S_IRUSR;
int err = dev_open(4, 0, DEV_CHAR, fd);
char *envp[] = {NULL};
int err = create_init_process(argv[optind], argv + optind, envp);
if (err < 0)
return err;
current->files[0] = fd;
current->files[1] = generic_dup(fd);
current->files[2] = generic_dup(fd);

// go.
char *envp[] = {NULL};
err = sys_execve(argv[optind], argv + optind, envp);
err = create_stdio(real_tty_driver);
if (err < 0)
return err;
return 0;
Expand Down

0 comments on commit 93c254d

Please sign in to comment.