Debian Codegraph @

debconf-apt-progress

raw
1 #!/usr/bin/perl
2
3 =head1 NAME
4
5 debconf-apt-progress - install packages using debconf to display a progress bar
6
7 =head1 SYNOPSIS
8
9 debconf-apt-progress [--] command [args ...]
10 debconf-apt-progress --config
11 debconf-apt-progress --start
12 debconf-apt-progress --from waypoint --to waypoint [--] command [args ...]
13 debconf-apt-progress --stop
14
15 =head1 DESCRIPTION
16
17 B<debconf-apt-progress> installs packages using debconf to display a
18 progress bar. The given I<command> should be any command-line apt frontend;
19 specifically, it must send progress information to the file descriptor
20 selected by the C<APT::Status-Fd> configuration option, and must keep the
21 file descriptors nominated by the C<APT::Keep-Fds> configuration option open
22 when invoking debconf (directly or indirectly), as those file descriptors
23 will be used for the debconf passthrough protocol.
24
25 The arguments to the command you supply should generally include B<-y> (for
26 B<apt-get> or B<aptitude>) or similar to avoid the apt frontend prompting
27 for input. B<debconf-apt-progress> cannot do this itself because the
28 appropriate argument may differ between apt frontends.
29
30 The B<--start>, B<--stop>, B<--from>, and B<--to> options may be used to
31 create a progress bar with multiple segments for different stages of
32 installation, provided that the caller is a debconf confmodule. The caller
33 may also interact with the progress bar itself using the debconf protocol if
34 it so desires.
35
36 debconf locks its config database when it starts up, which makes it
37 unfortunately inconvenient to have one instance of debconf displaying the
38 progress bar and another passing through questions from packages being
39 installed. If you're using a multiple-segment progress bar, you'll need to
40 eval the output of the B<--config> option before starting the debconf
41 frontend to work around this. See L<the EXAMPLES section/EXAMPLES> below.
42
43 =head1 OPTIONS
44
45 =over 4
46
47 =item B<--config>
48
49 Print environment variables necessary to start up a progress bar frontend.
50
51 =item B<--start>
52
53 Start up a progress bar, running from 0 to 100 by default. Use B<--from> and
54 B<--to> to use other endpoints.
55
56 =item B<--from> I<waypoint>
57
58 If used with B<--start>, make the progress bar begin at I<waypoint> rather
59 than 0.
60
61 Otherwise, install packages with their progress bar beginning at this
62 "waypoint". Must be used with B<--to>.
63
64 =item B<--to> I<waypoint>
65
66 If used with B<--start>, make the progress bar end at I<waypoint> rather
67 than 100.
68
69 Otherwise, install packages with their progress bar ending at this
70 "waypoint". Must be used with B<--from>.
71
72 =item B<--stop>
73
74 Stop a running progress bar.
75
76 =item B<--no-progress>
77
78 Avoid starting, stopping, or stepping the progress bar. Progress
79 messages from apt, media change events, and debconf questions will still
80 be passed through to debconf.
81
82 =item B<--dlwaypoint> I<percentage>
83
84 Specify what percent of the progress bar to use for downloading packages.
85 The remainder will be used for installing packages. The default is to use
86 15% for downloading and the remaining 85% for installing.
87
88 =item B<--logfile> I<file>
89
90 Send the normal output from apt to the given file.
91
92 =item B<--logstderr>
93
94 Send the normal output from apt to stderr. If you supply neither
95 B<--logfile> nor B<--logstderr>, the normal output from apt will be
96 discarded.
97
98 =item B<-->
99
100 Terminate options. Since you will normally need to give at least the B<-y>
101 argument to the command being run, you will usually need to use B<--> to
102 prevent that being interpreted as an option to B<debconf-apt-progress>
103 itself.
104
105 =back
106
107 =head1 EXAMPLES
108
109 Install the GNOME desktop and an X window system development environment
110 within a progress bar:
111
112 debconf-apt-progress -- aptitude -y install gnome x-window-system-dev
113
114 Install the GNOME, KDE, and XFCE desktops within a single progress bar,
115 allocating 45% of the progress bar for each of GNOME and KDE and the
116 remaining 10% for XFCE:
117
118 #! /bin/sh
119 set -e
120 case $1 in
121 '')
122 eval "$(debconf-apt-progress --config)"
123 "$0" debconf
124 ;;
125 debconf)
126 . /usr/share/debconf/confmodule
127 debconf-apt-progress --start
128 debconf-apt-progress --from 0 --to 45 -- apt-get -y install gnome
129 debconf-apt-progress --from 45 --to 90 -- apt-get -y install kde
130 debconf-apt-progress --from 90 --to 100 -- apt-get -y install xfce4
131 debconf-apt-progress --stop
132 ;;
133 esac
134
135 =head1 RETURN CODE
136
137 The exit code of the specified command is returned, unless the user hit the
138 cancel button on the progress bar. If the cancel button was hit, a value of
139 30 is returned. To avoid ambiguity, if the command returned 30, a value of
140 3 will be returned.
141
142 =cut
143
144 use warnings;
145 use strict;
146 use POSIX;
147 use Fcntl;
148 use Getopt::Long;
149 # Avoid starting the debconf frontend just yet.
150 use Debconf::Client::ConfModule ();
151
152 my ($config, $start, $from, $to, $stop);
153 my $progress=1;
154 my $dlwaypoint=15;
155 my ($logfile, $logstderr);
156 my $had_frontend;
157
158 our ($status_read, $status_write);
159 our ($command_read, $command_write);
160 our ($debconf_command_read, $debconf_command_write);
161 our ($debconf_reply_read, $debconf_reply_write);
162 our $apt_log;
163
164 sub checkopen (@) {
165 my $file = $_[0];
166 my $fd = POSIX::open($file, &POSIX::O_RDONLY);
167 defined $fd or die "$0: can't open $file: $!\n";
168 return $fd;
169 }
170
171 sub checkclose ($) {
172 my $fd = $_[0];
173 unless (POSIX::close($fd)) {
174 return if $! == &POSIX::EBADF;
175 die "$0: can't close fd $fd: $!\n";
176 }
177 }
178
179 sub checkdup2 ($$) {
180 my ($oldfd, $newfd) = @_;
181 checkclose($newfd);
182 POSIX::dup2($oldfd, $newfd)
183 or die "$0: can't dup fd $oldfd to $newfd: $!\n";
184 }
185
186 sub nocloexec ($) {
187 my $fh = shift;
188 my $flags = fcntl($fh, F_GETFD, 0);
189 fcntl($fh, F_SETFD, $flags & ~FD_CLOEXEC);
190 }
191
192 sub nonblock ($) {
193 my $fh = shift;
194 my $flags = fcntl($fh, F_GETFL, 0);
195 fcntl($fh, F_SETFL, $flags | O_NONBLOCK);
196 }
197
198 # Open the given file descriptors to make sure they won't accidentally be
199 # used by Perl, leading to confusion.
200 sub reservefds (@) {
201 my $null = checkopen('/dev/null');
202 my $close = 1;
203 for my $fd (@_) {
204 if ($null == $fd) {
205 $close = 0;
206 } else {
207 checkclose($fd);
208 checkdup2($null, $fd);
209 }
210 }
211 if ($close) {
212 checkclose($null);
213 }
214 }
215
216 # Does this environment variable exist, and is it non-empty?
217 sub envnonempty ($) {
218 my $name = shift;
219 return (exists $ENV{$name} and $ENV{$name} ne '');
220 }
221
222 sub start_debconf (@) {
223 if (! $ENV{DEBIAN_HAS_FRONTEND}) {
224 # Save existing environment variables.
225 if (envnonempty('DEBCONF_DB_REPLACE')) {
226 $ENV{DEBCONF_APT_PROGRESS_DB_REPLACE} =
227 $ENV{DEBCONF_DB_REPLACE};
228 }
229 if (envnonempty('DEBCONF_DB_OVERRIDE')) {
230 $ENV{DEBCONF_APT_PROGRESS_DB_OVERRIDE} =
231 $ENV{DEBCONF_DB_OVERRIDE};
232 }
233
234 # Make sure the main configdb is opened read-only ...
235 $ENV{DEBCONF_DB_REPLACE} = 'configdb';
236 # ... and stack a writable db on top of it, since the
237 # passthrough instance is going to be sending us db updates.
238 $ENV{DEBCONF_DB_OVERRIDE} = 'Pipe{infd:none outfd:none}';
239
240 # Leave a note for ourselves. We need to do it this way
241 # round since DEBIAN_HAS_FRONTEND will be set the second
242 # time round even if it isn't set initially.
243 $ENV{DEBCONF_APT_PROGRESS_NO_FRONTEND} = 1;
244
245 # Restore @ARGV so that
246 # Debconf::Client::ConfModule::import() can use it.
247 @ARGV = @_;
248 }
249
250 import Debconf::Client::ConfModule;
251 }
252
253 sub passthrough (@) {
254 my $priority = Debconf::Client::ConfModule::get('debconf/priority');
255
256 defined(my $pid = fork) or die "$0: can't fork: $!\n";
257 if (!$pid) {
258 close $status_read;
259 close $command_write;
260 close $debconf_command_read;
261 close $debconf_reply_write;
262 $^F = 6; # avoid close-on-exec
263 if (fileno($command_read) != 0) {
264 checkdup2(fileno($command_read), 0);
265 close $command_read;
266 }
267 if (fileno($apt_log) != 1) {
268 checkclose(1);
269 checkdup2(fileno($apt_log), 1);
270 }
271 if (fileno($apt_log) != 2) {
272 checkclose(2);
273 checkdup2(fileno($apt_log), 2);
274 }
275 close $apt_log;
276 delete $ENV{DEBIAN_HAS_FRONTEND};
277 delete $ENV{DEBCONF_REDIR};
278 delete $ENV{DEBCONF_SYSTEMRC};
279 delete $ENV{DEBCONF_PIPE}; # just in case ...
280 $ENV{DEBIAN_FRONTEND} = 'passthrough';
281 $ENV{DEBIAN_PRIORITY} = $priority;
282 $ENV{DEBCONF_READFD} = 5;
283 $ENV{DEBCONF_WRITEFD} = 6;
284 $ENV{APT_LISTCHANGES_FRONTEND} = 'none';
285 # If we already had a debconf frontend when we started, then
286 # the passthrough child needs to use the same pipe-database
287 # trick as we do. See start_debconf.
288 if ($had_frontend) {
289 $ENV{DEBCONF_DB_REPLACE} = 'configdb';
290 $ENV{DEBCONF_DB_OVERRIDE} = 'Pipe{infd:none outfd:none}';
291 }
292 exec @_ or die "can't exec $_[0]: $!";
293 }
294
295 close $status_write;
296 close $command_read;
297 close $debconf_command_write;
298 close $debconf_reply_read;
299 return $pid;
300 }
301
302 sub handle_status ($$$) {
303 my ($from, $to, $line) = @_;
304 my ($status, $pkg, $percent, $description) = split ':', $line, 4;
305
306 my ($min, $len);
307 if ($status eq 'dlstatus') {
308 $min = 0;
309 $len = $dlwaypoint;
310 }
311 elsif ($status eq 'pmstatus') {
312 $min = $dlwaypoint;
313 $len = 100 - $dlwaypoint;
314 }
315 elsif ($status eq 'media-change') {
316 Debconf::Client::ConfModule::subst(
317 'debconf-apt-progress/media-change', 'MESSAGE',
318 $description);
319 my @ret = Debconf::Client::ConfModule::input(
320 'critical', 'debconf-apt-progress/media-change');
321 $ret[0] == 0 or die "Can't display media change request!\n";
322 Debconf::Client::ConfModule::go();
323 print $command_write "\n" ||
324 die "can't talk to command fd: $!";
325 return;
326 }
327 else {
328 return;
329 }
330
331 $percent = ($percent * $len / 100 + $min);
332 $percent = ($percent * ($to - $from) / 100 + $from);
333 $percent =~ s/\..*//;
334 if ($progress) {
335 my @ret=Debconf::Client::ConfModule::progress('SET', $percent);
336 if ($ret[0] eq '30') {
337 cancel();
338 }
339 }
340 Debconf::Client::ConfModule::subst(
341 'debconf-apt-progress/info', 'DESCRIPTION', $description);
342 my @ret=Debconf::Client::ConfModule::progress(
343 'INFO', 'debconf-apt-progress/info');
344 if ($ret[0] eq '30') {
345 cancel();
346 }
347 }
348
349 sub handle_debconf_command ($) {
350 my $line = shift;
351
352 # Debconf::Client::ConfModule has already dealt with checking
353 # DEBCONF_REDIR.
354 print "$line\n" || die "can't write to stdout: $!";
355 my $ret = <STDIN>;
356 chomp $ret;
357 print $debconf_reply_write "$ret\n" ||
358 die "can't write to \$debconf_reply_write: $!";
359 }
360
361 my $pid;
362 sub run_progress ($$@) {
363 my $from = shift;
364 my $to = shift;
365 my $command = shift;
366 local ($status_read, $status_write);
367 local ($command_read, $command_write);
368 local ($debconf_command_read, $debconf_command_write);
369 local ($debconf_reply_read, $debconf_reply_write);
370 local $apt_log;
371 use IO::Handle;
372
373 if ($progress) {
374 my @ret=Debconf::Client::ConfModule::progress(
375 'INFO', 'debconf-apt-progress/preparing');
376 if ($ret[0] eq '30') {
377 cancel();
378 return 30;
379 }
380 }
381
382 reservefds(4, 5, 6);
383
384 pipe $status_read, $status_write
385 or die "$0: can't create status pipe: $!";
386 nonblock($status_read);
387 checkdup2(fileno($status_write), 4);
388 open $status_write, '>&=', 4
389 or die "$0: can't reopen \$status_write as fd 4: $!";
390 nocloexec($status_write);
391
392 pipe $command_read, $command_write
393 or die "$0: can't create command pipe: $!";
394 nocloexec($command_read);
395 $command_write->autoflush(1);
396
397 pipe $debconf_command_read, $debconf_command_write
398 or die "$0: can't create debconf command pipe: $!";
399 nonblock($debconf_command_read);
400 checkdup2(fileno($debconf_command_write), 6);
401 open $debconf_command_write, '>&=', 6
402 or die "$0: can't reopen \$debconf_command_write as fd 6: $!";
403 nocloexec($debconf_command_write);
404
405 pipe $debconf_reply_read, $debconf_reply_write
406 or die "$0: can't create debconf reply pipe: $!";
407 checkdup2(fileno($debconf_reply_read), 5);
408 open $debconf_reply_read, '<&=', 5
409 or die "$0: can't reopen \$debconf_reply_read as fd 5: $!";
410 nocloexec($debconf_reply_read);
411 $debconf_reply_write->autoflush(1);
412
413 if (defined $logfile) {
414 open $apt_log, '>>', $logfile
415 or die "$0: can't open $logfile: $!";
416 } elsif ($logstderr) {
417 open $apt_log, '>&', \*STDERR
418 or die "$0: can't duplicate stderr: $!";
419 } else {
420 open $apt_log, '>', '/dev/null'
421 or die "$0: can't open /dev/null: $!";
422 }
423 nocloexec($apt_log);
424
425 $pid = passthrough $command,
426 '-o', 'APT::Status-Fd=4',
427 '-o', 'APT::Keep-Fds::=5',
428 '-o', 'APT::Keep-Fds::=6',
429 @_;
430
431 my $status_eof = 0;
432 my $debconf_command_eof = 0;
433 my $status_buf = '';
434 my $debconf_command_buf = '';
435
436 # $status_read should be the last fd to close.
437 # $debconf_command_write may end up captured by buggy daemons, so
438 # terminate the loop even if we haven't hit $debconf_command_eof.
439 while (not $status_eof) {
440 my $rin = '';
441 my $rout;
442 vec($rin, fileno($status_read), 1) = 1;
443 vec($rin, fileno($debconf_command_read), 1) = 1
444 unless $debconf_command_eof;
445 my $sel = select($rout = $rin, undef, undef, undef);
446 if ($sel < 0) {
447 next if $! == &POSIX::EINTR;
448 die "$0: select failed: $!";
449 }
450
451 if (vec($rout, fileno($status_read), 1) == 1) {
452 # Status message from apt. Transform into debconf
453 # messages.
454 while (1) {
455 my $r = sysread($status_read, $status_buf,
456 4096, length $status_buf);
457 if (not defined $r) {
458 next if $! == &POSIX::EINTR;
459 last if $! == &POSIX::EAGAIN or
460 $! == &POSIX::EWOULDBLOCK;
461 die "$0: read \$status_read failed: $!";
462 }
463 elsif ($r == 0) {
464 if ($status_buf ne '' and
465 $status_buf !~ /\n$/) {
466 $status_buf .= "\n";
467 }
468 $status_eof = 1;
469 last;
470 }
471 last if $status_buf =~ /\n/;
472 }
473
474 while ($status_buf =~ /\n/) {
475 my $status_line;
476 ($status_line, $status_buf) =
477 split /\n/, $status_buf, 2;
478 handle_status $from, $to, $status_line;
479 }
480 }
481
482 if (vec($rout, fileno($debconf_command_read), 1) == 1) {
483 # Debconf command. Pass straight through.
484 while (1) {
485 my $r = sysread($debconf_command_read,
486 $debconf_command_buf, 4096,
487 length $debconf_command_buf);
488 if (not defined $r) {
489 next if $! == &POSIX::EINTR;
490 last if $! == &POSIX::EAGAIN or
491 $! == &POSIX::EWOULDBLOCK;
492 die "$0: read " .
493 "\$debconf_command_read " .
494 "failed: $!";
495 }
496 elsif ($r == 0) {
497 if ($debconf_command_buf ne '' and
498 $debconf_command_buf !~ /\n$/) {
499 $debconf_command_buf .= "\n";
500 }
501 $debconf_command_eof = 1;
502 last;
503 }
504 last if $debconf_command_buf =~ /\n/;
505 }
506
507 while ($debconf_command_buf =~ /\n/) {
508 my $debconf_command_line;
509 ($debconf_command_line, $debconf_command_buf) =
510 split /\n/, $debconf_command_buf, 2;
511 handle_debconf_command $debconf_command_line;
512 }
513 }
514 }
515
516 waitpid $pid, 0;
517 undef $pid;
518 my $status = $?;
519
520 # make sure that the progress bar always gets to the end
521 if ($progress) {
522 my @ret=Debconf::Client::ConfModule::progress('SET', $to);
523 if ($ret[0] eq '30') {
524 cancel();
525 }
526 }
527
528 if ($status & 127) {
529 return 127;
530 }
531
532 return ($status >> 8);
533 }
534
535 # Called if the progress bar is cancelled. Starts with a SIGINT but
536 # if called repeatedly, falls back to SIGKILL.
537 my $cancelled=0;
538 my $cancel_sent_signal=0;
539 sub cancel () {
540 $cancelled++;
541 if (defined $pid) {
542 $cancel_sent_signal++;
543 if ($cancel_sent_signal == 1) {
544 kill INT => $pid;
545 }
546 else {
547 kill KILL => $pid;
548 }
549 }
550 }
551
552 sub start_bar ($$) {
553 my ($from, $to) = @_;
554 if ($progress) {
555 Debconf::Client::ConfModule::progress(
556 'START', $from, $to, 'debconf-apt-progress/title');
557 my @ret=Debconf::Client::ConfModule::progress(
558 'INFO', 'debconf-apt-progress/preparing');
559 if ($ret[0] eq '30') {
560 cancel();
561 }
562 }
563 }
564
565 sub stop_bar () {
566 Debconf::Client::ConfModule::progress('STOP') if $progress;
567 # If we don't stop, we leave a zombie in case some daemon fails to
568 # disconnect from fd 3. Don't do this if debconf was already
569 # running, though, since in that case we're running as part of a
570 # larger application which will need to take its own care to stop
571 # when it's finished.
572 Debconf::Client::ConfModule::stop() unless $had_frontend;
573 }
574
575 # Restore saved environment variables.
576 if (envnonempty('DEBCONF_APT_PROGRESS_DB_REPLACE')) {
577 $ENV{DEBCONF_DB_REPLACE} = $ENV{DEBCONF_APT_PROGRESS_DB_REPLACE};
578 } else {
579 delete $ENV{DEBCONF_DB_REPLACE};
580 }
581 if (envnonempty('DEBCONF_APT_PROGRESS_DB_OVERRIDE')) {
582 $ENV{DEBCONF_DB_OVERRIDE} = $ENV{DEBCONF_APT_PROGRESS_DB_OVERRIDE};
583 } else {
584 delete $ENV{DEBCONF_DB_OVERRIDE};
585 }
586 $had_frontend = 1 unless $ENV{DEBCONF_APT_PROGRESS_NO_FRONTEND};
587 delete $ENV{DEBCONF_APT_PROGRESS_NO_FRONTEND}; # avoid inheritance
588
589 my @saved_argv = @ARGV;
590
591 my $result = GetOptions('config' => \$config,
592 'start' => \$start,
593 'from=i' => \$from,
594 'to=i' => \$to,
595 'stop' => \$stop,
596 'logfile=s' => \$logfile,
597 'logstderr' => \$logstderr,
598 'progress!' => \$progress,
599 'dlwaypoint=i' => \$dlwaypoint,
600 );
601
602 if (! $progress && ($start || $from || $to || $stop)) {
603 die "--no-progress cannot be used with --start, --from, --to, or --stop\n";
604 }
605
606 unless ($start) {
607 if (defined $from and not defined $to) {
608 die "$0: --from requires --to\n";
609 } elsif (defined $to and not defined $from) {
610 die "$0: --to requires --from\n";
611 }
612 }
613
614 my $mutex = 0;
615 ++$mutex if $config;
616 ++$mutex if $start;
617 ++$mutex if $stop;
618 if ($mutex > 1) {
619 die "$0: must use only one of --config, --start, or --stop\n";
620 }
621
622 if (($config or $stop) and (defined $from or defined $to)) {
623 die "$0: cannot use --from or --to with --config or --stop\n";
624 }
625
626 start_debconf(@saved_argv) unless $config;
627
628 my $status = 0;
629
630 if ($config) {
631 print <<'EOF';
632 DEBCONF_APT_PROGRESS_DB_REPLACE="$DEBCONF_DB_REPLACE"
633 DEBCONF_APT_PROGRESS_DB_OVERRIDE="$DEBCONF_DB_OVERRIDE"
634 export DEBCONF_APT_PROGRESS_DB_REPLACE DEBCONF_APT_PROGRESS_DB_OVERRIDE
635 DEBCONF_DB_REPLACE=configdb
636 DEBCONF_DB_OVERRIDE='Pipe{infd:none outfd:none}'
637 export DEBCONF_DB_REPLACE DEBCONF_DB_OVERRIDE
638 EOF
639 } elsif ($start) {
640 $from = 0 unless defined $from;
641 $to = 100 unless defined $to;
642 start_bar($from, $to);
643 } elsif (defined $from) {
644 $status = run_progress($from, $to, @ARGV);
645 } elsif ($stop) {
646 stop_bar();
647 } else {
648 start_bar(0, 100);
649 if (! $cancelled) {
650 $status = run_progress(0, 100, @ARGV);
651 stop_bar();
652 }
653 }
654
655 if ($cancelled) {
656 # This is pure paranoia. What if the child was in the
657 # middle of writing a debconf command out, only to be
658 # interrupted with a truncated write? Let's send a no-op
659 # command to finish it out just in case.
660 Debconf::Client::ConfModule::get("debconf/priority");
661
662 exit 30;
663 }
664 elsif ($status == 30) {
665 exit 3;
666 }
667 else {
668 exit $status;
669 }
670
671 =head1 AUTHORS
672
673 Colin Watson <[email protected]>
674
675 Joey Hess <[email protected]>
676
677 =cut