Christopher Ferris | 3bae40b | 2019-08-16 10:49:34 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include <procinfo/process_map.h> |
| 18 | |
| 19 | #include <fcntl.h> |
| 20 | #include <string.h> |
| 21 | #include <unistd.h> |
| 22 | |
| 23 | #include <procinfo/process.h> |
| 24 | |
| 25 | namespace android { |
| 26 | namespace procinfo { |
| 27 | |
| 28 | bool ReadMapFileAsyncSafe(const char* map_file, void* buffer, size_t buffer_size, |
| 29 | const std::function<void(uint64_t, uint64_t, uint16_t, uint64_t, ino_t, |
| 30 | const char*)>& callback) { |
| 31 | if (buffer == nullptr || buffer_size == 0) { |
| 32 | return false; |
| 33 | } |
| 34 | |
| 35 | int fd = open(map_file, O_RDONLY | O_CLOEXEC); |
| 36 | if (fd == -1) { |
| 37 | return false; |
| 38 | } |
| 39 | |
| 40 | char* char_buffer = reinterpret_cast<char*>(buffer); |
| 41 | size_t start = 0; |
| 42 | size_t read_bytes = 0; |
| 43 | char* line = nullptr; |
| 44 | bool read_complete = false; |
| 45 | while (true) { |
| 46 | ssize_t bytes = |
| 47 | TEMP_FAILURE_RETRY(read(fd, char_buffer + read_bytes, buffer_size - read_bytes - 1)); |
| 48 | if (bytes <= 0) { |
| 49 | if (read_bytes == 0) { |
| 50 | close(fd); |
| 51 | return bytes == 0; |
| 52 | } |
| 53 | // Treat the last piece of data as the last line. |
| 54 | char_buffer[start + read_bytes] = '\n'; |
| 55 | bytes = 1; |
| 56 | read_complete = true; |
| 57 | } |
| 58 | read_bytes += bytes; |
| 59 | |
| 60 | while (read_bytes > 0) { |
| 61 | char* newline = reinterpret_cast<char*>(memchr(&char_buffer[start], '\n', read_bytes)); |
| 62 | if (newline == nullptr) { |
| 63 | break; |
| 64 | } |
| 65 | *newline = '\0'; |
| 66 | line = &char_buffer[start]; |
| 67 | start = newline - char_buffer + 1; |
| 68 | read_bytes -= newline - line + 1; |
| 69 | |
| 70 | // Ignore the return code, errors are okay. |
| 71 | ReadMapFileContent(line, callback); |
| 72 | } |
| 73 | |
| 74 | if (read_complete) { |
| 75 | close(fd); |
| 76 | return true; |
| 77 | } |
| 78 | |
| 79 | if (start == 0 && read_bytes == buffer_size - 1) { |
| 80 | // The buffer provided is too small to contain this line, give up |
| 81 | // and indicate failure. |
| 82 | close(fd); |
| 83 | return false; |
| 84 | } |
| 85 | |
| 86 | // Copy any leftover data to the front of the buffer. |
| 87 | if (start > 0) { |
| 88 | if (read_bytes > 0) { |
| 89 | memmove(char_buffer, &char_buffer[start], read_bytes); |
| 90 | } |
| 91 | start = 0; |
| 92 | } |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | } /* namespace procinfo */ |
| 97 | } /* namespace android */ |