690 Commits
Author SHA1 Message Date
IntinteDAO ba3a7911fb Merge pull request #6 from cristim/fix/vnode-report-happened-events
libkqueue: report only the vnode events that happened
2026-11-05 14:28:20 +01:00
Cristian Magherusan-Stanciu dbec7aa8b8 tests: vnode write and chmod reported together
Add a case for a NOTE_WRITE|NOTE_ATTRIB watch where a write and a chmod
are both queued before kevent() runs; both notes must come back in one
event. test_kevent_vnode_oneshot_after_dropped_event already does
chmod then write before kevent(), which is the batch case fixed by the
previous commit, so it needs no change.

Verified only with clang -fsyntax-only -Wall (an empty config.h was
created for the check and removed). Not run against a real build; the
same two scenarios are exercised by the host shim harness against the
real vnode.c.
2026-11-04 17:02:16 +01:00
Cristian Magherusan-Stanciu b797424d8a libkqueue: handle every queued inotify event in vnode copyout
inotify returns every queued event that fits in one read, but copyout
looked only at the first one. When that first event mapped to none of
the requested notes, the whole batch was dropped and the requested
event queued behind it was lost. Reproduced with real inotify:

- NOTE_WRITE watch, fchmod then write before one copyout: one dropped
  event, NOTE_WRITE never reported (the base reported 0x6).
- NOTE_WRITE|NOTE_ATTRIB watch, write then chmod: only NOTE_WRITE.

Copyout now reads into a 4096 byte buffer, walks every struct
inotify_event (sizeof + len), and ORs the masks before the existing
IN_IGNORED handling, the single fstat size/nlink bookkeeping and the
drop on an empty result. The inotify descriptor is now created with
inotify_init1(IN_NONBLOCK) and EAGAIN is treated as "nothing queued"
(dropped), so a copyout with no data cannot block the kqueue; before,
the read was blocking. get_one_event had no other caller and is
replaced by a static helper. The duplicated block of note checks is
folded into one.

Verified with the host shim harness that includes the real vnode.c
(clang -fsanitize=address,undefined, leak detection off because the
harness leaks its knotes): new scenarios chmod+write on a WRITE
watch, write+chmod on a WRITE|ATTRIB watch, an empty copyout, and 100
queued write/chmod pairs. At the previous head the first two FAIL
(fflags 0 and 0x2) and the empty copyout hangs; after the change all
scenarios pass ("ok") with no ASan or UBSan reports.

Not verified: a full CMake build and the in-tree test suite (see the
next commit for the test change, which is only syntax-checked).
2026-11-04 17:02:16 +01:00
Cristian Magherusan-Stanciu f124e946c3 libkqueue: do not use IN_ONESHOT for vnode watches
The previous commit drops inotify events that map to none of the
requested notes. With EV_ONESHOT the watch carried IN_ONESHOT, which
makes the kernel remove it after the first event, but the core only
deletes a one-shot knote when an event is delivered (platform.c skips
that for EVFILT_DROP). A one-shot NOTE_WRITE watch hit by an unrequested
chmod therefore kept its knote with a dead watch and lost the later
write.

Stop passing IN_ONESHOT; the core already deletes the knote on delivery.
The mask now only gets IN_ATTRIB as a fallback when no requested note
needs any bit, since inotify_add_watch rejects an empty mask. Add a
regression test (one-shot NOTE_WRITE, chmod, then write).

Verified with the same host harness as the previous commits plus this
case: before this commit it fails (write after dropped chmod lost), after
it passes. The kqtest test was only checked with -fsyntax-only and not run.
2026-11-04 16:46:13 +01:00
Cristian Magherusan-Stanciu cc0a563d9f tests: cover vnode event reporting and unrelated close
Update test_kevent_vnode_note_write: it expected NOTE_EXTEND to be
added although only NOTE_WRITE was requested, which is what the previous
commit stops doing. Add a NOTE_EXTEND-only test and a test that closing a
second descriptor of the watched file neither produces an event nor
removes the knote.

