1 /* $NetBSD: showmount.c,v 1.16 2008/07/21 14:19:26 lukem Exp $ */
4 * Copyright (c) 1989, 1993, 1995
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
8 * Rick Macklem at The University of Guelph.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 #include <sys/cdefs.h>
37 __COPYRIGHT("@(#) Copyright (c) 1989, 1993, 1995\
38 The Regents of the University of California. All rights reserved.");
43 static char sccsid
[] = "@(#)showmount.c 8.3 (Berkeley) 3/29/95";
45 __RCSID("$NetBSD: showmount.c,v 1.16 2008/07/21 14:19:26 lukem Exp $");
48 #include <sys/types.h>
50 #include <sys/socket.h>
54 #include <rpc/pmap_clnt.h>
55 #include <rpc/pmap_prot.h>
56 #include <nfs/rpcv2.h>
71 struct mountlist
*ml_left
;
72 struct mountlist
*ml_right
;
73 char ml_host
[RPCMNT_NAMELEN
+1];
74 char ml_dirp
[RPCMNT_PATHLEN
+1];
78 struct grouplist
*gr_next
;
79 char gr_name
[RPCMNT_NAMELEN
+1];
83 struct exportslist
*ex_next
;
84 struct grouplist
*ex_groups
;
85 char ex_dirp
[RPCMNT_PATHLEN
+1];
88 static struct mountlist
*mntdump
;
89 static struct exportslist
*exports
;
92 int main(int, char **);
93 void print_dump(struct mountlist
*);
95 int xdr_mntdump(XDR
*, struct mountlist
**);
96 int xdr_exports(XDR
*, struct exportslist
**);
97 int tcp_callrpc(const char *host
, int prognum
, int versnum
, int procnum
,
98 xdrproc_t inproc
, char *in
, xdrproc_t outproc
, char *out
);
101 * This command queries the NFS mount daemon for it's mount list and/or
102 * it's exports list and prints them out.
103 * See "NFS: Network File System Protocol Specification, RFC1094, Appendix A"
104 * and the "Network File System Protocol XXX.."
105 * for detailed information on the protocol.
108 main(int argc
, char **argv
)
110 struct exportslist
*exp
;
111 struct grouplist
*grp
;
112 int estat
, rpcs
= 0, mntvers
= 1;
117 while ((ch
= getopt(argc
, argv
, "ade3")) != -1)
155 if ((estat
= tcp_callrpc(host
, RPCPROG_MNT
, mntvers
,
156 RPCMNT_DUMP
, xdr_void
, (char *)0,
157 xdr_mntdump
, (char *)&mntdump
)) != 0) {
158 fprintf(stderr
, "showmount: Can't do Mountdump rpc: ");
162 if (rpcs
& DOEXPORTS
)
163 if ((estat
= tcp_callrpc(host
, RPCPROG_MNT
, mntvers
,
164 RPCMNT_EXPORT
, xdr_void
, (char *)0,
165 xdr_exports
, (char *)&exports
)) != 0) {
166 fprintf(stderr
, "showmount: Can't do Exports rpc: ");
171 /* Now just print out the results */
175 printf("All mount points on %s:\n", host
);
178 printf("Directories on %s:\n", host
);
181 printf("Hosts on %s:\n", host
);
186 if (rpcs
& DOEXPORTS
) {
187 printf("Exports list on %s:\n", host
);
190 len
= printf("%-35s", exp
->ex_dirp
);
193 grp
= exp
->ex_groups
;
195 printf("Everyone\n");
198 printf("%s ", grp
->gr_name
);
211 * tcp_callrpc has the same interface as callrpc, but tries to
212 * use tcp as transport method in order to handle large replies.
216 tcp_callrpc(const char *host
, int prognum
, int versnum
, int procnum
,
217 xdrproc_t inproc
, char *in
, xdrproc_t outproc
, char *out
)
220 struct timeval timeout
;
223 if ((client
= clnt_create(host
, prognum
, versnum
, "tcp")) == NULL
&&
224 (client
= clnt_create(host
, prognum
, versnum
, "udp")) == NULL
)
225 return ((int) rpc_createerr
.cf_stat
);
229 rval
= (int) clnt_call(client
, procnum
,
233 clnt_destroy(client
);
238 * Xdr routine for retrieving the mount dump list
241 xdr_mntdump(XDR
*xdrsp
, struct mountlist
**mlp
)
243 struct mountlist
*mp
, **otp
, *tp
;
248 *mlp
= (struct mountlist
*)0;
249 if (!xdr_bool(xdrsp
, &bool))
252 mp
= (struct mountlist
*)malloc(sizeof(struct mountlist
));
255 mp
->ml_left
= mp
->ml_right
= (struct mountlist
*)0;
257 if (!xdr_string(xdrsp
, &strp
, RPCMNT_NAMELEN
))
260 if (!xdr_string(xdrsp
, &strp
, RPCMNT_PATHLEN
))
264 * Build a binary tree on sorted order of either host or dirp.
265 * Drop any duplications.
272 val
= strcmp(mp
->ml_host
, tp
->ml_host
);
273 val2
= strcmp(mp
->ml_dirp
, tp
->ml_dirp
);
309 if (!xdr_bool(xdrsp
, &bool))
316 * Xdr routine to retrieve exports list
319 xdr_exports(XDR
*xdrsp
, struct exportslist
**exp
)
321 struct exportslist
*ep
;
322 struct grouplist
*gp
;
326 *exp
= (struct exportslist
*)0;
327 if (!xdr_bool(xdrsp
, &bool))
330 ep
= (struct exportslist
*)malloc(sizeof(struct exportslist
));
333 ep
->ex_groups
= (struct grouplist
*)0;
335 if (!xdr_string(xdrsp
, &strp
, RPCMNT_PATHLEN
))
337 if (!xdr_bool(xdrsp
, &grpbool
))
340 gp
= (struct grouplist
*)malloc(sizeof(struct grouplist
));
344 if (!xdr_string(xdrsp
, &strp
, RPCMNT_NAMELEN
))
346 gp
->gr_next
= ep
->ex_groups
;
348 if (!xdr_bool(xdrsp
, &grpbool
))
353 if (!xdr_bool(xdrsp
, &bool))
363 fprintf(stderr
, "usage: showmount [-ade3] host\n");
368 * Print the binary tree in inorder so that output is sorted.
371 print_dump(struct mountlist
*mp
)
377 print_dump(mp
->ml_left
);
380 printf("%s:%s\n", mp
->ml_host
, mp
->ml_dirp
);
383 printf("%s\n", mp
->ml_dirp
);
386 printf("%s\n", mp
->ml_host
);
390 print_dump(mp
->ml_right
);