Mesa (master): auxiliary/os: add new os_get_command_line() function

Brian Paul brianp at kemper.freedesktop.org
Mon Aug 1 18:19:54 UTC 2016


Module: Mesa
Branch: master
Commit: 13fa0513569d0145487e12f3bc3aa10bc51088f7
URL:    http://cgit.freedesktop.org/mesa/mesa/commit/?id=13fa0513569d0145487e12f3bc3aa10bc51088f7

Author: Brian Paul <brianp at vmware.com>
Date:   Wed Jun 29 16:30:54 2016 -0600

auxiliary/os: add new os_get_command_line() function

This can be used by the driver to get the command line which started
the process.  Will be used by the VMware driver for extra logging.

For now, this is only implemented for Linux via /proc/self/cmdline
and Windows via GetCommandLine().

Reviewed-by: Charmaine Lee <charmainel at vmware.com>
Reviewed-by: Marek Olšák <marek.olsak at amd.com>

---

 src/gallium/auxiliary/os/os_process.c | 48 +++++++++++++++++++++++++++++++++++
 src/gallium/auxiliary/os/os_process.h |  4 +++
 2 files changed, 52 insertions(+)

diff --git a/src/gallium/auxiliary/os/os_process.c b/src/gallium/auxiliary/os/os_process.c
index 332e195..6622b9b 100644
--- a/src/gallium/auxiliary/os/os_process.c
+++ b/src/gallium/auxiliary/os/os_process.c
@@ -43,6 +43,10 @@
 #warning unexpected platform in os_process.c
 #endif
 
+#if defined(PIPE_OS_LINUX)
+#  include <fcntl.h>
+#endif
+
 
 /**
  * Return the name of the current process.
@@ -108,3 +112,47 @@ os_get_process_name(char *procname, size_t size)
       return FALSE;
    }
 }
+
+
+/**
+ * Return the command line for the calling process.  This is basically
+ * the argv[] array with the arguments separated by spaces.
+ * \param cmdline  returns the command line string
+ * \param size  size of the cmdline buffer
+ * \return  TRUE or FALSE for success, failure
+ */
+boolean
+os_get_command_line(char *cmdline, size_t size)
+{
+#if defined(PIPE_SUBSYSTEM_WINDOWS_USER)
+   const char *args = GetCommandLine();
+   if (args) {
+      strncpy(cmdline, args, size);
+      // make sure we terminate the string
+      cmdline[size - 1] = 0;
+      return TRUE;
+   }
+#elif defined(PIPE_OS_LINUX)
+   int f = open("/proc/self/cmdline", O_RDONLY);
+   if (f) {
+      const int n = read(f, cmdline, size - 1);
+      int i;
+      assert(n < size);
+      // The arguments are separated by '\0' chars.  Convert them to spaces.
+      for (i = 0; i < n; i++) {
+         if (cmdline[i] == 0) {
+            cmdline[i] = ' ';
+         }
+      }
+      // terminate the string
+      cmdline[n] = 0;
+      close(f);
+      return TRUE;
+   }
+#endif
+
+   /* XXX to-do: implement this function for other operating systems */
+
+   cmdline[0] = 0;
+   return FALSE;
+}
diff --git a/src/gallium/auxiliary/os/os_process.h b/src/gallium/auxiliary/os/os_process.h
index 0d50ddc..9c5b31d 100644
--- a/src/gallium/auxiliary/os/os_process.h
+++ b/src/gallium/auxiliary/os/os_process.h
@@ -37,4 +37,8 @@ extern boolean
 os_get_process_name(char *str, size_t size);
 
 
+extern boolean
+os_get_command_line(char *cmdline, size_t size);
+
+
 #endif /* OS_PROCESS_H */




More information about the mesa-commit mailing list