commit 755684bce7d7c7e0f730cbbbd2d3cb80490f4997 Author: Alinur Songül Date: Wed Nov 3 20:46:17 2021 +0200 Add code diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..d85785e --- /dev/null +++ b/LICENSE @@ -0,0 +1,22 @@ +MIT License + +Copyright (c) 2021 Alinur Songül + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..c887f02 --- /dev/null +++ b/Makefile @@ -0,0 +1,16 @@ +all=midmesg + +CC=gcc +CFLAGS=-Wall -Wextra -o midmesg + +midmesg: midmesg.c + $(CC) midmesg.c $(CFLAGS) + +install: + cp midmesg /usr/local/bin/ + +uninstall: + rm -f /usr/local/bin/midmesg + +clean: + rm -f midmesg diff --git a/README.md b/README.md new file mode 100644 index 0000000..6bd29fe --- /dev/null +++ b/README.md @@ -0,0 +1,26 @@ +# midmesg +**Mi**cro **dmesg** is a lighterweight alternative to dmesg. + +## Dependencies +- **midmesg** should work perfectly fine on both musl libc and glibc. +- **midmesg** requires header files for your Linux kernel. + +Your Linux distribution should bundle them in a package named `linux-headers` or something similar to that. + +## Installation +After you've cloned the Git repository, use these commands to compile and install **midmesg**. +``` +make +doas make install +``` + +To uninstall **midmesg**, use `doas make uninstall`. + +## Tasks +- find some way to retain logs +According to the [klogctl(3)](http://man.he.net/?topic=klogctl§ions=3) man page: + +> Bytes read from the log disappear from the log buffer: +> the information can be read only once. + +- use a built-in pager instead of printing logs to the terminal or hoping that `less` exists on the system diff --git a/midmesg.c b/midmesg.c new file mode 100644 index 0000000..2f174b1 --- /dev/null +++ b/midmesg.c @@ -0,0 +1,71 @@ +/* midmesg - Display Linux kernel log information + * + * MIT License + * + * Copyright (c) 2021 Alinur Songül + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. */ + +#include +#include +#include +#include +#include +#include + +#define PROGRAM "midmesg" + +#define eprintf(...) fprintf(stderr, __VA_ARGS__) + +int main() +{ + int kbuf_unread_size; + + /* Get the number of all unread bytes in + * the kernel ring buffer */ + kbuf_unread_size = klogctl(9, NULL, 0); + + if (kbuf_unread_size < 0) + { + eprintf("%s: unable to read log buffer: %s\n", + PROGRAM, strerror(errno)); + exit(errno); + } + + + /* allocate memory for kernel log to be stored in memory */ + char* kbuf = (char*)malloc(kbuf_unread_size); + + printf("Waiting for klogctl..."); + int read_bytes = klogctl(2, kbuf, kbuf_unread_size); + + if (read_bytes != -1) + printf("Read %d bytes\n\n", read_bytes); + else { + eprintf("%s\n", strerror(errno)); + free(kbuf); /* free up leftover allocated memory */ + exit(errno); + } + + printf("%s\n", kbuf); + free(kbuf); + + + return 0; +}