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, <firstterm>libdbus</firstterm>, that allows two
36 applications to connect to each other and exchange messages.
41 A <firstterm>message bus daemon</firstterm> executable, built on
42 libdbus, that multiple applications can connect to. The daemon can
43 route messages from one application to zero or more other
49 <firstterm>Wrapper libraries</firstterm> based on particular
50 application frameworks. For example, libdbus-glib and
51 libdbus-qt. There are also bindings to languages such as
52 Python. These wrapper libraries are the API most people should use,
53 as they simplify the details of D-BUS programming. libdbus is
54 intended to be a low-level backend for the higher level bindings.
55 Much of the libdbus API is only useful for binding implementation.
62 libdbus only supports one-to-one connections, just like a raw network
63 socket. However, rather than sending byte streams over the connection, you
64 send <firstterm>messages</firstterm>. Messages have a header identifying
65 the kind of message, and a body containing a data payload. libdbus also
66 abstracts the exact transport used (sockets vs. whatever else), and
67 handles details such as authentication.
71 The message bus daemon forms the hub of a wheel. Each spoke of the wheel
72 is a one-to-one connection to an application using libdbus. An
73 application sends a message to the bus daemon over its spoke, and the bus
74 daemon forwards the message to other connected applications as
75 appropriate. Think of the daemon as a router.
79 The bus daemon has multiple instances on a typical computer. The
80 first instance is a machine-global singleton, that is, a system daemon
81 similar to sendmail or Apache. This instance has heavy security
82 restrictions on what messages it will accept, and is used for systemwide
83 communication. The other instances are created one per user login session.
84 These instances allow applications in the user's session to communicate
89 The systemwide and per-user daemons are separate. Normal within-session
90 IPC does not involve the systemwide message bus process and vice versa.
94 <title>D-BUS applications</title>
96 There are many, many technologies in the world that have "Inter-process
97 communication" or "networking" in their stated purpose: <ulink
98 url="http://www.mbus.org/">MBUS</ulink>, <ulink
99 url="http://www.omg.org">CORBA</ulink>, <ulink
100 url="http://www.xmlrpc.com">XML-RPC</ulink>, <ulink
101 url="http://www.w3.org/TR/SOAP/">SOAP</ulink>, and probably hundreds
102 more. Each of these is tailored for particular kinds of application.
103 D-BUS is designed for two specific cases:
107 Communication between desktop applications in the same desktop
108 session; to allow integration of the desktop session as a whole,
109 and address issues of process lifecycle (when do desktop components
110 start and stop running).
115 Communication between the desktop session and the operating system,
116 where the operating system would typically include the kernel
117 and any system daemons or processes.
123 For the within-desktop-session use case, the GNOME and KDE desktops
124 have significant previous experience with different IPC solutions
125 such as CORBA and DCOP. D-BUS is built on that experience and
126 carefully tailored to meet the needs of these desktop projects
130 The problem solved by the systemwide or communication-with-the-OS case
131 is explained well by the following text from the Linux Hotplug project:
134 A gap in current Linux support is that policies with any sort of
135 dynamic "interact with user" component aren't currently
136 supported. For example, that's often needed the first time a network
137 adapter or printer is connected, and to determine appropriate places
138 to mount disk drives. It would seem that such actions could be
139 supported for any case where a responsible human can be identified:
140 single user workstations, or any system which is remotely
145 This is a classic "remote sysadmin" problem, where in this case
146 hotplugging needs to deliver an event from one security domain
147 (operating system kernel, in this case) to another (desktop for
148 logged-in user, or remote sysadmin). Any effective response must go
149 the other way: the remote domain taking some action that lets the
150 kernel expose the desired device capabilities. (The action can often
151 be taken asynchronously, for example letting new hardware be idle
152 until a meeting finishes.) At this writing, Linux doesn't have
153 widely adopted solutions to such problems. However, the new D-Bus
154 work may begin to solve that problem.
159 D-BUS may happen to be useful for purposes other than the one it was
160 designed for. Its general properties that distinguish it from
161 other forms of IPC are:
165 Binary protocol designed to be used asynchronously
166 (similar in spirit to the X Window System protocol).
171 Stateful, reliable connections held open over time.
176 The message bus is a daemon, not a "swarm" or
177 distributed architecture.
182 Many implementation and deployment issues are specified rather
188 Semantics are similar to the existing DCOP system, allowing
189 KDE to adopt it more easily.
194 Security features to support the systemwide mode of the
202 <sect1 id="concepts">
203 <title>Concepts</title>
205 Some basic concepts apply no matter what application framework you're
206 using to write a D-BUS application. The exact code you write will be
207 different for GLib vs. Qt vs. Python applications, however.
211 <title>Objects and Object Paths</title>
213 Each application using D-BUS contains <firstterm>objects</firstterm>,
214 which generally map to GObject, QObject, C++ objects, or Python objects
215 (but need not). An object is an <emphasis>instance</emphasis> rather
216 than a type. When messages are received over a D-BUS connection, they
217 are sent to a specific object, not to the application as a whole.
220 To allow messages to specify their destination object, there has to be a
221 way to refer to an object. In your favorite programming language, this
222 is normally called a <firstterm>pointer</firstterm> or
223 <firstterm>reference</firstterm>. However, these references are
224 implemented as memory addresses relative to the address space of your
225 application, and thus can't be passed from one application to another.
228 To solve this, D-BUS introduces a name for each object. The name
229 looks like a filesystem path, for example an object could be
230 named <literal>/org/kde/kspread/sheets/3/cells/4/5</literal>.
231 Human-readable paths are nice, but you are free to create an
232 object named <literal>/com/mycompany/c5yo817y0c1y1c5b</literal>
233 if it makes sense for your application.
236 Namespacing object paths is smart, by starting them with the components
237 of a domain name you own (e.g. <literal>/org/kde</literal>). This
238 keeps different code modules in the same process from stepping
239 on one another's toes.
243 <sect2 id="interfaces">
244 <title>Interfaces</title>
246 Each object supports one or more <firstterm>interfaces</firstterm>.
247 Think of an interface as a named group of methods and signals,
248 just as it is in GLib or Qt. Interfaces define the
249 <emphasis>type</emphasis> of an object instance.
253 <sect2 id="messages">
254 <title>Message Types</title>
256 Messages are not all the same; in particular, D-BUS has
257 4 built-in message types:
261 Method call messages ask to invoke a method
267 Method return messages return the results
268 of invoking a method.
273 Error messages return an exception caused by
279 Signal messages are notifications that a given signal
280 has been emitted (that an event has occurred).
281 You could also think of these as "event" messages.
287 A method call maps very simply to messages, then: you send a method call
288 message, and receive either a method return message or an error message
293 <sect2 id="services">
294 <title>Services</title>
297 Object paths, interfaces, and messages exist on the level of
298 libdbus and the D-BUS protocol; they are used even in the
299 1-to-1 case with no message bus involved.
303 Services, on the other hand, are a property of the message bus daemon.
304 A <firstterm>service</firstterm> is simply a name mapped to
305 some application connected to the message bus daemon.
306 These names are used to specify the origin and destination
307 of messages passing through the message bus. When a name is mapped
308 to a particular application, the application is said to
309 <firstterm>own</firstterm> that service.
313 On connecting to the bus daemon, each application immediately owns a
314 special name called the <firstterm>base service</firstterm>. A base
315 service begins with a ':' (colon) character; no other services are
316 allowed to begin with that character. Base services are special because
317 each one is unique. They are created dynamically, and are never re-used
318 during the lifetime of the same bus daemon. You know that a given
319 base service name will have the same owner at all times.
320 An example of a base service name might be <literal>:34-907</literal>.
324 Applications may ask to own additional <firstterm>well-known
325 services</firstterm>. For example, you could write a specification to
326 define a service called <literal>com.mycompany.TextEditor</literal>.
327 Your definition could specify that to own this service, an application
328 should have an object at the path
329 <literal>/com/mycompany/TextFileManager</literal> supporting the
330 interface <literal>org.freedesktop.FileHandler</literal>.
334 Applications could then send messages to this service,
335 object, and interface to execute method calls.
339 You could think of the base service names as IP addresses, and the
340 well-known services as domain names. So
341 <literal>com.mycompany.TextEditor</literal> might map to something like
342 <literal>:34-907</literal> just as <literal>mycompany.com</literal> maps
343 to something like <literal>192.168.0.5</literal>.
347 Services have a second important use, other than routing messages. They
348 are used to track lifecycle. When an application exits (or crashes), its
349 connection to the message bus will be closed by the operating system
350 kernel. The message bus then sends out notification messages telling
351 remaining applications that the application's services have lost their
352 owner. By tracking these notifications, your application can reliably
353 monitor the lifetime of other applications.
358 <sect2 id="addresses">
359 <title>Addresses</title>
362 Applications using D-BUS are either servers or clients. A server
363 listens for incoming connections; a client connects to a server. Once
364 the connection is established, it is a symmetric flow of messages; the
365 client-server distinction only matters when setting up the
370 A D-BUS <firstterm>address</firstterm> specifies where a server will
371 listen, and where a client will connect. For example, the address
372 <literal>unix:path=/tmp/abcdef</literal> specifies that the server will
373 listen on a UNIX domain socket at the path
374 <literal>/tmp/abcdef</literal> and the client will connect to that
375 socket. An address can also specify TCP/IP sockets, or any other
376 transport defined in future iterations of the D-BUS specification.
380 When using D-BUS with a message bus, the bus daemon is a server
381 and all other applications are clients of the bus daemon.
382 libdbus automatically discovers the address of the per-session bus
383 daemon by reading an environment variable. It discovers the
384 systemwide bus daemon by checking a well-known UNIX domain socket path
385 (though you can override this address with an environment variable).
389 If you're using D-BUS without a bus daemon, it's up to you to
390 define which application will be the server and which will be
391 the client, and specify a mechanism for them to agree on
392 the server's address.
397 <sect2 id="bigpicture">
398 <title>Big Conceptual Picture</title>
401 Pulling all these concepts together, to specify a particular
402 method call on a particular object instance, a number of
403 nested components have to be named:
405 Address -> [Service] -> Path -> Interface -> Method
407 The service is in brackets to indicate that it's optional -- you only
408 provide a service name to route the method call to the right application
409 when using the bus daemon. If you have a direct connection to another
410 application, services aren't used.
414 The interface is also optional, primarily for historical
415 reasons; DCOP does not require specifying the interface,
416 instead simply forbidding duplicate method names
417 on the same object instance. D-BUS will thus let you
418 omit the interface, but if your method name is ambiguous
419 it is undefined which method will be invoked.
426 <sect1 id="glib-client">
427 <title>GLib API: Using Remote Objects</title>
433 <sect1 id="glib-server">
434 <title>GLib API: Implementing Objects</title>
440 <sect1 id="qt-client">
441 <title>Qt API: Using Remote Objects</title>
447 <sect1 id="qt-server">
448 <title>Qt API: Implementing Objects</title>
455 <sect1 id="python-client">
456 <title>Python API: Using Remote Objects</title>
462 <sect1 id="python-server">
463 <title>Python API: Implementing Objects</title>