about summary refs log tree commit homepage
path: root/lib/PublicInbox/Syscall.pm
DateCommit message (Collapse)
2024-04-08syscall: set default constants for Inline::C platforms
This fixes compile errors on platforms we can't explicitly support from pure Perl due to the lack of syscall stability guarantees by the OS developers. Reported-by: Gaelan Steele <gbs@canishe.com> Tested-by: Gaelan Steele <gbs@canishe.com>
2024-01-30syscall: use pure Perl sendmsg/recvmsg on *BSD
While syscall symbols (e.g. SYS_*) have changed on us in FreeBSD during the history of Sys::Syscall and this project and did bite us in some cases; the actual numbers don't get recycled for new syscalls. We're also fortunate that sendmsg and recvmsg syscalls and associated msghdr and cmsg structs predate the BSD forks and are compatible across all the BSDs I've tried. OpenBSD routes Perl `syscall' through libc; while NetBSD + FreeBSD document procedures for maintaining backwards compatibility. It looks like Dragonfly follows FreeBSD, here. Tested on i386 OpenBSD, and amd64 {Free,Net,Open,Dragonfly}BSD This enables *BSD users to use lei, -cindex and future SCM_RIGHTS-only features without needing Inline::C. [1] https://cvsweb.openbsd.org/src/gnu/usr.bin/perl/gen_syscall_emulator.pl [2] https://www.netbsd.org/docs/internals/en/chap-processes.html#syscall_versioning [3] https://wiki.freebsd.org/AddingSyscalls#Backward_compatibily
2024-01-30syscall: update formatting to match our codebase
Sys::Syscall needs separate patches anyways (if it ever gets updated), and having a mix of indentation styles in our codebase gets confusing. We'll also update cfarm-related comments for the current URL.
2023-12-29pure Perl inotify support
This is a step towards improving the out-of-the-box experience in achieving notifications without XS, extra downloads, and .so loading + runtime mmap overhead. This also fixes loongarch support of all Linux syscalls due to a bad regexp :x All the reachable Linux architectures listed at <https://portal.cfarm.net/machines/list/> should be supported. At the moment, there appears to be no reachable sparc* Linux machines available to cfarm users. Fixes: b0e5093aa3572a86 (syscall: add support for riscv64, 2022-08-11)
2023-10-18syscall: common $F_SETPIPE_SZ definition
We use this in various places to minimize or maximize pipe size on Linux. So keep it all in one place.
2023-10-06ipc: lower-level send_cmd/recv_cmd handle EINTR directly
This ensures script/lei $send_cmd usage is EINTR-safe (since I prefer to avoid loading PublicInbox::IPC for startup time). Overall, it saves us some code, too.
2023-09-24syscall: fix valgrind error in pure Perl send_cmd4
We need to allocate CMSG_SPACE for the `struct cmsghdr', not the smaller CMSG_LEN. AFAIK this isn't a real world problem since the Linux kernel doesn't care about the uninitialized space as long as memory region belongs to the user, but valgrind complains.
2023-09-24syscall: have `vec' operate on bytes directly
Instead of converting to bytes to bits and asking `vec' to operate on single bits, we can just have `vec' work on 8 bits at-a-time. This also fixes an overallocation in pure Perl Linux recv_cmd4. Adding an extra byte ourselves for "\0" isn't necessary: Perl already does it internally everywhere when creating/resizing scalars.
2023-09-24ipc: recv_cmd4 clobbers destination buffer on errors
Handling this should be done at the lowest levels possible; so away from higher-level lei code.
2023-09-11ds: use object-oriented API for epoll
This allows us to cut down on imports and reduce code. This also makes it easier (in the next commit) to provide an option to disable epoll/kqueue when saving an FD is valued over scalability.
2023-09-11daemon: depend on DS event_loop in master process, too
The awaitpid API turns out to be quite handy for managing long-lived worker processes. This allows us to ensure all our uses of signalfd (and kevent emulation) are non-blocking.
2023-09-05update devel/syscall-list to devel/sysdefs-list
We use it to dump SIGWINCH and _SC_NPROCESSORS_ONLN, so "sysdefs" is a more appropriate list for *BSD users.
2023-02-22sendmsg: prefix sleep message with `#'
It's an informative message that's harmless, so hopefully the `#' prefix puts the users mind at ease. (I saw it on an `lei import' against an IMAP source)
2022-12-25syscall: fix i386/i686 detection
Both __ILP32__ and __x86_64__ need to be defined for a system to be considered x32. Without this, my 32-bit Debian VM on a 64-bit kernel would fail after upgrading to Perl 5.32.1 on Debian 11 (bullseye).
2022-12-23syscall: drop syscall.ph support
h2ph-generated *.ph files are often wrong or incomplete and IME they cause more problems than they solve. Furthermore, we need knowledge of struct layouts which h2ph-generated files can't get us. So trim down some bloat and leave a note for porters.
2022-12-23syscall: get rid of epoll_defined() sub
We can just check defined() on the `our' var itself and save the process several kilobytes of memory.
2022-10-17sigfd: set SIGWINCH for MIPS and PA-RISC on Linux
SIGWINCH is actually different for these architectures on Linux according to the signal(7) man page. Note: AFAICS there's no parisc machine in the GCC Farm[1], so it remains untested. I've only tested mips64 for mips, but I expect them to both work. OpenBSD (on gcc231) octeon defines SIGWINCH as the common `28', so it appears Linux is the only one with arch-dependent signal numbers (ditto with syscalls). [1] https://cfarm.tetaneutral.net/machines/list/
2022-10-17syscall: avoid needless string comparison on x86-64
For common x86-64 systems, we can avoid a needless string comparison on `mips64' by restructuring the branches for architecture detection.
2022-09-29syscall: initialize buffer for vec()
This is needed for older Perls (tested perl 5.16.3 on CentOS 7).
2022-08-11syscall: add support for riscv64
Tested on gcc92.fsffrance.org from cfarm.
2022-04-26lei: move to v5.12 to avoid "use strict"
Socket.pm still loads strict.pm, unfortunately, which hurts startup time; but we'll save some LoC this way.
2022-04-18syscall: golf + more idiomatic buffer initialization
While `vec' is useful for user-supplied buffers to avoid excess memory traffic, but provides no benefit when we need to allocate our own buffers as we do in nodatacow_fh, since Perl can't elide memset(ptr, 0, len). So just use the idiomatic `"\0" x $LEN' here.
2022-04-18syscall: more idiomatic cmsghdr space allocation
Since we know the space required under Linux, we can use the same initialization as the Inline::C version instead of hard-coding 256 as we do for Socket::MsgHdr.
2022-03-24syscall: add sendmsg+recvmsg for remaining arches
aarch64, ppc64le, sparc64, loongarch64, and mips (32-bit userspace) are all tested via machines from the GCC Farm Project <https://cfarm.tetaneutral.net/> Remaining syscall numbers are from musl <https://musl.libc.org/>
2022-03-23syscall: implement sendmsg+recvmsg in pure Perl
Socket::MsgHdr is only packaged for Debian and derivatives at the moment, and Inline::C pulling in gcc/clang is a huge amount of disk space and bandwidth for some users. This enables disk space and/or bandwidth-limited users to use lei. Only Linux guarantees a stable ABI and syscall numbers, but that's the majority of our userbase. FreeBSD users will still have to use Inline::C (or get Socket::MsgHdr packaged). x86, x32, and x86-64 are all currently supported, more to be added.
2022-03-23syscall: drop unused EEXIST import
We've never used it, actually.
2022-02-01syscall: FS_IOC_*FLAGS: define on per-architecture basis
It turns out these Linux ioctls are unfortunately architecture-dependent, and not endian-dependent. Fixup some warning messages while we're at it, too. Fixes: 14fa0abdcc7b6513 ("rewrite Linux nodatacow use in pure Perl w/o system") Link: https://public-inbox.org/meta/YfdYqLhDVQRQ9NGT@codewreck.org/ Noticed-by: Dominique Martinet <asmadeus@codewreck.org>
2022-02-01syscall: fallback to rename on renameat2 EINVAL
ZFS appears to incorrectly return EINVAL on renameat2 when the operation is not supported: renameat2(AT_FDCWD, "...", AT_FDCWD, "...", RENAME_NOREPLACE) = -1 EINVAL Fall back to the racy rename in this case as well:
2022-01-31rewrite Linux nodatacow use in pure Perl w/o system
btrfs is Linux-only at the moment (and likely to remain that way for practical purposes). So rely on Linux ABI stability and use the `syscall' and `ioctl' perlops rather than relying on Inline::C. Inline::C (and gcc||clang) are monstrous dependencies which we can't expect users to have. This makes supporting new architectures more difficult, but new architectures come along rarely and this reduces the burden for the majority of Linux users on popular architectures (while still avoiding the distribution of pre-built binaries). Link: https://public-inbox.org/meta/YbCPWGaJEkV6eWfo@codewreck.org/
2021-10-22lei: use RENAME_NOREPLACE on Linux 3.15+
One syscall is better than two for atomicity in Maildirs. This means there's no window where another process can see both the old and new file at the same time (link && unlink), nor a window where we might inadvertantly clobber an existing file if we were to do `stat && rename'.
2021-10-01ds: simplify signalfd use
Since signalfd is often combined with our event loop, give it a convenient API and reduce the code duplication required to use it. EventLoop is replaced with ::event_loop to allow consistent parameter passing and avoid needlessly passing the package name on stack. We also avoid exporting SFD_NONBLOCK since it's the only flag we support. There's no sense in having the memory overhead of a constant function when it's in cold code.
2021-06-20scripts: add syscall-list tool for development
We'll be supporting inotify directly as we do with epoll so so Linux users won't have to deal with XS, extra DSOs or install Linux::Inotify2 (and common::sense) modules.
2021-05-06syscall: minor yak-shaving updates
FreeBSD (and other *BSDs) do not have stable syscall numbers, so drop no-op checks for it and add a note to use Inline::C, instead. Drop an implicit return for the syscall.ph loading while we're at it, too. On Linux, epoll_create(2) ignores the size arg since Linux 2.6.8, so just hard code it to some non-zero value. On a side note, we can probably drop epoll_create(2) support soon and just use epoll_create1(2) which appeared in 2.6.27+ (2008-10-09). Our userspace (Perl and git) requirements are already further ahead.
2021-01-18initialize scalar for `vec' perlop modification
Older Perls (tested 5.16.3) would warn on uninitialized scalars while newer (tested 5.28.1) do not. Just initialize it to an empty string since it'll be filled in by `vec'.
2021-01-01update copyrights for 2021
Using "make update-copyrights" after setting GNULIB_PATH in my config.mak
2021-01-01syscall: SFD_NONBLOCK can be a constant, again
Since Perl exposes O_NONBLOCK as a constant, we can safely make SFD_NONBLOCK a constant, too. This is not the case for SFD_CLOEXEC, since O_CLOEXEC is not exposed by Perl despite being used internally in the interpreter.
2020-12-28ds: flatten + reuse @events, epoll_wait style fixes
Consistently returning the equivalent of pollfd.revents in a portable manner was never worth the effort for us, as we use the same ->event_step callback regardless of POLLIN/POLLOUT/POLLHUP. Being a Perl, @events knows it size and we don't have to return a maximum index for the caller to iterate on. We can also avoid redundant integer coercion ("+0") since we ensure everything is an IV in other places. Finally, vec() is preferable to ("\0" x $size) for resizing buffers because it only needs to write the extended portion and not overwrite the entire buffer.
2020-08-07syscall: support sparc64 (and maybe other big-endian systems)
Thanks to the GCC compile farm project, we can wire up syscalls for sparc64 and set system-specific SFD_* constants properly. I've FINALLY figured out how to use POSIX::SigSet to generate a usable buffer for the syscall perlfunc. This is required for endian-neutral behavior and relevant to sparc64, at least. There's no need for signalfd-related stuff to be constants, either. signalfd initialization is never a hot path and a stub subroutine for constants uses several KB of memory in the interpreter. We'll drop the needless SEEK_CUR import while we're importing O_NONBLOCK, too.
2020-02-07syscall: support Linux x32 ABI
The x32 ABI allows users to take advantage of the extra registers on x86-64 without the bloat of 64-bit pointers and longs. This ought to be significant since Perl was designed when 32-bit was prevalent; and the common structs for ops, hashes, scalars, and arrays use longs (SSize_t/Size_t) for things which should never need 64-bits when processing emails. Debian's x32 port seems to work quite nicely under a chroot on an amd64 Linux system. All tests pass under x32, now.
2020-02-06treewide: run update-copyrights from gnulib for 2019
I didn't wait until September to do it, this year!
2020-01-06syscall: modernize away from pre-Perl-5.6 conventions
"use vars" was superseded by "our" in Perl 5.6, and we can "use parent qw(Exporter)" in favor of manipulating @ISA directly (or the bigger "use base ..."); While we're at it, avoid multiple invocations of constant->import by passing a hashref as a "use" parameter.
2019-11-27httpd|nntpd: avoid missed signal wakeups
Our attempt at using a self-pipe in signal handlers was ineffective, since pure Perl code execution is deferred and Perl doesn't use an internal self-pipe/eventfd. In retrospect, I actually prefer the simplicity of Perl in this regard... We can use sigprocmask() from Perl, so we can introduce signalfd(2) and EVFILT_SIGNAL support on Linux and *BSD-based systems, respectively. These OS primitives allow us to avoid a race where Perl checks for signals right before epoll_wait() or kevent() puts the process to sleep. The (few) systems nowadays without signalfd(2) or IO::KQueue will now see wakeups every second to avoid missed signals.
2019-10-22syscall: get rid of sendfile wrappers for now
I'm not sure they'll make a measurable difference or will be worth the effort in the future given the prevalance of HTTPS and giant socket buffers. Using Inline::C for this may make more sense in the future, too, especially if we want to be able to use GnuTLS.
2019-06-29listener: use edge-triggered notifications
We don't need extra wakeups from the kernel when we know a listener is already active.
2019-06-24ds: split out IO::KQueue-specific code
We don't need to code multiple event loops or have branches in watch() if we can easily make the IO::KQueue-based interface look like our lower-level epoll_* API.
2019-06-24ds: get rid of event_watch field
We don't need to keep track of that field since we always know what events we're interested in when using one-shot wakeups.
2019-06-24syscall: get rid of unnecessary uname local vars
We don't need to keep information from uname(2) around outside of startup.
2019-06-24syscall: get rid of unused EPOLL* constants
EPOLLRDBAND is used for DECnet; and I'm pretty sure I won't be updating any of our code to work with DECnet. I've never found use for EPOLLHUP or EPOLLERR, either; so disable those for now and add comments for things I might actually use: EPOLLET and EPOLLONESHOT.
2019-05-08syscall: drop readahead wrapper
No backwards compatibility to worry about for us; and fadvise is superior anyways.
2019-05-05listener: use EPOLLEXCLUSIVE for listen sockets
Since our listen sockets are non-blocking and we may run multiple httpd|nntpd processes; we need a way to avoid thundering herds when there are multiple httpd|nntpd worker processes. EPOLLEXCLUSIVE was added just for that in Linux 4.5