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