From b484f983fcf85a5b5b7b492626a1432c3f7db5f5 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Mon, 13 Jul 2026 10:18:04 +0200 Subject: [PATCH] memfd-util: port to pid_get_comm() 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 | 21 ++++++++------------- 1 file changed, 8 insertions(+), 13 deletions(-) diff --git a/src/basic/memfd-util.c b/src/basic/memfd-util.c index 3257c1b9dd..54fecd9bdc 100644 --- a/src/basic/memfd-util.c +++ b/src/basic/memfd-util.c @@ -1,8 +1,6 @@ /* SPDX-License-Identifier: LGPL-2.1-or-later */ -#include #include -#include #include #include @@ -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;