2 * showmount.c -- show mount information for an NFS server
3 * Copyright (C) 1993 Rick Sladkey <jrs@world.std.com>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2, or (at your option)
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
22 #include <rpc/pmap_prot.h>
23 #include <sys/socket.h>
24 #include <netinet/in.h>
27 #include <sys/types.h>
34 #include <arpa/inet.h>
43 #define TOTAL_TIMEOUT 20
45 static char * version = "showmount for " VERSION;
46 static char * program_name;
47 static int headers = 1;
53 static struct option longopts[] =
56 { "directories", 0, 0, 'd' },
57 { "exports", 0, 0, 'e' },
58 { "no-headers", 0, &headers, 0 },
59 { "version", 0, 0, 'v' },
60 { "help", 0, 0, 'h' },
64 #define MAXHOSTLEN 256
66 static int dump_cmp(const void *pv, const void *qv)
68 const char **p = (const char **)pv;
69 const char **q = (const char **)qv;
70 return strcmp(*p, *q);
73 static void usage(FILE *fp, int n)
75 fprintf(fp, "Usage: %s [-adehv]\n", program_name);
76 fprintf(fp, " [--all] [--directories] [--exports]\n");
77 fprintf(fp, " [--no-headers] [--help] [--version] [host]\n");
81 static const char *nfs_sm_pgmtbl[] = {
89 * Generate an RPC client handle connected to the mountd service
90 * at @hostname, or die trying.
92 * Supports both AF_INET and AF_INET6 server addresses.
94 static CLIENT *nfs_get_mount_client(const char *hostname)
96 rpcprog_t program = nfs_getrpcbyname(MOUNTPROG, nfs_sm_pgmtbl);
99 client = clnt_create(hostname, program, MOUNTVERS, "tcp");
103 client = clnt_create(hostname, program, MOUNTVERS, "udp");
107 clnt_pcreateerror("clnt_create");
111 int main(int argc, char **argv)
113 char hostname_buf[MAXHOSTLEN];
115 enum clnt_stat clnt_stat;
116 struct timeval total_timeout;
120 exports exportlist, exl;
128 program_name = argv[0];
129 while ((c = getopt_long(argc, argv, "adehv", longopts, NULL)) != EOF) {
144 printf("%s\n", version);
157 switch (aflag + dflag + eflag) {
164 fprintf(stderr, "%s: only one of -a, -d or -e is allowed\n",
172 if (gethostname(hostname_buf, MAXHOSTLEN) < 0) {
173 perror("getting hostname");
176 hostname = hostname_buf;
182 fprintf(stderr, "%s: only one hostname is allowed\n",
188 mclient = nfs_get_mount_client(hostname);
189 mclient->cl_auth = authunix_create_default();
190 total_timeout.tv_sec = TOTAL_TIMEOUT;
191 total_timeout.tv_usec = 0;
194 memset(&exportlist, '\0', sizeof(exportlist));
196 clnt_stat = clnt_call(mclient, MOUNTPROC_EXPORT,
197 (xdrproc_t) xdr_void, NULL,
198 (xdrproc_t) xdr_exports, (caddr_t) &exportlist,
200 if (clnt_stat != RPC_SUCCESS) {
201 clnt_perror(mclient, "rpc mount export");
202 clnt_destroy(mclient);
206 printf("Export list for %s:\n", hostname);
208 for (exl = exportlist; exl; exl = exl->ex_next) {
209 if ((n = strlen(exl->ex_dir)) > maxlen)
213 printf("%-*s ", maxlen, exportlist->ex_dir);
214 grouplist = exportlist->ex_groups;
217 printf("%s%s", grouplist->gr_name,
218 grouplist->gr_next ? "," : "");
219 grouplist = grouplist->gr_next;
222 printf("(everyone)");
224 exportlist = exportlist->ex_next;
226 clnt_destroy(mclient);
230 memset(&dumplist, '\0', sizeof(dumplist));
231 clnt_stat = clnt_call(mclient, MOUNTPROC_DUMP,
232 (xdrproc_t) xdr_void, NULL,
233 (xdrproc_t) xdr_mountlist, (caddr_t) &dumplist,
235 if (clnt_stat != RPC_SUCCESS) {
236 clnt_perror(mclient, "rpc mount dump");
237 clnt_destroy(mclient);
240 clnt_destroy(mclient);
243 for (list = dumplist; list; list = list->ml_next)
245 dumpv = (char **) calloc(n, sizeof (char *));
247 fprintf(stderr, "%s: out of memory\n", program_name);
254 printf("Hosts on %s:\n", hostname);
256 dumpv[i++] = dumplist->ml_hostname;
257 dumplist = dumplist->ml_next;
262 printf("All mount points on %s:\n", hostname);
266 t=malloc(strlen(dumplist->ml_hostname)+strlen(dumplist->ml_directory)+2);
269 fprintf(stderr, "%s: out of memory\n", program_name);
272 sprintf(t, "%s:%s", dumplist->ml_hostname, dumplist->ml_directory);
274 dumplist = dumplist->ml_next;
279 printf("Directories on %s:\n", hostname);
281 dumpv[i++] = dumplist->ml_directory;
282 dumplist = dumplist->ml_next;
286 qsort(dumpv, n, sizeof (char *), dump_cmp);
288 for (i = 0; i < n; i++) {
289 if (i == 0 || strcmp(dumpv[i], dumpv[i - 1]) != 0)
290 printf("%s\n", dumpv[i]);