]> git.proxmox.com Git - mirror_lxc.git/blob - src/tests/lxc_raw_clone.c
Merge pull request #4028 from brauner/2021-11-02.fixes
[mirror_lxc.git] / src / tests / lxc_raw_clone.c
1 /*
2 * lxc: linux Container library
3 *
4 * Copyright © 2017 Canonical Ltd.
5 *
6 * Authors:
7 * Christian Brauner <christian.brauner@ubuntu.com>
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 */
23
24 #include "config.h"
25
26 #include <errno.h>
27 #include <fcntl.h>
28 #include <inttypes.h>
29 #include <limits.h>
30 #include <sched.h>
31 #include <stdbool.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <unistd.h>
36 #include <sys/mount.h>
37 #include <sys/stat.h>
38 #include <sys/types.h>
39
40 #include "cgroups/cgroup_utils.h"
41 #include "lxctest.h"
42 #include "namespace.h"
43 #include "process_utils.h"
44 #include "utils.h"
45
46 int main(int argc, char *argv[])
47 {
48 int status;
49 pid_t pid;
50 int flags = 0;
51
52 pid = lxc_raw_clone(CLONE_PARENT_SETTID, NULL);
53 if (pid >= 0 || pid != -EINVAL) {
54 lxc_error("%s\n", "Calling lxc_raw_clone(CLONE_PARENT_SETTID) "
55 "should not be possible");
56 exit(EXIT_FAILURE);
57 }
58
59 pid = lxc_raw_clone(CLONE_CHILD_SETTID, NULL);
60 if (pid >= 0 || pid != -EINVAL) {
61 lxc_error("%s\n", "Calling lxc_raw_clone(CLONE_CHILD_SETTID) "
62 "should not be possible");
63 exit(EXIT_FAILURE);
64 }
65
66 pid = lxc_raw_clone(CLONE_CHILD_CLEARTID, NULL);
67 if (pid >= 0 || pid != -EINVAL) {
68 lxc_error("%s\n", "Calling lxc_raw_clone(CLONE_CHILD_CLEARTID) "
69 "should not be possible");
70 exit(EXIT_FAILURE);
71 }
72
73 pid = lxc_raw_clone(CLONE_SETTLS, NULL);
74 if (pid >= 0 || pid != -EINVAL) {
75 lxc_error("%s\n", "Calling lxc_raw_clone(CLONE_SETTLS) should "
76 "not be possible");
77 exit(EXIT_FAILURE);
78 }
79
80 pid = lxc_raw_clone(CLONE_VM, NULL);
81 if (pid >= 0 || pid != -EINVAL) {
82 lxc_error("%s\n", "Calling lxc_raw_clone(CLONE_VM) should "
83 "not be possible");
84 exit(EXIT_FAILURE);
85 }
86
87 pid = lxc_raw_clone(0, NULL);
88 if (pid < 0) {
89 lxc_error("%s\n", "Failed to call lxc_raw_clone(0)");
90 exit(EXIT_FAILURE);
91 }
92
93 if (pid == 0) {
94 lxc_error("%s\n", "Child will exit(EXIT_SUCCESS)");
95 exit(EXIT_SUCCESS);
96 }
97
98 status = wait_for_pid(pid);
99 if (status != 0) {
100 lxc_error("%s\n", "Failed to retrieve correct exit status");
101 exit(EXIT_FAILURE);
102 }
103
104 pid = lxc_raw_clone(0, NULL);
105 if (pid < 0) {
106 lxc_error("%s\n", "Failed to call lxc_raw_clone(0)");
107 exit(EXIT_FAILURE);
108 }
109
110 if (pid == 0) {
111 lxc_error("%s\n", "Child will exit(EXIT_FAILURE)");
112 exit(EXIT_FAILURE);
113 }
114
115 status = wait_for_pid(pid);
116 if (status == 0) {
117 lxc_error("%s\n", "Failed to retrieve correct exit status");
118 exit(EXIT_FAILURE);
119 }
120
121 flags |= CLONE_NEWUSER;
122 if (cgns_supported())
123 flags |= CLONE_NEWCGROUP;
124 flags |= CLONE_NEWNS;
125 flags |= CLONE_NEWIPC;
126 flags |= CLONE_NEWNET;
127 flags |= CLONE_NEWIPC;
128 flags |= CLONE_NEWPID;
129 flags |= CLONE_NEWUTS;
130
131 pid = lxc_raw_clone(flags, NULL);
132 if (pid < 0) {
133 lxc_error("%s\n", "Failed to call lxc_raw_clone(CLONE_NEWUSER "
134 "| CLONE_NEWCGROUP | CLONE_NEWNS | "
135 "CLONE_NEWIPC | CLONE_NEWNET | CLONE_NEWIPC "
136 "| CLONE_NEWPID | CLONE_NEWUTS);");
137 exit(EXIT_FAILURE);
138 }
139
140 if (pid == 0) {
141 lxc_error("%s\n", "Child will exit(EXIT_SUCCESS)");
142 exit(EXIT_SUCCESS);
143 }
144
145 status = wait_for_pid(pid);
146 if (status != 0) {
147 lxc_error("%s\n", "Failed to retrieve correct exit status");
148 exit(EXIT_FAILURE);
149 }
150
151 pid = lxc_raw_clone(flags, NULL);
152 if (pid < 0) {
153 lxc_error("%s\n", "Failed to call lxc_raw_clone(CLONE_NEWUSER "
154 "| CLONE_NEWCGROUP | CLONE_NEWNS | "
155 "CLONE_NEWIPC | CLONE_NEWNET | CLONE_NEWIPC "
156 "| CLONE_NEWPID | CLONE_NEWUTS);");
157 exit(EXIT_FAILURE);
158 }
159
160
161 if (pid == 0) {
162 lxc_error("%s\n", "Child will exit(EXIT_FAILURE)");
163 exit(EXIT_FAILURE);
164 }
165
166 status = wait_for_pid(pid);
167 if (status == 0) {
168 lxc_error("%s\n", "Failed to retrieve correct exit status");
169 exit(EXIT_FAILURE);
170 }
171
172 pid = lxc_raw_clone(CLONE_VFORK, NULL);
173 if (pid < 0) {
174 lxc_error("%s\n", "Failed to call lxc_raw_clone(CLONE_VFORK);");
175 exit(EXIT_FAILURE);
176 }
177
178 if (pid == 0) {
179 lxc_error("%s\n", "Child will exit(EXIT_SUCCESS)");
180 exit(EXIT_SUCCESS);
181 }
182
183 status = wait_for_pid(pid);
184 if (status != 0) {
185 lxc_error("%s\n", "Failed to retrieve correct exit status");
186 exit(EXIT_FAILURE);
187 }
188
189 pid = lxc_raw_clone(CLONE_VFORK, NULL);
190 if (pid < 0) {
191 lxc_error("%s\n", "Failed to call lxc_raw_clone(CLONE_VFORK);");
192 exit(EXIT_FAILURE);
193 }
194
195 if (pid == 0) {
196 lxc_error("%s\n", "Child will exit(EXIT_FAILURE)");
197 exit(EXIT_FAILURE);
198 }
199
200 status = wait_for_pid(pid);
201 if (status == 0) {
202 lxc_error("%s\n", "Failed to retrieve correct exit status");
203 exit(EXIT_FAILURE);
204 }
205
206 pid = lxc_raw_clone(CLONE_FILES, NULL);
207 if (pid < 0) {
208 lxc_error("%s\n", "Failed to call lxc_raw_clone(CLONE_FILES);");
209 exit(EXIT_FAILURE);
210 }
211
212 if (pid == 0) {
213 lxc_error("%s\n", "Child will exit(EXIT_SUCCESS)");
214 exit(EXIT_SUCCESS);
215 }
216
217 status = wait_for_pid(pid);
218 if (status != 0) {
219 lxc_error("%s\n", "Failed to retrieve correct exit status");
220 exit(EXIT_FAILURE);
221 }
222
223 pid = lxc_raw_clone(CLONE_FILES, NULL);
224 if (pid < 0) {
225 lxc_error("%s\n", "Failed to call lxc_raw_clone(CLONE_FILES);");
226 exit(EXIT_FAILURE);
227 }
228
229 if (pid == 0) {
230 lxc_error("%s\n", "Child will exit(EXIT_FAILURE)");
231 exit(EXIT_FAILURE);
232 }
233
234 status = wait_for_pid(pid);
235 if (status == 0) {
236 lxc_error("%s\n", "Failed to retrieve correct exit status");
237 exit(EXIT_FAILURE);
238 }
239
240 lxc_debug("%s\n", "All lxc_raw_clone() tests successful");
241 exit(EXIT_SUCCESS);
242 }