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>
41 #define TIMEOUT_TCP 10
42 #define TOTAL_TIMEOUT 20
44 static char * version = "showmount for " VERSION;
45 static char * program_name;
46 static int headers = 1;
52 static struct option longopts[] =
55 { "directories", 0, 0, 'd' },
56 { "exports", 0, 0, 'e' },
57 { "no-headers", 0, &headers, 0 },
58 { "version", 0, 0, 'v' },
59 { "help", 0, 0, 'h' },
63 #define MAXHOSTLEN 256
69 return strcmp(*p, *q);
72 static void usage(fp, n)
76 fprintf(fp, "Usage: %s [-adehv]\n", program_name);
77 fprintf(fp, " [--all] [--directories] [--exports]\n");
78 fprintf(fp, " [--no-headers] [--help] [--version] [host]\n");
83 * Perform a non-blocking connect on the socket fd.
85 * tout contains the timeout. It will be modified to contain the time
86 * remaining (i.e. time provided - time elasped).
88 static int connect_nb(int fd, struct sockaddr_in *addr, struct timeval *tout)
94 flags = fcntl(fd, F_GETFL, 0);
98 ret = fcntl(fd, F_SETFL, flags | O_NONBLOCK);
103 * From here on subsequent sys calls could change errno so
104 * we set ret = -errno to capture it in case we decide to
107 len = sizeof(struct sockaddr);
108 ret = connect(fd, (struct sockaddr *)addr, len);
109 if (ret < 0 && errno != EINPROGRESS) {
121 ret = select(fd + 1, NULL, &rset, NULL, tout);
130 if (FD_ISSET(fd, &rset)) {
134 status = getsockopt(fd, SOL_SOCKET, SO_ERROR, &ret, &len);
140 /* Oops - something wrong with connect */
146 fcntl(fd, F_SETFL, flags);
150 static unsigned short getport(struct sockaddr_in *addr,
151 unsigned long prog, unsigned long vers, int prot)
154 enum clnt_stat status;
157 struct sockaddr_in laddr, saddr;
158 struct timeval tout = {0, 0};
160 unsigned int send_sz = 0;
161 unsigned int recv_sz = 0;
164 memset(&laddr, 0, sizeof(laddr));
165 memset(&saddr, 0, sizeof(saddr));
166 memset(&parms, 0, sizeof(parms));
168 memcpy(&saddr, addr, sizeof(saddr));
169 saddr.sin_port = htons(PMAPPORT);
171 if (prot == IPPROTO_TCP) {
172 sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
174 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
175 rpc_createerr.cf_error.re_errno = errno;
179 tout.tv_sec = TIMEOUT_TCP;
181 ret = connect_nb(sock, &saddr, &tout);
184 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
185 rpc_createerr.cf_error.re_errno = errno;
188 client = clnttcp_create(&saddr,
189 PMAPPROG, PMAPVERS, &sock,
193 * bind to any unused port. If we left this up to the rpc
194 * layer, it would bind to a reserved port, which has been shown
195 * to exhaust the reserved port range in some situations.
197 sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
199 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
200 rpc_createerr.cf_error.re_errno = errno;
204 laddr.sin_family = AF_INET;
206 laddr.sin_addr.s_addr = htonl(INADDR_ANY);
208 tout.tv_sec = TIMEOUT_UDP;
210 send_sz = RPCSMALLMSGSIZE;
211 recv_sz = RPCSMALLMSGSIZE;
213 len = sizeof(struct sockaddr_in);
214 if (bind(sock, (struct sockaddr *)&laddr, len) < 0) {
219 client = clntudp_bufcreate(&saddr, PMAPPROG, PMAPVERS,
220 tout, &sock, send_sz, recv_sz);
225 rpc_createerr.cf_stat = RPC_RPCBFAILURE;
229 clnt_control(client, CLSET_FD_CLOSE, NULL);
231 parms.pm_prog = prog;
232 parms.pm_vers = vers;
233 parms.pm_prot = prot;
235 status = clnt_call(client, PMAPPROC_GETPORT,
236 (xdrproc_t) xdr_pmap, (caddr_t) &parms,
237 (xdrproc_t) xdr_u_short, (caddr_t) &port,
240 if (status != RPC_SUCCESS) {
241 clnt_geterr(client, &rpc_createerr.cf_error);
242 rpc_createerr.cf_stat = status;
243 clnt_destroy(client);
247 clnt_destroy(client);
256 char hostname_buf[MAXHOSTLEN];
258 enum clnt_stat clnt_stat;
260 struct sockaddr_in server_addr;
262 struct timeval total_timeout;
263 struct timeval pertry_timeout;
267 exports exportlist, exl;
275 program_name = argv[0];
276 while ((c = getopt_long(argc, argv, "adehv", longopts, NULL)) != EOF) {
291 printf("%s\n", version);
304 switch (aflag + dflag + eflag) {
311 fprintf(stderr, "%s: only one of -a, -d or -e is allowed\n",
319 if (gethostname(hostname_buf, MAXHOSTLEN) < 0) {
320 perror("getting hostname");
323 hostname = hostname_buf;
329 fprintf(stderr, "%s: only one hostname is allowed\n",
335 if (inet_aton(hostname, &server_addr.sin_addr)) {
336 server_addr.sin_family = AF_INET;
339 if ((hp = gethostbyname(hostname)) == NULL) {
340 fprintf(stderr, "%s: can't get address for %s\n",
341 program_name, hostname);
344 server_addr.sin_family = AF_INET;
345 memcpy(&server_addr.sin_addr, hp->h_addr, hp->h_length);
348 /* create mount deamon client */
351 msock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
353 server_addr.sin_port = getport(&server_addr,
354 MOUNTPROG, MOUNTVERS, IPPROTO_TCP);
355 if (server_addr.sin_port) {
356 ret = connect_nb(msock, &server_addr, 0);
358 mclient = clnttcp_create(&server_addr,
359 MOUNTPROG, MOUNTVERS, &msock,
368 server_addr.sin_port = getport(&server_addr,
369 MOUNTPROG, MOUNTVERS, IPPROTO_UDP);
370 if (!server_addr.sin_port) {
371 clnt_pcreateerror("portmap getport");
375 pertry_timeout.tv_sec = TIMEOUT_UDP;
376 pertry_timeout.tv_usec = 0;
377 if ((mclient = clntudp_create(&server_addr,
378 MOUNTPROG, MOUNTVERS, pertry_timeout, &msock)) == NULL) {
379 clnt_pcreateerror("mount clntudp_create");
383 mclient->cl_auth = authunix_create_default();
384 total_timeout.tv_sec = TOTAL_TIMEOUT;
385 total_timeout.tv_usec = 0;
388 memset(&exportlist, '\0', sizeof(exportlist));
390 clnt_stat = clnt_call(mclient, MOUNTPROC_EXPORT,
391 (xdrproc_t) xdr_void, NULL,
392 (xdrproc_t) xdr_exports, (caddr_t) &exportlist,
394 if (clnt_stat != RPC_SUCCESS) {
395 clnt_perror(mclient, "rpc mount export");
399 printf("Export list for %s:\n", hostname);
401 for (exl = exportlist; exl; exl = exl->ex_next) {
402 if ((n = strlen(exl->ex_dir)) > maxlen)
406 printf("%-*s ", maxlen, exportlist->ex_dir);
407 grouplist = exportlist->ex_groups;
410 printf("%s%s", grouplist->gr_name,
411 grouplist->gr_next ? "," : "");
412 grouplist = grouplist->gr_next;
415 printf("(everyone)");
417 exportlist = exportlist->ex_next;
422 memset(&dumplist, '\0', sizeof(dumplist));
423 clnt_stat = clnt_call(mclient, MOUNTPROC_DUMP,
424 (xdrproc_t) xdr_void, NULL,
425 (xdrproc_t) xdr_mountlist, (caddr_t) &dumplist,
427 if (clnt_stat != RPC_SUCCESS) {
428 clnt_perror(mclient, "rpc mount dump");
433 for (list = dumplist; list; list = list->ml_next)
435 dumpv = (char **) calloc(n, sizeof (char *));
437 fprintf(stderr, "%s: out of memory\n", program_name);
444 printf("Hosts on %s:\n", hostname);
446 dumpv[i++] = dumplist->ml_hostname;
447 dumplist = dumplist->ml_next;
452 printf("All mount points on %s:\n", hostname);
456 t=malloc(strlen(dumplist->ml_hostname)+strlen(dumplist->ml_directory)+2);
459 fprintf(stderr, "%s: out of memory\n", program_name);
462 sprintf(t, "%s:%s", dumplist->ml_hostname, dumplist->ml_directory);
464 dumplist = dumplist->ml_next;
469 printf("Directories on %s:\n", hostname);
471 dumpv[i++] = dumplist->ml_directory;
472 dumplist = dumplist->ml_next;
476 qsort(dumpv, n, sizeof (char *), dump_cmp);
478 for (i = 0; i < n; i++) {
479 if (i == 0 || strcmp(dumpv[i], dumpv[i - 1]) != 0)
480 printf("%s\n", dumpv[i]);