Path: blob/21.2-virgl/src/gallium/auxiliary/os/os_process.c
4561 views
/**************************************************************************1*2* Copyright 2013 VMware, Inc.3* All Rights Reserved.4*5* Permission is hereby granted, free of charge, to any person obtaining a6* copy of this software and associated documentation files (the7* "Software"), to deal in the Software without restriction, including8* without limitation the rights to use, copy, modify, merge, publish,9* distribute, sub license, and/or sell copies of the Software, and to10* permit persons to whom the Software is furnished to do so, subject to11* the following conditions:12*13* The above copyright notice and this permission notice (including the14* next paragraph) shall be included in all copies or substantial portions15* of the Software.16*17* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS18* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF19* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.20* IN NO EVENT SHALL THE AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR21* ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,22* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE23* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.24*25**************************************************************************/262728#include "pipe/p_config.h"29#include "os/os_process.h"30#include "util/u_memory.h"31#include "util/u_process.h"3233#if defined(PIPE_OS_WINDOWS)34# include <windows.h>35#elif defined(PIPE_OS_HAIKU)36# include <kernel/OS.h>37# include <kernel/image.h>38#endif3940#if defined(PIPE_OS_LINUX)41# include <fcntl.h>42#endif434445/**46* Return the name of the current process.47* \param procname returns the process name48* \param size size of the procname buffer49* \return TRUE or FALSE for success, failure50*/51boolean52os_get_process_name(char *procname, size_t size)53{54const char *name;5556/* First, check if the GALLIUM_PROCESS_NAME env var is set to57* override the normal process name query.58*/59name = os_get_option("GALLIUM_PROCESS_NAME");6061if (!name) {62/* do normal query */6364#if defined(PIPE_OS_WINDOWS)65char szProcessPath[MAX_PATH];66char *lpProcessName;67char *lpProcessExt;6869GetModuleFileNameA(NULL, szProcessPath, ARRAY_SIZE(szProcessPath));7071lpProcessName = strrchr(szProcessPath, '\\');72lpProcessName = lpProcessName ? lpProcessName + 1 : szProcessPath;7374lpProcessExt = strrchr(lpProcessName, '.');75if (lpProcessExt) {76*lpProcessExt = '\0';77}7879name = lpProcessName;8081#elif defined(PIPE_OS_HAIKU)82image_info info;83get_image_info(B_CURRENT_TEAM, &info);84name = info.name;85#else86name = util_get_process_name();87#endif88}8990assert(size > 0);91assert(procname);9293if (name && procname && size > 0) {94strncpy(procname, name, size);95procname[size - 1] = '\0';96return TRUE;97}98else {99return FALSE;100}101}102103104/**105* Return the command line for the calling process. This is basically106* the argv[] array with the arguments separated by spaces.107* \param cmdline returns the command line string108* \param size size of the cmdline buffer109* \return TRUE or FALSE for success, failure110*/111boolean112os_get_command_line(char *cmdline, size_t size)113{114#if defined(PIPE_OS_WINDOWS)115const char *args = GetCommandLineA();116if (args) {117strncpy(cmdline, args, size);118// make sure we terminate the string119cmdline[size - 1] = 0;120return TRUE;121}122#elif defined(PIPE_OS_LINUX)123int f = open("/proc/self/cmdline", O_RDONLY);124if (f != -1) {125const int n = read(f, cmdline, size - 1);126int i;127assert(n < size);128// The arguments are separated by '\0' chars. Convert them to spaces.129for (i = 0; i < n; i++) {130if (cmdline[i] == 0) {131cmdline[i] = ' ';132}133}134// terminate the string135cmdline[n] = 0;136close(f);137return TRUE;138}139#endif140141/* XXX to-do: implement this function for other operating systems */142143cmdline[0] = 0;144return FALSE;145}146147148