Debian Codegraph @

dpkg-preconfigure

raw
1 #!/usr/bin/perl
2
3 =head1 NAME
4
5 dpkg-preconfigure - let packages ask questions prior to their installation
6
7 =head1 SYNOPSIS
8
9 dpkg-preconfigure [options] package.deb
10
11 dpkg-preconfigure --apt
12
13 =head1 DESCRIPTION
14
15 B<dpkg-preconfigure> lets packages ask questions before they are installed.
16 It operates on a set of debian packages, and all packages that use debconf
17 will have their config script run so they can examine the system and ask
18 questions.
19
20 =head1 OPTIONS
21
22 =over 4
23
24 =item B<-f>I<type>, B<--frontend=>I<type>
25
26 Select the frontend to use.
27
28 =item B<-p>I<value>, B<--priority=>I<value>
29
30 Set the lowest priority of questions you are interested in. Any questions
31 with a priority below the selected priority will be ignored and their
32 default answers will be used.
33
34 =item B<--terse>
35
36 Enables terse output mode. This affects only some frontends.
37
38 =item B<--apt>
39
40 Run in apt mode. It will expect to read a set of package filenames from
41 stdin, rather than getting them as parameters. Typically this is used to
42 make apt run dpkg-preconfigure on all packages before they are installed.
43 To do this, add something like this to /etc/apt/apt.conf:
44
45 // Pre-configure all packages before
46 // they are installed.
47 DPkg::Pre-Install-Pkgs {
48 "dpkg-preconfigure --apt --priority=low";
49 };
50
51 =item B<-h>, B<--help>
52
53 Display usage help.
54
55 =back
56
57 =cut
58
59 =head1 SEE ALSO
60
61 L<debconf(7)>
62
63 =cut
64
65 use warnings;
66 use strict;
67
68 # This program may be running from apt. If so, if it fails, apt is going to
69 # fail as well, which will make it hard for people to fix whatever the failure
70 # was. One thing someone encountered was perl failing to install. Apt then
71 # was re-run with perl unpacked and not configured, and modules weren't
72 # able to be loaded. As a sanity check, detect that case. If found, exit with
73 # an error message, but a return code of 0 so apt continues.
74 BEGIN {
75 ## no critic (BuiltinFunctions::ProhibitStringyEval)
76 eval qq{
77 use File::Path qw(make_path remove_tree);
78 use FileHandle;
79 use Debconf::Log qw(:all);
80 use Debconf::Db;
81 use Debconf::Template;
82 use Debconf::Config;
83 use Debconf::AutoSelect qw(:all);
84 use Debconf::Gettext;
85 use Debconf::Path;
86 };
87 ## use critic
88 if ($@) {
89 # Don't use gettext() on this because it may have failed to
90 # load!
91 print STDERR "debconf: Perl may be unconfigured ($@) -- aborting\n";
92 exit 0;
93 }
94 }
95
96 if (exists $ENV{DEBCONF_USE_CDEBCONF} and $ENV{DEBCONF_USE_CDEBCONF} ne '') {
97 exec "/usr/lib/cdebconf/dpkg-preconfigure", @ARGV;
98 }
99
100 # remove_tree calls getcwd() and fails if it can't determine the current
101 # working directory. This is possible if the current working directory has
102 # been removed, and there's no legitimate reason for dpkg-preconfigure to
103 # depend on the current working directory, so just change to / to give
104 # ourselves a more stable environment.
105 chdir('/');
106
107 Debconf::Db->load;
108
109 my $apt=0;
110 Debconf::Config->getopt(
111 qq{Usage: dpkg-preconfigure [options] [debs]
112 --apt Apt mode.},
113 "apt" => \$apt,
114 );
115
116 # In apt mode, need unbuffered output. Let's just enable it.
117 $|=1;
118
119 my @debs=@ARGV;
120 @ARGV=();
121
122 my $have_tty=1;
123
124 # If running from apt, read package filenames on stdin.
125 if ($apt) {
126 while (<STDIN>) {
127 chomp;
128 push @debs, $_ if length $_;
129 }
130
131 exit unless @debs;
132
133 # We have now reached the end of the first file on stdin.
134 # Future reads from stdin should get input from the user, so re-open.
135 $have_tty=0 unless open (STDIN, "<", "/dev/tty");
136 }
137 elsif (! @debs) {
138 print STDERR sprintf("dpkg-preconfigure: ".gettext("must specify some debs to preconfigure")), "\n";
139 exit(1);
140 }
141
142 my $apt_extracttemplates;
143 if (-x '/usr/lib/apt/apt-extracttemplates') {
144 $apt_extracttemplates = '/usr/lib/apt/apt-extracttemplates';
145 } elsif (Debconf::Path::find('apt-extracttemplates')) {
146 $apt_extracttemplates = 'apt-extracttemplates';
147 } else {
148 warn gettext("delaying package configuration, since apt-utils is not installed");
149 exit;
150 }
151
152 my $frontend=make_frontend();
153
154 if (! $have_tty && $frontend->need_tty) {
155 print STDERR sprintf("dpkg-preconfigure: ".gettext("unable to re-open stdin: %s"), $!)."\n";
156 exit 0;
157 }
158
159 my $tempdir='/var/cache/debconf/tmp.ci';
160 remove_tree($tempdir, { safe => 1, keep_root => 1 });
161 make_path($tempdir);
162
163 # Use the external package scanner for speed. It takes a list of debs
164 # and outputs to stdout a table with these fields separated by spaces:
165 my ($package, $version, $template, $config);
166 # Note that it also handles making sure the current version of debconf can
167 # deal with the package, because apt-extracttemplates skips over any
168 # package that depends on a version of debconf newer than what is
169 # installed.
170 pipe(my $info_reader, my $info_writer) or die "debconf: pipe failed: $!";
171 my $pid = fork();
172 defined $pid or die "fork failed: $!";
173 unless ($pid) {
174 close($info_reader);
175
176 # The kernel can accept command lines up to 20k worth of
177 # characters. It has happened that the list of packages to
178 # configure is more than that, which requires splitting it up into
179 # multiple apt-extracttemplates runs.
180 my $command_max=20000; # LINUX SPECIFIC!!
181 # I could use POSIX::ARG_MAX, but that would be slow.
182 my $static_len=length($apt_extracttemplates);
183 my $len=$static_len;
184 my @collect;
185 my $progress=0;
186 my $show_progress=($apt && @debs > 30 && -t STDERR);
187
188 sub apt_extracttemplates {
189 my $apt_extracttemplates_pid = fork();
190 my $error;
191 if (not defined $apt_extracttemplates_pid) {
192 $error = $!;
193 } elsif (not $apt_extracttemplates_pid) {
194 open(STDOUT, ">&", $info_writer)
195 or die "debconf: redirect stdout failed: $!";
196 exec($apt_extracttemplates, "--tempdir", $tempdir, @collect)
197 or die "debconf: exec $apt_extracttemplates failed: $!";
198 } else {
199 my $child = waitpid($apt_extracttemplates_pid, 0);
200 if ($child == -1) {
201 $error = $!;
202 } elsif ($?) {
203 $error = $?;
204 }
205 }
206 print STDERR sprintf("debconf: ".gettext("apt-extracttemplates failed: %s")."\n", $error) if defined $error;
207 if ($show_progress) {
208 $progress += @collect;
209 printf "\r".gettext("Extracting templates from packages: %d%%"), $progress * 100 / @debs;
210 }
211 return;
212 }
213
214 if ($show_progress) {
215 STDOUT->autoflush(1);
216 }
217 foreach my $deb (@debs) {
218 $len += length($deb) + 1;
219 if ($len < $command_max && @collect < 30) {
220 push @collect, $deb;
221 }
222 else {
223 apt_extracttemplates();
224 @collect=($deb);
225 $len=$static_len + length($deb) + 1;
226 }
227 }
228 apt_extracttemplates();
229 print "\n" if $show_progress;
230 exit;
231 }
232 close($info_writer);
233 # Why buffer here? Well, suppose 300 packages are being installed, and
234 # only the first and last use debconf. The first is preconfigured. Then
235 # the user watches their UI lock up until it scans all the way to the last..
236 # Blowing a bit of memory on a buffer seems like a saner approach.
237 my @buffer=<$info_reader>;
238 if ($apt && @buffer) {
239 print gettext("Preconfiguring packages ...\n");
240 }
241 foreach my $line (@buffer) {
242 ($package, $version, $template, $config)=split /\s/, $line;
243
244 # Load templates if any.
245 if (defined $template && length $template) {
246 eval {
247 Debconf::Template->load($template, $package)
248 };
249 unlink $template;
250 if ($@) {
251 print STDERR "$package ".sprintf(gettext("template parse error: %s"), $@)."\n";
252 unlink $config;
253 next;
254 }
255 }
256 }
257
258 foreach my $line (@buffer) {
259 ($package, $version, $template, $config)=split /\s/, $line;
260
261 # Run config script if any.
262 if (defined $config && length $config && -e $config) {
263 debug user => sprintf("preconfiguring %s (%s)",$package,$version);
264 chmod(0755, $config) or
265 die sprintf(gettext("debconf: can't chmod: %s"), $!);
266 $frontend->default_title($package);
267 $frontend->info(undef);
268 my $confmodule=make_confmodule($config, 'configure', $version);
269 # Make sure any questions created are owned by the correct package.
270 $confmodule->owner($package);
271 1 while ($confmodule->communicate);
272 # I could just exit, but it's good to be robust so
273 # other packages can still be configured and installed.
274 if ($confmodule->exitcode > 0) {
275 print STDERR sprintf(
276 gettext("%s failed to preconfigure, with exit status %s"),
277 $package, $confmodule->exitcode)."\n";
278 }
279 unlink $config;
280 }
281 }
282
283 $frontend->shutdown;
284
285 # Save state.
286 Debconf::Db->save;
287
288 remove_tree($tempdir, { safe => 1, keep_root => 1 });
289
290 =head1 AUTHOR
291
292 Joey Hess <[email protected]>
293
294 =cut