2018-06-29 07:55:03 +03:00
|
|
|
#include <stdio.h>
|
2018-02-25 08:13:54 +03:00
|
|
|
#include <syscall.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <wait.h>
|
2018-03-04 16:50:53 +03:00
|
|
|
#include <string.h>
|
2018-08-01 21:40:43 +03:00
|
|
|
#include <unistd.h>
|
2018-02-25 08:13:54 +03:00
|
|
|
|
2018-08-01 21:40:43 +03:00
|
|
|
void set_console(void) {
|
2018-08-02 12:48:45 +03:00
|
|
|
int _stdin = syscall_open("/dev/ttyS0", 0, 0);
|
2018-02-25 08:13:54 +03:00
|
|
|
int _stdout = syscall_open("/dev/ttyS0", 1, 0);
|
|
|
|
int _stderr = syscall_open("/dev/ttyS0", 1, 0);
|
|
|
|
|
|
|
|
if (_stdout < 0) {
|
|
|
|
_stdout = syscall_open("/dev/null", 1, 0);
|
|
|
|
_stderr = syscall_open("/dev/null", 1, 0);
|
|
|
|
}
|
2018-04-25 08:03:29 +03:00
|
|
|
|
|
|
|
(void)_stderr;
|
|
|
|
(void)_stdin;
|
2018-02-25 08:13:54 +03:00
|
|
|
}
|
|
|
|
|
2018-08-01 21:40:43 +03:00
|
|
|
void set_hostname(void) {
|
|
|
|
FILE * f = fopen("/etc/hostname", "r");
|
|
|
|
|
|
|
|
if (!f) {
|
|
|
|
/* set fallback hostname */
|
2018-08-01 21:42:25 +03:00
|
|
|
sethostname("localhost", 4);
|
2018-08-01 21:40:43 +03:00
|
|
|
} else {
|
|
|
|
char tmp[128];
|
|
|
|
fgets(tmp, 128, f);
|
|
|
|
char * nl = strchr(tmp, '\n');
|
|
|
|
if (nl) *nl = '\0';
|
|
|
|
sethostname(tmp, strlen(tmp));
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-02-25 08:13:54 +03:00
|
|
|
int start_options(char * args[]) {
|
2018-08-02 12:48:45 +03:00
|
|
|
int cpid = syscall_fork();
|
|
|
|
if (!cpid) {
|
2018-02-25 08:13:54 +03:00
|
|
|
char * _envp[] = {
|
2018-06-25 10:46:13 +03:00
|
|
|
"LD_LIBRARY_PATH=/lib:/usr/lib",
|
|
|
|
"PATH=/bin:/usr/bin",
|
2018-02-25 08:13:54 +03:00
|
|
|
"USER=root",
|
2018-05-10 16:33:32 +03:00
|
|
|
"HOME=/home/root",
|
2018-02-25 08:13:54 +03:00
|
|
|
NULL,
|
|
|
|
};
|
2018-04-25 08:03:29 +03:00
|
|
|
syscall_execve(args[0], args, _envp);
|
2018-02-25 08:13:54 +03:00
|
|
|
syscall_exit(0);
|
|
|
|
} else {
|
|
|
|
int pid = 0;
|
|
|
|
do {
|
|
|
|
pid = wait(NULL);
|
2018-08-02 12:48:45 +03:00
|
|
|
if (pid == cpid) {
|
|
|
|
break;
|
|
|
|
}
|
2018-02-25 08:13:54 +03:00
|
|
|
} while ((pid > 0) || (pid == -1 && errno == EINTR));
|
|
|
|
}
|
2018-08-02 12:48:45 +03:00
|
|
|
|
|
|
|
syscall_reboot();
|
|
|
|
|
2018-04-25 08:03:29 +03:00
|
|
|
return 0;
|
2018-02-25 08:13:54 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char * argv[]) {
|
|
|
|
set_console();
|
2018-08-01 21:40:43 +03:00
|
|
|
set_hostname();
|
2018-03-04 16:50:53 +03:00
|
|
|
|
|
|
|
if (argc > 1) {
|
|
|
|
if (!strcmp(argv[1], "--vga")) {
|
2018-05-10 16:33:32 +03:00
|
|
|
return start_options((char *[]){"/bin/terminal-vga","-l",NULL});
|
2018-03-04 16:50:53 +03:00
|
|
|
} else if (!strcmp(argv[1], "--migrate")) {
|
2018-03-15 13:30:04 +03:00
|
|
|
return start_options((char *[]){"/bin/migrate",NULL});
|
2018-07-06 04:40:49 +03:00
|
|
|
} else if (!strcmp(argv[1], "--headless")) {
|
|
|
|
return start_options((char *[]){"/bin/getty",NULL});
|
2018-03-04 16:50:53 +03:00
|
|
|
} else {
|
|
|
|
/* Pass it to the compositor... */
|
|
|
|
return start_options((char *[]){"/bin/compositor","--",argv[1],NULL});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return start_options((char *[]){"/bin/compositor",NULL});
|
2018-02-25 08:13:54 +03:00
|
|
|
}
|