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.
This commit is contained in:
Lennart Poettering
2026-07-13 15:39:17 +02:00
parent 4d84f8fac3
commit b484f983fc
+8 -13
View 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;