16873 tools: add lex to build tools
[illumos-gate.git] / usr / src / cmd / fs.d / nfs / showmount / showmount.c
blobb1959f2090bdbb4f22b8ef35989ed67bfa28408f
1 /*
2 * CDDL HEADER START
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.
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.
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]
20 * CDDL HEADER END
23 * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
28 /* All Rights Reserved */
31 * University Copyright- Copyright (c) 1982, 1986, 1988
32 * The Regents of the University of California
33 * All Rights Reserved
35 * University Acknowledgment- Portions of this document are derived from
36 * software developed by the University of California, Berkeley, and its
37 * contributors.
41 * showmount
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>
59 int sorthost(const void *, const void *);
60 int sortpath(const void *, const void *);
61 void printex(CLIENT *, char *);
62 void usage(void);
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.
69 static struct mountbody **table;
71 struct timeval rpc_totout_new = {15, 0};
73 int
74 main(int argc, char *argv[])
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;
88 (void) setlocale(LC_ALL, "");
90 #if !defined(TEXT_DOMAIN)
91 #define TEXT_DOMAIN "SYS_TEST"
92 #endif
93 (void) textdomain(TEXT_DOMAIN);
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);
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);
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);
128 (void) __rpc_control(CLCR_GET_RPCB_TIMEOUT, &rpc_totout_old);
129 (void) __rpc_control(CLCR_SET_RPCB_TIMEOUT, &rpc_totout_new);
131 cl = mountprog_client_create(host, &rpc_totout_old);
132 if (cl == NULL) {
133 exit(1);
136 (void) __rpc_control(CLCR_SET_RPCB_TIMEOUT, &rpc_totout_old);
138 if (eflg) {
139 printex(cl, host);
140 if (aflg + dflg == 0) {
141 exit(0);
145 tout.tv_sec = 10;
146 tout.tv_usec = 0;
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);
156 * Count the number of entries in the list. If the list is empty,
157 * quit now.
159 numentries = 0;
160 for (ml = result_list; ml != NULL; ml = ml->ml_next)
161 numentries++;
162 if (numentries == 0)
163 exit(0);
166 * Allocate memory for the array and initialize the array.
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);
175 for (ml = result_list, tb = &table[0];
176 ml != NULL;
177 ml = ml->ml_next, tb++) {
178 *tb = ml;
182 * Sort the entries and print the results.
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;
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;
208 return (0);
212 sorthost(const void *_a, const void *_b)
214 struct mountbody **a = (struct mountbody **)_a;
215 struct mountbody **b = (struct mountbody **)_b;
217 return (strcmp((*a)->ml_hostname, (*b)->ml_hostname));
221 sortpath(const void *_a, const void *_b)
223 struct mountbody **a = (struct mountbody **)_a;
224 struct mountbody **b = (struct mountbody **)_b;
226 return (strcmp((*a)->ml_directory, (*b)->ml_directory));
229 void
230 usage(void)
232 (void) fprintf(stderr,
233 gettext("Usage: showmount [-a] [-d] [-e] [host]\n"));
236 void
237 pr_err(char *fmt, ...)
239 va_list ap;
241 va_start(ap, fmt);
242 (void) fprintf(stderr, "showmount: ");
243 (void) vfprintf(stderr, fmt, ap);
244 va_end(ap);
247 void
248 printex(CLIENT *cl, char *host)
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;
257 tout.tv_sec = 10;
258 tout.tv_usec = 0;
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);
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);
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);
278 while (ex) {
279 printf("%-*s ", max, ex->ex_dir);
280 gr = ex->ex_groups;
281 if (gr == NULL) {
282 printf(gettext("(everyone)"));
284 while (gr) {
285 printf("%s", gr->gr_name);
286 gr = gr->gr_next;
287 if (gr) {
288 printf(",");
291 printf("\n");
292 ex = ex->ex_next;