1bf215546Sopenharmony_ci/**************************************************************************
2bf215546Sopenharmony_ci *
3bf215546Sopenharmony_ci * Copyright 2013 VMware, Inc.
4bf215546Sopenharmony_ci * All Rights Reserved.
5bf215546Sopenharmony_ci *
6bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
7bf215546Sopenharmony_ci * copy of this software and associated documentation files (the
8bf215546Sopenharmony_ci * "Software"), to deal in the Software without restriction, including
9bf215546Sopenharmony_ci * without limitation the rights to use, copy, modify, merge, publish,
10bf215546Sopenharmony_ci * distribute, sub license, and/or sell copies of the Software, and to
11bf215546Sopenharmony_ci * permit persons to whom the Software is furnished to do so, subject to
12bf215546Sopenharmony_ci * the following conditions:
13bf215546Sopenharmony_ci *
14bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the
15bf215546Sopenharmony_ci * next paragraph) shall be included in all copies or substantial portions
16bf215546Sopenharmony_ci * of the Software.
17bf215546Sopenharmony_ci *
18bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19bf215546Sopenharmony_ci * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20bf215546Sopenharmony_ci * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21bf215546Sopenharmony_ci * IN NO EVENT SHALL THE AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR
22bf215546Sopenharmony_ci * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23bf215546Sopenharmony_ci * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24bf215546Sopenharmony_ci * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25bf215546Sopenharmony_ci *
26bf215546Sopenharmony_ci **************************************************************************/
27bf215546Sopenharmony_ci
28bf215546Sopenharmony_ci
29bf215546Sopenharmony_ci#include "pipe/p_config.h"
30bf215546Sopenharmony_ci#include "os/os_process.h"
31bf215546Sopenharmony_ci#include "util/u_memory.h"
32bf215546Sopenharmony_ci#include "util/u_process.h"
33bf215546Sopenharmony_ci
34bf215546Sopenharmony_ci#if defined(PIPE_OS_WINDOWS)
35bf215546Sopenharmony_ci#  include <windows.h>
36bf215546Sopenharmony_ci#elif defined(PIPE_OS_HAIKU)
37bf215546Sopenharmony_ci#  include <kernel/OS.h>
38bf215546Sopenharmony_ci#  include <kernel/image.h>
39bf215546Sopenharmony_ci#endif
40bf215546Sopenharmony_ci
41bf215546Sopenharmony_ci#if defined(PIPE_OS_LINUX)
42bf215546Sopenharmony_ci#  include <fcntl.h>
43bf215546Sopenharmony_ci#endif
44bf215546Sopenharmony_ci
45bf215546Sopenharmony_ci
46bf215546Sopenharmony_ci/**
47bf215546Sopenharmony_ci * Return the name of the current process.
48bf215546Sopenharmony_ci * \param procname  returns the process name
49bf215546Sopenharmony_ci * \param size  size of the procname buffer
50bf215546Sopenharmony_ci * \return  TRUE or FALSE for success, failure
51bf215546Sopenharmony_ci */
52bf215546Sopenharmony_ciboolean
53bf215546Sopenharmony_cios_get_process_name(char *procname, size_t size)
54bf215546Sopenharmony_ci{
55bf215546Sopenharmony_ci   const char *name;
56bf215546Sopenharmony_ci
57bf215546Sopenharmony_ci   /* First, check if the GALLIUM_PROCESS_NAME env var is set to
58bf215546Sopenharmony_ci    * override the normal process name query.
59bf215546Sopenharmony_ci    */
60bf215546Sopenharmony_ci   name = os_get_option("GALLIUM_PROCESS_NAME");
61bf215546Sopenharmony_ci
62bf215546Sopenharmony_ci   if (!name) {
63bf215546Sopenharmony_ci      /* do normal query */
64bf215546Sopenharmony_ci
65bf215546Sopenharmony_ci#if defined(PIPE_OS_WINDOWS)
66bf215546Sopenharmony_ci      char szProcessPath[MAX_PATH];
67bf215546Sopenharmony_ci      char *lpProcessName;
68bf215546Sopenharmony_ci      char *lpProcessExt;
69bf215546Sopenharmony_ci
70bf215546Sopenharmony_ci      GetModuleFileNameA(NULL, szProcessPath, ARRAY_SIZE(szProcessPath));
71bf215546Sopenharmony_ci
72bf215546Sopenharmony_ci      lpProcessName = strrchr(szProcessPath, '\\');
73bf215546Sopenharmony_ci      lpProcessName = lpProcessName ? lpProcessName + 1 : szProcessPath;
74bf215546Sopenharmony_ci
75bf215546Sopenharmony_ci      lpProcessExt = strrchr(lpProcessName, '.');
76bf215546Sopenharmony_ci      if (lpProcessExt) {
77bf215546Sopenharmony_ci         *lpProcessExt = '\0';
78bf215546Sopenharmony_ci      }
79bf215546Sopenharmony_ci
80bf215546Sopenharmony_ci      name = lpProcessName;
81bf215546Sopenharmony_ci
82bf215546Sopenharmony_ci#elif defined(PIPE_OS_HAIKU)
83bf215546Sopenharmony_ci      image_info info;
84bf215546Sopenharmony_ci      get_image_info(B_CURRENT_TEAM, &info);
85bf215546Sopenharmony_ci      name = info.name;
86bf215546Sopenharmony_ci#else
87bf215546Sopenharmony_ci      name = util_get_process_name();
88bf215546Sopenharmony_ci#endif
89bf215546Sopenharmony_ci   }
90bf215546Sopenharmony_ci
91bf215546Sopenharmony_ci   assert(size > 0);
92bf215546Sopenharmony_ci   assert(procname);
93bf215546Sopenharmony_ci
94bf215546Sopenharmony_ci   if (name && procname && size > 0) {
95bf215546Sopenharmony_ci      strncpy(procname, name, size);
96bf215546Sopenharmony_ci      procname[size - 1] = '\0';
97bf215546Sopenharmony_ci      return TRUE;
98bf215546Sopenharmony_ci   }
99bf215546Sopenharmony_ci   else {
100bf215546Sopenharmony_ci      return FALSE;
101bf215546Sopenharmony_ci   }
102bf215546Sopenharmony_ci}
103bf215546Sopenharmony_ci
104bf215546Sopenharmony_ci
105bf215546Sopenharmony_ci/**
106bf215546Sopenharmony_ci * Return the command line for the calling process.  This is basically
107bf215546Sopenharmony_ci * the argv[] array with the arguments separated by spaces.
108bf215546Sopenharmony_ci * \param cmdline  returns the command line string
109bf215546Sopenharmony_ci * \param size  size of the cmdline buffer
110bf215546Sopenharmony_ci * \return  TRUE or FALSE for success, failure
111bf215546Sopenharmony_ci */
112bf215546Sopenharmony_ciboolean
113bf215546Sopenharmony_cios_get_command_line(char *cmdline, size_t size)
114bf215546Sopenharmony_ci{
115bf215546Sopenharmony_ci#if defined(PIPE_OS_WINDOWS)
116bf215546Sopenharmony_ci   const char *args = GetCommandLineA();
117bf215546Sopenharmony_ci   if (args) {
118bf215546Sopenharmony_ci      strncpy(cmdline, args, size);
119bf215546Sopenharmony_ci      // make sure we terminate the string
120bf215546Sopenharmony_ci      cmdline[size - 1] = 0;
121bf215546Sopenharmony_ci      return TRUE;
122bf215546Sopenharmony_ci   }
123bf215546Sopenharmony_ci#elif defined(PIPE_OS_LINUX)
124bf215546Sopenharmony_ci   int f = open("/proc/self/cmdline", O_RDONLY);
125bf215546Sopenharmony_ci   if (f != -1) {
126bf215546Sopenharmony_ci      const int n = read(f, cmdline, size - 1);
127bf215546Sopenharmony_ci      int i;
128bf215546Sopenharmony_ci      assert(n < size);
129bf215546Sopenharmony_ci      // The arguments are separated by '\0' chars.  Convert them to spaces.
130bf215546Sopenharmony_ci      for (i = 0; i < n; i++) {
131bf215546Sopenharmony_ci         if (cmdline[i] == 0) {
132bf215546Sopenharmony_ci            cmdline[i] = ' ';
133bf215546Sopenharmony_ci         }
134bf215546Sopenharmony_ci      }
135bf215546Sopenharmony_ci      // terminate the string
136bf215546Sopenharmony_ci      cmdline[n] = 0;
137bf215546Sopenharmony_ci      close(f);
138bf215546Sopenharmony_ci      return TRUE;
139bf215546Sopenharmony_ci   }
140bf215546Sopenharmony_ci#endif
141bf215546Sopenharmony_ci
142bf215546Sopenharmony_ci   /* XXX to-do: implement this function for other operating systems */
143bf215546Sopenharmony_ci
144bf215546Sopenharmony_ci   cmdline[0] = 0;
145bf215546Sopenharmony_ci   return FALSE;
146bf215546Sopenharmony_ci}
147