Files
mcde/lib/McdeSys/mount.c
T
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

145 lines
4.0 KiB
C

/*
* mount.c -- MCDE: every mount of the computer (McdeSys)
*
* License: CNCL 1.0 (Could Not Care Less), see LICENSE at the top of the tree.
*
* The backend reads the system's mount table (mount_linux.c: /proc/mounts,
* mount_netbsd.c: getmntinfo(3)) and fills source, mountpoint, fstype and
* label. Here the list is classified: a network file system (NFS, SMB/CIFS,
* AFP, sshfs and the other FUSE networks), a removable medium, and what the
* computer runs from. The desktop (WmMcdeDesktop.c) puts the icons of this
* list on the screen; the four kinds are its icons: a network mount, a
* volume, a stick or CD, and the computer itself.
*
* C99 and POSIX only (ext/src/mcde/AGENTS.md).
*/
#define _POSIX_C_SOURCE 200809L
#include <stdio.h>
#include <string.h>
#include <Mcde/McdeSys.h>
#include "private.h"
/* the file system types that mean "somebody else's disk" */
static const char *const networkTypes[] = {
"nfs", "nfs4", "cifs", "smbfs", "smb", "afpfs", "afp",
"fuse.sshfs", "sshfs", "fuse.rclone", "rclone",
"9p", "ceph", "glusterfs", "davfs", "davfs2", "afs", "ncpfs",
"fuse.smbnetfs", "smbnetfs", "fuse.davfs2",
NULL
};
/* the file system types of real volumes; anything else that is mounted
* (proc, sysfs, tmpfs, cgroup, devpts, ...) is a pseudo file system of the
* kernel and gets no icon on the desktop */
static const char *const volumeTypes[] = {
"ext2", "ext3", "ext4", "xfs", "btrfs", "f2fs", "jfs", "reiserfs",
"vfat", "msdos", "exfat", "ntfs", "ntfs3", "hfs", "hfsplus", "apfs",
"iso9660", "udf", "ffs", "ufs", "zfs", "hammer", "hammer2",
"cd9660", "msdosfs", "nilfs2", "bcachefs", "erofs", "squashfs",
"fuse", "fuseblk",
NULL
};
static int
IsNetwork(const char *fstype)
{
int i;
for (i = 0; networkTypes[i] != NULL; i++)
if (strcmp(fstype, networkTypes[i]) == 0)
return 1;
return 0;
}
static int
IsVolume(const char *fstype)
{
int i;
for (i = 0; volumeTypes[i] != NULL; i++)
if (strcmp(fstype, volumeTypes[i]) == 0)
return 1;
return 0;
}
/* the directories whose file system the computer cannot lose */
static int
IsSystem(const char *mountpoint)
{
static const char *const roots[] = {
"/", "/usr", "/var", "/home", "/boot", "/boot/efi", NULL
};
int i;
if (strcmp(mountpoint, "swap") == 0)
return 1;
for (i = 0; roots[i] != NULL; i++)
if (strcmp(mountpoint, roots[i]) == 0)
return 1;
return 0;
}
int
mcde_sys_mounts(McdeSysMount *m, size_t max)
{
int n, i;
if (m == NULL || max == 0)
return mcde_sys_fail(MCDE_SYS_EINVAL, mcde_sys_msg(MSG_MNT_NO_ROOM, "No room for the mounts."));
n = mcde_sys_mounts_backend(m, max);
if (n < 0)
return -1;
for (i = 0; i < n; i++) {
m[i].network = IsNetwork(m[i].fstype);
m[i].volume = IsVolume(m[i].fstype);
m[i].system = IsSystem(m[i].mountpoint);
/* a network mount is never the computer's own disk and
* never removable, whatever the backend guessed; it is a
* volume for the desktop all the same */
if (m[i].network) {
m[i].system = 0;
m[i].removable = 0;
m[i].volume = 1;
}
}
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;
}