summaryrefslogtreecommitdiffstats
path: root/contrib/netbsd-tests/lib/libc/rpc/t_rpc.c
blob: 9c14a22fdc3c866c297523e2dc255d34ae5bcede (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
/*	$NetBSD: t_rpc.c,v 1.10 2016/08/27 14:36:22 christos Exp $	*/

#include <sys/cdefs.h>
__RCSID("$NetBSD: t_rpc.c,v 1.10 2016/08/27 14:36:22 christos Exp $");

#include <sys/types.h>
#include <sys/socket.h>
#include <rpc/rpc.h>
#include <stdlib.h>
#include <string.h>
#include <err.h>
#include <netdb.h>
#include <stdio.h>
#include <errno.h>
#include <unistd.h>

#ifndef TEST
#include <atf-c.h>

#define ERRX(ev, msg, ...)	ATF_REQUIRE_MSG(0, msg, __VA_ARGS__)

#define SKIPX(ev, msg, ...)	do {			\
	atf_tc_skip(msg, __VA_ARGS__);			\
	return ev;					\
} while(/*CONSTCOND*/0)

#ifdef __FreeBSD__
#define SKIPXI(ev, msg, ...)	do {			\
	atf_tc_skip(msg, __VA_ARGS__);			\
	return ev;					\
} while(/*CONSTCOND*/0)
#endif

#else
#define ERRX(ev, msg, ...)	errx(EXIT_FAILURE, msg, __VA_ARGS__)
#define SKIPX(ev, msg, ...)	errx(EXIT_FAILURE, msg, __VA_ARGS__)
#endif

#ifdef DEBUG
#define DPRINTF(...)	printf(__VA_ARGS__)
#else
#define DPRINTF(...)
#endif


#define RPCBPROC_NULL 0

/* XXX (ngie): for clarity on what needs to be reverted later. */
#define	__FreeBSD_bug_216954__
#ifdef	__FreeBSD_bug_216954__
#include <signal.h>
#endif

static int
reply(caddr_t replyp, struct netbuf * raddrp, struct netconfig * nconf)
{
	char host[NI_MAXHOST];
	struct sockaddr *sock = raddrp->buf;
	int error;


	error = getnameinfo(sock, sock->sa_len, host, sizeof(host), NULL, 0, 0);
	if (error)
		warnx("Cannot resolve address (%s)", gai_strerror(error));
	else
		printf("response from: %s\n", host);
	return 0;
}

#ifdef __FreeBSD__
#define	__rpc_control	rpc_control
#endif

extern bool_t __rpc_control(int, void *);

static void
onehost(const char *host, const char *transp)
{
	CLIENT         *clnt;
	struct netbuf   addr;
	struct timeval  tv;

	/*
	 * Magic!
	 */
	tv.tv_sec = 0;
	tv.tv_usec = 500000;
#define CLCR_SET_RPCB_TIMEOUT   2
	__rpc_control(CLCR_SET_RPCB_TIMEOUT, &tv);

	if ((clnt = clnt_create(host, RPCBPROG, RPCBVERS, transp)) == NULL)
		SKIPX(, "clnt_create (%s)", clnt_spcreateerror(""));

	tv.tv_sec = 1;
	tv.tv_usec = 0;
#ifdef __FreeBSD__
	if (clnt_call(clnt, RPCBPROC_NULL, (xdrproc_t)xdr_void, NULL,
	    (xdrproc_t)xdr_void, NULL, tv)
	    != RPC_SUCCESS)
#else
	if (clnt_call(clnt, RPCBPROC_NULL, xdr_void, NULL, xdr_void, NULL, tv)
	    != RPC_SUCCESS)
#endif
		ERRX(, "clnt_call (%s)", clnt_sperror(clnt, ""));
	clnt_control(clnt, CLGET_SVC_ADDR, (char *) &addr);
	reply(NULL, &addr, NULL);
}

#define PROGNUM 0x81
#define VERSNUM 0x01
#define PLUSONE 1
#define DESTROY 2

static struct timeval 	tout = {1, 0};

