diff options
author | Zbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl> | 2022-10-07 16:19:21 +0200 |
---|---|---|
committer | Zbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl> | 2022-10-11 16:59:00 +0200 |
commit | b4096cecffd13491848b84bc27f00dbee06af52e (patch) | |
tree | f77ff3d40d693e8bd396848179ca09a5af9184be | |
parent | 29c45dc4348e7db61aa80ba1657cbc2d8b1a19ee (diff) | |
download | systemd-b4096cecffd13491848b84bc27f00dbee06af52e.tar.gz |
man: recommend %m over strerror()
The need to set errno is very very ugly, but at least it is thread-safe and
works correctly. Using strerror() is likely to be wrong, so let's not recommend
that. People who do a lot of logging would provide use some wrapper that sets
errno like we do, so nudge people towards %m.
I tested that all the separate .c files compile cleanly.
-rw-r--r-- | man/journal-enumerate-fields.c | 5 | ||||
-rw-r--r-- | man/journal-iterate-foreach.c | 8 | ||||
-rw-r--r-- | man/journal-iterate-unique.c | 8 | ||||
-rw-r--r-- | man/journal-iterate-wait.c | 14 | ||||
-rw-r--r-- | man/journal-stream-fd.c | 5 | ||||
-rw-r--r-- | man/print-unit-path.c | 5 | ||||
-rw-r--r-- | man/sd_bus_new.xml | 6 | ||||
-rw-r--r-- | man/sd_device_ref.xml | 6 | ||||
-rw-r--r-- | man/sd_event_new.xml | 6 | ||||
-rw-r--r-- | man/sd_login_monitor_new.xml | 6 |
10 files changed, 44 insertions, 25 deletions
diff --git a/man/journal-enumerate-fields.c b/man/journal-enumerate-fields.c index 8a35785440..f4b6b7b078 100644 --- a/man/journal-enumerate-fields.c +++ b/man/journal-enumerate-fields.c @@ -1,7 +1,7 @@ /* SPDX-License-Identifier: CC0-1.0 */ +#include <errno.h> #include <stdio.h> -#include <string.h> #include <systemd/sd-journal.h> int main(int argc, char *argv[]) { @@ -11,7 +11,8 @@ int main(int argc, char *argv[]) { r = sd_journal_open(&j, SD_JOURNAL_LOCAL_ONLY); if (r < 0) { - fprintf(stderr, "Failed to open journal: %s\n", strerror(-r)); + errno = -r; + fprintf(stderr, "Failed to open journal: %m\n"); return 1; } SD_JOURNAL_FOREACH_FIELD(j, field) diff --git a/man/journal-iterate-foreach.c b/man/journal-iterate-foreach.c index ae53d467ed..363101d5ba 100644 --- a/man/journal-iterate-foreach.c +++ b/man/journal-iterate-foreach.c @@ -1,7 +1,7 @@ /* SPDX-License-Identifier: CC0-1.0 */ +#include <errno.h> #include <stdio.h> -#include <string.h> #include <systemd/sd-journal.h> int main(int argc, char *argv[]) { @@ -9,7 +9,8 @@ int main(int argc, char *argv[]) { sd_journal *j; r = sd_journal_open(&j, SD_JOURNAL_LOCAL_ONLY); if (r < 0) { - fprintf(stderr, "Failed to open journal: %s\n", strerror(-r)); + errno = -r; + fprintf(stderr, "Failed to open journal: %m\n"); return 1; } SD_JOURNAL_FOREACH(j) { @@ -18,7 +19,8 @@ int main(int argc, char *argv[]) { r = sd_journal_get_data(j, "MESSAGE", (const void **)&d, &l); if (r < 0) { - fprintf(stderr, "Failed to read message field: %s\n", strerror(-r)); + errno = -r; + fprintf(stderr, "Failed to read message field: %m\n"); continue; } diff --git a/man/journal-iterate-unique.c b/man/journal-iterate-unique.c index e9f273c6e5..ac0c638032 100644 --- a/man/journal-iterate-unique.c +++ b/man/journal-iterate-unique.c @@ -1,7 +1,7 @@ /* SPDX-License-Identifier: CC0-1.0 */ +#include <errno.h> #include <stdio.h> -#include <string.h> #include <systemd/sd-journal.h> int main(int argc, char *argv[]) { @@ -12,12 +12,14 @@ int main(int argc, char *argv[]) { r = sd_journal_open(&j, SD_JOURNAL_LOCAL_ONLY); if (r < 0) { - fprintf(stderr, "Failed to open journal: %s\n", strerror(-r)); + errno = -r; + fprintf(stderr, "Failed to open journal: %m\n"); return 1; } r = sd_journal_query_unique(j, "_SYSTEMD_UNIT"); if (r < 0) { - fprintf(stderr, "Failed to query journal: %s\n", strerror(-r)); + errno = -r; + fprintf(stderr, "Failed to query journal: %m\n"); return 1; } SD_JOURNAL_FOREACH_UNIQUE(j, d, l) diff --git a/man/journal-iterate-wait.c b/man/journal-iterate-wait.c index 2b6d8a0fa8..60b3459a18 100644 --- a/man/journal-iterate-wait.c +++ b/man/journal-iterate-wait.c @@ -1,7 +1,7 @@ /* SPDX-License-Identifier: CC0-1.0 */ +#include <errno.h> #include <stdio.h> -#include <string.h> #include <systemd/sd-journal.h> int main(int argc, char *argv[]) { @@ -9,7 +9,8 @@ int main(int argc, char *argv[]) { sd_journal *j; r = sd_journal_open(&j, SD_JOURNAL_LOCAL_ONLY); if (r < 0) { - fprintf(stderr, "Failed to open journal: %s\n", strerror(-r)); + errno = -r; + fprintf(stderr, "Failed to open journal: %m\n"); return 1; } for (;;) { @@ -17,21 +18,24 @@ int main(int argc, char *argv[]) { size_t l; r = sd_journal_next(j); if (r < 0) { - fprintf(stderr, "Failed to iterate to next entry: %s\n", strerror(-r)); + errno = -r; + fprintf(stderr, "Failed to iterate to next entry: %m\n"); break; } if (r == 0) { /* Reached the end, let's wait for changes, and try again */ r = sd_journal_wait(j, (uint64_t) -1); if (r < 0) { - fprintf(stderr, "Failed to wait for changes: %s\n", strerror(-r)); + errno = -r; + fprintf(stderr, "Failed to wait for changes: %m\n"); break; } continue; } r = sd_journal_get_data(j, "MESSAGE", &d, &l); if (r < 0) { - fprintf(stderr, "Failed to read message field: %s\n", strerror(-r)); + errno = -r; + fprintf(stderr, "Failed to read message field: %m\n"); continue; } printf("%.*s\n", (int) l, (const char*) d); diff --git a/man/journal-stream-fd.c b/man/journal-stream-fd.c index 6bc5582189..d59aa14c13 100644 --- a/man/journal-stream-fd.c +++ b/man/journal-stream-fd.c @@ -1,8 +1,8 @@ /* SPDX-License-Identifier: CC0-1.0 */ +#include <errno.h> #include <syslog.h> #include <stdio.h> -#include <string.h> #include <unistd.h> #include <systemd/sd-journal.h> #include <systemd/sd-daemon.h> @@ -12,7 +12,8 @@ int main(int argc, char *argv[]) { FILE *log; fd = sd_journal_stream_fd("test", LOG_INFO, 1); if (fd < 0) { - fprintf(stderr, "Failed to create stream fd: %s\n", strerror(-fd)); + errno = -fd; + fprintf(stderr, "Failed to create stream fd: %m\n"); return 1; } log = fdopen(fd, "w"); diff --git a/man/print-unit-path.c b/man/print-unit-path.c index 41a21aab52..ff52fd28cd 100644 --- a/man/print-unit-path.c +++ b/man/print-unit-path.c @@ -1,7 +1,7 @@ /* SPDX-License-Identifier: CC0-1.0 */ +#include <errno.h> #include <stdio.h> -#include <string.h> #include <unistd.h> #include <sys/types.h> @@ -21,7 +21,8 @@ #define MEMBER "GetUnitByPID" static int log_error(int error, const char *message) { - fprintf(stderr, "%s: %s\n", message, strerror(-error)); + errno = -error; + fprintf(stderr, "%s: %m\n", message); return error; } diff --git a/man/sd_bus_new.xml b/man/sd_bus_new.xml index 41964640b6..b45106168c 100644 --- a/man/sd_bus_new.xml +++ b/man/sd_bus_new.xml @@ -119,8 +119,10 @@ int r; … r = sd_bus_default(&bus); - if (r < 0) - fprintf(stderr, "Failed to allocate bus: %s\n", strerror(-r)); + if (r < 0) { + errno = -r; + fprintf(stderr, "Failed to allocate bus: %m\n"); + } … }</programlisting> diff --git a/man/sd_device_ref.xml b/man/sd_device_ref.xml index c2ba6b1571..2b8512a572 100644 --- a/man/sd_device_ref.xml +++ b/man/sd_device_ref.xml @@ -62,8 +62,10 @@ int r; … r = sd_device_new_from_syspath(&device, "…"); - if (r < 0) - fprintf(stderr, "Failed to allocate device: %s\n", strerror(-r)); + if (r < 0) { + errno = -r; + fprintf(stderr, "Failed to allocate device: %m\n"); + } … }</programlisting> diff --git a/man/sd_event_new.xml b/man/sd_event_new.xml index 352137ec09..8a105a764b 100644 --- a/man/sd_event_new.xml +++ b/man/sd_event_new.xml @@ -135,8 +135,10 @@ int r; … r = sd_event_default(&event); - if (r < 0) - fprintf(stderr, "Failed to allocate event loop: %s\n", strerror(-r)); + if (r < 0) { + errno = -r; + fprintf(stderr, "Failed to allocate event loop: %m\n"); + } … }</programlisting> diff --git a/man/sd_login_monitor_new.xml b/man/sd_login_monitor_new.xml index 081796249d..8118121281 100644 --- a/man/sd_login_monitor_new.xml +++ b/man/sd_login_monitor_new.xml @@ -116,8 +116,10 @@ int r; … r = sd_login_monitor_new(NULL, &m); - if (r < 0) - fprintf(stderr, "Failed to allocate login monitor object: %s\n", strerror(-r)); + if (r < 0) { + errno = -r; + fprintf(stderr, "Failed to allocate login monitor object: %m\n"); + } … }</programlisting> |