2
* See the file LICENSE for redistribution information.
4
* Copyright (c) 1996-2002
5
* Sleepycat Software. All rights reserved.
11
static const char revid[] = "$Id$";
15
#ifndef NO_SYSTEM_INCLUDES
16
#include <sys/types.h>
30
#include "dbinc/db_page.h"
31
#include "dbinc/db_am.h"
32
#include "dbinc/txn.h"
34
#include "dbinc_auto/db_server.h"
35
#include "dbinc_auto/rpc_client_ext.h"
37
static int __dbcl_c_destroy __P((DBC *));
38
static int __dbcl_txn_close __P((DB_ENV *));
41
* __dbcl_envrpcserver --
42
* Initialize an environment's server.
44
* PUBLIC: int __dbcl_envrpcserver
45
* PUBLIC: __P((DB_ENV *, void *, const char *, long, long, u_int32_t));
48
__dbcl_envrpcserver(dbenv, clnt, host, tsec, ssec, flags)
61
if (rpcTaskInit() != 0) {
62
__db_err(dbenv, "Could not initialize VxWorks RPC");
67
__db_err(dbenv, "Already set an RPC handle");
71
* Only create the client and set its timeout if the user
72
* did not pass us a client structure to begin with.
75
if ((cl = clnt_create((char *)host, DB_RPC_SERVERPROG,
76
DB_RPC_SERVERVERS, "tcp")) == NULL) {
77
__db_err(dbenv, clnt_spcreateerror((char *)host));
83
(void)clnt_control(cl, CLSET_TIMEOUT, (char *)&tp);
87
F_SET(dbenv, DB_ENV_RPCCLIENT_GIVEN);
89
dbenv->cl_handle = cl;
91
return (__dbcl_env_create(dbenv, ssec));
95
* __dbcl_env_open_wrap --
96
* Wrapper function for DB_ENV->open function for clients.
97
* We need a wrapper function to deal with DB_USE_ENVIRON* flags
98
* and we don't want to complicate the generated code for env_open.
100
* PUBLIC: int __dbcl_env_open_wrap
101
* PUBLIC: __P((DB_ENV *, const char *, u_int32_t, int));
104
__dbcl_env_open_wrap(dbenv, home, flags, mode)
112
if (LF_ISSET(DB_THREAD)) {
113
__db_err(dbenv, "DB_THREAD not allowed on RPC clients");
116
if ((ret = __db_home(dbenv, home, flags)) != 0)
118
return (__dbcl_env_open(dbenv, dbenv->db_home, flags, mode));
122
* __dbcl_db_open_wrap --
123
* Wrapper function for DB->open function for clients.
124
* We need a wrapper function to error on DB_THREAD flag.
125
* and we don't want to complicate the generated code.
127
* PUBLIC: int __dbcl_db_open_wrap
128
* PUBLIC: __P((DB *, DB_TXN *, const char *, const char *,
129
* PUBLIC: DBTYPE, u_int32_t, int));
132
__dbcl_db_open_wrap(dbp, txnp, name, subdb, type, flags, mode)
141
if (LF_ISSET(DB_THREAD)) {
142
__db_err(dbp->dbenv, "DB_THREAD not allowed on RPC clients");
145
return (__dbcl_db_open(dbp, txnp, name, subdb, type, flags, mode));
150
* Clean up an environment.
152
* PUBLIC: int __dbcl_refresh __P((DB_ENV *));
155
__dbcl_refresh(dbenv)
161
cl = (CLIENT *)dbenv->cl_handle;
164
if (dbenv->tx_handle != NULL) {
166
* We only need to free up our stuff, the caller
167
* of this function will call the server who will
168
* do all the real work.
170
ret = __dbcl_txn_close(dbenv);
171
dbenv->tx_handle = NULL;
173
if (!F_ISSET(dbenv, DB_ENV_RPCCLIENT_GIVEN) && cl != NULL)
175
dbenv->cl_handle = NULL;
176
if (dbenv->db_home != NULL) {
177
__os_free(dbenv, dbenv->db_home);
178
dbenv->db_home = NULL;
185
* Copy the returned data into the user's DBT, handling allocation flags,
186
* but not DB_DBT_PARTIAL.
188
* PUBLIC: int __dbcl_retcopy __P((DB_ENV *, DBT *,
189
* PUBLIC: void *, u_int32_t, void **, u_int32_t *));
192
__dbcl_retcopy(dbenv, dbt, data, len, memp, memsize)
201
u_int32_t orig_flags;
204
* The RPC server handles DB_DBT_PARTIAL, so we mask it out here to
205
* avoid the handling of partials in __db_retcopy.
207
orig_flags = dbt->flags;
208
F_CLR(dbt, DB_DBT_PARTIAL);
209
ret = __db_retcopy(dbenv, dbt, data, len, memp, memsize);
210
dbt->flags = orig_flags;
215
* __dbcl_txn_close --
216
* Clean up an environment's transactions.
219
__dbcl_txn_close(dbenv)
227
tmgrp = dbenv->tx_handle;
230
* This function can only be called once per process (i.e., not
231
* once per thread), so no synchronization is required.
232
* Also this function is called *after* the server has been called,
233
* so the server has already closed/aborted any transactions that
234
* were open on its side. We only need to do local cleanup.
236
while ((txnp = TAILQ_FIRST(&tmgrp->txn_chain)) != NULL)
237
__dbcl_txn_end(txnp);
239
__os_free(dbenv, tmgrp);
246
* Clean up an transaction.
247
* RECURSIVE FUNCTION: Clean up nested transactions.
249
* PUBLIC: void __dbcl_txn_end __P((DB_TXN *));
263
* First take care of any kids we have
265
for (kids = TAILQ_FIRST(&txnp->kids);
267
kids = TAILQ_FIRST(&txnp->kids))
268
__dbcl_txn_end(kids);
271
* We are ending this transaction no matter what the parent
272
* may eventually do, if we have a parent. All those details
273
* are taken care of by the server. We only need to make sure
274
* that we properly release resources.
276
if (txnp->parent != NULL)
277
TAILQ_REMOVE(&txnp->parent->kids, txnp, klinks);
278
TAILQ_REMOVE(&mgr->txn_chain, txnp, links);
279
__os_free(dbenv, txnp);
283
* __dbcl_txn_setup --
284
* Setup a client transaction structure.
286
* PUBLIC: void __dbcl_txn_setup __P((DB_ENV *, DB_TXN *, DB_TXN *, u_int32_t));
289
__dbcl_txn_setup(dbenv, txn, parent, id)
295
txn->mgrp = dbenv->tx_handle;
296
txn->parent = parent;
301
* In DB library the txn_chain is protected by the mgrp->mutexp.
302
* However, that mutex is implemented in the environments shared
303
* memory region. The client library does not support all of the
304
* region - that just get forwarded to the server. Therefore,
305
* the chain is unprotected here, but properly protected on the
308
TAILQ_INSERT_TAIL(&txn->mgrp->txn_chain, txn, links);
310
TAILQ_INIT(&txn->kids);
313
TAILQ_INSERT_HEAD(&parent->kids, txn, klinks);
315
txn->abort = __dbcl_txn_abort;
316
txn->commit = __dbcl_txn_commit;
317
txn->discard = __dbcl_txn_discard;
319
txn->prepare = __dbcl_txn_prepare;
320
txn->set_timeout = __dbcl_txn_timeout;
322
txn->flags = TXN_MALLOC;
326
* __dbcl_c_destroy --
330
__dbcl_c_destroy(dbc)
337
TAILQ_REMOVE(&dbp->free_queue, dbc, links);
338
/* Discard any memory used to store returned data. */
339
if (dbc->my_rskey.data != NULL)
340
__os_free(dbc->dbp->dbenv, dbc->my_rskey.data);
341
if (dbc->my_rkey.data != NULL)
342
__os_free(dbc->dbp->dbenv, dbc->my_rkey.data);
343
if (dbc->my_rdata.data != NULL)
344
__os_free(dbc->dbp->dbenv, dbc->my_rdata.data);
345
__os_free(NULL, dbc);
351
* __dbcl_c_refresh --
352
* Refresh a cursor. Move it from the active queue to the free queue.
354
* PUBLIC: void __dbcl_c_refresh __P((DBC *));
357
__dbcl_c_refresh(dbc)
367
* If dbp->cursor fails locally, we use a local dbc so that
368
* we can close it. In that case, dbp will be NULL.
371
TAILQ_REMOVE(&dbp->active_queue, dbc, links);
372
TAILQ_INSERT_TAIL(&dbp->free_queue, dbc, links);
380
* PUBLIC: int __dbcl_c_setup __P((long, DB *, DBC **));
383
__dbcl_c_setup(cl_id, dbp, dbcp)
391
if ((dbc = TAILQ_FIRST(&dbp->free_queue)) != NULL)
392
TAILQ_REMOVE(&dbp->free_queue, dbc, links);
395
__os_calloc(dbp->dbenv, 1, sizeof(DBC), &dbc)) != 0) {
397
* If we die here, set up a tmp dbc to call the
398
* server to shut down that cursor.
401
tmpdbc.cl_id = cl_id;
402
(void)__dbcl_dbc_close(&tmpdbc);
405
dbc->c_close = __dbcl_dbc_close;
406
dbc->c_count = __dbcl_dbc_count;
407
dbc->c_del = __dbcl_dbc_del;
408
dbc->c_dup = __dbcl_dbc_dup;
409
dbc->c_get = __dbcl_dbc_get;
410
dbc->c_pget = __dbcl_dbc_pget;
411
dbc->c_put = __dbcl_dbc_put;
412
dbc->c_am_destroy = __dbcl_c_destroy;
416
TAILQ_INSERT_TAIL(&dbp->active_queue, dbc, links);
422
* __dbcl_dbclose_common --
423
* Common code for closing/cleaning a dbp.
425
* PUBLIC: int __dbcl_dbclose_common __P((DB *));
428
__dbcl_dbclose_common(dbp)
435
* Go through the active cursors and call the cursor recycle routine,
436
* which resolves pending operations and moves the cursors onto the
437
* free list. Then, walk the free list and call the cursor destroy
440
* NOTE: We do not need to use the join_queue for join cursors.
441
* See comment in __dbcl_dbjoin_ret.
444
while ((dbc = TAILQ_FIRST(&dbp->active_queue)) != NULL)
445
__dbcl_c_refresh(dbc);
446
while ((dbc = TAILQ_FIRST(&dbp->free_queue)) != NULL)
447
if ((t_ret = __dbcl_c_destroy(dbc)) != 0 && ret == 0)
450
TAILQ_INIT(&dbp->free_queue);
451
TAILQ_INIT(&dbp->active_queue);
452
/* Discard any memory used to store returned data. */
453
if (dbp->my_rskey.data != NULL)
454
__os_free(dbp->dbenv, dbp->my_rskey.data);
455
if (dbp->my_rkey.data != NULL)
456
__os_free(dbp->dbenv, dbp->my_rkey.data);
457
if (dbp->my_rdata.data != NULL)
458
__os_free(dbp->dbenv, dbp->my_rdata.data);
460
memset(dbp, CLEAR_BYTE, sizeof(*dbp));
461
__os_free(NULL, dbp);
464
#endif /* HAVE_RPC */