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 /*
22c690e840SJohn.Zolnowsky@Sun.COM * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
237c478bd9Sstevel@tonic-gate */
247c478bd9Sstevel@tonic-gate
257c478bd9Sstevel@tonic-gate #include <signal.h>
267c478bd9Sstevel@tonic-gate #include <unistd.h>
277c478bd9Sstevel@tonic-gate #include <sys/acl.h>
287c478bd9Sstevel@tonic-gate #include <sys/statvfs.h>
297c478bd9Sstevel@tonic-gate #include <sys/wait.h>
307c478bd9Sstevel@tonic-gate #include "bart.h"
31fa9e4066Sahrens #include <aclutils.h>
327c478bd9Sstevel@tonic-gate
337c478bd9Sstevel@tonic-gate static int sanitize_reloc_root(char *root, size_t bufsize);
347c478bd9Sstevel@tonic-gate static int create_manifest_filelist(char **argv, char *reloc_root);
357c478bd9Sstevel@tonic-gate static int create_manifest_rule(char *reloc_root, FILE *rule_fp);
367c478bd9Sstevel@tonic-gate static void output_manifest(void);
37*99389cdeSJan Parcel static int eval_file(const char *fname, const struct stat64 *statb,
38*99389cdeSJan Parcel struct FTW *ftwx);
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];
56a2721256SWilliam Young static struct statvfs64 parent_vfs;
577c478bd9Sstevel@tonic-gate
587c478bd9Sstevel@tonic-gate int
bart_create(int argc,char ** argv)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
sanitize_reloc_root(char * root,size_t bufsize)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
create_manifest_rule(char * reloc_root,FILE * rule_fp)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 */
253a2721256SWilliam 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 /*
260c690e840SJohn.Zolnowsky@Sun.COM * Walk the subtree and invoke the callback function walker()
261c690e840SJohn.Zolnowsky@Sun.COM * Use FTW_ANYERR to get FTW_NS and FTW_DNR entries *and*
262c690e840SJohn.Zolnowsky@Sun.COM * to continue past those errors.
2637c478bd9Sstevel@tonic-gate */
2647c478bd9Sstevel@tonic-gate subtree_root = root;
265c690e840SJohn.Zolnowsky@Sun.COM (void) nftw64(root->subtree, &walker, 20, FTW_PHYS|FTW_ANYERR);
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
create_manifest_filelist(char ** argv,char * reloc_root)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 {
297*99389cdeSJan Parcel ret = eval_file(input_fname, &stat_buf, NULL);
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
output_manifest(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 */
3329c84d166Srm88369 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
walker(const char * name,const struct stat64 * sp,int type,struct FTW * ftwx)3447c478bd9Sstevel@tonic-gate walker(const char *name, const struct stat64 *sp, int type, struct FTW *ftwx)
3457c478bd9Sstevel@tonic-gate {
3467c478bd9Sstevel@tonic-gate int ret;
347a2721256SWilliam Young struct statvfs64 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;
361c690e840SJohn.Zolnowsky@Sun.COM case FTW_SL: /* symbolic link, FTW_PHYS */
362c690e840SJohn.Zolnowsky@Sun.COM case FTW_SLN: /* symbolic link, ~FTW_PHYS */
3637c478bd9Sstevel@tonic-gate break;
364c690e840SJohn.Zolnowsky@Sun.COM case FTW_DP: /* end of directory, FTW_DEPTH */
365c690e840SJohn.Zolnowsky@Sun.COM case FTW_D: /* enter directory, ~FTW_DEPTH */
3667c478bd9Sstevel@tonic-gate dir_flag = B_TRUE;
367a2721256SWilliam Young ret = statvfs64(name, &path_vfs);
3687c478bd9Sstevel@tonic-gate if (ret < 0)
3697c478bd9Sstevel@tonic-gate eval_err = WARNING_EXIT;
3707c478bd9Sstevel@tonic-gate break;
371c690e840SJohn.Zolnowsky@Sun.COM case FTW_NS: /* unstatable file */
372c690e840SJohn.Zolnowsky@Sun.COM (void) fprintf(stderr, UNKNOWN_FILE, name);
3737c478bd9Sstevel@tonic-gate eval_err = WARNING_EXIT;
374c690e840SJohn.Zolnowsky@Sun.COM return (0);
375c690e840SJohn.Zolnowsky@Sun.COM case FTW_DNR: /* unreadable directory */
376c690e840SJohn.Zolnowsky@Sun.COM (void) fprintf(stderr, CANTLIST_DIR, name);
377c690e840SJohn.Zolnowsky@Sun.COM eval_err = WARNING_EXIT;
378c690e840SJohn.Zolnowsky@Sun.COM return (0);
379c690e840SJohn.Zolnowsky@Sun.COM default:
380c690e840SJohn.Zolnowsky@Sun.COM (void) fprintf(stderr, INTERNAL_ERR, name);
381c690e840SJohn.Zolnowsky@Sun.COM eval_err = WARNING_EXIT;
382c690e840SJohn.Zolnowsky@Sun.COM return (0);
3837c478bd9Sstevel@tonic-gate }
3847c478bd9Sstevel@tonic-gate
3857c478bd9Sstevel@tonic-gate /* This is the function which really processes the file */
386*99389cdeSJan Parcel ret = eval_file(name, sp, ftwx);
3877c478bd9Sstevel@tonic-gate
3887c478bd9Sstevel@tonic-gate /*
3897c478bd9Sstevel@tonic-gate * Since the parameters to walker() are constrained by nftw(),
3907c478bd9Sstevel@tonic-gate * need to use a global to reflect a WARNING. Sigh.
3917c478bd9Sstevel@tonic-gate */
3927c478bd9Sstevel@tonic-gate if (ret == WARNING_EXIT)
3937c478bd9Sstevel@tonic-gate eval_err = WARNING_EXIT;
3947c478bd9Sstevel@tonic-gate
3957c478bd9Sstevel@tonic-gate /*
3967c478bd9Sstevel@tonic-gate * This is a case of a directory which crosses into a mounted
3977c478bd9Sstevel@tonic-gate * filesystem of a different type, e.g., UFS -> NFS.
3987c478bd9Sstevel@tonic-gate * BART should not walk the new filesystem (by specification), so
3997c478bd9Sstevel@tonic-gate * set this consolidation-private flag so the rest of the subtree
4007c478bd9Sstevel@tonic-gate * under this directory is not waled.
4017c478bd9Sstevel@tonic-gate */
4027c478bd9Sstevel@tonic-gate if (dir_flag &&
4037c478bd9Sstevel@tonic-gate (strcmp(parent_vfs.f_basetype, path_vfs.f_basetype) != 0))
4047c478bd9Sstevel@tonic-gate ftwx->quit = FTW_PRUNE;
4057c478bd9Sstevel@tonic-gate
4067c478bd9Sstevel@tonic-gate return (0);
4077c478bd9Sstevel@tonic-gate }
4087c478bd9Sstevel@tonic-gate
4097c478bd9Sstevel@tonic-gate /*
4107c478bd9Sstevel@tonic-gate * This file does the per-file evaluation and is run to generate every entry
4117c478bd9Sstevel@tonic-gate * in the manifest.
4127c478bd9Sstevel@tonic-gate *
4137c478bd9Sstevel@tonic-gate * All output is written to a pipe which is read by the child process,
4147c478bd9Sstevel@tonic-gate * which is running output_manifest().
4157c478bd9Sstevel@tonic-gate */
4167c478bd9Sstevel@tonic-gate static int
eval_file(const char * fname,const struct stat64 * statb,struct FTW * ftwx)417*99389cdeSJan Parcel eval_file(const char *fname, const struct stat64 *statb, struct FTW *ftwx)
4187c478bd9Sstevel@tonic-gate {
419*99389cdeSJan Parcel int fd, ret, err_code, i, result;
420a2721256SWilliam Young char last_field[PATH_MAX], ftype, *acl_str;
421a2721256SWilliam Young char *quoted_name;
4227c478bd9Sstevel@tonic-gate
4237c478bd9Sstevel@tonic-gate err_code = EXIT;
4247c478bd9Sstevel@tonic-gate
4257c478bd9Sstevel@tonic-gate switch (statb->st_mode & S_IFMT) {
4267c478bd9Sstevel@tonic-gate /* Regular file */
4277c478bd9Sstevel@tonic-gate case S_IFREG: ftype = 'F'; break;
4287c478bd9Sstevel@tonic-gate
4297c478bd9Sstevel@tonic-gate /* Directory */
4307c478bd9Sstevel@tonic-gate case S_IFDIR: ftype = 'D'; break;
4317c478bd9Sstevel@tonic-gate
4327c478bd9Sstevel@tonic-gate /* Block Device */
4337c478bd9Sstevel@tonic-gate case S_IFBLK: ftype = 'B'; break;
4347c478bd9Sstevel@tonic-gate
4357c478bd9Sstevel@tonic-gate /* Character Device */
4367c478bd9Sstevel@tonic-gate case S_IFCHR: ftype = 'C'; break;
4377c478bd9Sstevel@tonic-gate
4387c478bd9Sstevel@tonic-gate /* Named Pipe */
4397c478bd9Sstevel@tonic-gate case S_IFIFO: ftype = 'P'; break;
4407c478bd9Sstevel@tonic-gate
4417c478bd9Sstevel@tonic-gate /* Socket */
4427c478bd9Sstevel@tonic-gate case S_IFSOCK: ftype = 'S'; break;
4437c478bd9Sstevel@tonic-gate
4447c478bd9Sstevel@tonic-gate /* Door */
4457c478bd9Sstevel@tonic-gate case S_IFDOOR: ftype = 'O'; break;
4467c478bd9Sstevel@tonic-gate
4477c478bd9Sstevel@tonic-gate /* Symbolic link */
4487c478bd9Sstevel@tonic-gate case S_IFLNK: ftype = 'L'; break;
4497c478bd9Sstevel@tonic-gate
4507c478bd9Sstevel@tonic-gate default: ftype = '-'; break;
4517c478bd9Sstevel@tonic-gate }
4527c478bd9Sstevel@tonic-gate
4537c478bd9Sstevel@tonic-gate /* First, make sure this file should be cataloged */
4547c478bd9Sstevel@tonic-gate
4557c478bd9Sstevel@tonic-gate if ((subtree_root != NULL) &&
456*99389cdeSJan Parcel ((result = exclude_fname(fname, ftype, subtree_root)) !=
457*99389cdeSJan Parcel NO_EXCLUDE)) {
458*99389cdeSJan Parcel if ((result == EXCLUDE_PRUNE) && (ftwx != (struct FTW *)NULL))
459*99389cdeSJan Parcel ftwx->quit = FTW_PRUNE;
4607c478bd9Sstevel@tonic-gate return (err_code);
461*99389cdeSJan Parcel }
4627c478bd9Sstevel@tonic-gate for (i = 0; i < PATH_MAX; i++)
4637c478bd9Sstevel@tonic-gate last_field[i] = '\0';
4647c478bd9Sstevel@tonic-gate
4657c478bd9Sstevel@tonic-gate /*
4667c478bd9Sstevel@tonic-gate * Regular files, compute the MD5 checksum and put it into 'last_field'
4677c478bd9Sstevel@tonic-gate * UNLESS instructed to ignore the checksums.
4687c478bd9Sstevel@tonic-gate */
4697c478bd9Sstevel@tonic-gate if (ftype == 'F') {
4707c478bd9Sstevel@tonic-gate if (compute_chksum) {
4717c478bd9Sstevel@tonic-gate fd = open(fname, O_RDONLY|O_LARGEFILE);
4727c478bd9Sstevel@tonic-gate if (fd < 0) {
4737c478bd9Sstevel@tonic-gate err_code = WARNING_EXIT;
4747c478bd9Sstevel@tonic-gate perror(fname);
4757c478bd9Sstevel@tonic-gate
4767c478bd9Sstevel@tonic-gate /* default value since the computution failed */
4777c478bd9Sstevel@tonic-gate (void) strcpy(last_field, "-");
4787c478bd9Sstevel@tonic-gate } else {
4797c478bd9Sstevel@tonic-gate if (generate_hash(fd, last_field) != 0) {
4807c478bd9Sstevel@tonic-gate err_code = WARNING_EXIT;
4817c478bd9Sstevel@tonic-gate (void) fprintf(stderr, CONTENTS_WARN,
4827c478bd9Sstevel@tonic-gate fname);
4837c478bd9Sstevel@tonic-gate (void) strcpy(last_field, "-");
4847c478bd9Sstevel@tonic-gate }
4857c478bd9Sstevel@tonic-gate }
4867c478bd9Sstevel@tonic-gate (void) close(fd);
4877c478bd9Sstevel@tonic-gate }
4887c478bd9Sstevel@tonic-gate /* Instructed to ignore checksums, just put in a '-' */
4897c478bd9Sstevel@tonic-gate else
4907c478bd9Sstevel@tonic-gate (void) strcpy(last_field, "-");
4917c478bd9Sstevel@tonic-gate }
4927c478bd9Sstevel@tonic-gate
4937c478bd9Sstevel@tonic-gate /*
4947c478bd9Sstevel@tonic-gate * For symbolic links, put the destination of the symbolic link into
4957c478bd9Sstevel@tonic-gate * 'last_field'
4967c478bd9Sstevel@tonic-gate */
4977c478bd9Sstevel@tonic-gate if (ftype == 'L') {
4987c478bd9Sstevel@tonic-gate ret = readlink(fname, last_field, sizeof (last_field));
4997c478bd9Sstevel@tonic-gate if (ret < 0) {
5007c478bd9Sstevel@tonic-gate err_code = WARNING_EXIT;
5017c478bd9Sstevel@tonic-gate perror(fname);
5027c478bd9Sstevel@tonic-gate
5037c478bd9Sstevel@tonic-gate /* default value since the computation failed */
5047c478bd9Sstevel@tonic-gate (void) strcpy(last_field, "-");
5057c478bd9Sstevel@tonic-gate }
5067c478bd9Sstevel@tonic-gate else
5077c478bd9Sstevel@tonic-gate (void) strlcpy(last_field,
5087c478bd9Sstevel@tonic-gate sanitized_fname(last_field, B_FALSE),
5097c478bd9Sstevel@tonic-gate sizeof (last_field));
5107c478bd9Sstevel@tonic-gate
5117c478bd9Sstevel@tonic-gate /*
5127c478bd9Sstevel@tonic-gate * Boundary condition: possible for a symlink to point to
5137c478bd9Sstevel@tonic-gate * nothing [ ln -s '' link_name ]. For this case, set the
5147c478bd9Sstevel@tonic-gate * destination to "\000".
5157c478bd9Sstevel@tonic-gate */
5167c478bd9Sstevel@tonic-gate if (strlen(last_field) == 0)
5177c478bd9Sstevel@tonic-gate (void) strcpy(last_field, "\\000");
5187c478bd9Sstevel@tonic-gate }
5197c478bd9Sstevel@tonic-gate
5207c478bd9Sstevel@tonic-gate acl_str = get_acl_string(fname, statb, &err_code);
5217c478bd9Sstevel@tonic-gate
5227c478bd9Sstevel@tonic-gate /* Sanitize 'fname', so its in the proper format for the manifest */
5237c478bd9Sstevel@tonic-gate quoted_name = sanitized_fname(fname, B_TRUE);
5247c478bd9Sstevel@tonic-gate
5257c478bd9Sstevel@tonic-gate /* Start to build the entry.... */
5267c478bd9Sstevel@tonic-gate (void) printf("%s %c %d %o %s %x %d %d", quoted_name, ftype,
5277c478bd9Sstevel@tonic-gate (int)statb->st_size, (int)statb->st_mode, acl_str,
5287c478bd9Sstevel@tonic-gate (int)statb->st_mtime, (int)statb->st_uid, (int)statb->st_gid);
5297c478bd9Sstevel@tonic-gate
5307c478bd9Sstevel@tonic-gate /* Finish it off based upon whether or not it's a device node */
531c40f76e3Sjc144527 if ((ftype == 'B') || (ftype == 'C'))
5327c478bd9Sstevel@tonic-gate (void) printf(" %x\n", (int)statb->st_rdev);
5337c478bd9Sstevel@tonic-gate else if (strlen(last_field) > 0)
5347c478bd9Sstevel@tonic-gate (void) printf(" %s\n", last_field);
5357c478bd9Sstevel@tonic-gate else
5367c478bd9Sstevel@tonic-gate (void) printf("\n");
5377c478bd9Sstevel@tonic-gate
5387c478bd9Sstevel@tonic-gate /* free the memory consumed */
5397c478bd9Sstevel@tonic-gate free(acl_str);
5407c478bd9Sstevel@tonic-gate free(quoted_name);
5417c478bd9Sstevel@tonic-gate
5427c478bd9Sstevel@tonic-gate return (err_code);
5437c478bd9Sstevel@tonic-gate }
5447c478bd9Sstevel@tonic-gate
5457c478bd9Sstevel@tonic-gate /*
5467c478bd9Sstevel@tonic-gate * When creating a manifest, make sure all '?', tabs, space, newline, '/'
5477c478bd9Sstevel@tonic-gate * and '[' are all properly quoted. Convert them to a "\ooo" where the 'ooo'
5487c478bd9Sstevel@tonic-gate * represents their octal value. For filesystem objects, as opposed to symlink
5497c478bd9Sstevel@tonic-gate * targets, also canonicalize the pathname.
5507c478bd9Sstevel@tonic-gate */
5517c478bd9Sstevel@tonic-gate static char *
sanitized_fname(const char * fname,boolean_t canon_path)5527c478bd9Sstevel@tonic-gate sanitized_fname(const char *fname, boolean_t canon_path)
5537c478bd9Sstevel@tonic-gate {
5547c478bd9Sstevel@tonic-gate const char *ip;
5557c478bd9Sstevel@tonic-gate unsigned char ch;
5567c478bd9Sstevel@tonic-gate char *op, *quoted_name;
5577c478bd9Sstevel@tonic-gate
5587c478bd9Sstevel@tonic-gate /* Initialize everything */
5597c478bd9Sstevel@tonic-gate quoted_name = safe_calloc((4 * PATH_MAX) + 1);
5607c478bd9Sstevel@tonic-gate ip = fname;
5617c478bd9Sstevel@tonic-gate op = quoted_name;
5627c478bd9Sstevel@tonic-gate
5637c478bd9Sstevel@tonic-gate if (canon_path) {
5647c478bd9Sstevel@tonic-gate /*
5657c478bd9Sstevel@tonic-gate * In the case when a relocatable root was used, the relocatable
5667c478bd9Sstevel@tonic-gate * root should *not* be part of the manifest.
5677c478bd9Sstevel@tonic-gate */
5687c478bd9Sstevel@tonic-gate ip += strlen(reloc_root);
5697c478bd9Sstevel@tonic-gate
5707c478bd9Sstevel@tonic-gate /*
5717c478bd9Sstevel@tonic-gate * In the case when the '-I' option was used, make sure
5727c478bd9Sstevel@tonic-gate * the quoted_name starts with a '/'.
5737c478bd9Sstevel@tonic-gate */
5747c478bd9Sstevel@tonic-gate if (*ip != '/')
5757c478bd9Sstevel@tonic-gate *op++ = '/';
5767c478bd9Sstevel@tonic-gate }
5777c478bd9Sstevel@tonic-gate
5787c478bd9Sstevel@tonic-gate /* Now walk through 'fname' and build the quoted string */
5797c478bd9Sstevel@tonic-gate while ((ch = *ip++) != 0) {
5807c478bd9Sstevel@tonic-gate switch (ch) {
5817c478bd9Sstevel@tonic-gate /* Quote the following characters */
5827c478bd9Sstevel@tonic-gate case ' ':
5837c478bd9Sstevel@tonic-gate case '*':
5847c478bd9Sstevel@tonic-gate case '\n':
5857c478bd9Sstevel@tonic-gate case '?':
5867c478bd9Sstevel@tonic-gate case '[':
5877c478bd9Sstevel@tonic-gate case '\\':
5887c478bd9Sstevel@tonic-gate case '\t':
5897c478bd9Sstevel@tonic-gate op += sprintf(op, "\\%.3o", (unsigned char)ch);
5907c478bd9Sstevel@tonic-gate break;
5917c478bd9Sstevel@tonic-gate
5927c478bd9Sstevel@tonic-gate /* Otherwise, simply append them */
5937c478bd9Sstevel@tonic-gate default:
5947c478bd9Sstevel@tonic-gate *op++ = ch;
5957c478bd9Sstevel@tonic-gate break;
5967c478bd9Sstevel@tonic-gate }
5977c478bd9Sstevel@tonic-gate }
5987c478bd9Sstevel@tonic-gate
5997c478bd9Sstevel@tonic-gate *op = 0;
6007c478bd9Sstevel@tonic-gate
6017c478bd9Sstevel@tonic-gate return (quoted_name);
6027c478bd9Sstevel@tonic-gate }
6037c478bd9Sstevel@tonic-gate
6047c478bd9Sstevel@tonic-gate /*
6057c478bd9Sstevel@tonic-gate * Function responsible for generating the ACL information for a given
6067c478bd9Sstevel@tonic-gate * file. Note, the string is put into buffer malloc'd by this function.
607a2721256SWilliam Young * It's the responsibility of the caller to free the buffer. This function
608a2721256SWilliam Young * should never return a NULL pointer.
6097c478bd9Sstevel@tonic-gate */
6107c478bd9Sstevel@tonic-gate static char *
get_acl_string(const char * fname,const struct stat64 * statb,int * err_code)6117c478bd9Sstevel@tonic-gate get_acl_string(const char *fname, const struct stat64 *statb, int *err_code)
6127c478bd9Sstevel@tonic-gate {
613fa9e4066Sahrens acl_t *aclp;
614fa9e4066Sahrens char *acltext;
615fa9e4066Sahrens int error;
6167c478bd9Sstevel@tonic-gate
6177c478bd9Sstevel@tonic-gate if (S_ISLNK(statb->st_mode)) {
6187c478bd9Sstevel@tonic-gate return (safe_strdup("-"));
6197c478bd9Sstevel@tonic-gate }
6207c478bd9Sstevel@tonic-gate
6217c478bd9Sstevel@tonic-gate /*
622fa9e4066Sahrens * Include trivial acl's
6237c478bd9Sstevel@tonic-gate */
624fa9e4066Sahrens error = acl_get(fname, 0, &aclp);
625fa9e4066Sahrens
626fa9e4066Sahrens if (error != 0) {
6277c478bd9Sstevel@tonic-gate *err_code = WARNING_EXIT;
628fa9e4066Sahrens (void) fprintf(stderr, "%s: %s\n", fname, acl_strerror(error));
6297c478bd9Sstevel@tonic-gate return (safe_strdup("-"));
630fa9e4066Sahrens } else {
6315a5eeccaSmarks acltext = acl_totext(aclp, 0);
632fa9e4066Sahrens acl_free(aclp);
633a2721256SWilliam Young if (acltext == NULL)
634a2721256SWilliam Young return (safe_strdup("-"));
635a2721256SWilliam Young else
636fa9e4066Sahrens return (acltext);
6377c478bd9Sstevel@tonic-gate }
6387c478bd9Sstevel@tonic-gate }
6397c478bd9Sstevel@tonic-gate
6407c478bd9Sstevel@tonic-gate
6417c478bd9Sstevel@tonic-gate /*
6427c478bd9Sstevel@tonic-gate *
6437c478bd9Sstevel@tonic-gate * description: This routine reads stdin in BUF_SIZE chunks, uses the bits
6447c478bd9Sstevel@tonic-gate * to update the md5 hash buffer, and outputs the chunks
6457c478bd9Sstevel@tonic-gate * to stdout. When stdin is exhausted, the hash is computed,
6467c478bd9Sstevel@tonic-gate * converted to a hexadecimal string, and returned.
6477c478bd9Sstevel@tonic-gate *
6487c478bd9Sstevel@tonic-gate * returns: The md5 hash of stdin, or NULL if unsuccessful for any reason.
6497c478bd9Sstevel@tonic-gate */
6507c478bd9Sstevel@tonic-gate static int
generate_hash(int fdin,char * hash_str)6517c478bd9Sstevel@tonic-gate generate_hash(int fdin, char *hash_str)
6527c478bd9Sstevel@tonic-gate {
6537c478bd9Sstevel@tonic-gate unsigned char buf[BUF_SIZE];
6547c478bd9Sstevel@tonic-gate unsigned char hash[MD5_DIGEST_LENGTH];
6557c478bd9Sstevel@tonic-gate int i, amtread;
6567c478bd9Sstevel@tonic-gate MD5_CTX ctx;
6577c478bd9Sstevel@tonic-gate
6587c478bd9Sstevel@tonic-gate MD5Init(&ctx);
6597c478bd9Sstevel@tonic-gate
6607c478bd9Sstevel@tonic-gate for (;;) {
6617c478bd9Sstevel@tonic-gate amtread = read(fdin, buf, sizeof (buf));
6627c478bd9Sstevel@tonic-gate if (amtread == 0)
6637c478bd9Sstevel@tonic-gate break;
6647c478bd9Sstevel@tonic-gate if (amtread < 0)
6657c478bd9Sstevel@tonic-gate return (1);
6667c478bd9Sstevel@tonic-gate
6677c478bd9Sstevel@tonic-gate /* got some data. Now update hash */
6687c478bd9Sstevel@tonic-gate MD5Update(&ctx, buf, amtread);
6697c478bd9Sstevel@tonic-gate }
6707c478bd9Sstevel@tonic-gate
6717c478bd9Sstevel@tonic-gate /* done passing through data, calculate hash */
6727c478bd9Sstevel@tonic-gate MD5Final(hash, &ctx);
6737c478bd9Sstevel@tonic-gate
6747c478bd9Sstevel@tonic-gate for (i = 0; i < MD5_DIGEST_LENGTH; i++)
6757c478bd9Sstevel@tonic-gate (void) sprintf(hash_str + (i*2), "%2.2x", hash[i]);
6767c478bd9Sstevel@tonic-gate
6777c478bd9Sstevel@tonic-gate return (0);
6787c478bd9Sstevel@tonic-gate }
6797c478bd9Sstevel@tonic-gate
6807c478bd9Sstevel@tonic-gate /*
6817c478bd9Sstevel@tonic-gate * Used by 'bart create' with the '-I' option. Return each entry into a 'buf'
6827c478bd9Sstevel@tonic-gate * with the appropriate exit code: '0' for success and '-1' for failure.
6837c478bd9Sstevel@tonic-gate */
6847c478bd9Sstevel@tonic-gate static int
read_filelist(char * reloc_root,char ** argv,char * buf,size_t bufsize)6857c478bd9Sstevel@tonic-gate read_filelist(char *reloc_root, char **argv, char *buf, size_t bufsize)
6867c478bd9Sstevel@tonic-gate {
6877c478bd9Sstevel@tonic-gate static int argv_index = -1;
6887c478bd9Sstevel@tonic-gate static boolean_t read_stdinput = B_FALSE;
6897c478bd9Sstevel@tonic-gate char temp_buf[PATH_MAX];
6907c478bd9Sstevel@tonic-gate char *cp;
6917c478bd9Sstevel@tonic-gate
6927c478bd9Sstevel@tonic-gate /*
6937c478bd9Sstevel@tonic-gate * INITIALIZATION:
6947c478bd9Sstevel@tonic-gate * Setup this code so it knows whether or not to read sdtin.
6957c478bd9Sstevel@tonic-gate * Also, if reading from argv, setup the index, "argv_index"
6967c478bd9Sstevel@tonic-gate */
6977c478bd9Sstevel@tonic-gate if (argv_index == -1) {
6987c478bd9Sstevel@tonic-gate argv_index = 0;
6997c478bd9Sstevel@tonic-gate
7007c478bd9Sstevel@tonic-gate /* In this case, no args after '-I', so read stdin */
7017c478bd9Sstevel@tonic-gate if (argv[0] == NULL)
7027c478bd9Sstevel@tonic-gate read_stdinput = B_TRUE;
7037c478bd9Sstevel@tonic-gate }
7047c478bd9Sstevel@tonic-gate
7057c478bd9Sstevel@tonic-gate buf[0] = '\0';
7067c478bd9Sstevel@tonic-gate
7077c478bd9Sstevel@tonic-gate if (read_stdinput) {
7087c478bd9Sstevel@tonic-gate if (fgets(temp_buf, PATH_MAX, stdin) == NULL)
7097c478bd9Sstevel@tonic-gate return (-1);
7107c478bd9Sstevel@tonic-gate cp = strtok(temp_buf, "\n");
7117c478bd9Sstevel@tonic-gate } else {
7127c478bd9Sstevel@tonic-gate cp = argv[argv_index++];
7137c478bd9Sstevel@tonic-gate }
7147c478bd9Sstevel@tonic-gate
7157c478bd9Sstevel@tonic-gate if (cp == NULL)
7167c478bd9Sstevel@tonic-gate return (-1);
7177c478bd9Sstevel@tonic-gate
7187c478bd9Sstevel@tonic-gate /*
7197c478bd9Sstevel@tonic-gate * Unlike similar code elsewhere, avoid adding a leading
7207c478bd9Sstevel@tonic-gate * slash for relative pathnames.
7217c478bd9Sstevel@tonic-gate */
7227c478bd9Sstevel@tonic-gate (void) snprintf(buf, bufsize,
7237c478bd9Sstevel@tonic-gate (reloc_root[0] == '\0' || cp[0] == '/') ? "%s%s" : "%s/%s",
7247c478bd9Sstevel@tonic-gate reloc_root, cp);
7257c478bd9Sstevel@tonic-gate
7267c478bd9Sstevel@tonic-gate return (0);
7277c478bd9Sstevel@tonic-gate }
728