partname: Correct fstools_partname_fallback_scan comparison
[project/fstools.git] / blockd.c
index 94cb02e4bbe9b3f44b35c9d6aaab4c8c144ac2e9..9572fd0087d5c98d385e2fac492c7a6876970710 100644 (file)
--- a/blockd.c
+++ b/blockd.c
@@ -14,6 +14,7 @@
 #include <linux/auto_fs4.h>
 
 #include <libubox/uloop.h>
+#include <libubox/utils.h>
 #include <libubox/vlist.h>
 #include <libubox/ulog.h>
 #include <libubox/avl-cmp.h>
@@ -134,7 +135,7 @@ block(char *cmd, char *action, char *device, int sync, struct uloop_process *pro
 }
 
 static int send_block_notification(struct ubus_context *ctx, const char *action,
-                           const char *devname);
+                           const char *devname, const char *target);
 static int hotplug_call_mount(struct ubus_context *ctx, const char *action,
                              const char *devname, uloop_process_handler cb, void *priv)
 {
@@ -152,6 +153,9 @@ static int hotplug_call_mount(struct ubus_context *ctx, const char *action,
        pid = fork();
        switch (pid) {
        case -1:
+               if (c)
+                       free(c);
+
                err = -errno;
                ULOG_ERR("fork() failed\n");
                return err;
@@ -174,8 +178,6 @@ static int hotplug_call_mount(struct ubus_context *ctx, const char *action,
                break;
        }
 
-       send_block_notification(ctx, action, devname);
-
        return 0;
 }
 
@@ -200,8 +202,12 @@ static void device_mount_remove_hotplug_cb(struct uloop_process *p, int stat)
 
 static void device_mount_remove(struct ubus_context *ctx, struct device *device)
 {
-       hotplug_call_mount(ctx, "remove", device->name,
+       static const char *action = "remove";
+
+       hotplug_call_mount(ctx, action, device->name,
                           device_mount_remove_hotplug_cb, device);
+
+       send_block_notification(ctx, action, device->name, device->target);
 }
 
 static void device_mount_add(struct ubus_context *ctx, struct device *device)
@@ -226,11 +232,13 @@ static void device_mount_add(struct ubus_context *ctx, struct device *device)
                *tmp = '/';
        }
 
-       if (symlink(path, device->target))
+       if (symlink(path, device->target)) {
                ULOG_ERR("failed to symlink %s->%s (%d) - %m\n", device->target, path, errno);
-       else
-               hotplug_call_mount(ctx, "add", device->name, NULL, NULL);
-
+       } else {
+               static const char *action = "add";
+               hotplug_call_mount(ctx, action, device->name, NULL, NULL);
+               send_block_notification(ctx, action, device->name, device->target);
+       }
        free(path);
 }
 
@@ -341,13 +349,11 @@ block_hotplug(struct ubus_context *ctx, struct ubus_object *obj,
 
                vlist_add(&devices, &device->node, device->name);
 
-               if (old && !device_move(old, device)) {
-                       if (device->autofs) {
-                               device_mount_remove(ctx, old);
-                               device_mount_add(ctx, device);
-                       } else {
+               if (old && device_move(old, device)) {
+                       device_mount_remove(ctx, old);
+                       device_mount_add(ctx, device);
+                       if (!device->autofs)
                                block("mount", NULL, NULL, 0, NULL);
-                       }
                } else if (device->autofs) {
                        device_mount_add(ctx, device);
                }
@@ -360,6 +366,7 @@ static int blockd_mount(struct ubus_context *ctx, struct ubus_object *obj,
                        struct ubus_request_data *req, const char *method,
                        struct blob_attr *msg)
 {
+       static const char *action = "add";
        struct blob_attr *data[__MOUNT_MAX];
        struct device *device;
        char *devname;
@@ -375,7 +382,8 @@ static int blockd_mount(struct ubus_context *ctx, struct ubus_object *obj,
        if (!device)
                return UBUS_STATUS_UNKNOWN_ERROR;
 
-       hotplug_call_mount(ctx, "add", device->name, NULL, NULL);
+       hotplug_call_mount(ctx, action, device->name, NULL, NULL);
+       send_block_notification(ctx, action, device->name, device->target);
 
        return 0;
 }
@@ -402,7 +410,10 @@ static int blockd_umount(struct ubus_context *ctx, struct ubus_object *obj,
 {
        struct blob_attr *data[__MOUNT_MAX];
        struct blockd_umount_context *c;
+       static const char *action = "remove";
        char *devname;
+       static char oldtarget[PATH_MAX];
+       struct device *device;
        int err;
 
        blobmsg_parse(mount_policy, __MOUNT_MAX, data, blob_data(msg), blob_len(msg));
@@ -411,6 +422,11 @@ static int blockd_umount(struct ubus_context *ctx, struct ubus_object *obj,
                return UBUS_STATUS_INVALID_ARGUMENT;
 
        devname = blobmsg_get_string(data[MOUNT_DEVICE]);
+       device = vlist_find(&devices, devname, device, node);
+       if (device) {
+               strncpy(oldtarget, device->target, sizeof(oldtarget)-1);
+               oldtarget[PATH_MAX - 1] = '\0';
+       }
 
        c = calloc(1, sizeof(*c));
        if (!c)
@@ -419,12 +435,14 @@ static int blockd_umount(struct ubus_context *ctx, struct ubus_object *obj,
        c->ctx = ctx;
        ubus_defer_request(ctx, req, &c->req);
 
-       err = hotplug_call_mount(ctx, "remove", devname, blockd_umount_hotplug_cb, c);
+       err = hotplug_call_mount(ctx, action, devname, blockd_umount_hotplug_cb, c);
        if (err) {
                free(c);
                return UBUS_STATUS_UNKNOWN_ERROR;
        }
 
+       send_block_notification(ctx, action, devname, oldtarget);
+
        return 0;
 }
 
@@ -502,7 +520,7 @@ static struct ubus_object block_object = {
 
 /* send ubus event for successful mounts, useful for procd triggers */
 static int send_block_notification(struct ubus_context *ctx, const char *action,
-                           const char *devname)
+                           const char *devname, const char *target)
 {
        struct blob_buf buf = { 0 };
        char evname[16] = "mount.";
@@ -516,7 +534,10 @@ static int send_block_notification(struct ubus_context *ctx, const char *action,
        blob_buf_init(&buf, 0);
 
        if (devname)
-               blobmsg_add_string(&buf, "devname", devname);
+               blobmsg_add_string(&buf, "device", devname);
+
+       if (target)
+               blobmsg_add_string(&buf, "target", target);
 
        err = ubus_notify(ctx, &block_object, evname, buf.head, -1);
 
@@ -634,7 +655,7 @@ static int autofs_mount(void)
 
 static void blockd_startup_cb(struct uloop_process *p, int stat)
 {
-       send_block_notification(&conn.ctx, "ready", NULL);
+       send_block_notification(&conn.ctx, "ready", NULL, NULL);
 }
 
 static struct uloop_process startup_process = {