--- IO-AIO/AIO.xs 2012/06/17 17:07:25 1.215 +++ IO-AIO/AIO.xs 2022/09/25 16:30:51 1.297 @@ -5,31 +5,22 @@ #include "EXTERN.h" #include "perl.h" #include "XSUB.h" +#include "perliol.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 - -#if __linux__ -# include -# ifdef FS_IOC_FIEMAP -# include -# include -# define HAVE_FIEMAP 1 -# endif -#endif - /* perl namespace pollution */ #undef VERSION @@ -72,6 +63,7 @@ #undef dup2 #undef abort #undef pipe + #undef utime #define EIO_STRUCT_STAT struct _stati64 #define EIO_STRUCT_STATI64 @@ -80,6 +72,7 @@ #include #include + #include #include #include #include @@ -101,7 +94,93 @@ /*****************************************************************************/ +#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_SYS_UIO_H +# include +#endif + +/* MUST be included before linux/fs.h, as the latter includes + * linux/mount.h, which is incompatible to sys/mount.h + */ +#if HAVE_SYS_MOUNT_H +# 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 implementation. don't complain to me if this fails + * for you. + */ +#if __linux__ && (defined __GLIBC__ || defined __UCLIBC__) +# include /* MUST be included after sys/mount.h */ +# ifdef FS_IOC_FIEMAP +# include +# include +# undef HAVE_FIEMAP +# define HAVE_FIEMAP 1 +# endif +#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 + #include +#endif + typedef SV SV8; /* byte-sv, used for argument-checking */ +typedef char *octet_string; +typedef char *octet_string_ornull; typedef int aio_rfd; /* read file desriptor */ typedef int aio_wfd; /* write file descriptor */ @@ -116,7 +195,6 @@ #define EIO_NO_WRAPPERS 1 -#include "libeio/config.h" #include "libeio/eio.h" static int req_invoke (eio_req *req); @@ -142,9 +220,6 @@ # endif #endif -/* defines all sorts of constants to 0 unless they are already defined */ -#include "def0.h" - #ifndef makedev # define makedev(maj,min) (((maj) << 8) | (min)) #endif @@ -155,10 +230,128 @@ # define minor(dev) ((dev) & 0xff) #endif -#ifndef PAGESIZE +/* 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 +/* solaris perl seems to declare a wrong syscall function that clashes with system includes */ +#ifdef __sun +# undef HAVE_SYSCALL +#endif + +#if HAVE_SYSCALL +#include +#else +# define syscall(nr,...) (errno = ENOSYS, -1) +#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 */ +ecb_noinline +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 @@ -167,49 +360,89 @@ req->result = -1; #if HAVE_FIEMAP + /* assume some c99 */ + struct fiemap *fiemap = 0; + size_t end_offset; int count = req->int3; - /* heuristic: first try with 64 extents if we don't know how many, */ - /* as most files have (hopefully) fewer than this many extents */ - /* in fact, most should have <= 2, so maybe the 72 below is probably overkill */ + req->flags |= EIO_FLAG_PTR1_FREE; + + /* heuristic: start with 512 bytes (8 extents), and if that isn't enough, */ + /* increase in fixed steps */ if (count < 0) - count = 72; /* for what it's worth, 72 extents fit nicely into 4kb */ + count = 8; + + fiemap = malloc (sizeof (*fiemap) + sizeof (struct fiemap_extent) * count); + errno = ENOMEM; + if (!fiemap) + return; + + req->ptr1 = fiemap; + + fiemap->fm_start = req->offs; + fiemap->fm_length = req->size; + fiemap->fm_flags = req->int2; + fiemap->fm_extent_count = count; + + if (ioctl (req->int1, FS_IOC_FIEMAP, fiemap) < 0) + return; + + if (req->int3 >= 0 /* not autosizing */ + || !fiemap->fm_mapped_extents /* no more extents */ + || fiemap->fm_extents [fiemap->fm_mapped_extents - 1].fe_flags & FIEMAP_EXTENT_LAST /* hit eof */) + goto done; + + /* else we have to loop - + * it would be tempting (actually I tried that first) to just query the + * number of extents needed, but linux often feels like not returning all + * extents, without telling us it left any out. this complicates + * this quite a bit. + */ + + end_offset = fiemap->fm_length + (fiemap->fm_length == FIEMAP_MAX_OFFSET ? 0 : fiemap->fm_start); for (;;) { - struct fiemap *fiemap = malloc (sizeof (*fiemap) + sizeof (struct fiemap_extent) * count); - errno = ENOMEM; - if (!fiemap) - return; + /* we go in 54 extent steps - 3kb, in the hope that this fits nicely on the eio stack (normally 16+ kb) */ + char scratch[3072]; + struct fiemap *incmap = (struct fiemap *)scratch; + + incmap->fm_start = fiemap->fm_extents [fiemap->fm_mapped_extents - 1].fe_logical + + fiemap->fm_extents [fiemap->fm_mapped_extents - 1].fe_length; + incmap->fm_length = fiemap->fm_length - (incmap->fm_start - fiemap->fm_start); + incmap->fm_flags = fiemap->fm_flags; + incmap->fm_extent_count = (sizeof (scratch) - sizeof (struct fiemap)) / sizeof (struct fiemap_extent); - req->ptr1 = fiemap; - req->flags |= EIO_FLAG_PTR1_FREE; + if (ioctl (req->int1, FS_IOC_FIEMAP, incmap) < 0) + return; - fiemap->fm_start = req->offs; - fiemap->fm_length = req->size; - fiemap->fm_flags = req->int2; - fiemap->fm_extent_count = count; + if (!incmap->fm_mapped_extents) + goto done; - if (ioctl (req->int1, FS_IOC_FIEMAP, fiemap)) + count = fiemap->fm_mapped_extents + incmap->fm_mapped_extents; + fiemap = realloc (fiemap, sizeof (*fiemap) + sizeof (struct fiemap_extent) * count); + errno = ENOMEM; + if (!fiemap) return; - if (req->int3 >= 0) - break; /* when not autosizing we are done */ + req->ptr1 = fiemap; - if (fiemap->fm_extents [fiemap->fm_mapped_extents - 1].fe_flags & FIEMAP_EXTENT_LAST) - break; /* autosizing successful, we are done */ + for (count = 0; count < incmap->fm_mapped_extents; ++count) + { + struct fiemap_extent *e = incmap->fm_extents + count; - fiemap->fm_flags = req->int2; - fiemap->fm_extent_count = 0; + fiemap->fm_extents [fiemap->fm_mapped_extents++] = *e; - if (ioctl (req->int1, FS_IOC_FIEMAP, fiemap)) - return; + if (e->fe_logical >= end_offset) + goto done; - count = fiemap->fm_mapped_extents; + if (e->fe_flags & FIEMAP_EXTENT_LAST) + goto done; - free (fiemap); + } } +done: req->result = 0; #else @@ -219,6 +452,17 @@ /*****************************************************************************/ +static int close_fd; /* dummy fd to close fds via dup2 */ + +#if HAVE_STATX +static struct statx stx; +#define statx_offsetof(member) offsetof (struct statx, member) +#define eio__statx statx +#else +#define statx_offsetof(member) 0 +#define eio__statx(dir,path,flags,mask,stx) EIO_ENOSYS() +#endif + enum { FLAG_SV2_RO_OFF = 0x40, /* data was set readonly */ }; @@ -251,6 +495,7 @@ } /* must be called at most once */ +ecb_noinline static SV * req_sv (aio_req req, HV *stash) { @@ -266,9 +511,10 @@ static SV * newSVaio_wd (aio_wd wd) { - return sv_bless (newRV_noinc (newSViv ((IV)wd)), aio_wd_stash); + return sv_bless (newRV_noinc (newSViv ((intptr_t)wd)), aio_wd_stash); } +ecb_noinline static aio_req SvAIO_REQ (SV *sv) { @@ -297,6 +543,31 @@ return (aio_wd)(long)SvIVX (SvRV (sv)); } +static SV * +newmortalFH (int fd, int flags) +{ + if (fd < 0) + return &PL_sv_undef; + + GV *gv = (GV *)sv_newmortal (); + char sym[64]; + int symlen; + + symlen = snprintf (sym, sizeof (sym), "fd#%d", fd); + gv_init (gv, aio_stash, sym, symlen, 0); + + symlen = snprintf ( + sym, + sizeof (sym), + "%s&=%d", + flags == O_RDONLY ? "<" : flags == O_WRONLY ? ">" : "+<", + fd + ); + + return do_open (gv, sym, symlen, 0, 0, 0, 0) + ? (SV *)gv : &PL_sv_undef; +} + static void aio_grp_feed (aio_req grp) { @@ -316,6 +587,7 @@ } } +ecb_noinline static void req_submit (eio_req *req) { @@ -432,34 +704,7 @@ break; case EIO_OPEN: - { - /* convert fd to fh */ - SV *fh = &PL_sv_undef; - - if (req->result >= 0) - { - GV *gv = (GV *)sv_newmortal (); - int flags = req->int1 & (O_RDONLY | O_WRONLY | O_RDWR); - char sym [64]; - int symlen; - - symlen = snprintf (sym, sizeof (sym), "fd#%d", (int)req->result); - gv_init (gv, aio_stash, sym, symlen, 0); - - symlen = snprintf ( - sym, - sizeof (sym), - "%s&=%d", - flags == O_RDONLY ? "<" : flags == O_WRONLY ? ">" : "+<", - (int)req->result - ); - - if (do_open (gv, sym, symlen, 0, 0, 0, 0)) - fh = (SV *)gv; - } - - PUSHs (fh); - } + PUSHs (newmortalFH (req->result, req->int1 & (O_RDONLY | O_WRONLY | O_RDWR))); break; case EIO_STATVFS: @@ -472,6 +717,11 @@ { EIO_STRUCT_STATVFS *f = EIO_STATVFS_BUF (req); HV *hv = newHV (); + /* POSIX requires fsid to be unsigned long, but AIX in its infinite wisdom + * chooses to make it a struct. + */ + unsigned long fsid = 0; + memcpy (&fsid, &f->f_fsid, sizeof (unsigned long) < sizeof (f->f_fsid) ? sizeof (unsigned long) : sizeof (f->f_fsid)); rv = sv_2mortal (newRV_noinc ((SV *)hv)); @@ -483,7 +733,7 @@ hv_store (hv, "files" , sizeof ("files" ) - 1, newSVval64 (f->f_files ), 0); hv_store (hv, "ffree" , sizeof ("ffree" ) - 1, newSVval64 (f->f_ffree ), 0); hv_store (hv, "favail" , sizeof ("favail" ) - 1, newSVval64 (f->f_favail ), 0); - hv_store (hv, "fsid" , sizeof ("fsid" ) - 1, newSVval64 (f->f_fsid ), 0); + hv_store (hv, "fsid" , sizeof ("fsid" ) - 1, newSVval64 (fsid ), 0); hv_store (hv, "flag" , sizeof ("flag" ) - 1, newSVval64 (f->f_flag ), 0); hv_store (hv, "namemax", sizeof ("namemax") - 1, newSVval64 (f->f_namemax), 0); } @@ -544,6 +794,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) { @@ -584,6 +858,12 @@ PUSHs (sv_result); break; +#if 0 + case EIO_CLOSE: + PerlIOUnix_refcnt_dec (req->int1); + break; +#endif + case EIO_DUP2: /* EIO_DUP2 actually means aio_close(), so fudge result value */ if (req->result > 0) SvIV_set (sv_result, 0); @@ -644,7 +924,8 @@ eio_grp_cancel (grp); } -static void ecb_cold +ecb_cold +static void create_respipe (void) { if (s_epipe_renew (&respipe)) @@ -658,14 +939,14 @@ { int size; - X_LOCK (reslock); - size = res_queue.size; - X_UNLOCK (reslock); + X_LOCK (EIO_POOL->reslock); + size = EIO_POOL->res_queue.size; + X_UNLOCK (EIO_POOL->reslock); if (size) return; - etp_maybe_start_thread (); + etp_maybe_start_thread (EIO_POOL); s_epipe_wait (&respipe); } @@ -688,7 +969,8 @@ } } -static void ecb_cold +ecb_cold +static void reinit (void) { create_respipe (); @@ -699,49 +981,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 () -# define PROT_NONE 0 -# define PROT_WRITE 0 -# define MAP_PRIVATE 0 -# define MAP_SHARED 0 -# define MAP_FIXED 0 -#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) { @@ -749,6 +988,7 @@ return SvOK (cb_sv) ? s_get_cv_croak (cb_sv) : 0; } +ecb_noinline static aio_req ecb_noinline dreq (SV *callback) { @@ -780,8 +1020,10 @@ if (GIMME_V != G_VOID) \ XPUSHs (req_sv (req, aio_req_stash)); -ecb_inline void -req_set_path (aio_req req, SV *path, SV **wdsv, SV **pathsv, eio_wd *wd, void **ptr) +/* *wdsv, *pathsv, *wd and *ptr must be 0-initialized */ +ecb_inline +void +req_set_path (SV *path, SV **wdsv, SV **pathsv, eio_wd *wd, void **ptr) { if (expect_false (SvROK (path))) { @@ -816,13 +1058,15 @@ *ptr = SvPVbyte_nolen (*pathsv); } -static void ecb_noinline +ecb_noinline +static void req_set_path1 (aio_req req, SV *path) { - req_set_path (req, path, &req->sv1, &req->sv3, &req->wd, &req->ptr1); + req_set_path (path, &req->sv1, &req->sv3, &req->wd, &req->ptr1); } -static void ecb_noinline +ecb_noinline +static void req_set_fh_or_path (aio_req req, int type_path, int type_fh, SV *fh_or_path) { SV *rv = SvROK (fh_or_path) ? SvRV (fh_or_path) : fh_or_path; @@ -844,6 +1088,51 @@ } } +/*****************************************************************************/ + +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; +} + +/*****************************************************************************/ + +/* extract a ref-to-array of strings into a temporary c style string vector */ +static char ** +extract_stringvec (SV *sv, const char *croakmsg) +{ + if (!SvROK (sv) || SvTYPE (SvRV (sv)) != SVt_PVAV) + croak ("%s", croakmsg); + + AV *av = (AV *)SvRV (sv); + int i, nelem = av_len (av) + 1; + char **vecp = (char **)SvPVX (sv_2mortal (newSV (sizeof (char *) * (nelem + 1)))); + + for (i = 0; i < nelem; ++i) + { + SV **e = av_fetch (av, i, 0); + + if (e && *e) + vecp[i] = SvPVbyte_nolen (*e); + else + vecp[i] = ""; + } + + vecp[nelem] = 0; + + return vecp; +} + +/*****************************************************************************/ + XS(boot_IO__AIO) ecb_cold; MODULE = IO::AIO PACKAGE = IO::AIO @@ -860,12 +1149,32 @@ # define const_iv(name) { # name, (IV) name }, # define const_eio(name) { # name, (IV) EIO_ ## name }, - /* you have to re-run ./gendef0 after adding/Removing any constants here */ - + /* you have to re-run ./gendef0 after adding/removing any constants here */ + /* the first block can be undef if missing */ const_iv (ENOSYS) const_iv (EXDEV) const_iv (EBADR) + /* for lseek */ + const_iv (SEEK_DATA) + const_iv (SEEK_HOLE) + + const_niv (FADV_NORMAL , POSIX_FADV_NORMAL) + const_niv (FADV_SEQUENTIAL, POSIX_FADV_SEQUENTIAL) + const_niv (FADV_RANDOM , POSIX_FADV_RANDOM) + const_niv (FADV_NOREUSE , POSIX_FADV_NOREUSE) + const_niv (FADV_WILLNEED , POSIX_FADV_WILLNEED) + const_niv (FADV_DONTNEED , POSIX_FADV_DONTNEED) + + const_niv (MADV_NORMAL , POSIX_MADV_NORMAL) + const_niv (MADV_SEQUENTIAL, POSIX_MADV_SEQUENTIAL) + const_niv (MADV_RANDOM , POSIX_MADV_RANDOM) + const_niv (MADV_WILLNEED , POSIX_MADV_WILLNEED) + 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) @@ -888,6 +1197,8 @@ const_iv (O_DSYNC) const_iv (O_RSYNC) const_iv (O_SYNC) + const_iv (O_PATH) + const_iv (O_TMPFILE) const_iv (O_TTY_INIT) const_iv (S_IFIFO) @@ -900,19 +1211,6 @@ const_iv (S_IFSOCK) const_iv (S_IFMT) - const_niv (FADV_NORMAL , POSIX_FADV_NORMAL) - const_niv (FADV_SEQUENTIAL, POSIX_FADV_SEQUENTIAL) - const_niv (FADV_RANDOM , POSIX_FADV_RANDOM) - const_niv (FADV_NOREUSE , POSIX_FADV_NOREUSE) - const_niv (FADV_WILLNEED , POSIX_FADV_WILLNEED) - const_niv (FADV_DONTNEED , POSIX_FADV_DONTNEED) - - const_niv (MADV_NORMAL , POSIX_MADV_NORMAL) - const_niv (MADV_SEQUENTIAL, POSIX_MADV_SEQUENTIAL) - const_niv (MADV_RANDOM , POSIX_MADV_RANDOM) - const_niv (MADV_WILLNEED , POSIX_MADV_WILLNEED) - const_niv (MADV_DONTNEED , POSIX_MADV_DONTNEED) - const_iv (ST_RDONLY) const_iv (ST_NOSUID) const_iv (ST_NODEV) @@ -931,17 +1229,100 @@ const_iv (PROT_READ) const_iv (PROT_WRITE) - /*const_iv (MAP_FIXED)*/ const_iv (MAP_PRIVATE) const_iv (MAP_SHARED) + const_iv (MAP_FIXED) const_iv (MAP_ANONYMOUS) /* linuxish */ - const_iv (MAP_HUGETLB) const_iv (MAP_LOCKED) const_iv (MAP_NORESERVE) const_iv (MAP_POPULATE) const_iv (MAP_NONBLOCK) + const_iv (MAP_GROWSDOWN) + const_iv (MAP_32BIT) + const_iv (MAP_HUGETLB) + const_iv (MAP_STACK) + const_iv (MAP_FIXED_NOREPLACE) + const_iv (MAP_SHARED_VALIDATE) + const_iv (MAP_SYNC) + const_iv (MAP_UNINITIALIZED) + + const_iv (MREMAP_MAYMOVE) + const_iv (MREMAP_FIXED) + + const_iv (MSG_CMSG_CLOEXEC) + const_iv (SOCK_CLOEXEC) + + const_iv (F_DUPFD_CLOEXEC) + + const_iv (F_ADD_SEALS) + const_iv (F_GET_SEALS) + const_iv (F_SEAL_SEAL) + const_iv (F_SEAL_SHRINK) + const_iv (F_SEAL_GROW) + const_iv (F_SEAL_WRITE) + + const_iv (F_OFD_GETLK) + const_iv (F_OFD_SETLK) + const_iv (F_OFD_GETLKW) + + const_iv (FIFREEZE) + const_iv (FITHAW) + const_iv (FITRIM) + const_iv (FICLONE) + const_iv (FICLONERANGE) + const_iv (FIDEDUPERANGE) + + const_iv (FS_IOC_GETFLAGS) + const_iv (FS_IOC_SETFLAGS) + const_iv (FS_IOC_GETVERSION) + const_iv (FS_IOC_SETVERSION) + const_iv (FS_IOC_FIEMAP) + const_iv (FS_IOC_FSGETXATTR) + const_iv (FS_IOC_FSSETXATTR) + const_iv (FS_IOC_SET_ENCRYPTION_POLICY) + const_iv (FS_IOC_GET_ENCRYPTION_PWSALT) + const_iv (FS_IOC_GET_ENCRYPTION_POLICY) + + const_iv (FS_KEY_DESCRIPTOR_SIZE) + + const_iv (FS_SECRM_FL) + const_iv (FS_UNRM_FL) + const_iv (FS_COMPR_FL) + const_iv (FS_SYNC_FL) + const_iv (FS_IMMUTABLE_FL) + const_iv (FS_APPEND_FL) + const_iv (FS_NODUMP_FL) + const_iv (FS_NOATIME_FL) + const_iv (FS_DIRTY_FL) + const_iv (FS_COMPRBLK_FL) + const_iv (FS_NOCOMP_FL) + const_iv (FS_ENCRYPT_FL) + const_iv (FS_BTREE_FL) + const_iv (FS_INDEX_FL) + const_iv (FS_JOURNAL_DATA_FL) + const_iv (FS_NOTAIL_FL) + const_iv (FS_DIRSYNC_FL) + const_iv (FS_TOPDIR_FL) + const_iv (FS_FL_USER_MODIFIABLE) + + const_iv (FS_XFLAG_REALTIME) + const_iv (FS_XFLAG_PREALLOC) + const_iv (FS_XFLAG_IMMUTABLE) + const_iv (FS_XFLAG_APPEND) + const_iv (FS_XFLAG_SYNC) + const_iv (FS_XFLAG_NOATIME) + const_iv (FS_XFLAG_NODUMP) + const_iv (FS_XFLAG_RTINHERIT) + const_iv (FS_XFLAG_PROJINHERIT) + const_iv (FS_XFLAG_NOSYMLINKS) + const_iv (FS_XFLAG_EXTSIZE) + const_iv (FS_XFLAG_EXTSZINHERIT) + const_iv (FS_XFLAG_NODEFRAG) + const_iv (FS_XFLAG_FILESTREAM) + const_iv (FS_XFLAG_DAX) + const_iv (FS_XFLAG_HASATTR) const_iv (FIEMAP_FLAG_SYNC) const_iv (FIEMAP_FLAG_XATTR) @@ -963,16 +1344,163 @@ const_iv (SPLICE_F_MORE) const_iv (SPLICE_F_GIFT) - const_iv (SEEK_DATA) - const_iv (SEEK_HOLE) + const_iv (EFD_CLOEXEC) + const_iv (EFD_NONBLOCK) + const_iv (EFD_SEMAPHORE) + + const_iv (MFD_CLOEXEC) + const_iv (MFD_ALLOW_SEALING) + const_iv (MFD_HUGETLB) + const_iv (MFD_HUGETLB_2MB) + const_iv (MFD_HUGETLB_1GB) + + 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) + + const_iv (STATX_TYPE) + const_iv (STATX_MODE) + const_iv (STATX_NLINK) + const_iv (STATX_UID) + const_iv (STATX_GID) + const_iv (STATX_ATIME) + const_iv (STATX_MTIME) + const_iv (STATX_CTIME) + const_iv (STATX_INO) + const_iv (STATX_SIZE) + const_iv (STATX_BLOCKS) + const_iv (STATX_BASIC_STATS) + const_iv (STATX_ALL) + const_iv (STATX_BTIME) + const_iv (STATX_ATTR_COMPRESSED) + const_iv (STATX_ATTR_IMMUTABLE) + const_iv (STATX_ATTR_APPEND) + const_iv (STATX_ATTR_NODUMP) + const_iv (STATX_ATTR_ENCRYPTED) + const_iv (STATX_ATTR_AUTOMOUNT) + + const_iv (AT_FDCWD) + const_iv (AT_SYMLINK_NOFOLLOW) + const_iv (AT_EACCESS) + const_iv (AT_REMOVEDIR) + const_iv (AT_SYMLINK_FOLLOW) + const_iv (AT_NO_AUTOMOUNT) + const_iv (AT_EMPTY_PATH) + const_iv (AT_STATX_SYNC_TYPE) + const_iv (AT_STATX_AS_STAT) + const_iv (AT_STATX_FORCE_SYNC) + const_iv (AT_STATX_DONT_SYNC) + const_iv (AT_RECURSIVE) + + const_iv (OPEN_TREE_CLONE) + + const_iv (FSOPEN_CLOEXEC) + + const_iv (FSPICK_CLOEXEC) + const_iv (FSPICK_SYMLINK_NOFOLLOW) + const_iv (FSPICK_NO_AUTOMOUNT) + const_iv (FSPICK_EMPTY_PATH) + + const_iv (MOVE_MOUNT_F_SYMLINKS) + const_iv (MOVE_MOUNT_F_AUTOMOUNTS) + const_iv (MOVE_MOUNT_F_EMPTY_PATH) + const_iv (MOVE_MOUNT_T_SYMLINKS) + const_iv (MOVE_MOUNT_T_AUTOMOUNTS) + const_iv (MOVE_MOUNT_T_EMPTY_PATH) + + /* waitid */ + const_iv (P_PID) + const_iv (P_PIDFD) + const_iv (P_PGID) + const_iv (P_ALL) + + const_iv (FSCONFIG_SET_FLAG) + const_iv (FSCONFIG_SET_STRING) + const_iv (FSCONFIG_SET_BINARY) + const_iv (FSCONFIG_SET_PATH) + const_iv (FSCONFIG_SET_PATH_EMPTY) + const_iv (FSCONFIG_SET_FD) + const_iv (FSCONFIG_CMD_CREATE) + const_iv (FSCONFIG_CMD_RECONFIGURE) + + const_iv (MOUNT_ATTR_RDONLY) + const_iv (MOUNT_ATTR_NOSUID) + const_iv (MOUNT_ATTR_NODEV) + const_iv (MOUNT_ATTR_NOEXEC) + const_iv (MOUNT_ATTR__ATIME) + const_iv (MOUNT_ATTR_RELATIME) + const_iv (MOUNT_ATTR_NOATIME) + const_iv (MOUNT_ATTR_STRICTATIME) + const_iv (MOUNT_ATTR_NODIRATIME) + + /* sys/mount.h */ + const_iv (MS_RDONLY) + const_iv (MS_NOSUID) + const_iv (MS_NODEV) + const_iv (MS_NOEXEC) + const_iv (MS_SYNCHRONOUS) + const_iv (MS_REMOUNT) + const_iv (MS_MANDLOCK) + const_iv (MS_DIRSYNC) + const_iv (MS_NOATIME) + const_iv (MS_NODIRATIME) + const_iv (MS_BIND) + const_iv (MS_MOVE) + const_iv (MS_REC) + const_iv (MS_SILENT) + const_iv (MS_POSIXACL) + const_iv (MS_UNBINDABLE) + const_iv (MS_PRIVATE) + const_iv (MS_SLAVE) + const_iv (MS_SHARED) + const_iv (MS_RELATIME) + const_iv (MS_KERNMOUNT) + const_iv (MS_I_VERSION) + const_iv (MS_STRICTATIME) + const_iv (MS_LAZYTIME) + const_iv (MS_ACTIVE) + const_iv (MS_NOUSER) + const_iv (MS_RMT_MASK) + const_iv (MS_MGC_VAL) + const_iv (MS_MGC_MSK) + + const_iv (MNT_FORCE) + const_iv (MNT_DETACH) + const_iv (MNT_EXPIRE) + const_iv (UMOUNT_NOFOLLOW) + + const_iv (BLKROSET) + const_iv (BLKROGET) + const_iv (BLKRRPART) + const_iv (BLKGETSIZE) + const_iv (BLKFLSBUF) + const_iv (BLKRASET) + const_iv (BLKRAGET) + const_iv (BLKFRASET) + const_iv (BLKFRAGET) + const_iv (BLKSECTSET) + const_iv (BLKSECTGET) + const_iv (BLKSSZGET) + const_iv (BLKBSZGET) + const_iv (BLKBSZSET) + const_iv (BLKGETSIZE64) - /* libeio constants */ + /* these are libeio constants, and are independent of gendef0 */ const_eio (SEEK_SET) const_eio (SEEK_CUR) const_eio (SEEK_END) const_eio (MCL_FUTURE) const_eio (MCL_CURRENT) + const_eio (MCL_ONFAULT) const_eio (MS_ASYNC) const_eio (MS_INVALIDATE) @@ -985,6 +1513,15 @@ const_eio (SYNC_FILE_RANGE_WAIT_AFTER) const_eio (FALLOC_FL_KEEP_SIZE) + const_eio (FALLOC_FL_PUNCH_HOLE) + const_eio (FALLOC_FL_COLLAPSE_RANGE) + const_eio (FALLOC_FL_ZERO_RANGE) + const_eio (FALLOC_FL_INSERT_RANGE) + const_eio (FALLOC_FL_UNSHARE_RANGE) + + const_eio (RENAME_NOREPLACE) + const_eio (RENAME_EXCHANGE) + const_eio (RENAME_WHITEOUT) const_eio (READDIR_DENTS) const_eio (READDIR_DIRS_FIRST) @@ -1012,6 +1549,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 (); } @@ -1062,7 +1617,7 @@ max_outstanding = maxreqs; void -aio_wd (SV8 *pathname, SV *callback=&PL_sv_undef) +aio_wd (SV8 *pathname, SV *callback = &PL_sv_undef) PPCODE: { dREQ; @@ -1074,7 +1629,7 @@ } void -aio_open (SV8 *pathname, int flags, int mode, SV *callback=&PL_sv_undef) +aio_open (SV8 *pathname, int flags, int mode, SV *callback = &PL_sv_undef) PPCODE: { dREQ; @@ -1088,14 +1643,14 @@ } void -aio_fsync (SV *fh, SV *callback=&PL_sv_undef) +aio_fsync (SV *fh, SV *callback = &PL_sv_undef) ALIAS: aio_fsync = EIO_FSYNC aio_fdatasync = EIO_FDATASYNC aio_syncfs = EIO_SYNCFS PPCODE: { - int fd = s_fileno_croak (fh, 0); + int fd = s_fileno_croak (fh, 0); dREQ; req->type = ix; @@ -1106,10 +1661,10 @@ } void -aio_sync_file_range (SV *fh, off_t offset, size_t nbytes, UV flags, SV *callback=&PL_sv_undef) +aio_sync_file_range (SV *fh, off_t offset, size_t nbytes, UV flags, SV *callback = &PL_sv_undef) PPCODE: { - int fd = s_fileno_croak (fh, 0); + int fd = s_fileno_croak (fh, 0); dREQ; req->type = EIO_SYNC_FILE_RANGE; @@ -1123,10 +1678,10 @@ } void -aio_fallocate (SV *fh, int mode, off_t offset, size_t len, SV *callback=&PL_sv_undef) +aio_allocate (SV *fh, int mode, off_t offset, size_t len, SV *callback = &PL_sv_undef) PPCODE: { - int fd = s_fileno_croak (fh, 0); + int fd = s_fileno_croak (fh, 0); dREQ; req->type = EIO_FALLOCATE; @@ -1140,41 +1695,46 @@ } void -aio_close (SV *fh, SV *callback=&PL_sv_undef) +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); + int fd = s_fileno_croak (fh, 0); dREQ; +#if 0 + /* partially duplicate logic in s_fileno */ + SvGETMAGIC (fh); - if (expect_false (close_fd < 0)) - { - int pipefd [2]; + if (SvROK (fh)) + { + fh = SvRV (fh); + SvGETMAGIC (fh); + } - if ( -#ifdef _WIN32 - _pipe (pipefd, 1, _O_BINARY) < 0 -#else - pipe (pipefd) < 0 - || fcntl (pipefd [0], F_SETFD, FD_CLOEXEC) < 0 + if (SvTYPE (fh) == SVt_PVGV) + { + /* perl filehandle */ + PerlIOUnix_refcnt_inc (fd); + do_close ((GV *)fh, 1); + + req->type = EIO_CLOSE; + req->int1 = fd; + /*req->sv2 = newSVsv (fh);*/ /* since we stole the fd, no need to keep the fh */ + } + else #endif - || close (pipefd [1]) < 0 - ) - abort (); /*D*/ - - close_fd = pipefd [0]; + { + /* fd number */ + req->type = EIO_DUP2; + req->int1 = close_fd; + req->sv2 = newSVsv (fh); + req->int2 = fd; } - req->type = EIO_DUP2; - req->int1 = close_fd; - req->sv2 = newSVsv (fh); - req->int2 = fd; - REQ_SEND; } void -aio_seek (SV *fh, SV *offset, int whence, SV *callback=&PL_sv_undef) +aio_seek (SV *fh, SV *offset, int whence, SV *callback = &PL_sv_undef) PPCODE: { int fd = s_fileno_croak (fh, 0); @@ -1190,7 +1750,7 @@ } void -aio_read (SV *fh, SV *offset, SV *length, SV8 *data, IV dataoffset, SV *callback=&PL_sv_undef) +aio_read (SV *fh, SV *offset, SV *length, SV8 *data, IV dataoffset, SV *callback = &PL_sv_undef) ALIAS: aio_read = EIO_READ aio_write = EIO_WRITE @@ -1216,9 +1776,8 @@ else { /* read: check type and grow scalar as necessary */ - SvUPGRADE (data, SVt_PV); - if (SvLEN (data) >= SvCUR (data)) - svptr = SvGROW (data, len + dataoffset + 1); + if (!SvPOK (data) || SvLEN (data) >= SvCUR (data)) + svptr = sv_grow (data, len + dataoffset + 1); else if (SvCUR (data) < len + dataoffset) croak ("length + dataoffset outside of scalar, and cannot grow"); } @@ -1246,7 +1805,47 @@ } void -aio_readlink (SV8 *pathname, SV *callback=&PL_sv_undef) +aio_ioctl (SV *fh, unsigned long request, SV8 *arg, SV *callback = &PL_sv_undef) + ALIAS: + aio_ioctl = EIO_IOCTL + aio_fcntl = EIO_FCNTL + PPCODE: +{ + int fd = s_fileno_croak (fh, 0); + char *svptr; + + if (SvPOK (arg) || !SvNIOK (arg)) + { + STRLEN svlen; + /* perl uses IOCPARM_LEN for fcntl, so we do, too */ +#ifdef IOCPARM_LEN + STRLEN need = IOCPARM_LEN (request); +#else + STRLEN need = 256; +#endif + + if (svlen < need) + svptr = SvGROW (arg, need); + } + else + svptr = (char *)SvIV (arg); + + { + dREQ; + + req->type = ix; + req->sv1 = newSVsv (fh); + req->int1 = fd; + req->int2 = (long)request; + req->sv2 = SvREFCNT_inc (arg); + req->ptr2 = svptr; + + REQ_SEND; + } +} + +void +aio_readlink (SV8 *pathname, SV *callback = &PL_sv_undef) ALIAS: aio_readlink = EIO_READLINK aio_realpath = EIO_REALPATH @@ -1261,11 +1860,11 @@ } void -aio_sendfile (SV *out_fh, SV *in_fh, off_t in_offset, size_t length, SV *callback=&PL_sv_undef) +aio_sendfile (SV *out_fh, SV *in_fh, off_t in_offset, size_t length, SV *callback = &PL_sv_undef) PPCODE: { - int ifd = s_fileno_croak (in_fh , 0); - int ofd = s_fileno_croak (out_fh, 1); + int ifd = s_fileno_croak (in_fh , 0); + int ofd = s_fileno_croak (out_fh, 1); dREQ; req->type = EIO_SENDFILE; @@ -1280,10 +1879,10 @@ } void -aio_readahead (SV *fh, off_t offset, size_t length, SV *callback=&PL_sv_undef) +aio_readahead (SV *fh, off_t offset, size_t length, SV *callback = &PL_sv_undef) PPCODE: { - int fd = s_fileno_croak (fh, 0); + int fd = s_fileno_croak (fh, 0); dREQ; req->type = EIO_READAHEAD; @@ -1296,7 +1895,7 @@ } void -aio_stat (SV8 *fh_or_path, SV *callback=&PL_sv_undef) +aio_stat (SV8 *fh_or_path, SV *callback = &PL_sv_undef) ALIAS: aio_stat = EIO_STAT aio_lstat = EIO_LSTAT @@ -1310,12 +1909,46 @@ 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: minor = 1 CODE: - RETVAL = ix ? major (dev) : minor (dev); + RETVAL = ix ? minor (dev) : major (dev); OUTPUT: RETVAL @@ -1327,7 +1960,7 @@ RETVAL void -aio_utime (SV8 *fh_or_path, SV *atime, SV *mtime, SV *callback=&PL_sv_undef) +aio_utime (SV8 *fh_or_path, SV *atime, SV *mtime, SV *callback = &PL_sv_undef) PPCODE: { dREQ; @@ -1340,7 +1973,7 @@ } void -aio_truncate (SV8 *fh_or_path, SV *offset, SV *callback=&PL_sv_undef) +aio_truncate (SV8 *fh_or_path, SV *offset, SV *callback = &PL_sv_undef) PPCODE: { dREQ; @@ -1352,7 +1985,7 @@ } void -aio_chmod (SV8 *fh_or_path, int mode, SV *callback=&PL_sv_undef) +aio_chmod (SV8 *fh_or_path, int mode, SV *callback = &PL_sv_undef) PPCODE: { dREQ; @@ -1364,7 +1997,7 @@ } void -aio_chown (SV8 *fh_or_path, SV *uid, SV *gid, SV *callback=&PL_sv_undef) +aio_chown (SV8 *fh_or_path, SV *uid, SV *gid, SV *callback = &PL_sv_undef) PPCODE: { dREQ; @@ -1377,7 +2010,7 @@ } void -aio_readdirx (SV8 *pathname, IV flags, SV *callback=&PL_sv_undef) +aio_readdirx (SV8 *pathname, IV flags, SV *callback = &PL_sv_undef) PPCODE: { dREQ; @@ -1394,7 +2027,7 @@ } void -aio_mkdir (SV8 *pathname, int mode, SV *callback=&PL_sv_undef) +aio_mkdir (SV8 *pathname, int mode, SV *callback = &PL_sv_undef) PPCODE: { dREQ; @@ -1407,7 +2040,7 @@ } void -aio_unlink (SV8 *pathname, SV *callback=&PL_sv_undef) +aio_unlink (SV8 *pathname, SV *callback = &PL_sv_undef) ALIAS: aio_unlink = EIO_UNLINK aio_rmdir = EIO_RMDIR @@ -1423,7 +2056,7 @@ } void -aio_link (SV8 *oldpath, SV8 *newpath, SV *callback=&PL_sv_undef) +aio_link (SV8 *oldpath, SV8 *newpath, SV *callback = &PL_sv_undef) ALIAS: aio_link = EIO_LINK aio_symlink = EIO_SYMLINK @@ -1435,14 +2068,30 @@ req->type = ix; req_set_path1 (req, oldpath); - req_set_path (req, newpath, &req->sv2, &req->sv4, &wd2, &req->ptr2); + req_set_path (newpath, &req->sv2, &req->sv4, &wd2, &req->ptr2); req->int3 = (long)wd2; REQ_SEND; } void -aio_mknod (SV8 *pathname, int mode, UV dev, SV *callback=&PL_sv_undef) +aio_rename2 (SV8 *oldpath, SV8 *newpath, int flags = 0, SV *callback = &PL_sv_undef) + PPCODE: +{ + eio_wd wd2 = 0; + dREQ; + + req->type = EIO_RENAME; + req_set_path1 (req, oldpath); + req_set_path (newpath, &req->sv2, &req->sv4, &wd2, &req->ptr2); + req->int2 = flags; + req->int3 = (long)wd2; + + REQ_SEND; +} + +void +aio_mknod (SV8 *pathname, int mode, UV dev, SV *callback = &PL_sv_undef) PPCODE: { dREQ; @@ -1456,7 +2105,7 @@ } void -aio_mtouch (SV8 *data, IV offset = 0, SV *length = &PL_sv_undef, int flags = 0, SV *callback=&PL_sv_undef) +aio_mtouch (SV8 *data, IV offset = 0, SV *length = &PL_sv_undef, int flags = -1, SV *callback = &PL_sv_undef) ALIAS: aio_mtouch = EIO_MTOUCH aio_msync = EIO_MSYNC @@ -1466,6 +2115,9 @@ char *svptr = SvPVbyte (data, svlen); UV len = SvUV (length); + if (flags < 0) + flags = ix == EIO_MSYNC ? EIO_MS_SYNC : 0; + if (offset < 0) offset += svlen; @@ -1489,7 +2141,7 @@ } void -aio_mlock (SV8 *data, IV offset = 0, SV *length = &PL_sv_undef, SV *callback=&PL_sv_undef) +aio_mlock (SV8 *data, IV offset = 0, SV *length = &PL_sv_undef, SV *callback = &PL_sv_undef) PPCODE: { STRLEN svlen; @@ -1518,7 +2170,7 @@ } void -aio_mlockall (IV flags, SV *callback=&PL_sv_undef) +aio_mlockall (IV flags, SV *callback = &PL_sv_undef) PPCODE: { dREQ; @@ -1530,10 +2182,10 @@ } void -aio_fiemap (SV *fh, off_t start, SV *length, U32 flags, SV *count, SV *callback=&PL_sv_undef) +aio_fiemap (SV *fh, off_t start, SV *length, U32 flags, SV *count, SV *callback = &PL_sv_undef) PPCODE: { - int fd = s_fileno_croak (fh, 0); + int fd = s_fileno_croak (fh, 0); dREQ; req->type = EIO_CUSTOM; @@ -1553,7 +2205,45 @@ } void -aio_busy (double delay, SV *callback=&PL_sv_undef) +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: { dREQ; @@ -1565,19 +2255,22 @@ } void -aio_group (SV *callback=&PL_sv_undef) +aio_group (SV *callback = &PL_sv_undef) PPCODE: { dREQ; req->type = EIO_GROUP; + PUTBACK; req_submit (req); + SPAGAIN; + XPUSHs (req_sv (req, aio_grp_stash)); } void -aio_nop (SV *callback=&PL_sv_undef) +aio_nop (SV *callback = &PL_sv_undef) ALIAS: aio_nop = EIO_NOP aio_sync = EIO_SYNC @@ -1591,7 +2284,7 @@ } int -aioreq_pri (int pri = 0) +aioreq_pri (int pri = NO_INIT) CODE: RETVAL = next_pri; if (items > 0) @@ -1691,51 +2384,67 @@ RETVAL void -mmap (SV *scalar, size_t length, int prot, int flags, SV *fh = &PL_sv_undef, off_t offset = 0) +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, off_t 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: { STRLEN svlen; void *addr = SvPVbyte (scalar, svlen); - size_t len = SvUV (length); + STRLEN len = SvUV (length); if (offset < 0) offset += svlen; @@ -1759,11 +2468,11 @@ RETVAL int -munlock (SV *scalar, off_t offset = 0, SV *length = &PL_sv_undef) +munlock (SV *scalar, IV offset = 0, SV *length = &PL_sv_undef) CODE: { STRLEN svlen; - void *addr = SvPVbyte (scalar, svlen); + void *addr = SvPVbyte (scalar, svlen); size_t len = SvUV (length); if (offset < 0) @@ -1787,6 +2496,14 @@ RETVAL int +mlockall (int flags) + PROTOTYPE: $; + CODE: + RETVAL = eio_mlockall_sync (flags); + OUTPUT: + RETVAL + +int munlockall () CODE: #if _POSIX_MEMLOCK @@ -1798,6 +2515,184 @@ RETVAL int +statx (SV8 *pathname, int flags, UV mask) + CODE: +{ + /* undocumented, and might go away, and anyway, should use eio_statx */ + SV *wdsv = 0; + SV *pathsv = 0; + eio_wd wd = EIO_CWD; + void *ptr; + int res; + + req_set_path (pathname, &wdsv, &pathsv, &wd, &ptr); + RETVAL = eio__statx (!wd || wd->fd == EIO_CWD ? AT_FDCWD : wd->fd, ptr, flags, mask & STATX_ALL, &stx); + + SvREFCNT_dec (pathsv); + SvREFCNT_dec (wdsv); +} + OUTPUT: + RETVAL + +U32 +stx_mode () + PROTOTYPE: + CODE: +#if HAVE_STATX + RETVAL = stx.stx_mode; +#else + XSRETURN_UNDEF; +#endif + OUTPUT: + RETVAL + +#define STATX_OFFSET_mask statx_offsetof (stx_mask) +#define STATX_OFFSET_blksize statx_offsetof (stx_blksize) +#define STATX_OFFSET_nlink statx_offsetof (stx_nlink) +#define STATX_OFFSET_uid statx_offsetof (stx_uid) +#define STATX_OFFSET_gid statx_offsetof (stx_gid) +#define STATX_OFFSET_rdev_major statx_offsetof (stx_rdev_major) +#define STATX_OFFSET_rdev_minor statx_offsetof (stx_rdev_minor) +#define STATX_OFFSET_dev_major statx_offsetof (stx_dev_major) +#define STATX_OFFSET_dev_minor statx_offsetof (stx_dev_minor) +#define STATX_OFFSET_attributes statx_offsetof (stx_attributes) +#define STATX_OFFSET_ino statx_offsetof (stx_ino) +#define STATX_OFFSET_size statx_offsetof (stx_size) +#define STATX_OFFSET_blocks statx_offsetof (stx_blocks) +#define STATX_OFFSET_attributes_mask statx_offsetof (stx_attributes_mask) +#define STATX_OFFSET_atime statx_offsetof (stx_atime) +#define STATX_OFFSET_btime statx_offsetof (stx_btime) +#define STATX_OFFSET_ctime statx_offsetof (stx_ctime) +#define STATX_OFFSET_mtime statx_offsetof (stx_mtime) + +U32 +stx_mask () + PROTOTYPE: + ALIAS: + stx_mask = STATX_OFFSET_mask + stx_blksize = STATX_OFFSET_blksize + stx_nlink = STATX_OFFSET_nlink + stx_uid = STATX_OFFSET_uid + stx_gid = STATX_OFFSET_gid + stx_rdev_major = STATX_OFFSET_rdev_major + stx_rdev_minor = STATX_OFFSET_rdev_minor + stx_dev_major = STATX_OFFSET_dev_major + stx_dev_minor = STATX_OFFSET_dev_minor + CODE: +#if HAVE_STATX + RETVAL = *(__u32 *)((char *)&stx + ix); +#else + XSRETURN_UNDEF; +#endif + OUTPUT: + RETVAL + +VAL64 +stx_attributes () + PROTOTYPE: + ALIAS: + stx_attributes = STATX_OFFSET_attributes + stx_ino = STATX_OFFSET_ino + stx_size = STATX_OFFSET_size + stx_blocks = STATX_OFFSET_blocks + stx_attributes_mask = STATX_OFFSET_attributes_mask + CODE: +#if HAVE_STATX + RETVAL = *(__u64 *)((char *)&stx + ix); +#else + XSRETURN_UNDEF; +#endif + OUTPUT: + RETVAL + +NV +stx_atime () + PROTOTYPE: + ALIAS: + stx_atime = STATX_OFFSET_atime + stx_btime = STATX_OFFSET_btime + stx_ctime = STATX_OFFSET_ctime + stx_mtime = STATX_OFFSET_mtime + CODE: +#if HAVE_STATX + struct statx_timestamp *ts = (struct statx_timestamp *)((char *)&stx + ix); + RETVAL = ts->tv_sec + ts->tv_nsec * 1e-9; +#else + XSRETURN_UNDEF; +#endif + OUTPUT: + RETVAL + +VAL64 +stx_atimesec () + PROTOTYPE: + ALIAS: + stx_atimesec = STATX_OFFSET_atime + stx_btimesec = STATX_OFFSET_btime + stx_ctimesec = STATX_OFFSET_ctime + stx_mtimesec = STATX_OFFSET_mtime + CODE: +#if HAVE_STATX + struct statx_timestamp *ts = (struct statx_timestamp *)((char *)&stx + ix); + RETVAL = ts->tv_sec; +#else + XSRETURN_UNDEF; +#endif + OUTPUT: + RETVAL + +U32 +stx_atimensec () + PROTOTYPE: + ALIAS: + stx_atimensec = STATX_OFFSET_atime + stx_btimensec = STATX_OFFSET_btime + stx_ctimensec = STATX_OFFSET_ctime + stx_mtimensec = STATX_OFFSET_mtime + CODE: +#if HAVE_STATX + struct statx_timestamp *ts = (struct statx_timestamp *)((char *)&stx + ix); + RETVAL = ts->tv_nsec; +#else + RETVAL = 0; +#endif + OUTPUT: + RETVAL + +void +accept4 (aio_rfd rfh, SV *sockaddr, int salen, int flags) + PPCODE: +{ + SV *retval; +#if HAVE_ACCEPT4 + socklen_t salen_ = salen ? salen + 1 : 0; + + if (salen) + { + sv_upgrade (sockaddr, SVt_PV); + sv_grow (sockaddr, salen_); + } + + int res = accept4 (rfh, salen ? (struct sockaddr *)SvPVX (sockaddr) : 0, salen ? &salen_ : 0, flags); + + retval = newmortalFH (res, O_RDWR); + + if (res >= 0 && salen > 0) + { + if (salen_ > salen + 1) + salen_ = salen + 1; + + SvPOK_only (sockaddr); + SvCUR_set (sockaddr, salen_); + } +#else + errno = ENOSYS; + retval = &PL_sv_undef; +#endif + XPUSHs (retval); +} + +ssize_t splice (aio_rfd rfh, SV *off_in, aio_wfd wfh, SV *off_out, size_t length, unsigned int flags) CODE: { @@ -1815,7 +2710,7 @@ OUTPUT: RETVAL -int +ssize_t tee (aio_rfd rfh, aio_wfd wfh, size_t length, unsigned int flags) CODE: #if HAVE_LINUX_SPLICE @@ -1826,6 +2721,307 @@ OUTPUT: RETVAL +int +pipesize (aio_rfd rfh, int new_size = -1) + PROTOTYPE: $;$ + CODE: +#if defined(F_SETPIPE_SZ) && defined(F_GETPIPE_SZ) + if (new_size >= 0) + RETVAL = fcntl (rfh, F_SETPIPE_SZ, new_size); + else + RETVAL = fcntl (rfh, F_GETPIPE_SZ); +#else + errno = ENOSYS; + RETVAL = -1; +#endif + OUTPUT: + RETVAL + +void +pipe2 (int flags = 0) + PROTOTYPE: ;$ + PPCODE: +{ + int fd[2]; + int res; + + if (flags) +#if HAVE_PIPE2 + res = pipe2 (fd, flags); +#else + res = (errno = ENOSYS, -1); +#endif + else + res = pipe (fd); + + if (!res) + { + EXTEND (SP, 2); + PUSHs (newmortalFH (fd[0], O_RDONLY)); + PUSHs (newmortalFH (fd[1], O_WRONLY)); + } +} + +void +pidfd_open (int pid, unsigned int flags = 0) + PPCODE: +{ + /*GENDEF0_SYSCALL(pidfd_open,434)*/ + int fd = syscall (SYS_pidfd_open, pid, flags); + XPUSHs (newmortalFH (fd, O_RDWR)); +} + +int +pidfd_send_signal (SV *pidfh, int sig, SV *siginfo = &PL_sv_undef, unsigned int flags = 0) + PPCODE: +{ + int res; +#if HAVE_SIGINFO_T + siginfo_t si = { 0 }; + + if (SvOK (siginfo)) + { + HV *hv; + SV **svp; + + if (!SvROK (siginfo) || SvTYPE (SvRV (siginfo)) != SVt_PVHV) + croak ("siginfo argument must be a hashref with 'code', 'pid', 'uid' and 'value_int' or 'value_ptr' members, caught"); + + hv = (HV *)SvRV (siginfo); + + if ((svp = hv_fetchs (hv, "code" , 0))) si.si_code = SvIV (*svp); + if ((svp = hv_fetchs (hv, "pid" , 0))) si.si_pid = SvIV (*svp); + if ((svp = hv_fetchs (hv, "uid" , 0))) si.si_uid = SvIV (*svp); + if ((svp = hv_fetchs (hv, "value_int", 0))) si.si_value.sival_int = SvIV (*svp); + if ((svp = hv_fetchs (hv, "value_ptr", 0))) si.si_value.sival_ptr = (void *)SvIV (*svp); + } + + /*GENDEF0_SYSCALL(pidfd_send_signal,424)*/ + res = syscall (SYS_pidfd_send_signal, s_fileno_croak (pidfh, 0), sig, SvOK (siginfo) ? &si : 0, flags); +#else + res = (errno = ENOSYS, -1); +#endif + + XPUSHs (sv_2mortal (newSViv (res))); +} + +void +pidfd_getfd (SV *pidfh, int targetfd, unsigned int flags = 0) + PPCODE: +{ + /*GENDEF0_SYSCALL(pidfd_getfd,438)*/ + int fd = syscall (SYS_pidfd_getfd, s_fileno_croak (pidfh, 0), targetfd, flags); + XPUSHs (newmortalFH (fd, O_RDWR)); +} + +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 +} + +void +memfd_create (octet_string pathname, int flags = 0) + PPCODE: +{ + int fd; +#if HAVE_MEMFD_CREATE + fd = memfd_create (pathname, flags); +#else + fd = (errno = ENOSYS, -1); +#endif + + XPUSHs (newmortalFH (fd, O_RDWR)); +} + +int +fexecve (SV *fh, SV *args, SV *envs = &PL_sv_undef) + CODE: +{ + int fd = PerlIO_fileno (IoIFP (sv_2io (fh))); + char **envp, **argv; + argv = extract_stringvec (args, "IO::AIO::fexecve: args must be an array of strings"); + if (!SvOK (envs)) + { + extern char **environ; + envp = environ; + } + else + envp = extract_stringvec (envs, "IO::AIO::fexecve: envs must be an array of strings"); +#if _POSIX_VERSION >= 200809L + RETVAL = fexecve (fd, argv, envp); +#else + RETVAL = (errno = ENOSYS, -1); +#endif +} + OUTPUT: RETVAL + +int +mount (octet_string special, octet_string path, octet_string fstype, UV flags = 0, octet_string_ornull data = 0) + CODE: +#if HAVE_MOUNT + RETVAL = mount (special, path, fstype, flags, data); +#else + RETVAL = (errno = ENOSYS, -1); +#endif + OUTPUT: RETVAL + +int +umount (octet_string path, int flags = 0) + CODE: + if (flags) +#if HAVE_UMOUNT2 + RETVAL = umount2 (path, flags); +#else + RETVAL = (errno = ENOSYS, -1); +#endif + else +#if HAVE_MOUNT + RETVAL = umount (path); +#else + RETVAL = (errno = ENOSYS, -1); +#endif + OUTPUT: RETVAL + +UV +get_fdlimit () + CODE: +#if HAVE_RLIMITS + struct rlimit rl; + if (0 == getrlimit (RLIMIT_NOFILE, &rl)) + XSRETURN_UV (rl.rlim_cur == RLIM_INFINITY ? (UV)-1 : rl.rlim_cur); +#endif + XSRETURN_UNDEF; + OUTPUT: + RETVAL + +void +min_fdlimit (UV limit = 0x7fffffffU) + CODE: +{ +#if HAVE_RLIMITS + struct rlimit rl; + rlim_t orig_rlim_max; + UV bit; + + if (0 != getrlimit (RLIMIT_NOFILE, &rl)) + goto fail; + + if (rl.rlim_cur == RLIM_INFINITY) + XSRETURN_YES; + + orig_rlim_max = rl.rlim_max == RLIM_INFINITY ? ((rlim_t)0)-1 : rl.rlim_max; + + if (rl.rlim_cur < limit) + { + rl.rlim_cur = limit; + + if (rl.rlim_max < rl.rlim_cur && rl.rlim_max != RLIM_INFINITY) + rl.rlim_max = rl.rlim_cur; + } + + if (0 == setrlimit (RLIMIT_NOFILE, &rl)) + XSRETURN_YES; + + if (errno == EPERM) + { + /* setrlimit failed with EPERM - maybe we can't raise the hardlimit, or maybe */ + /* our limit overflows a system-wide limit */ + /* try an adaptive algorithm, but do not lower the hardlimit */ + rl.rlim_max = 0; + for (bit = 0x40000000U; bit; bit >>= 1) + { + rl.rlim_max |= bit; + rl.rlim_cur = rl.rlim_max; + + /* never decrease the hard limit */ + if (rl.rlim_max < orig_rlim_max) + break; + + if (0 != setrlimit (RLIMIT_NOFILE, &rl)) + rl.rlim_max &= ~bit; /* too high, remove bit again */ + } + + /* now, raise the soft limit to the max permitted */ + if (0 == getrlimit (RLIMIT_NOFILE, &rl)) + { + rl.rlim_cur = rl.rlim_max; + if (0 == setrlimit (RLIMIT_NOFILE, &rl)) + errno = EPERM; + } + } +#endif + fail: + XSRETURN_UNDEF; +} + void _on_next_submit (SV *cb) CODE: SvREFCNT_dec (on_next_submit); @@ -1869,7 +3065,7 @@ eio_cancel (req); void -cb (aio_req_ornot req, SV *callback=&PL_sv_undef) +cb (aio_req_ornot req, SV *callback = NO_INIT) PPCODE: { if (GIMME_V != G_VOID) @@ -1877,7 +3073,7 @@ if (items > 1) { - SV *cb_cv =get_cb (callback); + SV *cb_cv = get_cb (callback); SvREFCNT_dec (req->callback); req->callback = SvREFCNT_inc (cb_cv); @@ -1944,7 +3140,7 @@ eio_grp_limit (grp, limit); void -feed (aio_req grp, SV *callback=&PL_sv_undef) +feed (aio_req grp, SV *callback = &PL_sv_undef) CODE: { SvREFCNT_dec (grp->sv2);