… | |
… | |
6 | #include <sys/mman.h> |
6 | #include <sys/mman.h> |
7 | |
7 | |
8 | #undef _GNU_SOURCE |
8 | #undef _GNU_SOURCE |
9 | #define _GNU_SOURCE |
9 | #define _GNU_SOURCE |
10 | #include <sched.h> |
10 | #include <sched.h> |
|
|
11 | #include <unistd.h> |
|
|
12 | #include <sys/syscall.h> |
|
|
13 | |
|
|
14 | #include <sched.h> |
|
|
15 | |
|
|
16 | #include <sys/ioctl.h> |
|
|
17 | #include <net/if.h> |
|
|
18 | |
|
|
19 | #include <linux/nsfs.h> |
|
|
20 | #include <linux/kcmp.h> |
|
|
21 | |
|
|
22 | /* kcmp.h does not define symbols, so there is no way to detect preesence of enum */ |
|
|
23 | /* members, so we just hardcode new symbols here. */ |
|
|
24 | #ifndef KCMP_EPOLL_TFD |
|
|
25 | #define KCMP_EPOLL_TFD 7 |
|
|
26 | #endif |
|
|
27 | |
|
|
28 | #ifdef __has_include |
|
|
29 | #if !__has_include("linux/kcmp.h") // use "" as GCC wrongly expands macros |
|
|
30 | #undef SYS_kcmp |
|
|
31 | #endif |
|
|
32 | #endif |
|
|
33 | |
|
|
34 | #ifdef SYS_kcmp |
|
|
35 | #include "linux/kcmp.h" |
|
|
36 | #define kcmp(pid1,pid2,type,idx1,idx2) \ |
|
|
37 | syscall (SYS_kcmp, (pid_t)pid1, (pid_t)pid2, \ |
|
|
38 | (int)type, (unsigned long)idx1, (unsigned long)idx2) |
|
|
39 | #else |
|
|
40 | #define kcmp(pid1,pid2,type,idx1,idx2) \ |
|
|
41 | (errno = ENOSYS, -1) |
|
|
42 | #endif |
|
|
43 | |
|
|
44 | /* from schmorp.h */ |
|
|
45 | static int |
|
|
46 | s_fileno (SV *fh, int wr) |
|
|
47 | { |
|
|
48 | dTHX; |
|
|
49 | SvGETMAGIC (fh); |
|
|
50 | |
|
|
51 | if (SvROK (fh)) |
|
|
52 | { |
|
|
53 | fh = SvRV (fh); |
|
|
54 | SvGETMAGIC (fh); |
|
|
55 | } |
|
|
56 | |
|
|
57 | if (SvTYPE (fh) == SVt_PVGV) |
|
|
58 | return PerlIO_fileno (wr ? IoOFP (sv_2io (fh)) : IoIFP (sv_2io (fh))); |
|
|
59 | |
|
|
60 | if (SvOK (fh) && (SvIV (fh) >= 0) && (SvIV (fh) < 0x7fffffffL)) |
|
|
61 | return SvIV (fh); |
|
|
62 | |
|
|
63 | return -1; |
|
|
64 | } |
11 | |
65 | |
12 | static int |
66 | static int |
13 | clone_cb (void *arg) |
67 | clone_cb (void *arg) |
14 | { |
68 | { |
15 | dSP; |
69 | dSP; |
… | |
… | |
34 | |
88 | |
35 | static const struct { |
89 | static const struct { |
36 | const char *name; |
90 | const char *name; |
37 | IV iv; |
91 | IV iv; |
38 | } *civ, const_iv[] = { |
92 | } *civ, const_iv[] = { |
|
|
93 | # define const_iv(name) { # name, (IV)name }, |
39 | # define const_iv(name) { # name, (IV) CLONE_ ## name }, |
94 | # define const_iv_clone(name) { # name, (IV) CLONE_ ## name }, |
|
|
95 | # ifdef CSIGNAL |
|
|
96 | const_iv (CSIGNAL) |
|
|
97 | # endif |
40 | # ifdef CLONE_FILES |
98 | # ifdef CLONE_FILES |
41 | const_iv (FILES) |
99 | const_iv_clone (FILES) |
42 | # endif |
100 | # endif |
43 | # ifdef CLONE_FS |
101 | # ifdef CLONE_FS |
44 | const_iv (FS) |
102 | const_iv_clone (FS) |
45 | # endif |
103 | # endif |
46 | # ifdef CLONE_NEWNS |
104 | # ifdef CLONE_NEWNS |
47 | const_iv (NEWNS) |
105 | const_iv_clone (NEWNS) |
48 | # endif |
106 | # endif |
49 | # ifdef CLONE_VM |
107 | # ifdef CLONE_VM |
50 | const_iv (VM) |
108 | const_iv_clone (VM) |
51 | # endif |
109 | # endif |
52 | # ifdef CLONE_THREAD |
110 | # ifdef CLONE_THREAD |
53 | const_iv (THREAD) |
111 | const_iv_clone (THREAD) |
54 | # endif |
112 | # endif |
55 | # ifdef CLONE_SIGHAND |
113 | # ifdef CLONE_SIGHAND |
56 | const_iv (SIGHAND) |
114 | const_iv_clone (SIGHAND) |
57 | # endif |
115 | # endif |
58 | # ifdef CLONE_SYSVSEM |
116 | # ifdef CLONE_SYSVSEM |
59 | const_iv (SYSVSEM) |
117 | const_iv_clone (SYSVSEM) |
60 | # endif |
118 | # endif |
61 | # ifdef CLONE_NEWUTS |
119 | # ifdef CLONE_NEWUTS |
62 | const_iv (NEWUTS) |
120 | const_iv_clone (NEWUTS) |
63 | # endif |
121 | # endif |
64 | # ifdef CLONE_NEWIPC |
122 | # ifdef CLONE_NEWIPC |
65 | const_iv (NEWIPC) |
123 | const_iv_clone (NEWIPC) |
66 | # endif |
124 | # endif |
67 | # ifdef CLONE_NEWNET |
125 | # ifdef CLONE_NEWNET |
68 | const_iv (NEWNET) |
126 | const_iv_clone (NEWNET) |
|
|
127 | # endif |
|
|
128 | # ifdef CLONE_NEWTIME |
|
|
129 | x |
|
|
130 | const_iv_clone (NEWTIME) |
69 | # endif |
131 | # endif |
70 | # ifdef CLONE_PTRACE |
132 | # ifdef CLONE_PTRACE |
71 | const_iv (PTRACE) |
133 | const_iv_clone (PTRACE) |
72 | # endif |
134 | # endif |
73 | # ifdef CLONE_VFORK |
135 | # ifdef CLONE_VFORK |
74 | const_iv (VFORK) |
136 | const_iv_clone (VFORK) |
75 | # endif |
137 | # endif |
76 | # ifdef CLONE_SETTLS |
138 | # ifdef CLONE_SETTLS |
77 | const_iv (SETTLS) |
139 | const_iv_clone (SETTLS) |
78 | # endif |
140 | # endif |
79 | # ifdef CLONE_PARENT_SETTID |
141 | # ifdef CLONE_PARENT_SETTID |
80 | const_iv (PARENT_SETTID) |
142 | const_iv_clone (PARENT_SETTID) |
81 | # endif |
143 | # endif |
82 | # ifdef CLONE_CHILD_CLEARTID |
144 | # ifdef CLONE_CHILD_CLEARTID |
83 | const_iv (CHILD_CLEARTID) |
145 | const_iv_clone (CHILD_CLEARTID) |
84 | # endif |
146 | # endif |
85 | # ifdef CLONE_DETACHED |
147 | # ifdef CLONE_DETACHED |
86 | const_iv (DETACHED) |
148 | const_iv_clone (DETACHED) |
87 | # endif |
149 | # endif |
88 | # ifdef CLONE_UNTRACED |
150 | # ifdef CLONE_UNTRACED |
89 | const_iv (UNTRACED) |
151 | const_iv_clone (UNTRACED) |
90 | # endif |
152 | # endif |
91 | # ifdef CLONE_CHILD_SETTID |
153 | # ifdef CLONE_CHILD_SETTID |
92 | const_iv (CHILD_SETTID) |
154 | const_iv_clone (CHILD_SETTID) |
93 | # endif |
155 | # endif |
94 | # ifdef CLONE_NEWUSER |
156 | # ifdef CLONE_NEWUSER |
95 | const_iv (NEWUSER) |
157 | const_iv_clone (NEWUSER) |
96 | # endif |
158 | # endif |
97 | # ifdef CLONE_NEWPID |
159 | # ifdef CLONE_NEWPID |
98 | const_iv (NEWPID) |
160 | const_iv_clone (NEWPID) |
99 | # endif |
161 | # endif |
100 | # ifdef CLONE_IO |
162 | # ifdef CLONE_IO |
|
|
163 | const_iv_clone (IO) |
|
|
164 | # endif |
|
|
165 | # ifdef CLONE_NEWCGROUP |
|
|
166 | const_iv_clone (NEWCGROUP) |
|
|
167 | # endif |
|
|
168 | # ifdef CLONE_PIDFD |
|
|
169 | const_iv_clone (PIDFD) |
|
|
170 | # endif |
|
|
171 | # ifdef SYS_kcmp |
|
|
172 | const_iv (KCMP_FILE) |
|
|
173 | const_iv (KCMP_VM) |
|
|
174 | const_iv (KCMP_FILES) |
|
|
175 | const_iv (KCMP_FS) |
|
|
176 | const_iv (KCMP_SIGHAND) |
101 | const_iv (IO) |
177 | const_iv (KCMP_IO) |
|
|
178 | const_iv (KCMP_SYSVSEM) |
|
|
179 | const_iv (KCMP_EPOLL_TFD) |
|
|
180 | # endif |
|
|
181 | # ifdef NS_GET_USERNS |
|
|
182 | const_iv (NS_GET_USERNS) |
|
|
183 | # endif |
|
|
184 | # ifdef NS_GET_PARENT |
|
|
185 | const_iv (NS_GET_PARENT) |
|
|
186 | # endif |
|
|
187 | # ifdef NS_GET_NSTYPE |
|
|
188 | const_iv (NS_GET_NSTYPE) |
|
|
189 | # endif |
|
|
190 | # ifdef NS_OWNER_UID |
|
|
191 | const_iv (NS_OWNER_UID |
102 | # endif |
192 | # endif |
103 | }; |
193 | }; |
104 | |
194 | |
105 | for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ > const_iv; civ--) |
195 | for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ > const_iv; civ--) |
106 | newCONSTSUB (stash, (char *)civ[-1].name, newSViv (civ[-1].iv)); |
196 | newCONSTSUB (stash, (char *)civ[-1].name, newSViv (civ[-1].iv)); |
107 | |
197 | |
108 | int |
198 | int |
109 | unshare (int flags) |
|
|
110 | CODE: |
|
|
111 | RETVAL = unshare (flags); |
|
|
112 | OUTPUT: |
|
|
113 | RETVAL |
|
|
114 | |
|
|
115 | int |
|
|
116 | clone (SV *sub, IV stacksize, int flags, SV *ptid = 0, SV *tls = &PL_sv_undef) |
199 | clone (SV *sub, IV stacksize, int flags, SV *ptid = 0, SV *tls = &PL_sv_undef) |
117 | CODE: |
200 | CODE: |
118 | { |
201 | { |
119 | if (!stacksize) |
202 | if (!stacksize) |
120 | stacksize = 4 << 20; |
203 | stacksize = 4 << 20; |
121 | |
204 | |
122 | pid_t ptid_; |
205 | pid_t ptid_; |
123 | char *stack_ptr = mmap (0, stacksize, PROT_EXEC | PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_GROWSDOWN | MAP_STACK, -1, 0); |
206 | char *stack_ptr = mmap (0, stacksize, PROT_EXEC | PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_GROWSDOWN | MAP_STACK, -1, 0); |
124 | |
207 | |
… | |
… | |
141 | munmap (stack_ptr, stacksize); |
224 | munmap (stack_ptr, stacksize); |
142 | errno = old_errno; |
225 | errno = old_errno; |
143 | } |
226 | } |
144 | } |
227 | } |
145 | } |
228 | } |
146 | OUTPUT: |
229 | OUTPUT: RETVAL |
147 | RETVAL |
|
|
148 | |
230 | |
|
|
231 | int |
|
|
232 | unshare (int flags) |
|
|
233 | PROTOTYPE: @ |
|
|
234 | |
|
|
235 | int |
|
|
236 | setns (SV *fh_or_fd, int nstype = 0) |
|
|
237 | C_ARGS: s_fileno (fh_or_fd, 0), nstype |
|
|
238 | |
|
|
239 | int |
|
|
240 | pivot_root (SV *new_root, SV *old_root) |
|
|
241 | CODE: |
|
|
242 | RETVAL = syscall (SYS_pivot_root, |
|
|
243 | (const char *)SvPVbyte_nolen (new_root), |
|
|
244 | (const char *)SvPVbyte_nolen (old_root)); |
|
|
245 | OUTPUT: RETVAL |
|
|
246 | |
|
|
247 | int |
|
|
248 | kcmp (IV pid1, IV pid2, IV type, UV idx1 = 0, UV idx2 = 0) |
|
|
249 | |
|
|
250 | int |
|
|
251 | siocsifflags (char *ifname, U32 flags = IFF_UP) |
|
|
252 | CODE: |
|
|
253 | { |
|
|
254 | int saved_errno; |
|
|
255 | struct ifreq ifr; |
|
|
256 | int fd = socket (AF_INET, SOCK_DGRAM, 0); |
|
|
257 | strncpy (ifr.ifr_name, ifname, sizeof (ifr.ifr_name)); |
|
|
258 | RETVAL = ioctl (fd, SIOCSIFFLAGS, &ifr); |
|
|
259 | saved_errno = errno; |
|
|
260 | close (fd); |
|
|
261 | errno = saved_errno; |
|
|
262 | } |
|
|
263 | OUTPUT: RETVAL |
|
|
264 | |