xref: /illumos-gate/usr/src/cmd/fs.d/nfs/showmount/showmount.c (revision 2aeafac3612e19716bf8164f89c3c9196342979c)
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 2005 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 int
73 main(int argc, char *argv[])
74 {
75 	int aflg = 0, dflg = 0, eflg = 0;
76 	int err;
77 	struct mountbody *result_list = NULL;
78 	struct mountbody *ml = NULL;
79 	struct mountbody **tb;		/* pointer into table */
80 	char *host, hostbuf[256];
81 	char *last;
82 	CLIENT *cl;
83 	extern int optind;
84 	extern char *optarg;
85 	int c;
86 	struct	timeval	tout, rpc_totout_old;
87 	int	numentries;
88 	(void) setlocale(LC_ALL, "");
89 
90 #if !defined(TEXT_DOMAIN)
91 #define	TEXT_DOMAIN "SYS_TEST"
92 #endif
93 	(void) textdomain(TEXT_DOMAIN);
94 
95 	while ((c = getopt(argc, argv, "ade")) != EOF) {
96 		switch (c) {
97 		case 'a':
98 			aflg++;
99 			break;
100 		case 'd':
101 			dflg++;
102 			break;
103 		case 'e':
104 			eflg++;
105 			break;
106 		default:
107 			usage();
108 			exit(1);
109 		}
110 	}
111 
112 	switch (argc - optind) {
113 	case 0:		/* no args */
114 		if (gethostname(hostbuf, sizeof (hostbuf)) < 0) {
115 			pr_err("gethostname: %s\n", strerror(errno));
116 			exit(1);
117 		}
118 		host = hostbuf;
119 		break;
120 	case 1:		/* one arg */
121 		host = argv[optind];
122 		break;
123 	default:	/* too many args */
124 		usage();
125 		exit(1);
126 	}
127 
128 	__rpc_control(CLCR_GET_RPCB_TIMEOUT, &rpc_totout_old);
129 	__rpc_control(CLCR_SET_RPCB_TIMEOUT, &rpc_totout_new);
130 
131 	/*
132 	 * First try circuit, then drop back to datagram if
133 	 * circuit is unavailable (an old version of mountd perhaps)
134 	 * Using circuit is preferred because it can handle
135 	 * arbitrarily long export lists.
136 	 */
137 	cl = clnt_create(host, MOUNTPROG, MOUNTVERS, "circuit_n");
138 	if (cl == NULL) {
139 		if (rpc_createerr.cf_stat == RPC_PROGNOTREGISTERED)
140 			cl = clnt_create(host, MOUNTPROG, MOUNTVERS,
141 					"datagram_n");
142 		if (cl == NULL) {
143 			pr_err("");
144 			clnt_pcreateerror(host);
145 			__rpc_control(CLCR_SET_RPCB_TIMEOUT, &rpc_totout_old);
146 			exit(1);
147 		}
148 	}
149 
150 	__rpc_control(CLCR_SET_RPCB_TIMEOUT, &rpc_totout_old);
151 
152 	if (eflg) {
153 		printex(cl, host);
154 		if (aflg + dflg == 0) {
155 			exit(0);
156 		}
157 	}
158 
159 	tout.tv_sec = 10;
160 	tout.tv_usec = 0;
161 
162 	if (err = clnt_call(cl, MOUNTPROC_DUMP,
163 			    xdr_void, 0, xdr_mountlist,
164 				(caddr_t)&result_list, tout)) {
165 		pr_err("%s\n", clnt_sperrno(err));
166 		exit(1);
167 	}
168 
169 	/*
170 	 * Count the number of entries in the list.  If the list is empty,
171 	 * quit now.
172 	 */
173 	numentries = 0;
174 	for (ml = result_list; ml != NULL; ml = ml->ml_next)
175 		numentries++;
176 	if (numentries == 0)
177 		exit(0);
178 
179 	/*
180 	 * Allocate memory for the array and initialize the array.
181 	 */
182 
183 	table = (struct mountbody **)calloc(numentries,
184 						sizeof (struct mountbody *));
185 	if (table == NULL) {
186 		pr_err(gettext("not enough memory for %d entries\n"),
187 		    numentries);
188 		exit(1);
189 	}
190 	for (ml = result_list, tb = &table[0];
191 	    ml != NULL;
192 	    ml = ml->ml_next, tb++) {
193 		*tb = ml;
194 	}
195 
196 	/*
197 	 * Sort the entries and print the results.
198 	 */
199 
200 	if (dflg)
201 	    qsort(table, numentries, sizeof (struct mountbody *), sortpath);
202 	else
203 	    qsort(table, numentries, sizeof (struct mountbody *), sorthost);
204 	if (aflg) {
205 		for (tb = table; tb < table + numentries; tb++)
206 			printf("%s:%s\n", (*tb)->ml_hostname,
207 			    (*tb)->ml_directory);
208 	} else if (dflg) {
209 		last = "";
210 		for (tb = table; tb < table + numentries; tb++) {
211 			if (strcmp(last, (*tb)->ml_directory))
212 				printf("%s\n", (*tb)->ml_directory);
213 			last = (*tb)->ml_directory;
214 		}
215 	} else {
216 		last = "";
217 		for (tb = table; tb < table + numentries; tb++) {
218 			if (strcmp(last, (*tb)->ml_hostname))
219 				printf("%s\n", (*tb)->ml_hostname);
220 			last = (*tb)->ml_hostname;
221 		}
222 	}
223 	return (0);
224 }
225 
226 int
227 sorthost(a, b)
228 	struct mountbody **a, **b;
229 {
230 	return (strcmp((*a)->ml_hostname, (*b)->ml_hostname));
231 }
232 
233 int
234 sortpath(a, b)
235 	struct mountbody **a, **b;
236 {
237 	return (strcmp((*a)->ml_directory, (*b)->ml_directory));
238 }
239 
240 void
241 usage()
242 {
243 	(void) fprintf(stderr,
244 			gettext("Usage: showmount [-a] [-d] [-e] [host]\n"));
245 }
246 
247 void
248 pr_err(char *fmt, ...)
249 {
250 	va_list ap;
251 
252 	va_start(ap, fmt);
253 	(void) fprintf(stderr, "showmount: ");
254 	(void) vfprintf(stderr, fmt, ap);
255 	va_end(ap);
256 }
257 
258 void
259 printex(cl, host)
260 	CLIENT *cl;
261 	char *host;
262 {
263 	struct exportnode *ex = NULL;
264 	struct exportnode *e;
265 	struct groupnode *gr;
266 	enum clnt_stat err;
267 	int max;
268 	struct	timeval	tout;
269 
270 	tout.tv_sec = 10;
271 	tout.tv_usec = 0;
272 
273 	if (err = clnt_call(cl, MOUNTPROC_EXPORT,
274 	    xdr_void, 0, xdr_exports, (caddr_t)&ex, tout)) {
275 		pr_err("%s\n", clnt_sperrno(err));
276 		exit(1);
277 	}
278 
279 	if (ex == NULL) {
280 		printf(gettext("no exported file systems for %s\n"), host);
281 	} else {
282 		printf(gettext("export list for %s:\n"), host);
283 	}
284 	max = 0;
285 	for (e = ex; e != NULL; e = e->ex_next) {
286 		if (strlen(e->ex_dir) > max) {
287 			max = strlen(e->ex_dir);
288 		}
289 	}
290 	while (ex) {
291 		printf("%-*s ", max, ex->ex_dir);
292 		gr = ex->ex_groups;
293 		if (gr == NULL) {
294 			printf(gettext("(everyone)"));
295 		}
296 		while (gr) {
297 			printf("%s", gr->gr_name);
298 			gr = gr->gr_next;
299 			if (gr) {
300 				printf(",");
301 			}
302 		}
303 		printf("\n");
304 		ex = ex->ex_next;
305 	}
306 }
307