xref: /titanic_41/usr/src/cmd/svc/mfstscan/mfstscan.c (revision c5024742c2f7d10880eae26cc592353b20a58f4a)
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, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include <sys/types.h>
30 
31 #include <ftw.h>
32 #include <libintl.h>
33 #include <libscf.h>
34 #include <libuutil.h>
35 #include <locale.h>
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <unistd.h>
40 
41 #include "manifest_hash.h"
42 
43 #define	MAX_DEPTH	24
44 
45 static scf_handle_t *hndl;
46 static int tflag;
47 
48 /*
49  * mfstscan - service manifest change detection utility
50  *
51  * mfstscan walks the given filesystem hierarchies, and reports those manifests
52  * with changed or absent hash entries.  Manifests are expected to end with a
53  * .xml suffix--other files will be ignored.
54  */
55 
56 static void
57 usage()
58 {
59 	(void) fprintf(stderr, gettext("Usage: %s [-t] path ...\n"),
60 	    uu_getpname());
61 	exit(UU_EXIT_USAGE);
62 }
63 
64 /*ARGSUSED*/
65 static int
66 process(const char *fn, const struct stat *sp, int ftw_type,
67     struct FTW *ftws)
68 {
69 	char *name;
70 	char *suffix_match;
71 
72 	uchar_t hash[MHASH_SIZE];
73 
74 	if (ftw_type != FTW_F)
75 		return (0);
76 
77 	suffix_match = strstr(fn, ".xml");
78 	if (suffix_match == NULL || strcmp(suffix_match, ".xml") != 0)
79 		return (0);
80 
81 	name = mhash_filename_to_propname(fn);
82 
83 	if (mhash_retrieve_entry(hndl, name, hash) == -1 ||
84 	    mhash_test_file(hndl, fn, 0, &name, hash) == 0)
85 		(void) printf("%s\n", fn);
86 
87 	return (0);
88 }
89 
90 int
91 main(int argc, char *argv[])
92 {
93 	int i;
94 	int paths_walked = 0;
95 	struct stat sb;
96 
97 	(void) uu_setpname(argv[0]);
98 
99 	while ((i = getopt(argc, argv, "t")) != -1) {
100 		switch (i) {
101 		case 't':
102 			tflag = 1;
103 			paths_walked = 1;
104 			break;
105 		case '?':
106 		default:
107 			usage();
108 			/*NOTREACHED*/
109 		}
110 	}
111 
112 	if (optind >= argc)
113 		usage();
114 
115 	hndl = scf_handle_create(SCF_VERSION);
116 
117 	if (scf_handle_bind(hndl) != SCF_SUCCESS)
118 		uu_die(gettext("cannot bind to repository: %s\n"),
119 		    scf_strerror(scf_error()));
120 
121 	for (i = optind; i < argc; i++) {
122 		if (tflag) {
123 			(void) puts(mhash_filename_to_propname(argv[i]));
124 			continue;
125 		}
126 
127 		if (stat(argv[i], &sb) == -1) {
128 			uu_warn(gettext("cannot stat %s"), argv[i]);
129 			continue;
130 		}
131 
132 		if (nftw(argv[i], process, MAX_DEPTH, FTW_MOUNT) == -1)
133 			uu_warn(gettext("file tree walk of %s encountered "
134 			    "error"), argv[i]);
135 		else
136 			paths_walked++;
137 	}
138 
139 	(void) scf_handle_unbind(hndl);
140 	(void) scf_handle_destroy(hndl);
141 
142 	if (!paths_walked)
143 		uu_die(gettext("no paths walked\n"));
144 
145 	return (0);
146 }
147