|
|
1 | #define PERL_NO_GET_CONTEXT |
|
|
2 | |
1 | #include "EXTERN.h" |
3 | #include "EXTERN.h" |
2 | #include "perl.h" |
4 | #include "perl.h" |
3 | #include "XSUB.h" |
5 | #include "XSUB.h" |
4 | |
6 | |
5 | #include <sys/types.h> |
7 | #include <sys/types.h> |
… | |
… | |
7 | #include <unistd.h> |
9 | #include <unistd.h> |
8 | #include <fcntl.h> |
10 | #include <fcntl.h> |
9 | #include <signal.h> |
11 | #include <signal.h> |
10 | #include <sched.h> |
12 | #include <sched.h> |
11 | |
13 | |
12 | typedef void *InputStream; /* hack, but 5.6.1 is simply toooo old ;) */ |
14 | typedef void *InputStream; /* hack, but 5.6.1 is simply toooo old ;) */ |
13 | typedef void *OutputStream; /* hack, but 5.6.1 is simply toooo old ;) */ |
15 | typedef void *OutputStream; /* hack, but 5.6.1 is simply toooo old ;) */ |
14 | typedef void *InOutStream; /* hack, but 5.6.1 is simply toooo old ;) */ |
16 | typedef void *InOutStream; /* hack, but 5.6.1 is simply toooo old ;) */ |
15 | |
17 | |
16 | #define STACKSIZE 1024 /* yeah */ |
18 | #define STACKSIZE 1024 /* yeah */ |
17 | |
19 | |
18 | enum { REQ_QUIT, REQ_OPEN, REQ_CLOSE, REQ_READ, REQ_WRITE, REQ_STAT, REQ_LSTAT, REQ_FSTAT}; |
20 | enum { |
|
|
21 | REQ_QUIT, |
|
|
22 | REQ_OPEN, REQ_CLOSE, REQ_READ, REQ_WRITE, |
|
|
23 | REQ_STAT, REQ_LSTAT, REQ_FSTAT, REQ_UNLINK |
|
|
24 | }; |
19 | |
25 | |
20 | typedef struct { |
26 | typedef struct { |
21 | char stack[STACKSIZE]; |
27 | char stack[STACKSIZE]; |
22 | } aio_thread; |
28 | } aio_thread; |
23 | |
29 | |
24 | typedef struct { |
30 | typedef struct aio_cb { |
|
|
31 | struct aio_cb *next; |
|
|
32 | |
25 | int type; |
33 | int type; |
26 | aio_thread *thread; |
34 | aio_thread *thread; |
27 | |
35 | |
28 | int fd; |
36 | int fd; |
29 | off_t offset; |
37 | off_t offset; |
… | |
… | |
33 | int errorno; |
41 | int errorno; |
34 | SV *data, *callback; |
42 | SV *data, *callback; |
35 | void *dataptr; |
43 | void *dataptr; |
36 | STRLEN dataoffset; |
44 | STRLEN dataoffset; |
37 | |
45 | |
38 | struct stat64 *statdata; |
46 | Stat_t *statdata; |
39 | } aio_cb; |
47 | } aio_cb; |
40 | |
48 | |
41 | typedef aio_cb *aio_req; |
49 | typedef aio_cb *aio_req; |
42 | |
50 | |
43 | static int started; |
51 | static int started; |
44 | static int nreqs; |
52 | static int nreqs; |
45 | static int reqpipe[2], respipe[2]; |
53 | static int reqpipe[2], respipe[2]; |
46 | |
54 | |
|
|
55 | static aio_req qs, qe; /* queue start, queue end */ |
|
|
56 | |
47 | static int aio_proc(void *arg); |
57 | static int aio_proc(void *arg); |
48 | |
58 | |
49 | static void |
59 | static void |
50 | start_thread(void) |
60 | start_thread (void) |
51 | { |
61 | { |
52 | aio_thread *thr; |
62 | aio_thread *thr; |
53 | |
63 | |
54 | New (0, thr, 1, aio_thread); |
64 | New (0, thr, 1, aio_thread); |
55 | |
65 | |
56 | if (clone (aio_proc, |
66 | if (clone (aio_proc, |
57 | &(thr->stack[STACKSIZE]), |
67 | &(thr->stack[STACKSIZE]), |
58 | CLONE_VM|CLONE_FS|CLONE_FILES|CLONE_SIGHAND, |
68 | CLONE_VM|CLONE_FS|CLONE_FILES, |
59 | thr) >= 0) |
69 | thr) >= 0) |
60 | started++; |
70 | started++; |
61 | else |
71 | else |
62 | Safefree (thr); |
72 | Safefree (thr); |
63 | } |
73 | } |
64 | |
74 | |
65 | static void |
75 | static void |
|
|
76 | send_reqs (void) |
|
|
77 | { |
|
|
78 | /* this write is atomic */ |
|
|
79 | while (qs && write (reqpipe[1], &qs, sizeof qs) == sizeof qs) |
|
|
80 | { |
|
|
81 | qs = qs->next; |
|
|
82 | if (!qs) qe = 0; |
|
|
83 | } |
|
|
84 | } |
|
|
85 | |
|
|
86 | static void |
|
|
87 | send_req (aio_req req) |
|
|
88 | { |
|
|
89 | nreqs++; |
|
|
90 | req->next = 0; |
|
|
91 | |
|
|
92 | if (qe) |
|
|
93 | qe->next = req; |
|
|
94 | else |
|
|
95 | qe = qs = req; |
|
|
96 | |
|
|
97 | send_reqs (); |
|
|
98 | } |
|
|
99 | |
|
|
100 | static void |
66 | end_thread(void) |
101 | end_thread (void) |
67 | { |
102 | { |
68 | aio_req req; |
103 | aio_req req; |
69 | New (0, req, 1, aio_cb); |
104 | New (0, req, 1, aio_cb); |
70 | req->type = REQ_QUIT; |
105 | req->type = REQ_QUIT; |
71 | write (reqpipe[1], &req, sizeof (aio_req)); |
106 | |
|
|
107 | send_req (req); |
72 | } |
108 | } |
73 | |
109 | |
74 | static void |
110 | static void |
75 | send_req (aio_req req) |
111 | read_write (pTHX_ |
76 | { |
|
|
77 | nreqs++; |
|
|
78 | write (reqpipe[1], &req, sizeof (aio_req)); |
|
|
79 | } |
|
|
80 | |
|
|
81 | static void |
|
|
82 | read_write (pTHX_ int dowrite, int fd, off_t offset, size_t length, |
112 | int dowrite, int fd, off_t offset, size_t length, |
83 | SV *data, STRLEN dataoffset, SV*callback) |
113 | SV *data, STRLEN dataoffset, SV *callback) |
84 | { |
114 | { |
85 | aio_req req; |
115 | aio_req req; |
86 | STRLEN svlen; |
116 | STRLEN svlen; |
87 | char *svptr = SvPV (data, svlen); |
117 | char *svptr = SvPV (data, svlen); |
88 | |
118 | |
… | |
… | |
152 | if (req->data) |
182 | if (req->data) |
153 | SvREFCNT_dec (req->data); |
183 | SvREFCNT_dec (req->data); |
154 | |
184 | |
155 | if (req->type == REQ_STAT || req->type == REQ_LSTAT || req->type == REQ_FSTAT) |
185 | if (req->type == REQ_STAT || req->type == REQ_LSTAT || req->type == REQ_FSTAT) |
156 | { |
186 | { |
157 | PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT; |
187 | PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT; |
158 | PL_laststatval = req->result; |
188 | PL_laststatval = req->result; |
159 | PL_statcache.st_dev = req->statdata->st_dev; |
189 | PL_statcache = *(req->statdata); |
160 | PL_statcache.st_ino = req->statdata->st_ino; |
|
|
161 | PL_statcache.st_mode = req->statdata->st_mode; |
|
|
162 | PL_statcache.st_nlink = req->statdata->st_nlink; |
|
|
163 | PL_statcache.st_uid = req->statdata->st_uid; |
|
|
164 | PL_statcache.st_gid = req->statdata->st_gid; |
|
|
165 | PL_statcache.st_rdev = req->statdata->st_rdev; |
|
|
166 | PL_statcache.st_size = req->statdata->st_size; |
|
|
167 | PL_statcache.st_atime = req->statdata->st_atime; |
|
|
168 | PL_statcache.st_mtime = req->statdata->st_mtime; |
|
|
169 | PL_statcache.st_ctime = req->statdata->st_ctime; |
|
|
170 | PL_statcache.st_blksize = req->statdata->st_blksize; |
|
|
171 | PL_statcache.st_blocks = req->statdata->st_blocks; |
|
|
172 | |
190 | |
173 | Safefree (req->statdata); |
191 | Safefree (req->statdata); |
174 | } |
192 | } |
175 | |
193 | |
176 | PUSHMARK (SP); |
194 | PUSHMARK (SP); |
… | |
… | |
188 | } |
206 | } |
189 | |
207 | |
190 | Safefree (req); |
208 | Safefree (req); |
191 | } |
209 | } |
192 | |
210 | |
|
|
211 | if (qs) |
|
|
212 | send_reqs (); |
|
|
213 | |
193 | return count; |
214 | return count; |
194 | } |
215 | } |
195 | |
216 | |
196 | static sigset_t fullsigset; |
217 | static sigset_t fullsigset; |
197 | |
218 | |
198 | #undef errno |
219 | #undef errno |
199 | #include <asm/unistd.h> |
220 | #include <asm/unistd.h> |
|
|
221 | #include <sys/prctl.h> |
|
|
222 | |
|
|
223 | #define COPY_STATDATA \ |
|
|
224 | req->statdata->st_dev = statdata.st_dev; \ |
|
|
225 | req->statdata->st_ino = statdata.st_ino; \ |
|
|
226 | req->statdata->st_mode = statdata.st_mode; \ |
|
|
227 | req->statdata->st_nlink = statdata.st_nlink; \ |
|
|
228 | req->statdata->st_uid = statdata.st_uid; \ |
|
|
229 | req->statdata->st_gid = statdata.st_gid; \ |
|
|
230 | req->statdata->st_rdev = statdata.st_rdev; \ |
|
|
231 | req->statdata->st_size = statdata.st_size; \ |
|
|
232 | req->statdata->st_atime = statdata.st_atime; \ |
|
|
233 | req->statdata->st_mtime = statdata.st_mtime; \ |
|
|
234 | req->statdata->st_ctime = statdata.st_ctime; \ |
|
|
235 | req->statdata->st_blksize = statdata.st_blksize; \ |
|
|
236 | req->statdata->st_blocks = statdata.st_blocks; \ |
200 | |
237 | |
201 | static int |
238 | static int |
202 | aio_proc(void *thr_arg) |
239 | aio_proc (void *thr_arg) |
203 | { |
240 | { |
204 | aio_thread *thr = thr_arg; |
241 | aio_thread *thr = thr_arg; |
205 | aio_req req; |
242 | aio_req req; |
206 | int errno; |
243 | int errno; |
207 | |
244 | |
|
|
245 | /* this is very much kernel-specific :(:(:( */ |
208 | /* we rely on gcc's ability to create closures. */ |
246 | /* we rely on gcc's ability to create closures. */ |
209 | _syscall3(int,read,int,fd,char *,buf,size_t,count) |
247 | _syscall3(int,read,int,fd,char *,buf,size_t,count) |
210 | _syscall3(int,write,int,fd,char *,buf,size_t,count) |
248 | _syscall3(int,write,int,fd,char *,buf,size_t,count) |
211 | |
249 | |
212 | _syscall3(int,open,char *,pathname,int,flags,mode_t,mode) |
250 | _syscall3(int,open,char *,pathname,int,flags,mode_t,mode) |
213 | _syscall1(int,close,int,fd) |
251 | _syscall1(int,close,int,fd) |
214 | |
252 | |
|
|
253 | #ifdef __NR_pread64 |
|
|
254 | _syscall5(int,pread64,int,fd,char *,buf,size_t,count,unsigned int,offset_lo,unsigned int,offset_hi) |
|
|
255 | _syscall5(int,pwrite64,int,fd,char *,buf,size_t,count,unsigned int,offset_lo,unsigned int,offset_hi) |
|
|
256 | #elif __NR_pread |
215 | _syscall4(int,pread,int,fd,char *,buf,size_t,count,off_t,offset) |
257 | _syscall4(int,pread,int,fd,char *,buf,size_t,count,offset_t,offset) |
216 | _syscall4(int,pwrite,int,fd,char *,buf,size_t,count,off_t,offset) |
258 | _syscall4(int,pwrite,int,fd,char *,buf,size_t,count,offset_t,offset) |
|
|
259 | #else |
|
|
260 | # error "neither pread nor pread64 defined" |
|
|
261 | #endif |
217 | |
262 | |
|
|
263 | |
|
|
264 | #ifdef __NR_stat64 |
218 | _syscall2(int,stat64, const char *, filename, struct stat64 *, buf) |
265 | _syscall2(int,stat64, const char *, filename, struct stat64 *, buf) |
219 | _syscall2(int,lstat64, const char *, filename, struct stat64 *, buf) |
266 | _syscall2(int,lstat64, const char *, filename, struct stat64 *, buf) |
220 | _syscall2(int,fstat64, int, fd, struct stat64 *, buf) |
267 | _syscall2(int,fstat64, int, fd, struct stat64 *, buf) |
|
|
268 | #elif __NR_stat |
|
|
269 | _syscall2(int,stat, const char *, filename, struct stat *, buf) |
|
|
270 | _syscall2(int,lstat, const char *, filename, struct stat *, buf) |
|
|
271 | _syscall2(int,fstat, int, fd, struct stat *, buf) |
|
|
272 | #else |
|
|
273 | # error "neither stat64 nor stat defined" |
|
|
274 | #endif |
|
|
275 | |
|
|
276 | _syscall1(int,unlink, char *, filename); |
221 | |
277 | |
222 | sigprocmask (SIG_SETMASK, &fullsigset, 0); |
278 | sigprocmask (SIG_SETMASK, &fullsigset, 0); |
|
|
279 | prctl (PR_SET_PDEATHSIG, SIGKILL); |
223 | |
280 | |
224 | /* then loop */ |
281 | /* then loop */ |
225 | while (read (reqpipe[0], (void *)&req, sizeof (req)) == sizeof (req)) |
282 | while (read (reqpipe[0], (void *)&req, sizeof (req)) == sizeof (req)) |
226 | { |
283 | { |
227 | req->thread = thr; |
284 | req->thread = thr; |
228 | errno = 0; /* strictly unnecessary */ |
285 | errno = 0; /* strictly unnecessary */ |
229 | |
286 | |
230 | if (req->type == REQ_READ) |
287 | switch (req->type) |
231 | req->result = pread (req->fd, req->dataptr, req->length, req->offset); |
|
|
232 | else if (req->type == REQ_WRITE) |
|
|
233 | req->result = pwrite (req->fd, req->dataptr, req->length, req->offset); |
|
|
234 | else if (req->type == REQ_OPEN) |
|
|
235 | req->result = open (req->dataptr, req->fd, req->mode); |
|
|
236 | else if (req->type == REQ_CLOSE) |
|
|
237 | req->result = close (req->fd); |
|
|
238 | else if (req->type == REQ_STAT) |
|
|
239 | req->result = stat64 (req->dataptr, req->statdata); |
|
|
240 | else if (req->type == REQ_LSTAT) |
|
|
241 | req->result = lstat64 (req->dataptr, req->statdata); |
|
|
242 | else if (req->type == REQ_FSTAT) |
|
|
243 | req->result = fstat64 (req->fd, req->statdata); |
|
|
244 | else |
|
|
245 | { |
288 | { |
|
|
289 | #ifdef __NR_pread64 |
|
|
290 | case REQ_READ: req->result = pread64 (req->fd, req->dataptr, req->length, req->offset & 0xffffffff, req->offset >> 32); break; |
|
|
291 | case REQ_WRITE: req->result = pwrite64(req->fd, req->dataptr, req->length, req->offset & 0xffffffff, req->offset >> 32); break; |
|
|
292 | #else |
|
|
293 | case REQ_READ: req->result = pread (req->fd, req->dataptr, req->length, req->offset); break; |
|
|
294 | case REQ_WRITE: req->result = pwrite (req->fd, req->dataptr, req->length, req->offset); break; |
|
|
295 | #endif |
|
|
296 | #ifdef __NR_stat64 |
|
|
297 | struct stat64 statdata; |
|
|
298 | case REQ_STAT: req->result = stat64 (req->dataptr, &statdata); COPY_STATDATA; break; |
|
|
299 | case REQ_LSTAT: req->result = lstat64 (req->dataptr, &statdata); COPY_STATDATA; break; |
|
|
300 | case REQ_FSTAT: req->result = fstat64 (req->fd, &statdata); COPY_STATDATA; break; |
|
|
301 | #else |
|
|
302 | struct stat statdata; |
|
|
303 | case REQ_STAT: req->result = stat (req->dataptr, &statdata); COPY_STATDATA; break; |
|
|
304 | case REQ_LSTAT: req->result = lstat (req->dataptr, &statdata); COPY_STATDATA; break; |
|
|
305 | case REQ_FSTAT: req->result = fstat (req->fd, &statdata); COPY_STATDATA; break; |
|
|
306 | #endif |
|
|
307 | case REQ_OPEN: req->result = open (req->dataptr, req->fd, req->mode); break; |
|
|
308 | case REQ_CLOSE: req->result = close (req->fd); break; |
|
|
309 | case REQ_UNLINK: req->result = unlink (req->dataptr); break; |
|
|
310 | |
|
|
311 | case REQ_QUIT: |
|
|
312 | default: |
246 | write (respipe[1], (void *)&req, sizeof (req)); |
313 | write (respipe[1], (void *)&req, sizeof (req)); |
247 | break; |
314 | return 0; |
248 | } |
315 | } |
249 | |
316 | |
250 | req->errorno = errno; |
317 | req->errorno = errno; |
251 | write (respipe[1], (void *)&req, sizeof (req)); |
318 | write (respipe[1], (void *)&req, sizeof (req)); |
252 | } |
319 | } |
… | |
… | |
264 | sigdelset (&fullsigset, SIGABRT); |
331 | sigdelset (&fullsigset, SIGABRT); |
265 | sigdelset (&fullsigset, SIGINT); |
332 | sigdelset (&fullsigset, SIGINT); |
266 | |
333 | |
267 | if (pipe (reqpipe) || pipe (respipe)) |
334 | if (pipe (reqpipe) || pipe (respipe)) |
268 | croak ("unable to initialize request or result pipe"); |
335 | croak ("unable to initialize request or result pipe"); |
|
|
336 | |
|
|
337 | if (fcntl (reqpipe[1], F_SETFL, O_NONBLOCK)) |
|
|
338 | croak ("cannot set result pipe to nonblocking mode"); |
269 | |
339 | |
270 | if (fcntl (respipe[0], F_SETFL, O_NONBLOCK)) |
340 | if (fcntl (respipe[0], F_SETFL, O_NONBLOCK)) |
271 | croak ("cannot set result pipe to nonblocking mode"); |
341 | croak ("cannot set result pipe to nonblocking mode"); |
272 | } |
342 | } |
273 | |
343 | |
… | |
… | |
296 | fd_set rfd; |
366 | fd_set rfd; |
297 | FD_ZERO(&rfd); |
367 | FD_ZERO(&rfd); |
298 | FD_SET(respipe[0], &rfd); |
368 | FD_SET(respipe[0], &rfd); |
299 | |
369 | |
300 | select (respipe[0] + 1, &rfd, 0, 0, 0); |
370 | select (respipe[0] + 1, &rfd, 0, 0, 0); |
301 | poll_cb (); |
371 | poll_cb (aTHX); |
302 | } |
372 | } |
303 | |
373 | |
304 | void |
374 | void |
305 | aio_open(pathname,flags,mode,callback) |
375 | aio_open(pathname,flags,mode,callback) |
306 | SV * pathname |
376 | SV * pathname |
… | |
… | |
381 | Newz (0, req, 1, aio_cb); |
451 | Newz (0, req, 1, aio_cb); |
382 | |
452 | |
383 | if (!req) |
453 | if (!req) |
384 | croak ("out of memory during aio_req allocation"); |
454 | croak ("out of memory during aio_req allocation"); |
385 | |
455 | |
386 | New (0, req->statdata, 1, struct stat64); |
456 | New (0, req->statdata, 1, Stat_t); |
387 | |
457 | |
388 | if (!req->statdata) |
458 | if (!req->statdata) |
389 | croak ("out of memory during aio_req->statdata allocation"); |
459 | croak ("out of memory during aio_req->statdata allocation"); |
390 | |
460 | |
391 | if (SvPOK (fh_or_path)) |
461 | if (SvPOK (fh_or_path)) |
… | |
… | |
402 | |
472 | |
403 | req->callback = SvREFCNT_inc (callback); |
473 | req->callback = SvREFCNT_inc (callback); |
404 | |
474 | |
405 | send_req (req); |
475 | send_req (req); |
406 | |
476 | |
|
|
477 | void |
|
|
478 | aio_unlink(pathname,callback) |
|
|
479 | SV * pathname |
|
|
480 | SV * callback |
|
|
481 | PROTOTYPE: $$ |
|
|
482 | CODE: |
|
|
483 | aio_req req; |
|
|
484 | |
|
|
485 | Newz (0, req, 1, aio_cb); |
|
|
486 | |
|
|
487 | if (!req) |
|
|
488 | croak ("out of memory during aio_req allocation"); |
|
|
489 | |
|
|
490 | req->type = REQ_UNLINK; |
|
|
491 | req->data = newSVsv (pathname); |
|
|
492 | req->dataptr = SvPV_nolen (req->data); |
|
|
493 | req->callback = SvREFCNT_inc (callback); |
|
|
494 | |
|
|
495 | send_req (req); |
|
|
496 | |
407 | int |
497 | int |
408 | poll_fileno() |
498 | poll_fileno() |
409 | PROTOTYPE: |
499 | PROTOTYPE: |
410 | CODE: |
500 | CODE: |
411 | RETVAL = respipe[0]; |
501 | RETVAL = respipe[0]; |