1 <?xml version="1.0" standalone="no"?>
2 <!DOCTYPE article PUBLIC "-//OASIS//DTD DocBook XML V4.1.2//EN"
3 "http://www.oasis-open.org/docbook/xml/4.1.2/docbookx.dtd"
9 <title>D-BUS Tutorial</title>
10 <releaseinfo>Version 0.1</releaseinfo>
11 <date>02 October 2003</date>
14 <firstname>Havoc</firstname>
15 <surname>Pennington</surname>
17 <orgname>Red Hat, Inc.</orgname>
19 <email>hp@pobox.com</email>
27 <title>What is D-BUS?</title>
29 D-BUS is a system for <firstterm>interprocess communication</firstterm>
30 (IPC). Architecturally, it has several layers:
35 A library, libdbus, that allows two applications to connect
36 to each other and exchange messages.
41 A message bus daemon executable, built on libdbus, that multiple
42 applications can connect to. The daemon can route messages
43 from one application to zero or more other applications.
48 Wrapper libraries based on particular application frameworks.
49 For example, libdbus-glib and libdbus-qt. There are also
50 bindings to languages such as Python. These wrapper libraries
51 are the API most people should use, as they simplify the
52 details of D-BUS programming.
59 libdbus only supports one-to-one connections, just like a raw network
60 socket. However, rather than sending byte streams over the connection, you
61 send <firstterm>messages</firstterm>. Messages have a header identifying
62 the kind of message, and a body containing a data payload. libdbus also
63 abstracts the exact transport used (sockets vs. whatever else), and
64 handles details such as authentication.
68 The message bus daemon forms the hub of a wheel. Each spoke of the wheel
69 is a one-to-one connection to an application using libdbus. An
70 application sends a message to the bus daemon over its spoke, and the bus
71 daemon forwards the message to other connected applications as
72 appropriate. Think of the daemon as a router.
76 The bus daemon has multiple instances on a typical computer. The
77 first instance is a machine-global singleton, that is, a system daemon
78 similar to sendmail or Apache. This instance has heavy security
79 restrictions on what messages it will accept, and is used for systemwide
80 communication. The other instances are created one per user login session.
81 These instances allow applications in the user's session to communicate
86 The systemwide and per-user daemons are separate. Normal within-session
87 IPC does not involve the systemwide message bus process and vice versa.
91 <title>D-BUS applications</title>
93 There are many, many technologies in the world that have "Inter-process
94 communication" or "networking" in their stated purpose: <ulink
95 url="http://www.mbus.org/">MBUS</ulink>, <ulink
96 url="http://www.omg.org">CORBA</ulink>, <ulink
97 url="http://www.xmlrpc.com">XML-RPC</ulink>, <ulink
98 url="http://www.w3.org/TR/SOAP/">SOAP</ulink>, and probably hundreds
99 more. Each of these is tailored for particular kinds of application.
100 D-BUS is designed for two specific cases:
104 Communication between desktop applications in the same desktop
105 session; to allow integration of the desktop session as a whole,
106 and address issues of process lifecycle (when do desktop components
107 start and stop running).
112 Communication between the desktop session and the operating system,
113 where the operating system would typically include the kernel
114 and any system daemons or processes.
120 For the within-desktop-session use case, the GNOME and KDE desktops
121 have significant previous experience with different IPC solutions
122 such as CORBA and DCOP. D-BUS is built on that experience and
123 carefully tailored to meet the needs of these desktop projects
127 The problem solved by the systemwide or communication-with-the-OS case
128 is explained well by the following text from the Linux Hotplug project:
131 A gap in current Linux support is that policies with any sort of
132 dynamic "interact with user" component aren't currently
133 supported. For example, that's often needed the first time a network
134 adapter or printer is connected, and to determine appropriate places
135 to mount disk drives. It would seem that such actions could be
136 supported for any case where a responsible human can be identified:
137 single user workstations, or any system which is remotely
142 This is a classic "remote sysadmin" problem, where in this case
143 hotplugging needs to deliver an event from one security domain
144 (operating system kernel, in this case) to another (desktop for
145 logged-in user, or remote sysadmin). Any effective response must go
146 the other way: the remote domain taking some action that lets the
147 kernel expose the desired device capabilities. (The action can often
148 be taken asynchronously, for example letting new hardware be idle
149 until a meeting finishes.) At this writing, Linux doesn't have
150 widely adopted solutions to such problems. However, the new D-Bus
151 work may begin to solve that problem.
156 D-BUS may happen to be useful for purposes other than the one it was
157 designed for. Its general properties that distinguish it from
158 other forms of IPC are:
162 Binary protocol designed to be used asynchronously
163 (similar in spirit to the X Window System protocol).
168 Stateful, reliable connections held open over time.
173 The message bus is a daemon, not a "swarm" or
174 distributed architecture.
179 Many implementation and deployment issues are specified rather
185 Semantics are similar to the existing DCOP system, allowing
186 KDE to adopt it more easily.
191 Security features to support the systemwide mode of the
199 <sect1 id="concepts">
200 <title>Concepts</title>
202 Some basic concepts apply no matter what application framework you're
203 using to write a D-BUS application. The exact code you write will be
204 different for GLib vs. Qt vs. Python applications, however.
208 <title>Objects and Object Paths</title>
210 Each application using D-BUS contains <firstterm>objects</firstterm>,
211 which generally map to GObject, QObject, C++ objects, or Python objects
212 (but need not). An object is an <emphasis>instance</emphasis> rather
213 than a type. When messages are received over a D-BUS connection, they
214 are sent to a specific object, not to the application as a whole.
217 To allow messages to specify their destination object, there has to be a
218 way to refer to an object. In your favorite programming language, this
219 is normally called a <firstterm>pointer</firstterm> or
220 <firstterm>reference</firstterm>. However, these references are
221 implemented as memory addresses relative to the address space of your
222 application, and thus can't be passed from one application to another.
225 To solve this, D-BUS introduces a name for each object. The name
226 looks like a filesystem path, for example an object could be
227 named <literal>/org/kde/kspread/sheets/3/cells/4/5</literal>.
228 Human-readable paths are nice, but you are free to create an
229 object named <literal>/com/mycompany/c5yo817y0c1y1c5b</literal>
230 if it makes sense for your application.
233 Namespacing object paths is smart, by starting them with the components
234 of a domain name you own (e.g. <literal>/org/kde</literal>). This
235 keeps different code modules in the same process from stepping
236 on one another's toes.
240 <sect2 id="interfaces">
241 <title>Interfaces</title>
243 Each object supports one or more <firstterm>interfaces</firstterm>.
244 Think of an interface as a named group of methods and signals,
245 just as it is in GLib or Qt. Interfaces define the
246 <emphasis>type</emphasis> of an object instance.
250 <sect2 id="messages">
251 <title>Message Types</title>
253 Messages are not all the same; in particular, D-BUS has
254 4 built-in message types:
258 Method call messages ask to invoke a method
264 Method return messages return the results
265 of invoking a method.
270 Error messages return an exception caused by
276 Signal messages are notifications that a given signal
277 has been emitted (that an event has occurred).
278 You could also think of these as "event" messages.
284 A method call maps very simply to messages, then: you send a method call
285 message, and receive either a method return message or an error message
290 <sect2 id="services">
291 <title>Services</title>
294 Object paths, interfaces, and messages exist on the level of
295 libdbus and the D-BUS protocol; they are used even in the
296 1-to-1 case with no message bus involved.
300 Services, on the other hand, are a property of the message bus daemon.
301 A <firstterm>service</firstterm> is simply a name mapped to
302 some application connected to the message bus daemon.
303 These names are used to specify the origin and destination
304 of messages passing through the message bus. When a name is mapped
305 to a particular application, the application is said to
306 <firstterm>own</firstterm> that service.
310 On connecting to the bus daemon, each application immediately owns a
311 special name called the <firstterm>base service</firstterm>. A base
312 service begins with a ':' (colon) character; no other services are
313 allowed to begin with that character. Base services are special because
314 each one is unique. They are created dynamically, and are never re-used
315 during the lifetime of the same bus daemon. You know that a given
316 base service name will have the same owner at all times.
317 An example of a base service name might be <literal>:34-907</literal>.
321 Applications may ask to own additional <firstterm>well-known
322 services</firstterm>. For example, you could write a specification to
323 define a service called <literal>com.mycompany.TextEditor</literal>.
324 Your definition could specify that to own this service, an application
325 should have an object at the path
326 <literal>/com/mycompany/TextFileManager</literal> supporting the
327 interface <literal>org.freedesktop.FileHandler</literal>.
331 Applications could then send messages to this service,
332 object, and interface to execute method calls.
336 You could think of the base service names as IP addresses, and the
337 well-known services as domain names. So
338 <literal>com.mycompany.TextEditor</literal> might map to something like
339 <literal>:34-907</literal> just as <literal>mycompany.com</literal> maps
340 to something like <literal>192.168.0.5</literal>.
344 Services have a second important use, other than routing messages. They
345 are used to track lifecycle. When an application exits (or crashes), its
346 connection to the message bus will be closed by the operating system
347 kernel. The message bus then sends out notification messages telling
348 remaining applications that the application's services have lost their
349 owner. By tracking these notifications, your application can reliably
350 monitor the lifetime of other applications.
355 <sect2 id="addresses">
356 <title>Addresses</title>
359 Applications using D-BUS are either servers or clients. A server
360 listens for incoming connections; a client connects to a server. Once
361 the connection is established, it is a symmetric flow of messages; the
362 client-server distinction only matters when setting up the
367 A D-BUS <firstterm>address</firstterm> specifies where a server will
368 listen, and where a client will connect. For example, the address
369 <literal>unix:path=/tmp/abcdef</literal> specifies that the server will
370 listen on a UNIX domain socket at the path
371 <literal>/tmp/abcdef</literal> and the client will connect to that
372 socket. An address can also specify TCP/IP sockets, or any other
373 transport defined in future iterations of the D-BUS specification.
377 When using D-BUS with a message bus, the bus daemon is a server
378 and all other applications are clients of the bus daemon.
379 libdbus automatically discovers the address of the per-session bus
380 daemon by reading an environment variable. It discovers the
381 systemwide bus daemon by checking a well-known UNIX domain socket path
382 (though you can override this address with an environment variable).
386 If you're using D-BUS without a bus daemon, it's up to you to
387 define which application will be the server and which will be
388 the client, and specify a mechanism for them to agree on
389 the server's address.
394 <sect2 id="bigpicture">
395 <title>Big Conceptual Picture</title>
398 Pulling all these concepts together, to specify a particular
399 method call on a particular object instance, a number of
400 nested components have to be named:
402 Address -> [Service] -> Path -> Interface -> Method
404 The service is in brackets to indicate that it's optional -- you only
405 provide a service name to route the method call to the right application
406 when using the bus daemon. If you have a direct connection to another
407 application, services aren't used.
411 The interface is also optional, primarily for historical
412 reasons; DCOP does not require specifying the interface,
413 instead simply forbidding duplicate method names
414 on the same object instance. D-BUS will thus let you
415 omit the interface, but if your method name is ambiguous
416 it is undefined which method will be invoked.
423 <sect1 id="glib-client">
424 <title>GLib API: Using Remote Objects</title>
430 <sect1 id="glib-server">
431 <title>GLib API: Implementing Objects</title>
437 <sect1 id="qt-client">
438 <title>Qt API: Using Remote Objects</title>
444 <sect1 id="qt-server">
445 <title>Qt API: Implementing Objects</title>
452 <sect1 id="python-client">
453 <title>Python API: Using Remote Objects</title>
459 <sect1 id="python-server">
460 <title>Python API: Implementing Objects</title>