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