xref: /illumos-gate/usr/src/cmd/fs.d/nfs/dfshares/dfshares.c (revision dd72704bd9e794056c558153663c739e2012d721)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
28 /*	  All Rights Reserved  	*/
29 
30 /*
31  * University Copyright- Copyright (c) 1982, 1986, 1988
32  * The Regents of the University of California
33  * All Rights Reserved
34  *
35  * University Acknowledgment- Portions of this document are derived from
36  * software developed by the University of California, Berkeley, and its
37  * contributors.
38  */
39 
40 /*
41  * nfs dfshares
42  */
43 #include <stdio.h>
44 #include <string.h>
45 #include <rpc/rpc.h>
46 #include <rpc/rpcb_clnt.h>
47 #include <sys/socket.h>
48 #include <netdb.h>
49 #include <sys/time.h>
50 #include <sys/errno.h>
51 #include <nfs/nfs.h>
52 #include <rpcsvc/mount.h>
53 
54 int hflg;
55 void pr_exports();
56 void free_ex();
57 void usage();
58 
59 int
60 main(int argc, char *argv[])
61 {
62 
63 	char hostbuf[256];
64 	extern int optind;
65 	extern char *optarg;
66 	int i, c;
67 
68 	while ((c = getopt(argc, argv, "h")) != EOF) {
69 		switch (c) {
70 		case 'h':
71 			hflg++;
72 			break;
73 		default:
74 			usage();
75 			exit(1);
76 		}
77 	}
78 
79 	if (optind < argc) {
80 		for (i = optind; i < argc; i++)
81 			pr_exports(argv[i]);
82 	} else {
83 		if (gethostname(hostbuf, sizeof (hostbuf)) < 0) {
84 			perror("nfs dfshares: gethostname");
85 			exit(1);
86 		}
87 		pr_exports(hostbuf);
88 	}
89 
90 	return (0);
91 }
92 
93 struct	timeval	rpc_totout_new = {15, 0};
94 
95 void
96 pr_exports(host)
97 	char *host;
98 {
99 	CLIENT *cl;
100 	struct exportnode *ex = NULL;
101 	enum clnt_stat err;
102 	struct	timeval	tout, rpc_totout_old;
103 
104 	(void) __rpc_control(CLCR_GET_RPCB_TIMEOUT, &rpc_totout_old);
105 	(void) __rpc_control(CLCR_SET_RPCB_TIMEOUT, &rpc_totout_new);
106 
107 	/*
108 	 * First try circuit, then drop back to datagram if
109 	 * circuit is unavailable (an old version of mountd perhaps)
110 	 * Using circuit is preferred because it can handle
111 	 * arbitrarily long export lists.
112 	 */
113 	cl = clnt_create(host, MOUNTPROG, MOUNTVERS, "circuit_n");
114 	if (cl == NULL) {
115 		if (rpc_createerr.cf_stat == RPC_PROGNOTREGISTERED)
116 			cl = clnt_create(host, MOUNTPROG, MOUNTVERS,
117 					"datagram_n");
118 		if (cl == NULL) {
119 			(void) fprintf(stderr, "nfs dfshares:");
120 			clnt_pcreateerror(host);
121 			(void) __rpc_control(CLCR_SET_RPCB_TIMEOUT,
122 				&rpc_totout_old);
123 			exit(1);
124 		}
125 	}
126 
127 	(void) __rpc_control(CLCR_SET_RPCB_TIMEOUT, &rpc_totout_old);
128 	tout.tv_sec = 10;
129 	tout.tv_usec = 0;
130 
131 	if (err = clnt_call(cl, MOUNTPROC_EXPORT, xdr_void,
132 	    0, xdr_exports, (caddr_t)&ex, tout)) {
133 		(void) fprintf(stderr, "nfs dfshares: %s\n", clnt_sperrno(err));
134 		clnt_destroy(cl);
135 		exit(1);
136 	}
137 
138 	if (ex == NULL) {
139 		clnt_destroy(cl);
140 		exit(1);
141 	}
142 
143 	if (!hflg) {
144 		printf("%-35s %12s %-8s  %s\n",
145 			"RESOURCE", "SERVER", "ACCESS", "TRANSPORT");
146 		hflg++;
147 	}
148 
149 	while (ex) {
150 		printf("%10s:%-24s %12s %-8s  %s\n",
151 			host, ex->ex_dir, host, " -", " -");
152 		ex = ex->ex_next;
153 	}
154 	free_ex(ex);
155 	clnt_destroy(cl);
156 }
157 
158 void
159 free_ex(ex)
160 	struct exportnode *ex;
161 {
162 	struct groupnode *gr, *tmpgr;
163 	struct exportnode *tmpex;
164 
165 	while (ex) {
166 		free(ex->ex_dir);
167 		gr = ex->ex_groups;
168 		while (gr) {
169 			tmpgr = gr->gr_next;
170 			free((char *)gr);
171 			gr = tmpgr;
172 		}
173 		tmpex = ex;
174 		ex = ex->ex_next;
175 		free((char *)tmpex);
176 	}
177 }
178 
179 void
180 usage()
181 {
182 	(void) fprintf(stderr, "Usage: dfshares [-h] [host ...]\n");
183 }
184