2 Commits

2 changed files with 96 additions and 10 deletions

82
test/Manual experiment.md Normal file
View File

@@ -0,0 +1,82 @@
## Manual experiment
> [!NOTE]
> This manual experiment shows how we can do the testing (teardown not included). Note that we don't need the `losetup`-stuff, we know where everything is.
### Compile
```sh
gcc fs-watcher.c json-writer.c -o fa2json
```
### Create image file
```sh
mktemp /tmp/fa2json-test-XXXXXX.img
```
```text
/tmp/fa2json-test-UrwpOb.img
```
```sh
truncate -s 10M /tmp/fa2json-test-UrwpOb.img
```
```sh
mkfs.ext4 /tmp/fa2json-test-UrwpOb.img
```
```text
mke2fs 1.47.3 (8-Jul-2025)
Discarding device blocks: done
Creating filesystem with 10240 1k blocks and 2560 inodes
Filesystem UUID: 035c508e-dec0-4a21-a4d1-1efb6fa72415
Superblock backups stored on blocks:
8193
Allocating group tables: done
Writing inode tables: done
Creating journal (1024 blocks): done
Writing superblocks and filesystem accounting information: done
```
### Create mount point
```sh
mktemp -d /tmp/fa2json-mnt-XXXXXX
```
```text
/tmp/fa2json-mnt-ts2Dik
```
### Mount loop device
```sh
sudo mount /tmp/fa2json-test-UrwpOb.img /tmp/fa2json-mnt-ts2Dik/
```
> [!NOTE]
> In a different terminal I now ran - but we could do this after `chown` or possibly `chown` + `sync`?
> ```sh
> fa2json /tmp/fa2json-mnt-ts2Dik
> ```
### Let current user own file system
```sh
sudo chown $(id -u) /tmp/fa2json-mnt-ts2Dik/
```
#### `fa2json` output
```json
{"ts": [1772658052, 704412412, 386988, 865842867], "name": "/tmp/fa2json-mnt-ts2Dik/.", "mask": 1073741828}
```
### Touch marker
```sh
touch /tmp/fa2json-mnt-ts2Dik/MARKER
```
#### `fa2json` output
```json
{"ts": [1772658064, 151070715, 387000, 312501190], "name": "/tmp/fa2json-mnt-ts2Dik/MARKER", "mask": 256}
{"ts": [1772658064, 151099105, 387000, 312529600], "name": "/tmp/fa2json-mnt-ts2Dik/MARKER", "mask": 12}
```

View File

@@ -21,15 +21,20 @@ Requires root (`fanotify` FID reporting and `mount` both need `CAP_SYS_ADMIN`).
## Setup
1. Create a temporary image file (`mktemp`)
2. `dd` 10M of zeros into it
1. Create a temporary image file (`mktemp /tmp/fa2json-test-XXXXXX.img`)
2. `truncate -s 10M` the image (sparse file, no need for `dd`)
3. `mkfs.ext4` the image
4. `losetup --find --show` to attach it as a loop device
5. `mount` the loop device to a temporary directory (`mktemp -d`)
6. Spawn `fa2json <mountpoint>` as a child process
7. Attach a `readline` interface to its stdout; parse each line as JSON and
4. Create a temporary mount directory (`mktemp -d /tmp/fa2json-mnt-XXXXXX`)
5. `sudo mount <img> <mntdir>` (no `losetup` needed — `mount` accepts image files directly)
6. `sudo chown $(id -u) <mntdir>` to hand ownership to the current user
7. `sync` to flush before fa2json starts listening
8. `sudo` spawn `fa2json <mountpoint>` as a child process (needs `CAP_SYS_ADMIN`)
9. Attach a `readline` interface to its stdout; parse each line as JSON and
push into an event buffer
Steps 6 and 7 ensure the `chown` event never enters the fa2json stream, and
all subsequent FS operations run unprivileged.
---
## Teardown
@@ -37,10 +42,9 @@ Requires root (`fanotify` FID reporting and `mount` both need `CAP_SYS_ADMIN`).
Runs unconditionally in a `finally` block:
1. Kill the `fa2json` child process
2. `umount <mountpoint>`
3. `losetup -d <loopdev>`
4. `rm` the image file
5. `rmdir` the mount directory
2. `sudo umount <mountpoint>`
3. `rm` the image file
4. `rmdir` the mount directory
---