2021-11-26 06:41:56 +03:00
|
|
|
/**
|
|
|
|
* @brief Console log manager.
|
|
|
|
*
|
|
|
|
* Presents a PEX endpoint for startup processes to write log
|
|
|
|
* messages to and will only send them to the console if the
|
|
|
|
* debug flag is set or 2 seconds have elapsed since we started.
|
|
|
|
*
|
|
|
|
* This also makes message writes a bit more asynchonrous, which
|
|
|
|
* is useful because the framebuffer console output can be quite
|
|
|
|
* slow and we don't want to slow down start up processes...
|
|
|
|
*
|
|
|
|
* The downside to that is that splash-log may have to play catch-up
|
|
|
|
* and could still be spewing messages to the console after startup
|
|
|
|
* has finished...
|
|
|
|
*
|
|
|
|
* This used to do a lot more work, as it managed both graphical
|
|
|
|
* output of messages and output to the VGA terminal, but that has
|
|
|
|
* all moved back into the kernel's 'fbterm', so now we're just
|
|
|
|
* a message buffer.
|
|
|
|
*
|
|
|
|
* @copyright
|
2018-12-18 06:12:17 +03:00
|
|
|
* 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
|
|
|
*/
|
|
|
|
#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
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2023-10-09 14:01:42 +03:00
|
|
|
#include "../kernel/misc/args.c"
|
2021-10-26 10:00:44 +03:00
|
|
|
static hashmap_t * get_cmdline(void) {
|
2023-10-09 14:01:42 +03:00
|
|
|
char * results = args_from_procfs();
|
|
|
|
if (results) free(results);
|
|
|
|
return kernel_args_map;
|
2021-10-26 10:00:44 +03:00
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|