Providing DBus services using the Perl Net::DBus APIs

Posted: October 2nd, 2005 | Filed under: Uncategorized | 1 Comment »

This posting provides a tutorial on providing a DBus service using the
Perl Net::DBus application bindings, which will later appear in as a tutorial
in the POD document for Net::DBus. The examples in this document will
be based on the code from the Music::Player distribution, which is a simple DBus service providing a music track player.

CREATING AN OBJECT

The first step in creating an object is to create a new package which
inherits from Net::DBus::Object. The Music::Player::Manager object
provides an API for managing the collection of music player backends for
different track types. To start with, lets create the skeleton of the
package & its constructor. The constructor of the super type,
Net::DBus::Object expects to be given to parameters, a handle to the
Net::DBus::Service owning the object, and a path under which the object
shall be exported. Since the manager class is intended to be a singleton
object, we can hard code the path to it within the constructor:

      package Music::Player::Manager;

      use base qw(Net::DBus);

      sub new {
          my $class = shift;
          my $service = shift;
          my $self = $class->SUPER::new($service, "/music/player/manager");

          bless $self, $class;

          return $self;
      }

      1;

Now, as mentioned, the manager with handle a number of different player
backends. So we need to provide methods for registering new backends,
and querying for backends capable of playing a particular file type. So
modifying the above code we add a hash table in the constructor, to
store the backends:

      sub new {
          my $class = shift;
          my $service = shift;
          my $self = $class->SUPER::new($service, "/music/player/manager");

          $self->{backends} = {};

          bless $self, $class;

          return $self;
      }

And now a method to register a new backend. This takes a Perl module
name and uses it to instantiate a backend. Since the backends are also
going to be DBus objects, we need to pass in a reference to the service
we are attached to, along with a path under which to register the
backend. We use the “get_service” method to retreieve a reference to the
service the manager is attached to, and attach the player backend to
this same service: When a method on DBus object is invoked, the first
parameter is the object reference ($self), and the remainder are the
parameters provided to the method call. Thus writing a method
implementation on a DBUs is really no different to normal object
oriented Perl (cf perltoot):

      sub register_backend {
          my $self = shift;
          my $name = shift;
          my $module = shift;

          eval "use $module";
          if ($@) {
              die "cannot load backend $module: $@" ;
          }

          $self->{backends} = $module->new($self->get_service,
                                           "/music/player/backend/$name");
      }

Looking at this one might wonder what happens if the “die” method is
triggered. In such a scenario, rather than terminating the service
process, the error will be caught and propagated back to the remote
caller to deal with.

The player backends provide a method “get_track_types” which returns an
array reference of the music track types they support. We can use this
method to provide an API to allow easy retrieval of a backend for a
particular track type. This method will return a path with which the
backend object can be accessed

      sub find_backend {
          my $self = shift;
          my $extension = shift;

          foreach my $name (keys %{$self->{backends}}) {
             my $backend = $self->{backends}->{$name};
             foreach my $type (@{$backend->get_track_types}) {
                if ($type eq $extension) {
                    return $backend->get_object_path;
                }
             }
          }

          die "no backend for type $extension";
      }

Lets take a quick moment to consider how this method would be used to
play a music track. Now, we have an MP3 file which
we wish to play, so we search for the path to a backend, then retrieve
the object for it, and play the track:

      ...get the music player service...
      # Ask for a path to a player for mp3 files
      my $path = $service->find_backend("mp3");
      # $path now contains '/music/player/backend/mpg123'
      # and we can get the backend object
      my $backend = $service->get_object($path);
      # and finally play the track
      $backend->play("/vol/music/beck/guero/09-scarecrow.mp3");

PROVIDING INTROSPECTION DATA

The code above is a complete working object, ready to be registered with
a service, and since the parameters and return values for the two
methods are both simple strings we could stop there. In some cases,
however, one might want to be more specific about data types expected
for parameters, for example signed vs unsigned integers. Adding explicit
data typing also makes interaction with other programming languages more
reliable. Providing explicit data type defintions for exported method is
known in the DBus world as “Introspection”, and it makes life much more
reliable for users of one’s service whom may be using a strongly typed
language such as C.

