block: use dynamically allocated target string
[project/fstools.git] / block.c
diff --git a/block.c b/block.c
index 9458d52ba6ee0123b897412d175cba8944a9da23..a613fd7648c3112578528f2d7fb5833d08e1e7c1 100644 (file)
--- a/block.c
+++ b/block.c
 #include <uci.h>
 #include <uci_blob.h>
 
-#include <libubox/ulog.h>
+#include <libubox/avl-cmp.h>
+#include <libubox/blobmsg_json.h>
 #include <libubox/list.h>
+#include <libubox/ulog.h>
+#include <libubox/utils.h>
 #include <libubox/vlist.h>
-#include <libubox/blobmsg_json.h>
-#include <libubox/avl-cmp.h>
 #include <libubus.h>
 
 #include "probe.h"
@@ -697,18 +698,6 @@ static int print_block_info(struct probe_info *pr)
        return 0;
 }
 
-static void mkdir_p(char *dir)
-{
-       char *l = strrchr(dir, '/');
-
-       if (l) {
-               *l = '\0';
-               mkdir_p(dir);
-               *l = '/';
-               mkdir(dir, 0755);
-       }
-}
-
 static void check_filesystem(struct probe_info *pr)
 {
        pid_t pid;
@@ -1005,7 +994,7 @@ static int mount_device(struct probe_info *pr, int type)
 {
        struct mount *m;
        struct stat st;
-       char _target[32];
+       char *_target = NULL;
        char *target;
        char *device;
        char *mp;
@@ -1032,7 +1021,7 @@ static int mount_device(struct probe_info *pr, int type)
 
        mp = find_mount_point(pr->dev);
        if (mp) {
-               if (m && m->type == TYPE_MOUNT && strcmp(m->target, mp)) {
+               if (m && m->type == TYPE_MOUNT && m->target && strcmp(m->target, mp)) {
                        ULOG_ERR("%s is already mounted on %s\n", pr->dev, mp);
                        err = -1;
                } else
@@ -1064,16 +1053,22 @@ static int mount_device(struct probe_info *pr, int type)
                }
 
                if (m->autofs) {
-                       snprintf(_target, sizeof(_target), "/tmp/run/blockd/%s", device);
+                       if (asprintf(&_target, "/tmp/run/blockd/%s", device) == -1)
+                               exit(ENOMEM);
+
                        target = _target;
                } else if (m->target) {
                        target = m->target;
                } else {
-                       snprintf(_target, sizeof(_target), "/mnt/%s", device);
+                       if (asprintf(&_target, "/mnt/%s", device) == -1)
+                               exit(ENOMEM);
+
                        target = _target;
                }
        } else if (anon_mount) {
-               snprintf(_target, sizeof(_target), "/mnt/%s", device);
+               if (asprintf(&_target, "/mnt/%s", device) == -1)
+                       exit(ENOMEM);
+
                target = _target;
        } else {
                /* No reason to mount this device */
@@ -1085,7 +1080,7 @@ static int mount_device(struct probe_info *pr, int type)
        if (check_fs)
                check_filesystem(pr);
 
-       mkdir_p(target);
+       mkdir_p(target, 0755);
        if (!lstat(target, &st) && S_ISLNK(st.st_mode))
                unlink(target);
 
@@ -1093,9 +1088,16 @@ static int mount_device(struct probe_info *pr, int type)
        if (err) {
                ULOG_ERR("mounting %s (%s) as %s failed (%d) - %m\n",
                                pr->dev, pr->type, target, errno);
+
+               if (_target)
+                       free(_target);
+
                return err;
        }
 
+       if (_target)
+               free(_target);
+
        handle_swapfiles(true);
 
        if (type != TYPE_AUTOFS)
@@ -1190,7 +1192,7 @@ static int main_autofs(int argc, char **argv)
                                continue;
 
                        blockd_notify("hotplug", pr->dev, m, pr);
-                       if (!m->autofs && (mp = find_mount_point(pr->dev))) {
+                       if ((!m || !m->autofs) && (mp = find_mount_point(pr->dev))) {
                                blockd_notify("mount", pr->dev, NULL, NULL);
                                free(mp);
                        }
@@ -1396,7 +1398,7 @@ static int check_extroot(char *path)
 
        snprintf(tag, sizeof(tag), "%s/etc", path);
        if (stat(tag, &s))
-               mkdir_p(tag);
+               mkdir_p(tag, 0755);
 
        snprintf(tag, sizeof(tag), "%s/etc/.extroot-uuid", path);
        if (stat(tag, &s)) {
@@ -1486,7 +1488,7 @@ static int mount_extroot(char *cfg)
 
                if (m->overlay)
                        path = overlay;
-               mkdir_p(path);
+               mkdir_p(path, 0755);
 
                if (check_fs)
                        check_filesystem(pr);
@@ -1556,7 +1558,7 @@ static int main_extroot(int argc, char **argv)
                         * Mount MTD part and try extroot (using
                         * /etc/config/fstab from that partition)
                         */
-                       mkdir_p(cfg);
+                       mkdir_p(cfg, 0755);
                        if (!mount(blkdev_path, cfg, "jffs2", MS_NOATIME, NULL)) {
                                err = mount_extroot(cfg);
                                umount2(cfg, MNT_DETACH);
@@ -1578,7 +1580,7 @@ static int main_extroot(int argc, char **argv)
                char cfg[] = "/tmp/ubifs_cfg";
 
                /* Mount volume and try extroot (using fstab from that vol) */
-               mkdir_p(cfg);
+               mkdir_p(cfg, 0755);
                if (!mount(blkdev_path, cfg, "ubifs", MS_NOATIME, NULL)) {
                        err = mount_extroot(cfg);
                        umount2(cfg, MNT_DETACH);