blockd: use uloop_process for calling /sbin/hotplug-call mount
[project/fstools.git] / blockd.c
index 3af5390858ace9b15ecc26a5c33fb409c59fe591..d283f24c0af9cdc5a3522d7ca1196abfbe4fb8c2 100644 (file)
--- a/blockd.c
+++ b/blockd.c
 #define AUTOFS_TIMEOUT         30
 #define AUTOFS_EXPIRE_TIMER    (5 * 1000)
 
+struct hotplug_context {
+       struct uloop_process process;
+       void *priv;
+};
+
 struct device {
        struct vlist_node node;
        struct blob_attr *msg;
@@ -95,7 +100,7 @@ block(char *cmd, char *action, char *device)
                argv[a++] = device;
                execvp(argv[0], argv);
                ULOG_ERR("failed to spawn %s %s %s\n", *argv, action, device);
-               exit(-1);
+               exit(EXIT_FAILURE);
 
        default:
                waitpid(pid, &status, 0);
@@ -108,56 +113,89 @@ block(char *cmd, char *action, char *device)
        return ret;
 }
 
-static void
-device_free(struct device *device)
+static int hotplug_call_mount(const char *action, const char *devname,
+                             uloop_process_handler cb, void *priv)
 {
-       struct blob_attr *data[__MOUNT_MAX];
-       char *target = NULL;
-       char *path = NULL, _path[64], *mp;
+       char * const argv[] = { "hotplug-call", "mount", NULL };
+       struct hotplug_context *c = NULL;
+       pid_t pid;
+       int err;
+
+       if (cb) {
+               c = calloc(1, sizeof(*c));
+               if (!c)
+                       return -ENOMEM;
+       }
+
+       pid = fork();
+       switch (pid) {
+       case -1:
+               err = -errno;
+               ULOG_ERR("fork() failed\n");
+               return err;
+       case 0:
+               uloop_end();
 
-       blobmsg_parse(mount_policy, __MOUNT_MAX, data,
-                     blob_data(device->msg), blob_len(device->msg));
+               setenv("ACTION", action, 1);
+               setenv("DEVICE", devname, 1);
 
-       if (data[MOUNT_AUTOFS]) {
-               target = device->target;
-               snprintf(_path, sizeof(_path), "/tmp/run/blockd/%s",
-                        blobmsg_get_string(data[MOUNT_DEVICE]));
-               path = _path;
-       } else {
-               path = target = device->target;
+               execv("/sbin/hotplug-call", argv);
+               exit(-1);
+               break;
+       default:
+               if (c) {
+                       c->process.pid = pid;
+                       c->process.cb = cb;
+                       c->priv = priv;
+                       uloop_process_add(&c->process);
+               }
+               break;
        }
 
+       return 0;
+}
+
+static void device_mount_remove_hotplug_cb(struct uloop_process *p, int stat)
+{
+       struct hotplug_context *hctx = container_of(p, struct hotplug_context, process);
+       struct device *device = hctx->priv;
+       char *mp;
+
+       if (device->target)
+               unlink(device->target);
+
        mp = _find_mount_point(device->name);
-       if (path && mp)
-               if (umount2(path, MNT_DETACH))
-                       ULOG_ERR("failed to unmount %s\n", path);
-       free(mp);
+       if (mp) {
+               block("autofs", "remove", device->name);
+               free(mp);
+       }
 
-       if (!target)
-               return;
+       free(device);
+       free(hctx);
+}
 
-       if (data[MOUNT_AUTOFS])
-               unlink(target);
-       else
-               rmdir(target);
+static void device_mount_remove(struct device *device)
+{
+       hotplug_call_mount("remove", device->name,
+                          device_mount_remove_hotplug_cb, device);
 }
 
-static void
-device_add(struct device *device)
+static void device_mount_add(struct device *device)
 {
-       struct blob_attr *data[__MOUNT_MAX];
+       struct stat st;
        char path[64];
 
-       blobmsg_parse(mount_policy, __MOUNT_MAX, data,
-                     blob_data(device->msg), blob_len(device->msg));
-
-       if (!data[MOUNT_AUTOFS])
-               return;
-
-       snprintf(path, sizeof(path), "/tmp/run/blockd/%s",
-                blobmsg_get_string(data[MOUNT_DEVICE]));
+       snprintf(path, sizeof(path), "/tmp/run/blockd/%s", device->name);
+       if (!lstat(device->target, &st)) {
+               if (S_ISLNK(st.st_mode))
+                       unlink(device->target);
+               else if (S_ISDIR(st.st_mode))
+                       rmdir(device->target);
+       }
        if (symlink(path, device->target))
-               ULOG_ERR("failed to symlink %s->%s\n", device->target, path);
+               ULOG_ERR("failed to symlink %s->%s (%d) - %m\n", device->target, path, errno);
+       else
+               hotplug_call_mount("add", device->name, NULL, NULL);
 }
 
 static int
@@ -175,7 +213,7 @@ device_move(struct device *device_o, struct device *device_n)
                unlink(device_o->target);
                snprintf(path, sizeof(path), "/tmp/run/blockd/%s", device_n->name);
                if (symlink(path, device_n->target))
-                       ULOG_ERR("failed to symlink %s->%s\n", device_n->target, path);
+                       ULOG_ERR("failed to symlink %s->%s (%d) - %m\n", device_n->target, path, errno);
        } else {
                mkdir(device_n->target, 0755);
                if (mount(device_o->target, device_n->target, NULL, MS_MOVE, NULL))
@@ -187,36 +225,13 @@ device_move(struct device *device_o, struct device *device_n)
        return 0;
 }
 
