/* * 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 #include #include #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; }