]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
tree-wide: flatten nested error checks
authorDavid Tardon <dtardon@redhat.com>
Tue, 9 Dec 2025 09:04:36 +0000 (10:04 +0100)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Wed, 10 Dec 2025 09:59:44 +0000 (18:59 +0900)
13 files changed:
src/basic/socket-util.c
src/basic/virt.c
src/core/import-creds.c
src/core/load-fragment.c
src/core/manager-serialize.c
src/escape/escape-tool.c
src/home/homework-fscrypt.c
src/home/homework-luks.c
src/libsystemd/sd-device/sd-device.c
src/login/logind-core.c
src/login/logind-dbus.c
src/repart/repart.c
src/udev/udev-builtin-net_setup_link.c

index cd0bc4fd13f1f14bd377f8e5ea40813448bc1dc9..27635ad899cc553c74f56e65495f0148308fa671 100644 (file)
@@ -1263,12 +1263,10 @@ int flush_accept(int fd) {
                 int cfd;
 
                 r = fd_wait_for_event(fd, POLLIN, 0);
-                if (r < 0) {
-                        if (r == -EINTR)
-                                continue;
-
+                if (r == -EINTR)
+                        continue;
+                if (r < 0)
                         return r;
-                }
                 if (r == 0)
                         return 0;
 
@@ -1305,12 +1303,10 @@ ssize_t flush_mqueue(int fd) {
                 ssize_t l;
 
                 r = fd_wait_for_event(fd, POLLIN, /* timeout= */ 0);
-                if (r < 0) {
-                        if (r == -EINTR)
-                                continue;
-
+                if (r == -EINTR)
+                        continue;
+                if (r < 0)
                         return r;
-                }
                 if (r == 0)
                         return count;
 
index 2493ef8ac7f7b40fb2de869d26845b165cabd0b6..707af529e0de96afb36e951d8618a80fdbaae632 100644 (file)
@@ -199,12 +199,10 @@ static Virtualization detect_vm_dmi_vendor(void) {
                 _cleanup_free_ char *s = NULL;
 
                 r = read_one_line_file(*vendor, &s);
-                if (r < 0) {
-                        if (r == -ENOENT)
-                                continue;
-
+                if (r == -ENOENT)
+                        continue;
+                if (r < 0)
                         return r;
-                }
 
                 FOREACH_ELEMENT(dmi_vendor, dmi_vendor_table)
                         if (startswith(s, dmi_vendor->vendor)) {
index e0df078448f7f62c25281f97b50d25bd2f382bd4..6d81d2ca63e7f11d4c67ffdc59b43f8ed1504d9d 100644 (file)
@@ -810,12 +810,10 @@ static int report_credentials_per_func(const char *title, int (*get_directory_fu
         assert(get_directory_func);
 
         r = get_directory_func(&d);
-        if (r < 0) {
-                if (r == -ENXIO) /* Env var not set */
-                        return 0;
-
+        if (r == -ENXIO) /* Env var not set */
+                return 0;
+        if (r < 0)
                 return log_warning_errno(r, "Failed to determine %s directory: %m", title);
-        }
 
         r = readdir_all_at(AT_FDCWD, d, RECURSE_DIR_SORT|RECURSE_DIR_IGNORE_DOT, &de);
         if (r < 0)
index ca1ce53066d865b68128ae9e1a339376745a0dc3..6e5fb6afc427e7d92a4bb18a044bbed144df2a09 100644 (file)
@@ -5573,16 +5573,13 @@ int config_parse_emergency_action(
                 runtime_scope = ltype; /* otherwise, assume the scope is passed in via ltype */
 
         r = parse_emergency_action(rvalue, runtime_scope, x);
-        if (r < 0) {
-                if (r == -EOPNOTSUPP)
-                        log_syntax(unit, LOG_WARNING, filename, line, r,
-                                   "%s= specified as %s mode action, ignoring: %s",
-                                   lvalue, runtime_scope_to_string(runtime_scope), rvalue);
-                else
-                        log_syntax(unit, LOG_WARNING, filename, line, r,
-                                   "Failed to parse %s=, ignoring: %s", lvalue, rvalue);
-                return 0;
-        }
+        if (r == -EOPNOTSUPP)
+                log_syntax(unit, LOG_WARNING, filename, line, r,
+                                "%s= specified as %s mode action, ignoring: %s",
+                                lvalue, runtime_scope_to_string(runtime_scope), rvalue);
+        else if (r < 0)
+                log_syntax(unit, LOG_WARNING, filename, line, r,
+                                "Failed to parse %s=, ignoring: %s", lvalue, rvalue);
 
         return 0;
 }
index df97fde97ed3511f2af7a82208f83582c8ad8eae..2ce8e894ebb134b3ada95331b86b4b72ee5a43b6 100644 (file)
@@ -198,18 +198,16 @@ static int manager_deserialize_one_unit(Manager *m, const char *name, FILE *f, F
         int r;
 
         r = manager_load_unit(m, name, NULL, NULL, &u);
-        if (r < 0) {
-                if (r == -ENOMEM)
-                        return r;
+        if (r == -ENOMEM)
+                return r;
+        if (r < 0)
                 return log_notice_errno(r, "Failed to load unit \"%s\", skipping deserialization: %m", name);
-        }
 
         r = unit_deserialize_state(u, f, fds);
-        if (r < 0) {
-                if (r == -ENOMEM)
-                        return r;
+        if (r == -ENOMEM)
+                return r;
+        if (r < 0)
                 return log_notice_errno(r, "Failed to deserialize unit \"%s\", skipping: %m", name);
-        }
 
         return 0;
 }
index 0742bf3f05de087a350a53a74104c7e0b94f4084..621182897d8f8057154d30307d616de5ed2018f7 100644 (file)
@@ -174,23 +174,21 @@ static int run(int argc, char *argv[]) {
                 case ACTION_ESCAPE:
                         if (arg_path) {
                                 r = unit_name_path_escape(*i, &e);
-                                if (r < 0) {
-                                        if (r == -EINVAL) {
-                                                /* If escaping failed because the string was invalid, let's print a
-                                                 * friendly message about it. Catch these specific error cases
-                                                 * explicitly. */
-
-                                                if (!path_is_valid(*i))
-                                                        return log_error_errno(r, "Input '%s' is not a valid file system path, failed to escape.", *i);
-                                                if (!path_is_absolute(*i))
-                                                        return log_error_errno(r, "Input '%s' is not an absolute file system path, failed to escape.", *i);
-                                                if (!path_is_normalized(*i))
-                                                        return log_error_errno(r, "Input '%s' is not a normalized file system path, failed to escape.", *i);
-                                        }
-
+                                if (r == -EINVAL) {
+                                        /* If escaping failed because the string was invalid, let's print a
+                                         * friendly message about it. Catch these specific error cases
+                                         * explicitly. */
+
+                                        if (!path_is_valid(*i))
+                                                return log_error_errno(r, "Input '%s' is not a valid file system path, failed to escape.", *i);
+                                        if (!path_is_absolute(*i))
+                                                return log_error_errno(r, "Input '%s' is not an absolute file system path, failed to escape.", *i);
+                                        if (!path_is_normalized(*i))
+                                                return log_error_errno(r, "Input '%s' is not a normalized file system path, failed to escape.", *i);
+                                }
+                                if (r < 0)
                                         /* All other error cases. */
                                         return log_error_errno(r, "Failed to escape string: %m");
-                                }
 
                                 /* If the escaping worked, then still warn if the path is not like we'd like
                                  * it. Because that means escaping is not necessarily reversible. */
index be61d2b36f8b4c205449df9d3b23e6ca730224a4..ac45c191d73498c27ff3769a065e6e3661fe21b3 100644 (file)
@@ -71,12 +71,10 @@ static int fscrypt_unlink_key(UserRecord *h) {
                 char *d;
 
                 r = keyring_describe(*key, &description);
-                if (r < 0) {
-                        if (r == -ENOKEY) /* Something else deleted it already, that's ok. */
-                                continue;
-
+                if (r == -ENOKEY) /* Something else deleted it already, that's ok. */
+                        continue;
+                if (r < 0)
                         return log_error_errno(r, "Failed to describe key id %d: %m", *key);
-                }
 
                 /* The description is the final element as per manpage. */
                 d = strrchr(description, ';');
index 489343cddcd3705f02a43f7a054cd5e6464e4006..0b637c1fd9891ddef218c0afaf95bd242c2563ad 100644 (file)
@@ -2353,16 +2353,12 @@ int home_create_luks(
                         0,
                         LOCK_EX,
                         &setup->loop);
-        if (r < 0) {
-                if (r == -ENOENT) { /* this means /dev/loop-control doesn't exist, i.e. we are in a container
-                                     * or similar and loopback bock devices are not available, return a
-                                     * recognizable error in this case. */
-                        log_error_errno(r, "Loopback block device support is not available on this system.");
-                        return -ENOLINK; /* Make recognizable */
-                }
-
+        if (r == -ENOENT) /* this means /dev/loop-control doesn't exist, i.e. we are in a container
+                           * or similar and loopback bock devices are not available, return a
+                           * recognizable error in this case. */
+                return log_error_errno(SYNTHETIC_ERRNO(ENOLINK), "Loopback block device support is not available on this system.");
+        if (r < 0)
                 return log_error_errno(r, "Failed to set up loopback device for %s: %m", setup->temporary_image_path);
-        }
 
         log_info("Setting up loopback device %s completed.", setup->loop->node ?: ip);
 
index 94d4a8647ec93975c58f0394082f104740f05ac3..dbee88c4520f73715c4da7d053e13583099d46b1 100644 (file)
@@ -1802,12 +1802,10 @@ int device_read_db_internal_filename(sd_device *device, const char *filename) {
         assert(filename);
 
         r = read_full_file(filename, &db, &db_len);
-        if (r < 0) {
-                if (r == -ENOENT)
-                        return 0;
-
+        if (r == -ENOENT)
+                return 0;
+        if (r < 0)
                 return log_device_debug_errno(device, r, "sd-device: Failed to read db '%s': %m", filename);
-        }
 
         /* devices with a database entry are initialized */
         device->is_initialized = true;
index 4962aa03087427d0420237b6f186ba16cee34542..33f895f0041e9a89531b6fa65da21ecf429c094f 100644 (file)
@@ -772,14 +772,13 @@ int manager_read_efi_boot_loader_entries(Manager *m) {
                 return 0;
 
         r = efi_loader_get_entries(&m->efi_boot_loader_entries);
-        if (r < 0) {
-                if (r == -ENOENT || ERRNO_IS_NOT_SUPPORTED(r)) {
-                        log_debug_errno(r, "Boot loader reported no entries.");
-                        m->efi_boot_loader_entries_set = true;
-                        return 0;
-                }
-                return log_error_errno(r, "Failed to determine entries reported by boot loader: %m");
+        if (r == -ENOENT || ERRNO_IS_NEG_NOT_SUPPORTED(r)) {
+                log_debug_errno(r, "Boot loader reported no entries.");
+                m->efi_boot_loader_entries_set = true;
+                return 0;
         }
+        if (r < 0)
+                return log_error_errno(r, "Failed to determine entries reported by boot loader: %m");
 
         m->efi_boot_loader_entries_set = true;
         return 1;
index 6adcc789b7814a5d427d7206e4bc1d7c06fd2caf..1e729761acb5e740c401f81d7726369aedf73de6 100644 (file)
@@ -2820,15 +2820,13 @@ static int method_schedule_shutdown(sd_bus_message *message, void *userdata, sd_
         if (elapse == USEC_INFINITY) {
                 if (m->maintenance_time) {
                         r = calendar_spec_next_usec(m->maintenance_time, now(CLOCK_REALTIME), &elapse);
-                        if (r < 0) {
-                                if (r == -ENOENT)
-                                        return sd_bus_error_set(error,
-                                                                BUS_ERROR_DESIGNATED_MAINTENANCE_TIME_NOT_SCHEDULED,
-                                                                "No upcoming maintenance window scheduled");
-
+                        if (r == -ENOENT)
+                                return sd_bus_error_set(error,
+                                                        BUS_ERROR_DESIGNATED_MAINTENANCE_TIME_NOT_SCHEDULED,
+                                                        "No upcoming maintenance window scheduled");
+                        if (r < 0)
                                 return sd_bus_error_set_errnof(error, r,
                                                                "Failed to determine next maintenance window: %m");
-                        }
 
                         log_info("Scheduled %s at maintenance window %s", type, FORMAT_TIMESTAMP(elapse));
                 } else
index 497654d012d03ed5470eee5cfd71d51db6062773..ad5f14299fb635334bdd6a54a58e3b0547be1c87 100644 (file)
@@ -8297,13 +8297,12 @@ static int context_fstab(Context *context) {
         switch (arg_append_fstab) {
         case APPEND_AUTO: {
                 r = read_full_file(path, &c, NULL);
-                if (r < 0) {
-                        if (r == -ENOENT) {
-                                log_debug("File fstab not found in %s", path);
-                                break;
-                        }
-                        return log_error_errno(r, "Failed to open %s: %m", path);
+                if (r == -ENOENT) {
+                        log_debug("File fstab not found in %s", path);
+                        break;
                 }
+                if (r < 0)
+                        return log_error_errno(r, "Failed to open %s: %m", path);
 
                 const char *acs, *ace;
                 acs = find_line(c, AUTOMATIC_FSTAB_HEADER_START);
@@ -9713,11 +9712,10 @@ static int parse_efi_variable_factory_reset(void) {
                 return 0;
 
         r = efi_get_variable_string(EFI_SYSTEMD_VARIABLE_STR("FactoryReset"), &value);
-        if (r < 0) {
-                if (r == -ENOENT || ERRNO_IS_NOT_SUPPORTED(r))
-                        return 0;
+        if (r == -ENOENT || ERRNO_IS_NEG_NOT_SUPPORTED(r))
+                return 0;
+        if (r < 0)
                 return log_error_errno(r, "Failed to read EFI variable FactoryReset: %m");
-        }
 
         log_warning("Warning, EFI variable FactoryReset is in use, please migrate to use FactoryResetRequest instead, support will be removed in v260!");
 
@@ -9738,11 +9736,10 @@ static int remove_efi_variable_factory_reset(void) {
         // FIXME: Remove this in v260, see above
 
         r = efi_set_variable(EFI_SYSTEMD_VARIABLE_STR("FactoryReset"), NULL, 0);
-        if (r < 0) {
-                if (r == -ENOENT || ERRNO_IS_NOT_SUPPORTED(r))
-                        return 0;
+        if (r == -ENOENT || ERRNO_IS_NEG_NOT_SUPPORTED(r))
+                return 0;
+        if (r < 0)
                 return log_error_errno(r, "Failed to remove EFI variable FactoryReset: %m");
-        }
 
         log_info("Successfully unset EFI variable FactoryReset.");
         return 0;
index 1277d0fb45c420c1246dc216d643b9344f5369bc..83f647da023b5c728b22314f7d9867ba7e55429b 100644 (file)
@@ -51,14 +51,12 @@ static int builtin_net_setup_link(UdevEvent *event, int argc, char **argv) {
                 return log_device_warning_errno(dev, r, "Failed to get link information: %m");
 
         r = link_get_config(ctx, link);
-        if (r < 0) {
-                if (r == -ENOENT) {
-                        log_device_debug_errno(dev, r, "No matching link configuration found, ignoring device.");
-                        return 0;
-                }
-
-                return log_device_error_errno(dev, r, "Failed to get link config: %m");
+        if (r == -ENOENT) {
+                log_device_debug_errno(dev, r, "No matching link configuration found, ignoring device.");
+                return 0;
         }
+        if (r < 0)
+                return log_device_error_errno(dev, r, "Failed to get link config: %m");
 
         r = link_apply_config(ctx, link);
         if (r == -ENODEV)