xref: /titanic_50/usr/src/cmd/svc/mfstscan/mfstscan.c (revision 70cbfe41f2338b77c15f79c6625eca6e70c412f3)
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
5f1f71133Sbustos  * Common Development and Distribution License (the "License").
6f1f71133Sbustos  * 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*70cbfe41SPhilippe Jung  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #include <sys/types.h>
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate #include <ftw.h>
307c478bd9Sstevel@tonic-gate #include <libintl.h>
317c478bd9Sstevel@tonic-gate #include <libscf.h>
327c478bd9Sstevel@tonic-gate #include <libuutil.h>
337c478bd9Sstevel@tonic-gate #include <locale.h>
347c478bd9Sstevel@tonic-gate #include <stdio.h>
357c478bd9Sstevel@tonic-gate #include <stdlib.h>
367c478bd9Sstevel@tonic-gate #include <string.h>
377c478bd9Sstevel@tonic-gate #include <unistd.h>
387c478bd9Sstevel@tonic-gate 
397c478bd9Sstevel@tonic-gate #include "manifest_hash.h"
407c478bd9Sstevel@tonic-gate 
417c478bd9Sstevel@tonic-gate #define	MAX_DEPTH	24
427c478bd9Sstevel@tonic-gate 
437c478bd9Sstevel@tonic-gate static scf_handle_t *hndl;
447c478bd9Sstevel@tonic-gate static int tflag;
457c478bd9Sstevel@tonic-gate 
467c478bd9Sstevel@tonic-gate /*
477c478bd9Sstevel@tonic-gate  * mfstscan - service manifest change detection utility
487c478bd9Sstevel@tonic-gate  *
497c478bd9Sstevel@tonic-gate  * mfstscan walks the given filesystem hierarchies, and reports those manifests
507c478bd9Sstevel@tonic-gate  * with changed or absent hash entries.  Manifests are expected to end with a
517c478bd9Sstevel@tonic-gate  * .xml suffix--other files will be ignored.
527c478bd9Sstevel@tonic-gate  */
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate static void
557c478bd9Sstevel@tonic-gate usage()
567c478bd9Sstevel@tonic-gate {
577c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, gettext("Usage: %s [-t] path ...\n"),
587c478bd9Sstevel@tonic-gate 	    uu_getpname());
597c478bd9Sstevel@tonic-gate 	exit(UU_EXIT_USAGE);
607c478bd9Sstevel@tonic-gate }
617c478bd9Sstevel@tonic-gate 
627c478bd9Sstevel@tonic-gate /*ARGSUSED*/
637c478bd9Sstevel@tonic-gate static int
647c478bd9Sstevel@tonic-gate process(const char *fn, const struct stat *sp, int ftw_type,
657c478bd9Sstevel@tonic-gate     struct FTW *ftws)
667c478bd9Sstevel@tonic-gate {
677c478bd9Sstevel@tonic-gate 	char *suffix_match;
687c478bd9Sstevel@tonic-gate 
697c478bd9Sstevel@tonic-gate 	if (ftw_type != FTW_F)
707c478bd9Sstevel@tonic-gate 		return (0);
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate 	suffix_match = strstr(fn, ".xml");
737c478bd9Sstevel@tonic-gate 	if (suffix_match == NULL || strcmp(suffix_match, ".xml") != 0)
747c478bd9Sstevel@tonic-gate 		return (0);
757c478bd9Sstevel@tonic-gate 
76449975fdScasper 	if (mhash_test_file(hndl, fn, 0, NULL, NULL) == MHASH_NEWFILE)
777c478bd9Sstevel@tonic-gate 		(void) printf("%s\n", fn);
787c478bd9Sstevel@tonic-gate 
797c478bd9Sstevel@tonic-gate 	return (0);
807c478bd9Sstevel@tonic-gate }
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate int
837c478bd9Sstevel@tonic-gate main(int argc, char *argv[])
847c478bd9Sstevel@tonic-gate {
857c478bd9Sstevel@tonic-gate 	int i;
867c478bd9Sstevel@tonic-gate 	int paths_walked = 0;
877c478bd9Sstevel@tonic-gate 	struct stat sb;
887c478bd9Sstevel@tonic-gate 
897c478bd9Sstevel@tonic-gate 	(void) uu_setpname(argv[0]);
907c478bd9Sstevel@tonic-gate 
917c478bd9Sstevel@tonic-gate 	while ((i = getopt(argc, argv, "t")) != -1) {
927c478bd9Sstevel@tonic-gate 		switch (i) {
937c478bd9Sstevel@tonic-gate 		case 't':
947c478bd9Sstevel@tonic-gate 			tflag = 1;
957c478bd9Sstevel@tonic-gate 			paths_walked = 1;
967c478bd9Sstevel@tonic-gate 			break;
977c478bd9Sstevel@tonic-gate 		case '?':
987c478bd9Sstevel@tonic-gate 		default:
997c478bd9Sstevel@tonic-gate 			usage();
1007c478bd9Sstevel@tonic-gate 			/*NOTREACHED*/
1017c478bd9Sstevel@tonic-gate 		}
1027c478bd9Sstevel@tonic-gate 	}
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate 	if (optind >= argc)
1057c478bd9Sstevel@tonic-gate 		usage();
1067c478bd9Sstevel@tonic-gate 
1077c478bd9Sstevel@tonic-gate 	hndl = scf_handle_create(SCF_VERSION);
1087c478bd9Sstevel@tonic-gate 
1097c478bd9Sstevel@tonic-gate 	if (scf_handle_bind(hndl) != SCF_SUCCESS)
1107c478bd9Sstevel@tonic-gate 		uu_die(gettext("cannot bind to repository: %s\n"),
1117c478bd9Sstevel@tonic-gate 		    scf_strerror(scf_error()));
1127c478bd9Sstevel@tonic-gate 
1137c478bd9Sstevel@tonic-gate 	for (i = optind; i < argc; i++) {
1147c478bd9Sstevel@tonic-gate 		if (tflag) {
115*70cbfe41SPhilippe Jung 			char *pname = mhash_filename_to_propname(argv[i],
116*70cbfe41SPhilippe Jung 			    B_FALSE);
117f1f71133Sbustos 
118f1f71133Sbustos 			if (pname != NULL)
119f1f71133Sbustos 				(void) puts(pname);
120f1f71133Sbustos 			else
121f1f71133Sbustos 				uu_warn(gettext("cannot resolve pathname "
122f1f71133Sbustos 				    "for %s"), argv[i]);
123f1f71133Sbustos 
1247c478bd9Sstevel@tonic-gate 			continue;
1257c478bd9Sstevel@tonic-gate 		}
1267c478bd9Sstevel@tonic-gate 
1277c478bd9Sstevel@tonic-gate 		if (stat(argv[i], &sb) == -1) {
1287c478bd9Sstevel@tonic-gate 			uu_warn(gettext("cannot stat %s"), argv[i]);
1297c478bd9Sstevel@tonic-gate 			continue;
1307c478bd9Sstevel@tonic-gate 		}
1317c478bd9Sstevel@tonic-gate 
1327c478bd9Sstevel@tonic-gate 		if (nftw(argv[i], process, MAX_DEPTH, FTW_MOUNT) == -1)
1337c478bd9Sstevel@tonic-gate 			uu_warn(gettext("file tree walk of %s encountered "
1347c478bd9Sstevel@tonic-gate 			    "error"), argv[i]);
1357c478bd9Sstevel@tonic-gate 		else
1367c478bd9Sstevel@tonic-gate 			paths_walked++;
1377c478bd9Sstevel@tonic-gate 	}
1387c478bd9Sstevel@tonic-gate 
1397c478bd9Sstevel@tonic-gate 	(void) scf_handle_unbind(hndl);
1407c478bd9Sstevel@tonic-gate 	(void) scf_handle_destroy(hndl);
1417c478bd9Sstevel@tonic-gate 
1427c478bd9Sstevel@tonic-gate 	if (!paths_walked)
1437c478bd9Sstevel@tonic-gate 		uu_die(gettext("no paths walked\n"));
1447c478bd9Sstevel@tonic-gate 
1457c478bd9Sstevel@tonic-gate 	return (0);
1467c478bd9Sstevel@tonic-gate }
147