xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/pidfd_open/pidfd_open02.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (c) 2020 Viresh Kumar <[email protected]>
4  */
5 
6 /*\
7  * [Description]
8  *
9  * Tests basic error handling of the pidfd_open syscall.
10  *
11  * - ESRCH the process specified by pid does not exist
12  * - EINVAL pid is not valid
13  * - EINVAL flags is not valid
14  */
15 #include "tst_test.h"
16 #include "lapi/pidfd.h"
17 
18 static pid_t expired_pid, my_pid, invalid_pid = -1;
19 
20 static struct tcase {
21 	char *name;
22 	pid_t *pid;
23 	int flags;
24 	int exp_errno;
25 } tcases[] = {
26 	{"expired pid", &expired_pid, 0, ESRCH},
27 	{"invalid pid", &invalid_pid, 0, EINVAL},
28 	{"invalid flags", &my_pid, 1, EINVAL},
29 };
30 
setup(void)31 static void setup(void)
32 {
33 	pidfd_open_supported();
34 	expired_pid = tst_get_unused_pid();
35 	my_pid = getpid();
36 }
37 
run(unsigned int n)38 static void run(unsigned int n)
39 {
40 	struct tcase *tc = &tcases[n];
41 
42 	TST_EXP_FAIL2(pidfd_open(*tc->pid, tc->flags), tc->exp_errno,
43 			"pidfd_open with %s", tc->name);
44 }
45 
46 static struct tst_test test = {
47 	.tcnt = ARRAY_SIZE(tcases),
48 	.test = run,
49 	.setup = setup,
50 };
51