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);
153 int main(int argc, char **argv)
155 char hostname_buf[MAXHOSTLEN];
157 enum clnt_stat clnt_stat;
159 struct sockaddr_in server_addr;
161 struct timeval total_timeout;
162 struct timeval pertry_timeout;
166 exports exportlist, exl;
174 program_name = argv[0];
175 while ((c = getopt_long(argc, argv, "adehv", longopts, NULL)) != EOF) {
190 printf("%s\n", version);
203 switch (aflag + dflag + eflag) {
210 fprintf(stderr, "%s: only one of -a, -d or -e is allowed\n",
218 if (gethostname(hostname_buf, MAXHOSTLEN) < 0) {
219 perror("getting hostname");
222 hostname = hostname_buf;
228 fprintf(stderr, "%s: only one hostname is allowed\n",
234 if (inet_aton(hostname, &server_addr.sin_addr)) {
235 server_addr.sin_family = AF_INET;
238 if ((hp = gethostbyname(hostname)) == NULL) {
239 fprintf(stderr, "%s: can't get address for %s\n",
240 program_name, hostname);
243 server_addr.sin_family = AF_INET;
244 memcpy(&server_addr.sin_addr, hp->h_addr, hp->h_length);
247 /* create mount deamon client */
250 msock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
252 if (nfs_getport_ping((struct sockaddr *)&server_addr,
253 sizeof(server_addr), MOUNTPROG,
254 MOUNTVERS, IPPROTO_TCP)) {
255 ret = connect_nb(msock, &server_addr, 0);
256 if (ret == 0) /* success */
257 mclient = clnttcp_create(&server_addr,
258 MOUNTPROG, MOUNTVERS, &msock,
267 if (nfs_getport_ping((struct sockaddr *)&server_addr,
268 sizeof(server_addr), MOUNTPROG,
269 MOUNTVERS, IPPROTO_UDP)) {
270 clnt_pcreateerror("showmount");
274 pertry_timeout.tv_sec = TIMEOUT_UDP;
275 pertry_timeout.tv_usec = 0;
276 if ((mclient = clntudp_create(&server_addr,
277 MOUNTPROG, MOUNTVERS, pertry_timeout, &msock)) == NULL) {
278 clnt_pcreateerror("mount clntudp_create");
282 mclient->cl_auth = authunix_create_default();
283 total_timeout.tv_sec = TOTAL_TIMEOUT;
284 total_timeout.tv_usec = 0;
287 memset(&exportlist, '\0', sizeof(exportlist));
289 clnt_stat = clnt_call(mclient, MOUNTPROC_EXPORT,
290 (xdrproc_t) xdr_void, NULL,
291 (xdrproc_t) xdr_exports, (caddr_t) &exportlist,
293 if (clnt_stat != RPC_SUCCESS) {
294 clnt_perror(mclient, "rpc mount export");
295 clnt_destroy(mclient);
299 printf("Export list for %s:\n", hostname);
301 for (exl = exportlist; exl; exl = exl->ex_next) {
302 if ((n = strlen(exl->ex_dir)) > maxlen)
306 printf("%-*s ", maxlen, exportlist->ex_dir);
307 grouplist = exportlist->ex_groups;
310 printf("%s%s", grouplist->gr_name,
311 grouplist->gr_next ? "," : "");
312 grouplist = grouplist->gr_next;
315 printf("(everyone)");
317 exportlist = exportlist->ex_next;
319 clnt_destroy(mclient);
323 memset(&dumplist, '\0', sizeof(dumplist));
324 clnt_stat = clnt_call(mclient, MOUNTPROC_DUMP,
325 (xdrproc_t) xdr_void, NULL,
326 (xdrproc_t) xdr_mountlist, (caddr_t) &dumplist,
328 if (clnt_stat != RPC_SUCCESS) {
329 clnt_perror(mclient, "rpc mount dump");
330 clnt_destroy(mclient);
333 clnt_destroy(mclient);
336 for (list = dumplist; list; list = list->ml_next)
338 dumpv = (char **) calloc(n, sizeof (char *));
340 fprintf(stderr, "%s: out of memory\n", program_name);
347 printf("Hosts on %s:\n", hostname);
349 dumpv[i++] = dumplist->ml_hostname;
350 dumplist = dumplist->ml_next;
355 printf("All mount points on %s:\n", hostname);
359 t=malloc(strlen(dumplist->ml_hostname)+strlen(dumplist->ml_directory)+2);
362 fprintf(stderr, "%s: out of memory\n", program_name);
365 sprintf(t, "%s:%s", dumplist->ml_hostname, dumplist->ml_directory);
367 dumplist = dumplist->ml_next;
372 printf("Directories on %s:\n", hostname);
374 dumpv[i++] = dumplist->ml_directory;
375 dumplist = dumplist->ml_next;
379 qsort(dumpv, n, sizeof (char *), dump_cmp);
381 for (i = 0; i < n; i++) {
382 if (i == 0 || strcmp(dumpv[i], dumpv[i - 1]) != 0)
383 printf("%s\n", dumpv[i]);