xref: /titanic_50/usr/src/cmd/svc/mfstscan/mfstscan.c (revision 910cba4f2f1e94daf355ee8635285732ac47326c)
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 2006 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 #include <sys/types.h>
29 
30 #include <ftw.h>
31 #include <libintl.h>
32 #include <libscf.h>
33 #include <libuutil.h>
34 #include <locale.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <unistd.h>
39 
40 #include "manifest_hash.h"
41 
42 #define	MAX_DEPTH	24
43 
44 static scf_handle_t *hndl;
45 static int tflag;
46 
47 /*
48  * mfstscan - service manifest change detection utility
49  *
50  * mfstscan walks the given filesystem hierarchies, and reports those manifests
51  * with changed or absent hash entries.  Manifests are expected to end with a
52  * .xml suffix--other files will be ignored.
53  */
54 
55 static void
56 usage()
57 {
58 	(void) fprintf(stderr, gettext("Usage: %s [-t] path ...\n"),
59 	    uu_getpname());
60 	exit(UU_EXIT_USAGE);
61 }
62 
63 /*ARGSUSED*/
64 static int
65 process(const char *fn, const struct stat *sp, int ftw_type,
66     struct FTW *ftws)
67 {
68 	char *name;
69 	char *suffix_match;
70 
71 	uchar_t hash[MHASH_SIZE];
72 
73 	if (ftw_type != FTW_F)
74 		return (0);
75 
76 	suffix_match = strstr(fn, ".xml");
77 	if (suffix_match == NULL || strcmp(suffix_match, ".xml") != 0)
78 		return (0);
79 
80 	name = mhash_filename_to_propname(fn);
81 
82 	if (mhash_retrieve_entry(hndl, name, hash) == -1 ||
83 	    mhash_test_file(hndl, fn, 0, &name, hash) == 0)
84 		(void) printf("%s\n", fn);
85 
86 	return (0);
87 }
88 
89 int
90 main(int argc, char *argv[])
91 {
92 	int i;
93 	int paths_walked = 0;
94 	struct stat sb;
95 
96 	(void) uu_setpname(argv[0]);
97 
98 	while ((i = getopt(argc, argv, "t")) != -1) {
99 		switch (i) {
100 		case 't':
101 			tflag = 1;
102 			paths_walked = 1;
103 			break;
104 		case '?':
105 		default:
106 			usage();
107 			/*NOTREACHED*/
108 		}
109 	}
110 
111 	if (optind >= argc)
112 		usage();
113 
114 	hndl = scf_handle_create(SCF_VERSION);
115 
116 	if (scf_handle_bind(hndl) != SCF_SUCCESS)
117 		uu_die(gettext("cannot bind to repository: %s\n"),
118 		    scf_strerror(scf_error()));
119 
120 	for (i = optind; i < argc; i++) {
121 		if (tflag) {
122 			char *pname = mhash_filename_to_propname(argv[i]);
123 
124 			if (pname != NULL)
125 				(void) puts(pname);
126 			else
127 				uu_warn(gettext("cannot resolve pathname "
128 				    "for %s"), argv[i]);
129 
130 			continue;
131 		}
132 
133 		if (stat(argv[i], &sb) == -1) {
134 			uu_warn(gettext("cannot stat %s"), argv[i]);
135 			continue;
136 		}
137 
138 		if (nftw(argv[i], process, MAX_DEPTH, FTW_MOUNT) == -1)
139 			uu_warn(gettext("file tree walk of %s encountered "
140 			    "error"), argv[i]);
141 		else
142 			paths_walked++;
143 	}
144 
145 	(void) scf_handle_unbind(hndl);
146 	(void) scf_handle_destroy(hndl);
147 
148 	if (!paths_walked)
149 		uu_die(gettext("no paths walked\n"));
150 
151 	return (0);
152 }
153