Fix file-descriptor leak when pipes fail via taint checks:
[p5sagit/p5-mst-13.2.git] / installperl
CommitLineData
c623bd54 1#!./perl
68dc0745 2
3BEGIN {
c90c0ff4 4 require 5.004;
d07c2202 5 chdir '..' if !-d 'lib' and -d '..\lib';
68dc0745 6 @INC = 'lib';
7 $ENV{PERL5LIB} = 'lib';
8}
9
a0d0e21e 10use File::Find;
5f05dabc 11use File::Compare;
68dc0745 12use File::Copy ();
0ecb046b 13use File::Path ();
4633a7c4 14use Config;
91a06757 15use subs qw(unlink link chmod);
c623bd54 16
0ecb046b 17# override the ones in the rest of the script
68dc0745 18sub mkpath {
19 File::Path::mkpath(@_) unless $nonono;
0ecb046b 20}
21
bee1dbe2 22$mainperldir = "/usr/bin";
39add537 23$exe_ext = $Config{exe_ext};
bee1dbe2 24
c623bd54 25while (@ARGV) {
26 $nonono = 1 if $ARGV[0] eq '-n';
27 $versiononly = 1 if $ARGV[0] eq '-v';
28 shift;
29}
30
45d8adaa 31umask 022;
32
1fef88e7 33@scripts = qw( utils/c2ph utils/h2ph utils/h2xs
5f05dabc 34 utils/perlbug utils/perldoc utils/pl2pm utils/splain
dd6decf0 35 x2p/s2p x2p/find2perl
36 pod/pod2man pod/pod2html pod/pod2latex pod/pod2text);
16d20bd9 37
16d20bd9 38@pods = (<pod/*.pod>);
c623bd54 39
1dd15ed4 40%archpms = (Config => 1, FileHandle => 1, overload => 1);
39e571d4 41
42if ($^O eq 'dos') {
43 push(@scripts,'djgpp/fixpmain');
44 $archpms{config} = $archpms{filehand} = 1;
45}
46
1dd15ed4 47find(sub {
48 if ("$File::Find::dir/$_" =~ m{^ext/[^/]+/(.*)\.pm$}) {
49 (my $pm = $1) =~ s{^lib/}{};
50 $archpms{$pm} = 1;
51 }
52 }, 'ext');
53
748a9306 54$ver = $];
4633a7c4 55$release = substr($ver,0,3); # Not used presently.
bee1dbe2 56$patchlevel = substr($ver,3,2);
748a9306 57die "Patchlevel of perl ($patchlevel)",
4633a7c4 58 "and patchlevel of config.sh ($Config{'PATCHLEVEL'}) don't match\n"
59 if $patchlevel != $Config{'PATCHLEVEL'};
60
61# Fetch some frequently-used items from %Config
62$installbin = $Config{installbin};
63$installscript = $Config{installscript};
64$installprivlib = $Config{installprivlib};
65$installarchlib = $Config{installarchlib};
66$installsitelib = $Config{installsitelib};
67$installsitearch = $Config{installsitearch};
68$installman1dir = $Config{installman1dir};
69$man1ext = $Config{man1ext};
15792f64 70$libperl = $Config{libperl};
4633a7c4 71# Shared library and dynamic loading suffixes.
72$so = $Config{so};
73$dlext = $Config{dlext};
74
75$d_dosuid = $Config{d_dosuid};
76$binexp = $Config{binexp};
c623bd54 77
78# Do some quick sanity checks.
79
80if ($d_dosuid && $>) { die "You must run as root to install suidperl\n"; }
81
fe14fcc3 82 $installbin || die "No installbin directory in config.sh\n";
fcbdbaa8 83-d $installbin || mkpath($installbin, 1, 0777);
0ecb046b 84-d $installbin || $nonono || die "$installbin is not a directory\n";
85-w $installbin || $nonono || die "$installbin is not writable by you\n"
3edbfbe5 86 unless $installbin =~ m#^/afs/# || $nonono;
c623bd54 87
39add537 88-x 'perl' . $exe_ext || die "perl isn't executable!\n";
89-x 'suidperl' . $exe_ext|| die "suidperl isn't executable!\n" if $d_dosuid;
c623bd54 90
d07c2202 91-x 't/TEST' || $^O eq 'MSWin32'
92 || warn "WARNING: You've never run 'make test'!!!",
93 " (Installing anyway.)\n";
c623bd54 94
eef1cf2a 95if ($^O eq 'MSWin32') {
96
97-f 'perl.' . $dlext || die "No perl DLL built\n";
98
99# Install the DLL
100
101safe_unlink("$installbin/perl.$dlext");
102copy("perl.$dlext", "$installbin/perl.$dlext");
103chmod(0755, "$installbin/perl.$dlext");
104}
105
c623bd54 106# First we install the version-numbered executables.
107
39e571d4 108if ($^O ne 'dos') {
109 safe_unlink("$installbin/perl$ver$exe_ext");
110 copy("perl$exe_ext", "$installbin/perl$ver$exe_ext");
111 chmod(0755, "$installbin/perl$ver$exe_ext");
112} else {
113 safe_unlink("$installbin/perl.exe");
114 copy("perl.exe", "$installbin/perl.exe");
115}
c623bd54 116
68dc0745 117safe_unlink("$installbin/sperl$ver$exe_ext");
c623bd54 118if ($d_dosuid) {
68dc0745 119 copy("suidperl$exe_ext", "$installbin/sperl$ver$exe_ext");
120 chmod(04711, "$installbin/sperl$ver$exe_ext");
c623bd54 121}
122
45d8adaa 123# Install library files.
124
a0d0e21e 125$do_installarchlib = $do_installprivlib = 0;
126
fcbdbaa8 127mkpath($installprivlib, 1, 0777);
128mkpath($installarchlib, 1, 0777);
129mkpath($installsitelib, 1, 0777) if ($installsitelib);
130mkpath($installsitearch, 1, 0777) if ($installsitearch);
4633a7c4 131
45d8adaa 132if (chdir "lib") {
68dc0745 133 $do_installarchlib = ! samepath($installarchlib, '.');
134 $do_installprivlib = ! samepath($installprivlib, '.');
b971f6e4 135 $do_installprivlib = 0 if $versiononly && !($installprivlib =~ m/\Q$]/);
45d8adaa 136
a0d0e21e 137 if ($do_installarchlib || $do_installprivlib) {
138 find(\&installlib, '.');
45d8adaa 139 }
140 chdir ".." || die "Can't cd back to source directory: $!\n";
141}
142else {
143 warn "Can't cd to lib to install lib files: $!\n";
144}
145
1aef975c 146# Install header files and libraries.
fcbdbaa8 147mkpath("$installarchlib/CORE", 1, 0777);
340f689c 148@corefiles = <*.h libperl*.*>;
1aef975c 149# AIX needs perl.exp installed as well.
340f689c 150push(@corefiles,'perl.exp') if $^O eq 'aix';
fed7345c 151# If they have built sperl.o...
340f689c 152push(@corefiles,'sperl.o') if -f 'sperl.o';
153foreach $file (@corefiles) {
aa3cf465 154 # HP-UX (at least) needs to maintain execute permissions
efc5ad56 155 # on dynamically-loadable libraries. So we do it for all.
68dc0745 156 copy_if_diff($file,"$installarchlib/CORE/$file")
efc5ad56 157 and chmod($file =~ /\.(so|\Q$dlext\E)$/ ? 0555 : 0444,
aa3cf465 158 "$installarchlib/CORE/$file");
340f689c 159}
3edbfbe5 160
156620fa 161# Install main perl executables
162# Make links to ordinary names if installbin directory isn't current directory.
163
164if (! $versiononly && ! samepath($installbin, '.') && ($^O ne 'dos')) {
165 safe_unlink("$installbin/perl$exe_ext", "$installbin/suidperl$exe_ext");
166 link("$installbin/perl$ver$exe_ext", "$installbin/perl$exe_ext");
167 link("$installbin/sperl$ver$exe_ext", "$installbin/suidperl$exe_ext")
168 if $d_dosuid;
169}
170
a0d0e21e 171# Offer to install perl in a "standard" location
172
a0d0e21e 173$mainperl_is_instperl = 0;
174
d07c2202 175if (!$versiononly && !$nonono && $^O ne 'MSWin32' && -t STDIN && -t STDERR
68dc0745 176 && -w $mainperldir && ! samepath($mainperldir, $installbin)) {
55497cff 177 local($usrbinperl) = "$mainperldir/perl$exe_ext";
178 local($instperl) = "$installbin/perl$exe_ext";
179 local($expinstperl) = "$binexp/perl$exe_ext";
180
181 # First make sure $usrbinperl is not already the same as the perl we
182 # just installed.
183 if (-x $usrbinperl) {
a0d0e21e 184 # Try to be clever about mainperl being a symbolic link
185 # to binexp/perl if binexp and installbin are different.
186 $mainperl_is_instperl =
68dc0745 187 samepath($usrbinperl, $instperl) ||
188 samepath($usrbinperl, $expinstperl) ||
a0d0e21e 189 (($binexp ne $installbin) &&
55497cff 190 (-l $usrbinperl) &&
191 ((readlink $usrbinperl) eq $expinstperl));
a0d0e21e 192 }
193 if ((! $mainperl_is_instperl) &&
68dc0745 194 (yn("Many scripts expect perl to be installed as $usrbinperl.\n" .
55497cff 195 "Do you wish to have $usrbinperl be the same as\n" .
196 "$expinstperl? [y] ")))
a0d0e21e 197 {
55497cff 198 unlink($usrbinperl);
199 eval { CORE::link $instperl, $usrbinperl } ||
200 eval { symlink $expinstperl, $usrbinperl } ||
68dc0745 201 copy($instperl, $usrbinperl);
a0d0e21e 202 $mainperl_is_instperl = 1;
203 }
204}
205
b971f6e4 206# Make links to ordinary names if installbin directory isn't current directory.
207
68dc0745 208if (!$versiononly && ! samepath($installbin, 'x2p')) {
209 safe_unlink("$installbin/a2p$exe_ext");
210 copy("x2p/a2p$exe_ext", "$installbin/a2p$exe_ext");
211 chmod(0755, "$installbin/a2p$exe_ext");
b971f6e4 212}
213
214# cppstdin is just a script, but it is architecture-dependent, so
215# it can't safely be shared. Place it in $installbin.
216# Note that Configure doesn't build cppstin if it isn't needed, so
217# we skip this if cppstdin doesn't exist.
68dc0745 218if (! $versiononly && (-f 'cppstdin') && (! samepath($installbin, '.'))) {
219 safe_unlink("$installbin/cppstdin");
220 copy("cppstdin", "$installbin/cppstdin");
221 chmod(0755, "$installbin/cppstdin");
b971f6e4 222}
223
224# Install scripts.
225
226mkpath($installscript, 1, 0777);
227
228if (! $versiononly) {
229 for (@scripts) {
68dc0745 230 (my $base = $_) =~ s#.*/##;
231 copy($_, "$installscript/$base");
232 chmod(0755, "$installscript/$base");
b971f6e4 233 }
234}
235
236# pstruct should be a link to c2ph
237
238if (! $versiononly) {
68dc0745 239 safe_unlink("$installscript/pstruct");
39e571d4 240 if ($^O eq 'dos') {
241 copy("$installscript/c2ph","$installscript/pstruct");
242 } else {
243 link("$installscript/c2ph","$installscript/pstruct");
244 }
b971f6e4 245}
246
247# Install pod pages. Where? I guess in $installprivlib/pod.
248
68dc0745 249if (! $versiononly || !($installprivlib =~ m/\Q$]/)) {
b971f6e4 250 mkpath("${installprivlib}/pod", 1, 0777);
91a06757 251
252 # If Perl 5.003's perldiag.pod is there, rename it.
253 if (open POD, "${installprivlib}/pod/perldiag.pod") {
254 read POD, $_, 4000;
255 close POD;
256 # Some of Perl 5.003's diagnostic messages ended with periods.
257 if (/^=.*\.$/m) {
258 my ($from, $to) = ("${installprivlib}/pod/perldiag.pod",
259 "${installprivlib}/pod/perldiag-5.003.pod");
260 print STDERR " rename $from $to";
261 rename($from, $to)
262 or warn "Couldn't rename $from to $to: $!\n"
263 unless $nonono;
264 }
265 }
266
b971f6e4 267 foreach $file (@pods) {
268 # $file is a name like pod/perl.pod
68dc0745 269 copy_if_diff($file, "${installprivlib}/${file}");
b971f6e4 270 }
91a06757 271
272 # Link perldiag.pod into archlib
273 my ($from, $to) = ("${installprivlib}/pod/perldiag.pod",
274 "${installarchlib}/pod/perldiag.pod");
275 if (compare($from, $to) || $nonono) {
276 mkpath("${installarchlib}/pod", 1, 0777);
a3b6010a 277 unlink($to);
39e571d4 278 link($from, $to) if ($^O ne 'dos');
91a06757 279 }
b971f6e4 280}
281
a0d0e21e 282# Check to make sure there aren't other perls around in installer's
283# path. This is probably UNIX-specific. Check all absolute directories
284# in the path except for where public executables are supposed to live.
285# Also skip $mainperl if the user opted to have it be a link to the
286# installed perl.
287
b971f6e4 288if (!$versiononly) {
289
eef1cf2a 290 $dirsep = ($^O eq 'os2' || $^O eq 'MSWin32') ? ';' : ':' ;
b971f6e4 291 ($path = $ENV{"PATH"}) =~ s:\\:/:g ;
292 @path = split(/$dirsep/, $path);
293 @otherperls = ();
294 for (@path) {
295 next unless m,^/,;
296 # Use &samepath here because some systems have other dirs linked
297 # to $mainperldir (like SunOS)
68dc0745 298 next if samepath($_, $binexp);
299 next if ($mainperl_is_instperl && samepath($_, $mainperldir));
b971f6e4 300 push(@otherperls, "$_/perl$exe_ext")
301 if (-x "$_/perl$exe_ext" && ! -d "$_/perl$exe_ext");
302 }
303 if (@otherperls) {
304 print STDERR "\nWarning: perl appears in your path in the following " .
305 "locations beyond where\nwe just installed it:\n";
306 for (@otherperls) {
307 print STDERR " ", $_, "\n";
308 }
309 print STDERR "\n";
a0d0e21e 310 }
b971f6e4 311
a0d0e21e 312}
313
c623bd54 314print STDERR " Installation complete\n";
315
316exit 0;
317
318###############################################################################
319
a0d0e21e 320sub yn {
321 local($prompt) = @_;
322 local($answer);
323 local($default) = $prompt =~ m/\[([yn])\]\s*$/i;
324 print STDERR $prompt;
325 chop($answer = <STDIN>);
326 $answer = $default if $answer =~ m/^\s*$/;
327 ($answer =~ m/^[yY]/);
328}
329
c623bd54 330sub unlink {
331 local(@names) = @_;
39add537 332 my($cnt) = 0;
c623bd54 333
334 foreach $name (@names) {
335 next unless -e $name;
eef1cf2a 336 chmod 0777, $name if ($^O eq 'os2' || $^O eq 'MSWin32');
c623bd54 337 print STDERR " unlink $name\n";
39add537 338 ( CORE::unlink($name) and ++$cnt
339 or warn "Couldn't unlink $name: $!\n" ) unless $nonono;
c623bd54 340 }
39add537 341 return $cnt;
c623bd54 342}
343
e50aee73 344sub safe_unlink {
68dc0745 345 return if $nonono;
346 local @names = @_;
e50aee73 347 foreach $name (@names) {
348 next unless -e $name;
eef1cf2a 349 chmod 0777, $name if ($^O eq 'os2' || $^O eq 'MSWin32');
39add537 350 print STDERR " unlink $name\n";
351 next if CORE::unlink($name);
e50aee73 352 warn "Couldn't unlink $name: $!\n";
353 if ($! =~ /busy/i) {
354 print STDERR " mv $name $name.old\n";
68dc0745 355 safe_rename($name, "$name.old")
356 or warn "Couldn't rename $name: $!\n";
e50aee73 357 }
358 }
359}
360
68dc0745 361sub safe_rename {
e50aee73 362 local($from,$to) = @_;
39add537 363 if (-f $to and not unlink($to)) {
e50aee73 364 my($i);
365 for ($i = 1; $i < 50; $i++) {
91a06757 366 last if rename($to, "$to.$i");
e50aee73 367 }
39add537 368 warn("Cannot rename to `$to.$i': $!"), return 0
369 if $i >= 50; # Give up!
e50aee73 370 }
371 link($from,$to) || return 0;
372 unlink($from);
373}
374
c623bd54 375sub link {
15792f64 376 my($from,$to) = @_;
377 my($success) = 0;
c623bd54 378
379 print STDERR " ln $from $to\n";
39add537 380 eval {
68dc0745 381 CORE::link($from, $to)
382 ? $success++
7bac28a0 383 : ($from =~ m#^/afs/# || $to =~ m#^/afs/#)
384 ? die "AFS" # okay inside eval {}
385 : warn "Couldn't link $from to $to: $!\n"
68dc0745 386 unless $nonono;
39add537 387 };
388 if ($@) {
68dc0745 389 File::Copy::copy($from, $to)
390 ? $success++
391 : warn "Couldn't copy $from to $to: $!\n"
392 unless $nonono;
39add537 393 }
15792f64 394 $success;
c623bd54 395}
396
397sub chmod {
398 local($mode,$name) = @_;
399
39e571d4 400 return if ($^O eq 'dos');
c623bd54 401 printf STDERR " chmod %o %s\n", $mode, $name;
68dc0745 402 CORE::chmod($mode,$name)
403 || warn sprintf("Couldn't chmod %o %s: $!\n", $mode, $name)
404 unless $nonono;
405}
406
407sub copy {
408 my($from,$to) = @_;
409
410 print STDERR " cp $from $to\n";
411 File::Copy::copy($from, $to)
412 || warn "Couldn't copy $from to $to: $!\n"
413 unless $nonono;
c623bd54 414}
415
a0d0e21e 416sub samepath {
417 local($p1, $p2) = @_;
eef1cf2a 418
419 return (lc($p1) eq lc($p2)) if ($^O eq 'MSWin32');
a0d0e21e 420
75f92628 421 if ($p1 ne $p2) {
eef1cf2a 422 local($dev1, $ino1, $dev2, $ino2);
a0d0e21e 423 ($dev1, $ino1) = stat($p1);
424 ($dev2, $ino2) = stat($p2);
425 ($dev1 == $dev2 && $ino1 == $ino2);
426 }
427 else {
428 1;
429 }
430}
431
432sub installlib {
433 my $dir = $File::Find::dir;
434 $dir =~ s#^\.(?![^/])/?##;
0ecb046b 435 local($depth) = $dir ? "lib/$dir" : "lib";
a0d0e21e 436
437 my $name = $_;
efc5ad56 438
439 if ($name eq 'CVS' && -d $name) {
440 $File::Find::prune = 1;
441 return;
442 }
e50aee73 443
444 # ignore patch backups and the .exists files.
445 return if $name =~ m{\.orig$|~$|^\.exists};
446
a0d0e21e 447 $name = "$dir/$name" if $dir ne '';
448
449 my $installlib = $installprivlib;
1dd15ed4 450 if ($dir =~ /^auto/ ||
eef1cf2a 451 ($name =~ /^(.*)\.(?:pm|pod)$/ && $archpms{$1}) ||
452 ($name =~ /^(.*)\.(?:h|lib)$/i && $^O eq 'MSWin32')
453 ) {
a0d0e21e 454 $installlib = $installarchlib;
455 return unless $do_installarchlib;
456 } else {
457 return unless $do_installprivlib;
458 }
459
a0d0e21e 460 if (-f $_) {
5cc330c8 461 if (/\.(?:al|ix)$/ && !($dir =~ m[^auto/(.*)$] && $archpms{$1})) {
3edbfbe5 462 $installlib = $installprivlib;
463 #We're installing *.al and *.ix files into $installprivlib,
464 #but we have to delete old *.al and *.ix files from the 5.000
465 #distribution:
75f92628 466 #This might not work because $archname might have changed.
68dc0745 467 unlink("$installarchlib/$name");
3edbfbe5 468 }
5f05dabc 469 if (compare($_, "$installlib/$name") || $nonono) {
68dc0745 470 unlink("$installlib/$name");
fcbdbaa8 471 mkpath("$installlib/$dir", 1, 0777);
75f92628 472 # HP-UX (at least) needs to maintain execute permissions
473 # on dynamically-loaded libraries.
68dc0745 474 copy_if_diff($_, "$installlib/$name")
475 and chmod($name =~ /\.(so|$dlext)$/o ? 0555 : 0444,
aa3cf465 476 "$installlib/$name");
a0d0e21e 477 }
478 } elsif (-d $_) {
fcbdbaa8 479 mkpath("$installlib/$name", 1, 0777);
a0d0e21e 480 }
481}
3edbfbe5 482
16d20bd9 483# Copy $from to $to, only if $from is different than $to.
484# Also preserve modification times for .a libraries.
485# On some systems, if you do
486# ranlib libperl.a
487# cp libperl.a /usr/local/lib/perl5/archlib/CORE/libperl.a
488# and then try to link against the installed libperl.a, you might
489# get an error message to the effect that the symbol table is older
490# than the library.
aa3cf465 491# Return true if copying occurred.
68dc0745 492
493sub copy_if_diff {
3edbfbe5 494 my($from,$to)=@_;
495 -f $from || die "$0: $from not found";
5f05dabc 496 if (compare($from, $to) || $nonono) {
68dc0745 497 safe_unlink($to); # In case we don't have write permissions.
0ecb046b 498 if ($nonono) {
499 $from = $depth . "/" . $from if $depth;
500 }
68dc0745 501 copy($from, $to);
502 # Restore timestamps if it's a .a library or for OS/2.
503 if (!$nonono && ($^O eq 'os2' || $to =~ /\.a$/)) {
504 my ($atime, $mtime) = (stat $from)[8,9];
16d20bd9 505 utime $atime, $mtime, $to;
506 }
aa3cf465 507 1;
3edbfbe5 508 }
509}