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.5 by root, Mon Jul 25 10:59:29 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#ifdef __has_include
15 #if !__has_include("linux/kcmp.h") // use "" as GCC wrongly expands macros
16 #undef SYS_kcmp
17 #endif
18#endif
19
20#ifdef SYS_kcmp
21 #include "linux/kcmp.h"
22 #define kcmp(pid1,pid2,type,idx1,idx2) \
23 syscall (SYS_kcmp, (pid_t)pid1, (pid_t)pid2, \
24 (int)type, (unsigned long)idx1, (unsigned long)idx2)
25#else
26 #define kcmp(pid1,pid2,type,idx1,idx2) \
27 (errno = ENOSYS, -1)
28#endif
29
30/* from schmorp.h */
31static int
32s_fileno (SV *fh, int wr)
33{
34 dTHX;
35 SvGETMAGIC (fh);
36
37 if (SvROK (fh))
38 {
39 fh = SvRV (fh);
40 SvGETMAGIC (fh);
41 }
42
43 if (SvTYPE (fh) == SVt_PVGV)
44 return PerlIO_fileno (wr ? IoOFP (sv_2io (fh)) : IoIFP (sv_2io (fh)));
45
46 if (SvOK (fh) && (SvIV (fh) >= 0) && (SvIV (fh) < 0x7fffffffL))
47 return SvIV (fh);
48
49 return -1;
50}
11 51
12static int 52static int
13clone_cb (void *arg) 53clone_cb (void *arg)
14{ 54{
15 dSP; 55 dSP;
34 74
35 static const struct { 75 static const struct {
36 const char *name; 76 const char *name;
37 IV iv; 77 IV iv;
38 } *civ, const_iv[] = { 78 } *civ, const_iv[] = {
79# define const_iv(name) { # name, (IV)name },
39# define const_iv(name) { # name, (IV) CLONE_ ## name }, 80# define const_iv_clone(name) { # name, (IV) CLONE_ ## name },
40# ifdef CLONE_FILES 81# ifdef CLONE_FILES
41 const_iv (FILES) 82 const_iv_clone (FILES)
42# endif 83# endif
43# ifdef CLONE_FS 84# ifdef CLONE_FS
44 const_iv (FS) 85 const_iv_clone (FS)
45# endif 86# endif
46# ifdef CLONE_NEWNS 87# ifdef CLONE_NEWNS
47 const_iv (NEWNS) 88 const_iv_clone (NEWNS)
48# endif 89# endif
49# ifdef CLONE_VM 90# ifdef CLONE_VM
50 const_iv (VM) 91 const_iv_clone (VM)
51# endif 92# endif
52# ifdef CLONE_THREAD 93# ifdef CLONE_THREAD
53 const_iv (THREAD) 94 const_iv_clone (THREAD)
54# endif 95# endif
55# ifdef CLONE_SIGHAND 96# ifdef CLONE_SIGHAND
56 const_iv (SIGHAND) 97 const_iv_clone (SIGHAND)
57# endif 98# endif
58# ifdef CLONE_SYSVSEM 99# ifdef CLONE_SYSVSEM
59 const_iv (SYSVSEM) 100 const_iv_clone (SYSVSEM)
60# endif 101# endif
61# ifdef CLONE_NEWUTS 102# ifdef CLONE_NEWUTS
62 const_iv (NEWUTS) 103 const_iv_clone (NEWUTS)
63# endif 104# endif
64# ifdef CLONE_NEWIPC 105# ifdef CLONE_NEWIPC
65 const_iv (NEWIPC) 106 const_iv_clone (NEWIPC)
66# endif 107# endif
67# ifdef CLONE_NEWNET 108# ifdef CLONE_NEWNET
68 const_iv (NEWNET) 109 const_iv_clone (NEWNET)
69# endif 110# endif
70# ifdef CLONE_PTRACE 111# ifdef CLONE_PTRACE
71 const_iv (PTRACE) 112 const_iv_clone (PTRACE)
72# endif 113# endif
73# ifdef CLONE_VFORK 114# ifdef CLONE_VFORK
74 const_iv (VFORK) 115 const_iv_clone (VFORK)
75# endif 116# endif
76# ifdef CLONE_SETTLS 117# ifdef CLONE_SETTLS
77 const_iv (SETTLS) 118 const_iv_clone (SETTLS)
78# endif 119# endif
79# ifdef CLONE_PARENT_SETTID 120# ifdef CLONE_PARENT_SETTID
80 const_iv (PARENT_SETTID) 121 const_iv_clone (PARENT_SETTID)
81# endif 122# endif
82# ifdef CLONE_CHILD_CLEARTID 123# ifdef CLONE_CHILD_CLEARTID
83 const_iv (CHILD_CLEARTID) 124 const_iv_clone (CHILD_CLEARTID)
84# endif 125# endif
85# ifdef CLONE_DETACHED 126# ifdef CLONE_DETACHED
86 const_iv (DETACHED) 127 const_iv_clone (DETACHED)
87# endif 128# endif
88# ifdef CLONE_UNTRACED 129# ifdef CLONE_UNTRACED
89 const_iv (UNTRACED) 130 const_iv_clone (UNTRACED)
90# endif 131# endif
91# ifdef CLONE_CHILD_SETTID 132# ifdef CLONE_CHILD_SETTID
92 const_iv (CHILD_SETTID) 133 const_iv_clone (CHILD_SETTID)
93# endif 134# endif
94# ifdef CLONE_NEWUSER 135# ifdef CLONE_NEWUSER
95 const_iv (NEWUSER) 136 const_iv_clone (NEWUSER)
96# endif 137# endif
97# ifdef CLONE_NEWPID 138# ifdef CLONE_NEWPID
98 const_iv (NEWPID) 139 const_iv_clone (NEWPID)
99# endif 140# endif
100# ifdef CLONE_IO 141# ifdef CLONE_IO
142 const_iv_clone (IO)
143# endif
144# ifdef CLONE_NEWCGROUP
145 const_iv_clone (NEWCGROUP)
146# endif
147# ifdef CLONE_PIDFD
148 const_iv_clone (PIDFD)
149# endif
150# ifdef SYS_kcmp
151 const_iv (KCMP_FILE)
152 const_iv (KCMP_VM)
153 const_iv (KCMP_FILES)
154 const_iv (KCMP_FS)
155 const_iv (KCMP_SIGHAND)
101 const_iv (IO) 156 const_iv (KCMP_IO)
157 const_iv (KCMP_SYSVSEM)
158 const_iv (KCMP_FILE)
102# endif 159# endif
103 }; 160 };
104 161
105 for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ > const_iv; civ--) 162 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)); 163 newCONSTSUB (stash, (char *)civ[-1].name, newSViv (civ[-1].iv));
107
108int
109unshare (int flags)
110 CODE:
111 RETVAL = unshare (flags);
112 OUTPUT:
113 RETVAL
114 164
115int 165int
116clone (SV *sub, IV stacksize, int flags, SV *ptid = 0, SV *tls = &PL_sv_undef) 166clone (SV *sub, IV stacksize, int flags, SV *ptid = 0, SV *tls = &PL_sv_undef)
117 CODE: 167 CODE:
118{ 168{
119 if (!stacksize) 169 if (!stacksize)
120 stacksize = 4 << 20; 170 stacksize = 4 << 20;
121 171
122 pid_t ptid_; 172 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); 173 char *stack_ptr = mmap (0, stacksize, PROT_EXEC | PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_GROWSDOWN | MAP_STACK, -1, 0);
124 174
144 } 194 }
145} 195}
146 OUTPUT: 196 OUTPUT:
147 RETVAL 197 RETVAL
148 198
199int
200unshare (int flags)
201
202int
203setns (SV *fh_or_fd, int nstype = 0)
204 C_ARGS: s_fileno (fh_or_fd, 0), nstype
205
206int
207pivot_root (SV *new_root, SV *old_root)
208 CODE:
209 RETVAL = syscall (SYS_pivot_root,
210 (const char *)SvPVbyte_nolen (new_root),
211 (const char *)SvPVbyte_nolen (old_root));
212 OUTPUT:
213 RETVAL
214
215int
216kcmp (IV pid1, IV pid2, IV type, UV idx1 = 0, UV idx2 = 0)
217

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines