From 442ac2692a04a171fa2e99ac2405150b7bc38ca7 Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Wed, 3 Jul 2019 23:27:27 +0900 Subject: [PATCH 1/4] sd-event: replace snprintf() with strpcpyf() --- src/libsystemd/sd-event/sd-event.c | 14 ++++++++------ src/test/test-strxcpyx.c | 21 +++++++++++++++++++++ 2 files changed, 29 insertions(+), 6 deletions(-) diff --git a/src/libsystemd/sd-event/sd-event.c b/src/libsystemd/sd-event/sd-event.c index 09285c19d8..5adbceeb02 100644 --- a/src/libsystemd/sd-event/sd-event.c +++ b/src/libsystemd/sd-event/sd-event.c @@ -23,6 +23,7 @@ #include "signal-util.h" #include "string-table.h" #include "string-util.h" +#include "strxcpyx.h" #include "time-util.h" #define DEFAULT_ACCURACY_USEC (250 * USEC_PER_MSEC) @@ -3248,15 +3249,16 @@ _public_ int sd_event_dispatch(sd_event *e) { } static void event_log_delays(sd_event *e) { - char b[ELEMENTSOF(e->delays) * DECIMAL_STR_MAX(unsigned) + 1]; - unsigned i; - int o; + char b[ELEMENTSOF(e->delays) * DECIMAL_STR_MAX(unsigned) + 1], *p; + size_t l, i; - for (i = o = 0; i < ELEMENTSOF(e->delays); i++) { - o += snprintf(&b[o], sizeof(b) - o, "%u ", e->delays[i]); + p = b; + l = sizeof(b); + for (i = 0; i < ELEMENTSOF(e->delays); i++) { + l = strpcpyf(&p, l, "%u ", e->delays[i]); e->delays[i] = 0; } - log_debug("Event loop iterations: %.*s", o, b); + log_debug("Event loop iterations: %s", b); } _public_ int sd_event_run(sd_event *e, uint64_t timeout) { diff --git a/src/test/test-strxcpyx.c b/src/test/test-strxcpyx.c index 21d56d9be6..d7199124a3 100644 --- a/src/test/test-strxcpyx.c +++ b/src/test/test-strxcpyx.c @@ -1,5 +1,6 @@ /* SPDX-License-Identifier: LGPL-2.1+ */ +#include #include #include "string-util.h" @@ -78,6 +79,24 @@ static void test_strscpyl(void) { assert_se(space_left == 10); } +static void test_sd_event_code_migration(void) { + char b[100 * DECIMAL_STR_MAX(unsigned) + 1]; + char c[100 * DECIMAL_STR_MAX(unsigned) + 1], *p; + unsigned i; + size_t l; + int o; + + for (i = o = 0; i < 100; i++) + o += snprintf(&b[o], sizeof(b) - o, "%u ", i); + + p = c; + l = sizeof(c); + for (i = 0; i < 100; i++) + l = strpcpyf(&p, l, "%u ", i); + + assert_se(streq(b, c)); +} + int main(int argc, char *argv[]) { test_strpcpy(); test_strpcpyf(); @@ -85,5 +104,7 @@ int main(int argc, char *argv[]) { test_strscpy(); test_strscpyl(); + test_sd_event_code_migration(); + return 0; } From 7fa6223f67662e8d5ee26092240b4e1d091a69f5 Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Wed, 3 Jul 2019 23:55:45 +0900 Subject: [PATCH 2/4] dhcp: replace strerror() with %m --- src/libsystemd-network/sd-dhcp-client.c | 2 +- src/libsystemd-network/sd-dhcp6-lease.c | 16 ++++------------ 2 files changed, 5 insertions(+), 13 deletions(-) diff --git a/src/libsystemd-network/sd-dhcp-client.c b/src/libsystemd-network/sd-dhcp-client.c index b0f0f84937..85238c21d1 100644 --- a/src/libsystemd-network/sd-dhcp-client.c +++ b/src/libsystemd-network/sd-dhcp-client.c @@ -576,7 +576,7 @@ static void client_stop(sd_dhcp_client *client, int error) { assert(client); if (error < 0) - log_dhcp_client(client, "STOPPED: %s", strerror(-error)); + log_dhcp_client_errno(client, error, "STOPPED: %m"); else if (error == SD_DHCP_CLIENT_EVENT_STOP) log_dhcp_client(client, "STOPPED"); else diff --git a/src/libsystemd-network/sd-dhcp6-lease.c b/src/libsystemd-network/sd-dhcp6-lease.c index 8b424811ad..8aebb53c87 100644 --- a/src/libsystemd-network/sd-dhcp6-lease.c +++ b/src/libsystemd-network/sd-dhcp6-lease.c @@ -205,12 +205,8 @@ int dhcp6_lease_set_dns(sd_dhcp6_lease *lease, uint8_t *optval, size_t optlen) { r = dhcp6_option_parse_ip6addrs(optval, optlen, &lease->dns, lease->dns_count, &lease->dns_allocated); - if (r < 0) { - log_dhcp6_client(client, "Invalid DNS server option: %s", - strerror(-r)); - - return r; - } + if (r < 0) + return log_dhcp6_client_errno(client, r, "Invalid DNS server option: %m"); lease->dns_count = r; @@ -336,12 +332,8 @@ int dhcp6_lease_set_sntp(sd_dhcp6_lease *lease, uint8_t *optval, size_t optlen) r = dhcp6_option_parse_ip6addrs(optval, optlen, &lease->ntp, lease->ntp_count, &lease->ntp_allocated); - if (r < 0) { - log_dhcp6_client(client, "Invalid SNTP server option: %s", - strerror(-r)); - - return r; - } + if (r < 0) + return log_dhcp6_client_errno(client, r, "Invalid SNTP server option: %m"); lease->ntp_count = r; From 4bbccb02eaee35e20d3031e7ba4b3d08631ce934 Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Wed, 3 Jul 2019 23:56:17 +0900 Subject: [PATCH 3/4] tree-wide: introduce strerror_safe() --- src/basic/errno-util.h | 8 ++++++++ src/core/execute.c | 2 +- src/core/path.c | 2 +- src/core/selinux-access.c | 3 ++- src/core/socket.c | 2 +- src/core/unit.c | 2 +- src/journal-remote/journal-gatewayd.c | 5 +++-- src/journal/journalctl.c | 2 +- src/journal/journald-server.c | 2 +- src/libsystemd/sd-bus/bus-error.c | 2 +- src/libsystemd/sd-bus/test-bus-chat.c | 3 ++- src/libsystemd/sd-bus/test-bus-error.c | 3 ++- src/login/logind-seat.c | 3 ++- src/login/pam_systemd.c | 27 +++++++++++++------------- src/shared/journal-importer.c | 3 ++- src/systemctl/systemctl.c | 2 +- src/systemd/sd-daemon.h | 2 +- src/test/test-calendarspec.c | 9 +++++---- src/test/test-path-util.c | 2 +- src/test/test-sleep.c | 9 +++++---- 20 files changed, 55 insertions(+), 38 deletions(-) diff --git a/src/basic/errno-util.h b/src/basic/errno-util.h index d7a5ea771f..3ad732b91e 100644 --- a/src/basic/errno-util.h +++ b/src/basic/errno-util.h @@ -1,6 +1,9 @@ /* SPDX-License-Identifier: LGPL-2.1+ */ #pragma once +#include +#include + #include "macro.h" static inline void _reset_errno_(int *saved_errno) { @@ -28,6 +31,11 @@ static inline int negative_errno(void) { return -errno; } +static inline char *strerror_safe(int error) { + /* 'safe' here does NOT mean thread safety. */ + return strerror(abs(error)); +} + /* Hint #1: ENETUNREACH happens if we try to connect to "non-existing" special IP addresses, such as ::5. * * Hint #2: The kernel sends e.g., EHOSTUNREACH or ENONET to userspace in some ICMP error cases. See the diff --git a/src/core/execute.c b/src/core/execute.c index abf241aa81..1cbb2a83de 100644 --- a/src/core/execute.c +++ b/src/core/execute.c @@ -4891,7 +4891,7 @@ static void exec_command_dump(ExecCommand *c, FILE *f, const char *prefix) { cmd = exec_command_line(c->argv); fprintf(f, "%sCommand Line: %s\n", - prefix, cmd ? cmd : strerror(ENOMEM)); + prefix, cmd ? cmd : strerror_safe(ENOMEM)); exec_status_dump(&c->exec_status, f, prefix2); } diff --git a/src/core/path.c b/src/core/path.c index 5a00150e9b..562cd352a3 100644 --- a/src/core/path.c +++ b/src/core/path.c @@ -89,7 +89,7 @@ int path_spec_watch(PathSpec *s, sd_event_io_handler_t handler) { break; } - r = log_warning_errno(errno, "Failed to add watch on %s: %s", s->path, errno == ENOSPC ? "too many watches" : strerror(-r)); + r = log_warning_errno(errno, "Failed to add watch on %s: %s", s->path, errno == ENOSPC ? "too many watches" : strerror_safe(r)); if (cut) *cut = tmp; goto fail; diff --git a/src/core/selinux-access.c b/src/core/selinux-access.c index c8b1a8588b..960ba6cc6d 100644 --- a/src/core/selinux-access.c +++ b/src/core/selinux-access.c @@ -17,6 +17,7 @@ #include "alloc-util.h" #include "audit-fd.h" #include "bus-util.h" +#include "errno-util.h" #include "format-util.h" #include "log.h" #include "path-util.h" @@ -158,7 +159,7 @@ static int access_init(sd_bus_error *error) { /* Return an access denied error, if we couldn't load * the AVC but enforcing mode was on, or we couldn't * determine whether it is one. */ - return sd_bus_error_setf(error, SD_BUS_ERROR_ACCESS_DENIED, "Failed to open the SELinux AVC: %s", strerror(saved_errno)); + return sd_bus_error_setf(error, SD_BUS_ERROR_ACCESS_DENIED, "Failed to open the SELinux AVC: %s", strerror_safe(saved_errno)); } selinux_set_callback(SELINUX_CB_AUDIT, (union selinux_callback) audit_callback); diff --git a/src/core/socket.c b/src/core/socket.c index b6a4290c3f..cc3dc5973d 100644 --- a/src/core/socket.c +++ b/src/core/socket.c @@ -782,7 +782,7 @@ static void socket_dump(Unit *u, FILE *f, const char *prefix) { r = socket_address_print(&p->address, &k); if (r < 0) - t = strerror(-r); + t = strerror_safe(r); else t = k; diff --git a/src/core/unit.c b/src/core/unit.c index 463db73ff1..210a4a1a6e 100644 --- a/src/core/unit.c +++ b/src/core/unit.c @@ -1326,7 +1326,7 @@ void unit_dump(Unit *u, FILE *f, const char *prefix) { "%s\tMerged into: %s\n", prefix, u->merged_into->id); else if (u->load_state == UNIT_ERROR) - fprintf(f, "%s\tLoad Error Code: %s\n", prefix, strerror(-u->load_error)); + fprintf(f, "%s\tLoad Error Code: %s\n", prefix, strerror_safe(u->load_error)); for (n = sd_bus_track_first(u->bus_track); n; n = sd_bus_track_next(u->bus_track)) fprintf(f, "%s\tBus Ref: %s\n", prefix, n); diff --git a/src/journal-remote/journal-gatewayd.c b/src/journal-remote/journal-gatewayd.c index 734e8c7f33..459d8e86a1 100644 --- a/src/journal-remote/journal-gatewayd.c +++ b/src/journal-remote/journal-gatewayd.c @@ -15,6 +15,7 @@ #include "alloc-util.h" #include "bus-util.h" +#include "errno-util.h" #include "fd-util.h" #include "fileio.h" #include "hostname-util.h" @@ -252,7 +253,7 @@ static ssize_t request_reader_entries( errno = 0; k = fread(buf, 1, n, m->tmp); if (k != n) { - log_error("Failed to read from file: %s", errno ? strerror(errno) : "Premature EOF"); + log_error("Failed to read from file: %s", errno != 0 ? strerror_safe(errno) : "Premature EOF"); return MHD_CONTENT_READER_END_WITH_ERROR; } @@ -605,7 +606,7 @@ static ssize_t request_reader_fields( errno = 0; k = fread(buf, 1, n, m->tmp); if (k != n) { - log_error("Failed to read from file: %s", errno ? strerror(errno) : "Premature EOF"); + log_error("Failed to read from file: %s", errno != 0 ? strerror_safe(errno) : "Premature EOF"); return MHD_CONTENT_READER_END_WITH_ERROR; } diff --git a/src/journal/journalctl.c b/src/journal/journalctl.c index 27518b6363..c25bbc3f26 100644 --- a/src/journal/journalctl.c +++ b/src/journal/journalctl.c @@ -1425,7 +1425,7 @@ static int add_boot(sd_journal *j) { r = get_boots(j, NULL, &boot_id, arg_boot_offset); assert(r <= 1); if (r <= 0) { - const char *reason = (r == 0) ? "No such boot ID in journal" : strerror(-r); + const char *reason = (r == 0) ? "No such boot ID in journal" : strerror_safe(r); if (sd_id128_is_null(arg_boot_id)) log_error("Data from the specified boot (%+i) is not available: %s", diff --git a/src/journal/journald-server.c b/src/journal/journald-server.c index 50ccc80e87..ce0d9ce8c9 100644 --- a/src/journal/journald-server.c +++ b/src/journal/journald-server.c @@ -1061,7 +1061,7 @@ void server_driver_message(Server *s, pid_t object_pid, const char *message_id, /* We failed to format the message. Emit a warning instead. */ char buf[LINE_MAX]; - xsprintf(buf, "MESSAGE=Entry printing failed: %s", strerror(-r)); + xsprintf(buf, "MESSAGE=Entry printing failed: %s", strerror_safe(r)); n = 3; iovec[n++] = IOVEC_MAKE_STRING("PRIORITY=4"); diff --git a/src/libsystemd/sd-bus/bus-error.c b/src/libsystemd/sd-bus/bus-error.c index 96319645d1..386187412b 100644 --- a/src/libsystemd/sd-bus/bus-error.c +++ b/src/libsystemd/sd-bus/bus-error.c @@ -577,7 +577,7 @@ const char *bus_error_message(const sd_bus_error *e, int error) { if (error < 0) error = -error; - return strerror(error); + return strerror_safe(error); } static bool map_ok(const sd_bus_error_map *map) { diff --git a/src/libsystemd/sd-bus/test-bus-chat.c b/src/libsystemd/sd-bus/test-bus-chat.c index 6181fb163e..d9716ae745 100644 --- a/src/libsystemd/sd-bus/test-bus-chat.c +++ b/src/libsystemd/sd-bus/test-bus-chat.c @@ -12,6 +12,7 @@ #include "bus-internal.h" #include "bus-match.h" #include "bus-util.h" +#include "errno-util.h" #include "fd-util.h" #include "format-util.h" #include "log.h" @@ -308,7 +309,7 @@ static void* client1(void *p) { errno = 0; if (read(pp[0], &x, 1) <= 0) { - log_error("Failed to read from pipe: %s", errno ? strerror(errno) : "early read"); + log_error("Failed to read from pipe: %s", errno != 0 ? strerror_safe(errno) : "early read"); goto finish; } diff --git a/src/libsystemd/sd-bus/test-bus-error.c b/src/libsystemd/sd-bus/test-bus-error.c index f464b5b23d..cb5d620993 100644 --- a/src/libsystemd/sd-bus/test-bus-error.c +++ b/src/libsystemd/sd-bus/test-bus-error.c @@ -6,6 +6,7 @@ #include "bus-error.h" #include "bus-util.h" #include "errno-list.h" +#include "errno-util.h" static void test_error(void) { _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL, second = SD_BUS_ERROR_NULL; @@ -88,7 +89,7 @@ static void test_error(void) { assert_se(!sd_bus_error_is_set(&error)); assert_se(sd_bus_error_set_errno(&error, EBUSY) == -EBUSY); assert_se(streq(error.name, "System.Error.EBUSY")); - assert_se(streq(error.message, strerror(EBUSY))); + assert_se(streq(error.message, strerror_safe(EBUSY))); assert_se(sd_bus_error_has_name(&error, "System.Error.EBUSY")); assert_se(sd_bus_error_get_errno(&error) == EBUSY); assert_se(sd_bus_error_is_set(&error)); diff --git a/src/login/logind-seat.c b/src/login/logind-seat.c index dc578adf63..c3ad5f9b5b 100644 --- a/src/login/logind-seat.c +++ b/src/login/logind-seat.c @@ -9,6 +9,7 @@ #include "sd-messages.h" #include "alloc-util.h" +#include "errno-util.h" #include "fd-util.h" #include "fileio.h" #include "format-util.h" @@ -377,7 +378,7 @@ int seat_read_active_vt(Seat *s) { k = read(s->manager->console_active_fd, t, sizeof(t)-1); if (k <= 0) { - log_error("Failed to read current console: %s", k < 0 ? strerror(errno) : "EOF"); + log_error("Failed to read current console: %s", k < 0 ? strerror_safe(errno) : "EOF"); return k < 0 ? -errno : -EIO; } diff --git a/src/login/pam_systemd.c b/src/login/pam_systemd.c index cd070329f4..3f762cbbc3 100644 --- a/src/login/pam_systemd.c +++ b/src/login/pam_systemd.c @@ -21,6 +21,7 @@ #include "bus-internal.h" #include "bus-util.h" #include "cgroup-util.h" +#include "errno-util.h" #include "fd-util.h" #include "fileio.h" #include "format-util.h" @@ -251,7 +252,7 @@ static int append_session_memory_max(pam_handle_t *handle, sd_bus_message *m, co if (streq(limit, "infinity")) { r = sd_bus_message_append(m, "(sv)", "MemoryMax", "t", (uint64_t)-1); if (r < 0) { - pam_syslog(handle, LOG_ERR, "Failed to append to bus message: %s", strerror(-r)); + pam_syslog(handle, LOG_ERR, "Failed to append to bus message: %s", strerror_safe(r)); return r; } } else { @@ -259,7 +260,7 @@ static int append_session_memory_max(pam_handle_t *handle, sd_bus_message *m, co if (r >= 0) { r = sd_bus_message_append(m, "(sv)", "MemoryMaxScale", "u", (uint32_t) (((uint64_t) r * UINT32_MAX) / 1000U)); if (r < 0) { - pam_syslog(handle, LOG_ERR, "Failed to append to bus message: %s", strerror(-r)); + pam_syslog(handle, LOG_ERR, "Failed to append to bus message: %s", strerror_safe(r)); return r; } } else { @@ -267,7 +268,7 @@ static int append_session_memory_max(pam_handle_t *handle, sd_bus_message *m, co if (r >= 0) { r = sd_bus_message_append(m, "(sv)", "MemoryMax", "t", val); if (r < 0) { - pam_syslog(handle, LOG_ERR, "Failed to append to bus message: %s", strerror(-r)); + pam_syslog(handle, LOG_ERR, "Failed to append to bus message: %s", strerror_safe(r)); return r; } } else @@ -290,7 +291,7 @@ static int append_session_tasks_max(pam_handle_t *handle, sd_bus_message *m, con if (r >= 0) { r = sd_bus_message_append(m, "(sv)", "TasksMax", "t", val); if (r < 0) { - pam_syslog(handle, LOG_ERR, "Failed to append to bus message: %s", strerror(-r)); + pam_syslog(handle, LOG_ERR, "Failed to append to bus message: %s", strerror_safe(r)); return r; } } else @@ -310,7 +311,7 @@ static int append_session_cg_weight(pam_handle_t *handle, sd_bus_message *m, con if (r >= 0) { r = sd_bus_message_append(m, "(sv)", field, "t", val); if (r < 0) { - pam_syslog(handle, LOG_ERR, "Failed to append to bus message: %s", strerror(-r)); + pam_syslog(handle, LOG_ERR, "Failed to append to bus message: %s", strerror_safe(r)); return r; } } else if (streq(field, "CPUWeight")) @@ -375,7 +376,7 @@ static bool validate_runtime_directory(pam_handle_t *handle, const char *path, u * up properly for us. */ if (lstat(path, &st) < 0) { - pam_syslog(handle, LOG_ERR, "Failed to stat() runtime directory '%s': %s", path, strerror(errno)); + pam_syslog(handle, LOG_ERR, "Failed to stat() runtime directory '%s': %s", path, strerror_safe(errno)); goto fail; } @@ -549,7 +550,7 @@ _public_ PAM_EXTERN int pam_sm_open_session( r = sd_bus_open_system(&bus); if (r < 0) { - pam_syslog(handle, LOG_ERR, "Failed to connect to system bus: %s", strerror(-r)); + pam_syslog(handle, LOG_ERR, "Failed to connect to system bus: %s", strerror_safe(r)); return PAM_SESSION_ERR; } @@ -574,7 +575,7 @@ _public_ PAM_EXTERN int pam_sm_open_session( "org.freedesktop.login1.Manager", "CreateSession"); if (r < 0) { - pam_syslog(handle, LOG_ERR, "Failed to create CreateSession method call: %s", strerror(-r)); + pam_syslog(handle, LOG_ERR, "Failed to create CreateSession method call: %s", strerror_safe(r)); return PAM_SESSION_ERR; } @@ -593,13 +594,13 @@ _public_ PAM_EXTERN int pam_sm_open_session( remote_user, remote_host); if (r < 0) { - pam_syslog(handle, LOG_ERR, "Failed to append to bus message: %s", strerror(-r)); + pam_syslog(handle, LOG_ERR, "Failed to append to bus message: %s", strerror_safe(r)); return PAM_SESSION_ERR; } r = sd_bus_message_open_container(m, 'a', "(sv)"); if (r < 0) { - pam_syslog(handle, LOG_ERR, "Failed to open message container: %s", strerror(-r)); + pam_syslog(handle, LOG_ERR, "Failed to open message container: %s", strerror_safe(r)); return PAM_SYSTEM_ERR; } @@ -621,7 +622,7 @@ _public_ PAM_EXTERN int pam_sm_open_session( r = sd_bus_message_close_container(m); if (r < 0) { - pam_syslog(handle, LOG_ERR, "Failed to close message container: %s", strerror(-r)); + pam_syslog(handle, LOG_ERR, "Failed to close message container: %s", strerror_safe(r)); return PAM_SYSTEM_ERR; } @@ -648,7 +649,7 @@ _public_ PAM_EXTERN int pam_sm_open_session( &vtnr, &existing); if (r < 0) { - pam_syslog(handle, LOG_ERR, "Failed to parse message: %s", strerror(-r)); + pam_syslog(handle, LOG_ERR, "Failed to parse message: %s", strerror_safe(r)); return PAM_SESSION_ERR; } @@ -759,7 +760,7 @@ _public_ PAM_EXTERN int pam_sm_close_session( r = sd_bus_open_system(&bus); if (r < 0) { - pam_syslog(handle, LOG_ERR, "Failed to connect to system bus: %s", strerror(-r)); + pam_syslog(handle, LOG_ERR, "Failed to connect to system bus: %s", strerror_safe(r)); return PAM_SESSION_ERR; } diff --git a/src/shared/journal-importer.c b/src/shared/journal-importer.c index 218fbe9057..44032548ad 100644 --- a/src/shared/journal-importer.c +++ b/src/shared/journal-importer.c @@ -4,6 +4,7 @@ #include #include "alloc-util.h" +#include "errno-util.h" #include "escape.h" #include "fd-util.h" #include "io-util.h" @@ -422,7 +423,7 @@ int journal_importer_push_data(JournalImporter *imp, const char *data, size_t si "Failed to store received data of size %zu " "(in addition to existing %zu bytes with %zu filled): %s", size, imp->size, imp->filled, - strerror(ENOMEM)); + strerror_safe(ENOMEM)); memcpy(imp->buf + imp->filled, data, size); imp->filled += size; diff --git a/src/systemctl/systemctl.c b/src/systemctl/systemctl.c index dbe442d7da..2fd282e307 100644 --- a/src/systemctl/systemctl.c +++ b/src/systemctl/systemctl.c @@ -4500,7 +4500,7 @@ static void print_status_info( if (i->status_text) printf(" Status: \"%s\"\n", i->status_text); if (i->status_errno > 0) - printf(" Error: %i (%s)\n", i->status_errno, strerror(i->status_errno)); + printf(" Error: %i (%s)\n", i->status_errno, strerror_safe(i->status_errno)); if (i->ip_ingress_bytes != (uint64_t) -1 && i->ip_egress_bytes != (uint64_t) -1) { char buf_in[FORMAT_BYTES_MAX], buf_out[FORMAT_BYTES_MAX]; diff --git a/src/systemd/sd-daemon.h b/src/systemd/sd-daemon.h index 62b0f723c7..860961e2ba 100644 --- a/src/systemd/sd-daemon.h +++ b/src/systemd/sd-daemon.h @@ -260,7 +260,7 @@ int sd_notify(int unset_environment, const char *state); sd_notifyf(0, "STATUS=Failed to start up: %s\n" "ERRNO=%i", - strerror(errno), + strerror_safe(errno), errno); See sd_notifyf(3) for more information. diff --git a/src/test/test-calendarspec.c b/src/test/test-calendarspec.c index 10c9f63b9b..46358ef952 100644 --- a/src/test/test-calendarspec.c +++ b/src/test/test-calendarspec.c @@ -4,6 +4,7 @@ #include "alloc-util.h" #include "calendarspec.h" +#include "errno-util.h" #include "string-util.h" #include "util.h" @@ -23,7 +24,7 @@ static void test_one(const char *input, const char *output) { u = now(CLOCK_REALTIME); r = calendar_spec_next_usec(c, u, &u); - printf("Next: %s\n", r < 0 ? strerror(-r) : format_timestamp(buf, sizeof(buf), u)); + printf("Next: %s\n", r < 0 ? strerror_safe(r) : format_timestamp(buf, sizeof(buf), u)); calendar_spec_free(c); assert_se(calendar_spec_from_string(p, &c) >= 0); @@ -56,7 +57,7 @@ static void test_next(const char *input, const char *new_tz, usec_t after, usec_ u = after; r = calendar_spec_next_usec(c, after, &u); - printf("At: %s\n", r < 0 ? strerror(-r) : format_timestamp_us(buf, sizeof buf, u)); + printf("At: %s\n", r < 0 ? strerror_safe(r) : format_timestamp_us(buf, sizeof buf, u)); if (expect != (usec_t)-1) assert_se(r >= 0 && u == expect); else @@ -103,10 +104,10 @@ static void test_hourly_bug_4031(void) { assert_se((r = calendar_spec_next_usec(c, n, &u)) >= 0); printf("Now: %s (%"PRIu64")\n", format_timestamp_us(buf, sizeof buf, n), n); - printf("Next hourly: %s (%"PRIu64")\n", r < 0 ? strerror(-r) : format_timestamp_us(buf, sizeof buf, u), u); + printf("Next hourly: %s (%"PRIu64")\n", r < 0 ? strerror_safe(r) : format_timestamp_us(buf, sizeof buf, u), u); assert_se((r = calendar_spec_next_usec(c, u, &w)) >= 0); - printf("Next hourly: %s (%"PRIu64")\n", r < 0 ? strerror(-r) : format_timestamp_us(zaf, sizeof zaf, w), w); + printf("Next hourly: %s (%"PRIu64")\n", r < 0 ? strerror_safe(r) : format_timestamp_us(zaf, sizeof zaf, w), w); assert_se(n < u); assert_se(u <= n + USEC_PER_HOUR); diff --git a/src/test/test-path-util.c b/src/test/test-path-util.c index 3e91c87eea..90a8d5f36c 100644 --- a/src/test/test-path-util.c +++ b/src/test/test-path-util.c @@ -464,7 +464,7 @@ static void test_path_extract_filename_one(const char *input, const char *output int r; r = path_extract_filename(input, &k); - log_info("%s → %s/%s [expected: %s/%s]", strnull(input), strnull(k), strerror(-r), strnull(output), strerror(-ret)); + log_info("%s → %s/%s [expected: %s/%s]", strnull(input), strnull(k), strerror_safe(r), strnull(output), strerror_safe(ret)); assert_se(streq_ptr(k, output)); assert_se(r == ret); } diff --git a/src/test/test-sleep.c b/src/test/test-sleep.c index 2a23bf2384..014b1aa7a2 100644 --- a/src/test/test-sleep.c +++ b/src/test/test-sleep.c @@ -7,6 +7,7 @@ #include #include +#include "errno-util.h" #include "fd-util.h" #include "log.h" #include "memory-util.h" @@ -95,13 +96,13 @@ static void test_sleep(void) { log_info("/= running system =/"); r = can_sleep("suspend"); - log_info("Suspend configured and possible: %s", r >= 0 ? yes_no(r) : strerror(-r)); + log_info("Suspend configured and possible: %s", r >= 0 ? yes_no(r) : strerror_safe(r)); r = can_sleep("hibernate"); - log_info("Hibernation configured and possible: %s", r >= 0 ? yes_no(r) : strerror(-r)); + log_info("Hibernation configured and possible: %s", r >= 0 ? yes_no(r) : strerror_safe(r)); r = can_sleep("hybrid-sleep"); - log_info("Hybrid-sleep configured and possible: %s", r >= 0 ? yes_no(r) : strerror(-r)); + log_info("Hybrid-sleep configured and possible: %s", r >= 0 ? yes_no(r) : strerror_safe(r)); r = can_sleep("suspend-then-hibernate"); - log_info("Suspend-then-Hibernate configured and possible: %s", r >= 0 ? yes_no(r) : strerror(-r)); + log_info("Suspend-then-Hibernate configured and possible: %s", r >= 0 ? yes_no(r) : strerror_safe(r)); } int main(int argc, char* argv[]) { From d14be488a7de58cbd16d19f62f0894f60e63b027 Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Thu, 4 Jul 2019 00:09:31 +0900 Subject: [PATCH 4/4] firewall-util: use fixed size array for chain name xt_chainlabel is defined as ``` typedef char xt_chainlabel[32]; ``` --- src/shared/firewall-util.c | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/src/shared/firewall-util.c b/src/shared/firewall-util.c index 87bc3f76fe..93bdd8c7f1 100644 --- a/src/shared/firewall-util.c +++ b/src/shared/firewall-util.c @@ -90,6 +90,7 @@ int fw_add_masquerade( const union in_addr_union *destination, unsigned destination_prefixlen) { + static const xt_chainlabel chain = "POSTROUTING"; _cleanup_(iptc_freep) struct xtc_handle *h = NULL; struct ipt_entry *entry, *mask; struct ipt_entry_target *t; @@ -133,15 +134,15 @@ int fw_add_masquerade( memset(mask, 0xFF, sz); if (add) { - if (iptc_check_entry("POSTROUTING", entry, (unsigned char*) mask, h)) + if (iptc_check_entry(chain, entry, (unsigned char*) mask, h)) return 0; if (errno != ENOENT) /* if other error than not existing yet, fail */ return -errno; - if (!iptc_insert_entry("POSTROUTING", entry, 0, h)) + if (!iptc_insert_entry(chain, entry, 0, h)) return -errno; } else { - if (!iptc_delete_entry("POSTROUTING", entry, (unsigned char*) mask, h)) { + if (!iptc_delete_entry(chain, entry, (unsigned char*) mask, h)) { if (errno == ENOENT) /* if it's already gone, all is good! */ return 0; @@ -169,6 +170,7 @@ int fw_add_local_dnat( uint16_t remote_port, const union in_addr_union *previous_remote) { + static const xt_chainlabel chain_pre = "PREROUTING", chain_output = "OUTPUT"; _cleanup_(iptc_freep) struct xtc_handle *h = NULL; struct ipt_entry *entry, *mask; struct ipt_entry_target *t; @@ -275,11 +277,11 @@ int fw_add_local_dnat( if (add) { /* Add the PREROUTING rule, if it is missing so far */ - if (!iptc_check_entry("PREROUTING", entry, (unsigned char*) mask, h)) { + if (!iptc_check_entry(chain_pre, entry, (unsigned char*) mask, h)) { if (errno != ENOENT) return -EINVAL; - if (!iptc_insert_entry("PREROUTING", entry, 0, h)) + if (!iptc_insert_entry(chain_pre, entry, 0, h)) return -errno; } @@ -287,7 +289,7 @@ int fw_add_local_dnat( if (previous_remote && previous_remote->in.s_addr != remote->in.s_addr) { mr->range[0].min_ip = mr->range[0].max_ip = previous_remote->in.s_addr; - if (!iptc_delete_entry("PREROUTING", entry, (unsigned char*) mask, h)) { + if (!iptc_delete_entry(chain_pre, entry, (unsigned char*) mask, h)) { if (errno != ENOENT) return -errno; } @@ -305,11 +307,11 @@ int fw_add_local_dnat( entry->ip.invflags = IPT_INV_DSTIP; } - if (!iptc_check_entry("OUTPUT", entry, (unsigned char*) mask, h)) { + if (!iptc_check_entry(chain_output, entry, (unsigned char*) mask, h)) { if (errno != ENOENT) return -errno; - if (!iptc_insert_entry("OUTPUT", entry, 0, h)) + if (!iptc_insert_entry(chain_output, entry, 0, h)) return -errno; } @@ -317,14 +319,14 @@ int fw_add_local_dnat( if (previous_remote && previous_remote->in.s_addr != remote->in.s_addr) { mr->range[0].min_ip = mr->range[0].max_ip = previous_remote->in.s_addr; - if (!iptc_delete_entry("OUTPUT", entry, (unsigned char*) mask, h)) { + if (!iptc_delete_entry(chain_output, entry, (unsigned char*) mask, h)) { if (errno != ENOENT) return -errno; } } } } else { - if (!iptc_delete_entry("PREROUTING", entry, (unsigned char*) mask, h)) { + if (!iptc_delete_entry(chain_pre, entry, (unsigned char*) mask, h)) { if (errno != ENOENT) return -errno; } @@ -336,7 +338,7 @@ int fw_add_local_dnat( entry->ip.invflags = IPT_INV_DSTIP; } - if (!iptc_delete_entry("OUTPUT", entry, (unsigned char*) mask, h)) { + if (!iptc_delete_entry(chain_output, entry, (unsigned char*) mask, h)) { if (errno != ENOENT) return -errno; }