SD_BUS_CLOSE(3)
π sd_bus_close(3) β the sd_bus_close manual, bestie edition π
okay so this is the sd_bus_close(3) page, and it's basically the "how to say goodbye to your D-Bus connection without losing messages" saga. grab a strawberry drink π and let's get into it.
β¨ NAME β¨
sd_bus_close, sd_bus_flush, sd_bus_default_flush_close β close and flush a bus connection. that's the whole vibe: shutting things down cleanly. π
π¦ SYNOPSIS π¦
#include <systemd/sd-bus.h>
void sd_bus_close(sd_bus *bus);
int sd_bus_flush(sd_bus *bus);
void sd_bus_default_flush_close(void);
like, look at those signatures. void in, void out, and one little int in the middle that's gonna tell you if things went off. π
πΈ DESCRIPTION πΈ
sd_bus_close() β the actual breakup π
sd_bus_close() disconnects the specified bus connection. and bestie, when this call is invoked and the specified bus object refers to an active connection, it is immediately terminated. like, no negotiation, no "let's talk about it", it's done.
no further messages may be sent or received on it. periodt.
any messages queued in the bus object β both incoming and outgoing β are released. gone. released into the void. π«Ά
but here's the thing: if you invoke it on a NULL bus object, or when the bus connection is already closed, this function executes no operation. it's chill like that. no crash, no drama.
and the big one, the thing everyone gets wrong: this call does not free or unreference the bus object itself. it does not. use sd_bus_unref(3) for that. don't @ me, that's just how it is. π
sd_bus_flush() β the "wait, I still have things to say" π€
sd_bus_flush() synchronously writes out all outgoing queued messages on a bus connection, if there are any. that's it. that's the job.
β οΈ heads up: this function call may block if the peer is not processing bus messages quickly. so if the other side is being slow, you're just gonna sit there. lowkey the price of doing business.
the exit ritual π―οΈ
before a program exits it is usually a good idea to flush any pending messages with sd_bus_flush() and then close connections with sd_bus_close() β this ensures that:
- no unwritten messages are lost π
- no further messages may be queued π«
- all incoming but unprocessed messages are released ποΈ
after both operations have been done, it is a good idea to also drop any remaining references to the bus object so that it may be freed. three steps, one vibe.
and because these three operations are frequently done together, a helper call sd_bus_flush_close_unref(3) is provided that combines them into one. iconic. ate. no notes. π₯
sd_bus_default_flush_close() β the group-chat cleanup π§Ή
sd_bus_default_flush_close() is similar to sd_bus_flush_close_unref(), but it does not take a bus pointer argument. instead, it iterates over any of the "default" buses opened by:
sd_bus_default(3)sd_bus_default_user(3)sd_bus_default_system(3)- and similar calls
it's particularly useful to clean up any buses opened using those calls before the program exits. like closing all your tabs at once. main character energy. β¨
β RETURN VALUE β
on success, sd_bus_flush() returns a non-negative integer. π
on failure, it returns a negative errno-style error code. π
(also shouting out that sd_bus_close() and sd_bus_default_flush_close() return void, so they don't have a return value at all β it's giving "no thoughts, just vibes".)
π Errors π
returned errors may indicate the following problems:
-ECHILDβ the bus connection has been created in a different process, library or module instance. π¬ so like, not your bus, not your business.
π 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. π«Ά
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. π
β οΈ 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 is recommended to only do calls tosetenv()from an early phase of the program when no other threads have been started. fr fr, do your env stuff early and then leave it alone. π
π HISTORY π
sd_bus_close()andsd_bus_flush()were added in version 221.sd_bus_default_flush_close()was added in version 227.
glow-up timeline, honestly. β
π SEE ALSO π
systemd(1), sd-bus(3), sd_bus_unref(3), sd_bus_set_close_on_exit(3)
okay that's the page!! go flush your messages and close your buses responsibly, bestie π this was dropped by systemd 261.3, so if your build says something else, check your version β iykyk πΈπ¦
π¦ 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