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");
82 * Perform a non-blocking connect on the socket fd.
84 * tout contains the timeout. It will be modified to contain the time
85 * remaining (i.e. time provided - time elasped).
87 * Returns zero on success; otherwise, -1 is returned and errno is set
88 * to reflect the nature of the error.
90 static int connect_nb(int fd, struct sockaddr_in *addr, struct timeval *tout)
96 flags = fcntl(fd, F_GETFL, 0);
100 ret = fcntl(fd, F_SETFL, flags | O_NONBLOCK);
105 * From here on subsequent sys calls could change errno so
106 * we set ret = -errno to capture it in case we decide to
109 len = sizeof(struct sockaddr);
110 ret = connect(fd, (struct sockaddr *)addr, len);
111 if (ret < 0 && errno != EINPROGRESS) {
123 ret = select(fd + 1, NULL, &rset, NULL, tout);
131 if (FD_ISSET(fd, &rset)) {
135 status = getsockopt(fd, SOL_SOCKET, SO_ERROR, &ret, &len);
141 /* Oops - something wrong with connect */
149 fcntl(fd, F_SETFL, flags);
154 * Generate an RPC client handle connected to the mountd service
155 * at @hostname, or die trying.
157 * Supports only AF_INET server addresses.
159 static CLIENT *nfs_get_mount_client(const char *hostname)
162 struct sockaddr_in server_addr;
163 struct timeval pertry_timeout;
164 CLIENT *mclient = NULL;
167 if (inet_aton(hostname, &server_addr.sin_addr)) {
168 server_addr.sin_family = AF_INET;
171 if ((hp = gethostbyname(hostname)) == NULL) {
172 fprintf(stderr, "%s: can't get address for %s\n",
173 program_name, hostname);
176 server_addr.sin_family = AF_INET;
177 memcpy(&server_addr.sin_addr, hp->h_addr, hp->h_length);
180 msock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
182 if (nfs_getport_ping((struct sockaddr *)&server_addr,
183 sizeof(server_addr), MOUNTPROG,
184 MOUNTVERS, IPPROTO_TCP)) {
185 ret = connect_nb(msock, &server_addr, 0);
187 mclient = clnttcp_create(&server_addr,
188 MOUNTPROG, MOUNTVERS, &msock,
197 if (nfs_getport_ping((struct sockaddr *)&server_addr,
198 sizeof(server_addr), MOUNTPROG,
199 MOUNTVERS, IPPROTO_UDP)) {
200 clnt_pcreateerror("showmount");
204 pertry_timeout.tv_sec = TIMEOUT_UDP;
205 pertry_timeout.tv_usec = 0;
206 if ((mclient = clntudp_create(&server_addr,
207 MOUNTPROG, MOUNTVERS, pertry_timeout, &msock)) == NULL) {
208 clnt_pcreateerror("mount clntudp_create");
216 int main(int argc, char **argv)
218 char hostname_buf[MAXHOSTLEN];
220 enum clnt_stat clnt_stat;
221 struct timeval total_timeout;
225 exports exportlist, exl;
233 program_name = argv[0];
234 while ((c = getopt_long(argc, argv, "adehv", longopts, NULL)) != EOF) {
249 printf("%s\n", version);
262 switch (aflag + dflag + eflag) {
269 fprintf(stderr, "%s: only one of -a, -d or -e is allowed\n",
277 if (gethostname(hostname_buf, MAXHOSTLEN) < 0) {
278 perror("getting hostname");
281 hostname = hostname_buf;
287 fprintf(stderr, "%s: only one hostname is allowed\n",
293 mclient = nfs_get_mount_client(hostname);
294 mclient->cl_auth = authunix_create_default();
295 total_timeout.tv_sec = TOTAL_TIMEOUT;
296 total_timeout.tv_usec = 0;
299 memset(&exportlist, '\0', sizeof(exportlist));
301 clnt_stat = clnt_call(mclient, MOUNTPROC_EXPORT,
302 (xdrproc_t) xdr_void, NULL,
303 (xdrproc_t) xdr_exports, (caddr_t) &exportlist,
305 if (clnt_stat != RPC_SUCCESS) {
306 clnt_perror(mclient, "rpc mount export");
307 clnt_destroy(mclient);
311 printf("Export list for %s:\n", hostname);
313 for (exl = exportlist; exl; exl = exl->ex_next) {
314 if ((n = strlen(exl->ex_dir)) > maxlen)
318 printf("%-*s ", maxlen, exportlist->ex_dir);
319 grouplist = exportlist->ex_groups;
322 printf("%s%s", grouplist->gr_name,
323 grouplist->gr_next ? "," : "");
324 grouplist = grouplist->gr_next;
327 printf("(everyone)");
329 exportlist = exportlist->ex_next;
331 clnt_destroy(mclient);
335 memset(&dumplist, '\0', sizeof(dumplist));
336 clnt_stat = clnt_call(mclient, MOUNTPROC_DUMP,
337 (xdrproc_t) xdr_void, NULL,
338 (xdrproc_t) xdr_mountlist, (caddr_t) &dumplist,
340 if (clnt_stat != RPC_SUCCESS) {
341 clnt_perror(mclient, "rpc mount dump");
342 clnt_destroy(mclient);
345 clnt_destroy(mclient);
348 for (list = dumplist; list; list = list->ml_next)
350 dumpv = (char **) calloc(n, sizeof (char *));
352 fprintf(stderr, "%s: out of memory\n", program_name);
359 printf("Hosts on %s:\n", hostname);
361 dumpv[i++] = dumplist->ml_hostname;
362 dumplist = dumplist->ml_next;
367 printf("All mount points on %s:\n", hostname);
371 t=malloc(strlen(dumplist->ml_hostname)+strlen(dumplist->ml_directory)+2);
374 fprintf(stderr, "%s: out of memory\n", program_name);
377 sprintf(t, "%s:%s", dumplist->ml_hostname, dumplist->ml_directory);
379 dumplist = dumplist->ml_next;
384 printf("Directories on %s:\n", hostname);
386 dumpv[i++] = dumplist->ml_directory;
387 dumplist = dumplist->ml_next;
391 qsort(dumpv, n, sizeof (char *), dump_cmp);
393 for (i = 0; i < n; i++) {
394 if (i == 0 || strcmp(dumpv[i], dumpv[i - 1]) != 0)
395 printf("%s\n", dumpv[i]);