[git commit] init: improve log message when a process exits: show exit code

Denys Vlasenko vda.linux at googlemail.com
Sun Apr 6 08:44:04 UTC 2025


commit: https://git.busybox.net/busybox/commit/?id=f9274e8d6e15d1c3cc0368aa55974de84bbbb002
branch: https://git.busybox.net/busybox/commit/?id=refs/heads/master

function                                             old     new   delta
.rodata                                           105649  105680     +31
init_main                                            776     804     +28
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 2/0 up/down: 59/0)               Total: 59 bytes

Signed-off-by: Sébastien Parisot <sparisot at free-mobile.fr>
Signed-off-by: Denys Vlasenko <vda.linux at googlemail.com>
---
 init/init.c | 18 +++++++++++++++---
 1 file changed, 15 insertions(+), 3 deletions(-)

diff --git a/init/init.c b/init/init.c
index 2ee1e4cde..797e0a0eb 100644
--- a/init/init.c
+++ b/init/init.c
@@ -1198,17 +1198,29 @@ int init_main(int argc UNUSED_PARAM, char **argv)
 		/* Wait for any child process(es) to exit */
 		while (1) {
 			pid_t wpid;
+			int status;
 			struct init_action *a;
 
-			wpid = waitpid(-1, NULL, WNOHANG);
+			wpid = waitpid(-1, &status, WNOHANG);
 			if (wpid <= 0)
 				break;
 
 			a = mark_terminated(wpid);
 			if (a) {
-				message(L_LOG, "process '%s' (pid %u) exited. "
+				const char *s = "killed, signal";
+				int ex = WTERMSIG(status);
+				/* "if (!WIFSIGNALED(status))" generates more code:
+				 * on linux, WIFEXITED(status) is "WTERMSIG(status) == 0"
+				 * and WTERMSIG(status) is known, so compiler optimizes.
+				 */
+				if (WIFEXITED(status)) {
+					s = "exited, exitcode";
+					ex = WEXITSTATUS(status);
+				}
+				message(L_LOG, "process '%s' (pid %u) %s:%d. "
 						"Scheduling for restart.",
-						a->command, (unsigned)wpid);
+						a->command, (unsigned)wpid,
+						s, ex);
 			}
 		}
 


More information about the busybox-cvs mailing list