From: Juergen Gross <jgross@suse.com>
To: xen-devel@lists.xen.org
Cc: Juergen Gross <jgross@suse.com>,
wei.liu2@citrix.com, ian.jackson@eu.citrix.com
Subject: [PATCH v2 3/5] xenstore: add explicit memory context parameter to read_node()
Date: Mon, 18 Jul 2016 09:31:27 +0200 [thread overview]
Message-ID: <1468827089-9054-4-git-send-email-jgross@suse.com> (raw)
In-Reply-To: <1468827089-9054-1-git-send-email-jgross@suse.com>
Add a parameter to xenstored read_node() function to explicitly
specify the memory context to be used for allocations. This will make
it easier to avoid memory leaks by using a context which is freed
soon.
When calling read_node() select a sensible memory context for the new
parameter by preferring a temporary one.
Signed-off-by: Juergen Gross <jgross@suse.com>
---
tools/xenstore/xenstored_core.c | 24 +++++++++++++-----------
1 file changed, 13 insertions(+), 11 deletions(-)
diff --git a/tools/xenstore/xenstored_core.c b/tools/xenstore/xenstored_core.c
index 9448ee8..e5c74f4 100644
--- a/tools/xenstore/xenstored_core.c
+++ b/tools/xenstore/xenstored_core.c
@@ -398,7 +398,8 @@ bool is_child(const char *child, const char *parent)
}
/* If it fails, returns NULL and sets errno. */
-static struct node *read_node(struct connection *conn, const char *name)
+static struct node *read_node(struct connection *conn, const void *mem,
+ const char *name)
{
TDB_DATA key, data;
uint32_t *p;
@@ -419,7 +420,7 @@ static struct node *read_node(struct connection *conn, const char *name)
return NULL;
}
- node = talloc(name, struct node);
+ node = talloc(mem, struct node);
node->name = talloc_strdup(node, name);
node->parent = NULL;
node->tdb = tdb_context(conn);
@@ -522,7 +523,7 @@ static enum xs_perm_type ask_parents(struct connection *conn, const char *name)
do {
name = get_parent(name, name);
- node = read_node(conn, name);
+ node = read_node(conn, name, name);
if (node)
break;
} while (!streq(name, "/"));
@@ -563,7 +564,7 @@ struct node *get_node(struct connection *conn,
errno = EINVAL;
return NULL;
}
- node = read_node(conn, name);
+ node = read_node(conn, name, name);
/* If we don't have permission, we don't have node. */
if (node) {
if ((perm_for_conn(conn, node->perms, node->num_perms) & perm)
@@ -819,7 +820,7 @@ static struct node *construct_node(struct connection *conn, const char *name)
char *children, *parentname = get_parent(name, name);
/* If parent doesn't exist, create it. */
- parent = read_node(conn, parentname);
+ parent = read_node(conn, parentname, parentname);
if (!parent)
parent = construct_node(conn, parentname);
if (!parent)
@@ -984,7 +985,7 @@ static void delete_node(struct connection *conn, struct node *node)
for (i = 0; i < node->childlen; i += strlen(node->children+i) + 1) {
struct node *child;
- child = read_node(conn,
+ child = read_node(conn, node,
talloc_asprintf(node, "%s/%s", node->name,
node->children + i));
if (child) {
@@ -1036,7 +1037,7 @@ static int _rm(struct connection *conn, struct node *node, const char *name)
/* Delete from parent first, then if we crash, the worst that can
happen is the child will continue to take up space, but will
otherwise be unreachable. */
- struct node *parent = read_node(conn, get_parent(name, name));
+ struct node *parent = read_node(conn, name, get_parent(name, name));
if (!parent) {
send_error(conn, EINVAL);
return 0;
@@ -1055,7 +1056,7 @@ static int _rm(struct connection *conn, struct node *node, const char *name)
static void internal_rm(const char *name)
{
char *tname = talloc_strdup(NULL, name);
- struct node *node = read_node(NULL, tname);
+ struct node *node = read_node(NULL, tname, tname);
if (node)
_rm(NULL, node, tname);
talloc_free(node);
@@ -1073,7 +1074,7 @@ static void do_rm(struct connection *conn, struct buffered_data *in)
if (!node) {
/* Didn't exist already? Fine, if parent exists. */
if (errno == ENOENT) {
- node = read_node(conn, get_parent(in, name));
+ node = read_node(conn, in, get_parent(in, name));
if (node) {
send_ack(conn, XS_RM);
return;
@@ -1604,7 +1605,7 @@ static void remember_string(struct hashtable *hash, const char *str)
*/
static void check_store_(const char *name, struct hashtable *reachable)
{
- struct node *node = read_node(NULL, name);
+ struct node *node = read_node(NULL, name, name);
if (node) {
size_t i = 0;
@@ -1618,7 +1619,8 @@ static void check_store_(const char *name, struct hashtable *reachable)
size_t childlen = strlen(node->children + i);
char * childname = child_name(node->name,
node->children + i);
- struct node *childnode = read_node(NULL, childname);
+ struct node *childnode = read_node(NULL, childname,
+ childname);
if (childnode) {
if (hashtable_search(children, childname)) {
--
2.6.6
_______________________________________________
Xen-devel mailing list
Xen-devel@lists.xen.org
https://lists.xen.org/xen-devel
next prev parent reply other threads:[~2016-07-18 7:31 UTC|newest]
Thread overview: 17+ messages / expand[flat|nested] mbox.gz Atom feed top
2016-07-18 7:31 [PATCH v2 0/5] xenstore: fix memory leak of xenstored Juergen Gross
2016-07-18 7:31 ` [PATCH v2 1/5] xenstore: call each xenstored command function with temporary context Juergen Gross
2016-07-19 10:04 ` Wei Liu
2016-07-19 10:35 ` Ian Jackson
2016-07-18 7:31 ` [PATCH v2 2/5] xenstore: add explicit memory context parameter to get_parent() Juergen Gross
2016-07-19 10:04 ` Wei Liu
2016-07-19 10:37 ` Ian Jackson
2016-07-19 10:37 ` Ian Jackson
2016-07-18 7:31 ` Juergen Gross [this message]
2016-07-19 10:04 ` [PATCH v2 3/5] xenstore: add explicit memory context parameter to read_node() Wei Liu
2016-07-19 10:38 ` Ian Jackson
2016-07-18 7:31 ` [PATCH v2 4/5] xenstore: add explicit memory context parameter to get_node() Juergen Gross
2016-07-19 10:05 ` Wei Liu
2016-07-19 10:39 ` Ian Jackson
2016-07-18 7:31 ` [PATCH v2 5/5] xenstore: use temporary memory context for firing watches Juergen Gross
2016-07-19 10:23 ` Wei Liu
2016-07-19 10:40 ` Ian Jackson
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1468827089-9054-4-git-send-email-jgross@suse.com \
--to=jgross@suse.com \
--cc=ian.jackson@eu.citrix.com \
--cc=wei.liu2@citrix.com \
--cc=xen-devel@lists.xen.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).