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