MCPcopy Create free account
hub / github.com/apache/brpc / DetermineDevShmExecutable

Function DetermineDevShmExecutable

src/butil/file_util_posix.cc:163–182  ·  view source on GitHub ↗

Determine if /dev/shm files can be mapped and then mprotect'd PROT_EXEC. This depends on the mount options used for /dev/shm, which vary among different Linux distributions and possibly local configuration. It also depends on details of kernel--ChromeOS uses the noexec option for /dev/shm but its kernel allows mprotect with PROT_EXEC anyway.

Source from the content-addressed store, hash-verified

161// depends on details of kernel--ChromeOS uses the noexec option for /dev/shm
162// but its kernel allows mprotect with PROT_EXEC anyway.
163bool DetermineDevShmExecutable() {
164 bool result = false;
165 FilePath path;
166
167 ScopedFD fd(CreateAndOpenFdForTemporaryFile(FilePath("/dev/shm"), &path));
168 if (fd.is_valid()) {
169 DeleteFile(path, false);
170 long sysconf_result = sysconf(_SC_PAGESIZE);
171 CHECK_GE(sysconf_result, 0);
172 size_t pagesize = static_cast<size_t>(sysconf_result);
173 CHECK_GE(sizeof(pagesize), sizeof(sysconf_result));
174 void* mapping = mmap(NULL, pagesize, PROT_READ, MAP_SHARED, fd.get(), 0);
175 if (mapping != MAP_FAILED) {
176 if (mprotect(mapping, pagesize, PROT_READ | PROT_EXEC) == 0)
177 result = true;
178 munmap(mapping, pagesize);
179 }
180 }
181 return result;
182}
183#endif // defined(OS_LINUX)
184
185} // namespace

Callers 1

GetShmemTempDirFunction · 0.85

Calls 5

DeleteFileFunction · 0.85
FilePathClass · 0.50
is_validMethod · 0.45
getMethod · 0.45

Tested by

no test coverage detected