Deduplicate error string patterns (#37676)

I wanted to see if moving moving out constant string arguments our of
error messages results in smaller binary sizes. Turns out it does, but
the savings are not consistent. Sometimes we get a few kB in a single
binary, sometimes there is no size change.
This commit is contained in:
Zbigniew Jędrzejewski-Szmek
2025-06-02 15:34:17 +02:00
committed by GitHub
44 changed files with 162 additions and 150 deletions
+1 -1
View File
@@ -344,7 +344,7 @@ static int open_dev_autofs(Manager *m) {
m->dev_autofs_fd = open("/dev/autofs", O_CLOEXEC|O_RDONLY);
if (m->dev_autofs_fd < 0)
return log_error_errno(errno, "Failed to open /dev/autofs: %m");
return log_error_errno(errno, "Failed to open %s: %m", "/dev/autofs");
init_autofs_dev_ioctl(&param);
r = RET_NERRNO(ioctl(m->dev_autofs_fd, AUTOFS_DEV_IOCTL_VERSION, &param));
+1 -1
View File
@@ -626,7 +626,7 @@ static int import_credentials_initrd(ImportCredentialContext *c) {
if (errno == ENOENT)
log_debug_errno(errno, "No credentials passed from initrd.");
else
log_warning_errno(errno, "Failed to open '/run/credentials/@initrd', ignoring: %m");
log_warning_errno(errno, "Failed to open '%s', ignoring: %m", "/run/credentials/@initrd");
return 0;
}
+4 -4
View File
@@ -245,7 +245,7 @@ static int console_setup(void) {
tty_fd = open_terminal("/dev/console", O_RDWR|O_NOCTTY|O_CLOEXEC);
if (tty_fd < 0)
return log_error_errno(tty_fd, "Failed to open /dev/console: %m");
return log_error_errno(tty_fd, "Failed to open %s: %m", "/dev/console");
/* We don't want to force text mode. Plymouth may be showing pictures already from initrd. */
reset_dev_console_fd(tty_fd, /* switch_to_text= */ false);
@@ -1619,7 +1619,7 @@ static void redirect_telinit(int argc, char *argv[]) {
return;
execv(SYSTEMCTL_BINARY_PATH, argv);
log_error_errno(errno, "Failed to exec " SYSTEMCTL_BINARY_PATH ": %m");
log_error_errno(errno, "Failed to execute %s: %m", SYSTEMCTL_BINARY_PATH);
exit(EXIT_FAILURE);
#endif
}
@@ -2604,12 +2604,12 @@ static int do_queue_default_job(
/* Fall back to default.target, which we used to always use by default. Only do this if no
* explicit configuration was given. */
log_info("Falling back to " SPECIAL_DEFAULT_TARGET ".");
log_info("Falling back to %s.", SPECIAL_DEFAULT_TARGET);
r = manager_load_startable_unit_or_warn(m, SPECIAL_DEFAULT_TARGET, NULL, &target);
}
if (r < 0) {
log_info("Falling back to " SPECIAL_RESCUE_TARGET ".");
log_info("Falling back to %s.", SPECIAL_RESCUE_TARGET);
r = manager_load_startable_unit_or_warn(m, SPECIAL_RESCUE_TARGET, NULL, &target);
if (r < 0) {
+1 -1
View File
@@ -497,7 +497,7 @@ static int manager_enable_special_signals(Manager *m) {
fd = open_terminal("/dev/tty0", O_RDWR|O_NOCTTY|O_CLOEXEC);
if (fd < 0)
/* Support systems without virtual console (ENOENT) gracefully */
log_full_errno(fd == -ENOENT ? LOG_DEBUG : LOG_WARNING, fd, "Failed to open /dev/tty0, ignoring: %m");
log_full_errno(fd == -ENOENT ? LOG_DEBUG : LOG_WARNING, fd, "Failed to open %s, ignoring: %m", "/dev/tty0");
else {
/* Enable that we get SIGWINCH on kbrequest */
if (ioctl(fd, KDSIGACCEPT, SIGWINCH) < 0)
+3 -4
View File
@@ -2034,10 +2034,9 @@ static void mount_enumerate_perpetual(Manager *m) {
u = manager_get_unit(m, SPECIAL_ROOT_MOUNT);
if (!u) {
r = unit_new_for_name(m, sizeof(Mount), SPECIAL_ROOT_MOUNT, &u);
if (r < 0) {
log_error_errno(r, "Failed to allocate the special " SPECIAL_ROOT_MOUNT " unit: %m");
return;
}
if (r < 0)
return (void) log_error_errno(r, "Failed to allocate the special %s unit: %m",
SPECIAL_ROOT_MOUNT);
}
u->perpetual = true;
+1 -1
View File
@@ -2264,7 +2264,7 @@ static int apply_mounts(
if (reterr_path)
*reterr_path = strdup("/proc/self/mountinfo");
return log_debug_errno(r, "Failed to open /proc/self/mountinfo: %m");
return log_debug_errno(r, "Failed to open %s: %m", "/proc/self/mountinfo");
}
/* First round, establish all mounts we need */
+3 -4
View File
@@ -705,10 +705,9 @@ static void scope_enumerate_perpetual(Manager *m) {
u = manager_get_unit(m, SPECIAL_INIT_SCOPE);
if (!u) {
r = unit_new_for_name(m, sizeof(Scope), SPECIAL_INIT_SCOPE, &u);
if (r < 0) {
log_error_errno(r, "Failed to allocate the special " SPECIAL_INIT_SCOPE " unit: %m");
return;
}
if (r < 0)
return (void) log_error_errno(r, "Failed to allocate the special %s unit: %m",
SPECIAL_INIT_SCOPE);
}
u->transient = true;
+2 -2
View File
@@ -819,12 +819,12 @@ static int service_setup_bus_name(Service *s) {
if (s->type == SERVICE_DBUS) {
r = unit_add_dependency_by_name(UNIT(s), UNIT_REQUIRES, SPECIAL_DBUS_SOCKET, true, UNIT_DEPENDENCY_FILE);
if (r < 0)
return log_unit_error_errno(UNIT(s), r, "Failed to add dependency on " SPECIAL_DBUS_SOCKET ": %m");
return log_unit_error_errno(UNIT(s), r, "Failed to add dependency on %s: %m", SPECIAL_DBUS_SOCKET);
/* We always want to be ordered against dbus.socket if both are in the transaction. */
r = unit_add_dependency_by_name(UNIT(s), UNIT_AFTER, SPECIAL_DBUS_SOCKET, true, UNIT_DEPENDENCY_FILE);
if (r < 0)
return log_unit_error_errno(UNIT(s), r, "Failed to add dependency on " SPECIAL_DBUS_SOCKET ": %m");
return log_unit_error_errno(UNIT(s), r, "Failed to add dependency on %s: %m", SPECIAL_DBUS_SOCKET);
}
r = unit_watch_bus_name(UNIT(s), s->bus_name);
+62 -55
View File
@@ -13,6 +13,7 @@
#include "alloc-util.h"
#include "dirent-util.h"
#include "errno-util.h"
#include "fd-util.h"
#include "fileio.h"
#include "log.h"
@@ -49,33 +50,34 @@ static int fdopen_unlocked_at(int dfd, const char *dir, const char *name, int *s
static int write_access2_rules(const char *srcdir) {
_cleanup_close_ int load2_fd = -EBADF, change_fd = -EBADF;
_cleanup_closedir_ DIR *dir = NULL;
int dfd = -EBADF, r = 0;
int dfd, r;
load2_fd = open("/sys/fs/smackfs/load2", O_RDWR|O_CLOEXEC|O_NONBLOCK|O_NOCTTY);
if (load2_fd < 0) {
if (errno != ENOENT)
log_warning_errno(errno, "Failed to open '/sys/fs/smackfs/load2': %m");
return -errno; /* negative error */
load2_fd = r = RET_NERRNO(open("/sys/fs/smackfs/load2", O_RDWR|O_CLOEXEC|O_NONBLOCK|O_NOCTTY));
if (r < 0) {
if (r != -ENOENT)
log_warning_errno(r, "Failed to open %s: %m", "/sys/fs/smackfs/load2");
return r;
}
change_fd = open("/sys/fs/smackfs/change-rule", O_RDWR|O_CLOEXEC|O_NONBLOCK|O_NOCTTY);
if (change_fd < 0) {
if (errno != ENOENT)
log_warning_errno(errno, "Failed to open '/sys/fs/smackfs/change-rule': %m");
return -errno; /* negative error */
change_fd = r = RET_NERRNO(open("/sys/fs/smackfs/change-rule", O_RDWR|O_CLOEXEC|O_NONBLOCK|O_NOCTTY));
if (r < 0) {
if (r != -ENOENT)
log_warning_errno(r, "Failed to open %s: %m", "/sys/fs/smackfs/change-rule");
return r;
}
/* write rules to load2 or change-rule from every file in the directory */
dir = opendir(srcdir);
if (!dir) {
if (errno != ENOENT)
log_warning_errno(errno, "Failed to opendir '%s': %m", srcdir);
log_warning_errno(errno, "Failed to open %s/: %m", srcdir);
return errno; /* positive on purpose */
}
dfd = dirfd(dir);
assert(dfd >= 0);
r = 0;
FOREACH_DIRENT(entry, dir, return 0) {
_cleanup_fclose_ FILE *policy = NULL;
@@ -92,7 +94,7 @@ static int write_access2_rules(const char *srcdir) {
q = read_line(policy, NAME_MAX, &buf);
if (q < 0)
return log_error_errno(q, "Failed to read line from '%s': %m", entry->d_name);
return log_error_errno(q, "%s/%s: failed to read line: %m", srcdir, entry->d_name);
if (q == 0)
break;
@@ -102,15 +104,17 @@ static int write_access2_rules(const char *srcdir) {
/* if 3 args -> load rule : subject object access1 */
/* if 4 args -> change rule : subject object access1 access2 */
if (sscanf(buf, "%ms %ms %ms %ms", &sbj, &obj, &acc1, &acc2) < 3) {
log_error_errno(errno, "Failed to parse rule '%s' in '%s', ignoring.", buf, entry->d_name);
log_error_errno(errno, "%s/%s: failed to parse rule '%s', ignoring.",
srcdir, entry->d_name, buf);
continue;
}
if (write(isempty(acc2) ? load2_fd : change_fd, buf, strlen(buf)) < 0) {
if (r == 0)
r = -errno;
log_error_errno(errno, "Failed to write '%s' to '%s' in '%s': %m",
buf, isempty(acc2) ? "/sys/fs/smackfs/load2" : "/sys/fs/smackfs/change-rule", entry->d_name);
q = RET_NERRNO(write(isempty(acc2) ? load2_fd : change_fd, buf, strlen(buf)));
if (q < 0) {
log_error_errno(q, "%s/%s: failed to write '%s' to '%s': %m",
srcdir, entry->d_name,
buf, isempty(acc2) ? "/sys/fs/smackfs/load2" : "/sys/fs/smackfs/change-rule");
RET_GATHER(r, q);
}
}
}
@@ -121,26 +125,27 @@ static int write_access2_rules(const char *srcdir) {
static int write_cipso2_rules(const char *srcdir) {
_cleanup_close_ int cipso2_fd = -EBADF;
_cleanup_closedir_ DIR *dir = NULL;
int dfd = -EBADF, r = 0;
int dfd, r;
cipso2_fd = open("/sys/fs/smackfs/cipso2", O_RDWR|O_CLOEXEC|O_NONBLOCK|O_NOCTTY);
if (cipso2_fd < 0) {
if (errno != ENOENT)
log_warning_errno(errno, "Failed to open '/sys/fs/smackfs/cipso2': %m");
return -errno; /* negative error */
cipso2_fd = r = RET_NERRNO(open("/sys/fs/smackfs/cipso2", O_RDWR|O_CLOEXEC|O_NONBLOCK|O_NOCTTY));
if (r < 0) {
if (r != -ENOENT)
log_warning_errno(r, "Failed to open %s: %m", "/sys/fs/smackfs/cipso2");
return r;
}
/* write rules to cipso2 from every file in the directory */
dir = opendir(srcdir);
if (!dir) {
if (errno != ENOENT)
log_warning_errno(errno, "Failed to opendir '%s': %m", srcdir);
log_warning_errno(errno, "Failed to open %s/: %m", srcdir);
return errno; /* positive on purpose */
}
dfd = dirfd(dir);
assert(dfd >= 0);
r = 0;
FOREACH_DIRENT(entry, dir, return 0) {
_cleanup_fclose_ FILE *policy = NULL;
@@ -157,18 +162,20 @@ static int write_cipso2_rules(const char *srcdir) {
q = read_line(policy, NAME_MAX, &buf);
if (q < 0)
return log_error_errno(q, "Failed to read line from '%s': %m", entry->d_name);
return log_error_errno(q, "%s/%s: failed to read line: %m",
srcdir, entry->d_name);
if (q == 0)
break;
if (isempty(buf) || strchr(COMMENTS, buf[0]))
continue;
if (write(cipso2_fd, buf, strlen(buf)) < 0) {
if (r == 0)
r = -errno;
log_error_errno(errno, "Failed to write '%s' to '/sys/fs/smackfs/cipso2' in '%s': %m",
buf, entry->d_name);
q = RET_NERRNO(write(cipso2_fd, buf, strlen(buf)));
if (q < 0) {
log_error_errno(q, "%s/%s: failed to write '%s' to %s: %m",
srcdir, entry->d_name,
buf, "/sys/fs/smackfs/cipso2");
RET_GATHER(r, q);
break;
}
}
@@ -180,12 +187,12 @@ static int write_cipso2_rules(const char *srcdir) {
static int write_netlabel_rules(const char *srcdir) {
_cleanup_fclose_ FILE *dst = NULL;
_cleanup_closedir_ DIR *dir = NULL;
int dfd = -EBADF, r = 0;
int dfd, r;
dst = fopen("/sys/fs/smackfs/netlabel", "we");
if (!dst) {
if (errno != ENOENT)
log_warning_errno(errno, "Failed to open /sys/fs/smackfs/netlabel: %m");
log_warning_errno(errno, "Failed to open %s/: %m", "/sys/fs/smackfs/netlabel");
return -errno; /* negative error */
}
@@ -193,13 +200,14 @@ static int write_netlabel_rules(const char *srcdir) {
dir = opendir(srcdir);
if (!dir) {
if (errno != ENOENT)
log_warning_errno(errno, "Failed to opendir %s: %m", srcdir);
log_warning_errno(errno, "Failed to open %s/: %m", srcdir);
return errno; /* positive on purpose */
}
dfd = dirfd(dir);
assert(dfd >= 0);
r = 0;
FOREACH_DIRENT(entry, dir, return 0) {
_cleanup_fclose_ FILE *policy = NULL;
@@ -213,21 +221,20 @@ static int write_netlabel_rules(const char *srcdir) {
q = read_line(policy, NAME_MAX, &buf);
if (q < 0)
return log_error_errno(q, "Failed to read line from %s: %m", entry->d_name);
return log_error_errno(q, "%s/%s: failed to read line: %m",
srcdir, entry->d_name);
if (q == 0)
break;
if (!fputs(buf, dst)) {
if (r == 0)
r = -EINVAL;
log_error_errno(errno, "Failed to write line to /sys/fs/smackfs/netlabel: %m");
log_error_errno(errno, "Failed to write line to %s: %m", "/sys/fs/smackfs/netlabel");
RET_GATHER(r, -errno);
break;
}
q = fflush_and_check(dst);
if (q < 0) {
if (r == 0)
r = q;
log_error_errno(q, "Failed to flush writes to /sys/fs/smackfs/netlabel: %m");
log_error_errno(q, "Failed to flush %s: %m", "/sys/fs/smackfs/netlabel");
RET_GATHER(r, q);
break;
}
}
@@ -246,7 +253,7 @@ static int write_onlycap_list(void) {
f = fopen("/etc/smack/onlycap", "re");
if (!f) {
if (errno != ENOENT)
log_warning_errno(errno, "Failed to read '/etc/smack/onlycap': %m");
log_warning_errno(errno, "Failed to open %s: %m", "/etc/smack/onlycap");
return errno == ENOENT ? ENOENT : -errno;
}
@@ -257,7 +264,7 @@ static int write_onlycap_list(void) {
r = read_line(f, LONG_LINE_MAX, &buf);
if (r < 0)
return log_error_errno(r, "Failed to read line from /etc/smack/onlycap: %m");
return log_error_errno(r, "%s: failed to read line: %m", "/etc/smack/onlycap");
if (r == 0)
break;
@@ -277,16 +284,17 @@ static int write_onlycap_list(void) {
list[len - 1] = 0;
onlycap_fd = open("/sys/fs/smackfs/onlycap", O_WRONLY|O_CLOEXEC|O_NONBLOCK|O_NOCTTY);
if (onlycap_fd < 0) {
if (errno != ENOENT)
log_warning_errno(errno, "Failed to open '/sys/fs/smackfs/onlycap': %m");
return -errno; /* negative error */
onlycap_fd = r = RET_NERRNO(open("/sys/fs/smackfs/onlycap", O_WRONLY|O_CLOEXEC|O_NONBLOCK|O_NOCTTY));
if (r < 0) {
if (r != -ENOENT)
log_warning_errno(r, "Failed to open %s: %m", "/sys/fs/smackfs/onlycap");
return r;
}
r = write(onlycap_fd, list, len);
r = RET_NERRNO(write(onlycap_fd, list, len));
if (r < 0)
return log_error_errno(errno, "Failed to write onlycap list(%s) to '/sys/fs/smackfs/onlycap': %m", list);
return log_error_errno(r, "%s: failed to write onlycap list(%s): %m",
"/sys/fs/smackfs/onlycap", list);
return 0;
}
@@ -296,12 +304,11 @@ static int write_onlycap_list(void) {
int mac_smack_setup(bool *loaded_policy) {
#if ENABLE_SMACK
int r;
assert(loaded_policy);
r = write_access2_rules("/etc/smack/accesses.d/");
r = write_access2_rules("/etc/smack/accesses.d");
switch (r) {
case -ENOENT:
log_debug("Smack is not enabled in the kernel.");
@@ -333,7 +340,7 @@ int mac_smack_setup(bool *loaded_policy) {
log_warning_errno(r, "Failed to set SMACK netlabel rule \"127.0.0.1 -CIPSO\": %m");
#endif
r = write_cipso2_rules("/etc/smack/cipso.d/");
r = write_cipso2_rules("/etc/smack/cipso.d");
switch (r) {
case -ENOENT:
log_debug("Smack/CIPSO is not enabled in the kernel.");
@@ -348,7 +355,7 @@ int mac_smack_setup(bool *loaded_policy) {
log_warning_errno(r, "Failed to load Smack/CIPSO access rules, ignoring: %m");
}
r = write_netlabel_rules("/etc/smack/netlabel.d/");
r = write_netlabel_rules("/etc/smack/netlabel.d");
switch (r) {
case -ENOENT:
log_debug("Smack/CIPSO is not enabled in the kernel.");
+1 -1
View File
@@ -1317,7 +1317,7 @@ static void swap_enumerate(Manager *m) {
if (errno == ENOENT)
log_debug_errno(errno, "Not swap enabled, skipping enumeration.");
else
log_warning_errno(errno, "Failed to open /proc/swaps, ignoring: %m");
log_warning_errno(errno, "Failed to open %s, ignoring: %m", "/proc/swaps");
return;
}