]> git.proxmox.com Git - mirror_lxc.git/blame - src/tests/lxc_raw_clone.c
tests: include config.h
[mirror_lxc.git] / src / tests / lxc_raw_clone.c
CommitLineData
8ab93249
CB
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
e49c56d6
CB
24#include "config.h"
25
8ab93249
CB
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
c988c8b1 40#include "cgroups/cgroup_utils.h"
8ab93249 41#include "lxctest.h"
1f797c3a 42#include "namespace.h"
f40988c7 43#include "process_utils.h"
8ab93249
CB
44#include "utils.h"
45
46int main(int argc, char *argv[])
47{
48 int status;
49 pid_t pid;
50 int flags = 0;
51
a59440be 52 pid = lxc_raw_clone(CLONE_PARENT_SETTID, NULL);
8ab93249
CB
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
a59440be 59 pid = lxc_raw_clone(CLONE_CHILD_SETTID, NULL);
8ab93249
CB
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
a59440be 66 pid = lxc_raw_clone(CLONE_CHILD_CLEARTID, NULL);
8ab93249
CB
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
a59440be 73 pid = lxc_raw_clone(CLONE_SETTLS, NULL);
8ab93249
CB
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
a59440be 80 pid = lxc_raw_clone(CLONE_VM, NULL);
8ab93249
CB
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
a59440be 87 pid = lxc_raw_clone(0, NULL);
8ab93249
CB
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
a59440be 104 pid = lxc_raw_clone(0, NULL);
8ab93249
CB
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
b01b36e9
CB
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;
c3c504a8 130
a59440be 131 pid = lxc_raw_clone(flags, NULL);
8ab93249
CB
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
a59440be 151 pid = lxc_raw_clone(flags, NULL);
8ab93249
CB
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");
b01b36e9
CB
169 exit(EXIT_FAILURE);
170 }
171
a59440be 172 pid = lxc_raw_clone(CLONE_VFORK, NULL);
b01b36e9
CB
173 if (pid < 0) {
174 lxc_error("%s\n", "Failed to call lxc_raw_clone(CLONE_VFORK);");
175 exit(EXIT_FAILURE);
176 }
177
b01b36e9
CB
178 if (pid == 0) {
179 lxc_error("%s\n", "Child will exit(EXIT_SUCCESS)");
8ab93249
CB
180 exit(EXIT_SUCCESS);
181 }
182
b01b36e9
CB
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
a59440be 189 pid = lxc_raw_clone(CLONE_VFORK, NULL);
b01b36e9
CB
190 if (pid < 0) {
191 lxc_error("%s\n", "Failed to call lxc_raw_clone(CLONE_VFORK);");
192 exit(EXIT_FAILURE);
193 }
194
b01b36e9
CB
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
a59440be 206 pid = lxc_raw_clone(CLONE_FILES, NULL);
b01b36e9
CB
207 if (pid < 0) {
208 lxc_error("%s\n", "Failed to call lxc_raw_clone(CLONE_FILES);");
209 exit(EXIT_FAILURE);
210 }
211
b01b36e9
CB
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
a59440be 223 pid = lxc_raw_clone(CLONE_FILES, NULL);
b01b36e9
CB
224 if (pid < 0) {
225 lxc_error("%s\n", "Failed to call lxc_raw_clone(CLONE_FILES);");
226 exit(EXIT_FAILURE);
227 }
228
b01b36e9
CB
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
8ab93249
CB
240 lxc_debug("%s\n", "All lxc_raw_clone() tests successful");
241 exit(EXIT_SUCCESS);
242}