summaryrefslogtreecommitdiff
path: root/bdb/rpc_client/client.c
blob: 70744f54b4ce3407a5959944e76715bef868eb51 (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
/*-
 * See the file LICENSE for redistribution information.
 *
 * Copyright (c) 1996, 1997, 1998, 1999, 2000
 *	Sleepycat Software.  All rights reserved.
 */

#include "db_config.h"

#ifndef lint
static const char revid[] = "$Id: client.c,v 1.21 2000/11/30 00:58:44 ubell Exp $";
#endif /* not lint */

#ifdef HAVE_RPC
#ifndef NO_SYSTEM_INCLUDES
#include <sys/types.h>

#include <rpc/rpc.h>

#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#endif
#include "db_server.h"

#include "db_int.h"
#include "txn.h"
#include "gen_client_ext.h"
#include "rpc_client_ext.h"

/*
 * __dbclenv_server --
 *	Initialize an environment's server.
 *
 * PUBLIC: int __dbcl_envserver __P((DB_ENV *, char *, long, long, u_int32_t));
 */
int
__dbcl_envserver(dbenv, host, tsec, ssec, flags)
	DB_ENV *dbenv;
	char *host;
	long tsec, ssec;
	u_int32_t flags;
{
	CLIENT *cl;
	__env_create_msg req;
	__env_create_reply *replyp;
	struct timeval tp;
	int ret;

	COMPQUIET(flags, 0);

#ifdef HAVE_VXWORKS
	if ((ret = rpcTaskInit()) != 0) {
		__db_err(dbenv, "Could not initialize VxWorks RPC");
		return (ERROR);
	}
#endif
	if ((cl =
	    clnt_create(host, DB_SERVERPROG, DB_SERVERVERS, "tcp")) == NULL) {
		__db_err(dbenv, clnt_spcreateerror(host));
		return (DB_NOSERVER);
	}
	dbenv->cl_handle = cl;

	if (tsec != 0) {
		tp.tv_sec = tsec;
		tp.tv_usec = 0;
		(void)clnt_control(cl, CLSET_TIMEOUT, (char *)&tp);
	}

	req.timeout = ssec;
	/*
	 * CALL THE SERVER
	 */
	if ((replyp = __db_env_create_1(&req, cl)) == NULL) {
		__db_err(dbenv, clnt_sperror(cl, "Berkeley DB"));
		return (DB_NOSERVER);
	}

	/*
	 * Process reply and free up our space from request
	 * SUCCESS:  Store ID from server.
	 */
	if ((ret = replyp->status) != 0)
		return (ret);

	dbenv->cl_id = replyp->envcl_id;
	return (0);
}

/*
 * __dbcl_refresh --
 *	Clean up an environment.
 *
 * PUBLIC: int __dbcl_refresh __P((DB_ENV *));
 */
int
__dbcl_refresh(dbenv)
	DB_ENV *dbenv;
{
	CLIENT *cl;
	int ret;

	cl = (CLIENT *)dbenv->cl_handle;

	ret = 0;
	if (dbenv->tx_handle != NULL) {
		/*
		 * We only need to free up our stuff, the caller
		 * of this function will call the server who will
		 * do all the real work.
		 */
		ret = __dbcl_txn_close(dbenv);
		dbenv->tx_handle = NULL;
	}
	if (cl != NULL)
		clnt_destroy(cl);
	dbenv->cl_handle = NULL;
	return (ret);
}

/*
 * __dbcl_txn_close --
 *	Clean up an environment's transactions.
 *
 * PUBLIC: int __dbcl_txn_close __P((DB_ENV *));
 */
int
__dbcl_txn_close(dbenv)
	DB_ENV *dbenv;
{
	DB_TXN *txnp;
	DB_TXNMGR *tmgrp;
	int ret;

	ret = 0;
	tmgrp = dbenv->tx_handle;

	/*
	 * This function can only be called once per process (i.e., not
	 * once per thread), so no synchronization is required.
	 * Also this function is called *after* the server has been called,
	 * so the server has already closed/aborted any transactions that
	 * were open on its side.  We only need to do local cleanup.
	 */
	while ((txnp = TAILQ_FIRST(&tmgrp->txn_chain)) != NULL)
		__dbcl_txn_end(txnp);

	__os_free(tmgrp, sizeof(*tmgrp));
	return (ret);

}

/*
 * __dbcl_txn_end --
 *	Clean up an transaction.
 * RECURSIVE FUNCTION:  Clean up nested transactions.
 *
 * PUBLIC: void __dbcl_txn_end __P((DB_TXN *));
 */
void
__dbcl_txn_end(txnp)
	DB_TXN *txnp;
{
	DB_ENV *dbenv;
	DB_TXN *kids;
	DB_TXNMGR *mgr;

	mgr = txnp->mgrp;
	dbenv = mgr->dbenv;

	/*
	 * First take care of any kids we have
	 */
	for (kids = TAILQ_FIRST(&txnp->kids);
	    kids != NULL;
	    kids = TAILQ_FIRST(&txnp->kids))
		__dbcl_txn_end(kids);

	/*
	 * We are ending this transaction no matter what the parent
	 * may eventually do, if we have a parent.  All those details
	 * are taken care of by the server.  We only need to make sure
	 * that we properly release resources.
	 */
	if (txnp->parent != NULL)
		TAILQ_REMOVE(&txnp->parent->kids, txnp, klinks);
	TAILQ_REMOVE(&mgr->txn_chain, txnp, links);
	__os_free(txnp, sizeof(*txnp));

	return;
}

/*
 * __dbcl_c_destroy --
 *	Destroy a cursor.
 *
 * PUBLIC: int __dbcl_c_destroy __P((DBC *));
 */
int
__dbcl_c_destroy(dbc)
	DBC *dbc;
{
	DB *dbp;

	dbp = dbc->dbp;

	TAILQ_REMOVE(&dbp->free_queue, dbc, links);
	__os_free(dbc, sizeof(*dbc));

	return (0);
}

/*
 * __dbcl_c_refresh --
 *	Refresh a cursor.  Move it from the active queue to the free queue.
 *
 * PUBLIC: void __dbcl_c_refresh __P((DBC *));
 */
void
__dbcl_c_refresh(dbcp)
	DBC *dbcp;
{
	DB *dbp;

	dbp = dbcp->dbp;
	dbcp->flags = 0;
	dbcp->cl_id = 0;

	/*
	 * If dbp->cursor fails locally, we use a local dbc so that
	 * we can close it.  In that case, dbp will be NULL.
	 */
	if (dbp != NULL) {
		TAILQ_REMOVE(&dbp->active_queue, dbcp, links);
		TAILQ_INSERT_TAIL(&dbp->free_queue, dbcp, links);
	}
	return;
}

/*
 * __dbcl_c_setup --
 *	Allocate a cursor.
 *
 * PUBLIC: int __dbcl_c_setup __P((long, DB *, DBC **));
 */
int
__dbcl_c_setup(cl_id, dbp, dbcpp)
	long cl_id;
	DB *dbp;
	DBC **dbcpp;
{
	DBC *dbc, tmpdbc;
	int ret, t_ret;

	if ((dbc = TAILQ_FIRST(&dbp->free_queue)) != NULL)
		TAILQ_REMOVE(&dbp->free_queue, dbc, links);
	else {
		if ((ret =
		    __os_calloc(dbp->dbenv, 1, sizeof(DBC), &dbc)) != 0) {
			/*
			* If we die here, set up a tmp dbc to call the
			* server to shut down that cursor.
			*/
			tmpdbc.dbp = NULL;
			tmpdbc.cl_id = cl_id;
			t_ret = __dbcl_dbc_close(&tmpdbc);
			return (ret);
		}
		dbc->c_close = __dbcl_dbc_close;
		dbc->c_count = __dbcl_dbc_count;
		dbc->c_del = __dbcl_dbc_del;
		dbc->c_dup = __dbcl_dbc_dup;
		dbc->c_get = __dbcl_dbc_get;
		dbc->c_put = __dbcl_dbc_put;
		dbc->c_am_destroy = __dbcl_c_destroy;
	}
	dbc->cl_id = cl_id;
	dbc->dbp = dbp;
	TAILQ_INSERT_TAIL(&dbp->active_queue, dbc, links);
	*dbcpp = dbc;
	return (0);
}

/*
 * __dbcl_retcopy --
 *	Copy the returned data into the user's DBT, handling special flags
 *	as they apply to a client.  Modeled after __db_retcopy().
 *
 * PUBLIC: int __dbcl_retcopy __P((DB_ENV *, DBT *, void *, u_int32_t));
 */
int
__dbcl_retcopy(dbenv, dbt, data, len)
	DB_ENV *dbenv;
	DBT *dbt;
	void *data;
	u_int32_t len;
{
	int ret;

	/*
	 * No need to handle DB_DBT_PARTIAL here, server already did.
	 */
	dbt->size = len;

	/*
	 * Allocate memory to be owned by the application: DB_DBT_MALLOC
	 * and DB_DBT_REALLOC.  Always allocate even if we're copying 0 bytes.
	 * Or use memory specified by application: DB_DBT_USERMEM.
	 */
	if (F_ISSET(dbt, DB_DBT_MALLOC)) {
		if ((ret = __os_malloc(dbenv, len, NULL, &dbt->data)) != 0)
			return (ret);
	} else if (F_ISSET(dbt, DB_DBT_REALLOC)) {
		if ((ret = __os_realloc(dbenv, len, NULL, &dbt->data)) != 0)
			return (ret);
	} else if (F_ISSET(dbt, DB_DBT_USERMEM)) {
		if (len != 0 && (dbt->data == NULL || dbt->ulen < len))
			return (ENOMEM);
	} else {
		/*
		 * If no user flags, then set the DBT to point to the
		 * returned data pointer and return.
		 */
		dbt->data = data;
		return (0);
	}

	if (len != 0)
		memcpy(dbt->data, data, len);
	return (0);
}

/*
 * __dbcl_dbclose_common --
 *	Common code for closing/cleaning a dbp.
 *
 * PUBLIC: int __dbcl_dbclose_common __P((DB *));
 */
int
__dbcl_dbclose_common(dbp)
	DB *dbp;
{
	int ret, t_ret;
	DBC *dbc;

	/*
	 * Go through the active cursors and call the cursor recycle routine,
	 * which resolves pending operations and moves the cursors onto the
	 * free list.  Then, walk the free list and call the cursor destroy
	 * routine.
	 *
	 * NOTE:  We do not need to use the join_queue for join cursors.
	 * See comment in __dbcl_dbjoin_ret.
	 */
	ret = 0;
	while ((dbc = TAILQ_FIRST(&dbp->active_queue)) != NULL)
		__dbcl_c_refresh(dbc);
	while ((dbc = TAILQ_FIRST(&dbp->free_queue)) != NULL)
		if ((t_ret = __dbcl_c_destroy(dbc)) != 0 && ret == 0)
			ret = t_ret;

	TAILQ_INIT(&dbp->free_queue);
	TAILQ_INIT(&dbp->active_queue);

	memset(dbp, CLEAR_BYTE, sizeof(*dbp));
	__os_free(dbp, sizeof(*dbp));
	return (ret);
}
#endif /* HAVE_RPC */