]> git.proxmox.com Git - mirror_lxc.git/blob - src/tests/lxc_raw_clone.c
Merge pull request #2842 from brauner/2019-02-11/fix_licensing
[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 #define _GNU_SOURCE
25 #define __STDC_FORMAT_MACROS
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 "lxctest.h"
41 #include "namespace.h"
42 #include "raw_syscalls.h"
43 #include "utils.h"
44
45 int main(int argc, char *argv[])
46 {
47 int status;
48 pid_t pid;
49 int flags = 0;
50
51 pid = lxc_raw_clone(CLONE_PARENT_SETTID, NULL);
52 if (pid >= 0 || pid != -EINVAL) {
53 lxc_error("%s\n", "Calling lxc_raw_clone(CLONE_PARENT_SETTID) "
54 "should not be possible");
55 exit(EXIT_FAILURE);
56 }
57
58 pid = lxc_raw_clone(CLONE_CHILD_SETTID, NULL);
59 if (pid >= 0 || pid != -EINVAL) {
60 lxc_error("%s\n", "Calling lxc_raw_clone(CLONE_CHILD_SETTID) "
61 "should not be possible");
62 exit(EXIT_FAILURE);
63 }
64
65 pid = lxc_raw_clone(CLONE_CHILD_CLEARTID, NULL);
66 if (pid >= 0 || pid != -EINVAL) {
67 lxc_error("%s\n", "Calling lxc_raw_clone(CLONE_CHILD_CLEARTID) "
68 "should not be possible");
69 exit(EXIT_FAILURE);
70 }
71
72 pid = lxc_raw_clone(CLONE_SETTLS, NULL);
73 if (pid >= 0 || pid != -EINVAL) {
74 lxc_error("%s\n", "Calling lxc_raw_clone(CLONE_SETTLS) should "
75 "not be possible");
76 exit(EXIT_FAILURE);
77 }
78
79 pid = lxc_raw_clone(CLONE_VM, NULL);
80 if (pid >= 0 || pid != -EINVAL) {
81 lxc_error("%s\n", "Calling lxc_raw_clone(CLONE_VM) should "
82 "not be possible");
83 exit(EXIT_FAILURE);
84 }
85
86 pid = lxc_raw_clone(0, NULL);
87 if (pid < 0) {
88 lxc_error("%s\n", "Failed to call lxc_raw_clone(0)");
89 exit(EXIT_FAILURE);
90 }
91
92 if (pid == 0) {
93 lxc_error("%s\n", "Child will exit(EXIT_SUCCESS)");
94 exit(EXIT_SUCCESS);
95 }
96
97 status = wait_for_pid(pid);
98 if (status != 0) {
99 lxc_error("%s\n", "Failed to retrieve correct exit status");
100 exit(EXIT_FAILURE);
101 }
102
103 pid = lxc_raw_clone(0, NULL);
104 if (pid < 0) {
105 lxc_error("%s\n", "Failed to call lxc_raw_clone(0)");
106 exit(EXIT_FAILURE);
107 }
108
109 if (pid == 0) {
110 lxc_error("%s\n", "Child will exit(EXIT_FAILURE)");
111 exit(EXIT_FAILURE);
112 }
113
114 status = wait_for_pid(pid);
115 if (status == 0) {
116 lxc_error("%s\n", "Failed to retrieve correct exit status");
117 exit(EXIT_FAILURE);
118 }
119
120 flags |= CLONE_NEWUSER;
121 if (cgns_supported())
122 flags |= CLONE_NEWCGROUP;
123 flags |= CLONE_NEWNS;
124 flags |= CLONE_NEWIPC;
125 flags |= CLONE_NEWNET;
126 flags |= CLONE_NEWIPC;
127 flags |= CLONE_NEWPID;
128 flags |= CLONE_NEWUTS;
129
130 pid = lxc_raw_clone(flags, NULL);
131 if (pid < 0) {
132 lxc_error("%s\n", "Failed to call lxc_raw_clone(CLONE_NEWUSER "
133 "| CLONE_NEWCGROUP | CLONE_NEWNS | "
134 "CLONE_NEWIPC | CLONE_NEWNET | CLONE_NEWIPC "
135 "| CLONE_NEWPID | CLONE_NEWUTS);");
136 exit(EXIT_FAILURE);
137 }
138
139 if (pid == 0) {
140 lxc_error("%s\n", "Child will exit(EXIT_SUCCESS)");
141 exit(EXIT_SUCCESS);
142 }
143
144 status = wait_for_pid(pid);
145 if (status != 0) {
146 lxc_error("%s\n", "Failed to retrieve correct exit status");
147 exit(EXIT_FAILURE);
148 }
149
150 pid = lxc_raw_clone(flags, NULL);
151 if (pid < 0) {
152 lxc_error("%s\n", "Failed to call lxc_raw_clone(CLONE_NEWUSER "
153 "| CLONE_NEWCGROUP | CLONE_NEWNS | "
154 "CLONE_NEWIPC | CLONE_NEWNET | CLONE_NEWIPC "
155 "| CLONE_NEWPID | CLONE_NEWUTS);");
156 exit(EXIT_FAILURE);
157 }
158
159
160 if (pid == 0) {
161 lxc_error("%s\n", "Child will exit(EXIT_FAILURE)");
162 exit(EXIT_FAILURE);
163 }
164
165 status = wait_for_pid(pid);
166 if (status == 0) {
167 lxc_error("%s\n", "Failed to retrieve correct exit status");
168 exit(EXIT_FAILURE);
169 }
170
171 pid = lxc_raw_clone(CLONE_VFORK, NULL);
172 if (pid < 0) {
173 lxc_error("%s\n", "Failed to call lxc_raw_clone(CLONE_VFORK);");
174 exit(EXIT_FAILURE);
175 }
176
177 if (pid == 0) {
178 lxc_error("%s\n", "Child will exit(EXIT_SUCCESS)");
179 exit(EXIT_SUCCESS);
180 }
181
182 status = wait_for_pid(pid);
183 if (status != 0) {
184 lxc_error("%s\n", "Failed to retrieve correct exit status");
185 exit(EXIT_FAILURE);
186 }
187
188 pid = lxc_raw_clone(CLONE_VFORK, NULL);
189 if (pid < 0) {
190 lxc_error("%s\n", "Failed to call lxc_raw_clone(CLONE_VFORK);");
191 exit(EXIT_FAILURE);
192 }
193
194 if (pid == 0) {
195 lxc_error("%s\n", "Child will exit(EXIT_FAILURE)");
196 exit(EXIT_FAILURE);
197 }
198
199 status = wait_for_pid(pid);
200 if (status == 0) {
201 lxc_error("%s\n", "Failed to retrieve correct exit status");
202 exit(EXIT_FAILURE);
203 }
204
205 pid = lxc_raw_clone(CLONE_FILES, NULL);
206 if (pid < 0) {
207 lxc_error("%s\n", "Failed to call lxc_raw_clone(CLONE_FILES);");
208 exit(EXIT_FAILURE);
209 }
210
211 if (pid == 0) {
212 lxc_error("%s\n", "Child will exit(EXIT_SUCCESS)");
213 exit(EXIT_SUCCESS);
214 }
215
216 status = wait_for_pid(pid);
217 if (status != 0) {
218 lxc_error("%s\n", "Failed to retrieve correct exit status");
219 exit(EXIT_FAILURE);
220 }
221
222 pid = lxc_raw_clone(CLONE_FILES, NULL);
223 if (pid < 0) {
224 lxc_error("%s\n", "Failed to call lxc_raw_clone(CLONE_FILES);");
225 exit(EXIT_FAILURE);
226 }
227
228 if (pid == 0) {
229 lxc_error("%s\n", "Child will exit(EXIT_FAILURE)");
230 exit(EXIT_FAILURE);
231 }
232
233 status = wait_for_pid(pid);
234 if (status == 0) {
235 lxc_error("%s\n", "Failed to retrieve correct exit status");
236 exit(EXIT_FAILURE);
237 }
238
239 lxc_debug("%s\n", "All lxc_raw_clone() tests successful");
240 exit(EXIT_SUCCESS);
241 }