xref: /titanic_51/usr/src/cmd/svc/mfstscan/mfstscan.c (revision 293e3ab30a546cd932ee22f9d54b3979fcb52095)
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*293e3ab3STruong Q. Nguyen /*
23*293e3ab3STruong Q. Nguyen  * Copyright (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved.
24*293e3ab3STruong Q. Nguyen  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #include <sys/types.h>
277c478bd9Sstevel@tonic-gate 
289444c26fSTom Whitten #include <errno.h>
299444c26fSTom Whitten #include <fcntl.h>
307c478bd9Sstevel@tonic-gate #include <libintl.h>
31*293e3ab3STruong Q. Nguyen #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>
379444c26fSTom Whitten #include <sys/stat.h>
387c478bd9Sstevel@tonic-gate #include <unistd.h>
397c478bd9Sstevel@tonic-gate 
409444c26fSTom Whitten #include "manifest_find.h"
417c478bd9Sstevel@tonic-gate #include "manifest_hash.h"
427c478bd9Sstevel@tonic-gate 
437c478bd9Sstevel@tonic-gate #define	MAX_DEPTH	24
447c478bd9Sstevel@tonic-gate 
457c478bd9Sstevel@tonic-gate /*
467c478bd9Sstevel@tonic-gate  * mfstscan - service manifest change detection utility
477c478bd9Sstevel@tonic-gate  *
487c478bd9Sstevel@tonic-gate  * mfstscan walks the given filesystem hierarchies, and reports those manifests
497c478bd9Sstevel@tonic-gate  * with changed or absent hash entries.  Manifests are expected to end with a
507c478bd9Sstevel@tonic-gate  * .xml suffix--other files will be ignored.
517c478bd9Sstevel@tonic-gate  */
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate static void
547c478bd9Sstevel@tonic-gate usage()
557c478bd9Sstevel@tonic-gate {
567c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, gettext("Usage: %s [-t] path ...\n"),
577c478bd9Sstevel@tonic-gate 	    uu_getpname());
587c478bd9Sstevel@tonic-gate 	exit(UU_EXIT_USAGE);
597c478bd9Sstevel@tonic-gate }
607c478bd9Sstevel@tonic-gate 
617c478bd9Sstevel@tonic-gate int
627c478bd9Sstevel@tonic-gate main(int argc, char *argv[])
637c478bd9Sstevel@tonic-gate {
649444c26fSTom Whitten 	manifest_info_t **entry;
659444c26fSTom Whitten 	manifest_info_t **manifests;
66*293e3ab3STruong Q. Nguyen 	scf_handle_t *hndl = NULL;
677c478bd9Sstevel@tonic-gate 	int i;
687c478bd9Sstevel@tonic-gate 	int paths_walked = 0;
697c478bd9Sstevel@tonic-gate 	struct stat sb;
709444c26fSTom Whitten 	int status;
719444c26fSTom Whitten 	int tflag = 0;
727c478bd9Sstevel@tonic-gate 
737c478bd9Sstevel@tonic-gate 	(void) uu_setpname(argv[0]);
747c478bd9Sstevel@tonic-gate 
757c478bd9Sstevel@tonic-gate 	while ((i = getopt(argc, argv, "t")) != -1) {
767c478bd9Sstevel@tonic-gate 		switch (i) {
777c478bd9Sstevel@tonic-gate 		case 't':
787c478bd9Sstevel@tonic-gate 			tflag = 1;
797c478bd9Sstevel@tonic-gate 			paths_walked = 1;
807c478bd9Sstevel@tonic-gate 			break;
817c478bd9Sstevel@tonic-gate 		case '?':
827c478bd9Sstevel@tonic-gate 		default:
837c478bd9Sstevel@tonic-gate 			usage();
847c478bd9Sstevel@tonic-gate 			/*NOTREACHED*/
857c478bd9Sstevel@tonic-gate 		}
867c478bd9Sstevel@tonic-gate 	}
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate 	if (optind >= argc)
897c478bd9Sstevel@tonic-gate 		usage();
907c478bd9Sstevel@tonic-gate 
91*293e3ab3STruong Q. Nguyen 	if ((hndl = scf_handle_create(SCF_VERSION)) == NULL)
92*293e3ab3STruong Q. Nguyen 		uu_die(gettext("Unexpected libscf error: %s.  Exiting.\n"),
93*293e3ab3STruong Q. Nguyen 		    scf_strerror(scf_error()));
94*293e3ab3STruong Q. Nguyen 
95*293e3ab3STruong Q. Nguyen 	if (scf_handle_bind(hndl) != SCF_SUCCESS)
96*293e3ab3STruong Q. Nguyen 		uu_die(gettext("Couldn't bind to svc.configd.\n"));
97*293e3ab3STruong Q. Nguyen 
987c478bd9Sstevel@tonic-gate 	for (i = optind; i < argc; i++) {
997c478bd9Sstevel@tonic-gate 		if (tflag) {
10070cbfe41SPhilippe Jung 			char *pname = mhash_filename_to_propname(argv[i],
10170cbfe41SPhilippe Jung 			    B_FALSE);
102f1f71133Sbustos 
103f1f71133Sbustos 			if (pname != NULL)
104f1f71133Sbustos 				(void) puts(pname);
105f1f71133Sbustos 			else
106f1f71133Sbustos 				uu_warn(gettext("cannot resolve pathname "
107f1f71133Sbustos 				    "for %s"), argv[i]);
108f1f71133Sbustos 
1097c478bd9Sstevel@tonic-gate 			continue;
1107c478bd9Sstevel@tonic-gate 		}
1117c478bd9Sstevel@tonic-gate 
1127c478bd9Sstevel@tonic-gate 		if (stat(argv[i], &sb) == -1) {
1137c478bd9Sstevel@tonic-gate 			uu_warn(gettext("cannot stat %s"), argv[i]);
1147c478bd9Sstevel@tonic-gate 			continue;
1157c478bd9Sstevel@tonic-gate 		}
1167c478bd9Sstevel@tonic-gate 
117*293e3ab3STruong Q. Nguyen 		status = find_manifests(hndl, argv[i], &manifests,
1189444c26fSTom Whitten 		    CHECKHASH|CHECKEXT);
1199444c26fSTom Whitten 		if (status < 0) {
1207c478bd9Sstevel@tonic-gate 			uu_warn(gettext("file tree walk of %s encountered "
1219444c26fSTom Whitten 			    "error.  %s\n"), argv[i], strerror(errno));
1229444c26fSTom Whitten 		} else {
1237c478bd9Sstevel@tonic-gate 			paths_walked++;
1249444c26fSTom Whitten 			if (manifests != NULL) {
1259444c26fSTom Whitten 				for (entry = manifests;
1269444c26fSTom Whitten 				    *entry != NULL;
1279444c26fSTom Whitten 				    entry++) {
1289444c26fSTom Whitten 					(void) printf("%s\n",
1299444c26fSTom Whitten 					    (*entry)->mi_path);
1309444c26fSTom Whitten 				}
1319444c26fSTom Whitten 				free_manifest_array(manifests);
1329444c26fSTom Whitten 			}
1337c478bd9Sstevel@tonic-gate 		}
1347c478bd9Sstevel@tonic-gate 
1359444c26fSTom Whitten 	}
1367c478bd9Sstevel@tonic-gate 
1377c478bd9Sstevel@tonic-gate 	if (!paths_walked)
1387c478bd9Sstevel@tonic-gate 		uu_die(gettext("no paths walked\n"));
1397c478bd9Sstevel@tonic-gate 
140*293e3ab3STruong Q. Nguyen 	if (hndl != NULL) {
141*293e3ab3STruong Q. Nguyen 		(void) scf_handle_unbind(hndl);
142*293e3ab3STruong Q. Nguyen 		(void) scf_handle_destroy(hndl);
143*293e3ab3STruong Q. Nguyen 	}
144*293e3ab3STruong Q. Nguyen 
1457c478bd9Sstevel@tonic-gate 	return (0);
1467c478bd9Sstevel@tonic-gate }
147