diff options
Diffstat (limited to 'bdb/rpc_client/client.c')
-rw-r--r-- | bdb/rpc_client/client.c | 321 |
1 files changed, 207 insertions, 114 deletions
diff --git a/bdb/rpc_client/client.c b/bdb/rpc_client/client.c index 70744f54b4c..b6367e21449 100644 --- a/bdb/rpc_client/client.c +++ b/bdb/rpc_client/client.c @@ -1,20 +1,23 @@ /*- * See the file LICENSE for redistribution information. * - * Copyright (c) 1996, 1997, 1998, 1999, 2000 + * Copyright (c) 1996-2002 * 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 $"; +static const char revid[] = "$Id: client.c,v 1.51 2002/08/06 06:18:15 bostic Exp $"; #endif /* not lint */ #ifdef HAVE_RPC #ifndef NO_SYSTEM_INCLUDES #include <sys/types.h> +#ifdef HAVE_VXWORKS +#include <rpcLib.h> +#endif #include <rpc/rpc.h> #include <ctype.h> @@ -22,71 +25,124 @@ static const char revid[] = "$Id: client.c,v 1.21 2000/11/30 00:58:44 ubell Exp #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" +#include "dbinc/db_page.h" +#include "dbinc/db_am.h" +#include "dbinc/txn.h" + +#include "dbinc_auto/db_server.h" +#include "dbinc_auto/rpc_client_ext.h" + +static int __dbcl_c_destroy __P((DBC *)); +static int __dbcl_txn_close __P((DB_ENV *)); /* - * __dbclenv_server -- + * __dbcl_envrpcserver -- * Initialize an environment's server. * - * PUBLIC: int __dbcl_envserver __P((DB_ENV *, char *, long, long, u_int32_t)); + * PUBLIC: int __dbcl_envrpcserver + * PUBLIC: __P((DB_ENV *, void *, const char *, long, long, u_int32_t)); */ int -__dbcl_envserver(dbenv, host, tsec, ssec, flags) +__dbcl_envrpcserver(dbenv, clnt, host, tsec, ssec, flags) DB_ENV *dbenv; - char *host; + void *clnt; + const 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) { + if (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); + if (RPC_ON(dbenv)) { + __db_err(dbenv, "Already set an RPC handle"); + return (EINVAL); } - 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 + * Only create the client and set its timeout if the user + * did not pass us a client structure to begin with. */ - if ((replyp = __db_env_create_1(&req, cl)) == NULL) { - __db_err(dbenv, clnt_sperror(cl, "Berkeley DB")); - return (DB_NOSERVER); + if (clnt == NULL) { + if ((cl = clnt_create((char *)host, DB_RPC_SERVERPROG, + DB_RPC_SERVERVERS, "tcp")) == NULL) { + __db_err(dbenv, clnt_spcreateerror((char *)host)); + return (DB_NOSERVER); + } + if (tsec != 0) { + tp.tv_sec = tsec; + tp.tv_usec = 0; + (void)clnt_control(cl, CLSET_TIMEOUT, (char *)&tp); + } + } else { + cl = (CLIENT *)clnt; + F_SET(dbenv, DB_ENV_RPCCLIENT_GIVEN); } + dbenv->cl_handle = cl; - /* - * Process reply and free up our space from request - * SUCCESS: Store ID from server. - */ - if ((ret = replyp->status) != 0) + return (__dbcl_env_create(dbenv, ssec)); +} + +/* + * __dbcl_env_open_wrap -- + * Wrapper function for DB_ENV->open function for clients. + * We need a wrapper function to deal with DB_USE_ENVIRON* flags + * and we don't want to complicate the generated code for env_open. + * + * PUBLIC: int __dbcl_env_open_wrap + * PUBLIC: __P((DB_ENV *, const char *, u_int32_t, int)); + */ +int +__dbcl_env_open_wrap(dbenv, home, flags, mode) + DB_ENV * dbenv; + const char * home; + u_int32_t flags; + int mode; +{ + int ret; + + if (LF_ISSET(DB_THREAD)) { + __db_err(dbenv, "DB_THREAD not allowed on RPC clients"); + return (EINVAL); + } + if ((ret = __db_home(dbenv, home, flags)) != 0) return (ret); + return (__dbcl_env_open(dbenv, dbenv->db_home, flags, mode)); +} - dbenv->cl_id = replyp->envcl_id; - return (0); +/* + * __dbcl_db_open_wrap -- + * Wrapper function for DB->open function for clients. + * We need a wrapper function to error on DB_THREAD flag. + * and we don't want to complicate the generated code. + * + * PUBLIC: int __dbcl_db_open_wrap + * PUBLIC: __P((DB *, DB_TXN *, const char *, const char *, + * PUBLIC: DBTYPE, u_int32_t, int)); + */ +int +__dbcl_db_open_wrap(dbp, txnp, name, subdb, type, flags, mode) + DB * dbp; + DB_TXN * txnp; + const char * name; + const char * subdb; + DBTYPE type; + u_int32_t flags; + int mode; +{ + if (LF_ISSET(DB_THREAD)) { + __db_err(dbp->dbenv, "DB_THREAD not allowed on RPC clients"); + return (EINVAL); + } + return (__dbcl_db_open(dbp, txnp, name, subdb, type, flags, mode)); } /* @@ -114,17 +170,50 @@ __dbcl_refresh(dbenv) ret = __dbcl_txn_close(dbenv); dbenv->tx_handle = NULL; } - if (cl != NULL) + if (!F_ISSET(dbenv, DB_ENV_RPCCLIENT_GIVEN) && cl != NULL) clnt_destroy(cl); dbenv->cl_handle = NULL; + if (dbenv->db_home != NULL) { + __os_free(dbenv, dbenv->db_home); + dbenv->db_home = NULL; + } + return (ret); +} + +/* + * __dbcl_retcopy -- + * Copy the returned data into the user's DBT, handling allocation flags, + * but not DB_DBT_PARTIAL. + * + * PUBLIC: int __dbcl_retcopy __P((DB_ENV *, DBT *, + * PUBLIC: void *, u_int32_t, void **, u_int32_t *)); + */ +int +__dbcl_retcopy(dbenv, dbt, data, len, memp, memsize) + DB_ENV *dbenv; + DBT *dbt; + void *data; + u_int32_t len; + void **memp; + u_int32_t *memsize; +{ + int ret; + u_int32_t orig_flags; + + /* + * The RPC server handles DB_DBT_PARTIAL, so we mask it out here to + * avoid the handling of partials in __db_retcopy. + */ + orig_flags = dbt->flags; + F_CLR(dbt, DB_DBT_PARTIAL); + ret = __db_retcopy(dbenv, dbt, data, len, memp, memsize); + dbt->flags = orig_flags; return (ret); } /* * __dbcl_txn_close -- * Clean up an environment's transactions. - * - * PUBLIC: int __dbcl_txn_close __P((DB_ENV *)); */ int __dbcl_txn_close(dbenv) @@ -147,7 +236,7 @@ __dbcl_txn_close(dbenv) while ((txnp = TAILQ_FIRST(&tmgrp->txn_chain)) != NULL) __dbcl_txn_end(txnp); - __os_free(tmgrp, sizeof(*tmgrp)); + __os_free(dbenv, tmgrp); return (ret); } @@ -187,18 +276,57 @@ __dbcl_txn_end(txnp) if (txnp->parent != NULL) TAILQ_REMOVE(&txnp->parent->kids, txnp, klinks); TAILQ_REMOVE(&mgr->txn_chain, txnp, links); - __os_free(txnp, sizeof(*txnp)); + __os_free(dbenv, txnp); +} - return; +/* + * __dbcl_txn_setup -- + * Setup a client transaction structure. + * + * PUBLIC: void __dbcl_txn_setup __P((DB_ENV *, DB_TXN *, DB_TXN *, u_int32_t)); + */ +void +__dbcl_txn_setup(dbenv, txn, parent, id) + DB_ENV *dbenv; + DB_TXN *txn; + DB_TXN *parent; + u_int32_t id; +{ + txn->mgrp = dbenv->tx_handle; + txn->parent = parent; + txn->txnid = id; + + /* + * XXX + * In DB library the txn_chain is protected by the mgrp->mutexp. + * However, that mutex is implemented in the environments shared + * memory region. The client library does not support all of the + * region - that just get forwarded to the server. Therefore, + * the chain is unprotected here, but properly protected on the + * server. + */ + TAILQ_INSERT_TAIL(&txn->mgrp->txn_chain, txn, links); + + TAILQ_INIT(&txn->kids); + + if (parent != NULL) + TAILQ_INSERT_HEAD(&parent->kids, txn, klinks); + + txn->abort = __dbcl_txn_abort; + txn->commit = __dbcl_txn_commit; + txn->discard = __dbcl_txn_discard; + txn->id = __txn_id; + txn->prepare = __dbcl_txn_prepare; + txn->set_timeout = __dbcl_txn_timeout; + + txn->flags = TXN_MALLOC; } /* * __dbcl_c_destroy -- * Destroy a cursor. - * - * PUBLIC: int __dbcl_c_destroy __P((DBC *)); */ -int +static int __dbcl_c_destroy(dbc) DBC *dbc; { @@ -207,7 +335,14 @@ __dbcl_c_destroy(dbc) dbp = dbc->dbp; TAILQ_REMOVE(&dbp->free_queue, dbc, links); - __os_free(dbc, sizeof(*dbc)); + /* Discard any memory used to store returned data. */ + if (dbc->my_rskey.data != NULL) + __os_free(dbc->dbp->dbenv, dbc->my_rskey.data); + if (dbc->my_rkey.data != NULL) + __os_free(dbc->dbp->dbenv, dbc->my_rkey.data); + if (dbc->my_rdata.data != NULL) + __os_free(dbc->dbp->dbenv, dbc->my_rdata.data); + __os_free(NULL, dbc); return (0); } @@ -219,24 +354,23 @@ __dbcl_c_destroy(dbc) * PUBLIC: void __dbcl_c_refresh __P((DBC *)); */ void -__dbcl_c_refresh(dbcp) - DBC *dbcp; +__dbcl_c_refresh(dbc) + DBC *dbc; { DB *dbp; - dbp = dbcp->dbp; - dbcp->flags = 0; - dbcp->cl_id = 0; + dbp = dbc->dbp; + dbc->flags = 0; + dbc->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); + TAILQ_REMOVE(&dbp->active_queue, dbc, links); + TAILQ_INSERT_TAIL(&dbp->free_queue, dbc, links); } - return; } /* @@ -246,13 +380,13 @@ __dbcl_c_refresh(dbcp) * PUBLIC: int __dbcl_c_setup __P((long, DB *, DBC **)); */ int -__dbcl_c_setup(cl_id, dbp, dbcpp) +__dbcl_c_setup(cl_id, dbp, dbcp) long cl_id; DB *dbp; - DBC **dbcpp; + DBC **dbcp; { DBC *dbc, tmpdbc; - int ret, t_ret; + int ret; if ((dbc = TAILQ_FIRST(&dbp->free_queue)) != NULL) TAILQ_REMOVE(&dbp->free_queue, dbc, links); @@ -260,12 +394,12 @@ __dbcl_c_setup(cl_id, dbp, dbcpp) 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. - */ + * 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); + (void)__dbcl_dbc_close(&tmpdbc); return (ret); } dbc->c_close = __dbcl_dbc_close; @@ -273,62 +407,14 @@ __dbcl_c_setup(cl_id, dbp, dbcpp) dbc->c_del = __dbcl_dbc_del; dbc->c_dup = __dbcl_dbc_dup; dbc->c_get = __dbcl_dbc_get; + dbc->c_pget = __dbcl_dbc_pget; 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); + *dbcp = dbc; return (0); } @@ -363,9 +449,16 @@ __dbcl_dbclose_common(dbp) TAILQ_INIT(&dbp->free_queue); TAILQ_INIT(&dbp->active_queue); + /* Discard any memory used to store returned data. */ + if (dbp->my_rskey.data != NULL) + __os_free(dbp->dbenv, dbp->my_rskey.data); + if (dbp->my_rkey.data != NULL) + __os_free(dbp->dbenv, dbp->my_rkey.data); + if (dbp->my_rdata.data != NULL) + __os_free(dbp->dbenv, dbp->my_rdata.data); memset(dbp, CLEAR_BYTE, sizeof(*dbp)); - __os_free(dbp, sizeof(*dbp)); + __os_free(NULL, dbp); return (ret); } #endif /* HAVE_RPC */ |