File Coverage

blib/lib/Protocol/DBus/Client.pm
Criterion Covered Total %
statement 65 92 70.6
branch 14 38 36.8
condition 5 17 29.4
subroutine 17 19 89.4
pod 7 8 87.5
total 108 174 62.0


line stmt bran cond sub pod time code
1             package Protocol::DBus::Client;
2              
3 6     6   775061 use strict;
  6         23  
  6         145  
4 6     6   30 use warnings;
  6         6  
  6         154  
5              
6             =encoding utf-8
7              
8             =head1 NAME
9              
10             Protocol::DBus::Client
11              
12             =head1 SYNOPSIS
13              
14             my $dbus = Protocol::DBus::Client::system();
15              
16             $dbus->initialize();
17              
18             =head1 DESCRIPTION
19              
20             This is the end class for use in DBus client applications. It subclasses
21             L.
22              
23             B This module will automatically send a “Hello” message after
24             authentication completes. That message’s response will be processed
25             automatically. Because this is part of the protocol’s handshake
26             logic rather than something useful for callers, it is abstracted away from
27             the caller. It is neither necessary nor productive for callers to send a
28             “Hello” message.
29              
30             =cut
31              
32 6     6   25 use parent 'Protocol::DBus::Peer';
  6         7  
  6         25  
33              
34 6     6   2300 use Protocol::DBus::Authn;
  6         17  
  6         155  
35 6     6   1927 use Protocol::DBus::Connect;
  6         12  
  6         144  
36 6     6   1911 use Protocol::DBus::Path;
  6         12  
  6         4191  
