… | |
… | |
9 | #include <unistd.h> |
9 | #include <unistd.h> |
10 | #include <fcntl.h> |
10 | #include <fcntl.h> |
11 | #include <signal.h> |
11 | #include <signal.h> |
12 | #include <sched.h> |
12 | #include <sched.h> |
13 | |
13 | |
14 | 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 ;) */ |
15 | 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 ;) */ |
16 | 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 ;) */ |
17 | |
|
|
18 | #ifndef __NR_pread64 |
|
|
19 | # define __NR_pread64 __NR_pread |
|
|
20 | #endif |
|
|
21 | #ifndef __NR_pwrite64 |
|
|
22 | # define __NR_pwrite64 __NR_pwrite |
|
|
23 | #endif |
|
|
24 | |
17 | |
25 | #define STACKSIZE 1024 /* yeah */ |
18 | #define STACKSIZE 1024 /* yeah */ |
26 | |
19 | |
27 | 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 | }; |
28 | |
25 | |
29 | typedef struct { |
26 | typedef struct { |
30 | char stack[STACKSIZE]; |
27 | char stack[STACKSIZE]; |
31 | } aio_thread; |
28 | } aio_thread; |
32 | |
29 | |
… | |
… | |
44 | int errorno; |
41 | int errorno; |
45 | SV *data, *callback; |
42 | SV *data, *callback; |
46 | void *dataptr; |
43 | void *dataptr; |
47 | STRLEN dataoffset; |
44 | STRLEN dataoffset; |
48 | |
45 | |
49 | struct stat64 *statdata; |
46 | Stat_t *statdata; |
50 | } aio_cb; |
47 | } aio_cb; |
51 | |
48 | |
52 | typedef aio_cb *aio_req; |
49 | typedef aio_cb *aio_req; |
53 | |
50 | |
54 | static int started; |
51 | static int started; |
… | |
… | |
66 | |
63 | |
67 | New (0, thr, 1, aio_thread); |
64 | New (0, thr, 1, aio_thread); |
68 | |
65 | |
69 | if (clone (aio_proc, |
66 | if (clone (aio_proc, |
70 | &(thr->stack[STACKSIZE]), |
67 | &(thr->stack[STACKSIZE]), |
71 | CLONE_VM|CLONE_FS|CLONE_FILES|CLONE_SIGHAND, |
68 | CLONE_VM|CLONE_FS|CLONE_FILES, |
72 | thr) >= 0) |
69 | thr) >= 0) |
73 | started++; |
70 | started++; |
74 | else |
71 | else |
75 | Safefree (thr); |
72 | Safefree (thr); |
76 | } |
73 | } |
… | |
… | |
185 | if (req->data) |
182 | if (req->data) |
186 | SvREFCNT_dec (req->data); |
183 | SvREFCNT_dec (req->data); |
187 | |
184 | |
188 | 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) |
189 | { |
186 | { |
190 | PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT; |
187 | PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT; |
191 | PL_laststatval = req->result; |
188 | PL_laststatval = req->result; |
192 | PL_statcache.st_dev = req->statdata->st_dev; |
189 | PL_statcache = *(req->statdata); |
193 | PL_statcache.st_ino = req->statdata->st_ino; |
|
|
194 | PL_statcache.st_mode = req->statdata->st_mode; |
|
|
195 | PL_statcache.st_nlink = req->statdata->st_nlink; |
|
|
196 | PL_statcache.st_uid = req->statdata->st_uid; |
|
|
197 | PL_statcache.st_gid = req->statdata->st_gid; |
|
|
198 | PL_statcache.st_rdev = req->statdata->st_rdev; |
|
|
199 | PL_statcache.st_size = req->statdata->st_size; |
|
|
200 | PL_statcache.st_atime = req->statdata->st_atime; |
|
|
201 | PL_statcache.st_mtime = req->statdata->st_mtime; |
|
|
202 | PL_statcache.st_ctime = req->statdata->st_ctime; |
|
|
203 | PL_statcache.st_blksize = req->statdata->st_blksize; |
|
|
204 | PL_statcache.st_blocks = req->statdata->st_blocks; |
|
|
205 | |
190 | |
206 | Safefree (req->statdata); |
191 | Safefree (req->statdata); |
207 | } |
192 | } |
208 | |
193 | |
209 | PUSHMARK (SP); |
194 | PUSHMARK (SP); |
… | |
… | |
231 | |
216 | |
232 | static sigset_t fullsigset; |
217 | static sigset_t fullsigset; |
233 | |
218 | |
234 | #undef errno |
219 | #undef errno |
235 | #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; \ |
236 | |
237 | |
237 | static int |
238 | static int |
238 | aio_proc (void *thr_arg) |
239 | aio_proc (void *thr_arg) |
239 | { |
240 | { |
240 | aio_thread *thr = thr_arg; |
241 | aio_thread *thr = thr_arg; |
241 | aio_req req; |
242 | aio_req req; |
242 | int errno; |
243 | int errno; |
243 | |
244 | |
244 | /* this is very much x86 and kernel-specific :(:(:( */ |
245 | /* this is very much kernel-specific :(:(:( */ |
245 | /* we rely on gcc's ability to create closures. */ |
246 | /* we rely on gcc's ability to create closures. */ |
246 | _syscall3(int,read,int,fd,char *,buf,size_t,count) |
247 | _syscall3(int,read,int,fd,char *,buf,size_t,count) |
247 | _syscall3(int,write,int,fd,char *,buf,size_t,count) |
248 | _syscall3(int,write,int,fd,char *,buf,size_t,count) |
248 | |
249 | |
249 | _syscall3(int,open,char *,pathname,int,flags,mode_t,mode) |
250 | _syscall3(int,open,char *,pathname,int,flags,mode_t,mode) |
250 | _syscall1(int,close,int,fd) |
251 | _syscall1(int,close,int,fd) |
251 | |
252 | |
|
|
253 | #ifdef __NR_pread64 |
252 | _syscall5(int,pread64,int,fd,char *,buf,size_t,count,unsigned int,offset_lo,unsigned int,offset_hi) |
254 | _syscall5(int,pread64,int,fd,char *,buf,size_t,count,unsigned int,offset_lo,unsigned int,offset_hi) |
253 | _syscall5(int,pwrite64,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 |
|
|
257 | _syscall4(int,pread,int,fd,char *,buf,size_t,count,offset_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 |
254 | |
262 | |
|
|
263 | |
|
|
264 | #ifdef __NR_stat64 |
255 | _syscall2(int,stat64, const char *, filename, struct stat64 *, buf) |
265 | _syscall2(int,stat64, const char *, filename, struct stat64 *, buf) |
256 | _syscall2(int,lstat64, const char *, filename, struct stat64 *, buf) |
266 | _syscall2(int,lstat64, const char *, filename, struct stat64 *, buf) |
257 | _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); |
258 | |
277 | |
259 | sigprocmask (SIG_SETMASK, &fullsigset, 0); |
278 | sigprocmask (SIG_SETMASK, &fullsigset, 0); |
|
|
279 | prctl (PR_SET_PDEATHSIG, SIGKILL); |
260 | |
280 | |
261 | /* then loop */ |
281 | /* then loop */ |
262 | while (read (reqpipe[0], (void *)&req, sizeof (req)) == sizeof (req)) |
282 | while (read (reqpipe[0], (void *)&req, sizeof (req)) == sizeof (req)) |
263 | { |
283 | { |
264 | req->thread = thr; |
284 | req->thread = thr; |
265 | errno = 0; /* strictly unnecessary */ |
285 | errno = 0; /* strictly unnecessary */ |
266 | |
286 | |
267 | switch (req->type) |
287 | switch (req->type) |
268 | { |
288 | { |
|
|
289 | #ifdef __NR_pread64 |
269 | case REQ_READ: req->result = pread64 (req->fd, req->dataptr, req->length, req->offset & 0xffffffff, req->offset >> 32); break; |
290 | case REQ_READ: req->result = pread64 (req->fd, req->dataptr, req->length, req->offset & 0xffffffff, req->offset >> 32); break; |
270 | case REQ_WRITE: req->result = pwrite64(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 |
271 | case REQ_OPEN: req->result = open (req->dataptr, req->fd, req->mode); break; |
307 | case REQ_OPEN: req->result = open (req->dataptr, req->fd, req->mode); break; |
272 | case REQ_CLOSE: req->result = close (req->fd); break; |
308 | case REQ_CLOSE: req->result = close (req->fd); break; |
273 | case REQ_STAT: req->result = stat64 (req->dataptr, req->statdata); break; |
|
|
274 | case REQ_LSTAT: req->result = lstat64 (req->dataptr, req->statdata); break; |
309 | case REQ_UNLINK: req->result = unlink (req->dataptr); break; |
275 | case REQ_FSTAT: req->result = fstat64 (req->fd, req->statdata); break; |
|
|
276 | |
310 | |
277 | case REQ_QUIT: |
311 | case REQ_QUIT: |
278 | default: |
312 | default: |
279 | write (respipe[1], (void *)&req, sizeof (req)); |
313 | write (respipe[1], (void *)&req, sizeof (req)); |
280 | return 0; |
314 | return 0; |
… | |
… | |
417 | Newz (0, req, 1, aio_cb); |
451 | Newz (0, req, 1, aio_cb); |
418 | |
452 | |
419 | if (!req) |
453 | if (!req) |
420 | croak ("out of memory during aio_req allocation"); |
454 | croak ("out of memory during aio_req allocation"); |
421 | |
455 | |
422 | New (0, req->statdata, 1, struct stat64); |
456 | New (0, req->statdata, 1, Stat_t); |
423 | |
457 | |
424 | if (!req->statdata) |
458 | if (!req->statdata) |
425 | croak ("out of memory during aio_req->statdata allocation"); |
459 | croak ("out of memory during aio_req->statdata allocation"); |
426 | |
460 | |
427 | if (SvPOK (fh_or_path)) |
461 | if (SvPOK (fh_or_path)) |
… | |
… | |
438 | |
472 | |
439 | req->callback = SvREFCNT_inc (callback); |
473 | req->callback = SvREFCNT_inc (callback); |
440 | |
474 | |
441 | send_req (req); |
475 | send_req (req); |
442 | |
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 | |
443 | int |
497 | int |
444 | poll_fileno() |
498 | poll_fileno() |
445 | PROTOTYPE: |
499 | PROTOTYPE: |
446 | CODE: |
500 | CODE: |
447 | RETVAL = respipe[0]; |
501 | RETVAL = respipe[0]; |