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