xref: /titanic_44/usr/src/cmd/bart/create.c (revision c40f76e346ad844b9326c2049644b7b1d1a93e48)
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
5*c40f76e3Sjc144527  * Common Development and Distribution License (the "License").
6*c40f76e3Sjc144527  * 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 		 * This subtree has already been traversed by a
2467c478bd9Sstevel@tonic-gate 		 * previous stanza, i.e. this rule is a subset of a
2477c478bd9Sstevel@tonic-gate 		 * previous rule.
2487c478bd9Sstevel@tonic-gate 		 *
2497c478bd9Sstevel@tonic-gate 		 * Subtree has already been handled so move on!
2507c478bd9Sstevel@tonic-gate 		 */
2517c478bd9Sstevel@tonic-gate 		if (root->traversed)
2527c478bd9Sstevel@tonic-gate 			continue;
2537c478bd9Sstevel@tonic-gate 
2547c478bd9Sstevel@tonic-gate 		/*
2557c478bd9Sstevel@tonic-gate 		 * Check to see if this subtree should have contents
2567c478bd9Sstevel@tonic-gate 		 * checking turned on or off.
2577c478bd9Sstevel@tonic-gate 		 *
2587c478bd9Sstevel@tonic-gate 		 * NOTE: The 'compute_chksum' and 'parent_vfs'
2597c478bd9Sstevel@tonic-gate 		 * are a necessary hack: the variables are used in
2607c478bd9Sstevel@tonic-gate 		 * walker(), both directly and indirectly.  Since
2617c478bd9Sstevel@tonic-gate 		 * the parameters to walker() are defined by nftw(),
2627c478bd9Sstevel@tonic-gate 		 * the globals are really a backdoor mechanism.
2637c478bd9Sstevel@tonic-gate 		 */
2647c478bd9Sstevel@tonic-gate 		ret_status = statvfs(root->subtree, &parent_vfs);
2657c478bd9Sstevel@tonic-gate 		if (ret_status < 0) {
2667c478bd9Sstevel@tonic-gate 			perror(root->subtree);
2677c478bd9Sstevel@tonic-gate 			continue;
2687c478bd9Sstevel@tonic-gate 		}
2697c478bd9Sstevel@tonic-gate 
2707c478bd9Sstevel@tonic-gate 		/*
2717c478bd9Sstevel@tonic-gate 		 * Walk the subtree and invoke the callback function
2727c478bd9Sstevel@tonic-gate 		 * walker()
2737c478bd9Sstevel@tonic-gate 		 */
2747c478bd9Sstevel@tonic-gate 		subtree_root = root;
2757c478bd9Sstevel@tonic-gate 		(void) nftw64(root->subtree, &walker, 20, FTW_PHYS);
2767c478bd9Sstevel@tonic-gate 		root->traversed = B_TRUE;
2777c478bd9Sstevel@tonic-gate 
2787c478bd9Sstevel@tonic-gate 		/*
2797c478bd9Sstevel@tonic-gate 		 * Ugly but necessary:
2807c478bd9Sstevel@tonic-gate 		 *
2817c478bd9Sstevel@tonic-gate 		 * walker() must return 0, or the tree walk will stop,
2827c478bd9Sstevel@tonic-gate 		 * so warning flags must be set through a global.
2837c478bd9Sstevel@tonic-gate 		 */
2847c478bd9Sstevel@tonic-gate 		if (eval_err == WARNING_EXIT)
2857c478bd9Sstevel@tonic-gate 			ret_status = WARNING_EXIT;
2867c478bd9Sstevel@tonic-gate 
2877c478bd9Sstevel@tonic-gate 	}
2887c478bd9Sstevel@tonic-gate 	return (ret_status);
2897c478bd9Sstevel@tonic-gate }
2907c478bd9Sstevel@tonic-gate 
2917c478bd9Sstevel@tonic-gate static int
2927c478bd9Sstevel@tonic-gate create_manifest_filelist(char **argv, char *reloc_root)
2937c478bd9Sstevel@tonic-gate {
2947c478bd9Sstevel@tonic-gate 	int	ret_status = EXIT;
2957c478bd9Sstevel@tonic-gate 	char	input_fname[PATH_MAX];
2967c478bd9Sstevel@tonic-gate 
2977c478bd9Sstevel@tonic-gate 	while (read_filelist(reloc_root, argv,
2987c478bd9Sstevel@tonic-gate 	    input_fname, sizeof (input_fname)) != -1) {
2997c478bd9Sstevel@tonic-gate 
3007c478bd9Sstevel@tonic-gate 		struct stat64	stat_buf;
3017c478bd9Sstevel@tonic-gate 		int		ret;
3027c478bd9Sstevel@tonic-gate 
3037c478bd9Sstevel@tonic-gate 		ret = lstat64(input_fname, &stat_buf);
3047c478bd9Sstevel@tonic-gate 		if (ret < 0) {
3057c478bd9Sstevel@tonic-gate 			ret_status = WARNING_EXIT;
3067c478bd9Sstevel@tonic-gate 			perror(input_fname);
3077c478bd9Sstevel@tonic-gate 		} else {
3087c478bd9Sstevel@tonic-gate 			ret = eval_file(input_fname, &stat_buf);
3097c478bd9Sstevel@tonic-gate 
3107c478bd9Sstevel@tonic-gate 			if (ret == WARNING_EXIT)
3117c478bd9Sstevel@tonic-gate 				ret_status = WARNING_EXIT;
3127c478bd9Sstevel@tonic-gate 		}
3137c478bd9Sstevel@tonic-gate 	}
3147c478bd9Sstevel@tonic-gate 
3157c478bd9Sstevel@tonic-gate 	return (ret_status);
3167c478bd9Sstevel@tonic-gate }
3177c478bd9Sstevel@tonic-gate 
3187c478bd9Sstevel@tonic-gate /*
3197c478bd9Sstevel@tonic-gate  * output_manifest() the child process.  It reads in the output from
3207c478bd9Sstevel@tonic-gate  * create_manifest() and sorts it.
3217c478bd9Sstevel@tonic-gate  */
3227c478bd9Sstevel@tonic-gate static void
3237c478bd9Sstevel@tonic-gate output_manifest(void)
3247c478bd9Sstevel@tonic-gate {
3257c478bd9Sstevel@tonic-gate 	char	*env[] = {"LC_CTYPE=C", "LC_COLLATE=C", "LC_NUMERIC=C", NULL};
3267c478bd9Sstevel@tonic-gate 	time_t		time_val;
3277c478bd9Sstevel@tonic-gate 	struct tm	*tm;
3287c478bd9Sstevel@tonic-gate 	char		time_buf[1024];
3297c478bd9Sstevel@tonic-gate 
3307c478bd9Sstevel@tonic-gate 	(void) printf("%s", MANIFEST_VER);
3317c478bd9Sstevel@tonic-gate 	time_val = time((time_t)0);
3327c478bd9Sstevel@tonic-gate 	tm = localtime(&time_val);
3337c478bd9Sstevel@tonic-gate 	(void) strftime(time_buf, sizeof (time_buf), "%A, %B %d, %Y (%T)", tm);
3347c478bd9Sstevel@tonic-gate 	(void) printf("! %s\n", time_buf);
3357c478bd9Sstevel@tonic-gate 	(void) printf("%s", FORMAT_STR);
3367c478bd9Sstevel@tonic-gate 	(void) fflush(stdout);
3377c478bd9Sstevel@tonic-gate 	/*
3387c478bd9Sstevel@tonic-gate 	 * Simply run sort and read from the the current stdin, which is really
3397c478bd9Sstevel@tonic-gate 	 * the output of create_manifest().
3407c478bd9Sstevel@tonic-gate 	 * Also, make sure the output is unique, since a given file may be
3417c478bd9Sstevel@tonic-gate 	 * included by several stanzas.
3427c478bd9Sstevel@tonic-gate 	 */
3437c478bd9Sstevel@tonic-gate 	if (execle("/usr/bin/sort", "sort", NULL, env) < 0) {
3447c478bd9Sstevel@tonic-gate 		perror("");
3457c478bd9Sstevel@tonic-gate 		exit(FATAL_EXIT);
3467c478bd9Sstevel@tonic-gate 	}
3477c478bd9Sstevel@tonic-gate 
3487c478bd9Sstevel@tonic-gate 	/*NOTREACHED*/
3497c478bd9Sstevel@tonic-gate }
3507c478bd9Sstevel@tonic-gate 
3517c478bd9Sstevel@tonic-gate /*
3527c478bd9Sstevel@tonic-gate  * Callback function for nftw()
3537c478bd9Sstevel@tonic-gate  */
3547c478bd9Sstevel@tonic-gate static int
3557c478bd9Sstevel@tonic-gate walker(const char *name, const struct stat64 *sp, int type, struct FTW *ftwx)
3567c478bd9Sstevel@tonic-gate {
3577c478bd9Sstevel@tonic-gate 	int		ret;
3587c478bd9Sstevel@tonic-gate 	struct statvfs	path_vfs;
3597c478bd9Sstevel@tonic-gate 	boolean_t	dir_flag = B_FALSE;
3607c478bd9Sstevel@tonic-gate 	struct rule	*rule;
3617c478bd9Sstevel@tonic-gate 
3627c478bd9Sstevel@tonic-gate 	switch (type) {
3637c478bd9Sstevel@tonic-gate 	case FTW_F:	/* file 		*/
3647c478bd9Sstevel@tonic-gate 		rule = check_rules(name, 'F');
3657c478bd9Sstevel@tonic-gate 		if (rule != NULL) {
3667c478bd9Sstevel@tonic-gate 			if (rule->attr_list & ATTR_CONTENTS)
3677c478bd9Sstevel@tonic-gate 				compute_chksum = 1;
3687c478bd9Sstevel@tonic-gate 			else
3697c478bd9Sstevel@tonic-gate 				compute_chksum = 0;
3707c478bd9Sstevel@tonic-gate 		}
3717c478bd9Sstevel@tonic-gate 		break;
3727c478bd9Sstevel@tonic-gate 	case FTW_SL:	/* symbolic link	*/
3737c478bd9Sstevel@tonic-gate 	case FTW_DP:	/* end of directory	*/
3747c478bd9Sstevel@tonic-gate 	case FTW_DNR:	/* unreadable directory	*/
3757c478bd9Sstevel@tonic-gate 	case FTW_NS:	/* unstatable file	*/
3767c478bd9Sstevel@tonic-gate 		break;
3777c478bd9Sstevel@tonic-gate 	case FTW_D:	/* enter directory 		*/
3787c478bd9Sstevel@tonic-gate 
3797c478bd9Sstevel@tonic-gate 		/*
3807c478bd9Sstevel@tonic-gate 		 * Check to see if any subsequent rules are a subset
3817c478bd9Sstevel@tonic-gate 		 * of this rule; if they are, then mark them as
3827c478bd9Sstevel@tonic-gate 		 * "traversed".
3837c478bd9Sstevel@tonic-gate 		 */
3847c478bd9Sstevel@tonic-gate 		rule = subtree_root->next;
3857c478bd9Sstevel@tonic-gate 		while (rule != NULL) {
3867c478bd9Sstevel@tonic-gate 			if (strcmp(name, rule->subtree) == 0)
3877c478bd9Sstevel@tonic-gate 				rule->traversed = B_TRUE;
3887c478bd9Sstevel@tonic-gate 
3897c478bd9Sstevel@tonic-gate 			rule = rule->next;
3907c478bd9Sstevel@tonic-gate 		}
3917c478bd9Sstevel@tonic-gate 		dir_flag = B_TRUE;
3927c478bd9Sstevel@tonic-gate 		ret = statvfs(name, &path_vfs);
3937c478bd9Sstevel@tonic-gate 		if (ret < 0)
3947c478bd9Sstevel@tonic-gate 			eval_err = WARNING_EXIT;
3957c478bd9Sstevel@tonic-gate 		break;
3967c478bd9Sstevel@tonic-gate 	default:
3977c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, INVALID_FILE, name);
3987c478bd9Sstevel@tonic-gate 		eval_err = WARNING_EXIT;
3997c478bd9Sstevel@tonic-gate 		break;
4007c478bd9Sstevel@tonic-gate 	}
4017c478bd9Sstevel@tonic-gate 
4027c478bd9Sstevel@tonic-gate 	/* This is the function which really processes the file */
4037c478bd9Sstevel@tonic-gate 	ret = eval_file(name, sp);
4047c478bd9Sstevel@tonic-gate 
4057c478bd9Sstevel@tonic-gate 	/*
4067c478bd9Sstevel@tonic-gate 	 * Since the parameters to walker() are constrained by nftw(),
4077c478bd9Sstevel@tonic-gate 	 * need to use a global to reflect a WARNING.  Sigh.
4087c478bd9Sstevel@tonic-gate 	 */
4097c478bd9Sstevel@tonic-gate 	if (ret == WARNING_EXIT)
4107c478bd9Sstevel@tonic-gate 		eval_err = WARNING_EXIT;
4117c478bd9Sstevel@tonic-gate 
4127c478bd9Sstevel@tonic-gate 	/*
4137c478bd9Sstevel@tonic-gate 	 * This is a case of a directory which crosses into a mounted
4147c478bd9Sstevel@tonic-gate 	 * filesystem of a different type, e.g., UFS -> NFS.
4157c478bd9Sstevel@tonic-gate 	 * BART should not walk the new filesystem (by specification), so
4167c478bd9Sstevel@tonic-gate 	 * set this consolidation-private flag so the rest of the subtree
4177c478bd9Sstevel@tonic-gate 	 * under this directory is not waled.
4187c478bd9Sstevel@tonic-gate 	 */
4197c478bd9Sstevel@tonic-gate 	if (dir_flag &&
4207c478bd9Sstevel@tonic-gate 	    (strcmp(parent_vfs.f_basetype, path_vfs.f_basetype) != 0))
4217c478bd9Sstevel@tonic-gate 		ftwx->quit = FTW_PRUNE;
4227c478bd9Sstevel@tonic-gate 
4237c478bd9Sstevel@tonic-gate 	return (0);
4247c478bd9Sstevel@tonic-gate }
4257c478bd9Sstevel@tonic-gate 
4267c478bd9Sstevel@tonic-gate /*
4277c478bd9Sstevel@tonic-gate  * This file does the per-file evaluation and is run to generate every entry
4287c478bd9Sstevel@tonic-gate  * in the manifest.
4297c478bd9Sstevel@tonic-gate  *
4307c478bd9Sstevel@tonic-gate  * All output is written to a pipe which is read by the child process,
4317c478bd9Sstevel@tonic-gate  * which is running output_manifest().
4327c478bd9Sstevel@tonic-gate  */
4337c478bd9Sstevel@tonic-gate static int
4347c478bd9Sstevel@tonic-gate eval_file(const char *fname, const struct stat64 *statb)
4357c478bd9Sstevel@tonic-gate {
4367c478bd9Sstevel@tonic-gate 	int	fd, ret, err_code, i;
4377c478bd9Sstevel@tonic-gate 	char	last_field[PATH_MAX], ftype, *acl_str,
4387c478bd9Sstevel@tonic-gate 		*quoted_name;
4397c478bd9Sstevel@tonic-gate 
4407c478bd9Sstevel@tonic-gate 	err_code = EXIT;
4417c478bd9Sstevel@tonic-gate 
4427c478bd9Sstevel@tonic-gate 	switch (statb->st_mode & S_IFMT) {
4437c478bd9Sstevel@tonic-gate 	/* Regular file */
4447c478bd9Sstevel@tonic-gate 	case S_IFREG: ftype = 'F'; break;
4457c478bd9Sstevel@tonic-gate 
4467c478bd9Sstevel@tonic-gate 	/* Directory */
4477c478bd9Sstevel@tonic-gate 	case S_IFDIR: ftype = 'D'; break;
4487c478bd9Sstevel@tonic-gate 
4497c478bd9Sstevel@tonic-gate 	/* Block Device */
4507c478bd9Sstevel@tonic-gate 	case S_IFBLK: ftype = 'B'; break;
4517c478bd9Sstevel@tonic-gate 
4527c478bd9Sstevel@tonic-gate 	/* Character Device */
4537c478bd9Sstevel@tonic-gate 	case S_IFCHR: ftype = 'C'; break;
4547c478bd9Sstevel@tonic-gate 
4557c478bd9Sstevel@tonic-gate 	/* Named Pipe */
4567c478bd9Sstevel@tonic-gate 	case S_IFIFO: ftype = 'P'; break;
4577c478bd9Sstevel@tonic-gate 
4587c478bd9Sstevel@tonic-gate 	/* Socket */
4597c478bd9Sstevel@tonic-gate 	case S_IFSOCK: ftype = 'S'; break;
4607c478bd9Sstevel@tonic-gate 
4617c478bd9Sstevel@tonic-gate 	/* Door */
4627c478bd9Sstevel@tonic-gate 	case S_IFDOOR: ftype = 'O'; break;
4637c478bd9Sstevel@tonic-gate 
4647c478bd9Sstevel@tonic-gate 	/* Symbolic link */
4657c478bd9Sstevel@tonic-gate 	case S_IFLNK: ftype = 'L'; break;
4667c478bd9Sstevel@tonic-gate 
4677c478bd9Sstevel@tonic-gate 	default: ftype = '-'; break;
4687c478bd9Sstevel@tonic-gate 	}
4697c478bd9Sstevel@tonic-gate 
4707c478bd9Sstevel@tonic-gate 	/* First, make sure this file should be cataloged */
4717c478bd9Sstevel@tonic-gate 
4727c478bd9Sstevel@tonic-gate 	if ((subtree_root != NULL) &&
4737c478bd9Sstevel@tonic-gate 	    (exclude_fname(fname, ftype, subtree_root)))
4747c478bd9Sstevel@tonic-gate 		return (err_code);
4757c478bd9Sstevel@tonic-gate 
4767c478bd9Sstevel@tonic-gate 	for (i = 0; i < PATH_MAX; i++)
4777c478bd9Sstevel@tonic-gate 		last_field[i] = '\0';
4787c478bd9Sstevel@tonic-gate 
4797c478bd9Sstevel@tonic-gate 	/*
4807c478bd9Sstevel@tonic-gate 	 * Regular files, compute the MD5 checksum and put it into 'last_field'
4817c478bd9Sstevel@tonic-gate 	 * UNLESS instructed to ignore the checksums.
4827c478bd9Sstevel@tonic-gate 	 */
4837c478bd9Sstevel@tonic-gate 	if (ftype == 'F') {
4847c478bd9Sstevel@tonic-gate 		if (compute_chksum) {
4857c478bd9Sstevel@tonic-gate 			fd = open(fname, O_RDONLY|O_LARGEFILE);
4867c478bd9Sstevel@tonic-gate 			if (fd < 0) {
4877c478bd9Sstevel@tonic-gate 				err_code = WARNING_EXIT;
4887c478bd9Sstevel@tonic-gate 				perror(fname);
4897c478bd9Sstevel@tonic-gate 
4907c478bd9Sstevel@tonic-gate 				/* default value since the computution failed */
4917c478bd9Sstevel@tonic-gate 				(void) strcpy(last_field, "-");
4927c478bd9Sstevel@tonic-gate 			} else {
4937c478bd9Sstevel@tonic-gate 				if (generate_hash(fd, last_field) != 0) {
4947c478bd9Sstevel@tonic-gate 					err_code = WARNING_EXIT;
4957c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr, CONTENTS_WARN,
4967c478bd9Sstevel@tonic-gate 					    fname);
4977c478bd9Sstevel@tonic-gate 					(void) strcpy(last_field, "-");
4987c478bd9Sstevel@tonic-gate 				}
4997c478bd9Sstevel@tonic-gate 			}
5007c478bd9Sstevel@tonic-gate 			(void) close(fd);
5017c478bd9Sstevel@tonic-gate 		}
5027c478bd9Sstevel@tonic-gate 		/* Instructed to ignore checksums, just put in a '-' */
5037c478bd9Sstevel@tonic-gate 		else
5047c478bd9Sstevel@tonic-gate 			(void) strcpy(last_field, "-");
5057c478bd9Sstevel@tonic-gate 	}
5067c478bd9Sstevel@tonic-gate 
5077c478bd9Sstevel@tonic-gate 	/*
5087c478bd9Sstevel@tonic-gate 	 * For symbolic links, put the destination of the symbolic link into
5097c478bd9Sstevel@tonic-gate 	 * 'last_field'
5107c478bd9Sstevel@tonic-gate 	 */
5117c478bd9Sstevel@tonic-gate 	if (ftype == 'L') {
5127c478bd9Sstevel@tonic-gate 		ret = readlink(fname, last_field, sizeof (last_field));
5137c478bd9Sstevel@tonic-gate 		if (ret < 0) {
5147c478bd9Sstevel@tonic-gate 			err_code = WARNING_EXIT;
5157c478bd9Sstevel@tonic-gate 			perror(fname);
5167c478bd9Sstevel@tonic-gate 
5177c478bd9Sstevel@tonic-gate 			/* default value since the computation failed */
5187c478bd9Sstevel@tonic-gate 			(void) strcpy(last_field, "-");
5197c478bd9Sstevel@tonic-gate 		}
5207c478bd9Sstevel@tonic-gate 		else
5217c478bd9Sstevel@tonic-gate 			(void) strlcpy(last_field,
5227c478bd9Sstevel@tonic-gate 			    sanitized_fname(last_field, B_FALSE),
5237c478bd9Sstevel@tonic-gate 			    sizeof (last_field));
5247c478bd9Sstevel@tonic-gate 
5257c478bd9Sstevel@tonic-gate 		/*
5267c478bd9Sstevel@tonic-gate 		 * Boundary condition: possible for a symlink to point to
5277c478bd9Sstevel@tonic-gate 		 * nothing [ ln -s '' link_name ].  For this case, set the
5287c478bd9Sstevel@tonic-gate 		 * destination to "\000".
5297c478bd9Sstevel@tonic-gate 		 */
5307c478bd9Sstevel@tonic-gate 		if (strlen(last_field) == 0)
5317c478bd9Sstevel@tonic-gate 			(void) strcpy(last_field, "\\000");
5327c478bd9Sstevel@tonic-gate 	}
5337c478bd9Sstevel@tonic-gate 
5347c478bd9Sstevel@tonic-gate 	acl_str = get_acl_string(fname, statb, &err_code);
5357c478bd9Sstevel@tonic-gate 
5367c478bd9Sstevel@tonic-gate 	/* Sanitize 'fname', so its in the proper format for the manifest */
5377c478bd9Sstevel@tonic-gate 	quoted_name = sanitized_fname(fname, B_TRUE);
5387c478bd9Sstevel@tonic-gate 
5397c478bd9Sstevel@tonic-gate 	/* Start to build the entry.... */
5407c478bd9Sstevel@tonic-gate 	(void) printf("%s %c %d %o %s %x %d %d", quoted_name, ftype,
5417c478bd9Sstevel@tonic-gate 	    (int)statb->st_size, (int)statb->st_mode, acl_str,
5427c478bd9Sstevel@tonic-gate 	    (int)statb->st_mtime, (int)statb->st_uid, (int)statb->st_gid);
5437c478bd9Sstevel@tonic-gate 
5447c478bd9Sstevel@tonic-gate 	/* Finish it off based upon whether or not it's a device node */
545*c40f76e3Sjc144527 	if ((ftype == 'B') || (ftype == 'C'))
5467c478bd9Sstevel@tonic-gate 		(void) printf(" %x\n", (int)statb->st_rdev);
5477c478bd9Sstevel@tonic-gate 	else if (strlen(last_field) > 0)
5487c478bd9Sstevel@tonic-gate 		(void) printf(" %s\n", last_field);
5497c478bd9Sstevel@tonic-gate 	else
5507c478bd9Sstevel@tonic-gate 		(void) printf("\n");
5517c478bd9Sstevel@tonic-gate 
5527c478bd9Sstevel@tonic-gate 	/* free the memory consumed */
5537c478bd9Sstevel@tonic-gate 	free(acl_str);
5547c478bd9Sstevel@tonic-gate 	free(quoted_name);
5557c478bd9Sstevel@tonic-gate 
5567c478bd9Sstevel@tonic-gate 	return (err_code);
5577c478bd9Sstevel@tonic-gate }
5587c478bd9Sstevel@tonic-gate 
5597c478bd9Sstevel@tonic-gate /*
5607c478bd9Sstevel@tonic-gate  * When creating a manifest, make sure all '?', tabs, space, newline, '/'
5617c478bd9Sstevel@tonic-gate  * and '[' are all properly quoted.  Convert them to a "\ooo" where the 'ooo'
5627c478bd9Sstevel@tonic-gate  * represents their octal value. For filesystem objects, as opposed to symlink
5637c478bd9Sstevel@tonic-gate  * targets, also canonicalize the pathname.
5647c478bd9Sstevel@tonic-gate  */
5657c478bd9Sstevel@tonic-gate static char *
5667c478bd9Sstevel@tonic-gate sanitized_fname(const char *fname, boolean_t canon_path)
5677c478bd9Sstevel@tonic-gate {
5687c478bd9Sstevel@tonic-gate 	const char *ip;
5697c478bd9Sstevel@tonic-gate 	unsigned char ch;
5707c478bd9Sstevel@tonic-gate 	char *op, *quoted_name;
5717c478bd9Sstevel@tonic-gate 
5727c478bd9Sstevel@tonic-gate 	/* Initialize everything */
5737c478bd9Sstevel@tonic-gate 	quoted_name = safe_calloc((4 * PATH_MAX) + 1);
5747c478bd9Sstevel@tonic-gate 	ip = fname;
5757c478bd9Sstevel@tonic-gate 	op = quoted_name;
5767c478bd9Sstevel@tonic-gate 
5777c478bd9Sstevel@tonic-gate 	if (canon_path) {
5787c478bd9Sstevel@tonic-gate 		/*
5797c478bd9Sstevel@tonic-gate 		 * In the case when a relocatable root was used, the relocatable
5807c478bd9Sstevel@tonic-gate 		 * root should *not* be part of the manifest.
5817c478bd9Sstevel@tonic-gate 		 */
5827c478bd9Sstevel@tonic-gate 		ip += strlen(reloc_root);
5837c478bd9Sstevel@tonic-gate 
5847c478bd9Sstevel@tonic-gate 		/*
5857c478bd9Sstevel@tonic-gate 		 * In the case when the '-I' option was used, make sure
5867c478bd9Sstevel@tonic-gate 		 * the quoted_name starts with a '/'.
5877c478bd9Sstevel@tonic-gate 		 */
5887c478bd9Sstevel@tonic-gate 		if (*ip != '/')
5897c478bd9Sstevel@tonic-gate 			*op++ = '/';
5907c478bd9Sstevel@tonic-gate 	}
5917c478bd9Sstevel@tonic-gate 
5927c478bd9Sstevel@tonic-gate 	/* Now walk through 'fname' and build the quoted string */
5937c478bd9Sstevel@tonic-gate 	while ((ch = *ip++) != 0) {
5947c478bd9Sstevel@tonic-gate 		switch (ch) {
5957c478bd9Sstevel@tonic-gate 		/* Quote the following characters */
5967c478bd9Sstevel@tonic-gate 		case ' ':
5977c478bd9Sstevel@tonic-gate 		case '*':
5987c478bd9Sstevel@tonic-gate 		case '\n':
5997c478bd9Sstevel@tonic-gate 		case '?':
6007c478bd9Sstevel@tonic-gate 		case '[':
6017c478bd9Sstevel@tonic-gate 		case '\\':
6027c478bd9Sstevel@tonic-gate 		case '\t':
6037c478bd9Sstevel@tonic-gate 			op += sprintf(op, "\\%.3o", (unsigned char)ch);
6047c478bd9Sstevel@tonic-gate 			break;
6057c478bd9Sstevel@tonic-gate 
6067c478bd9Sstevel@tonic-gate 		/* Otherwise, simply append them */
6077c478bd9Sstevel@tonic-gate 		default:
6087c478bd9Sstevel@tonic-gate 			*op++ = ch;
6097c478bd9Sstevel@tonic-gate 			break;
6107c478bd9Sstevel@tonic-gate 		}
6117c478bd9Sstevel@tonic-gate 	}
6127c478bd9Sstevel@tonic-gate 
6137c478bd9Sstevel@tonic-gate 	*op = 0;
6147c478bd9Sstevel@tonic-gate 
6157c478bd9Sstevel@tonic-gate 	return (quoted_name);
6167c478bd9Sstevel@tonic-gate }
6177c478bd9Sstevel@tonic-gate 
6187c478bd9Sstevel@tonic-gate /*
6197c478bd9Sstevel@tonic-gate  * Function responsible for generating the ACL information for a given
6207c478bd9Sstevel@tonic-gate  * file.  Note, the string is put into buffer malloc'd by this function.
6217c478bd9Sstevel@tonic-gate  * Its the responsibility of the caller to free the buffer.
6227c478bd9Sstevel@tonic-gate  */
6237c478bd9Sstevel@tonic-gate static char *
6247c478bd9Sstevel@tonic-gate get_acl_string(const char *fname, const struct stat64 *statb, int *err_code)
6257c478bd9Sstevel@tonic-gate {
626fa9e4066Sahrens 	acl_t		*aclp;
627fa9e4066Sahrens 	char		*acltext;
628fa9e4066Sahrens 	int		error;
6297c478bd9Sstevel@tonic-gate 
6307c478bd9Sstevel@tonic-gate 	if (S_ISLNK(statb->st_mode)) {
6317c478bd9Sstevel@tonic-gate 		return (safe_strdup("-"));
6327c478bd9Sstevel@tonic-gate 	}
6337c478bd9Sstevel@tonic-gate 
6347c478bd9Sstevel@tonic-gate 	/*
635fa9e4066Sahrens 	 *  Include trivial acl's
6367c478bd9Sstevel@tonic-gate 	 */
637fa9e4066Sahrens 	error = acl_get(fname, 0, &aclp);
638fa9e4066Sahrens 
639fa9e4066Sahrens 	if (error != 0) {
6407c478bd9Sstevel@tonic-gate 		*err_code = WARNING_EXIT;
641fa9e4066Sahrens 		(void) fprintf(stderr, "%s: %s\n", fname, acl_strerror(error));
6427c478bd9Sstevel@tonic-gate 		return (safe_strdup("-"));
643fa9e4066Sahrens 	} else {
6445a5eeccaSmarks 		acltext = acl_totext(aclp, 0);
645fa9e4066Sahrens 		acl_free(aclp);
646fa9e4066Sahrens 		return (acltext);
6477c478bd9Sstevel@tonic-gate 	}
6487c478bd9Sstevel@tonic-gate }
6497c478bd9Sstevel@tonic-gate 
6507c478bd9Sstevel@tonic-gate 
6517c478bd9Sstevel@tonic-gate /*
6527c478bd9Sstevel@tonic-gate  *
6537c478bd9Sstevel@tonic-gate  * description:	This routine reads stdin in BUF_SIZE chunks, uses the bits
6547c478bd9Sstevel@tonic-gate  *		to update the md5 hash buffer, and outputs the chunks
6557c478bd9Sstevel@tonic-gate  *		to stdout.  When stdin is exhausted, the hash is computed,
6567c478bd9Sstevel@tonic-gate  *		converted to a hexadecimal string, and returned.
6577c478bd9Sstevel@tonic-gate  *
6587c478bd9Sstevel@tonic-gate  * returns:	The md5 hash of stdin, or NULL if unsuccessful for any reason.
6597c478bd9Sstevel@tonic-gate  */
6607c478bd9Sstevel@tonic-gate static int
6617c478bd9Sstevel@tonic-gate generate_hash(int fdin, char *hash_str)
6627c478bd9Sstevel@tonic-gate {
6637c478bd9Sstevel@tonic-gate 	unsigned char buf[BUF_SIZE];
6647c478bd9Sstevel@tonic-gate 	unsigned char hash[MD5_DIGEST_LENGTH];
6657c478bd9Sstevel@tonic-gate 	int i, amtread;
6667c478bd9Sstevel@tonic-gate 	MD5_CTX ctx;
6677c478bd9Sstevel@tonic-gate 
6687c478bd9Sstevel@tonic-gate 	MD5Init(&ctx);
6697c478bd9Sstevel@tonic-gate 
6707c478bd9Sstevel@tonic-gate 	for (;;) {
6717c478bd9Sstevel@tonic-gate 		amtread = read(fdin, buf, sizeof (buf));
6727c478bd9Sstevel@tonic-gate 		if (amtread == 0)
6737c478bd9Sstevel@tonic-gate 			break;
6747c478bd9Sstevel@tonic-gate 		if (amtread <  0)
6757c478bd9Sstevel@tonic-gate 			return (1);
6767c478bd9Sstevel@tonic-gate 
6777c478bd9Sstevel@tonic-gate 		/* got some data.  Now update hash */
6787c478bd9Sstevel@tonic-gate 		MD5Update(&ctx, buf, amtread);
6797c478bd9Sstevel@tonic-gate 	}
6807c478bd9Sstevel@tonic-gate 
6817c478bd9Sstevel@tonic-gate 	/* done passing through data, calculate hash */
6827c478bd9Sstevel@tonic-gate 	MD5Final(hash, &ctx);
6837c478bd9Sstevel@tonic-gate 
6847c478bd9Sstevel@tonic-gate 	for (i = 0; i < MD5_DIGEST_LENGTH; i++)
6857c478bd9Sstevel@tonic-gate 		(void) sprintf(hash_str + (i*2), "%2.2x", hash[i]);
6867c478bd9Sstevel@tonic-gate 
6877c478bd9Sstevel@tonic-gate 	return (0);
6887c478bd9Sstevel@tonic-gate }
6897c478bd9Sstevel@tonic-gate 
6907c478bd9Sstevel@tonic-gate /*
6917c478bd9Sstevel@tonic-gate  * Used by 'bart create' with the '-I' option.  Return each entry into a 'buf'
6927c478bd9Sstevel@tonic-gate  * with the appropriate exit code: '0' for success and '-1' for failure.
6937c478bd9Sstevel@tonic-gate  */
6947c478bd9Sstevel@tonic-gate static int
6957c478bd9Sstevel@tonic-gate read_filelist(char *reloc_root, char **argv, char *buf, size_t bufsize)
6967c478bd9Sstevel@tonic-gate {
6977c478bd9Sstevel@tonic-gate 	static int		argv_index = -1;
6987c478bd9Sstevel@tonic-gate 	static boolean_t	read_stdinput = B_FALSE;
6997c478bd9Sstevel@tonic-gate 	char			temp_buf[PATH_MAX];
7007c478bd9Sstevel@tonic-gate 	char 			*cp;
7017c478bd9Sstevel@tonic-gate 
7027c478bd9Sstevel@tonic-gate 	/*
7037c478bd9Sstevel@tonic-gate 	 * INITIALIZATION:
7047c478bd9Sstevel@tonic-gate 	 * Setup this code so it knows whether or not to read sdtin.
7057c478bd9Sstevel@tonic-gate 	 * Also, if reading from argv, setup the index, "argv_index"
7067c478bd9Sstevel@tonic-gate 	 */
7077c478bd9Sstevel@tonic-gate 	if (argv_index == -1) {
7087c478bd9Sstevel@tonic-gate 		argv_index = 0;
7097c478bd9Sstevel@tonic-gate 
7107c478bd9Sstevel@tonic-gate 		/* In this case, no args after '-I', so read stdin */
7117c478bd9Sstevel@tonic-gate 		if (argv[0] == NULL)
7127c478bd9Sstevel@tonic-gate 			read_stdinput = B_TRUE;
7137c478bd9Sstevel@tonic-gate 	}
7147c478bd9Sstevel@tonic-gate 
7157c478bd9Sstevel@tonic-gate 	buf[0] = '\0';
7167c478bd9Sstevel@tonic-gate 
7177c478bd9Sstevel@tonic-gate 	if (read_stdinput) {
7187c478bd9Sstevel@tonic-gate 		if (fgets(temp_buf, PATH_MAX, stdin) == NULL)
7197c478bd9Sstevel@tonic-gate 			return (-1);
7207c478bd9Sstevel@tonic-gate 		cp = strtok(temp_buf, "\n");
7217c478bd9Sstevel@tonic-gate 	} else {
7227c478bd9Sstevel@tonic-gate 		cp = argv[argv_index++];
7237c478bd9Sstevel@tonic-gate 	}
7247c478bd9Sstevel@tonic-gate 
7257c478bd9Sstevel@tonic-gate 	if (cp == NULL)
7267c478bd9Sstevel@tonic-gate 		return (-1);
7277c478bd9Sstevel@tonic-gate 
7287c478bd9Sstevel@tonic-gate 	/*
7297c478bd9Sstevel@tonic-gate 	 * Unlike similar code elsewhere, avoid adding a leading
7307c478bd9Sstevel@tonic-gate 	 * slash for relative pathnames.
7317c478bd9Sstevel@tonic-gate 	 */
7327c478bd9Sstevel@tonic-gate 	(void) snprintf(buf, bufsize,
7337c478bd9Sstevel@tonic-gate 	    (reloc_root[0] == '\0' || cp[0] == '/') ? "%s%s" : "%s/%s",
7347c478bd9Sstevel@tonic-gate 	    reloc_root, cp);
7357c478bd9Sstevel@tonic-gate 
7367c478bd9Sstevel@tonic-gate 	return (0);
7377c478bd9Sstevel@tonic-gate }
738