1848b8605Smrg/**************************************************************************
2848b8605Smrg *
3848b8605Smrg * Copyright 2013 VMware, Inc.
4848b8605Smrg * All Rights Reserved.
5848b8605Smrg *
6848b8605Smrg * Permission is hereby granted, free of charge, to any person obtaining a
7848b8605Smrg * copy of this software and associated documentation files (the
8848b8605Smrg * "Software"), to deal in the Software without restriction, including
9848b8605Smrg * without limitation the rights to use, copy, modify, merge, publish,
10848b8605Smrg * distribute, sub license, and/or sell copies of the Software, and to
11848b8605Smrg * permit persons to whom the Software is furnished to do so, subject to
12848b8605Smrg * the following conditions:
13848b8605Smrg *
14848b8605Smrg * The above copyright notice and this permission notice (including the
15848b8605Smrg * next paragraph) shall be included in all copies or substantial portions
16848b8605Smrg * of the Software.
17848b8605Smrg *
18848b8605Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19848b8605Smrg * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20848b8605Smrg * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21848b8605Smrg * IN NO EVENT SHALL THE AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR
22848b8605Smrg * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23848b8605Smrg * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24848b8605Smrg * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25848b8605Smrg *
26848b8605Smrg **************************************************************************/
27848b8605Smrg
28848b8605Smrg
29848b8605Smrg#include "pipe/p_config.h"
30848b8605Smrg#include "os/os_process.h"
31848b8605Smrg#include "util/u_memory.h"
32b8e80941Smrg#include "util/u_process.h"
33848b8605Smrg
34848b8605Smrg#if defined(PIPE_SUBSYSTEM_WINDOWS_USER)
35848b8605Smrg#  include <windows.h>
36848b8605Smrg#elif defined(PIPE_OS_HAIKU)
37848b8605Smrg#  include <kernel/OS.h>
38848b8605Smrg#  include <kernel/image.h>
39b8e80941Smrg#endif
40b8e80941Smrg
41b8e80941Smrg#if defined(PIPE_OS_LINUX)
42b8e80941Smrg#  include <fcntl.h>
43848b8605Smrg#endif
44848b8605Smrg
45848b8605Smrg
46848b8605Smrg/**
47848b8605Smrg * Return the name of the current process.
48848b8605Smrg * \param procname  returns the process name
49848b8605Smrg * \param size  size of the procname buffer
50848b8605Smrg * \return  TRUE or FALSE for success, failure
51848b8605Smrg */
52848b8605Smrgboolean
53848b8605Smrgos_get_process_name(char *procname, size_t size)
54848b8605Smrg{
55848b8605Smrg   const char *name;
56b8e80941Smrg
57b8e80941Smrg   /* First, check if the GALLIUM_PROCESS_NAME env var is set to
58b8e80941Smrg    * override the normal process name query.
59b8e80941Smrg    */
60b8e80941Smrg   name = os_get_option("GALLIUM_PROCESS_NAME");
61b8e80941Smrg
62b8e80941Smrg   if (!name) {
63b8e80941Smrg      /* do normal query */
64b8e80941Smrg
65848b8605Smrg#if defined(PIPE_SUBSYSTEM_WINDOWS_USER)
66b8e80941Smrg      char szProcessPath[MAX_PATH];
67b8e80941Smrg      char *lpProcessName;
68b8e80941Smrg      char *lpProcessExt;
69848b8605Smrg
70b8e80941Smrg      GetModuleFileNameA(NULL, szProcessPath, ARRAY_SIZE(szProcessPath));
71848b8605Smrg
72b8e80941Smrg      lpProcessName = strrchr(szProcessPath, '\\');
73b8e80941Smrg      lpProcessName = lpProcessName ? lpProcessName + 1 : szProcessPath;
74848b8605Smrg
75b8e80941Smrg      lpProcessExt = strrchr(lpProcessName, '.');
76b8e80941Smrg      if (lpProcessExt) {
77b8e80941Smrg         *lpProcessExt = '\0';
78b8e80941Smrg      }
79848b8605Smrg
80b8e80941Smrg      name = lpProcessName;
81848b8605Smrg
82848b8605Smrg#elif defined(PIPE_OS_HAIKU)
83b8e80941Smrg      image_info info;
84b8e80941Smrg      get_image_info(B_CURRENT_TEAM, &info);
85b8e80941Smrg      name = info.name;
86848b8605Smrg#else
87b8e80941Smrg      name = util_get_process_name();
88848b8605Smrg#endif
89b8e80941Smrg   }
90848b8605Smrg
91848b8605Smrg   assert(size > 0);
92848b8605Smrg   assert(procname);
93848b8605Smrg
94848b8605Smrg   if (name && procname && size > 0) {
95848b8605Smrg      strncpy(procname, name, size);
96848b8605Smrg      procname[size - 1] = '\0';
97848b8605Smrg      return TRUE;
98848b8605Smrg   }
99848b8605Smrg   else {
100848b8605Smrg      return FALSE;
101848b8605Smrg   }
102848b8605Smrg}
103b8e80941Smrg
104b8e80941Smrg
105b8e80941Smrg/**
106b8e80941Smrg * Return the command line for the calling process.  This is basically
107b8e80941Smrg * the argv[] array with the arguments separated by spaces.
108b8e80941Smrg * \param cmdline  returns the command line string
109b8e80941Smrg * \param size  size of the cmdline buffer
110b8e80941Smrg * \return  TRUE or FALSE for success, failure
111b8e80941Smrg */
112b8e80941Smrgboolean
113b8e80941Smrgos_get_command_line(char *cmdline, size_t size)
114b8e80941Smrg{
115b8e80941Smrg#if defined(PIPE_SUBSYSTEM_WINDOWS_USER)
116b8e80941Smrg   const char *args = GetCommandLine();
117b8e80941Smrg   if (args) {
118b8e80941Smrg      strncpy(cmdline, args, size);
119b8e80941Smrg      // make sure we terminate the string
120b8e80941Smrg      cmdline[size - 1] = 0;
121b8e80941Smrg      return TRUE;
122b8e80941Smrg   }
123b8e80941Smrg#elif defined(PIPE_OS_LINUX)
124b8e80941Smrg   int f = open("/proc/self/cmdline", O_RDONLY);
125b8e80941Smrg   if (f) {
126b8e80941Smrg      const int n = read(f, cmdline, size - 1);
127b8e80941Smrg      int i;
128b8e80941Smrg      assert(n < size);
129b8e80941Smrg      // The arguments are separated by '\0' chars.  Convert them to spaces.
130b8e80941Smrg      for (i = 0; i < n; i++) {
131b8e80941Smrg         if (cmdline[i] == 0) {
132b8e80941Smrg            cmdline[i] = ' ';
133b8e80941Smrg         }
134b8e80941Smrg      }
135b8e80941Smrg      // terminate the string
136b8e80941Smrg      cmdline[n] = 0;
137b8e80941Smrg      close(f);
138b8e80941Smrg      return TRUE;
139b8e80941Smrg   }
140b8e80941Smrg#endif
141b8e80941Smrg
142b8e80941Smrg   /* XXX to-do: implement this function for other operating systems */
143b8e80941Smrg
144b8e80941Smrg   cmdline[0] = 0;
145b8e80941Smrg   return FALSE;
146b8e80941Smrg}
147