1 |
root |
1.1 |
#include "EXTERN.h" |
2 |
|
|
#include "perl.h" |
3 |
|
|
#include "XSUB.h" |
4 |
|
|
|
5 |
|
|
#include <sys/errno.h> |
6 |
|
|
#include <sys/mman.h> |
7 |
|
|
|
8 |
|
|
#undef _GNU_SOURCE |
9 |
|
|
#define _GNU_SOURCE |
10 |
|
|
#include <sched.h> |
11 |
root |
1.3 |
#include <unistd.h> |
12 |
|
|
#include <sys/syscall.h> |
13 |
|
|
|
14 |
root |
1.6 |
#include <sched.h> |
15 |
|
|
|
16 |
|
|
#include <sys/ioctl.h> |
17 |
|
|
#include <net/if.h> |
18 |
|
|
|
19 |
root |
1.4 |
#ifdef __has_include |
20 |
|
|
#if !__has_include("linux/kcmp.h") // use "" as GCC wrongly expands macros |
21 |
|
|
#undef SYS_kcmp |
22 |
|
|
#endif |
23 |
|
|
#endif |
24 |
|
|
|
25 |
root |
1.3 |
#ifdef SYS_kcmp |
26 |
root |
1.4 |
#include "linux/kcmp.h" |
27 |
root |
1.3 |
#define kcmp(pid1,pid2,type,idx1,idx2) \ |
28 |
|
|
syscall (SYS_kcmp, (pid_t)pid1, (pid_t)pid2, \ |
29 |
|
|
(int)type, (unsigned long)idx1, (unsigned long)idx2) |
30 |
|
|
#else |
31 |
|
|
#define kcmp(pid1,pid2,type,idx1,idx2) \ |
32 |
|
|
(errno = ENOSYS, -1) |
33 |
|
|
#endif |
34 |
root |
1.1 |
|
35 |
root |
1.2 |
/* from schmorp.h */ |
36 |
|
|
static int |
37 |
|
|
s_fileno (SV *fh, int wr) |
38 |
|
|
{ |
39 |
|
|
dTHX; |
40 |
|
|
SvGETMAGIC (fh); |
41 |
|
|
|
42 |
|
|
if (SvROK (fh)) |
43 |
|
|
{ |
44 |
|
|
fh = SvRV (fh); |
45 |
|
|
SvGETMAGIC (fh); |
46 |
|
|
} |
47 |
|
|
|
48 |
|
|
if (SvTYPE (fh) == SVt_PVGV) |
49 |
|
|
return PerlIO_fileno (wr ? IoOFP (sv_2io (fh)) : IoIFP (sv_2io (fh))); |
50 |
|
|
|
51 |
|
|
if (SvOK (fh) && (SvIV (fh) >= 0) && (SvIV (fh) < 0x7fffffffL)) |
52 |
|
|
return SvIV (fh); |
53 |
|
|
|
54 |
|
|
return -1; |
55 |
|
|
} |
56 |
|
|
|
57 |
root |
1.1 |
static int |
58 |
|
|
clone_cb (void *arg) |
59 |
|
|
{ |
60 |
|
|
dSP; |
61 |
|
|
|
62 |
|
|
PUSHMARK (SP); |
63 |
|
|
|
64 |
|
|
PUTBACK; |
65 |
|
|
int count = call_sv (sv_2mortal ((SV *)arg), G_SCALAR); |
66 |
|
|
SPAGAIN; |
67 |
|
|
int retval = count ? SvIV (POPs) : 0; |
68 |
|
|
PUTBACK; |
69 |
|
|
|
70 |
|
|
return retval; |
71 |
|
|
} |
72 |
|
|
|
73 |
|
|
MODULE = Linux::Clone PACKAGE = Linux::Clone |
74 |
|
|
|
75 |
|
|
PROTOTYPES: ENABLE |
76 |
|
|
|
77 |
|
|
BOOT: |
78 |
|
|
HV *stash = gv_stashpv ("Linux::Clone", 1); |
79 |
|
|
|
80 |
|
|
static const struct { |
81 |
|
|
const char *name; |
82 |
|
|
IV iv; |
83 |
|
|
} *civ, const_iv[] = { |
84 |
root |
1.4 |
# define const_iv(name) { # name, (IV)name }, |
85 |
|
|
# define const_iv_clone(name) { # name, (IV) CLONE_ ## name }, |
86 |
root |
1.6 |
# ifdef CSIGNAL |
87 |
|
|
const_iv (CSIGNAL) |
88 |
|
|
# endif |
89 |
root |
1.1 |
# ifdef CLONE_FILES |
90 |
root |
1.4 |
const_iv_clone (FILES) |
91 |
root |
1.1 |
# endif |
92 |
|
|
# ifdef CLONE_FS |
93 |
root |
1.4 |
const_iv_clone (FS) |
94 |
root |
1.1 |
# endif |
95 |
|
|
# ifdef CLONE_NEWNS |
96 |
root |
1.4 |
const_iv_clone (NEWNS) |
97 |
root |
1.1 |
# endif |
98 |
|
|
# ifdef CLONE_VM |
99 |
root |
1.4 |
const_iv_clone (VM) |
100 |
root |
1.1 |
# endif |
101 |
|
|
# ifdef CLONE_THREAD |
102 |
root |
1.4 |
const_iv_clone (THREAD) |
103 |
root |
1.1 |
# endif |
104 |
|
|
# ifdef CLONE_SIGHAND |
105 |
root |
1.4 |
const_iv_clone (SIGHAND) |
106 |
root |
1.1 |
# endif |
107 |
|
|
# ifdef CLONE_SYSVSEM |
108 |
root |
1.4 |
const_iv_clone (SYSVSEM) |
109 |
root |
1.1 |
# endif |
110 |
|
|
# ifdef CLONE_NEWUTS |
111 |
root |
1.4 |
const_iv_clone (NEWUTS) |
112 |
root |
1.1 |
# endif |
113 |
|
|
# ifdef CLONE_NEWIPC |
114 |
root |
1.4 |
const_iv_clone (NEWIPC) |
115 |
root |
1.1 |
# endif |
116 |
|
|
# ifdef CLONE_NEWNET |
117 |
root |
1.4 |
const_iv_clone (NEWNET) |
118 |
root |
1.1 |
# endif |
119 |
|
|
# ifdef CLONE_PTRACE |
120 |
root |
1.4 |
const_iv_clone (PTRACE) |
121 |
root |
1.1 |
# endif |
122 |
|
|
# ifdef CLONE_VFORK |
123 |
root |
1.4 |
const_iv_clone (VFORK) |
124 |
root |
1.1 |
# endif |
125 |
|
|
# ifdef CLONE_SETTLS |
126 |
root |
1.4 |
const_iv_clone (SETTLS) |
127 |
root |
1.1 |
# endif |
128 |
|
|
# ifdef CLONE_PARENT_SETTID |
129 |
root |
1.4 |
const_iv_clone (PARENT_SETTID) |
130 |
root |
1.1 |
# endif |
131 |
|
|
# ifdef CLONE_CHILD_CLEARTID |
132 |
root |
1.4 |
const_iv_clone (CHILD_CLEARTID) |
133 |
root |
1.1 |
# endif |
134 |
|
|
# ifdef CLONE_DETACHED |
135 |
root |
1.4 |
const_iv_clone (DETACHED) |
136 |
root |
1.1 |
# endif |
137 |
|
|
# ifdef CLONE_UNTRACED |
138 |
root |
1.4 |
const_iv_clone (UNTRACED) |
139 |
root |
1.1 |
# endif |
140 |
|
|
# ifdef CLONE_CHILD_SETTID |
141 |
root |
1.4 |
const_iv_clone (CHILD_SETTID) |
142 |
root |
1.1 |
# endif |
143 |
|
|
# ifdef CLONE_NEWUSER |
144 |
root |
1.4 |
const_iv_clone (NEWUSER) |
145 |
root |
1.1 |
# endif |
146 |
|
|
# ifdef CLONE_NEWPID |
147 |
root |
1.4 |
const_iv_clone (NEWPID) |
148 |
root |
1.1 |
# endif |
149 |
|
|
# ifdef CLONE_IO |
150 |
root |
1.4 |
const_iv_clone (IO) |
151 |
root |
1.1 |
# endif |
152 |
root |
1.2 |
# ifdef CLONE_NEWCGROUP |
153 |
root |
1.4 |
const_iv_clone (NEWCGROUP) |
154 |
root |
1.3 |
# endif |
155 |
root |
1.5 |
# ifdef CLONE_PIDFD |
156 |
|
|
const_iv_clone (PIDFD) |
157 |
|
|
# endif |
158 |
root |
1.3 |
# ifdef SYS_kcmp |
159 |
|
|
const_iv (KCMP_FILE) |
160 |
|
|
const_iv (KCMP_VM) |
161 |
|
|
const_iv (KCMP_FILES) |
162 |
|
|
const_iv (KCMP_FS) |
163 |
|
|
const_iv (KCMP_SIGHAND) |
164 |
|
|
const_iv (KCMP_IO) |
165 |
|
|
const_iv (KCMP_SYSVSEM) |
166 |
|
|
const_iv (KCMP_FILE) |
167 |
root |
1.2 |
# endif |
168 |
root |
1.1 |
}; |
169 |
|
|
|
170 |
|
|
for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ > const_iv; civ--) |
171 |
|
|
newCONSTSUB (stash, (char *)civ[-1].name, newSViv (civ[-1].iv)); |
172 |
|
|
|
173 |
|
|
int |
174 |
|
|
clone (SV *sub, IV stacksize, int flags, SV *ptid = 0, SV *tls = &PL_sv_undef) |
175 |
|
|
CODE: |
176 |
|
|
{ |
177 |
root |
1.2 |
if (!stacksize) |
178 |
root |
1.1 |
stacksize = 4 << 20; |
179 |
|
|
|
180 |
|
|
pid_t ptid_; |
181 |
|
|
char *stack_ptr = mmap (0, stacksize, PROT_EXEC | PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_GROWSDOWN | MAP_STACK, -1, 0); |
182 |
|
|
|
183 |
|
|
#ifndef __hppa |
184 |
|
|
stack_ptr += stacksize - 16; /* be safe and put the sp at 16 bytes below the end */ |
185 |
|
|
#endif |
186 |
|
|
|
187 |
|
|
RETVAL = -1; |
188 |
|
|
if (stack_ptr != (void *)-1) |
189 |
|
|
{ |
190 |
|
|
SV *my_sub = newSVsv (sub); |
191 |
|
|
|
192 |
|
|
RETVAL = clone (clone_cb, (void *)stack_ptr, flags, (void *)my_sub, &ptid, SvOK (tls) ? SvPV_nolen (tls) : 0, 0); |
193 |
|
|
|
194 |
|
|
if (ptid) sv_setiv (ptid, (IV)ptid_); |
195 |
|
|
|
196 |
|
|
if ((flags & (CLONE_VM | CLONE_VFORK)) != CLONE_VM) |
197 |
|
|
{ |
198 |
|
|
int old_errno = errno; |
199 |
|
|
munmap (stack_ptr, stacksize); |
200 |
|
|
errno = old_errno; |
201 |
|
|
} |
202 |
|
|
} |
203 |
|
|
} |
204 |
root |
1.6 |
OUTPUT: RETVAL |
205 |
root |
1.1 |
|
206 |
root |
1.2 |
int |
207 |
|
|
unshare (int flags) |
208 |
|
|
|
209 |
|
|
int |
210 |
|
|
setns (SV *fh_or_fd, int nstype = 0) |
211 |
|
|
C_ARGS: s_fileno (fh_or_fd, 0), nstype |
212 |
root |
1.3 |
|
213 |
|
|
int |
214 |
|
|
pivot_root (SV *new_root, SV *old_root) |
215 |
|
|
CODE: |
216 |
|
|
RETVAL = syscall (SYS_pivot_root, |
217 |
|
|
(const char *)SvPVbyte_nolen (new_root), |
218 |
|
|
(const char *)SvPVbyte_nolen (old_root)); |
219 |
root |
1.6 |
OUTPUT: RETVAL |
220 |
root |
1.3 |
|
221 |
|
|
int |
222 |
|
|
kcmp (IV pid1, IV pid2, IV type, UV idx1 = 0, UV idx2 = 0) |
223 |
|
|
|
224 |
root |
1.6 |
int |
225 |
|
|
siocsifflags (char *ifname, U32 flags = IFF_UP) |
226 |
|
|
CODE: |
227 |
|
|
{ |
228 |
|
|
int saved_errno; |
229 |
|
|
struct ifreq ifr; |
230 |
|
|
int fd = socket (AF_INET, SOCK_DGRAM, 0); |
231 |
|
|
strncpy (ifr.ifr_name, ifname, sizeof (ifr.ifr_name)); |
232 |
|
|
RETVAL = ioctl (fd, SIOCSIFFLAGS, &ifr); |
233 |
|
|
saved_errno = errno; |
234 |
|
|
close (fd); |
235 |
|
|
errno = saved_errno; |
236 |
|
|
} |
237 |
|
|
OUTPUT: RETVAL |
238 |
|
|
|