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.
This commit is contained in:
Zbigniew Jędrzejewski-Szmek 2022-10-07 16:19:21 +02:00
parent 29c45dc434
commit b4096cecff
10 changed files with 44 additions and 25 deletions

View file

@ -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)

View file

@ -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;
}

View file

@ -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)

View file

@ -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);

View file

@ -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");

View file

@ -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;
}

View file

@ -119,8 +119,10 @@
int r;
r = sd_bus_default(&amp;bus);
if (r &lt; 0)
fprintf(stderr, "Failed to allocate bus: %s\n", strerror(-r));
if (r &lt; 0) {
errno = -r;
fprintf(stderr, "Failed to allocate bus: %m\n");
}
}</programlisting>

View file

@ -62,8 +62,10 @@
int r;
r = sd_device_new_from_syspath(&amp;device, "…");
if (r &lt; 0)
fprintf(stderr, "Failed to allocate device: %s\n", strerror(-r));
if (r &lt; 0) {
errno = -r;
fprintf(stderr, "Failed to allocate device: %m\n");
}
}</programlisting>

View file

@ -135,8 +135,10 @@
int r;
r = sd_event_default(&amp;event);
if (r &lt; 0)
fprintf(stderr, "Failed to allocate event loop: %s\n", strerror(-r));
if (r &lt; 0) {
errno = -r;
fprintf(stderr, "Failed to allocate event loop: %m\n");
}
}</programlisting>

View file

@ -116,8 +116,10 @@
int r;
r = sd_login_monitor_new(NULL, &amp;m);
if (r &lt; 0)
fprintf(stderr, "Failed to allocate login monitor object: %s\n", strerror(-r));
if (r &lt; 0) {
errno = -r;
fprintf(stderr, "Failed to allocate login monitor object: %m\n");
}
}</programlisting>