Last active
September 27, 2021 18:07
-
-
Save m1nuz/848f2b375a15575f04f28ce0ebd95b39 to your computer and use it in GitHub Desktop.
/dev/kmsg reader
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| cmake_minimum_required(VERSION 3.18.0) | |
| project(kmsgreader VERSION 0.1.0) | |
| set(APP_NAME "kmsgreader") | |
| set(THREADS_PREFER_PTHREAD_FLAG ON) | |
| find_package(Threads REQUIRED) | |
| add_executable(${APP_NAME} | |
| kmsgreader.c | |
| ) | |
| target_compile_features(${APP_NAME} | |
| PUBLIC | |
| c_std_11 | |
| ) | |
| target_compile_options(${APP_NAME} | |
| PUBLIC | |
| -pthread | |
| -pedantic | |
| -Wall | |
| -Wextra | |
| -Werror | |
| ) | |
| target_include_directories(${APP_NAME} | |
| PUBLIC | |
| $<BUILD_INTERFACE:${CMAKE_BINARY_DIR}> | |
| ) | |
| target_link_libraries(${APP_NAME} | |
| PUBLIC | |
| Threads::Threads | |
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // MIT License | |
| // | |
| // Copyright (c) 2021 Mykhailo Piddubnyi | |
| // | |
| // 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 <errno.h> | |
| #include <fcntl.h> | |
| #include <poll.h> | |
| #include <signal.h> | |
| #include <stdio.h> | |
| #include <stdlib.h> | |
| #include <string.h> | |
| #include <unistd.h> | |
| static void sig_handler(int signo) { | |
| if (signo == SIGINT) { | |
| puts("Program terminated!"); | |
| exit(0); | |
| } | |
| } | |
| static int open_kmsg(void) { | |
| int fd = open("/dev/kmsg", O_RDONLY | O_NONBLOCK); | |
| if (fd == -1) { | |
| perror("open"); | |
| return -1; | |
| } | |
| lseek(fd, 0, SEEK_END); | |
| return fd; | |
| } | |
| int main(int argc, char* argv[]) { | |
| (void)argc, (void)argv; | |
| signal(SIGINT, sig_handler); | |
| int fd = open_kmsg(); | |
| struct pollfd pfd; | |
| pfd.fd = fd; | |
| pfd.events = 0; | |
| pfd.revents = 0; | |
| pfd.events |= POLLIN; | |
| while (1) { | |
| int ret = poll(&pfd, 1, -1); | |
| if (ret > 0) { | |
| if (pfd.revents & POLLIN) { | |
| const size_t BUF_SIZE = 8192; | |
| char buf[BUF_SIZE + 1]; | |
| // https://www.kernel.org/doc/Documentation/ABI/testing/dev-kmsg | |
| // Every read() call gives us one record | |
| ssize_t nbytes = read(pfd.fd, buf, BUF_SIZE); | |
| if (nbytes > 0) { | |
| buf[nbytes] = '\0'; | |
| printf("%s\n", buf); | |
| } else if (nbytes == -1) { | |
| perror("read"); | |
| } else if (nbytes == 0) { | |
| puts("EOF"); | |
| } else if (nbytes == -EPIPE) { | |
| puts("Some messages in circular buffer got overwritten"); | |
| continue; | |
| } else if (nbytes == -EAGAIN) { | |
| puts("-EAGAIN"); | |
| } | |
| } | |
| } | |
| } | |
| close(fd); | |
| return EXIT_SUCCESS; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment