xref: /titanic_41/usr/src/cmd/fs.d/nfs/showmount/showmount.c (revision 8eea8e29cc4374d1ee24c25a07f45af132db3499)
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, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
28 /*	  All Rights Reserved  	*/
29 
30 /*
31  * University Copyright- Copyright (c) 1982, 1986, 1988
32  * The Regents of the University of California
33  * All Rights Reserved
34  *
35  * University Acknowledgment- Portions of this document are derived from
36  * software developed by the University of California, Berkeley, and its
37  * contributors.
38  */
39 
40 #pragma ident	"%Z%%M%	%I%	%E% SMI"
41 
42 /*
43  * showmount
44  */
45 #include <stdio.h>
46 #include <stdarg.h>
47 #include <rpc/rpc.h>
48 #include <rpc/rpcb_clnt.h>
49 #include <sys/socket.h>
50 #include <netdb.h>
51 #include <sys/time.h>
52 #include <errno.h>
53 #include <nfs/nfs.h>
54 #include <rpcsvc/mount.h>
55 #include <locale.h>
56 
57 int sorthost();
58 int sortpath();
59 void pr_err(char *, ...);
60 void printex();
61 void usage();
62 
63 /*
64  * Dynamically-sized array of pointers to mountlist entries.  Each element
65  * points into the linked list returned by the RPC call.  We use an array
66  * so that we can conveniently sort the entries.
67  */
68 static struct mountbody **table;
69 
70 struct	timeval	rpc_totout_new = {15, 0};
71 
72 main(argc, argv)
73 	int argc;
74 	char **argv;
75 {
76 	int aflg = 0, dflg = 0, eflg = 0;
77 	int err;
78 	struct mountbody *result_list = NULL;
79 	struct mountbody *ml = NULL;
80 	struct mountbody **tb;		/* pointer into table */
81 	char *host, hostbuf[256];
82 	char *last;
83 	CLIENT *cl;
84 	extern int optind;
85 	extern char *optarg;
86 	int c;
87 	struct	timeval	tout, rpc_totout_old;
88 	int	numentries;
89 	(void) setlocale(LC_ALL, "");
90 
91 #if !defined(TEXT_DOMAIN)
92 #define	TEXT_DOMAIN "SYS_TEST"
93 #endif
94 	(void) textdomain(TEXT_DOMAIN);
95 
96 	while ((c = getopt(argc, argv, "ade")) != EOF) {
97 		switch (c) {
98 		case 'a':
99 			aflg++;
100 			break;
101 		case 'd':
102 			dflg++;
103 			break;
104 		case 'e':
105 			eflg++;
106 			break;
107 		default:
108 			usage();
109 			exit(1);
110 		}
111 	}
112 
113 	switch (argc - optind) {
114 	case 0:		/* no args */
115 		if (gethostname(hostbuf, sizeof (hostbuf)) < 0) {
116 			pr_err("gethostname: %s\n", strerror(errno));
117 			exit(1);
118 		}
119 		host = hostbuf;
120 		break;
121 	case 1:		/* one arg */
122 		host = argv[optind];
123 		break;
124 	default:	/* too many args */
125 		usage();
126 		exit(1);
127 	}
128 
129 	__rpc_control(CLCR_GET_RPCB_TIMEOUT, &rpc_totout_old);
130 	__rpc_control(CLCR_SET_RPCB_TIMEOUT, &rpc_totout_new);
131 
132 	/*
133 	 * First try circuit, then drop back to datagram if
134 	 * circuit is unavailable (an old version of mountd perhaps)
135 	 * Using circuit is preferred because it can handle
136 	 * arbitrarily long export lists.
137 	 */
138 	cl = clnt_create(host, MOUNTPROG, MOUNTVERS, "circuit_n");
139 	if (cl == NULL) {
140 		if (rpc_createerr.cf_stat == RPC_PROGNOTREGISTERED)
141 			cl = clnt_create(host, MOUNTPROG, MOUNTVERS,
142 					"datagram_n");
143 		if (cl == NULL) {
144 			pr_err("");
145 			clnt_pcreateerror(host);
146 			__rpc_control(CLCR_SET_RPCB_TIMEOUT, &rpc_totout_old);
147 			exit(1);
148 		}
149 	}
150 
151 	__rpc_control(CLCR_SET_RPCB_TIMEOUT, &rpc_totout_old);
152 
153 	if (eflg) {
154 		printex(cl, host);
155 		if (aflg + dflg == 0) {
156 			exit(0);
157 		}
158 	}
159 
160 	tout.tv_sec = 10;
161 	tout.tv_usec = 0;
162 
163 	if (err = clnt_call(cl, MOUNTPROC_DUMP,
164 			    xdr_void, 0, xdr_mountlist,
165 				(caddr_t)&result_list, tout)) {
166 		pr_err("%s\n", clnt_sperrno(err));
167 		exit(1);
168 	}
169 
170 	/*
171 	 * Count the number of entries in the list.  If the list is empty,
172 	 * quit now.
173 	 */
174 	numentries = 0;
175 	for (ml = result_list; ml != NULL; ml = ml->ml_next)
176 		numentries++;
177 	if (numentries == 0)
178 		exit(0);
179 
180 	/*
181 	 * Allocate memory for the array and initialize the array.
182 	 */
183 
184 	table = (struct mountbody **)calloc(numentries,
185 						sizeof (struct mountbody *));
186 	if (table == NULL) {
187 		pr_err(gettext("not enough memory for %d entries\n"),
188 		    numentries);
189 		exit(1);
190 	}
191 	for (ml = result_list, tb = &table[0];
192 	    ml != NULL;
193 	    ml = ml->ml_next, tb++) {
194 		*tb = ml;
195 	}
196 
197 	/*
198 	 * Sort the entries and print the results.
199 	 */
200 
201 	if (dflg)
202 	    qsort(table, numentries, sizeof (struct mountbody *), sortpath);
203 	else
204 	    qsort(table, numentries, sizeof (struct mountbody *), sorthost);
205 	if (aflg) {
206 		for (tb = table; tb < table + numentries; tb++)
207 			printf("%s:%s\n", (*tb)->ml_hostname,
208 			    (*tb)->ml_directory);
209 	} else if (dflg) {
210 		last = "";
211 		for (tb = table; tb < table + numentries; tb++) {
212 			if (strcmp(last, (*tb)->ml_directory))
213 				printf("%s\n", (*tb)->ml_directory);
214 			last = (*tb)->ml_directory;
215 		}
216 	} else {
217 		last = "";
218 		for (tb = table; tb < table + numentries; tb++) {
219 			if (strcmp(last, (*tb)->ml_hostname))
220 				printf("%s\n", (*tb)->ml_hostname);
221 			last = (*tb)->ml_hostname;
222 		}
223 	}
224 	return (0);
225 }
226 
227 sorthost(a, b)
228 	struct mountbody **a, **b;
229 {
230 	return (strcmp((*a)->ml_hostname, (*b)->ml_hostname));
231 }
232 
233 sortpath(a, b)
234 	struct mountbody **a, **b;
235 {
236 	return (strcmp((*a)->ml_directory, (*b)->ml_directory));
237 }
238 
239 void
240 usage()
241 {
242 	(void) fprintf(stderr,
243 			gettext("Usage: showmount [-a] [-d] [-e] [host]\n"));
244 }
245 
246 void
247 pr_err(char *fmt, ...)
248 {
249 	va_list ap;
250 
251 	va_start(ap, fmt);
252 	(void) fprintf(stderr, "showmount: ");
253 	(void) vfprintf(stderr, fmt, ap);
254 	va_end(ap);
255 }
256 
257 void
258 printex(cl, host)
259 	CLIENT *cl;
260 	char *host;
261 {
262 	struct exportnode *ex = NULL;
263 	struct exportnode *e;
264 	struct groupnode *gr;
265 	enum clnt_stat err;
266 	int max;
267 	struct	timeval	tout;
268 
269 	tout.tv_sec = 10;
270 	tout.tv_usec = 0;
271 
272 	if (err = clnt_call(cl, MOUNTPROC_EXPORT,
273 	    xdr_void, 0, xdr_exports, (caddr_t) &ex, tout)) {
274 		pr_err("%s\n", clnt_sperrno(err));
275 		exit(1);
276 	}
277 
278 	if (ex == NULL) {
279 		printf(gettext("no exported file systems for %s\n"), host);
280 	} else {
281 		printf(gettext("export list for %s:\n"), host);
282 	}
283 	max = 0;
284 	for (e = ex; e != NULL; e = e->ex_next) {
285 		if (strlen(e->ex_dir) > max) {
286 			max = strlen(e->ex_dir);
287 		}
288 	}
289 	while (ex) {
290 		printf("%-*s ", max, ex->ex_dir);
291 		gr = ex->ex_groups;
292 		if (gr == NULL) {
293 			printf(gettext("(everyone)"));
294 		}
295 		while (gr) {
296 			printf("%s", gr->gr_name);
297 			gr = gr->gr_next;
298 			if (gr) {
299 				printf(",");
300 			}
301 		}
302 		printf("\n");
303 		ex = ex->ex_next;
304 	}
305 }
306