• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (c) 2020 Viresh Kumar <viresh.kumar@linaro.org>
4  *
5  * Description:
6  * Basic pidfd_open() test to test invalid arguments.
7  */
8 #include "tst_test.h"
9 #include "lapi/pidfd_open.h"
10 
11 pid_t expired_pid, my_pid, invalid_pid = -1;
12 
13 static struct tcase {
14 	char *name;
15 	pid_t *pid;
16 	int flags;
17 	int exp_errno;
18 } tcases[] = {
19 	{"expired pid", &expired_pid, 0, ESRCH},
20 	{"invalid pid", &invalid_pid, 0, EINVAL},
21 	{"invalid flags", &my_pid, 1, EINVAL},
22 };
23 
setup(void)24 static void setup(void)
25 {
26 	expired_pid = tst_get_unused_pid();
27 	my_pid = getpid();
28 }
29 
run(unsigned int n)30 static void run(unsigned int n)
31 {
32 	struct tcase *tc = &tcases[n];
33 
34 	TEST(pidfd_open(*tc->pid, tc->flags));
35 
36 	if (TST_RET != -1) {
37 		SAFE_CLOSE(TST_RET);
38 		tst_res(TFAIL, "%s: pidfd_open succeeded unexpectedly (index: %d)",
39 			tc->name, n);
40 		return;
41 	}
42 
43 	if (tc->exp_errno != TST_ERR) {
44 		tst_res(TFAIL | TTERRNO, "%s: pidfd_open() should fail with %s",
45 			tc->name, tst_strerrno(tc->exp_errno));
46 		return;
47 	}
48 
49 	tst_res(TPASS | TTERRNO, "%s: pidfd_open() failed as expected",
50 		tc->name);
51 }
52 
53 static struct tst_test test = {
54 	.min_kver = "5.3",
55 	.tcnt = ARRAY_SIZE(tcases),
56 	.test = run,
57 	.setup = setup,
58 };
59