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 theSD_BUS_NAME_REPLACE_EXISTINGflag set. but listen π β ifSD_BUS_NAME_ALLOW_REPLACEMENTwas 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 settingSD_BUS_NAME_ALLOW_REPLACEMENTwhile 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, andSD_BUS_NAME_REPLACE_EXISTINGwas not specified, or the other peer did not specifySD_BUS_NAME_ALLOW_REPLACEMENTwhile 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 usesgetenv(3), which is declared to be not multi-thread-safe. this means that the code calling the functions described here must not callsetenv(3)from a parallel thread. it's recommended to only do calls tosetenv()from an early phase of the program when no other threads have been started. no cap, this one matters π
π HISTORY π
sd_bus_request_name()andsd_bus_release_name()were added in version 209.sd_bus_request_name_async()andsd_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
- ubuntu stonking: libsystemd-dev 261.3-0ubuntu3 Β· LGPL-2.1-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms