signal_chain.cpp 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. // SPDX-FileCopyrightText: Copyright 2023 yuzu Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #include <dlfcn.h>
  4. #include "common/assert.h"
  5. #include "common/dynamic_library.h"
  6. #include "common/scope_exit.h"
  7. #include "common/signal_chain.h"
  8. namespace Common {
  9. template <typename T>
  10. T* LookupLibcSymbol(const char* name) {
  11. #if defined(__BIONIC__)
  12. Common::DynamicLibrary provider("libc.so");
  13. if (!provider.IsOpen()) {
  14. UNREACHABLE_MSG("Failed to open libc!");
  15. }
  16. #else
  17. // For other operating environments, we assume the symbol is not overridden.
  18. const char* base = nullptr;
  19. Common::DynamicLibrary provider(base);
  20. #endif
  21. void* sym = provider.GetSymbolAddress(name);
  22. if (sym == nullptr) {
  23. sym = dlsym(RTLD_DEFAULT, name);
  24. }
  25. if (sym == nullptr) {
  26. UNREACHABLE_MSG("Unable to find symbol {}!", name);
  27. }
  28. return reinterpret_cast<T*>(sym);
  29. }
  30. int SigAction(int signum, const struct sigaction* act, struct sigaction* oldact) {
  31. static auto libc_sigaction = LookupLibcSymbol<decltype(sigaction)>("sigaction");
  32. return libc_sigaction(signum, act, oldact);
  33. }
  34. } // namespace Common