| 1 |
#!/usr/bin/perl |
| 2 |
|
| 3 |
=head NAME |
| 4 |
|
| 5 |
Debconf::FrontEnd::Passthrough - pass-through meta-frontend for Debconf |
| 6 |
|
| 7 |
=cut |
| 8 |
|
| 9 |
package Debconf::FrontEnd::Passthrough; |
| 10 |
use warnings; |
| 11 |
use strict; |
| 12 |
use Carp; |
| 13 |
use IO::Socket; |
| 14 |
use IO::Handle; |
| 15 |
use IO::Select; |
| 16 |
use Debconf::FrontEnd; |
| 17 |
use Debconf::Element; |
| 18 |
use Debconf::Element::Select; |
| 19 |
use Debconf::Element::Multiselect; |
| 20 |
use Debconf::Log qw(:all); |
| 21 |
use Debconf::Encoding; |
| 22 |
use base qw(Debconf::FrontEnd); |
| 23 |
|
| 24 |
=head1 DESCRIPTION |
| 25 |
|
| 26 |
This is a IPC pass-through frontend for Debconf. It is meant to enable |
| 27 |
integration of Debconf frontend components with installation systems. |
| 28 |
|
| 29 |
The basic idea of this frontend is to replay messages between the |
| 30 |
ConfModule and an arbitrary UI agent. For the most part, messages are |
| 31 |
simply relayed back and forth unchanged. |
| 32 |
|
| 33 |
=head1 METHODS |
| 34 |
|
| 35 |
=over 4 |
| 36 |
|
| 37 |
=item init |
| 38 |
|
| 39 |
Set up the pipe to the UI agent and other housekeeping chores. |
| 40 |
|
| 41 |
=cut |
| 42 |
|
| 43 |
sub init { |
| 44 |
my $this=shift; |
| 45 |
|
| 46 |
# If readfh and writefh were not initialized before (by child class), |
| 47 |
# initialize them from environment |
| 48 |
if (not defined $this->{readfh} or not defined $this->{writefh}) { |
| 49 |
if (not defined $this->init_fh_from_env()) { |
| 50 |
die "Neither DEBCONF_PIPE nor DEBCONF_READFD and DEBCONF_WRITEFD were set\n"; |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
binmode $this->{readfh}, ":utf8"; |
| 55 |
binmode $this->{writefh}, ":utf8"; |
| 56 |
|
| 57 |
$this->{readfh}->autoflush(1); |
| 58 |
$this->{writefh}->autoflush(1); |
| 59 |
|
| 60 |
# Note: SUPER init is not called, since it does several things |
| 61 |
# inappropriate for passthrough frontends, including clearing the capb. |
| 62 |
$this->elements([]); |
| 63 |
$this->interactive(1); |
| 64 |
$this->need_tty(0); |
| 65 |
} |
| 66 |
|
| 67 |
=head2 init_fh_from_env |
| 68 |
|
| 69 |
Initialize file handles from the environment variables: DEBCONF_PIPE - socket, |
| 70 |
DEBCONF_READFD and DEBCONF_WRITEFD - file descriptors of the FIFO pipes. |
| 71 |
|
| 72 |
=cut |
| 73 |
|
| 74 |
sub init_fh_from_env { |
| 75 |
my $this = shift; |
| 76 |
my ($socket_path, $readfd, $writefd); |
| 77 |
|
| 78 |
if (defined $ENV{DEBCONF_PIPE}) { |
| 79 |
my $socket_path = $ENV{DEBCONF_PIPE}; |
| 80 |
$this->{readfh} = $this->{writefh} = IO::Socket::UNIX->new( |
| 81 |
Type => SOCK_STREAM, |
| 82 |
Peer => $socket_path |
| 83 |
) || croak "Cannot connect to $socket_path: $!"; |
| 84 |
return "socket"; |
| 85 |
} elsif (defined $ENV{DEBCONF_READFD} and defined $ENV{DEBCONF_WRITEFD}) { |
| 86 |
$readfd = $ENV{DEBCONF_READFD}; |
| 87 |
$writefd = $ENV{DEBCONF_WRITEFD}; |
| 88 |
$this->{readfh} = IO::Handle->new_from_fd(int($readfd), "r") |
| 89 |
or croak "Failed to open fd $readfd: $!"; |
| 90 |
$this->{writefh} = IO::Handle->new_from_fd(int($writefd), "w") |
| 91 |
or croak "Failed to open fd $writefd: $!"; |
| 92 |
return "fifo"; |
| 93 |
} |
| 94 |
return; |
| 95 |
} |
| 96 |
|
| 97 |
=head2 talk_with_timeout |
| 98 |
|
| 99 |
Communicates with the UI agent. Joins all parameters together to create a |
| 100 |
command, sends it to the agent, and reads and processes its reply. If timeout |
| 101 |
is specified (the first argument), the subroutine will only wait a speficied |
| 102 |
number of seconds for the other end to reply. If timeout occurs, undef will be |
| 103 |
returned. |
| 104 |
|
| 105 |
=cut |
| 106 |
|
| 107 |
sub talk_with_timeout { |
| 108 |
my $this=shift; |
| 109 |
my $timeout=shift; |
| 110 |
my $command=join(' ', map { Debconf::Encoding::to_Unicode($_) } @_); |
| 111 |
my $reply; |
| 112 |
|
| 113 |
my $readfh = $this->{readfh} || croak "Broken pipe"; |
| 114 |
my $writefh = $this->{writefh} || croak "Broken pipe"; |
| 115 |
|
| 116 |
debug developer => "----> (passthrough) $command"; |
| 117 |
print $writefh $command."\n"; |
| 118 |
$writefh->flush; |
| 119 |
|
| 120 |
if (defined $timeout) { |
| 121 |
my $select = IO::Select->new($readfh); |
| 122 |
return if !$select->can_read($timeout); |
| 123 |
} |
| 124 |
return if ($readfh->eof()); |
| 125 |
|
| 126 |
$reply = <$readfh>; |
| 127 |
chomp($reply); |
| 128 |
debug developer => "<---- (passthrough) $reply"; |
| 129 |
my ($tag, $val) = split(' ', $reply, 2); |
| 130 |
$val = '' unless defined $val; |
| 131 |
$val = Debconf::Encoding::convert("UTF-8", $val); |
| 132 |
|
| 133 |
return ($tag, $val) if wantarray; |
| 134 |
return $tag; |
| 135 |
} |
| 136 |
|
| 137 |
=head2 talk |
| 138 |
|
| 139 |
Same as talk_with_timeout() just waits for the answer infinitely. |
| 140 |
|
| 141 |
=cut |
| 142 |
|
| 143 |
sub talk { |
| 144 |
my $this=shift; |
| 145 |
return $this->talk_with_timeout(undef, @_); |
| 146 |
} |
| 147 |
|
| 148 |
=head2 makeelement |
| 149 |
|
| 150 |
This frontend doesn't really make use of Elements to interact with the user, |
| 151 |
so it uses generic Elements as placeholders (except for select and |
| 152 |
multiselect Elements for which it needs translation methods). This method |
| 153 |
simply makes one. |
| 154 |
|
| 155 |
=cut |
| 156 |
|
| 157 |
sub makeelement |
| 158 |
{ |
| 159 |
my $this=shift; |
| 160 |
my $question=shift; |
| 161 |
|
| 162 |
my $type=$question->type; |
| 163 |
if ($type eq "select" || $type eq "multiselect") { |
| 164 |
$type=ucfirst($type); |
| 165 |
return "Debconf::Element::$type"->new(question => $question); |
| 166 |
} else { |
| 167 |
return Debconf::Element->new(question => $question); |
| 168 |
} |
| 169 |
} |
| 170 |
|
| 171 |
=head2 capb_backup |
| 172 |
|
| 173 |
Pass capability information along to the UI agent. |
| 174 |
|
| 175 |
=cut |
| 176 |
|
| 177 |
sub capb_backup |
| 178 |
{ |
| 179 |
my $this=shift; |
| 180 |
my $val = shift; |
| 181 |
|
| 182 |
$this->{capb_backup} = $val; |
| 183 |
$this->talk('CAPB', 'backup') if $val; |
| 184 |
} |
| 185 |
|
| 186 |
=head2 capb |
| 187 |
|
| 188 |
Gets UI agent capabilities. |
| 189 |
|
| 190 |
=cut |
| 191 |
|
| 192 |
sub capb |
| 193 |
{ |
| 194 |
my $this=shift; |
| 195 |
my $ret; |
| 196 |
return $this->{capb} if exists $this->{capb}; |
| 197 |
|
| 198 |
($ret, $this->{capb}) = $this->talk('CAPB'); |
| 199 |
return $this->{capb} if $ret eq '0'; |
| 200 |
} |
| 201 |
|
| 202 |
=head2 title |
| 203 |
|
| 204 |
Pass title along to the UI agent. |
| 205 |
|
| 206 |
=cut |
| 207 |
|
| 208 |
sub title |
| 209 |
{ |
| 210 |
my $this = shift; |
| 211 |
return $this->{title} unless @_; |
| 212 |
my $title = shift; |
| 213 |
|
| 214 |
$this->{title} = $title; |
| 215 |
$this->talk('TITLE', $title); |
| 216 |
} |
| 217 |
|
| 218 |
=head2 settitle |
| 219 |
|
| 220 |
Pass title question name along to the UI agent, along with necessary data |
| 221 |
about it. |
| 222 |
|
| 223 |
=cut |
| 224 |
|
| 225 |
sub settitle |
| 226 |
{ |
| 227 |
my $this = shift; |
| 228 |
my $question = shift; |
| 229 |
|
| 230 |
$this->{title} = $question->description; |
| 231 |
|
| 232 |
my $tag = $question->template->template; |
| 233 |
my $type = $question->template->type; |
| 234 |
my $desc = $question->description; |
| 235 |
my $extdesc = $question->extended_description; |
| 236 |
|
| 237 |
$this->talk('DATA', $tag, 'type', $type); |
| 238 |
|
| 239 |
if ($desc) { |
| 240 |
$desc =~ s/\n/\\n/g; |
| 241 |
$this->talk('DATA', $tag, 'description', $desc); |
| 242 |
} |
| 243 |
|
| 244 |
if ($extdesc) { |
| 245 |
$extdesc =~ s/\n/\\n/g; |
| 246 |
$this->talk('DATA', $tag, 'extended_description', $extdesc); |
| 247 |
} |
| 248 |
|
| 249 |
$this->talk('SETTITLE', $tag); |
| 250 |
} |
| 251 |
|
| 252 |
=head2 go |
| 253 |
|
| 254 |
Asks the UI agent to display all pending questions, first using the special |
| 255 |
data command to tell it necessary data about them. Then read answers from |
| 256 |
the UI agent. |
| 257 |
|
| 258 |
=cut |
| 259 |
|
| 260 |
sub go { |
| 261 |
my $this = shift; |
| 262 |
|
| 263 |
my @elements=grep { $_->visible } @{$this->elements}; |
| 264 |
foreach my $element (@elements) { |
| 265 |
my $question = $element->question; |
| 266 |
my $tag = $question->template->template; |
| 267 |
my $type = $question->template->type; |
| 268 |
my $desc = $question->description; |
| 269 |
my $extdesc = $question->extended_description; |
| 270 |
my $default; |
| 271 |
if ($type eq 'select') { |
| 272 |
$default = $element->translate_default; |
| 273 |
} elsif ($type eq 'multiselect') { |
| 274 |
$default = join ', ', $element->translate_default; |
| 275 |
} else { |
| 276 |
$default = $question->value; |
| 277 |
} |
| 278 |
|
| 279 |
$this->talk('DATA', $tag, 'type', $type); |
| 280 |
|
| 281 |
if ($desc) { |
| 282 |
$desc =~ s/\n/\\n/g; |
| 283 |
$this->talk('DATA', $tag, 'description', $desc); |
| 284 |
} |
| 285 |
|
| 286 |
if ($extdesc) { |
| 287 |
$extdesc =~ s/\n/\\n/g; |
| 288 |
$this->talk('DATA', $tag, 'extended_description', |
| 289 |
$extdesc); |
| 290 |
} |
| 291 |
|
| 292 |
if ($type eq "select" || $type eq "multiselect") { |
| 293 |
my $choices = $question->choices; |
| 294 |
$choices =~ s/\n/\\n/g if ($choices); |
| 295 |
$this->talk('DATA', $tag, 'choices', $choices); |
| 296 |
} |
| 297 |
|
| 298 |
$this->talk('SET', $tag, $default) if $default ne ''; |
| 299 |
|
| 300 |
my @vars=$Debconf::Db::config->variables($question->{name}); |
| 301 |
for my $var (@vars) { |
| 302 |
my $val=$Debconf::Db::config->getvariable($question->{name}, $var); |
| 303 |
$val='' unless defined $val; |
| 304 |
$this->talk('SUBST', $tag, $var, $val); |
| 305 |
} |
| 306 |
|
| 307 |
$this->talk('INPUT', $question->priority, $tag); |
| 308 |
} |
| 309 |
|
| 310 |
# Tell the agent to display the question(s), and check |
| 311 |
# for a back button. |
| 312 |
if (@elements && (scalar($this->talk('GO')) eq "30") && $this->{capb_backup}) { |
| 313 |
return; |
| 314 |
} |
| 315 |
|
| 316 |
# Retrieve the answers. |
| 317 |
foreach my $element (@{$this->elements}) { |
| 318 |
if ($element->visible) { |
| 319 |
my $tag = $element->question->template->template; |
| 320 |
my $type = $element->question->template->type; |
| 321 |
|
| 322 |
my ($ret, $val)=$this->talk('GET', $tag); |
| 323 |
if ($ret eq "0") { |
| 324 |
if ($type eq 'select') { |
| 325 |
$element->value($element->translate_to_C($val)); |
| 326 |
} elsif ($type eq 'multiselect') { |
| 327 |
$element->value(join(', ', map { $element->translate_to_C($_) } split(', ', $val))); |
| 328 |
} else { |
| 329 |
$element->value($val); |
| 330 |
} |
| 331 |
debug developer => "Got \"$val\" for $tag"; |
| 332 |
} |
| 333 |
} else { |
| 334 |
# "show" noninteractive elements, which don't need |
| 335 |
# to pass through, but may do something when shown. |
| 336 |
$element->show; |
| 337 |
} |
| 338 |
} |
| 339 |
|
| 340 |
return 1; |
| 341 |
} |
| 342 |
|
| 343 |
=head2 progress_data |
| 344 |
|
| 345 |
Send necessary data about any progress bar template to the UI agent. |
| 346 |
|
| 347 |
=cut |
| 348 |
|
| 349 |
sub progress_data { |
| 350 |
my $this=shift; |
| 351 |
my $question=shift; |
| 352 |
|
| 353 |
my $tag=$question->template->template; |
| 354 |
my $type=$question->template->type; |
| 355 |
my $desc=$question->description; |
| 356 |
my $extdesc=$question->extended_description; |
| 357 |
|
| 358 |
$this->talk('DATA', $tag, 'type', $type); |
| 359 |
|
| 360 |
if ($desc) { |
| 361 |
$desc =~ s/\n/\\n/g; |
| 362 |
$this->talk('DATA', $tag, 'description', $desc); |
| 363 |
} |
| 364 |
|
| 365 |
if ($extdesc) { |
| 366 |
$extdesc =~ s/\n/\\n/g; |
| 367 |
$this->talk('DATA', $tag, 'extended_description', $extdesc); |
| 368 |
} |
| 369 |
} |
| 370 |
|
| 371 |
sub progress_start { |
| 372 |
my $this=shift; |
| 373 |
|
| 374 |
$this->progress_data($_[2]); |
| 375 |
return $this->talk('PROGRESS', 'START', $_[0], $_[1], $_[2]->template->template); |
| 376 |
} |
| 377 |
|
| 378 |
sub progress_set { |
| 379 |
my $this=shift; |
| 380 |
|
| 381 |
return (scalar($this->talk('PROGRESS', 'SET', $_[0])) ne "30"); |
| 382 |
} |
| 383 |
|
| 384 |
sub progress_step { |
| 385 |
my $this=shift; |
| 386 |
|
| 387 |
return (scalar($this->talk('PROGRESS', 'STEP', $_[0])) ne "30"); |
| 388 |
} |
| 389 |
|
| 390 |
sub progress_info { |
| 391 |
my $this=shift; |
| 392 |
|
| 393 |
$this->progress_data($_[0]); |
| 394 |
return (scalar($this->talk('PROGRESS', 'INFO', $_[0]->template->template)) ne "30"); |
| 395 |
} |
| 396 |
|
| 397 |
sub progress_stop { |
| 398 |
my $this=shift; |
| 399 |
|
| 400 |
return $this->talk('PROGRESS', 'STOP'); |
| 401 |
} |
| 402 |
|
| 403 |
sub shutdown { |
| 404 |
my $this=shift; |
| 405 |
$this->SUPER::shutdown(); |
| 406 |
# Close readfh if it is not the same as writefh (in case of socket) |
| 407 |
if (defined $this->{readfh} && |
| 408 |
(not defined $this->{writefh} or $this->{readfh} != $this->{writefh})) |
| 409 |
{ |
| 410 |
close $this->{readfh}; |
| 411 |
delete $this->{readfh}; |
| 412 |
} |
| 413 |
if (defined $this->{writefh}) { |
| 414 |
close $this->{writefh}; |
| 415 |
delete $this->{writefh}; |
| 416 |
} |
| 417 |
} |
| 418 |
|
| 419 |
=back |
| 420 |
|
| 421 |
=head1 AUTHOR |
| 422 |
|
| 423 |
Randolph Chung <[email protected]> |
| 424 |
|
| 425 |
=cut |
| 426 |
|
| 427 |
1 |