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 TIMEOUT_TCP 10
44 #define TOTAL_TIMEOUT 20
46 static char * version = "showmount for " VERSION;
47 static char * program_name;
48 static int headers = 1;
54 static struct option longopts[] =
57 { "directories", 0, 0, 'd' },
58 { "exports", 0, 0, 'e' },
59 { "no-headers", 0, &headers, 0 },
60 { "version", 0, 0, 'v' },
61 { "help", 0, 0, 'h' },
65 #define MAXHOSTLEN 256
67 static int dump_cmp(const void *pv, const void *qv)
69 const char **p = (const char **)pv;
70 const char **q = (const char **)qv;
71 return strcmp(*p, *q);
74 static void usage(FILE *fp, int 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 * Returns zero on success; otherwise, -1 is returned and errno is set
89 * to reflect the nature of the error.
91 static int connect_nb(int fd, struct sockaddr_in *addr, struct timeval *tout)
97 flags = fcntl(fd, F_GETFL, 0);
101 ret = fcntl(fd, F_SETFL, flags | O_NONBLOCK);
106 * From here on subsequent sys calls could change errno so
107 * we set ret = -errno to capture it in case we decide to
110 len = sizeof(struct sockaddr);
111 ret = connect(fd, (struct sockaddr *)addr, len);
112 if (ret < 0 && errno != EINPROGRESS) {
124 ret = select(fd + 1, NULL, &rset, NULL, tout);
132 if (FD_ISSET(fd, &rset)) {
136 status = getsockopt(fd, SOL_SOCKET, SO_ERROR, &ret, &len);
142 /* Oops - something wrong with connect */
150 fcntl(fd, F_SETFL, flags);
154 static unsigned short getport(struct sockaddr_in *addr,
155 unsigned long prog, unsigned long vers, int prot)
158 enum clnt_stat status;
161 struct sockaddr_in laddr, saddr;
162 struct timeval tout = {0, 0};
164 unsigned int send_sz = 0;
165 unsigned int recv_sz = 0;
168 memset(&laddr, 0, sizeof(laddr));
169 memset(&saddr, 0, sizeof(saddr));
170 memset(&parms, 0, sizeof(parms));
172 memcpy(&saddr, addr, sizeof(saddr));
173 saddr.sin_port = htons(PMAPPORT);
175 if (prot == IPPROTO_TCP) {
176 sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
178 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
179 rpc_createerr.cf_error.re_errno = errno;
183 tout.tv_sec = TIMEOUT_TCP;
185 ret = connect_nb(sock, &saddr, &tout);
187 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
188 rpc_createerr.cf_error.re_errno = errno;
192 client = clnttcp_create(&saddr,
193 PMAPPROG, PMAPVERS, &sock,
197 * bind to any unused port. If we left this up to the rpc
198 * layer, it would bind to a reserved port, which has been shown
199 * to exhaust the reserved port range in some situations.
201 sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
203 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
204 rpc_createerr.cf_error.re_errno = errno;
208 laddr.sin_family = AF_INET;
210 laddr.sin_addr.s_addr = htonl(INADDR_ANY);
212 tout.tv_sec = TIMEOUT_UDP;
214 send_sz = RPCSMALLMSGSIZE;
215 recv_sz = RPCSMALLMSGSIZE;
217 len = sizeof(struct sockaddr_in);
218 if (bind(sock, (struct sockaddr *)&laddr, len) < 0) {
223 client = clntudp_bufcreate(&saddr, PMAPPROG, PMAPVERS,
224 tout, &sock, send_sz, recv_sz);
229 rpc_createerr.cf_stat = RPC_RPCBFAILURE;
233 clnt_control(client, CLSET_FD_CLOSE, NULL);
235 parms.pm_prog = prog;
236 parms.pm_vers = vers;
237 parms.pm_prot = prot;
239 status = clnt_call(client, PMAPPROC_GETPORT,
240 (xdrproc_t) xdr_pmap, (caddr_t) &parms,
241 (xdrproc_t) xdr_u_short, (caddr_t) &port,
244 if (status != RPC_SUCCESS) {
245 clnt_geterr(client, &rpc_createerr.cf_error);
246 rpc_createerr.cf_stat = status;
247 clnt_destroy(client);
249 } else if (port == 0) {
250 rpc_createerr.cf_stat = RPC_PROGNOTREGISTERED;
253 clnt_destroy(client);
258 int main(int argc, char **argv)
260 char hostname_buf[MAXHOSTLEN];
262 enum clnt_stat clnt_stat;
264 struct sockaddr_in server_addr;
266 struct timeval total_timeout;
267 struct timeval pertry_timeout;
271 exports exportlist, exl;
279 program_name = argv[0];
280 while ((c = getopt_long(argc, argv, "adehv", longopts, NULL)) != EOF) {
295 printf("%s\n", version);
308 switch (aflag + dflag + eflag) {
315 fprintf(stderr, "%s: only one of -a, -d or -e is allowed\n",
323 if (gethostname(hostname_buf, MAXHOSTLEN) < 0) {
324 perror("getting hostname");
327 hostname = hostname_buf;
333 fprintf(stderr, "%s: only one hostname is allowed\n",
339 if (inet_aton(hostname, &server_addr.sin_addr)) {
340 server_addr.sin_family = AF_INET;
343 if ((hp = gethostbyname(hostname)) == NULL) {
344 fprintf(stderr, "%s: can't get address for %s\n",
345 program_name, hostname);
348 server_addr.sin_family = AF_INET;
349 memcpy(&server_addr.sin_addr, hp->h_addr, hp->h_length);
352 /* create mount deamon client */
355 msock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
357 if (nfs_getport_ping((struct sockaddr *)&server_addr,
358 sizeof(server_addr), MOUNTPROG,
359 MOUNTVERS, IPPROTO_TCP)) {
360 ret = connect_nb(msock, &server_addr, 0);
361 if (ret == 0) /* success */
362 mclient = clnttcp_create(&server_addr,
363 MOUNTPROG, MOUNTVERS, &msock,
372 if (nfs_getport_ping((struct sockaddr *)&server_addr,
373 sizeof(server_addr), MOUNTPROG,
374 MOUNTVERS, IPPROTO_UDP)) {
375 clnt_pcreateerror("showmount");
379 pertry_timeout.tv_sec = TIMEOUT_UDP;
380 pertry_timeout.tv_usec = 0;
381 if ((mclient = clntudp_create(&server_addr,
382 MOUNTPROG, MOUNTVERS, pertry_timeout, &msock)) == NULL) {
383 clnt_pcreateerror("mount clntudp_create");
387 mclient->cl_auth = authunix_create_default();
388 total_timeout.tv_sec = TOTAL_TIMEOUT;
389 total_timeout.tv_usec = 0;
392 memset(&exportlist, '\0', sizeof(exportlist));
394 clnt_stat = clnt_call(mclient, MOUNTPROC_EXPORT,
395 (xdrproc_t) xdr_void, NULL,
396 (xdrproc_t) xdr_exports, (caddr_t) &exportlist,
398 if (clnt_stat != RPC_SUCCESS) {
399 clnt_perror(mclient, "rpc mount export");
400 clnt_destroy(mclient);
404 printf("Export list for %s:\n", hostname);
406 for (exl = exportlist; exl; exl = exl->ex_next) {
407 if ((n = strlen(exl->ex_dir)) > maxlen)
411 printf("%-*s ", maxlen, exportlist->ex_dir);
412 grouplist = exportlist->ex_groups;
415 printf("%s%s", grouplist->gr_name,
416 grouplist->gr_next ? "," : "");
417 grouplist = grouplist->gr_next;
420 printf("(everyone)");
422 exportlist = exportlist->ex_next;
424 clnt_destroy(mclient);
428 memset(&dumplist, '\0', sizeof(dumplist));
429 clnt_stat = clnt_call(mclient, MOUNTPROC_DUMP,
430 (xdrproc_t) xdr_void, NULL,
431 (xdrproc_t) xdr_mountlist, (caddr_t) &dumplist,
433 if (clnt_stat != RPC_SUCCESS) {
434 clnt_perror(mclient, "rpc mount dump");
435 clnt_destroy(mclient);
438 clnt_destroy(mclient);
441 for (list = dumplist; list; list = list->ml_next)
443 dumpv = (char **) calloc(n, sizeof (char *));
445 fprintf(stderr, "%s: out of memory\n", program_name);
452 printf("Hosts on %s:\n", hostname);
454 dumpv[i++] = dumplist->ml_hostname;
455 dumplist = dumplist->ml_next;
460 printf("All mount points on %s:\n", hostname);
464 t=malloc(strlen(dumplist->ml_hostname)+strlen(dumplist->ml_directory)+2);
467 fprintf(stderr, "%s: out of memory\n", program_name);
470 sprintf(t, "%s:%s", dumplist->ml_hostname, dumplist->ml_directory);
472 dumplist = dumplist->ml_next;
477 printf("Directories on %s:\n", hostname);
479 dumpv[i++] = dumplist->ml_directory;
480 dumplist = dumplist->ml_next;
484 qsort(dumpv, n, sizeof (char *), dump_cmp);
486 for (i = 0; i < n; i++) {
487 if (i == 0 || strcmp(dumpv[i], dumpv[i - 1]) != 0)
488 printf("%s\n", dumpv[i]);