<div dir="ltr">On Mon, Sep 16, 2013 at 2:39 PM, Stéphane Graber <span dir="ltr"><<a href="mailto:stgraber@ubuntu.com" target="_blank">stgraber@ubuntu.com</a>></span> wrote:<br><div class="gmail_extra"><div class="gmail_quote">

<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div class="im">On Mon, Sep 16, 2013 at 02:26:47PM -0400, S.Çağlar Onur wrote:<br>


> get_ips accepts an interface name as a parameter but there was no<br>
> way to get the interfaces names from the container. This patch<br>
> introduces a new get_interfaces call to the API so that users<br>
> can obtain the name of the interfaces.<br>
><br>
> Signed-off-by: S.Çağlar Onur <<a href="mailto:caglar@10ur.org">caglar@10ur.org</a>><br>
<br>
</div>I think I'm fine with this version:<br>
<br>
One quick comment though, wouldn't we expect get_interfaces() to return<br>
all the interfaces by default, including loopback, including those<br>
without addresses and those that aren't standard packet based IP<br>
interfaces?<br>
<br>
I still think we don't want to list loopback addresses in get_ips(), at<br>
least by default, but I think I'd expect get_interfaces to return me all<br>
the existing interfaces without filtering. Does that make sense?</blockquote><div><br></div><div>Guess so. I've no objection including loopback in get_interfaces. I excluded it thinking that there is no point returning it as get_ips will ignore it. What about something like below as a replacement to v2 (this version starts to use if_nameindex instead of getifaddrs)?</div>

<div><br></div><div><div>+static char** lxcapi_get_interfaces(struct lxc_container *c)</div><div>+{</div><div>+       int count = 0;</div><div>+       struct if_nameindex *if_ni = NULL, *i;</div><div>+       char **interfaces = NULL, **temp;</div>

<div>+       int old_netns = -1, new_netns = -1;</div><div>+</div><div>+       if (!enter_to_ns(c, &old_netns, &new_netns))</div><div>+               goto out;</div><div>+</div><div>+       if_ni = if_nameindex();</div>

<div>+       if (if_ni == NULL) {</div><div>+               SYSERROR("failed to get interfaces list");</div><div>+               goto out;</div><div>+       }</div><div>+</div><div>+    for (i = if_ni; ! (i->if_index == 0 && i->if_name == NULL); i++) {</div>

<div>+        count += 1;</div><div>+        temp = realloc(interfaces, count * sizeof(*interfaces));</div><div>+        if (!temp) {</div><div>+            count -= 1;</div><div>+            goto out;</div><div>+        }</div>

<div>+        interfaces = temp;</div><div>+        interfaces[count - 1] = strdup(i->if_name);</div><div>+    }</div><div>+</div><div>+out:</div><div>+       if_freenameindex(if_ni);</div><div>+</div><div>+       exit_from_ns(c, &old_netns, &new_netns);</div>

<div>+</div><div>+       /* Append NULL to the array */</div><div>+       interfaces = (char **)lxc_append_null_to_array((void **)interfaces, count);</div><div>+</div><div>+       return interfaces;</div><div>+}</div></div>

<div> </div><div>And if we need get_ips to return ips then I guess we can always add a flag to get_ips as you suggested before?</div><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">

