2 * Copyright (c) 1989, 1993, 1995
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
6 * Rick Macklem at The University of Guelph.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 4. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 static const char copyright[] =
35 "@(#) Copyright (c) 1989, 1993, 1995\n\
36 The Regents of the University of California. All rights reserved.\n";
41 static char sccsid[] = "@(#)showmount.c 8.3 (Berkeley) 3/29/95";
43 static const char rcsid[] =
47 #include <sys/types.h>
48 #include <sys/queue.h>
50 #include <sys/socket.h>
51 #include <sys/socketvar.h>
56 #include <rpc/pmap_clnt.h>
57 #include <rpc/pmap_prot.h>
58 #include <rpcsvc/mount.h>
73 struct mountlist *ml_left;
74 struct mountlist *ml_right;
75 char ml_host[MNTNAMLEN+1];
76 char ml_dirp[MNTPATHLEN+1];
80 struct grouplist *gr_next;
81 char gr_name[MNTNAMLEN+1];
85 struct exportslist *ex_next;
86 struct grouplist *ex_groups;
87 char ex_dirp[MNTPATHLEN+1];
90 static struct mountlist *mntdump;
91 static struct exportslist *exportslist;
94 void print_dump(struct mountlist *);
95 static void usage(void);
96 int xdr_mntdump(XDR *, struct mountlist **);
97 int xdr_exportslist(XDR *, struct exportslist **);
98 int tcp_callrpc(const char *host, int prognum, int versnum, int procnum,
99 xdrproc_t inproc, char *in, xdrproc_t outproc, char *out);
102 * This command queries the NFS mount daemon for it's mount list and/or
103 * it's exports list and prints them out.
104 * See "NFS: Network File System Protocol Specification, RFC1094, Appendix A"
105 * and the "Network File System Protocol XXX.."
106 * for detailed information on the protocol.
109 main(int argc, char **argv)
111 register struct exportslist *exp;
112 register struct grouplist *grp;
113 register int rpcs = 0, mntvers = 1;
117 while ((ch = getopt(argc, argv, "ade3")) != -1)
155 if ((estat = tcp_callrpc(host, MOUNTPROG, mntvers,
156 MOUNTPROC_DUMP, (xdrproc_t)xdr_void, (char *)0,
157 (xdrproc_t)xdr_mntdump, (char *)&mntdump)) != 0) {
159 errx(1, "can't do mountdump rpc");
161 if (rpcs & DOEXPORTS)
162 if ((estat = tcp_callrpc(host, MOUNTPROG, mntvers,
163 MOUNTPROC_EXPORT, (xdrproc_t)xdr_void, (char *)0,
164 (xdrproc_t)xdr_exportslist, (char *)&exportslist)) != 0) {
166 errx(1, "can't do exports rpc");
169 /* Now just print out the results */
173 printf("All mount points on %s:\n", host);
176 printf("Directories on %s:\n", host);
179 printf("Hosts on %s:\n", host);
184 if (rpcs & DOEXPORTS) {
185 printf("Exports list on %s:\n", host);
188 printf("%-34s ", exp->ex_dirp);
189 grp = exp->ex_groups;
191 printf("Everyone\n");
194 printf("%s ", grp->gr_name);
206 * tcp_callrpc has the same interface as callrpc, but tries to
207 * use tcp as transport method in order to handle large replies.
210 tcp_callrpc(const char *host, int prognum, int versnum, int procnum,
211 xdrproc_t inproc, char *in, xdrproc_t outproc, char *out)
214 struct timeval timeout;
217 if ((client = clnt_create(host, prognum, versnum, "tcp")) == NULL &&
218 (client = clnt_create(host, prognum, versnum, "udp")) == NULL)
219 return ((int) rpc_createerr.cf_stat);
223 rval = (int) clnt_call(client, procnum,
227 clnt_destroy(client);
232 * Xdr routine for retrieving the mount dump list
235 xdr_mntdump(XDR *xdrsp, struct mountlist **mlp)
237 register struct mountlist *mp;
238 register struct mountlist *tp;
239 register struct mountlist **otp;
244 *mlp = (struct mountlist *)0;
245 if (!xdr_bool(xdrsp, &bool))
248 mp = (struct mountlist *)malloc(sizeof(struct mountlist));
251 mp->ml_left = mp->ml_right = (struct mountlist *)0;
253 if (!xdr_string(xdrsp, &strp, MNTNAMLEN))
256 if (!xdr_string(xdrsp, &strp, MNTPATHLEN))
260 * Build a binary tree on sorted order of either host or dirp.
261 * Drop any duplications.
268 val = strcmp(mp->ml_host, tp->ml_host);
269 val2 = strcmp(mp->ml_dirp, tp->ml_dirp);
305 if (!xdr_bool(xdrsp, &bool))
312 * Xdr routine to retrieve exports list
315 xdr_exportslist(XDR *xdrsp, struct exportslist **exp)
317 register struct exportslist *ep;
318 register struct grouplist *gp;
322 *exp = (struct exportslist *)0;
323 if (!xdr_bool(xdrsp, &bool))
326 ep = (struct exportslist *)malloc(sizeof(struct exportslist));
329 ep->ex_groups = (struct grouplist *)0;
331 if (!xdr_string(xdrsp, &strp, MNTPATHLEN))
333 if (!xdr_bool(xdrsp, &grpbool))
336 gp = (struct grouplist *)malloc(sizeof(struct grouplist));
340 if (!xdr_string(xdrsp, &strp, MNTNAMLEN))
342 gp->gr_next = ep->ex_groups;
344 if (!xdr_bool(xdrsp, &grpbool))
349 if (!xdr_bool(xdrsp, &bool))
358 fprintf(stderr, "usage: showmount [-a | -d] [-e3] [host]\n");
363 * Print the binary tree in inorder so that output is sorted.
366 print_dump(struct mountlist *mp)
372 print_dump(mp->ml_left);
375 printf("%s:%s\n", mp->ml_host, mp->ml_dirp);
378 printf("%s\n", mp->ml_dirp);
381 printf("%s\n", mp->ml_host);
385 print_dump(mp->ml_right);