From f4ec4a1da182afca6ecfcf7f289b630b2df1b558 Mon Sep 17 00:00:00 2001 From: lotherk Date: Mon, 16 Nov 2026 13:56:06 +0000 Subject: [PATCH] MCDE: the disk-removed warning through the kernel's device events (t0093) A medium that is pulled while it is still mounted now gets the reference's one-button "Disk Not Ejected" warning: Motif's error dialog, the device and its stale mountpoint in the text, the Warning sound. The desktop (dtwm, WmMcdeDesktop.c) owns the event descriptor already; it reads each event, asks what it says, and checks the mount table before it shows anything. McdeSys: - mcde_sys_disk_event_info() parses both shapes the read gives: Linux's uevent with its NULs turned into spaces ("remove@/... ACTION=remove DEVNAME=sdb1 ...") and NetBSD's "device-detach sd0"; 1 when the device went away, 0 for add/change/attach, -1 on bad arguments. - mcde_sys_disk_detached(name, mountpoint, len): looks in the mount table for a partition of NAME (sdb takes sdb1, sd0 takes sd0a), copies the mountpoint and unmounts it the forced way -- "umount -l" on Linux, "umount -f" on NetBSD (disk_linux.c, disk_netbsd.c) -- but never what the computer runs from. 0 when nothing of NAME was mounted. - test_disk_events.c: the uevent and drvctl shapes, add/change/attach, missing DEVNAME, the bad arguments. make check: 8/8 PASS (the seven before and this one). Measured under Xvfb :79 with a real ext4 loop mount at /tmp/opencode/mnt: - start of dtwm with MCDE_DESK_TEST_DEVICE="remove@/... DEVNAME=loop0": log "desktop: loop0 (remove)", "umount: /tmp/opencode/mnt: must be superuser to unmount.", "desktop: loop0 was mounted at /tmp/opencode/mnt"; the dialog is 354x167 at (335,300), OK as the only button (docs/screenshots/leopard/Device-Removal-mcde.png). - sudo umount /tmp/opencode/mnt, the same event again: log "desktop: loop0 (remove)" and no "was mounted"; zero dialog windows -- a device that was put away stays quiet. - make mcde-linux MCDE_DIRS="install lib/McdeSys programs/dtwm" exit=0; MCDE_DIRS=plist "mcde-plist-check: ok". MCDE_DESK_TEST_DEVICE is the test hook: one event line through the same handler a real event takes, without a kernel. The NetBSD drvctl branch is unchanged (t0272); the force unmount there is written for it but was not run. Still open in t0093: the Eject item in dtfile (Selected menu, Command+E) and the qemu device_del test in a VM. --- AGENTS.md | 11 +++- include/Mcde/McdeSys.h | 12 ++++ lib/McdeSys/Makefile.am | 7 ++- lib/McdeSys/disk_linux.c | 9 +++ lib/McdeSys/disk_netbsd.c | 9 +++ lib/McdeSys/events.c | 55 +++++++++++++++++ lib/McdeSys/mount.c | 34 ++++++++++ lib/McdeSys/private.h | 3 + lib/McdeSys/test_disk_events.c | 82 +++++++++++++++++++++++++ programs/dtwm/WmMcdeDesktop.c | 109 ++++++++++++++++++++++++++++++++- 10 files changed, 325 insertions(+), 6 deletions(-) create mode 100644 lib/McdeSys/test_disk_events.c diff --git a/AGENTS.md b/AGENTS.md index 752a92dc..142682ea 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -491,9 +491,14 @@ narratives and test notes, is `docs/reference/mcde-agents-history-2026-11-13.txt otherwise. A volume plugged in later shows through the kernel's device events (`mcde_sys_disk_events`, 500 ms gathered); NetBSD has no drvctl branch yet (t0272), there a slow timer and a workspace change rebuild the - list. `MCDE_DESK_DEBUG=1` prints what it found (the shot harness passes - the variable, and `MCDE_DESK_TEST_MOUNTS`/`_DISKS`/`_DESKTOP` fill a test - list without real mounts). McdeSys had no list of network mounts before: + list. A device removed while a partition of it was still mounted gets the + reference's one-button "Disk Not Ejected" warning (Motif error dialog, the + Warning sound; `mcde_sys_disk_detached` copies the stale mountpoint and + unmounts it the forced way, lazy on Linux, `-f` on NetBSD). `MCDE_DESK_DEBUG=1` + prints what it found (the shot harness passes the variable, and + `MCDE_DESK_TEST_MOUNTS`/`_DISKS`/`_DESKTOP` fill a test list without real + mounts; `MCDE_DESK_TEST_DEVICE` hands the desktop one event line, the path + a test or a screenshot takes without a kernel). McdeSys had no list of network mounts before: `mcde_sys_mounts()` (`mount.c`, `/proc/mounts` on Linux, `getmntinfo` on NetBSD) names every mount and marks the network file systems; the desktop takes the local volumes from `mcde_sys_disks` (labels) and the network diff --git a/include/Mcde/McdeSys.h b/include/Mcde/McdeSys.h index 8c7b16ca..428c3055 100644 --- a/include/Mcde/McdeSys.h +++ b/include/Mcde/McdeSys.h @@ -361,6 +361,13 @@ int mcde_sys_disks(McdeSysDisk *d, size_t max); /* mount partition NAME on /media/LABEL (made); where, into MOUNTPOINT */ int mcde_sys_disk_mount(const char *name, char *mountpoint, size_t len); int mcde_sys_disk_unmount(const char *name); +/* The device NAME (sdb1, sd0a, or the whole drive sdb) went away. If a + * partition of it is still in the mount table, its mountpoint is copied into + * MOUNTPOINT and the stale mount is detached the forced way (lazy on Linux, + * -f on NetBSD); returns 1. 0 when nothing of NAME was mounted, -1 on an + * error. What the computer runs from is never unmounted. */ +int mcde_sys_disk_detached(const char *name, char *mountpoint, + size_t len); /* check partition NAME without changing it (fsck -n); its report into LOG */ int mcde_sys_disk_check(const char *name, char *log, size_t len); /* erase the whole drive NAME: one partition, FSTYPE ("ffs", "msdos"; @@ -588,6 +595,11 @@ int mcde_sys_time_set(time_t when); */ int mcde_sys_disk_events(void); int mcde_sys_disk_event_read(int fd, char *buf, size_t len); +/* what an event says: ACTION and DEVICE (Linux "remove" and "sdb1", + * NetBSD "detach" and "sd0"). Returns 1 when the device went away, 0 for + * another event, -1 on bad arguments. */ +int mcde_sys_disk_event_info(const char *event, char *action, size_t alen, + char *device, size_t dlen); /* * Logs diff --git a/lib/McdeSys/Makefile.am b/lib/McdeSys/Makefile.am index ec7828b0..65c05f93 100644 --- a/lib/McdeSys/Makefile.am +++ b/lib/McdeSys/Makefile.am @@ -22,7 +22,7 @@ endif libMcdeSys_la_LDFLAGS = -version-info 1:0:0 # what the system tells about itself, for a look by hand: make check runs it -check_PROGRAMS = test_sysinfo test_netmount test_updatesource test_processes test_job test_netinput test_sysload +check_PROGRAMS = test_sysinfo test_netmount test_updatesource test_processes test_job test_netinput test_sysload test_disk_events test_sysinfo_SOURCES = test_sysinfo.c test_sysinfo_CPPFLAGS = -I$(top_srcdir)/include test_sysinfo_LDADD = libMcdeSys.la @@ -44,4 +44,7 @@ test_netinput_LDADD = libMcdeSys.la test_sysload_SOURCES = test_sysload.c test_sysload_CPPFLAGS = -I$(top_srcdir)/include test_sysload_LDADD = libMcdeSys.la -TESTS = test_sysinfo test_netmount test_updatesource test_processes test_job test_netinput test_sysload +test_disk_events_SOURCES = test_disk_events.c +test_disk_events_CPPFLAGS = -I$(top_srcdir)/include +test_disk_events_LDADD = libMcdeSys.la +TESTS = test_sysinfo test_netmount test_updatesource test_processes test_job test_netinput test_sysload test_disk_events diff --git a/lib/McdeSys/disk_linux.c b/lib/McdeSys/disk_linux.c index b5af2671..24254794 100644 --- a/lib/McdeSys/disk_linux.c +++ b/lib/McdeSys/disk_linux.c @@ -188,6 +188,15 @@ mcde_sys_disk_unmount(const char *name) } } +/* the device is gone; detach the stale mount now and clean up later */ +int +mcde_sys_disk_force_unmount(const char *mountpoint) +{ + char *argv[] = { "/bin/umount", "-l", (char *) mountpoint, NULL }; + + return mcde_sys_run(argv); +} + int mcde_sys_disk_check(const char *name, char *log, size_t len) { diff --git a/lib/McdeSys/disk_netbsd.c b/lib/McdeSys/disk_netbsd.c index b158ad03..fbe016c9 100644 --- a/lib/McdeSys/disk_netbsd.c +++ b/lib/McdeSys/disk_netbsd.c @@ -275,6 +275,15 @@ mcde_sys_disk_unmount(const char *name) return mcde_sys_run(argv); } +/* the device is gone; detach the stale mount even against a busy file */ +int +mcde_sys_disk_force_unmount(const char *mountpoint) +{ + char *argv[] = { "/sbin/umount", "-f", (char *) mountpoint, NULL }; + + return mcde_sys_run(argv); +} + int mcde_sys_disk_check(const char *name, char *log, size_t len) { diff --git a/lib/McdeSys/events.c b/lib/McdeSys/events.c index 92ef8d6e..20a71c16 100644 --- a/lib/McdeSys/events.c +++ b/lib/McdeSys/events.c @@ -71,3 +71,58 @@ mcde_sys_disk_event_read(int fd, char *buf, size_t len) return mcde_sys_fail(MCDE_SYS_EINVAL, mcde_sys_msg(MSG_EVT_NO_ROOM, "No room for the event.")); return mcde_sys_disk_event_read_backend(fd, buf, len); } + +/* one event, as the read gives it: Linux "remove@/devices/... ACTION=remove + * DEVNAME=sdb1 ..." (the NULs became spaces), NetBSD "device-detach sd0". + * ACTION and DEVICE are filled for both. 1 means the device went away, 0 + * another event, -1 bad arguments. */ +int +mcde_sys_disk_event_info(const char *event, char *action, size_t alen, + char *device, size_t dlen) +{ + const char *p; + size_t n; + + if (event == NULL || action == NULL || alen == 0 || device == NULL || + dlen == 0) + return -1; + action[0] = '\0'; + device[0] = '\0'; + if (strncmp(event, "device-detach", 13) == 0 || + strncmp(event, "device-attach", 13) == 0) { + int gone = event[7] == 'd'; + + snprintf(action, alen, "%s", gone ? "detach" : "attach"); + p = event + 13; + while (*p == ' ' || *p == '\t') + p++; + for (n = 0; p[n] != '\0' && p[n] != ' ' && p[n] != '\t' && + n + 1 < dlen; n++) + device[n] = p[n]; + device[n] = '\0'; + return gone; + } + for (p = event; *p != '\0'; ) { + while (*p == ' ' || *p == '\t') + p++; + if (*p == '\0') + break; + if (strncmp(p, "ACTION=", 7) == 0) { + p += 7; + for (n = 0; *p != '\0' && *p != ' ' && *p != '\t' && + n + 1 < alen; n++) + action[n] = *p++; + action[n] = '\0'; + } else if (strncmp(p, "DEVNAME=", 8) == 0) { + p += 8; + for (n = 0; *p != '\0' && *p != ' ' && *p != '\t' && + n + 1 < dlen; n++) + device[n] = *p++; + device[n] = '\0'; + } else { + while (*p != '\0' && *p != ' ' && *p != '\t') + p++; + } + } + return strcmp(action, "remove") == 0 || strcmp(action, "detach") == 0; +} diff --git a/lib/McdeSys/mount.c b/lib/McdeSys/mount.c index 1571eb8f..868094ec 100644 --- a/lib/McdeSys/mount.c +++ b/lib/McdeSys/mount.c @@ -108,3 +108,37 @@ mcde_sys_mounts(McdeSysMount *m, size_t max) } return n; } + +int +mcde_sys_disk_detached(const char *name, char *mountpoint, size_t len) +{ + McdeSysMount m[128]; + size_t l; + int n, i; + + if (name == NULL || name[0] == '\0' || mountpoint == NULL || len == 0) + return mcde_sys_fail(MCDE_SYS_EINVAL, mcde_sys_msg(MSG_DISK_NOT_FOUND, "There is no disk %s."), name != NULL ? name : ""); + mountpoint[0] = '\0'; + if ((n = mcde_sys_mounts(m, 128)) < 0) + return -1; + l = strlen(name); + for (i = 0; i < n; i++) { + const char *src = m[i].source; + + /* the mount table names the device ("/dev/sdb1"); the event + * names the partition ("sdb1") or the whole drive ("sdb"), + * in which case the rest of the name is the partition */ + if (strncmp(src, "/dev/", 5) != 0) + continue; + src += 5; + if (strncmp(src, name, l) != 0) + continue; + snprintf(mountpoint, len, "%s", m[i].mountpoint); + /* never unmount what the computer runs from, even if a + * removal event names it (the system would be gone anyway) */ + if (!m[i].system) + (void) mcde_sys_disk_force_unmount(m[i].mountpoint); + return 1; + } + return 0; +} diff --git a/lib/McdeSys/private.h b/lib/McdeSys/private.h index 58da9f99..51d7b11f 100644 --- a/lib/McdeSys/private.h +++ b/lib/McdeSys/private.h @@ -293,6 +293,9 @@ int mcde_sys_processes_backend(McdeSysProcess **out, size_t *n); int mcde_sys_mounts_backend(McdeSysMount *m, size_t max); /* events.c: one queued device event (1), none (0) or an error (-1) */ int mcde_sys_disk_event_read_backend(int fd, char *buf, size_t len); +/* mount.c: the device NAME is gone; unmount MOUNTPOINT the forced way (a + * lazy unmount on Linux, -f on NetBSD); the backend */ +int mcde_sys_disk_force_unmount(const char *mountpoint); /* the backends: the CPU ticks of every processor, and the memory */ int mcde_sys_cpu_times_backend(McdeSysCpu **out, size_t *n); int mcde_sys_memory_backend(McdeSysMemory *m); diff --git a/lib/McdeSys/test_disk_events.c b/lib/McdeSys/test_disk_events.c new file mode 100644 index 00000000..5ef745e5 --- /dev/null +++ b/lib/McdeSys/test_disk_events.c @@ -0,0 +1,82 @@ +/* + * test_disk_events.c -- McdeSys: the device event line (make check) + * + * License: CNCL 1.0 (Could Not Care Less), see LICENSE at the top of the tree. + * + * mcde_sys_disk_event_info() is what the desktop asks about an event that + * mcde_sys_disk_event_read() returned: Linux's uevent text with its NULs + * turned into spaces ("remove@/... ACTION=remove ... DEVNAME=sdb1 ...") and + * NetBSD's "device-detach sd0". The parser is what decides whether the + * removal warning (t0093) is looked at, so the cases here are the real + * shapes of both systems and the ones a wrong answer would hurt: an add is + * no removal, a change is none either, a missing DEVNAME leaves it empty, + * and bad arguments are refused. + */ + +#define _POSIX_C_SOURCE 200809L + +#include +#include + +#include + +static int failures; + +#define CHECK(cond) do { \ + if (!(cond)) { \ + printf("FAIL %s:%d: %s\n", __FILE__, __LINE__, #cond); \ + failures++; \ + } \ +} while (0) + +static void +one(const char *event, int want, const char *wantAction, + const char *wantDevice) +{ + char action[32] = "?", device[64] = "?"; + int r = mcde_sys_disk_event_info(event, action, sizeof action, device, + sizeof device); + + CHECK(r == want); + CHECK(strcmp(action, wantAction) == 0); + CHECK(strcmp(device, wantDevice) == 0); + if (r != want || strcmp(action, wantAction) != 0 || + strcmp(device, wantDevice) != 0) + printf(" event [%s]: got %d [%s] [%s]\n", event, r, action, + device); +} + +int +main(void) +{ + one("remove@/devices/pci0000:00/0000:00:14.0/usb1/1-2/1-2:1.0/" + "host8/target8:0:0/8:0:0:0/block/sdb/sdb1 ACTION=remove " + "DEVPATH=/devices/pci0000:00/0000:00:14.0/usb1/1-2/1-2:1.0/" + "host8/target8:0:0/8:0:0:0/block/sdb/sdb1 SUBSYSTEM=block " + "DEVNAME=sdb1 DEVTYPE=partition SEQNUM=4711", + 1, "remove", "sdb1"); + one("add@/devices/pci0000:00/0000:00:14.0/usb1/1-2/1-2:1.0/host8/" + "target8:0:0/8:0:0:0/block/sdb/sdb1 ACTION=add " + "DEVNAME=sdb1 DEVTYPE=partition", 0, "add", "sdb1"); + one("change@/devices/virtual/block/dm-0 ACTION=change " + "DEVPATH=/devices/virtual/block/dm-0 SUBSYSTEM=block", + 0, "change", ""); + one("device-detach sd0", 1, "detach", "sd0"); + one("device-attach sd0", 0, "attach", "sd0"); + one("device-detach", 1, "detach", ""); + one("", 0, "", ""); + CHECK(mcde_sys_disk_event_info(NULL, NULL, 0, NULL, 0) == -1); + CHECK(mcde_sys_disk_event_info("ACTION=remove", NULL, 0, NULL, 0) == + -1); + { + char action[8], device[8]; + + CHECK(mcde_sys_disk_event_info("ACTION=remove DEVNAME=sdb1", + action, 0, device, sizeof device) == -1); + CHECK(mcde_sys_disk_event_info("ACTION=remove DEVNAME=sdb1", + action, sizeof action, device, 0) == -1); + } + if (failures == 0) + printf("test_disk_events: all passed\n"); + return failures != 0; +} diff --git a/programs/dtwm/WmMcdeDesktop.c b/programs/dtwm/WmMcdeDesktop.c index 08c54501..5ca3519b 100644 --- a/programs/dtwm/WmMcdeDesktop.c +++ b/programs/dtwm/WmMcdeDesktop.c @@ -41,7 +41,11 @@ #include #include +#include +#include + #include +#include #include "WmGlobal.h" #include "WmFunction.h" @@ -1768,6 +1772,103 @@ DeskTimerCB(XtPointer client, XtIntervalId *id) } } +/* the one warning dialog of the desktop (t0093): a device that went away + * while it was still mounted. Motif's error dialog, one OK button, the + * Warning sound of the theme. */ +static Widget diskRemovedDialog; + +static void +DiskRemovedClosed(Widget w, XtPointer client, XtPointer call) +{ + (void) client; + (void) call; + if (diskRemovedDialog == w) + diskRemovedDialog = NULL; + XtDestroyWidget(w); +} + +static void +DiskRemovedDialog(const char *device, const char *mountpoint) +{ + char text[700]; + XmString m, t; + Arg a[4]; + int n = 0; + Widget d; + + if (diskRemovedDialog != NULL && XtIsManaged(diskRemovedDialog)) + return; + snprintf(text, sizeof text, + "The device \"%s\" was removed while it was still\n" + "mounted at \"%s\".\n\n" + "Data might have been lost or damaged. Before you\n" + "unplug a device, unmount it first (Disk Utility,\n" + "Unmount).", + device, mountpoint); + m = XmStringCreateLocalized(text); + t = XmStringCreateLocalized("Disk Not Ejected"); + XtSetArg(a[n], XmNdialogTitle, t); n++; + XtSetArg(a[n], XmNdefaultPosition, False); n++; + XtSetArg(a[n], XmNautoUnmanage, False); n++; + XtSetArg(a[n], XmNuseAsyncGeometry, True); n++; + d = XmCreateErrorDialog(wmGD.topLevelW, "diskRemoved", a, n); + /* the message label itself (as _DtMessageDialog does): the newlines + * of the text are the lines of the box */ + XtSetArg(a[0], XmNlabelString, m); + XtSetValues(XmMessageBoxGetChild(d, XmDIALOG_MESSAGE_LABEL), a, 1); + XtUnmanageChild(XmMessageBoxGetChild(d, XmDIALOG_CANCEL_BUTTON)); + XtUnmanageChild(XmMessageBoxGetChild(d, XmDIALOG_HELP_BUTTON)); + XtAddCallback(d, XmNokCallback, DiskRemovedClosed, NULL); + XtManageChild(d); + { + /* dtwm is the window manager: its own dialog gets no frame, + * so it is placed here (the middle of the screen) */ + Widget shell = XtParent(d); + Dimension w = 1, h = 1; + + XtVaGetValues(d, XmNwidth, &w, XmNheight, &h, NULL); + if (shell != NULL && XtWindow(shell) != None) + XMoveWindow(deskDpy, XtWindow(shell), + (deskScreenW - (int) w) / 2, + (deskScreenH - (int) h) / 2); + } + XmStringFree(m); + XmStringFree(t); + diskRemovedDialog = d; + (void) McdeSoundEvent(deskDpy, "Warning"); +} + +/* what an event line means for the desktop: a device that went away while a + * partition of it was still mounted gets the warning; the icon refresh is + * the caller's (DeskDeviceCB) */ +static void +DiskEventSeen(const char *line) +{ + char action[32], device[64], mountpoint[256]; + + if (mcde_sys_disk_event_info(line, action, sizeof action, device, + sizeof device) != 1 || device[0] == '\0') + return; + if (getenv("MCDE_DESK_DEBUG") != NULL) + fprintf(stderr, "desktop: %s (%s)\n", device, action); + if (mcde_sys_disk_detached(device, mountpoint, sizeof mountpoint) == 1) { + if (getenv("MCDE_DESK_DEBUG") != NULL) + fprintf(stderr, "desktop: %s was mounted at %s\n", + device, mountpoint); + DiskRemovedDialog(device, mountpoint); + } +} + +static void +DeskTestDeviceCB(XtPointer client, XtIntervalId *id) +{ + char *line = client; + + (void) id; + DiskEventSeen(line); + XtFree(line); +} + static void DeskDeviceCB(XtPointer client, int *fd, XtInputId *id) { @@ -1777,7 +1878,7 @@ DeskDeviceCB(XtPointer client, int *fd, XtInputId *id) (void) id; /* one line per event (Linux: the uevent, NetBSD: DRVGETEVENT) */ while (mcde_sys_disk_event_read(*fd, buf, sizeof buf) > 0) - ; + DiskEventSeen(buf); deskDirty = 1; if (deskTimer == 0) deskTimer = XtAppAddTimeOut(wmGD.mwmAppContext, 500, @@ -1872,4 +1973,10 @@ McdeDesktopInit(void) else if (getenv("MCDE_DESK_DEBUG") != NULL) fprintf(stderr, "desktop: no device events: %s\n", mcde_sys_error()); + /* tests and screenshots hand the desktop one event line without a + * kernel (the same path a real event takes, after the read) */ + if (getenv("MCDE_DESK_TEST_DEVICE") != NULL) + (void) XtAppAddTimeOut(wmGD.mwmAppContext, 1000, + DeskTestDeviceCB, + (XtPointer) XtNewString(getenv("MCDE_DESK_TEST_DEVICE"))); }