Skip to content

docs(debt): record the EVFILT_VNODE hints for ftruncate, fchmod and utimes on firmware 11.60 - #22936

Merged
oneandonlydean merged 1 commit into
boykopovar:mainfrom
xXPBSXx:docs/libkernel-vnode-attrib-console-1160
Nov 18, 2026
Merged

oneandonlydean merged 1 commit into
boykopovar:mainfrom
xXPBSXx:docs/libkernel-vnode-attrib-console-1160

Conversation

@xXPBSXx

@xXPBSXx xXPBSXx commented Nov 18, 2026

Copy link
Copy Markdown
Contributor

What

The EVFILT_VNODE, sceKernelAddFileEvent and sceKernelDeleteFileEvent entry in docs/dev/debt/libkernel.md turns host changes into FreeBSD 11 vnode hints and says: "FreeBSD's hints depend on the filesystem and the console's filesystems are not known: a growing ftruncate reports NOTE_WRITE | NOTE_EXTEND where vop_setattr_post reports NOTE_ATTRIB, and on Windows utimes on a file reports NOTE_WRITE." Three probes have now registered EVFILT_VNODE on a regular file in a payload process on firmware 11.60 and made the changes themselves. This pull request replaces that sentence through a debt fragment, as the contribution rules ask; no code changes. The probes also measured directories, links, renames and bad registrations; those results are not used here.

With fflags 0x3f and with 0x8 alone, every ftruncate reported NOTE_ATTRIB (0x8) and nothing else: shrinking to 0 (case S4.1 of the second probe, line 21), growing to 8192 (S4.2, line 22), growing to 8192 and then to the size the file has (third probe, S.5 and S.6, lines 10 and 11), shrinking to 100 and again to 100 (S.7 and S.8, lines 12 and 13), and to the size the file already had (S.2 and S.3, lines 7 and 8). fchmod to the mode the file already had (S.4, line 9), to 0640 (S4.3, line 23) and utimes of the path (S4.4 and S.11, lines 24 and 16) also gave 0x8. A pwrite behind the end that grew the file gave 0x6 (NOTE_WRITE | NOTE_EXTEND, S.1 line 6; S4.5 line 25) and a pwrite inside the file 0x2 (NOTE_WRITE, S.9 line 14; S4.6 line 26). With fflags 0x37, which has no NOTE_ATTRIB, the ftruncate, fchmod and utimes calls of the first probe reported nothing, as expected (cases 2.3 to 2.6, lines 7 to 10 of its output). A second EV_ADD on the same file replaced the fflags of the first: after 0x2 and then 0x4 only the growing write reported (0x6), after 0x4 and then 0x2 both writes did, and after 0x1 and then 0x20 only the rename did (S1 to S3 of the second probe, lines 5 to 19).

