xref: /titanic_44/usr/src/cmd/bart/create.c (revision 9c84d166d34399061330ff651f11e12622bc0700)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5c40f76e3Sjc144527  * Common Development and Distribution License (the "License").
6c40f76e3Sjc144527  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
225a5eeccaSmarks  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #include <signal.h>
287c478bd9Sstevel@tonic-gate #include <unistd.h>
297c478bd9Sstevel@tonic-gate #include <sys/acl.h>
307c478bd9Sstevel@tonic-gate #include <sys/statvfs.h>
317c478bd9Sstevel@tonic-gate #include <sys/wait.h>
327c478bd9Sstevel@tonic-gate #include "bart.h"
33fa9e4066Sahrens #include <aclutils.h>
347c478bd9Sstevel@tonic-gate 
357c478bd9Sstevel@tonic-gate static int	sanitize_reloc_root(char *root, size_t bufsize);
367c478bd9Sstevel@tonic-gate static int	create_manifest_filelist(char **argv, char *reloc_root);
377c478bd9Sstevel@tonic-gate static int	create_manifest_rule(char *reloc_root, FILE *rule_fp);
387c478bd9Sstevel@tonic-gate static void	output_manifest(void);
397c478bd9Sstevel@tonic-gate static int	eval_file(const char *fname, const struct stat64 *statb);
407c478bd9Sstevel@tonic-gate static char	*sanitized_fname(const char *, boolean_t);
417c478bd9Sstevel@tonic-gate static char	*get_acl_string(const char *fname, const struct stat64 *statb,
427c478bd9Sstevel@tonic-gate     int *err_code);
437c478bd9Sstevel@tonic-gate static int	generate_hash(int fdin, char *hash_str);
447c478bd9Sstevel@tonic-gate static int	read_filelist(char *reloc_root, char **argv, char *buf,
457c478bd9Sstevel@tonic-gate     size_t bufsize);
467c478bd9Sstevel@tonic-gate static int	walker(const char *name, const struct stat64 *sp,
477c478bd9Sstevel@tonic-gate     int type, struct FTW *ftwx);
487c478bd9Sstevel@tonic-gate 
497c478bd9Sstevel@tonic-gate /*
507c478bd9Sstevel@tonic-gate  * The following globals are necessary due to the "walker" function
517c478bd9Sstevel@tonic-gate  * provided by nftw().  Since there is no way to pass them through to the
527c478bd9Sstevel@tonic-gate  * walker function, they must be global.
537c478bd9Sstevel@tonic-gate  */
547c478bd9Sstevel@tonic-gate static int		compute_chksum = 1, eval_err = 0;
557c478bd9Sstevel@tonic-gate static struct rule	*subtree_root;
567c478bd9Sstevel@tonic-gate static char		reloc_root[PATH_MAX];
577c478bd9Sstevel@tonic-gate static struct statvfs	parent_vfs;
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate int
607c478bd9Sstevel@tonic-gate bart_create(int argc, char **argv)
617c478bd9Sstevel@tonic-gate {
627c478bd9Sstevel@tonic-gate 	boolean_t	filelist_input;
637c478bd9Sstevel@tonic-gate 	int		ret, c, output_pipe[2];
647c478bd9Sstevel@tonic-gate 	FILE 		*rules_fd = NULL;
657c478bd9Sstevel@tonic-gate 	pid_t		pid;
667c478bd9Sstevel@tonic-gate 
677c478bd9Sstevel@tonic-gate 	filelist_input = B_FALSE;
687c478bd9Sstevel@tonic-gate 	reloc_root[0] = '\0';
697c478bd9Sstevel@tonic-gate 
707c478bd9Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "Inr:R:")) != EOF) {
717c478bd9Sstevel@tonic-gate 		switch (c) {
727c478bd9Sstevel@tonic-gate 		case 'I':
737c478bd9Sstevel@tonic-gate 			if (rules_fd != NULL) {
747c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, "%s", INPUT_ERR);
757c478bd9Sstevel@tonic-gate 				usage();
767c478bd9Sstevel@tonic-gate 			}
777c478bd9Sstevel@tonic-gate 			filelist_input = B_TRUE;
787c478bd9Sstevel@tonic-gate 			break;
797c478bd9Sstevel@tonic-gate 
807c478bd9Sstevel@tonic-gate 		case 'n':
817c478bd9Sstevel@tonic-gate 			compute_chksum = 0;
827c478bd9Sstevel@tonic-gate 			break;
837c478bd9Sstevel@tonic-gate 
847c478bd9Sstevel@tonic-gate 		case 'r':
857c478bd9Sstevel@tonic-gate 			if (strcmp(optarg, "-") == 0)
867c478bd9Sstevel@tonic-gate 				rules_fd = stdin;
877c478bd9Sstevel@tonic-gate 			else
887c478bd9Sstevel@tonic-gate 				rules_fd = fopen(optarg, "r");
897c478bd9Sstevel@tonic-gate 			if (rules_fd == NULL) {
907c478bd9Sstevel@tonic-gate 				perror(optarg);
917c478bd9Sstevel@tonic-gate 				usage();
927c478bd9Sstevel@tonic-gate 			}
937c478bd9Sstevel@tonic-gate 			break;
947c478bd9Sstevel@tonic-gate 
957c478bd9Sstevel@tonic-gate 		case 'R':
967c478bd9Sstevel@tonic-gate 			(void) strlcpy(reloc_root, optarg, sizeof (reloc_root));
977c478bd9Sstevel@tonic-gate 			ret = sanitize_reloc_root(reloc_root,
987c478bd9Sstevel@tonic-gate 			    sizeof (reloc_root));
997c478bd9Sstevel@tonic-gate 			if (ret == 0)
1007c478bd9Sstevel@tonic-gate 				usage();
1017c478bd9Sstevel@tonic-gate 			break;
1027c478bd9Sstevel@tonic-gate 
1037c478bd9Sstevel@tonic-gate 		case '?':
1047c478bd9Sstevel@tonic-gate 		default :
1057c478bd9Sstevel@tonic-gate 			usage();
1067c478bd9Sstevel@tonic-gate 		}
1077c478bd9Sstevel@tonic-gate 	}
1087c478bd9Sstevel@tonic-gate 	argv += optind;
1097c478bd9Sstevel@tonic-gate 
1107c478bd9Sstevel@tonic-gate 	if (pipe(output_pipe) < 0) {
1117c478bd9Sstevel@tonic-gate 		perror("");
1127c478bd9Sstevel@tonic-gate 		exit(FATAL_EXIT);
1137c478bd9Sstevel@tonic-gate 	}
1147c478bd9Sstevel@tonic-gate 
1157c478bd9Sstevel@tonic-gate 	pid = fork();
1167c478bd9Sstevel@tonic-gate 	if (pid < 0) {
1177c478bd9Sstevel@tonic-gate 		perror(NULL);
1187c478bd9Sstevel@tonic-gate 		exit(FATAL_EXIT);
1197c478bd9Sstevel@tonic-gate 	}
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate 	/*
1227c478bd9Sstevel@tonic-gate 	 * Break the creation of a manifest into two parts: the parent process
1237c478bd9Sstevel@tonic-gate 	 * generated the data whereas the child process sorts the data.
1247c478bd9Sstevel@tonic-gate 	 *
1257c478bd9Sstevel@tonic-gate 	 * The processes communicate through the pipe.
1267c478bd9Sstevel@tonic-gate 	 */
1277c478bd9Sstevel@tonic-gate 	if (pid > 0) {
1287c478bd9Sstevel@tonic-gate 		/*
1297c478bd9Sstevel@tonic-gate 		 * Redirect the stdout of this process so it goes into
1307c478bd9Sstevel@tonic-gate 		 * output_pipe[0].  The output of this process will be read
1317c478bd9Sstevel@tonic-gate 		 * by the child, which will sort the output.
1327c478bd9Sstevel@tonic-gate 		 */
1337c478bd9Sstevel@tonic-gate 		if (dup2(output_pipe[0], STDOUT_FILENO) != STDOUT_FILENO) {
1347c478bd9Sstevel@tonic-gate 			perror(NULL);
1357c478bd9Sstevel@tonic-gate 			exit(FATAL_EXIT);
1367c478bd9Sstevel@tonic-gate 		}
1377c478bd9Sstevel@tonic-gate 		(void) close(output_pipe[0]);
1387c478bd9Sstevel@tonic-gate 		(void) close(output_pipe[1]);
1397c478bd9Sstevel@tonic-gate 
1407c478bd9Sstevel@tonic-gate 		if (filelist_input == B_TRUE) {
1417c478bd9Sstevel@tonic-gate 			ret = create_manifest_filelist(argv, reloc_root);
1427c478bd9Sstevel@tonic-gate 		} else {
1437c478bd9Sstevel@tonic-gate 			ret = create_manifest_rule(reloc_root, rules_fd);
1447c478bd9Sstevel@tonic-gate 		}
1457c478bd9Sstevel@tonic-gate 
1467c478bd9Sstevel@tonic-gate 		/* Close stdout so the sort in the child proc will complete */
1477c478bd9Sstevel@tonic-gate 		(void) fclose(stdout);
1487c478bd9Sstevel@tonic-gate 	} else {
1497c478bd9Sstevel@tonic-gate 		/*
1507c478bd9Sstevel@tonic-gate 		 * Redirect the stdin of this process so its read in from
1517c478bd9Sstevel@tonic-gate 		 * the pipe, which is the parent process in this case.
1527c478bd9Sstevel@tonic-gate 		 */
1537c478bd9Sstevel@tonic-gate 		if (dup2(output_pipe[1], STDIN_FILENO) != STDIN_FILENO) {
1547c478bd9Sstevel@tonic-gate 			perror(NULL);
1557c478bd9Sstevel@tonic-gate 			exit(FATAL_EXIT);
1567c478bd9Sstevel@tonic-gate 		}
1577c478bd9Sstevel@tonic-gate 		(void) close(output_pipe[0]);
1587c478bd9Sstevel@tonic-gate 
1597c478bd9Sstevel@tonic-gate 		output_manifest();
1607c478bd9Sstevel@tonic-gate 	}
1617c478bd9Sstevel@tonic-gate 
1627c478bd9Sstevel@tonic-gate 	/* Wait for the child proc (the sort) to complete */
1637c478bd9Sstevel@tonic-gate 	(void) wait(0);
1647c478bd9Sstevel@tonic-gate 
1657c478bd9Sstevel@tonic-gate 	return (ret);
1667c478bd9Sstevel@tonic-gate }
1677c478bd9Sstevel@tonic-gate 
1687c478bd9Sstevel@tonic-gate /*
1697c478bd9Sstevel@tonic-gate  * Handle the -R option and sets 'root' to be the absolute path of the
1707c478bd9Sstevel@tonic-gate  * relocatable root.  This is useful when the user specifies '-R ../../foo'.
1717c478bd9Sstevel@tonic-gate  *
1727c478bd9Sstevel@tonic-gate  * Return code is whether or not the location spec'd by the -R flag is a
1737c478bd9Sstevel@tonic-gate  * directory or not.
1747c478bd9Sstevel@tonic-gate  */
1757c478bd9Sstevel@tonic-gate static int
1767c478bd9Sstevel@tonic-gate sanitize_reloc_root(char *root, size_t bufsize)
1777c478bd9Sstevel@tonic-gate {
1787c478bd9Sstevel@tonic-gate 	char		pwd[PATH_MAX];
1797c478bd9Sstevel@tonic-gate 
1807c478bd9Sstevel@tonic-gate 	/*
1817c478bd9Sstevel@tonic-gate 	 * First, save the current directory and go to the location
1827c478bd9Sstevel@tonic-gate 	 * specified with the -R option.
1837c478bd9Sstevel@tonic-gate 	 */
1847c478bd9Sstevel@tonic-gate 	(void) getcwd(pwd, sizeof (pwd));
1857c478bd9Sstevel@tonic-gate 	if (chdir(root) < 0) {
1867c478bd9Sstevel@tonic-gate 		/* Failed to change directory, something is wrong.... */
1877c478bd9Sstevel@tonic-gate 		perror(root);
1887c478bd9Sstevel@tonic-gate 		return (0);
1897c478bd9Sstevel@tonic-gate 	}
1907c478bd9Sstevel@tonic-gate 
1917c478bd9Sstevel@tonic-gate 	/*
1927c478bd9Sstevel@tonic-gate 	 * Save the absolute path of the relocatable root directory.
1937c478bd9Sstevel@tonic-gate 	 */
1947c478bd9Sstevel@tonic-gate 	(void) getcwd(root, bufsize);
1957c478bd9Sstevel@tonic-gate 
1967c478bd9Sstevel@tonic-gate 	/*
1977c478bd9Sstevel@tonic-gate 	 * Now, go back to where we started, necessary for picking up a rules
1987c478bd9Sstevel@tonic-gate 	 * file.
1997c478bd9Sstevel@tonic-gate 	 */
2007c478bd9Sstevel@tonic-gate 	if (chdir(pwd) < 0) {
2017c478bd9Sstevel@tonic-gate 		/* Failed to change directory, something is wrong.... */
2027c478bd9Sstevel@tonic-gate 		perror(root);
2037c478bd9Sstevel@tonic-gate 		return (0);
2047c478bd9Sstevel@tonic-gate 	}
2057c478bd9Sstevel@tonic-gate 
2067c478bd9Sstevel@tonic-gate 	/*
2077c478bd9Sstevel@tonic-gate 	 * Make sure the path returned does not have a trailing /. This
2087c478bd9Sstevel@tonic-gate 	 * can only happen when the entire pathname is "/".
2097c478bd9Sstevel@tonic-gate 	 */
2107c478bd9Sstevel@tonic-gate 	if (strcmp(root, "/") == 0)
2117c478bd9Sstevel@tonic-gate 		root[0] = '\0';
2127c478bd9Sstevel@tonic-gate 
2137c478bd9Sstevel@tonic-gate 	/*
2147c478bd9Sstevel@tonic-gate 	 * Since the earlier chdir() succeeded, return success.
2157c478bd9Sstevel@tonic-gate 	 */
2167c478bd9Sstevel@tonic-gate 	return (1);
2177c478bd9Sstevel@tonic-gate }
2187c478bd9Sstevel@tonic-gate 
2197c478bd9Sstevel@tonic-gate /*
2207c478bd9Sstevel@tonic-gate  * This is the worker bee which creates the manifest based upon the command
2217c478bd9Sstevel@tonic-gate  * line options supplied by the user.
2227c478bd9Sstevel@tonic-gate  *
2237c478bd9Sstevel@tonic-gate  * NOTE: create_manifest() eventually outputs data to a pipe, which is read in
2247c478bd9Sstevel@tonic-gate  * by the child process.  The child process is running output_manifest(), which
2257c478bd9Sstevel@tonic-gate  * is responsible for generating sorted output.
2267c478bd9Sstevel@tonic-gate  */
2277c478bd9Sstevel@tonic-gate static int
2287c478bd9Sstevel@tonic-gate create_manifest_rule(char *reloc_root, FILE *rule_fp)
2297c478bd9Sstevel@tonic-gate {
2307c478bd9Sstevel@tonic-gate 	struct rule	*root;
2317c478bd9Sstevel@tonic-gate 	int		ret_status = EXIT;
2327c478bd9Sstevel@tonic-gate 	uint_t		flags;
2337c478bd9Sstevel@tonic-gate 
2347c478bd9Sstevel@tonic-gate 	if (compute_chksum)
2357c478bd9Sstevel@tonic-gate 		flags = ATTR_CONTENTS;
2367c478bd9Sstevel@tonic-gate 	else
2377c478bd9Sstevel@tonic-gate 		flags = 0;
2387c478bd9Sstevel@tonic-gate 	ret_status = read_rules(rule_fp, reloc_root, flags, 1);
2397c478bd9Sstevel@tonic-gate 
2407c478bd9Sstevel@tonic-gate 	/* Loop through every single subtree */
2417c478bd9Sstevel@tonic-gate 	for (root = get_first_subtree(); root != NULL;
2427c478bd9Sstevel@tonic-gate 	    root = get_next_subtree(root)) {
2437c478bd9Sstevel@tonic-gate 
2447c478bd9Sstevel@tonic-gate 		/*
2457c478bd9Sstevel@tonic-gate 		 * Check to see if this subtree should have contents
2467c478bd9Sstevel@tonic-gate 		 * checking turned on or off.
2477c478bd9Sstevel@tonic-gate 		 *
2487c478bd9Sstevel@tonic-gate 		 * NOTE: The 'compute_chksum' and 'parent_vfs'
2497c478bd9Sstevel@tonic-gate 		 * are a necessary hack: the variables are used in
2507c478bd9Sstevel@tonic-gate 		 * walker(), both directly and indirectly.  Since
2517c478bd9Sstevel@tonic-gate 		 * the parameters to walker() are defined by nftw(),
2527c478bd9Sstevel@tonic-gate 		 * the globals are really a backdoor mechanism.
2537c478bd9Sstevel@tonic-gate 		 */
2547c478bd9Sstevel@tonic-gate 		ret_status = statvfs(root->subtree, &parent_vfs);
2557c478bd9Sstevel@tonic-gate 		if (ret_status < 0) {
2567c478bd9Sstevel@tonic-gate 			perror(root->subtree);
2577c478bd9Sstevel@tonic-gate 			continue;
2587c478bd9Sstevel@tonic-gate 		}
2597c478bd9Sstevel@tonic-gate 
2607c478bd9Sstevel@tonic-gate 		/*
2617c478bd9Sstevel@tonic-gate 		 * Walk the subtree and invoke the callback function
2627c478bd9Sstevel@tonic-gate 		 * walker()
2637c478bd9Sstevel@tonic-gate 		 */
2647c478bd9Sstevel@tonic-gate 		subtree_root = root;
2657c478bd9Sstevel@tonic-gate 		(void) nftw64(root->subtree, &walker, 20, FTW_PHYS);
2667c478bd9Sstevel@tonic-gate 
2677c478bd9Sstevel@tonic-gate 		/*
2687c478bd9Sstevel@tonic-gate 		 * Ugly but necessary:
2697c478bd9Sstevel@tonic-gate 		 *
2707c478bd9Sstevel@tonic-gate 		 * walker() must return 0, or the tree walk will stop,
2717c478bd9Sstevel@tonic-gate 		 * so warning flags must be set through a global.
2727c478bd9Sstevel@tonic-gate 		 */
2737c478bd9Sstevel@tonic-gate 		if (eval_err == WARNING_EXIT)
2747c478bd9Sstevel@tonic-gate 			ret_status = WARNING_EXIT;
2757c478bd9Sstevel@tonic-gate 
2767c478bd9Sstevel@tonic-gate 	}
2777c478bd9Sstevel@tonic-gate 	return (ret_status);
2787c478bd9Sstevel@tonic-gate }
2797c478bd9Sstevel@tonic-gate 
2807c478bd9Sstevel@tonic-gate static int
2817c478bd9Sstevel@tonic-gate create_manifest_filelist(char **argv, char *reloc_root)
2827c478bd9Sstevel@tonic-gate {
2837c478bd9Sstevel@tonic-gate 	int	ret_status = EXIT;
2847c478bd9Sstevel@tonic-gate 	char	input_fname[PATH_MAX];
2857c478bd9Sstevel@tonic-gate 
2867c478bd9Sstevel@tonic-gate 	while (read_filelist(reloc_root, argv,
2877c478bd9Sstevel@tonic-gate 	    input_fname, sizeof (input_fname)) != -1) {
2887c478bd9Sstevel@tonic-gate 
2897c478bd9Sstevel@tonic-gate 		struct stat64	stat_buf;
2907c478bd9Sstevel@tonic-gate 		int		ret;
2917c478bd9Sstevel@tonic-gate 
2927c478bd9Sstevel@tonic-gate 		ret = lstat64(input_fname, &stat_buf);
2937c478bd9Sstevel@tonic-gate 		if (ret < 0) {
2947c478bd9Sstevel@tonic-gate 			ret_status = WARNING_EXIT;
2957c478bd9Sstevel@tonic-gate 			perror(input_fname);
2967c478bd9Sstevel@tonic-gate 		} else {
2977c478bd9Sstevel@tonic-gate 			ret = eval_file(input_fname, &stat_buf);
2987c478bd9Sstevel@tonic-gate 
2997c478bd9Sstevel@tonic-gate 			if (ret == WARNING_EXIT)
3007c478bd9Sstevel@tonic-gate 				ret_status = WARNING_EXIT;
3017c478bd9Sstevel@tonic-gate 		}
3027c478bd9Sstevel@tonic-gate 	}
3037c478bd9Sstevel@tonic-gate 
3047c478bd9Sstevel@tonic-gate 	return (ret_status);
3057c478bd9Sstevel@tonic-gate }
3067c478bd9Sstevel@tonic-gate 
3077c478bd9Sstevel@tonic-gate /*
3087c478bd9Sstevel@tonic-gate  * output_manifest() the child process.  It reads in the output from
3097c478bd9Sstevel@tonic-gate  * create_manifest() and sorts it.
3107c478bd9Sstevel@tonic-gate  */
3117c478bd9Sstevel@tonic-gate static void
3127c478bd9Sstevel@tonic-gate output_manifest(void)
3137c478bd9Sstevel@tonic-gate {
3147c478bd9Sstevel@tonic-gate 	char	*env[] = {"LC_CTYPE=C", "LC_COLLATE=C", "LC_NUMERIC=C", NULL};
3157c478bd9Sstevel@tonic-gate 	time_t		time_val;
3167c478bd9Sstevel@tonic-gate 	struct tm	*tm;
3177c478bd9Sstevel@tonic-gate 	char		time_buf[1024];
3187c478bd9Sstevel@tonic-gate 
3197c478bd9Sstevel@tonic-gate 	(void) printf("%s", MANIFEST_VER);
3207c478bd9Sstevel@tonic-gate 	time_val = time((time_t)0);
3217c478bd9Sstevel@tonic-gate 	tm = localtime(&time_val);
3227c478bd9Sstevel@tonic-gate 	(void) strftime(time_buf, sizeof (time_buf), "%A, %B %d, %Y (%T)", tm);
3237c478bd9Sstevel@tonic-gate 	(void) printf("! %s\n", time_buf);
3247c478bd9Sstevel@tonic-gate 	(void) printf("%s", FORMAT_STR);
3257c478bd9Sstevel@tonic-gate 	(void) fflush(stdout);
3267c478bd9Sstevel@tonic-gate 	/*
3277c478bd9Sstevel@tonic-gate 	 * Simply run sort and read from the the current stdin, which is really
3287c478bd9Sstevel@tonic-gate 	 * the output of create_manifest().
3297c478bd9Sstevel@tonic-gate 	 * Also, make sure the output is unique, since a given file may be
3307c478bd9Sstevel@tonic-gate 	 * included by several stanzas.
3317c478bd9Sstevel@tonic-gate 	 */
332*9c84d166Srm88369 	if (execle("/usr/bin/sort", "sort", "-u", NULL, env) < 0) {
3337c478bd9Sstevel@tonic-gate 		perror("");
3347c478bd9Sstevel@tonic-gate 		exit(FATAL_EXIT);
3357c478bd9Sstevel@tonic-gate 	}
3367c478bd9Sstevel@tonic-gate 
3377c478bd9Sstevel@tonic-gate 	/*NOTREACHED*/
3387c478bd9Sstevel@tonic-gate }
3397c478bd9Sstevel@tonic-gate 
3407c478bd9Sstevel@tonic-gate /*
3417c478bd9Sstevel@tonic-gate  * Callback function for nftw()
3427c478bd9Sstevel@tonic-gate  */
3437c478bd9Sstevel@tonic-gate static int
3447c478bd9Sstevel@tonic-gate walker(const char *name, const struct stat64 *sp, int type, struct FTW *ftwx)
3457c478bd9Sstevel@tonic-gate {
3467c478bd9Sstevel@tonic-gate 	int		ret;
3477c478bd9Sstevel@tonic-gate 	struct statvfs	path_vfs;
3487c478bd9Sstevel@tonic-gate 	boolean_t	dir_flag = B_FALSE;
3497c478bd9Sstevel@tonic-gate 	struct rule	*rule;
3507c478bd9Sstevel@tonic-gate 
3517c478bd9Sstevel@tonic-gate 	switch (type) {
3527c478bd9Sstevel@tonic-gate 	case FTW_F:	/* file 		*/
3537c478bd9Sstevel@tonic-gate 		rule = check_rules(name, 'F');
3547c478bd9Sstevel@tonic-gate 		if (rule != NULL) {
3557c478bd9Sstevel@tonic-gate 			if (rule->attr_list & ATTR_CONTENTS)
3567c478bd9Sstevel@tonic-gate 				compute_chksum = 1;
3577c478bd9Sstevel@tonic-gate 			else
3587c478bd9Sstevel@tonic-gate 				compute_chksum = 0;
3597c478bd9Sstevel@tonic-gate 		}
3607c478bd9Sstevel@tonic-gate 		break;
3617c478bd9Sstevel@tonic-gate 	case FTW_SL:	/* symbolic link	*/
3627c478bd9Sstevel@tonic-gate 	case FTW_DP:	/* end of directory	*/
3637c478bd9Sstevel@tonic-gate 	case FTW_DNR:	/* unreadable directory	*/
3647c478bd9Sstevel@tonic-gate 	case FTW_NS:	/* unstatable file	*/
3657c478bd9Sstevel@tonic-gate 		break;
3667c478bd9Sstevel@tonic-gate 	case FTW_D:	/* enter directory 		*/
3677c478bd9Sstevel@tonic-gate 		dir_flag = B_TRUE;
3687c478bd9Sstevel@tonic-gate 		ret = statvfs(name, &path_vfs);
3697c478bd9Sstevel@tonic-gate 		if (ret < 0)
3707c478bd9Sstevel@tonic-gate 			eval_err = WARNING_EXIT;
3717c478bd9Sstevel@tonic-gate 		break;
3727c478bd9Sstevel@tonic-gate 	default:
3737c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, INVALID_FILE, name);
3747c478bd9Sstevel@tonic-gate 		eval_err = WARNING_EXIT;
3757c478bd9Sstevel@tonic-gate 		break;
3767c478bd9Sstevel@tonic-gate 	}
3777c478bd9Sstevel@tonic-gate 
3787c478bd9Sstevel@tonic-gate 	/* This is the function which really processes the file */
3797c478bd9Sstevel@tonic-gate 	ret = eval_file(name, sp);
3807c478bd9Sstevel@tonic-gate 
3817c478bd9Sstevel@tonic-gate 	/*
3827c478bd9Sstevel@tonic-gate 	 * Since the parameters to walker() are constrained by nftw(),
3837c478bd9Sstevel@tonic-gate 	 * need to use a global to reflect a WARNING.  Sigh.
3847c478bd9Sstevel@tonic-gate 	 */
3857c478bd9Sstevel@tonic-gate 	if (ret == WARNING_EXIT)
3867c478bd9Sstevel@tonic-gate 		eval_err = WARNING_EXIT;
3877c478bd9Sstevel@tonic-gate 
3887c478bd9Sstevel@tonic-gate 	/*
3897c478bd9Sstevel@tonic-gate 	 * This is a case of a directory which crosses into a mounted
3907c478bd9Sstevel@tonic-gate 	 * filesystem of a different type, e.g., UFS -> NFS.
3917c478bd9Sstevel@tonic-gate 	 * BART should not walk the new filesystem (by specification), so
3927c478bd9Sstevel@tonic-gate 	 * set this consolidation-private flag so the rest of the subtree
3937c478bd9Sstevel@tonic-gate 	 * under this directory is not waled.
3947c478bd9Sstevel@tonic-gate 	 */
3957c478bd9Sstevel@tonic-gate 	if (dir_flag &&
3967c478bd9Sstevel@tonic-gate 	    (strcmp(parent_vfs.f_basetype, path_vfs.f_basetype) != 0))
3977c478bd9Sstevel@tonic-gate 		ftwx->quit = FTW_PRUNE;
3987c478bd9Sstevel@tonic-gate 
3997c478bd9Sstevel@tonic-gate 	return (0);
4007c478bd9Sstevel@tonic-gate }
4017c478bd9Sstevel@tonic-gate 
4027c478bd9Sstevel@tonic-gate /*
4037c478bd9Sstevel@tonic-gate  * This file does the per-file evaluation and is run to generate every entry
4047c478bd9Sstevel@tonic-gate  * in the manifest.
4057c478bd9Sstevel@tonic-gate  *
4067c478bd9Sstevel@tonic-gate  * All output is written to a pipe which is read by the child process,
4077c478bd9Sstevel@tonic-gate  * which is running output_manifest().
4087c478bd9Sstevel@tonic-gate  */
4097c478bd9Sstevel@tonic-gate static int
4107c478bd9Sstevel@tonic-gate eval_file(const char *fname, const struct stat64 *statb)
4117c478bd9Sstevel@tonic-gate {
4127c478bd9Sstevel@tonic-gate 	int	fd, ret, err_code, i;
4137c478bd9Sstevel@tonic-gate 	char	last_field[PATH_MAX], ftype, *acl_str,
4147c478bd9Sstevel@tonic-gate 		*quoted_name;
4157c478bd9Sstevel@tonic-gate 
4167c478bd9Sstevel@tonic-gate 	err_code = EXIT;
4177c478bd9Sstevel@tonic-gate 
4187c478bd9Sstevel@tonic-gate 	switch (statb->st_mode & S_IFMT) {
4197c478bd9Sstevel@tonic-gate 	/* Regular file */
4207c478bd9Sstevel@tonic-gate 	case S_IFREG: ftype = 'F'; break;
4217c478bd9Sstevel@tonic-gate 
4227c478bd9Sstevel@tonic-gate 	/* Directory */
4237c478bd9Sstevel@tonic-gate 	case S_IFDIR: ftype = 'D'; break;
4247c478bd9Sstevel@tonic-gate 
4257c478bd9Sstevel@tonic-gate 	/* Block Device */
4267c478bd9Sstevel@tonic-gate 	case S_IFBLK: ftype = 'B'; break;
4277c478bd9Sstevel@tonic-gate 
4287c478bd9Sstevel@tonic-gate 	/* Character Device */
4297c478bd9Sstevel@tonic-gate 	case S_IFCHR: ftype = 'C'; break;
4307c478bd9Sstevel@tonic-gate 
4317c478bd9Sstevel@tonic-gate 	/* Named Pipe */
4327c478bd9Sstevel@tonic-gate 	case S_IFIFO: ftype = 'P'; break;
4337c478bd9Sstevel@tonic-gate 
4347c478bd9Sstevel@tonic-gate 	/* Socket */
4357c478bd9Sstevel@tonic-gate 	case S_IFSOCK: ftype = 'S'; break;
4367c478bd9Sstevel@tonic-gate 
4377c478bd9Sstevel@tonic-gate 	/* Door */
4387c478bd9Sstevel@tonic-gate 	case S_IFDOOR: ftype = 'O'; break;
4397c478bd9Sstevel@tonic-gate 
4407c478bd9Sstevel@tonic-gate 	/* Symbolic link */
4417c478bd9Sstevel@tonic-gate 	case S_IFLNK: ftype = 'L'; break;
4427c478bd9Sstevel@tonic-gate 
4437c478bd9Sstevel@tonic-gate 	default: ftype = '-'; break;
4447c478bd9Sstevel@tonic-gate 	}
4457c478bd9Sstevel@tonic-gate 
4467c478bd9Sstevel@tonic-gate 	/* First, make sure this file should be cataloged */
4477c478bd9Sstevel@tonic-gate 
4487c478bd9Sstevel@tonic-gate 	if ((subtree_root != NULL) &&
4497c478bd9Sstevel@tonic-gate 	    (exclude_fname(fname, ftype, subtree_root)))
4507c478bd9Sstevel@tonic-gate 		return (err_code);
4517c478bd9Sstevel@tonic-gate 
4527c478bd9Sstevel@tonic-gate 	for (i = 0; i < PATH_MAX; i++)
4537c478bd9Sstevel@tonic-gate 		last_field[i] = '\0';
4547c478bd9Sstevel@tonic-gate 
4557c478bd9Sstevel@tonic-gate 	/*
4567c478bd9Sstevel@tonic-gate 	 * Regular files, compute the MD5 checksum and put it into 'last_field'
4577c478bd9Sstevel@tonic-gate 	 * UNLESS instructed to ignore the checksums.
4587c478bd9Sstevel@tonic-gate 	 */
4597c478bd9Sstevel@tonic-gate 	if (ftype == 'F') {
4607c478bd9Sstevel@tonic-gate 		if (compute_chksum) {
4617c478bd9Sstevel@tonic-gate 			fd = open(fname, O_RDONLY|O_LARGEFILE);
4627c478bd9Sstevel@tonic-gate 			if (fd < 0) {
4637c478bd9Sstevel@tonic-gate 				err_code = WARNING_EXIT;
4647c478bd9Sstevel@tonic-gate 				perror(fname);
4657c478bd9Sstevel@tonic-gate 
4667c478bd9Sstevel@tonic-gate 				/* default value since the computution failed */
4677c478bd9Sstevel@tonic-gate 				(void) strcpy(last_field, "-");
4687c478bd9Sstevel@tonic-gate 			} else {
4697c478bd9Sstevel@tonic-gate 				if (generate_hash(fd, last_field) != 0) {
4707c478bd9Sstevel@tonic-gate 					err_code = WARNING_EXIT;
4717c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr, CONTENTS_WARN,
4727c478bd9Sstevel@tonic-gate 					    fname);
4737c478bd9Sstevel@tonic-gate 					(void) strcpy(last_field, "-");
4747c478bd9Sstevel@tonic-gate 				}
4757c478bd9Sstevel@tonic-gate 			}
4767c478bd9Sstevel@tonic-gate 			(void) close(fd);
4777c478bd9Sstevel@tonic-gate 		}
4787c478bd9Sstevel@tonic-gate 		/* Instructed to ignore checksums, just put in a '-' */
4797c478bd9Sstevel@tonic-gate 		else
4807c478bd9Sstevel@tonic-gate 			(void) strcpy(last_field, "-");
4817c478bd9Sstevel@tonic-gate 	}
4827c478bd9Sstevel@tonic-gate 
4837c478bd9Sstevel@tonic-gate 	/*
4847c478bd9Sstevel@tonic-gate 	 * For symbolic links, put the destination of the symbolic link into
4857c478bd9Sstevel@tonic-gate 	 * 'last_field'
4867c478bd9Sstevel@tonic-gate 	 */
4877c478bd9Sstevel@tonic-gate 	if (ftype == 'L') {
4887c478bd9Sstevel@tonic-gate 		ret = readlink(fname, last_field, sizeof (last_field));
4897c478bd9Sstevel@tonic-gate 		if (ret < 0) {
4907c478bd9Sstevel@tonic-gate 			err_code = WARNING_EXIT;
4917c478bd9Sstevel@tonic-gate 			perror(fname);
4927c478bd9Sstevel@tonic-gate 
4937c478bd9Sstevel@tonic-gate 			/* default value since the computation failed */
4947c478bd9Sstevel@tonic-gate 			(void) strcpy(last_field, "-");
4957c478bd9Sstevel@tonic-gate 		}
4967c478bd9Sstevel@tonic-gate 		else
4977c478bd9Sstevel@tonic-gate 			(void) strlcpy(last_field,
4987c478bd9Sstevel@tonic-gate 			    sanitized_fname(last_field, B_FALSE),
4997c478bd9Sstevel@tonic-gate 			    sizeof (last_field));
5007c478bd9Sstevel@tonic-gate 
5017c478bd9Sstevel@tonic-gate 		/*
5027c478bd9Sstevel@tonic-gate 		 * Boundary condition: possible for a symlink to point to
5037c478bd9Sstevel@tonic-gate 		 * nothing [ ln -s '' link_name ].  For this case, set the
5047c478bd9Sstevel@tonic-gate 		 * destination to "\000".
5057c478bd9Sstevel@tonic-gate 		 */
5067c478bd9Sstevel@tonic-gate 		if (strlen(last_field) == 0)
5077c478bd9Sstevel@tonic-gate 			(void) strcpy(last_field, "\\000");
5087c478bd9Sstevel@tonic-gate 	}
5097c478bd9Sstevel@tonic-gate 
5107c478bd9Sstevel@tonic-gate 	acl_str = get_acl_string(fname, statb, &err_code);
5117c478bd9Sstevel@tonic-gate 
5127c478bd9Sstevel@tonic-gate 	/* Sanitize 'fname', so its in the proper format for the manifest */
5137c478bd9Sstevel@tonic-gate 	quoted_name = sanitized_fname(fname, B_TRUE);
5147c478bd9Sstevel@tonic-gate 
5157c478bd9Sstevel@tonic-gate 	/* Start to build the entry.... */
5167c478bd9Sstevel@tonic-gate 	(void) printf("%s %c %d %o %s %x %d %d", quoted_name, ftype,
5177c478bd9Sstevel@tonic-gate 	    (int)statb->st_size, (int)statb->st_mode, acl_str,
5187c478bd9Sstevel@tonic-gate 	    (int)statb->st_mtime, (int)statb->st_uid, (int)statb->st_gid);
5197c478bd9Sstevel@tonic-gate 
5207c478bd9Sstevel@tonic-gate 	/* Finish it off based upon whether or not it's a device node */
521c40f76e3Sjc144527 	if ((ftype == 'B') || (ftype == 'C'))
5227c478bd9Sstevel@tonic-gate 		(void) printf(" %x\n", (int)statb->st_rdev);
5237c478bd9Sstevel@tonic-gate 	else if (strlen(last_field) > 0)
5247c478bd9Sstevel@tonic-gate 		(void) printf(" %s\n", last_field);
5257c478bd9Sstevel@tonic-gate 	else
5267c478bd9Sstevel@tonic-gate 		(void) printf("\n");
5277c478bd9Sstevel@tonic-gate 
5287c478bd9Sstevel@tonic-gate 	/* free the memory consumed */
5297c478bd9Sstevel@tonic-gate 	free(acl_str);
5307c478bd9Sstevel@tonic-gate 	free(quoted_name);
5317c478bd9Sstevel@tonic-gate 
5327c478bd9Sstevel@tonic-gate 	return (err_code);
5337c478bd9Sstevel@tonic-gate }
5347c478bd9Sstevel@tonic-gate 
5357c478bd9Sstevel@tonic-gate /*
5367c478bd9Sstevel@tonic-gate  * When creating a manifest, make sure all '?', tabs, space, newline, '/'
5377c478bd9Sstevel@tonic-gate  * and '[' are all properly quoted.  Convert them to a "\ooo" where the 'ooo'
5387c478bd9Sstevel@tonic-gate  * represents their octal value. For filesystem objects, as opposed to symlink
5397c478bd9Sstevel@tonic-gate  * targets, also canonicalize the pathname.
5407c478bd9Sstevel@tonic-gate  */
5417c478bd9Sstevel@tonic-gate static char *
5427c478bd9Sstevel@tonic-gate sanitized_fname(const char *fname, boolean_t canon_path)
5437c478bd9Sstevel@tonic-gate {
5447c478bd9Sstevel@tonic-gate 	const char *ip;
5457c478bd9Sstevel@tonic-gate 	unsigned char ch;
5467c478bd9Sstevel@tonic-gate 	char *op, *quoted_name;
5477c478bd9Sstevel@tonic-gate 
5487c478bd9Sstevel@tonic-gate 	/* Initialize everything */
5497c478bd9Sstevel@tonic-gate 	quoted_name = safe_calloc((4 * PATH_MAX) + 1);
5507c478bd9Sstevel@tonic-gate 	ip = fname;
5517c478bd9Sstevel@tonic-gate 	op = quoted_name;
5527c478bd9Sstevel@tonic-gate 
5537c478bd9Sstevel@tonic-gate 	if (canon_path) {
5547c478bd9Sstevel@tonic-gate 		/*
5557c478bd9Sstevel@tonic-gate 		 * In the case when a relocatable root was used, the relocatable
5567c478bd9Sstevel@tonic-gate 		 * root should *not* be part of the manifest.
5577c478bd9Sstevel@tonic-gate 		 */
5587c478bd9Sstevel@tonic-gate 		ip += strlen(reloc_root);
5597c478bd9Sstevel@tonic-gate 
5607c478bd9Sstevel@tonic-gate 		/*
5617c478bd9Sstevel@tonic-gate 		 * In the case when the '-I' option was used, make sure
5627c478bd9Sstevel@tonic-gate 		 * the quoted_name starts with a '/'.
5637c478bd9Sstevel@tonic-gate 		 */
5647c478bd9Sstevel@tonic-gate 		if (*ip != '/')
5657c478bd9Sstevel@tonic-gate 			*op++ = '/';
5667c478bd9Sstevel@tonic-gate 	}
5677c478bd9Sstevel@tonic-gate 
5687c478bd9Sstevel@tonic-gate 	/* Now walk through 'fname' and build the quoted string */
5697c478bd9Sstevel@tonic-gate 	while ((ch = *ip++) != 0) {
5707c478bd9Sstevel@tonic-gate 		switch (ch) {
5717c478bd9Sstevel@tonic-gate 		/* Quote the following characters */
5727c478bd9Sstevel@tonic-gate 		case ' ':
5737c478bd9Sstevel@tonic-gate 		case '*':
5747c478bd9Sstevel@tonic-gate 		case '\n':
5757c478bd9Sstevel@tonic-gate 		case '?':
5767c478bd9Sstevel@tonic-gate 		case '[':
5777c478bd9Sstevel@tonic-gate 		case '\\':
5787c478bd9Sstevel@tonic-gate 		case '\t':
5797c478bd9Sstevel@tonic-gate 			op += sprintf(op, "\\%.3o", (unsigned char)ch);
5807c478bd9Sstevel@tonic-gate 			break;
5817c478bd9Sstevel@tonic-gate 
5827c478bd9Sstevel@tonic-gate 		/* Otherwise, simply append them */
5837c478bd9Sstevel@tonic-gate 		default:
5847c478bd9Sstevel@tonic-gate 			*op++ = ch;
5857c478bd9Sstevel@tonic-gate 			break;
5867c478bd9Sstevel@tonic-gate 		}
5877c478bd9Sstevel@tonic-gate 	}
5887c478bd9Sstevel@tonic-gate 
5897c478bd9Sstevel@tonic-gate 	*op = 0;
5907c478bd9Sstevel@tonic-gate 
5917c478bd9Sstevel@tonic-gate 	return (quoted_name);
5927c478bd9Sstevel@tonic-gate }
5937c478bd9Sstevel@tonic-gate 
5947c478bd9Sstevel@tonic-gate /*
5957c478bd9Sstevel@tonic-gate  * Function responsible for generating the ACL information for a given
5967c478bd9Sstevel@tonic-gate  * file.  Note, the string is put into buffer malloc'd by this function.
5977c478bd9Sstevel@tonic-gate  * Its the responsibility of the caller to free the buffer.
5987c478bd9Sstevel@tonic-gate  */
5997c478bd9Sstevel@tonic-gate static char *
6007c478bd9Sstevel@tonic-gate get_acl_string(const char *fname, const struct stat64 *statb, int *err_code)
6017c478bd9Sstevel@tonic-gate {
602fa9e4066Sahrens 	acl_t		*aclp;
603fa9e4066Sahrens 	char		*acltext;
604fa9e4066Sahrens 	int		error;
6057c478bd9Sstevel@tonic-gate 
6067c478bd9Sstevel@tonic-gate 	if (S_ISLNK(statb->st_mode)) {
6077c478bd9Sstevel@tonic-gate 		return (safe_strdup("-"));
6087c478bd9Sstevel@tonic-gate 	}
6097c478bd9Sstevel@tonic-gate 
6107c478bd9Sstevel@tonic-gate 	/*
611fa9e4066Sahrens 	 *  Include trivial acl's
6127c478bd9Sstevel@tonic-gate 	 */
613fa9e4066Sahrens 	error = acl_get(fname, 0, &aclp);
614fa9e4066Sahrens 
615fa9e4066Sahrens 	if (error != 0) {
6167c478bd9Sstevel@tonic-gate 		*err_code = WARNING_EXIT;
617fa9e4066Sahrens 		(void) fprintf(stderr, "%s: %s\n", fname, acl_strerror(error));
6187c478bd9Sstevel@tonic-gate 		return (safe_strdup("-"));
619fa9e4066Sahrens 	} else {
6205a5eeccaSmarks 		acltext = acl_totext(aclp, 0);
621fa9e4066Sahrens 		acl_free(aclp);
622fa9e4066Sahrens 		return (acltext);
6237c478bd9Sstevel@tonic-gate 	}
6247c478bd9Sstevel@tonic-gate }
6257c478bd9Sstevel@tonic-gate 
6267c478bd9Sstevel@tonic-gate 
6277c478bd9Sstevel@tonic-gate /*
6287c478bd9Sstevel@tonic-gate  *
6297c478bd9Sstevel@tonic-gate  * description:	This routine reads stdin in BUF_SIZE chunks, uses the bits
6307c478bd9Sstevel@tonic-gate  *		to update the md5 hash buffer, and outputs the chunks
6317c478bd9Sstevel@tonic-gate  *		to stdout.  When stdin is exhausted, the hash is computed,
6327c478bd9Sstevel@tonic-gate  *		converted to a hexadecimal string, and returned.
6337c478bd9Sstevel@tonic-gate  *
6347c478bd9Sstevel@tonic-gate  * returns:	The md5 hash of stdin, or NULL if unsuccessful for any reason.
6357c478bd9Sstevel@tonic-gate  */
6367c478bd9Sstevel@tonic-gate static int
6377c478bd9Sstevel@tonic-gate generate_hash(int fdin, char *hash_str)
6387c478bd9Sstevel@tonic-gate {
6397c478bd9Sstevel@tonic-gate 	unsigned char buf[BUF_SIZE];
6407c478bd9Sstevel@tonic-gate 	unsigned char hash[MD5_DIGEST_LENGTH];
6417c478bd9Sstevel@tonic-gate 	int i, amtread;
6427c478bd9Sstevel@tonic-gate 	MD5_CTX ctx;
6437c478bd9Sstevel@tonic-gate 
6447c478bd9Sstevel@tonic-gate 	MD5Init(&ctx);
6457c478bd9Sstevel@tonic-gate 
6467c478bd9Sstevel@tonic-gate 	for (;;) {
6477c478bd9Sstevel@tonic-gate 		amtread = read(fdin, buf, sizeof (buf));
6487c478bd9Sstevel@tonic-gate 		if (amtread == 0)
6497c478bd9Sstevel@tonic-gate 			break;
6507c478bd9Sstevel@tonic-gate 		if (amtread <  0)
6517c478bd9Sstevel@tonic-gate 			return (1);
6527c478bd9Sstevel@tonic-gate 
6537c478bd9Sstevel@tonic-gate 		/* got some data.  Now update hash */
6547c478bd9Sstevel@tonic-gate 		MD5Update(&ctx, buf, amtread);
6557c478bd9Sstevel@tonic-gate 	}
6567c478bd9Sstevel@tonic-gate 
6577c478bd9Sstevel@tonic-gate 	/* done passing through data, calculate hash */
6587c478bd9Sstevel@tonic-gate 	MD5Final(hash, &ctx);
6597c478bd9Sstevel@tonic-gate 
6607c478bd9Sstevel@tonic-gate 	for (i = 0; i < MD5_DIGEST_LENGTH; i++)
6617c478bd9Sstevel@tonic-gate 		(void) sprintf(hash_str + (i*2), "%2.2x", hash[i]);
6627c478bd9Sstevel@tonic-gate 
6637c478bd9Sstevel@tonic-gate 	return (0);
6647c478bd9Sstevel@tonic-gate }
6657c478bd9Sstevel@tonic-gate 
6667c478bd9Sstevel@tonic-gate /*
6677c478bd9Sstevel@tonic-gate  * Used by 'bart create' with the '-I' option.  Return each entry into a 'buf'
6687c478bd9Sstevel@tonic-gate  * with the appropriate exit code: '0' for success and '-1' for failure.
6697c478bd9Sstevel@tonic-gate  */
6707c478bd9Sstevel@tonic-gate static int
6717c478bd9Sstevel@tonic-gate read_filelist(char *reloc_root, char **argv, char *buf, size_t bufsize)
6727c478bd9Sstevel@tonic-gate {
6737c478bd9Sstevel@tonic-gate 	static int		argv_index = -1;
6747c478bd9Sstevel@tonic-gate 	static boolean_t	read_stdinput = B_FALSE;
6757c478bd9Sstevel@tonic-gate 	char			temp_buf[PATH_MAX];
6767c478bd9Sstevel@tonic-gate 	char 			*cp;
6777c478bd9Sstevel@tonic-gate 
6787c478bd9Sstevel@tonic-gate 	/*
6797c478bd9Sstevel@tonic-gate 	 * INITIALIZATION:
6807c478bd9Sstevel@tonic-gate 	 * Setup this code so it knows whether or not to read sdtin.
6817c478bd9Sstevel@tonic-gate 	 * Also, if reading from argv, setup the index, "argv_index"
6827c478bd9Sstevel@tonic-gate 	 */
6837c478bd9Sstevel@tonic-gate 	if (argv_index == -1) {
6847c478bd9Sstevel@tonic-gate 		argv_index = 0;
6857c478bd9Sstevel@tonic-gate 
6867c478bd9Sstevel@tonic-gate 		/* In this case, no args after '-I', so read stdin */
6877c478bd9Sstevel@tonic-gate 		if (argv[0] == NULL)
6887c478bd9Sstevel@tonic-gate 			read_stdinput = B_TRUE;
6897c478bd9Sstevel@tonic-gate 	}
6907c478bd9Sstevel@tonic-gate 
6917c478bd9Sstevel@tonic-gate 	buf[0] = '\0';
6927c478bd9Sstevel@tonic-gate 
6937c478bd9Sstevel@tonic-gate 	if (read_stdinput) {
6947c478bd9Sstevel@tonic-gate 		if (fgets(temp_buf, PATH_MAX, stdin) == NULL)
6957c478bd9Sstevel@tonic-gate 			return (-1);
6967c478bd9Sstevel@tonic-gate 		cp = strtok(temp_buf, "\n");
6977c478bd9Sstevel@tonic-gate 	} else {
6987c478bd9Sstevel@tonic-gate 		cp = argv[argv_index++];
6997c478bd9Sstevel@tonic-gate 	}
7007c478bd9Sstevel@tonic-gate 
7017c478bd9Sstevel@tonic-gate 	if (cp == NULL)
7027c478bd9Sstevel@tonic-gate 		return (-1);
7037c478bd9Sstevel@tonic-gate 
7047c478bd9Sstevel@tonic-gate 	/*
7057c478bd9Sstevel@tonic-gate 	 * Unlike similar code elsewhere, avoid adding a leading
7067c478bd9Sstevel@tonic-gate 	 * slash for relative pathnames.
7077c478bd9Sstevel@tonic-gate 	 */
7087c478bd9Sstevel@tonic-gate 	(void) snprintf(buf, bufsize,
7097c478bd9Sstevel@tonic-gate 	    (reloc_root[0] == '\0' || cp[0] == '/') ? "%s%s" : "%s/%s",
7107c478bd9Sstevel@tonic-gate 	    reloc_root, cp);
7117c478bd9Sstevel@tonic-gate 
7127c478bd9Sstevel@tonic-gate 	return (0);
7137c478bd9Sstevel@tonic-gate }
714