Quoting S.Çağlar Onur (cag...@10ur.org):
> Hi Serge,
> 
> [resending as list rejected my first mail due to its size]
> 
> On Wed, Apr 24, 2013 at 9:26 AM, Serge Hallyn <serge.hal...@ubuntu.com>wrote:
> >
> > +static void new_hwaddr(char *hwaddr)
> > +{
> > +       snprintf(hwaddr, 18, "00:16:3e:%02x:%02x:%02x",
> > +                       rand() % 255, rand() % 255, rand() % 255);
> > +}
> >
> 
> I believe we need to call srand here otherwise it will always give the same
> set of numbers with the same sequence

Subject: [PATCH 1/2] initialize random generator when picking new macaddr

Also fix wrong use of bitmask flags

Signed-off-by: Serge Hallyn <serge.hal...@ubuntu.com>
---
 src/lxc/lxccontainer.c | 15 +++++++++++++--
 src/lxc/lxccontainer.h | 14 ++++++--------
 2 files changed, 19 insertions(+), 10 deletions(-)

diff --git a/src/lxc/lxccontainer.c b/src/lxc/lxccontainer.c
index e4ff92c..ef111c0 100644
--- a/src/lxc/lxccontainer.c
+++ b/src/lxc/lxccontainer.c
@@ -1248,6 +1248,16 @@ static int copyhooks(struct lxc_container *oldc, struct 
lxc_container *c)
 
 static void new_hwaddr(char *hwaddr)
 {
+       FILE *f = fopen("/dev/urandom", "r");
+       if (f) {
+               unsigned int seed;
+               int ret = fread(&seed, sizeof(seed), 1, f);
+               if (ret != 1)
+                       seed = time(NULL);
+               fclose(f);
+               srand(seed);
+       } else
+               srand(time(NULL));
        snprintf(hwaddr, 18, "00:16:3e:%02x:%02x:%02x",
                        rand() % 255, rand() % 255, rand() % 255);
 }
@@ -1255,6 +1265,7 @@ static void new_hwaddr(char *hwaddr)
 static void network_new_hwaddrs(struct lxc_container *c)
 {
        struct lxc_list *it;
+
        lxc_list_for_each(it, &c->lxc_conf->network) {
                struct lxc_netdev *n = it->elem;
                if (n->hwaddr)
@@ -1395,7 +1406,7 @@ static int create_file_dirname(char *path)
 }
 
 struct lxc_container *lxcapi_clone(struct lxc_container *c, const char 
*newname,
-               const char *lxcpath, enum lxc_clone_flags flags,
+               const char *lxcpath, int flags,
                const char *bdevtype, const char *bdevdata, unsigned long 
newsize)
 {
        struct lxc_container *c2 = NULL;
@@ -1509,7 +1520,7 @@ out:
        lxcunlock(c->privlock);
        if (c2)
                lxc_container_put(c2);
-               
+
        return NULL;
 }
 
diff --git a/src/lxc/lxccontainer.h b/src/lxc/lxccontainer.h
index 3bebdf3..a4be753 100644
--- a/src/lxc/lxccontainer.h
+++ b/src/lxc/lxccontainer.h
@@ -6,13 +6,11 @@
 
 #include <stdbool.h>
 
-enum lxc_clone_flags {
-       LXC_CLONE_KEEPNAME,
-       LXC_CLONE_COPYHOOKS,
-       LXC_CLONE_KEEPMACADDR,
-       LXC_CLONE_SNAPSHOT,
-       LXC_CLONE_MAXFLAGS,
-};
+#define LXC_CLONE_KEEPNAME        (1 << 0)
+#define LXC_CLONE_COPYHOOKS       (1 << 1)
+#define LXC_CLONE_KEEPMACADDR     (1 << 2)
+#define LXC_CLONE_SNAPSHOT        (1 << 3)
+#define LXC_CLONE_MAXFLAGS        (1 << 4)
 
 struct lxc_container {
        // private fields
@@ -106,7 +104,7 @@ struct lxc_container {
         *  will be duplicated.
         */
        struct lxc_container *(*clone)(struct lxc_container *c, const char 
*newname,
-               const char *lxcpath, enum lxc_clone_flags flags, const char 
*bdevtype,
+               const char *lxcpath, int flags, const char *bdevtype,
                const char *bdevdata, unsigned long newsize);
 
 #if 0
-- 
1.8.1.2


------------------------------------------------------------------------------
Try New Relic Now & We'll Send You this Cool Shirt
New Relic is the only SaaS-based application performance monitoring service 
that delivers powerful full stack analytics. Optimize and monitor your
browser, app, & servers with just a few lines of code. Try New Relic
and get this awesome Nerd Life shirt! http://p.sf.net/sfu/newrelic_d2d_apr
_______________________________________________
Lxc-devel mailing list
Lxc-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/lxc-devel

Reply via email to