These are written in the style of test/vnode.c and pass -fsyntax-only
with the repo include path. They were not run: the library does not build
standalone here (needs Darling's headers), so the kqtest binary could not
be linked. The behaviour they check was exercised by the host harness
described in the previous commit.
2026-11-04 16:43:10 +01:00
lucannez64 e182d23b05 libkqueue: report only the vnode events that happened
evfilt_vnode_copyout started from the registered knote, so every
fflags bit the caller asked for was reported on any inotify event: a
write on a NOTE_WRITE|NOTE_ATTRIB|NOTE_DELETE|NOTE_RENAME watch came
back as all four. NOTE_EXTEND was also only produced when NOTE_WRITE was
requested, not NOTE_EXTEND.

The IN_CLOSE handling was wrong as well: add_watch always watched
IN_CLOSE, and a close of any descriptor on the inode set EV_ONESHOT on
the knote, which deleted it although the watched descriptor was still
open. A watcher lost all later events after an unrelated open/close.

Zero dst->fflags before filling it, gate NOTE_EXTEND on NOTE_EXTEND, stop
watching IN_CLOSE and remove the close kludge, and drop an inotify event
that maps to none of the requested notes. The watch mask now starts from
IN_ATTRIB so it is never empty (inotify_add_watch rejects a zero mask);
those attribute events are discarded by the drop above unless
NOTE_ATTRIB and friends were requested.

Rung: source patch review (the lol diff is source, not Apple binary
code). Adapted from lucannez64/darling-lol@a96cdc6 (patches/libkqueue.patch)
by lucannez64. Only the vnode.c hunks are taken; the rest of that patch
is not part of this change.

Verified: a host harness (outside the tree) that includes the real
src/linux/vnode.c with shimmed headers and real inotify, calling
evfilt_vnode_knote_create/copyout for write, touch, close of a second
descriptor and extend-only/write-only watches. Before: 4 failures (extra
requested bits reported, NOTE_EXTEND on a NOTE_WRITE watch, knote flagged
EV_ONESHOT by the close). After: all pass. Not verified: a full library
build (the tree builds only inside Darling's header environment) and the
repo's kqtest suite.
2026-11-04 16:43:10 +01:00
Fervi (IntinteDAO) a0ea536be8 Merge pull request #5 from muness/fix/independent-socket-filters
linux: preserve independent socket read and write registrations
2026-10-26 19:59:23 +01:00
Fervi (IntinteDAO) 91fbeeb6a8 Merge pull request #4 from muness/fix/kevent-receipt-buffer-capacity
kevent: accept successful receipts without output space
2026-10-26 19:59:23 +01:00
Muness Castle ce37e35f69 linux: preserve independent socket read and write registrations
Registering READ and WRITE on one socket currently uses the same epoll key: the second registration changes the first mask/knote with EPOLL_CTL_MOD. Both calls succeed, but delayed .NET ReceiveAsync can wait forever while recv can read the peer's byte.

Use the existing internal duplication helper for separate epoll registrations, share descriptor ownership/cleanup, and destroy remaining READ/WRITE registrations when the queue closes. Preserve corrected 32-bit FIONREAD storage. The regression uses explicit receipt output and does not depend on the separate no-output-receipt PR.

The baseline regression misses one direction; the isolated fixed library passes both registration orders, per-direction user data, modify/disable/re-enable/delete, peer EOF, queue-close ownership, regular-file surrogate cleanup and repeated descriptor accounting. Independent managed delayed receive and HTTP also pass with the companion registry fixes. HTTPS certificate-chain construction remains a separate failure.

Integration requirement: the libc and libSystem changes for VibeDarling/darling#1275 must leave a single registry in system_kernel so close/fork callbacks resolve to the same state. Without those changes, duplicated descriptors cannot be reliably cleaned up by the application close path.

Provenance: existing libkqueue duplication/close-hook source, Linux epoll interfaces and authored public-API controls (rungs1–4). No guest application internals used.

Validated locally on ARM64 Asahi with 16KiB pages. No CI result is claimed. These are compatibility-layer fixes; macOS Roon is still not a working application. No Roon or Apple binaries were modified or inspected internally.

Fixes https://github.com/VibeDarling/darling/issues/1274
2026-10-25 08:40:34 -04:00
Muness Castle 32d8a4e0ec fix(kevent): accept successful receipts without output space
A successful EV_RECEIPT change has no error to return when the event list is full or absent. Continue applying the remaining changes instead of reporting failure with a stale errno. Actual registration errors remain errors.

Provenance: Apple published XNU kern_event.c change-processing loop (rung 1), as cited in test notes. The authored public-kevent regression fails against baseline with seeded EAGAIN. It checks empty, partial and full receipt capacity, actual pipe readiness, and invalid-filter errors; rebuilt target validation follows private staging.

Refs VibeDarling/darling#1271
2026-10-24 21:13:37 -04:00
Fervi (IntinteDAO) dfbe365089 kqueue: early return on changelist error and fix NOTE_TRIGGER in EVFILT_USER
On Darwin/BSD, if an error occurs while processing the changelist in
kevent64, kevent returns immediately with the error events in
eventlist rather than waiting. In EVFILT_USER, trigger eventfd on
knote creation if NOTE_TRIGGER is set, and preserve level-triggered
semantics during copyout.

Where: Rung 1/3 (Apple kqueue/kevent man pages and xnu bsd_kqueue).
Verified: Transmission.app networking event loop in Xephyr.
2026-10-22 23:15:24 +02:00
Fervi (IntinteDAO) 96cee191b2 listenregistry, kqueue: fix heap allocation bounds, unregister OOB underflow, and add atfork reset 2026-10-11 09:53:45 +02:00
Lucas Francesco b51bec91f2 Optimize map iteration and bounds checking, cap fd limit to 65536 2026-10-02 01:16:48 -03:00
Fervi (IntinteDAO) 4ced850030 Merge pull request #1 from deepai-org/fix-timer-rearm-update 2026-09-30 12:07:18 +02:00
kevin d6350419da Apply EV_ADD updates before enable and disable transitions 2026-09-29 20:54:44 +00:00
Fervi (IntinteDAO) 7ef17bacc2 fix(kqueue): do not duplicate file descriptors in epoll read/write filters and fix 64-bit FIONREAD 2026-09-29 16:45:54 +02:00
Fervi (IntinteDAO) d1dc0bf708 libkqueue: refactor raw_syscall6 with RAW_NR_* defines for all archs + raw_getsockname 2026-09-06 11:38:57 +02:00
Fervi (IntinteDAO) 3a9b46ea5a libkqueue: add KQ_DLOG debug logging macro 2026-09-05 11:54:17 +02:00
kkHAIKE 186d76c0c3 all fixes 2026-09-03 23:33:48 +02:00
CuriousTommy b0795a2e1d Merge pull request #10 from darlinghq/fedora_44_fix
Fix Building For Fedora 44
2026-06-06 12:09:34 -07:00
Thomas A.andsuperbonaci c95c06ba64 Fix Improper Usage of Type Punning
The original code sets `&kn->kev` (`struct kevent_internal_s`) as the argument for `const struct kevent64_s *kev`. This is an issue because some variables are stored in different locations (mainly `data` & `fflags`), which can lead to data corruption.

The new code is based on the logic from `evfilt_timer_knote_modify`. Credit to superbonaci for the idea.

Co-authored-by: superbonaci <[email protected]>
2026-05-31 19:54:56 -07:00
CuriousTommy f673c801cb Merge pull request #8 from darlinghq/syscall_rework
(Syscall Redesign | Part 2) Organize Syscall Source & Header Files
2025-05-14 13:54:44 -07:00
Thomas A. 56f88c0727 Update Syscall Header Includes 2025-05-08 22:05:34 -07:00
CuriousTommy 6be5b2bee4 Merge pull request #7 from darlinghq/syscall_rework
(Syscall Redesign | Part 1) Relocate Syscall Source & Header To Legacy Path
2025-04-07 08:35:28 -07:00
Thomas A. f8a3192499 Fix Building 2025-04-06 21:26:47 -07:00
Ariel Abreu 7e2ade452a Add NOTE_MACHTIME support
This is actually used in libdispatch for pretty much all non-wall-clock
timers, so as you might imagine, it's used quite frequently and we were
processing it incorrectly, leading to stalls in some programs that use
such timers for short intervals/deadlines.
2023-10-04 00:27:01 -04:00
CuriousTommy 599a9e8316 Merge pull request #4 from darlinghq/remove_lkm
Remove LKM
2023-07-26 11:57:27 -07:00
Thomas A 7c71d0d993 Update CMake Version To 3.13 2023-07-26 10:28:23 -07:00
Thomas A 203b36b0da Update Include Path To Use XNU Repo Instead 2023-05-19 10:58:19 -07:00
Thomas A 7e944dd0e3 Don't Depend On <lkm/api.h> 2023-05-16 11:57:42 -07:00
Thomas A a60532d647 Update Minimum CMake Version to 3.10 2022-11-30 22:27:10 -08:00
Thomas A 2c0ddde743 Add "private.h" symbol-link in "linux" directory 2022-09-10 12:48:48 -07:00
Ariel Abreu 8411c8c69f Support EV_DISPATCH together with EV_ONESHOT
Apple uses this combination to defer deletion of knotes until they're re-enabled.
2022-09-06 00:52:42 -04:00
Ariel Abreu 5118b93d80 Merge pull request #3 from trungnt2910/dev/trungnt2910/wsl1-support
fix: Socket SO_ACCEPTCONN hack for WSL1
2022-09-01 10:12:54 -04:00
Trung Nguyen 4e6a9f048b fix: Socket SO_ACCEPTCONN hack for WSL1
As SO_ACCEPTCONN does not work properly on these systems,
we have to rely on Darling to report listening sockets in
libsystem_kernel and keep a registry of listening sockets
in libkqueue.
2022-08-25 09:18:03 +07:00
Ariel Abreu 8572682de1 Fix FD leaks in FS, Mach port, and proc filters 2022-06-30 22:43:03 -04:00
Ariel Abreu dfe9fbc0d2 Various synchronization and resource management fixes
The two most notable fixes are:
1) libsystem_kernel now delegates closing kqueue descriptor to us.
Rather than close the descriptor and inform us later, it allows us to
handle kqueue closing ourselves.
This avoids a double-close that was causing issues (most notably
with multi-threaded programs).
2) We now `map_delete` all references to kqueues being freed. We were
previously only deleting the original FD reference.

