SD_BUS_REQUEST_NAME(3)

πŸ’– SD_BUS_REQUEST_NAME(3) πŸ’–

okay bestie, so this is the sd_bus_request_name page from the SD_BUS_REQUEST_NAME(3) manual β€” it's all about grabbing (and letting go of) a well-known service name on a bus. like claiming your username but for D-Bus. no cap, it's iconic.


✨ NAME ✨

sd_bus_request_name, sd_bus_request_name_async, sd_bus_release_name, sd_bus_release_name_async β€” request or release a well-known service name on a bus πŸŽ€


🌸 SYNOPSIS 🌸

#include <systemd/sd-bus.h>

typedef int (*sd_bus_message_handler_t)(sd_bus_message *m,
                                        void *userdata,
                                        sd_bus_error *ret_error);

int sd_bus_request_name(sd_bus *bus, const char *name, uint64_t flags);

int sd_bus_request_name_async(sd_bus *bus, sd_bus_slot **slot,
                              const char *name, uint64_t flags,
                              sd_bus_message_handler_t callback,
                              void *userdata);

int sd_bus_release_name(sd_bus *bus, const char *name);

int sd_bus_release_name_async(sd_bus *bus, sd_bus_slot **slot,
                              const char *name,
                              sd_bus_message_handler_t callback,
                              void *userdata);

πŸ¦‹ DESCRIPTION πŸ¦‹

so sd_bus_request_name() is the main character here πŸ’… β€” it requests a well-known service name on a bus. it takes a bus connection, a valid bus name, and a flags parameter. and that flags parameter? it's a combination of zero or more of these bad boys:

  • SD_BUS_NAME_ALLOW_REPLACEMENT β€” after you've successfully acquired the name, this lets other peers take it over when they try to acquire it with the SD_BUS_NAME_REPLACE_EXISTING flag set. but listen πŸ‘€ β€” if SD_BUS_NAME_ALLOW_REPLACEMENT was not set on the original request, those takeover attempts by other peers get denied. periodt.

Added in version 209. ✨

  • SD_BUS_NAME_REPLACE_EXISTING β€” this one takes over the name if it was already acquired by another peer... and that other peer allowed takeover by setting SD_BUS_NAME_ALLOW_REPLACEMENT while acquiring it. it's giving "respectfully, hand it over" 🫢

Added in version 209. ✨

  • SD_BUS_NAME_QUEUE β€” queues up the acquisition of the name when the name is already taken. like being on the waitlist for a drop, ngl πŸ“

Added in version 209. ✨

sd_bus_request_name() operates in a synchronous fashion: a message requesting the name gets sent to the bus broker, and the call literally just waits until the broker responds. no vibes-only half-measures, it waits.

sd_bus_request_name_async() is the asynchronous version of sd_bus_request_name(). instead of waiting for the request to complete, the request message is enqueued. the specified callback gets called when the broker's response comes in πŸ“¬. if that parameter is specified as NULL, a default implementation is used instead β€” which will terminate the connection when the name cannot be acquired 😭 (yes, dramatic). the function returns a slot object in its slot parameter β€” if it's passed as non-NULL β€” which may be used as a reference to the name request operation. use sd_bus_slot_unref(3) to destroy this reference. and heads up: destroying the reference will not unregister the name, it simply ensures the specified callback is no longer called. iykyk.

sd_bus_release_name() releases an acquired well-known name. it takes a bus connection and a valid bus name as parameters. this one operates synchronously too β€” sends a release request message to the bus broker and waits for it to reply. bye bestie πŸ‘‹

sd_bus_release_name_async() is the asynchronous version of sd_bus_release_name(). the specified callback function gets called when the name has been released successfully πŸŽ‰. if specified as NULL, a generic implementation is used that ignores the result of the operation. as above, the slot (if non-NULL) is set to an object that may be used to reference the operation.

the tea β˜•: these functions are supported only on bus connections β€” meaning connections to a bus broker, NOT direct connections. know the difference, bestie πŸ’€

πŸ”₯ RETURN VALUE πŸ”₯

on success, these calls return 0 or a positive integer. on failure, they return a negative errno-style error code. standard.

if SD_BUS_NAME_QUEUE is specified, sd_bus_request_name() will return 0 when the name is already taken by another peer and the client has been added to the queue for the name. in that case, the caller can subscribe to "NameOwnerChanged" signals to get notified when the name is successfully acquired πŸ””. sd_bus_request_name() returns > 0 when the name has immediately been acquired successfully. slay πŸ’…

πŸ™ˆ Errors

returned errors may indicate the following problems, bestie β€” memorize these fr fr:

  • -EALREADY β€” the caller already is the owner of the specified name. like, you already have it, girl 😭
  • -EEXIST β€” the name has already been acquired by a different peer, and SD_BUS_NAME_REPLACE_EXISTING was not specified, or the other peer did not specify SD_BUS_NAME_ALLOW_REPLACEMENT while acquiring the name.
  • -ESRCH β€” it was attempted to release a name that is currently not registered on the bus.
  • -EADDRINUSE β€” it was attempted to release a name that is owned by a different peer on the bus.
  • -EINVAL β€” a specified parameter is invalid. this is also generated when the requested name is a special service name reserved by the D-Bus specification, or when the operation is requested on a connection that does not refer to a bus.
  • -ENOTCONN β€” the bus connection has been disconnected πŸ’”
  • -ECHILD β€” the bus connection has been created in a different process than the current one.

πŸ‘‘ NOTES πŸ‘‘

functions described here are available as a shared library, which can be compiled against and linked to with the libsystemd pkg-config(1) file πŸŽ€

all functions listed here are thread-agnostic β€” and only a single thread may operate on a given object at any given time. different threads may access the same object at different times. multiple independent objects may be used from different threads in parallel. so like, share nicely ✨

important caveat!! 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's recommended to only do calls to setenv() from an early phase of the program when no other threads have been started. no cap, this one matters πŸ™

🌈 HISTORY 🌈

  • sd_bus_request_name() and sd_bus_release_name() were added in version 209.
  • sd_bus_request_name_async() and sd_bus_release_name_async() were added in version 237.

πŸ’œ SEE ALSO πŸ’œ

systemd(1), sd-bus(3), sd_bus_new(3), sd_bus_slot_unref(3) β€” go read those next, bestie, they're literally so good 🫢


⭐ and that's the whole page, bestie! brought to you by systemd 261.3 β€” the version stamping this whole thing. now go claim your name, main character πŸ‘‘πŸ’–

πŸ“¦ where this page comes from