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.
281 lines
8.6 KiB
C
281 lines
8.6 KiB
C
/*
|
|
* disk_linux.c -- MCDE: the disks of a Linux system (McdeSys)
|
|
*
|
|
* License: CNCL 1.0 (Could Not Care Less), see LICENSE at the top of the tree.
|
|
*
|
|
* lsblk(8) -P -b (KEY="value" pairs, sizes in bytes) for the drives and
|
|
* partitions; udisksctl(1) mounts and unmounts where it is there (as the
|
|
* user, with the system's permission), else mount(8); fsck(8) -n looks;
|
|
* erasing is sfdisk(8) (one GPT partition) and mkfs.ext4 or mkfs.vfat.
|
|
* Loop devices, LVM and crypto layers are not drives of their own here.
|
|
*/
|
|
|
|
#define _POSIX_C_SOURCE 200809L
|
|
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <sys/statvfs.h>
|
|
#include <unistd.h>
|
|
|
|
#include <Mcde/McdeSys.h>
|
|
|
|
#include "private.h"
|
|
|
|
static const char *const lsblkPaths[] = { "/usr/bin/lsblk", "/bin/lsblk",
|
|
NULL };
|
|
static const char *const udisksPaths[] = { "/usr/bin/udisksctl", NULL };
|
|
|
|
/* the value of KEY="..." in LINE */
|
|
static void
|
|
Pair(const char *line, const char *key, char *buf, size_t len)
|
|
{
|
|
char pat[32];
|
|
const char *p, *e;
|
|
|
|
buf[0] = '\0';
|
|
snprintf(pat, sizeof pat, " %s=\"", key);
|
|
if (strncmp(line, pat + 1, strlen(pat + 1)) == 0)
|
|
p = line + strlen(pat + 1);
|
|
else if ((p = strstr(line, pat)) != NULL)
|
|
p += strlen(pat);
|
|
else
|
|
return;
|
|
if ((e = strchr(p, '"')) == NULL)
|
|
return;
|
|
snprintf(buf, len, "%.*s", (int) (e - p), p);
|
|
}
|
|
|
|
int
|
|
mcde_sys_disks(McdeSysDisk *d, size_t max)
|
|
{
|
|
static char out[65536];
|
|
const char *lb = mcde_sys_which(lsblkPaths);
|
|
char *argv[] = { NULL, "-P", "-b", "-o",
|
|
"NAME,PKNAME,TYPE,SIZE,MODEL,FSTYPE,LABEL,MOUNTPOINT,RM", NULL };
|
|
char *line, *last = NULL, type[16], size[32], rm[8];
|
|
size_t n = 0;
|
|
|
|
if (lb == NULL)
|
|
return mcde_sys_fail(MCDE_SYS_ENOTSUP, mcde_sys_msg(MSG_DISK_NO_LSBLK, "lsblk is not there."));
|
|
argv[0] = (char *) lb;
|
|
if (mcde_sys_capture(argv, out, sizeof out) != MCDE_SYS_OK)
|
|
return -1;
|
|
for (line = strtok_r(out, "\n", &last); line != NULL && n < max;
|
|
line = strtok_r(NULL, "\n", &last)) {
|
|
McdeSysDisk *x = &d[n];
|
|
|
|
Pair(line, "TYPE", type, sizeof type);
|
|
/* lvm: an LV (the root of this host is one); crypt: a
|
|
* dm-crypt volume, also a whole volume of its own */
|
|
if (strcmp(type, "disk") != 0 && strcmp(type, "part") != 0 &&
|
|
strcmp(type, "rom") != 0 && strcmp(type, "lvm") != 0 &&
|
|
strcmp(type, "crypt") != 0)
|
|
continue;
|
|
memset(x, 0, sizeof *x);
|
|
Pair(line, "NAME", x->name, sizeof x->name);
|
|
if (strcmp(type, "part") == 0)
|
|
Pair(line, "PKNAME", x->parent, sizeof x->parent);
|
|
Pair(line, "MODEL", x->model, sizeof x->model);
|
|
Pair(line, "FSTYPE", x->fstype, sizeof x->fstype);
|
|
Pair(line, "LABEL", x->label, sizeof x->label);
|
|
Pair(line, "MOUNTPOINT", x->mountpoint, sizeof x->mountpoint);
|
|
Pair(line, "SIZE", size, sizeof size);
|
|
Pair(line, "RM", rm, sizeof rm);
|
|
x->bytes = strtoull(size, NULL, 10);
|
|
x->removable = strcmp(rm, "1") == 0 || strcmp(type, "rom") == 0;
|
|
if (strcmp(x->mountpoint, "[SWAP]") == 0) {
|
|
x->mountpoint[0] = '\0';
|
|
x->system = 1;
|
|
}
|
|
if (strcmp(x->mountpoint, "/") == 0 ||
|
|
strcmp(x->mountpoint, "/boot") == 0 ||
|
|
strcmp(x->mountpoint, "/boot/efi") == 0 ||
|
|
strcmp(x->mountpoint, "/usr") == 0 ||
|
|
strcmp(x->mountpoint, "/home") == 0 ||
|
|
strcmp(x->fstype, "crypto_LUKS") == 0 ||
|
|
strcmp(x->fstype, "LVM2_member") == 0)
|
|
x->system = 1;
|
|
if (x->mountpoint[0] != '\0') {
|
|
struct statvfs sv;
|
|
|
|
if (statvfs(x->mountpoint, &sv) == 0) {
|
|
x->used = (unsigned long long) (sv.f_blocks -
|
|
sv.f_bfree) * sv.f_frsize;
|
|
x->avail = (unsigned long long) sv.f_bavail *
|
|
sv.f_frsize;
|
|
}
|
|
}
|
|
/* a system partition makes its drive the system's */
|
|
if (x->system && x->parent[0] != '\0') {
|
|
size_t k;
|
|
|
|
for (k = 0; k < n; k++)
|
|
if (strcmp(d[k].name, x->parent) == 0)
|
|
d[k].system = 1;
|
|
}
|
|
n++;
|
|
}
|
|
return (int) n;
|
|
}
|
|
|
|
static int
|
|
Find(const char *name, McdeSysDisk *out)
|
|
{
|
|
static McdeSysDisk all[64];
|
|
int n = mcde_sys_disks(all, 64), i;
|
|
|
|
for (i = 0; i < n; i++)
|
|
if (strcmp(all[i].name, name) == 0) {
|
|
*out = all[i];
|
|
return 1;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
int
|
|
mcde_sys_disk_mount(const char *name, char *mountpoint, size_t len)
|
|
{
|
|
McdeSysDisk d;
|
|
char dev[64], out[512];
|
|
const char *ud = mcde_sys_which(udisksPaths);
|
|
|
|
if (!Find(name, &d))
|
|
return mcde_sys_fail(MCDE_SYS_ENOENT, mcde_sys_msg(MSG_DISK_NOT_FOUND, "There is no disk %s."),
|
|
name);
|
|
if (d.mountpoint[0] != '\0') {
|
|
snprintf(mountpoint, len, "%s", d.mountpoint);
|
|
return MCDE_SYS_OK;
|
|
}
|
|
if (ud == NULL)
|
|
return mcde_sys_fail(MCDE_SYS_ENOTSUP, mcde_sys_msg(MSG_DISK_NO_UDISKS, "udisksctl is not there."));
|
|
snprintf(dev, sizeof dev, "/dev/%s", name);
|
|
{
|
|
char *argv[] = { (char *) ud, "mount", "-b", dev, NULL };
|
|
|
|
if (mcde_sys_capture(argv, out, sizeof out) != MCDE_SYS_OK)
|
|
return MCDE_SYS_EFAIL;
|
|
}
|
|
/* "Mounted /dev/sdb1 at /media/user/STICK" */
|
|
if (!Find(name, &d) || d.mountpoint[0] == '\0')
|
|
return mcde_sys_fail(MCDE_SYS_EFAIL, "%s", out);
|
|
snprintf(mountpoint, len, "%s", d.mountpoint);
|
|
return MCDE_SYS_OK;
|
|
}
|
|
|
|
int
|
|
mcde_sys_disk_unmount(const char *name)
|
|
{
|
|
McdeSysDisk d;
|
|
char dev[64];
|
|
const char *ud = mcde_sys_which(udisksPaths);
|
|
|
|
if (!Find(name, &d) || d.mountpoint[0] == '\0')
|
|
return mcde_sys_fail(MCDE_SYS_ENOENT, mcde_sys_msg(MSG_DISK_NOT_MOUNTED, "%s is not mounted."),
|
|
name);
|
|
if (d.system)
|
|
return mcde_sys_fail(MCDE_SYS_EPERM, mcde_sys_msg(MSG_DISK_SYSTEM, "%s is the system's."),
|
|
name);
|
|
snprintf(dev, sizeof dev, "/dev/%s", name);
|
|
if (ud != NULL) {
|
|
char *argv[] = { (char *) ud, "unmount", "-b", dev, NULL };
|
|
|
|
return mcde_sys_run(argv);
|
|
} else {
|
|
char *argv[] = { "/bin/umount", d.mountpoint, NULL };
|
|
|
|
return mcde_sys_run(argv);
|
|
}
|
|
}
|
|
|
|
/* 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)
|
|
{
|
|
McdeSysDisk d;
|
|
char dev[64];
|
|
char *argv[] = { "/sbin/fsck", "-n", NULL, NULL };
|
|
|
|
log[0] = '\0';
|
|
if (!Find(name, &d) || d.fstype[0] == '\0' ||
|
|
strcmp(d.fstype, "swap") == 0)
|
|
return mcde_sys_fail(MCDE_SYS_ENOTSUP, mcde_sys_msg(MSG_DISK_CHECK_FAIL, "%s cannot be checked."),
|
|
name);
|
|
snprintf(dev, sizeof dev, "/dev/%s", name);
|
|
argv[2] = dev;
|
|
return mcde_sys_capture(argv, log, len);
|
|
}
|
|
|
|
int
|
|
mcde_sys_disk_erase(const char *name, const char *fstype, const char *label,
|
|
char *part, size_t len)
|
|
{
|
|
static McdeSysDisk all[64];
|
|
char dev[64], pdev[80];
|
|
int n, i;
|
|
|
|
if (geteuid() != 0)
|
|
return mcde_sys_fail(MCDE_SYS_EPERM, mcde_sys_msg(MSG_DISK_ERASE_EPERM, "Only the administrator can erase a disk."));
|
|
if (label == NULL || label[0] == '\0' || strlen(label) > 11 ||
|
|
strspn(label, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
|
"0123456789_-") != strlen(label))
|
|
return mcde_sys_fail(MCDE_SYS_EINVAL, mcde_sys_msg(MSG_DISK_BAD_NAME, "A name has letters, digits, - and _, at most 11."));
|
|
n = mcde_sys_disks(all, 64);
|
|
for (i = 0; i < n; i++)
|
|
if (strcmp(all[i].name, name) == 0)
|
|
break;
|
|
if (i == n || all[i].parent[0] != '\0')
|
|
return mcde_sys_fail(MCDE_SYS_EINVAL, mcde_sys_msg(MSG_DISK_NOT_DRIVE, "%s is no drive."), name);
|
|
if (all[i].system)
|
|
return mcde_sys_fail(MCDE_SYS_EPERM, mcde_sys_msg(MSG_DISK_HOLDS_SYSTEM, "%s holds the system and is not erased."), name);
|
|
for (i = 0; i < n; i++)
|
|
if ((strcmp(all[i].name, name) == 0 ||
|
|
strcmp(all[i].parent, name) == 0) &&
|
|
all[i].mountpoint[0] != '\0')
|
|
return mcde_sys_fail(MCDE_SYS_EPERM, mcde_sys_msg(MSG_DISK_MOUNTED, "%s is mounted on %s: unmount it first."), all[i].name,
|
|
all[i].mountpoint);
|
|
snprintf(dev, sizeof dev, "/dev/%s", name);
|
|
{
|
|
char *argv[] = { "/sbin/sfdisk", "--quiet", dev, NULL };
|
|
|
|
/* one GPT partition over the whole drive (sfdisk(8)) */
|
|
if (mcde_sys_run_input(argv, "label: gpt\n,,L\n") != MCDE_SYS_OK)
|
|
return MCDE_SYS_EFAIL;
|
|
}
|
|
/* sda -> sda1, nvme0n1 -> nvme0n1p1 */
|
|
snprintf(part, len, "%s%s1", name,
|
|
name[strlen(name) - 1] >= '0' && name[strlen(name) - 1] <= '9' ?
|
|
"p" : "");
|
|
snprintf(pdev, sizeof pdev, "/dev/%s", part);
|
|
if (strcmp(fstype, "msdos") == 0) {
|
|
char *argv[] = { "/sbin/mkfs.vfat", "-F", "32", "-n",
|
|
(char *) label, pdev, NULL };
|
|
|
|
return mcde_sys_run(argv);
|
|
} else {
|
|
char *argv[] = { "/sbin/mkfs.ext4", "-q", "-L", (char *) label,
|
|
pdev, NULL };
|
|
|
|
return mcde_sys_run(argv);
|
|
}
|
|
}
|
|
|
|
/* ---- the permissions: Linux distributions keep no list of how their
|
|
* files' permissions should be (dpkg --verify checks contents) */
|
|
int
|
|
mcde_sys_permissions(int repair, char *log, size_t len)
|
|
{
|
|
(void) repair;
|
|
if (len > 0)
|
|
log[0] = '\0';
|
|
return mcde_sys_fail(MCDE_SYS_ENOTSUP, mcde_sys_msg(MSG_DISK_NO_PERM_LIST, "This system keeps no list of how its files' permissions should be."));
|
|
}
|