From 1ac7bd87535f1147de6343673eef39134725e6f1 Mon Sep 17 00:00:00 2001 From: Jonas 'Sortie' Termansen Date: Sat, 3 Aug 2024 01:10:35 +0200 Subject: [PATCH] Add init(5) echo feature. --- init/init.c | 17 +++++++++++++++++ share/man/man5/init.5 | 8 ++++++++ 2 files changed, 25 insertions(+) diff --git a/init/init.c b/init/init.c index f94bbd49..34b46637 100644 --- a/init/init.c +++ b/init/init.c @@ -220,6 +220,7 @@ struct daemon int readyfd; int outputfd; bool configured; + bool echo; bool need_tty; bool was_ready; bool was_terminated; @@ -243,6 +244,7 @@ struct daemon_config int argc; char** argv; enum exit_code_meaning exit_code_meaning; + bool echo; bool per_if; bool need_tty; enum log_method log_method; @@ -1107,6 +1109,16 @@ static bool daemon_process_command(struct daemon_config* daemon_config, return false; } } + else if ( !strcmp(argv[0], "echo") ) + { + if ( !strcmp(argv[1], "true") ) + daemon_config->echo = true; + else if ( !strcmp(argv[1], "false") ) + daemon_config->echo = false; + else + warning("%s:%ji: unknown %s: %s", + path, (intmax_t) line_number, argv[0], argv[1]); + } else if ( !strcmp(argv[0], "exec") ) { for ( int i = 0; i < daemon_config->argc; i++ ) @@ -1329,6 +1341,8 @@ static bool daemon_process_command(struct daemon_config* daemon_config, free(daemon_config->cd); daemon_config->cd = NULL; } + else if ( !strcmp(argv[1], "echo") ) + daemon_config->echo = default_config.echo; else if ( !strcmp(argv[1], "exec") ) { for ( int i = 0; i < daemon_config->argc; i++ ) @@ -1845,6 +1859,7 @@ static void daemon_configure_sub(struct daemon* daemon, fatal("malloc: %m"); if ( !log_initialize(&daemon->log, daemon->name, daemon_config) ) fatal("malloc: %m"); + daemon->echo = daemon_config->echo; daemon->need_tty = daemon_config->need_tty; daemon->configured = true; } @@ -2338,6 +2353,8 @@ static bool daemon_process_output(struct daemon* daemon) else if ( amount == 0 ) return false; log_formatted(&daemon->log, data, amount); + if ( daemon->echo ) + writeall(1, data, amount); return true; } diff --git a/share/man/man5/init.5 b/share/man/man5/init.5 index 93d59676..02b62465 100644 --- a/share/man/man5/init.5 +++ b/share/man/man5/init.5 @@ -228,6 +228,14 @@ The # character starts a comment and the rest of the line is ignored. The working directory to run the deamon inside. (Default is .Pa / ) +.It Sy echo Oo Sy false "|" true Oc +Whether to echo the output of the daemon to the console. +.Pp +The default is +.Sy false +inherited from the +.Sy default +deamon. .It Sy exec Ar command The command line that starts the daemon. The daemon becomes ready when it writes