xref: /netbsd-src/usr.sbin/rpc.lockd/test.c (revision 666bec36a3d4c6f74f8d9d64dcf3012413c47e30)
1 /*	$NetBSD: test.c,v 1.8 2016/03/12 02:07:44 dholland Exp $	*/
2 
3 #include <sys/cdefs.h>
4 #include <stdio.h>
5 #include <string.h>
6 #include <err.h>
7 #include <rpc/rpc.h>
8 #include <rpcsvc/nlm_prot.h>
9 
10 #ifndef lint
11 #if 0
12 static char sccsid[] = "from: @(#)nlm_prot.x 1.8 87/09/21 Copyr 1987 Sun Micro";
13 static char sccsid[] = "from: * @(#)nlm_prot.x	2.1 88/08/01 4.0 RPCSRC";
14 #else
15 __RCSID("$NetBSD: test.c,v 1.8 2016/03/12 02:07:44 dholland Exp $");
16 #endif
17 #endif				/* not lint */
18 
19 /* Default timeout can be changed using clnt_control() */
20 static struct timeval TIMEOUT = {0, 0};
21 
22 nlm_testres *
nlm_test_1(struct nlm_testargs * argp,CLIENT * clnt)23 nlm_test_1(struct nlm_testargs *argp, CLIENT *clnt)
24 {
25 	enum clnt_stat st;
26 	static nlm_testres res;
27 
28 	(void)memset(&res, 0, sizeof(res));
29 	st = clnt_call(clnt, NLM_TEST, xdr_nlm_testargs, argp, xdr_nlm_testres,
30 			&res, TIMEOUT);
31 	if (st != RPC_SUCCESS)
32 		return NULL;
33 	return &res;
34 }
35 
36 
37 nlm_res *
nlm_lock_1(struct nlm_lockargs * argp,CLIENT * clnt)38 nlm_lock_1(struct nlm_lockargs *argp, CLIENT *clnt)
39 {
40 	enum clnt_stat st;
41 	static nlm_res res;
42 
43 	(void)memset(&res, 0, sizeof(res));
44 	st = clnt_call(clnt, NLM_LOCK, xdr_nlm_lockargs, argp, xdr_nlm_res,
45 			&res, TIMEOUT);
46 	if (st != RPC_SUCCESS) {
47 		printf("clnt_call returns %d\n", st);
48 		clnt_perror(clnt, "humbug");
49 		return NULL;
50 	}
51 	return &res;
52 }
53 
54 
55 nlm_res *
nlm_cancel_1(struct nlm_cancargs * argp,CLIENT * clnt)56 nlm_cancel_1(struct nlm_cancargs *argp, CLIENT *clnt)
57 {
58 	enum clnt_stat st;
59 	static nlm_res res;
60 
61 	(void)memset(&res, 0, sizeof(res));
62 	st = clnt_call(clnt, NLM_CANCEL, xdr_nlm_cancargs, argp, xdr_nlm_res,
63 			&res, TIMEOUT);
64 	if (st != RPC_SUCCESS) {
65 		return NULL;
66 	}
67 	return &res;
68 }
69 
70 
71 nlm_res *
nlm_unlock_1(struct nlm_unlockargs * argp,CLIENT * clnt)72 nlm_unlock_1(struct nlm_unlockargs *argp, CLIENT *clnt)
73 {
74 	enum clnt_stat st;
75 	static nlm_res res;
76 
77 	(void)memset(&res, 0, sizeof(res));
78 	st = clnt_call(clnt, NLM_UNLOCK, xdr_nlm_unlockargs, argp, xdr_nlm_res,
79 			&res, TIMEOUT);
80 	if (st != RPC_SUCCESS) {
81 		return NULL;
82 	}
83 	return &res;
84 }
85 
86 
87 nlm_res *
nlm_granted_1(struct nlm_testargs * argp,CLIENT * clnt)88 nlm_granted_1(struct nlm_testargs *argp, CLIENT *clnt)
89 {
90 	enum clnt_stat st;
91 	static nlm_res res;
92 
93 	(void)memset(&res, 0, sizeof(res));
94 	st = clnt_call(clnt, NLM_GRANTED, xdr_nlm_testargs, argp, xdr_nlm_res,
95 			&res, TIMEOUT);
96 	if (st != RPC_SUCCESS) {
97 		return NULL;
98 	}
99 	return &res;
100 }
101 
102 
103 void *
nlm_test_msg_1(struct nlm_testargs * argp,CLIENT * clnt)104 nlm_test_msg_1(struct nlm_testargs *argp, CLIENT *clnt)
105 {
106 	enum clnt_stat st;
107 	static char res;
108 
109 	(void)memset(&res, 0, sizeof(res));
110 	st = clnt_call(clnt, NLM_TEST_MSG, xdr_nlm_testargs, argp, xdr_void,
111 			&res, TIMEOUT);
112 	if (st != RPC_SUCCESS) {
113 		return NULL;
114 	}
115 	return &res;
116 }
117 
118 
119 void *
nlm_lock_msg_1(struct nlm_lockargs * argp,CLIENT * clnt)120 nlm_lock_msg_1(struct nlm_lockargs *argp, CLIENT *clnt)
121 {
122 	enum clnt_stat st;
123 	static char res;
124 
125 	(void)memset(&res, 0, sizeof(res));
126 	st = clnt_call(clnt, NLM_LOCK_MSG, xdr_nlm_lockargs, argp, xdr_void,
127 			NULL, TIMEOUT);
128 	if (st != RPC_SUCCESS) {
129 		clnt_perror(clnt, "nlm_lock_msg_1");
130 		return NULL;
131 	}
132 	return &res;
133 }
134 
135 
136 void *
nlm_cancel_msg_1(struct nlm_cancargs * argp,CLIENT * clnt)137 nlm_cancel_msg_1(struct nlm_cancargs *argp, CLIENT *clnt)
138 {
139 	enum clnt_stat st;
140 	static char res;
141 
142 	(void)memset(&res, 0, sizeof(res));
143 	st = clnt_call(clnt, NLM_CANCEL_MSG, xdr_nlm_cancargs, argp, xdr_void,
144 			&res, TIMEOUT);
145 	if (st != RPC_SUCCESS) {
146 		return NULL;
147 	}
148 	return &res;
149 }
150 
151 
152 void *
nlm_unlock_msg_1(struct nlm_unlockargs * argp,CLIENT * clnt)153 nlm_unlock_msg_1(struct nlm_unlockargs *argp, CLIENT *clnt)
154 {
155 	enum clnt_stat st;
156 	static char res;
157 
158 	(void)memset(&res, 0, sizeof(res));
159 	st = clnt_call(clnt, NLM_UNLOCK_MSG, xdr_nlm_unlockargs, argp, xdr_void,
160 			&res, TIMEOUT);
161 	if (st != RPC_SUCCESS) {
162 		return NULL;
163 	}
164 	return &res;
165 }
166 
167 
168 void *
nlm_granted_msg_1(struct nlm_testargs * argp,CLIENT * clnt)169 nlm_granted_msg_1(struct nlm_testargs *argp, CLIENT *clnt)
170 {
171 	enum clnt_stat st;
172 	static char res;
173 
174 	(void)memset(&res, 0, sizeof(res));
175 	st = clnt_call(clnt, NLM_GRANTED_MSG, xdr_nlm_testargs, argp, xdr_void,
176 			&res, TIMEOUT);
177 	if (st != RPC_SUCCESS) {
178 		return NULL;
179 	}
180 	return &res;
181 }
182 
183 
184 void *
nlm_test_res_1(nlm_testres * argp,CLIENT * clnt)185 nlm_test_res_1(nlm_testres *argp, CLIENT *clnt)
186 {
187 	enum clnt_stat st;
188 	static char res;
189 
190 	(void)memset(&res, 0, sizeof(res));
191 	st = clnt_call(clnt, NLM_TEST_RES, xdr_nlm_testres, argp, xdr_void,
192 			&res, TIMEOUT);
193 	if (st != RPC_SUCCESS) {
194 		return NULL;
195 	}
196 	return &res;
197 }
198 
199 
200 void *
nlm_lock_res_1(nlm_res * argp,CLIENT * clnt)201 nlm_lock_res_1(nlm_res *argp, CLIENT *clnt)
202 {
203 	enum clnt_stat st;
204 	static char res;
205 
206 	(void)memset(&res, 0, sizeof(res));
207 	st = clnt_call(clnt, NLM_LOCK_RES, xdr_nlm_res, argp, xdr_void,
208 			&res, TIMEOUT);
209 	if (st != RPC_SUCCESS) {
210 		return NULL;
211 	}
212 	return &res;
213 }
214 
215 
216 void *
nlm_cancel_res_1(nlm_res * argp,CLIENT * clnt)217 nlm_cancel_res_1(nlm_res *argp, CLIENT *clnt)
218 {
219 	enum clnt_stat st;
220 	static char res;
221 
222 	(void)memset(&res, 0, sizeof(res));
223 	st = clnt_call(clnt, NLM_CANCEL_RES, xdr_nlm_res, argp, xdr_void,
224 			&res, TIMEOUT);
225 	if (st != RPC_SUCCESS) {
226 		return NULL;
227 	}
228 	return &res;
229 }
230 
231 
232 void *
nlm_unlock_res_1(nlm_res * argp,CLIENT * clnt)233 nlm_unlock_res_1(nlm_res *argp, CLIENT *clnt)
234 {
235 	enum clnt_stat st;
236 	static char res;
237 
238 	(void)memset(&res, 0, sizeof(res));
239 	st = clnt_call(clnt, NLM_UNLOCK_RES, xdr_nlm_res, argp, xdr_void,
240 			&res, TIMEOUT);
241 	if (st != RPC_SUCCESS) {
242 		return NULL;
243 	}
244 	return &res;
245 }
246 
247 
248 void *
nlm_granted_res_1(nlm_res * argp,CLIENT * clnt)249 nlm_granted_res_1(nlm_res *argp, CLIENT *clnt)
250 {
251 	enum clnt_stat st;
252 	static char res;
253 
254 	(void)memset(&res, 0, sizeof(res));
255 	st = clnt_call(clnt, NLM_GRANTED_RES, xdr_nlm_res, argp, xdr_void,
256 			&res, TIMEOUT);
257 	if (st != RPC_SUCCESS) {
258 		return NULL;
259 	}
260 	return &res;
261 }
262 
263 
264 nlm_shareres *
nlm_share_3(nlm_shareargs * argp,CLIENT * clnt)265 nlm_share_3(nlm_shareargs *argp, CLIENT *clnt)
266 {
267 	enum clnt_stat st;
268 	static nlm_shareres res;
269 
270 	(void)memset(&res, 0, sizeof(res));
271 	st = clnt_call(clnt, NLM_SHARE, xdr_nlm_shareargs, argp,
272 			xdr_nlm_shareres, &res, TIMEOUT);
273 	if (st != RPC_SUCCESS) {
274 		return NULL;
275 	}
276 	return &res;
277 }
278 
279 
280 nlm_shareres *
nlm_unshare_3(nlm_shareargs * argp,CLIENT * clnt)281 nlm_unshare_3(nlm_shareargs *argp, CLIENT *clnt)
282 {
283 	enum clnt_stat st;
284 	static nlm_shareres res;
285 
286 	(void)memset(&res, 0, sizeof(res));
287 	st = clnt_call(clnt, NLM_UNSHARE, xdr_nlm_shareargs, argp,
288 			xdr_nlm_shareres, &res, TIMEOUT);
289 	if (st != RPC_SUCCESS) {
290 		return NULL;
291 	}
292 	return &res;
293 }
294 
295 
296 nlm_res *
nlm_nm_lock_3(nlm_lockargs * argp,CLIENT * clnt)297 nlm_nm_lock_3(nlm_lockargs *argp, CLIENT *clnt)
298 {
299 	enum clnt_stat st;
300 	static nlm_res res;
301 
302 	(void)memset(&res, 0, sizeof(res));
303 	st = clnt_call(clnt, NLM_NM_LOCK, xdr_nlm_lockargs, argp, xdr_nlm_res,
304 			&res, TIMEOUT);
305 	if (st != RPC_SUCCESS) {
306 		return NULL;
307 	}
308 	return &res;
309 }
310 
311 
312 void *
nlm_free_all_3(nlm_notify * argp,CLIENT * clnt)313 nlm_free_all_3(nlm_notify *argp, CLIENT *clnt)
314 {
315 	enum clnt_stat st;
316 	static char res;
317 
318 	(void)memset(&res, 0, sizeof(res));
319 	st = clnt_call(clnt, NLM_FREE_ALL, xdr_nlm_notify, argp, xdr_void,
320 			&res, TIMEOUT);
321 	if (st != RPC_SUCCESS) {
322 		return NULL;
323 	}
324 	return &res;
325 }
326 
327 
328 int
main(int argc,char ** argv)329 main(int argc, char **argv)
330 {
331 	CLIENT *cli;
332 	nlm_res res_block;
333 	nlm_res *out;
334 	nlm_lockargs arg;
335 	struct timeval tim;
336 
337 	if (argc != 2) {
338 		errx(1, "usage: %s host", argv[0]);
339 	}
340 
341 	printf("Creating client for host %s\n", argv[1]);
342 	cli = clnt_create(argv[1], NLM_PROG, NLM_VERS, "udp");
343 	if (!cli) {
344 		errx(1, "Failed to create client");
345 		/* NOTREACHED */
346 	}
347 	clnt_control(cli, CLGET_TIMEOUT, (void *)&tim);
348 	printf("Default timeout was %lld.%d\n",
349 		(long long)tim.tv_sec, tim.tv_usec);
350 	tim.tv_usec = -1;
351 	tim.tv_sec = -1;
352 	clnt_control(cli, CLSET_TIMEOUT, (void *)&tim);
353 	clnt_control(cli, CLGET_TIMEOUT, (void *)&tim);
354 	printf("timeout now %lld.%u\n", (long long)tim.tv_sec, tim.tv_usec);
355 
356 
357 	arg.cookie.n_len = 4;
358 	arg.cookie.n_bytes = "hello";
359 	arg.block = 0;
360 	arg.exclusive = 0;
361 	arg.reclaim = 0;
362 	arg.state = 0x1234;
363 	arg.alock.caller_name = "localhost";
364 	arg.alock.fh.n_len = 32;
365 	arg.alock.fh.n_bytes = "\x04\x04\x02\x00\x01\x00\x00\x00\x0c\x00\x00\x00\xff\xff\xff\xd0\x16\x00\x00\x5b\x7c\xff\xff\xff\xec\x2f\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x19\x54\xef\xbf\xd7\x94";
366 	arg.alock.oh.n_len = 8;
367 	arg.alock.oh.n_bytes = "\x00\x00\x02\xff\xff\xff\xd3";
368 	arg.alock.svid = 0x5678;
369 	arg.alock.l_offset = 0;
370 	arg.alock.l_len = 100;
371 
372 	res_block.stat.stat = nlm_granted;
373 	res_block.cookie.n_bytes = "hello";
374 	res_block.cookie.n_len = 5;
375 
376 #if 0
377 	if (nlm_lock_res_1(&res_block, cli))
378 		printf("Success!\n");
379 	else
380 		printf("Fail\n");
381 #else
382 	(void)res_block;
383 
384 	out = nlm_lock_msg_1(&arg, cli);
385 	if (out != NULL) {
386 		printf("Success!\n");
387 		printf("out->stat = %d", out->stat.stat);
388 	} else {
389 		printf("Fail\n");
390 	}
391 #endif
392 
393 	return 0;
394 }
395