Use a single set of FDs for all random plugin RNG instances

This commit is contained in:
Martin Willi
2012-03-20 17:31:40 +01:00
parent eff331f799
commit 35852af7b1
3 changed files with 89 additions and 36 deletions
@@ -15,9 +15,24 @@
#include "random_plugin.h"
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <library.h>
#include <debug.h>
#include "random_rng.h"
#ifndef DEV_RANDOM
# define DEV_RANDOM "/dev/random"
#endif
#ifndef DEV_URANDOM
# define DEV_URANDOM "/dev/urandom"
#endif
typedef struct private_random_plugin_t private_random_plugin_t;
/**
@@ -31,6 +46,41 @@ struct private_random_plugin_t {
random_plugin_t public;
};
/** /dev/random file descriptor */
static int dev_random = -1;
/** /dev/urandom file descriptor */
static int dev_urandom = -1;
/**
* See header.
*/
int random_plugin_get_dev_random()
{
return dev_random;
}
/**
* See header.
*/
int random_plugin_get_dev_urandom()
{
return dev_urandom;
}
/**
* Open a random device file
*/
static bool open_dev(char *file, int *fd)
{
*fd = open(file, O_RDONLY);
if (*fd == -1)
{
DBG1(DBG_LIB, "opening \"%s\" failed: %s", file, strerror(errno));
return FALSE;
}
return TRUE;
}
METHOD(plugin_t, get_name, char*,
private_random_plugin_t *this)
{
@@ -52,6 +102,14 @@ METHOD(plugin_t, get_features, int,
METHOD(plugin_t, destroy, void,
private_random_plugin_t *this)
{
if (dev_random != -1)
{
close(dev_random);
}
if (dev_urandom != -1)
{
close(dev_urandom);
}
free(this);
}
@@ -72,6 +130,13 @@ plugin_t *random_plugin_create()
},
);
if (!open_dev(DEV_URANDOM, &dev_urandom) ||
!open_dev(DEV_RANDOM, &dev_random))
{
destroy(this);
return NULL;
}
return &this->public.plugin;
}