In addition, wherever `kqueue_delref` is called, the `kq_mtx` must be held.
This is required because that function can call `kqueue_free`, which
requires that lock to be held. While inefficient, this should help avoid
races between threads concurrently modifying the kqueue map. A better
solution should be devised later to make this more efficient.
2022-06-15 11:42:47 -04:00
Ariel Abreu 8b748d1070 Use a special version of dup
libsystem_kernel's `sys_dup` and `sys_dup2` call `kqueue_dup`, so if we
use the normal `dup` call in libkqueue, we end up recursing back into
libkqueue (unecessarily).
2022-03-09 17:13:29 -05:00
Ariel Abreu f3f8cb99b0 Fix kqueue_close_cb logic to use EV_RECEIPT
Using this flag tells kevent to keep processing even when an error is
encountered adding an event. This means that it will actually check both
EVFILT_READ and EVFILT_WRITE now.

With the old code, we were leaking descriptors created for EVFILT_WRITE.
2022-03-09 17:09:26 -05:00
Ariel Abreu f721a1d2d8 Keep processing events after EV_RECEIPT and remove unnecessary race check
I added that race check back when I was trying to use libkqueue with update-sources because launchd was segfaulting and libkqueue seemed to be the cause, but I can't reproduce this anymore. The way it was, it was actually causing issues with EPOLLET events.
2022-03-09 17:07:16 -05:00
Ariel Abreu ed45525ac4 Use kprintf rather than logging to a file 2022-02-26 01:14:07 -05:00
Ariel Abreu 9b4eecbd88 Don't check for new events if KEVENT_FLAG_ERROR_EVENTS is given
We were dropping events before because libdispatch calls kevent with this flag to update a knote without checking for new events; when it did that and we handed it some new events, it would ignore them since they didn't contain `EV_ERROR`, which meant those events were lost forever.
2022-02-25 16:15:41 -05:00
Ariel Abreu 76d8aa3dce Only process EV_DISPATCH and EV_ONESHOT when we get a valid event
If the copyout callback for a filter indicates that the event should be ignored/dropped, we should not process EV_DISPATCH or EV_ONESHOT, as this would lead to missing actual events in the future.