The entry now says what the console did and keeps what it says for AnyPS5: a growing ftruncate reports NOTE_WRITE | NOTE_EXTEND here, where the console reports NOTE_ATTRIB, and on Windows utimes reports NOTE_WRITE. A ftruncate to the size the file has, and fchmod to its own mode, reported NOTE_ATTRIB on the console; whether AnyPS5 reports anything for them was not compared (the host shows no change, and the entry's description was not run again). This pull request records the difference and changes no behaviour: the hints come from the host's change notifications, and a console that reports NOTE_ATTRIB even for a change that changes nothing cannot be matched from them. The emulator itself was not run for this pull request.

Not measured: directories and other filesystems than the ones a payload process sees, files on other volumes, NOTE_LINK, NOTE_DELETE and NOTE_RENAME beyond the cases above, and a title process.

Tested

Measured on a PS5 with firmware 11.60 (kernel_get_fw_version 0x11600005), from homebrew payload processes (ps5-payload-sdk v0.45, elfldr) that load libkernel.sprx and take kqueue and kevent with dlsym, on 2026-11-18 at 07:05 UTC (first probe), 09:08 UTC (second) and 11:06 UTC (third). A title process may differ. The cases used are in the first comment.

Change (watched file, fflags 0x3f) PS5 payload process, firmware 11.60 in the entry
ftruncate growing, shrinking, to the size it has NOTE_ATTRIB (0x8) each time now stated (growing: differs from here)
fchmod to its own mode or another, utimes 0x8 now stated
pwrite that grows the file; one that does not 0x6; 0x2 now stated
second EV_ADD with other fflags replaces the first now stated

tools/check_conventions.py --base upstream/main passes with no error; it warns that the entry is over 1000 characters (now 5099, up from 4783; it was over 1000 long before this change, and the new sentence is the only change). This is a documentation change, so no test was added or run, and no GPU test applies. Not tested: the emulator against these calls, a title process, other firmware, and the cases listed above as not measured.

Overlap: the other open pull requests that change the file of this branch (the fragment docs/dev/debt/changes/docs-libkernel-vnode-attrib-console-1160.md, a new file), core/libs/prx/libkernel/Equeue/src/FileEvent.cpp or core/libs/prx/libkernel/Equeue/src/Kqueue.cpp, which the entry describes, or the debt entry EVFILT_VNODE, sceKernelAddFileEvent and sceKernelDeleteFileEvent, found by reading the changed files of all 1756 open pull requests at about 12:33 to 12:48 UTC, by reading what they change in docs/dev/debt/libkernel.md and the fragments under docs/dev/debt/changes/ for EVFILT_VNODE, sceKernelAddFileEvent, vop_setattr and NOTE_ATTRIB, and by searching their titles and branch names for the same names (one hit, #7121), were checked on 2026-11-18 at about 13:02 UTC against main 60589980324 together with this head: the branch is based on 47bd2c6a1d3, an ancestor of it, merges into main without a conflict, and tools/debt_fold.py --check applies the fragment together with every other fragment on main without an error. Sixty-two pull requests list one of the files or docs/dev/debt/libkernel.md, most of them for other entries; as this branch adds only a fragment, none of them conflicts only with this change in the files. Two of them edit this entry itself in libkernel.md, in the older way without a fragment: #7121 (report open, read and close notes on watched files) rewrites the entry in its own hunk, and #7088 (support kevent read and write events on regular files) carries it as context and changes FileEvent.cpp and Kqueue.cpp too. The fragment here replaces the whole entry when it is folded, so whichever of them lands second has to bring the entry text of the other along, not only the sentence this pull request replaces. #21463 changes Kqueue.cpp for the completion of asynchronous I/O and #18462 the EV_ONESHOT handling of kevent; neither changes a line of the entry or the vnode notes recorded here. The merged result was not built or run; this is a documentation change.

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a (documentation only)
  • AI-assisted: yes

…times on firmware 11.60

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@xXPBSXx

xXPBSXx commented Nov 18, 2026

Copy link
Copy Markdown
Contributor Author

Probe sources (built with ps5-payload-sdk v0.45; each PROBE_BUILD is the first 12 hex digits of the source's SHA-256) and their raw output from the runs on 2026-11-18 at 07:05 UTC (2025da6d594b), 09:08 UTC (ee49b1522d34) and 11:06 UTC (aaa5a61e79c1). The clock in the PROBE lines is the console's own clock. Each probe loads libkernel.sprx, takes kqueue and kevent with dlsym, makes files in its own folder, registers EVFILT_VNODE with EV_ADD|EV_ENABLE|EV_CLEAR on a regular file, makes one change at a time, and reads the hints with kevent; every call is guarded against signals and by a self limit, and the files and the folder are removed at the end. Internal probe numbers and case labels in the header comments were removed; the code is unchanged, and each build ID is the hash of the original file.

1st probe (2026-11-18, 07:05 UTC): probe.c (build 2025da6d594b)
/* kqueue and kevent with EVFILT_VNODE on a file and a directory, payload process, firmware 11.60 (debt libkernel.md entry: EVFILT_VNODE,
   sceKernelAddFileEvent and sceKernelDeleteFileEvent watch the host file or directory behind the descriptor and turn host changes into FreeBSD 11 vnode hints, which
   filt_vfsvnode adds whole when they share a bit with the requested fflags: a write that grows a file is NOTE_WRITE | NOTE_EXTEND, another write NOTE_WRITE, a shrinking
   truncate or an attribute change NOTE_ATTRIB, a higher link count NOTE_LINK, a lower one or a pending delete NOTE_DELETE, a new path NOTE_RENAME; in a directory an entry
   created, removed or renamed is NOTE_WRITE, with NOTE_LINK for a subdirectory; FreeBSD's hints depend on the filesystem and the console's filesystems are not known: a
   growing ftruncate reports NOTE_WRITE | NOTE_EXTEND where vop_setattr_post reports NOTE_ATTRIB, and on Windows utimes on a file reports NOTE_WRITE). The probe looks kqueue
   and kevent up with dlsym in libkernel.sprx and uses only the POSIX interface (the Sony equeue functions sceKernelAddFileEvent/sceKernelDeleteFileEvent are not part of this
   probe). Everything happens in /data/anyps5_probe/n2_17 (created and removed by the probe): vn.bin and a few names derived from it, one subdirectory. Cases on the
   file (opened read-only, watched with EV_ADD|EV_ENABLE|EV_CLEAR and fflags 0x37), each action followed by a kevent wait of at most 1 s, the table action -> the fflags
   returned (hex) and the time to the event in milliseconds: a growing write through a second descriptor, a write without growth, a shrinking ftruncate, a growing ftruncate,
   fchmod, utimes of the path, link, unlink of the link, rename, unlink of the renamed file while the descriptor is open. On the directory (watched the same way): create a
   file, mkdir, rmdir, rename, unlink inside it, and a registration with fflags 0x4 only. Then: registering the same descriptor twice with a different fflags and one write
   (how many events), EV_DELETE and a write (no event within 300 ms), EV_ADD on a closed descriptor, fflags 0, 0x80 and 0xFF; the errors: EV_ADD with the ident -1 and
   999, a pipe descriptor (EVFILT_VNODE on a pipe), and the directory with NOTE_DELETE. Both the return code and an event with EV_ERROR in the list are printed. Every descriptor is closed and
   every file and directory removed at the end. A fault ends the probe at once with _exit. Nothing is sent. */
#include <dlfcn.h>
#include <errno.h>
#include <pthread.h>
#include <signal.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <ps5/kernel.h>   /* only for kernel_get_fw_version() */

#ifndef PROBE_NAME
#define PROBE_NAME "unnamed"
#endif
#ifndef PROBE_BUILD
#define PROBE_BUILD "unbuilt"
#endif
/* Self limit: the runner can only close the connection, it cannot end the process on the console, so the probe ends
   itself after PROBE_MAX_S seconds at the latest (set by the build script). */
#ifndef PROBE_MAX_S
#define PROBE_MAX_S 50
#endif

static volatile int g_handle = -1;
static int (*volatile g_setmargins)(int, int, int) = 0; /* unused here */
static void reset_rate(void) {
  if (g_setmargins && g_handle >= 0) g_setmargins(g_handle, 0, 0);
}

static void probe_alarm(int sig) {
  static const char msg[] = "TIMEOUT\tprobe self limit\n";
  (void)sig;
  (void)!write(1, msg, sizeof msg - 1);
  reset_rate();
  _exit(124);
}

/* Watchdog thread: alarm()/SIGALRM does not help when the main thread hangs in a blocking system call or IPC. The
   thread sleeps PROBE_MAX_S seconds, writes TIMEOUT with write() and ends the process with _exit(124). alarm() stays
   as a second safeguard. */
static void* probe_watchdog(void* arg) {
  static const char msg[] = "TIMEOUT\tprobe watchdog thread\n";
  unsigned left = PROBE_MAX_S;
  (void)arg;
  while (left) left = sleep(left);
  (void)!write(1, msg, sizeof msg - 1);
  reset_rate();
  _exit(124);
  return 0;
}

static void probe_watchdog_start(void) {
  pthread_t t;
  if (pthread_create(&t, 0, probe_watchdog, 0) == 0) pthread_detach(t);
}

static int cases = 0;

static void probe_header(void) {
  char utc[32];
  time_t now = time(0);
  probe_watchdog_start();
  struct tm tmv;
  gmtime_r(&now, &tmv);
  strftime(utc, sizeof utc, "%Y-%m-%dT%H:%M:%SZ", &tmv);
  printf("PROBE\t%s\tfw=0x%08x\tbuild=%s\tpid=%d\tutc=%s\n", PROBE_NAME, kernel_get_fw_version(), PROBE_BUILD,
         getpid(), utc);
  fflush(stdout);
}

static void probe_case(const char* id, const char* call, long ret, int err, const char* extra) {
  printf("CASE\t%s\t%s\t%ld\t%d\t%s\n", id, call, ret, err, extra ? extra : "-");
  fflush(stdout);
  cases++;
}

static void probe_end(void) {
  printf("END\t%d\n", cases);
  fflush(stdout);
}



#include <fcntl.h>
#include <sys/event.h>
#include <sys/stat.h>
#include <sys/time.h>

typedef int (*fn_kqueue)(void);
typedef int (*fn_kevent)(int, const struct kevent*, int, struct kevent*, int, const struct timespec*);

static fn_kevent f_kevent;

static void fault(int sig) {
  static const char msg[] = "FAULT\tsignal in the call\n";
  (void)sig;
  (void)!write(1, msg, sizeof msg - 1);
  _exit(125);
}

static long long now_ms(void) {
  struct timespec t;
  clock_gettime(CLOCK_MONOTONIC, &t);
  return (long long)t.tv_sec * 1000LL + t.tv_nsec / 1000000;
}

/* wait for one event; returns the number of events, fills fflags, flags and data */
static int wait_ev(int kq, int ms, unsigned* fflags, unsigned* flags, long* data, long long* took) {
  struct kevent ev[4];
  struct timespec ts = {ms / 1000, (long)(ms % 1000) * 1000000L};
  long long t0 = now_ms();
  memset(ev, 0, sizeof ev);
  errno = 0;
  int n = f_kevent(kq, 0, 0, ev, 4, &ts);
  *took = now_ms() - t0;
  *fflags = n > 0 ? ev[0].fflags : 0;
  *flags = n > 0 ? ev[0].flags : 0;
  *data = n > 0 ? (long)ev[0].data : 0;
  return n;
}

static void action(const char* id, const char* label, int kq) {
  unsigned ff, fl;
  long d;
  long long took;
  int n = wait_ev(kq, 1000, &ff, &fl, &d, &took);
  char extra[160];
  snprintf(extra, sizeof extra, "events=%d fflags=0x%x flags=0x%x data=%ld time_ms=%lld", n, ff, fl, d, took);
  probe_case(id, label, n, 0, extra);
}

static int reg(int kq, int ident, unsigned fflags, int flags, struct kevent* out, int* errn) {
  struct kevent ch, ev[2];
  EV_SET(&ch, ident, EVFILT_VNODE, flags, fflags, 0, 0);
  struct timespec ts0 = {0, 0};
  memset(ev, 0, sizeof ev);
  errno = 0;
  int r = f_kevent(kq, &ch, 1, ev, 2, &ts0);
  *errn = errno;
  if (out) *out = ev[0];
  return r;
}

static void regcase(const char* id, const char* label, int kq, int ident, unsigned fflags, int flags) {
  struct kevent o;
  int e;
  memset(&o, 0, sizeof o);
  int r = reg(kq, ident, fflags, flags, &o, &e);
  char extra[200];
  snprintf(extra, sizeof extra, "rc=%d errno=%d first_event: flags=0x%x data=%ld", r, e, r > 0 ? (unsigned)o.flags : 0, r > 0 ? (long)o.data : 0);
  probe_case(id, label, r, e, extra);
}

int main(void) {
  signal(SIGALRM, probe_alarm);
  signal(SIGSEGV, fault);
  signal(SIGBUS, fault);
  alarm(PROBE_MAX_S);
  probe_header();
  char extra[260];

  errno = 0;
  void* lk = dlopen("libkernel.sprx", RTLD_LAZY);
  probe_case("0", "load libkernel.sprx", lk ? 0 : -1, errno, lk ? "loaded" : dlerror());
  if (!lk) { probe_end(); return 0; }
  fn_kqueue f_kq = (fn_kqueue)dlsym(lk, "kqueue");
  f_kevent = (fn_kevent)dlsym(lk, "kevent");
  void* f_add = dlsym(lk, "sceKernelAddFileEvent");
  void* f_del = dlsym(lk, "sceKernelDeleteFileEvent");
  void* f_ceq = dlsym(lk, "sceKernelCreateEqueue");
  void* f_weq = dlsym(lk, "sceKernelWaitEqueue");
  snprintf(extra, sizeof extra, "kqueue=%d kevent=%d sceKernelAddFileEvent=%d sceKernelDeleteFileEvent=%d sceKernelCreateEqueue=%d sceKernelWaitEqueue=%d", f_kq != 0, f_kevent != 0,
           f_add != 0, f_del != 0, f_ceq != 0, f_weq != 0);
  probe_case("0b", "dlsym of the functions (only existence for the Sony ones)", 0, 0, extra);
  if (!f_kq || !f_kevent) { probe_end(); return 0; }

  int kq = -1, fd = -1, fd2 = -1, dfd = -1, pfd[2] = {-1, -1};
  errno = 0;
  int mk = mkdir("/data/anyps5_probe/n2_17", 0700);
  int mke = errno;
  fd2 = open("/data/anyps5_probe/n2_17/vn.bin", O_CREAT | O_RDWR | O_EXCL, 0600);
  if (fd2 >= 0) (void)!write(fd2, "x", 1);
  fd = open("/data/anyps5_probe/n2_17/vn.bin", O_RDONLY);
  kq = f_kq();
  if (fd < 0 || fd2 < 0 || kq < 0) {
    snprintf(extra, sizeof extra, "mkdir_rc=%d (errno %d) fd=%d fd2=%d kq=%d", mk, mke, fd, fd2, kq);
    probe_case("1", "set up the directory, vn.bin, the descriptors and the kqueue", -1, 0, extra);
    goto cleanup;
  }
  {
    struct kevent o;
    int e;
    int r = reg(kq, fd, 0x37, EV_ADD | EV_ENABLE | EV_CLEAR, &o, &e);
    snprintf(extra, sizeof extra, "rc=%d errno=%d", r, e);
    probe_case("1", "kevent(EV_ADD|EV_ENABLE|EV_CLEAR, EVFILT_VNODE, fflags 0x37) on vn.bin", r, e, extra);
  }

  /* b) actions on the file */
  unsigned char big[4096];
  memset(big, 'a', sizeof big);
  (void)!write(fd2, big, 4096);
  action("2.1", "write of 4096 bytes (file grows)", kq);
  (void)!pwrite(fd2, "yy", 2, 0);
  action("2.2", "pwrite of 2 bytes at 0 (no growth)", kq);
  (void)!ftruncate(fd2, 100);
  action("2.3", "ftruncate to 100 (shrink)", kq);
  (void)!ftruncate(fd2, 8192);
  action("2.4", "ftruncate to 8192 (grow)", kq);
  (void)!fchmod(fd2, 0640);
  action("2.5", "fchmod 0640", kq);
  utimes("/data/anyps5_probe/n2_17/vn.bin", 0);
  action("2.6", "utimes of the path (now)", kq);
  int lr = link("/data/anyps5_probe/n2_17/vn.bin", "/data/anyps5_probe/n2_17/vn2.bin");
  action("2.7", "link to vn2.bin", kq);
  int ur = unlink("/data/anyps5_probe/n2_17/vn2.bin");
  action("2.8", "unlink of vn2.bin", kq);
  int rr = rename("/data/anyps5_probe/n2_17/vn.bin", "/data/anyps5_probe/n2_17/vn3.bin");
  action("2.9", "rename vn.bin to vn3.bin", kq);
  int dr = unlink("/data/anyps5_probe/n2_17/vn3.bin");
  action("2.10", "unlink of vn3.bin while the descriptor is open", kq);
  snprintf(extra, sizeof extra, "link_rc=%d unlink_rc=%d rename_rc=%d unlink_open_rc=%d", lr, ur, rr, dr);
  probe_case("2.11", "return codes of the file actions", 0, 0, extra);

  /* c) directory */
  dfd = open("/data/anyps5_probe/n2_17", O_RDONLY | O_DIRECTORY);
  if (dfd >= 0) {
    regcase("3.0", "kevent(EV_ADD..., fflags 0x37) on the directory", kq, dfd, 0x37, EV_ADD | EV_ENABLE | EV_CLEAR);
    int a = open("/data/anyps5_probe/n2_17/d1.bin", O_CREAT | O_RDWR, 0600);
    if (a >= 0) close(a);
    action("3.1", "create d1.bin in the directory", kq);
    int m = mkdir("/data/anyps5_probe/n2_17/dsub", 0700);
    action("3.2", "mkdir dsub", kq);
    int rd = rmdir("/data/anyps5_probe/n2_17/dsub");
    action("3.3", "rmdir dsub", kq);
    int rn = rename("/data/anyps5_probe/n2_17/d1.bin", "/data/anyps5_probe/n2_17/d2.bin");
    action("3.4", "rename d1.bin to d2.bin", kq);
    int ul = unlink("/data/anyps5_probe/n2_17/d2.bin");
    action("3.5", "unlink of d2.bin", kq);
    snprintf(extra, sizeof extra, "mkdir_rc=%d rmdir_rc=%d rename_rc=%d unlink_rc=%d", m, rd, rn, ul);
    probe_case("3.6", "return codes of the directory actions", 0, 0, extra);
    regcase("3.7", "kevent(EV_ADD, fflags 0x4 only) on the directory", kq, dfd, 0x4, EV_ADD | EV_ENABLE | EV_CLEAR);
    regcase("3.8", "kevent(EV_ADD, fflags NOTE_DELETE) on the directory", kq, dfd, 0x1, EV_ADD | EV_ENABLE | EV_CLEAR);
  }

  /* d) repeated registration, delete, closed descriptor, odd fflags. vn.bin was removed, a new file is made */
  int g2 = open("/data/anyps5_probe/n2_17/vn4.bin", O_CREAT | O_RDWR | O_EXCL, 0600);
  int g1 = open("/data/anyps5_probe/n2_17/vn4.bin", O_RDONLY);
  if (g1 >= 0 && g2 >= 0) {
    regcase("4.1", "EV_ADD on vn4.bin fflags 0x37", kq, g1, 0x37, EV_ADD | EV_ENABLE | EV_CLEAR);
    regcase("4.2", "EV_ADD on vn4.bin again with fflags 0x2", kq, g1, 0x2, EV_ADD | EV_ENABLE | EV_CLEAR);
    (void)!write(g2, "z", 1);
    {
      unsigned ff, fl;
      long d;
      long long took;
      struct kevent ev[4];
      struct timespec ts = {1, 0};
      memset(ev, 0, sizeof ev);
      long long t0 = now_ms();
      int n = f_kevent(kq, 0, 0, ev, 4, &ts);
      took = now_ms() - t0;
      ff = n > 0 ? ev[0].fflags : 0;
      fl = n > 0 ? ev[0].flags : 0;
      d = 0;
      snprintf(extra, sizeof extra, "events=%d first_fflags=0x%x second_fflags=0x%x time_ms=%lld (flags 0x%x)", n, ff, n > 1 ? ev[1].fflags : 0, took, fl);
      (void)d;
      probe_case("4.3", "write after the double registration", n, 0, extra);
    }
    regcase("4.4", "EV_DELETE on vn4.bin", kq, g1, 0, EV_DELETE);
    (void)!write(g2, "z", 1);
    {
      unsigned ff, fl;
      long d;
      long long took;
      int n = wait_ev(kq, 300, &ff, &fl, &d, &took);
      snprintf(extra, sizeof extra, "events=%d fflags=0x%x", n, ff);
      probe_case("4.5", "write after EV_DELETE (no event expected within 300 ms)", n, 0, extra);
    }
    regcase("4.6", "EV_ADD with fflags 0", kq, g1, 0, EV_ADD | EV_ENABLE | EV_CLEAR);
    regcase("4.7", "EV_ADD with fflags 0x80", kq, g1, 0x80, EV_ADD | EV_ENABLE | EV_CLEAR);
    regcase("4.8", "EV_ADD with fflags 0xFF", kq, g1, 0xFF, EV_ADD | EV_ENABLE | EV_CLEAR);
    int closedfd = g1;
    close(g1);
    g1 = -1;
    regcase("4.9", "EV_ADD on the descriptor that was just closed", kq, closedfd, 0x37, EV_ADD | EV_ENABLE | EV_CLEAR);
  }
  if (g1 >= 0) close(g1);
  if (g2 >= 0) close(g2);

  /* e) errors */
  regcase("5.1", "EV_ADD with ident -1", kq, -1, 0x37, EV_ADD);
  regcase("5.2", "EV_ADD with ident 999", kq, 999, 0x37, EV_ADD);
  if (pipe(pfd) == 0) regcase("5.3", "EV_ADD EVFILT_VNODE on a pipe descriptor", kq, pfd[0], 0x37, EV_ADD);

cleanup:
  {
    if (pfd[0] >= 0) close(pfd[0]);
    if (pfd[1] >= 0) close(pfd[1]);
    if (fd >= 0) close(fd);
    if (fd2 >= 0) close(fd2);
    if (dfd >= 0) close(dfd);
    if (kq >= 0) close(kq);
    int c1 = unlink("/data/anyps5_probe/n2_17/vn.bin");
    int c2 = unlink("/data/anyps5_probe/n2_17/vn2.bin");
    int c3 = unlink("/data/anyps5_probe/n2_17/vn3.bin");
    int c4 = unlink("/data/anyps5_probe/n2_17/vn4.bin");
    int c5 = unlink("/data/anyps5_probe/n2_17/d1.bin");
    int c6 = unlink("/data/anyps5_probe/n2_17/d2.bin");
    int c7 = rmdir("/data/anyps5_probe/n2_17/dsub");
    int c8 = rmdir("/data/anyps5_probe/n2_17");
    snprintf(extra, sizeof extra, "unlink vn=%d vn2=%d vn3=%d vn4=%d d1=%d d2=%d rmdir dsub=%d rmdir dir=%d (-1 = nothing to remove)", c1, c2, c3, c4, c5, c6, c7, c8);
    probe_case("9", "cleanup: close everything, remove every file and the directory", c8, 0, extra);
  }
  probe_end();
  return 0;
}
1st probe (2026-11-18, 07:05 UTC): stdout
PROBE	kqueue_vnode	fw=0x11600005	build=2025da6d594b	pid=280	utc=2026-11-18T08:47:53Z
CASE	0	load libkernel.sprx	0	2	loaded
CASE	0b	dlsym of the functions (only existence for the Sony ones)	0	0	kqueue=1 kevent=1 sceKernelAddFileEvent=1 sceKernelDeleteFileEvent=1 sceKernelCreateEqueue=1 sceKernelWaitEqueue=1
CASE	1	kevent(EV_ADD|EV_ENABLE|EV_CLEAR, EVFILT_VNODE, fflags 0x37) on vn.bin	0	0	rc=0 errno=0
CASE	2.1	write of 4096 bytes (file grows)	1	0	events=1 fflags=0x6 flags=0x20 data=0 time_ms=0
CASE	2.2	pwrite of 2 bytes at 0 (no growth)	1	0	events=1 fflags=0x2 flags=0x20 data=0 time_ms=0
CASE	2.3	ftruncate to 100 (shrink)	0	0	events=0 fflags=0x0 flags=0x0 data=0 time_ms=1000
CASE	2.4	ftruncate to 8192 (grow)	0	0	events=0 fflags=0x0 flags=0x0 data=0 time_ms=1000
CASE	2.5	fchmod 0640	0	0	events=0 fflags=0x0 flags=0x0 data=0 time_ms=1000
CASE	2.6	utimes of the path (now)	0	0	events=0 fflags=0x0 flags=0x0 data=0 time_ms=1000
CASE	2.7	link to vn2.bin	0	0	events=0 fflags=0x0 flags=0x0 data=0 time_ms=1000
CASE	2.8	unlink of vn2.bin	0	0	events=0 fflags=0x0 flags=0x0 data=0 time_ms=1001
CASE	2.9	rename vn.bin to vn3.bin	1	0	events=1 fflags=0x20 flags=0x20 data=0 time_ms=0
CASE	2.10	unlink of vn3.bin while the descriptor is open	1	0	events=1 fflags=0x1 flags=0x20 data=0 time_ms=0
CASE	2.11	return codes of the file actions	0	0	link_rc=45 unlink_rc=-1 rename_rc=0 unlink_open_rc=0
CASE	3.0	kevent(EV_ADD..., fflags 0x37) on the directory	0	0	rc=0 errno=0 first_event: flags=0x0 data=0
CASE	3.1	create d1.bin in the directory	1	0	events=1 fflags=0x2 flags=0x20 data=0 time_ms=0
CASE	3.2	mkdir dsub	1	0	events=1 fflags=0x12 flags=0x20 data=0 time_ms=0
CASE	3.3	rmdir dsub	1	0	events=1 fflags=0x12 flags=0x20 data=0 time_ms=0
CASE	3.4	rename d1.bin to d2.bin	1	0	events=1 fflags=0x2 flags=0x20 data=0 time_ms=0
CASE	3.5	unlink of d2.bin	1	0	events=1 fflags=0x2 flags=0x20 data=0 time_ms=0
CASE	3.6	return codes of the directory actions	0	0	mkdir_rc=0 rmdir_rc=0 rename_rc=0 unlink_rc=0
CASE	3.7	kevent(EV_ADD, fflags 0x4 only) on the directory	0	0	rc=0 errno=0 first_event: flags=0x0 data=0
CASE	3.8	kevent(EV_ADD, fflags NOTE_DELETE) on the directory	0	0	rc=0 errno=0 first_event: flags=0x0 data=0
CASE	4.1	EV_ADD on vn4.bin fflags 0x37	0	0	rc=0 errno=0 first_event: flags=0x0 data=0
CASE	4.2	EV_ADD on vn4.bin again with fflags 0x2	0	0	rc=0 errno=0 first_event: flags=0x0 data=0
CASE	4.3	write after the double registration	1	0	events=1 first_fflags=0x6 second_fflags=0x0 time_ms=0 (flags 0x20)
CASE	4.4	EV_DELETE on vn4.bin	0	0	rc=0 errno=0 first_event: flags=0x0 data=0
CASE	4.5	write after EV_DELETE (no event expected within 300 ms)	0	0	events=0 fflags=0x0
CASE	4.6	EV_ADD with fflags 0	0	0	rc=0 errno=0 first_event: flags=0x0 data=0
CASE	4.7	EV_ADD with fflags 0x80	0	0	rc=0 errno=0 first_event: flags=0x0 data=0
CASE	4.8	EV_ADD with fflags 0xFF	0	0	rc=0 errno=0 first_event: flags=0x0 data=0
CASE	4.9	EV_ADD on the descriptor that was just closed	1	0	rc=1 errno=0 first_event: flags=0x4000 data=9
CASE	5.1	EV_ADD with ident -1	1	0	rc=1 errno=0 first_event: flags=0x4000 data=9
CASE	5.2	EV_ADD with ident 999	1	0	rc=1 errno=0 first_event: flags=0x4000 data=9
CASE	5.3	EV_ADD EVFILT_VNODE on a pipe descriptor	1	0	rc=1 errno=0 first_event: flags=0x4000 data=22
CASE	9	cleanup: close everything, remove every file and the directory	0	0	unlink vn=-1 vn2=-1 vn3=-1 vn4=0 d1=-1 d2=-1 rmdir dsub=-1 rmdir dir=0 (-1 = nothing to remove)
END	36
2nd probe (09:08 UTC): probe.c (build ee49b1522d34)
/* kevent EVFILT_VNODE, re-registration with clearly different fflags sets and the attribute hint with fflags 0x3f, follow-up, payload process, firmware 11.60 (debt
   libkernel.md entry: EVFILT_VNODE, sceKernelAddFileEvent and sceKernelDeleteFileEvent watch the host file or directory behind the descriptor and turn host changes into FreeBSD 11 vnode hints,
   which filt_vfsvnode adds whole when they share a bit with the requested fflags; a growing ftruncate reports NOTE_WRITE | NOTE_EXTEND where vop_setattr_post reports NOTE_ATTRIB). The first
   run (kqueue_vnode) measured on the console: a growing write 0x6, a write without growth 0x2, rename 0x20, unlink of an open file 0x1, no event for ftruncate (shrink and grow), fchmod and utimes with the fflags 0x37, and
   after registering 0x37 and then 0x2 for the same descriptor a write that still reported 0x6; GL1-1042 asked for a follow-up with clearly different sets and with fflags 0x3f for the attribute hint. The probe looks kqueue and kevent up with dlsym in
   libkernel.sprx and uses only the POSIX interface. Everything happens in /data/anyps5_probe/n2_17 (created and removed by the probe), one fresh file and one fresh kqueue per scenario (EV_ADD|EV_ENABLE|EV_CLEAR), each action followed by a kevent wait of at most 400 ms, the result as events,
   fflags in hex: S1: EV_ADD with fflags 0x2, then EV_ADD again with fflags 0x4, then a growing write, a write without growth, a rename, an unlink of the renamed file; S2: 0x4 then 0x2 with the same actions; S3: 0x1 (NOTE_DELETE) then 0x20 (NOTE_RENAME) with a rename and an unlink; S4: 0x3f
   registered once with the actions ftruncate to a smaller size, ftruncate to a larger size, fchmod, utimes of the path, a growing write, a write without growth, a rename, an unlink while the descriptor is open; S5: 0x8 (NOTE_ATTRIB) alone with the same attribute actions. Every
   descriptor is closed and every file and directory removed at the end. A fault ends the probe at once with _exit. Nothing is written outside the probe directory, nothing is sent. */
#include <dlfcn.h>
#include <errno.h>
#include <pthread.h>
#include <signal.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <ps5/kernel.h>   /* only for kernel_get_fw_version() */

#ifndef PROBE_NAME
#define PROBE_NAME "unnamed"
#endif
#ifndef PROBE_BUILD
#define PROBE_BUILD "unbuilt"
#endif
/* Self limit: the runner can only close the connection, it cannot end the process on the console, so the probe ends
   itself after PROBE_MAX_S seconds at the latest (set by the build script). */
#ifndef PROBE_MAX_S
#define PROBE_MAX_S 50
#endif

static volatile int g_handle = -1;
static int (*volatile g_setmargins)(int, int, int) = 0; /* unused here */
static void reset_rate(void) {
  if (g_setmargins && g_handle >= 0) g_setmargins(g_handle, 0, 0);
}

static void probe_alarm(int sig) {
  static const char msg[] = "TIMEOUT\tprobe self limit\n";
  (void)sig;
  (void)!write(1, msg, sizeof msg - 1);
  reset_rate();
  _exit(124);
}

/* Watchdog thread: alarm()/SIGALRM does not help when the main thread hangs in a blocking system call or IPC. The
   thread sleeps PROBE_MAX_S seconds, writes TIMEOUT with write() and ends the process with _exit(124). alarm() stays
   as a second safeguard. */
static void* probe_watchdog(void* arg) {
  static const char msg[] = "TIMEOUT\tprobe watchdog thread\n";
  unsigned left = PROBE_MAX_S;
  (void)arg;
  while (left) left = sleep(left);
  (void)!write(1, msg, sizeof msg - 1);
  reset_rate();
  _exit(124);
  return 0;
}

static void probe_watchdog_start(void) {
  pthread_t t;
  if (pthread_create(&t, 0, probe_watchdog, 0) == 0) pthread_detach(t);
}

static int cases = 0;

static void probe_header(void) {
  char utc[32];
  time_t now = time(0);
  probe_watchdog_start();
  struct tm tmv;
  gmtime_r(&now, &tmv);
  strftime(utc, sizeof utc, "%Y-%m-%dT%H:%M:%SZ", &tmv);
  printf("PROBE\t%s\tfw=0x%08x\tbuild=%s\tpid=%d\tutc=%s\n", PROBE_NAME, kernel_get_fw_version(), PROBE_BUILD,
         getpid(), utc);
  fflush(stdout);
}

static void probe_case(const char* id, const char* call, long ret, int err, const char* extra) {
  printf("CASE\t%s\t%s\t%ld\t%d\t%s\n", id, call, ret, err, extra ? extra : "-");
  fflush(stdout);
  cases++;
}

static void probe_end(void) {
  printf("END\t%d\n", cases);
  fflush(stdout);
}



#include <fcntl.h>
#include <sys/event.h>
#include <sys/stat.h>
#include <sys/time.h>

typedef int (*fn_kqueue)(void);
typedef int (*fn_kevent)(int, const struct kevent*, int, struct kevent*, int, const struct timespec*);

static fn_kevent f_kevent;

static void fault(int sig) {
  static const char msg[] = "FAULT\tsignal in the call\n";
  (void)sig;
  (void)!write(1, msg, sizeof msg - 1);
  _exit(125);
}

static int reg(int kq, int ident, unsigned fflags, int flags) {
  struct kevent ch, ev[2];
  EV_SET(&ch, ident, EVFILT_VNODE, flags, fflags, 0, 0);
  struct timespec ts0 = {0, 0};
  memset(ev, 0, sizeof ev);
  return f_kevent(kq, &ch, 1, ev, 2, &ts0);
}

static void act(const char* scen, int n, const char* label, int kq) {
  struct kevent ev[4];
  struct timespec ts = {0, 400000000L};
  memset(ev, 0, sizeof ev);
  int r = f_kevent(kq, 0, 0, ev, 4, &ts);
  char id[16], extra[120];
  snprintf(id, sizeof id, "%s.%d", scen, n);
  snprintf(extra, sizeof extra, "events=%d fflags=0x%x second_event_fflags=0x%x", r, r > 0 ? ev[0].fflags : 0, r > 1 ? ev[1].fflags : 0);
  probe_case(id, label, r, 0, extra);
}

static void scenario(const char* scen, const char* title, unsigned first, unsigned second, int attr_actions) {
  char extra[200], id[16];
  unlink("/data/anyps5_probe/n2_17/k2.bin");
  unlink("/data/anyps5_probe/n2_17/k2r.bin");
  int w = open("/data/anyps5_probe/n2_17/k2.bin", O_CREAT | O_RDWR | O_EXCL, 0600);
  if (w >= 0) (void)!write(w, "x", 1);
  int r = open("/data/anyps5_probe/n2_17/k2.bin", O_RDONLY);
  int kq = ((fn_kqueue)dlsym(RTLD_DEFAULT, "kqueue"))();
  if (w < 0 || r < 0 || kq < 0) {
    snprintf(extra, sizeof extra, "w=%d r=%d kq=%d", w, r, kq);
    snprintf(id, sizeof id, "%s.0", scen);
    probe_case(id, title, -1, 0, extra);
    if (w >= 0) close(w);
    if (r >= 0) close(r);
    if (kq >= 0) close(kq);
    unlink("/data/anyps5_probe/n2_17/k2.bin");
    return;
  }
  int a = reg(kq, r, first, EV_ADD | EV_ENABLE | EV_CLEAR);
  int b = second ? reg(kq, r, second, EV_ADD | EV_ENABLE | EV_CLEAR) : 0;
  snprintf(extra, sizeof extra, "first_fflags=0x%x add_rc=%d second_fflags=0x%x add_rc=%d", first, a, second, b);
  snprintf(id, sizeof id, "%s.0", scen);
  probe_case(id, title, 0, 0, extra);
  int n = 1;
  if (attr_actions) {
    (void)!ftruncate(w, 0);
    act(scen, n++, "ftruncate to 0 (shrink)", kq);
    (void)!ftruncate(w, 8192);
    act(scen, n++, "ftruncate to 8192 (grow)", kq);
    (void)!fchmod(w, 0640);
    act(scen, n++, "fchmod 0640", kq);
    utimes("/data/anyps5_probe/n2_17/k2.bin", 0);
    act(scen, n++, "utimes of the path (now)", kq);
  }
  unsigned char big[4096];
  memset(big, 'a', sizeof big);
  (void)!pwrite(w, big, 4096, 8192);
  act(scen, n++, "pwrite of 4096 bytes behind the end (grows)", kq);
  (void)!pwrite(w, "yy", 2, 0);
  act(scen, n++, "pwrite of 2 bytes at 0 (no growth)", kq);
  rename("/data/anyps5_probe/n2_17/k2.bin", "/data/anyps5_probe/n2_17/k2r.bin");
  act(scen, n++, "rename to k2r.bin", kq);
  unlink("/data/anyps5_probe/n2_17/k2r.bin");
  act(scen, n++, "unlink of k2r.bin while the descriptor is open", kq);
  close(w);
  close(r);
  close(kq);
  unlink("/data/anyps5_probe/n2_17/k2.bin");
  unlink("/data/anyps5_probe/n2_17/k2r.bin");
}

int main(void) {
  signal(SIGALRM, probe_alarm);
  signal(SIGSEGV, fault);
  signal(SIGBUS, fault);
  alarm(PROBE_MAX_S);
  probe_header();
  char extra[200];

  errno = 0;
  void* lk = dlopen("libkernel.sprx", RTLD_LAZY);
  probe_case("0", "load libkernel.sprx", lk ? 0 : -1, errno, lk ? "loaded" : dlerror());
  if (!lk) { probe_end(); return 0; }
  fn_kqueue f_kq = (fn_kqueue)dlsym(lk, "kqueue");
  f_kevent = (fn_kevent)dlsym(lk, "kevent");
  snprintf(extra, sizeof extra, "kqueue=%d kevent=%d", f_kq != 0, f_kevent != 0);
  probe_case("0b", "dlsym of the functions", 0, 0, extra);
  if (!f_kq || !f_kevent) { probe_end(); return 0; }

  errno = 0;
  int mk = mkdir("/data/anyps5_probe/n2_17", 0700);
  snprintf(extra, sizeof extra, "mkdir_rc=%d errno=%d", mk, errno);
  probe_case("1", "make the probe subdirectory", mk == 0 || errno == EEXIST ? 0 : -1, errno, extra);

  scenario("S1", "EV_ADD fflags 0x2 then EV_ADD fflags 0x4", 0x2, 0x4, 0);
  scenario("S2", "EV_ADD fflags 0x4 then EV_ADD fflags 0x2", 0x4, 0x2, 0);
  scenario("S3", "EV_ADD fflags 0x1 then EV_ADD fflags 0x20", 0x1, 0x20, 0);
  scenario("S4", "EV_ADD fflags 0x3f once, attribute actions", 0x3f, 0, 1);
  scenario("S5", "EV_ADD fflags 0x8 (ATTRIB) alone, attribute actions", 0x8, 0, 1);

  int c1 = unlink("/data/anyps5_probe/n2_17/k2.bin");
  int c2 = unlink("/data/anyps5_probe/n2_17/k2r.bin");
  int c3 = rmdir("/data/anyps5_probe/n2_17");
  snprintf(extra, sizeof extra, "unlink k2=%d k2r=%d rmdir dir=%d (-1 = nothing to remove)", c1, c2, c3);
  probe_case("9", "cleanup: remove the files and the directory", c3, 0, extra);
  probe_end();
  return 0;
}
2nd probe (09:08 UTC): stdout
PROBE	kqueue_vnode2	fw=0x11600005	build=ee49b1522d34	pid=295	utc=2026-11-18T10:50:15Z
CASE	0	load libkernel.sprx	0	2	loaded
CASE	0b	dlsym of the functions	0	0	kqueue=1 kevent=1
CASE	1	make the probe subdirectory	0	0	mkdir_rc=0 errno=0
CASE	S1.0	EV_ADD fflags 0x2 then EV_ADD fflags 0x4	0	0	first_fflags=0x2 add_rc=0 second_fflags=0x4 add_rc=0
CASE	S1.1	pwrite of 4096 bytes behind the end (grows)	1	0	events=1 fflags=0x6 second_event_fflags=0x0
CASE	S1.2	pwrite of 2 bytes at 0 (no growth)	0	0	events=0 fflags=0x0 second_event_fflags=0x0
CASE	S1.3	rename to k2r.bin	0	0	events=0 fflags=0x0 second_event_fflags=0x0
CASE	S1.4	unlink of k2r.bin while the descriptor is open	0	0	events=0 fflags=0x0 second_event_fflags=0x0
CASE	S2.0	EV_ADD fflags 0x4 then EV_ADD fflags 0x2	0	0	first_fflags=0x4 add_rc=0 second_fflags=0x2 add_rc=0
CASE	S2.1	pwrite of 4096 bytes behind the end (grows)	1	0	events=1 fflags=0x6 second_event_fflags=0x0
CASE	S2.2	pwrite of 2 bytes at 0 (no growth)	1	0	events=1 fflags=0x2 second_event_fflags=0x0
CASE	S2.3	rename to k2r.bin	0	0	events=0 fflags=0x0 second_event_fflags=0x0
CASE	S2.4	unlink of k2r.bin while the descriptor is open	0	0	events=0 fflags=0x0 second_event_fflags=0x0
CASE	S3.0	EV_ADD fflags 0x1 then EV_ADD fflags 0x20	0	0	first_fflags=0x1 add_rc=0 second_fflags=0x20 add_rc=0
CASE	S3.1	pwrite of 4096 bytes behind the end (grows)	0	0	events=0 fflags=0x0 second_event_fflags=0x0
CASE	S3.2	pwrite of 2 bytes at 0 (no growth)	0	0	events=0 fflags=0x0 second_event_fflags=0x0
CASE	S3.3	rename to k2r.bin	1	0	events=1 fflags=0x20 second_event_fflags=0x0
CASE	S3.4	unlink of k2r.bin while the descriptor is open	0	0	events=0 fflags=0x0 second_event_fflags=0x0
CASE	S4.0	EV_ADD fflags 0x3f once, attribute actions	0	0	first_fflags=0x3f add_rc=0 second_fflags=0x0 add_rc=0
CASE	S4.1	ftruncate to 0 (shrink)	1	0	events=1 fflags=0x8 second_event_fflags=0x0
CASE	S4.2	ftruncate to 8192 (grow)	1	0	events=1 fflags=0x8 second_event_fflags=0x0
CASE	S4.3	fchmod 0640	1	0	events=1 fflags=0x8 second_event_fflags=0x0
CASE	S4.4	utimes of the path (now)	1	0	events=1 fflags=0x8 second_event_fflags=0x0
CASE	S4.5	pwrite of 4096 bytes behind the end (grows)	1	0	events=1 fflags=0x6 second_event_fflags=0x0
CASE	S4.6	pwrite of 2 bytes at 0 (no growth)	1	0	events=1 fflags=0x2 second_event_fflags=0x0
CASE	S4.7	rename to k2r.bin	1	0	events=1 fflags=0x20 second_event_fflags=0x0
CASE	S4.8	unlink of k2r.bin while the descriptor is open	1	0	events=1 fflags=0x1 second_event_fflags=0x0
CASE	S5.0	EV_ADD fflags 0x8 (ATTRIB) alone, attribute actions	0	0	first_fflags=0x8 add_rc=0 second_fflags=0x0 add_rc=0
CASE	S5.1	ftruncate to 0 (shrink)	1	0	events=1 fflags=0x8 second_event_fflags=0x0
CASE	S5.2	ftruncate to 8192 (grow)	1	0	events=1 fflags=0x8 second_event_fflags=0x0
CASE	S5.3	fchmod 0640	1	0	events=1 fflags=0x8 second_event_fflags=0x0
CASE	S5.4	utimes of the path (now)	1	0	events=1 fflags=0x8 second_event_fflags=0x0
CASE	S5.5	pwrite of 4096 bytes behind the end (grows)	0	0	events=0 fflags=0x0 second_event_fflags=0x0
CASE	S5.6	pwrite of 2 bytes at 0 (no growth)	0	0	events=0 fflags=0x0 second_event_fflags=0x0
CASE	S5.7	rename to k2r.bin	0	0	events=0 fflags=0x0 second_event_fflags=0x0
CASE	S5.8	unlink of k2r.bin while the descriptor is open	0	0	events=0 fflags=0x0 second_event_fflags=0x0
CASE	9	cleanup: remove the files and the directory	0	0	unlink k2=-1 k2r=-1 rmdir dir=0 (-1 = nothing to remove)
END	37
3rd probe (11:06 UTC): probe.c (build aaa5a61e79c1)
/* kevent EVFILT_VNODE with fflags 0x3f, ftruncate to the size the file already has, follow-up for GL1-1060 (kqueue_vnode2 measured on the console with fflags 0x3f: ftruncate to a smaller size, ftruncate to a larger
   size, fchmod and utimes each give exactly NOTE_ATTRIB 0x8, a pwrite behind the end 0x6, a pwrite without growth 0x2; not measured: ftruncate to the size the file already has). The probe looks kqueue and kevent up with dlsym in libkernel.sprx
   and uses only the POSIX interface; everything happens in /data/anyps5_probe/n2_17 (created and removed by the probe), one fresh file and one fresh kqueue (EV_ADD|EV_ENABLE|EV_CLEAR, fflags 0x3f), each action followed by a kevent wait of
   at most 400 ms, the result as events and fflags in hex: pwrite of 4096 bytes behind the end of a one-byte file (the event is read and counted), ftruncate to 4097 (the size the file now has), the same again, fchmod to the mode the file already has (0600),
   ftruncate to 8192 (grows), ftruncate to 8192 again, ftruncate to 100 (shrinks), ftruncate to 100 again, a pwrite of 2 bytes at 0 (no growth), ftruncate to 100 again after that write, utimes of the path. Every descriptor is closed and every file
   and the directory removed at the end. A fault ends the probe at once with _exit. Nothing is written outside the probe directory, nothing is sent. */
#include <dlfcn.h>
#include <errno.h>
#include <pthread.h>
#include <signal.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <ps5/kernel.h>   /* only for kernel_get_fw_version() */

#ifndef PROBE_NAME
#define PROBE_NAME "unnamed"
#endif
#ifndef PROBE_BUILD
#define PROBE_BUILD "unbuilt"
#endif
/* Self limit: the runner can only close the connection, it cannot end the process on the console, so the probe ends
   itself after PROBE_MAX_S seconds at the latest (set by the build script). */
#ifndef PROBE_MAX_S
#define PROBE_MAX_S 50
#endif

static volatile int g_handle = -1;
static int (*volatile g_setmargins)(int, int, int) = 0; /* unused here */
static void reset_rate(void) {
  if (g_setmargins && g_handle >= 0) g_setmargins(g_handle, 0, 0);
}

static void probe_alarm(int sig) {
  static const char msg[] = "TIMEOUT\tprobe self limit\n";
  (void)sig;
  (void)!write(1, msg, sizeof msg - 1);
  reset_rate();
  _exit(124);
}

/* Watchdog thread: alarm()/SIGALRM does not help when the main thread hangs in a blocking system call or IPC. The
   thread sleeps PROBE_MAX_S seconds, writes TIMEOUT with write() and ends the process with _exit(124). alarm() stays
   as a second safeguard. */
static void* probe_watchdog(void* arg) {
  static const char msg[] = "TIMEOUT\tprobe watchdog thread\n";
  unsigned left = PROBE_MAX_S;
  (void)arg;
  while (left) left = sleep(left);
  (void)!write(1, msg, sizeof msg - 1);
  reset_rate();
  _exit(124);
  return 0;
}

static void probe_watchdog_start(void) {
  pthread_t t;
  if (pthread_create(&t, 0, probe_watchdog, 0) == 0) pthread_detach(t);
}

static int cases = 0;

static void probe_header(void) {
  char utc[32];
  time_t now = time(0);
  probe_watchdog_start();
  struct tm tmv;
  gmtime_r(&now, &tmv);
  strftime(utc, sizeof utc, "%Y-%m-%dT%H:%M:%SZ", &tmv);
  printf("PROBE\t%s\tfw=0x%08x\tbuild=%s\tpid=%d\tutc=%s\n", PROBE_NAME, kernel_get_fw_version(), PROBE_BUILD,
         getpid(), utc);
  fflush(stdout);
}

static void probe_case(const char* id, const char* call, long ret, int err, const char* extra) {
  printf("CASE\t%s\t%s\t%ld\t%d\t%s\n", id, call, ret, err, extra ? extra : "-");
  fflush(stdout);
  cases++;
}

static void probe_end(void) {
  printf("END\t%d\n", cases);
  fflush(stdout);
}



#include <fcntl.h>
#include <sys/event.h>
#include <sys/stat.h>
#include <sys/time.h>

typedef int (*fn_kqueue)(void);
typedef int (*fn_kevent)(int, const struct kevent*, int, struct kevent*, int, const struct timespec*);

static fn_kevent f_kevent;

static void fault(int sig) {
  static const char msg[] = "FAULT\tsignal in the call\n";
  (void)sig;
  (void)!write(1, msg, sizeof msg - 1);
  _exit(125);
}

static int reg(int kq, int ident, unsigned fflags, int flags) {
  struct kevent ch, ev[2];
  EV_SET(&ch, ident, EVFILT_VNODE, flags, fflags, 0, 0);
  struct timespec ts0 = {0, 0};
  memset(ev, 0, sizeof ev);
  return f_kevent(kq, &ch, 1, ev, 2, &ts0);
}

static void act(const char* scen, int n, const char* label, int kq) {
  struct kevent ev[4];
  struct timespec ts = {0, 400000000L};
  memset(ev, 0, sizeof ev);
  int r = f_kevent(kq, 0, 0, ev, 4, &ts);
  char id[16], extra[120];
  snprintf(id, sizeof id, "%s.%d", scen, n);
  snprintf(extra, sizeof extra, "events=%d fflags=0x%x second_event_fflags=0x%x", r, r > 0 ? ev[0].fflags : 0, r > 1 ? ev[1].fflags : 0);
  probe_case(id, label, r, 0, extra);
}

static void scenario(void) {
  char extra[200];
  unlink("/data/anyps5_probe/n2_17/k3.bin");
  int w = open("/data/anyps5_probe/n2_17/k3.bin", O_CREAT | O_RDWR | O_EXCL, 0600);
  if (w >= 0) (void)!write(w, "x", 1);
  int r = open("/data/anyps5_probe/n2_17/k3.bin", O_RDONLY);
  int kq = ((fn_kqueue)dlsym(RTLD_DEFAULT, "kqueue"))();
  if (w < 0 || r < 0 || kq < 0) {
    snprintf(extra, sizeof extra, "w=%d r=%d kq=%d", w, r, kq);
    probe_case("S.0", "set up the file, the descriptors and the kqueue", -1, 0, extra);
    if (w >= 0) close(w);
    if (r >= 0) close(r);
    if (kq >= 0) close(kq);
    unlink("/data/anyps5_probe/n2_17/k3.bin");
    return;
  }
  int a = reg(kq, r, 0x3f, EV_ADD | EV_ENABLE | EV_CLEAR);
  snprintf(extra, sizeof extra, "fflags=0x3f add_rc=%d", a);
  probe_case("S.0", "EV_ADD fflags 0x3f", a, 0, extra);
  unsigned char big[4096];
  memset(big, 'a', sizeof big);
  int n = 1;
  (void)!pwrite(w, big, 4096, 1);
  act("S", n++, "pwrite of 4096 bytes behind the end (file is 4097 bytes now)", kq);
  (void)!ftruncate(w, 4097);
  act("S", n++, "ftruncate to 4097 (the size it has)", kq);
  (void)!ftruncate(w, 4097);
  act("S", n++, "ftruncate to 4097 again", kq);
  (void)!fchmod(w, 0600);
  act("S", n++, "fchmod to 0600 (the mode it has)", kq);
  (void)!ftruncate(w, 8192);
  act("S", n++, "ftruncate to 8192 (grows)", kq);
  (void)!ftruncate(w, 8192);
  act("S", n++, "ftruncate to 8192 again (the size it has)", kq);
  (void)!ftruncate(w, 100);
  act("S", n++, "ftruncate to 100 (shrinks)", kq);
  (void)!ftruncate(w, 100);
  act("S", n++, "ftruncate to 100 again (the size it has)", kq);
  (void)!pwrite(w, "yy", 2, 0);
  act("S", n++, "pwrite of 2 bytes at 0 (no growth)", kq);
  (void)!ftruncate(w, 100);
  act("S", n++, "ftruncate to 100 after that write (the size it has)", kq);
  utimes("/data/anyps5_probe/n2_17/k3.bin", 0);
  act("S", n++, "utimes of the path (now)", kq);
  close(w);
  close(r);
  close(kq);
  unlink("/data/anyps5_probe/n2_17/k3.bin");
}

int main(void) {
  signal(SIGALRM, probe_alarm);
  signal(SIGSEGV, fault);
  signal(SIGBUS, fault);
  alarm(PROBE_MAX_S);
  probe_header();
  char extra[200];

  errno = 0;
  void* lk = dlopen("libkernel.sprx", RTLD_LAZY);
  probe_case("0", "load libkernel.sprx", lk ? 0 : -1, errno, lk ? "loaded" : dlerror());
  if (!lk) { probe_end(); return 0; }
  fn_kqueue f_kq = (fn_kqueue)dlsym(lk, "kqueue");
  f_kevent = (fn_kevent)dlsym(lk, "kevent");
  snprintf(extra, sizeof extra, "kqueue=%d kevent=%d", f_kq != 0, f_kevent != 0);
  probe_case("0b", "dlsym of the functions", 0, 0, extra);
  if (!f_kq || !f_kevent) { probe_end(); return 0; }

  errno = 0;
  int mk = mkdir("/data/anyps5_probe/n2_17", 0700);
  snprintf(extra, sizeof extra, "mkdir_rc=%d errno=%d", mk, errno);
  probe_case("1", "make the probe subdirectory", mk == 0 || errno == EEXIST ? 0 : -1, errno, extra);

  scenario();

  int c1 = unlink("/data/anyps5_probe/n2_17/k3.bin");
  int c2 = 0;
  int c3 = rmdir("/data/anyps5_probe/n2_17");
  snprintf(extra, sizeof extra, "unlink k3=%d (unused=%d) rmdir dir=%d (-1 = nothing to remove)", c1, c2, c3);
  probe_case("9", "cleanup: remove the files and the directory", c3, 0, extra);
  probe_end();
  return 0;
}
3rd probe (11:06 UTC): stdout
PROBE	kqueue_ftruncate_same	fw=0x11600005	build=aaa5a61e79c1	pid=299	utc=2026-11-18T12:48:19Z
CASE	0	load libkernel.sprx	0	2	loaded
CASE	0b	dlsym of the functions	0	0	kqueue=1 kevent=1
CASE	1	make the probe subdirectory	0	0	mkdir_rc=0 errno=0
CASE	S.0	EV_ADD fflags 0x3f	0	0	fflags=0x3f add_rc=0
CASE	S.1	pwrite of 4096 bytes behind the end (file is 4097 bytes now)	1	0	events=1 fflags=0x6 second_event_fflags=0x0
CASE	S.2	ftruncate to 4097 (the size it has)	1	0	events=1 fflags=0x8 second_event_fflags=0x0
CASE	S.3	ftruncate to 4097 again	1	0	events=1 fflags=0x8 second_event_fflags=0x0
CASE	S.4	fchmod to 0600 (the mode it has)	1	0	events=1 fflags=0x8 second_event_fflags=0x0
CASE	S.5	ftruncate to 8192 (grows)	1	0	events=1 fflags=0x8 second_event_fflags=0x0
CASE	S.6	ftruncate to 8192 again (the size it has)	1	0	events=1 fflags=0x8 second_event_fflags=0x0
CASE	S.7	ftruncate to 100 (shrinks)	1	0	events=1 fflags=0x8 second_event_fflags=0x0
CASE	S.8	ftruncate to 100 again (the size it has)	1	0	events=1 fflags=0x8 second_event_fflags=0x0
CASE	S.9	pwrite of 2 bytes at 0 (no growth)	1	0	events=1 fflags=0x2 second_event_fflags=0x0
CASE	S.10	ftruncate to 100 after that write (the size it has)	1	0	events=1 fflags=0x8 second_event_fflags=0x0
CASE	S.11	utimes of the path (now)	1	0	events=1 fflags=0x8 second_event_fflags=0x0
CASE	9	cleanup: remove the files and the directory	0	0	unlink k3=-1 (unused=0) rmdir dir=0 (-1 = nothing to remove)
END	16

AI-assisted (Claude Code).

@oneandonlydean
oneandonlydean merged commit 6830fe7 into boykopovar:main Nov 18, 2026
12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants