util: A generic function to convert error to string for windows.
authorGurucharan Shetty <gshetty@nicira.com>
Thu, 16 Jan 2014 21:49:38 +0000 (13:49 -0800)
committerGurucharan Shetty <gshetty@nicira.com>
Tue, 28 Jan 2014 21:08:13 +0000 (13:08 -0800)
More users will be added in an upcoming commit.

Signed-off-by: Gurucharan Shetty <gshetty@nicira.com>
Acked-by: Ben Pfaff <blp@nicira.com>
lib/entropy.c
lib/util.c
lib/util.h

index fd73566..53f7e72 100644 (file)
@@ -56,22 +56,12 @@ get_entropy(void *buffer, size_t n)
 #else
     int error = 0;
     HCRYPTPROV   crypt_prov = 0;
-    LPVOID msg_buf;
 
     CryptAcquireContext(&crypt_prov, NULL, NULL,
                         PROV_RSA_FULL, CRYPT_VERIFYCONTEXT);
     if (!CryptGenRandom(crypt_prov, n, buffer)) {
+        char *msg_buf = ovs_lasterror_to_string();
         error = EINVAL;
-        FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER
-                      | FORMAT_MESSAGE_FROM_SYSTEM
-                      | FORMAT_MESSAGE_IGNORE_INSERTS,
-                      NULL,
-                      GetLastError(),
-                      0,
-                      (LPTSTR)&msg_buf,
-                      0,
-                      NULL
-            );
         VLOG_ERR("CryptGenRandom: read error (%s)", msg_buf);
         LocalFree(msg_buf);
     }
index 0ebf085..87cc112 100644 (file)
@@ -1649,3 +1649,19 @@ exit:
     return ok;
 }
 
+#ifdef _WIN32
+\f
+/* Calls FormatMessage() with GetLastError() as an argument. Returns
+ * pointer to a buffer that receives the null-terminated string that specifies
+ * the formatted message and that has to be freed by the caller with
+ * LocalFree(). */
+char *
+ovs_lasterror_to_string(void)
+{
+    char *buffer;
+    FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM
+                  | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, GetLastError(), 0,
+                  (char *)&buffer, 0, NULL);
+    return buffer;
+}
+#endif
index 8886a54..2e3d1da 100644 (file)
@@ -490,6 +490,11 @@ void bitwise_put(uint64_t value,
 uint64_t bitwise_get(const void *src, unsigned int src_len,
                      unsigned int src_ofs, unsigned int n_bits);
 
+#ifdef _WIN32
+\f
+char *ovs_lasterror_to_string(void);
+#endif
+
 #ifdef  __cplusplus
 }
 #endif