From: Joel Rosdahl Date: Sun, 30 Jun 2024 16:54:13 +0000 (+0200) Subject: refactor: Introduce and use CHECK_LIB_CALL macro X-Git-Tag: v4.11~96 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=8916e2f529d19debd1a5199c28219d24d81b41fc;p=thirdparty%2Fccache.git refactor: Introduce and use CHECK_LIB_CALL macro --- diff --git a/src/ccache/core/exceptions.hpp b/src/ccache/core/exceptions.hpp index 7017e930..b67bb153 100644 --- a/src/ccache/core/exceptions.hpp +++ b/src/ccache/core/exceptions.hpp @@ -1,4 +1,4 @@ -// Copyright (C) 2019-2022 Joel Rosdahl and other contributors +// Copyright (C) 2019-2024 Joel Rosdahl and other contributors // // See doc/AUTHORS.adoc for a complete list of contributors. // @@ -20,6 +20,7 @@ #include +#include #include #include #include @@ -48,4 +49,14 @@ class Fatal : public ErrorBase using ErrorBase::ErrorBase; }; +// Call a libc-style function (returns 0 on success and sets errno) and throw +// Fatal on error. +#define CHECK_LIB_CALL(function, ...) \ + { \ + int _result = function(__VA_ARGS__); \ + if (_result != 0) { \ + throw core::Fatal(FMT(#function " failed: {}", strerror(_result))); \ + } \ + } + } // namespace core diff --git a/src/ccache/execute.cpp b/src/ccache/execute.cpp index aadf4e23..6359c176 100644 --- a/src/ccache/execute.cpp +++ b/src/ccache/execute.cpp @@ -300,43 +300,31 @@ execute(Context& ctx, { LOG("Executing {}", util::format_argv_for_logging(argv)); - int result; - posix_spawn_file_actions_t file_actions; - if ((result = posix_spawn_file_actions_init(&file_actions))) { - throw core::Fatal( - FMT("posix_spawn_file_actions_init failed: {}", strerror(result))); - } + posix_spawn_file_actions_t fa; - if ((result = posix_spawn_file_actions_adddup2( - &file_actions, *fd_out, STDOUT_FILENO)) - || (result = posix_spawn_file_actions_addclose(&file_actions, *fd_out)) - || (result = posix_spawn_file_actions_adddup2( - &file_actions, *fd_err, STDERR_FILENO)) - || (result = posix_spawn_file_actions_addclose(&file_actions, *fd_err))) { - throw core::Fatal(FMT("posix_spawn_file_actions_addclose/dup2 failed: {}", - strerror(result))); - } + CHECK_LIB_CALL(posix_spawn_file_actions_init, &fa); + CHECK_LIB_CALL(posix_spawn_file_actions_adddup2, &fa, *fd_out, STDOUT_FILENO); + CHECK_LIB_CALL(posix_spawn_file_actions_addclose, &fa, *fd_out); + CHECK_LIB_CALL(posix_spawn_file_actions_adddup2, &fa, *fd_err, STDERR_FILENO); + CHECK_LIB_CALL(posix_spawn_file_actions_addclose, &fa, *fd_err); + int result; { SignalHandlerBlocker signal_handler_blocker; pid_t pid; extern char** environ; - result = posix_spawn(&pid, - argv[0], - &file_actions, - nullptr, - const_cast(argv), - environ); - if (!result) { + result = posix_spawn( + &pid, argv[0], &fa, nullptr, const_cast(argv), environ); + if (result == 0) { ctx.compiler_pid = pid; } } - posix_spawn_file_actions_destroy(&file_actions); + posix_spawn_file_actions_destroy(&fa); fd_out.close(); fd_err.close(); - if (result) { + if (result != 0) { return -1; } diff --git a/src/ccache/hashutil.cpp b/src/ccache/hashutil.cpp index ead335be..b9e85a6a 100644 --- a/src/ccache/hashutil.cpp +++ b/src/ccache/hashutil.cpp @@ -376,9 +376,10 @@ hash_command_output(Hash& hash, } } - auto argv = args.to_argv(); + auto arg_vector = args.to_argv(); + auto argv = const_cast(arg_vector.data()); LOG("Executing compiler check command {}", - util::format_argv_for_logging(argv.data())); + util::format_argv_for_logging(argv)); #ifdef _WIN32 PROCESS_INFORMATION pi; @@ -410,7 +411,7 @@ hash_command_output(Hash& hash, if (using_cmd_exe) { win32args = adjusted_command; // quoted } else { - win32args = util::format_argv_as_win32_command_string(argv.data(), sh); + win32args = util::format_argv_as_win32_command_string(argv, sh); } BOOL ret = CreateProcess(path.c_str(), const_cast(win32args.c_str()), @@ -449,33 +450,21 @@ hash_command_output(Hash& hash, throw core::Fatal(FMT("pipe failed: {}", strerror(errno))); } - int result; - posix_spawn_file_actions_t file_actions; - if ((result = posix_spawn_file_actions_init(&file_actions))) { - throw core::Fatal( - FMT("posix_spawn_file_actions_init failed: {}", strerror(result))); - } + posix_spawn_file_actions_t fa; - if ((result = posix_spawn_file_actions_addclose(&file_actions, pipefd[0])) - || (result = posix_spawn_file_actions_addclose(&file_actions, 0)) - || (result = - posix_spawn_file_actions_adddup2(&file_actions, pipefd[1], 1)) - || (result = - posix_spawn_file_actions_adddup2(&file_actions, pipefd[1], 2))) { - throw core::Fatal(FMT("posix_spawn_file_actions_addclose/dup2 failed: {}", - strerror(result))); - } + CHECK_LIB_CALL(posix_spawn_file_actions_init, &fa); + CHECK_LIB_CALL(posix_spawn_file_actions_init, &fa); + CHECK_LIB_CALL(posix_spawn_file_actions_addclose, &fa, pipefd[0]); + CHECK_LIB_CALL(posix_spawn_file_actions_addclose, &fa, 0); + CHECK_LIB_CALL(posix_spawn_file_actions_adddup2, &fa, pipefd[1], 1); + CHECK_LIB_CALL(posix_spawn_file_actions_adddup2, &fa, pipefd[1], 2); pid_t pid; extern char** environ; - result = posix_spawnp(&pid, - argv[0], - &file_actions, - nullptr, - const_cast(argv.data()), - environ); - - posix_spawn_file_actions_destroy(&file_actions); + int result = posix_spawnp( + &pid, argv[0], &fa, nullptr, const_cast(argv), environ); + + posix_spawn_file_actions_destroy(&fa); close(pipefd[1]); const auto hash_result = hash.hash_fd(pipefd[0]); @@ -484,7 +473,7 @@ hash_command_output(Hash& hash, } close(pipefd[0]); - if (result) { + if (result != 0) { LOG("posix_spawnp failed: {}", strerror(errno)); return false; }