libblkid-tiny: use blkid_probe_set_utf8label for label set
[oweals/fstools.git] / block.c
diff --git a/block.c b/block.c
index cfd95b42ab871efcab49c265dec95b7215a5a3bb..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;
                }
@@ -960,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 };
@@ -1012,10 +1012,14 @@ 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)
@@ -1108,12 +1112,13 @@ static int mount_device(struct probe_info *pr, int type)
 
        handle_swapfiles(true);
 
-       hotplug_call_mount("add", device);
+       if (type != TYPE_AUTOFS)
+               hotplug_call_mount("add", device);
 
        return 0;
 }
 
-static int umount_device(char *path)
+static int umount_device(char *path, int type)
 {
        char *mp;
        int err;
@@ -1122,14 +1127,17 @@ static int umount_device(char *path)
        if (!mp)
                return -1;
 
-       hotplug_call_mount("remove", basename(path));
+       if (type != TYPE_AUTOFS)
+               hotplug_call_mount("remove", basename(path));
 
        err = umount2(mp, MNT_DETACH);
-       if (err)
+       if (err) {
                ULOG_ERR("unmounting %s (%s) failed (%d) - %m\n", path, mp,
                         errno);
-       else
+       } else {
                ULOG_INFO("unmounted %s (%s)\n", path, mp);
+               rmdir(mp);
+       }
 
        free(mp);
        return err;
@@ -1147,7 +1155,7 @@ static int mount_action(char *action, char *device, int type)
                if (type == TYPE_HOTPLUG)
                        blockd_notify(device, NULL, NULL);
 
-               umount_device(path);
+               umount_device(path, type);
 
                return 0;
        } else if (strcmp(action, "add")) {
@@ -1170,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;
 
@@ -1181,20 +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);
        }
 
-       if (argc < 4)
-               return -EINVAL;
+       if (err) {
+               ULOG_ERR("autofs: \"%s\" action has failed: %d\n", argv[2], err);
+       }
 
-       return mount_action(argv[2], argv[3], TYPE_AUTOFS);
+       return err;
 }
 
 static int find_block_mtd(char *name, char *part, int plen)
@@ -1601,7 +1624,7 @@ static int main_umount(int argc, char **argv)
                if (m && m->extroot)
                        continue;
 
-               umount_device(pr->dev);
+               umount_device(pr->dev, TYPE_DEV);
        }
 
        return 0;