Some filters (e.g. EVFILT_PROC) do specifically want to process EV_ONESHOT even when dropping an event, so this also introduces a special filter ID (EVFILT_DROP_POSTPROCESS) that enables the old behavior for individual events. Additionally, rather than just using `0` as a special filter ID, this commit introduces EVFILT_DROP (to be clear about its meaning).
2022-02-25 16:12:17 -05:00
Ariel Abreu bcef9ea04b Handle no-event case in copyin for EVFILT_MACHPORT 2022-02-20 01:03:17 -05:00
Ariel Abreu 532529e49c Implement EVFILT_PROC with darlingserver 2022-02-12 00:38:04 -05:00
Ariel Abreu 975d7457cd Implement EVFILT_MACHPORT with darlingserver
See the darlingserver repo for the majority of the logic behind the new implementation.

TODO: libkqueue needs proper kevent_qos support in order to be able to use the data_out parameter passed in to kevent_qos calls (which is what *should* be passed as the default buffer to use when ext[0] is not available in EVFILT_MACHPORT).
2022-02-10 13:23:16 -05:00
Ariel Abreu b395f5bf0d Disable EVFILT_{MACHPORT,PROC}
These need to be reimplemented for darlingserver. This commit simply replaces them with code that prints "TODO" and then aborts.
2022-01-28 00:10:59 -05:00
Ariel Abreu 3f76738619 Handle race condition when waiting for events
If multiple threads are waiting on the same kqueue, they'll all be woken up at the same time, however only one can acquire the lock at a time. The one that acquires it first gets to consume some events from the kqueue while everyone else waits. But, after they're done and they release the lock, the next person will have *less events available*. We were previously neglecting to check for this and trying to consume more events than were available (leading to some nasty deadlocks with certain events like EVFILT_USER).
2020-10-07 12:50:08 -04:00
Ariel Abreu 9a94fadf15 Workaround an issue caused by a flag
When we pass back EV_VANISHED to libdispatch, it thinks we're telling it the source vanished (because that's really what it means) and it explodes
2020-10-06 08:28:31 -04:00
Ariel Abreu 3c34d903b4 Initial update to clean up include directories
"initial" because there might some additional modifications necessary later on
2020-05-14 12:57:41 -04:00