The freedesktop.org project also developed afree and open-sourcesoftware library called libdbus, as areference implementationof the specification. This library should not be confused with D-Bus itself, as other implementations of the D-Bus specification also exist, such as GDBus (GNOME),[9]QtDBus (Qt/KDE),[10]dbus-java[11]and sd-bus (part ofsystemd).[12]
D-Bus is aninter-process communication(IPC) mechanism initially designed to replace thesoftware componentcommunications systems used by theGNOMEandKDELinuxdesktop environments(CORBAandDCOPrespectively).[13][14]The components of these desktop environments are normally distributed in many processes, each one providing only a few—usually one—services.These services may be used by regular clientapplicationsor by other components of the desktop environment to perform their tasks.[15]
Processes without D-Bus
The same processes with D-Bus
Large groups of cooperating processes demand a dense mesh of individual communication channels (using one-to-one IPC methods) between them. D-Bus simplifies the IPC requirements with one single shared channel.
D-Bus provides asoftware-busabstractionthat gathers all the communications between a group of processes over a single shared virtual channel.[6]Processes connected to a bus do not know how it is internally implemented, but D-Bus specification guarantees that all processes connected to the bus can communicate with each other through it. D-Bus incurs at least a 2.5x performance loss over one-to-one IPC.[16]
Linux desktop environments take advantage of the D-Bus facilities by instantiating multiple buses, notably:[17][6][18]
a singlesystem bus,available to all users and processes of the system, that provides access to system services (i.e. services provided by theoperating systemand also by any systemdaemons)
asession busfor each user login session, that provides desktop services to user applications in the same desktop session, and allows the integration of the desktop session as a whole
A process can connect to any number of buses, provided that it has been granted access to them. In practice, this means that any user process can connect to the system bus and to its current session bus, but not to another user's session buses, or even to a different session bus owned by the same user. The latter restriction may change in the future if all user sessions are combined into a single user bus.[19]
D-Bus provides additional or simplifies existing functionality to the applications, including information-sharing, modularity andprivilege separation.For example, information on an incoming voice-call received throughBluetoothorSkypecan be propagated and interpreted by any currently-running music player, which can react by muting the volume or by pausing playback until the call is finished.[20]
D-Bus can also be used as aframeworkto integrate different components of a user application. For instance, anoffice suitecan communicate through the session bus to share data between aword processorand aspreadsheet.
Every connection to a bus is identified in the context of D-Bus by what is called abus name.[5]A bus name consists of two or more dot-separated strings of letters, digits, dashes, and underscores—areverse domain name.An example of a valid bus name isorg.freedesktop.NetworkManager.[6]
When a process sets up a connection to a bus, the bus assigns to the connection a special bus name calledunique connection name.[18][6]Bus names of this type are immutable—it is guaranteed they will not change as long as the connection exists—and, more importantly, they cannot be reused during the bus lifetime.[5][18][6]This means that no other connection to that bus will ever have assigned such unique connection name, even if the same process closes down the connection to the bus and creates a new one. Unique connection names are easily recognizable because they start with the otherwise forbidden colon character.[18][6]An example of a unique connection name is:1.1553(the characters after the colon have no particular meaning[18]).
A process can ask for additional bus names for its connection,[18]provided that any requested name is not already being used by another connection to the bus. In D-Bus parlance, when a bus name is assigned to a connection, it is said the connectionownsthe bus name.[5][18]In that sense, a bus name cannot be owned by two connections at the same time, but, unlike unique connection names, these names can be reused if they are available: a process may reclaim a bus name released—purposely or not—by another process.[5][6]
The idea behind these additional bus names, commonly calledwell-known names,is to provide a way to refer to a service using a prearranged bus name.[18][6]For instance, the service that reports the current time and date in the system bus lies in the process whose connection owns theorg.freedesktop.timedate1bus name, regardless of which process it is.
Bus names can be used as a simple way to implement single-instance applications (second instances detect that the bus name is already taken).[18]It can also be used to track a service process lifecycle, since the bus sends a notification when a bus name is released due to a process termination.[18]
Because of its original conception as a replacement for several component oriented communications systems, D-Bus shares with its predecessors an object model in which to express the semantics of the communications between clients and services. The terms used in the D-Bus object model mimic those used by someobject orientedprogramming languages.That does not mean that D-Bus is somehow limited to OOP languages—in fact, the most used implementation (libdbus) is written inC,aprocedural programminglanguage.
Browsing the existing bus names, objects, interfaces, methods and signals in a D-Bus bus using D-Feet
In D-Bus, a process offers its services by exposingobjects.These objects havemethodsthat can be invoked, andsignalsthat the object can emit.[18]Methods and signals are collectively referred to as themembersof the object.[5]Any client connected to the bus can interact with an object by using its methods, making requests or commanding the object to perform actions.[18]For instance, an object representing a time service can be queried by a client using a method that returns the current date and time. A client can also listen to signals that an object emits when its state changes due to certain events, usually related to the underlying service. An example would be when a service that manages hardware devices—such as USB or network drivers—signals a "new hardware device added" event. Clients should instruct the bus that they are interested in receiving certain signals from a particular object, since a D-Bus bus only passes signals to those processes with a registered interest in them.[6]
A process connected to a D-Bus bus can request it toexportas many D-Bus objects as it wants. Each object is identified by anobject path,a string of numbers, letters and underscores separated and prefixed by the slash character, called that because of their resemblance toUnix filesystem paths.[5][18]The object path is selected by the requesting process, and must be unique in the context of that bus connection. An example of a valid object path is/org/kde/kspread/sheets/3/cells/4/5.[18]However, it is not enforced—but also not discouraged—to form hierarchies within object paths.[6]The particular naming convention for the objects of a service is entirely up to the developers of such service, but many developers choose tonamespacethem using the reserveddomain nameof the project as a prefix (e.g./org/kde).[18]
Every object is inextricably associated to the particular bus connection where it was exported, and, from the D-Bus point of view, only lives in the context of such connection. Therefore, in order to be able to use a certain service, a client must indicate not only the object path providing the desired service, but also the bus name under which the service process is connected to the bus.[5]This in turn allows that several processes connected to the bus can export different objects with identical object paths unambiguously.
Aninterfacespecifies members—methods and signals—that can be used with an object.[18]It is a set of declarations of methods (including its passing and returning parameters) and signals (including its parameters) identified by a dot-separated name resembling theJava languageinterfaces notation.[18][6]An example of a valid interface name isorg.freedesktop.Introspectable.[6]Despite their similarity, interface names and bus names should not be mistaken. A D-Bus object canimplementseveral interfaces, but at least must implement one, providing support for every method and signal defined by it. The combination of all interfaces implemented by an object is called the objecttype.[5][18]
When using an object, it is a good practice for the client process to provide the member's interface name besides the member's name, but is only mandatory when there is an ambiguity caused by duplicated member names available from different interfaces implemented by the object[5][18]—otherwise, the selected member is undefined or erroneous. An emitted signal, on the other hand, must always indicate to which interface it belongs.
The D-Bus specification also defines several standard interfaces that objects may want to implement in addition to its own interfaces.[17]Although technically optional, most D-Bus service developers choose to support them in their exported objects since they offer important additional features to D-Bus clients, such asintrospection.[6]These standard interfaces are:[17][6]
org.freedesktop.DBus.Peer:provides a way to test if a D-Bus connection is alive.[6]
org.freedesktop.DBus.Introspectable:provides an introspection mechanism by which a client process can, at run-time, get a description (inXMLformat) of the interfaces, methods and signals that the object implements.[18][17]
org.freedesktop.DBus.Properties:allows a D-Bus object to expose the underlying native objectpropertiesor attributes, or simulate them if it does not exist.[17]
org.freedesktop.DBus.ObjectManager:when a D-Bus service arranges its objects hierarchically, this interface provides a way to query an object about all sub-objects under its path, as well as their interfaces and properties, using a single method call.[17]
The D-Bus specification defines a number of administrative bus operations (called "bus services" ) to be performed using the/org/freedesktop/DBusobject that resides in theorg.freedesktop.DBusbus name.[17]Each bus reserves this special bus name for itself, and manages any requests made specifically to this combination of bus name and object path. The administrative operations provided by the bus are those defined by the object's interfaceorg.freedesktop.DBus.These operations are used for example to provide information about the status of the bus,[5]or to manage the request and release of additionalwell-knownbus names.[17][6]
D-Bus was conceived as a generic, high-level inter-process communication system. To accomplish such goals, D-Bus communications are based on the exchange ofmessagesbetween processes instead of "raw bytes".[5][18]D-Bus messages are high-level discrete items that a process can send through the bus to another connected process. Messages have a well-defined structure (even the types of the data carried in their payload are defined), allowing the bus to validate them and to reject any ill-formed message.
In this regard, D-Bus is closer to anRPCmechanism than to a classic IPC mechanism, with its own type definition system and its ownmarshaling.[5]
Example of one-to-one request-response message exchange to invoke a method over D-Bus. Here the client process invokes theSetFoo()method of the/org/example/object1object from the service process namedorg.example.foo(or:1.14) in the bus.
The bus supports two modes of interchanging messages between a client and a service process[5]:
One-to-onerequest-response:This is the way for a client to invoke an object's method. The client sends a message to the service process exporting the object, and the service in turn replies with a message back to the client process.[18]The message sent by the client must contain the object path, the name of the invoked method (and optionally the name of its interface), and the values of the input parameters (if any) as defined by the object's selected interface. The reply message carries the result of the request, including the values of the output parameters returned by the object's method invocation, orexceptioninformation if there was an error.[5][18]
Publish/subscribe:This is the way for an object to announce the occurrence of a signal to the interested parties. The object's service process broadcasts a message that the bus passes only to the connected clients subscribed to the object's signal.[18]The message carries the object path, the name of the signal, the interface to which the signal belongs, and also the values of the signal's parameters (if any). The communication is one-way: there are no response messages to the original message from any client process, since the sender knows neither the identities nor the number of the recipients.[5][18]
Every D-Bus message consists of a header and a body.[18]The header is formed by several fields that identify the type of message, the sender, as well as information required to deliver the message to its recipient (destination bus name, object path, method or signal name, interface name, etc.).[18][17]The body contains the data payload that the receiver process interprets—for instance the input or output arguments. All the data is encoded in a well known binary format called thewire formatwhich supports theserializationof various types, such as integers and floating-point numbers, strings, compound types, and so on,[17]also referred to asmarshaling.
The D-Bus specification defines thewire protocol:how to build the D-Bus messages to be exchanged between processes within a D-Bus connection. However, it does not define the underlying transport method for delivering these messages.
Most existing D-Bus implementations follow the architecture of the reference implementation. This architecture consists of two main components:[5]
a point-to-point communicationslibrarythat implements the D-Buswire protocolin order to exchange messages between two processes. In the reference implementation this library islibdbus.In other implementationslibdbusmay be wrapped by another higher-level library, language binding, or entirely replaced by a different standalone implementation that serves the same purpose.[21]This library only supports one-to-one communications between two processes.[18]
Adbus-daemonprocess acting as a D-Bus message bus daemon. Every process connected to the bus keeps one D-Bus connection with it.a specialdaemon processthat plays the bus role and to which the rest of the processes connect using any D-Bus point-to-point communications library. This process is also known as themessage bus daemon,[20]since it is responsible for routing messages from any process connected to the bus to another. In the reference implementation this role is performed bydbus-daemon,which itself is built on top oflibdbus.Another implementation of the message bus daemon isdbus-broker,which is built on top ofsd-bus.
Process A and B have a one-to-one D-Bus connection usinglibdbusover a Unix domain socket. They can use it to exchange messages directly.[22]In this scenario bus names are not required.[18]
Process A and B both connected to adbus-daemonusinglibdbusover a Unix domain socket. They can exchange messages sending them to the message bus process, which in turn will deliver the messages to the appropriate process. In this scenario bus names are mandatory to identify the destination process.
Thelibdbuslibrary (or its equivalent) internally uses a native lower-level IPC mechanism to transport the required D-Bus messages between the two processes in both ends of the D-Bus connection. D-Bus specification does not mandate which particular IPC transport mechanisms should be available to use, as it is the communications library that decides what transport methods it supports. For instance, in Unix-like operating systems such as Linuxlibdbustypically usesUnix domain socketsas the underlying transport method, but it also supportsTCP sockets.[5][18]
The communications libraries of both processes must agree on the selected transport method and also on the particular channel used for their communication. This information is defined by what D-Bus calls anaddress.[6][18]Unix-domain sockets arefilesystemobjects, and therefore they can be identified by a filename, so a valid address would beunix:path=/tmp/.hiddensocket.[5][17]Both processes must pass the same address to their respective communications libraries to establish the D-Bus connection between them. An address can also provide additional data to the communications library in the form of comma-separatedkey=valuepairs.[6][17]This way, for example, it can provide authentication information to a specific type of connection that supports it.
When a message bus daemon likedbus-daemonis used to implement a D-Bus bus, all processes that want to connect to the bus must know thebus address,the address by which a process can establish a D-Bus connection to the central message bus process.[5][18]In this scenario, the message bus daemon selects the bus address and the remainder processes must pass that value to their correspondinglibdbusor equivalent libraries.dbus-daemondefines a different bus address for every bus instance it provides. These addresses are defined in the daemon's configuration files.
Two processes can use a D-Bus connection to exchange messages directly between them,[22]but this is not the way in which D-Bus is normally intended to be used. The usual way is to always use a message bus daemon (i.e.dbus-daemon) as a communications central point to which each process should establish its point-to-point D-Bus connection. When a process—client or service—sends a D-Bus message, the message bus process receives it in the first instance and delivers it to the appropriate recipient. The message bus daemon may be seen as a hub or router in charge of getting each message to its destination by repeating it through the D-Bus connection to the recipient process.[18]The recipient process is determined by the destination bus name in the message's header field,[17]or by the subscription information to signals maintained by the message bus daemon in the case of signal propagation messages.[6]The message bus daemon can also produce its own messages as a response to certain conditions, such as an error message to a process that sent a message to a nonexistent bus name.[18]
dbus-daemonimproves the feature set already provided by D-Bus itself with additional functionality. For example,service activationallows automatic starting of services when needed—when the first request to any bus name of such service arrives at the message bus daemon.[5]This way, service processes neither need to be launched during thesystem initializationor user initialization stage nor need they consume memory or other resources when not being used. This feature was originally implemented usingsetuidhelpers,[23]but nowadays it can also be provided bysystemd's service activation framework.[citation needed]Service activation is an important feature that facilitates the management of the process lifecycle of services (for example when a desktop component should start or stop).[18]
D-Bus was started in 2002 by Havoc Pennington, Alex Larsson (Red Hat) and Anders Carlsson.[8]The version 1.0—consideredAPIstable—was released in November 2006.[24][25]
Heavily influenced by theDCOPsystem used by versions 2 and 3 ofKDE,D-Bus has replaced DCOP in theKDE 4release.[25][26]An implementation of D-Bus supports mostPOSIXoperating systems, and a port forWindowsexists. It is used byQt4 and later byGNOME.In GNOME it has gradually replaced most parts of the earlierBonobomechanism. It is also used byXfce.
One of the earlier adopters was the (nowadays deprecated)Hardware Abstraction Layer.HAL used D-Bus to export information about hardware that has been added to or removed from the computer.[8]
The usage of D-Bus is steadily expanding beyond the initial scope of desktop environments to cover an increasing amount of system services. For instance, theNetworkManagernetwork daemon,BlueZbluetooth stack andPulseAudiosound server use D-Bus to provide part or all of their services.systemduses the D-Bus wire protocol for communication betweensystemctland systemd, and is also promoting traditional system daemons to D-Bus services, such aslogind.[27]Another heavy user of D-Bus isPolkit,whose policy authority daemon is implemented as a service connected to the system bus.[28]
Although there are several implementations of D-Bus, the most widely used is the reference implementationlibdbus,developed by the same freedesktop.org project that designed the specification. However, libdbus is a low-level implementation that was never meant to be used directly by application developers, but as a reference guide for other reimplementations of D-Bus (such as those included in standard libraries of desktop environments, or inprogramming languagebindings). The freedesktop.org project itself recommends applications authors to "use one of the higher level bindings or implementations" instead.[29]The predominance of libdbus as the most used D-Bus implementation caused the terms "D-Bus" and "libdbus" to be often used interchangeably, leading to confusion.[citation needed]
GDBus[9]is an implementation of D-Bus based onGIO streamsincluded inGLib,aiming to be used byGTK+andGNOME.GDBus is not a wrapper of libdbus, but a complete and independent reimplementation of the D-Bus specification and protocol.[30]MATE Desktop[31]andXfce(version 4.14), which are also based on GTK+ 3, also use GDBus.[citation needed]
In 2013, thesystemdproject rewrote libdbus in an effort to simplify the code,[32]but it also resulted in a significant increase of the overall D-Bus performance. In preliminary benchmarks,BMWfound that the systemd's D-Bus library increased performance by 360%.[33]By version 221 ofsystemd,the sd-busAPIwas declared stable.[34]
kdbus is implemented as a character device driver.[35][36]All communication between processes take place over special character device nodes in/dev/kdbus(cf.devfs).
kdbuswas a project that aimed to reimplement D-Bus as a kernel-mediated peer-to-peerinter-process communicationmechanism. Beside performance improvements, kdbus would have advantages arising from otherLinux kernelfeatures such asnamespacesand auditing,[33][37]security from the kernel mediating, closing race conditions, and allowing D-Bus to be used during boot and shutdown (as needed by systemd).[38]kdbus inclusion in the Linux kernel proved controversial,[39]and was dropped in favor ofBUS1,as a more genericinter-process communication.[40]
^
Ward, Brian (2004). "14: A brief survey of the Linux desktop".How Linux Works: What Every Superuser Should Know(2 ed.). San Francisco: No Starch Press (published 2014). p. 305.ISBN9781593275679.Retrieved2016-11-07.One of the most important developments to come out of the Linux desktop is the Desktop Bus (D-Bus), a message-passing system. D-Bus is important because it serves as an interprocess communication mechanism that allows desktop applications to talk to each other [...].
^Vermeulen, Jeroen (14 Jul 2013)."Introduction to D-Bus".FreeDesktop.org.Retrieved3 October2015.D-Bus [...] is designed for use as a unified middleware layer underneath the main free desktop environments.
^abcPalmieri, John (January 2005)."Get on D-BUS".Red Hat Magazine. Archived fromthe originalon 23 October 2015.Retrieved3 November2015.
^Pennington, Havoc; Wheeler, David; Walters, Colin."D-Bus Tutorial".Retrieved21 October2015.For the within-desktop-session use case, the GNOME and KDE desktops have significant previous experience with different IPC solutions such as CORBA and DCOP. D-Bus is built on that experience and carefully tailored to meet the needs of these desktop projects in particular.
^Vermeulen, Jeroen (14 Jul 2013)."Introduction to D-Bus".FreeDesktop.org.Retrieved3 October2015.D-Bus was first built to replace the CORBA-like component model underlying the GNOME desktop environment. Similar to DCOP (which is used by KDE), D-Bus is set to become a standard component of the major free desktop environments for GNU/Linux and other platforms.
^Poettering, Lennart (19 June 2015)."The new sd-bus API of systemd".Retrieved21 October2015.we are working on moving things to a true user bus, of which there is only one per user on a system, regardless how many times that user happens to log in
^"What is D-Bus?".FreeDesktop.org.Retrieved29 October2015.There are also some reimplementations of the D-Bus protocol for languages such as C#, Java, and Ruby. These do not use the libdbus reference implementation
^ab"What is D-Bus?".FreeDesktop.org.Retrieved29 October2015.is built on top of a general one-to-one message passing framework, which can be used by any two apps to communicate directly (without going through the message bus daemon)
^Poettering, Lennart (19 June 2015)."The new sd-bus API of systemd".Retrieved21 October2015.Since systemd's inception it has been the IPC system it exposes its interfaces on.
^"What is D-Bus?".FreeDesktop.org.Retrieved5 January2015.The low-level implementation is not primarily designed for application authors to use. Rather, it is a basis for binding authors and a reference for reimplementations. If you are able to do so it is recommended that you use one of the higher level bindings or implementations.
^"Migrating to GDBus".GNOME Developer.Retrieved21 October2015.dbus-glib uses the libdbus reference implementation, GDBus doesn't. Instead, it relies on GIO streams as transport layer, and has its own implementation for the D-Bus connection setup and authentication.