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
|
/* Test svc_register/svc_unregister rpcbind interaction (bug 5010).
Copyright (C) 2017-2019 Free Software Foundation, Inc.
This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with the GNU C Library; if not, see
<http://www.gnu.org/licenses/>. */
/* This test uses a stub rpcbind server (implemented in a child
process using rpcbind_dispatch/run_rpcbind) to check how RPC
services are registered and unregistered using the rpcbind
protocol. For each subtest, a separate rpcbind test server is
spawned and terminated. */
#include <errno.h>
#include <netinet/in.h>
#include <rpc/clnt.h>
#include <rpc/pmap_prot.h>
#include <rpc/svc.h>
#include <signal.h>
#include <support/check.h>
#include <support/namespace.h>
#include <support/test-driver.h>
#include <support/xsocket.h>
#include <support/xthread.h>
#include <support/xunistd.h>
#include <sys/socket.h>
#include <sys/wait.h>
#include <unistd.h>
#include <libc-symbols.h>
#include <shlib-compat.h>
/* These functions are only available as compat symbols. */
compat_symbol_reference (libc, xdr_pmap, xdr_pmap, GLIBC_2_0);
compat_symbol_reference (libc, svc_unregister, svc_unregister, GLIBC_2_0);
/* Server callback for the unused RPC service which is registered and
unregistered. */
static void
server_dispatch (struct svc_req *request, SVCXPRT *transport)
{
FAIL_EXIT1 ("server_dispatch called");
}
/* The port on which rpcbind listens for incoming requests. */
static inline struct sockaddr_in
rpcbind_address (void)
{
return (struct sockaddr_in)
{
.sin_family = AF_INET,
.sin_addr.s_addr = htonl (INADDR_LOOPBACK),
.sin_port = htons (PMAPPORT)
};
}
/* Data provided by the test server after running the test, to see
that the expected calls (and only those) happened. */
struct test_state
{
bool_t set_called;
bool_t unset_called;
};
static bool_t
xdr_test_state (XDR *xdrs, void *data, ...)
{
struct test_state *p = data;
return xdr_bool (xdrs, &p->set_called)
&& xdr_bool (xdrs, &p->unset_called);
}
enum
{
/* Coordinates of our test service. These numbers are
arbitrary. */
PROGNUM = 123,
VERSNUM = 456,
/* Extension for this test. */
PROC_GET_STATE_AND_EXIT = 10760
};
/* Dummy implementation of the rpcbind service, with the
PROC_GET_STATE_AND_EXIT extension. */
static void
rpcbind_dispatch (struct svc_req *request, SVCXPRT *transport)
{
static struct test_state state = { 0, };
if (test_verbose)
printf ("info: rpcbind request %lu\n", request->rq_proc);
switch (request->rq_proc)
{
case PMAPPROC_SET:
case PMAPPROC_UNSET:
TEST_VERIFY (state.set_called == (request->rq_proc == PMAPPROC_UNSET));
TEST_VERIFY (!state.unset_called);
struct pmap query = { 0, };
TEST_VERIFY
(svc_getargs (transport, (xdrproc_t) xdr_pmap, (void *) &query));
if (test_verbose)
printf (" pm_prog=%lu pm_vers=%lu pm_prot=%lu pm_port=%lu\n",
query.pm_prog, query.pm_vers, query.pm_prot, query.pm_port);
TEST_VERIFY (query.pm_prog == PROGNUM);
TEST_VERIFY (query.pm_vers == VERSNUM);
if (request->rq_proc == PMAPPROC_SET)
state.set_called = TRUE;
else
state.unset_called = TRUE;
bool_t result = TRUE;
TEST_VERIFY (svc_sendreply (transport,
(xdrproc_t) xdr_bool, (void *) &result));
break;
case PROC_GET_STATE_AND_EXIT:
TEST_VERIFY (svc_sendreply (transport,
xdr_test_state, (void *) &state));
_exit (0);
break;
default:
FAIL_EXIT1 ("invalid rq_proc value: %lu", request->rq_proc);
}
}
/* Run the rpcbind test server. */
static void
run_rpcbind (int rpcbind_sock)
{
SVCXPRT *rpcbind_transport = svcudp_create (rpcbind_sock);
TEST_VERIFY (svc_register (rpcbind_transport, PMAPPROG, PMAPVERS,
rpcbind_dispatch,
/* Do not register with rpcbind. */
0));
svc_run ();
}
/* Call out to the rpcbind test server to retrieve the test status
information. */
static struct test_state
get_test_state (void)
{
int socket = RPC_ANYSOCK;
struct sockaddr_in address = rpcbind_address ();
CLIENT *client = clntudp_create
(&address, PMAPPROG, PMAPVERS, (struct timeval) { 1, 0}, &socket);
struct test_state result = { 0 };
TEST_VERIFY (clnt_call (client, PROC_GET_STATE_AND_EXIT,
(xdrproc_t) xdr_void, NULL,
xdr_test_state, (void *) &result,
((struct timeval) { 3, 0}))
== RPC_SUCCESS);
clnt_destroy (client);
return result;
}
/* Used by test_server_thread to receive test parameters. */
struct test_server_args
{
bool use_rpcbind;
bool use_unregister;
};
/* RPC test server. Used to verify the svc_unregister behavior during
thread cleanup. */
static void *
test_server_thread (void *closure)
{
struct test_server_args *args = closure;
SVCXPRT *transport = svcudp_create (RPC_ANYSOCK);
int protocol;
if (args->use_rpcbind)
protocol = IPPROTO_UDP;
else
/* Do not register with rpcbind. */
protocol =
|