1f08c3bdfSopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
2f08c3bdfSopenharmony_ci/*
3f08c3bdfSopenharmony_ci * Copyright (c) 2000 Silicon Graphics, Inc.  All Rights Reserved.
4f08c3bdfSopenharmony_ci * Copyright (C) 2015 Cyril Hrubis <chrubis@suse.cz>
5f08c3bdfSopenharmony_ci * AUTHOR	: Dave Baumgartner
6f08c3bdfSopenharmony_ci * CO-PILOT	: Barrie Kletscher
7f08c3bdfSopenharmony_ci */
8f08c3bdfSopenharmony_ci
9f08c3bdfSopenharmony_ci/*\
10f08c3bdfSopenharmony_ci * [Description]
11f08c3bdfSopenharmony_ci *
12f08c3bdfSopenharmony_ci * Test SIGKILL for these items:
13f08c3bdfSopenharmony_ci *	1. SIGKILL can not be set to be ignored, errno:EINVAL (POSIX).
14f08c3bdfSopenharmony_ci *	2. SIGKILL can not be reset to default, errno:EINVAL (POSIX).
15f08c3bdfSopenharmony_ci *	3. SIGKILL can not be set to be caught, errno:EINVAL (POSIX).
16f08c3bdfSopenharmony_ci *	4. SIGKILL can not be ignored.
17f08c3bdfSopenharmony_ci *	5. SIGKILL is reset to default failed but processed by default.
18f08c3bdfSopenharmony_ci *	6. SIGKILL can not be caught.
19f08c3bdfSopenharmony_ci */
20f08c3bdfSopenharmony_ci
21f08c3bdfSopenharmony_ci#include <stdlib.h>
22f08c3bdfSopenharmony_ci#include "tst_test.h"
23f08c3bdfSopenharmony_ci
24f08c3bdfSopenharmony_cistatic void catchsig(int sig)
25f08c3bdfSopenharmony_ci{
26f08c3bdfSopenharmony_ci	(void)sig;
27f08c3bdfSopenharmony_ci}
28f08c3bdfSopenharmony_ci
29f08c3bdfSopenharmony_cistatic struct tcase {
30f08c3bdfSopenharmony_ci	void (*sighandler)(int i);
31f08c3bdfSopenharmony_ci	int kill;
32f08c3bdfSopenharmony_ci} tcases[] = {
33f08c3bdfSopenharmony_ci	{SIG_IGN, 0},
34f08c3bdfSopenharmony_ci	{SIG_DFL, 0},
35f08c3bdfSopenharmony_ci	{catchsig, 0},
36f08c3bdfSopenharmony_ci	{SIG_IGN, 1},
37f08c3bdfSopenharmony_ci	{SIG_DFL, 1},
38f08c3bdfSopenharmony_ci	{catchsig, 1},
39f08c3bdfSopenharmony_ci};
40f08c3bdfSopenharmony_ci
41f08c3bdfSopenharmony_cistatic void do_test(unsigned int n)
42f08c3bdfSopenharmony_ci{
43f08c3bdfSopenharmony_ci	pid_t pid;
44f08c3bdfSopenharmony_ci	int res;
45f08c3bdfSopenharmony_ci
46f08c3bdfSopenharmony_ci	struct tcase *tc = &tcases[n];
47f08c3bdfSopenharmony_ci
48f08c3bdfSopenharmony_ci	pid = SAFE_FORK();
49f08c3bdfSopenharmony_ci
50f08c3bdfSopenharmony_ci	if (!pid) {
51f08c3bdfSopenharmony_ci		if (tc->kill) {
52f08c3bdfSopenharmony_ci			signal(SIGKILL, tc->sighandler);
53f08c3bdfSopenharmony_ci			pause();
54f08c3bdfSopenharmony_ci		}
55f08c3bdfSopenharmony_ci
56f08c3bdfSopenharmony_ci		TST_EXP_FAIL2((long)signal(SIGKILL, tc->sighandler), EINVAL);
57f08c3bdfSopenharmony_ci		return;
58f08c3bdfSopenharmony_ci	}
59f08c3bdfSopenharmony_ci
60f08c3bdfSopenharmony_ci	if (!tc->kill) {
61f08c3bdfSopenharmony_ci		SAFE_WAITPID(pid, &res, 0);
62f08c3bdfSopenharmony_ci		return;
63f08c3bdfSopenharmony_ci	}
64f08c3bdfSopenharmony_ci
65f08c3bdfSopenharmony_ci	TST_PROCESS_STATE_WAIT(pid, 'S', 0);
66f08c3bdfSopenharmony_ci	SAFE_KILL(pid, SIGKILL);
67f08c3bdfSopenharmony_ci	SAFE_WAITPID(pid, &res, 0);
68f08c3bdfSopenharmony_ci
69f08c3bdfSopenharmony_ci	if (WIFSIGNALED(res))
70f08c3bdfSopenharmony_ci		TST_EXP_EQ_SSZ(WTERMSIG(res), SIGKILL);
71f08c3bdfSopenharmony_ci	else
72f08c3bdfSopenharmony_ci		tst_res(TFAIL, "Child not killed by signal");
73f08c3bdfSopenharmony_ci}
74f08c3bdfSopenharmony_ci
75f08c3bdfSopenharmony_cistatic struct tst_test test = {
76f08c3bdfSopenharmony_ci	.tcnt = ARRAY_SIZE(tcases),
77f08c3bdfSopenharmony_ci	.forks_child = 1,
78f08c3bdfSopenharmony_ci	.test = do_test,
79f08c3bdfSopenharmony_ci};
80