]> git.apps.os.sepia.ceph.com Git - ceph-ci.git/commitdiff
common: Add get_process_name implementation for Windows
authorAlin Gabriel Serdean <aserdean@cloudbasesolutions.com>
Thu, 12 Nov 2020 21:34:46 +0000 (21:34 +0000)
committerAlin Gabriel Serdean <aserdean@cloudbasesolutions.com>
Fri, 18 Dec 2020 02:58:50 +0000 (04:58 +0200)
This patch adds get_process_name for Windows based on
GetModuleFileName.

Signed-off-by: Alin Gabriel Serdean <aserdean@cloudbasesolutions.com>
src/common/code_environment.cc

index 414e2b62ada219481fd5c4faa5a55c387b6b43ef..a821dfcdeace1adf6379af7495a2265641ea878c 100644 (file)
@@ -79,6 +79,31 @@ int get_process_name(char *buf, int len)
   return 0;
 }
 
+#elif defined(_WIN32)
+
+int get_process_name(char *buf, int len)
+{
+  if (len <= 0) {
+    return -EINVAL;
+  }
+
+  int length = GetModuleFileNameA(nullptr, buf, len);
+  if (length <= 0)
+    return -ENOSYS;
+
+  char* start = strrchr(buf, '\\');
+  if (!start)
+    return -ENOSYS;
+  start++;
+  char* end = strstr(start, ".exe");
+  if (!end)
+    return -ENOSYS;
+
+  memmove(buf, start, end - start);
+  buf[end - start] = '\0';
+  return 0;
+}
+
 #else
 
 int get_process_name(char *buf, int len)