2018-12-18 06:12:17 +03:00
|
|
|
/* vim: tabstop=4 shiftwidth=4 noexpandtab
|
|
|
|
* This file is part of ToaruOS and is released under the terms
|
|
|
|
* of the NCSA / University of Illinois License - see LICENSE.md
|
2021-06-07 16:00:20 +03:00
|
|
|
* Copyright (C) 2018-2021 K. Lange
|
2018-12-18 06:12:17 +03:00
|
|
|
*
|
|
|
|
* splash-log - Display startup messages before UI has started.
|
|
|
|
*/
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <string.h>
|
2021-07-19 13:43:57 +03:00
|
|
|
#include <signal.h>
|
2018-12-18 06:12:17 +03:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <sys/ioctl.h>
|
2021-06-07 16:00:20 +03:00
|
|
|
#include <sys/utsname.h>
|
2021-10-26 10:00:44 +03:00
|
|
|
#include <sys/times.h>
|
|
|
|
#include <sys/fswait.h>
|
2018-12-18 06:12:17 +03:00
|
|
|
|
|
|
|
#include <kernel/video.h>
|
|
|
|
#include <toaru/pex.h>
|
2021-10-26 10:00:44 +03:00
|
|
|
#include <toaru/hashmap.h>
|
2018-12-18 06:12:17 +03:00
|
|
|
|
|
|
|
#include "terminal-font.h"
|
|
|
|
|
2021-10-26 10:00:44 +03:00
|
|
|
#define TIMEOUT_SECS 2
|
|
|
|
|
2021-10-20 12:49:58 +03:00
|
|
|
static FILE * console;
|
2018-12-18 06:12:17 +03:00
|
|
|
|
2021-10-20 12:49:58 +03:00
|
|
|
static void update_message(char * c) {
|
|
|
|
fprintf(console, "%s\n", c);
|
2018-12-18 06:12:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static FILE * pex_endpoint = NULL;
|
|
|
|
static void open_socket(void) {
|
|
|
|
pex_endpoint = pex_bind("splash");
|
|
|
|
if (!pex_endpoint) exit(1);
|
|
|
|
}
|
|
|
|
|
2021-06-07 16:00:20 +03:00
|
|
|
static void say_hello(void) {
|
|
|
|
/* Get our release version */
|
|
|
|
struct utsname u;
|
|
|
|
uname(&u);
|
|
|
|
/* Strip git tag */
|
|
|
|
char * tmp = strstr(u.release, "-");
|
|
|
|
if (tmp) *tmp = '\0';
|
|
|
|
/* Setup hello message */
|
|
|
|
char hello_msg[512];
|
|
|
|
snprintf(hello_msg, 511, "ToaruOS %s is starting up...", u.release);
|
|
|
|
/* Add it to the log */
|
2021-10-20 12:49:58 +03:00
|
|
|
update_message(hello_msg);
|
2021-06-07 16:00:20 +03:00
|
|
|
}
|
|
|
|
|
2021-10-26 10:00:44 +03:00
|
|
|
static int tokenize(char * str, char * sep, char **buf) {
|
|
|
|
char * pch_i;
|
|
|
|
char * save_i;
|
|
|
|
int argc = 0;
|
|
|
|
pch_i = strtok_r(str,sep,&save_i);
|
|
|
|
if (!pch_i) { return 0; }
|
|
|
|
while (pch_i != NULL) {
|
|
|
|
buf[argc] = (char *)pch_i;
|
|
|
|
++argc;
|
|
|
|
pch_i = strtok_r(NULL,sep,&save_i);
|
|
|
|
}
|
|
|
|
buf[argc] = NULL;
|
|
|
|
return argc;
|
|
|
|
}
|
|
|
|
|
|
|
|
static hashmap_t * get_cmdline(void) {
|
|
|
|
int fd = open("/proc/cmdline", O_RDONLY);
|
|
|
|
char * out = malloc(1024);
|
|
|
|
size_t r = read(fd, out, 1024);
|
|
|
|
out[r] = '\0';
|
|
|
|
if (out[r-1] == '\n') {
|
|
|
|
out[r-1] = '\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
char * arg = strdup(out);
|
|
|
|
char * argv[1024];
|
|
|
|
int argc = tokenize(arg, " ", argv);
|
|
|
|
|
|
|
|
/* New let's parse the tokens into the arguments list so we can index by key */
|
|
|
|
|
|
|
|
hashmap_t * args = hashmap_create(10);
|
|
|
|
|
|
|
|
for (int i = 0; i < argc; ++i) {
|
|
|
|
char * c = strdup(argv[i]);
|
|
|
|
|
|
|
|
char * name;
|
|
|
|
char * value;
|
|
|
|
|
|
|
|
name = c;
|
|
|
|
value = NULL;
|
|
|
|
/* Find the first = and replace it with a null */
|
|
|
|
char * v = c;
|
|
|
|
while (*v) {
|
|
|
|
if (*v == '=') {
|
|
|
|
*v = '\0';
|
|
|
|
v++;
|
|
|
|
value = v;
|
|
|
|
goto _break;
|
|
|
|
}
|
|
|
|
v++;
|
|
|
|
}
|
|
|
|
|
|
|
|
_break:
|
|
|
|
hashmap_set(args, name, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
free(arg);
|
|
|
|
free(out);
|
|
|
|
|
|
|
|
return args;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-12-18 06:12:17 +03:00
|
|
|
int main(int argc, char * argv[]) {
|
|
|
|
if (getuid() != 0) {
|
|
|
|
fprintf(stderr, "%s: only root should run this\n", argv[0]);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!fork()) {
|
2021-10-26 10:00:44 +03:00
|
|
|
hashmap_t * cmdline = get_cmdline();
|
|
|
|
|
|
|
|
int quiet = 0;
|
2021-10-28 00:53:55 +03:00
|
|
|
char * last_message = NULL;
|
2021-10-26 10:00:44 +03:00
|
|
|
clock_t start = times(NULL);
|
|
|
|
|
|
|
|
if (!hashmap_has(cmdline, "debug")) {
|
|
|
|
quiet = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
open_socket();
|
|
|
|
console = fopen("/dev/console","a");
|
|
|
|
|
|
|
|
if (!quiet) say_hello();
|
2018-12-18 06:12:17 +03:00
|
|
|
|
|
|
|
while (1) {
|
2021-10-26 10:00:44 +03:00
|
|
|
int pex_fd[] = {fileno(pex_endpoint)};
|
|
|
|
int index = fswait2(1, pex_fd, 100);
|
|
|
|
|
|
|
|
if (index == 0) {
|
|
|
|
pex_packet_t * p = calloc(PACKET_SIZE, 1);
|
|
|
|
pex_listen(pex_endpoint, p);
|
|
|
|
|
|
|
|
if (p->size < 4) { free(p); continue; } /* Ignore blank messages, erroneous line feeds, etc. */
|
|
|
|
if (p->size > 80) { free(p); continue; } /* Ignore overly large messages */
|
|
|
|
|
|
|
|
if (!strncmp((char*)p->data, "!quit", 5)) {
|
|
|
|
/* Use the special message !quit to exit. */
|
|
|
|
fclose(pex_endpoint);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!quiet) {
|
|
|
|
p->data[p->size] = '\0';
|
|
|
|
update_message((char*)p->data + (p->data[0] == ':' ? 1 : 0));
|
|
|
|
}
|
|
|
|
|
2021-10-28 00:53:55 +03:00
|
|
|
if (last_message) {
|
|
|
|
free(last_message);
|
|
|
|
last_message = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (quiet) {
|
|
|
|
last_message = strdup((char*)p->data + (p->data[0] == ':' ? 1 : 0));
|
|
|
|
}
|
|
|
|
|
2021-10-26 10:00:44 +03:00
|
|
|
free(p);
|
|
|
|
} else if (quiet && times(NULL) - start > TIMEOUT_SECS * 1000000L) {
|
|
|
|
quiet = 0;
|
2021-10-28 00:53:55 +03:00
|
|
|
if (last_message) {
|
|
|
|
update_message("Startup is taking a while, enabling log. Last message was:");
|
|
|
|
update_message(last_message);
|
|
|
|
free(last_message);
|
|
|
|
last_message = NULL;
|
|
|
|
} else {
|
|
|
|
update_message("Startup is taking a while, enabling log.");
|
|
|
|
}
|
2018-12-18 06:12:17 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|