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
65 static int dump_cmp(const void *pv, const void *qv)
67 const char **p = (const char **)pv;
68 const char **q = (const char **)qv;
69 return strcmp(*p, *q);
72 static void usage(FILE *fp, int n)
74 fprintf(fp, "Usage: %s [-adehv]\n", program_name);
75 fprintf(fp, " [--all] [--directories] [--exports]\n");
76 fprintf(fp, " [--no-headers] [--help] [--version] [host]\n");
81 * Perform a non-blocking connect on the socket fd.
83 * tout contains the timeout. It will be modified to contain the time
84 * remaining (i.e. time provided - time elasped).
86 static int connect_nb(int fd, struct sockaddr_in *addr, struct timeval *tout)
92 flags = fcntl(fd, F_GETFL, 0);
96 ret = fcntl(fd, F_SETFL, flags | O_NONBLOCK);
101 * From here on subsequent sys calls could change errno so
102 * we set ret = -errno to capture it in case we decide to
105 len = sizeof(struct sockaddr);
106 ret = connect(fd, (struct sockaddr *)addr, len);
107 if (ret < 0 && errno != EINPROGRESS) {
119 ret = select(fd + 1, NULL, &rset, NULL, tout);
128 if (FD_ISSET(fd, &rset)) {
132 status = getsockopt(fd, SOL_SOCKET, SO_ERROR, &ret, &len);
138 /* Oops - something wrong with connect */
144 fcntl(fd, F_SETFL, flags);
148 static unsigned short getport(struct sockaddr_in *addr,
149 unsigned long prog, unsigned long vers, int prot)
152 enum clnt_stat status;
155 struct sockaddr_in laddr, saddr;
156 struct timeval tout = {0, 0};
158 unsigned int send_sz = 0;
159 unsigned int recv_sz = 0;
162 memset(&laddr, 0, sizeof(laddr));
163 memset(&saddr, 0, sizeof(saddr));
164 memset(&parms, 0, sizeof(parms));
166 memcpy(&saddr, addr, sizeof(saddr));
167 saddr.sin_port = htons(PMAPPORT);
169 if (prot == IPPROTO_TCP) {
170 sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
172 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
173 rpc_createerr.cf_error.re_errno = errno;
177 tout.tv_sec = TIMEOUT_TCP;
179 ret = connect_nb(sock, &saddr, &tout);
182 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
183 rpc_createerr.cf_error.re_errno = errno;
186 client = clnttcp_create(&saddr,
187 PMAPPROG, PMAPVERS, &sock,
191 * bind to any unused port. If we left this up to the rpc
192 * layer, it would bind to a reserved port, which has been shown
193 * to exhaust the reserved port range in some situations.
195 sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
197 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
198 rpc_createerr.cf_error.re_errno = errno;
202 laddr.sin_family = AF_INET;
204 laddr.sin_addr.s_addr = htonl(INADDR_ANY);
206 tout.tv_sec = TIMEOUT_UDP;
208 send_sz = RPCSMALLMSGSIZE;
209 recv_sz = RPCSMALLMSGSIZE;
211 len = sizeof(struct sockaddr_in);
212 if (bind(sock, (struct sockaddr *)&laddr, len) < 0) {
217 client = clntudp_bufcreate(&saddr, PMAPPROG, PMAPVERS,
218 tout, &sock, send_sz, recv_sz);
223 rpc_createerr.cf_stat = RPC_RPCBFAILURE;
227 clnt_control(client, CLSET_FD_CLOSE, NULL);
229 parms.pm_prog = prog;
230 parms.pm_vers = vers;
231 parms.pm_prot = prot;
233 status = clnt_call(client, PMAPPROC_GETPORT,
234 (xdrproc_t) xdr_pmap, (caddr_t) &parms,
235 (xdrproc_t) xdr_u_short, (caddr_t) &port,
238 if (status != RPC_SUCCESS) {
239 clnt_geterr(client, &rpc_createerr.cf_error);
240 rpc_createerr.cf_stat = status;
241 clnt_destroy(client);
245 clnt_destroy(client);
250 int main(int argc, char **argv)
252 char hostname_buf[MAXHOSTLEN];
254 enum clnt_stat clnt_stat;
256 struct sockaddr_in server_addr;
258 struct timeval total_timeout;
259 struct timeval pertry_timeout;
263 exports exportlist, exl;
271 program_name = argv[0];
272 while ((c = getopt_long(argc, argv, "adehv", longopts, NULL)) != EOF) {
287 printf("%s\n", version);
300 switch (aflag + dflag + eflag) {
307 fprintf(stderr, "%s: only one of -a, -d or -e is allowed\n",
315 if (gethostname(hostname_buf, MAXHOSTLEN) < 0) {
316 perror("getting hostname");
319 hostname = hostname_buf;
325 fprintf(stderr, "%s: only one hostname is allowed\n",
331 if (inet_aton(hostname, &server_addr.sin_addr)) {
332 server_addr.sin_family = AF_INET;
335 if ((hp = gethostbyname(hostname)) == NULL) {
336 fprintf(stderr, "%s: can't get address for %s\n",
337 program_name, hostname);
340 server_addr.sin_family = AF_INET;
341 memcpy(&server_addr.sin_addr, hp->h_addr, hp->h_length);
344 /* create mount deamon client */
347 msock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
349 server_addr.sin_port = getport(&server_addr,
350 MOUNTPROG, MOUNTVERS, IPPROTO_TCP);
351 if (server_addr.sin_port) {
352 ret = connect_nb(msock, &server_addr, 0);
354 mclient = clnttcp_create(&server_addr,
355 MOUNTPROG, MOUNTVERS, &msock,
364 server_addr.sin_port = getport(&server_addr,
365 MOUNTPROG, MOUNTVERS, IPPROTO_UDP);
366 if (!server_addr.sin_port) {
367 clnt_pcreateerror("portmap getport");
371 pertry_timeout.tv_sec = TIMEOUT_UDP;
372 pertry_timeout.tv_usec = 0;
373 if ((mclient = clntudp_create(&server_addr,
374 MOUNTPROG, MOUNTVERS, pertry_timeout, &msock)) == NULL) {
375 clnt_pcreateerror("mount clntudp_create");
379 mclient->cl_auth = authunix_create_default();
380 total_timeout.tv_sec = TOTAL_TIMEOUT;
381 total_timeout.tv_usec = 0;
384 memset(&exportlist, '\0', sizeof(exportlist));
386 clnt_stat = clnt_call(mclient, MOUNTPROC_EXPORT,
387 (xdrproc_t) xdr_void, NULL,
388 (xdrproc_t) xdr_exports, (caddr_t) &exportlist,
390 if (clnt_stat != RPC_SUCCESS) {
391 clnt_perror(mclient, "rpc mount export");
395 printf("Export list for %s:\n", hostname);
397 for (exl = exportlist; exl; exl = exl->ex_next) {
398 if ((n = strlen(exl->ex_dir)) > maxlen)
402 printf("%-*s ", maxlen, exportlist->ex_dir);
403 grouplist = exportlist->ex_groups;
406 printf("%s%s", grouplist->gr_name,
407 grouplist->gr_next ? "," : "");
408 grouplist = grouplist->gr_next;
411 printf("(everyone)");
413 exportlist = exportlist->ex_next;
418 memset(&dumplist, '\0', sizeof(dumplist));
419 clnt_stat = clnt_call(mclient, MOUNTPROC_DUMP,
420 (xdrproc_t) xdr_void, NULL,
421 (xdrproc_t) xdr_mountlist, (caddr_t) &dumplist,
423 if (clnt_stat != RPC_SUCCESS) {
424 clnt_perror(mclient, "rpc mount dump");
429 for (list = dumplist; list; list = list->ml_next)
431 dumpv = (char **) calloc(n, sizeof (char *));
433 fprintf(stderr, "%s: out of memory\n", program_name);
440 printf("Hosts on %s:\n", hostname);
442 dumpv[i++] = dumplist->ml_hostname;
443 dumplist = dumplist->ml_next;
448 printf("All mount points on %s:\n", hostname);
452 t=malloc(strlen(dumplist->ml_hostname)+strlen(dumplist->ml_directory)+2);
455 fprintf(stderr, "%s: out of memory\n", program_name);
458 sprintf(t, "%s:%s", dumplist->ml_hostname, dumplist->ml_directory);
460 dumplist = dumplist->ml_next;
465 printf("Directories on %s:\n", hostname);
467 dumpv[i++] = dumplist->ml_directory;
468 dumplist = dumplist->ml_next;
472 qsort(dumpv, n, sizeof (char *), dump_cmp);
474 for (i = 0; i < n; i++) {
475 if (i == 0 || strcmp(dumpv[i], dumpv[i - 1]) != 0)
476 printf("%s\n", dumpv[i]);