Files
lotherk f4ec4a1da1 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.
2026-11-16 13:56:06 +00:00

129 lines
3.9 KiB
C

/*
* events.c -- MCDE: device events as a descriptor (McdeSys.h)
*
* License: CNCL 1.0 (Could Not Care Less), see LICENSE at the top of the tree.
*
* Linux: the kernel's uevent socket (AF_NETLINK, NETLINK_KOBJECT_UEVENT,
* group 1). It carries "add"/"remove"/"change" for every device, so the
* programs that care about a medium -- the sidebar's devices (t0108), the
* eject warning (t0093), the autoplay (t0083) -- read it instead of asking
* in a loop.
*
* NetBSD: /dev/drvctl opened O_NONBLOCK (drvctl(4)); the autoconf framework
* queues "device-attach" and "device-detach" events. There is no read(2)
* on that descriptor -- the events come as the DRVGETEVENT ioctl with a
* property list (devpubd(8) does exactly this) -- so mcde_sys_disk_event_read()
* is the one call a program uses on both systems.
*
* C99 and POSIX only (ext/src/mcde/AGENTS.md).
*/
#define _POSIX_C_SOURCE 200809L
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#if defined(__linux__)
#include <sys/socket.h>
#include <linux/netlink.h>
#endif
#include "private.h"
int
mcde_sys_disk_events(void)
{
#if defined(__linux__)
int fd;
struct sockaddr_nl sa;
fd = socket(AF_NETLINK, SOCK_DGRAM | SOCK_NONBLOCK, NETLINK_KOBJECT_UEVENT);
if (fd < 0)
return mcde_sys_fail(MCDE_SYS_EFAIL, mcde_sys_msg(MSG_EVT_SOCKET_OPEN, "The kernel's event socket cannot be opened."));
memset(&sa, 0, sizeof sa);
sa.nl_family = AF_NETLINK;
sa.nl_groups = 1; /* the kernel's uevents */
if (bind(fd, (struct sockaddr *) &sa, sizeof sa) < 0) {
(void)close(fd);
return mcde_sys_fail(MCDE_SYS_EFAIL, mcde_sys_msg(MSG_EVT_SOCKET_BIND, "The kernel's event socket cannot be bound."));
}
return fd;
#elif defined(__NetBSD__)
int fd;
fd = open("/dev/drvctl", O_RDONLY | O_NONBLOCK);
if (fd < 0)
return mcde_sys_fail(MCDE_SYS_EFAIL, mcde_sys_msg(MSG_EVT_DRVCTL, "Cannot open /dev/drvctl (is the drvctl pseudo-device configured?)."));
return fd;
#else
return mcde_sys_fail(MCDE_SYS_ENOTSUP, mcde_sys_msg(MSG_EVT_NO_SOURCE, "Device events have no source on this system."));
#endif
}
int
mcde_sys_disk_event_read(int fd, char *buf, size_t len)
{
if (fd < 0 || buf == NULL || len == 0)
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;
}