summaryrefslogtreecommitdiff
path: root/usr/src/cmd/fs.d/nfs/showmount/showmount.c
blob: 48f3110266d5ae1b7772e001cbbcf4bb013b061b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License, Version 1.0 only
 * (the "License").  You may not use this file except in compliance
 * with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

/*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
/*	  All Rights Reserved  	*/

/*
 * University Copyright- Copyright (c) 1982, 1986, 1988
 * The Regents of the University of California
 * All Rights Reserved
 *
 * University Acknowledgment- Portions of this document are derived from
 * software developed by the University of California, Berkeley, and its
 * contributors.
 */

#pragma ident	"%Z%%M%	%I%	%E% SMI"

/*
 * showmount
 */
#include <stdio.h>
#include <stdarg.h>
#include <rpc/rpc.h>
#include <rpc/rpcb_clnt.h>
#include <sys/socket.h>
#include <netdb.h>
#include <sys/time.h>
#include <errno.h>
#include <nfs/nfs.h>
#include <rpcsvc/mount.h>
#include <locale.h>

int sorthost();
int sortpath();
void pr_err(char *, ...);
void printex();
void usage();

/*
 * Dynamically-sized array of pointers to mountlist entries.  Each element
 * points into the linked list returned by the RPC call.  We use an array
 * so that we can conveniently sort the entries.
 */
static struct mountbody **table;

struct	timeval	rpc_totout_new = {15, 0};

int
main(int argc, char *argv[])
{
	int aflg = 0, dflg = 0, eflg = 0;
	int err;
	struct mountbody *result_list = NULL;
	struct mountbody *ml = NULL;
	struct mountbody **tb;		/* pointer into table */
	char *host, hostbuf[256];
	char *last;
	CLIENT *cl;
	extern int optind;
	extern char *optarg;
	int c;
	struct	timeval	tout, rpc_totout_old;
	int	numentries;
	(void) setlocale(LC_ALL, "");

#if !defined(TEXT_DOMAIN)
#define	TEXT_DOMAIN "SYS_TEST"
#endif
	(void) textdomain(TEXT_DOMAIN);

	while ((c = getopt(argc, argv, "ade")) != EOF) {
		switch (c) {
		case 'a':
			aflg++;
			break;
		case 'd':
			dflg++;
			break;
		case 'e':
			eflg++;
			break;
		default:
			usage();
			exit(1);
		}
	}

	switch (argc - optind) {
	case 0:		/* no args */
		if (gethostname(hostbuf, sizeof (hostbuf)) < 0) {
			pr_err("gethostname: %s\n", strerror(errno));
			exit(1);
		}
		host = hostbuf;
		break;
	case 1:		/* one arg */
		host = argv[optind];
		break;
	default:	/* too many args */
		usage();
		exit(1);
	}

	__rpc_control(CLCR_GET_RPCB_TIMEOUT, &rpc_totout_old);
	__rpc_control(CLCR_SET_RPCB_TIMEOUT, &rpc_totout_new);

	/*
	 * First try circuit, then drop back to datagram if
	 * circuit is unavailable (an old version of mountd perhaps)
	 * Using circuit is preferred because it can handle
	 * arbitrarily long export lists.
	 */
	cl = clnt_create(host, MOUNTPROG, MOUNTVERS, "circuit_n");
	if (cl == NULL) {
		if (rpc_createerr.cf_stat == RPC_PROGNOTREGISTERED)
			cl = clnt_create(host, MOUNTPROG, MOUNTVERS,
					"datagram_n");
		if (cl == NULL) {
			pr_err("");
			clnt_pcreateerror(host);
			__rpc_control(CLCR_SET_RPCB_TIMEOUT, &rpc_totout_old);
			exit(1);
		}
	}

	__rpc_control(CLCR_SET_RPCB_TIMEOUT, &rpc_totout_old);

	if (eflg) {
		printex(cl, host);
		if (aflg + dflg == 0) {
			exit(0);
		}
	}

	tout.tv_sec = 10;
	tout.tv_usec = 0;

	if (err = clnt_call(cl, MOUNTPROC_DUMP,
			    xdr_void, 0, xdr_mountlist,
				(caddr_t)&result_list, tout)) {
		pr_err("%s\n", clnt_sperrno(err));
		exit(1);
	}

	/*
	 * Count the number of entries in the list.  If the list is empty,
	 * quit now.
	 */
	numentries = 0;
	for (ml = result_list; ml != NULL; ml = ml->ml_next)
		numentries++;
	if (numentries == 0)
		exit(0);

	/*
	 * Allocate memory for the array and initialize the array.
	 */

	table = (struct mountbody **)calloc(numentries,
						sizeof (struct mountbody *));
	if (table == NULL) {
		pr_err(gettext("not enough memory for %d entries\n"),
		    numentries);
		exit(1);
	}
	for (ml = result_list, tb = &table[0];
	    ml != NULL;
	    ml = ml->ml_next, tb++) {
		*tb = ml;
	}

	/*
	 * Sort the entries and print the results.
	 */

	if (dflg)
	    qsort(table, numentries, sizeof (struct mountbody *), sortpath);
	else
	    qsort(table, numentries, sizeof (struct mountbody *), sorthost);
	if (aflg) {
		for (tb = table; tb < table + numentries; tb++)
			printf("%s:%s\n", (*tb)->ml_hostname,
			    (*tb)->ml_directory);
	} else if (dflg) {
		last = "";
		for (tb = table; tb < table + numentries; tb++) {
			if (strcmp(last, (*tb)->ml_directory))
				printf("%s\n", (*tb)->ml_directory);
			last = (*tb)->ml_directory;
		}
	} else {
		last = "";
		for (tb = table; tb < table + numentries; tb++) {
			if (strcmp(last, (*tb)->ml_hostname))
				printf("%s\n", (*tb)->ml_hostname);
			last = (*tb)->ml_hostname;
		}
	}
	return (0);
}

