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 * Child cloned with the CLONE_NEWPID flag is created in a new pid namespace.
13 * That's checked by comparing its /proc/self/ns/pid symlink and the parent's
14 * one. Also child thinks its pid is 1.
15 */
16
17 #define _GNU_SOURCE
18
19 #include <errno.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include "tst_test.h"
23 #include "lapi/ioctl_ns.h"
24 #include "lapi/sched.h"
25
26 #define STACK_SIZE (1024 * 1024)
27
28 static char *child_stack;
29
setup(void)30 static void setup(void)
31 {
32 int exists = access("/proc/self/ns/pid", F_OK);
33
34 if (exists < 0)
35 tst_res(TCONF, "namespace not available");
36
37 child_stack = ltp_alloc_stack(STACK_SIZE);
38 if (!child_stack)
39 tst_brk(TBROK|TERRNO, "stack alloc");
40 }
41
cleanup(void)42 static void cleanup(void)
43 {
44 free(child_stack);
45 }
46
child(void * arg LTP_ATTRIBUTE_UNUSED)47 static int child(void *arg LTP_ATTRIBUTE_UNUSED)
48 {
49 if (getpid() != 1)
50 tst_res(TFAIL, "child should think its pid is 1");
51 else
52 tst_res(TPASS, "child thinks its pid is 1");
53 TST_CHECKPOINT_WAIT(0);
54 return 0;
55 }
56
run(void)57 static void run(void)
58 {
59 pid_t pid = ltp_clone(CLONE_NEWPID | SIGCHLD, &child, 0,
60 STACK_SIZE, child_stack);
61 if (pid == -1)
62 tst_brk(TBROK | TERRNO, "ltp_clone failed");
63
64 char child_namespace[30];
65 int my_fd, child_fd, parent_fd;
66
67 snprintf(child_namespace, sizeof(child_namespace), "/proc/%i/ns/pid", pid);
68 my_fd = SAFE_OPEN("/proc/self/ns/pid", O_RDONLY);
69 child_fd = SAFE_OPEN(child_namespace, O_RDONLY);
70 parent_fd = ioctl(child_fd, NS_GET_PARENT);
71
72 if (parent_fd == -1) {
73 TST_CHECKPOINT_WAKE(0);
74
75 if (errno == ENOTTY) {
76 tst_res(TCONF, "ioctl(NS_GET_PARENT) not implemented");
77 return;
78 }
79
80 tst_brk(TBROK | TERRNO, "ioctl(NS_GET_PARENT) failed");
81 }
82
83 struct stat my_stat, child_stat, parent_stat;
84
85 SAFE_FSTAT(my_fd, &my_stat);
86 SAFE_FSTAT(child_fd, &child_stat);
87 SAFE_FSTAT(parent_fd, &parent_stat);
88 if (my_stat.st_ino != parent_stat.st_ino)
89 tst_res(TFAIL, "parents have different inodes");
90 else if (parent_stat.st_ino == child_stat.st_ino)
91 tst_res(TFAIL, "child and parent have same inode");
92 else
93 tst_res(TPASS, "child and parent are consistent");
94 SAFE_CLOSE(my_fd);
95 SAFE_CLOSE(child_fd);
96 SAFE_CLOSE(parent_fd);
97 TST_CHECKPOINT_WAKE(0);
98 }
99
100 static struct tst_test test = {
101 .test_all = run,
102 .forks_child = 1,
103 .needs_root = 1,
104 .needs_checkpoints = 1,
105 .min_kver = "4.9",
106 .setup = setup,
107 .cleanup = cleanup,
108 };
109