[asterisk-commits] mmichelson: branch mmichelson/uuid r377845 - /team/mmichelson/uuid/main/uuid.c

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Tue Dec 11 14:55:36 CST 2012


Author: mmichelson
Date: Tue Dec 11 14:55:35 2012
New Revision: 377845

URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=377845
Log:
Add a runtime check for /dev/urandom instead of using the compile-time constant.


Modified:
    team/mmichelson/uuid/main/uuid.c

Modified: team/mmichelson/uuid/main/uuid.c
URL: http://svnview.digium.com/svn/asterisk/team/mmichelson/uuid/main/uuid.c?view=diff&rev=377845&r1=377844&r2=377845
==============================================================================
--- team/mmichelson/uuid/main/uuid.c (original)
+++ team/mmichelson/uuid/main/uuid.c Tue Dec 11 14:55:35 2012
@@ -22,15 +22,17 @@
 
 #include "asterisk.h"
 #include <uuid/uuid.h>
+#include <fcntl.h>
 
 #include "asterisk/uuid.h"
 #include "asterisk/utils.h"
 #include "asterisk/strings.h"
 #include "asterisk/logger.h"
-
-#ifndef HAVE_DEV_URANDOM
-AST_MUTEX_STATIC(uuid_lock);
-#endif
+#include "asterisk/lock.h"
+
+AST_MUTEX_DEFINE_STATIC(uuid_lock);
+
+static int has_dev_urandom;
 
 struct ast_uuid {
 	uuid_t uu;
@@ -100,18 +102,18 @@
 	 * Given these drawbacks, we stick to only using random UUIDs. The chance of /dev/random
 	 * or /dev/urandom not existing on systems in this age is next to none.
 	 */
-	
+
 	/* XXX Currently, we only protect this call if the user has no /dev/urandon on their system.
 	 * If it turns out that there are issues with UUID generation despite the presence of
 	 * /dev/urandom, then we may need to make the locking/unlocking unconditional.
 	 */
-#ifndef HAVE_DEV_URANDOM
-	ast_mutex_lock(&uuid_lock);
-#endif
+	if (!has_dev_urandom) {
+		ast_mutex_lock(&uuid_lock);
+	}
 	uuid_generate_random(uuid->uu);
-#ifndef HAVE_DEV_URANDOM
-	ast_mutex_unlock(&uuid_lock);
-#endif
+	if (!has_dev_urandom) {
+		ast_mutex_unlock(&uuid_lock);
+	}
 	return uuid;
 }
 
@@ -183,17 +185,20 @@
 	 * Think of this along the same lines as initializing a singleton.
 	 */
 	uuid_t uu;
-#ifndef HAVE_DEV_URANDOM
-	ast_log(LOG_WARNING, "It appears your system does not have /dev/urandom on it. This\n"
-			"means that UUID generation will use a pseudorandom number generator. This\n"
-			"has two implications:\n"
-			"    1. Since the thread-safety of your system's random number generator cannot\n"
-			"       be guaranteed, we have to synchronize UUID generation. This may result\n"
-			"       in decreased performance.\n"
-			"    2. Random number generation is not guaranteed to be as random, meaning there is\n"
-			"       a very remote chance you may see duplicate UUIDs.\n"
-			"It is highly recommended that you set up your system to have /dev/urandom\n");
-#endif
+	int dev_urandom_fd;
+
+	dev_urandom_fd = open("/dev/urandom", O_RDONLY);
+	if (dev_urandom_fd < 0) {
+		ast_log(LOG_WARNING, "It appears your system does not have /dev/urandom on it. This\n"
+				"means that UUID generation will use a pseudorandom number generator. Since\n"
+				"the thread-safety of your system's random number generator cannot\n"
+				"be guaranteed, we have to synchronize UUID generation. This may result\n"
+				"in decreased performance. It is highly recommended that you set up your\n"
+				"system to have /dev/urandom\n");
+	} else {
+		has_dev_urandom = 1;
+		close(dev_urandom_fd);
+	}
 	uuid_generate_random(uu);
 
 	ast_debug(1, "UUID system initiated\n");




More information about the asterisk-commits mailing list