[parted-devel] generate_random_id() fix
Jim Meyering
jim at meyering.net
Wed Jul 11 20:48:28 UTC 2007
"H. Peter Anvin" <hpa at zytor.com> wrote:
> The existing generate_random_id() in libparted only produces numbers in
> the range 0..999999, less than 20 bits. This patch tries to get a
> random number from /dev/urandom if available, and otherwise produces a
> 32-bit value from gettimeofday().
...
> -static inline uint32_t generate_random_id (void)
> +static uint32_t
> +generate_random_id (void)
> {
> + int fd;
> struct timeval tv;
> int rc;
> + uint32_t v;
> +
> + fd = open("/dev/urandom", O_RDONLY);
Thanks for the patch.
It's definitely an improvement.
However, since there's already code to generate a 32-bit
serial number in fs/fat/fat.c:
/* hack: use the ext2 uuid library to generate a reasonably random (hopefully
* with /dev/random) number. Unfortunately, we can only use 4 bytes of it
*/
static uint32_t
_gen_new_serial_number ()
{
uuid_t uuid;
uuid_generate (uuid);
return * (uint32_t*) &uuid [0];
}
I'm thinking about using something like this from both places:
static uint32_t
_gen_new_serial_number (void)
{
static uuid_t uuid;
static unsigned int i = 0;
unsigned int n = sizeof uuid / sizeof (uint32_t);
if (i % n == 0)
{
uuid_generate (uuid);
i = 0;
}
return ((uint32_t *)uuid)[i++];
}
What do you think?
More information about the parted-devel
mailing list