xref: /onnv-gate/usr/src/cmd/abi/appcert/scripts/appcert.pl (revision 0:68f95e015346)
1*0Sstevel@tonic-gate#!/usr/perl5/bin/perl -w
2*0Sstevel@tonic-gate#
3*0Sstevel@tonic-gate# CDDL HEADER START
4*0Sstevel@tonic-gate#
5*0Sstevel@tonic-gate# The contents of this file are subject to the terms of the
6*0Sstevel@tonic-gate# Common Development and Distribution License, Version 1.0 only
7*0Sstevel@tonic-gate# (the "License").  You may not use this file except in compliance
8*0Sstevel@tonic-gate# with the License.
9*0Sstevel@tonic-gate#
10*0Sstevel@tonic-gate# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
11*0Sstevel@tonic-gate# or http://www.opensolaris.org/os/licensing.
12*0Sstevel@tonic-gate# See the License for the specific language governing permissions
13*0Sstevel@tonic-gate# and limitations under the License.
14*0Sstevel@tonic-gate#
15*0Sstevel@tonic-gate# When distributing Covered Code, include this CDDL HEADER in each
16*0Sstevel@tonic-gate# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
17*0Sstevel@tonic-gate# If applicable, add the following below this CDDL HEADER, with the
18*0Sstevel@tonic-gate# fields enclosed by brackets "[]" replaced with your own identifying
19*0Sstevel@tonic-gate# information: Portions Copyright [yyyy] [name of copyright owner]
20*0Sstevel@tonic-gate#
21*0Sstevel@tonic-gate# CDDL HEADER END
22*0Sstevel@tonic-gate#
23*0Sstevel@tonic-gate#
24*0Sstevel@tonic-gate# ident	"%Z%%M%	%I%	%E% SMI"
25*0Sstevel@tonic-gate#
26*0Sstevel@tonic-gate# Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
27*0Sstevel@tonic-gate# Use is subject to license terms.
28*0Sstevel@tonic-gate#
29*0Sstevel@tonic-gate
30*0Sstevel@tonic-gate#
31*0Sstevel@tonic-gate# This is the top level script for performing the appcert checks.  It
32*0Sstevel@tonic-gate# reads the command line options, determines list of binaries to check,
33*0Sstevel@tonic-gate# and then calls symprof (the raw symbol profiler), symcheck (that
34*0Sstevel@tonic-gate# checks for unstable behavior), and symreport (that constructs and
35*0Sstevel@tonic-gate# outputs a rollup report)
36*0Sstevel@tonic-gate#
37*0Sstevel@tonic-gate
38*0Sstevel@tonic-gaterequire 5.005;
39*0Sstevel@tonic-gateuse strict;
40*0Sstevel@tonic-gateuse locale;
41*0Sstevel@tonic-gateuse Getopt::Std;
42*0Sstevel@tonic-gateuse POSIX qw(locale_h);
43*0Sstevel@tonic-gateuse Sun::Solaris::Utils qw(textdomain gettext);
44*0Sstevel@tonic-gateuse File::Basename;
45*0Sstevel@tonic-gateuse File::Path;
46*0Sstevel@tonic-gate
47*0Sstevel@tonic-gateuse lib qw(/usr/lib/abi/appcert);
48*0Sstevel@tonic-gateuse AppcertUtil;
49*0Sstevel@tonic-gate
50*0Sstevel@tonic-gatesetlocale(LC_ALL, "");
51*0Sstevel@tonic-gatetextdomain(TEXT_DOMAIN);
52*0Sstevel@tonic-gate
53*0Sstevel@tonic-gateuse vars qw(
54*0Sstevel@tonic-gate	@item_list
55*0Sstevel@tonic-gate	$file_list
56*0Sstevel@tonic-gate	$do_not_follow_symlinks
57*0Sstevel@tonic-gate	$modify_ld_path
58*0Sstevel@tonic-gate	$append_solaris_dirs_to_ld_path
59*0Sstevel@tonic-gate	$skipped_count
60*0Sstevel@tonic-gate);
61*0Sstevel@tonic-gate
62*0Sstevel@tonic-gatemy $caught_signal = 0;
63*0Sstevel@tonic-gatemy $record_binary_call_count = 0;
64*0Sstevel@tonic-gate
65*0Sstevel@tonic-gate# The directory where the appcert specific scripts and data reside:
66*0Sstevel@tonic-gate$appcert_lib_dir = "/usr/lib/abi/appcert";
67*0Sstevel@tonic-gate
68*0Sstevel@tonic-gateset_clean_up_exit_routine(\&clean_up_exit);
69*0Sstevel@tonic-gate
70*0Sstevel@tonic-gatesignals('on', \&interrupted);
71*0Sstevel@tonic-gate
72*0Sstevel@tonic-gateget_options();
73*0Sstevel@tonic-gate
74*0Sstevel@tonic-gate@item_list = @ARGV;		# List of directories and/or objects to check.
75*0Sstevel@tonic-gatecheck_item_list();
76*0Sstevel@tonic-gate
77*0Sstevel@tonic-gateset_working_dir();
78*0Sstevel@tonic-gate
79*0Sstevel@tonic-gatefind_binaries();		# Records all of the binary objects to check.
80*0Sstevel@tonic-gate
81*0Sstevel@tonic-gatesupplement_ld_library_path();
82*0Sstevel@tonic-gate
83*0Sstevel@tonic-gateexport_vars_to_environment();	# Exports info for our child scripts to use.
84*0Sstevel@tonic-gate
85*0Sstevel@tonic-gaterun_profiler();			# Run the script symprof.
86*0Sstevel@tonic-gate
87*0Sstevel@tonic-gaterun_checker();			# Run script symcheck.
88*0Sstevel@tonic-gate
89*0Sstevel@tonic-gaterun_report_generator();		# Run the script symreport.
90*0Sstevel@tonic-gate
91*0Sstevel@tonic-gatemy $rc = overall_result_code();
92*0Sstevel@tonic-gate
93*0Sstevel@tonic-gateclean_up();
94*0Sstevel@tonic-gate
95*0Sstevel@tonic-gateexit $rc;
96*0Sstevel@tonic-gate
97*0Sstevel@tonic-gate
98*0Sstevel@tonic-gate#
99*0Sstevel@tonic-gate# This subroutine calls getopts() and sets up variables reflecting how
100*0Sstevel@tonic-gate# we were called.
101*0Sstevel@tonic-gate#
102*0Sstevel@tonic-gatesub get_options
103*0Sstevel@tonic-gate{
104*0Sstevel@tonic-gate	my %opt;
105*0Sstevel@tonic-gate
106*0Sstevel@tonic-gate	getopts('?hnLBSw:f:', \%opt) || (show_usage() && exiter(2));
107*0Sstevel@tonic-gate
108*0Sstevel@tonic-gate	if (exists($opt{'?'}) || exists($opt{'h'})) {
109*0Sstevel@tonic-gate		show_usage();
110*0Sstevel@tonic-gate		exiter(2);
111*0Sstevel@tonic-gate	}
112*0Sstevel@tonic-gate
113*0Sstevel@tonic-gate	if (exists($opt{'f'})) {
114*0Sstevel@tonic-gate		$file_list = $opt{'f'};
115*0Sstevel@tonic-gate	} else {
116*0Sstevel@tonic-gate		$file_list = '';
117*0Sstevel@tonic-gate	}
118*0Sstevel@tonic-gate
119*0Sstevel@tonic-gate	if (exists($opt{'w'})) {
120*0Sstevel@tonic-gate		$working_dir = $opt{'w'};
121*0Sstevel@tonic-gate	} else {
122*0Sstevel@tonic-gate		$working_dir = '';
123*0Sstevel@tonic-gate	}
124*0Sstevel@tonic-gate	if ($working_dir =~ /'/) {
125*0Sstevel@tonic-gate		#
126*0Sstevel@tonic-gate		# This character will ultimately cause problems with
127*0Sstevel@tonic-gate		# system() and pipelines so we exit now.
128*0Sstevel@tonic-gate		#
129*0Sstevel@tonic-gate		exiter(sprintf(gettext(
130*0Sstevel@tonic-gate		    "directory contains the single-quote character ': %s\n"),
131*0Sstevel@tonic-gate		    $working_dir));
132*0Sstevel@tonic-gate	}
133*0Sstevel@tonic-gate
134*0Sstevel@tonic-gate	if (defined($opt{'B'})) {
135*0Sstevel@tonic-gate		$batch_report = 1;
136*0Sstevel@tonic-gate	} else {
137*0Sstevel@tonic-gate		$batch_report = 0;
138*0Sstevel@tonic-gate	}
139*0Sstevel@tonic-gate
140*0Sstevel@tonic-gate	if (defined($opt{'n'})) {
141*0Sstevel@tonic-gate		$do_not_follow_symlinks = 1;
142*0Sstevel@tonic-gate	} else {
143*0Sstevel@tonic-gate		$do_not_follow_symlinks = 0;
144*0Sstevel@tonic-gate	}
145*0Sstevel@tonic-gate
146*0Sstevel@tonic-gate	if (defined($opt{'L'})) {
147*0Sstevel@tonic-gate		$modify_ld_path = 0;
148*0Sstevel@tonic-gate	} else {
149*0Sstevel@tonic-gate		$modify_ld_path = 1;
150*0Sstevel@tonic-gate	}
151*0Sstevel@tonic-gate
152*0Sstevel@tonic-gate	if (defined($opt{'S'})) {
153*0Sstevel@tonic-gate		$append_solaris_dirs_to_ld_path = 1;
154*0Sstevel@tonic-gate	} else {
155*0Sstevel@tonic-gate		$append_solaris_dirs_to_ld_path = 0;
156*0Sstevel@tonic-gate	}
157*0Sstevel@tonic-gate}
158*0Sstevel@tonic-gate
159*0Sstevel@tonic-gate#
160*0Sstevel@tonic-gate# Performs an initial check to see if the user supplied anything at all
161*0Sstevel@tonic-gate# to check.  Also reads in the file list if the user supplied one via -f <file>
162*0Sstevel@tonic-gate#
163*0Sstevel@tonic-gatesub check_item_list
164*0Sstevel@tonic-gate{
165*0Sstevel@tonic-gate	# Add the items if the -f flag was used.
166*0Sstevel@tonic-gate	if ($file_list) {
167*0Sstevel@tonic-gate		my $file;
168*0Sstevel@tonic-gate		my $list_fh = do { local *FH; *FH };
169*0Sstevel@tonic-gate		if (-f $file_list && open($list_fh, "<$file_list")) {
170*0Sstevel@tonic-gate			while (<$list_fh>) {
171*0Sstevel@tonic-gate				chomp($file = $_);
172*0Sstevel@tonic-gate				push(@item_list, $file);
173*0Sstevel@tonic-gate			}
174*0Sstevel@tonic-gate			close($list_fh);
175*0Sstevel@tonic-gate		} else {
176*0Sstevel@tonic-gate			exiter(nofile($file_list, $!));
177*0Sstevel@tonic-gate		}
178*0Sstevel@tonic-gate	}
179*0Sstevel@tonic-gate
180*0Sstevel@tonic-gate	return if (@item_list);
181*0Sstevel@tonic-gate
182*0Sstevel@tonic-gate	emsg("$command_name: " . gettext(
183*0Sstevel@tonic-gate	    "at least one file or directory to check must be specified.") .
184*0Sstevel@tonic-gate	    "\n\n");
185*0Sstevel@tonic-gate
186*0Sstevel@tonic-gate	show_usage();
187*0Sstevel@tonic-gate	exiter(3);
188*0Sstevel@tonic-gate}
189*0Sstevel@tonic-gate
190*0Sstevel@tonic-gate#
191*0Sstevel@tonic-gate# This subroutine sets up the working directory, the default something
192*0Sstevel@tonic-gate# like: /tmp/appcert.<PID>
193*0Sstevel@tonic-gate#
194*0Sstevel@tonic-gatesub set_working_dir
195*0Sstevel@tonic-gate{
196*0Sstevel@tonic-gate	if ($working_dir) {
197*0Sstevel@tonic-gate		# working_dir has been set in get_options().
198*0Sstevel@tonic-gate		if (! -d $working_dir) {
199*0Sstevel@tonic-gate			if (! mkpath($working_dir) || ! -d $working_dir) {
200*0Sstevel@tonic-gate				exiter(nocreatedir($working_dir, $!));
201*0Sstevel@tonic-gate			}
202*0Sstevel@tonic-gate		} else {
203*0Sstevel@tonic-gate			if (! dir_is_empty($working_dir)) {
204*0Sstevel@tonic-gate				# create a subdir of it for our use.
205*0Sstevel@tonic-gate				$working_dir = create_tmp_dir($working_dir);
206*0Sstevel@tonic-gate			}
207*0Sstevel@tonic-gate		}
208*0Sstevel@tonic-gate	} else {
209*0Sstevel@tonic-gate		# Default case: will create, e.g., /tmp/appcert.12345
210*0Sstevel@tonic-gate		$working_dir = create_tmp_dir();
211*0Sstevel@tonic-gate	}
212*0Sstevel@tonic-gate
213*0Sstevel@tonic-gate	if (! -d $working_dir) {
214*0Sstevel@tonic-gate		# We have no working directory.
215*0Sstevel@tonic-gate		exiter(nocreatedir($working_dir));
216*0Sstevel@tonic-gate	}
217*0Sstevel@tonic-gate
218*0Sstevel@tonic-gate	#
219*0Sstevel@tonic-gate	# Create a subdirectory of working_dir that will contain all of
220*0Sstevel@tonic-gate	# the object subdirs.
221*0Sstevel@tonic-gate	#
222*0Sstevel@tonic-gate	my $dir = "$working_dir/$object_dir";
223*0Sstevel@tonic-gate	if (! mkpath($dir) || ! -d $dir) {
224*0Sstevel@tonic-gate		exiter(nocreatedir($dir, $!));
225*0Sstevel@tonic-gate	}
226*0Sstevel@tonic-gate	#
227*0Sstevel@tonic-gate	# Make a tmp subdirectory for small temporary work. It is
228*0Sstevel@tonic-gate	# preferred to have it on tmpfs (especially not NFS) for
229*0Sstevel@tonic-gate	# performance reasons.
230*0Sstevel@tonic-gate	#
231*0Sstevel@tonic-gate	$tmp_dir = "/tmp/${command_name}_tmp.$$";
232*0Sstevel@tonic-gate	if (-d $tmp_dir) {
233*0Sstevel@tonic-gate		exiter(nocreatedir("$tmp_dir", $!));
234*0Sstevel@tonic-gate	}
235*0Sstevel@tonic-gate	if (! mkpath($tmp_dir, 0, 0700) || ! -d $tmp_dir) {
236*0Sstevel@tonic-gate		emsg("%s", nocreatedir($tmp_dir, $!));
237*0Sstevel@tonic-gate		# fall back to our output dir (which could have slow access)
238*0Sstevel@tonic-gate		$tmp_dir = "$working_dir/tmp";
239*0Sstevel@tonic-gate		if (! mkpath($tmp_dir)) {
240*0Sstevel@tonic-gate			exiter(nocreatedir($tmp_dir, $!));
241*0Sstevel@tonic-gate		}
242*0Sstevel@tonic-gate	}
243*0Sstevel@tonic-gate
244*0Sstevel@tonic-gate	if (! -d $tmp_dir) {
245*0Sstevel@tonic-gate		exiter(nocreatedir($tmp_dir, $!));
246*0Sstevel@tonic-gate	}
247*0Sstevel@tonic-gate}
248*0Sstevel@tonic-gate
249*0Sstevel@tonic-gate#
250*0Sstevel@tonic-gate# Top level function to find all the binaries to be checked.  Calls
251*0Sstevel@tonic-gate# record_binary() to do the actual deciding and recording.
252*0Sstevel@tonic-gate#
253*0Sstevel@tonic-gate# The array @item_list contains all the items to find.
254*0Sstevel@tonic-gate#
255*0Sstevel@tonic-gatesub find_binaries
256*0Sstevel@tonic-gate{
257*0Sstevel@tonic-gate	$binary_count = 0;
258*0Sstevel@tonic-gate
259*0Sstevel@tonic-gate	my $skipped_file = "$working_dir/Skipped";
260*0Sstevel@tonic-gate	my $skipped_fh = do { local *FH; *FH };
261*0Sstevel@tonic-gate	open($skipped_fh, ">$skipped_file") ||
262*0Sstevel@tonic-gate	    exiter(nofile($skipped_file, $!));
263*0Sstevel@tonic-gate
264*0Sstevel@tonic-gate	$skipped_count = 0;
265*0Sstevel@tonic-gate
266*0Sstevel@tonic-gate	my ($item, $args, $file);
267*0Sstevel@tonic-gate	emsg("\n" .  gettext(
268*0Sstevel@tonic-gate	    "finding executables and shared libraries to check") . " ...\n");
269*0Sstevel@tonic-gate
270*0Sstevel@tonic-gate	$args = '';
271*0Sstevel@tonic-gate	$args .= '-follow ' unless ($do_not_follow_symlinks);
272*0Sstevel@tonic-gate	$args .= '-type f -print';
273*0Sstevel@tonic-gate
274*0Sstevel@tonic-gate	my $quote_fmt = gettext(
275*0Sstevel@tonic-gate	    "skipping:  item contains the single-quote character ': %s\n");
276*0Sstevel@tonic-gate
277*0Sstevel@tonic-gate	foreach $item (@item_list) {
278*0Sstevel@tonic-gate		if (! -e $item) {
279*0Sstevel@tonic-gate			emsg(gettext("skipping:  %s: %s\n"), $item, $!);
280*0Sstevel@tonic-gate			print $skipped_fh "$item: no_exist\n";
281*0Sstevel@tonic-gate			$skipped_count++;
282*0Sstevel@tonic-gate			next;
283*0Sstevel@tonic-gate		} elsif ($item =~ /'/)  {
284*0Sstevel@tonic-gate			emsg($quote_fmt, $item);
285*0Sstevel@tonic-gate			print $skipped_fh "$item: item_has_bad_char\n";
286*0Sstevel@tonic-gate			$skipped_count++;
287*0Sstevel@tonic-gate			next;
288*0Sstevel@tonic-gate		}
289*0Sstevel@tonic-gate		# note that $item does not contain a single-quote.
290*0Sstevel@tonic-gate		my $find_fh = do { local *FH; *FH };
291*0Sstevel@tonic-gate		open($find_fh, "$cmd_find '$item' $args|") ||
292*0Sstevel@tonic-gate		    exiter(norunprog("$cmd_find '$item' $args", $!));
293*0Sstevel@tonic-gate
294*0Sstevel@tonic-gate		while (<$find_fh>) {
295*0Sstevel@tonic-gate			chomp($file = $_);
296*0Sstevel@tonic-gate			#
297*0Sstevel@tonic-gate			# We are free to remove leading "./". This will
298*0Sstevel@tonic-gate			# minimize directory names we create that would
299*0Sstevel@tonic-gate			# start with a dot.
300*0Sstevel@tonic-gate			#
301*0Sstevel@tonic-gate			$file =~ s,^\./,,;
302*0Sstevel@tonic-gate
303*0Sstevel@tonic-gate			next if ($file eq '');
304*0Sstevel@tonic-gate
305*0Sstevel@tonic-gate			record_binary($file, $skipped_fh);
306*0Sstevel@tonic-gate		}
307*0Sstevel@tonic-gate		close($find_fh);
308*0Sstevel@tonic-gate	}
309*0Sstevel@tonic-gate
310*0Sstevel@tonic-gate	if ($binary_count == 0) {
311*0Sstevel@tonic-gate		exiter("$command_name: " . gettext(
312*0Sstevel@tonic-gate		    "no checkable binary objects were found."), 3);
313*0Sstevel@tonic-gate	}
314*0Sstevel@tonic-gate
315*0Sstevel@tonic-gate	if ($skipped_count == 0) {
316*0Sstevel@tonic-gate		print $skipped_fh "# NO_FILES_WERE_SKIPPED\n";
317*0Sstevel@tonic-gate	}
318*0Sstevel@tonic-gate	close($skipped_fh);
319*0Sstevel@tonic-gate}
320*0Sstevel@tonic-gate
321*0Sstevel@tonic-gate#
322*0Sstevel@tonic-gate# This subroutine will determine if a binary is checkable.
323*0Sstevel@tonic-gate#
324*0Sstevel@tonic-gate# If so, it will reserve a directory for its output in the $working_dir
325*0Sstevel@tonic-gate# location, and store the output of a number of commands there.
326*0Sstevel@tonic-gate#
327*0Sstevel@tonic-gatesub record_binary
328*0Sstevel@tonic-gate{
329*0Sstevel@tonic-gate	my ($file, $skipped_fh) = @_;
330*0Sstevel@tonic-gate
331*0Sstevel@tonic-gate	if ((++$record_binary_call_count % 500) == 0) {
332*0Sstevel@tonic-gate		#
333*0Sstevel@tonic-gate		# This indicates are being called many times for a large
334*0Sstevel@tonic-gate		# product.  Clear out our caches.
335*0Sstevel@tonic-gate		#
336*0Sstevel@tonic-gate		purge_caches();
337*0Sstevel@tonic-gate	}
338*0Sstevel@tonic-gate
339*0Sstevel@tonic-gate	#
340*0Sstevel@tonic-gate	# Check if the object exists and is regular file.  Note that
341*0Sstevel@tonic-gate	# this test also passes a symlink as long as that symlink
342*0Sstevel@tonic-gate	# ultimately refers to a regular file.
343*0Sstevel@tonic-gate	#
344*0Sstevel@tonic-gate	if (! -f $file) {
345*0Sstevel@tonic-gate		emsg(gettext("skipping:  not a file: %s\n"), $file);
346*0Sstevel@tonic-gate		print $skipped_fh "$file: not_a_file\n";
347*0Sstevel@tonic-gate		$skipped_count++;
348*0Sstevel@tonic-gate		return 0;
349*0Sstevel@tonic-gate	}
350*0Sstevel@tonic-gate
351*0Sstevel@tonic-gate	# Check if it is readable:
352*0Sstevel@tonic-gate	if (! -r $file) {
353*0Sstevel@tonic-gate		emsg(gettext("skipping:  cannot read: %s\n"), $file);
354*0Sstevel@tonic-gate		print $skipped_fh "$file: unreadable\n";
355*0Sstevel@tonic-gate		$skipped_count++;
356*0Sstevel@tonic-gate		return 0;
357*0Sstevel@tonic-gate	}
358*0Sstevel@tonic-gate
359*0Sstevel@tonic-gate	#
360*0Sstevel@tonic-gate	# Since the filename will be used as operands passed to utility
361*0Sstevel@tonic-gate	# commands via the shell, we exclude at the outset certain meta
362*0Sstevel@tonic-gate	# characters in the filenames.
363*0Sstevel@tonic-gate	#
364*0Sstevel@tonic-gate	my $quote_fmt = gettext(
365*0Sstevel@tonic-gate	    "skipping:  filename contains the single-quote character: ': %s\n");
366*0Sstevel@tonic-gate	if ($file =~ /'/) {
367*0Sstevel@tonic-gate		emsg($quote_fmt, $file);
368*0Sstevel@tonic-gate		print $skipped_fh "$file: filename_has_bad_char\n";
369*0Sstevel@tonic-gate		$skipped_count++;
370*0Sstevel@tonic-gate		return 0;
371*0Sstevel@tonic-gate	}
372*0Sstevel@tonic-gate
373*0Sstevel@tonic-gate	my $newline_fmt = gettext(
374*0Sstevel@tonic-gate	    "skipping:  filename contains the newline character: \\n: %s\n");
375*0Sstevel@tonic-gate	if ($file =~ /\n/) {
376*0Sstevel@tonic-gate		emsg($newline_fmt, $file);
377*0Sstevel@tonic-gate		print $skipped_fh "$file: filename_has_bad_char\n";
378*0Sstevel@tonic-gate		$skipped_count++;
379*0Sstevel@tonic-gate		return 0;
380*0Sstevel@tonic-gate	}
381*0Sstevel@tonic-gate
382*0Sstevel@tonic-gate	my $pipe_fmt = gettext(
383*0Sstevel@tonic-gate	    "skipping:  filename contains the pipe character: \|: %s\n");
384*0Sstevel@tonic-gate	if ($file =~ /\|/) {
385*0Sstevel@tonic-gate		emsg($pipe_fmt, $file);
386*0Sstevel@tonic-gate		print $skipped_fh "$file: filename_has_bad_char\n";
387*0Sstevel@tonic-gate		$skipped_count++;
388*0Sstevel@tonic-gate		return 0;
389*0Sstevel@tonic-gate	}
390*0Sstevel@tonic-gate
391*0Sstevel@tonic-gate	my $file_output;
392*0Sstevel@tonic-gate
393*0Sstevel@tonic-gate	# Run the file(1) command on it.
394*0Sstevel@tonic-gate
395*0Sstevel@tonic-gate	c_locale(1);
396*0Sstevel@tonic-gate	# note that $file does not contain a single-quote.
397*0Sstevel@tonic-gate	$file_output = `$cmd_file '$file' 2>/dev/null`;
398*0Sstevel@tonic-gate	c_locale(0);
399*0Sstevel@tonic-gate
400*0Sstevel@tonic-gate	if ($file_output =~ /script$/) {
401*0Sstevel@tonic-gate		$file_output =~ s/:\s+/: /;
402*0Sstevel@tonic-gate		$file_output =~ s/: /: script /;
403*0Sstevel@tonic-gate		print $skipped_fh "$file_output";
404*0Sstevel@tonic-gate
405*0Sstevel@tonic-gate		#
406*0Sstevel@tonic-gate		# again now without the c_locale() setting:
407*0Sstevel@tonic-gate		# note that $file does not contain a single-quote.
408*0Sstevel@tonic-gate		#
409*0Sstevel@tonic-gate		$file_output = `$cmd_file '$file' 2>/dev/null`;
410*0Sstevel@tonic-gate		$file_output =~ s/:\s+/: /;
411*0Sstevel@tonic-gate		emsg(gettext("skipping:  %s"), $file_output);
412*0Sstevel@tonic-gate		$skipped_count++;
413*0Sstevel@tonic-gate		return 0;
414*0Sstevel@tonic-gate	}
415*0Sstevel@tonic-gate
416*0Sstevel@tonic-gate	# create ELF and a.out matching regex:
417*0Sstevel@tonic-gate	my $object_match =
418*0Sstevel@tonic-gate	    'ELF.*executable.*dynamically' . '|' .
419*0Sstevel@tonic-gate	    'ELF.*dynamic lib' . '|' .
420*0Sstevel@tonic-gate	    'ELF.*executable.*statically' . '|' .
421*0Sstevel@tonic-gate	    'Sun demand paged SPARC.*dynamically linked' . '|' .
422*0Sstevel@tonic-gate	    'Sun demand paged SPARC executable' . '|' .
423*0Sstevel@tonic-gate	    'pure SPARC executable' . '|' .
424*0Sstevel@tonic-gate	    'impure SPARC executable';
425*0Sstevel@tonic-gate
426*0Sstevel@tonic-gate	#
427*0Sstevel@tonic-gate	# Note that we let the "statically linked" binaries through
428*0Sstevel@tonic-gate	# here, but will catch them later in the profiler and checker.
429*0Sstevel@tonic-gate	#
430*0Sstevel@tonic-gate
431*0Sstevel@tonic-gate	if ($file_output !~ /$object_match/io) {
432*0Sstevel@tonic-gate		# it is not an ELF object file and so does not interest us.
433*0Sstevel@tonic-gate		return 0;
434*0Sstevel@tonic-gate	}
435*0Sstevel@tonic-gate
436*0Sstevel@tonic-gate	my $exec_fmt = gettext(
437*0Sstevel@tonic-gate	    "skipping:  must have exec permission to be checked: %s\n");
438*0Sstevel@tonic-gate	if (! -x $file) {
439*0Sstevel@tonic-gate		#
440*0Sstevel@tonic-gate		# It interests us, but the execute bit not set.  Shared
441*0Sstevel@tonic-gate		# objects will be let through here since ldd will still
442*0Sstevel@tonic-gate		# work on them (since it uses lddstub).  Otherwise, we
443*0Sstevel@tonic-gate		# cannot check it.
444*0Sstevel@tonic-gate		#
445*0Sstevel@tonic-gate		if (! is_shared_object($file)) {
446*0Sstevel@tonic-gate			# warn the user exec bit should be set:
447*0Sstevel@tonic-gate			emsg($exec_fmt, $file);
448*0Sstevel@tonic-gate			print $skipped_fh "$file: no_exec_permission\n";
449*0Sstevel@tonic-gate			$skipped_count++;
450*0Sstevel@tonic-gate			return 0;
451*0Sstevel@tonic-gate		}
452*0Sstevel@tonic-gate	}
453*0Sstevel@tonic-gate
454*0Sstevel@tonic-gate	#
455*0Sstevel@tonic-gate	# Rather than let ldd fail later on in symprof, we check the
456*0Sstevel@tonic-gate	# arch here to make sure it matches $uname_p.  If it does not
457*0Sstevel@tonic-gate	# match, we anticipate a 64-bit application and so we
458*0Sstevel@tonic-gate	# immediately test how ldd will handle it (kernel might be
459*0Sstevel@tonic-gate	# 32-bit, etc).
460*0Sstevel@tonic-gate	#
461*0Sstevel@tonic-gate	my ($arch, $type, $wordsize, $endian, $e_machine) = bin_type($file);
462*0Sstevel@tonic-gate
463*0Sstevel@tonic-gate	if ($arch !~ /^${uname_p}$/io) {
464*0Sstevel@tonic-gate		my ($ldd_output, $ldd_output2);
465*0Sstevel@tonic-gate
466*0Sstevel@tonic-gate		#
467*0Sstevel@tonic-gate		# Now run ldd on it to see how things would go.  If it
468*0Sstevel@tonic-gate		# fails we must skip it.
469*0Sstevel@tonic-gate		#
470*0Sstevel@tonic-gate		c_locale(1);
471*0Sstevel@tonic-gate		# note that $file does not contain single-quote
472*0Sstevel@tonic-gate		$ldd_output = `$cmd_ldd '$file' 2>&1 1>/dev/null`;
473*0Sstevel@tonic-gate		c_locale(0);
474*0Sstevel@tonic-gate		if ($? != 0) {
475*0Sstevel@tonic-gate			# note that $file does not contain a single-quote
476*0Sstevel@tonic-gate			$ldd_output2 = `$cmd_ldd '$file' 2>&1 1>/dev/null`;
477*0Sstevel@tonic-gate			$ldd_output	=~ s/\n.*$//;
478*0Sstevel@tonic-gate			$ldd_output2	=~ s/\n.*$//;
479*0Sstevel@tonic-gate			if ($ldd_output !~ /wrong class/) {
480*0Sstevel@tonic-gate				$ldd_output = "$file: " . sprintf(
481*0Sstevel@tonic-gate				    gettext("ldd failed for arch: %s"), $arch);
482*0Sstevel@tonic-gate				$ldd_output2 = $ldd_output;
483*0Sstevel@tonic-gate			} else {
484*0Sstevel@tonic-gate				$ldd_output	.= " ($arch)";
485*0Sstevel@tonic-gate				$ldd_output2	.= " ($arch)";
486*0Sstevel@tonic-gate			}
487*0Sstevel@tonic-gate			$ldd_output	=~ s/:\s+/: /;
488*0Sstevel@tonic-gate			$ldd_output2	=~ s/:\s+/: /;
489*0Sstevel@tonic-gate			emsg(gettext("skipping:  %s\n"), $ldd_output2);
490*0Sstevel@tonic-gate			$ldd_output =~ s/: /: ldd_failed /;
491*0Sstevel@tonic-gate			print $skipped_fh "$ldd_output\n";
492*0Sstevel@tonic-gate			$skipped_count++;
493*0Sstevel@tonic-gate			return 0;
494*0Sstevel@tonic-gate		}
495*0Sstevel@tonic-gate	}
496*0Sstevel@tonic-gate
497*0Sstevel@tonic-gate	# From this point on, object is one we decided to check.
498*0Sstevel@tonic-gate
499*0Sstevel@tonic-gate	# Create the directory name for this object:
500*0Sstevel@tonic-gate	my $dirname = object_to_dir_name($file);
501*0Sstevel@tonic-gate	my $dirpath = "$working_dir/$dirname";
502*0Sstevel@tonic-gate	my $early_fmt = gettext(
503*0Sstevel@tonic-gate	    "skipping:  %s referenced earlier on the command line\n");
504*0Sstevel@tonic-gate	if (-e $dirpath) {
505*0Sstevel@tonic-gate		#
506*0Sstevel@tonic-gate		# Directory already exists.  We assume this means the
507*0Sstevel@tonic-gate		# user listed it twice (possibly indirectly via "find").
508*0Sstevel@tonic-gate		#
509*0Sstevel@tonic-gate		emsg($early_fmt, $file);
510*0Sstevel@tonic-gate		return 0;
511*0Sstevel@tonic-gate	}
512*0Sstevel@tonic-gate
513*0Sstevel@tonic-gate	if (! mkdir($dirpath, 0777)) {
514*0Sstevel@tonic-gate		exiter(nocreatedir($dirpath, $!));
515*0Sstevel@tonic-gate	}
516*0Sstevel@tonic-gate
517*0Sstevel@tonic-gate	$binary_count++;
518*0Sstevel@tonic-gate
519*0Sstevel@tonic-gate	# Record binary object's location:
520*0Sstevel@tonic-gate	my $path_fh = do { local *FH; *FH };
521*0Sstevel@tonic-gate	open($path_fh, ">$dirpath/info.path") ||
522*0Sstevel@tonic-gate	    exiter(nofile("$dirpath/info.path", $!));
523*0Sstevel@tonic-gate	print $path_fh $file, "\n";
524*0Sstevel@tonic-gate	close($path_fh);
525*0Sstevel@tonic-gate
526*0Sstevel@tonic-gate	#
527*0Sstevel@tonic-gate	# Record /usr/bin/file output.  Note that the programmatical way
528*0Sstevel@tonic-gate	# to access this info is through the command cmd_output_file().
529*0Sstevel@tonic-gate	#
530*0Sstevel@tonic-gate	my $file_fh = do { local *FH; *FH };
531*0Sstevel@tonic-gate	open($file_fh, ">$dirpath/info.file") ||
532*0Sstevel@tonic-gate	    exiter(nofile("$dirpath/info.file", $!));
533*0Sstevel@tonic-gate	print $file_fh $file_output;
534*0Sstevel@tonic-gate	close($file_fh);
535*0Sstevel@tonic-gate
536*0Sstevel@tonic-gate	#
537*0Sstevel@tonic-gate	# Record dump -Lv output.  Note that the programmatical way to
538*0Sstevel@tonic-gate	# access this info is through the command cmd_output_dump().
539*0Sstevel@tonic-gate	#
540*0Sstevel@tonic-gate	my $dump_fh = do { local *FH; *FH };
541*0Sstevel@tonic-gate	open($dump_fh, ">$dirpath/info.dump") ||
542*0Sstevel@tonic-gate	    exiter(nofile("$dirpath/info.dump", $!));
543*0Sstevel@tonic-gate
544*0Sstevel@tonic-gate	my $dump_output;
545*0Sstevel@tonic-gate	c_locale(1);
546*0Sstevel@tonic-gate	# note that $file does not contain a single-quote
547*0Sstevel@tonic-gate	$dump_output = `$cmd_dump -Lv '$file' 2>&1`;
548*0Sstevel@tonic-gate	c_locale(0);
549*0Sstevel@tonic-gate	print $dump_fh $dump_output;
550*0Sstevel@tonic-gate	close($dump_fh);
551*0Sstevel@tonic-gate
552*0Sstevel@tonic-gate	#
553*0Sstevel@tonic-gate	# Record arch and etc binary type.
554*0Sstevel@tonic-gate	#
555*0Sstevel@tonic-gate	my $arch_fh = do { local *FH; *FH };
556*0Sstevel@tonic-gate	open($arch_fh, ">$dirpath/info.arch") ||
557*0Sstevel@tonic-gate	    exiter(nofile("$dirpath/info.arch", $!));
558*0Sstevel@tonic-gate
559*0Sstevel@tonic-gate	if ($arch eq 'unknown') {
560*0Sstevel@tonic-gate		my $tmp = $file_output;
561*0Sstevel@tonic-gate		chomp($tmp);
562*0Sstevel@tonic-gate		emsg(gettext("warning:   cannot determine arch: %s\n"), $tmp);
563*0Sstevel@tonic-gate	}
564*0Sstevel@tonic-gate
565*0Sstevel@tonic-gate	print $arch_fh "ARCH: $arch\n";
566*0Sstevel@tonic-gate	print $arch_fh "TYPE: $type\n";
567*0Sstevel@tonic-gate	print $arch_fh "WORDSIZE: $wordsize\n";
568*0Sstevel@tonic-gate	print $arch_fh "BYTEORDER: $endian\n";
569*0Sstevel@tonic-gate	print $arch_fh "E_MACHINE: $e_machine\n";
570*0Sstevel@tonic-gate	close($arch_fh);
571*0Sstevel@tonic-gate
572*0Sstevel@tonic-gate	# Record the file -> directory name mapping in the index file.
573*0Sstevel@tonic-gate	my $index_file   = "$working_dir/Index";
574*0Sstevel@tonic-gate	my $index_fh = do { local *FH; *FH };
575*0Sstevel@tonic-gate	open($index_fh, ">>$index_file") ||
576*0Sstevel@tonic-gate	    exiter(nofile($index_file, $!));
577*0Sstevel@tonic-gate	print $index_fh "$file => $dirname\n";
578*0Sstevel@tonic-gate	close($index_fh);
579*0Sstevel@tonic-gate
580*0Sstevel@tonic-gate	return 1;
581*0Sstevel@tonic-gate}
582*0Sstevel@tonic-gate
583*0Sstevel@tonic-gate#
584*0Sstevel@tonic-gate# Prints the usage statement to standard out.
585*0Sstevel@tonic-gate#
586*0Sstevel@tonic-gatesub show_usage
587*0Sstevel@tonic-gate{
588*0Sstevel@tonic-gate	emsg(gettext(
589*0Sstevel@tonic-gate	"usage:	appcert [ -nBLS ] [ -f file ] [ -w dir ] { obj | dir } ...\n" .
590*0Sstevel@tonic-gate	"	Examine binary object files for use of private Solaris\n" .
591*0Sstevel@tonic-gate	"	interfaces, unstable use of static linking, and other\n" .
592*0Sstevel@tonic-gate	"	unstable practices.\n")
593*0Sstevel@tonic-gate	);
594*0Sstevel@tonic-gate}
595*0Sstevel@tonic-gate
596*0Sstevel@tonic-gate#
597*0Sstevel@tonic-gate# Examines the set of binaries to be checked and notes which ones are
598*0Sstevel@tonic-gate# shared libraries. Constructs a LD_LIBRARY_PATH that would find ALL of
599*0Sstevel@tonic-gate# these shared objects. The new directories are placed at the END of the
600*0Sstevel@tonic-gate# current LD_LIBRARY_PATH (if any).
601*0Sstevel@tonic-gate#
602*0Sstevel@tonic-gatesub supplement_ld_library_path
603*0Sstevel@tonic-gate{
604*0Sstevel@tonic-gate	my (@orig, @add_product, @add_solaris, %ldpath);
605*0Sstevel@tonic-gate
606*0Sstevel@tonic-gate	# First, note the current LD_LIBRARY_PATH parts:
607*0Sstevel@tonic-gate
608*0Sstevel@tonic-gate	my $dirname;
609*0Sstevel@tonic-gate	if (defined($ENV{'LD_LIBRARY_PATH'})) {
610*0Sstevel@tonic-gate		foreach $dirname (split(/:/, $ENV{'LD_LIBRARY_PATH'})) {
611*0Sstevel@tonic-gate			if (! exists($ldpath{$dirname})) {
612*0Sstevel@tonic-gate				push(@orig, $dirname);
613*0Sstevel@tonic-gate				$ldpath{$dirname} = 1;
614*0Sstevel@tonic-gate			}
615*0Sstevel@tonic-gate		}
616*0Sstevel@tonic-gate	}
617*0Sstevel@tonic-gate
618*0Sstevel@tonic-gate	# Next, search for ELF shared objects.
619*0Sstevel@tonic-gate	my ($dir, $path);
620*0Sstevel@tonic-gate
621*0Sstevel@tonic-gate	if ($modify_ld_path) {
622*0Sstevel@tonic-gate		while (defined($dir = next_dir_name())) {
623*0Sstevel@tonic-gate			$path = dir_name_to_path($dir);
624*0Sstevel@tonic-gate
625*0Sstevel@tonic-gate			$dirname = dirname($path);
626*0Sstevel@tonic-gate			next if (exists($ldpath{$dirname}));
627*0Sstevel@tonic-gate
628*0Sstevel@tonic-gate			#
629*0Sstevel@tonic-gate			# A colon ":" in directory name is cannot be
630*0Sstevel@tonic-gate			# accepted because that is the LD_LIBRARY_PATH
631*0Sstevel@tonic-gate			# separator.
632*0Sstevel@tonic-gate			#
633*0Sstevel@tonic-gate			next if ($dirname =~ /:/);
634*0Sstevel@tonic-gate
635*0Sstevel@tonic-gate			if (is_shared_object($path)) {
636*0Sstevel@tonic-gate				if (! exists($ldpath{$dirname})) {
637*0Sstevel@tonic-gate					push(@add_product, $dirname);
638*0Sstevel@tonic-gate					$ldpath{$dirname} = 1;
639*0Sstevel@tonic-gate				}
640*0Sstevel@tonic-gate			}
641*0Sstevel@tonic-gate		}
642*0Sstevel@tonic-gate	}
643*0Sstevel@tonic-gate
644*0Sstevel@tonic-gate	if ($append_solaris_dirs_to_ld_path) {
645*0Sstevel@tonic-gate		foreach $dirname (split(/:/, $solaris_library_ld_path)) {
646*0Sstevel@tonic-gate			if (! exists($ldpath{$dirname})) {
647*0Sstevel@tonic-gate				push(@add_solaris, $dirname);
648*0Sstevel@tonic-gate				$ldpath{$dirname} = 1;
649*0Sstevel@tonic-gate			}
650*0Sstevel@tonic-gate		}
651*0Sstevel@tonic-gate	}
652*0Sstevel@tonic-gate
653*0Sstevel@tonic-gate	# modify the LD_LIBRARY_PATH:
654*0Sstevel@tonic-gate	if (@add_product || @add_solaris) {
655*0Sstevel@tonic-gate		$ENV{'LD_LIBRARY_PATH'} =
656*0Sstevel@tonic-gate		    join(':', (@orig, @add_product, @add_solaris));
657*0Sstevel@tonic-gate	}
658*0Sstevel@tonic-gate
659*0Sstevel@tonic-gate	emsg("\n");
660*0Sstevel@tonic-gate	if (@add_product) {
661*0Sstevel@tonic-gate		emsg(gettext(
662*0Sstevel@tonic-gate		    "Shared libraries were found in the application and the\n" .
663*0Sstevel@tonic-gate		    "following directories are appended to LD_LIBRARY_PATH:\n"
664*0Sstevel@tonic-gate		    ) . "\n");
665*0Sstevel@tonic-gate
666*0Sstevel@tonic-gate		foreach $dir (@add_product) {
667*0Sstevel@tonic-gate			$dir = "./$dir" unless ($dir =~ m,^/,);
668*0Sstevel@tonic-gate			emsg("   $dir\n");
669*0Sstevel@tonic-gate		}
670*0Sstevel@tonic-gate		emsg("\n");
671*0Sstevel@tonic-gate	}
672*0Sstevel@tonic-gate
673*0Sstevel@tonic-gate	if (@add_solaris) {
674*0Sstevel@tonic-gate		emsg(gettext(
675*0Sstevel@tonic-gate		    "These Solaris library directories are being appended\n" .
676*0Sstevel@tonic-gate		    "to LD_LIBRARY_PATH:\n") . "\n");
677*0Sstevel@tonic-gate
678*0Sstevel@tonic-gate		foreach $dir (@add_solaris) {
679*0Sstevel@tonic-gate			emsg("   $dir\n");
680*0Sstevel@tonic-gate		}
681*0Sstevel@tonic-gate		emsg("\n");
682*0Sstevel@tonic-gate	}
683*0Sstevel@tonic-gate}
684*0Sstevel@tonic-gate
685*0Sstevel@tonic-gate#
686*0Sstevel@tonic-gate# Everything is correctly exported by now, and so we just run "symprof".
687*0Sstevel@tonic-gate# It is run in batches of $block_size binaries to minimize the effect of
688*0Sstevel@tonic-gate# memory usage caused by huge binaries in the product to be checked.
689*0Sstevel@tonic-gate#
690*0Sstevel@tonic-gatesub run_profiler
691*0Sstevel@tonic-gate{
692*0Sstevel@tonic-gate	my $block_size = 20;
693*0Sstevel@tonic-gate
694*0Sstevel@tonic-gate	my $i = 0;
695*0Sstevel@tonic-gate
696*0Sstevel@tonic-gate	# record old values of the blocks (if any)
697*0Sstevel@tonic-gate	my $env_min = $ENV{'AC_BLOCK_MIN'};
698*0Sstevel@tonic-gate	my $env_max = $ENV{'AC_BLOCK_MAX'};
699*0Sstevel@tonic-gate
700*0Sstevel@tonic-gate	while ($i < $binary_count) { # do each block
701*0Sstevel@tonic-gate		# export our symprof values of the block limits
702*0Sstevel@tonic-gate		$ENV{'AC_BLOCK_MIN'} = $i;
703*0Sstevel@tonic-gate		$ENV{'AC_BLOCK_MAX'} = $i + $block_size;
704*0Sstevel@tonic-gate
705*0Sstevel@tonic-gate		run_symprof();
706*0Sstevel@tonic-gate
707*0Sstevel@tonic-gate		$i += $block_size;
708*0Sstevel@tonic-gate	}
709*0Sstevel@tonic-gate
710*0Sstevel@tonic-gate	# restore old values of the blocks (if any)
711*0Sstevel@tonic-gate	if (defined($env_min)) {
712*0Sstevel@tonic-gate		$ENV{'AC_BLOCK_MIN'} = $env_min;
713*0Sstevel@tonic-gate	} else {
714*0Sstevel@tonic-gate		delete $ENV{'AC_BLOCK_MIN'};
715*0Sstevel@tonic-gate	}
716*0Sstevel@tonic-gate	if (defined($env_max)) {
717*0Sstevel@tonic-gate		$ENV{'AC_BLOCK_MAX'} = $env_max;
718*0Sstevel@tonic-gate	} else {
719*0Sstevel@tonic-gate		delete $ENV{'AC_BLOCK_MAX'};
720*0Sstevel@tonic-gate	}
721*0Sstevel@tonic-gate}
722*0Sstevel@tonic-gate
723*0Sstevel@tonic-gate#
724*0Sstevel@tonic-gate# Sub that actually runs "symprof".
725*0Sstevel@tonic-gate#
726*0Sstevel@tonic-gatesub run_symprof
727*0Sstevel@tonic-gate{
728*0Sstevel@tonic-gate	system("$appcert_lib_dir/symprof");
729*0Sstevel@tonic-gate	if ($? != 0) {
730*0Sstevel@tonic-gate		emsg("%s", utilityfailed("symprof"));
731*0Sstevel@tonic-gate		clean_up_exit(1);
732*0Sstevel@tonic-gate	}
733*0Sstevel@tonic-gate}
734*0Sstevel@tonic-gate
735*0Sstevel@tonic-gate#
736*0Sstevel@tonic-gate# Sub to run "symcheck".
737*0Sstevel@tonic-gate#
738*0Sstevel@tonic-gatesub run_checker
739*0Sstevel@tonic-gate{
740*0Sstevel@tonic-gate	system("$appcert_lib_dir/symcheck");
741*0Sstevel@tonic-gate	if ($? != 0) {
742*0Sstevel@tonic-gate		emsg("%s", utilityfailed("symcheck"));
743*0Sstevel@tonic-gate		clean_up_exit(1);
744*0Sstevel@tonic-gate	}
745*0Sstevel@tonic-gate}
746*0Sstevel@tonic-gate
747*0Sstevel@tonic-gate#
748*0Sstevel@tonic-gate# Sub to run "symreport".
749*0Sstevel@tonic-gate#
750*0Sstevel@tonic-gatesub run_report_generator
751*0Sstevel@tonic-gate{
752*0Sstevel@tonic-gate	system("$appcert_lib_dir/symreport");
753*0Sstevel@tonic-gate	if ($? != 0) {
754*0Sstevel@tonic-gate		emsg("%s", utilityfailed("symreport"));
755*0Sstevel@tonic-gate		clean_up_exit(1);
756*0Sstevel@tonic-gate	}
757*0Sstevel@tonic-gate}
758*0Sstevel@tonic-gate
759*0Sstevel@tonic-gate#
760*0Sstevel@tonic-gate# General routine to be called if one of our utility programs (symprof,
761*0Sstevel@tonic-gate# symcheck, symreport) failed (that is, return != 0).  returns the
762*0Sstevel@tonic-gate# formatted error message string to pass to the user.
763*0Sstevel@tonic-gate#
764*0Sstevel@tonic-gatesub utilityfailed
765*0Sstevel@tonic-gate{
766*0Sstevel@tonic-gate	my ($prog) = @_;
767*0Sstevel@tonic-gate	my $fmt;
768*0Sstevel@tonic-gate	$fmt = "\n *** " . gettext("utility program failed: %s\n");
769*0Sstevel@tonic-gate	return sprintf($fmt, $prog);
770*0Sstevel@tonic-gate}
771*0Sstevel@tonic-gate
772*0Sstevel@tonic-gate#
773*0Sstevel@tonic-gate# Does the cleanup and then exits with return code $rc.  The utility
774*0Sstevel@tonic-gate# subroutine exiter() will call this subroutine.  No general cleanup is
775*0Sstevel@tonic-gate# performed if exiting with error ($rc > 0) so that the user can examine
776*0Sstevel@tonic-gate# at the output files, etc.
777*0Sstevel@tonic-gate#
778*0Sstevel@tonic-gatesub clean_up_exit
779*0Sstevel@tonic-gate{
780*0Sstevel@tonic-gate	my ($rc) = @_;
781*0Sstevel@tonic-gate
782*0Sstevel@tonic-gate	if ($rc != 0) {
783*0Sstevel@tonic-gate		working_dir_msg();
784*0Sstevel@tonic-gate	} else {
785*0Sstevel@tonic-gate		clean_up();
786*0Sstevel@tonic-gate	}
787*0Sstevel@tonic-gate
788*0Sstevel@tonic-gate	exit $rc;
789*0Sstevel@tonic-gate}
790*0Sstevel@tonic-gate
791*0Sstevel@tonic-gate#
792*0Sstevel@tonic-gate# General cleanup routine.
793*0Sstevel@tonic-gate#
794*0Sstevel@tonic-gatesub clean_up
795*0Sstevel@tonic-gate{
796*0Sstevel@tonic-gate	if (-d $tmp_dir && ($tmp_dir !~ m,^/+$,)) {
797*0Sstevel@tonic-gate		rmdir($tmp_dir);
798*0Sstevel@tonic-gate	}
799*0Sstevel@tonic-gate}
800*0Sstevel@tonic-gate
801*0Sstevel@tonic-gate#
802*0Sstevel@tonic-gate# Routine that is called when an error has occurred.  It indicates to
803*0Sstevel@tonic-gate# user where the working and/or temporary directory is and that they are
804*0Sstevel@tonic-gate# not being removed.
805*0Sstevel@tonic-gate#
806*0Sstevel@tonic-gatesub working_dir_msg
807*0Sstevel@tonic-gate{
808*0Sstevel@tonic-gate
809*0Sstevel@tonic-gate	my @dirlist;
810*0Sstevel@tonic-gate	emsg("\n");
811*0Sstevel@tonic-gate	if (defined($working_dir) && -d $working_dir) {
812*0Sstevel@tonic-gate		push(@dirlist, $working_dir);
813*0Sstevel@tonic-gate	}
814*0Sstevel@tonic-gate	if (defined($tmp_dir) && -d $tmp_dir) {
815*0Sstevel@tonic-gate		push(@dirlist, $tmp_dir);
816*0Sstevel@tonic-gate	}
817*0Sstevel@tonic-gate
818*0Sstevel@tonic-gate	return if (! @dirlist);
819*0Sstevel@tonic-gate
820*0Sstevel@tonic-gate	emsg(gettext(
821*0Sstevel@tonic-gate	    "Note that the temporary working directories still exist:") .
822*0Sstevel@tonic-gate	    "\n\n");
823*0Sstevel@tonic-gate
824*0Sstevel@tonic-gate	my $dir;
825*0Sstevel@tonic-gate	# show the user explicitly which directories remains:
826*0Sstevel@tonic-gate	foreach $dir (@dirlist) {
827*0Sstevel@tonic-gate		system($cmd_ls, '-ld', $dir);
828*0Sstevel@tonic-gate	}
829*0Sstevel@tonic-gate
830*0Sstevel@tonic-gate	emsg("\n");
831*0Sstevel@tonic-gate}
832*0Sstevel@tonic-gate
833*0Sstevel@tonic-gate#
834*0Sstevel@tonic-gate# Signal handler for interruptions (E.g. Ctrl-C SIGINT).
835*0Sstevel@tonic-gate#
836*0Sstevel@tonic-gatesub interrupted
837*0Sstevel@tonic-gate{
838*0Sstevel@tonic-gate	$SIG{$_[0]} = 'IGNORE';
839*0Sstevel@tonic-gate
840*0Sstevel@tonic-gate	exit 1 if ($caught_signal);
841*0Sstevel@tonic-gate	$caught_signal = 1;
842*0Sstevel@tonic-gate
843*0Sstevel@tonic-gate	signals('off');
844*0Sstevel@tonic-gate	emsg("\n** " . gettext("interrupted") . " **\n");
845*0Sstevel@tonic-gate
846*0Sstevel@tonic-gate	clean_up_exit(1);
847*0Sstevel@tonic-gate}
848