KNOW-HOW D-Bus and HAL

Interprocess communication with D-Bus and HAL FAST TRAFFIC

It’s the end of the line for CORBA! Gnome now relies on the D-Bus mes- saging system, and KDE is in the process of migrating.

BY OLIVER FROMMEL

obody likes applications HAL [2], will be the communication sys- that spend all day sitting in tem for future generations of . Nthe corners of the desktop and refusing to talk to anything Talk to Me else. The least you might expect D-Bus is an Inter Process Communica- would be for them to exchange tion (IPC) system; that is, it provides the data with other desktop resi- infrastructure that lets applications talk dents using simple drag & drop to one another and to parts of the oper- techniques. But many users ating system. Although IPC mechanisms expect their programs to were introduced to Unix years ago, they demonstrate more ad- are restricted to signals, pipes, and simi- vanced communication lar things. skills at all levels. Of This may sound familiar to you; after course, users want plug- all, competitive approaches have been gable USB disks no mat- around right from the beginning – just ter what program they think of CORBA, Microsoft’s DCOM, or are using. And VOIP hundreds of other projects. Both KDE softphones should and Gnome have experimented with make friends with their own CORBA implementations from the new hardware the outset. KDE introduced its own when you change DCOP system, and Gnome’s CORBA leg- the headset with- acy is evidenced by the Bonobo compo- out demanding a nent system. Regardless of their personal reboot. opinions of CORBA, most developers To allow this to who just want to code a desktop applica- happen, a Linux tion are overtaxed by the system. And system needs a com- this possibly explains why Bonobo has munication system been vegetating behind the Gnome that lets desktop appli- scenes for so long. cations talk to one an- D-Bus is designed as a simple system other and to the un- with a small footprint. The basic Lib- derlying levels right down through the Oliver spent several kernel to the years as a sysop and at hardware. And if Ars Electronica Cen- the Freedesktop ter in Linz/Austria. developers After finishing his have any say studies in Philoso- in the mat- phy, Linguistics and ter, D-Bus Computer Science, he became an [1], which editor for the Bavarian Broadcasting THEAUTHOR www.photocase. relies on Corporation. Today he is head of the Hardware Editorial Competence Center for Abstrac- Software and Programming at Linux tion Layer New Media AG. com

44 ISSUE 68 JULY 2006 D-Bus and HAL KNOW-HOW

