From b86fde14a13edabcb6b9552910c89e64f68a6e8e Mon Sep 17 00:00:00 2001 From: Ian Moffett Date: Sat, 31 May 2025 17:14:55 -0400 Subject: usr.bin: osh: Make kmsg its own program - Give command table entries a path field to refer to paths of executable files - Make kmsg its own program in /usr/bin/kmsg Signed-off-by: Ian Moffett --- usr.bin/Makefile | 1 + usr.bin/kmsg/Makefile | 6 +++++ usr.bin/kmsg/kmsg.c | 62 +++++++++++++++++++++++++++++++++++++++++++++++++++ usr.bin/osh/osh.c | 52 ++++++++++++++++++------------------------ 4 files changed, 91 insertions(+), 30 deletions(-) create mode 100644 usr.bin/kmsg/Makefile create mode 100644 usr.bin/kmsg/kmsg.c diff --git a/usr.bin/Makefile b/usr.bin/Makefile index 1c973ff..09d593d 100644 --- a/usr.bin/Makefile +++ b/usr.bin/Makefile @@ -6,3 +6,4 @@ ARGS = -I$(ROOT)/builddeps LDSCRIPT=$(LDSCRIPT) USRDIR=$(USRDIR) ROOT=$(ROOT) .PHONY: all all: make -C osh/ $(ARGS) + make -C kmsg/ $(ARGS) diff --git a/usr.bin/kmsg/Makefile b/usr.bin/kmsg/Makefile new file mode 100644 index 0000000..9b76cc2 --- /dev/null +++ b/usr.bin/kmsg/Makefile @@ -0,0 +1,6 @@ +include user.mk + +CFILES = $(shell find . -name "*.c") + +$(ROOT)/base/usr/bin/kmsg: + gcc $(CFILES) -o $@ $(INTERNAL_CFLAGS) diff --git a/usr.bin/kmsg/kmsg.c b/usr.bin/kmsg/kmsg.c new file mode 100644 index 0000000..678ad8c --- /dev/null +++ b/usr.bin/kmsg/kmsg.c @@ -0,0 +1,62 @@ +/* + * Copyright (c) 2023-2025 Ian Marco Moffett and the Osmora Team. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of Hyra nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#include +#include +#include +#include + +int +main(void) +{ + int mfd, cons_fd; + ssize_t retval; + char linebuf[256]; + + if ((mfd = open("/dev/kmsg", O_RDONLY)) < 0) { + return mfd; + } + if ((cons_fd = open("/dev/console", O_WRONLY)) < 0) { + close(mfd); + return cons_fd; + } + + for (;;) { + retval = read(mfd, linebuf, sizeof(linebuf) - 1); + if (retval <= 0) { + break; + } + linebuf[retval] = '\0'; + write(cons_fd, linebuf, strlen(linebuf)); + } + + close(cons_fd); + close(mfd); + return 0; +} diff --git a/usr.bin/osh/osh.c b/usr.bin/osh/osh.c index 9b4e9ab..4e16463 100644 --- a/usr.bin/osh/osh.c +++ b/usr.bin/osh/osh.c @@ -30,6 +30,7 @@ #include #include #include +#include #include #include #include @@ -60,6 +61,7 @@ static int running; struct command { const char *name; + const char *path; void (*func)(int fd, int argc, char *argv[]); }; @@ -87,29 +89,6 @@ cmd_shutdown(int fd, int argc, char *argv[]) cpu_reboot(REBOOT_POWEROFF | REBOOT_HALT); } -void -cmd_kmsg(int fd, int argc, char *argv[]) -{ - int mfd; - ssize_t retval; - char linebuf[256]; - - if ((mfd = open("/dev/kmsg", O_RDONLY)) < 0) { - return; - } - - for (;;) { - retval = read(mfd, linebuf, sizeof(linebuf) - 1); - if (retval <= 0) { - break; - } - linebuf[retval] = '\0'; - prcons(fd, linebuf); - } - - close(mfd); -} - void cmd_echo(int fd, int argc, char *argv[]) { @@ -189,13 +168,26 @@ getstr(int fd) } } +static void +command_run(struct command *cmd, int fd, int argc, char *argv[]) +{ + if (cmd->func != NULL) { + cmd->func(fd, argc, argv); + return; + } + + if (cmd->path != NULL) { + spawn(cmd->path, SPAWN_WAIT); + } +} + struct command cmds[] = { - {"help", cmd_help}, - {"echo", cmd_echo}, - {"exit", cmd_exit}, - {"reboot", cmd_reboot}, - {"shutdown", cmd_shutdown}, - {"kmsg", cmd_kmsg}, + {"help", NULL, cmd_help}, + {"echo", NULL, cmd_echo}, + {"exit", NULL, cmd_exit}, + {"reboot", NULL, cmd_reboot}, + {"shutdown", NULL, cmd_shutdown}, + {"kmsg", "/usr/bin/kmsg", NULL}, {NULL, NULL} }; @@ -230,7 +222,7 @@ main(void) for (i = 0; cmds[i].name != NULL; i++) { if (strcmp(input, cmds[i].name) == 0) { - cmds[i].func(fd, argc, argv); + command_run(&cmds[i], fd, argc, argv); found = 1; break; } -- cgit v1.2.3