hachyderm.io is one of the many independent Mastodon servers you can use to participate in the fediverse.
Hachyderm is a safe space, LGBTQIA+ and BLM, primarily comprised of tech industry professionals world wide. Note that many non-user account types have restrictions - please see our About page.

Administered by:

Server stats:

9.6K
active users

mxk

One of my favorite apparmor trivia bits is, that Linux offers two ways of setting thread names, one is writing to /proc/<pid>/task/<tid>/comm and the other one is a prctl();
The prctl should be preferred for a multitude of reasons, but one of them is, that this doesn't require DAC or MAC permissions on /proc!
(And yet libc being libc uses the first variant for pthread_setname_np() 😩)

Obviously the main reasons why you want to use the prctl() are containers and other environments without /proc/ and the pure fact that open, write, close are 3 syscalls, while prctl is one, without having to do any magic.

@mxk Both glibc and musl will actually uses prctl() when changing the name for pthread_self(). They only use the /proc path when changing the name of another thread. This seems like the correct way to implement it, because the prctl() only works for the calling thread.

The only alternative would be to send a signal to the other thread and then change its name from the calling thread with prctl(). But that would likely be even more overhead than using the open/write/close syscalls.

glibc: elixir.bootlin.com/glibc/glibc

musl: elixir.bootlin.com/musl/v1.2.5

elixir.bootlin.compthread_setname.c - nptl/pthread_setname.c - Glibc source code glibc-2.41.9000 - Bootlin Elixir Cross ReferencerElixir Cross Referencer - source code of Glibc glibc-2.41.9000: nptl/pthread_setname.c