]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
unit: unify some code with new unit_new_for_name() call
authorLennart Poettering <lennart@poettering.net>
Mon, 24 Oct 2016 22:29:05 +0000 (00:29 +0200)
committerLennart Poettering <lennart@poettering.net>
Wed, 2 Nov 2016 17:29:59 +0000 (11:29 -0600)
src/core/device.c
src/core/mount.c
src/core/scope.c
src/core/slice.c
src/core/swap.c
src/core/unit.c
src/core/unit.h

index 8a3e888e5eb9e3bbbf3f0bd3c393b69693c68a55..bd87a447cda8d9453e3e4db14f5760f45625b1b1 100644 (file)
@@ -331,11 +331,7 @@ static int device_setup_unit(Manager *m, struct udev_device *dev, const char *pa
         if (!u) {
                 delete = true;
 
-                u = unit_new(m, sizeof(Device));
-                if (!u)
-                        return log_oom();
-
-                r = unit_add_name(u, e);
+                r = unit_new_for_name(m, sizeof(Device), e, &u);
                 if (r < 0)
                         goto fail;
 
index 03e5ea1376adf93fb73efff221e57ece3aa47a65..0641621d8fea660c611572c44a427dcf8e1a7753 100644 (file)
@@ -1404,11 +1404,7 @@ static int mount_setup_unit(
         if (!u) {
                 delete = true;
 
-                u = unit_new(m, sizeof(Mount));
-                if (!u)
-                        return log_oom();
-
-                r = unit_add_name(u, e);
+                r = unit_new_for_name(m, sizeof(Mount), e, &u);
                 if (r < 0)
                         goto fail;
 
@@ -1614,16 +1610,9 @@ static void synthesize_root_mount(Manager *m) {
 
         u = manager_get_unit(m, SPECIAL_ROOT_MOUNT);
         if (!u) {
-                u = unit_new(m, sizeof(Mount));
-                if (!u) {
-                        log_oom();
-                        return;
-                }
-
-                r = unit_add_name(u, SPECIAL_ROOT_MOUNT);
+                r = unit_new_for_name(m, sizeof(Mount), SPECIAL_ROOT_MOUNT, &u);
                 if (r < 0) {
-                        unit_free(u);
-                        log_error_errno(r, "Failed to add the " SPECIAL_ROOT_MOUNT " name: %m");
+                        log_error_errno(r, "Failed to allocate the special " SPECIAL_ROOT_MOUNT " unit: %m");
                         return;
                 }
         }
index 77f3fdc2aa24a31822579d6d764d48a087c32851..d6e1f8e392cbcafbdb08ff863c9c97a22c94bcdb 100644 (file)
@@ -563,16 +563,9 @@ static void scope_enumerate(Manager *m) {
 
         u = manager_get_unit(m, SPECIAL_INIT_SCOPE);
         if (!u) {
-                u = unit_new(m, sizeof(Scope));
-                if (!u) {
-                        log_oom();
-                        return;
-                }
-
-                r = unit_add_name(u, SPECIAL_INIT_SCOPE);
+                r = unit_new_for_name(m, sizeof(Scope), SPECIAL_INIT_SCOPE, &u);
                 if (r < 0)  {
-                        unit_free(u);
-                        log_error_errno(r, "Failed to add the " SPECIAL_INIT_SCOPE " name: %m");
+                        log_error_errno(r, "Failed to allocate the special " SPECIAL_INIT_SCOPE " unit: %m");
                         return;
                 }
         }
index c505fa1916e64b1539b6d2b9a19364c1de7ddd26..ed5d3fd701a024ad0026dcf057cd81299abc0f77 100644 (file)
@@ -299,16 +299,9 @@ static void slice_enumerate(Manager *m) {
 
         u = manager_get_unit(m, SPECIAL_ROOT_SLICE);
         if (!u) {
-                u = unit_new(m, sizeof(Slice));
-                if (!u) {
-                        log_oom();
-                        return;
-                }
-
-                r = unit_add_name(u, SPECIAL_ROOT_SLICE);
+                r = unit_new_for_name(m, sizeof(Slice), SPECIAL_ROOT_SLICE, &u);
                 if (r < 0) {
-                        unit_free(u);
-                        log_error_errno(r, "Failed to add the " SPECIAL_ROOT_SLICE " name: %m");
+                        log_error_errno(r, "Failed to allocate the special " SPECIAL_ROOT_SLICE " unit: %m");
                         return;
                 }
         }
index b592abb9fbc0244a2e0bd814eb42804813ab1d4b..2228a254bbc1a06238ac3606c8a66b7d20eac884 100644 (file)
@@ -381,11 +381,7 @@ static int swap_setup_unit(
         if (!u) {
                 delete = true;
 
-                u = unit_new(m, sizeof(Swap));
-                if (!u)
-                        return log_oom();
-
-                r = unit_add_name(u, e);
+                r = unit_new_for_name(m, sizeof(Swap), e, &u);
                 if (r < 0)
                         goto fail;
 
index 8d74b44f737c74643f8af2398494b6e48936c016..068d319206c8735b3d192c0cec8f51716b74e8de 100644 (file)
@@ -109,6 +109,24 @@ Unit *unit_new(Manager *m, size_t size) {
         return u;
 }
 
+int unit_new_for_name(Manager *m, size_t size, const char *name, Unit **ret) {
+        Unit *u;
+        int r;
+
+        u = unit_new(m, size);
+        if (!u)
+                return -ENOMEM;
+
+        r = unit_add_name(u, name);
+        if (r < 0) {
+                unit_free(u);
+                return r;
+        }
+
+        *ret = u;
+        return r;
+}
+
 bool unit_has_name(Unit *u, const char *name) {
         assert(u);
         assert(name);
index e524553ed32ecc94855d0441e6d71a20d57f628f..899cd62c9202e03f2d7b0c84da25d6ed1802b6d0 100644 (file)
@@ -481,6 +481,7 @@ DEFINE_CAST(SCOPE, Scope);
 Unit *unit_new(Manager *m, size_t size);
 void unit_free(Unit *u);
 
+int unit_new_for_name(Manager *m, size_t size, const char *name, Unit **ret);
 int unit_add_name(Unit *u, const char *name);
 
 int unit_add_dependency(Unit *u, UnitDependency d, Unit *other, bool add_reference);