xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/ioctl/ioctl_ns07.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, 2022
5  */
6 
7 /*\
8  * [Description]
9  *
10  * Test ioctl_ns with NS_GET_* request for file descriptors
11  * that aren't namespaces.
12  *
13  * Calling ioctl with test directory's file descriptor
14  * should make the call fail with ENOTTY.
15  */
16 
17 #define _GNU_SOURCE
18 
19 #include <errno.h>
20 #include "tst_test.h"
21 #include "lapi/ioctl_ns.h"
22 
23 static int requests[] = {NS_GET_PARENT, NS_GET_USERNS,
24 	NS_GET_OWNER_UID, NS_GET_NSTYPE};
25 
test_request(unsigned int n)26 static void test_request(unsigned int n)
27 {
28 	int request = requests[n];
29 	int fd, ns_fd;
30 
31 	fd = SAFE_OPEN(".", O_RDONLY);
32 	ns_fd = ioctl(fd, request);
33 	if (ns_fd == -1) {
34 		if (errno == ENOTTY)
35 			tst_res(TPASS, "request failed with ENOTTY");
36 		else
37 			tst_res(TFAIL | TERRNO, "unexpected ioctl error");
38 	} else {
39 		tst_res(TFAIL, "request success for invalid fd");
40 		SAFE_CLOSE(ns_fd);
41 	}
42 	SAFE_CLOSE(fd);
43 }
44 
45 static struct tst_test test = {
46 	.tcnt = ARRAY_SIZE(requests),
47 	.test = test_request,
48 	.needs_tmpdir = 1,
49 	.min_kver = "4.11"
50 };
51