summaryrefslogtreecommitdiff
path: root/vswitchd/xenserver.c
diff options
context:
space:
mode:
authorBen Pfaff <blp@nicira.com>2013-04-23 13:21:07 -0700
committerBen Pfaff <blp@nicira.com>2013-07-22 10:53:09 -0700
commitd3aa37104caa3a936db67ad8807af1c3d5ff35d8 (patch)
treefb3c5c6a0aaeadd5bf2f7cb82fb62d3d77b1acbc /vswitchd/xenserver.c
parent4663f9e0dab9d5f21ddb28f124d1f66d97a9db35 (diff)
downloadopenvswitch-d3aa37104caa3a936db67ad8807af1c3d5ff35d8.tar.gz
xenserver: Make thread-safe.
Signed-off-by: Ben Pfaff <blp@nicira.com>
Diffstat (limited to 'vswitchd/xenserver.c')
-rw-r--r--vswitchd/xenserver.c23
1 files changed, 11 insertions, 12 deletions
diff --git a/vswitchd/xenserver.c b/vswitchd/xenserver.c
index 1f26e0d17..bc57f0ead 100644
--- a/vswitchd/xenserver.c
+++ b/vswitchd/xenserver.c
@@ -17,6 +17,7 @@
#include "xenserver.h"
#include <ctype.h>
#include <errno.h>
+#include <pthread.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
@@ -26,7 +27,11 @@
VLOG_DEFINE_THIS_MODULE(xenserver);
-static char *
+/* If running on a XenServer, the XenServer host UUID as a 36-character string,
+ * otherwise null. */
+static char *host_uuid;
+
+static void
read_host_uuid(void)
{
static const char filename[] = "/etc/xensource-inventory";
@@ -40,7 +45,7 @@ read_host_uuid(void)
} else {
VLOG_INFO("%s: open: %s", filename, ovs_strerror(errno));
}
- return NULL;
+ return;
}
while (fgets(line, sizeof line, file)) {
@@ -53,27 +58,21 @@ read_host_uuid(void)
if (strlen(line) == leader_len + uuid_len + trailer_len
&& !memcmp(line, leader, leader_len)
&& !memcmp(line + leader_len + uuid_len, trailer, trailer_len)) {
- char *host_uuid = xmemdup0(line + leader_len, uuid_len);
+ host_uuid = xmemdup0(line + leader_len, uuid_len);
VLOG_INFO("running on XenServer, host-uuid %s", host_uuid);
fclose(file);
- return host_uuid;
+ return;
}
}
fclose(file);
VLOG_ERR("%s: INSTALLATION_UUID not found", filename);
- return NULL;
}
const char *
xenserver_get_host_uuid(void)
{
- static char *host_uuid;
- static bool inited;
-
- if (!inited) {
- host_uuid = read_host_uuid();
- inited = true;
- }
+ static pthread_once_t once = PTHREAD_ONCE_INIT;
+ pthread_once(&once, read_host_uuid);
return host_uuid;
}