xref: /titanic_50/usr/src/cmd/svc/mfstscan/mfstscan.c (revision b56bf881a9655cb27b53cba1468312f7c6dfb0a2)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 
27 #include <sys/types.h>
28 
29 #include <errno.h>
30 #include <fcntl.h>
31 #include <libintl.h>
32 #include <libuutil.h>
33 #include <locale.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <sys/stat.h>
38 #include <unistd.h>
39 
40 #include "manifest_find.h"
41 #include "manifest_hash.h"
42 
43 #define	MAX_DEPTH	24
44 
45 /*
46  * mfstscan - service manifest change detection utility
47  *
48  * mfstscan walks the given filesystem hierarchies, and reports those manifests
49  * with changed or absent hash entries.  Manifests are expected to end with a
50  * .xml suffix--other files will be ignored.
51  */
52 
53 static void
54 usage()
55 {
56 	(void) fprintf(stderr, gettext("Usage: %s [-t] path ...\n"),
57 	    uu_getpname());
58 	exit(UU_EXIT_USAGE);
59 }
60 
61 int
62 main(int argc, char *argv[])
63 {
64 	manifest_info_t **entry;
65 	manifest_info_t **manifests;
66 	int i;
67 	int paths_walked = 0;
68 	struct stat sb;
69 	int status;
70 	int tflag = 0;
71 
72 	(void) uu_setpname(argv[0]);
73 
74 	while ((i = getopt(argc, argv, "t")) != -1) {
75 		switch (i) {
76 		case 't':
77 			tflag = 1;
78 			paths_walked = 1;
79 			break;
80 		case '?':
81 		default:
82 			usage();
83 			/*NOTREACHED*/
84 		}
85 	}
86 
87 	if (optind >= argc)
88 		usage();
89 
90 	for (i = optind; i < argc; i++) {
91 		if (tflag) {
92 			char *pname = mhash_filename_to_propname(argv[i],
93 			    B_FALSE);
94 
95 			if (pname != NULL)
96 				(void) puts(pname);
97 			else
98 				uu_warn(gettext("cannot resolve pathname "
99 				    "for %s"), argv[i]);
100 
101 			continue;
102 		}
103 
104 		if (stat(argv[i], &sb) == -1) {
105 			uu_warn(gettext("cannot stat %s"), argv[i]);
106 			continue;
107 		}
108 
109 		status = find_manifests(argv[i], &manifests,
110 		    CHECKHASH|CHECKEXT);
111 		if (status < 0) {
112 			uu_warn(gettext("file tree walk of %s encountered "
113 			    "error.  %s\n"), argv[i], strerror(errno));
114 		} else {
115 			paths_walked++;
116 			if (manifests != NULL) {
117 				for (entry = manifests;
118 				    *entry != NULL;
119 				    entry++) {
120 					(void) printf("%s\n",
121 					    (*entry)->mi_path);
122 				}
123 				free_manifest_array(manifests);
124 			}
125 		}
126 
127 	}
128 
129 	if (!paths_walked)
130 		uu_die(gettext("no paths walked\n"));
131 
132 	return (0);
133 }
134