The first step in providing introspection data for a DBus object in
Perl, is to specify the name of the interface provided by the object.
This is typically a period separated string, by convention containing
the domain name of the application as its first component. Since most
Perl modules end up living on CPAN, one might use “org.cpan” as the
first component, followed by the package name of the module (replacing
:: with .), eg “org.cpan.music.player.manager”. If it is not planned to
host the module on CPAN, a personal/project domain might be used eg
“com.berrange.music.player.manager”. The interface for an object is
defined by loading the Net::DBus::Exporter module, providing the
interface as its first parameter. So the earlier code example would be
modified to look like:

      package Music::Player::Manager;

      use base qw(Net::DBus);
      use Net::DBus::Exporter qw(com.berrange.music.player.manager)

Next up, it is neccessary to provide data types for the parameters and
return values of the methods. The Net::DBus::Exporter module provides a
method “dbus_method” for this purpose, which takes three parameter, the
name of the method being exported, an array reference of parameter
types, and an array reference of return types (the latter can be omitted
if there are no return values). This can be called at any point in the
module’s code, but by convention it is preferrable to associate calls to
“dbus_method” with the actual method implementation, thus:

      dbus_method("register_backend", ["string", "string"]);
      sub register_backend {
          my $self = shift;
          my $name = shift;
          my $module = shift;

          .. snipped rest of method body ...
      }

And, thus:

      dbus_method("find_backend", ["string"], ["string"])
      sub find_backend {
          my $self = shift;
          my $extension = shift;
          ... snip method body...
      }

DEFINING A SERVICE

Now that the objects have been written, it is time to define a service.
A service is nothing more than a well known name for a given API
contract. A contract can be thought of as a definition of a list of
object paths, and the corresponding interfaces they provide. So, someone
else could come along a provide an alternate music player implementation
using the Python or QT bindings for DBus, and if they provided the same
set of object paths & interfaces, they could justifiably register the
same service on the bus.

The Net::DBus::Service module provides the means to register a service.
Its constructor expects a reference to the bus object (an instance of
Net::DBus), along with the name of the service. As with interface names,
the first component of a service name is usually derived from a domain
name, and then suffixed with the name of the application, in our example
forming “org.cpan.Music.Player”. While some objects will be created on
the fly during execution of the application, others are created upon
initial startup. The music player manager object created earlier in this
tutorial is an example of the latter. It is typical to instantiate and
register these objects in the constructor for the service. Thus a
service object for the music player application would look like:

        package Music::Player;

        use base qw(Net::DBus::Service);

        sub new {
            my $class = shift;
            my $bus = shift;
            my $self = $class->SUPER::new($bus, "org.cpan.music.player");

            bless $self, $class;

            $self->{manager} = Music::Player::Manager->new($self);

            return $self;
        }

CONNECTING TO THE BUS

The final step in getting our service up and running is to connect it to
the bus. This brings up an interesting conundrum, does one export the
service on the system bus (shared by all users & processes on the
machine), or the session bus (one per user logged into a machine). In
some cases the answer, with only one of the two buses conceptually
making sense. In other cases, however, both the session & system bus are
valid. In the former one would use the “session” or methods on
Net::DBus to get a handle to the desired bus, while in the latter case,
the “find” method would be used. This applies a heuristic to determine
the correct bus based on execution environment. In the case of the music
player, either bus is relevant, so the code to connect the service to
the bus would look like:

       use Net::DBus;

       my $bus = Net::DBus->find;
       my $player = Music::Player->new($bus);

With the service attached to the bus, it is merely neccessary to run the
main event processing loop to listen out for & handle incoming DBus
messages. So the above code is modified to start a simple reactor:

       use Net::DBus;
       use Net::DBus::Reactor;

       my $bus = Net::DBus->find;
       my $player = Music::Player->new($bus);

       Net::DBus::Reactor->main->run;

       exit 0;

Saving this code into a script “/usr/bin/music-player.pl”, coding is
complete and the service ready for use by clients on the bus.

SERVICE ACTIVATION

One might now wonder how best to start the service, particularly if it
is a service capable of running on both the system and session buses.
DBus has the answer in the concept of “activation”. What happens is that
when a client on the bus attempts to call a method, or register a signal
handler against, a service not currently running, it will first try and
start the service. Service’s which wish to participate in this process
merely need stick a simple service definition file into the directoy
“/usr/share/dbus-1/services”. The file should be named to match the
service name, with the file extension “.service” appended. eg,
“/usr/share/dbus-1/services/org.cpan.music.player.service” The file
contains two keys, first the name of the service, and second the name of
the executable used to run the service, or in this case the Perl script.
So, for our simple service the data file would contain:

      [D-BUS Service]
      Name=org.cpan.music.player
      Exec=/usr/bin/music-player.pl

Anarchy In The UK

Posted: October 2nd, 2005 | Filed under: Uncategorized | No Comments »

Actually I really mean Liberty in the UK. Following my previous blog I went off and signed up as a member, making an additional sizable donation on top of the basic membership charge. I’m not going to sit by and watch this country turn into a police state, even given the horrors of terrorism showing themselves again in Bali this weekend. Charles Clark’s plans to outlaw the ‘glorification of terrorism’ will have sod all impact on terrorism, rather directly impacting on freedom of speech. All they could achieve would be to drive the ‘glorification’ further underground where it’ll be even harder to detect until it strikes again. Again, the only long term viable way to fight terrorism is to address the causes, rather than the symptoms. Sadly this isn’t a task which quick or easy results, thus isn’t all that ‘sexy’ for a politician looking for a quick win to illustrate how ‘tough’ on terrorism the government is.

A few (not so) recent tunes

Posted: October 2nd, 2005 | Filed under: Uncategorized | No Comments »

So over the summer i’ve been on a bit of a retro kick, picking up CD’s for INXS’ albums X and Kick, Texas’ Southside, a several by Nina Simone, and several more artists. And then (a year late to this particular party) I came across Franz Ferdinand’s self titled debut. The high energy songs really get you feeling good, and were just what I’ve been looking for recently. Best of all, their new album is out tomorrow, but for the rest of this evening its time for Elvis! Don’t ever say my musical tastes/moods are predictable :-)

Utterly unacceptable violation of personal liberties

Posted: September 27th, 2005 | Filed under: Uncategorized | No Comments »

This Guardian article highlights the utterly unacceptable violation of personal liberties taking place under the banner of ‘fighting terrorism’. If one puts aside the pathetic grounds on which police suspected David Mery of being a terrorist, what really makes me mad as hell is the fact the fact that even without any evidence of terrorist involvement, the Police will keep details of the arrest on their computers for ever more & happily share this information which countries around the world.

the police are not only entitled to keep my fingerprints and DNA samples, but according to my solicitor, they are also entitled to hold on to what they gather during their investigation: notepads of arresting officers, photographs, interviewing tapes and any other documents they entered in the police national computer (PNC). So even though the police consider me innocent there will remain some mention (what exactly?) in the PNC and, if they fully share their information with Interpol, in other police databases around the world as well. Isn’t a state that keeps files on innocent persons a police state?

What guarentees of use & data protection would apply if the records were to make their way to, say, the US. With the way US immigration policy is going, it is not at all far fetched that the police records could lead of all manner of complications visiting the US. All this the consquences of so called “terrorist” behaviour

  • They found my behaviour suspicious from direct observation and then from watching me on the CCTV system
  • I went into the station without looking at the police officers at the entrance or by the gates
  • Two other men entered the station at about the same time as me
  • I am wearing a jacket “too warm for the season”
  • I am carrying a bulky rucksack, and kept my rucksack with me at all times
  • I looked at people coming on the platform
  • I played with my phone and then took a paper from inside my jacket

Pretty much every commuter in London would match these criteria several times a week. It is an absolutely unacceptable violation of personal liberties for such criteria to lead to a permanent record of an innocent person being a potential “terrorist” suspect. I’ve been considering it for a while, but this just highlights that its way past time for one to join Liberty. Right now.

Recovering encryption keys via acoustic analysis

Posted: September 13th, 2005 | Filed under: Uncategorized | No Comments »

Have just been reading this article about how, given a 15 minute recording of a user typing, it is possible to ‘recover’ details of every word & even letter typed. Each key has a subtly different sound, so given a mapping of sound <-> key it is possible analyse a recording to recover the letters / words typed. By assuming that the text being typed was, say English, it is possible to apply statistical to the 15 minute recording to generate the sound <-> key mappings. Combine the two techniques with a suitably planted microphone and you have a pretty damn good channel for covertly monitoring what someone types. Another good reason to stop using regular passwords & move wholesale to one time keys / secure id generators – as if existing spyware weren’t enough of a reason already.

Even more intriguing though, is the last paragraph where they link to another study suggesting that, since CPUs make different sounds (high frequency, inaudible to humans) depending on what they are computing, that it might be possible to analyse a recording of a decryption operation to recover the encryption keys. Damn, encryption & security breaks down in the most obscure & unimaginable/unexpected ways.