1f08c3bdfSopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
2f08c3bdfSopenharmony_ci/*
3f08c3bdfSopenharmony_ci * Copyright (c) 2022 SUSE LLC <mdoucha@suse.cz>
4f08c3bdfSopenharmony_ci */
5f08c3bdfSopenharmony_ci
6f08c3bdfSopenharmony_ci/*\
7f08c3bdfSopenharmony_ci * Test whether the LTP library properly reaps any children left over when
8f08c3bdfSopenharmony_ci * the main test process dies. Run using test_children_cleanup.sh.
9f08c3bdfSopenharmony_ci */
10f08c3bdfSopenharmony_ci
11f08c3bdfSopenharmony_ci#include <unistd.h>
12f08c3bdfSopenharmony_ci#include <signal.h>
13f08c3bdfSopenharmony_ci
14f08c3bdfSopenharmony_ci#include "tst_test.h"
15f08c3bdfSopenharmony_ci
16f08c3bdfSopenharmony_cistatic void run(void)
17f08c3bdfSopenharmony_ci{
18f08c3bdfSopenharmony_ci	pid_t child_pid, main_pid = getpid();
19f08c3bdfSopenharmony_ci
20f08c3bdfSopenharmony_ci	tst_res(TINFO, "Main process %d starting", main_pid);
21f08c3bdfSopenharmony_ci
22f08c3bdfSopenharmony_ci	/* Check that normal child reaping does not disrupt the test */
23f08c3bdfSopenharmony_ci	if (!SAFE_FORK())
24f08c3bdfSopenharmony_ci		return;
25f08c3bdfSopenharmony_ci
26f08c3bdfSopenharmony_ci	SAFE_WAIT(NULL);
27f08c3bdfSopenharmony_ci	child_pid = SAFE_FORK();
28f08c3bdfSopenharmony_ci
29f08c3bdfSopenharmony_ci	/* Start child that will outlive the main test process */
30f08c3bdfSopenharmony_ci	if (!child_pid) {
31f08c3bdfSopenharmony_ci		sleep(30);
32f08c3bdfSopenharmony_ci		return;
33f08c3bdfSopenharmony_ci	}
34f08c3bdfSopenharmony_ci
35f08c3bdfSopenharmony_ci	tst_res(TINFO, "Forked child %d", child_pid);
36f08c3bdfSopenharmony_ci	kill(main_pid, SIGKILL);
37f08c3bdfSopenharmony_ci}
38f08c3bdfSopenharmony_ci
39f08c3bdfSopenharmony_cistatic struct tst_test test = {
40f08c3bdfSopenharmony_ci	.test_all = run,
41f08c3bdfSopenharmony_ci	.forks_child = 1,
42f08c3bdfSopenharmony_ci};
43