block: prevent mount point confusion
[oweals/fstools.git] / block.c
diff --git a/block.c b/block.c
index 2e3841ab15554b2b7980b54e3f7f62ddb51adb32..39212d247fe46e07b4ea4113edf4687b59b7d4e7 100644 (file)
--- a/block.c
+++ b/block.c
@@ -579,7 +579,6 @@ static char* find_mount_point(char *block)
 {
        FILE *fp = fopen("/proc/self/mountinfo", "r");
        static char line[256];
-       int len = strlen(block);
        char *point = NULL, *pos, *tmp, *cpoint, *devname;
        struct stat s;
        int rstat;
@@ -643,7 +642,7 @@ static char* find_mount_point(char *block)
 
                *pos = '\0';
                devname = tmp;
-               if (!strncmp(block, devname, len)) {
+               if (!strcmp(block, devname)) {
                        point = strdup(cpoint);
                        break;
                }
@@ -742,23 +741,25 @@ static void check_filesystem(struct probe_info *pr)
        if (!pid) {
                if(!strncmp(pr->type, "f2fs", 4)) {
                        execl(ckfs, ckfs, "-f", pr->dev, NULL);
-                       exit(-1);
+                       exit(EXIT_FAILURE);
                } else if(!strncmp(pr->type, "btrfs", 5)) {
                        execl(ckfs, ckfs, "--repair", pr->dev, NULL);
-                       exit(-1);
+                       exit(EXIT_FAILURE);
                } else if(!strncmp(pr->type, "ntfs", 4)) {
                        execl(ckfs, ckfs, "-b", pr->dev, NULL);
-                       exit(-1);
+                       exit(EXIT_FAILURE);
                } else {
                        execl(ckfs, ckfs, "-p", pr->dev, NULL);
-                       exit(-1);
+                       exit(EXIT_FAILURE);
                }
        } else if (pid > 0) {
                int status;
 
                waitpid(pid, &status, 0);
-               if (WEXITSTATUS(status))
+               if (WIFEXITED(status) && WEXITSTATUS(status))
                        ULOG_ERR("check_filesystem: %s returned %d\n", ckfs, WEXITSTATUS(status));
+               if (WIFSIGNALED(status))
+                       ULOG_ERR("check_filesystem: %s terminated by %s\n", ckfs, strsignal(WTERMSIG(status)));
        }
 }
 
@@ -878,6 +879,35 @@ static int exec_mount(const char *source, const char *target,
        return err;
 }
 
+static int hotplug_call_mount(const char *action, const char *device)
+{
+       pid_t pid;
+       int err = 0;
+
+       pid = fork();
+       if (!pid) {
+               char * const argv[] = { "hotplug-call", "mount", NULL };
+
+               setenv("ACTION", action, 1);
+               setenv("DEVICE", device, 1);
+
+               execv("/sbin/hotplug-call", argv);
+               exit(-1);
+       } else if (pid > 0) {
+               int status;
+
+               pid = waitpid(pid, &status, 0);
+               if (pid <= 0 || !WIFEXITED(status) || WEXITSTATUS(status)) {
+                       err = -ENOEXEC;
+                       ULOG_ERR("hotplug-call call failed\n");
+               }
+       } else {
+               err = -errno;
+       }
+
+       return err;
+}
+
 static int handle_mount(const char *source, const char *target,
                         const char *fstype, struct mount *m)
 {
@@ -929,13 +959,14 @@ static int handle_mount(const char *source, const char *target,
        return err;
 }
 
-static void blockd_notify(char *device, struct mount *m, struct probe_info *pr)
+static int blockd_notify(char *device, struct mount *m, struct probe_info *pr)
 {
        struct ubus_context *ctx = ubus_connect(NULL);
        uint32_t id;
+       int err;
 
        if (!ctx)
-               return;
+               return -ENXIO;
 
        if (!ubus_lookup_id(ctx, "block", &id)) {
                struct blob_buf buf = { 0 };
@@ -981,17 +1012,24 @@ static void blockd_notify(char *device, struct mount *m, struct probe_info *pr)
                        blobmsg_add_u32(&buf, "remove", 1);
                }
 
-               ubus_invoke(ctx, id, "hotplug", buf.head, NULL, NULL, 3000);
+               err = ubus_invoke(ctx, id, "hotplug", buf.head, NULL, NULL, 3000);
+       } else {
+               err = -ENOENT;
        }
 
        ubus_free(ctx);
+
+       return err;
 }
 
 static int mount_device(struct probe_info *pr, int type)
 {
        struct mount *m;
+       char _target[32];
+       char *target;
        char *device;
        char *mp;
+       int err;
 
        if (!pr)
                return -1;
@@ -1019,103 +1057,87 @@ static int mount_device(struct probe_info *pr, int type)
        if (m && m->extroot)
                return -1;
 
-       if (m) switch (type) {
-       case TYPE_HOTPLUG:
+       if (type == TYPE_HOTPLUG)
                blockd_notify(device, m, pr);
-               if (m->autofs)
-                       return 0;
-               if (!auto_mount)
-                       return -1;
-               break;
-       case TYPE_AUTOFS:
-               if (!m->autofs)
-                       return -1;
-               break;
-       case TYPE_DEV:
-               if (m->autofs)
-                       return -1;
-               break;
-       } else if (type == TYPE_HOTPLUG) {
-               blockd_notify(device, NULL, pr);
-       }
 
+       /* Check if device should be mounted & set the target directory */
        if (m) {
-               char *target = m->target;
-               char _target[32];
-               int err = 0;
+               switch (type) {
+               case TYPE_HOTPLUG:
+                       if (m->autofs)
+                               return 0;
+                       if (!auto_mount)
+                               return -1;
+                       break;
+               case TYPE_AUTOFS:
+                       if (!m->autofs)
+                               return -1;
+                       break;
+               case TYPE_DEV:
+                       if (m->autofs)
+                               return -1;
+                       break;
+               }
 
                if (m->autofs) {
                        snprintf(_target, sizeof(_target), "/tmp/run/blockd/%s", device);
                        target = _target;
-               }
-               if (!target) {
+               } else if (m->target) {
+                       target = m->target;
+               } else {
                        snprintf(_target, sizeof(_target), "/mnt/%s", device);
                        target = _target;
                }
-               mkdir_p(target);
-
-               if (check_fs)
-                       check_filesystem(pr);
-
-               err = handle_mount(pr->dev, target, pr->type, m);
-               if (err)
-                       ULOG_ERR("mounting %s (%s) as %s failed (%d) - %m\n",
-                                pr->dev, pr->type, target, errno);
-               else
-                       handle_swapfiles(true);
-               return err;
+       } else if (anon_mount) {
+               snprintf(_target, sizeof(_target), "/mnt/%s", device);
+               target = _target;
+       } else {
+               /* No reason to mount this device */
+               return 0;
        }
 
-       if (anon_mount) {
-               char target[32];
-               int err = 0;
+       /* Mount the device */
 
-               snprintf(target, sizeof(target), "/mnt/%s", device);
-               mkdir_p(target);
+       if (check_fs)
+               check_filesystem(pr);
 
-               if (check_fs)
-                       check_filesystem(pr);
+       mkdir_p(target);
 
-               err = handle_mount(pr->dev, target, pr->type, NULL);
-               if (err)
-                       ULOG_ERR("mounting %s (%s) as %s failed (%d) - %m\n",
-                                pr->dev, pr->type, target, errno);
-               else
-                       handle_swapfiles(true);
+       err = handle_mount(pr->dev, target, pr->type, m);
+       if (err) {
+               ULOG_ERR("mounting %s (%s) as %s failed (%d) - %m\n",
+                               pr->dev, pr->type, target, errno);
                return err;
        }
 
+       handle_swapfiles(true);
+
+       if (type != TYPE_AUTOFS)
+               hotplug_call_mount("add", device);
+
        return 0;
 }
 
-static int umount_device(struct probe_info *pr)
+static int umount_device(char *path, int type)
 {
-       struct mount *m;
-       char *device = basename(pr->dev);
        char *mp;
        int err;
 
-       if (!pr)
-               return -1;
-
-       if (!strcmp(pr->type, "swap"))
-               return -1;
-
-       mp = find_mount_point(pr->dev);
+       mp = find_mount_point(path);
        if (!mp)
                return -1;
 
-       m = find_block(pr->uuid, pr->label, device, NULL);
-       if (m && m->extroot)
-               return -1;
+       if (type != TYPE_AUTOFS)
+               hotplug_call_mount("remove", basename(path));
 
        err = umount2(mp, MNT_DETACH);
-       if (err)
-               ULOG_ERR("unmounting %s (%s)  failed (%d) - %m\n",
-                        pr->dev, mp, errno);
-       else
-               ULOG_INFO("unmounted %s (%s)\n",
-                         pr->dev, mp);
+       if (err) {
+               ULOG_ERR("unmounting %s (%s) failed (%d) - %m\n", path, mp,
+                        errno);
+       } else {
+               ULOG_INFO("unmounted %s (%s)\n", path, mp);
+               rmdir(mp);
+       }
 
        free(mp);
        return err;
@@ -1124,27 +1146,17 @@ static int umount_device(struct probe_info *pr)
 static int mount_action(char *action, char *device, int type)
 {
        char path[32];
-       char *mount_point;
 
        if (!action || !device)
                return -1;
        snprintf(path, sizeof(path), "/dev/%s", device);
 
        if (!strcmp(action, "remove")) {
-               int err = 0;
-
                if (type == TYPE_HOTPLUG)
                        blockd_notify(device, NULL, NULL);
 
-               mount_point = find_mount_point(path);
-               if (mount_point)
-                       err = umount2(mount_point, MNT_DETACH);
-
-               if (err)
-                       ULOG_ERR("umount of %s failed (%d) - %m\n",
-                                mount_point, errno);
+               umount_device(path, type);
 
-               free(mount_point);
                return 0;
        } else if (strcmp(action, "add")) {
                ULOG_ERR("Unkown action %s\n", action);
@@ -1166,6 +1178,8 @@ static int main_hotplug(int argc, char **argv)
 
 static int main_autofs(int argc, char **argv)
 {
+       int err = 0;
+
        if (argc < 3)
                return -1;
 
@@ -1177,16 +1191,33 @@ static int main_autofs(int argc, char **argv)
 
                cache_load(0);
                list_for_each_entry(pr, &devices, list) {
-                       struct mount *m = find_block(pr->uuid, pr->label, NULL, NULL);
+                       struct mount *m;
 
-                       if (m && m->autofs)
-                               mount_device(pr, TYPE_HOTPLUG);
-                       else
-                               blockd_notify(pr->dev, m, pr);
+                       if (!strcmp(pr->type, "swap"))
+                               continue;
+
+                       m = find_block(pr->uuid, pr->label, NULL, NULL);
+                       if (m && m->extroot)
+                               continue;
+
+                       blockd_notify(pr->dev, m, pr);
                }
-               return 0;
+       } else if (!strcmp(argv[2], "available")) {
+               err = hotplug_call_mount("add", argv[3]);
+       } else if (!strcmp(argv[2], "unavailable")) {
+               err = hotplug_call_mount("remove", argv[3]);
+       } else {
+               if (argc < 4)
+                       return -EINVAL;
+
+               err = mount_action(argv[2], argv[3], TYPE_AUTOFS);
        }
-       return mount_action(argv[2], argv[3], TYPE_AUTOFS);
+
+       if (err) {
+               ULOG_ERR("autofs: \"%s\" action has failed: %d\n", argv[2], err);
+       }
+
+       return err;
 }
 
 static int find_block_mtd(char *name, char *part, int plen)
@@ -1583,8 +1614,18 @@ static int main_umount(int argc, char **argv)
        handle_swapfiles(false);
 
        cache_load(0);
-       list_for_each_entry(pr, &devices, list)
-               umount_device(pr);
+       list_for_each_entry(pr, &devices, list) {
+               struct mount *m;
+
+               if (!strcmp(pr->type, "swap"))
+                       continue;
+
+               m = find_block(pr->uuid, pr->label, basename(pr->dev), NULL);
+               if (m && m->extroot)
+                       continue;
+
+               umount_device(pr->dev, TYPE_DEV);
+       }
 
        return 0;
 }