本文介绍了使用Perl Net :: DBus启动DBus的会话总线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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;

我正在通过ssh连接并使用:

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)

当我尝试启动 test.pl 时,出现错误:

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

此行引发此错误:

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

Google向我提示了 dbus启动 。我执行了 yum install dbus-x11

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.

阅读手册后,我检测到DBUS会话守护程序未启动,并且我的ENV变量DBUS_SESSION_BUS_ADDRESS为空:

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

然后我执行:

[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是空的。

But DBUS_SESSION_BUS_ADDRESS is same empty.

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

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?

推荐答案

首先,您需要 eval dbus启动输出。像这样:

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

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

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.

这篇关于使用Perl Net :: DBus启动DBus的会话总线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-26 12:35