37              
38             =head1 STATIC FUNCTIONS
39              
40             =head2 system()
41              
42             Creates an instance of this class that includes a connection to the
43             system’s message bus.
44              
45             This does not do authentication; you’ll need to do that via the class’s
46             methods.
47              
48             =cut
49              
50             sub system {
51 1     1 1 1126 my @addrs = Protocol::DBus::Path::system_message_bus();
52              
53 1         3 return _create_local(@addrs);
54             }
55              
56             =head2 login_session()
57              
58             Like C but for the login session’s message bus.
59              
60             =cut
61              
62             sub login_session {
63 1     1 1 2965 my @addrs = Protocol::DBus::Path::login_session_message_bus();
64              
65 0 0       0 if (!@addrs) {
66 0         0 die "Failed to identify login system message bus!";
67             }
68              
69 0         0 return _create_local(@addrs);
70             }
71              
72             sub _create_local {
73 1     1   2 my ($addr) = @_;
74 1         4 my ($socket, $bin_addr) = Protocol::DBus::Connect::create_socket($addr);
75              
76 1         5 return __PACKAGE__->new(
77             socket => $socket,
78             address => $bin_addr,
79             human_address => $addr->to_string(),
80             authn_mechanism => 'EXTERNAL',
81             );
82             }
83              
84             #----------------------------------------------------------------------
85              
86             =head1 METHODS
87              
88             =head2 $done_yn = I->initialize()
89              
90             This returns truthy once the connection is ready to use and falsy until then.
91             In blocking I/O contexts the call will block.
92              
93             Note that this automatically handles D-Bus’s initial C message and
94             its response.
95              
96             Previously this function was called C and did not wait for
97             the C message’s response. The older name is retained
98             as an alias for backward compatibility.
99              
100             =cut
101              
102             sub initialize {
103 3     3 1 55 my ($self) = @_;
104              
105 3 50 33     10 if ($self->_connect() && $self->{'_authn'}->go()) {
106 2   33     14 $self->{'_sent_hello'} ||= do {
107 2         20 my $connection_name_sr = \$self->{'_connection_name'};
108              
109             $self->send_call(
110             path => '/org/freedesktop/DBus',
111             interface => 'org.freedesktop.DBus',
112             destination => 'org.freedesktop.DBus',
113             member => 'Hello',
114 2     2   49 )->then( sub { $$connection_name_sr = $_[0]->get_body()->[0]; } );
  2         209  
115             };
116              
117 2 50       75 if (!$self->{'_connection_name'}) {
118             GET_MESSAGE: {
119 2 50       23 if (my $msg = $self->SUPER::get_message()) {
  4         18  
120 4 100       86 return 1 if $self->{'_connection_name'};
121              
122 2         4 push @{ $self->{'_pending_received_messages'} }, $msg;
  2         5  
123              
124 2         7 redo GET_MESSAGE;
125             }
126             }
127             }
128             }
129              
130 0 0       0 return $self->{'_connection_name'} ? 1 : 0;
131             }
132              
133             sub _connect {
134 3     3   6 my ($self) = @_;
135              
136 3         68 local $!;
137              
138 3 100       13 if (!$self->{'_connected'}) {
139 1   33     3 $self->{'_sent_connect'} ||= do {
140 1 50       1484 if ( connect $self->{'_socket'}, $self->{'_address'} ) {
    50          
141 0         0 $self->{'_connected'} = 1;
142             }
143             elsif (!$!{'EINPROGRESS'}) {
144 1         54 die "connect($self->{'_human_address'}): $!";
145             }
146             };
147             }
148              
149 2 50       12 if (!$self->{'_connected'}) {
150              
151             # This non-blocking connect logic will ordinarily be unneeded
152             # since even in non-blocking mode a UNIX socket connect() doesn’t
153             # normally block. Where such a connect() *will* have to wait is
154             # when the server has no more space for a new connection.
155              
156 0         0 my $mask = q<>;
157 0         0 vec( $mask, fileno $self->{'_socket'}, 1 ) = 1;
158              
159 0         0 my $got = select undef, $mask, undef, 0;
160              
161 0 0       0 if ($got > 0) {
162 0         0 my $errno = getsockopt( $self->{'_socket'}, Socket::SOL_SOCKET(), Socket::SO_ERROR() );
163 0 0       0 if (!defined $errno) {
164 0         0 die "getsockopt(SOL_SOCKET, SO_ERROR): $!";
165             }
166              
167 0         0 local $! = unpack 'I', $errno;
168              
169 0 0       0 if (0 + $!) {
170 0         0 die "connect($self->{'_human_address'}): $!";
171             }
172             else {
173 0         0 $self->{'_connected'} = 1;
174             }
175             }
176             }
177              
178 2         29 return $self->{'_connected'};
179             }
180              
181             *do_authn = *initialize;
182              
183             #----------------------------------------------------------------------
184              
185             =head2 $yn = I->init_pending_send()
186              
187             This indicates whether there is data queued up to send for the initialization.
188             Only useful with non-blocking I/O.
189              
190             This function was previously called C; the former
191             name is retained for backward compatibility.
192              
193             =cut
194              
195             sub init_pending_send {
196 0     0 1 0 my ($self) = @_;
197              
198 0 0       0 if ($self->{'_connection_name'}) {
199 0         0 die "Don’t call this after initialize() is done!";
200             }
201              
202 0 0 0     0 return 1 if $self->{'_sent_connect'} && !$self->{'_connected'};
203              
204 0 0       0 if ($self->{'_sent_hello'}) {
205 0         0 return $self->pending_send();
206             }
207              
208 0         0 return $self->{'_authn'}->pending_send();
209             }
210              
211             *authn_pending_send = \*init_pending_send;
212              
213             #----------------------------------------------------------------------
214              
215             =head2 $yn = I->supports_unix_fd()
216              
217             Boolean that indicates whether this client supports UNIX FD passing.
218              
219             (See the main L documentation for details about
220             support for UNIX FD passing.)
221              
222             =cut
223              
224             sub supports_unix_fd {
225 0     0 1 0 my ($self) = @_;
226              
227 0         0 return $self->{'_authn'}->negotiated_unix_fd();
228             }
229              
230             #----------------------------------------------------------------------
231              
232             =head2 $msg = I->get_message()
233              
234             Same as in the base class, but for clients the initial “Hello” message and
235             its response are abstracted
236              
237             =cut
238              
239             sub get_message {
240 2     2 1 5 my ($self) = @_;
241              
242 2 50       8 die "initialize() is not finished!" if !$self->{'_connection_name'};
243              
244 2 50 33     8 if ($self->{'_pending_received_messages'} && @{ $self->{'_pending_received_messages'} }) {
  2         8  
245 2         4 return shift @{ $self->{'_pending_received_messages'} };
  2         7  
246             }
247              
248 6     6   41 no warnings 'redefine';
  6         7  
  6         592  
249 0         0 *get_message = Protocol::DBus::Peer->can('get_message');
250              
251 0         0 return $_[0]->get_message();
252             }
253              
254             =head2 $name = I->get_unique_bus_name()
255              
256             Returns the connection’s unique bus name.
257              
258             C is a historical alias for this method.
259              
260             =cut
261              
262             sub get_unique_bus_name {
263 2   50 2 1 70 return $_[0]->{'_connection_name'} || die 'No connection name known yet!';
264             }
265              
266             BEGIN {
267 6     6   799 *get_connection_name = *get_unique_bus_name;
268             }
269              
270             # undocumented for now
271             sub new {
272 3     3 0 1023723 my ($class, %opts) = @_;
273              
274             my $authn = Protocol::DBus::Authn->new(
275             socket => $opts{'socket'},
276 3         140 mechanism => $opts{'authn_mechanism'},
277             );
278              
279 3         85 my $self = $class->SUPER::new( $opts{'socket'} );
280              
281 3         25 $self->{'_authn'} = $authn;
282              
283 3 100       26 if (my $address = $opts{'address'}) {
284 1         8 $self->{'_address'} = $address;
285 1         4 $self->{'_human_address'} = $opts{'human_address'};
286             }
287             else {
288 2         10 $self->{'_connected'} = 1;
289             }
290              
291 3         17 return $self;
292             }
293              
294             #sub DESTROY {
295             # print "DESTROYED: [$_[0]]\n";
296             #}
297              
298             1;