Start the session bus of DBus with Perl Net::DBus

烂漫一生 提交于 2019-12-08 00:39:32

问题


I am using Perl and the Net::DBus module. I wrote a simple test program:

#!/usr/bin/perl
use strict;
use warnings;

package MyObj;
use Net::DBus::Exporter qw(org.example.Tao);
use base qw(Net::DBus::Object);

sub new {
    my $class = shift;
    my $service = shift;
    my $self = $class->SUPER::new($service, '/MyObj');
    bless $self, $class;
    return $self;
}

dbus_method("Hello", ["string"]);

sub Hello {
    return 'Hello';
}

package main;
use Net::DBus;
use Net::DBus::Reactor;

my $bus = Net::DBus->session;
my $service = $bus->export_service("org.example.Tao");
my $object = MyObj->new($service);
my $reactor = Net::DBus::Reactor->main();
$reactor->run();

return 0;

I am connecting by ssh and using:

Perl, v5.8.8 built for x86_64-linux-thread-multi
Linux example.com 2.6.32.19-0.2.99.17.22250fd-xen #1 SMP 2010-09-13 10:16:50 +0200 x86_64 x86_64 x86_64 GNU/Linux
CentOS release 5.4 (Final)

When I try to start my test.pl, I get the error:

org.freedesktop.DBus.Error.Spawn.ExecFailed:
Failed to execute dbus-launch to autolaunch D-Bus session

This error is raised by this line:

my $bus = Net::DBus->session;

Google hinted to me about dbus-launch. I executed yum install dbus-x11.

I try start my test code again and get error in the same line:

org.freedesktop.DBus.Error.Spawn.ExecFailed: 
dbus-launch failed to autolaunch D-Bus session: 
Autolaunch error: X11 initialization failed.

After read manuals, I detect that DBUS session daemon isn't started and my ENV var DBUS_SESSION_BUS_ADDRESS is empty:

[root@zion perl]# ps ax|grep dbus|grep -v grep
1019 ?        Ss     0:00 dbus-daemon --system

Then I exec:

[root@zion perl]# dbus-launch --sh-syntax
DBUS_SESSION_BUS_ADDRESS='unix:abstract=/tmp/dbus-smHadq6yxV,guid=101ccd74fb75ae501485ed004e2a9043';
export DBUS_SESSION_BUS_ADDRESS;
DBUS_SESSION_BUS_PID=5037;
[root@zion perl]# ps ax|grep dbus|grep -v grep
1019 ?        Ss     0:00 dbus-daemon --system
5037 ?        Ss     0:00 /bin/dbus-daemon --fork --print-pid 4 --print-address 6 --session

But DBUS_SESSION_BUS_ADDRESS is same empty.

Question: I need simple two Perl apps. The first app registers the dbus session service. Another app using my registered service. What is the best and correct way to do it in my environment?


回答1:


First of all, you need to eval dbus-launch output. Like this:

$ env | grep DBUS
(empty output; no DBUS session bus launched yet)
$ eval `dbus-launch --sh-syntax`
(empty output; DBUS session bus started, output is evaluated to set shell vars)
$ env | grep DBUS
DBUS_SESSION_BUS_ADDRESS=unix:abstract=/tmp/dbus-ZkMjn6B47b,guid=85b2da814a8a061d4e7a55004e35b499

Second, you should consider how are you going to use your apps. Try to answer yourself these questions: why are you trying to bind to session bus (which is by definition a bus, associated with an interactive user session)? If this is a system-wide service, it should bind to system bus. If it is a user service, user session manager should take care of starting dbus session bus.



来源:https://stackoverflow.com/questions/6799472/start-the-session-bus-of-dbus-with-perl-netdbus

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!