From 60ec75cbc7dccfceec9c57799e2af5be21a08609 Mon Sep 17 00:00:00 2001 From: Andrew Tridgell Date: Sun, 10 Jul 2005 02:37:50 +0000 Subject: r8276: fixed the remaining memory leaks in smbscript. We can now loop doing lots of rpc calls without memory usage increasing. (This used to be commit 9c885a7edb771486793eb287288158157b34e8f3) --- source4/scripting/ejs/ejsrpc.c | 59 +-------------- source4/scripting/ejs/mprutil.c | 126 ++++++++++++++++++++++++-------- source4/scripting/ejs/smbcalls.c | 15 ++-- source4/scripting/ejs/smbcalls.h | 6 +- source4/scripting/ejs/smbcalls_cli.c | 30 ++++---- source4/scripting/ejs/smbcalls_config.c | 12 +-- source4/scripting/ejs/smbcalls_ldb.c | 3 +- source4/scripting/ejs/smbcalls_nbt.c | 3 +- source4/scripting/ejs/smbcalls_rpc.c | 5 +- source4/scripting/ejs/smbscript.c | 3 +- 10 files changed, 138 insertions(+), 124 deletions(-) (limited to 'source4/scripting') diff --git a/source4/scripting/ejs/ejsrpc.c b/source4/scripting/ejs/ejsrpc.c index f2c2b258e4..8b6bc328dd 100644 --- a/source4/scripting/ejs/ejsrpc.c +++ b/source4/scripting/ejs/ejsrpc.c @@ -22,6 +22,7 @@ #include "includes.h" #include "lib/ejs/ejs.h" +#include "scripting/ejs/smbcalls.h" #include "librpc/gen_ndr/ndr_security.h" #include "librpc/gen_ndr/ndr_lsa.h" #include "scripting/ejs/ejsrpc.h" @@ -43,64 +44,6 @@ NTSTATUS ejs_panic(struct ejs_rpc *ejs, const char *why) return NT_STATUS_INTERNAL_ERROR; } -/* - find a mpr component, allowing for sub objects, using the '.' convention -*/ -static NTSTATUS mprGetVar(struct MprVar **v, const char *name) -{ - const char *p = strchr(name, '.'); - char *objname; - NTSTATUS status; - if (p == NULL) { - *v = mprGetProperty(*v, name, NULL); - if (*v == NULL) { - DEBUG(1,("mprGetVar unable to find '%s'\n", name)); - return NT_STATUS_INVALID_PARAMETER; - } - return NT_STATUS_OK; - } - objname = talloc_strndup(mprMemCtx(), name, p-name); - NT_STATUS_HAVE_NO_MEMORY(objname); - *v = mprGetProperty(*v, objname, NULL); - NT_STATUS_HAVE_NO_MEMORY(*v); - status = mprGetVar(v, p+1); - talloc_free(objname); - return status; -} - - -/* - set a mpr component, allowing for sub objects, using the '.' convention -*/ -static NTSTATUS mprSetVar(struct MprVar *v, const char *name, struct MprVar val) -{ - const char *p = strchr(name, '.'); - char *objname; - struct MprVar *v2; - NTSTATUS status; - if (p == NULL) { - v2 = mprSetProperty(v, name, &val); - if (v2 == NULL) { - DEBUG(1,("mprSetVar unable to set '%s'\n", name)); - return NT_STATUS_INVALID_PARAMETER_MIX; - } - return NT_STATUS_OK; - } - objname = talloc_strndup(mprMemCtx(), name, p-name); - if (objname == NULL) { - return NT_STATUS_NO_MEMORY; - } - v2 = mprGetProperty(v, objname, NULL); - if (v2 == NULL) { - struct MprVar val2 = mprCreateObjVar(objname, MPR_DEFAULT_HASH_SIZE); - v2 = mprCreateProperty(v, objname, &val2); - } - status = mprSetVar(v2, p+1, val); - talloc_free(objname); - return status; -} - - /* start the ejs pull process for a structure */ diff --git a/source4/scripting/ejs/mprutil.c b/source4/scripting/ejs/mprutil.c index 7b64d042f5..3c28cb4bf1 100644 --- a/source4/scripting/ejs/mprutil.c +++ b/source4/scripting/ejs/mprutil.c @@ -24,6 +24,67 @@ #include "lib/ejs/ejs.h" #include "lib/ldb/include/ldb.h" +/* + find a mpr component, allowing for sub objects, using the '.' convention +*/ + NTSTATUS mprGetVar(struct MprVar **v, const char *name) +{ + const char *p = strchr(name, '.'); + char *objname; + NTSTATUS status; + if (p == NULL) { + *v = mprGetProperty(*v, name, NULL); + if (*v == NULL) { + DEBUG(1,("mprGetVar unable to find '%s'\n", name)); + return NT_STATUS_INVALID_PARAMETER; + } + return NT_STATUS_OK; + } + objname = talloc_strndup(mprMemCtx(), name, p-name); + NT_STATUS_HAVE_NO_MEMORY(objname); + *v = mprGetProperty(*v, objname, NULL); + NT_STATUS_HAVE_NO_MEMORY(*v); + status = mprGetVar(v, p+1); + talloc_free(objname); + return status; +} + + +/* + set a mpr component, allowing for sub objects, using the '.' convention + destroys 'val' after setting +*/ + NTSTATUS mprSetVar(struct MprVar *v, const char *name, struct MprVar val) +{ + const char *p = strchr(name, '.'); + char *objname; + struct MprVar *v2; + NTSTATUS status; + if (p == NULL) { + v2 = mprSetProperty(v, name, &val); + if (v2 == NULL) { + DEBUG(1,("mprSetVar unable to set '%s'\n", name)); + return NT_STATUS_INVALID_PARAMETER_MIX; + } + mprDestroyVar(&val); + return NT_STATUS_OK; + } + objname = talloc_strndup(mprMemCtx(), name, p-name); + if (objname == NULL) { + return NT_STATUS_NO_MEMORY; + } + v2 = mprGetProperty(v, objname, NULL); + if (v2 == NULL) { + mprSetVar(v, objname, mprCreateObjVar(objname, MPR_DEFAULT_HASH_SIZE)); + v2 = mprGetProperty(v, objname, NULL); + } + status = mprSetVar(v2, p+1, val); + talloc_free(objname); + return status; +} + + + /* add an indexed array element to a property */ @@ -31,7 +92,7 @@ static void mprAddArray(struct MprVar *var, int i, struct MprVar v) { char idx[16]; mprItoa(i, idx, sizeof(idx)); - mprCreateProperty(var, idx, &v); + mprSetVar(var, idx, v); } /* @@ -76,12 +137,12 @@ struct MprVar mprLdbMessage(struct ldb_message *msg) need a special case for the single value case */ const char *multivalued[] = { "objectClass", "memberOf", "privilege", "member", NULL }; - struct MprVar val; var = mprCreateObjVar(msg->dn, MPR_DEFAULT_HASH_SIZE); for (i=0;inum_elements;i++) { struct ldb_message_element *el = &msg->elements[i]; + struct MprVar val; if (el->num_values == 1 && !str_list_check_ci(multivalued, el->name)) { val = mprData(el->values[0].data, el->values[0].length); @@ -94,13 +155,12 @@ struct MprVar mprLdbMessage(struct ldb_message *msg) el->values[j].length)); } } - mprCreateProperty(&var, el->name, &val); + mprSetVar(&var, el->name, val); } /* add the dn if it is not already specified */ if (mprGetProperty(&var, "dn", 0) == 0) { - val = mprCreateStringVar(msg->dn, 1); - mprCreateProperty(&var, "dn", &val); + mprSetVar(&var, "dn", mprCreateStringVar(msg->dn, 1)); } return var; @@ -172,21 +232,14 @@ const char **mprToList(TALLOC_CTX *mem_ctx, struct MprVar *v) */ struct MprVar mprNTSTATUS(NTSTATUS status) { - struct MprVar res, val; + struct MprVar res; res = mprCreateObjVar("ntstatus", MPR_DEFAULT_HASH_SIZE); - val = mprCreateStringVar(nt_errstr(status), 1); - mprCreateProperty(&res, "errstr", &val); - - val = mprCreateIntegerVar(NT_STATUS_V(status)); - mprCreateProperty(&res, "v", &val); - - val = mprCreateBoolVar(NT_STATUS_IS_OK(status)); - mprCreateProperty(&res, "is_ok", &val); - - val = mprCreateBoolVar(NT_STATUS_IS_ERR(status)); - mprCreateProperty(&res, "is_err", &val); + mprSetVar(&res, "errstr", mprCreateStringVar(nt_errstr(status), 1)); + mprSetVar(&res, "v", mprCreateIntegerVar(NT_STATUS_V(status))); + mprSetVar(&res, "is_ok", mprCreateBoolVar(NT_STATUS_IS_OK(status))); + mprSetVar(&res, "is_err", mprCreateBoolVar(NT_STATUS_IS_ERR(status))); return res; } @@ -196,21 +249,14 @@ struct MprVar mprNTSTATUS(NTSTATUS status) */ struct MprVar mprWERROR(WERROR status) { - struct MprVar res, val; + struct MprVar res; res = mprCreateObjVar("werror", MPR_DEFAULT_HASH_SIZE); - val = mprCreateStringVar(win_errstr(status), 1); - mprCreateProperty(&res, "errstr", &val); - - val = mprCreateIntegerVar(W_ERROR_V(status)); - mprCreateProperty(&res, "v", &val); - - val = mprCreateBoolVar(W_ERROR_IS_OK(status)); - mprCreateProperty(&res, "is_ok", &val); - - val = mprCreateBoolVar(True); - mprCreateProperty(&res, "is_err", &val); + mprSetVar(&res, "errstr", mprCreateStringVar(win_errstr(status), 1)); + mprSetVar(&res, "v", mprCreateIntegerVar(W_ERROR_V(status))); + mprSetVar(&res, "is_ok", mprCreateBoolVar(W_ERROR_IS_OK(status))); + mprSetVar(&res, "is_err", mprCreateBoolVar(!W_ERROR_IS_OK(status))); return res; } @@ -221,8 +267,7 @@ struct MprVar mprWERROR(WERROR status) */ void mprSetPtr(struct MprVar *v, const char *propname, const void *p) { - struct MprVar val = mprCreatePtrVar(discard_const(p), NULL); - mprCreateProperty(v, propname, &val); + mprSetVar(v, propname, mprCreatePtrVar(discard_const(p), NULL)); } /* @@ -240,3 +285,22 @@ void *mprGetPtr(struct MprVar *v, const char *propname) } return val->ptr; } + +/* + set the return value then free the variable +*/ + void mpr_Return(int eid, struct MprVar v) +{ + ejsSetReturnValue(eid, v); + mprDestroyVar(&v); +} + +/* + set the return value then free the variable +*/ +void mpr_ReturnString(int eid, const char *s) +{ + mpr_Return(eid, mprCreateStringVar(s, False)); +} + + diff --git a/source4/scripting/ejs/smbcalls.c b/source4/scripting/ejs/smbcalls.c index d8bb0c20a5..75702e4e76 100644 --- a/source4/scripting/ejs/smbcalls.c +++ b/source4/scripting/ejs/smbcalls.c @@ -24,6 +24,7 @@ #include "includes.h" #include "lib/ejs/ejs.h" #include "auth/auth.h" +#include "scripting/ejs/smbcalls.h" /* return the type of a variable @@ -60,16 +61,16 @@ static int ejs_typeof(MprVarHandle eid, int argc, struct MprVar **argv) } if (type == NULL) return -1; - ejsSetReturnString(eid, type); + mpr_ReturnString(eid, type); return 0; } /* setup a return of a string list */ - void ejs_returnlist(MprVarHandle eid, const char *name, const char **list) +void ejs_returnlist(int eid, const char *name, const char **list) { - ejsSetReturnValue(eid, mprList(name, list)); + mpr_Return(eid, mprList(name, list)); } static int ejs_systemAuth(TALLOC_CTX *tmp_ctx, struct MprVar *auth, const char *username, const char *password, const char *domain, const char *remote_host) @@ -158,7 +159,7 @@ static int ejs_userAuth(MprVarHandle eid, int argc, struct MprVar **argv) mprSetPropertyValue(&auth, "report", mprCreateStringVar("Unknown Domain", 1)); } - ejsSetReturnValue(eid, auth); + mpr_Return(eid, auth); talloc_free(tmp_ctx); return 0; } @@ -166,7 +167,6 @@ static int ejs_userAuth(MprVarHandle eid, int argc, struct MprVar **argv) static int ejs_domain_list(MprVarHandle eid, int argc, char **argv) { struct MprVar list; - struct MprVar dom; if (argc != 0) { ejsSetErrorMsg(eid, "domList invalid arguments"); @@ -174,10 +174,9 @@ static int ejs_domain_list(MprVarHandle eid, int argc, char **argv) } list = mprCreateObjVar("list", MPR_DEFAULT_HASH_SIZE); - dom = mprCreateStringVar("System User", 1); - mprCreateProperty(&list, "0", &dom); + mprSetVar(&list, "0", mprCreateStringVar("System User", 1)); - ejsSetReturnValue(eid, list); + mpr_Return(eid, list); return 0; } diff --git a/source4/scripting/ejs/smbcalls.h b/source4/scripting/ejs/smbcalls.h index 8b26e13873..c16b65b3c1 100644 --- a/source4/scripting/ejs/smbcalls.h +++ b/source4/scripting/ejs/smbcalls.h @@ -22,4 +22,8 @@ #include "lib/ejs/ejs.h" -void ejs_returnlist(MprVarHandle eid, const char *name, const char **list); +void mpr_Return(int eid, struct MprVar); +NTSTATUS mprSetVar(struct MprVar *v, const char *name, struct MprVar val); +NTSTATUS mprGetVar(struct MprVar **v, const char *name); + + diff --git a/source4/scripting/ejs/smbcalls_cli.c b/source4/scripting/ejs/smbcalls_cli.c index ca8fbd3ea2..8d00b80362 100644 --- a/source4/scripting/ejs/smbcalls_cli.c +++ b/source4/scripting/ejs/smbcalls_cli.c @@ -21,6 +21,7 @@ */ #include "includes.h" +#include "scripting/ejs/smbcalls.h" #include "lib/ejs/ejs.h" #include "libcli/raw/libcliraw.h" #include "libcli/composite/composite.h" @@ -79,13 +80,13 @@ static int ejs_cli_connect(MprVarHandle eid, int argc, char **argv) result = smb_raw_negotiate(transport, lp_maxprotocol()); if (!NT_STATUS_IS_OK(result)) { - ejsSetReturnValue(eid, mprNTSTATUS(result)); + mpr_Return(eid, mprNTSTATUS(result)); return 0; } /* Return a socket object */ - ejsSetReturnValue(eid, mprCreatePtrVar(transport, + mpr_Return(eid, mprCreatePtrVar(transport, talloc_get_name(transport))); return 0; @@ -213,7 +214,7 @@ static int ejs_cli_ssetup(MprVarHandle eid, int argc, MprVar **argv) /* Return a session object */ - ejsSetReturnValue(eid, mprCreatePtrVar(session, + mpr_Return(eid, mprCreatePtrVar(session, talloc_get_name(session))); result = 0; @@ -296,7 +297,7 @@ static int ejs_cli_tree_connect(MprVarHandle eid, int argc, MprVar **argv) talloc_free(mem_ctx); - ejsSetReturnValue(eid, mprCreatePtrVar(tree, + mpr_Return(eid, mprCreatePtrVar(tree, talloc_get_name(tree))); return 0; @@ -441,11 +442,11 @@ static int ejs_tree_connect(MprVarHandle eid, int argc, char **argv) talloc_free(mem_ctx); if (!NT_STATUS_IS_OK(result)) { - ejsSetReturnValue(eid, mprNTSTATUS(result)); + mpr_Return(eid, mprNTSTATUS(result)); return 0; } - ejsSetReturnValue(eid, mprCreatePtrVar(tree, talloc_get_name(tree))); + mpr_Return(eid, mprCreatePtrVar(tree, talloc_get_name(tree))); return 0; } @@ -478,7 +479,7 @@ static int ejs_tree_disconnect(MprVarHandle eid, int argc, MprVar **argv) result = smb_tree_disconnect(tree); - ejsSetReturnValue(eid, mprNTSTATUS(result)); + mpr_Return(eid, mprNTSTATUS(result)); return 0; } @@ -512,7 +513,7 @@ static int ejs_mkdir(MprVarHandle eid, int argc, MprVar **argv) result = smbcli_mkdir(tree, argv[1]->string); - ejsSetReturnValue(eid, mprNTSTATUS(result)); + mpr_Return(eid, mprNTSTATUS(result)); return 0; } @@ -546,7 +547,7 @@ static int ejs_rmdir(MprVarHandle eid, int argc, MprVar **argv) result = smbcli_rmdir(tree, argv[1]->string); - ejsSetReturnValue(eid, mprNTSTATUS(result)); + mpr_Return(eid, mprNTSTATUS(result)); return 0; } @@ -585,7 +586,7 @@ static int ejs_rename(MprVarHandle eid, int argc, MprVar **argv) result = smbcli_rename(tree, argv[1]->string, argv[2]->string); - ejsSetReturnValue(eid, mprNTSTATUS(result)); + mpr_Return(eid, mprNTSTATUS(result)); return 0; } @@ -619,7 +620,7 @@ static int ejs_unlink(MprVarHandle eid, int argc, MprVar **argv) result = smbcli_unlink(tree, argv[1]->string); - ejsSetReturnValue(eid, mprNTSTATUS(result)); + mpr_Return(eid, mprNTSTATUS(result)); return 0; } @@ -633,12 +634,11 @@ static void ejs_list_helper(struct clilist_file_info *info, const char *mask, void *state) { - MprVar *result = (MprVar *)state, value; + MprVar *result = (MprVar *)state; char idx[16]; mprItoa(result->properties->numDataItems, idx, sizeof(idx)); - value = mprCreateStringVar(info->name, 1); - mprCreateProperty(result, idx, &value); + mprSetVar(result, idx, mprCreateStringVar(info->name, 1)); } static int ejs_list(MprVarHandle eid, int argc, MprVar **argv) @@ -678,7 +678,7 @@ static int ejs_list(MprVarHandle eid, int argc, MprVar **argv) smbcli_list(tree, mask, attribute, ejs_list_helper, &result); - ejsSetReturnValue(eid, result); + mpr_Return(eid, result); return 0; } diff --git a/source4/scripting/ejs/smbcalls_config.c b/source4/scripting/ejs/smbcalls_config.c index cd0ecbd767..19e7a1c744 100644 --- a/source4/scripting/ejs/smbcalls_config.c +++ b/source4/scripting/ejs/smbcalls_config.c @@ -79,7 +79,7 @@ static int ejs_lpGet(MprVarHandle eid, int argc, char **argv) if (type == NULL || option == NULL) return -1; value = lp_get_parametric(snum, type, option); if (value == NULL) return -1; - ejsSetReturnString(eid, value); + mpr_ReturnString(eid, value); return 0; } @@ -97,7 +97,7 @@ static int ejs_lpGet(MprVarHandle eid, int argc, char **argv) if (type == NULL || option == NULL) return -1; value = lp_get_parametric(-1, type, option); if (value == NULL) return -1; - ejsSetReturnString(eid, value); + mpr_ReturnString(eid, value); return 0; } else { /* its a global parameter */ @@ -114,18 +114,18 @@ static int ejs_lpGet(MprVarHandle eid, int argc, char **argv) switch (parm->type) { case P_STRING: case P_USTRING: - ejsSetReturnString(eid, *(char **)parm_ptr); + mpr_ReturnString(eid, *(char **)parm_ptr); break; case P_BOOL: - ejsSetReturnValue(eid, mprCreateBoolVar(*(BOOL *)parm_ptr)); + mpr_Return(eid, mprCreateBoolVar(*(BOOL *)parm_ptr)); break; case P_INTEGER: - ejsSetReturnValue(eid, mprCreateIntegerVar(*(int *)parm_ptr)); + mpr_Return(eid, mprCreateIntegerVar(*(int *)parm_ptr)); break; case P_ENUM: for (i=0; parm->enum_list[i].name; i++) { if (*(int *)parm_ptr == parm->enum_list[i].value) { - ejsSetReturnString(eid, parm->enum_list[i].name); + mpr_ReturnString(eid, parm->enum_list[i].name); return 0; } } diff --git a/source4/scripting/ejs/smbcalls_ldb.c b/source4/scripting/ejs/smbcalls_ldb.c index cae3857686..414251a31c 100644 --- a/source4/scripting/ejs/smbcalls_ldb.c +++ b/source4/scripting/ejs/smbcalls_ldb.c @@ -21,6 +21,7 @@ */ #include "includes.h" +#include "scripting/ejs/smbcalls.h" #include "lib/ejs/ejs.h" #include "lib/ldb/include/ldb.h" @@ -74,7 +75,7 @@ static int ejs_ldbSearch(MprVarHandle eid, int argc, struct MprVar **argv) goto failed; } - ejsSetReturnValue(eid, mprLdbArray(res, ret, "ldb_message")); + mpr_Return(eid, mprLdbArray(res, ret, "ldb_message")); talloc_free(tmp_ctx); return 0; diff --git a/source4/scripting/ejs/smbcalls_nbt.c b/source4/scripting/ejs/smbcalls_nbt.c index a82b636c9c..a4dc943f51 100644 --- a/source4/scripting/ejs/smbcalls_nbt.c +++ b/source4/scripting/ejs/smbcalls_nbt.c @@ -21,6 +21,7 @@ */ #include "includes.h" +#include "scripting/ejs/smbcalls.h" #include "lib/ejs/ejs.h" #include "librpc/gen_ndr/ndr_nbt.h" @@ -75,7 +76,7 @@ static int ejs_resolve_name(MprVarHandle eid, int argc, struct MprVar **argv) mprCreateStringVar(reply_addr, 1)); } - ejsSetReturnValue(eid, mprNTSTATUS(nt_status)); + mpr_Return(eid, mprNTSTATUS(nt_status)); done: talloc_free(tmp_ctx); diff --git a/source4/scripting/ejs/smbcalls_rpc.c b/source4/scripting/ejs/smbcalls_rpc.c index 8a5389fbab..9546dc5b01 100644 --- a/source4/scripting/ejs/smbcalls_rpc.c +++ b/source4/scripting/ejs/smbcalls_rpc.c @@ -21,6 +21,7 @@ */ #include "includes.h" +#include "scripting/ejs/smbcalls.h" #include "lib/ejs/ejs.h" #include "librpc/gen_ndr/ndr_echo.h" #include "lib/cmdline/popt_common.h" @@ -83,7 +84,7 @@ static int ejs_rpc_connect(MprVarHandle eid, int argc, struct MprVar **argv) mprSetPtr(conn, "iface", iface); done: - ejsSetReturnValue(eid, mprNTSTATUS(status)); + mpr_Return(eid, mprNTSTATUS(status)); return 0; } @@ -177,7 +178,7 @@ done: done: talloc_free(ejs); - ejsSetReturnValue(eid, mprNTSTATUS(status)); + mpr_Return(eid, mprNTSTATUS(status)); if (NT_STATUS_EQUAL(status, NT_STATUS_INTERNAL_ERROR)) { return -1; } diff --git a/source4/scripting/ejs/smbscript.c b/source4/scripting/ejs/smbscript.c index f7556b5384..80ef96e1af 100644 --- a/source4/scripting/ejs/smbscript.c +++ b/source4/scripting/ejs/smbscript.c @@ -25,6 +25,7 @@ #include "lib/cmdline/popt_common.h" #include "dynconfig.h" #include "lib/ejs/ejs.h" +#include "scripting/ejs/smbcalls.h" void ejs_exception(const char *reason) { @@ -97,7 +98,7 @@ void ejs_exception(const char *reason) talloc_steal(mem_ctx, argv_list); v = mprList("ARGV", argv_list); mprSetPropertyValue(&v, "length", mprCreateIntegerVar(i-1)); - mprCreateProperty(ejsGetGlobalObject(eid), "ARGV", &v); + mprSetVar(ejsGetGlobalObject(eid), "ARGV", v); /* load the script and advance past interpreter line*/ script = file_load(fname, &script_size, mem_ctx); -- cgit