int
sorthost(a, b)
	struct mountbody **a, **b;
{
	return (strcmp((*a)->ml_hostname, (*b)->ml_hostname));
}

int
sortpath(a, b)
	struct mountbody **a, **b;
{
	return (strcmp((*a)->ml_directory, (*b)->ml_directory));
}

void
usage()
{
	(void) fprintf(stderr,
			gettext("Usage: showmount [-a] [-d] [-e] [host]\n"));
}

void
pr_err(char *fmt, ...)
{
	va_list ap;

	va_start(ap, fmt);
	(void) fprintf(stderr, "showmount: ");
	(void) vfprintf(stderr, fmt, ap);
	va_end(ap);
}

void
printex(cl, host)
	CLIENT *cl;
	char *host;
{
	struct exportnode *ex = NULL;
	struct exportnode *e;
	struct groupnode *gr;
	enum clnt_stat err;
	int max;
	struct	timeval	tout;

	tout.tv_sec = 10;
	tout.tv_usec = 0;

	if (err = clnt_call(cl, MOUNTPROC_EXPORT,
	    xdr_void, 0, xdr_exports, (caddr_t)&ex, tout)) {
		pr_err("%s\n", clnt_sperrno(err));
		exit(1);
	}

	if (ex == NULL) {
		printf(gettext("no exported file systems for %s\n"), host);
	} else {
		printf(gettext("export list for %s:\n"), host);
	}
	max = 0;
	for (e = ex; e != NULL; e = e->ex_next) {
		if (strlen(e->ex_dir) > max) {
			max = strlen(e->ex_dir);
		}
	}
	while (ex) {
		printf("%-*s ", max, ex->ex_dir);
		gr = ex->ex_groups;
		if (gr == NULL) {
			printf(gettext("(everyone)"));
		}
		while (gr) {
			printf("%s", gr->gr_name);
			gr = gr->gr_next;
			if (gr) {
				printf(",");
			}
		}
		printf("\n");
		ex = ex->ex_next;
	}
}