]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
core,logind,systemctl,journald: replace calls to strerror() with setting errno + %m
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Fri, 7 Oct 2022 07:15:44 +0000 (09:15 +0200)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Tue, 11 Oct 2022 14:10:21 +0000 (16:10 +0200)
strerror() is not thread safe and calling it just isn't worth the effort
required to justify why it would be safe in those cases. It's easier to just
use %m which is thread-safe out of the box. I don't think that any of the
changes in the patch cause any functional difference. This is just about
getting rid of calls to strerror() in general.

When we print an error message and fail to format the string, using something
like "(null)" is good enough. This is very very unlikely to happen anyway.

src/core/cgroup.c
src/core/execute.c
src/core/selinux-access.c
src/core/socket.c
src/core/unit-serialize.c
src/journal/journald-server.c
src/systemctl/systemctl-show.c

index 6d40faa48b07ddeb3f521c154e47cea9aa4b23b7..bbd107849ce4f55c01d322321ea185d206eec126 100644 (file)
@@ -398,17 +398,13 @@ static char *format_cgroup_memory_limit_comparison(char *buf, size_t l, Unit *u,
          * In the absence of reliably being able to detect whether memcg swap support is available or not,
          * only complain if the error is not ENOENT. */
         if (r > 0 || IN_SET(r, -ENODATA, -EOWNERDEAD) ||
-            (r == -ENOENT && streq(property_name, "MemorySwapMax"))) {
+            (r == -ENOENT && streq(property_name, "MemorySwapMax")))
                 buf[0] = 0;
-                return buf;
-        }
-
-        if (r < 0) {
-                (void) snprintf(buf, l, " (error getting kernel value: %s)", strerror_safe(r));
-                return buf;
-        }
-
-        (void) snprintf(buf, l, " (different value in kernel: %" PRIu64 ")", kval);
+        else if (r < 0) {
+                errno = -r;
+                (void) snprintf(buf, l, " (error getting kernel value: %m)");
+        } else
+                (void) snprintf(buf, l, " (different value in kernel: %" PRIu64 ")", kval);
 
         return buf;
 }
index 6a4e1e09547444ff9d743324c6b766bb76e75f3b..a2ed66f3cac5b78d1154b2402e8050198df74acf 100644 (file)
@@ -6428,9 +6428,10 @@ static void exec_command_dump(ExecCommand *c, FILE *f, const char *prefix) {
         prefix2 = strjoina(prefix, "\t");
 
         cmd = quote_command_line(c->argv, SHELL_ESCAPE_EMPTY);
+
         fprintf(f,
                 "%sCommand Line: %s\n",
-                prefix, cmd ?: strerror_safe(ENOMEM));
+                prefix, strnull(cmd));
 
         exec_status_dump(&c->exec_status, f, prefix2);
 }
index 848ae246a7f7b075e78ec9d858e9e5278647d557..c69baa8a1a7ac31d9f9898d62fe984a30de8d283 100644 (file)
@@ -137,6 +137,7 @@ _printf_(2, 3) static int log_callback(int type, const char *fmt, ...) {
 }
 
 static int access_init(sd_bus_error *error) {
+        int r;
 
         if (!mac_selinux_use())
                 return 0;
@@ -145,23 +146,20 @@ static int access_init(sd_bus_error *error) {
                 return 1;
 
         if (avc_open(NULL, 0) != 0) {
-                int saved_errno = errno;
-                bool enforce;
+                r = -errno;  /* Save original errno for later */
 
-                enforce = security_getenforce() != 0;
-                log_full_errno(enforce ? LOG_ERR : LOG_WARNING, saved_errno, "Failed to open the SELinux AVC: %m");
+                bool enforce = security_getenforce() != 0;
+                log_full_errno(enforce ? LOG_ERR : LOG_WARNING, r, "Failed to open the SELinux AVC: %m");
 
-                /* If enforcement isn't on, then let's suppress this
-                 * error, and just don't do any AVC checks. The
-                 * warning we printed is hence all the admin will
-                 * see. */
+                /* If enforcement isn't on, then let's suppress this error, and just don't do any AVC checks.
+                 * The warning we printed is hence all the admin will see. */
                 if (!enforce)
                         return 0;
 
-                /* 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_safe(saved_errno));
+                /* Return an access denied error based on the original errno, if we couldn't load the AVC but
+                 * enforcing mode was on, or we couldn't determine whether it is one. */
+                errno = -r;
+                return sd_bus_error_setf(error, SD_BUS_ERROR_ACCESS_DENIED, "Failed to open the SELinux AVC: %m");
         }
 
         selinux_set_callback(SELINUX_CB_AUDIT, (union selinux_callback) { .func_audit = audit_callback });
index 308f84898caec1cfef3dcaa296ec51feb74214e8..2ac041f9d7273bb9838dc850bf864651614a792c 100644 (file)
@@ -739,16 +739,14 @@ static void socket_dump(Unit *u, FILE *f, const char *prefix) {
                 switch (p->type) {
                 case SOCKET_SOCKET: {
                         _cleanup_free_ char *k = NULL;
-                        const char *t;
                         int r;
 
                         r = socket_address_print(&p->address, &k);
-                        if (r < 0)
-                                t = strerror_safe(r);
-                        else
-                                t = k;
-
-                        fprintf(f, "%s%s: %s\n", prefix, listen_lookup(socket_address_family(&p->address), p->address.type), t);
+                        if (r < 0) {
+                                errno = -r;
+                                fprintf(f, "%s%s: %m\n", prefix, listen_lookup(socket_address_family(&p->address), p->address.type));
+                        } else
+                                fprintf(f, "%s%s: %s\n", prefix, listen_lookup(socket_address_family(&p->address), p->address.type), k);
                         break;
                 }
                 case SOCKET_SPECIAL:
index 797bf6c5ce92248e2c66b850ee89605431a197fa..7fe9f3f2314e59e4fcf158c0daec5889d15a7d83 100644 (file)
@@ -849,8 +849,10 @@ void unit_dump(Unit *u, FILE *f, const char *prefix) {
                 fprintf(f,
                         "%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_safe(u->load_error));
+        else if (u->load_state == UNIT_ERROR) {
+                errno = abs(u->load_error);
+                fprintf(f, "%s\tLoad Error Code: %m\n", prefix);
+        }
 
         for (const char *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);
index bfa9f44a832fc71b070bf4f58b1145de7c17e59a..71d7a59bdac6cee80962bf45d77cc0cffaabfcec 100644 (file)
@@ -1094,7 +1094,8 @@ 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_safe(r));
+                errno = -r;
+                xsprintf(buf, "MESSAGE=Entry printing failed: %m");
 
                 n = 3;
                 iovec[n++] = IOVEC_MAKE_STRING("PRIORITY=4");
index 4c7094678c9341db3f32abd0cd84d2b1d37835f0..8d3db98c0a5966e4659f0f77b86597b0d22d4acb 100644 (file)
@@ -670,8 +670,10 @@ 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_safe(i->status_errno));
+        if (i->status_errno > 0) {
+                errno = i->status_errno;
+                printf("      Error: %i (%m)\n", i->status_errno);
+        }
 
         if (i->ip_ingress_bytes != UINT64_MAX && i->ip_egress_bytes != UINT64_MAX)
                 printf("         IP: %s in, %s out\n",