<div><div class="h5">
> ---<br>
>  src/lxc/lxccontainer.c | 117 ++++++++++++++++++++++++++++++++++---------------<br>
>  src/lxc/lxccontainer.h |   1 +<br>
>  src/lxc/utils.c        |  22 +++++++++-<br>
>  src/lxc/utils.h        |   1 +<br>
>  4 files changed, 105 insertions(+), 36 deletions(-)<br>
><br>
> diff --git a/src/lxc/lxccontainer.c b/src/lxc/lxccontainer.c<br>
> index 79237df..6aa59fc 100644<br>
> --- a/src/lxc/lxccontainer.c<br>
> +++ b/src/lxc/lxccontainer.c<br>
> @@ -1179,23 +1179,26 @@ static bool lxcapi_clear_config_item(struct lxc_container *c, const char *key)<br>
>       return ret == 0;<br>
>  }<br>
><br>
> -char** lxcapi_get_ips(struct lxc_container *c, char* interface, char* family, int scope)<br>
> -{<br>
> -     int count = 0;<br>
> -     struct ifaddrs *interfaceArray = NULL, *tempIfAddr = NULL;<br>
> -     char addressOutputBuffer[INET6_ADDRSTRLEN];<br>
> -     void *tempAddrPtr = NULL;<br>
> -     char **addresses = NULL, **temp;<br>
> -     char *address = NULL;<br>
> +static inline void exit_from_ns(struct lxc_container *c, int *old_netns, int *new_netns) {<br>
> +     /* Switch back to original netns */<br>
> +     if (*old_netns >= 0 && setns(*old_netns, CLONE_NEWNET))<br>
> +             SYSERROR("failed to setns");<br>
> +     if (*new_netns >= 0)<br>
> +             close(*new_netns);<br>
> +     if (*old_netns >= 0)<br>
> +             close(*old_netns);<br>
> +}<br>
> +<br>
> +static inline bool enter_to_ns(struct lxc_container *c, int *old_netns, int *new_netns) {<br>
> +     int ret = 0;<br>
>       char new_netns_path[MAXPATHLEN];<br>
> -     int old_netns = -1, new_netns = -1, ret = 0;<br>
><br>
>       if (!c->is_running(c))<br>
>               goto out;<br>
><br>
>       /* Save reference to old netns */<br>
> -     old_netns = open("/proc/self/ns/net", O_RDONLY);<br>
> -     if (old_netns < 0) {<br>
> +     *old_netns = open("/proc/self/ns/net", O_RDONLY);<br>
> +     if (*old_netns < 0) {<br>
>               SYSERROR("failed to open /proc/self/ns/net");<br>
>               goto out;<br>
>       }<br>
> @@ -1205,16 +1208,78 @@ char** lxcapi_get_ips(struct lxc_container *c, char* interface, char* family, in<br>
>       if (ret < 0 || ret >= MAXPATHLEN)<br>
>               goto out;<br>
><br>
> -     new_netns = open(new_netns_path, O_RDONLY);<br>
> -     if (new_netns < 0) {<br>
> +     *new_netns = open(new_netns_path, O_RDONLY);<br>
> +     if (*new_netns < 0) {<br>
>               SYSERROR("failed to open %s", new_netns_path);<br>
>               goto out;<br>
>       }<br>
><br>
> -     if (setns(new_netns, CLONE_NEWNET)) {<br>
> +     if (setns(*new_netns, CLONE_NEWNET)) {<br>
>               SYSERROR("failed to setns");<br>
>               goto out;<br>
>       }<br>
> +     return true;<br>
> +out:<br>
> +     exit_from_ns(c, old_netns, new_netns);<br>
> +     return false;<br>
> +}<br>
> +<br>
> +static char** lxcapi_get_interfaces(struct lxc_container *c)<br>
> +{<br>
> +     int count = 0;<br>
> +     struct ifaddrs *interfaceArray = NULL, *tempIfAddr = NULL;<br>
> +     char **interfaces = NULL, **temp;<br>
> +     int old_netns = -1, new_netns = -1;<br>
> +<br>
> +     if (!enter_to_ns(c, &old_netns, &new_netns))<br>
> +             goto out;<br>
> +<br>
> +     /* Grab the list of interfaces */<br>
> +     if (getifaddrs(&interfaceArray)) {<br>
> +             SYSERROR("failed to get interfaces list");<br>
> +             goto out;<br>
> +     }<br>
> +<br>
> +     /* Iterate through the interfaces */<br>
> +     for (tempIfAddr = interfaceArray; tempIfAddr != NULL; tempIfAddr = tempIfAddr->ifa_next) {<br>
> +             /* filter out loopback interface */<br>
> +             if(tempIfAddr->ifa_addr == NULL || tempIfAddr->ifa_addr->sa_family != AF_PACKET || strcmp("lo", tempIfAddr->ifa_name) == 0)<br>
> +                     continue;<br>
> +<br>
> +        count += 1;<br>
> +        temp = realloc(interfaces, count * sizeof(*interfaces));<br>
> +        if (!temp) {<br>
> +            count -= 1;<br>
> +            goto out;<br>
> +        }<br>
> +        interfaces = temp;<br>
> +        interfaces[count - 1] = strdup(tempIfAddr->ifa_name);<br>
> +    }<br>
> +<br>
> +out:<br>
> +     if(interfaceArray)<br>
> +             freeifaddrs(interfaceArray);<br>
> +<br>
> +     exit_from_ns(c, &old_netns, &new_netns);<br>
> +<br>
> +     /* Append NULL to the array */<br>
> +     interfaces = (char **)lxc_append_null_to_array((void **)interfaces, count);<br>
> +<br>
> +     return interfaces;<br>
> +}<br>
> +<br>
> +static char** lxcapi_get_ips(struct lxc_container *c, char* interface, char* family, int scope)<br>
> +{<br>
> +     int count = 0;<br>
> +     struct ifaddrs *interfaceArray = NULL, *tempIfAddr = NULL;<br>
> +     char addressOutputBuffer[INET6_ADDRSTRLEN];<br>
> +     void *tempAddrPtr = NULL;<br>
> +     char **addresses = NULL, **temp;<br>
> +     char *address = NULL;<br>
> +     int old_netns = -1, new_netns = -1;<br>
> +<br>
> +     if (!enter_to_ns(c, &old_netns, &new_netns))<br>
> +             goto out;<br>
><br>
>       /* Grab the list of interfaces */<br>
>       if (getifaddrs(&interfaceArray)) {<br>
> @@ -1243,7 +1308,6 @@ char** lxcapi_get_ips(struct lxc_container *c, char* interface, char* family, in<br>
>                       continue;<br>
>               else if (!interface && strcmp("lo", tempIfAddr->ifa_name) == 0)<br>
>                       continue;<br>
> -<br>
>               address = (char *)inet_ntop(tempIfAddr->ifa_addr->sa_family,<br>
>                                          tempAddrPtr,<br>
>                                          addressOutputBuffer,<br>
> @@ -1265,28 +1329,10 @@ out:<br>
>       if(interfaceArray)<br>
>               freeifaddrs(interfaceArray);<br>
><br>
> -     /* Switch back to original netns */<br>
> -     if (old_netns >= 0 && setns(old_netns, CLONE_NEWNET))<br>
> -             SYSERROR("failed to setns");<br>
> -     if (new_netns >= 0)<br>
> -             close(new_netns);<br>
> -     if (old_netns >= 0)<br>
> -             close(old_netns);<br>
> +     exit_from_ns(c, &old_netns, &new_netns);<br>
><br>
>       /* Append NULL to the array */<br>
> -     if (count) {<br>
> -             count++;<br>
> -             temp = realloc(addresses, count * sizeof(*addresses));<br>
> -             if (!temp) {<br>
> -                     int i;<br>
> -                     for (i = 0; i < count-1; i++)<br>
> -                             free(addresses[i]);<br>
> -                     free(addresses);<br>
> -                     return NULL;<br>
> -             }<br>
> -             addresses = temp;<br>
> -             addresses[count - 1] = NULL;<br>
> -     }<br>
> +     addresses = (char **)lxc_append_null_to_array((void **)addresses, count);<br>
><br>
>       return addresses;<br>
>  }<br>
> @@ -2576,6 +2622,7 @@ struct lxc_container *lxc_container_new(const char *name, const char *configpath<br>
>       c->get_config_path = lxcapi_get_config_path;<br>
>       c->set_config_path = lxcapi_set_config_path;<br>
>       c->clone = lxcapi_clone;<br>
> +     c->get_interfaces = lxcapi_get_interfaces;<br>
>       c->get_ips = lxcapi_get_ips;<br>
>       c->attach = lxcapi_attach;<br>
>       c->attach_run_wait = lxcapi_attach_run_wait;<br>
> diff --git a/src/lxc/lxccontainer.h b/src/lxc/lxccontainer.h<br>
> index f9ae43b..f17d975 100644<br>
> --- a/src/lxc/lxccontainer.h<br>
> +++ b/src/lxc/lxccontainer.h<br>
> @@ -90,6 +90,7 @@ struct lxc_container {<br>
>        * the length which was our would be printed. */<br>
>       int (*get_config_item)(struct lxc_container *c, const char *key, char *retv, int inlen);<br>
>       int (*get_keys)(struct lxc_container *c, const char *key, char *retv, int inlen);<br>
> +     char** (*get_interfaces)(struct lxc_container *c);<br>
>       char** (*get_ips)(struct lxc_container *c, char* interface, char* family, int scope);<br>
>       /*<br>
>        * get_cgroup_item returns the number of bytes read, or an error (<0).<br>
> diff --git a/src/lxc/utils.c b/src/lxc/utils.c<br>
> index 78b234d..924cc19 100644<br>
> --- a/src/lxc/utils.c<br>
> +++ b/src/lxc/utils.c<br>
> @@ -852,7 +852,7 @@ int lxc_write_to_file(const char *filename, const void* buf, size_t count, bool<br>
>               return -1;<br>
>       ret = lxc_write_nointr(fd, buf, count);<br>
>       if (ret < 0)<br>
> -             goto out_error;<br>
> +             goto out_error;<br>
>       if ((size_t)ret != count)<br>
>               goto out_error;<br>
>       if (add_newline) {<br>
> @@ -899,3 +899,23 @@ int lxc_read_from_file(const char *filename, void* buf, size_t count)<br>
>       errno = saved_errno;<br>
>       return ret;<br>
>  }<br>
> +<br>
> +void **lxc_append_null_to_array(void **array, size_t count)<br>
> +{<br>
> +     void **temp;<br>
> +<br>
> +     /* Append NULL to the array */<br>
> +     if (count) {<br>
> +             temp = realloc(array, (count + 1) * sizeof(*array));<br>
> +             if (!temp) {<br>
> +                     int i;<br>
> +                     for (i = 0; i < count; i++)<br>
> +                             free(array[i]);<br>
> +                     free(array);<br>
> +                     return NULL;<br>
> +             }<br>
> +             array = temp;<br>
> +             array[count] = NULL;<br>
> +     }<br>
> +     return array;<br>
> +}<br>
> diff --git a/src/lxc/utils.h b/src/lxc/utils.h<br>
> index ba7cfb3..55f98fa 100644<br>
> --- a/src/lxc/utils.h<br>
> +++ b/src/lxc/utils.h<br>
> @@ -236,4 +236,5 @@ extern void lxc_free_array(void **array, lxc_free_fn element_free_fn);<br>
>  extern size_t lxc_array_len(void **array);<br>
>  extern void **lxc_dup_array(void **array, lxc_dup_fn element_dup_fn, lxc_free_fn element_free_fn);<br>
><br>
> +extern void **lxc_append_null_to_array(void **array, size_t count);<br>
>  #endif<br>
> --<br>
> 1.8.1.2<br>
><br>
><br>
</div></div>> ------------------------------------------------------------------------------<br>
> LIMITED TIME SALE - Full Year of Microsoft Training For Just $49.99!<br>
> 1,500+ hours of tutorials including VisualStudio 2012, Windows 8, SharePoint<br>
> 2013, SQL 2012, MVC 4, more. BEST VALUE: New Multi-Library Power Pack includes<br>
> Mobile, Cloud, Java, and UX Design. Lowest price ever! Ends 9/20/13.<br>
> <a href="http://pubads.g.doubleclick.net/gampad/clk?id=58041151&iu=/4140/ostg.clktrk" target="_blank">http://pubads.g.doubleclick.net/gampad/clk?id=58041151&iu=/4140/ostg.clktrk</a><br>
> _______________________________________________<br>
> Lxc-devel mailing list<br>
> <a href="mailto:Lxc-devel@lists.sourceforge.net">Lxc-devel@lists.sourceforge.net</a><br>
> <a href="https://lists.sourceforge.net/lists/listinfo/lxc-devel" target="_blank">https://lists.sourceforge.net/lists/listinfo/lxc-devel</a><br>
<span class=""><font color="#888888"><br>
--<br>
Stéphane Graber<br>
Ubuntu developer<br>
<a href="http://www.ubuntu.com" target="_blank">http://www.ubuntu.com</a><br>
</font></span></blockquote></div><br><br clear="all"><div><br></div>-- <br>S.Çağlar Onur <<a href="mailto:caglar@10ur.org">caglar@10ur.org</a>>
</div></div>