util: Merge the code from os_process.c into u_process.c

Signed-off-by: Yonggang Luo <luoyonggang@gmail.com>
Reviewed-by: Marek Olšák <marek.olsak@amd.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/19645>
This commit is contained in:
Yonggang Luo
2022-11-10 19:05:07 +08:00
committed by Marge Bot
parent fdad0477f0
commit d600a0ed34
5 changed files with 100 additions and 129 deletions
-1
View File
@@ -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',
-124
View File
@@ -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 <windows.h>
#elif defined(PIPE_OS_HAIKU)
# include <kernel/OS.h>
# include <kernel/image.h>
#endif
#if defined(PIPE_OS_LINUX)
# include <fcntl.h>
#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;
}
+10 -4
View File
@@ -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 */
+69
View File
@@ -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 <sys/sysctl.h>
#endif
#if DETECT_OS_LINUX
#include <fcntl.h>
#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;
}
+21
View File
@@ -28,6 +28,7 @@
#ifndef PROCESS_H
#define PROCESS_H
#include <stdbool.h>
#include <stddef.h>
#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