diff -ur src/lib/coil/posix/coil/UUID.cpp.orig src/lib/coil/posix/coil/UUID.cpp
--- src/lib/coil/posix/coil/UUID.cpp.orig	2012-05-24 23:18:14.000000000 +0900
+++ src/lib/coil/posix/coil/UUID.cpp	2012-07-17 19:24:47.000000000 +0900
@@ -1,128 +1,319 @@
-// -*- C++ -*-
-/*!
- * @file  MutexPosix.h
- * @brief RT-Middleware Service interface
- * @date  $Date$
- * @author Noriaki Ando <n-ando@aist.go.jp>
- *
- * Copyright (C) 2008
- *     Noriaki Ando
- *     Task-intelligence Research Group,
- *     Intelligent Systems Research Institute,
- *     National Institute of
- *         Advanced Industrial Science and Technology (AIST), Japan
- *     All rights reserved.
- *
- * $Id$
- *
- */
-
-#include <coil/UUID.h>
-#include <iostream>
-#include <string.h>
-
-#ifdef COIL_OS_FREEBSD
-void error_code(uint32_t status)
-{
-  if (status == uuid_s_ok)
-    std::cout << "uuid_s_ok" << std::endl;
-  else if (status == uuid_s_bad_version)
-    std::cout << "uuid_s_bad_version" << std::endl;
-  else if (status == uuid_s_invalid_string_uuid)
-    std::cout << "uuid_s_invalid_string_uuid" << std::endl;
-  else if (status == uuid_s_no_memory)
-    std::cout << "uuid_s_no_memory" << std::endl;
-  else
-    std::cout << "other error" << std::endl;
-}
-
-void uuid_clear(uuid_t& uu)
-{
-  uint32_t status;
-  uuid_create_nil(&uu, &status);
-}
-void uuid_unparse(uuid_t& uu, char*& uuidstr)
-{
-  uint32_t status;
-  uuid_to_string(&uu, &uuidstr, &status);
-}
-void uuid_generate(uuid_t& out)
-{
-  uint32_t status;
-  uuid_create(&out, &status);
-}
-#endif
-
-namespace coil
-{
-
-#ifdef COIL_OS_FREEBSD
-  UUID::UUID()
-    : m_uuidstr(0)
-  {
-    ::uuid_clear(m_uuid);
-  }
-  UUID::UUID(const uuid_t& uuid)
-    : m_uuid(uuid), m_uuidstr(0)
-  {
-  }
-
-  UUID::~UUID()
-  {
-    free(m_uuidstr);
-  }
-
-  const char* UUID::to_string()
-  {
-    uuid_unparse(m_uuid, m_uuidstr);
-    return m_uuidstr;
-  }
-    
-
-  UUID_Generator::UUID_Generator()
-  {
-  }
-
-  UUID_Generator::~UUID_Generator()
-  {
-  }
-
-  void UUID_Generator::init()
-  {
-  }
-
-  UUID* UUID_Generator::generateUUID(int n, int h)
-  {
-    uuid_t uuid;
-    uuid_generate(uuid);
-    return new UUID(uuid);
-  }
-#endif
-
-#if defined(COIL_OS_LINUX) || defined(COIL_OS_DARWIN) || defined(COIL_OS_CYGWIN)
-
-  UUID_Generator::UUID_Generator(){}
-  
-  void UUID_Generator::init(){}
-  UUID* UUID_Generator::generateUUID(int varsion, int variant){
-    uuid_t uuid;
-    
-    uuid_generate(uuid);
-    return new UUID(&uuid);
-  }
-  
-  UUID::UUID(){
-    uuid_clear(this->_uuid);
-  }
-  
-  UUID::UUID(uuid_t *uuid){
-    strncpy((char *)this->_uuid, (char *)(*uuid), sizeof(this->_uuid));
-  }
-  
-  const char* UUID::to_string(){
-    uuid_unparse(this->_uuid, buf);
-    return buf;
-  }
-
-#endif
-};
+// -*- C++ -*-
+/*
+ * Copyright (C) 1996, 1997 Theodore Ts'o.
+ *
+ * %Begin-Header%
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, and the entire permission notice in its entirety,
+ *    including the disclaimer of warranties.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. The name of the author may not be used to endorse or promote
+ *    products derived from this software without specific prior
+ *    written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ALL OF
+ * WHICH ARE HEREBY DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
+ * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
+ * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
+ * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
+ * USE OF THIS SOFTWARE, EVEN IF NOT ADVISED OF THE POSSIBILITY OF SUCH
+ * DAMAGE.
+ * %End-Header%
+ */
+/*!
+ * @file  MutexPosix.h
+ * @brief RT-Middleware Service interface
+ * @date  $Date$
+ * @author Noriaki Ando <n-ando@aist.go.jp>
+ *
+ * Copyright (C) 2008
+ *     Noriaki Ando
+ *     Task-intelligence Research Group,
+ *     Intelligent Systems Research Institute,
+ *     National Institute of
+ *         Advanced Industrial Science and Technology (AIST), Japan
+ *     All rights reserved.
+ *
+ * $Id$
+ *
+ */
+
+#include <coil/UUID.h>
+#include <coil/Time.h>
+#include <iostream>
+#include <string.h>
+
+#include <errno.h>
+#include <unistd.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <fcntl.h>
+
+void uuid_unpack(const uuid_t in, struct uuid *uu)
+{
+	const uint8_t	*ptr = in;
+	uint32_t		tmp;
+
+	tmp = *ptr++;
+	tmp = (tmp << 8) | *ptr++;
+	tmp = (tmp << 8) | *ptr++;
+	tmp = (tmp << 8) | *ptr++;
+	uu->time_low = tmp;
+
+	tmp = *ptr++;
+	tmp = (tmp << 8) | *ptr++;
+	uu->time_mid = tmp;
+
+	tmp = *ptr++;
+	tmp = (tmp << 8) | *ptr++;
+	uu->time_hi_and_version = tmp;
+
+	tmp = *ptr++;
+	tmp = (tmp << 8) | *ptr++;
+	uu->clock_seq = tmp;
+
+	memcpy(uu->node, ptr, 6);
+}
+void uuid_pack(const struct uuid *uu, uuid_t ptr)
+{
+	uint32_t	tmp;
+	unsigned char	*out = ptr;
+
+	tmp = uu->time_low;
+	out[3] = (unsigned char) tmp;
+	tmp >>= 8;
+	out[2] = (unsigned char) tmp;
+	tmp >>= 8;
+	out[1] = (unsigned char) tmp;
+	tmp >>= 8;
+	out[0] = (unsigned char) tmp;
+
+	tmp = uu->time_mid;
+	out[5] = (unsigned char) tmp;
+	tmp >>= 8;
+	out[4] = (unsigned char) tmp;
+
+	tmp = uu->time_hi_and_version;
+	out[7] = (unsigned char) tmp;
+	tmp >>= 8;
+	out[6] = (unsigned char) tmp;
+
+	tmp = uu->clock_seq;
+	out[9] = (unsigned char) tmp;
+	tmp >>= 8;
+	out[8] = (unsigned char) tmp;
+
+	memcpy(out+10, uu->node, 6);
+}
+
+
+namespace coil
+{
+  UUID_Generator::UUID_Generator(){}
+  
+  void UUID_Generator::init(){}
+  UUID* UUID_Generator::generateUUID(int varsion, int variant){
+    uuid_t uuid;
+    
+    uuid_generate(uuid);
+    return new UUID(&uuid);
+  }
+  
+	void UUID_Generator::uuid_generate(uuid_t out)
+	{
+		struct timeval	tv;
+		int				i;
+
+		/* Crank the random number generator a few times */
+		coil::gettimeofday(&tv, (struct timezone*)0);
+		for (i = (tv.tv_sec ^ tv.tv_usec) & 0x1F; i > 0; i--)
+			rand();
+
+		uuid_generate_time(out, 0);
+	}
+
+	void UUID_Generator::uuid_generate_time(uuid_t out, int *num)
+	{
+		static unsigned char	node_id[6];
+		static int				has_init = 0;
+		struct uuid				uu;
+		uint32_t				clock_mid;
+
+		if (!has_init) {
+			get_random_bytes(node_id, 6);
+			/*
+			* Set multicast bit, to prevent conflicts
+			* with IEEE 802 addresses obtained from
+			* network cards
+			*/
+			node_id[0] |= 0x01;
+			has_init = 1;
+		}
+		get_clock(&clock_mid, &uu.time_low, &uu.clock_seq, num);
+		uu.clock_seq |= 0x8000;
+		uu.time_mid = (uint16_t) clock_mid;
+		uu.time_hi_and_version = ((clock_mid >> 16) & 0x0FFF) | 0x1000;
+		memcpy(uu.node, node_id, 6);
+		uuid_pack(&uu, out);
+	}
+
+	/**
+	* Assume that the gettimeofday() has microsecond granularity
+	**/
+	int UUID_Generator::get_clock(uint32_t *clock_high, uint32_t *clock_low, uint16_t *ret_clock_seq, int *num)
+	{
+		THREAD_LOCAL int			adjustment = 0;
+		THREAD_LOCAL struct timeval	last = {0, 0};
+		THREAD_LOCAL int			state_fd = -2;
+		THREAD_LOCAL FILE			*state_f;
+		THREAD_LOCAL uint16_t		clock_seq;
+		struct timeval 				tv;
+		struct flock				fl;
+		uint64_t					clock_reg;
+
+		if (state_fd == -2) {
+			state_fd = open("./clock.txt", O_RDWR|O_CREAT, 0660);
+			state_f = fdopen(state_fd, "r+");
+			if (!state_f) {
+				close(state_fd);
+				state_fd = -1;
+			}
+		}
+		fl.l_type = F_WRLCK;
+		fl.l_whence = SEEK_SET;
+		fl.l_start = 0;
+		fl.l_len = 0;
+		fl.l_pid = 0;
+		if (state_fd >= 0) {
+			rewind(state_f);
+			while (fcntl(state_fd, F_SETLKW, &fl) < 0) {
+				if ((errno == EAGAIN) || (errno == EINTR))
+					continue;
+				fclose(state_f);
+				close(state_fd);
+				state_fd = -1;
+				break;
+			}
+		}
+		if (state_fd >= 0) {
+			unsigned int	cl;
+			unsigned long	tv1, tv2;
+			int				a;
+
+			if (fscanf(state_f, "clock: %04x tv: %lu %lu adj: %d\n", &cl, &tv1, &tv2, &a) == 4) {
+				clock_seq    = cl & 0x3FFF;
+				last.tv_sec  = tv1;
+				last.tv_usec = tv2;
+				adjustment   = a;
+			}
+		}
+
+		if ((last.tv_sec == 0) && (last.tv_usec == 0)) {
+			get_random_bytes(&clock_seq, sizeof(clock_seq));
+			clock_seq &= 0x3FFF;
+			coil::gettimeofday(&last, (struct timezone*)0);
+			last.tv_sec--;
+		}
+
+try_again:
+		coil::gettimeofday(&tv, (struct timezone*)0);
+		if ((tv.tv_sec < last.tv_sec) || ((tv.tv_sec == last.tv_sec) && (tv.tv_usec < last.tv_usec))) {
+			clock_seq = (clock_seq+1) & 0x3FFF;
+			adjustment = 0;
+			last = tv;
+		} else if ((tv.tv_sec == last.tv_sec) && (tv.tv_usec == last.tv_usec)) {
+			if (adjustment >= MAX_ADJUSTMENT)
+				goto try_again;
+			adjustment++;
+		} else {
+			adjustment = 0;
+			last = tv;
+		}
+
+		clock_reg = tv.tv_usec*10 + adjustment;
+		clock_reg += ((uint64_t) tv.tv_sec)*10000000;
+		clock_reg += (((uint64_t) 0x01B21DD2) << 32) + 0x13814000;
+
+		if (num && (*num > 1)) {
+			adjustment += *num - 1;
+			last.tv_usec += adjustment / 10;
+			adjustment = adjustment % 10;
+			last.tv_sec += last.tv_usec / 1000000;
+			last.tv_usec = last.tv_usec % 1000000;
+		}
+
+		if (state_fd > 0) {
+			rewind(state_f);
+			ftruncate(state_fd, 0);
+			fprintf(state_f, "clock: %04x tv: %lu %lu adj: %d\n", 
+								clock_seq, last.tv_sec, last.tv_usec, adjustment);
+			fflush(state_f);
+			rewind(state_f);
+			fl.l_type = F_UNLCK;
+			fcntl(state_fd, F_SETLK, &fl);
+		}
+
+		*clock_high    = clock_reg >> 32;
+		*clock_low     = clock_reg;
+		*ret_clock_seq = clock_seq;
+		return 0;
+	}
+
+
+	void UUID_Generator::get_random_bytes(void *buf, int nbytes)
+	{
+		int i;
+		unsigned char *cp;
+
+		/*
+		 * We do this all the time, but this is the only source of
+		 * randomness if /dev/random/urandom is out to lunch.
+		 */
+		for (cp = (unsigned char*)buf, i = 0; i < nbytes; i++)
+			*cp++ ^= (rand() >> 7) & 0xFF;
+		return;
+	}
+
+  UUID::UUID(){
+    uuid_clear(this->_uuid);
+  }
+  
+  UUID::UUID(uuid_t *uuid){
+    memcpy((char *)this->_uuid, (char *)(*uuid), sizeof(_uuid));
+  }
+  
+  const char* UUID::to_string(){
+    uuid_unparse(this->_uuid, buf);
+    return buf;
+  }
+
+	void UUID::uuid_clear(uuid_t uu)
+	{
+		memset(uu, 0, 16);
+	}
+
+	void UUID::uuid_unparse_x(const uuid_t uu, char *out, const char *fmt)
+	{
+		struct uuid uuid;
+
+		uuid_unpack(uu, &uuid);
+		sprintf(out, fmt, uuid.time_low, uuid.time_mid, uuid.time_hi_and_version,
+						uuid.clock_seq >> 8, uuid.clock_seq & 0xFF,
+						uuid.node[0], uuid.node[1], uuid.node[2],
+						uuid.node[3], uuid.node[4], uuid.node[5]);
+	}
+	void UUID::uuid_unparse(const uuid_t uu, char *out)
+	{
+		uuid_unparse_x(uu, out, FMT_DEFAULT);
+	}
+};
