[lxc-devel] [PATCH] export lxc_cmd_get_config_item via API and use in lxc-info (v2)

S.Çağlar Onur caglar at 10ur.org
Mon Jan 27 22:57:37 UTC 2014


This allows external users to query network related config items
from the running containers.

changes since v1:
- function name change

Signed-off-by: S.Çağlar Onur <caglar at 10ur.org>
---
 src/lxc/lxc_info.c     |  8 ++++----
 src/lxc/lxccontainer.c | 14 ++++++++++++++
 src/lxc/lxccontainer.h | 13 +++++++++++++
 3 files changed, 31 insertions(+), 4 deletions(-)

diff --git a/src/lxc/lxc_info.c b/src/lxc/lxc_info.c
index ee45c58..7915c0e 100644
--- a/src/lxc/lxc_info.c
+++ b/src/lxc/lxc_info.c
@@ -139,7 +139,7 @@ static unsigned long long str_size_humanize(char *iobuf, size_t iobufsz)
 	return val;
 }
 
-static void print_net_stats(const char *name, const char *lxcpath)
+static void print_net_stats(struct lxc_container *c)
 {
 	int rc,netnr;
 	unsigned long long rx_bytes = 0, tx_bytes = 0;
@@ -149,7 +149,7 @@ static void print_net_stats(const char *name, const char *lxcpath)
 
 	for(netnr = 0; ;netnr++) {
 		sprintf(buf, "lxc.network.%d.type", netnr);
-		type = lxc_cmd_get_config_item(name, buf, lxcpath);
+		type = c->get_running_config_item(c, buf);
 		if (!type)
 			break;
 
@@ -159,7 +159,7 @@ static void print_net_stats(const char *name, const char *lxcpath)
 			sprintf(buf, "lxc.network.%d.link", netnr);
 		}
 		free(type);
-		ifname = lxc_cmd_get_config_item(name, buf, lxcpath);
+		ifname = c->get_running_config_item(c, buf);
 		if (!ifname)
 			return;
 		printf("%-15s %s\n", "Link:", ifname);
@@ -326,7 +326,7 @@ static int print_info(const char *name, const char *lxcpath)
 
 	if (stats) {
 		print_stats(c);
-		print_net_stats(name, lxcpath);
+		print_net_stats(c);
 	}
 
 	for(i = 0; i < keys; i++) {
diff --git a/src/lxc/lxccontainer.c b/src/lxc/lxccontainer.c
index dcdeaad..aad692f 100644
--- a/src/lxc/lxccontainer.c
+++ b/src/lxc/lxccontainer.c
@@ -1738,6 +1738,19 @@ static int lxcapi_get_config_item(struct lxc_container *c, const char *key, char
 	return ret;
 }
 
+static char* lxcapi_get_running_config_item(struct lxc_container *c, const char *key)
+{
+	char *ret;
+
+	if (!c || !c->lxc_conf)
+		return NULL;
+	if (container_mem_lock(c))
+		return NULL;
+	ret = lxc_cmd_get_config_item(c->name, key, c->get_config_path(c));
+	container_mem_unlock(c);
+	return ret;
+}
+
 static int lxcapi_get_keys(struct lxc_container *c, const char *key, char *retv, int inlen)
 {
 	if (!key)
@@ -3259,6 +3272,7 @@ struct lxc_container *lxc_container_new(const char *name, const char *configpath
 	c->clear_config = lxcapi_clear_config;
 	c->clear_config_item = lxcapi_clear_config_item;
 	c->get_config_item = lxcapi_get_config_item;
+	c->get_running_config_item = lxcapi_get_running_config_item;
 	c->get_cgroup_item = lxcapi_get_cgroup_item;
 	c->set_cgroup_item = lxcapi_set_cgroup_item;
 	c->get_config_path = lxcapi_get_config_path;
diff --git a/src/lxc/lxccontainer.h b/src/lxc/lxccontainer.h
index 84caa31..5f5d9b2 100644
--- a/src/lxc/lxccontainer.h
+++ b/src/lxc/lxccontainer.h
@@ -410,6 +410,19 @@ struct lxc_container {
 	 */
 	int (*get_config_item)(struct lxc_container *c, const char *key, char *retv, int inlen);
 
+
+	/*!
+	 * \brief Retrieve the value of a config item from running container.
+	 *
+	 * \param c Container.
+	 * \param key Name of option to get.
+	 *
+	 * \return the item or NULL on error.
+	 *
+	 * \note Returned string must be freed by the caller.
+	 */
+	char* (*get_running_config_item)(struct lxc_container *c, const char *key);
+
 	/*!
 	 * \brief Retrieve a list of config item keys given a key
 	 * prefix.
-- 
1.8.3.2



More information about the lxc-devel mailing list