From 7c1a8d932acfdae0cb75f5926cdcbf9b240f5804 Mon Sep 17 00:00:00 2001 From: Jesse Smith Date: Mon, 12 Nov 2018 20:29:28 -0400 Subject: [PATCH] Added new tool (readbootlog) which will read the /var/log/boot file produced by bootlogd. The output is displayed cleaned up so there are no control characters. This avoids the need to use sed or related tools to try to clean up the contents of the log. --- src/Makefile | 2 +- src/bootlogd.c | 6 +-- src/bootlogd.h | 12 ++++++ src/readbootlog.c | 101 ++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 116 insertions(+), 5 deletions(-) create mode 100644 src/bootlogd.h create mode 100644 src/readbootlog.c diff --git a/src/Makefile b/src/Makefile index 258912e..7b5b622 100644 --- a/src/Makefile +++ b/src/Makefile @@ -137,7 +137,7 @@ shutdown: LDLIBS += $(STATIC) shutdown: dowall.o shutdown.o utmp.o bootlogd: LDLIBS += -lutil $(STATIC) -bootlogd: bootlogd.o +bootlogd: bootlogd.o bootlogd.h fstab-decode: LDLIBS += $(STATIC) fstab-decode: fstab-decode.o diff --git a/src/bootlogd.c b/src/bootlogd.c index 0d4d4b4..c8c06d6 100644 --- a/src/bootlogd.c +++ b/src/bootlogd.c @@ -53,10 +53,8 @@ #ifdef __linux__ #include #endif +#include "bootlogd.h" -char *Version = "@(#) bootlogd 2.86 03-Jun-2004 miquels@cistron.nl"; - -#define LOGFILE "/var/log/boot" #define MAX_CONSOLES 16 char ringbuf[32768]; @@ -508,7 +506,7 @@ int main(int argc, char **argv) rotate = 1; break; case 'v': - printf("%s\n", Version); + printf("bootlogd - %s\n", VERSION); exit(0); break; case 'p': diff --git a/src/bootlogd.h b/src/bootlogd.h new file mode 100644 index 0000000..b47bc9d --- /dev/null +++ b/src/bootlogd.h @@ -0,0 +1,12 @@ +#ifndef LOGFILE +#define LOGFILE "/var/log/boot" +#endif + +#ifndef TRUE +#define TRUE 1 +#endif + +#ifndef FALSE +#define FALSE 0 +#endif + diff --git a/src/readbootlog.c b/src/readbootlog.c new file mode 100644 index 0000000..a7d2a1d --- /dev/null +++ b/src/readbootlog.c @@ -0,0 +1,101 @@ +#include +#include +#include "bootlogd.h" + +#ifndef MAX_LINE +#define MAX_LINE 256 +#endif + +void print_usage() +{ + +} + +/* +Clean up the unwanted characters from a line of input. +Cleaned line is passed back in output_line. +Returns TRUE on success or FALSE if we encounter an error. +*/ +int Clean_Line(char *source_line, char *output_line) +{ + int source_index = 0, target_index = 0; + int source_max_index; + char a_letter; + int done; + char *garbage; + + if (! source_line) return FALSE; + if (! output_line) return FALSE; + + source_max_index = strlen(source_line); + while (source_index < source_max_index) + { + a_letter = source_line[source_index]; + if (a_letter == '^') + { + /* skip ahead until we find a valid place to stop */ + done = FALSE; + while (! done) + { + source_index++; + if (source_index >= source_max_index) + done = TRUE; + else + { + a_letter = source_line[source_index]; + if ( (a_letter == '.') || (a_letter == ' ') || + (a_letter == '(') || (a_letter == 'd') || + (a_letter == '\n') ) + done = TRUE; + } + } + } /* done found character to scrub */ + else + { + output_line[target_index] = a_letter; + target_index++; + source_index++; + } /* found valid character */ + } /* done processing line */ + + garbage = strstr(output_line, " .\n"); + if (garbage) + { + garbage[0] = '\n'; + garbage[1] = '\0'; + } + return TRUE; +} + + +int main(int argc, char *argv[]) +{ + FILE *log_file = NULL; + char *log_filename = LOGFILE; + char line[MAX_LINE]; + char output[MAX_LINE]; + char *status; + + log_file = fopen(log_filename, "r"); + if (log_file) + { + status = fgets(line, MAX_LINE, log_file); + while (status) + { + memset(output, '\0', MAX_LINE); + if ( Clean_Line(line, output) ) + { + printf("%s", output); + } + status = fgets(line, MAX_LINE, log_file); + } /* done reading file lines */ + fclose(log_file); + } /* end of successfully opened log file */ + else + { + fprintf(stderr, "Unable to open file %s\n", log_filename); + return 1; + } + + return 0; +}