commit 688886add2fcc47b2563854f97bbab8a5b2a08f4
parent 6cab8bc1f930e679a1e0cfde7d5fde9804e4ec1f
Author: xplshn <anto@xplshn.com.ar>
Date: Fri, 27 Nov 2026 13:14:57 -0300
diskutil: removable disks, mount, unmount and power off by backend
Diffstat:
6 files changed, 244 insertions(+), 0 deletions(-)
diff --git a/shared/diskutil.h b/shared/diskutil.h
@@ -24,6 +24,7 @@ struct BlockDevInfo {
char mountpoint[256];
char fstype[32];
char model[64]; // what the disk calls itself, "" when it does not say
+ int removable; // made to be pulled out: a USB stick, a card. a partition takes its disk's
struct BlockDevInfo *parts;
struct BlockDevInfo *next;
};
@@ -53,4 +54,17 @@ void blockdev_sys_size(struct BlockDev *dev);
void blockdev_sys_reread(struct BlockDev *dev);
struct BlockDevInfo *blockdev_probe_list(void);
+/* mounting, by backend too. the flags are these whatever a kernel calls
+ * them, and data is the filesystem's own options, NULL for none. each is
+ * -1 with errno set where it fails, ENOSYS where the kernel has no backend */
+#define BLOCKDEV_RDONLY 0x1
+#define BLOCKDEV_NOSUID 0x2
+#define BLOCKDEV_NODEV 0x4
+#define BLOCKDEV_NOEXEC 0x8
+int blockdev_mount(const char *dev, const char *dir, const char *fstype, int flags, const char *data);
+int blockdev_unmount(const char *dir);
+/* the unit holding disk, a name as blockdev_get_list gives it, let go and
+ * powered off so it can be pulled. its filesystems must be unmounted first */
+int blockdev_poweroff(const char *disk);
+
#endif
diff --git a/shared/libutil/diskutil_fbsd.c b/shared/libutil/diskutil_fbsd.c
@@ -4,6 +4,7 @@
#if defined(__FreeBSD__) && !defined(ARUU_FORCE_POSIX)
#include <dirent.h>
+#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@@ -137,4 +138,32 @@ blockdev_get_list(void)
return head;
}
+
+int
+blockdev_mount(const char *dev, const char *dir, const char *fstype, int flags, const char *data)
+{
+ (void)dev;
+ (void)dir;
+ (void)fstype;
+ (void)flags;
+ (void)data;
+ errno = ENOSYS;
+ return -1;
+}
+
+int
+blockdev_unmount(const char *dir)
+{
+ (void)dir;
+ errno = ENOSYS;
+ return -1;
+}
+
+int
+blockdev_poweroff(const char *disk)
+{
+ (void)disk;
+ errno = ENOSYS;
+ return -1;
+}
#endif
diff --git a/shared/libutil/diskutil_linux.c b/shared/libutil/diskutil_linux.c
@@ -3,12 +3,19 @@
#include "../util.h"
#if defined(__linux__) && !defined(ARUU_FORCE_POSIX)
+#include <errno.h>
+#include <fcntl.h>
+#include <limits.h>
#include <linux/fs.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
+#include <sys/mount.h>
#include <sys/stat.h>
+#include <unistd.h>
+
+#define SYS_BLOCK ARUU_LINUX_PATH_SYS "/block"
void
blockdev_sys_size(struct BlockDev *dev)
@@ -57,6 +64,30 @@ disk_model(const char *name, char *out, size_t n)
fclose(f);
}
+/* a disk the kernel calls removable is a card reader or a disc drive;
+ * a USB stick or an SD card often says it is not, so the bus it hangs
+ * from counts as well */
+static int
+disk_removable(const char *name)
+{
+ char path[PATH_MAX];
+ char real[PATH_MAX];
+ FILE *f;
+ int c = 0;
+
+ snprintf(path, sizeof(path), SYS_BLOCK "/%s/removable", name);
+ if ((f = fopen(path, "r"))) {
+ c = fgetc(f);
+ fclose(f);
+ }
+ if (c == '1')
+ return 1;
+ snprintf(path, sizeof(path), SYS_BLOCK "/%s", name);
+ if (!realpath(path, real))
+ return 0;
+ return strstr(real, "/usb") || strstr(real, "/mmc_host/");
+}
+
struct BlockDevInfo *
blockdev_get_list(void)
{
@@ -108,6 +139,7 @@ blockdev_get_list(void)
}
if (is_part) {
strlcpy(info->type, "part", sizeof(info->type));
+ info->removable = curr->removable;
struct BlockDevInfo *p = curr->parts;
if (!p) {
curr->parts = info;
@@ -124,6 +156,7 @@ blockdev_get_list(void)
if (!is_part) {
strlcpy(info->type, "disk", sizeof(info->type));
disk_model(info->name, info->model, sizeof(info->model));
+ info->removable = disk_removable(info->name);
if (!head) {
head = info;
} else {
@@ -163,4 +196,84 @@ blockdev_get_list(void)
return head;
}
+int
+blockdev_mount(const char *dev, const char *dir, const char *fstype, int flags, const char *data)
+{
+ unsigned long mf = 0;
+
+ if (flags & BLOCKDEV_RDONLY)
+ mf |= MS_RDONLY;
+ if (flags & BLOCKDEV_NOSUID)
+ mf |= MS_NOSUID;
+ if (flags & BLOCKDEV_NODEV)
+ mf |= MS_NODEV;
+ if (flags & BLOCKDEV_NOEXEC)
+ mf |= MS_NOEXEC;
+ return mount(dev, dir, fstype, mf, data);
+}
+
+int
+blockdev_unmount(const char *dir)
+{
+ return umount(dir);
+}
+
+static int
+sys_write1(const char *path)
+{
+ int fd, n;
+
+ if ((fd = open(path, O_WRONLY)) < 0)
+ return -1;
+ n = write(fd, "1", 1);
+ close(fd);
+ return n == 1 ? 0 : -1;
+}
+
+/* the USB device a disk hangs from: the nearest folder above it in sysfs
+ * that has an idVendor, as a device has and its interfaces do not */
+static int
+usb_parent(const char *real, char *out, size_t n)
+{
+ char path[PATH_MAX];
+ char *slash;
+
+ strlcpy(out, real, n);
+ while ((slash = strrchr(out, '/')) && slash != out) {
+ *slash = '\0';
+ snprintf(path, sizeof(path), "%s/idVendor", out);
+ if (access(path, F_OK) == 0)
+ return 0;
+ }
+ return -1;
+}
+
+int
+blockdev_poweroff(const char *disk)
+{
+ char path[PATH_MAX];
+ char real[PATH_MAX];
+ char usb[PATH_MAX];
+ int have_usb;
+
+ if (strchr(disk, '/') || disk[0] == '.') {
+ errno = EINVAL;
+ return -1;
+ }
+ snprintf(path, sizeof(path), SYS_BLOCK "/%s", disk);
+ if (!realpath(path, real))
+ return -1;
+ /* found before the disk is let go, which takes its sysfs folders away */
+ have_usb = usb_parent(real, usb, sizeof(usb)) == 0;
+ sync();
+ /* the disk let go of: the kernel flushes its cache and forgets it */
+ snprintf(path, sizeof(path), SYS_BLOCK "/%s/device/delete", disk);
+ if (sys_write1(path) < 0 && !have_usb)
+ return -1;
+ if (!have_usb)
+ return 0;
+ /* the port cuts the unit's power, as a hub does for a device it removes */
+ snprintf(path, sizeof(path), "%s/remove", usb);
+ return sys_write1(path);
+}
#endif
diff --git a/shared/libutil/diskutil_nbsd.c b/shared/libutil/diskutil_nbsd.c
@@ -2,6 +2,7 @@
#include "../diskutil.h"
#if defined(__NetBSD__) && !defined(ARUU_FORCE_POSIX)
+#include <errno.h>
#include <sys/types.h>
#include <sys/disklabel.h>
#include <sys/dkio.h>
@@ -29,4 +30,32 @@ blockdev_get_list(void)
{
return blockdev_probe_list();
}
+
+int
+blockdev_mount(const char *dev, const char *dir, const char *fstype, int flags, const char *data)
+{
+ (void)dev;
+ (void)dir;
+ (void)fstype;
+ (void)flags;
+ (void)data;
+ errno = ENOSYS;
+ return -1;
+}
+
+int
+blockdev_unmount(const char *dir)
+{
+ (void)dir;
+ errno = ENOSYS;
+ return -1;
+}
+
+int
+blockdev_poweroff(const char *disk)
+{
+ (void)disk;
+ errno = ENOSYS;
+ return -1;
+}
#endif
diff --git a/shared/libutil/diskutil_obsd.c b/shared/libutil/diskutil_obsd.c
@@ -3,6 +3,7 @@
#include "../util.h"
#if defined(__OpenBSD__) && !defined(ARUU_FORCE_POSIX)
+#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@@ -133,4 +134,32 @@ blockdev_get_list(void)
return head;
}
+
+int
+blockdev_mount(const char *dev, const char *dir, const char *fstype, int flags, const char *data)
+{
+ (void)dev;
+ (void)dir;
+ (void)fstype;
+ (void)flags;
+ (void)data;
+ errno = ENOSYS;
+ return -1;
+}
+
+int
+blockdev_unmount(const char *dir)
+{
+ (void)dir;
+ errno = ENOSYS;
+ return -1;
+}
+
+int
+blockdev_poweroff(const char *disk)
+{
+ (void)disk;
+ errno = ENOSYS;
+ return -1;
+}
#endif
diff --git a/shared/libutil/diskutil_posix.c b/shared/libutil/diskutil_posix.c
@@ -1,6 +1,8 @@
/* see LICENSE file for copyright and license details */
#include "../diskutil.h"
+#include <errno.h>
+
/* a kernel with no backend: a device's size is what fstat says, and the
* list of disks is what opens under the names most systems give them */
#if !(defined(__linux__) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__)) \
@@ -22,4 +24,32 @@ blockdev_get_list(void)
{
return blockdev_probe_list();
}
+
+int
+blockdev_mount(const char *dev, const char *dir, const char *fstype, int flags, const char *data)
+{
+ (void)dev;
+ (void)dir;
+ (void)fstype;
+ (void)flags;
+ (void)data;
+ errno = ENOSYS;
+ return -1;
+}
+
+int
+blockdev_unmount(const char *dir)
+{
+ (void)dir;
+ errno = ENOSYS;
+ return -1;
+}
+
+int
+blockdev_poweroff(const char *disk)
+{
+ (void)disk;
+ errno = ENOSYS;
+ return -1;
+}
#endif