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