… | |
… | |
4 | |
4 | |
5 | #include "EXTERN.h" |
5 | #include "EXTERN.h" |
6 | #include "perl.h" |
6 | #include "perl.h" |
7 | #include "XSUB.h" |
7 | #include "XSUB.h" |
8 | |
8 | |
9 | #include "schmorp.h" |
9 | #if !defined mg_findext |
|
|
10 | # define mg_findext(sv,type,vtbl) mg_find (sv, type) |
|
|
11 | #endif |
10 | |
12 | |
11 | #include <stddef.h> |
13 | #include <stddef.h> |
12 | #include <stdlib.h> |
14 | #include <stdlib.h> |
13 | #include <errno.h> |
15 | #include <errno.h> |
14 | #include <sys/types.h> |
16 | #include <sys/types.h> |
|
|
17 | #include <sys/socket.h> |
15 | #include <sys/stat.h> |
18 | #include <sys/stat.h> |
16 | #include <limits.h> |
19 | #include <limits.h> |
17 | #include <fcntl.h> |
20 | #include <fcntl.h> |
18 | #include <sched.h> |
21 | #include <sched.h> |
19 | |
22 | |
… | |
… | |
104 | #define expect_false(expr) expect ((expr) != 0, 0) |
107 | #define expect_false(expr) expect ((expr) != 0, 0) |
105 | #define expect_true(expr) expect ((expr) != 0, 1) |
108 | #define expect_true(expr) expect ((expr) != 0, 1) |
106 | |
109 | |
107 | /*****************************************************************************/ |
110 | /*****************************************************************************/ |
108 | |
111 | |
109 | #include "libeio/config.h" |
112 | #include "config.h" |
|
|
113 | |
|
|
114 | #if HAVE_ST_XTIMENSEC |
|
|
115 | # define ATIMENSEC PL_statcache.st_atimensec |
|
|
116 | # define MTIMENSEC PL_statcache.st_mtimensec |
|
|
117 | # define CTIMENSEC PL_statcache.st_ctimensec |
|
|
118 | #elif HAVE_ST_XTIMESPEC |
|
|
119 | # define ATIMENSEC PL_statcache.st_atim.tv_nsec |
|
|
120 | # define MTIMENSEC PL_statcache.st_mtim.tv_nsec |
|
|
121 | # define CTIMENSEC PL_statcache.st_ctim.tv_nsec |
|
|
122 | #else |
|
|
123 | # define ATIMENSEC 0 |
|
|
124 | # define MTIMENSEC 0 |
|
|
125 | # define CTIMENSEC 0 |
|
|
126 | #endif |
|
|
127 | |
|
|
128 | #include "schmorp.h" |
|
|
129 | |
|
|
130 | #if HAVE_EVENTFD |
|
|
131 | # include <sys/eventfd.h> |
|
|
132 | #endif |
|
|
133 | |
|
|
134 | #if HAVE_TIMERFD |
|
|
135 | # include <sys/timerfd.h> |
|
|
136 | #endif |
110 | |
137 | |
111 | #if HAVE_RLIMITS |
138 | #if HAVE_RLIMITS |
112 | #include <sys/time.h> |
139 | #include <sys/time.h> |
113 | #include <sys/resource.h> |
140 | #include <sys/resource.h> |
114 | #endif |
141 | #endif |
… | |
… | |
151 | # else |
178 | # else |
152 | # define MAP_ANONYMOUS MAP_FIXED /* and hope this fails */ |
179 | # define MAP_ANONYMOUS MAP_FIXED /* and hope this fails */ |
153 | # endif |
180 | # endif |
154 | #endif |
181 | #endif |
155 | |
182 | |
156 | /* defines all sorts of constants to 0 unless they are already defined */ |
|
|
157 | /* also provides const_iv_ and const_niv_ macros for them */ |
|
|
158 | #include "def0.h" |
|
|
159 | |
|
|
160 | #ifndef makedev |
183 | #ifndef makedev |
161 | # define makedev(maj,min) (((maj) << 8) | (min)) |
184 | # define makedev(maj,min) (((maj) << 8) | (min)) |
162 | #endif |
185 | #endif |
163 | #ifndef major |
186 | #ifndef major |
164 | # define major(dev) ((dev) >> 8) |
187 | # define major(dev) ((dev) >> 8) |
… | |
… | |
172 | #endif |
195 | #endif |
173 | |
196 | |
174 | /*****************************************************************************/ |
197 | /*****************************************************************************/ |
175 | |
198 | |
176 | #if !_POSIX_MAPPED_FILES |
199 | #if !_POSIX_MAPPED_FILES |
177 | # define mmap(addr,length,prot,flags,fd,offs) EIO_ENOSYS () |
200 | # define mmap(addr,length,prot,flags,fd,offs) (errno = ENOSYS, (void *)-1) |
178 | # define munmap(addr,length) EIO_ENOSYS () |
201 | # define munmap(addr,length) EIO_ENOSYS () |
179 | #endif |
202 | #endif |
180 | |
203 | |
181 | #if !_POSIX_MEMORY_PROTECTION |
204 | #if !_POSIX_MEMORY_PROTECTION |
182 | # define mprotect(addr,len,prot) EIO_ENOSYS () |
205 | # define mprotect(addr,len,prot) EIO_ENOSYS () |
|
|
206 | #endif |
|
|
207 | |
|
|
208 | #if !MREMAP_MAYMOVE |
|
|
209 | # define mremap(old_address,old_size,new_size,flags,new_address) (errno = ENOSYS, (void *)-1) |
183 | #endif |
210 | #endif |
184 | |
211 | |
185 | #define FOREIGN_MAGIC PERL_MAGIC_ext |
212 | #define FOREIGN_MAGIC PERL_MAGIC_ext |
186 | |
213 | |
187 | static int ecb_cold |
214 | static int ecb_cold |
… | |
… | |
259 | { |
286 | { |
260 | /* todo: iterate over magic and only free ours, but of course */ |
287 | /* todo: iterate over magic and only free ours, but of course */ |
261 | /* the perl5porters will call that (correct) behaviour buggy */ |
288 | /* the perl5porters will call that (correct) behaviour buggy */ |
262 | sv_unmagic (sv, FOREIGN_MAGIC); |
289 | sv_unmagic (sv, FOREIGN_MAGIC); |
263 | } |
290 | } |
|
|
291 | |
|
|
292 | /*****************************************************************************/ |
|
|
293 | |
|
|
294 | /* defines all sorts of constants to 0 unless they are already defined */ |
|
|
295 | /* also provides const_iv_ and const_niv_ macros for them */ |
|
|
296 | #include "def0.h" |
264 | |
297 | |
265 | /*****************************************************************************/ |
298 | /*****************************************************************************/ |
266 | |
299 | |
267 | static void |
300 | static void |
268 | fiemap (eio_req *req) |
301 | fiemap (eio_req *req) |
… | |
… | |
699 | { |
732 | { |
700 | req->flags &= ~EIO_FLAG_PTR2_FREE; |
733 | req->flags &= ~EIO_FLAG_PTR2_FREE; |
701 | sv_set_foreign (req->sv2, &sysfree_vtbl, req->ptr2, req->result); |
734 | sv_set_foreign (req->sv2, &sysfree_vtbl, req->ptr2, req->result); |
702 | } |
735 | } |
703 | else |
736 | else |
|
|
737 | { |
704 | SvCUR_set (req->sv2, req->result); |
738 | SvCUR_set (req->sv2, req->result); |
|
|
739 | *SvEND (req->sv2) = 0; |
|
|
740 | SvPOK_only (req->sv2); |
|
|
741 | } |
|
|
742 | |
|
|
743 | SvSETMAGIC (req->sv2); |
705 | } |
744 | } |
706 | |
745 | |
707 | PUSHs (sv_result); |
746 | PUSHs (sv_result); |
708 | } |
747 | } |
709 | break; |
748 | break; |
… | |
… | |
963 | req_set_path1 (req, fh_or_path); |
1002 | req_set_path1 (req, fh_or_path); |
964 | break; |
1003 | break; |
965 | } |
1004 | } |
966 | } |
1005 | } |
967 | |
1006 | |
|
|
1007 | /*****************************************************************************/ |
|
|
1008 | |
|
|
1009 | static void |
|
|
1010 | ts_set (struct timespec *ts, NV value) |
|
|
1011 | { |
|
|
1012 | ts->tv_sec = value; |
|
|
1013 | ts->tv_nsec = (value - ts->tv_sec) * 1e9; |
|
|
1014 | } |
|
|
1015 | |
|
|
1016 | static NV |
|
|
1017 | ts_get (const struct timespec *ts) |
|
|
1018 | { |
|
|
1019 | return ts->tv_sec + ts->tv_nsec * 1e-9; |
|
|
1020 | } |
|
|
1021 | |
|
|
1022 | /*****************************************************************************/ |
|
|
1023 | |
968 | XS(boot_IO__AIO) ecb_cold; |
1024 | XS(boot_IO__AIO) ecb_cold; |
969 | |
1025 | |
970 | MODULE = IO::AIO PACKAGE = IO::AIO |
1026 | MODULE = IO::AIO PACKAGE = IO::AIO |
971 | |
1027 | |
972 | PROTOTYPES: ENABLE |
1028 | PROTOTYPES: ENABLE |
… | |
… | |
1003 | const_niv (MADV_RANDOM , POSIX_MADV_RANDOM) |
1059 | const_niv (MADV_RANDOM , POSIX_MADV_RANDOM) |
1004 | const_niv (MADV_WILLNEED , POSIX_MADV_WILLNEED) |
1060 | const_niv (MADV_WILLNEED , POSIX_MADV_WILLNEED) |
1005 | const_niv (MADV_DONTNEED , POSIX_MADV_DONTNEED) |
1061 | const_niv (MADV_DONTNEED , POSIX_MADV_DONTNEED) |
1006 | |
1062 | |
1007 | /* the second block will be 0 when missing */ |
1063 | /* the second block will be 0 when missing */ |
|
|
1064 | const_iv (O_ACCMODE) |
|
|
1065 | |
1008 | const_iv (O_RDONLY) |
1066 | const_iv (O_RDONLY) |
1009 | const_iv (O_WRONLY) |
1067 | const_iv (O_WRONLY) |
1010 | const_iv (O_RDWR) |
1068 | const_iv (O_RDWR) |
1011 | const_iv (O_CREAT) |
1069 | const_iv (O_CREAT) |
1012 | const_iv (O_TRUNC) |
1070 | const_iv (O_TRUNC) |
… | |
… | |
1072 | const_iv (MAP_GROWSDOWN) |
1130 | const_iv (MAP_GROWSDOWN) |
1073 | const_iv (MAP_32BIT) |
1131 | const_iv (MAP_32BIT) |
1074 | const_iv (MAP_HUGETLB) |
1132 | const_iv (MAP_HUGETLB) |
1075 | const_iv (MAP_STACK) |
1133 | const_iv (MAP_STACK) |
1076 | |
1134 | |
|
|
1135 | const_iv (MREMAP_MAYMOVE) |
|
|
1136 | const_iv (MREMAP_FIXED) |
|
|
1137 | |
1077 | const_iv (F_DUPFD_CLOEXEC) |
1138 | const_iv (F_DUPFD_CLOEXEC) |
|
|
1139 | |
|
|
1140 | const_iv (MSG_CMSG_CLOEXEC) |
|
|
1141 | const_iv (SOCK_CLOEXEC) |
1078 | |
1142 | |
1079 | const_iv (F_OFD_GETLK) |
1143 | const_iv (F_OFD_GETLK) |
1080 | const_iv (F_OFD_SETLK) |
1144 | const_iv (F_OFD_SETLK) |
1081 | const_iv (F_OFD_GETLKW) |
1145 | const_iv (F_OFD_GETLKW) |
1082 | |
1146 | |
… | |
… | |
1155 | const_iv (SPLICE_F_MOVE) |
1219 | const_iv (SPLICE_F_MOVE) |
1156 | const_iv (SPLICE_F_NONBLOCK) |
1220 | const_iv (SPLICE_F_NONBLOCK) |
1157 | const_iv (SPLICE_F_MORE) |
1221 | const_iv (SPLICE_F_MORE) |
1158 | const_iv (SPLICE_F_GIFT) |
1222 | const_iv (SPLICE_F_GIFT) |
1159 | |
1223 | |
|
|
1224 | const_iv (EFD_CLOEXEC) |
|
|
1225 | const_iv (EFD_NONBLOCK) |
|
|
1226 | const_iv (EFD_SEMAPHORE) |
|
|
1227 | |
|
|
1228 | const_iv (CLOCK_REALTIME) |
|
|
1229 | const_iv (CLOCK_MONOTONIC) |
|
|
1230 | const_iv (CLOCK_BOOTTIME) |
|
|
1231 | const_iv (CLOCK_REALTIME_ALARM) |
|
|
1232 | const_iv (CLOCK_BOOTTIME_ALARM) |
|
|
1233 | |
|
|
1234 | const_iv (TFD_NONBLOCK) |
|
|
1235 | const_iv (TFD_CLOEXEC) |
|
|
1236 | |
|
|
1237 | const_iv (TFD_TIMER_ABSTIME) |
|
|
1238 | const_iv (TFD_TIMER_CANCEL_ON_SET) |
|
|
1239 | |
1160 | /* these are libeio constants, and are independent of gendef0 */ |
1240 | /* these are libeio constants, and are independent of gendef0 */ |
1161 | const_eio (SEEK_SET) |
1241 | const_eio (SEEK_SET) |
1162 | const_eio (SEEK_CUR) |
1242 | const_eio (SEEK_CUR) |
1163 | const_eio (SEEK_END) |
1243 | const_eio (SEEK_END) |
1164 | |
1244 | |
… | |
… | |
1415 | } |
1495 | } |
1416 | else |
1496 | else |
1417 | { |
1497 | { |
1418 | /* read: check type and grow scalar as necessary */ |
1498 | /* read: check type and grow scalar as necessary */ |
1419 | if (!SvPOK (data) || SvLEN (data) >= SvCUR (data)) |
1499 | if (!SvPOK (data) || SvLEN (data) >= SvCUR (data)) |
1420 | svptr = sv_grow (data, len + dataoffset); |
1500 | svptr = sv_grow (data, len + dataoffset + 1); |
1421 | else if (SvCUR (data) < len + dataoffset) |
1501 | else if (SvCUR (data) < len + dataoffset) |
1422 | croak ("length + dataoffset outside of scalar, and cannot grow"); |
1502 | croak ("length + dataoffset outside of scalar, and cannot grow"); |
1423 | } |
1503 | } |
1424 | |
1504 | |
1425 | { |
1505 | { |
… | |
… | |
1546 | |
1626 | |
1547 | req_set_fh_or_path (req, ix, ix == EIO_STATVFS ? EIO_FSTATVFS : EIO_FSTAT, fh_or_path); |
1627 | req_set_fh_or_path (req, ix, ix == EIO_STATVFS ? EIO_FSTATVFS : EIO_FSTAT, fh_or_path); |
1548 | |
1628 | |
1549 | REQ_SEND; |
1629 | REQ_SEND; |
1550 | } |
1630 | } |
|
|
1631 | |
|
|
1632 | void |
|
|
1633 | st_xtime () |
|
|
1634 | ALIAS: |
|
|
1635 | st_atime = 1 |
|
|
1636 | st_mtime = 2 |
|
|
1637 | st_ctime = 4 |
|
|
1638 | st_xtime = 7 |
|
|
1639 | PPCODE: |
|
|
1640 | EXTEND (SP, 3); |
|
|
1641 | if (ix & 1) PUSHs (newSVnv (PL_statcache.st_atime + 1e-9 * ATIMENSEC)); |
|
|
1642 | if (ix & 2) PUSHs (newSVnv (PL_statcache.st_mtime + 1e-9 * MTIMENSEC)); |
|
|
1643 | if (ix & 4) PUSHs (newSVnv (PL_statcache.st_ctime + 1e-9 * CTIMENSEC)); |
|
|
1644 | |
|
|
1645 | void |
|
|
1646 | st_xtimensec () |
|
|
1647 | ALIAS: |
|
|
1648 | st_atimensec = 1 |
|
|
1649 | st_mtimensec = 2 |
|
|
1650 | st_ctimensec = 4 |
|
|
1651 | st_xtimensec = 7 |
|
|
1652 | PPCODE: |
|
|
1653 | EXTEND (SP, 3); |
|
|
1654 | if (ix & 1) PUSHs (newSViv (ATIMENSEC)); |
|
|
1655 | if (ix & 2) PUSHs (newSViv (MTIMENSEC)); |
|
|
1656 | if (ix & 4) PUSHs (newSViv (CTIMENSEC)); |
1551 | |
1657 | |
1552 | UV |
1658 | UV |
1553 | major (UV dev) |
1659 | major (UV dev) |
1554 | ALIAS: |
1660 | ALIAS: |
1555 | minor = 1 |
1661 | minor = 1 |
… | |
… | |
1819 | sv_clear_foreign (data); |
1925 | sv_clear_foreign (data); |
1820 | |
1926 | |
1821 | if (length) /* known length, directly read into scalar */ |
1927 | if (length) /* known length, directly read into scalar */ |
1822 | { |
1928 | { |
1823 | if (!SvPOK (data) || SvLEN (data) >= SvCUR (data)) |
1929 | if (!SvPOK (data) || SvLEN (data) >= SvCUR (data)) |
1824 | svptr = sv_grow (data, length); |
1930 | svptr = sv_grow (data, length + 1); |
1825 | else if (SvCUR (data) < length) |
1931 | else if (SvCUR (data) < length) |
1826 | croak ("length outside of scalar, and cannot grow"); |
1932 | croak ("length outside of scalar, and cannot grow"); |
1827 | else |
1933 | else |
1828 | svptr = SvPVbyte_nolen (data); |
1934 | svptr = SvPVbyte_nolen (data); |
1829 | } |
1935 | } |
… | |
… | |
2010 | void |
2116 | void |
2011 | munmap (SV *scalar) |
2117 | munmap (SV *scalar) |
2012 | CODE: |
2118 | CODE: |
2013 | sv_clear_foreign (scalar); |
2119 | sv_clear_foreign (scalar); |
2014 | |
2120 | |
|
|
2121 | SV * |
|
|
2122 | mremap (SV *scalar, STRLEN new_length, int flags = MREMAP_MAYMOVE, IV new_address = 0) |
|
|
2123 | CODE: |
|
|
2124 | { |
|
|
2125 | MAGIC *mg = mg_findext (scalar, FOREIGN_MAGIC, &mmap_vtbl); |
|
|
2126 | void *new; |
|
|
2127 | |
|
|
2128 | if (!mg || SvPVX (scalar) != mg->mg_ptr) |
|
|
2129 | croak ("IO::AIO::mremap: scalar not mapped by IO::AIO::mmap or improperly modified"); |
|
|
2130 | |
|
|
2131 | new = mremap (mg->mg_ptr, (size_t)mg->mg_obj, new_length, flags, (void *)new_address); |
|
|
2132 | |
|
|
2133 | RETVAL = &PL_sv_no; |
|
|
2134 | |
|
|
2135 | if (new != (void *)-1) |
|
|
2136 | { |
|
|
2137 | RETVAL = new == (void *)mg->mg_ptr |
|
|
2138 | ? newSVpvn ("0 but true", 10) |
|
|
2139 | : &PL_sv_yes; |
|
|
2140 | |
|
|
2141 | mg->mg_ptr = (char *)new; |
|
|
2142 | mg->mg_obj = (SV *)new_length; |
|
|
2143 | |
|
|
2144 | SvPVX (scalar) = mg->mg_ptr; |
|
|
2145 | SvCUR_set (scalar, new_length); |
|
|
2146 | } |
|
|
2147 | } |
|
|
2148 | OUTPUT: |
|
|
2149 | RETVAL |
|
|
2150 | |
2015 | int |
2151 | int |
2016 | madvise (SV *scalar, STRLEN offset = 0, SV *length = &PL_sv_undef, IV advice_or_prot) |
2152 | madvise (SV *scalar, STRLEN offset = 0, SV *length = &PL_sv_undef, IV advice_or_prot) |
2017 | ALIAS: |
2153 | ALIAS: |
2018 | mprotect = 1 |
2154 | mprotect = 1 |
2019 | CODE: |
2155 | CODE: |
… | |
… | |
2148 | { |
2284 | { |
2149 | EXTEND (SP, 2); |
2285 | EXTEND (SP, 2); |
2150 | PUSHs (newmortalFH (fd[0], O_RDONLY)); |
2286 | PUSHs (newmortalFH (fd[0], O_RDONLY)); |
2151 | PUSHs (newmortalFH (fd[1], O_WRONLY)); |
2287 | PUSHs (newmortalFH (fd[1], O_WRONLY)); |
2152 | } |
2288 | } |
|
|
2289 | } |
|
|
2290 | |
|
|
2291 | void |
|
|
2292 | eventfd (unsigned int initval = 0, int flags = 0) |
|
|
2293 | PPCODE: |
|
|
2294 | { |
|
|
2295 | int fd; |
|
|
2296 | #if HAVE_EVENTFD |
|
|
2297 | fd = eventfd (initval, flags); |
|
|
2298 | #else |
|
|
2299 | fd = (errno = ENOSYS, -1); |
|
|
2300 | #endif |
|
|
2301 | |
|
|
2302 | XPUSHs (newmortalFH (fd, O_RDWR)); |
|
|
2303 | } |
|
|
2304 | |
|
|
2305 | void |
|
|
2306 | timerfd_create (int clockid, int flags = 0) |
|
|
2307 | PPCODE: |
|
|
2308 | { |
|
|
2309 | int fd; |
|
|
2310 | #if HAVE_TIMERFD |
|
|
2311 | fd = timerfd_create (clockid, flags); |
|
|
2312 | #else |
|
|
2313 | fd = (errno = ENOSYS, -1); |
|
|
2314 | #endif |
|
|
2315 | |
|
|
2316 | XPUSHs (newmortalFH (fd, O_RDWR)); |
|
|
2317 | } |
|
|
2318 | |
|
|
2319 | void |
|
|
2320 | timerfd_settime (SV *fh, int flags, NV interval, NV value) |
|
|
2321 | PPCODE: |
|
|
2322 | { |
|
|
2323 | int fd = s_fileno_croak (fh, 0); |
|
|
2324 | #if HAVE_TIMERFD |
|
|
2325 | int res; |
|
|
2326 | struct itimerspec its, ots; |
|
|
2327 | |
|
|
2328 | ts_set (&its.it_interval, interval); |
|
|
2329 | ts_set (&its.it_value , value); |
|
|
2330 | res = timerfd_settime (fd, flags, &its, &ots); |
|
|
2331 | |
|
|
2332 | if (!res) |
|
|
2333 | { |
|
|
2334 | EXTEND (SP, 2); |
|
|
2335 | PUSHs (newSVnv (ts_get (&ots.it_interval))); |
|
|
2336 | PUSHs (newSVnv (ts_get (&ots.it_value))); |
|
|
2337 | } |
|
|
2338 | #else |
|
|
2339 | errno = ENOSYS; |
|
|
2340 | #endif |
|
|
2341 | } |
|
|
2342 | |
|
|
2343 | void |
|
|
2344 | timerfd_gettime (SV *fh) |
|
|
2345 | PPCODE: |
|
|
2346 | { |
|
|
2347 | int fd = s_fileno_croak (fh, 0); |
|
|
2348 | #if HAVE_TIMERFD |
|
|
2349 | int res; |
|
|
2350 | struct itimerspec ots; |
|
|
2351 | res = timerfd_gettime (fd, &ots); |
|
|
2352 | |
|
|
2353 | if (!res) |
|
|
2354 | { |
|
|
2355 | EXTEND (SP, 2); |
|
|
2356 | PUSHs (newSVnv (ts_get (&ots.it_interval))); |
|
|
2357 | PUSHs (newSVnv (ts_get (&ots.it_value))); |
|
|
2358 | } |
|
|
2359 | #else |
|
|
2360 | errno = ENOSYS; |
|
|
2361 | #endif |
2153 | } |
2362 | } |
2154 | |
2363 | |
2155 | UV |
2364 | UV |
2156 | get_fdlimit () |
2365 | get_fdlimit () |
2157 | CODE: |
2366 | CODE: |