static void
server(struct svc_req *rqstp, SVCXPRT *transp)
{
	int num;

	DPRINTF("Starting server\n");

	switch (rqstp->rq_proc) {
	case NULLPROC:
		if (!svc_sendreply(transp, (xdrproc_t)xdr_void, NULL))
			ERRX(, "svc_sendreply failed %d", 0);
		return;
	case PLUSONE:
		break;
	case DESTROY:
		if (!svc_sendreply(transp, (xdrproc_t)xdr_void, NULL))
			ERRX(, "svc_sendreply failed %d", 0);
		svc_destroy(transp);
		exit(0);
	default:
		svcerr_noproc(transp);
		return;
	}

	if (!svc_getargs(transp, (xdrproc_t)xdr_int, (void *)&num)) {
		svcerr_decode(transp);
		return;
	}
	DPRINTF("About to increment\n");
	num++;
	if (!svc_sendreply(transp, (xdrproc_t)xdr_int, (void *)&num))
		ERRX(, "svc_sendreply failed %d", 1);
	DPRINTF("Leaving server procedure.\n");
}

static int
rawtest(const char *arg)
{
	CLIENT         *clnt;
	SVCXPRT        *svc;
	int 		num, resp;
	enum clnt_stat  rv;

	if (arg)
		num = atoi(arg);
	else
		num = 0;

	svc = svc_raw_create();
	if (svc == NULL)
		ERRX(EXIT_FAILURE, "Cannot create server %d", num);
	if (!svc_reg(svc, PROGNUM, VERSNUM, server, NULL))
		ERRX(EXIT_FAILURE, "Cannot register server %d", num);

	clnt = clnt_raw_create(PROGNUM, VERSNUM);
	if (clnt == NULL)
		ERRX(EXIT_FAILURE, "%s",
		    clnt_spcreateerror("clnt_raw_create"));
	rv = clnt_call(clnt, PLUSONE, (xdrproc_t)xdr_int, (void *)&num,
	    (xdrproc_t)xdr_int, (void *)&resp, tout);
	if (rv != RPC_SUCCESS)
		ERRX(EXIT_FAILURE, "clnt_call: %s", clnt_sperrno(rv));
	DPRINTF("Got %d\n", resp);
	clnt_destroy(clnt);
	svc_destroy(svc);
	if (++num != resp)
		ERRX(EXIT_FAILURE, "expected %d got %d", num, resp);

	return EXIT_SUCCESS;
}

static int
regtest(const char *hostname, const char *transp, const char *arg, int p)
{
	CLIENT         *clnt;
	int 		num, resp;
	enum clnt_stat  rv;
	pid_t		pid;

	if (arg)
		num = atoi(arg);
	else
		num = 0;

#ifdef __NetBSD__
	svc_fdset_init(p ? SVC_FDSET_POLL : 0);
#endif
	if (!svc_create(server, PROGNUM, VERSNUM, transp))
	{
		SKIPXI(EXIT_FAILURE, "Cannot create server %d", num);
	}

	switch ((pid = fork())) {
	case 0:
		DPRINTF("Calling svc_run\n");
		svc_run();
		ERRX(EXIT_FAILURE, "svc_run returned %d!", num);
	case -1:
		ERRX(EXIT_FAILURE, "Fork failed (%s)", strerror(errno));
	default:
		sleep(1);
		break;
	}

	DPRINTF("Initializing client\n");
	clnt = clnt_create(hostname, PROGNUM, VERSNUM, transp);
	if (clnt == NULL)
		ERRX(EXIT_FAILURE, "%s",
		    clnt_spcreateerror("clnt_raw_create"));
	rv = clnt_call(clnt, PLUSONE, (xdrproc_t)xdr_int, (void *)&num,
	    (xdrproc_t)xdr_int, (void *)&resp, tout);
	if (rv != RPC_SUCCESS)
		ERRX(EXIT_FAILURE, "clnt_call: %s", clnt_sperrno(rv));
	DPRINTF("Got %d\n", resp);
	if (++num != resp)
		ERRX(EXIT_FAILURE, "expected %d got %d", num, resp);
	rv = clnt_call(clnt, DESTROY, (xdrproc_t)xdr_void, NULL,
	    (xdrproc_t)xdr_void, NULL, tout);
	if (rv != RPC_SUCCESS)
		ERRX(EXIT_FAILURE, "clnt_call: %s", clnt_sperrno(rv));
	clnt_destroy(clnt);

	return EXIT_SUCCESS;
}


#ifdef TEST
static void
allhosts(const char *transp)
{
	enum clnt_stat  clnt_stat;

	clnt_stat = rpc_broadcast(RPCBPROG, RPCBVERS, RPCBPROC_NULL,
	    (xdrproc_t)xdr_void, NULL, (xdrproc_t)xdr_void,
	    NULL, (resultproc_t)reply, transp);
	if (clnt_stat != RPC_SUCCESS && clnt_stat != RPC_TIMEDOUT)
		ERRX(EXIT_FAILURE, "%s", clnt_sperrno(clnt_stat));
}

