xref: /illumos-gate/usr/src/cmd/fs.d/nfs/showmount/showmount.c (revision 67fa3f2c31312dc0caac188f568ab1fdc6b27295)
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 /*
41  * showmount
42  */
43 #include <stdio.h>
44 #include <stdarg.h>
45 #include <stdlib.h>
46 #include <string.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 #include <unistd.h>
57 #include <clnt_subr.h>
58 
59 int sorthost(const void *, const void *);
60 int sortpath(const void *, const void *);
61 void printex(CLIENT *, char *);
62 void usage(void);
63 
64 /*
65  * Dynamically-sized array of pointers to mountlist entries.  Each element
66  * points into the linked list returned by the RPC call.  We use an array
67  * so that we can conveniently sort the entries.
68  */
69 static struct mountbody **table;
70 
71 struct	timeval	rpc_totout_new = {15, 0};
72 
73 int
74 main(int argc, 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 	int c;
85 	struct timeval	tout, rpc_totout_old;
86 	int numentries;
87 
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 	(void) __rpc_control(CLCR_GET_RPCB_TIMEOUT, &rpc_totout_old);
129 	(void) __rpc_control(CLCR_SET_RPCB_TIMEOUT, &rpc_totout_new);
130 
131 	cl = mountprog_client_create(host, &rpc_totout_old);
132 	if (cl == NULL) {
133 		exit(1);
134 	}
135 
136 	(void) __rpc_control(CLCR_SET_RPCB_TIMEOUT, &rpc_totout_old);
137 
138 	if (eflg) {
139 		printex(cl, host);
140 		if (aflg + dflg == 0) {
141 			exit(0);
142 		}
143 	}
144 
145 	tout.tv_sec = 10;
146 	tout.tv_usec = 0;
147 
148 	err = clnt_call(cl, MOUNTPROC_DUMP, xdr_void, 0, xdr_mountlist,
149 	    (caddr_t)&result_list, tout);
150 	if (err != 0) {
151 		pr_err("%s\n", clnt_sperrno(err));
152 		exit(1);
153 	}
154 
155 	/*
156 	 * Count the number of entries in the list.  If the list is empty,
157 	 * quit now.
158 	 */
159 	numentries = 0;
160 	for (ml = result_list; ml != NULL; ml = ml->ml_next)
161 		numentries++;
162 	if (numentries == 0)
163 		exit(0);
164 
165 	/*
166 	 * Allocate memory for the array and initialize the array.
167 	 */
168 
169 	table = calloc(numentries, sizeof (struct mountbody *));
170 	if (table == NULL) {
171 		pr_err(gettext("not enough memory for %d entries\n"),
172 		    numentries);
173 		exit(1);
174 	}
175 	for (ml = result_list, tb = &table[0];
176 	    ml != NULL;
177 	    ml = ml->ml_next, tb++) {
178 		*tb = ml;
179 	}
180 
181 	/*
182 	 * Sort the entries and print the results.
183 	 */
184 
185 	if (dflg)
186 		qsort(table, numentries, sizeof (struct mountbody *), sortpath);
187 	else
188 		qsort(table, numentries, sizeof (struct mountbody *), sorthost);
189 	if (aflg) {
190 		for (tb = table; tb < table + numentries; tb++)
191 			printf("%s:%s\n", (*tb)->ml_hostname,
192 			    (*tb)->ml_directory);
193 	} else if (dflg) {
194 		last = "";
195 		for (tb = table; tb < table + numentries; tb++) {
196 			if (strcmp(last, (*tb)->ml_directory))
197 				printf("%s\n", (*tb)->ml_directory);
198 			last = (*tb)->ml_directory;
199 		}
200 	} else {
201 		last = "";
202 		for (tb = table; tb < table + numentries; tb++) {
203 			if (strcmp(last, (*tb)->ml_hostname))
204 				printf("%s\n", (*tb)->ml_hostname);
205 			last = (*tb)->ml_hostname;
206 		}
207 	}
208 	return (0);
209 }
210 
211 int
212 sorthost(const void *_a, const void *_b)
213 {
214 	struct mountbody **a = (struct mountbody **)_a;
215 	struct mountbody **b = (struct mountbody **)_b;
216 
217 	return (strcmp((*a)->ml_hostname, (*b)->ml_hostname));
218 }
219 
220 int
221 sortpath(const void *_a, const void *_b)
222 {
223 	struct mountbody **a = (struct mountbody **)_a;
224 	struct mountbody **b = (struct mountbody **)_b;
225 
226 	return (strcmp((*a)->ml_directory, (*b)->ml_directory));
227 }
228 
229 void
230 usage(void)
231 {
232 	(void) fprintf(stderr,
233 	    gettext("Usage: showmount [-a] [-d] [-e] [host]\n"));
234 }
235 
236 void
237 pr_err(char *fmt, ...)
238 {
239 	va_list ap;
240 
241 	va_start(ap, fmt);
242 	(void) fprintf(stderr, "showmount: ");
243 	(void) vfprintf(stderr, fmt, ap);
244 	va_end(ap);
245 }
246 
247 void
248 printex(CLIENT *cl, char *host)
249 {
250 	struct exportnode *ex = NULL;
251 	struct exportnode *e;
252 	struct groupnode *gr;
253 	enum clnt_stat err;
254 	int max;
255 	struct	timeval	tout;
256 
257 	tout.tv_sec = 10;
258 	tout.tv_usec = 0;
259 
260 	err = clnt_call(cl, MOUNTPROC_EXPORT, xdr_void, 0, xdr_exports,
261 	    (caddr_t)&ex, tout);
262 	if (err != 0) {
263 		pr_err("%s\n", clnt_sperrno(err));
264 		exit(1);
265 	}
266 
267 	if (ex == NULL) {
268 		printf(gettext("no exported file systems for %s\n"), host);
269 	} else {
270 		printf(gettext("export list for %s:\n"), host);
271 	}
272 	max = 0;
273 	for (e = ex; e != NULL; e = e->ex_next) {
274 		if (strlen(e->ex_dir) > max) {
275 			max = strlen(e->ex_dir);
276 		}
277 	}
278 	while (ex) {
279 		printf("%-*s ", max, ex->ex_dir);
280 		gr = ex->ex_groups;
281 		if (gr == NULL) {
282 			printf(gettext("(everyone)"));
283 		}
284 		while (gr) {
285 			printf("%s", gr->gr_name);
286 			gr = gr->gr_next;
287 			if (gr) {
288 				printf(",");
289 			}
290 		}
291 		printf("\n");
292 		ex = ex->ex_next;
293 	}
294 }
295