--- IO-AIO/AIO.xs 2017/09/22 05:20:39 1.250 +++ IO-AIO/AIO.xs 2018/11/29 19:49:48 1.268 @@ -6,21 +6,20 @@ #include "perl.h" #include "XSUB.h" -#include "schmorp.h" +#if !defined mg_findext +# define mg_findext(sv,type,vtbl) mg_find (sv, type) +#endif #include #include #include #include +#include #include #include #include #include -#if _POSIX_MEMLOCK || _POSIX_MEMLOCK_RANGE || _POSIX_MAPPED_FILES -# include -#endif - /* the incompetent fool that created musl keeps __linux__, refuses * to implement any linux standard apis, and also has no way to test * for his broken iplementation. don't complain to me if this fails @@ -77,6 +76,7 @@ #undef dup2 #undef abort #undef pipe + #undef utime #define EIO_STRUCT_STAT struct _stati64 #define EIO_STRUCT_STATI64 @@ -106,7 +106,58 @@ /*****************************************************************************/ -#include "libeio/config.h" +#include "config.h" + +#if HAVE_SYS_MKDEV_H +# include +#elif HAVE_SYS_SYSMACROS_H +# include +#endif + +#if _POSIX_MEMLOCK || _POSIX_MEMLOCK_RANGE || _POSIX_MAPPED_FILES +# include +#endif + +#if HAVE_ST_XTIMENSEC +# define ATIMENSEC PL_statcache.st_atimensec +# define MTIMENSEC PL_statcache.st_mtimensec +# define CTIMENSEC PL_statcache.st_ctimensec +#elif HAVE_ST_XTIMESPEC +# define ATIMENSEC PL_statcache.st_atim.tv_nsec +# define MTIMENSEC PL_statcache.st_mtim.tv_nsec +# define CTIMENSEC PL_statcache.st_ctim.tv_nsec +#else +# define ATIMENSEC 0 +# define MTIMENSEC 0 +# define CTIMENSEC 0 +#endif + +#if HAVE_ST_BIRTHTIMENSEC +# define BTIMESEC PL_statcache.st_birthtime +# define BTIMENSEC PL_statcache.st_birthtimensec +#elif HAVE_ST_BIRTHTIMESPEC +# define BTIMESEC PL_statcache.st_birthtim.tv_sec +# define BTIMENSEC PL_statcache.st_birthtim.tv_nsec +#else +# define BTIMESEC 0 +# define BTIMENSEC 0 +#endif + +#if HAVE_ST_GEN +# define ST_GEN PL_statcache.st_gen +#else +# define ST_GEN 0 +#endif + +#include "schmorp.h" + +#if HAVE_EVENTFD +# include +#endif + +#if HAVE_TIMERFD +# include +#endif #if HAVE_RLIMITS #include @@ -153,10 +204,6 @@ # endif #endif -/* defines all sorts of constants to 0 unless they are already defined */ -/* also provides const_iv_ and const_niv_ macros for them */ -#include "def0.h" - #ifndef makedev # define makedev(maj,min) (((maj) << 8) | (min)) #endif @@ -167,12 +214,118 @@ # define minor(dev) ((dev) & 0xff) #endif +/* solaris has a non-posix/unix compliant PAGESIZE that breaks compilation */ +#ifdef __sun +# undef PAGESIZE +#endif + #if PAGESIZE <= 0 # define PAGESIZE sysconf (_SC_PAGESIZE) #endif /*****************************************************************************/ +#if !_POSIX_MAPPED_FILES +# define mmap(addr,length,prot,flags,fd,offs) (errno = ENOSYS, (void *)-1) +# define munmap(addr,length) EIO_ENOSYS () +#endif + +#if !_POSIX_MEMORY_PROTECTION +# define mprotect(addr,len,prot) EIO_ENOSYS () +#endif + +#if !MREMAP_MAYMOVE +# define mremap(old_address,old_size,new_size,flags,new_address) (errno = ENOSYS, (void *)-1) +#endif + +#define FOREIGN_MAGIC PERL_MAGIC_ext + +static int ecb_cold +mmap_free (pTHX_ SV *sv, MAGIC *mg) +{ + int old_errno = errno; + munmap (mg->mg_ptr, (size_t)mg->mg_obj); + errno = old_errno; + + mg->mg_obj = 0; /* just in case */ + + SvREADONLY_off (sv); + + if (SvPVX (sv) != mg->mg_ptr) + croak ("ERROR: IO::AIO::mmap-mapped scalar changed location, detected"); + + SvCUR_set (sv, 0); + SvPVX (sv) = 0; + SvOK_off (sv); + + return 0; +} + +static MGVTBL mmap_vtbl = { + 0, 0, 0, 0, mmap_free +}; + +static int ecb_cold +sysfree_free (pTHX_ SV *sv, MAGIC *mg) +{ + free (mg->mg_ptr); + mg->mg_obj = 0; /* just in case */ + + SvREADONLY_off (sv); + + if (SvPVX (sv) != mg->mg_ptr) + croak ("ERROR: IO::AIO mapped scalar changed location, detected"); + + SvCUR_set (sv, 0); + SvPVX (sv) = 0; + SvOK_off (sv); + + return 0; +} + +static MGVTBL sysfree_vtbl = { + 0, 0, 0, 0, sysfree_free +}; + +/*****************************************************************************/ + +/* helper: set scalar to foreign ptr with custom free */ +static void +sv_set_foreign (SV *sv, const MGVTBL *const vtbl, void *addr, IV length) +{ + sv_force_normal (sv); + + /* we store the length in mg_obj, as namlen is I32 :/ */ + sv_magicext (sv, 0, FOREIGN_MAGIC, vtbl, (char *)addr, 0) + ->mg_obj = (SV *)length; + + SvUPGRADE (sv, SVt_PV); /* nop... */ + + if (SvLEN (sv)) + Safefree (SvPVX (sv)); + + SvPVX (sv) = (char *)addr; + SvCUR_set (sv, length); + SvLEN_set (sv, 0); + SvPOK_only (sv); +} + +static void +sv_clear_foreign (SV *sv) +{ + /* todo: iterate over magic and only free ours, but of course */ + /* the perl5porters will call that (correct) behaviour buggy */ + sv_unmagic (sv, FOREIGN_MAGIC); +} + +/*****************************************************************************/ + +/* defines all sorts of constants to 0 unless they are already defined */ +/* also provides const_iv_ and const_niv_ macros for them */ +#include "def0.h" + +/*****************************************************************************/ + static void fiemap (eio_req *req) { @@ -271,6 +424,8 @@ /*****************************************************************************/ +static int close_fd; /* dummy fd to close fds via dup2 */ + enum { FLAG_SV2_RO_OFF = 0x40, /* data was set readonly */ }; @@ -599,6 +754,30 @@ } break; + case EIO_SLURP: + { + if (req->result >= 0) + { + /* if length was originally not known, we steal the malloc'ed memory */ + if (req->flags & EIO_FLAG_PTR2_FREE) + { + req->flags &= ~EIO_FLAG_PTR2_FREE; + sv_set_foreign (req->sv2, &sysfree_vtbl, req->ptr2, req->result); + } + else + { + SvCUR_set (req->sv2, req->result); + *SvEND (req->sv2) = 0; + SvPOK_only (req->sv2); + } + + SvSETMAGIC (req->sv2); + } + + PUSHs (sv_result); + } + break; + case EIO_CUSTOM: if (req->feed == fiemap) { @@ -754,44 +933,6 @@ /*****************************************************************************/ -#if !_POSIX_MAPPED_FILES -# define mmap(addr,length,prot,flags,fd,offs) EIO_ENOSYS () -# define munmap(addr,length) EIO_ENOSYS () -#endif - -#if !_POSIX_MEMORY_PROTECTION -# define mprotect(addr,len,prot) EIO_ENOSYS () -#endif - -#define MMAP_MAGIC PERL_MAGIC_ext - -static int ecb_cold -mmap_free (pTHX_ SV *sv, MAGIC *mg) -{ - int old_errno = errno; - munmap (mg->mg_ptr, (size_t)mg->mg_obj); - errno = old_errno; - - mg->mg_obj = 0; /* just in case */ - - SvREADONLY_off (sv); - - if (SvPVX (sv) != mg->mg_ptr) - croak ("ERROR: IO::AIO::mmap-mapped scalar changed location, detected"); - - SvCUR_set (sv, 0); - SvPVX (sv) = 0; - SvOK_off (sv); - - return 0; -} - -static MGVTBL mmap_vtbl = { - 0, 0, 0, 0, mmap_free -}; - -/*****************************************************************************/ - static SV * get_cb (SV *cb_sv) { @@ -894,6 +1035,23 @@ } } +/*****************************************************************************/ + +static void +ts_set (struct timespec *ts, NV value) +{ + ts->tv_sec = value; + ts->tv_nsec = (value - ts->tv_sec) * 1e9; +} + +static NV +ts_get (const struct timespec *ts) +{ + return ts->tv_sec + ts->tv_nsec * 1e-9; +} + +/*****************************************************************************/ + XS(boot_IO__AIO) ecb_cold; MODULE = IO::AIO PACKAGE = IO::AIO @@ -934,6 +1092,8 @@ const_niv (MADV_DONTNEED , POSIX_MADV_DONTNEED) /* the second block will be 0 when missing */ + const_iv (O_ACCMODE) + const_iv (O_RDONLY) const_iv (O_WRONLY) const_iv (O_RDWR) @@ -1003,8 +1163,14 @@ const_iv (MAP_HUGETLB) const_iv (MAP_STACK) + const_iv (MREMAP_MAYMOVE) + const_iv (MREMAP_FIXED) + const_iv (F_DUPFD_CLOEXEC) + const_iv (MSG_CMSG_CLOEXEC) + const_iv (SOCK_CLOEXEC) + const_iv (F_OFD_GETLK) const_iv (F_OFD_SETLK) const_iv (F_OFD_GETLKW) @@ -1086,6 +1252,22 @@ const_iv (SPLICE_F_MORE) const_iv (SPLICE_F_GIFT) + const_iv (EFD_CLOEXEC) + const_iv (EFD_NONBLOCK) + const_iv (EFD_SEMAPHORE) + + const_iv (CLOCK_REALTIME) + const_iv (CLOCK_MONOTONIC) + const_iv (CLOCK_BOOTTIME) + const_iv (CLOCK_REALTIME_ALARM) + const_iv (CLOCK_BOOTTIME_ALARM) + + const_iv (TFD_NONBLOCK) + const_iv (TFD_CLOEXEC) + + const_iv (TFD_TIMER_ABSTIME) + const_iv (TFD_TIMER_CANCEL_ON_SET) + /* these are libeio constants, and are independent of gendef0 */ const_eio (SEEK_SET) const_eio (SEEK_CUR) @@ -1093,6 +1275,7 @@ const_eio (MCL_FUTURE) const_eio (MCL_CURRENT) + const_eio (MCL_ONFAULT) const_eio (MS_ASYNC) const_eio (MS_INVALIDATE) @@ -1141,6 +1324,24 @@ newCONSTSUB (aio_stash, "PAGESIZE", newSViv (PAGESIZE)); + /* allocate dummy pipe fd for aio_close */ + { + int pipefd [2]; + + if ( +#ifdef _WIN32 + _pipe (pipefd, 1, _O_BINARY) < 0 +#else + pipe (pipefd) < 0 + || fcntl (pipefd [0], F_SETFD, FD_CLOEXEC) < 0 +#endif + || close (pipefd [1]) < 0 + ) + croak ("IO::AIO: unable to create dummy pipe for aio_close"); + + close_fd = pipefd [0]; + } + reinit (); } @@ -1272,28 +1473,9 @@ aio_close (SV *fh, SV *callback = &PL_sv_undef) PPCODE: { - static int close_fd = -1; /* dummy fd to close fds via dup2 */ int fd = s_fileno_croak (fh, 0); dREQ; - if (expect_false (close_fd < 0)) - { - int pipefd [2]; - - if ( -#ifdef _WIN32 - _pipe (pipefd, 1, _O_BINARY) < 0 -#else - pipe (pipefd) < 0 - || fcntl (pipefd [0], F_SETFD, FD_CLOEXEC) < 0 -#endif - || close (pipefd [1]) < 0 - ) - abort (); /*D*/ - - close_fd = pipefd [0]; - } - req->type = EIO_DUP2; req->int1 = close_fd; req->sv2 = newSVsv (fh); @@ -1478,6 +1660,40 @@ REQ_SEND; } +void +st_xtime () + ALIAS: + st_atime = 0x01 + st_mtime = 0x02 + st_ctime = 0x04 + st_btime = 0x08 + st_xtime = 0x0f + PPCODE: + EXTEND (SP, 4); + if (ix & 0x01) PUSHs (newSVnv (PL_statcache.st_atime + 1e-9 * ATIMENSEC)); + if (ix & 0x02) PUSHs (newSVnv (PL_statcache.st_mtime + 1e-9 * MTIMENSEC)); + if (ix & 0x04) PUSHs (newSVnv (PL_statcache.st_ctime + 1e-9 * CTIMENSEC)); + if (ix & 0x08) PUSHs (newSVnv (BTIMESEC + 1e-9 * BTIMENSEC)); + +void +st_xtimensec () + ALIAS: + st_atimensec = 0x01 + st_mtimensec = 0x02 + st_ctimensec = 0x04 + st_btimensec = 0x08 + st_xtimensec = 0x0f + st_btimesec = 0x10 + st_gen = 0x20 + PPCODE: + EXTEND (SP, 4); + if (ix & 0x01) PUSHs (newSViv (ATIMENSEC)); + if (ix & 0x02) PUSHs (newSViv (MTIMENSEC)); + if (ix & 0x04) PUSHs (newSViv (CTIMENSEC)); + if (ix & 0x08) PUSHs (newSViv (BTIMENSEC)); + if (ix & 0x10) PUSHs (newSVuv (BTIMESEC)); + if (ix & 0x20) PUSHs (newSVuv (ST_GEN)); + UV major (UV dev) ALIAS: @@ -1740,6 +1956,44 @@ } void +aio_slurp (SV *pathname, off_t offset, UV length, SV8 *data, SV *callback = &PL_sv_undef) + PPCODE: +{ + char *svptr = 0; + + sv_clear_foreign (data); + + if (length) /* known length, directly read into scalar */ + { + if (!SvPOK (data) || SvLEN (data) >= SvCUR (data)) + svptr = sv_grow (data, length + 1); + else if (SvCUR (data) < length) + croak ("length outside of scalar, and cannot grow"); + else + svptr = SvPVbyte_nolen (data); + } + + { + dREQ; + + req->type = EIO_SLURP; + req_set_path1 (req, pathname); + req->offs = offset; + req->size = length; + req->sv2 = SvREFCNT_inc (data); + req->ptr2 = svptr; + + if (!SvREADONLY (data)) + { + SvREADONLY_on (data); + req->flags |= FLAG_SV2_RO_OFF; + } + + REQ_SEND; + } +} + +void aio_busy (double delay, SV *callback = &PL_sv_undef) PPCODE: { @@ -1883,42 +2137,58 @@ void mmap (SV *scalar, STRLEN length, int prot, int flags, SV *fh = &PL_sv_undef, off_t offset = 0) PPCODE: - sv_unmagic (scalar, MMAP_MAGIC); + sv_clear_foreign (scalar); { int fd = SvOK (fh) ? s_fileno_croak (fh, flags & PROT_WRITE) : -1; void *addr = (void *)mmap (0, length, prot, flags, fd, offset); if (addr == (void *)-1) XSRETURN_NO; - sv_force_normal (scalar); - - /* we store the length in mg_obj, as namlen is I32 :/ */ - sv_magicext (scalar, 0, MMAP_MAGIC, &mmap_vtbl, (char *)addr, 0) - ->mg_obj = (SV *)length; - - SvUPGRADE (scalar, SVt_PV); /* nop... */ + sv_set_foreign (scalar, &mmap_vtbl, addr, length); if (!(prot & PROT_WRITE)) SvREADONLY_on (scalar); - if (SvLEN (scalar)) - Safefree (SvPVX (scalar)); - - SvPVX (scalar) = (char *)addr; - SvCUR_set (scalar, length); - SvLEN_set (scalar, 0); - SvPOK_only (scalar); - XSRETURN_YES; } void munmap (SV *scalar) CODE: - sv_unmagic (scalar, MMAP_MAGIC); + sv_clear_foreign (scalar); + +SV * +mremap (SV *scalar, STRLEN new_length, int flags = MREMAP_MAYMOVE, IV new_address = 0) + CODE: +{ + MAGIC *mg = mg_findext (scalar, FOREIGN_MAGIC, &mmap_vtbl); + void *new; + + if (!mg || SvPVX (scalar) != mg->mg_ptr) + croak ("IO::AIO::mremap: scalar not mapped by IO::AIO::mmap or improperly modified"); + + new = mremap (mg->mg_ptr, (size_t)mg->mg_obj, new_length, flags, (void *)new_address); + + RETVAL = &PL_sv_no; + + if (new != (void *)-1) + { + RETVAL = new == (void *)mg->mg_ptr + ? newSVpvn ("0 but true", 10) + : &PL_sv_yes; + + mg->mg_ptr = (char *)new; + mg->mg_obj = (SV *)new_length; + + SvPVX (scalar) = mg->mg_ptr; + SvCUR_set (scalar, new_length); + } +} + OUTPUT: + RETVAL int -madvise (SV *scalar, STRLEN offset = 0, SV *length = &PL_sv_undef, IV advice_or_prot) +madvise (SV *scalar, IV offset = 0, SV *length = &PL_sv_undef, IV advice_or_prot) ALIAS: mprotect = 1 CODE: @@ -1949,7 +2219,7 @@ RETVAL int -munlock (SV *scalar, STRLEN offset = 0, SV *length = &PL_sv_undef) +munlock (SV *scalar, IV offset = 0, SV *length = &PL_sv_undef) CODE: { STRLEN svlen; @@ -2057,6 +2327,79 @@ } } +void +eventfd (unsigned int initval = 0, int flags = 0) + PPCODE: +{ + int fd; +#if HAVE_EVENTFD + fd = eventfd (initval, flags); +#else + fd = (errno = ENOSYS, -1); +#endif + + XPUSHs (newmortalFH (fd, O_RDWR)); +} + +void +timerfd_create (int clockid, int flags = 0) + PPCODE: +{ + int fd; +#if HAVE_TIMERFD + fd = timerfd_create (clockid, flags); +#else + fd = (errno = ENOSYS, -1); +#endif + + XPUSHs (newmortalFH (fd, O_RDWR)); +} + +void +timerfd_settime (SV *fh, int flags, NV interval, NV value) + PPCODE: +{ + int fd = s_fileno_croak (fh, 0); +#if HAVE_TIMERFD + int res; + struct itimerspec its, ots; + + ts_set (&its.it_interval, interval); + ts_set (&its.it_value , value); + res = timerfd_settime (fd, flags, &its, &ots); + + if (!res) + { + EXTEND (SP, 2); + PUSHs (newSVnv (ts_get (&ots.it_interval))); + PUSHs (newSVnv (ts_get (&ots.it_value))); + } +#else + errno = ENOSYS; +#endif +} + +void +timerfd_gettime (SV *fh) + PPCODE: +{ + int fd = s_fileno_croak (fh, 0); +#if HAVE_TIMERFD + int res; + struct itimerspec ots; + res = timerfd_gettime (fd, &ots); + + if (!res) + { + EXTEND (SP, 2); + PUSHs (newSVnv (ts_get (&ots.it_interval))); + PUSHs (newSVnv (ts_get (&ots.it_value))); + } +#else + errno = ENOSYS; +#endif +} + UV get_fdlimit () CODE: