… | |
… | |
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 | |
… | |
… | |
106 | |
109 | |
107 | /*****************************************************************************/ |
110 | /*****************************************************************************/ |
108 | |
111 | |
109 | #include "libeio/config.h" |
112 | #include "libeio/config.h" |
110 | |
113 | |
|
|
114 | #include "schmorp.h" |
|
|
115 | |
|
|
116 | #if HAVE_EVENTFD |
|
|
117 | # include <sys/eventfd.h> |
|
|
118 | #endif |
|
|
119 | |
|
|
120 | #if HAVE_TIMERFD |
|
|
121 | # include <sys/timerfd.h> |
|
|
122 | #endif |
|
|
123 | |
111 | #if HAVE_RLIMITS |
124 | #if HAVE_RLIMITS |
112 | #include <sys/time.h> |
125 | #include <sys/time.h> |
113 | #include <sys/resource.h> |
126 | #include <sys/resource.h> |
114 | #endif |
127 | #endif |
115 | |
128 | |
… | |
… | |
151 | # else |
164 | # else |
152 | # define MAP_ANONYMOUS MAP_FIXED /* and hope this fails */ |
165 | # define MAP_ANONYMOUS MAP_FIXED /* and hope this fails */ |
153 | # endif |
166 | # endif |
154 | #endif |
167 | #endif |
155 | |
168 | |
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 |
169 | #ifndef makedev |
161 | # define makedev(maj,min) (((maj) << 8) | (min)) |
170 | # define makedev(maj,min) (((maj) << 8) | (min)) |
162 | #endif |
171 | #endif |
163 | #ifndef major |
172 | #ifndef major |
164 | # define major(dev) ((dev) >> 8) |
173 | # define major(dev) ((dev) >> 8) |
… | |
… | |
172 | #endif |
181 | #endif |
173 | |
182 | |
174 | /*****************************************************************************/ |
183 | /*****************************************************************************/ |
175 | |
184 | |
176 | #if !_POSIX_MAPPED_FILES |
185 | #if !_POSIX_MAPPED_FILES |
177 | # define mmap(addr,length,prot,flags,fd,offs) EIO_ENOSYS () |
186 | # define mmap(addr,length,prot,flags,fd,offs) (errno = ENOSYS, (void *)-1) |
178 | # define munmap(addr,length) EIO_ENOSYS () |
187 | # define munmap(addr,length) EIO_ENOSYS () |
179 | #endif |
188 | #endif |
180 | |
189 | |
181 | #if !_POSIX_MEMORY_PROTECTION |
190 | #if !_POSIX_MEMORY_PROTECTION |
182 | # define mprotect(addr,len,prot) EIO_ENOSYS () |
191 | # define mprotect(addr,len,prot) EIO_ENOSYS () |
|
|
192 | #endif |
|
|
193 | |
|
|
194 | #if !MREMAP_MAYMOVE |
|
|
195 | # define mremap(old_address,old_size,new_size,flags,new_address) (errno = ENOSYS, (void *)-1) |
183 | #endif |
196 | #endif |
184 | |
197 | |
185 | #define FOREIGN_MAGIC PERL_MAGIC_ext |
198 | #define FOREIGN_MAGIC PERL_MAGIC_ext |
186 | |
199 | |
187 | static int ecb_cold |
200 | static int ecb_cold |
… | |
… | |
259 | { |
272 | { |
260 | /* todo: iterate over magic and only free ours, but of course */ |
273 | /* todo: iterate over magic and only free ours, but of course */ |
261 | /* the perl5porters will call that (correct) behaviour buggy */ |
274 | /* the perl5porters will call that (correct) behaviour buggy */ |
262 | sv_unmagic (sv, FOREIGN_MAGIC); |
275 | sv_unmagic (sv, FOREIGN_MAGIC); |
263 | } |
276 | } |
|
|
277 | |
|
|
278 | /*****************************************************************************/ |
|
|
279 | |
|
|
280 | /* defines all sorts of constants to 0 unless they are already defined */ |
|
|
281 | /* also provides const_iv_ and const_niv_ macros for them */ |
|
|
282 | #include "def0.h" |
264 | |
283 | |
265 | /*****************************************************************************/ |
284 | /*****************************************************************************/ |
266 | |
285 | |
267 | static void |
286 | static void |
268 | fiemap (eio_req *req) |
287 | fiemap (eio_req *req) |
… | |
… | |
699 | { |
718 | { |
700 | req->flags &= ~EIO_FLAG_PTR2_FREE; |
719 | req->flags &= ~EIO_FLAG_PTR2_FREE; |
701 | sv_set_foreign (req->sv2, &sysfree_vtbl, req->ptr2, req->result); |
720 | sv_set_foreign (req->sv2, &sysfree_vtbl, req->ptr2, req->result); |
702 | } |
721 | } |
703 | else |
722 | else |
|
|
723 | { |
704 | SvCUR_set (req->sv2, req->result); |
724 | SvCUR_set (req->sv2, req->result); |
|
|
725 | *SvEND (req->sv2) = 0; |
|
|
726 | SvPOK_only (req->sv2); |
|
|
727 | } |
|
|
728 | |
|
|
729 | SvSETMAGIC (req->sv2); |
705 | } |
730 | } |
706 | |
731 | |
707 | PUSHs (sv_result); |
732 | PUSHs (sv_result); |
708 | } |
733 | } |
709 | break; |
734 | break; |
… | |
… | |
963 | req_set_path1 (req, fh_or_path); |
988 | req_set_path1 (req, fh_or_path); |
964 | break; |
989 | break; |
965 | } |
990 | } |
966 | } |
991 | } |
967 | |
992 | |
|
|
993 | /*****************************************************************************/ |
|
|
994 | |
|
|
995 | static void |
|
|
996 | ts_set (struct timespec *ts, NV value) |
|
|
997 | { |
|
|
998 | ts->tv_sec = value; |
|
|
999 | ts->tv_nsec = (value - ts->tv_sec) * 1e9; |
|
|
1000 | } |
|
|
1001 | |
|
|
1002 | static NV |
|
|
1003 | ts_get (const struct timespec *ts) |
|
|
1004 | { |
|
|
1005 | return ts->tv_sec + ts->tv_nsec * 1e-9; |
|
|
1006 | } |
|
|
1007 | |
|
|
1008 | /*****************************************************************************/ |
|
|
1009 | |
968 | XS(boot_IO__AIO) ecb_cold; |
1010 | XS(boot_IO__AIO) ecb_cold; |
969 | |
1011 | |
970 | MODULE = IO::AIO PACKAGE = IO::AIO |
1012 | MODULE = IO::AIO PACKAGE = IO::AIO |
971 | |
1013 | |
972 | PROTOTYPES: ENABLE |
1014 | PROTOTYPES: ENABLE |
… | |
… | |
1003 | const_niv (MADV_RANDOM , POSIX_MADV_RANDOM) |
1045 | const_niv (MADV_RANDOM , POSIX_MADV_RANDOM) |
1004 | const_niv (MADV_WILLNEED , POSIX_MADV_WILLNEED) |
1046 | const_niv (MADV_WILLNEED , POSIX_MADV_WILLNEED) |
1005 | const_niv (MADV_DONTNEED , POSIX_MADV_DONTNEED) |
1047 | const_niv (MADV_DONTNEED , POSIX_MADV_DONTNEED) |
1006 | |
1048 | |
1007 | /* the second block will be 0 when missing */ |
1049 | /* the second block will be 0 when missing */ |
|
|
1050 | const_iv (O_ACCMODE) |
|
|
1051 | |
1008 | const_iv (O_RDONLY) |
1052 | const_iv (O_RDONLY) |
1009 | const_iv (O_WRONLY) |
1053 | const_iv (O_WRONLY) |
1010 | const_iv (O_RDWR) |
1054 | const_iv (O_RDWR) |
1011 | const_iv (O_CREAT) |
1055 | const_iv (O_CREAT) |
1012 | const_iv (O_TRUNC) |
1056 | const_iv (O_TRUNC) |
… | |
… | |
1072 | const_iv (MAP_GROWSDOWN) |
1116 | const_iv (MAP_GROWSDOWN) |
1073 | const_iv (MAP_32BIT) |
1117 | const_iv (MAP_32BIT) |
1074 | const_iv (MAP_HUGETLB) |
1118 | const_iv (MAP_HUGETLB) |
1075 | const_iv (MAP_STACK) |
1119 | const_iv (MAP_STACK) |
1076 | |
1120 | |
|
|
1121 | const_iv (MREMAP_MAYMOVE) |
|
|
1122 | const_iv (MREMAP_FIXED) |
|
|
1123 | |
1077 | const_iv (F_DUPFD_CLOEXEC) |
1124 | const_iv (F_DUPFD_CLOEXEC) |
|
|
1125 | |
|
|
1126 | const_iv (MSG_CMSG_CLOEXEC) |
|
|
1127 | const_iv (SOCK_CLOEXEC) |
1078 | |
1128 | |
1079 | const_iv (F_OFD_GETLK) |
1129 | const_iv (F_OFD_GETLK) |
1080 | const_iv (F_OFD_SETLK) |
1130 | const_iv (F_OFD_SETLK) |
1081 | const_iv (F_OFD_GETLKW) |
1131 | const_iv (F_OFD_GETLKW) |
1082 | |
1132 | |
… | |
… | |
1155 | const_iv (SPLICE_F_MOVE) |
1205 | const_iv (SPLICE_F_MOVE) |
1156 | const_iv (SPLICE_F_NONBLOCK) |
1206 | const_iv (SPLICE_F_NONBLOCK) |
1157 | const_iv (SPLICE_F_MORE) |
1207 | const_iv (SPLICE_F_MORE) |
1158 | const_iv (SPLICE_F_GIFT) |
1208 | const_iv (SPLICE_F_GIFT) |
1159 | |
1209 | |
|
|
1210 | const_iv (EFD_CLOEXEC) |
|
|
1211 | const_iv (EFD_NONBLOCK) |
|
|
1212 | const_iv (EFD_SEMAPHORE) |
|
|
1213 | |
|
|
1214 | const_iv (CLOCK_REALTIME) |
|
|
1215 | const_iv (CLOCK_MONOTONIC) |
|
|
1216 | const_iv (CLOCK_BOOTTIME) |
|
|
1217 | const_iv (CLOCK_REALTIME_ALARM) |
|
|
1218 | const_iv (CLOCK_BOOTTIME_ALARM) |
|
|
1219 | |
|
|
1220 | const_iv (TFD_NONBLOCK) |
|
|
1221 | const_iv (TFD_CLOEXEC) |
|
|
1222 | |
|
|
1223 | const_iv (TFD_TIMER_ABSTIME) |
|
|
1224 | const_iv (TFD_TIMER_CANCEL_ON_SET) |
|
|
1225 | |
1160 | /* these are libeio constants, and are independent of gendef0 */ |
1226 | /* these are libeio constants, and are independent of gendef0 */ |
1161 | const_eio (SEEK_SET) |
1227 | const_eio (SEEK_SET) |
1162 | const_eio (SEEK_CUR) |
1228 | const_eio (SEEK_CUR) |
1163 | const_eio (SEEK_END) |
1229 | const_eio (SEEK_END) |
1164 | |
1230 | |
… | |
… | |
1415 | } |
1481 | } |
1416 | else |
1482 | else |
1417 | { |
1483 | { |
1418 | /* read: check type and grow scalar as necessary */ |
1484 | /* read: check type and grow scalar as necessary */ |
1419 | if (!SvPOK (data) || SvLEN (data) >= SvCUR (data)) |
1485 | if (!SvPOK (data) || SvLEN (data) >= SvCUR (data)) |
1420 | svptr = sv_grow (data, len + dataoffset); |
1486 | svptr = sv_grow (data, len + dataoffset + 1); |
1421 | else if (SvCUR (data) < len + dataoffset) |
1487 | else if (SvCUR (data) < len + dataoffset) |
1422 | croak ("length + dataoffset outside of scalar, and cannot grow"); |
1488 | croak ("length + dataoffset outside of scalar, and cannot grow"); |
1423 | } |
1489 | } |
1424 | |
1490 | |
1425 | { |
1491 | { |
… | |
… | |
1819 | sv_clear_foreign (data); |
1885 | sv_clear_foreign (data); |
1820 | |
1886 | |
1821 | if (length) /* known length, directly read into scalar */ |
1887 | if (length) /* known length, directly read into scalar */ |
1822 | { |
1888 | { |
1823 | if (!SvPOK (data) || SvLEN (data) >= SvCUR (data)) |
1889 | if (!SvPOK (data) || SvLEN (data) >= SvCUR (data)) |
1824 | svptr = sv_grow (data, length); |
1890 | svptr = sv_grow (data, length + 1); |
1825 | else if (SvCUR (data) < length) |
1891 | else if (SvCUR (data) < length) |
1826 | croak ("length outside of scalar, and cannot grow"); |
1892 | croak ("length outside of scalar, and cannot grow"); |
1827 | else |
1893 | else |
1828 | svptr = SvPVbyte_nolen (data); |
1894 | svptr = SvPVbyte_nolen (data); |
1829 | } |
1895 | } |
… | |
… | |
2010 | void |
2076 | void |
2011 | munmap (SV *scalar) |
2077 | munmap (SV *scalar) |
2012 | CODE: |
2078 | CODE: |
2013 | sv_clear_foreign (scalar); |
2079 | sv_clear_foreign (scalar); |
2014 | |
2080 | |
|
|
2081 | SV * |
|
|
2082 | mremap (SV *scalar, STRLEN new_length, int flags = MREMAP_MAYMOVE, IV new_address = 0) |
|
|
2083 | CODE: |
|
|
2084 | { |
|
|
2085 | MAGIC *mg = mg_findext (scalar, FOREIGN_MAGIC, &mmap_vtbl); |
|
|
2086 | void *new; |
|
|
2087 | |
|
|
2088 | if (!mg || SvPVX (scalar) != mg->mg_ptr) |
|
|
2089 | croak ("IO::AIO::mremap: scalar not mapped by IO::AIO::mmap or improperly modified"); |
|
|
2090 | |
|
|
2091 | new = mremap (mg->mg_ptr, (size_t)mg->mg_obj, new_length, flags, (void *)new_address); |
|
|
2092 | |
|
|
2093 | RETVAL = &PL_sv_no; |
|
|
2094 | |
|
|
2095 | if (new != (void *)-1) |
|
|
2096 | { |
|
|
2097 | RETVAL = new == (void *)mg->mg_ptr |
|
|
2098 | ? newSVpvn ("0 but true", 10) |
|
|
2099 | : &PL_sv_yes; |
|
|
2100 | |
|
|
2101 | mg->mg_ptr = (char *)new; |
|
|
2102 | mg->mg_obj = (SV *)new_length; |
|
|
2103 | |
|
|
2104 | SvPVX (scalar) = mg->mg_ptr; |
|
|
2105 | SvCUR_set (scalar, new_length); |
|
|
2106 | } |
|
|
2107 | } |
|
|
2108 | OUTPUT: |
|
|
2109 | RETVAL |
|
|
2110 | |
2015 | int |
2111 | int |
2016 | madvise (SV *scalar, STRLEN offset = 0, SV *length = &PL_sv_undef, IV advice_or_prot) |
2112 | madvise (SV *scalar, STRLEN offset = 0, SV *length = &PL_sv_undef, IV advice_or_prot) |
2017 | ALIAS: |
2113 | ALIAS: |
2018 | mprotect = 1 |
2114 | mprotect = 1 |
2019 | CODE: |
2115 | CODE: |
… | |
… | |
2148 | { |
2244 | { |
2149 | EXTEND (SP, 2); |
2245 | EXTEND (SP, 2); |
2150 | PUSHs (newmortalFH (fd[0], O_RDONLY)); |
2246 | PUSHs (newmortalFH (fd[0], O_RDONLY)); |
2151 | PUSHs (newmortalFH (fd[1], O_WRONLY)); |
2247 | PUSHs (newmortalFH (fd[1], O_WRONLY)); |
2152 | } |
2248 | } |
|
|
2249 | } |
|
|
2250 | |
|
|
2251 | void |
|
|
2252 | eventfd (unsigned int initval = 0, int flags = 0) |
|
|
2253 | PPCODE: |
|
|
2254 | { |
|
|
2255 | int fd; |
|
|
2256 | #if HAVE_EVENTFD |
|
|
2257 | fd = eventfd (initval, flags); |
|
|
2258 | #else |
|
|
2259 | fd = (errno = ENOSYS, -1); |
|
|
2260 | #endif |
|
|
2261 | |
|
|
2262 | XPUSHs (newmortalFH (fd, O_RDWR)); |
|
|
2263 | } |
|
|
2264 | |
|
|
2265 | void |
|
|
2266 | timerfd_create (int clockid, int flags = 0) |
|
|
2267 | PPCODE: |
|
|
2268 | { |
|
|
2269 | int fd; |
|
|
2270 | #if HAVE_TIMERFD |
|
|
2271 | fd = timerfd_create (clockid, flags); |
|
|
2272 | #else |
|
|
2273 | fd = (errno = ENOSYS, -1); |
|
|
2274 | #endif |
|
|
2275 | |
|
|
2276 | XPUSHs (newmortalFH (fd, O_RDWR)); |
|
|
2277 | } |
|
|
2278 | |
|
|
2279 | void |
|
|
2280 | timerfd_settime (SV *fh, int flags, NV interval, NV value) |
|
|
2281 | PPCODE: |
|
|
2282 | { |
|
|
2283 | int fd = s_fileno_croak (fh, 0); |
|
|
2284 | #if HAVE_TIMERFD |
|
|
2285 | int res; |
|
|
2286 | struct itimerspec its, ots; |
|
|
2287 | |
|
|
2288 | ts_set (&its.it_interval, interval); |
|
|
2289 | ts_set (&its.it_value , value); |
|
|
2290 | res = timerfd_settime (fd, flags, &its, &ots); |
|
|
2291 | |
|
|
2292 | if (!res) |
|
|
2293 | { |
|
|
2294 | EXTEND (SP, 2); |
|
|
2295 | PUSHs (newSVnv (ts_get (&ots.it_interval))); |
|
|
2296 | PUSHs (newSVnv (ts_get (&ots.it_value))); |
|
|
2297 | } |
|
|
2298 | #else |
|
|
2299 | errno = ENOSYS; |
|
|
2300 | #endif |
|
|
2301 | } |
|
|
2302 | |
|
|
2303 | void |
|
|
2304 | timerfd_gettime (SV *fh) |
|
|
2305 | PPCODE: |
|
|
2306 | { |
|
|
2307 | int fd = s_fileno_croak (fh, 0); |
|
|
2308 | #if HAVE_TIMERFD |
|
|
2309 | int res; |
|
|
2310 | struct itimerspec ots; |
|
|
2311 | res = timerfd_gettime (fd, &ots); |
|
|
2312 | |
|
|
2313 | if (!res) |
|
|
2314 | { |
|
|
2315 | EXTEND (SP, 2); |
|
|
2316 | PUSHs (newSVnv (ts_get (&ots.it_interval))); |
|
|
2317 | PUSHs (newSVnv (ts_get (&ots.it_value))); |
|
|
2318 | } |
|
|
2319 | #else |
|
|
2320 | errno = ENOSYS; |
|
|
2321 | #endif |
2153 | } |
2322 | } |
2154 | |
2323 | |
2155 | UV |
2324 | UV |
2156 | get_fdlimit () |
2325 | get_fdlimit () |
2157 | CODE: |
2326 | CODE: |