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
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2007 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
/*
* Just in case we're not in a build environment, make sure that
* TEXT_DOMAIN gets set to something.
*/
#if !defined(TEXT_DOMAIN)
#define TEXT_DOMAIN "SYS_TEST"
#endif
#include <meta.h>
#include <metad.h>
#include <sdssc.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#define CC_TTL_MAX 20
typedef struct {
char *cc_node;
struct timeval cc_ttl;
CLIENT *cc_clp;
} client_cache_t;
typedef struct client_header {
client_cache_t **ch_cache; /* array of clients. */
mutex_t ch_mutex; /* lock access to ch_cache */
} client_header_t;
/*
* This structure is used to pass data from meta_client_create to
* client_create_helper via meta_client_create_retry.
*/
typedef struct clnt_data {
rpcprog_t cd_prognum; /* RPC program number */
rpcvers_t cd_version; /* Desired interface version */
char *cd_nettype; /* Type of network to use */
} clnt_data_t;
#define MALLOC_BLK_SIZE 10
static client_header_t client_header = {(client_cache_t **)NULL, DEFAULTMUTEX};
static void
cc_add(
client_header_t *header,
char *node,
CLIENT *clntp,
md_error_t *ep
)
{
client_cache_t ***cachep = &header->ch_cache;
struct timeval now;
int i;
int j = 0;
if (gettimeofday(&now, NULL) == -1) {
(void) mdsyserror(ep, errno, "gettimeofday()");
return;
}
(void) mutex_lock(&header->ch_mutex);
if (*cachep) {
for (i = 0; (*cachep)[i] != NULL; i++)
if (strcmp((*cachep)[i]->cc_node, node) == 0 &&
(*cachep)[i]->cc_clp == NULL) {
(*cachep)[i]->cc_clp = clntp;
(*cachep)[i]->cc_ttl = now;
(void) mutex_unlock(&header->ch_mutex);
return;
}
} else {
*cachep = Calloc(MALLOC_BLK_SIZE, sizeof (**cachep));
i = 0;
}
(*cachep)[i] = Zalloc(sizeof (***cachep));
(*cachep)[i]->cc_node = Strdup(node);
(*cachep)[i]->cc_clp = clntp;
(*cachep)[i]->cc_ttl = now;
if ((++i % MALLOC_BLK_SIZE) == 0) {
*cachep = Realloc(*cachep,
(i + MALLOC_BLK_SIZE) * sizeof (**cachep));
for (j = i; j < (i + MALLOC_BLK_SIZE); j++)
(*cachep)[j] = NULL;
}
(void) mutex_unlock(&header->ch_mutex);
}
static void
rel_clntp(client_cache_t *cachep)
{
CLIENT *clntp = cachep->cc_clp;
if (clntp != NULL) {
auth_destroy(clntp->cl_auth);
clnt_destroy(clntp);
}
cachep->cc_clp = NULL;
}
static void
cc_destroy(client_header_t *header)
{
client_cache_t ***cachep = &header->ch_cache;
int i;
(void) mutex_lock(&header->ch_mutex);
if (*cachep) {
for (i = 0; ((*cachep)[i] != NULL); i++) {
client_cache_t *p = (*cachep)[i];
Free(p->cc_node);
rel_clntp(p);
Free(p);
}
Free(*cachep);
*cachep = NULL;
}
(void) mutex_unlock(&header->ch_mutex);
}
/*
* Set the timeout value for this client handle.
*/
int
cl_sto(
CLIENT *clntp,
char *hostname,
long time_out,
md_error_t *ep
)
{
struct timeval nto;
(void) memset(&nto, '\0', sizeof (nto));
nto.tv_sec = time_out;
if (clnt_control(clntp, CLSET_TIMEOUT, (char *)&nto) != TRUE)
return (mdrpcerror(ep, clntp, hostname,
dgettext(TEXT_DOMAIN, "metad client set timeout")));
return (0);
}
/*
* client_create_vers_retry is the helper function to be passed to
* meta_client_create_retry to do the actual work of creating the client
* when version selection is necessary.
*/
/* ARGSUSED */
static CLIENT *
client_create_vers_retry(char *hostname,
void *ignore,
struct timeval *tout
)
{
rpcvers_t vers; /* Version # not needed. */
return (clnt_create_vers_timed(hostname, METAD, &vers,
METAD_VERSION, METAD_VERSION_DEVID, "tcp", tout));
}
/*
* client_create_helper is the helper function to be passed to
* meta_client_create_retry when plain vanilla client create is desired.
*/
static CLIENT *
client_create_helper(char *hostname, void *private, struct timeval *time_out)
{
clnt_data_t *cd = (clnt_data_t *)private;
return (clnt_create_timed(hostname, cd->cd_prognum, cd->cd_version,
cd->cd_nettype, time_out));
}
/*
* meta_client_create_retry is a general function to assist in creating RPC
* clients. This function handles retrying if the attempt to create a
* client fails. meta_client_create_retry itself does not actually create
* the client. Instead it calls the helper function, func, to do that job.
*
* With the help of func, meta_client_create_retry will create an RPC
* connection allowing up to tout seconds to complete the task. If the
* connection creation fails for RPC_RPCBFAILURE, RPC_CANTRECV or
* RPC_PROGNOTREGISTERED and tout seconds have not passed,
* meta_client_create_retry will try again. The reason retries are
* important is that when the inet daemon is being refreshed, it can take
* 15-20 seconds for it to start responding again.
*
* Arguments:
*
* hostname - Name of remote host
*
* func - Pointer to the helper function, that will
* actually try to create the client.
*
* data - Private data to be passed on to func.
* meta_client_create_retry treats this as an opaque
* pointer.
*
* tout - Number of seconds to allow for the connection
* attempt.
*
* ep - Standard SVM error pointer. May be NULL.
*/
CLIENT *
meta_client_create_retry(
char *hostname,
clnt_create_func_t func,
void *data,
time_t tout,
md_error_t *ep
)
{
static int debug; /* print debugging info */
static int debug_set = 0;
CLIENT *clnt = (CLIENT *) NULL;
struct timeval curtime;
char *d;
struct timeval start;
struct timeval timeout;
if (debug_set == 0) {
d = getenv("MD_DEBUG");
if (d == NULL) {
debug = 0;
} else {
debug = (strstr(d, "RPC") == NULL) ? 0 : 1;
}
debug_set = 1;
}
timeout.tv_usec = 0;
if (gettimeofday(&start, NULL) == -1) {
if (ep != (md_error_t *)NULL) {
(void) mdsyserror(ep, errno, "gettimeofday()");
}
return (clnt);
}
curtime = start;
while ((curtime.tv_sec - start.tv_sec) < tout) {
/* Use remaining time as the timeout value. */
timeout.tv_sec = tout - (curtime.tv_sec - start.tv_sec);
clnt = (*func)(hostname, data, &timeout);
if (clnt != (CLIENT *) NULL)
break;
if ((rpc_createerr.cf_stat == RPC_RPCBFAILURE) ||
(rpc_createerr.cf_stat == RPC_PROGNOTREGISTERED) ||
(rpc_createerr.cf_stat == RPC_CANTRECV)) {
if (debug) {
clnt_pcreateerror("meta_client_create_retry");
}
/* If error might be fixed in time, sleep & try again */
(void) sleep(2);
if (gettimeofday(&curtime, NULL) == -1) {
if (ep != (md_error_t *)NULL) {
(void) mdsyserror(ep, errno,
"gettimeofday()");
}
return (clnt);
}
} else {
/* Not a recoverable error. */
break;
}
}
if ((clnt == (CLIENT *) NULL) && (ep != (md_error_t *)NULL)) {
(void) mdrpccreateerror(ep, hostname,
"meta_client_create_retry");
}
return (clnt);
}
/*
* meta_client_create is intended to be used within SVM as a replacement
* for calls to clnt_create. meta_client_create invokes the retry
* mechanism of meta_client_create_retry.
*/
CLIENT *
meta_client_create(char *host, rpcprog_t prognum, rpcvers_t version,
char *nettype)
{
clnt_data_t cd;
cd.cd_prognum = prognum;
cd.cd_version = version;
cd.cd_nettype = nettype;
return (meta_client_create_retry(host, client_create_helper,
(void *)&cd, MD_CLNT_CREATE_TOUT, (md_error_t *)NULL));
}
/*
* create and return RPC connection
*/
CLIENT *
metarpcopen(
char *hostname,
long time_out,
md_error_t *ep
)
{
CLIENT *clntp = NULL;
client_cache_t ***cachep = &client_header.ch_cache;
int i;
long delta;
struct timeval now;
struct in_addr p_ip;
char *host_sc = NULL;
char host_priv[18];
/*
* If we are in cluster mode, lets use the private interconnect
* hostnames to establish the rpc connections.
*/
if (sdssc_bind_library() != SDSSC_NOT_BOUND)
if (sdssc_get_priv_ipaddr(hostname, &p_ip) == SDSSC_OKAY) {
/*
* inet_ntoa() returns pointer to a string in the
* base 256 notation d.d.d.d (IPv4) and so
* host_priv[18] should be sufficient enough to
* hold it.
*/
host_sc = inet_ntoa(p_ip);
if (host_sc != NULL) {
int size = sizeof (host_priv);
if (strlcpy(host_priv, host_sc, size) < size)
hostname = host_priv;
}
}
if (gettimeofday(&now, NULL) == -1) {
(void) mdsyserror(ep, errno, "gettimeofday()");
return (NULL);
}
/*
* Before trying to create the client, make sure that the core SVM
* services are enabled by the Service Management Facility. We
* don't want to suffer the 60 second timeout if the services are
* not even enabled. This call actually only verifies that they
* are enabled on this host no matter which host the caller wants
* to connect to. Nonetheless, if the services are not enabled on
* the local host, our RPC stuff is not going to work as expected.
*/
if (meta_smf_isonline(META_SMF_CORE, ep) == 0) {
return (NULL);
}
(void) mutex_lock(&client_header.ch_mutex);
if (client_header.ch_cache) {
for (i = 0; (*cachep)[i] != NULL; i++) {
if (strcmp((*cachep)[i]->cc_node, hostname) == 0) {
clntp = (*cachep)[i]->cc_clp;
if (clntp == NULL)
continue;
delta = now.tv_sec -
(*cachep)[i]->cc_ttl.tv_sec;
if (delta > CC_TTL_MAX) {
rel_clntp((*cachep)[i]);
continue;
}
if (cl_sto(clntp, hostname, time_out,
ep) != 0) {
(void) mutex_unlock(
&client_header.ch_mutex);
return (NULL);
}
(void) mutex_unlock(&client_header.ch_mutex);
return (clntp);
}
}
}
(void) mutex_unlock(&client_header.ch_mutex);
/*
* Try to create a version 2 client handle by default.
* If this fails (i.e. client is version 1), try to
* create a version 1 client handle.
*/
clntp = meta_client_create_retry(hostname, client_create_vers_retry,
(void *)NULL, MD_CLNT_CREATE_TOUT, ep);
/* open connection */
if (clntp == NULL) {
(void) mdrpccreateerror(ep, hostname,
dgettext(TEXT_DOMAIN, "metad client create"));
cc_add(&client_header, hostname, NULL, ep);
return (NULL);
} else {
auth_destroy(clntp->cl_auth);
clntp->cl_auth = authsys_create_default();
assert(clntp->cl_auth != NULL);
}
cc_add(&client_header, hostname, clntp, ep);
if (cl_sto(clntp, hostname, time_out, ep) != 0)
return (NULL);
return (clntp);
}
/*
* metarpcclose - is a place holder so that when using
* metarpcopen, it does not appear that
* we have dangling opens. We can at some
* later decrement open counts here too, if needed.
*/
/*ARGSUSED*/
void
metarpcclose(CLIENT *clntp)
{
}
void
metarpccloseall(void)
{
cc_destroy(&client_header);
}
|