diff --git a/src/gallium/auxiliary/meson.build b/src/gallium/auxiliary/meson.build index eecd326530a..929baacab9d 100644 --- a/src/gallium/auxiliary/meson.build +++ b/src/gallium/auxiliary/meson.build @@ -113,7 +113,6 @@ files_libgallium = files( 'hud/hud_fps.c', 'hud/hud_private.h', 'os/os_mman.h', - 'os/os_process.c', 'os/os_process.h', 'pipebuffer/pb_buffer_fenced.c', 'pipebuffer/pb_buffer_fenced.h', diff --git a/src/gallium/auxiliary/os/os_process.c b/src/gallium/auxiliary/os/os_process.c deleted file mode 100644 index d7c62284187..00000000000 --- a/src/gallium/auxiliary/os/os_process.c +++ /dev/null @@ -1,124 +0,0 @@ -/************************************************************************** - * - * Copyright 2013 VMware, Inc. - * All Rights Reserved. - * - * Permission is hereby granted, free of charge, to any person obtaining a - * copy of this software and associated documentation files (the - * "Software"), to deal in the Software without restriction, including - * without limitation the rights to use, copy, modify, merge, publish, - * distribute, sub license, and/or sell copies of the Software, and to - * permit persons to whom the Software is furnished to do so, subject to - * the following conditions: - * - * The above copyright notice and this permission notice (including the - * next paragraph) shall be included in all copies or substantial portions - * of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS - * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF - * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. - * IN NO EVENT SHALL THE AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR - * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, - * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE - * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - * - **************************************************************************/ - - -#include "pipe/p_config.h" -#include "pipe/p_compiler.h" -#include "os/os_process.h" -#include "util/detect_os.h" -#include "util/u_memory.h" -#include "util/u_process.h" - -#if defined(PIPE_OS_WINDOWS) -# include -#elif defined(PIPE_OS_HAIKU) -# include -# include -#endif - -#if defined(PIPE_OS_LINUX) -# include -#endif - - -/** - * Return the name of the current process. - * \param procname returns the process name - * \param size size of the procname buffer - * \return true or false for success, failure - */ -bool -os_get_process_name(char *procname, size_t size) -{ - const char *name; - - /* First, check if the GALLIUM_PROCESS_NAME env var is set to - * override the normal process name query. - */ - name = os_get_option("GALLIUM_PROCESS_NAME"); - - if (!name) { - /* do normal query */ - name = util_get_process_name(); - } - - assert(size > 0); - assert(procname); - - if (name && procname && size > 0) { - strncpy(procname, name, size); - procname[size - 1] = '\0'; - return true; - } - else { - 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 - */ -bool -os_get_command_line(char *cmdline, size_t size) -{ -#if DETECT_OS_WINDOWS - const char *args = GetCommandLineA(); - if (args) { - strncpy(cmdline, args, size); - // make sure we terminate the string - cmdline[size - 1] = 0; - return true; - } -#elif DETECT_OS_LINUX - int f = open("/proc/self/cmdline", O_RDONLY); - if (f != -1) { - 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 e9dd49a5602..d22283aec7d 100644 --- a/src/gallium/auxiliary/os/os_process.h +++ b/src/gallium/auxiliary/os/os_process.h @@ -34,12 +34,18 @@ #include "util/u_process.h" -extern bool -os_get_process_name(char *str, size_t size); +static inline bool +os_get_process_name(char *str, size_t size) +{ + return util_get_process_name_may_override("GALLIUM_PROCESS_NAME", str, size); +} -extern bool -os_get_command_line(char *cmdline, size_t size); +static inline bool +os_get_command_line(char *cmdline, size_t size) +{ + return util_get_command_line(cmdline, size); +} #endif /* OS_PROCESS_H */ diff --git a/src/util/u_process.c b/src/util/u_process.c index 8ad078e2649..d548fcc7bbb 100644 --- a/src/util/u_process.c +++ b/src/util/u_process.c @@ -25,6 +25,7 @@ * of the Software. */ +#include "util/os_misc.h" #include "u_process.h" #include "detect_os.h" #include "macros.h" @@ -47,6 +48,10 @@ #include #endif +#if DETECT_OS_LINUX +#include +#endif + #include "util/u_call_once.h" #undef GET_PROGRAM_NAME_NOT_AVAILABLE @@ -253,3 +258,67 @@ success: #endif return 0; } + +bool +util_get_process_name_may_override(const char *env_name, char *procname, size_t size) +{ + const char *name; + + /* First, check if the env var with env_name is set to + * override the normal process name query. + */ + name = os_get_option(env_name); + + if (!name) { + /* do normal query */ + name = util_get_process_name(); + } + + assert(size > 0); + assert(procname); + + if (name && procname && size > 0) { + strncpy(procname, name, size); + procname[size - 1] = '\0'; + return true; + } + else { + return false; + } +} + +bool +util_get_command_line(char *cmdline, size_t size) +{ +#if DETECT_OS_WINDOWS + const char *args = GetCommandLineA(); + if (args) { + strncpy(cmdline, args, size); + // make sure we terminate the string + cmdline[size - 1] = 0; + return true; + } +#elif DETECT_OS_LINUX + int f = open("/proc/self/cmdline", O_RDONLY); + if (f != -1) { + 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/util/u_process.h b/src/util/u_process.h index 51b06056614..1a2bf1f855c 100644 --- a/src/util/u_process.h +++ b/src/util/u_process.h @@ -28,6 +28,7 @@ #ifndef PROCESS_H #define PROCESS_H +#include #include #ifdef __cplusplus @@ -40,6 +41,26 @@ util_get_process_name(void); size_t util_get_process_exec_path(char* process_path, size_t len); +/** + * Return the name of the current process. + * \param env_name the environment variable name used to override + * \param procname returns the process name + * \param size size of the procname buffer + * \return true or false for success, failure + */ +bool +util_get_process_name_may_override(const char *env_name, char *procname, size_t size); + +/** + * 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 + */ +bool +util_get_command_line(char *cmdline, size_t size); + #ifdef __cplusplus } /* extern C */ #endif