]> git.proxmox.com Git - mirror_lxc.git/blob - src/tests/lxc_raw_clone.c
Merge pull request #2048 from duguhaotian/master
[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 "utils.h"
43
44 int main(int argc, char *argv[])
45 {
46 int status;
47 pid_t pid;
48 int flags = 0;
49
50 pid = lxc_raw_clone(CLONE_PARENT_SETTID);
51 if (pid >= 0 || pid != -EINVAL) {
52 lxc_error("%s\n", "Calling lxc_raw_clone(CLONE_PARENT_SETTID) "
53 "should not be possible");
54 exit(EXIT_FAILURE);
55 }
56
57 pid = lxc_raw_clone(CLONE_CHILD_SETTID);
58 if (pid >= 0 || pid != -EINVAL) {
59 lxc_error("%s\n", "Calling lxc_raw_clone(CLONE_CHILD_SETTID) "
60 "should not be possible");
61 exit(EXIT_FAILURE);
62 }
63
64 pid = lxc_raw_clone(CLONE_CHILD_CLEARTID);
65 if (pid >= 0 || pid != -EINVAL) {
66 lxc_error("%s\n", "Calling lxc_raw_clone(CLONE_CHILD_CLEARTID) "
67 "should not be possible");
68 exit(EXIT_FAILURE);
69 }
70
71 pid = lxc_raw_clone(CLONE_SETTLS);
72 if (pid >= 0 || pid != -EINVAL) {
73 lxc_error("%s\n", "Calling lxc_raw_clone(CLONE_SETTLS) should "
74 "not be possible");
75 exit(EXIT_FAILURE);
76 }
77
78 pid = lxc_raw_clone(CLONE_VM);
79 if (pid >= 0 || pid != -EINVAL) {
80 lxc_error("%s\n", "Calling lxc_raw_clone(CLONE_VM) should "
81 "not be possible");
82 exit(EXIT_FAILURE);
83 }
84
85 pid = lxc_raw_clone(0);
86 if (pid < 0) {
87 lxc_error("%s\n", "Failed to call lxc_raw_clone(0)");
88 exit(EXIT_FAILURE);
89 }
90
91 if (pid == 0) {
92 lxc_error("%s\n", "Child will exit(EXIT_SUCCESS)");
93 exit(EXIT_SUCCESS);
94 }
95
96 status = wait_for_pid(pid);
97 if (status != 0) {
98 lxc_error("%s\n", "Failed to retrieve correct exit status");
99 exit(EXIT_FAILURE);
100 }
101
102 pid = lxc_raw_clone(0);
103 if (pid < 0) {
104 lxc_error("%s\n", "Failed to call lxc_raw_clone(0)");
105 exit(EXIT_FAILURE);
106 }
107
108 if (pid == 0) {
109 lxc_error("%s\n", "Child will exit(EXIT_FAILURE)");
110 exit(EXIT_FAILURE);
111 }
112
113 status = wait_for_pid(pid);
114 if (status == 0) {
115 lxc_error("%s\n", "Failed to retrieve correct exit status");
116 exit(EXIT_FAILURE);
117 }
118
119 pid = lxc_raw_clone(CLONE_NEWUSER | CLONE_NEWCGROUP | CLONE_NEWNS |
120 CLONE_NEWIPC | CLONE_NEWNET | CLONE_NEWIPC |
121 CLONE_NEWPID | CLONE_NEWUTS);
122 if (pid < 0) {
123 lxc_error("%s\n", "Failed to call lxc_raw_clone(CLONE_NEWUSER "
124 "| CLONE_NEWCGROUP | CLONE_NEWNS | "
125 "CLONE_NEWIPC | CLONE_NEWNET | CLONE_NEWIPC "
126 "| CLONE_NEWPID | CLONE_NEWUTS);");
127 exit(EXIT_FAILURE);
128 }
129
130 if (pid == 0) {
131 lxc_error("%s\n", "Child will exit(EXIT_SUCCESS)");
132 exit(EXIT_SUCCESS);
133 }
134
135 status = wait_for_pid(pid);
136 if (status != 0) {
137 lxc_error("%s\n", "Failed to retrieve correct exit status");
138 exit(EXIT_FAILURE);
139 }
140
141 flags |= CLONE_NEWUSER;
142 if (cgns_supported())
143 flags |= CLONE_NEWCGROUP;
144 flags |= CLONE_NEWNS;
145 flags |= CLONE_NEWIPC;
146 flags |= CLONE_NEWNET;
147 flags |= CLONE_NEWIPC;
148 flags |= CLONE_NEWPID;
149 flags |= CLONE_NEWUTS;
150 pid = lxc_raw_clone(flags);
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_SUCCESS);
169 }
170
171 lxc_debug("%s\n", "All lxc_raw_clone() tests successful");
172 exit(EXIT_SUCCESS);
173 }