SETPROCTITLE(3)

πŸ“¦ version 15.1, in freebsd 14.5, freebsd 15.1

✨ SETPROCTITLE(3) ✨

okay so bestie, this is the setproctitle manual β€” page 3 of the library functions section, from the Debian crew πŸ’… welcome to the group chat, we're gonna make your process title iconic.


πŸŽ€ NAME πŸŽ€

setproctitle, setproctitle_fast β€” set process title. that's it, that's the whole vibe. your process gets to have a name, a little main character label ✨


πŸ¦‹ SYNOPSIS πŸ¦‹

#include <unistd.h>

void
setproctitle(const char *fmt, ...);

void
setproctitle_fast(const char *fmt, ...);

two functions, same fit, slightly different energy. both return void because they're too cool to report back 🫢


🌸 DESCRIPTION 🌸

so the setproctitle() library routine does exactly what it says on the tin: it sets the process title, aka the thing that shows up when you run ps(1). your process is no longer "just some binary", it's someone πŸ’–

the setproctitle_fast() variant is the speedrun version β€” it's optimized for high frequency updates (think a bazillion title changes per second, go off king πŸ”₯). lowkey the tradeoff: it may make the ps(1) command slightly slower, because it doesn't update the kernel's cache of the program arguments. so like, fast for you, slightly sus for ps. ngl that's the deal ✨

the title gets set from the executable's name, followed by the result of a printf(3)-style expansion of the arguments as specified by the fmt argument. so fmt gets interpreted like a printf format string, args and all.

  • if the fmt argument begins with a - character, the executable's name is skipped. no name-tag, just the formatted part. periodt πŸ’…
  • if fmt is NULL, the process title is restored. a little reset, a little factory settings moment πŸŽ€

πŸ’œ EXAMPLES πŸ’œ

say you've got a daemon and you want to announce what it's currently up to β€” literally just:

setproctitle("talking to %s", inet_ntoa(addr));

now ps knows it's talking to somebody. cute, informative, slay 🌈


⭐ SEE ALSO ⭐

go read these next, bestie:

ps(1), w(1), kvm(3), kvm_getargv(3), printf(3), setprogname(3)


🧚 STANDARDS 🧚

bestie, this one's important so pay attention: the setproctitle() function is implicitly non-standard. like, it's not in the standard, it just exists and we deal.

other methods of causing the ps(1) command line to change β€” including copying over the argv[0] string β€” are also implicitly non-portable. so it's not just this function, it's the whole concept. if you're gonna do it, it's preferable to use an operating system supplied setproctitle() if present. use the native one, she's right there πŸ™ˆ

unfortunately, it's possible that there are other calling conventions to other versions of setproctitle(), although none have been found by the author as yet. this is believed to be the predominant convention. so like… there might be other flavors out there, but nobody's found them yet. we think this is the main one πŸ€·β€β™€οΈ

it is thought that the implementation is compatible with other systems, including NetBSD and BSD/OS. so go forth, it's probably fine ✨


🌺 HISTORY 🌺

story time πŸ’€:

  • the setproctitle() function first appeared in FreeBSD 2.2.
  • the setproctitle_fast() function first appeared in FreeBSD 12.

other operating systems have similar functions. everybody's doing it, we're just doing it with style πŸ‘‘


πŸ’– AUTHORS πŸ’–

Peter Wemm <peter@FreeBSD.org> stole the idea from the Sendmail 8.7.3 source code by Eric Allman <eric@sendmail.org>.

and honestly? iconic behavior. credit where it's due, king 🫢


πŸ’€ BUGS πŸ’€

okay bestie this is the part where we get serious for a sec, because this one is a real one 😭

Never pass a string with user-supplied data as a format without using %s. an attacker can put format specifiers in the string to mangle your stack, leading to a possible security hole. red flag, do not pass go 🚨

and this holds true even if the string was built using a function like snprintf(), because the resulting string may still contain user-supplied conversion specifiers for later interpolation by setproctitle(). so snprintf'ing it doesn't save you, bestie, it's still gonna be interpreted πŸ’€

always use the proper secure idiom:

setproctitle("%s", string);

like literally always. no exceptions. "%s" and your string as an argument. that's the whole flex πŸ”₯


that's the page, bestie, you're now a setproctitle girlie πŸ’… this one's from the Debian gang, dated November 28, 2022 β€” go set some iconic process titles, mwah πŸŽ€βœ¨πŸ’œ

πŸ“¦ where this page comes from