Cleanup USB stick image generation.
[dragonfly.git] / usr.bin / showmount / showmount.c
blob0fa10c878294ad64b5c8bbc1095193d0f49c61a6
1 /*
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
10 * are met:
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 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
36 * @(#) Copyright (c) 1989, 1993, 1995 The Regents of the University of California. All rights reserved.
37 * @(#)showmount.c 8.3 (Berkeley) 3/29/95
38 * $FreeBSD: src/usr.bin/showmount/showmount.c,v 1.16 2005/05/21 09:55:08 ru Exp $
39 * $DragonFly: src/usr.bin/showmount/showmount.c,v 1.5 2005/09/05 04:22:07 swildner Exp $
42 #include <sys/types.h>
43 #include <sys/queue.h>
44 #include <sys/file.h>
45 #include <sys/socket.h>
46 #include <sys/socketvar.h>
48 #include <err.h>
49 #include <netdb.h>
50 #include <rpc/rpc.h>
51 #include <rpc/pmap_clnt.h>
52 #include <rpc/pmap_prot.h>
53 #include <nfs/rpcv2.h>
55 #include <stdio.h>
56 #include <stdlib.h>
57 #include <string.h>
58 #include <unistd.h>
60 /* Constant defs */
61 #define ALL 1
62 #define DIRS 2
64 #define DODUMP 0x1
65 #define DOEXPORTS 0x2
67 struct mountlist {
68 struct mountlist *ml_left;
69 struct mountlist *ml_right;
70 char ml_host[RPCMNT_NAMELEN+1];
71 char ml_dirp[RPCMNT_PATHLEN+1];
74 struct grouplist {
75 struct grouplist *gr_next;
76 char gr_name[RPCMNT_NAMELEN+1];
79 struct exportslist {
80 struct exportslist *ex_next;
81 struct grouplist *ex_groups;
82 char ex_dirp[RPCMNT_PATHLEN+1];
85 static struct mountlist *mntdump;
86 static struct exportslist *exports;
87 static int type = 0;
89 void print_dump(struct mountlist *);
90 static void usage(void);
91 int xdr_mntdump(XDR *, struct mountlist **);
92 int xdr_exports(XDR *, struct exportslist **);
93 int tcp_callrpc(const char *, int, int, int, xdrproc_t, char *, xdrproc_t,
94 char *);
97 * This command queries the NFS mount daemon for it's mount list and/or
98 * it's exports list and prints them out.
99 * See "NFS: Network File System Protocol Specification, RFC1094, Appendix A"
100 * and the "Network File System Protocol XXX.."
101 * for detailed information on the protocol.
104 main(int argc, char **argv)
106 struct exportslist *exp;
107 struct grouplist *grp;
108 int rpcs = 0, mntvers = 1;
109 const char *host;
110 int ch, estat;
112 while ((ch = getopt(argc, argv, "ade3")) != -1)
113 switch (ch) {
114 case 'a':
115 if (type == 0) {
116 type = ALL;
117 rpcs |= DODUMP;
118 } else
119 usage();
120 break;
121 case 'd':
122 if (type == 0) {
123 type = DIRS;
124 rpcs |= DODUMP;
125 } else
126 usage();
127 break;
128 case 'e':
129 rpcs |= DOEXPORTS;
130 break;
131 case '3':
132 mntvers = 3;
133 break;
134 case '?':
135 default:
136 usage();
138 argc -= optind;
139 argv += optind;
141 if (argc > 0)
142 host = *argv;
143 else
144 host = "localhost";
146 if (rpcs == 0)
147 rpcs = DODUMP;
149 if (rpcs & DODUMP)
150 if ((estat = tcp_callrpc(host, RPCPROG_MNT, mntvers,
151 RPCMNT_DUMP, (xdrproc_t)xdr_void, NULL,
152 (xdrproc_t)xdr_mntdump, (char *)&mntdump)) != 0) {
153 clnt_perrno(estat);
154 errx(1, "can't do mountdump rpc");
156 if (rpcs & DOEXPORTS)
157 if ((estat = tcp_callrpc(host, RPCPROG_MNT, mntvers,
158 RPCMNT_EXPORT, (xdrproc_t)xdr_void, NULL,
159 (xdrproc_t)xdr_exports, (char *)&exports)) != 0) {
160 clnt_perrno(estat);
161 errx(1, "can't do exports rpc");
164 /* Now just print out the results */
165 if (rpcs & DODUMP) {
166 switch (type) {
167 case ALL:
168 printf("All mount points on %s:\n", host);
169 break;
170 case DIRS:
171 printf("Directories on %s:\n", host);
172 break;
173 default:
174 printf("Hosts on %s:\n", host);
175 break;
177 print_dump(mntdump);
179 if (rpcs & DOEXPORTS) {
180 printf("Exports list on %s:\n", host);
181 exp = exports;
182 while (exp) {
183 printf("%-35s", exp->ex_dirp);
184 grp = exp->ex_groups;
185 if (grp == NULL) {
186 printf("Everyone\n");
187 } else {
188 while (grp) {
189 printf("%s ", grp->gr_name);
190 grp = grp->gr_next;
192 printf("\n");
194 exp = exp->ex_next;
197 exit(0);
201 * tcp_callrpc has the same interface as callrpc, but tries to
202 * use tcp as transport method in order to handle large replies.
205 tcp_callrpc(const char *host, int prognum, int versnum, int procnum,
206 xdrproc_t inproc, char *in, xdrproc_t outproc, char *out)
208 CLIENT *client;
209 struct timeval timeout;
210 int rval;
212 if ((client = clnt_create(host, prognum, versnum, "tcp")) == NULL &&
213 (client = clnt_create(host, prognum, versnum, "udp")) == NULL)
214 return ((int) rpc_createerr.cf_stat);
216 timeout.tv_sec = 25;
217 timeout.tv_usec = 0;
218 rval = (int) clnt_call(client, procnum,
219 inproc, in,
220 outproc, out,
221 timeout);
222 clnt_destroy(client);
223 return rval;
227 * Xdr routine for retrieving the mount dump list
230 xdr_mntdump(XDR *xdrsp, struct mountlist **mlp)
232 struct mountlist *mp;
233 struct mountlist *tp;
234 struct mountlist **otp = NULL;
235 int val, val2;
236 int bool;
237 char *strp;
239 *mlp = NULL;
240 if (!xdr_bool(xdrsp, &bool))
241 return (0);
242 while (bool) {
243 mp = (struct mountlist *)malloc(sizeof(struct mountlist));
244 if (mp == NULL)
245 return (0);
246 mp->ml_left = mp->ml_right = NULL;
247 strp = mp->ml_host;
248 if (!xdr_string(xdrsp, &strp, RPCMNT_NAMELEN))
249 return (0);
250 strp = mp->ml_dirp;
251 if (!xdr_string(xdrsp, &strp, RPCMNT_PATHLEN))
252 return (0);
255 * Build a binary tree on sorted order of either host or dirp.
256 * Drop any duplications.
258 if (*mlp == NULL) {
259 *mlp = mp;
260 } else {
261 tp = *mlp;
262 while (tp) {
263 val = strcmp(mp->ml_host, tp->ml_host);
264 val2 = strcmp(mp->ml_dirp, tp->ml_dirp);
265 switch (type) {
266 case ALL:
267 if (val == 0) {
268 if (val2 == 0) {
269 free(mp);
270 goto next;
272 val = val2;
274 break;
275 case DIRS:
276 if (val2 == 0) {
277 free(mp);
278 goto next;
280 val = val2;
281 break;
282 default:
283 if (val == 0) {
284 free(mp);
285 goto next;
287 break;
289 if (val < 0) {
290 otp = &tp->ml_left;
291 tp = tp->ml_left;
292 } else {
293 otp = &tp->ml_right;
294 tp = tp->ml_right;
297 *otp = mp;
299 next:
300 if (!xdr_bool(xdrsp, &bool))
301 return (0);
303 return (1);
307 * Xdr routine to retrieve exports list
310 xdr_exports(XDR *xdrsp, struct exportslist **exp)
312 struct exportslist *ep;
313 struct grouplist *gp;
314 int bool, grpbool;
315 char *strp;
317 *exp = NULL;
318 if (!xdr_bool(xdrsp, &bool))
319 return (0);
320 while (bool) {
321 ep = (struct exportslist *)malloc(sizeof(struct exportslist));
322 if (ep == NULL)
323 return (0);
324 ep->ex_groups = NULL;
325 strp = ep->ex_dirp;
326 if (!xdr_string(xdrsp, &strp, RPCMNT_PATHLEN))
327 return (0);
328 if (!xdr_bool(xdrsp, &grpbool))
329 return (0);
330 while (grpbool) {
331 gp = (struct grouplist *)malloc(sizeof(struct grouplist));
332 if (gp == NULL)
333 return (0);
334 strp = gp->gr_name;
335 if (!xdr_string(xdrsp, &strp, RPCMNT_NAMELEN))
336 return (0);
337 gp->gr_next = ep->ex_groups;
338 ep->ex_groups = gp;
339 if (!xdr_bool(xdrsp, &grpbool))
340 return (0);
342 ep->ex_next = *exp;
343 *exp = ep;
344 if (!xdr_bool(xdrsp, &bool))
345 return (0);
347 return (1);
350 static void
351 usage(void)
353 fprintf(stderr, "usage: showmount [-a | -d] [-e3] [host]\n");
354 exit(1);
358 * Print the binary tree in inorder so that output is sorted.
360 void
361 print_dump(struct mountlist *mp)
364 if (mp == NULL)
365 return;
366 if (mp->ml_left)
367 print_dump(mp->ml_left);
368 switch (type) {
369 case ALL:
370 printf("%s:%s\n", mp->ml_host, mp->ml_dirp);
371 break;
372 case DIRS:
373 printf("%s\n", mp->ml_dirp);
374 break;
375 default:
376 printf("%s\n", mp->ml_host);
377 break;
379 if (mp->ml_right)
380 print_dump(mp->ml_right);