ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Linux-Clone/Clone.xs
(Generate patch)

Comparing Linux-Clone/Clone.xs (file contents):
Revision 1.1 by root, Mon Nov 28 05:43:03 2011 UTC vs.
Revision 1.7 by root, Sat Sep 3 23:56:10 2022 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines