diff --git a/coccinelle/errno-check.cocci b/coccinelle/errno-check.cocci new file mode 100644 index 0000000000..709cb4ace6 --- /dev/null +++ b/coccinelle/errno-check.cocci @@ -0,0 +1,10 @@ +@@ +constant c; +@@ +( +- errno == -c ++ errno == c +| +- errno != -c ++ errno != c +) diff --git a/coccinelle/macros.h b/coccinelle/macros.h new file mode 100644 index 0000000000..62177f0d86 --- /dev/null +++ b/coccinelle/macros.h @@ -0,0 +1,231 @@ +/* Collected macros from our systemd codebase to make the cocci semantic + * parser happy. Inspired by the original cocci macros file + * /usr/lib64/coccinelle/standard.h (including the YACFE_* symbols) + */ + +// General +#define PTR_TO_PID(x) + +// src/basic/macro.h +#define _printf_(a, b) __attribute__((__format__(printf, a, b))) +#define _alloc_(...) __attribute__((__alloc_size__(__VA_ARGS__))) +#define _sentinel_ __attribute__((__sentinel__)) +#define _section_(x) __attribute__((__section__(x))) +#define _used_ __attribute__((__used__)) +#define _unused_ __attribute__((__unused__)) +#define _destructor_ __attribute__((__destructor__)) +#define _pure_ __attribute__((__pure__)) +#define _const_ __attribute__((__const__)) +#define _deprecated_ __attribute__((__deprecated__)) +#define _packed_ __attribute__((__packed__)) +#define _malloc_ __attribute__((__malloc__)) +#define _weak_ __attribute__((__weak__)) +#define _likely_(x) (__builtin_expect(!!(x), 1)) +#define _unlikely_(x) (__builtin_expect(!!(x), 0)) +#define _public_ __attribute__((__visibility__("default"))) +#define _hidden_ __attribute__((__visibility__("hidden"))) +#define _weakref_(x) __attribute__((__weakref__(#x))) +#define _align_(x) __attribute__((__aligned__(x))) +#define _alignas_(x) __attribute__((__aligned__(__alignof(x)))) +#define _alignptr_ __attribute__((__aligned__(sizeof(void*)))) +#define _cleanup_(x) __attribute__((__cleanup__(x))) +#define _fallthrough_ +#define _noreturn_ __attribute__((__noreturn__)) +#define thread_local __thread + +#define ELEMENTSOF(x) \ + (__builtin_choose_expr( \ + !__builtin_types_compatible_p(typeof(x), typeof(&*(x))), \ + sizeof(x)/sizeof((x)[0]), \ + VOID_0)) + +// src/basic/umask-util.h +#define _cleanup_umask_ +#define RUN_WITH_UMASK(mask) \ + for (_cleanup_umask_ mode_t _saved_umask_ = umask(mask) | S_IFMT; \ + FLAGS_SET(_saved_umask_, S_IFMT); \ + _saved_umask_ &= 0777) + +// src/basic/hashmap.h +#define _IDX_ITERATOR_FIRST (UINT_MAX - 1) +#define HASHMAP_FOREACH(e, h) YACFE_ITERATOR +#define ORDERED_HASHMAP_FOREACH(e, h) YACFE_ITERATOR +#define HASHMAP_FOREACH_KEY(e, k, h) YACFE_ITERATOR +#define ORDERED_HASHMAP_FOREACH_KEY(e, k, h) YACFE_ITERATOR + +// src/basic/list.h +#define LIST_HEAD(t,name) \ + t *name +#define LIST_FIELDS(t,name) \ + t *name##_next, *name##_prev +#define LIST_HEAD_INIT(head) \ + do { \ + (head) = NULL; \ + } while (false) +#define LIST_INIT(name,item) \ + do { \ + typeof(*(item)) *_item = (item); \ + assert(_item); \ + _item->name##_prev = _item->name##_next = NULL; \ + } while (false) +#define LIST_PREPEND(name,head,item) \ + do { \ + typeof(*(head)) **_head = &(head), *_item = (item); \ + assert(_item); \ + if ((_item->name##_next = *_head)) \ + _item->name##_next->name##_prev = _item; \ + _item->name##_prev = NULL; \ + *_head = _item; \ + } while (false) +#define LIST_APPEND(name,head,item) \ + do { \ + typeof(*(head)) **_hhead = &(head), *_tail; \ + LIST_FIND_TAIL(name, *_hhead, _tail); \ + LIST_INSERT_AFTER(name, *_hhead, _tail, item); \ + } while (false) +#define LIST_REMOVE(name,head,item) \ + do { \ + typeof(*(head)) **_head = &(head), *_item = (item); \ + assert(_item); \ + if (_item->name##_next) \ + _item->name##_next->name##_prev = _item->name##_prev; \ + if (_item->name##_prev) \ + _item->name##_prev->name##_next = _item->name##_next; \ + else { \ + assert(*_head == _item); \ + *_head = _item->name##_next; \ + } \ + _item->name##_next = _item->name##_prev = NULL; \ + } while (false) +#define LIST_FIND_HEAD(name,item,head) \ + do { \ + typeof(*(item)) *_item = (item); \ + if (!_item) \ + (head) = NULL; \ + else { \ + while (_item->name##_prev) \ + _item = _item->name##_prev; \ + (head) = _item; \ + } \ + } while (false) +#define LIST_FIND_TAIL(name,item,tail) \ + do { \ + typeof(*(item)) *_item = (item); \ + if (!_item) \ + (tail) = NULL; \ + else { \ + while (_item->name##_next) \ + _item = _item->name##_next; \ + (tail) = _item; \ + } \ + } while (false) +#define LIST_INSERT_AFTER(name,head,a,b) \ + do { \ + typeof(*(head)) **_head = &(head), *_a = (a), *_b = (b); \ + assert(_b); \ + if (!_a) { \ + if ((_b->name##_next = *_head)) \ + _b->name##_next->name##_prev = _b; \ + _b->name##_prev = NULL; \ + *_head = _b; \ + } else { \ + if ((_b->name##_next = _a->name##_next)) \ + _b->name##_next->name##_prev = _b; \ + _b->name##_prev = _a; \ + _a->name##_next = _b; \ + } \ + } while (false) +#define LIST_INSERT_BEFORE(name,head,a,b) \ + do { \ + typeof(*(head)) **_head = &(head), *_a = (a), *_b = (b); \ + assert(_b); \ + if (!_a) { \ + if (!*_head) { \ + _b->name##_next = NULL; \ + _b->name##_prev = NULL; \ + *_head = _b; \ + } else { \ + typeof(*(head)) *_tail = (head); \ + while (_tail->name##_next) \ + _tail = _tail->name##_next; \ + _b->name##_next = NULL; \ + _b->name##_prev = _tail; \ + _tail->name##_next = _b; \ + } \ + } else { \ + if ((_b->name##_prev = _a->name##_prev)) \ + _b->name##_prev->name##_next = _b; \ + else \ + *_head = _b; \ + _b->name##_next = _a; \ + _a->name##_prev = _b; \ + } \ + } while (false) + +#define LIST_JUST_US(name,item) \ + (!(item)->name##_prev && !(item)->name##_next) \ +#define LIST_FOREACH(name,i,head) \ + for ((i) = (head); (i); (i) = (i)->name##_next) +#define LIST_FOREACH_SAFE(name,i,n,head) \ + for ((i) = (head); (i) && (((n) = (i)->name##_next), 1); (i) = (n)) +#define LIST_FOREACH_BEFORE(name,i,p) \ + for ((i) = (p)->name##_prev; (i); (i) = (i)->name##_prev) +#define LIST_FOREACH_AFTER(name,i,p) \ + for ((i) = (p)->name##_next; (i); (i) = (i)->name##_next) +#define LIST_FOREACH_OTHERS(name,i,p) \ + for (({ \ + (i) = (p); \ + while ((i) && (i)->name##_prev) \ + (i) = (i)->name##_prev; \ + if ((i) == (p)) \ + (i) = (p)->name##_next; \ + }); \ + (i); \ + (i) = (i)->name##_next == (p) ? (p)->name##_next : (i)->name##_next) +#define LIST_LOOP_BUT_ONE(name,i,head,p) \ + for ((i) = (p)->name##_next ? (p)->name##_next : (head); \ + (i) != (p); \ + (i) = (i)->name##_next ? (i)->name##_next : (head)) + +#define LIST_IS_EMPTY(head) \ + (!(head)) +#define LIST_JOIN(name,a,b) \ + do { \ + assert(b); \ + if (!(a)) \ + (a) = (b); \ + else { \ + typeof(*(a)) *_head = (b), *_tail; \ + LIST_FIND_TAIL(name, (a), _tail); \ + _tail->name##_next = _head; \ + _head->name##_prev = _tail; \ + } \ + (b) = NULL; \ + } while (false) + +// src/basic/strv.h +#define STRV_FOREACH(s, l) YACFE_ITERATOR +#define STRV_FOREACH_BACKWARDS(s, l) YACFE_ITERATOR +#define STRV_FOREACH_PAIR(x, y, l) YACFE_ITERATOR + +// src/basic/socket-util.h +#define CMSG_BUFFER_TYPE(size) \ + union { \ + struct cmsghdr cmsghdr; \ + uint8_t buf[size]; \ + uint8_t align_check[(size) >= CMSG_SPACE(0) && \ + (size) == CMSG_ALIGN(size) ? 1 : -1]; \ + } + +// src/libsystemd/sd-device/device-util.h +#define FOREACH_DEVICE_PROPERTY(device, key, value) YACFE_ITERATOR +#define FOREACH_DEVICE_TAG(device, tag) YACFE_ITERATOR +#define FOREACH_DEVICE_CURRENT_TAG(device, tag) YACFE_ITERATOR +#define FOREACH_DEVICE_SYSATTR(device, attr) YACFE_ITERATOR +#define FOREACH_DEVICE_DEVLINK(device, devlink) YACFE_ITERATOR +#define FOREACH_DEVICE(enumerator, device) YACFE_ITERATOR +#define FOREACH_SUBSYSTEM(enumerator, device) YACFE_ITERATOR + +// src/basic/dirent-util.h +#define FOREACH_DIRENT(de, d, on_error) YACFE_ITERATOR +#define FOREACH_DIRENT_ALL(de, d, on_error) YACFE_ITERATOR diff --git a/coccinelle/run-coccinelle.sh b/coccinelle/run-coccinelle.sh index 1fdef5f63e..871547a881 100755 --- a/coccinelle/run-coccinelle.sh +++ b/coccinelle/run-coccinelle.sh @@ -31,12 +31,12 @@ if ! parallel -h >/dev/null; then exit 1 fi -for SCRIPT in ${@-$top/coccinelle/*.cocci} ; do +for SCRIPT in ${@-$top/coccinelle/*.cocci}; do echo "--x-- Processing $SCRIPT --x--" TMPFILE=`mktemp` echo "+ spatch --sp-file $SCRIPT $args ..." parallel --halt now,fail=1 --keep-order --noswap --max-args=20 \ - spatch --sp-file $SCRIPT $args ::: "${files[@]}" \ + spatch --macro-file="$top/coccinelle/macros.h" --sp-file $SCRIPT $args ::: "${files[@]}" \ 2>"$TMPFILE" || cat "$TMPFILE" echo -e "--x-- Processed $SCRIPT --x--\n" done diff --git a/coccinelle/zz-drop-braces.cocci b/coccinelle/zz-drop-braces.cocci new file mode 100644 index 0000000000..34bf12fbea --- /dev/null +++ b/coccinelle/zz-drop-braces.cocci @@ -0,0 +1,27 @@ +@@ +position p : script:python() { p[0].file != "src/journal/lookup3.c" }; +identifier id; +expression e; +@@ +if (...) +- { +( + id@p(...); +| + e@p; +) +- } + +@@ +position p : script:python() { p[0].file != "src/journal/lookup3.c" }; +identifier id; +expression e; +@@ +if (...) +- { +( + return id@p(...); +| + return e@p; +) +- } diff --git a/src/analyze/analyze-security.c b/src/analyze/analyze-security.c index 16ab4f893e..9c1d63030d 100644 --- a/src/analyze/analyze-security.c +++ b/src/analyze/analyze-security.c @@ -2185,10 +2185,10 @@ int analyze_security(sd_bus *bus, char **units, AnalyzeSecurityFlags flags) { if (r < 0) return log_error_errno(r, "Failed to mangle unit name '%s': %m", *i); - if (!endswith(mangled, ".service")) { - log_error("Unit %s is not a service unit, refusing.", *i); - return -EINVAL; - } + if (!endswith(mangled, ".service")) + return log_error_errno(SYNTHETIC_ERRNO(EINVAL), + "Unit %s is not a service unit, refusing.", + *i); if (unit_name_is_valid(mangled, UNIT_NAME_TEMPLATE)) { r = unit_name_replace_instance(mangled, "test-instance", &instance); diff --git a/src/analyze/analyze.c b/src/analyze/analyze.c index a9e4b37573..591ba6d33c 100644 --- a/src/analyze/analyze.c +++ b/src/analyze/analyze.c @@ -1248,8 +1248,7 @@ static int expand_patterns(sd_bus *bus, char **patterns, char ***ret) { } } - *ret = expanded_patterns; - expanded_patterns = NULL; /* do not free */ + *ret = TAKE_PTR(expanded_patterns); /* do not free */ return 0; } diff --git a/src/basic/btrfs-util.c b/src/basic/btrfs-util.c index 43a58918e9..50a35268ea 100644 --- a/src/basic/btrfs-util.c +++ b/src/basic/btrfs-util.c @@ -1393,13 +1393,12 @@ static int copy_quota_hierarchy(int fd, uint64_t old_subvol_id, uint64_t new_sub } for (j = 0; j < n_old_parent_qgroups; j++) - if (old_parent_qgroups[j] == old_qgroups[i]) { + if (old_parent_qgroups[j] == old_qgroups[i]) /* The old subvolume shared a common * parent qgroup with its parent * subvolume. Let's set up something * similar in the destination. */ copy_from_parent = true; - } } if (!insert_intermediary_qgroup && !copy_from_parent) diff --git a/src/basic/khash.c b/src/basic/khash.c index e893298d68..dbae418282 100644 --- a/src/basic/khash.c +++ b/src/basic/khash.c @@ -160,8 +160,7 @@ int khash_new_with_key(khash **ret, const char *algorithm, const void *key, size /* Temporary fix for rc kernel bug: https://bugzilla.redhat.com/show_bug.cgi?id=1395896 */ (void) send(h->fd, NULL, 0, 0); - *ret = h; - h = NULL; + *ret = TAKE_PTR(h); return 0; } diff --git a/src/basic/socket-label.c b/src/basic/socket-label.c index dd69eaaac2..718a6953e5 100644 --- a/src/basic/socket-label.c +++ b/src/basic/socket-label.c @@ -129,8 +129,5 @@ int socket_address_listen( if (p) (void) touch(p); - r = fd; - fd = -1; - - return r; + return TAKE_FD(fd); } diff --git a/src/busctl/busctl.c b/src/busctl/busctl.c index 16dca649cf..4d98941d74 100644 --- a/src/busctl/busctl.c +++ b/src/busctl/busctl.c @@ -1130,14 +1130,13 @@ static int introspect(int argc, char **argv, void *userdata) { (void) pager_open(arg_pager_flags); - if (arg_legend) { + if (arg_legend) printf("%-*s %-*s %-*s %-*s %s\n", (int) name_width, "NAME", (int) type_width, "TYPE", (int) signature_width, "SIGNATURE", (int) result_width, "RESULT/VALUE", "FLAGS"); - } for (j = 0; j < k; j++) { _cleanup_free_ char *ellipsized = NULL; diff --git a/src/core/automount.c b/src/core/automount.c index b0d43a3ef4..9c0dca1b3a 100644 --- a/src/core/automount.c +++ b/src/core/automount.c @@ -540,10 +540,8 @@ static void automount_trigger_notify(Unit *u, Unit *other) { MOUNT_MOUNTED, MOUNT_REMOUNTING, MOUNT_REMOUNTING_SIGTERM, MOUNT_REMOUNTING_SIGKILL, MOUNT_UNMOUNTING_SIGTERM, MOUNT_UNMOUNTING_SIGKILL, - MOUNT_FAILED)) { - + MOUNT_FAILED)) (void) automount_send_ready(a, a->expire_tokens, -ENODEV); - } if (MOUNT(other)->state == MOUNT_DEAD) (void) automount_send_ready(a, a->expire_tokens, 0); diff --git a/src/core/dbus-cgroup.c b/src/core/dbus-cgroup.c index b7d2e32639..9fdd3d83ca 100644 --- a/src/core/dbus-cgroup.c +++ b/src/core/dbus-cgroup.c @@ -1500,9 +1500,9 @@ int bus_cgroup_set_property( LIST_PREPEND(device_allow, c->device_allow, a); } - a->r = !!strchr(rwm, 'r'); - a->w = !!strchr(rwm, 'w'); - a->m = !!strchr(rwm, 'm'); + a->r = strchr(rwm, 'r'); + a->w = strchr(rwm, 'w'); + a->m = strchr(rwm, 'm'); } n++; diff --git a/src/core/execute.c b/src/core/execute.c index 5264b8f091..d21de8c41e 100644 --- a/src/core/execute.c +++ b/src/core/execute.c @@ -5772,11 +5772,10 @@ void exec_status_start(ExecStatus *s, pid_t pid) { void exec_status_exit(ExecStatus *s, const ExecContext *context, pid_t pid, int code, int status) { assert(s); - if (s->pid != pid) { + if (s->pid != pid) *s = (ExecStatus) { .pid = pid, }; - } dual_timestamp_get(&s->exit_timestamp); diff --git a/src/core/main.c b/src/core/main.c index f957217eaa..6cd596fa79 100644 --- a/src/core/main.c +++ b/src/core/main.c @@ -1064,13 +1064,11 @@ static int parse_argv(int argc, char *argv[]) { assert_not_reached("Unhandled option code."); } - if (optind < argc && getpid_cached() != 1) { + if (optind < argc && getpid_cached() != 1) /* Hmm, when we aren't run as init system * let's complain about excess arguments */ - return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Excess arguments."); - } return 0; } @@ -1564,7 +1562,7 @@ static void initialize_clock(void) { else log_info("RTC configured in localtime, applying delta of %i minutes to system time.", min); - } else if (!in_initrd()) { + } else if (!in_initrd()) /* * Do a dummy very first call to seal the kernel's time warp magic. * @@ -1577,7 +1575,6 @@ static void initialize_clock(void) { * be treated as UTC that way. */ (void) clock_reset_timewarp(); - } r = clock_apply_epoch(); if (r < 0) diff --git a/src/core/namespace.c b/src/core/namespace.c index 92df97c4fb..05bd4da154 100644 --- a/src/core/namespace.c +++ b/src/core/namespace.c @@ -1618,13 +1618,12 @@ int setup_namespace( if (r < 0) goto finish; - if (ns_info->private_dev) { + if (ns_info->private_dev) *(m++) = (MountEntry) { .path_const = "/dev", .mode = PRIVATE_DEV, .flags = DEV_MOUNT_OPTIONS, }; - } if (ns_info->protect_kernel_tunables) { r = append_static_mounts(&m, @@ -1653,12 +1652,11 @@ int setup_namespace( goto finish; } - if (ns_info->protect_control_groups) { + if (ns_info->protect_control_groups) *(m++) = (MountEntry) { .path_const = "/sys/fs/cgroup", .mode = READONLY, }; - } r = append_protect_home(&m, ns_info->protect_home, ns_info->ignore_protect_paths); if (r < 0) diff --git a/src/core/selinux-setup.c b/src/core/selinux-setup.c index 40da8dd0e5..8c67352ddb 100644 --- a/src/core/selinux-setup.c +++ b/src/core/selinux-setup.c @@ -96,10 +96,9 @@ int mac_selinux_setup(bool *loaded_policy) { log_open(); if (enforce > 0) { - if (!initialized) { - log_emergency("Failed to load SELinux policy."); - return -EIO; - } + if (!initialized) + return log_emergency_errno(SYNTHETIC_ERRNO(EIO), + "Failed to load SELinux policy."); log_warning("Failed to load new SELinux policy. Continuing with old policy."); } else diff --git a/src/core/service.c b/src/core/service.c index 863b6755b1..d23384c475 100644 --- a/src/core/service.c +++ b/src/core/service.c @@ -4201,7 +4201,7 @@ static void service_bus_name_owner_change(Unit *u, const char *new_owner) { else log_unit_debug(u, "D-Bus name %s now not owned by anyone.", s->bus_name); - s->bus_name_good = !!new_owner; + s->bus_name_good = new_owner; /* Track the current owner, so we can reconstruct changes after a daemon reload */ r = free_and_strdup(&s->bus_name_owner, new_owner); diff --git a/src/coredump/coredump.c b/src/coredump/coredump.c index 229e2d8f87..104a051688 100644 --- a/src/coredump/coredump.c +++ b/src/coredump/coredump.c @@ -353,7 +353,7 @@ static int save_external_coredump( if (r < 0) return log_error_errno(r, "Failed to parse resource limit '%s': %m", context->meta[META_ARGV_RLIMIT]); - if (rlimit < page_size()) { + if (rlimit < page_size()) /* Is coredumping disabled? Then don't bother saving/processing the * coredump. Anything below PAGE_SIZE cannot give a readable coredump * (the kernel uses ELF_EXEC_PAGESIZE which is not easily accessible, but @@ -361,7 +361,6 @@ static int save_external_coredump( return log_info_errno(SYNTHETIC_ERRNO(EBADSLT), "Resource limits disable core dumping for process %s (%s).", context->meta[META_ARGV_PID], context->meta[META_COMM]); - } process_limit = MAX(arg_process_size_max, storage_size_max()); if (process_limit == 0) diff --git a/src/coredump/coredumpctl.c b/src/coredump/coredumpctl.c index 02502528af..f252c81758 100644 --- a/src/coredump/coredumpctl.c +++ b/src/coredump/coredumpctl.c @@ -888,10 +888,9 @@ static int dump_core(int argc, char **argv, void *userdata) { _cleanup_fclose_ FILE *f = NULL; int r; - if (arg_field) { - log_error("Option --field/-F only makes sense with list"); - return -EINVAL; - } + if (arg_field) + return log_error_errno(SYNTHETIC_ERRNO(EINVAL), + "Option --field/-F only makes sense with list"); r = acquire_journal(&j, argv + 1); if (r < 0) @@ -943,10 +942,9 @@ static int run_debug(int argc, char **argv, void *userdata) { if (!debugger) return -ENOMEM; - if (arg_field) { - log_error("Option --field/-F only makes sense with list"); - return -EINVAL; - } + if (arg_field) + return log_error_errno(SYNTHETIC_ERRNO(EINVAL), + "Option --field/-F only makes sense with list"); r = acquire_journal(&j, argv + 1); if (r < 0) @@ -971,15 +969,13 @@ static int run_debug(int argc, char **argv, void *userdata) { if (!exe) return log_oom(); - if (endswith(exe, " (deleted)")) { - log_error("Binary already deleted."); - return -ENOENT; - } + if (endswith(exe, " (deleted)")) + return log_error_errno(SYNTHETIC_ERRNO(ENOENT), + "Binary already deleted."); - if (!path_is_absolute(exe)) { - log_error("Binary is not an absolute path."); - return -ENOENT; - } + if (!path_is_absolute(exe)) + return log_error_errno(SYNTHETIC_ERRNO(ENOENT), + "Binary is not an absolute path."); r = save_core(j, NULL, &path, &unlink_path); if (r < 0) diff --git a/src/cryptsetup/cryptsetup-generator.c b/src/cryptsetup/cryptsetup-generator.c index cd0cedf118..bfd71cd595 100644 --- a/src/cryptsetup/cryptsetup-generator.c +++ b/src/cryptsetup/cryptsetup-generator.c @@ -378,14 +378,13 @@ static int create_disk( else fprintf(f, "Requires=%s\n", unit); - if (umount_unit) { + if (umount_unit) fprintf(f, "Wants=%s\n" "Before=%s\n", umount_unit, umount_unit ); - } } if (!nofail) diff --git a/src/cryptsetup/cryptsetup.c b/src/cryptsetup/cryptsetup.c index 10472bde26..fb08b4a1a0 100644 --- a/src/cryptsetup/cryptsetup.c +++ b/src/cryptsetup/cryptsetup.c @@ -832,10 +832,9 @@ static int run(int argc, char *argv[]) { if (argc <= 1) return help(); - if (argc < 3) { - log_error("This program requires at least two arguments."); - return -EINVAL; - } + if (argc < 3) + return log_error_errno(SYNTHETIC_ERRNO(EINVAL), + "This program requires at least two arguments."); log_setup_service(); diff --git a/src/fsck/fsck.c b/src/fsck/fsck.c index 80f7107b9d..472ba6d549 100644 --- a/src/fsck/fsck.c +++ b/src/fsck/fsck.c @@ -258,10 +258,9 @@ static int run(int argc, char *argv[]) { log_setup_service(); - if (argc > 2) { - log_error("This program expects one or no arguments."); - return -EINVAL; - } + if (argc > 2) + return log_error_errno(SYNTHETIC_ERRNO(EINVAL), + "This program expects one or no arguments."); umask(0022); @@ -284,10 +283,10 @@ static int run(int argc, char *argv[]) { if (stat(device, &st) < 0) return log_error_errno(errno, "Failed to stat %s: %m", device); - if (!S_ISBLK(st.st_mode)) { - log_error("%s is not a block device.", device); - return -EINVAL; - } + if (!S_ISBLK(st.st_mode)) + return log_error_errno(SYNTHETIC_ERRNO(EINVAL), + "%s is not a block device.", + device); r = sd_device_new_from_devnum(&dev, 'b', st.st_rdev); if (r < 0) diff --git a/src/home/homectl-fido2.c b/src/home/homectl-fido2.c index eb9098fda9..0019f59f0f 100644 --- a/src/home/homectl-fido2.c +++ b/src/home/homectl-fido2.c @@ -401,7 +401,8 @@ int identity_add_fido2_parameters( return 0; #else - return log_error_errno(EOPNOTSUPP, "FIDO2 tokens not supported on this build."); + return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), + "FIDO2 tokens not supported on this build."); #endif } @@ -467,7 +468,8 @@ finish: fido_dev_info_free(&di, allocated); return r; #else - return log_error_errno(EOPNOTSUPP, "FIDO2 tokens not supported on this build."); + return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), + "FIDO2 tokens not supported on this build."); #endif } diff --git a/src/home/homectl-pkcs11.c b/src/home/homectl-pkcs11.c index 0583171f04..592a6413e1 100644 --- a/src/home/homectl-pkcs11.c +++ b/src/home/homectl-pkcs11.c @@ -77,7 +77,9 @@ static int acquire_pkcs11_certificate( r = pkcs11_find_token(uri, pkcs11_callback, &data); if (r == -EAGAIN) /* pkcs11_find_token() doesn't log about this error, but all others */ - return log_error_errno(ENXIO, "Specified PKCS#11 token with URI '%s' not found.", uri); + return log_error_errno(SYNTHETIC_ERRNO(ENXIO), + "Specified PKCS#11 token with URI '%s' not found.", + uri); if (r < 0) return r; @@ -86,7 +88,8 @@ static int acquire_pkcs11_certificate( return 0; #else - return log_error_errno(EOPNOTSUPP, "PKCS#11 tokens not supported on this build."); + return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), + "PKCS#11 tokens not supported on this build."); #endif } @@ -415,7 +418,8 @@ int list_pkcs11_tokens(void) { return 0; #else - return log_error_errno(EOPNOTSUPP, "PKCS#11 tokens not supported on this build."); + return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), + "PKCS#11 tokens not supported on this build."); #endif } diff --git a/src/home/homework.c b/src/home/homework.c index 14e2002c73..4b924b2a7c 100644 --- a/src/home/homework.c +++ b/src/home/homework.c @@ -910,7 +910,8 @@ static int user_record_compile_effective_passwords( * the old literal password only (and do not care for the old PKCS#11 token) */ if (strv_isempty(h->hashed_password)) - return log_error_errno(EINVAL, "User record has no hashed passwords, refusing."); + return log_error_errno(SYNTHETIC_ERRNO(EINVAL), + "User record has no hashed passwords, refusing."); /* Generates the list of plaintext passwords to propagate to LUKS/fscrypt devices, and checks whether * we have a plaintext password for each hashed one. If we are missing one we'll fail, since we diff --git a/src/hostname/hostnamectl.c b/src/hostname/hostnamectl.c index c6d8b8d082..c38600b9c3 100644 --- a/src/hostname/hostnamectl.c +++ b/src/hostname/hostnamectl.c @@ -194,10 +194,9 @@ static int show_status(int argc, char **argv, void *userdata) { if (arg_pretty || arg_static || arg_transient) { const char *attr; - if (!!arg_static + !!arg_pretty + !!arg_transient > 1) { - log_error("Cannot query more than one name type at a time"); - return -EINVAL; - } + if (!!arg_static + !!arg_pretty + !!arg_transient > 1) + return log_error_errno(SYNTHETIC_ERRNO(EINVAL), + "Cannot query more than one name type at a time"); attr = arg_pretty ? "PrettyHostname" : arg_static ? "StaticHostname" : "Hostname"; diff --git a/src/import/import-fs.c b/src/import/import-fs.c index db55fa50d5..f631f857fc 100644 --- a/src/import/import-fs.c +++ b/src/import/import-fs.c @@ -126,10 +126,10 @@ static int import_fs(int argc, char *argv[], void *userdata) { local = empty_or_dash_to_null(local); if (local) { - if (!machine_name_is_valid(local)) { - log_error("Local image name '%s' is not valid.", local); - return -EINVAL; - } + if (!machine_name_is_valid(local)) + return log_error_errno(SYNTHETIC_ERRNO(EINVAL), + "Local image name '%s' is not valid.", + local); if (!arg_force) { r = image_find(IMAGE_MACHINE, local, NULL); @@ -137,8 +137,9 @@ static int import_fs(int argc, char *argv[], void *userdata) { if (r != -ENOENT) return log_error_errno(r, "Failed to check whether image '%s' exists: %m", local); } else { - log_error("Image '%s' already exists.", local); - return -EEXIST; + return log_error_errno(SYNTHETIC_ERRNO(EEXIST), + "Image '%s' already exists.", + local); } } } else diff --git a/src/import/import.c b/src/import/import.c index cc28557459..64ec066a8f 100644 --- a/src/import/import.c +++ b/src/import/import.c @@ -64,10 +64,10 @@ static int import_tar(int argc, char *argv[], void *userdata) { local = ll; - if (!machine_name_is_valid(local)) { - log_error("Local image name '%s' is not valid.", local); - return -EINVAL; - } + if (!machine_name_is_valid(local)) + return log_error_errno(SYNTHETIC_ERRNO(EINVAL), + "Local image name '%s' is not valid.", + local); if (!arg_force) { r = image_find(IMAGE_MACHINE, local, NULL); @@ -75,8 +75,9 @@ static int import_tar(int argc, char *argv[], void *userdata) { if (r != -ENOENT) return log_error_errno(r, "Failed to check whether image '%s' exists: %m", local); } else { - log_error("Image '%s' already exists.", local); - return -EEXIST; + return log_error_errno(SYNTHETIC_ERRNO(EEXIST), + "Image '%s' already exists.", + local); } } } else @@ -158,10 +159,10 @@ static int import_raw(int argc, char *argv[], void *userdata) { local = ll; - if (!machine_name_is_valid(local)) { - log_error("Local image name '%s' is not valid.", local); - return -EINVAL; - } + if (!machine_name_is_valid(local)) + return log_error_errno(SYNTHETIC_ERRNO(EINVAL), + "Local image name '%s' is not valid.", + local); if (!arg_force) { r = image_find(IMAGE_MACHINE, local, NULL); @@ -169,8 +170,9 @@ static int import_raw(int argc, char *argv[], void *userdata) { if (r != -ENOENT) return log_error_errno(r, "Failed to check whether image '%s' exists: %m", local); } else { - log_error("Image '%s' already exists.", local); - return -EEXIST; + return log_error_errno(SYNTHETIC_ERRNO(EEXIST), + "Image '%s' already exists.", + local); } } } else diff --git a/src/import/pull-common.c b/src/import/pull-common.c index 249ea533e6..fdc70447e1 100644 --- a/src/import/pull-common.c +++ b/src/import/pull-common.c @@ -389,10 +389,9 @@ int pull_verify(PullJob *main_job, assert(checksum_job->state == PULL_JOB_DONE); - if (!checksum_job->payload || checksum_job->payload_size <= 0) { - log_error("Checksum is empty, cannot verify."); - return -EBADMSG; - } + if (!checksum_job->payload || checksum_job->payload_size <= 0) + return log_error_errno(SYNTHETIC_ERRNO(EBADMSG), + "Checksum is empty, cannot verify."); r = verify_one(checksum_job, main_job); if (r < 0) @@ -414,10 +413,9 @@ int pull_verify(PullJob *main_job, assert(signature_job->state == PULL_JOB_DONE); - if (!signature_job->payload || signature_job->payload_size <= 0) { - log_error("Signature is empty, cannot verify."); - return -EBADMSG; - } + if (!signature_job->payload || signature_job->payload_size <= 0) + return log_error_errno(SYNTHETIC_ERRNO(EBADMSG), + "Signature is empty, cannot verify."); r = pipe2(gpg_pipe, O_CLOEXEC); if (r < 0) diff --git a/src/import/pull.c b/src/import/pull.c index 7e8712493f..9b27ec2630 100644 --- a/src/import/pull.c +++ b/src/import/pull.c @@ -49,10 +49,9 @@ static int pull_tar(int argc, char *argv[], void *userdata) { int r; url = argv[1]; - if (!http_url_is_valid(url)) { - log_error("URL '%s' is not valid.", url); - return -EINVAL; - } + if (!http_url_is_valid(url)) + return log_error_errno(SYNTHETIC_ERRNO(EINVAL), + "URL '%s' is not valid.", url); if (argc >= 3) local = argv[2]; @@ -73,10 +72,10 @@ static int pull_tar(int argc, char *argv[], void *userdata) { local = ll; - if (!machine_name_is_valid(local)) { - log_error("Local image name '%s' is not valid.", local); - return -EINVAL; - } + if (!machine_name_is_valid(local)) + return log_error_errno(SYNTHETIC_ERRNO(EINVAL), + "Local image name '%s' is not valid.", + local); if (!arg_force) { r = image_find(IMAGE_MACHINE, local, NULL); @@ -84,8 +83,9 @@ static int pull_tar(int argc, char *argv[], void *userdata) { if (r != -ENOENT) return log_error_errno(r, "Failed to check whether image '%s' exists: %m", local); } else { - log_error("Image '%s' already exists.", local); - return -EEXIST; + return log_error_errno(SYNTHETIC_ERRNO(EEXIST), + "Image '%s' already exists.", + local); } } @@ -135,10 +135,9 @@ static int pull_raw(int argc, char *argv[], void *userdata) { int r; url = argv[1]; - if (!http_url_is_valid(url)) { - log_error("URL '%s' is not valid.", url); - return -EINVAL; - } + if (!http_url_is_valid(url)) + return log_error_errno(SYNTHETIC_ERRNO(EINVAL), + "URL '%s' is not valid.", url); if (argc >= 3) local = argv[2]; @@ -159,10 +158,10 @@ static int pull_raw(int argc, char *argv[], void *userdata) { local = ll; - if (!machine_name_is_valid(local)) { - log_error("Local image name '%s' is not valid.", local); - return -EINVAL; - } + if (!machine_name_is_valid(local)) + return log_error_errno(SYNTHETIC_ERRNO(EINVAL), + "Local image name '%s' is not valid.", + local); if (!arg_force) { r = image_find(IMAGE_MACHINE, local, NULL); @@ -170,8 +169,9 @@ static int pull_raw(int argc, char *argv[], void *userdata) { if (r != -ENOENT) return log_error_errno(r, "Failed to check whether image '%s' exists: %m", local); } else { - log_error("Image '%s' already exists.", local); - return -EEXIST; + return log_error_errno(SYNTHETIC_ERRNO(EEXIST), + "Image '%s' already exists.", + local); } } diff --git a/src/journal/compress.c b/src/journal/compress.c index 852f9e07ba..a1bd4a379e 100644 --- a/src/journal/compress.c +++ b/src/journal/compress.c @@ -544,10 +544,10 @@ int compress_stream_xz(int fdf, int fdt, uint64_t max_bytes) { assert(fdt >= 0); ret = lzma_easy_encoder(&s, LZMA_PRESET_DEFAULT, LZMA_CHECK_CRC64); - if (ret != LZMA_OK) { - log_error("Failed to initialize XZ encoder: code %u", ret); - return -EINVAL; - } + if (ret != LZMA_OK) + return log_error_errno(SYNTHETIC_ERRNO(EINVAL), + "Failed to initialize XZ encoder: code %u", + ret); for (;;) { if (s.avail_in == 0 && action == LZMA_RUN) { @@ -579,10 +579,10 @@ int compress_stream_xz(int fdf, int fdt, uint64_t max_bytes) { } ret = lzma_code(&s, action); - if (!IN_SET(ret, LZMA_OK, LZMA_STREAM_END)) { - log_error("Compression failed: code %u", ret); - return -EBADMSG; - } + if (!IN_SET(ret, LZMA_OK, LZMA_STREAM_END)) + return log_error_errno(SYNTHETIC_ERRNO(EBADMSG), + "Compression failed: code %u", + ret); if (s.avail_out == 0 || ret == LZMA_STREAM_END) { ssize_t n, k; @@ -664,10 +664,10 @@ int compress_stream_lz4(int fdf, int fdt, uint64_t max_bytes) { offset += n; total_out += n; - if (max_bytes != (uint64_t) -1 && total_out > (size_t) max_bytes) { - log_debug("Compressed stream longer than %"PRIu64" bytes", max_bytes); - return -EFBIG; - } + if (max_bytes != (uint64_t) -1 && total_out > (size_t) max_bytes) + return log_debug_errno(SYNTHETIC_ERRNO(EFBIG), + "Compressed stream longer than %" PRIu64 " bytes", + max_bytes); if (size - offset < frame_size + 4) { k = loop_write(fdt, buf, offset, false); @@ -715,10 +715,10 @@ int decompress_stream_xz(int fdf, int fdt, uint64_t max_bytes) { assert(fdt >= 0); ret = lzma_stream_decoder(&s, UINT64_MAX, 0); - if (ret != LZMA_OK) { - log_debug("Failed to initialize XZ decoder: code %u", ret); - return -ENOMEM; - } + if (ret != LZMA_OK) + return log_debug_errno(SYNTHETIC_ERRNO(ENOMEM), + "Failed to initialize XZ decoder: code %u", + ret); for (;;) { if (s.avail_in == 0 && action == LZMA_RUN) { @@ -741,10 +741,10 @@ int decompress_stream_xz(int fdf, int fdt, uint64_t max_bytes) { } ret = lzma_code(&s, action); - if (!IN_SET(ret, LZMA_OK, LZMA_STREAM_END)) { - log_debug("Decompression failed: code %u", ret); - return -EBADMSG; - } + if (!IN_SET(ret, LZMA_OK, LZMA_STREAM_END)) + return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG), + "Decompression failed: code %u", + ret); if (s.avail_out == 0 || ret == LZMA_STREAM_END) { ssize_t n, k; @@ -772,8 +772,8 @@ int decompress_stream_xz(int fdf, int fdt, uint64_t max_bytes) { } } #else - log_debug("Cannot decompress file. Compiled without XZ support."); - return -EPROTONOSUPPORT; + return log_debug_errno(SYNTHETIC_ERRNO(EPROTONOSUPPORT), + "Cannot decompress file. Compiled without XZ support."); #endif } @@ -833,8 +833,8 @@ int decompress_stream_lz4(int in, int out, uint64_t max_bytes) { munmap(src, st.st_size); return r; #else - log_debug("Cannot decompress file. Compiled without LZ4 support."); - return -EPROTONOSUPPORT; + return log_debug_errno(SYNTHETIC_ERRNO(EPROTONOSUPPORT), + "Cannot decompress file. Compiled without LZ4 support."); #endif } @@ -1043,8 +1043,8 @@ int decompress_stream_zstd(int fdf, int fdt, uint64_t max_bytes) { (double) (max_bytes - left) / in_bytes * 100); return 0; #else - log_debug("Cannot decompress file. Compiled without ZSTD support."); - return -EPROTONOSUPPORT; + return log_debug_errno(SYNTHETIC_ERRNO(EPROTONOSUPPORT), + "Cannot decompress file. Compiled without ZSTD support."); #endif } diff --git a/src/journal/journal-send.c b/src/journal/journal-send.c index d51d03acd9..64882eb4ee 100644 --- a/src/journal/journal-send.c +++ b/src/journal/journal-send.c @@ -139,7 +139,7 @@ _printf_(1, 0) static int fill_iovec_sprintf(const char *format, va_list ap, int if (i >= n) { n = MAX(i*2, 4); - c = realloc(iov, n * sizeof(struct iovec)); + c = reallocarray(iov, n, sizeof(struct iovec)); if (!c) { r = -ENOMEM; goto fail; diff --git a/src/journal/journalctl.c b/src/journal/journalctl.c index 52e204ee11..e43e9d1afa 100644 --- a/src/journal/journalctl.c +++ b/src/journal/journalctl.c @@ -199,10 +199,9 @@ static int add_matches_for_device(sd_journal *j, const char *devpath) { assert(j); assert(devpath); - if (!path_startswith(devpath, "/dev/")) { - log_error("Devpath does not start with /dev/"); - return -EINVAL; - } + if (!path_startswith(devpath, "/dev/")) + return log_error_errno(SYNTHETIC_ERRNO(EINVAL), + "Devpath does not start with /dev/"); if (stat(devpath, &st) < 0) return log_error_errno(errno, "Couldn't stat file: %m"); @@ -1055,35 +1054,30 @@ static int parse_argv(int argc, char *argv[]) { if (arg_follow && !arg_no_tail && !arg_since && arg_lines == ARG_LINES_DEFAULT) arg_lines = 10; - if (!!arg_directory + !!arg_file + !!arg_machine + !!arg_root + !!arg_image > 1) { - log_error("Please specify at most one of -D/--directory=, --file=, -M/--machine=, --root=, --image=."); - return -EINVAL; - } + if (!!arg_directory + !!arg_file + !!arg_machine + !!arg_root + !!arg_image > 1) + return log_error_errno(SYNTHETIC_ERRNO(EINVAL), + "Please specify at most one of -D/--directory=, --file=, -M/--machine=, --root=, --image=."); - if (arg_since_set && arg_until_set && arg_since > arg_until) { - log_error("--since= must be before --until=."); - return -EINVAL; - } + if (arg_since_set && arg_until_set && arg_since > arg_until) + return log_error_errno(SYNTHETIC_ERRNO(EINVAL), + "--since= must be before --until=."); - if (!!arg_cursor + !!arg_after_cursor + !!arg_since_set > 1) { - log_error("Please specify only one of --since=, --cursor=, and --after-cursor."); - return -EINVAL; - } + if (!!arg_cursor + !!arg_after_cursor + !!arg_since_set > 1) + return log_error_errno(SYNTHETIC_ERRNO(EINVAL), + "Please specify only one of --since=, --cursor=, and --after-cursor."); - if (arg_follow && arg_reverse) { - log_error("Please specify either --reverse= or --follow=, not both."); - return -EINVAL; - } + if (arg_follow && arg_reverse) + return log_error_errno(SYNTHETIC_ERRNO(EINVAL), + "Please specify either --reverse= or --follow=, not both."); - if (!IN_SET(arg_action, ACTION_SHOW, ACTION_DUMP_CATALOG, ACTION_LIST_CATALOG) && optind < argc) { - log_error("Extraneous arguments starting with '%s'", argv[optind]); - return -EINVAL; - } + if (!IN_SET(arg_action, ACTION_SHOW, ACTION_DUMP_CATALOG, ACTION_LIST_CATALOG) && optind < argc) + return log_error_errno(SYNTHETIC_ERRNO(EINVAL), + "Extraneous arguments starting with '%s'", + argv[optind]); - if ((arg_boot || arg_action == ACTION_LIST_BOOTS) && arg_merge) { - log_error("Using --boot or --list-boots with --merge is not supported."); - return -EINVAL; - } + if ((arg_boot || arg_action == ACTION_LIST_BOOTS) && arg_merge) + return log_error_errno(SYNTHETIC_ERRNO(EINVAL), + "Using --boot or --list-boots with --merge is not supported."); if (!strv_isempty(arg_system_units) && arg_journal_type == SD_JOURNAL_CURRENT_USER) { /* Specifying --user and --unit= at the same time makes no sense (as the former excludes the user @@ -1921,7 +1915,8 @@ static int setup_keys(void) { "Please write down the following %ssecret verification key%s. It should be stored\n" "in a safe location and should not be saved locally on disk.\n" "\n\t%s", - hn ?: "", hn ? "/" : "", SD_ID128_FORMAT_VAL(machine), + strempty(hn), hn ? "/" : "", + SD_ID128_FORMAT_VAL(machine), ansi_highlight(), ansi_normal(), p, format_timespan(tsb, sizeof(tsb), arg_interval, 0), @@ -2476,7 +2471,7 @@ int main(int argc, char *argv[]) { after_cursor = true; } } else - after_cursor = !!arg_after_cursor; + after_cursor = arg_after_cursor; if (cursor) { r = sd_journal_seek_cursor(j, cursor); diff --git a/src/journal/test-journal-stream.c b/src/journal/test-journal-stream.c index 50aab11c6a..924d727d97 100644 --- a/src/journal/test-journal-stream.c +++ b/src/journal/test-journal-stream.c @@ -7,6 +7,7 @@ #include "alloc-util.h" #include "chattr-util.h" +#include "io-util.h" #include "journal-file.h" #include "journal-internal.h" #include "log.h" @@ -92,13 +93,11 @@ static void run_test(void) { previous_ts = ts; assert_se(asprintf(&p, "NUMBER=%u", i) >= 0); - iovec[0].iov_base = p; - iovec[0].iov_len = strlen(p); + iovec[0] = IOVEC_MAKE(p, strlen(p)); assert_se(asprintf(&q, "MAGIC=%s", i % 5 == 0 ? "quux" : "waldo") >= 0); - iovec[1].iov_base = q; - iovec[1].iov_len = strlen(q); + iovec[1] = IOVEC_MAKE(q, strlen(q)); if (i % 10 == 0) assert_se(journal_file_append_entry(three, &ts, NULL, iovec, 2, NULL, NULL, NULL) == 0); diff --git a/src/libsystemd-network/dhcp-identifier.c b/src/libsystemd-network/dhcp-identifier.c index d0610a32e2..e7dbec1511 100644 --- a/src/libsystemd-network/dhcp-identifier.c +++ b/src/libsystemd-network/dhcp-identifier.c @@ -26,11 +26,10 @@ int dhcp_validate_duid_len(uint16_t duid_type, size_t duid_len, bool strict) { if (duid_len > MAX_DUID_LEN) return -EINVAL; - if (!strict) { + if (!strict) /* Strict validation is not requested. We only ensure that the * DUID is not too long. */ return 0; - } switch (duid_type) { case DUID_TYPE_LLT: diff --git a/src/libsystemd-network/sd-dhcp-client.c b/src/libsystemd-network/sd-dhcp-client.c index 265b6eab44..1b622ce5a8 100644 --- a/src/libsystemd-network/sd-dhcp-client.c +++ b/src/libsystemd-network/sd-dhcp-client.c @@ -1880,12 +1880,11 @@ static int client_receive_message_udp( assert(client); buflen = next_datagram_size_fd(fd); - if (buflen == -ENETDOWN) { + if (buflen == -ENETDOWN) /* the link is down. Don't return an error or the I/O event source will be disconnected and we won't be able to receive packets again when the link comes back. */ return 0; - } if (buflen < 0) return buflen; diff --git a/src/libsystemd-network/sd-dhcp-lease.c b/src/libsystemd-network/sd-dhcp-lease.c index 83e1b90291..5f89d00452 100644 --- a/src/libsystemd-network/sd-dhcp-lease.c +++ b/src/libsystemd-network/sd-dhcp-lease.c @@ -1233,10 +1233,8 @@ int dhcp_lease_load(sd_dhcp_lease **ret, const char *lease_file) { if (!a) return -ENOMEM; - if (!strv_isempty(a)) { - lease->search_domains = a; - a = NULL; - } + if (!strv_isempty(a)) + lease->search_domains = TAKE_PTR(a); } if (routes) { diff --git a/src/libsystemd-network/sd-dhcp6-client.c b/src/libsystemd-network/sd-dhcp6-client.c index 38025b63d9..e1dbdaff4e 100644 --- a/src/libsystemd-network/sd-dhcp6-client.c +++ b/src/libsystemd-network/sd-dhcp6-client.c @@ -1420,12 +1420,11 @@ static int client_receive_message( assert(client->event); buflen = next_datagram_size_fd(fd); - if (buflen == -ENETDOWN) { + if (buflen == -ENETDOWN) /* the link is down. Don't return an error or the I/O event source will be disconnected and we won't be able to receive packets again when the link comes back. */ return 0; - } if (buflen < 0) return buflen; diff --git a/src/libsystemd-network/test-dhcp-server.c b/src/libsystemd-network/test-dhcp-server.c index a45d98f669..16a4f16bfa 100644 --- a/src/libsystemd-network/test-dhcp-server.c +++ b/src/libsystemd-network/test-dhcp-server.c @@ -23,10 +23,10 @@ static void test_pool(struct in_addr *address, unsigned size, int ret) { static int test_basic(sd_event *event) { _cleanup_(sd_dhcp_server_unrefp) sd_dhcp_server *server = NULL; struct in_addr address_lo = { - .s_addr = htonl(INADDR_LOOPBACK), + .s_addr = htobe32(INADDR_LOOPBACK), }; struct in_addr address_any = { - .s_addr = htonl(INADDR_ANY), + .s_addr = htobe32(INADDR_ANY), }; int r; @@ -105,7 +105,7 @@ static void test_message_handler(void) { .end = SD_DHCP_OPTION_END, }; struct in_addr address_lo = { - .s_addr = htonl(INADDR_LOOPBACK), + .s_addr = htobe32(INADDR_LOOPBACK), }; assert_se(sd_dhcp_server_new(&server, 1) >= 0); diff --git a/src/libsystemd/sd-bus/test-bus-chat.c b/src/libsystemd/sd-bus/test-bus-chat.c index f67d06ecec..1497dc696f 100644 --- a/src/libsystemd/sd-bus/test-bus-chat.c +++ b/src/libsystemd/sd-bus/test-bus-chat.c @@ -285,8 +285,7 @@ static void* client1(void *p) { assert_se(streq(hello, "hello")); if (pipe2(pp, O_CLOEXEC|O_NONBLOCK) < 0) { - log_error_errno(errno, "Failed to allocate pipe: %m"); - r = -errno; + r = log_error_errno(errno, "Failed to allocate pipe: %m"); goto finish; } diff --git a/src/libsystemd/sd-event/test-event.c b/src/libsystemd/sd-event/test-event.c index 54d293ca46..b623972365 100644 --- a/src/libsystemd/sd-event/test-event.c +++ b/src/libsystemd/sd-event/test-event.c @@ -548,10 +548,9 @@ static void test_pidfd(void) { assert_se(sigprocmask_many(SIG_BLOCK, NULL, SIGCHLD, -1) >= 0); pid = fork(); - if (pid == 0) { + if (pid == 0) /* child */ _exit(66); - } assert_se(pid > 1); diff --git a/src/libsystemd/sd-hwdb/sd-hwdb.c b/src/libsystemd/sd-hwdb/sd-hwdb.c index b3febdbb31..0e326f3d22 100644 --- a/src/libsystemd/sd-hwdb/sd-hwdb.c +++ b/src/libsystemd/sd-hwdb/sd-hwdb.c @@ -343,10 +343,10 @@ _public_ int sd_hwdb_new(sd_hwdb **ret) { return log_debug_errno(errno, "Failed to map %s: %m", hwdb_bin_path); if (memcmp(hwdb->map, sig, sizeof(hwdb->head->signature)) != 0 || - (size_t) hwdb->st.st_size != le64toh(hwdb->head->file_size)) { - log_debug("Failed to recognize the format of %s", hwdb_bin_path); - return -EINVAL; - } + (size_t) hwdb->st.st_size != le64toh(hwdb->head->file_size)) + return log_debug_errno(SYNTHETIC_ERRNO(EINVAL), + "Failed to recognize the format of %s", + hwdb_bin_path); log_debug("=== trie on-disk ==="); log_debug("tool version: %"PRIu64, le64toh(hwdb->head->tool_version)); diff --git a/src/libsystemd/sd-netlink/test-netlink.c b/src/libsystemd/sd-netlink/test-netlink.c index f9d79bc0d6..be7a4f7835 100644 --- a/src/libsystemd/sd-netlink/test-netlink.c +++ b/src/libsystemd/sd-netlink/test-netlink.c @@ -139,7 +139,7 @@ static void test_route(sd_netlink *rtnl) { return; } - addr.s_addr = htonl(INADDR_LOOPBACK); + addr.s_addr = htobe32(INADDR_LOOPBACK); r = sd_netlink_message_append_in_addr(req, RTA_GATEWAY, &addr); if (r < 0) { diff --git a/src/libsystemd/sd-resolve/test-resolve.c b/src/libsystemd/sd-resolve/test-resolve.c index 8377c66ae3..627b86b3cb 100644 --- a/src/libsystemd/sd-resolve/test-resolve.c +++ b/src/libsystemd/sd-resolve/test-resolve.c @@ -63,7 +63,7 @@ int main(int argc, char *argv[]) { struct sockaddr_in sa = { .sin_family = AF_INET, - .sin_port = htons(80) + .sin_port = htobe16(80) }; assert_se(sd_resolve_default(&resolve) >= 0); diff --git a/src/login/loginctl.c b/src/login/loginctl.c index 053bb601a2..b7f400dc5e 100644 --- a/src/login/loginctl.c +++ b/src/login/loginctl.c @@ -545,8 +545,7 @@ static int print_session_status_info(sd_bus *bus, const char *path, bool *new_li printf("\t Unit: %s\n", i.scope); show_unit_cgroup(bus, "org.freedesktop.systemd1.Scope", i.scope, i.leader); - if (arg_transport == BUS_TRANSPORT_LOCAL) { - + if (arg_transport == BUS_TRANSPORT_LOCAL) show_journal_by_unit( stdout, i.scope, @@ -560,7 +559,6 @@ static int print_session_status_info(sd_bus *bus, const char *path, bool *new_li SD_JOURNAL_LOCAL_ONLY, true, NULL); - } } return 0; diff --git a/src/login/logind-core.c b/src/login/logind-core.c index a685b8e5b8..370604f936 100644 --- a/src/login/logind-core.c +++ b/src/login/logind-core.c @@ -21,6 +21,7 @@ #include "parse-util.h" #include "path-util.h" #include "process-util.h" +#include "stdio-util.h" #include "strv.h" #include "terminal-util.h" #include "udev-util.h" @@ -533,7 +534,7 @@ int manager_spawn_autovt(Manager *m, unsigned vtnr) { return -EBUSY; } - snprintf(name, sizeof(name), "autovt@tty%u.service", vtnr); + xsprintf(name, "autovt@tty%u.service", vtnr); r = sd_bus_call_method( m->bus, "org.freedesktop.systemd1", diff --git a/src/machine/machinectl.c b/src/machine/machinectl.c index 41070dcda5..4869797b1e 100644 --- a/src/machine/machinectl.c +++ b/src/machine/machinectl.c @@ -1312,15 +1312,13 @@ static int login_machine(int argc, char *argv[], void *userdata) { assert(bus); - if (!strv_isempty(arg_setenv) || arg_uid) { - log_error("--setenv= and --uid= are not supported for 'login'. Use 'shell' instead."); - return -EINVAL; - } + if (!strv_isempty(arg_setenv) || arg_uid) + return log_error_errno(SYNTHETIC_ERRNO(EINVAL), + "--setenv= and --uid= are not supported for 'login'. Use 'shell' instead."); - if (!IN_SET(arg_transport, BUS_TRANSPORT_LOCAL, BUS_TRANSPORT_MACHINE)) { - log_error("Login only supported on local machines."); - return -EOPNOTSUPP; - } + if (!IN_SET(arg_transport, BUS_TRANSPORT_LOCAL, BUS_TRANSPORT_MACHINE)) + return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), + "Login only supported on local machines."); polkit_agent_open_if_enabled(arg_transport, arg_ask_password); @@ -1369,10 +1367,9 @@ static int shell_machine(int argc, char *argv[], void *userdata) { assert(bus); - if (!IN_SET(arg_transport, BUS_TRANSPORT_LOCAL, BUS_TRANSPORT_MACHINE)) { - log_error("Shell only supported on local machines."); - return -EOPNOTSUPP; - } + if (!IN_SET(arg_transport, BUS_TRANSPORT_LOCAL, BUS_TRANSPORT_MACHINE)) + return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), + "Shell only supported on local machines."); /* Pass $TERM to shell session, if not explicitly specified. */ if (!strv_find_prefix(arg_setenv, "TERM=")) { @@ -1525,10 +1522,10 @@ static int read_only_image(int argc, char *argv[], void *userdata) { if (argc > 2) { b = parse_boolean(argv[2]); - if (b < 0) { - log_error("Failed to parse boolean argument: %s", argv[2]); - return -EINVAL; - } + if (b < 0) + return log_error_errno(SYNTHETIC_ERRNO(EINVAL), + "Failed to parse boolean argument: %s", + argv[2]); } polkit_agent_open_if_enabled(arg_transport, arg_ask_password); @@ -1602,10 +1599,10 @@ static int start_machine(int argc, char *argv[], void *userdata) { r = image_exists(bus, argv[i]); if (r < 0) return r; - if (r == 0) { - log_error("Machine image '%s' does not exist.", argv[i]); - return -ENXIO; - } + if (r == 0) + return log_error_errno(SYNTHETIC_ERRNO(ENXIO), + "Machine image '%s' does not exist.", + argv[i]); r = sd_bus_call_method( bus, @@ -1674,10 +1671,10 @@ static int enable_machine(int argc, char *argv[], void *userdata) { r = image_exists(bus, argv[i]); if (r < 0) return r; - if (r == 0) { - log_error("Machine image '%s' does not exist.", argv[i]); - return -ENXIO; - } + if (r == 0) + return log_error_errno(SYNTHETIC_ERRNO(ENXIO), + "Machine image '%s' does not exist.", + argv[i]); r = sd_bus_message_append(m, "s", unit); if (r < 0) @@ -1874,10 +1871,9 @@ static int import_tar(int argc, char *argv[], void *userdata) { local = fn; } - if (!local) { - log_error("Need either path or local name."); - return -EINVAL; - } + if (!local) + return log_error_errno(SYNTHETIC_ERRNO(EINVAL), + "Need either path or local name."); r = tar_strip_suffixes(local, &ll); if (r < 0) @@ -1885,10 +1881,10 @@ static int import_tar(int argc, char *argv[], void *userdata) { local = ll; - if (!machine_name_is_valid(local)) { - log_error("Local name %s is not a suitable machine name.", local); - return -EINVAL; - } + if (!machine_name_is_valid(local)) + return log_error_errno(SYNTHETIC_ERRNO(EINVAL), + "Local name %s is not a suitable machine name.", + local); if (path) { fd = open(path, O_RDONLY|O_CLOEXEC|O_NOCTTY); @@ -1935,10 +1931,9 @@ static int import_raw(int argc, char *argv[], void *userdata) { local = fn; } - if (!local) { - log_error("Need either path or local name."); - return -EINVAL; - } + if (!local) + return log_error_errno(SYNTHETIC_ERRNO(EINVAL), + "Need either path or local name."); r = raw_strip_suffixes(local, &ll); if (r < 0) @@ -1946,10 +1941,10 @@ static int import_raw(int argc, char *argv[], void *userdata) { local = ll; - if (!machine_name_is_valid(local)) { - log_error("Local name %s is not a suitable machine name.", local); - return -EINVAL; - } + if (!machine_name_is_valid(local)) + return log_error_errno(SYNTHETIC_ERRNO(EINVAL), + "Local name %s is not a suitable machine name.", + local); if (path) { fd = open(path, O_RDONLY|O_CLOEXEC|O_NOCTTY); @@ -1996,15 +1991,14 @@ static int import_fs(int argc, char *argv[], void *userdata) { local = fn; } - if (!local) { - log_error("Need either path or local name."); - return -EINVAL; - } + if (!local) + return log_error_errno(SYNTHETIC_ERRNO(EINVAL), + "Need either path or local name."); - if (!machine_name_is_valid(local)) { - log_error("Local name %s is not a suitable machine name.", local); - return -EINVAL; - } + if (!machine_name_is_valid(local)) + return log_error_errno(SYNTHETIC_ERRNO(EINVAL), + "Local name %s is not a suitable machine name.", + local); if (path) { fd = open(path, O_DIRECTORY|O_RDONLY|O_CLOEXEC); @@ -2054,10 +2048,9 @@ static int export_tar(int argc, char *argv[], void *userdata) { assert(bus); local = argv[1]; - if (!machine_name_is_valid(local)) { - log_error("Machine name %s is not valid.", local); - return -EINVAL; - } + if (!machine_name_is_valid(local)) + return log_error_errno(SYNTHETIC_ERRNO(EINVAL), + "Machine name %s is not valid.", local); if (argc >= 3) path = argv[2]; @@ -2097,10 +2090,9 @@ static int export_raw(int argc, char *argv[], void *userdata) { assert(bus); local = argv[1]; - if (!machine_name_is_valid(local)) { - log_error("Machine name %s is not valid.", local); - return -EINVAL; - } + if (!machine_name_is_valid(local)) + return log_error_errno(SYNTHETIC_ERRNO(EINVAL), + "Machine name %s is not valid.", local); if (argc >= 3) path = argv[2]; @@ -2140,10 +2132,9 @@ static int pull_tar(int argc, char *argv[], void *userdata) { assert(bus); remote = argv[1]; - if (!http_url_is_valid(remote)) { - log_error("URL '%s' is not valid.", remote); - return -EINVAL; - } + if (!http_url_is_valid(remote)) + return log_error_errno(SYNTHETIC_ERRNO(EINVAL), + "URL '%s' is not valid.", remote); if (argc >= 3) local = argv[2]; @@ -2164,10 +2155,10 @@ static int pull_tar(int argc, char *argv[], void *userdata) { local = ll; - if (!machine_name_is_valid(local)) { - log_error("Local name %s is not a suitable machine name.", local); - return -EINVAL; - } + if (!machine_name_is_valid(local)) + return log_error_errno(SYNTHETIC_ERRNO(EINVAL), + "Local name %s is not a suitable machine name.", + local); } r = bus_message_new_method_call(bus, &m, bus_import_mgr, "PullTar"); @@ -2197,10 +2188,9 @@ static int pull_raw(int argc, char *argv[], void *userdata) { assert(bus); remote = argv[1]; - if (!http_url_is_valid(remote)) { - log_error("URL '%s' is not valid.", remote); - return -EINVAL; - } + if (!http_url_is_valid(remote)) + return log_error_errno(SYNTHETIC_ERRNO(EINVAL), + "URL '%s' is not valid.", remote); if (argc >= 3) local = argv[2]; @@ -2221,10 +2211,10 @@ static int pull_raw(int argc, char *argv[], void *userdata) { local = ll; - if (!machine_name_is_valid(local)) { - log_error("Local name %s is not a suitable machine name.", local); - return -EINVAL; - } + if (!machine_name_is_valid(local)) + return log_error_errno(SYNTHETIC_ERRNO(EINVAL), + "Local name %s is not a suitable machine name.", + local); } r = bus_message_new_method_call(bus, &m, bus_import_mgr, "PullRaw"); diff --git a/src/mount/mount-tool.c b/src/mount/mount-tool.c index 22da189684..9e57033999 100644 --- a/src/mount/mount-tool.c +++ b/src/mount/mount-tool.c @@ -923,10 +923,9 @@ static int umount_by_device(sd_bus *bus, const char *what) { if (stat(what, &st) < 0) return log_error_errno(errno, "Can't stat %s: %m", what); - if (!S_ISBLK(st.st_mode)) { - log_error("Not a block device: %s", what); - return -ENOTBLK; - } + if (!S_ISBLK(st.st_mode)) + return log_error_errno(SYNTHETIC_ERRNO(ENOTBLK), + "Not a block device: %s", what); r = sd_device_new_from_devnum(&d, 'b', st.st_rdev); if (r < 0) @@ -1249,10 +1248,10 @@ static int discover_loop_backing_file(void) { escaped = xescape(basename(arg_mount_what), "\\"); if (!escaped) return log_oom(); - if (!filename_is_valid(escaped)) { - log_error("Escaped name %s is not a valid filename.", escaped); - return -EINVAL; - } + if (!filename_is_valid(escaped)) + return log_error_errno(SYNTHETIC_ERRNO(EINVAL), + "Escaped name %s is not a valid filename.", + escaped); arg_mount_where = path_join("/run/media/system", escaped); if (!arg_mount_where) @@ -1265,10 +1264,9 @@ static int discover_loop_backing_file(void) { if (stat(loop_dev, &st) < 0) return log_error_errno(errno, "Can't stat %s: %m", loop_dev); - if (!S_ISBLK(st.st_mode)) { - log_error("Invalid file type: %s", loop_dev); - return -EINVAL; - } + if (!S_ISBLK(st.st_mode)) + return log_error_errno(SYNTHETIC_ERRNO(EINVAL), + "Invalid file type: %s", loop_dev); r = sd_device_new_from_devnum(&d, 'b', st.st_rdev); if (r < 0) @@ -1309,19 +1307,19 @@ static int discover_device(void) { if (S_ISREG(st.st_mode)) return discover_loop_backing_file(); - if (!S_ISBLK(st.st_mode)) { - log_error("Invalid file type: %s", arg_mount_what); - return -EINVAL; - } + if (!S_ISBLK(st.st_mode)) + return log_error_errno(SYNTHETIC_ERRNO(EINVAL), + "Invalid file type: %s", + arg_mount_what); r = sd_device_new_from_devnum(&d, 'b', st.st_rdev); if (r < 0) return log_error_errno(r, "Failed to get device from device number: %m"); - if (sd_device_get_property_value(d, "ID_FS_USAGE", &v) < 0 || !streq(v, "filesystem")) { - log_error("%s does not contain a known file system.", arg_mount_what); - return -EINVAL; - } + if (sd_device_get_property_value(d, "ID_FS_USAGE", &v) < 0 || !streq(v, "filesystem")) + return log_error_errno(SYNTHETIC_ERRNO(EINVAL), + "%s does not contain a known file system.", + arg_mount_what); r = acquire_mount_type(d); if (r < 0) @@ -1462,10 +1460,10 @@ static int run(int argc, char* argv[]) { return action_umount(bus, argc, argv); if ((!arg_mount_type || fstype_is_blockdev_backed(arg_mount_type)) - && !path_is_normalized(arg_mount_what)) { - log_error("Path contains non-normalized components: %s", arg_mount_what); - return -EINVAL; - } + && !path_is_normalized(arg_mount_what)) + return log_error_errno(SYNTHETIC_ERRNO(EINVAL), + "Path contains non-normalized components: %s", + arg_mount_what); if (arg_discover) { r = discover_device(); @@ -1473,20 +1471,19 @@ static int run(int argc, char* argv[]) { return r; } - if (!arg_mount_where) { - log_error("Can't figure out where to mount %s.", arg_mount_what); - return -EINVAL; - } + if (!arg_mount_where) + return log_error_errno(SYNTHETIC_ERRNO(EINVAL), + "Can't figure out where to mount %s.", + arg_mount_what); - if (path_equal(arg_mount_where, "/")) { - log_error("Refusing to operate on root directory."); - return -EINVAL; - } + if (path_equal(arg_mount_where, "/")) + return log_error_errno(SYNTHETIC_ERRNO(EINVAL), + "Refusing to operate on root directory."); - if (!path_is_normalized(arg_mount_where)) { - log_error("Path contains non-normalized components: %s", arg_mount_where); - return -EINVAL; - } + if (!path_is_normalized(arg_mount_where)) + return log_error_errno(SYNTHETIC_ERRNO(EINVAL), + "Path contains non-normalized components: %s", + arg_mount_where); if (streq_ptr(arg_mount_type, "auto")) arg_mount_type = mfree(arg_mount_type); @@ -1516,11 +1513,10 @@ static int run(int argc, char* argv[]) { if (arg_mount_type && !streq(arg_mount_type, "auto") && arg_uid != UID_INVALID && - !fstype_can_uid_gid(arg_mount_type)) { - log_error("File system type %s is not known to support uid=/gid=, refusing.", - arg_mount_type); - return -EOPNOTSUPP; - } + !fstype_can_uid_gid(arg_mount_type)) + return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), + "File system type %s is not known to support uid=/gid=, refusing.", + arg_mount_type); switch (arg_action) { diff --git a/src/network/networkctl.c b/src/network/networkctl.c index 0c4f93045f..86e53e6da4 100644 --- a/src/network/networkctl.c +++ b/src/network/networkctl.c @@ -1653,12 +1653,11 @@ static int link_status_one( if (r < 0) return table_log_add_error(r); - if (info->port_state <= BR_STATE_BLOCKING) { + if (info->port_state <= BR_STATE_BLOCKING) r = table_add_many(table, TABLE_EMPTY, TABLE_STRING, "Port State:", TABLE_STRING, bridge_state_to_string(info->port_state)); - } } else if (streq_ptr(info->netdev_kind, "bond")) { r = table_add_many(table, TABLE_EMPTY, diff --git a/src/network/networkd-dhcp-common.c b/src/network/networkd-dhcp-common.c index d3053d0354..46d27f8f54 100644 --- a/src/network/networkd-dhcp-common.c +++ b/src/network/networkd-dhcp-common.c @@ -762,10 +762,9 @@ int config_parse_dhcp_send_option( } case DHCP_OPTION_DATA_STRING: sz = cunescape(p, UNESCAPE_ACCEPT_NUL, &q); - if (sz < 0) { + if (sz < 0) log_syntax(unit, LOG_WARNING, filename, line, sz, "Failed to decode DHCP option data, ignoring assignment: %s", p); - } udata = q; break; diff --git a/src/network/tc/gred.c b/src/network/tc/gred.c index 5629ecd01a..7c9e01aba7 100644 --- a/src/network/tc/gred.c +++ b/src/network/tc/gred.c @@ -123,11 +123,10 @@ int config_parse_generic_random_early_detection_u32( return 0; } - if (v > MAX_DPs) { + if (v > MAX_DPs) log_syntax(unit, LOG_WARNING, filename, line, 0, "Invalid '%s=', ignoring assignment: %s", lvalue, rvalue); - } *p = v; qdisc = NULL; diff --git a/src/network/tc/teql.c b/src/network/tc/teql.c index bc64860a35..f727d14341 100644 --- a/src/network/tc/teql.c +++ b/src/network/tc/teql.c @@ -79,11 +79,10 @@ int config_parse_trivial_link_equalizer_id( lvalue, rvalue); return 0; } - if (id > INT_MAX) { + if (id > INT_MAX) log_syntax(unit, LOG_WARNING, filename, line, 0, "'%s=' is too large, ignoring assignment: %s", lvalue, rvalue); - } teql->id = id; diff --git a/src/nspawn/nspawn-oci.c b/src/nspawn/nspawn-oci.c index 60a59096fb..9445dc5b0e 100644 --- a/src/nspawn/nspawn-oci.c +++ b/src/nspawn/nspawn-oci.c @@ -660,10 +660,9 @@ static int oci_namespaces(const char *name, JsonVariant *v, JsonDispatchFlags fl s->network_namespace_path = data.path; } - if (FLAGS_SET(n, data.type)) { + if (FLAGS_SET(n, data.type)) return json_log(e, flags, SYNTHETIC_ERRNO(EINVAL), "Duplicate namespace specification, refusing."); - } n |= data.type; } @@ -2216,14 +2215,14 @@ int oci_load(FILE *f, const char *bundle, Settings **ret) { } v = json_variant_by_key(oci, "ociVersion"); - if (!v) { - log_error("JSON file '%s' is not an OCI bundle configuration file. Refusing.", path); - return -EINVAL; - } - if (!streq_ptr(json_variant_string(v), "1.0.0")) { - log_error("OCI bundle version not supported: %s", strna(json_variant_string(v))); - return -EINVAL; - } + if (!v) + return log_error_errno(SYNTHETIC_ERRNO(EINVAL), + "JSON file '%s' is not an OCI bundle configuration file. Refusing.", + path); + if (!streq_ptr(json_variant_string(v), "1.0.0")) + return log_error_errno(SYNTHETIC_ERRNO(EINVAL), + "OCI bundle version not supported: %s", + strna(json_variant_string(v))); // { // _cleanup_free_ char *formatted = NULL; diff --git a/src/nspawn/nspawn.c b/src/nspawn/nspawn.c index 12a04b0bae..82bb167397 100644 --- a/src/nspawn/nspawn.c +++ b/src/nspawn/nspawn.c @@ -4342,7 +4342,7 @@ static int load_settings(void) { /* If all settings are masked, there's no point in looking for * the settings file */ - if ((arg_settings_mask & _SETTINGS_MASK_ALL) == _SETTINGS_MASK_ALL) + if (FLAGS_SET(arg_settings_mask, _SETTINGS_MASK_ALL)) return 0; fn = strjoina(arg_machine, ".nspawn"); diff --git a/src/nss-systemd/nss-systemd.c b/src/nss-systemd/nss-systemd.c index 0e8c13f7ea..06ac335b29 100644 --- a/src/nss-systemd/nss-systemd.c +++ b/src/nss-systemd/nss-systemd.c @@ -595,7 +595,7 @@ enum nss_status _nss_systemd_initgroups_dyn( new_size = limit; /* Enlarge buffer */ - new_groups = realloc(*groupsp, new_size * sizeof(**groupsp)); + new_groups = reallocarray(*groupsp, new_size, sizeof(**groupsp)); if (!new_groups) { UNPROTECT_ERRNO; *errnop = ENOMEM; diff --git a/src/portable/portable.c b/src/portable/portable.c index d74331df3e..55ea6759e9 100644 --- a/src/portable/portable.c +++ b/src/portable/portable.c @@ -434,10 +434,9 @@ static int portable_extract_by_path( if (isempty(name) && fd < 0) break; - if (isempty(name) || fd < 0) { - log_debug("Invalid item sent from child."); - return -EINVAL; - } + if (isempty(name) || fd < 0) + return log_debug_errno(SYNTHETIC_ERRNO(EINVAL), + "Invalid item sent from child."); add = portable_metadata_new(name, fd); if (!add) diff --git a/src/resolve/resolvectl.c b/src/resolve/resolvectl.c index 4581c2e3c8..f3aabcc50e 100644 --- a/src/resolve/resolvectl.c +++ b/src/resolve/resolvectl.c @@ -261,10 +261,9 @@ static int resolve_host(sd_bus *bus, const char *name) { (int) strlen(name), c == 0 ? name : "", c == 0 ? ":" : " ", canonical); - if (c == 0) { - log_error("%s: no addresses found", name); - return -ESRCH; - } + if (c == 0) + return log_error_errno(SYNTHETIC_ERRNO(ESRCH), + "%s: no addresses found", name); print_source(flags, ts); @@ -356,10 +355,9 @@ static int resolve_address(sd_bus *bus, int family, const union in_addr_union *a if (r < 0) return bus_log_parse_error(r); - if (c == 0) { - log_error("%s: no names found", pretty); - return -ESRCH; - } + if (c == 0) + return log_error_errno(SYNTHETIC_ERRNO(ESRCH), + "%s: no names found", pretty); print_source(flags, ts); @@ -2050,10 +2048,10 @@ static int call_domain(sd_bus *bus, char **domain, const BusLocator *locator, sd r = dns_name_is_valid(n); if (r < 0) return log_error_errno(r, "Failed to validate specified domain %s: %m", n); - if (r == 0) { - log_error("Domain not valid: %s", n); - return -EINVAL; - } + if (r == 0) + return log_error_errno(SYNTHETIC_ERRNO(EINVAL), + "Domain not valid: %s", + n); r = sd_bus_message_append(req, "(sb)", n, **p == '~'); if (r < 0) @@ -2348,10 +2346,10 @@ static int verb_nta(int argc, char **argv, void *userdata) { r = dns_name_is_valid(*p); if (r < 0) return log_error_errno(r, "Failed to validate specified domain %s: %m", *p); - if (r == 0) { - log_error("Domain not valid: %s", *p); - return -EINVAL; - } + if (r == 0) + return log_error_errno(SYNTHETIC_ERRNO(EINVAL), + "Domain not valid: %s", + *p); } r = call_nta(bus, clear ? NULL : argv + 2, bus_resolve_mgr, &error); diff --git a/src/resolve/resolved-dns-packet.c b/src/resolve/resolved-dns-packet.c index a82ef388ee..6224039992 100644 --- a/src/resolve/resolved-dns-packet.c +++ b/src/resolve/resolved-dns-packet.c @@ -1435,7 +1435,7 @@ int dns_packet_read_name( n += r; continue; - } else if (allow_compression && (c & 0xc0) == 0xc0) { + } else if (allow_compression && FLAGS_SET(c, 0xc0)) { uint16_t ptr; /* Pointer */ @@ -2250,12 +2250,11 @@ static int dns_packet_extract_answer(DnsPacket *p, DnsAnswer **ret_answer) { if (DNS_PACKET_QR(p)) { /* Additional checks for responses */ - if (!DNS_RESOURCE_RECORD_OPT_VERSION_SUPPORTED(rr)) { + if (!DNS_RESOURCE_RECORD_OPT_VERSION_SUPPORTED(rr)) /* If this is a reply and we don't know the EDNS version * then something is weird... */ - log_debug("EDNS version newer that our request, bad server."); - return -EBADMSG; - } + return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG), + "EDNS version newer that our request, bad server."); if (has_rfc6975) { /* If the OPT RR contains RFC6975 algorithm data, then this diff --git a/src/resolve/resolved-dns-query.c b/src/resolve/resolved-dns-query.c index 3afe4cf73b..1eff893b21 100644 --- a/src/resolve/resolved-dns-query.c +++ b/src/resolve/resolved-dns-query.c @@ -908,13 +908,11 @@ static int dns_query_cname_redirect(DnsQuery *q, const DnsResourceRecord *cname) if (r == 0 && k == 0) /* No actual cname happened? */ return -ELOOP; - if (q->answer_protocol == DNS_PROTOCOL_DNS) { + if (q->answer_protocol == DNS_PROTOCOL_DNS) /* Don't permit CNAME redirects from unicast DNS to LLMNR or MulticastDNS, so that global resources * cannot invade the local namespace. The opposite way we permit: local names may redirect to global * ones. */ - q->flags &= ~(SD_RESOLVED_LLMNR|SD_RESOLVED_MDNS); /* mask away the local protocols */ - } /* Turn off searching for the new name */ q->flags |= SD_RESOLVED_NO_SEARCH; diff --git a/src/resolve/resolved-dnssd.c b/src/resolve/resolved-dnssd.c index 8ffe45e744..d7f3a5adef 100644 --- a/src/resolve/resolved-dnssd.c +++ b/src/resolve/resolved-dnssd.c @@ -97,15 +97,15 @@ static int dnssd_service_load(Manager *manager, const char *filename) { if (r < 0) return r; - if (!service->name_template) { - log_error("%s doesn't define service instance name", service->name); - return -EINVAL; - } + if (!service->name_template) + return log_error_errno(SYNTHETIC_ERRNO(EINVAL), + "%s doesn't define service instance name", + service->name); - if (!service->type) { - log_error("%s doesn't define service type", service->name); - return -EINVAL; - } + if (!service->type) + return log_error_errno(SYNTHETIC_ERRNO(EINVAL), + "%s doesn't define service type", + service->name); if (LIST_IS_EMPTY(service->txt_data_items)) { txt_data = new0(DnssdTxtData, 1); diff --git a/src/resolve/resolved-manager.c b/src/resolve/resolved-manager.c index 908454d756..c197108810 100644 --- a/src/resolve/resolved-manager.c +++ b/src/resolve/resolved-manager.c @@ -786,10 +786,8 @@ int manager_recv(Manager *m, int fd, DnsProtocol protocol, DnsPacket **ret) { l = recvmsg_safe(fd, &mh, 0); if (IN_SET(l, -EAGAIN, -EINTR)) return 0; - if (l < 0) + if (l <= 0) return l; - if (l == 0) - return 0; assert(!(mh.msg_flags & MSG_TRUNC)); diff --git a/src/run/run.c b/src/run/run.c index 214b269569..4179624313 100644 --- a/src/run/run.c +++ b/src/run/run.c @@ -538,14 +538,13 @@ static int parse_argv(int argc, char *argv[]) { arg_aggressive_gc = true; } - if (arg_stdio == ARG_STDIO_AUTO) { + if (arg_stdio == ARG_STDIO_AUTO) /* If we both --pty and --pipe are specified we'll automatically pick --pty if we are connected fully * to a TTY and pick direct fd passing otherwise. This way, we automatically adapt to usage in a shell * pipeline, but we are neatly interactive with tty-level isolation otherwise. */ arg_stdio = isatty(STDIN_FILENO) && isatty(STDOUT_FILENO) && isatty(STDERR_FILENO) ? ARG_STDIO_PTY : ARG_STDIO_DIRECT; - } if (argc > optind) { char **l; @@ -1336,7 +1335,7 @@ static int start_transient_service( if (c.cpu_usage_nsec != NSEC_INFINITY) { char ts[FORMAT_TIMESPAN_MAX]; log_info("CPU time consumed: %s", - format_timespan(ts, sizeof ts, (c.cpu_usage_nsec + NSEC_PER_USEC - 1) / NSEC_PER_USEC, USEC_PER_MSEC)); + format_timespan(ts, sizeof ts, DIV_ROUND_UP(c.cpu_usage_nsec, NSEC_PER_USEC), USEC_PER_MSEC)); } if (c.ip_ingress_bytes != UINT64_MAX) { diff --git a/src/shared/bus-util.c b/src/shared/bus-util.c index 9a306daa03..aa5dadba45 100644 --- a/src/shared/bus-util.c +++ b/src/shared/bus-util.c @@ -266,12 +266,10 @@ int bus_connect_transport(BusTransport transport, const char *host, bool user, s if (user) r = sd_bus_default_user(&bus); else { - if (sd_booted() <= 0) { + if (sd_booted() <= 0) /* Print a friendly message when the local system is actually not running systemd as PID 1. */ - log_error("System has not been booted with systemd as init system (PID 1). Can't operate."); - - return -EHOSTDOWN; - } + return log_error_errno(SYNTHETIC_ERRNO(EHOSTDOWN), + "System has not been booted with systemd as init system (PID 1). Can't operate."); r = sd_bus_default_system(&bus); } break; diff --git a/src/shared/dissect-image.c b/src/shared/dissect-image.c index eaf44f156c..91120d7219 100644 --- a/src/shared/dissect-image.c +++ b/src/shared/dissect-image.c @@ -224,7 +224,7 @@ static int wait_for_partitions_to_appear( if (r < 0) return r; if (r == 0) - return log_debug_errno(EPROTONOSUPPORT, + return log_debug_errno(SYNTHETIC_ERRNO(EPROTONOSUPPORT), "Device is a loop device and partition scanning is off!"); return -EINVAL; /* original error */ diff --git a/src/shared/pkcs11-util.c b/src/shared/pkcs11-util.c index 4088439f93..400cebb66f 100644 --- a/src/shared/pkcs11-util.c +++ b/src/shared/pkcs11-util.c @@ -791,8 +791,8 @@ static int slot_process( rv = m->C_GetTokenInfo(slotid, &token_info); if (rv == CKR_TOKEN_NOT_PRESENT) { - log_debug("Token not present in slot, ignoring."); - return -EAGAIN; + return log_debug_errno(SYNTHETIC_ERRNO(EAGAIN), + "Token not present in slot, ignoring."); } else if (rv != CKR_OK) { log_warning("Failed to acquire token info for slot %lu, ignoring slot: %s", slotid, p11_kit_strerror(rv)); return -EAGAIN; @@ -808,10 +808,10 @@ static int slot_process( return -EAGAIN; } - if (search_uri && !p11_kit_uri_match_token_info(search_uri, &token_info)) { - log_debug("Found non-matching token with URI %s.", token_uri_string); - return -EAGAIN; - } + if (search_uri && !p11_kit_uri_match_token_info(search_uri, &token_info)) + return log_debug_errno(SYNTHETIC_ERRNO(EAGAIN), + "Found non-matching token with URI %s.", + token_uri_string); log_debug("Found matching token with URI %s.", token_uri_string); @@ -876,10 +876,9 @@ static int module_process( log_warning("Failed to get slot list, ignoring module: %s", p11_kit_strerror(rv)); return -EAGAIN; } - if (n_slotids == 0) { - log_debug("This module has no slots? Ignoring module."); - return -EAGAIN; - } + if (n_slotids == 0) + return log_debug_errno(SYNTHETIC_ERRNO(EAGAIN), + "This module has no slots? Ignoring module."); for (k = 0; k < n_slotids; k++) { r = slot_process( diff --git a/src/shared/ptyfwd.c b/src/shared/ptyfwd.c index 305fd8a0f1..6bcdfff41a 100644 --- a/src/shared/ptyfwd.c +++ b/src/shared/ptyfwd.c @@ -473,15 +473,13 @@ int pty_forward_new( f->master = master; - if (ioctl(f->output_fd, TIOCGWINSZ, &ws) < 0) { + if (ioctl(f->output_fd, TIOCGWINSZ, &ws) < 0) /* If we can't get the resolution from the output fd, then use our internal, regular width/height, * i.e. something derived from $COLUMNS and $LINES if set. */ - ws = (struct winsize) { .ws_row = lines(), .ws_col = columns(), }; - } (void) ioctl(master, TIOCSWINSZ, &ws); diff --git a/src/shared/seccomp-util.c b/src/shared/seccomp-util.c index 9e3555d986..75ff2bb7bf 100644 --- a/src/shared/seccomp-util.c +++ b/src/shared/seccomp-util.c @@ -1190,7 +1190,7 @@ int seccomp_restrict_namespaces(unsigned long retain) { } /* NOOP? */ - if ((retain & NAMESPACE_FLAGS_ALL) == NAMESPACE_FLAGS_ALL) + if (FLAGS_SET(retain, NAMESPACE_FLAGS_ALL)) return 0; SECCOMP_FOREACH_LOCAL_ARCH(arch) { @@ -1228,7 +1228,7 @@ int seccomp_restrict_namespaces(unsigned long retain) { unsigned long f; f = namespace_flag_map[i].flag; - if ((retain & f) == f) { + if (FLAGS_SET(retain, f)) { log_debug("Permitting %s.", namespace_flag_map[i].name); continue; } diff --git a/src/systemctl/systemctl-logind.c b/src/systemctl/systemctl-logind.c index cf28ea7d1c..20486c968e 100644 --- a/src/systemctl/systemctl-logind.c +++ b/src/systemctl/systemctl-logind.c @@ -187,11 +187,10 @@ int logind_check_inhibitors(enum action a) { if (c <= 0) return 0; - log_error("Please retry operation after closing inhibitors and logging out other users.\n" - "Alternatively, ignore inhibitors and users with 'systemctl %s -i'.", - action_table[a].verb); - - return -EPERM; + return log_error_errno(SYNTHETIC_ERRNO(EPERM), + "Please retry operation after closing inhibitors and logging out other users.\n" + "Alternatively, ignore inhibitors and users with 'systemctl %s -i'.", + action_table[a].verb); #else return 0; #endif diff --git a/src/test/test-libudev.c b/src/test/test-libudev.c index 3ca8e1b108..2cc679f42e 100644 --- a/src/test/test-libudev.c +++ b/src/test/test-libudev.c @@ -106,7 +106,7 @@ static void test_device_parents(struct udev *udev, const char *syspath) { log_info("/* %s, device %s */", __func__, syspath); device = udev_device_new_from_syspath(udev, syspath); - if (device == NULL) + if (!device) return; log_info("looking at parents"); @@ -142,7 +142,7 @@ static void test_device_subsys_name(struct udev *udev, const char *subsys, const log_info("looking up device: '%s:%s'", subsys, dev); device = udev_device_new_from_subsystem_sysname(udev, subsys, dev); - if (device == NULL) + if (!device) log_warning_errno(errno, "udev_device_new_from_subsystem_sysname: %m"); else print_device(device); @@ -213,7 +213,7 @@ static void test_monitor(struct udev *udev) { for (i = 0; i < fdcount; i++) { if (ev[i].data.fd == fd_udev && ev[i].events & EPOLLIN) { device = udev_monitor_receive_device(udev_monitor); - if (device == NULL) { + if (!device) { printf("no device from socket\n"); continue; } diff --git a/src/tmpfiles/tmpfiles.c b/src/tmpfiles/tmpfiles.c index b9b37008b1..c3c1c6d49a 100644 --- a/src/tmpfiles/tmpfiles.c +++ b/src/tmpfiles/tmpfiles.c @@ -1417,10 +1417,10 @@ static int create_file(Item *i, const char *path) { if (fstat(fd, &stbuf) < 0) return log_error_errno(errno, "stat(%s) failed: %m", path); - if (!S_ISREG(stbuf.st_mode)) { - log_error("%s exists and is not a regular file.", path); - return -EEXIST; - } + if (!S_ISREG(stbuf.st_mode)) + return log_error_errno(SYNTHETIC_ERRNO(EEXIST), + "%s exists and is not a regular file.", + path); st = &stbuf; } else { @@ -1481,10 +1481,10 @@ static int truncate_file(Item *i, const char *path) { fd = openat(dir_fd, bn, O_NOFOLLOW|O_CLOEXEC|O_PATH, i->mode); if (fd < 0) { - if (errno == ENOENT) { - log_error("Cannot create file %s on a read-only file system.", path); - return -EROFS; - } + if (errno == ENOENT) + return log_error_errno(SYNTHETIC_ERRNO(EROFS), + "Cannot create file %s on a read-only file system.", + path); return log_error_errno(errno, "Failed to re-open file %s: %m", path); } @@ -1495,10 +1495,10 @@ static int truncate_file(Item *i, const char *path) { if (fstat(fd, &stbuf) < 0) return log_error_errno(errno, "stat(%s) failed: %m", path); - if (!S_ISREG(stbuf.st_mode)) { - log_error("%s exists and is not a regular file.", path); - return -EEXIST; - } + if (!S_ISREG(stbuf.st_mode)) + return log_error_errno(SYNTHETIC_ERRNO(EEXIST), + "%s exists and is not a regular file.", + path); if (stbuf.st_size > 0) { if (ftruncate(fd, 0) < 0) {