Skip to content

Instantly share code, notes, and snippets.

@vi
Created April 5, 2012 02:44
Show Gist options
  • Save vi/2307606 to your computer and use it in GitHub Desktop.
Save vi/2307606 to your computer and use it in GitHub Desktop.
Simple DBUS service that calls your command lines and returns their output
#!/usr/bin/perl -w
# Vitaly "_Vi" Shukela 2012 License=MIT
use strict;
package DbusShellService;
use Net::DBus::Exporter qw(org.vi_server.DbusShellService);
use base qw(Net::DBus::Object);
use POSIX qw(dup2);
sub new {
my $class = shift;
my $service = shift;
my $name = shift;
my $self = $class->SUPER::new($service, $name);
bless $self, $class;
return $self;
}
dbus_method("Call", [["array", "string"]], ["string"]);
dbus_method("Call0", [], ["string"]);
dbus_method("Call1", ["string"], ["string"]);
dbus_method("Call2", ["string", "string"], ["string"]);
dbus_method("Call3", ["string", "string", "string"], ["string"]);
dbus_method("Call4", ["string", "string", "string", "string"], ["string"]);
dbus_method("Call5", ["string", "string", "string", "string", "string"], ["string"]);
sub Call0 { my $self = shift; $self->Call(\@_); }
sub Call1 { my $self = shift; $self->Call(\@_); }
sub Call2 { my $self = shift; $self->Call(\@_); }
sub Call3 { my $self = shift; $self->Call(\@_); }
sub Call4 { my $self = shift; $self->Call(\@_); }
sub Call5 { my $self = shift; $self->Call(\@_); }
sub Call {
my $self = shift;
my $aref = shift;
my @a = (@ARGV, @$aref);
pipe READ, WRITE;
if(!fork) {
close READ;
dup2(fileno(WRITE),1);
close WRITE;
exec @a;
}
close WRITE;
local $/;
my $str = <READ>;
close READ;
return $str;
}
package main;
use Net::DBus;
use Net::DBus::Reactor;
if ($#ARGV < 0) {
print STDERR "Usage: dbus-shell-service.pl service_name command_line_arguments...
Example:
dbus-shell-service.pl org.example.Test /usr/bin/printf '%s Q %s\\n' &
qdbus org.example.Test / org.vi_server.DbusShellService.Call2 s1 s2
s1 Q s2\n\n";
exit 1;
}
my $name=shift @ARGV;
my $bus = Net::DBus->session;
my $service = $bus->export_service($name);
my $object = DbusShellService->new($service, "/");
my $reactor = Net::DBus::Reactor->main();
$reactor->run();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment