Linux › Section N · The Linux desktop and printing › Topic N.3
D-Bus and printing
Two quiet services hold a Linux desktop together: D-Bus, the message bus that lets programs announce events and ask each other for help, and CUPS, the print system that turns a document into something a printer understands. Both can be inspected and driven from the command line.
Why a message bus?
Programs on a desktop constantly need to tell each other things. A USB stick appears, the network drops, the battery runs low, a user asks to change the host name. Each program could open its own Unix domain socket to each other program, but that grows into a tangle. D-Bus, the Desktop Bus, replaces it with a hub. A library gives every program one standard way to send messages, and a bus daemon routes them: programs connect once, register what they offer and subscribe to the events they care about. The reference daemon is dbus-daemon; Fedora and Arch use a faster compatible implementation, dbus-broker.
Names, objects and messages
Each connection gets a unique bus name such as :1.42, and a service may also claim a well-known name such as org.freedesktop.login1 (systemd-logind) or org.freedesktop.UDisks2 (the disk service). A service exposes objects at paths such as /org/freedesktop/login1. Each object implements interfaces, and an interface defines methods (calls you can make), properties (values you can read or set) and signals (announcements sent to anyone listening). Calls that need privilege, such as rebooting or mounting a disk, are checked by polkit, which decides whether the caller is allowed, perhaps after asking for a password.
System bus and session bus
D-Bus started on the desktop, but core system services now depend on it, so two kinds of bus run. The system bus starts at boot under its own unprivileged user; it carries system events and requests to services such as systemd, logind, NetworkManager and UDisks2, through the socket /run/dbus/system_bus_socket. Each logged-in user also gets a session bus for desktop applications, usually at /run/user/UID/bus and named in the variable DBUS_SESSION_BUS_ADDRESS. A program on the session bus cannot read another user's session bus, which keeps users' desktop traffic separate.
Watching and calling the bus
Two tool sets let you look inside. dbus-monitor --system or dbus-monitor --session prints messages as they pass; on the system bus it is quiet until something happens, such as a disk being plugged in. systemd's busctl does more: busctl list shows the names on the system bus, busctl tree followed by a service name shows its object paths, busctl introspect shows an object's methods, properties and signals, and busctl monitor watches traffic. Add --user to any busctl command to use your session bus instead. GNOME's gdbus offers similar functions. Monitoring the whole system bus requires root.
Worked example: reading a property over D-Bus
hostnamectl shows the host name, but it gets the answer from a D-Bus service, so you can ask that service directly. 1. Find it: busctl list includes org.freedesktop.hostname1. 2. Find its object: busctl tree org.freedesktop.hostname1 shows /org/freedesktop/hostname1. 3. List its members: busctl introspect org.freedesktop.hostname1 /org/freedesktop/hostname1 shows a Hostname property. 4. Read it: busctl get-property org.freedesktop.hostname1 /org/freedesktop/hostname1 org.freedesktop.hostname1 Hostname. 5. Illustrative output: s "lab1", where s marks a string. The point is that a command-line tool and a settings window are both just D-Bus clients of the same service.
How printing works
Printing a document passes through a pipeline. 1. The application produces print data, today usually PDF, traditionally PostScript, a page-description programming language. 2. It hands the job to the print server, which places it in a queue. 3. When the job's turn comes, filters convert it into what the printer accepts: perhaps left as PDF, perhaps rasterised into a bitmap in the printer's own format, a step usually done by Ghostscript. 4. Printer options such as paper tray, copies and two-sided printing are applied. 5. A backend delivers the result over the network, USB or another connection.
CUPS and IPP
Linux, like macOS, uses CUPS, now maintained by the OpenPrinting project. Its daemon is cupsd, configured under /etc/cups, and it speaks the Internet Printing Protocol (IPP), an HTTP-based protocol on TCP port 631. Pointing a browser at http://localhost:631/ shows printers and jobs; the distribution's printer settings tool is usually the better way to add printers. Most network printers sold today are driverless: they advertise themselves on the local network and support standard formats through IPP Everywhere or AirPrint, so no model-specific driver is needed. Older printers were described by PPD files; these are deprecated, and printer applications that act like driverless printers are replacing them.
Printing from the command line
CUPS provides both the System V and Berkeley families of print commands. lpstat -p -d lists printers and shows the default destination; lpstat -o lists queued jobs. lp -d office report.pdf prints to the printer named office; the Berkeley equivalent is lpr -P office report.pdf. Options travel with -o: lp -n 2 -o sides=two-sided-long-edge prints two double-sided copies. cancel followed by a job ID, or lprm, removes a job. lpoptions -d office makes office your default. An administrator adds a driverless network printer with lpadmin -p office -E -v ipp://printer.local/ipp/print -m everywhere, which queries the printer for its own capabilities.
Worked example: a job that will not print
A user prints a 30-page report and nothing comes out. 1. lpstat -p -d shows that the default destination is an old printer, not office: the job went to the wrong queue. 2. lpstat -o lists the stuck job, say oldlaser-17. 3. cancel oldlaser-17 removes it. 4. lpoptions -d office fixes the default for next time. 5. lp -o sides=two-sided-long-edge report.pdf reprints it on office. 6. If that queue also stalls, lpstat -p office shows whether the printer is disabled, and the CUPS error log, /var/log/cups/error_log or the journal for cups.service, explains why. Checking the queue before resending avoids printing the report twice.
Other desktop topics
Much of the shared desktop plumbing, from D-Bus itself to the specifications for menus, icons and file associations, is coordinated through freedesktop.org, which is why programs from different desktops interoperate. ChromeOS shows how far the same building blocks can be reshaped: it is a Linux system built around the Chrome browser, with most of a traditional desktop removed. Whatever desktop you use, the same rule from the previous topics applies: find which component owns a job, whether the compositor, D-Bus service or print server, before changing settings.
Common mistakes to avoid
Thinking D-Bus moves pixels or files: it carries short messages, and bulk data travels by other means. Using the session bus to reach a system service such as logind, or the reverse; busctl without --user means the system bus. Editing D-Bus policy files to make a call work, when the refusal comes from polkit by design. Resending a print job before checking the queue with lpstat -o. Hunting for a model-specific driver for a modern network printer that is driverless. Assuming PostScript is still the only format in the pipeline; PDF is now the usual intermediate.
Key takeaways
D-Bus routes messages between programs: services claim names, expose objects with methods, properties and signals, and polkit guards privileged calls. The system bus serves system services; each user has a session bus. dbus-monitor and busctl (list, tree, introspect, get-property, monitor) let you watch and call it. Printing runs application, queue, filters, options and backend. CUPS (cupsd, port 631, IPP) manages queues; lp, lpr, lpstat, cancel, lpoptions and lpadmin drive it. Driverless IPP printing is replacing PPD-based drivers.
Flashcards
Flashcard
Message bus that routes requests and events between programs
Test your understanding
Question 1 of 5
A settings window and the hostnamectl command both report the same host name. What does the D-Bus model suggest?
Question 2 of 5
Which bus should a program use to ask systemd-logind, a system service, to suspend the machine?
Question 3 of 5
A user wants two double-sided copies of report.pdf on the printer named office. Which command does it?
Question 4 of 5
Why can a modern network printer often be added with lpadmin and -m everywhere, without any driver download?
Question 5 of 5
A print job is not appearing. Which step should come first?
