[lxc-devel] [PATCH 11/17] Use srand/rand instead of initstate/random

Serge Hallyn serge.hallyn at ubuntu.com
Fri Aug 16 16:06:41 UTC 2013


Quoting Stéphane Graber (stgraber at ubuntu.com):
> On Fri, Aug 16, 2013 at 10:36:58AM -0500, Serge Hallyn wrote:
> > Quoting Stéphane Graber (stgraber at ubuntu.com):
> > > initstate/random doesn't work on bionic, srand/rand works on everything,
> > > so let's use that.
> > 
> > Is rand_r available on bionic?
> > 
> > rand() isn't thread-safe.
> 
> Unfortunately no, I actually don't think Bionic has any of the _r
> functions.
> 
> Is it actually a problem in this case? It looks like the worst that
> could happen is that the random number generator gets re-seeded from
> urandom between two calls, which would just make it even more random,
> no?

That was my guess based on the manpage, but I didn't look at the
source.  Sounds compelling,

Acked-by: Serge E. Hallyn <serge.hallyn at ubuntu.com>

> > > Signed-off-by: Stéphane Graber <stgraber at ubuntu.com>
> > > ---
> > >  src/lxc/conf.c | 5 ++---
> > >  1 file changed, 2 insertions(+), 3 deletions(-)
> > > 
> > > diff --git a/src/lxc/conf.c b/src/lxc/conf.c
> > > index 867a150..3d99469 100644
> > > --- a/src/lxc/conf.c
> > > +++ b/src/lxc/conf.c
> > > @@ -288,7 +288,6 @@ static char *mkifname(char *template)
> > >  	int i = 0;
> > >  	FILE *urandom;
> > >  	unsigned int seed;
> > > -	char randstate[2048];
> > >  	struct ifaddrs *ifaddr, *ifa;
> > >  	int ifexists = 0;
> > >  
> > > @@ -304,7 +303,7 @@ static char *mkifname(char *template)
> > >  	}
> > >  	else
> > >  		seed = time(0);
> > > -	initstate(seed, randstate, 256);
> > > +	srand(seed);
> > >  
> > >  	/* Generate random names until we find one that doesn't exist */
> > >  	while(1) {
> > > @@ -316,7 +315,7 @@ static char *mkifname(char *template)
> > >  
> > >  		for (i = 0; i < strlen(name); i++) {
> > >  			if (name[i] == 'X') {
> > > -				name[i] = padchar[random() % (strlen(padchar) - 1)];
> > > +				name[i] = padchar[rand() % (strlen(padchar) - 1)];
> > >  			}
> > >  		}
> > >  
> > > -- 
> > > 1.8.3.2
> > > 
> > > 
> > > ------------------------------------------------------------------------------
> > > Get 100% visibility into Java/.NET code with AppDynamics Lite!
> > > It's a free troubleshooting tool designed for production.
> > > Get down to code-level detail for bottlenecks, with <2% overhead. 
> > > Download for free and get started troubleshooting in minutes. 
> > > http://pubads.g.doubleclick.net/gampad/clk?id=48897031&iu=/4140/ostg.clktrk
> > > _______________________________________________
> > > Lxc-devel mailing list
> > > Lxc-devel at lists.sourceforge.net
> > > https://lists.sourceforge.net/lists/listinfo/lxc-devel
> 
> -- 
> Stéphane Graber
> Ubuntu developer
> http://www.ubuntu.com






More information about the lxc-devel mailing list