【问题标题】:Start the session bus of DBus with Perl Net::DBus用 Perl Net::DBus 启动 DBus 的会话总线
【发布时间】:2011-07-23 09:20:46
【问题描述】:

我正在使用 Perl 和 Net::DBus 模块。我写了一个简单的测试程序:

#!/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;

我通过 ssh 连接并使用:

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)

当我尝试启动我的 test.pl 时,我收到错误:

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

这一行引发了这个错误:

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

Google 向我暗示了 dbus-launch。我执行了yum install dbus-x11

我尝试再次启动我的测试代码并在同一行出现错误:

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

阅读手册后,我发现 DBUS 会话守护程序没有启动,并且我的 ENV var DBUS_SESSION_BUS_ADDRESS 为空:

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

然后我执行:

[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

但是 DBUS_SESSION_BUS_ADDRESS 也是空的。

问题: 我需要两个简单的 Perl 应用程序。第一个应用注册 dbus 会话服务。另一个使用我注册服务的应用程序。在我的环境中最好和正确的方法是什么?

【问题讨论】:

  • 你问了三个不同的问题。 Stack Overflow 那样不好用,所以我删除了最后两个。其他主题请open new questions,如需文字请见revisions
  • 感谢标题更正,但我使用的是 CentOS,而不是 Fedora。我回滚到修订并删除不重要的问题。

标签: perl dbus


【解决方案1】:

首先,你需要evaldbus-launch 输出。像这样:

$ 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

其次,您应该考虑如何使用您的应用程序。试着回答自己这些问题:为什么要绑定到会话总线(根据定义,它是与交互式用户会话相关联的总线)?如果这是一个系统范围的服务,它应该绑定到系统总线。如果是用户服务,用户会话管理器应该负责启动 dbus 会话总线。

【讨论】:

    猜你喜欢
    • 2012-07-21
    • 2014-05-26
    • 2015-03-16
    • 1970-01-01
    • 2014-09-21
    • 2017-05-07
    • 2011-03-22
    • 2012-01-23
    • 1970-01-01
    相关资源
    最近更新 更多