-static void
-devices_update_cb(struct vlist_tree *tree, struct vlist_node *node_new,
-                 struct vlist_node *node_old)
+static void vlist_nop_update(struct vlist_tree *tree,
+                            struct vlist_node *node_new,
+                            struct vlist_node *node_old)
 {
-       struct device *device_o = NULL, *device_n = NULL;
-
-       if (node_old)
-               device_o = container_of(node_old, struct device, node);
-
-       if (node_new)
-               device_n = container_of(node_new, struct device, node);
-
-       if (device_o && device_n) {
-               if (device_move(device_o, device_n)) {
-                       device_free(device_o);
-                       device_add(device_n);
-                       if (!device_n->autofs)
-                               block("mount", NULL, NULL);
-               }
-       } else if (device_n) {
-               device_add(device_n);
-       } else {
-               device_free(device_o);
-       }
-
-       if (device_o)
-               free(device_o);
 }
 
-VLIST_TREE(devices, avl_strcmp, devices_update_cb, false, false);
+VLIST_TREE(devices, avl_strcmp, vlist_nop_update, false, false);
 
 static int
 block_hotplug(struct ubus_context *ctx, struct ubus_object *obj,
@@ -254,27 +269,38 @@ block_hotplug(struct ubus_context *ctx, struct ubus_object *obj,
        if (!device)
                return UBUS_STATUS_UNKNOWN_ERROR;
 
-       vlist_update(&devices);
        if (data[MOUNT_REMOVE]) {
                vlist_delete(&devices, &device->node);
-       } else {
-               if (data[MOUNT_AUTOFS])
-                       device->autofs = blobmsg_get_u32(data[MOUNT_AUTOFS]);
-               else
-                       device->autofs = 0;
-               if (data[MOUNT_ANON])
-                       device->anon = blobmsg_get_u32(data[MOUNT_ANON]);
+
+               if (device->autofs)
+                       device_mount_remove(device);
                else
-                       device->anon = 0;
+                       free(device);
+       } else {
+               struct device *old = vlist_find(&devices, devname, device, node);
+
+               device->autofs = data[MOUNT_AUTOFS] ? blobmsg_get_u32(data[MOUNT_AUTOFS]) : 0;
+               device->anon = data[MOUNT_ANON] ? blobmsg_get_u32(data[MOUNT_ANON]) : 0;
                device->msg = _msg;
                memcpy(_msg, msg, blob_raw_len(msg));
                device->name = _name;
                strcpy(_name, devname);
                device->target = __target;
                strcpy(__target, target);
-               vlist_add(&devices, &device->node, blobmsg_get_string(data[MOUNT_DEVICE]));
+
+               vlist_add(&devices, &device->node, device->name);
+
+               if (old && !device_move(old, device)) {
+                       if (device->autofs) {
+                               device_mount_remove(old);
+                               device_mount_add(device);
+                       } else {
+                               block("mount", NULL, NULL);
+                       }
+               } else if (device->autofs) {
+                       device_mount_add(device);
+               }
        }
-       vlist_flush(&devices);
 
        return 0;
 }
@@ -302,6 +328,8 @@ block_info(struct ubus_context *ctx, struct ubus_object *obj,
                if (mp) {
                        blobmsg_add_string(&bb, "mount", mp);
                        free(mp);
+               } else if (device->autofs && device->target) {
+                       blobmsg_add_string(&bb, "mount", device->target);
                }
                blobmsg_close_table(&bb, t);
        }
@@ -422,7 +450,7 @@ static int autofs_mount(void)
        if (kproto_version != 5) {
                ULOG_ERR("only kernel protocol version 5 is tested. You have %d.\n",
                        kproto_version);
-               exit(1);
+               exit(EXIT_FAILURE);
        }
        if (ioctl(fd_autofs_write, AUTOFS_IOC_SETTIMEOUT, &autofs_timeout))
                ULOG_ERR("failed to set autofs timeout\n");