tools/nolibc/test: exit with poweroff on success when getpid() == 1

The idea is to ease automated testing under qemu. If the test succeeds
while running as PID 1, indicating the system was booted with init=/test,
let's just power off so that qemu can exit with a successful code. In
other situations it will exit and provoke a panic, which may be caught
for example with CONFIG_PVPANIC.

Signed-off-by: Willy Tarreau <w@1wt.eu>
diff --git a/tools/include/nolibc/test/nolibc-test.c b/tools/include/nolibc/test/nolibc-test.c
index e3cfaae..be3aeb5 100644
--- a/tools/include/nolibc/test/nolibc-test.c
+++ b/tools/include/nolibc/test/nolibc-test.c
@@ -520,6 +520,18 @@ int main(int argc, char **argv, char **envp)
 	} else {
 		ret = run_syscalls(min, max);
 	}
+
+	if (getpid() == 1) {
+		/* we're running as init, there's no other process on the
+		 * system, thus likely started from a VM for a quick check.
+		 * Exiting will provoke a kernel panic that may be reported
+		 * as an error by Qemu or the hypervisor, while stopping
+		 * cleanly will often be reported as a success. This allows
+		 * to use the output of this program for bisecting kernels.
+		 */
+		if (ret == 0)
+			reboot(LINUX_REBOOT_CMD_POWER_OFF);
+	}
 	printf("Exiting with status %d\n", ret);
 	return ret;
 }