Files
mcde/lib/McdeSys/disk_netbsd.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

486 lines
15 KiB
C

/*
* disk_netbsd.c -- MCDE: the disks of a NetBSD system (McdeSys)
*
* License: CNCL 1.0 (Could Not Care Less), see LICENSE at the top of the tree.
*
* NetBSD's own tools and calls (docs/reference/disks.txt in the Makronix
* tree): the drives from sysctl hw.disknames, their size from dkctl(8)
* getgeometry, their partitions -- wedges, made from GPT or MBR by the
* kernel -- from dkctl listwedges, a drive's model from atactl(8)
* identify; what is mounted from getmntinfo(3), the swap from swapctl(8)
* -l. A drive that holds /, /usr, /var, /home or swap is the system's and
* is never erased. Erasing is gpt(8) destroy, create and add, dkctl
* makewedges, then newfs(8) -O 2 or newfs_msdos(8) -F 32.
*/
#define _NETBSD_SOURCE
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/param.h>
#include <sys/mount.h>
#include <sys/stat.h>
#include <sys/statvfs.h>
#include <sys/sysctl.h>
#include <Mcde/McdeSys.h>
#include "private.h"
static char dkctl[] = "/sbin/dkctl";
static char gptp[] = "/sbin/gpt";
static unsigned long long
Number(const char *s)
{
return strtoull(s, NULL, 10);
}
/* "/dev/rwd0: 68719476736 bytes in 134217728 blocks of 512 bytes" */
static void
Geometry(const char *name, unsigned long long *bytes, unsigned long *bsize)
{
char out[512], *p;
char *argv[] = { dkctl, (char *) name, "getgeometry", NULL };
*bytes = 0;
*bsize = 512;
if (mcde_sys_capture(argv, out, sizeof out) != MCDE_SYS_OK ||
(p = strchr(out, ':')) == NULL)
return;
*bytes = Number(p + 1);
if ((p = strstr(out, "blocks of ")) != NULL)
*bsize = (unsigned long) Number(p + 10);
}
static void
Model(const char *name, char *buf, size_t len)
{
char out[2048], *p, *e;
char *argv[] = { "/sbin/atactl", (char *) name, "identify", NULL };
buf[0] = '\0';
if (strncmp(name, "cd", 2) == 0) {
snprintf(buf, len, "CD/DVD drive");
return;
}
if (strncmp(name, "wd", 2) != 0 ||
mcde_sys_capture(argv, out, sizeof out) != MCDE_SYS_OK ||
(p = strstr(out, "Model: ")) == NULL)
return;
p += 7;
if ((e = strstr(p, ", Rev")) == NULL)
e = strchr(p, '\n');
snprintf(buf, len, "%.*s", (int) (e != NULL ? e - p : (long) strlen(p)),
p);
}
/* where every device is mounted, and the swap */
static void
MountOf(const char *name, McdeSysDisk *d)
{
struct statvfs *mnt;
char dev[64];
int n, i;
snprintf(dev, sizeof dev, "/dev/%s", name);
n = getmntinfo(&mnt, MNT_NOWAIT);
for (i = 0; i < n; i++)
if (strcmp(mnt[i].f_mntfromname, dev) == 0) {
snprintf(d->mountpoint, sizeof d->mountpoint, "%s",
mnt[i].f_mntonname);
d->used = (unsigned long long) (mnt[i].f_blocks -
mnt[i].f_bfree) * mnt[i].f_frsize;
d->avail = (unsigned long long) mnt[i].f_bavail *
mnt[i].f_frsize;
if (strcmp(d->mountpoint, "/") == 0 ||
strcmp(d->mountpoint, "/usr") == 0 ||
strcmp(d->mountpoint, "/var") == 0 ||
strcmp(d->mountpoint, "/home") == 0)
d->system = 1;
return;
}
}
static int
IsSwap(const char *name)
{
char out[2048], dev[64];
char *argv[] = { "/sbin/swapctl", "-l", NULL };
snprintf(dev, sizeof dev, "/dev/%s ", name);
return mcde_sys_capture(argv, out, sizeof out) == MCDE_SYS_OK &&
strstr(out, dev) != NULL;
}
int
mcde_sys_disks(McdeSysDisk *d, size_t max)
{
char names[1024], *tok, *last = NULL;
size_t len = sizeof names, n = 0;
if (sysctlbyname("hw.disknames", names, &len, NULL, 0) != 0)
return mcde_sys_fail(MCDE_SYS_EFAIL, mcde_sys_msg(MSG_NDISK_NO_LIST, "No list of disks (hw.disknames)."));
for (tok = strtok_r(names, " ", &last); tok != NULL && n < max;
tok = strtok_r(NULL, " ", &last)) {
McdeSysDisk *drive = &d[n];
unsigned long bsize;
char out[4096], *line, *l2 = NULL;
char *argv[] = { dkctl, tok, "listwedges", NULL };
size_t first;
/* the wedges come with their drive; floppies are left out */
if (strncmp(tok, "dk", 2) == 0 || strncmp(tok, "fd", 2) == 0)
continue;
memset(drive, 0, sizeof *drive);
snprintf(drive->name, sizeof drive->name, "%s", tok);
Geometry(tok, &drive->bytes, &bsize);
Model(tok, drive->model, sizeof drive->model);
drive->removable = strncmp(tok, "sd", 2) == 0 ||
strncmp(tok, "cd", 2) == 0;
if (strncmp(tok, "cd", 2) == 0 && drive->bytes > 0)
snprintf(drive->fstype, sizeof drive->fstype, "cd9660");
first = ++n;
if (mcde_sys_capture(argv, out, sizeof out) != MCDE_SYS_OK)
continue;
/* "dk0: LABEL, 113246080 blocks at 64, type: ffs" */
for (line = strtok_r(out, "\n", &l2); line != NULL && n < max;
line = strtok_r(NULL, "\n", &l2)) {
McdeSysDisk *p = &d[n];
char *colon = strchr(line, ':'), *comma, *type;
if (strncmp(line, "dk", 2) != 0 || colon == NULL ||
(comma = strrchr(line, ',')) == NULL)
continue;
memset(p, 0, sizeof *p);
snprintf(p->name, sizeof p->name, "%.*s",
(int) (colon - line), line);
snprintf(p->parent, sizeof p->parent, "%s", tok);
{
/* the label: up to the comma before the count */
char *c2 = strstr(colon, " blocks at ");
char *lab_end = c2;
while (lab_end != NULL && lab_end > colon &&
*lab_end != ',')
lab_end--;
if (lab_end != NULL && lab_end > colon + 2)
snprintf(p->label, sizeof p->label,
"%.*s", (int) (lab_end - colon - 2),
colon + 2);
if (c2 != NULL) {
char *b = c2;
while (b > colon && isdigit((unsigned
char) b[-1]))
b--;
p->bytes = Number(b) * bsize;
}
}
if ((type = strstr(comma, "type: ")) != NULL)
snprintf(p->fstype, sizeof p->fstype, "%s",
type + 6);
/* a GPT label of only a UUID says nothing */
if (strlen(p->label) == 36 && p->label[8] == '-')
p->label[0] = '\0';
MountOf(p->name, p);
if (strcmp(p->fstype, "swap") == 0 && IsSwap(p->name))
p->system = 1;
if (p->system)
drive->system = 1;
n++;
}
(void) first;
MountOf(tok, drive); /* a CD, or a disk without wedges */
}
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], mp[256], prog[64];
const char *base;
char *argv[5];
size_t i;
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 (strcmp(d.fstype, "ffs") != 0 && strcmp(d.fstype, "msdos") != 0 &&
strcmp(d.fstype, "ext2fs") != 0 && strcmp(d.fstype, "cd9660") != 0 &&
strcmp(d.fstype, "ntfs") != 0)
return mcde_sys_fail(MCDE_SYS_ENOTSUP, mcde_sys_msg(MSG_NDISK_PART_MOUNT, "A %s partition cannot be mounted."), d.fstype[0] != '\0' ? d.fstype : "blank");
snprintf(prog, sizeof prog, "/sbin/mount_%s", d.fstype);
snprintf(dev, sizeof dev, strcmp(d.fstype, "cd9660") == 0 ?
"/dev/%sa" : "/dev/%s", name);
/* /media/LABEL, or /media/NAME: letters, digits and - only */
base = d.label[0] != '\0' ? d.label : name;
snprintf(mp, sizeof mp, "/media/");
for (i = 0; base[i] != '\0' && strlen(mp) + 2 < sizeof mp; i++) {
size_t l = strlen(mp);
mp[l] = isalnum((unsigned char) base[i]) ? base[i] : '-';
mp[l + 1] = '\0';
}
(void) mkdir("/media", 0755);
(void) mkdir(mp, 0755);
argv[0] = prog;
argv[1] = dev;
argv[2] = mp;
argv[3] = NULL;
if (mcde_sys_run(argv) != MCDE_SYS_OK)
return MCDE_SYS_EFAIL;
snprintf(mountpoint, len, "%s", mp);
return MCDE_SYS_OK;
}
int
mcde_sys_disk_unmount(const char *name)
{
McdeSysDisk d;
char *argv[3];
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);
argv[0] = "/sbin/umount";
argv[1] = d.mountpoint;
argv[2] = NULL;
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)
{
McdeSysDisk d;
char prog[64], dev[64];
char *argv[4];
log[0] = '\0';
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 (strcmp(d.fstype, "ffs") != 0 && strcmp(d.fstype, "msdos") != 0 &&
strcmp(d.fstype, "ext2fs") != 0)
return mcde_sys_fail(MCDE_SYS_ENOTSUP, mcde_sys_msg(MSG_NDISK_PART_CHECK, "A %s partition cannot be checked."), d.fstype[0] != '\0' ? d.fstype : "blank");
snprintf(prog, sizeof prog, "/sbin/fsck_%s", d.fstype);
snprintf(dev, sizeof dev, "/dev/r%s", name);
argv[0] = prog;
argv[1] = "-n"; /* look, change nothing (fsck_ffs(8)) */
argv[2] = dev;
argv[3] = NULL;
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 out[2048], rdev[64], *p;
const char *gtype;
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 (strcmp(fstype, "ffs") == 0)
gtype = "ffs";
else if (strcmp(fstype, "msdos") == 0)
gtype = "windows";
else
return mcde_sys_fail(MCDE_SYS_ENOTSUP, mcde_sys_msg(MSG_NDISK_NO_DEVICE, "No %s here."), fstype);
if (label == NULL || label[0] == '\0' || strlen(label) > 32 ||
strspn(label, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
"0123456789 _-") != strlen(label))
return mcde_sys_fail(MCDE_SYS_EINVAL, mcde_sys_msg(MSG_NDISK_BAD_NAME, "A name has letters, digits, blanks, - and _, at most 32."));
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);
{
char *destroy[] = { gptp, "destroy", (char *) name, NULL };
char *create[] = { gptp, "create", "-f", (char *) name, NULL };
char *add[] = { gptp, "add", "-t", (char *) gtype, "-l",
(char *) label, (char *) name, NULL };
char *make[] = { dkctl, (char *) name, "makewedges", NULL };
char *list[] = { dkctl, (char *) name, "listwedges", NULL };
(void) mcde_sys_run(destroy); /* none there: that is fine */
if (mcde_sys_run(create) != MCDE_SYS_OK ||
mcde_sys_run(add) != MCDE_SYS_OK ||
mcde_sys_run(make) != MCDE_SYS_OK ||
mcde_sys_capture(list, out, sizeof out) != MCDE_SYS_OK)
return MCDE_SYS_EFAIL;
}
if ((p = strstr(out, "\ndk")) == NULL)
return mcde_sys_fail(MCDE_SYS_EFAIL, mcde_sys_msg(MSG_NDISK_PART_MISSING, "The new partition did not appear."));
p++;
snprintf(part, len, "%.*s", (int) strcspn(p, ":"), p);
snprintf(rdev, sizeof rdev, "/dev/r%s", part);
if (strcmp(fstype, "ffs") == 0) {
char *newfs[] = { "/sbin/newfs", "-O", "2", rdev, NULL };
return mcde_sys_run(newfs);
} else {
char *newfs[] = { "/sbin/newfs_msdos", "-F", "32", "-L",
(char *) label, rdev, NULL };
return mcde_sys_run(newfs);
}
}
/* ---- the permissions: mtree(8) against the specification sysinst left
* for every installed set, /etc/mtree/set.NAME -- files not in the set left
* alone (-e), -U to set them back (docs/reference/permissions.txt). Only
* owner, group and mode count: -k did not keep mtree from comparing the
* sizes and checksums the specification names (every changed file in /etc
* and every log was reported, measured), so the specification is copied
* with only type, uname, gname, mode and link. */
#include <dirent.h>
static int
Narrow(const char *from, const char *to)
{
FILE *in = fopen(from, "r"), *out;
char line[4096];
if (in == NULL)
return 0;
if ((out = fopen(to, "w")) == NULL) {
fclose(in);
return 0;
}
while (fgets(line, sizeof line, in) != NULL) {
char *tok, *last = NULL;
int first = 1;
line[strcspn(line, "\n")] = '\0';
if (line[0] == '#' || line[0] == '\0') {
fprintf(out, "%s\n", line);
continue;
}
for (tok = strtok_r(line, " \t", &last); tok != NULL;
tok = strtok_r(NULL, " \t", &last)) {
/* the path (or /set, ..) and the keywords that count */
if (first || strncmp(tok, "type=", 5) == 0 ||
strncmp(tok, "uname=", 6) == 0 ||
strncmp(tok, "gname=", 6) == 0 ||
strncmp(tok, "mode=", 5) == 0 ||
strncmp(tok, "link=", 5) == 0)
fprintf(out, "%s%s", first ? "" : " ", tok);
first = 0;
}
fputc('\n', out);
}
fclose(in);
return fclose(out) == 0;
}
int
mcde_sys_permissions(int repair, char *log, size_t len)
{
DIR *dir = opendir("/etc/mtree");
struct dirent *de;
static char out[65536];
size_t used = 0;
int lines = 0;
log[0] = '\0';
if (dir == NULL)
return mcde_sys_fail(MCDE_SYS_ENOENT, mcde_sys_msg(MSG_NDISK_NO_MTREE, "There is no /etc/mtree."));
if (repair && geteuid() != 0) {
closedir(dir);
return mcde_sys_fail(MCDE_SYS_EPERM, mcde_sys_msg(MSG_NDISK_FIX_EPERM, "Only the administrator can repair permissions."));
}
while ((de = readdir(dir)) != NULL) {
char full[300], spec[64], *line, *last = NULL;
char *argv[10];
int k = 0, fd;
if (strncmp(de->d_name, "set.", 4) != 0)
continue;
snprintf(full, sizeof full, "/etc/mtree/%s", de->d_name);
snprintf(spec, sizeof spec, "/tmp/mcde-mtree.XXXXXX");
if ((fd = mkstemp(spec)) < 0)
continue;
close(fd);
if (!Narrow(full, spec)) {
unlink(spec);
continue;
}
argv[k++] = "/usr/sbin/mtree";
argv[k++] = "-e";
if (repair)
argv[k++] = "-U";
argv[k++] = "-f";
argv[k++] = spec;
argv[k++] = "-p";
argv[k++] = "/";
argv[k] = NULL;
/* mtree ends with a non-zero status when something differs:
* its report is what is wanted either way */
(void) mcde_sys_capture(argv, out, sizeof out);
unlink(spec);
for (line = strtok_r(out, "\n", &last); line != NULL;
line = strtok_r(NULL, "\n", &last)) {
/* the report's own lines that name a file, indented
* lines tell what differed */
if (line[0] == '\0')
continue;
if (used + strlen(line) + 2 < len) {
used += (size_t) snprintf(log + used,
len - used, "%s\n", line);
}
if (line[0] != '\t' && line[0] != ' ')
lines++;
}
}
closedir(dir);
return lines;
}