this repo has no description
1// Copyright (c) Facebook, Inc. and its affiliates. (http://www.facebook.com)
2#include "os.h"
3
4#include <dlfcn.h>
5#include <pthread.h>
6#include <sys/syscall.h>
7#include <unistd.h>
8
9#include <csignal>
10#include <cstdlib>
11
12#include "utils.h"
13
14namespace py {
15
16const word OS::kNumSignals = _NSIG;
17bool volatile OS::pending_signals_[kNumSignals];
18
19const int OS::kRtldGlobal = RTLD_GLOBAL;
20const int OS::kRtldLocal = RTLD_LOCAL;
21const int OS::kRtldNow = RTLD_NOW;
22
23const char* OS::name() { return "linux"; }
24
25// clang-format off
26#define V(SIGNAL) { #SIGNAL, SIGNAL }
27const OS::Signal OS::kPlatformSignals[] = {
28 V(SIGCLD),
29 V(SIGIO),
30 V(SIGIOT),
31 V(SIGPOLL),
32 V(SIGPROF),
33 V(SIGPWR),
34 V(SIGRTMAX),
35 V(SIGRTMIN),
36 V(SIGSYS),
37 V(SIGVTALRM),
38 V(SIGWINCH),
39 { nullptr, 0 },
40};
41#undef V
42// clang-format on
43
44void OS::createThread(ThreadFunction func, void* arg) {
45 pthread_t thread;
46 pthread_create(&thread, nullptr, func, arg);
47 pthread_detach(thread);
48}
49
50char* OS::executablePath() {
51 char* buffer = readLink("/proc/self/exe");
52 CHECK(buffer != nullptr, "failed to determine executable path");
53 return buffer;
54}
55
56void* OS::openSharedObject(const char* filename, int mode,
57 const char** error_msg) {
58 void* result = ::dlopen(filename, mode);
59 if (result == nullptr) {
60 *error_msg = ::dlerror();
61 }
62 return result;
63}
64
65SignalHandler OS::setSignalHandler(int signum, SignalHandler handler) {
66 struct sigaction new_context, old_context;
67 new_context.sa_handler = handler;
68 sigemptyset(&new_context.sa_mask);
69 new_context.sa_flags = 0;
70 if (::sigaction(signum, &new_context, &old_context) == -1) {
71 return SIG_ERR;
72 }
73 return old_context.sa_handler;
74}
75
76SignalHandler OS::signalHandler(int signum) {
77 struct sigaction context;
78 if (::sigaction(signum, nullptr, &context) == -1) {
79 return SIG_ERR;
80 }
81 return context.sa_handler;
82}
83
84void* OS::sharedObjectSymbolAddress(void* handle, const char* symbol,
85 const char** error_msg) {
86 void* result = ::dlsym(handle, symbol);
87 if (result == nullptr && error_msg != nullptr) {
88 *error_msg = ::dlerror();
89 }
90 return result;
91}
92
93word OS::sharedObjectSymbolName(void* addr, char* buf, word size) {
94 Dl_info info;
95 if (::dladdr(addr, &info) && info.dli_sname != nullptr) {
96 return std::snprintf(buf, size, "%s", info.dli_sname);
97 }
98 return -1;
99}
100
101} // namespace py