int
main(int argc, char *argv[])
{
	int             ch;
	int		s, p;
	const char     *transp = "udp";

	p = s = 0;
	while ((ch = getopt(argc, argv, "prstu")) != -1)
		switch (ch) {
		case 'p':
			p = 1;
			break;
		case 's':
			s = 1;
			break;
		case 't':
			transp = "tcp";
			break;
		case 'u':
			transp = "udp";
			break;
		case 'r':
			transp = NULL;
			break;
		default:
			fprintf(stderr,
			    "Usage: %s -[r|s|t|u] [<hostname>...]\n",
			    getprogname());
			return EXIT_FAILURE;
		}

	if (argc == optind) {
		if  (transp)
			allhosts(transp);
		else
			rawtest(NULL);
	} else {
		for (; optind < argc; optind++) {
			if (transp)
				s == 0 ?
				    onehost(argv[optind], transp) :
				    regtest(argv[optind], transp, "1", p);
			else
				rawtest(argv[optind]);
		}
	}

	return EXIT_SUCCESS;
}

#else

ATF_TC(get_svc_addr_tcp);
ATF_TC_HEAD(get_svc_addr_tcp, tc)
{
	atf_tc_set_md_var(tc, "descr", "Checks CLGET_SVC_ADDR for tcp");

}

ATF_TC_BODY(get_svc_addr_tcp, tc)
{
	onehost("localhost", "tcp");

}

ATF_TC(get_svc_addr_udp);
ATF_TC_HEAD(get_svc_addr_udp, tc)
{
	atf_tc_set_md_var(tc, "descr", "Checks CLGET_SVC_ADDR for udp");
}

ATF_TC_BODY(get_svc_addr_udp, tc)
{
	onehost("localhost", "udp");

}

ATF_TC(raw);
ATF_TC_HEAD(raw, tc)
{
	atf_tc_set_md_var(tc, "descr", "Checks svc raw");
}

ATF_TC_BODY(raw, tc)
{
#ifdef __FreeBSD__
#ifdef __FreeBSD_bug_216954__
	atf_tc_expect_signal(SIGSEGV,
	    "fails with SIGSEGV only on ^/stable/10 -- bug # 216954");
#else
	atf_tc_expect_fail("fails with: clnt_call: "
	    "RPC: Can't decode result -- PR # 211804");
#endif
#endif
	rawtest(NULL);

}

ATF_TC(tcp);
ATF_TC_HEAD(tcp, tc)
{
	atf_tc_set_md_var(tc, "descr", "Checks svc tcp (select)");
#ifdef __FreeBSD__
	atf_tc_set_md_var(tc, "require.user", "root");
#endif
}

ATF_TC_BODY(tcp, tc)
{
	regtest("localhost", "tcp", "1", 0);

}

ATF_TC(udp);
ATF_TC_HEAD(udp, tc)
{
	atf_tc_set_md_var(tc, "descr", "Checks svc udp (select)");
#ifdef __FreeBSD__
	atf_tc_set_md_var(tc, "require.user", "root");
#endif
}

ATF_TC_BODY(udp, tc)
{
	regtest("localhost", "udp", "1", 0);

}

ATF_TC(tcp_poll);
ATF_TC_HEAD(tcp_poll, tc)
{
	atf_tc_set_md_var(tc, "descr", "Checks svc tcp (poll)");
#ifdef __FreeBSD__
	atf_tc_set_md_var(tc, "require.user", "root");
#endif
}

ATF_TC_BODY(tcp_poll, tc)
{
	regtest("localhost", "tcp", "1", 1);

}

ATF_TC(udp_poll);
ATF_TC_HEAD(udp_poll, tc)
{
	atf_tc_set_md_var(tc, "descr", "Checks svc udp (poll)");
#ifdef __FreeBSD__
	atf_tc_set_md_var(tc, "require.user", "root");
#endif
}

ATF_TC_BODY(udp_poll, tc)
{
	regtest("localhost", "udp", "1", 1);

}

ATF_TP_ADD_TCS(tp)
{
	ATF_TP_ADD_TC(tp, get_svc_addr_udp);
	ATF_TP_ADD_TC(tp, get_svc_addr_tcp);
	ATF_TP_ADD_TC(tp, raw);
	ATF_TP_ADD_TC(tp, tcp);
	ATF_TP_ADD_TC(tp, udp);
	ATF_TP_ADD_TC(tp, tcp_poll);
	ATF_TP_ADD_TC(tp, udp_poll);

	return atf_no_error();
}

#endif
OpenPOWER on IntegriCloud