xref: /titanic_51/usr/src/cmd/svc/mfstscan/mfstscan.c (revision f1f711336964964c391c797fdd56f4b3904e811d)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*f1f71133Sbustos  * Common Development and Distribution License (the "License").
6*f1f71133Sbustos  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22*f1f71133Sbustos  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate #include <sys/types.h>
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate #include <ftw.h>
317c478bd9Sstevel@tonic-gate #include <libintl.h>
327c478bd9Sstevel@tonic-gate #include <libscf.h>
337c478bd9Sstevel@tonic-gate #include <libuutil.h>
347c478bd9Sstevel@tonic-gate #include <locale.h>
357c478bd9Sstevel@tonic-gate #include <stdio.h>
367c478bd9Sstevel@tonic-gate #include <stdlib.h>
377c478bd9Sstevel@tonic-gate #include <string.h>
387c478bd9Sstevel@tonic-gate #include <unistd.h>
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate #include "manifest_hash.h"
417c478bd9Sstevel@tonic-gate 
427c478bd9Sstevel@tonic-gate #define	MAX_DEPTH	24
437c478bd9Sstevel@tonic-gate 
447c478bd9Sstevel@tonic-gate static scf_handle_t *hndl;
457c478bd9Sstevel@tonic-gate static int tflag;
467c478bd9Sstevel@tonic-gate 
477c478bd9Sstevel@tonic-gate /*
487c478bd9Sstevel@tonic-gate  * mfstscan - service manifest change detection utility
497c478bd9Sstevel@tonic-gate  *
507c478bd9Sstevel@tonic-gate  * mfstscan walks the given filesystem hierarchies, and reports those manifests
517c478bd9Sstevel@tonic-gate  * with changed or absent hash entries.  Manifests are expected to end with a
527c478bd9Sstevel@tonic-gate  * .xml suffix--other files will be ignored.
537c478bd9Sstevel@tonic-gate  */
547c478bd9Sstevel@tonic-gate 
557c478bd9Sstevel@tonic-gate static void
567c478bd9Sstevel@tonic-gate usage()
577c478bd9Sstevel@tonic-gate {
587c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, gettext("Usage: %s [-t] path ...\n"),
597c478bd9Sstevel@tonic-gate 	    uu_getpname());
607c478bd9Sstevel@tonic-gate 	exit(UU_EXIT_USAGE);
617c478bd9Sstevel@tonic-gate }
627c478bd9Sstevel@tonic-gate 
637c478bd9Sstevel@tonic-gate /*ARGSUSED*/
647c478bd9Sstevel@tonic-gate static int
657c478bd9Sstevel@tonic-gate process(const char *fn, const struct stat *sp, int ftw_type,
667c478bd9Sstevel@tonic-gate     struct FTW *ftws)
677c478bd9Sstevel@tonic-gate {
687c478bd9Sstevel@tonic-gate 	char *name;
697c478bd9Sstevel@tonic-gate 	char *suffix_match;
707c478bd9Sstevel@tonic-gate 
717c478bd9Sstevel@tonic-gate 	uchar_t hash[MHASH_SIZE];
727c478bd9Sstevel@tonic-gate 
737c478bd9Sstevel@tonic-gate 	if (ftw_type != FTW_F)
747c478bd9Sstevel@tonic-gate 		return (0);
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate 	suffix_match = strstr(fn, ".xml");
777c478bd9Sstevel@tonic-gate 	if (suffix_match == NULL || strcmp(suffix_match, ".xml") != 0)
787c478bd9Sstevel@tonic-gate 		return (0);
797c478bd9Sstevel@tonic-gate 
807c478bd9Sstevel@tonic-gate 	name = mhash_filename_to_propname(fn);
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate 	if (mhash_retrieve_entry(hndl, name, hash) == -1 ||
837c478bd9Sstevel@tonic-gate 	    mhash_test_file(hndl, fn, 0, &name, hash) == 0)
847c478bd9Sstevel@tonic-gate 		(void) printf("%s\n", fn);
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate 	return (0);
877c478bd9Sstevel@tonic-gate }
887c478bd9Sstevel@tonic-gate 
897c478bd9Sstevel@tonic-gate int
907c478bd9Sstevel@tonic-gate main(int argc, char *argv[])
917c478bd9Sstevel@tonic-gate {
927c478bd9Sstevel@tonic-gate 	int i;
937c478bd9Sstevel@tonic-gate 	int paths_walked = 0;
947c478bd9Sstevel@tonic-gate 	struct stat sb;
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate 	(void) uu_setpname(argv[0]);
977c478bd9Sstevel@tonic-gate 
987c478bd9Sstevel@tonic-gate 	while ((i = getopt(argc, argv, "t")) != -1) {
997c478bd9Sstevel@tonic-gate 		switch (i) {
1007c478bd9Sstevel@tonic-gate 		case 't':
1017c478bd9Sstevel@tonic-gate 			tflag = 1;
1027c478bd9Sstevel@tonic-gate 			paths_walked = 1;
1037c478bd9Sstevel@tonic-gate 			break;
1047c478bd9Sstevel@tonic-gate 		case '?':
1057c478bd9Sstevel@tonic-gate 		default:
1067c478bd9Sstevel@tonic-gate 			usage();
1077c478bd9Sstevel@tonic-gate 			/*NOTREACHED*/
1087c478bd9Sstevel@tonic-gate 		}
1097c478bd9Sstevel@tonic-gate 	}
1107c478bd9Sstevel@tonic-gate 
1117c478bd9Sstevel@tonic-gate 	if (optind >= argc)
1127c478bd9Sstevel@tonic-gate 		usage();
1137c478bd9Sstevel@tonic-gate 
1147c478bd9Sstevel@tonic-gate 	hndl = scf_handle_create(SCF_VERSION);
1157c478bd9Sstevel@tonic-gate 
1167c478bd9Sstevel@tonic-gate 	if (scf_handle_bind(hndl) != SCF_SUCCESS)
1177c478bd9Sstevel@tonic-gate 		uu_die(gettext("cannot bind to repository: %s\n"),
1187c478bd9Sstevel@tonic-gate 		    scf_strerror(scf_error()));
1197c478bd9Sstevel@tonic-gate 
1207c478bd9Sstevel@tonic-gate 	for (i = optind; i < argc; i++) {
1217c478bd9Sstevel@tonic-gate 		if (tflag) {
122*f1f71133Sbustos 			char *pname = mhash_filename_to_propname(argv[i]);
123*f1f71133Sbustos 
124*f1f71133Sbustos 			if (pname != NULL)
125*f1f71133Sbustos 				(void) puts(pname);
126*f1f71133Sbustos 			else
127*f1f71133Sbustos 				uu_warn(gettext("cannot resolve pathname "
128*f1f71133Sbustos 				    "for %s"), argv[i]);
129*f1f71133Sbustos 
1307c478bd9Sstevel@tonic-gate 			continue;
1317c478bd9Sstevel@tonic-gate 		}
1327c478bd9Sstevel@tonic-gate 
1337c478bd9Sstevel@tonic-gate 		if (stat(argv[i], &sb) == -1) {
1347c478bd9Sstevel@tonic-gate 			uu_warn(gettext("cannot stat %s"), argv[i]);
1357c478bd9Sstevel@tonic-gate 			continue;
1367c478bd9Sstevel@tonic-gate 		}
1377c478bd9Sstevel@tonic-gate 
1387c478bd9Sstevel@tonic-gate 		if (nftw(argv[i], process, MAX_DEPTH, FTW_MOUNT) == -1)
1397c478bd9Sstevel@tonic-gate 			uu_warn(gettext("file tree walk of %s encountered "
1407c478bd9Sstevel@tonic-gate 			    "error"), argv[i]);
1417c478bd9Sstevel@tonic-gate 		else
1427c478bd9Sstevel@tonic-gate 			paths_walked++;
1437c478bd9Sstevel@tonic-gate 	}
1447c478bd9Sstevel@tonic-gate 
1457c478bd9Sstevel@tonic-gate 	(void) scf_handle_unbind(hndl);
1467c478bd9Sstevel@tonic-gate 	(void) scf_handle_destroy(hndl);
1477c478bd9Sstevel@tonic-gate 
1487c478bd9Sstevel@tonic-gate 	if (!paths_walked)
1497c478bd9Sstevel@tonic-gate 		uu_die(gettext("no paths walked\n"));
1507c478bd9Sstevel@tonic-gate 
1517c478bd9Sstevel@tonic-gate 	return (0);
1527c478bd9Sstevel@tonic-gate }
153