aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/yt/memory/safe_memory_reader.cpp
blob: 49d88b3ba1bf277c7035117bd4be5555d01e5062 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include "safe_memory_reader.h"

#ifdef _linux_
#include <fcntl.h>
#include <unistd.h>
#endif

#include <library/cpp/yt/assert/assert.h>

namespace NYT {

////////////////////////////////////////////////////////////////////////////////

TSafeMemoryReader::TSafeMemoryReader()
{
#ifdef _linux_
    FD_ = ::open("/proc/self/mem", O_RDONLY);
    YT_VERIFY(FD_ >= 0);
#endif
}

TSafeMemoryReader::~TSafeMemoryReader()
{
#ifdef _linux_
    ::close(FD_);
#endif
}

bool TSafeMemoryReader::ReadRaw(const void* addr, void* ptr, size_t size)
{
#ifdef _linux_
    int ret;
    do {
        ret = ::pread64(FD_, ptr, size, reinterpret_cast<uintptr_t>(addr));
    } while (ret < 0 && errno == EINTR);
    return ret == static_cast<int>(size);
#else
    Y_UNUSED(FD_, addr, ptr, size);
    return false;
#endif
}

////////////////////////////////////////////////////////////////////////////////

} // namespace NYT