From f8f1413070ae079443ab31a75679cfd10cb756ed Mon Sep 17 00:00:00 2001 From: Pierre-Eric Pelloux-Prayer Date: Mon, 16 Mar 2020 10:49:17 +0100 Subject: [PATCH] util/u_process: add util_get_process_exec_path MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Reviewed-by: Marek Olšák Part-of: --- src/util/meson.build | 22 +++++++++++ src/util/process_test.c | 83 +++++++++++++++++++++++++++++++++++++++++ src/util/u_process.c | 31 +++++++++++++++ src/util/u_process.h | 5 +++ 4 files changed, 141 insertions(+) create mode 100644 src/util/process_test.c diff --git a/src/util/meson.build b/src/util/meson.build index ea61c055817..77e3973c1a0 100644 --- a/src/util/meson.build +++ b/src/util/meson.build @@ -278,6 +278,28 @@ if with_tests suite : ['util'], ) + process_test_exe = executable( + 'process_test', + files('process_test.c'), + include_directories : inc_common, + dependencies : idep_mesautil, + c_args : [c_msvc_compat_args], + ) + if not (host_machine.system() == 'windows') + process_test_exe_full_path = process_test_exe.full_path() + else + process_test_exe_full_path = run_command( + 'winepath', '-w', process_test_exe.full_path() + ).stdout().strip() + endif + + test( + 'process', + process_test_exe, + suite : ['util'], + env: ['BUILD_FULL_PATH='+process_test_exe_full_path] + ) + subdir('tests/fast_idiv_by_const') subdir('tests/fast_urem_by_const') subdir('tests/hash_table') diff --git a/src/util/process_test.c b/src/util/process_test.c new file mode 100644 index 00000000000..708c2766eb9 --- /dev/null +++ b/src/util/process_test.c @@ -0,0 +1,83 @@ +/* + * Copyright © 2020 Advanced Micro Devices, Inc. + * + * 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, sublicense, + * 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 NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS 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. + */ + +/* A collection of unit tests for u_process.c */ + +#include "util/detect_os.h" +#include "util/u_process.h" +#include +#include +#include +#include +#include + +bool error = false; + +static void +expect_equal_str(const char *expected, const char *actual, const char *test) +{ + if (strcmp(expected, actual)) { + fprintf (stderr, "Error: Test '%s' failed:\n\t" + "Expected=\"%s\", Actual=\"%s\"\n", + test, expected, actual); + error = true; + } +} + +static void +test_util_get_process_name (void) +{ + #if !DETECT_OS_WINDOWS + const char* name = util_get_process_name(); + expect_equal_str("process_test", name, "util_get_process_name"); + #endif +} + +/* This test gets the real path from Meson (BUILD_FULL_PATH env var), + * and compares it to the output of util_get_process_exec_path. + */ +static void +test_util_get_process_exec_path (void) +{ + char path[PATH_MAX]; + if (util_get_process_exec_path(path, PATH_MAX) == 0) { + error = true; + return; + } + char* build_path = getenv("BUILD_FULL_PATH"); + if (!build_path) { + fprintf(stderr, "BUILD_FULL_PATH environment variable should be set\n"); + error = true; + return; + } + expect_equal_str(build_path, path, "util_get_process_name"); +} + +int +main (void) +{ + test_util_get_process_name(); + test_util_get_process_exec_path(); + + return error ? 1 : 0; +} diff --git a/src/util/u_process.c b/src/util/u_process.c index b9328d58da4..686e4624429 100644 --- a/src/util/u_process.c +++ b/src/util/u_process.c @@ -26,12 +26,19 @@ */ #include "u_process.h" +#include "detect_os.h" +#include "macros.h" #include #include #include +#include #undef GET_PROGRAM_NAME +#if DETECT_OS_WINDOWS +#include +#endif + #if defined(__linux__) && defined(HAVE_PROGRAM_INVOCATION_NAME) static char *path = NULL; @@ -152,3 +159,27 @@ util_get_process_name(void) { return GET_PROGRAM_NAME(); } + +size_t +util_get_process_exec_path(char* process_path, size_t len) +{ +#if DETECT_OS_WINDOWS + return GetModuleFileNameA(NULL, process_path, len); +#elif DETECT_OS_UNIX + ssize_t r; + + if ((r = readlink("/proc/self/exe", process_path, len)) > 0) + goto success; + if ((r = readlink("/proc/curproc/exe", process_path, len)) > 0) + goto success; + if ((r = readlink("/proc/curproc/file", process_path, len)) > 0) + goto success; + + return 0; +success: + process_path[r] = '\0'; + return r; + +#endif + return 0; +} diff --git a/src/util/u_process.h b/src/util/u_process.h index 77f7cb1cbef..88b80e5ec12 100644 --- a/src/util/u_process.h +++ b/src/util/u_process.h @@ -28,7 +28,12 @@ #ifndef PROCESS_H #define PROCESS_H +#include + const char * util_get_process_name(void); +size_t +util_get_process_exec_path(char* process_path, size_t len); + #endif -- 2.30.2