xref: /titanic_52/usr/src/cmd/svc/mfstscan/mfstscan.c (revision 24da5b34f49324ed742a340010ed5bd3d4e06625)
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 *suffix_match;
69 
70 	if (ftw_type != FTW_F)
71 		return (0);
72 
73 	suffix_match = strstr(fn, ".xml");
74 	if (suffix_match == NULL || strcmp(suffix_match, ".xml") != 0)
75 		return (0);
76 
77 	if (mhash_test_file(hndl, fn, 0, NULL, NULL) == MHASH_NEWFILE)
78 		(void) printf("%s\n", fn);
79 
80 	return (0);
81 }
82 
83 int
84 main(int argc, char *argv[])
85 {
86 	int i;
87 	int paths_walked = 0;
88 	struct stat sb;
89 
90 	(void) uu_setpname(argv[0]);
91 
92 	while ((i = getopt(argc, argv, "t")) != -1) {
93 		switch (i) {
94 		case 't':
95 			tflag = 1;
96 			paths_walked = 1;
97 			break;
98 		case '?':
99 		default:
100 			usage();
101 			/*NOTREACHED*/
102 		}
103 	}
104 
105 	if (optind >= argc)
106 		usage();
107 
108 	hndl = scf_handle_create(SCF_VERSION);
109 
110 	if (scf_handle_bind(hndl) != SCF_SUCCESS)
111 		uu_die(gettext("cannot bind to repository: %s\n"),
112 		    scf_strerror(scf_error()));
113 
114 	for (i = optind; i < argc; i++) {
115 		if (tflag) {
116 			char *pname = mhash_filename_to_propname(argv[i]);
117 
118 			if (pname != NULL)
119 				(void) puts(pname);
120 			else
121 				uu_warn(gettext("cannot resolve pathname "
122 				    "for %s"), argv[i]);
123 
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