xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/ioctl/ioctl_ns02.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (c) 2019 Federico Bonfiglio <[email protected]>
4  * Copyright (c) Linux Test Project, 2019-2022
5  */
6 
7 /*\
8  * [Description]
9  *
10  * Test ioctl_ns with NS_GET_PARENT request.
11  *
12  * Test tries to get namespace parent for UTS namespace, which
13  * should make the call fail with EINVAL, being a nonhierarchical
14  * namespace.
15  */
16 
17 #define _GNU_SOURCE
18 
19 #include <errno.h>
20 #include "tst_test.h"
21 #include "lapi/ioctl_ns.h"
22 
setup(void)23 static void setup(void)
24 {
25 	int exists = access("/proc/self/ns/uts", F_OK);
26 
27 	if (exists < 0)
28 		tst_res(TCONF, "namespace not available");
29 }
30 
run(void)31 static void run(void)
32 {
33 	int fd, parent_fd;
34 
35 	fd = SAFE_OPEN("/proc/self/ns/uts", O_RDONLY);
36 	parent_fd = ioctl(fd, NS_GET_PARENT);
37 	if (parent_fd == -1) {
38 		if (errno == ENOTTY)
39 			tst_brk(TCONF, "ioctl(NS_GET_PARENT) not implemented");
40 
41 		if (errno == EINVAL)
42 			tst_res(TPASS, "NS_GET_PARENT fails with EINVAL");
43 		else
44 			tst_res(TFAIL | TERRNO, "unexpected ioctl error");
45 	} else {
46 		tst_res(TFAIL, "call to ioctl succeded");
47 	}
48 	SAFE_CLOSE(fd);
49 }
50 
51 static struct tst_test test = {
52 	.test_all = run,
53 	.min_kver = "4.9",
54 	.setup = setup
55 };
56