dbus simply provides functions desktop session talk to each other. Of also handle filesystems; it has the ability that allow two applications to communi- course, these applications can be ser- to identify filesystem types, including cate. Application developers don’t typi- vices provided by the desktop environ- LUKS-encrypted partitions [4]. In cally use the library, preferring, instead, ment. In contrast to this, the system bus Gnome, HAL now handles the lion’s the Glib-API-based Libdbus-Glib, which mainly ensures that desktop programs share of the hardware management, par- provides an object-oriented -API. It is can talk to the underlying layers. For ex- ticularly hot-pluggable devices. The at this level that D-Bus’s capabilities ex- ample, an application can use the system -volume-manager process, which tend to provide a genuine bus system bus to register for a specific hardware runs in the background, makes this hap- living up to its name. The server process, class, such as, say, digital cameras. pen; To configure the process, Gnome dbus-, runs in the background users run the gnome-volume-properties and listens for connection requests from Hardware Management with front-end (Figure 2). applications that register for various HAL There is also a front-end for HAL that event types, such as plugging and un- D-Bus does not provide its own hard- gives users a tree view of the attached plugging specific hardware. When the ware management; instead it relies on devices (Figure 3). Fedora users will find event occurs, the D-Bus daemon sends a the Hardware Abstraction Layer, HAL. the -device-manager hidden away in message along the bus, and the applica- Although HAL is independent of D-Bus, the hal-gnome package. tions respond accordingly. the two components work hand in hand: HAL uses D-Bus to provide services, and Using D-Bus System Global or Per D-Bus was mainly programmed for HAL. The D-Bus protocol specification is avail- Session Besides the kernel, modern distribu- able from [1]. The protocol defines four On systems that use D-Bus, each server tions use the subsystem for user- message types that users can send over process implements two buses: the sys- space hardware management. As of ver- the bus. For example, one application tem bus and the session bus. The system sion 0.59, Udev replaces the hotplug sys- can call the methods provided by an- bus launches at boot time and keeps on tem, which has only recently established running even if no users are logged on to itself as a mechanism for supporting Listing 1: 10-camera-ptp.fdi the system. When a user runs the GUI- pluggable hardware by running /sbin/ 01 based login manager to launch a desktop hotplug. Besides kernel and Udev infor- session, a server process for the session mation, HAL now has additional details 02 bus is launched. The dbus-daemon bi- on devices stored as FDI files (Device In- 03 both modes: --system or --session. The ing 1 shows a section from an FDI file 04 for starting the daemon and setting the Gnome NetworkManager [3] is a good required environmental variables. Most example of how components cooperate. 05 Figure 1 shows the role the two buses occur, such as a user plugging in or un- 06 bus lets applications belonging to a ager. Besides genuine devices, HAL can 07 camera Application Application 08 camera 09 ptp 10 Kernel 11 12 Hardware 13 14 Figure 1: D-Bus and HAL in the overall context of a Linux system’s components. Applications 15 use D-Bus to query HAL about the underlying hardware.

WWW.LINUX - MAGAZINE.COM ISSUE 68 JULY 2006 45 KNOW-HOW D-Bus and HAL

DBus. Objects pro- advised not to change working systems, vide services that you can use D-Bus to control the BMPx look similar, but are and audio players. A useful dot-separated: org. thing for network-aware programs is the freedesktop.DBus. current version of the Avahi Zero-Conf The interface package which supports D-Bus. This groups the methods means that applications can be notified and signals for an when servers appear on the network. object; again, a dot- ted notation is used D-Bus Programming in a similar ap- There is good news and bad news for proach to a Java D-Bus . The good news is interface. that bindings are available for a variety of programming languages [6], from the Security Glib-C API, through Python, to Ruby, C#, It would not be a and Java. The bad news is that the API Figure 2: Working with HAL and D-Bus: the Gnome Volume Man- good idea to let has changed so frequently in the past ager, which is user-configurable via gnome-volume-properties. non-privileged that many sample programs on the Inter- (shown above) users access D-Bus. net will not run on the current D-Bus According to the de- versions. There is a general lack of docu- other application. To reflect this, the sec- velopers, security has been a major con- mentation on D-Bus interaction. Your ond type represents a response to a call cern right from the outset. In a simple best bet is to investigate the source code request. The third message type is used case, the UID would be evaluated to con- of working program, such as the Gnome for error handling by the server applica- trol access. If the bus daemon and the tion. The fourth message type provides clients belong to the same user, no re- Listing 4: server.py signals transmitted by applications over strictions are applied. D-Bus also imple- 01 import the bus that do not require a response. ments security policies that define a us- 02 import dbus Programmers can call D-Bus methods er’s privileges to allow more granular synchronously or asynchronously. control (Listing 2). 03 import dbus. D-Bus uses a multi-level naming D-Bus can also be used with SE Linux, 04 import dbus.service schema to identify message sources and probably because is the main 05 targets. Each application contains one or developer. more objects that can be addressed via 06 class HelloWorldObject(dbus. paths made up of inverted domain Practical Applications service.Object): names, with the object name itself ap- Other applications besides Gnome have 07 def __init__(self, bus_ pended, for example, /org/freedesktop/ started to use D-Bus. A fairly up-to-date name, object_path): list is available at [5]. Although users are 08 dbus.service.Object.__ Listing 2: cups.conf init__(self, bus_name, object_ 01 Listing 3: hal.py path) 02 02 firstfloor.HelloWorldIFace') 03 03 bus = dbus.SystemBus() 10 def hello(self): 04 05 '/ 13 session_bus = dbus. 05 org/freedesktop/Hal/Manager') SessionBus() 06 06 hal_manager = dbus.Interface 14 bus_name = dbus.service. 07 Hal.Manager') HelloWorld', bus=session_bus) 08 07 15 object = HelloWorldObject(bus_ 09 09 16 10 10 for dev in dev_list: 17 mainloop = gobject.MainLoop() 11 11 print dev,"\n" 18 mainloop.run()

46 ISSUE 68 JULY 2006 WWW.LINUX - MAGAZINE.COM D-Bus and HAL KNOW-HOW

Network Manager, which was written in Python. The basic approach is the same for all supported programming languages: con- nect to the bus, pick up a reference for the remote object and the interface, and issue requests, or register signal han- dlers. When a program registers a signal handler, it requires a main loop that regularly checks for incoming signals. It is advisable to use the Glib mainloop object for D-Bus programs, no matter whether you use C or a scripting language. The following short sample code list- ing in Python shows you how to use D- Bus in your own programs. Using object- oriented constructs such as objects and interfaces is far easier in Python than in Glib-C programs. Just look how easy it is to import the D-Bus module in Line 1 of Listing 3. Some changes have occurred in D-Bus version 0.41 or newer: if you want to use the Glib mainloop, note that Figure 3: The HAL Device Manager showing a tree-view of the hardware the Glib objects and methods now reside in the dbus.glib module. method decorator specifies the interface have a system that works throughout a Interface-based access is another fea- methods. network. In this light, it is probably just ture that has developed over time. Many a question of time until the system starts sample programs on the Internet demon- Automatic Launch to mutate into the kind of monster that strate the use of the deprecated methods, Thus far, we have just assumed that an CORBA has become. If this happens, get_service. Today, you need a proxy ob- application connects to D-Bus of its own let’s hope a new group of developers at ject that uses an interface to place a accord and then runs as a client. A pro- or Red Hat steps into to start wrapper round the access. The bus gram that provides D-Bus services either again from scratch. object’s get_object method gives you needs to be launched at boot time, or the As of this writing, Gnome draws this ability, providing the arguments org. D-Bus server has to launch it. To allow most heavily on D-Bus, to manage hot- freedesktop.Hal and /org/freedesktop/ this to happen, the server needs to know plug devices such as cameras, hard Hal/Manager (Listing 3, Line 4). The the service name of the binary to exe- disks, or scanners, for example. But the proxy object’s static dbus.Interface cute; the server parses a .service configu- KDE developers are slowly migrating method generates an interface, which ration file to discover the name. Listing 5 their applications to D-Bus, and even the programmer can then call. gives an example of a configuration file now speaks D-Bus, so application devel- Some D-Bus API features require spe- for the BMPx audio player. opers should maybe take some time out cial expressions in the various program- As you have seen from the examples, to ride the D-Bus, too. ■ ming languages. For example, the Py- it is not really difficult to use D-Bus to let thon API uses Python 2.4’s new decora- your own applications talk to the outside INFO tors to identify signals and service meth- world, although finding the right func- [1] D-Bus: http:// www. freedesktop. org/ ods (see Listing 4). tion in the API jungle can be a pain. wiki/ Software/ dbus Just like in the client example, the D-Bus has spread like wildfire to many [2] HAL: http:// www. freedesktop. org/ wiki/ sample program starts by connecting to Linux distributions, despite the fact that Software/ hal the D-Bus. The HelloWorldObject object’s it is still under development and the in- [3] Gnome NetworkManager: constructor is then called to run the terfaces change from one release to the http:// www. gnome. org/ projects/ method __init__ . The @dbus.service. next. If you are interested in experiment- NetworkManager ing with D-Bus at this stage, be aware [4] LUKS for HAL: http:// www. redhat. Listing 5: BMPx.service that major changes to the D-Bus API can com/ magazine/ 012oct05/ features/ hal occur without notice. [5] D-Bus software: 01 [D-BUS Service] http:// www. freedesktop. org/ wiki/ 02 Name=org.beepmediaplayer.bmp Back to the Future Software_2fDbusProjects [6] Language bindings for D-Bus: 03 Exec=/usr/libexec/ In a that adds a touch of irony to the D-Bus development story, the inven- http:// www. freedesktop. org/ wiki/ beep-media-player-2-bin Software_2fDBusBindings tors of D-Bus now find it important to

WWW.LINUX - MAGAZINE.COM ISSUE 68 JULY 2006 47