]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
memfd-util: port to pid_get_comm()
authorLennart Poettering <lennart@amutable.com>
Mon, 13 Jul 2026 08:18:04 +0000 (10:18 +0200)
committerLennart Poettering <lennart@amutable.com>
Mon, 13 Jul 2026 13:39:17 +0000 (15:39 +0200)
Let's the common function for querying the local thread name.

This changes the escaping rules when the therad name is not quite
kosher: previously we'd just escape invalid UTF-8 charcaters, now we do
what we usually do: also escape control characters and such, and limit
us to ASCII.

The description generated here is mostly for debug purposes, and process
names should normally not require this escaping anyway (it's mostly
paranoia), hence I think this change in behaviour should be fine, it's
not part of the API in any form.

src/basic/memfd-util.c

index 3257c1b9dd4b0c8fbdbf962fc183fb75b8c3dd0e..54fecd9bdc07976d6ba8eef366aacd68572d780b 100644 (file)
@@ -1,8 +1,6 @@
 /* SPDX-License-Identifier: LGPL-2.1-or-later */
 
-#include <sched.h>
 #include <stdio.h>
-#include <sys/prctl.h>
 #include <sys/stat.h>
 #include <unistd.h>
 
@@ -10,8 +8,8 @@
 #include "errno-util.h"
 #include "fd-util.h"
 #include "memfd-util.h"
+#include "process-util.h"
 #include "string-util.h"
-#include "utf8.h"
 
 int memfd_create_wrapper(const char *name, unsigned mode) {
         unsigned mode_compat;
@@ -36,26 +34,23 @@ int memfd_create_wrapper(const char *name, unsigned mode) {
 
 int memfd_new_full(const char *name, unsigned extra_flags) {
         _cleanup_free_ char *g = NULL;
+        int r;
 
         if (!name) {
-                char pr[TASK_COMM_LEN] = {};
 
                 /* If no name is specified we generate one. We include
                  * a hint indicating our library implementation, and
                  * add the thread name to it */
 
-                assert_se(prctl(PR_GET_NAME, (unsigned long) pr) >= 0);
+                _cleanup_free_ char *comm = NULL;
+                r = pid_get_comm(/* pid= */ 0, &comm);
+                if (r < 0)
+                        return r;
 
-                if (isempty(pr))
+                if (isempty(comm))
                         name = "sd";
                 else {
-                        _cleanup_free_ char *e = NULL;
-
-                        e = utf8_escape_invalid(pr);
-                        if (!e)
-                                return -ENOMEM;
-
-                        g = strjoin("sd-", e);
+                        g = strjoin("sd-", comm);
                         if (!g)
                                 return -ENOMEM;