… | |
… | |
11 | #include <stddef.h> |
11 | #include <stddef.h> |
12 | #include <stdlib.h> |
12 | #include <stdlib.h> |
13 | #include <errno.h> |
13 | #include <errno.h> |
14 | #include <sys/types.h> |
14 | #include <sys/types.h> |
15 | #include <sys/stat.h> |
15 | #include <sys/stat.h> |
16 | #include <sys/statvfs.h> |
|
|
17 | #include <limits.h> |
16 | #include <limits.h> |
18 | #include <fcntl.h> |
17 | #include <fcntl.h> |
19 | #include <sched.h> |
18 | #include <sched.h> |
20 | |
19 | |
21 | #if _POSIX_MEMLOCK || _POSIX_MEMLOCK_RANGE || _POSIX_MAPPED_FILES |
20 | #if _POSIX_MEMLOCK || _POSIX_MEMLOCK_RANGE || _POSIX_MAPPED_FILES |
22 | # include <sys/mman.h> |
21 | # include <sys/mman.h> |
23 | #endif |
22 | #endif |
24 | |
23 | |
25 | /* perl namespace pollution */ |
24 | /* perl namespace pollution */ |
26 | #undef VERSION |
25 | #undef VERSION |
27 | |
|
|
28 | #ifdef _WIN32 |
|
|
29 | |
|
|
30 | # define EIO_STRUCT_DIRENT Direntry_t |
|
|
31 | # undef malloc |
|
|
32 | # undef free |
|
|
33 | |
|
|
34 | // perl overrides all those nice win32 functions |
|
|
35 | # undef open |
|
|
36 | # undef read |
|
|
37 | # undef write |
|
|
38 | # undef send |
|
|
39 | # undef recv |
|
|
40 | # undef stat |
|
|
41 | # undef fstat |
|
|
42 | # define lstat stat |
|
|
43 | # undef truncate |
|
|
44 | # undef ftruncate |
|
|
45 | # undef open |
|
|
46 | # undef close |
|
|
47 | # undef unlink |
|
|
48 | # undef rmdir |
|
|
49 | # undef rename |
|
|
50 | # undef lseek |
|
|
51 | |
|
|
52 | # define chown(a,b,c) (errno = ENOSYS, -1) |
|
|
53 | # define fchown(a,b,c) (errno = ENOSYS, -1) |
|
|
54 | # define fchmod(a,b) (errno = ENOSYS, -1) |
|
|
55 | # define symlink(a,b) (errno = ENOSYS, -1) |
|
|
56 | # define readlink(a,b,c) (errno = ENOSYS, -1) |
|
|
57 | # define mknod(a,b,c) (errno = ENOSYS, -1) |
|
|
58 | # define truncate(a,b) (errno = ENOSYS, -1) |
|
|
59 | # define ftruncate(fd,o) chsize ((fd), (o)) |
|
|
60 | # define fsync(fd) _commit (fd) |
|
|
61 | # define opendir(fd) (errno = ENOSYS, 0) |
|
|
62 | # define readdir(fd) (errno = ENOSYS, -1) |
|
|
63 | # define closedir(fd) (errno = ENOSYS, -1) |
|
|
64 | # define mkdir(a,b) mkdir (a) |
|
|
65 | |
|
|
66 | #else |
|
|
67 | |
|
|
68 | # include <sys/time.h> |
|
|
69 | # include <sys/select.h> |
|
|
70 | # include <unistd.h> |
|
|
71 | # include <utime.h> |
|
|
72 | # include <signal.h> |
|
|
73 | # define EIO_STRUCT_DIRENT struct dirent |
|
|
74 | |
|
|
75 | #endif |
|
|
76 | |
26 | |
77 | /* perl stupidly overrides readdir and maybe others */ |
27 | /* perl stupidly overrides readdir and maybe others */ |
78 | /* with thread-unsafe versions, imagine that :( */ |
28 | /* with thread-unsafe versions, imagine that :( */ |
79 | #undef readdir |
29 | #undef readdir |
80 | #undef opendir |
30 | #undef opendir |
81 | #undef closedir |
31 | #undef closedir |
|
|
32 | |
|
|
33 | #ifdef _WIN32 |
|
|
34 | |
|
|
35 | // perl overrides all those nice libc functions |
|
|
36 | |
|
|
37 | #undef malloc |
|
|
38 | #undef free |
|
|
39 | #undef open |
|
|
40 | #undef read |
|
|
41 | #undef write |
|
|
42 | #undef send |
|
|
43 | #undef recv |
|
|
44 | #undef stat |
|
|
45 | #undef lstat |
|
|
46 | #undef fstat |
|
|
47 | #undef truncate |
|
|
48 | #undef ftruncate |
|
|
49 | #undef open |
|
|
50 | #undef link |
|
|
51 | #undef close |
|
|
52 | #undef unlink |
|
|
53 | #undef mkdir |
|
|
54 | #undef rmdir |
|
|
55 | #undef rename |
|
|
56 | #undef lseek |
|
|
57 | #undef opendir |
|
|
58 | #undef readdir |
|
|
59 | #undef closedir |
|
|
60 | #undef chmod |
|
|
61 | #undef fchmod |
|
|
62 | #undef dup |
|
|
63 | #undef dup2 |
|
|
64 | #undef abort |
|
|
65 | #undef pipe |
|
|
66 | |
|
|
67 | #else |
|
|
68 | |
|
|
69 | #include <sys/time.h> |
|
|
70 | #include <sys/select.h> |
|
|
71 | #include <unistd.h> |
|
|
72 | #include <utime.h> |
|
|
73 | #include <signal.h> |
|
|
74 | |
|
|
75 | #endif |
82 | |
76 | |
83 | #define EIO_STRUCT_STAT Stat_t |
77 | #define EIO_STRUCT_STAT Stat_t |
84 | |
78 | |
85 | /* use NV for 32 bit perls as it allows larger offsets */ |
79 | /* use NV for 32 bit perls as it allows larger offsets */ |
86 | #if IVSIZE >= 8 |
80 | #if IVSIZE >= 8 |
… | |
… | |
121 | #define EIO_NO_WRAPPERS 1 |
115 | #define EIO_NO_WRAPPERS 1 |
122 | |
116 | |
123 | #include "libeio/config.h" |
117 | #include "libeio/config.h" |
124 | #include "libeio/eio.h" |
118 | #include "libeio/eio.h" |
125 | |
119 | |
|
|
120 | static int req_invoke (eio_req *req); |
|
|
121 | #define EIO_FINISH(req) req_invoke (req) |
|
|
122 | static void req_destroy (eio_req *grp); |
|
|
123 | #define EIO_DESTROY(req) req_destroy (req) |
|
|
124 | |
|
|
125 | #include "libeio/eio.c" |
|
|
126 | |
126 | /* Linux/others */ |
127 | /* Linux/others */ |
127 | #ifndef O_ASYNC |
128 | #ifndef O_ASYNC |
128 | # define O_ASYNC 0 |
129 | # define O_ASYNC 0 |
129 | #endif |
130 | #endif |
130 | #ifndef O_DIRECT |
131 | #ifndef O_DIRECT |
… | |
… | |
223 | #endif |
224 | #endif |
224 | #ifndef PROT_EXEC |
225 | #ifndef PROT_EXEC |
225 | # define PROT_EXEC 0 |
226 | # define PROT_EXEC 0 |
226 | #endif |
227 | #endif |
227 | |
228 | |
|
|
229 | #ifndef ST_RDONLY |
|
|
230 | # define ST_RDONLY 0 |
|
|
231 | #endif |
|
|
232 | #ifndef ST_NOSUID |
|
|
233 | # define ST_NOSUID 0 |
|
|
234 | #endif |
228 | #ifndef ST_NODEV |
235 | #ifndef ST_NODEV |
229 | # define ST_NODEV 0 |
236 | # define ST_NODEV 0 |
230 | #endif |
237 | #endif |
231 | #ifndef ST_NOEXEC |
238 | #ifndef ST_NOEXEC |
232 | # define ST_NOEXEC 0 |
239 | # define ST_NOEXEC 0 |
… | |
… | |
316 | |
323 | |
317 | #ifndef PAGESIZE |
324 | #ifndef PAGESIZE |
318 | # define PAGESIZE sysconf (_SC_PAGESIZE) |
325 | # define PAGESIZE sysconf (_SC_PAGESIZE) |
319 | #endif |
326 | #endif |
320 | |
327 | |
321 | static int req_invoke (eio_req *req); |
|
|
322 | #define EIO_FINISH(req) req_invoke (req) |
|
|
323 | static void req_destroy (eio_req *grp); |
|
|
324 | #define EIO_DESTROY(req) req_destroy (req) |
|
|
325 | |
|
|
326 | enum { |
328 | enum { |
327 | FLAG_SV2_RO_OFF = 0x40, /* data was set readonly */ |
329 | FLAG_SV2_RO_OFF = 0x40, /* data was set readonly */ |
328 | }; |
330 | }; |
329 | |
|
|
330 | #include "libeio/eio.c" |
|
|
331 | |
331 | |
332 | typedef eio_req *aio_req; |
332 | typedef eio_req *aio_req; |
333 | typedef eio_req *aio_req_ornot; |
333 | typedef eio_req *aio_req_ornot; |
334 | |
334 | |
335 | static SV *on_next_submit; |
335 | static SV *on_next_submit; |
… | |
… | |
541 | case EIO_STATVFS: |
541 | case EIO_STATVFS: |
542 | case EIO_FSTATVFS: |
542 | case EIO_FSTATVFS: |
543 | { |
543 | { |
544 | SV *rv = &PL_sv_undef; |
544 | SV *rv = &PL_sv_undef; |
545 | |
545 | |
|
|
546 | #ifndef _WIN32 |
546 | if (req->result >= 0) |
547 | if (req->result >= 0) |
547 | { |
548 | { |
548 | EIO_STRUCT_STATVFS *f = EIO_STATVFS_BUF (req); |
549 | EIO_STRUCT_STATVFS *f = EIO_STATVFS_BUF (req); |
549 | HV *hv = newHV (); |
550 | HV *hv = newHV (); |
550 | |
551 | |
… | |
… | |
560 | hv_store (hv, "favail" , sizeof ("favail" ) - 1, newSVval64 (f->f_favail ), 0); |
561 | hv_store (hv, "favail" , sizeof ("favail" ) - 1, newSVval64 (f->f_favail ), 0); |
561 | hv_store (hv, "fsid" , sizeof ("fsid" ) - 1, newSVval64 (f->f_fsid ), 0); |
562 | hv_store (hv, "fsid" , sizeof ("fsid" ) - 1, newSVval64 (f->f_fsid ), 0); |
562 | hv_store (hv, "flag" , sizeof ("flag" ) - 1, newSVval64 (f->f_flag ), 0); |
563 | hv_store (hv, "flag" , sizeof ("flag" ) - 1, newSVval64 (f->f_flag ), 0); |
563 | hv_store (hv, "namemax", sizeof ("namemax") - 1, newSVval64 (f->f_namemax), 0); |
564 | hv_store (hv, "namemax", sizeof ("namemax") - 1, newSVval64 (f->f_namemax), 0); |
564 | } |
565 | } |
|
|
566 | #endif |
565 | |
567 | |
566 | PUSHs (rv); |
568 | PUSHs (rv); |
567 | } |
569 | } |
568 | |
570 | |
569 | break; |
571 | break; |
… | |
… | |
595 | case EIO_STAT: |
597 | case EIO_STAT: |
596 | case EIO_LSTAT: |
598 | case EIO_LSTAT: |
597 | case EIO_FSTAT: |
599 | case EIO_FSTAT: |
598 | PL_laststype = req->type == EIO_LSTAT ? OP_LSTAT : OP_STAT; |
600 | PL_laststype = req->type == EIO_LSTAT ? OP_LSTAT : OP_STAT; |
599 | PL_laststatval = req->result; |
601 | PL_laststatval = req->result; |
|
|
602 | /* if compilation fails here then perl's Stat_t is not struct _stati64 */ |
600 | PL_statcache = *(EIO_STRUCT_STAT *)(req->ptr2); |
603 | PL_statcache = *(EIO_STRUCT_STAT *)(req->ptr2); |
601 | PUSHs (sv_result); |
604 | PUSHs (sv_result); |
602 | break; |
605 | break; |
603 | |
606 | |
604 | case EIO_READ: |
607 | case EIO_READ: |
… | |
… | |
656 | Safefree (req); |
659 | Safefree (req); |
657 | } |
660 | } |
658 | |
661 | |
659 | static void req_cancel_subs (aio_req grp) |
662 | static void req_cancel_subs (aio_req grp) |
660 | { |
663 | { |
661 | aio_req sub; |
|
|
662 | |
|
|
663 | if (grp->type != EIO_GROUP) |
664 | if (grp->type != EIO_GROUP) |
664 | return; |
665 | return; |
665 | |
666 | |
666 | SvREFCNT_dec (grp->sv2); |
667 | SvREFCNT_dec (grp->sv2); |
667 | grp->sv2 = 0; |
668 | grp->sv2 = 0; |
668 | |
669 | |
669 | eio_grp_cancel (grp); |
670 | eio_grp_cancel (grp); |
670 | } |
671 | } |
671 | |
672 | |
672 | static void |
|
|
673 | create_respipe (void) |
673 | static void create_respipe (void) |
674 | { |
674 | { |
675 | if (s_epipe_renew (&respipe)) |
675 | if (s_epipe_renew (&respipe)) |
676 | croak ("IO::AIO: unable to initialize result pipe"); |
676 | croak ("IO::AIO: unable to initialize result pipe"); |
677 | } |
677 | } |
678 | |
678 | |
… | |
… | |
709 | |
709 | |
710 | poll_wait (); |
710 | poll_wait (); |
711 | } |
711 | } |
712 | } |
712 | } |
713 | |
713 | |
714 | static void atfork_child (void) |
714 | static void ecb_cold |
|
|
715 | reinit (void) |
715 | { |
716 | { |
716 | create_respipe (); |
717 | create_respipe (); |
|
|
718 | |
|
|
719 | if (eio_init (want_poll, done_poll) < 0) |
|
|
720 | croak ("IO::AIO: unable to initialise eio library"); |
717 | } |
721 | } |
718 | |
722 | |
719 | /*****************************************************************************/ |
723 | /*****************************************************************************/ |
720 | |
724 | |
721 | #if !_POSIX_MAPPED_FILES |
725 | #if !_POSIX_MAPPED_FILES |
… | |
… | |
732 | # define MAP_FIXED 0 |
736 | # define MAP_FIXED 0 |
733 | #endif |
737 | #endif |
734 | |
738 | |
735 | #define MMAP_MAGIC PERL_MAGIC_ext |
739 | #define MMAP_MAGIC PERL_MAGIC_ext |
736 | |
740 | |
737 | static int |
|
|
738 | mmap_free (pTHX_ SV *sv, MAGIC *mg) |
741 | static int mmap_free (pTHX_ SV *sv, MAGIC *mg) |
739 | { |
742 | { |
740 | int old_errno = errno; |
743 | int old_errno = errno; |
741 | munmap (mg->mg_ptr, (size_t)mg->mg_obj); |
744 | munmap (mg->mg_ptr, (size_t)mg->mg_obj); |
742 | errno = old_errno; |
745 | errno = old_errno; |
743 | |
746 | |
… | |
… | |
759 | 0, 0, 0, 0, mmap_free |
762 | 0, 0, 0, 0, mmap_free |
760 | }; |
763 | }; |
761 | |
764 | |
762 | /*****************************************************************************/ |
765 | /*****************************************************************************/ |
763 | |
766 | |
764 | static SV * |
|
|
765 | get_cb (SV *cb_sv) |
767 | static SV * get_cb (SV *cb_sv) |
766 | { |
768 | { |
767 | SvGETMAGIC (cb_sv); |
769 | SvGETMAGIC (cb_sv); |
768 | return SvOK (cb_sv) ? s_get_cv_croak (cb_sv) : 0; |
770 | return SvOK (cb_sv) ? s_get_cv_croak (cb_sv) : 0; |
769 | } |
771 | } |
770 | |
772 | |
… | |
… | |
894 | |
896 | |
895 | const_eio (SYNC_FILE_RANGE_WAIT_BEFORE) |
897 | const_eio (SYNC_FILE_RANGE_WAIT_BEFORE) |
896 | const_eio (SYNC_FILE_RANGE_WRITE) |
898 | const_eio (SYNC_FILE_RANGE_WRITE) |
897 | const_eio (SYNC_FILE_RANGE_WAIT_AFTER) |
899 | const_eio (SYNC_FILE_RANGE_WAIT_AFTER) |
898 | |
900 | |
|
|
901 | const_eio (FALLOC_FL_KEEP_SIZE) |
|
|
902 | |
899 | const_eio (READDIR_DENTS) |
903 | const_eio (READDIR_DENTS) |
900 | const_eio (READDIR_DIRS_FIRST) |
904 | const_eio (READDIR_DIRS_FIRST) |
901 | const_eio (READDIR_STAT_ORDER) |
905 | const_eio (READDIR_STAT_ORDER) |
902 | const_eio (READDIR_FOUND_UNKNOWN) |
906 | const_eio (READDIR_FOUND_UNKNOWN) |
903 | |
907 | |
… | |
… | |
919 | for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ-- > const_iv; ) |
923 | for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ-- > const_iv; ) |
920 | newCONSTSUB (aio_stash, (char *)civ->name, newSViv (civ->iv)); |
924 | newCONSTSUB (aio_stash, (char *)civ->name, newSViv (civ->iv)); |
921 | |
925 | |
922 | newCONSTSUB (aio_stash, "PAGESIZE", newSViv (PAGESIZE)); |
926 | newCONSTSUB (aio_stash, "PAGESIZE", newSViv (PAGESIZE)); |
923 | |
927 | |
924 | create_respipe (); |
928 | reinit (); |
925 | |
|
|
926 | if (eio_init (want_poll, done_poll) < 0) |
|
|
927 | croak ("IO::AIO: unable to initialise eio library"); |
|
|
928 | |
|
|
929 | /* atfork child called in fifo order, so before eio's handler */ |
|
|
930 | X_THREAD_ATFORK (0, 0, atfork_child); |
|
|
931 | } |
929 | } |
|
|
930 | |
|
|
931 | void |
|
|
932 | reinit () |
|
|
933 | PROTOTYPE: |
|
|
934 | CODE: |
|
|
935 | reinit (); |
932 | |
936 | |
933 | void |
937 | void |
934 | max_poll_reqs (unsigned int nreqs) |
938 | max_poll_reqs (unsigned int nreqs) |
935 | PROTOTYPE: $ |
939 | PROTOTYPE: $ |
936 | CODE: |
940 | CODE: |
… | |
… | |
1020 | |
1024 | |
1021 | REQ_SEND (req); |
1025 | REQ_SEND (req); |
1022 | } |
1026 | } |
1023 | |
1027 | |
1024 | void |
1028 | void |
|
|
1029 | aio_fallocate (SV *fh, int mode, off_t offset, size_t len, SV *callback=&PL_sv_undef) |
|
|
1030 | PPCODE: |
|
|
1031 | { |
|
|
1032 | int fd = s_fileno_croak (fh, 0); |
|
|
1033 | dREQ; |
|
|
1034 | |
|
|
1035 | req->type = EIO_FALLOCATE; |
|
|
1036 | req->sv1 = newSVsv (fh); |
|
|
1037 | req->int1 = fd; |
|
|
1038 | req->int2 = mode; |
|
|
1039 | req->offs = offset; |
|
|
1040 | req->size = len; |
|
|
1041 | |
|
|
1042 | REQ_SEND (req); |
|
|
1043 | } |
|
|
1044 | |
|
|
1045 | void |
1025 | aio_close (SV *fh, SV *callback=&PL_sv_undef) |
1046 | aio_close (SV *fh, SV *callback=&PL_sv_undef) |
1026 | PPCODE: |
1047 | PPCODE: |
1027 | { |
1048 | { |
1028 | static int close_pipe = -1; /* dummy fd to close fds via dup2 */ |
1049 | static int close_fd = -1; /* dummy fd to close fds via dup2 */ |
1029 | int fd = s_fileno_croak (fh, 0); |
1050 | int fd = s_fileno_croak (fh, 0); |
1030 | dREQ; |
1051 | dREQ; |
1031 | |
1052 | |
1032 | if (close_pipe < 0) |
1053 | if (expect_false (close_fd < 0)) |
1033 | { |
1054 | { |
1034 | int pipefd [2]; |
1055 | int pipefd [2]; |
1035 | |
1056 | |
|
|
1057 | if ( |
|
|
1058 | #ifdef _WIN32 |
|
|
1059 | _pipe (pipefd, 1, _O_BINARY) < 0 |
|
|
1060 | #else |
1036 | if (pipe (pipefd) < 0 |
1061 | pipe (pipefd) < 0 |
1037 | || close (pipefd [1]) < 0 |
|
|
1038 | || fcntl (pipefd [0], F_SETFD, FD_CLOEXEC) < 0) |
1062 | || fcntl (pipefd [0], F_SETFD, FD_CLOEXEC) < 0 |
|
|
1063 | #endif |
|
|
1064 | || close (pipefd [1]) < 0 |
|
|
1065 | ) |
1039 | abort (); /*D*/ |
1066 | abort (); /*D*/ |
1040 | |
1067 | |
1041 | close_pipe = pipefd [0]; |
1068 | close_fd = pipefd [0]; |
1042 | } |
1069 | } |
1043 | |
1070 | |
1044 | req->type = EIO_DUP2; |
1071 | req->type = EIO_DUP2; |
1045 | req->int1 = close_pipe; |
1072 | req->int1 = close_fd; |
1046 | req->sv2 = newSVsv (fh); |
1073 | req->sv2 = newSVsv (fh); |
1047 | req->int2 = fd; |
1074 | req->int2 = fd; |
1048 | |
1075 | |
1049 | REQ_SEND (req); |
1076 | REQ_SEND (req); |
1050 | } |
1077 | } |
… | |
… | |
1107 | ALIAS: |
1134 | ALIAS: |
1108 | aio_readlink = EIO_READLINK |
1135 | aio_readlink = EIO_READLINK |
1109 | aio_realpath = EIO_REALPATH |
1136 | aio_realpath = EIO_REALPATH |
1110 | PPCODE: |
1137 | PPCODE: |
1111 | { |
1138 | { |
1112 | SV *data; |
|
|
1113 | dREQ; |
1139 | dREQ; |
1114 | |
1140 | |
1115 | req->type = ix; |
1141 | req->type = ix; |
1116 | req->sv1 = newSVsv (path); |
1142 | req->sv1 = newSVsv (path); |
1117 | req->ptr1 = SvPVbyte_nolen (req->sv1); |
1143 | req->ptr1 = SvPVbyte_nolen (req->sv1); |
… | |
… | |
1564 | CODE: |
1590 | CODE: |
1565 | RETVAL = posix_fadvise (fh, offset, length, advice); |
1591 | RETVAL = posix_fadvise (fh, offset, length, advice); |
1566 | OUTPUT: |
1592 | OUTPUT: |
1567 | RETVAL |
1593 | RETVAL |
1568 | |
1594 | |
1569 | ssize_t |
1595 | IV |
1570 | sendfile (aio_wfd ofh, aio_rfd ifh, off_t offset, size_t count) |
1596 | sendfile (aio_wfd ofh, aio_rfd ifh, off_t offset, size_t count) |
1571 | CODE: |
1597 | CODE: |
1572 | RETVAL = eio_sendfile_sync (ofh, ifh, offset, count); |
1598 | RETVAL = eio_sendfile_sync (ofh, ifh, offset, count); |
1573 | OUTPUT: |
1599 | OUTPUT: |
1574 | RETVAL |
1600 | RETVAL |