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 (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include <strings.h>
30 #include <rpc/rpc.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <sys/param.h>
34 #include <rpcsvc/mount.h>
35 
36 #include "rpcsvc/nfs_prot.h"
37 
38 char sharedpath[MAXPATHLEN];
39 fhandle3 *rootfh;
40 
41 /*
42  * The waiting() function returns the value passed in, until something
43  * external modifies it.  In this case, the D script tst.call.d will
44  * modify the value of *a, and thus break the while loop in dotest().
45  *
46  * This serves the purpose of not making the RPC calls until tst.call.d
47  * is active.  Thus, the probes in tst.call.d can fire as a result of
48  * the RPC call in dotest().
49  */
50 
51 int
waiting(volatile int * a)52 waiting(volatile int *a)
53 {
54 	return (*a);
55 }
56 
57 static void
getattr_arginit(void * argp)58 getattr_arginit(void *argp)
59 {
60 	GETATTR3args *args = argp;
61 
62 	args->object.data.data_len = rootfh->fhandle3_len;
63 	args->object.data.data_val = rootfh->fhandle3_val;
64 }
65 
66 static void
setattr_arginit(void * argp)67 setattr_arginit(void *argp)
68 {
69 	SETATTR3args *args = argp;
70 
71 	bzero(args, sizeof (*args));
72 	args->object.data.data_len = rootfh->fhandle3_len;
73 	args->object.data.data_val = rootfh->fhandle3_val;
74 }
75 
76 static void
lookup_arginit(void * argp)77 lookup_arginit(void *argp)
78 {
79 	LOOKUP3args *args = argp;
80 
81 	args->what.name = "giant-skunk";
82 	args->what.dir.data.data_len = rootfh->fhandle3_len;
83 	args->what.dir.data.data_val = rootfh->fhandle3_val;
84 }
85 
86 static void
access_arginit(void * argp)87 access_arginit(void *argp)
88 {
89 	ACCESS3args *args = argp;
90 
91 	args->object.data.data_len = rootfh->fhandle3_len;
92 	args->object.data.data_val = rootfh->fhandle3_val;
93 }
94 
95 static void
commit_arginit(void * argp)96 commit_arginit(void *argp)
97 {
98 	COMMIT3args *args = argp;
99 
100 	bzero(args, sizeof (*args));
101 	args->file.data.data_len = rootfh->fhandle3_len;
102 	args->file.data.data_val = rootfh->fhandle3_val;
103 }
104 
105 static void
create_arginit(void * argp)106 create_arginit(void *argp)
107 {
108 	CREATE3args *args = argp;
109 
110 	bzero(args, sizeof (*args));
111 	args->where.name = "pinky-blue";
112 	args->where.dir.data.data_len = rootfh->fhandle3_len;
113 	args->where.dir.data.data_val = rootfh->fhandle3_val;
114 }
115 
116 static void
fsinfo_arginit(void * argp)117 fsinfo_arginit(void *argp)
118 {
119 	FSINFO3args *args = argp;
120 
121 	args->fsroot.data.data_len = rootfh->fhandle3_len;
122 	args->fsroot.data.data_val = rootfh->fhandle3_val;
123 }
124 
125 static void
fsstat_arginit(void * argp)126 fsstat_arginit(void *argp)
127 {
128 	FSSTAT3args *args = argp;
129 
130 	args->fsroot.data.data_len = rootfh->fhandle3_len;
131 	args->fsroot.data.data_val = rootfh->fhandle3_val;
132 }
133 
134 static void
link_arginit(void * argp)135 link_arginit(void *argp)
136 {
137 	LINK3args *args = argp;
138 
139 	args->file.data.data_len = rootfh->fhandle3_len;
140 	args->file.data.data_val = rootfh->fhandle3_val;
141 	args->link.dir.data.data_len = rootfh->fhandle3_len;
142 	args->link.dir.data.data_val = rootfh->fhandle3_val;
143 	args->link.name = "samf";
144 }
145 
146 static void
mkdir_arginit(void * argp)147 mkdir_arginit(void *argp)
148 {
149 	MKDIR3args *args = argp;
150 
151 	bzero(args, sizeof (*args));
152 	args->where.dir.data.data_len = rootfh->fhandle3_len;
153 	args->where.dir.data.data_val = rootfh->fhandle3_val;
154 	args->where.name = "cookie";
155 }
156 
157 static void
mknod_arginit(void * argp)158 mknod_arginit(void *argp)
159 {
160 	MKNOD3args *args = argp;
161 
162 	bzero(args, sizeof (*args));
163 	args->where.dir.data.data_len = rootfh->fhandle3_len;
164 	args->where.dir.data.data_val = rootfh->fhandle3_val;
165 	args->where.name = "pookie";
166 }
167 
168 static void
null_arginit(void * argp)169 null_arginit(void *argp)
170 {
171 }
172 
173 static void
pathconf_arginit(void * argp)174 pathconf_arginit(void *argp)
175 {
176 	PATHCONF3args *args = argp;
177 
178 	args->object.data.data_len = rootfh->fhandle3_len;
179 	args->object.data.data_val = rootfh->fhandle3_val;
180 }
181 
182 static void
read_arginit(void * argp)183 read_arginit(void *argp)
184 {
185 	READ3args *args = argp;
186 
187 	bzero(args, sizeof (*args));
188 	args->file.data.data_len = rootfh->fhandle3_len;
189 	args->file.data.data_val = rootfh->fhandle3_val;
190 }
191 
192 static void
readdir_arginit(void * argp)193 readdir_arginit(void *argp)
194 {
195 	READDIR3args *args = argp;
196 
197 	bzero(args, sizeof (*args));
198 	args->dir.data.data_len = rootfh->fhandle3_len;
199 	args->dir.data.data_val = rootfh->fhandle3_val;
200 	args->count = 1024;
201 }
202 
203 static void
readdirplus_arginit(void * argp)204 readdirplus_arginit(void *argp)
205 {
206 	READDIRPLUS3args *args = argp;
207 
208 	bzero(args, sizeof (*args));
209 	args->dir.data.data_len = rootfh->fhandle3_len;
210 	args->dir.data.data_val = rootfh->fhandle3_val;
211 	args->dircount = 1024;
212 	args->maxcount = 1024;
213 }
214 
215 static void
readlink_arginit(void * argp)216 readlink_arginit(void *argp)
217 {
218 	READLINK3args *args = argp;
219 
220 	args->symlink.data.data_len = rootfh->fhandle3_len;
221 	args->symlink.data.data_val = rootfh->fhandle3_val;
222 }
223 
224 static void
remove_arginit(void * argp)225 remove_arginit(void *argp)
226 {
227 	REMOVE3args *args = argp;
228 
229 	args->object.dir.data.data_len = rootfh->fhandle3_len;
230 	args->object.dir.data.data_val = rootfh->fhandle3_val;
231 	args->object.name = "antelope";
232 }
233 
234 static void
rename_arginit(void * argp)235 rename_arginit(void *argp)
236 {
237 	RENAME3args *args = argp;
238 
239 	args->from.dir.data.data_len = rootfh->fhandle3_len;
240 	args->from.dir.data.data_val = rootfh->fhandle3_val;
241 	args->from.name = "walter";
242 	args->to.dir.data.data_len = rootfh->fhandle3_len;
243 	args->to.dir.data.data_val = rootfh->fhandle3_val;
244 	args->to.name = "wendy";
245 }
246 
247 static void
rmdir_arginit(void * argp)248 rmdir_arginit(void *argp)
249 {
250 	RMDIR3args *args = argp;
251 
252 	args->object.dir.data.data_len = rootfh->fhandle3_len;
253 	args->object.dir.data.data_val = rootfh->fhandle3_val;
254 	args->object.name = "bunny";
255 }
256 
257 static void
symlink_arginit(void * argp)258 symlink_arginit(void *argp)
259 {
260 	SYMLINK3args *args = argp;
261 
262 	bzero(args, sizeof (*args));
263 	args->where.dir.data.data_len = rootfh->fhandle3_len;
264 	args->where.dir.data.data_val = rootfh->fhandle3_val;
265 	args->where.name = "parlor";
266 	args->symlink.symlink_data = "interior";
267 }
268 
269 static void
write_arginit(void * argp)270 write_arginit(void *argp)
271 {
272 	WRITE3args *args = argp;
273 
274 	bzero(args, sizeof (*args));
275 	args->file.data.data_len = rootfh->fhandle3_len;
276 	args->file.data.data_val = rootfh->fhandle3_val;
277 }
278 
279 typedef void (*call3_arginit_t)(void *);
280 
281 typedef struct {
282 	call3_arginit_t arginit;
283 	rpcproc_t proc;
284 	xdrproc_t xdrargs;
285 	size_t argsize;
286 	xdrproc_t xdrres;
287 	size_t ressize;
288 } call3_test_t;
289 call3_test_t call3_tests[] = {
290 	{getattr_arginit, NFSPROC3_GETATTR, xdr_GETATTR3args,
291 	    sizeof (GETATTR3args), xdr_GETATTR3res, sizeof (GETATTR3res)},
292 	{setattr_arginit, NFSPROC3_SETATTR, xdr_SETATTR3args,
293 	    sizeof (SETATTR3args), xdr_SETATTR3res, sizeof (SETATTR3res)},
294 	{lookup_arginit, NFSPROC3_LOOKUP, xdr_LOOKUP3args,
295 	    sizeof (LOOKUP3args), xdr_LOOKUP3res, sizeof (LOOKUP3res)},
296 	{access_arginit, NFSPROC3_ACCESS, xdr_ACCESS3args,
297 	    sizeof (ACCESS3args), xdr_ACCESS3res, sizeof (ACCESS3res)},
298 	{commit_arginit, NFSPROC3_COMMIT, xdr_COMMIT3args,
299 	    sizeof (COMMIT3args), xdr_COMMIT3res, sizeof (COMMIT3res)},
300 	{create_arginit, NFSPROC3_CREATE, xdr_CREATE3args,
301 	    sizeof (CREATE3args), xdr_CREATE3res, sizeof (CREATE3res)},
302 	{fsinfo_arginit, NFSPROC3_FSINFO, xdr_FSINFO3args,
303 	    sizeof (FSINFO3args), xdr_FSINFO3res, sizeof (FSINFO3res)},
304 	{fsstat_arginit, NFSPROC3_FSSTAT, xdr_FSSTAT3args,
305 	    sizeof (FSSTAT3args), xdr_FSSTAT3res, sizeof (FSSTAT3res)},
306 	{link_arginit, NFSPROC3_LINK, xdr_LINK3args,
307 	    sizeof (LINK3args), xdr_LINK3res, sizeof (LINK3res)},
308 	{mkdir_arginit, NFSPROC3_MKDIR, xdr_MKDIR3args,
309 	    sizeof (MKDIR3args), xdr_MKDIR3res, sizeof (MKDIR3res)},
310 	{mknod_arginit, NFSPROC3_MKNOD, xdr_MKNOD3args,
311 	    sizeof (MKNOD3args), xdr_MKNOD3res, sizeof (MKNOD3res)},
312 	/*
313 	 * NULL proc is special.  Rather than special case its zero-sized
314 	 * args/results, we give it a small nonzero size, so as to not
315 	 * make realloc() do the wrong thing.
316 	 */
317 	{null_arginit, NFSPROC3_NULL, xdr_void, sizeof (int), xdr_void,
318 	    sizeof (int)},
319 	{pathconf_arginit, NFSPROC3_PATHCONF, xdr_PATHCONF3args,
320 	    sizeof (PATHCONF3args), xdr_PATHCONF3res, sizeof (PATHCONF3res)},
321 	{read_arginit, NFSPROC3_READ, xdr_READ3args,
322 	    sizeof (READ3args), xdr_READ3res, sizeof (READ3res)},
323 	{readdir_arginit, NFSPROC3_READDIR, xdr_READDIR3args,
324 	    sizeof (READDIR3args), xdr_READDIR3res, sizeof (READDIR3res)},
325 	{readdirplus_arginit, NFSPROC3_READDIRPLUS, xdr_READDIRPLUS3args,
326 	    sizeof (READDIRPLUS3args), xdr_READDIRPLUS3res,
327 	    sizeof (READDIRPLUS3res)},
328 	{readlink_arginit, NFSPROC3_READLINK, xdr_READLINK3args,
329 	    sizeof (READLINK3args), xdr_READLINK3res, sizeof (READLINK3res)},
330 	{remove_arginit, NFSPROC3_REMOVE, xdr_REMOVE3args,
331 	    sizeof (REMOVE3args), xdr_REMOVE3res, sizeof (REMOVE3res)},
332 	{rename_arginit, NFSPROC3_RENAME, xdr_RENAME3args,
333 	    sizeof (RENAME3args), xdr_RENAME3res, sizeof (RENAME3res)},
334 	{rmdir_arginit, NFSPROC3_RMDIR, xdr_RMDIR3args,
335 	    sizeof (RMDIR3args), xdr_RMDIR3res, sizeof (RMDIR3res)},
336 	{symlink_arginit, NFSPROC3_SYMLINK, xdr_SYMLINK3args,
337 	    sizeof (SYMLINK3args), xdr_SYMLINK3res, sizeof (SYMLINK3res)},
338 	{write_arginit, NFSPROC3_WRITE, xdr_WRITE3args,
339 	    sizeof (WRITE3args), xdr_WRITE3res, sizeof (WRITE3res)},
340 	{NULL}
341 };
342 
343 int
dotest(void)344 dotest(void)
345 {
346 	CLIENT *client, *mountclient;
347 	AUTH *auth;
348 	struct timeval timeout;
349 	caddr_t args, res;
350 	enum clnt_stat status;
351 	rpcproc_t proc;
352 	call3_test_t *test;
353 	void *argbuf = NULL;
354 	void *resbuf = NULL;
355 	struct mountres3 mountres3;
356 	char *sp;
357 	volatile int a = 0;
358 
359 	while (waiting(&a) == 0)
360 		continue;
361 
362 	timeout.tv_sec = 30;
363 	timeout.tv_usec = 0;
364 
365 	mountclient = clnt_create("localhost", MOUNTPROG, MOUNTVERS3, "tcp");
366 	if (mountclient == NULL) {
367 		clnt_pcreateerror("clnt_create mount");
368 		return (1);
369 	}
370 	auth = authsys_create_default();
371 	mountclient->cl_auth = auth;
372 	sp = sharedpath;
373 	bzero(&mountres3, sizeof (mountres3));
374 	status = clnt_call(mountclient, MOUNTPROC_MNT,
375 	    xdr_dirpath, (char *)&sp,
376 	    xdr_mountres3, (char *)&mountres3,
377 	    timeout);
378 	if (status != RPC_SUCCESS) {
379 		clnt_perror(mountclient, "mnt");
380 		return (1);
381 	}
382 	if (mountres3.fhs_status != 0) {
383 		fprintf(stderr, "MOUNTPROG/MOUNTVERS3 failed %d\n",
384 		    mountres3.fhs_status);
385 		return (1);
386 	}
387 	rootfh = &mountres3.mountres3_u.mountinfo.fhandle;
388 
389 	client = clnt_create("localhost", NFS3_PROGRAM, NFS_V3, "tcp");
390 	if (client == NULL) {
391 		clnt_pcreateerror("clnt_create");
392 		return (1);
393 	}
394 	client->cl_auth = auth;
395 
396 	for (test = call3_tests; test->arginit; ++test) {
397 		argbuf = realloc(argbuf, test->argsize);
398 		resbuf = realloc(resbuf, test->ressize);
399 		if ((argbuf == NULL) || (resbuf == NULL)) {
400 			perror("realloc() failed");
401 			return (1);
402 		}
403 		(test->arginit)(argbuf);
404 		bzero(resbuf, test->ressize);
405 		status = clnt_call(client, test->proc,
406 		    test->xdrargs, argbuf,
407 		    test->xdrres, resbuf,
408 		    timeout);
409 		if (status != RPC_SUCCESS)
410 			clnt_perror(client, "call");
411 	}
412 
413 	status = clnt_call(mountclient, MOUNTPROC_UMNT,
414 	    xdr_dirpath, (char *)&sp,
415 	    xdr_void, NULL,
416 	    timeout);
417 	if (status != RPC_SUCCESS)
418 		clnt_perror(mountclient, "umnt");
419 
420 	return (0);
421 }
422 
423 /*ARGSUSED*/
424 int
main(int argc,char ** argv)425 main(int argc, char **argv)
426 {
427 	char shareline[BUFSIZ], unshareline[BUFSIZ];
428 	int rc;
429 
430 	(void) snprintf(sharedpath, sizeof (sharedpath),
431 	    "/tmp/nfsv3test.%d", getpid());
432 	(void) snprintf(shareline, sizeof (shareline),
433 	    "mkdir %s ; share %s", sharedpath, sharedpath);
434 	(void) snprintf(unshareline, sizeof (unshareline),
435 	    "unshare %s ; rmdir %s", sharedpath, sharedpath);
436 
437 	(void) system(shareline);
438 	rc = dotest();
439 	(void) system(unshareline);
440 
441 	return (rc);
442 }
443