toaruos/userspace/core/init.c

87 lines
2.0 KiB
C
Raw Normal View History

/* This file is part of ToaruOS and is released under the terms
* of the NCSA / University of Illinois License - see LICENSE.md
* Copyright (C) 2013-2014 Kevin Lange
*/
2012-01-30 22:10:53 +04:00
/* vim: tabstop=4 shiftwidth=4 noexpandtab
*
* init
*
* Provides the standard boot routines and
* calls the user session (compositor / terminal)
2012-01-30 22:10:53 +04:00
*/
#include <stdio.h>
2012-01-30 22:13:11 +04:00
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
2014-04-29 11:28:41 +04:00
#include <errno.h>
2012-01-30 22:10:53 +04:00
#include <syscall.h>
#include <sys/wait.h>
2012-01-30 22:10:53 +04:00
#define DEFAULT_HOSTNAME "toaru-test"
void set_console() {
int _stdin = open("/dev/null", O_RDONLY);
int _stdout = open("/dev/ttyS0", O_WRONLY);
int _stderr = open("/dev/ttyS0", O_WRONLY);
if (_stdout < 0) {
_stdout = open("/dev/null", O_WRONLY);
2014-04-06 02:44:38 +04:00
_stderr = open("/dev/null", O_WRONLY);
}
}
2012-01-30 22:10:53 +04:00
/* Set the hostname to whatever is in /etc/hostname */
void set_hostname() {
FILE * _host_file = fopen("/etc/hostname", "r");
if (!_host_file) {
/* No /etc/hostname, use the default */
syscall_sethostname(DEFAULT_HOSTNAME);
} else {
char buf[256];
fgets(buf, 255, _host_file);
2012-01-30 22:13:11 +04:00
if (buf[strlen(buf)-1] == '\n') {
buf[strlen(buf)-1] = '\0';
}
2012-01-30 22:10:53 +04:00
syscall_sethostname(buf);
setenv("HOST", buf, 1);
2012-01-30 22:10:53 +04:00
fclose(_host_file);
}
}
2014-03-28 10:37:38 +04:00
int start_options(char * args[]) {
2012-01-30 22:10:53 +04:00
int pid = fork();
if (!pid) {
2014-03-28 10:37:38 +04:00
int i = execvp(args[0], args);
exit(0);
} else {
2014-04-29 11:28:41 +04:00
int pid = 0;
do {
pid = wait(NULL);
} while ((pid > 0) || (pid == -1 && errno == EINTR));
}
}
int main(int argc, char * argv[]) {
/* stdin/out/err */
set_console();
2012-01-30 22:10:53 +04:00
/* Hostname */
set_hostname();
if (argc > 1) {
char * args = NULL;
if (argc > 2) {
args = argv[2];
}
if (!strcmp(argv[1],"--single")) {
2014-04-16 06:45:56 +04:00
return start_options((char *[]){"/bin/compositor","--","/bin/terminal","-Fl",args,NULL});
} else if (!strcmp(argv[1], "--vga")) {
2014-04-12 07:03:01 +04:00
return start_options((char *[]){"/bin/terminal-vga","-l",NULL});
} else {
/* Pass it to the compositor... */
return start_options((char *[]){"/bin/compositor","--",argv[1],NULL});
}
}
2014-03-28 10:37:38 +04:00
return start_options((char *[]){"/bin/compositor",NULL});
2012-01-30 22:10:53 +04:00
}