From 39d879f240d468ab5161afe724437a1ecb3eefdf Mon Sep 17 00:00:00 2001 From: Joachim Wiberg Date: Sat, 5 Jun 2021 14:25:05 +0200 Subject: [PATCH] Add support for auto-detetcing OS heading for progress This patch removes the cognitive overhead of having to manually set your OS heading, --with-heading="Foo OS vX.YY". As of this patch, Finit by default extracts PRETTY_NAME from /etc/os-release. It is now possible to also disable the heading entirely using --without-heading Signed-off-by: Joachim Wiberg --- configure.ac | 10 ++++++---- contrib/alpine/build.sh | 2 +- contrib/debian/build.sh | 3 +-- contrib/void/build.sh | 2 +- doc/build.md | 3 +-- src/finit.c | 10 +++++++++- src/finit.h | 1 + src/helpers.c | 43 +++++++++++++++++++++++++++++++++++++++++ src/helpers.h | 2 ++ src/sm.c | 5 ++++- 10 files changed, 69 insertions(+), 12 deletions(-) diff --git a/configure.ac b/configure.ac index 19e1a0bf..f74e35c7 100644 --- a/configure.ac +++ b/configure.ac @@ -86,7 +86,7 @@ AC_PLUGIN([urandom], [yes], [Setup and save random seed at boot/shutdown]) # Check for extra arguments or packages AC_ARG_WITH(heading, - AS_HELP_STRING([--with-heading=GREET], [Display GREET instead of Finit version at boot]), + AS_HELP_STRING([--with-heading=GREET], [Heading for boot progress. Default: PRETTY_NAME from /etc/os-release, fallback to "Finit vX.YY"]), [heading=$withval], [heading=yes]) AC_ARG_WITH(config, @@ -170,9 +170,8 @@ AS_IF([test "x$with_sysconfig" != "xno"], [ [System environment, default /etc/default & /etc/conf.d])])]) AS_IF([test "x$with_heading" != "xno"], [ - AS_IF([test "x$heading" = "xyes"], [ - heading="$PACKAGE_NAME v$PACKAGE_VERSION"])]) -AC_DEFINE_UNQUOTED(INIT_HEADING, "$heading", [Displayed by Finit at boot]) + AS_IF([test "x$heading" = "xyes"], [heading=""]) + AC_DEFINE_UNQUOTED(INIT_OSHEADING, "$heading", [Displayed by Finit at boot, defaults to /etc/os-release PRETTY_NAME])]) AS_IF([test "x$with_hostname" != "xno"], [ AS_IF([test "x$hostname" = "xyes"], [ @@ -222,6 +221,9 @@ AC_DEFINE_UNQUOTED(RESCUE_CONF, "$rescue_conf", [Finit rescue mode .conf]) AC_EXPAND_DIR(sample_conf, "$libdir/finit/sample.conf") AC_DEFINE_UNQUOTED(SAMPLE_CONF, "$sample_conf", [Finit sameple.conf for initctl create]) +AS_IF([test "$heading" = ""], [heading=""]) +AS_IF([test "$heading" = "no"], [heading=""]) + # Generate all files AC_OUTPUT diff --git a/contrib/alpine/build.sh b/contrib/alpine/build.sh index 07ef9a07..3b1df456 100755 --- a/contrib/alpine/build.sh +++ b/contrib/alpine/build.sh @@ -18,7 +18,7 @@ PKG_CONFIG_LIBDIR=/usr/lib/pkgconfig:/usr/local/lib/pkgconfig ./configure \ --sysconfdir=/etc --localstatedir=/var \ --enable-dbus-plugin --enable-x11-common-plugin \ --enable-alsa-utils-plugin --with-watchdog \ - --with-heading="Alpine Linux" --with-keventd + --with-keventd echo echo "*** Building ..." diff --git a/contrib/debian/build.sh b/contrib/debian/build.sh index 93f35cc3..3085f83e 100755 --- a/contrib/debian/build.sh +++ b/contrib/debian/build.sh @@ -26,8 +26,7 @@ echo --sysconfdir=/etc --localstatedir=/var \ --enable-dbus-plugin --enable-x11-common-plugin \ --enable-alsa-utils-plugin --with-keventd \ - --with-random-seed=/var/lib/urandom/random-seed \ - --with-heading="Debian GNU/Linux" + --with-random-seed=/var/lib/urandom/random-seed if [ $? -ne 0 ]; then echo diff --git a/contrib/void/build.sh b/contrib/void/build.sh index 4ce32916..8b4585e5 100755 --- a/contrib/void/build.sh +++ b/contrib/void/build.sh @@ -18,7 +18,7 @@ PKG_CONFIG_LIBDIR=/usr/lib/pkgconfig:/usr/local/lib/pkgconfig ./configure \ --sysconfdir=/etc --localstatedir=/var \ --enable-dbus-plugin --enable-x11-common-plugin \ --enable-alsa-utils-plugin --with-watchdog \ - --with-heading="Void Linux" --with-keventd + --with-keventd echo echo "*** Building ..." diff --git a/doc/build.md b/doc/build.md index c131a267..72bb501c 100644 --- a/doc/build.md +++ b/doc/build.md @@ -83,8 +83,7 @@ Then configure, build and install: ```shell $ ./configure --prefix=/usr --exec-prefix= \ --sysconfdir=/etc --localstatedir=/var \ - --with-keventd --with-watchdog \ - --with-heading="Alpine Linux" --with-hostname=alpine + --with-keventd --with-watchdog $ make . . diff --git a/src/finit.c b/src/finit.c index 229ee6d0..54f34d59 100644 --- a/src/finit.c +++ b/src/finit.c @@ -66,6 +66,7 @@ char *network = NULL; char *hostname = NULL; char *rcsd = FINIT_RCSD; char *runparts = NULL; +char *osheading = NULL; uev_ctx_t *ctx = NULL; /* Main loop context */ svc_t *wdog = NULL; /* No watchdog by default */ @@ -90,7 +91,14 @@ static void banner(void) */ plugin_run_hooks(HOOK_BANNER); - print_banner(INIT_HEADING); +#ifdef INIT_OSHEADING + osheading = INIT_OSHEADING; + if (osheading) { + if (!osheading[0]) + osheading = release_heading(); + print_banner(osheading); + } +#endif } /* diff --git a/src/finit.h b/src/finit.h index 8c13aba5..562dbf1f 100644 --- a/src/finit.h +++ b/src/finit.h @@ -115,6 +115,7 @@ extern char *sdown; extern char *network; extern char *hostname; extern char *runparts; +extern char *osheading; extern uev_ctx_t *ctx; #endif /* FINIT_H_ */ diff --git a/src/helpers.c b/src/helpers.c index 6a839af4..540bc69d 100644 --- a/src/helpers.c +++ b/src/helpers.c @@ -124,6 +124,49 @@ void show_progress(pstyle_t style) progress_onoff = style; } +/* + * Find PRETTY_NAME in /etc/os-release to use as heading at boot + * and on runlevel changes. Fallback: "Finit vX.YY" + */ +char *release_heading(void) +{ + char *name = NULL; + char buf[256]; + FILE *fp; + + fp = fopen("/etc/os-release", "r"); + if (fp) { + while (fgets(buf, sizeof(buf), fp)) { + char *ptr = &buf[12]; + + if (strncmp(buf, "PRETTY_NAME=", 12)) + continue; + + if (*ptr == '"' || *ptr == '\"') { + char *end = &ptr[strlen(ptr) - 1]; + char q = *ptr; + + while (end > ptr && isspace(*end)) + *end-- = 0; + + if (*end == q) { + *end = 0; + ptr++; + } + } + + name = strdup(ptr); + break; + } + fclose(fp); + } + + if (!name) + name = strdup(PACKAGE_NAME " v" PACKAGE_VERSION); + + return name; +} + /* * Return screen length of string, not counting escape chars, and * accounting for unicode characters as only one screen byte wide diff --git a/src/helpers.h b/src/helpers.h index 4e225f38..1ca9ff90 100644 --- a/src/helpers.h +++ b/src/helpers.h @@ -68,6 +68,8 @@ char *strip_line (char *line); void enable_progress (int onoff); void show_progress (pstyle_t style); +char *release_heading (void); + speed_t stty_parse_speed(char *baud); void stty (int fd, speed_t speed); diff --git a/src/sm.c b/src/sm.c index 6a48158c..8d708bc0 100644 --- a/src/sm.c +++ b/src/sm.c @@ -150,7 +150,10 @@ restart: } _d("Setting new runlevel --> %d <-- previous %d", runlevel, prevlevel); - logit(LOG_CONSOLE | LOG_NOTICE, "%s, entering runlevel %d", INIT_HEADING, runlevel); + if (osheading) + logit(LOG_CONSOLE | LOG_NOTICE, "%s, entering runlevel %d", osheading, runlevel); + else + logit(LOG_CONSOLE | LOG_NOTICE, "Entering runlevel %d", runlevel); runlevel_set(prevlevel, runlevel); /* Disable login in single-user mode as well as shutdown/reboot */