chiark / gitweb /
fstab-generator: properly detect bind mounts
[elogind.git] / src / fstab-generator / fstab-generator.c
index f832730b4041ee2bb8e2ce29d1a3e7bd6750f88f..23e5051925429ed48fa5adbf0dcbdec2849641ae 100644 (file)
@@ -47,7 +47,7 @@ static int device_name(const char *path, char **unit) {
 
         p = unit_name_from_path(path, ".device");
         if (!p)
-                return -ENOMEM;
+                return log_oom();
 
         *unit = p;
         return 1;
@@ -98,22 +98,23 @@ static int add_swap(const char *what, struct mntent *me) {
 
         name = unit_name_from_path(what, ".swap");
         if (!name) {
-                log_error("Out of memory");
-                r = -ENOMEM;
+                r = log_oom();
                 goto finish;
         }
 
-        unit = join(arg_dest, "/", name, NULL);
+        unit = strjoin(arg_dest, "/", name, NULL);
         if (!unit) {
-                log_error("Out of memory");
-                r = -ENOMEM;
+                r = log_oom();
                 goto finish;
         }
 
         f = fopen(unit, "wxe");
         if (!f) {
                 r = -errno;
-                log_error("Failed to create unit file: %m");
+                if (errno == EEXIST)
+                        log_error("Failed to create swap unit file %s, as it already exists. Duplicate entry in /etc/fstab?", unit);
+                else
+                        log_error("Failed to create unit file %s: %m", unit);
                 goto finish;
         }
 
@@ -140,45 +141,40 @@ static int add_swap(const char *what, struct mntent *me) {
 
         fflush(f);
         if (ferror(f)) {
-                log_error("Failed to write unit file: %m");
+                log_error("Failed to write unit file %s: %m", unit);
                 r = -errno;
                 goto finish;
         }
 
         if (!noauto) {
-                lnk = join(arg_dest, "/" SPECIAL_SWAP_TARGET ".wants/", name, NULL);
+                lnk = strjoin(arg_dest, "/" SPECIAL_SWAP_TARGET ".wants/", name, NULL);
                 if (!lnk) {
-                        log_error("Out of memory");
-                        r = -ENOMEM;
+                        r = log_oom();
                         goto finish;
                 }
 
                 mkdir_parents_label(lnk, 0755);
                 if (symlink(unit, lnk) < 0) {
-                        log_error("Failed to create symlink: %m");
+                        log_error("Failed to create symlink %s: %m", lnk);
                         r = -errno;
                         goto finish;
                 }
 
                 r = device_name(what, &device);
-                if (r < 0) {
-                        log_error("Out of memory");
-                        r = -ENOMEM;
+                if (r < 0)
                         goto finish;
-                }
 
                 if (r > 0) {
                         free(lnk);
-                        lnk = join(arg_dest, "/", device, ".wants/", name, NULL);
+                        lnk = strjoin(arg_dest, "/", device, ".wants/", name, NULL);
                         if (!lnk) {
-                                log_error("Out of memory");
-                                r = -ENOMEM;
+                                r = log_oom();
                                 goto finish;
                         }
 
                         mkdir_parents_label(lnk, 0755);
                         if (symlink(unit, lnk) < 0) {
-                                log_error("Failed to create symlink: %m");
+                                log_error("Failed to create symlink %s: %m", lnk);
                                 r = -errno;
                                 goto finish;
                         }
@@ -203,7 +199,7 @@ static bool mount_is_bind(struct mntent *me) {
 
         return
                 hasmntopt(me, "bind") ||
-                streq(me->mnt_opts, "bind");
+                streq(me->mnt_type, "bind");
 }
 
 static bool mount_is_network(struct mntent *me) {
@@ -255,23 +251,24 @@ static int add_mount(const char *what, const char *where, struct mntent *me) {
         }
 
         name = unit_name_from_path(where, ".mount");
-        if (!name)  {
-                log_error("Out of memory");
-                r = -ENOMEM;
+        if (!name) {
+                r = log_oom();
                 goto finish;
         }
 
-        unit = join(arg_dest, "/", name, NULL);
+        unit = strjoin(arg_dest, "/", name, NULL);
         if (!unit) {
-                log_error("Out of memory");
-                r = -ENOMEM;
+                r = log_oom();
                 goto finish;
         }
 
         f = fopen(unit, "wxe");
         if (!f) {
                 r = -errno;
-                log_error("Failed to create unit file: %m");
+                if (errno == EEXIST)
+                        log_error("Failed to create mount unit file %s, as it already exists. Duplicate entry in /etc/fstab?", unit);
+                else
+                        log_error("Failed to create unit file %s: %m", unit);
                 goto finish;
         }
 
@@ -315,22 +312,21 @@ static int add_mount(const char *what, const char *where, struct mntent *me) {
 
         fflush(f);
         if (ferror(f)) {
-                log_error("Failed to write unit file: %m");
+                log_error("Failed to write unit file %s: %m", unit);
                 r = -errno;
                 goto finish;
         }
 
         if (!noauto) {
-                lnk = join(arg_dest, "/", post, nofail || automount ? ".wants/" : ".requires/", name, NULL);
+                lnk = strjoin(arg_dest, "/", post, nofail || automount ? ".wants/" : ".requires/", name, NULL);
                 if (!lnk) {
-                        log_error("Out of memory");
-                        r = -ENOMEM;
+                        r = log_oom();
                         goto finish;
                 }
 
                 mkdir_parents_label(lnk, 0755);
                 if (symlink(unit, lnk) < 0) {
-                        log_error("Failed to create symlink: %m");
+                        log_error("Failed to create symlink %s: %m", lnk);
                         r = -errno;
                         goto finish;
                 }
@@ -339,24 +335,20 @@ static int add_mount(const char *what, const char *where, struct mntent *me) {
                     !path_equal(where, "/")) {
 
                         r = device_name(what, &device);
-                        if (r < 0) {
-                                log_error("Out of memory");
-                                r = -ENOMEM;
+                        if (r < 0)
                                 goto finish;
-                        }
 
                         if (r > 0) {
                                 free(lnk);
-                                lnk = join(arg_dest, "/", device, ".wants/", name, NULL);
+                                lnk = strjoin(arg_dest, "/", device, ".wants/", name, NULL);
                                 if (!lnk) {
-                                        log_error("Out of memory");
-                                        r = -ENOMEM;
+                                        r = log_oom();
                                         goto finish;
                                 }
 
                                 mkdir_parents_label(lnk, 0755);
                                 if (symlink(unit, lnk) < 0) {
-                                        log_error("Failed to creat symlink: %m");
+                                        log_error("Failed to create symlink %s: %m", lnk);
                                         r = -errno;
                                         goto finish;
                                 }
@@ -367,15 +359,13 @@ static int add_mount(const char *what, const char *where, struct mntent *me) {
         if (automount && !path_equal(where, "/")) {
                 automount_name = unit_name_from_path(where, ".automount");
                 if (!name) {
-                        log_error("Out of memory");
-                        r = -ENOMEM;
+                        r = log_oom();
                         goto finish;
                 }
 
-                automount_unit = join(arg_dest, "/", automount_name, NULL);
+                automount_unit = strjoin(arg_dest, "/", automount_name, NULL);
                 if (!automount_unit) {
-                        log_error("Out of memory");
-                        r = -ENOMEM;
+                        r = log_oom();
                         goto finish;
                 }
 
@@ -383,7 +373,7 @@ static int add_mount(const char *what, const char *where, struct mntent *me) {
                 f = fopen(automount_unit, "wxe");
                 if (!f) {
                         r = -errno;
-                        log_error("Failed to create unit file: %m");
+                        log_error("Failed to create unit file %s: %m", automount_unit);
                         goto finish;
                 }
 
@@ -402,22 +392,21 @@ static int add_mount(const char *what, const char *where, struct mntent *me) {
 
                 fflush(f);
                 if (ferror(f)) {
-                        log_error("Failed to write unit file: %m");
+                        log_error("Failed to write unit file %s: %m", automount_unit);
                         r = -errno;
                         goto finish;
                 }
 
                 free(lnk);
-                lnk = join(arg_dest, "/", post, nofail ? ".wants/" : ".requires/", automount_name, NULL);
+                lnk = strjoin(arg_dest, "/", post, nofail ? ".wants/" : ".requires/", automount_name, NULL);
                 if (!lnk) {
-                        log_error("Out of memory");
-                        r = -ENOMEM;
+                        r = log_oom();
                         goto finish;
                 }
 
                 mkdir_parents_label(lnk, 0755);
                 if (symlink(automount_unit, lnk) < 0) {
-                        log_error("Failed to create symlink: %m");
+                        log_error("Failed to create symlink %s: %m", lnk);
                         r = -errno;
                         goto finish;
                 }
@@ -459,16 +448,14 @@ static int parse_fstab(void) {
 
                 what = fstab_node_to_udev_node(me->mnt_fsname);
                 if (!what) {
-                        log_error("Out of memory");
-                        r = -ENOMEM;
+                        r = log_oom();
                         goto finish;
                 }
 
                 where = strdup(me->mnt_dir);
                 if (!where) {
-                        log_error("Out of memory");
+                        r = log_oom();
                         free(what);
-                        r = -ENOMEM;
                         goto finish;
                 }
 
@@ -513,7 +500,7 @@ static int parse_proc_cmdline(void) {
 
                 word = strndup(w, l);
                 if (!word) {
-                        r = -ENOMEM;
+                        r = log_oom();
                         goto finish;
                 }