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