From patchwork Tue Jul 19 11:30:44 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Juergen Gross X-Patchwork-Id: 9236797 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id ABDCD6075D for ; Tue, 19 Jul 2016 11:33:52 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 9E2CA205A4 for ; Tue, 19 Jul 2016 11:33:52 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 92AF3269E2; Tue, 19 Jul 2016 11:33:52 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-4.2 required=2.0 tests=BAYES_00, RCVD_IN_DNSWL_MED autolearn=ham version=3.3.1 Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) (using TLSv1.2 with cipher AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 2017E205A4 for ; Tue, 19 Jul 2016 11:33:52 +0000 (UTC) Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.84_2) (envelope-from ) id 1bPTEi-0007BT-JT; Tue, 19 Jul 2016 11:30:52 +0000 Received: from mail6.bemta3.messagelabs.com ([195.245.230.39]) by lists.xenproject.org with esmtp (Exim 4.84_2) (envelope-from ) id 1bPTEh-0007Ai-Vw for xen-devel@lists.xen.org; Tue, 19 Jul 2016 11:30:52 +0000 Received: from [85.158.137.68] by server-16.bemta-3.messagelabs.com id E1/2A-29579-B6F0E875; Tue, 19 Jul 2016 11:30:51 +0000 X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFrrGLMWRWlGSWpSXmKPExsVyuP0Ov24Wf1+ 4wZ0PChZLPi5mcWD0OLr7N1MAYxRrZl5SfkUCa8bb/pvsBZNVK27ub2ZrYFwn18XIySEhYCTx duI/pi5GLg4hgYWMEq8bpjKDJNgEVCU2XD/FCmKLCEhLXPt8mRHEZhYIlTj57h5YjbBAmMTyl 7uYQGwWoPqZR2axgNi8AvYSV3cuYoNYICdxfeZ0sBpOAQeJu11rwGYKAdWc39vEOoGRewEjwy pGjeLUorLUIl1DQ72kosz0jJLcxMwcXUMDY73c1OLixPTUnMSkYr3k/NxNjED/MgDBDsbVv50 OMUpyMCmJ8qqK9oYL8SXlp1RmJBZnxBeV5qQWH2KU4eBQkuBV4esLFxIsSk1PrUjLzAEGGkxa goNHSYRXASTNW1yQmFucmQ6ROsWoKCXOu40XKCEAksgozYNrgwX3JUZZKWFeRqBDhHgKUotyM 0tQ5V8xinMwKgnzMoGM58nMK4Gb/gpoMRPQYgPVbpDFJYkIKakGxszgg5X2zZNOWD0of6F3uf 2YVNaXCX25C82MFMvF5z2+4SDr+fCPwNbTWac9Dh1zXqPKv2yKx9zJR3Ya3d9qeXozf4DYzHf LJ+9ewML05rn8ZnnxSgG9KU32iwU+3NUrV5FStDFV/6GxPbHqUEfu6cpHV65d35hZsaRC9doh niNcDok/WqQEHymxFGckGmoxFxUnAgD5FG9vaQIAAA== X-Env-Sender: jgross@suse.com X-Msg-Ref: server-3.tower-31.messagelabs.com!1468927850!51313260!1 X-Originating-IP: [195.135.220.15] X-SpamReason: No, hits=0.0 required=7.0 tests= X-StarScan-Received: X-StarScan-Version: 8.77; banners=-,-,- X-VirusChecked: Checked Received: (qmail 12526 invoked from network); 19 Jul 2016 11:30:50 -0000 Received: from mx2.suse.de (HELO mx2.suse.de) (195.135.220.15) by server-3.tower-31.messagelabs.com with DHE-RSA-CAMELLIA256-SHA encrypted SMTP; 19 Jul 2016 11:30:50 -0000 X-Virus-Scanned: by amavisd-new at test-mx.suse.de Received: from relay2.suse.de (charybdis-ext.suse.de [195.135.220.254]) by mx2.suse.de (Postfix) with ESMTP id 88C33ADC4; Tue, 19 Jul 2016 11:30:49 +0000 (UTC) From: Juergen Gross To: xen-devel@lists.xen.org Date: Tue, 19 Jul 2016 13:30:44 +0200 Message-Id: <1468927846-23676-4-git-send-email-jgross@suse.com> X-Mailer: git-send-email 2.6.6 In-Reply-To: <1468927846-23676-1-git-send-email-jgross@suse.com> References: <1468927846-23676-1-git-send-email-jgross@suse.com> Cc: Juergen Gross , wei.liu2@citrix.com, ian.jackson@eu.citrix.com Subject: [Xen-devel] [PATCH v3 3/5] xenstore: add explicit memory context parameter to read_node() X-BeenThere: xen-devel@lists.xen.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: xen-devel-bounces@lists.xen.org Sender: "Xen-devel" X-Virus-Scanned: ClamAV using ClamSMTP 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 Reviewed-by: Wei Liu Acked-by: Ian Jackson --- tools/xenstore/xenstored_core.c | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/tools/xenstore/xenstored_core.c b/tools/xenstore/xenstored_core.c index f2c12ab..c462115 100644 --- a/tools/xenstore/xenstored_core.c +++ b/tools/xenstore/xenstored_core.c @@ -397,8 +397,12 @@ bool is_child(const char *child, const char *parent) return child[len] == '/' || child[len] == '\0'; } -/* If it fails, returns NULL and sets errno. */ -static struct node *read_node(struct connection *conn, const char *name) +/* + * If it fails, returns NULL and sets errno. + * Temporary memory allocations will be done with ctx. + */ +static struct node *read_node(struct connection *conn, const void *ctx, + const char *name) { TDB_DATA key, data; uint32_t *p; @@ -419,7 +423,7 @@ static struct node *read_node(struct connection *conn, const char *name) return NULL; } - node = talloc(name, struct node); + node = talloc(ctx, struct node); node->name = talloc_strdup(node, name); node->parent = NULL; node->tdb = tdb_context(conn); @@ -526,7 +530,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, "/")); @@ -567,7 +571,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) @@ -823,7 +827,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) @@ -988,7 +992,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) { @@ -1040,7 +1044,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; @@ -1059,7 +1063,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); @@ -1077,7 +1081,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; @@ -1608,7 +1612,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; @@ -1622,7 +1626,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)) {