| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package Protocol::DBus::Client; | 
| 2 |  |  |  |  |  |  |  | 
| 3 | 4 |  |  | 4 |  | 425740 | use strict; | 
|  | 4 |  |  |  |  | 15 |  | 
|  | 4 |  |  |  |  | 85 |  | 
| 4 | 4 |  |  | 4 |  | 19 | use warnings; | 
|  | 4 |  |  |  |  | 5 |  | 
|  | 4 |  |  |  |  | 101 |  | 
| 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 | 4 |  |  | 4 |  | 17 | use parent 'Protocol::DBus::Peer'; | 
|  | 4 |  |  |  |  | 4 |  | 
|  | 4 |  |  |  |  | 23 |  | 
| 33 |  |  |  |  |  |  |  | 
| 34 | 4 |  |  | 4 |  | 1394 | use Protocol::DBus::Authn; | 
|  | 4 |  |  |  |  | 9 |  | 
|  | 4 |  |  |  |  | 113 |  | 
| 35 | 4 |  |  | 4 |  | 1142 | use Protocol::DBus::Connect; | 
|  | 4 |  |  |  |  | 8 |  | 
|  | 4 |  |  |  |  | 97 |  | 
| 36 | 4 |  |  | 4 |  | 1172 | use Protocol::DBus::Path; | 
|  | 4 |  |  |  |  | 8 |  | 
|  | 4 |  |  |  |  | 2418 |  | 
| 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 | 1159 | 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 | 2934 | 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 |  | 3 | my ($addr) = @_; | 
| 74 | 1 |  |  |  |  | 4 | my ($socket, $bin_addr) = Protocol::DBus::Connect::create_socket($addr); | 
| 75 |  |  |  |  |  |  |  | 
| 76 | 1 |  |  |  |  | 6 | 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 | 2 |  |  | 2 | 1 | 16 | my ($self) = @_; | 
| 104 |  |  |  |  |  |  |  | 
| 105 | 2 | 50 | 33 |  |  | 7 | if ($self->_connect() && $self->{'_authn'}->go()) { | 
| 106 | 1 |  | 33 |  |  | 4 | $self->{'_sent_hello'} ||= do { | 
| 107 | 1 |  |  |  |  | 14 | 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 | 1 |  |  | 1 |  | 25 | )->then( sub { $$connection_name_sr = $_[0]->get_body()->[0]; } ); | 
|  | 1 |  |  |  |  | 104 |  | 
| 115 |  |  |  |  |  |  | }; | 
| 116 |  |  |  |  |  |  |  | 
| 117 | 1 | 50 |  |  |  | 47 | if (!$self->{'_connection_name'}) { | 
| 118 |  |  |  |  |  |  | GET_MESSAGE: { | 
| 119 | 1 | 50 |  |  |  | 11 | if (my $msg = $self->SUPER::get_message()) { | 
|  | 2 |  |  |  |  | 17 |  | 
| 120 | 2 | 100 |  |  |  | 45 | return 1 if $self->{'_connection_name'}; | 
| 121 |  |  |  |  |  |  |  | 
| 122 | 1 |  |  |  |  | 2 | push @{ $self->{'_pending_received_messages'} }, $msg; | 
|  | 1 |  |  |  |  | 3 |  | 
| 123 |  |  |  |  |  |  |  | 
| 124 | 1 |  |  |  |  | 4 | redo GET_MESSAGE; | 
| 125 |  |  |  |  |  |  | } | 
| 126 |  |  |  |  |  |  | } | 
| 127 |  |  |  |  |  |  | } | 
| 128 |  |  |  |  |  |  | } | 
| 129 |  |  |  |  |  |  |  | 
| 130 | 0 | 0 |  |  |  | 0 | return $self->{'_connection_name'} ? 1 : 0; | 
| 131 |  |  |  |  |  |  | } | 
| 132 |  |  |  |  |  |  |  | 
| 133 |  |  |  |  |  |  | sub _connect { | 
| 134 | 2 |  |  | 2 |  | 3 | my ($self) = @_; | 
| 135 |  |  |  |  |  |  |  | 
| 136 | 2 |  |  |  |  | 46 | local $!; | 
| 137 |  |  |  |  |  |  |  | 
| 138 | 2 | 100 |  |  |  | 8 | if (!$self->{'_connected'}) { | 
| 139 | 1 |  | 33 |  |  | 11 | $self->{'_sent_connect'} ||= do { | 
| 140 | 1 | 50 |  |  |  | 997 | if ( connect $self->{'_socket'}, $self->{'_address'} ) { | 
|  |  | 50 |  |  |  |  |  | 
| 141 | 0 |  |  |  |  | 0 | $self->{'_connected'} = 1; | 
| 142 |  |  |  |  |  |  | } | 
| 143 |  |  |  |  |  |  | elsif (!$!{'EINPROGRESS'}) { | 
| 144 | 1 |  |  |  |  | 41 | die "connect($self->{'_human_address'}): $!"; | 
| 145 |  |  |  |  |  |  | } | 
| 146 |  |  |  |  |  |  | }; | 
| 147 |  |  |  |  |  |  | } | 
| 148 |  |  |  |  |  |  |  | 
| 149 | 1 | 50 |  |  |  | 3 | 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 | 1 |  |  |  |  | 7 | 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 | 1 |  |  | 1 | 1 | 3 | my ($self) = @_; | 
| 241 |  |  |  |  |  |  |  | 
| 242 | 1 | 50 |  |  |  | 4 | die "initialize() is not finished!" if !$self->{'_connection_name'}; | 
| 243 |  |  |  |  |  |  |  | 
| 244 | 1 | 50 | 33 |  |  | 10 | if ($self->{'_pending_received_messages'} && @{ $self->{'_pending_received_messages'} }) { | 
|  | 1 |  |  |  |  | 5 |  | 
| 245 | 1 |  |  |  |  | 3 | return shift @{ $self->{'_pending_received_messages'} }; | 
|  | 1 |  |  |  |  | 3 |  | 
| 246 |  |  |  |  |  |  | } | 
| 247 |  |  |  |  |  |  |  | 
| 248 | 4 |  |  | 4 |  | 25 | no warnings 'redefine'; | 
|  | 4 |  |  |  |  | 5 |  | 
|  | 4 |  |  |  |  | 403 |  | 
| 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 | 1 |  | 50 | 1 | 1 | 37 | return $_[0]->{'_connection_name'} || die 'No connection name known yet!'; | 
| 264 |  |  |  |  |  |  | } | 
| 265 |  |  |  |  |  |  |  | 
| 266 |  |  |  |  |  |  | BEGIN { | 
| 267 | 4 |  |  | 4 |  | 451 | *get_connection_name = *get_unique_bus_name; | 
| 268 |  |  |  |  |  |  | } | 
| 269 |  |  |  |  |  |  |  | 
| 270 |  |  |  |  |  |  | # undocumented for now | 
| 271 |  |  |  |  |  |  | sub new { | 
| 272 | 2 |  |  | 2 | 0 | 3595 | my ($class, %opts) = @_; | 
| 273 |  |  |  |  |  |  |  | 
| 274 |  |  |  |  |  |  | my $authn = Protocol::DBus::Authn->new( | 
| 275 |  |  |  |  |  |  | socket => $opts{'socket'}, | 
| 276 | 2 |  |  |  |  | 160 | mechanism => $opts{'authn_mechanism'}, | 
| 277 |  |  |  |  |  |  | ); | 
| 278 |  |  |  |  |  |  |  | 
| 279 | 2 |  |  |  |  | 64 | my $self = $class->SUPER::new( $opts{'socket'} ); | 
| 280 |  |  |  |  |  |  |  | 
| 281 | 2 |  |  |  |  | 11 | $self->{'_authn'} = $authn; | 
| 282 |  |  |  |  |  |  |  | 
| 283 | 2 | 100 |  |  |  | 13 | if (my $address = $opts{'address'}) { | 
| 284 | 1 |  |  |  |  | 2 | $self->{'_address'} = $address; | 
| 285 | 1 |  |  |  |  | 3 | $self->{'_human_address'} = $opts{'human_address'}; | 
| 286 |  |  |  |  |  |  | } | 
| 287 |  |  |  |  |  |  | else { | 
| 288 | 1 |  |  |  |  | 13 | $self->{'_connected'} = 1; | 
| 289 |  |  |  |  |  |  | } | 
| 290 |  |  |  |  |  |  |  | 
| 291 | 2 |  |  |  |  | 11 | return $self; | 
| 292 |  |  |  |  |  |  | } | 
| 293 |  |  |  |  |  |  |  | 
| 294 |  |  |  |  |  |  | #sub DESTROY { | 
| 295 |  |  |  |  |  |  | #    print "DESTROYED: [$_[0]]\n"; | 
| 296 |  |  |  |  |  |  | #} | 
| 297 |  |  |  |  |  |  |  | 
| 298 |  |  |  |  |  |  | 1; |