SD_MACHINE_GET_CLASS(3)

πŸ’… SD_MACHINE_GET_CLASS(3) β€” bestie edition ✨

okay so this is the sd_machine_get_class page from the sd_machine_get_class section 3 manual, and no cap it's lowkey one of the cutest little queries in all of systemd. it's giving "who dis" but for VMs and containers 🫢


🌸 NAME 🌸

sd_machine_get_class, sd_machine_get_ifindices β€” figure out the class and the network interface indices of a virtual machine or container that's running locally on your machine πŸ’–


✨ SYNOPSIS ✨

#include <systemd/sd-login.h>

int sd_machine_get_class(const char* machine, char **class);

int sd_machine_get_ifindices(const char* machine, int **ret_ifindices);

πŸ¦‹ DESCRIPTION πŸ¦‹

So sd_machine_get_class() is your girl for finding out the class of a locally running virtual machine or container that's registered with systemd-machined.service(8). The string it hands back is either "vm" or "container" β€” that's it, that's the whole vibe check 😌

πŸ’€ bestie please don't forget: the returned string needs to be freed with the libc free(3) call after use. don't leave it hanging, that's rude.

Then there's sd_machine_get_ifindices() β€” she tells you the numeric indices of the network interfaces on the host that are pointing towards the specified locally running virtual machine or container πŸ”₯ the vm or container has to be registered with systemd-machined.service(8) for this to work, periodt.

you can pass the output parameter ret_ifindices as NULL when you don't actually need the output value β€” very "no thoughts head empty" of you, and honestly valid πŸ™ˆ

πŸŽ€ also freeing time: the returned array needs to be freed with the libc free(3) call after use. same energy as before, okay?

πŸ‘‘ RETURN VALUE πŸ‘‘

On success, these functions return a non-negative integer ✨ slay. Specifically, sd_machine_get_ifindices() returns the number of the relevant network interfaces πŸ”’

On failure, these calls return a negative errno-style error code 😭 keep that in mind.

🌈 Errors

Returned errors may indicate the following problems, iykyk:

  • -ENXIO β€” the specified machine does not exist or is currently not running. she's not here, she's offline, she's on a break πŸ’€
  • -EINVAL β€” an input parameter was invalid (out of range, or NULL, where that is not accepted). basically you handed her something she can't work with πŸ™ˆ
  • -ENOMEM β€” memory allocation failed. the vibes ran out of RAM 😭

🧚 NOTES 🧚

The functions described here are available as a shared library, which can be compiled against and linked to with the libsystemd pkg-config(1) file 🫢 icon behaviour, honestly.

⚠️ and here's the serious one, bestie, listen up: the code described here uses getenv(3), which is declared to be not multi-thread-safe. This means that the code calling the functions described here must not call setenv(3) from a parallel thread 🚨

It is recommended to only do calls to setenv() from an early phase of the program when no other threads have been started ⭐ like, get it done first, then start the threads. main character scheduling energy.


πŸŽ€ HISTORY πŸŽ€

sd_machine_get_class() and sd_machine_get_ifindices() were added in version 217 πŸŽ‰ retro queens.


πŸ’œ SEE ALSO πŸ’œ

systemd(1), sd-login(3), systemd-machined.service(8), sd_pid_get_machine_name(3)

go read those next, bestie, they all slay in their own way 🌸


and that's the whole page!! brought to you by systemd 261.3 on the SD_MACHINE_GET_CLASS(3) tip β€” mwah 😘 πŸ’…βœ¨πŸ¦‹

πŸ“¦ where this page comes from