Mark Salyzyn | dd9094a | 2016-03-01 13:45:42 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2007-2016 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 | |
Tom Cherry | d7df098 | 2020-01-08 15:18:26 -0800 | [diff] [blame] | 17 | #include "pmsg_writer.h" |
Mark Salyzyn | dd9094a | 2016-03-01 13:45:42 -0800 | [diff] [blame] | 18 | |
| 19 | #include <errno.h> |
| 20 | #include <fcntl.h> |
| 21 | #include <stdlib.h> |
| 22 | #include <string.h> |
| 23 | #include <sys/types.h> |
| 24 | #include <time.h> |
| 25 | |
Steven Moreland | cd077ee | 2017-04-13 23:48:57 -0700 | [diff] [blame] | 26 | #include <log/log_properties.h> |
Mark Salyzyn | dd9094a | 2016-03-01 13:45:42 -0800 | [diff] [blame] | 27 | #include <private/android_logger.h> |
| 28 | |
Mark Salyzyn | dd9094a | 2016-03-01 13:45:42 -0800 | [diff] [blame] | 29 | #include "logger.h" |
Tom Cherry | ebebf69 | 2019-01-16 14:17:08 -0800 | [diff] [blame] | 30 | #include "uio.h" |
Mark Salyzyn | dd9094a | 2016-03-01 13:45:42 -0800 | [diff] [blame] | 31 | |
Tom Cherry | 32326ab | 2020-04-24 17:15:26 -0700 | [diff] [blame] | 32 | static atomic_int pmsg_fd; |
Mark Salyzyn | ceee2f5 | 2016-10-10 07:27:42 -0700 | [diff] [blame] | 33 | |
Tom Cherry | 32326ab | 2020-04-24 17:15:26 -0700 | [diff] [blame] | 34 | static void GetPmsgFd() { |
Tom Cherry | ce8fb8f | 2020-11-13 11:05:56 -0800 | [diff] [blame] | 35 | // Note if open() fails and returns -1, that value is stored into pmsg_fd as an indication that |
| 36 | // pmsg is not available and open() should not be retried. |
Tom Cherry | 32326ab | 2020-04-24 17:15:26 -0700 | [diff] [blame] | 37 | if (pmsg_fd != 0) { |
Tom Cherry | e9a284a | 2019-12-11 12:56:01 -0800 | [diff] [blame] | 38 | return; |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 39 | } |
Mark Salyzyn | dd9094a | 2016-03-01 13:45:42 -0800 | [diff] [blame] | 40 | |
Tom Cherry | 32326ab | 2020-04-24 17:15:26 -0700 | [diff] [blame] | 41 | int new_fd = TEMP_FAILURE_RETRY(open("/dev/pmsg0", O_WRONLY | O_CLOEXEC)); |
Tom Cherry | ce8fb8f | 2020-11-13 11:05:56 -0800 | [diff] [blame] | 42 | |
| 43 | // Unlikely that new_fd is 0, but that is synonymous with our uninitialized value, and we'd prefer |
| 44 | // STDIN_FILENO != stdin, so we call open() to get a new fd value in this case. |
| 45 | if (new_fd == 0) { |
| 46 | new_fd = TEMP_FAILURE_RETRY(open("/dev/pmsg0", O_WRONLY | O_CLOEXEC)); |
| 47 | close(0); |
Tom Cherry | 32326ab | 2020-04-24 17:15:26 -0700 | [diff] [blame] | 48 | } |
| 49 | |
Tom Cherry | ce8fb8f | 2020-11-13 11:05:56 -0800 | [diff] [blame] | 50 | // pmsg_fd should only be opened once. If we see that pmsg_fd is uninitialized, we open |
| 51 | // "/dev/pmsg0" then attempt to compare/exchange it into pmsg_fd. If the compare/exchange was |
| 52 | // successful, then that will be the fd used for the duration of the program, otherwise a |
| 53 | // different thread has already opened and written the fd to the atomic, so close the new fd and |
| 54 | // return. |
Tom Cherry | 32326ab | 2020-04-24 17:15:26 -0700 | [diff] [blame] | 55 | int uninitialized_value = 0; |
| 56 | if (!pmsg_fd.compare_exchange_strong(uninitialized_value, new_fd)) { |
Tom Cherry | ce8fb8f | 2020-11-13 11:05:56 -0800 | [diff] [blame] | 57 | if (new_fd != -1) { |
| 58 | close(new_fd); |
| 59 | } |
Tom Cherry | 32326ab | 2020-04-24 17:15:26 -0700 | [diff] [blame] | 60 | } |
Mark Salyzyn | dd9094a | 2016-03-01 13:45:42 -0800 | [diff] [blame] | 61 | } |
| 62 | |
Tom Cherry | d7df098 | 2020-01-08 15:18:26 -0800 | [diff] [blame] | 63 | void PmsgClose() { |
Tom Cherry | e9a284a | 2019-12-11 12:56:01 -0800 | [diff] [blame] | 64 | if (pmsg_fd > 0) { |
| 65 | close(pmsg_fd); |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 66 | } |
Tom Cherry | e9a284a | 2019-12-11 12:56:01 -0800 | [diff] [blame] | 67 | pmsg_fd = 0; |
Mark Salyzyn | dd9094a | 2016-03-01 13:45:42 -0800 | [diff] [blame] | 68 | } |
| 69 | |
Tom Cherry | d7df098 | 2020-01-08 15:18:26 -0800 | [diff] [blame] | 70 | int PmsgWrite(log_id_t logId, struct timespec* ts, struct iovec* vec, size_t nr) { |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 71 | static const unsigned headerLength = 2; |
| 72 | struct iovec newVec[nr + headerLength]; |
| 73 | android_log_header_t header; |
| 74 | android_pmsg_log_header_t pmsgHeader; |
| 75 | size_t i, payloadSize; |
| 76 | ssize_t ret; |
Mark Salyzyn | dd9094a | 2016-03-01 13:45:42 -0800 | [diff] [blame] | 77 | |
Alessandra Loro | f2e9cce | 2022-09-02 15:40:36 +0000 | [diff] [blame] | 78 | if (!ANDROID_DEBUGGABLE) { |
Tom Cherry | e9a284a | 2019-12-11 12:56:01 -0800 | [diff] [blame] | 79 | if (logId != LOG_ID_EVENTS && logId != LOG_ID_SECURITY) { |
| 80 | return -1; |
Mark Salyzyn | 509c142 | 2016-03-25 15:50:46 -0700 | [diff] [blame] | 81 | } |
| 82 | |
Tom Cherry | e9a284a | 2019-12-11 12:56:01 -0800 | [diff] [blame] | 83 | if (logId == LOG_ID_EVENTS) { |
| 84 | if (vec[0].iov_len < 4) { |
| 85 | return -EINVAL; |
| 86 | } |
| 87 | |
| 88 | if (SNET_EVENT_LOG_TAG != *static_cast<uint32_t*>(vec[0].iov_base)) { |
| 89 | return -EPERM; |
| 90 | } |
Mark Salyzyn | dd9094a | 2016-03-01 13:45:42 -0800 | [diff] [blame] | 91 | } |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 92 | } |
Mark Salyzyn | dd9094a | 2016-03-01 13:45:42 -0800 | [diff] [blame] | 93 | |
Tom Cherry | 32326ab | 2020-04-24 17:15:26 -0700 | [diff] [blame] | 94 | GetPmsgFd(); |
Tom Cherry | e9a284a | 2019-12-11 12:56:01 -0800 | [diff] [blame] | 95 | |
| 96 | if (pmsg_fd <= 0) { |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 97 | return -EBADF; |
| 98 | } |
Mark Salyzyn | dd9094a | 2016-03-01 13:45:42 -0800 | [diff] [blame] | 99 | |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 100 | /* |
| 101 | * struct { |
| 102 | * // what we provide to pstore |
| 103 | * android_pmsg_log_header_t pmsgHeader; |
| 104 | * // what we provide to file |
| 105 | * android_log_header_t header; |
| 106 | * // caller provides |
| 107 | * union { |
| 108 | * struct { |
| 109 | * char prio; |
| 110 | * char payload[]; |
| 111 | * } string; |
| 112 | * struct { |
| 113 | * uint32_t tag |
| 114 | * char payload[]; |
| 115 | * } binary; |
| 116 | * }; |
| 117 | * }; |
| 118 | */ |
Mark Salyzyn | dd9094a | 2016-03-01 13:45:42 -0800 | [diff] [blame] | 119 | |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 120 | pmsgHeader.magic = LOGGER_MAGIC; |
| 121 | pmsgHeader.len = sizeof(pmsgHeader) + sizeof(header); |
Tom Cherry | 366afd8 | 2020-01-08 15:34:14 -0800 | [diff] [blame] | 122 | pmsgHeader.uid = getuid(); |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 123 | pmsgHeader.pid = getpid(); |
Mark Salyzyn | dd9094a | 2016-03-01 13:45:42 -0800 | [diff] [blame] | 124 | |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 125 | header.id = logId; |
| 126 | header.tid = gettid(); |
| 127 | header.realtime.tv_sec = ts->tv_sec; |
| 128 | header.realtime.tv_nsec = ts->tv_nsec; |
Mark Salyzyn | dd9094a | 2016-03-01 13:45:42 -0800 | [diff] [blame] | 129 | |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 130 | newVec[0].iov_base = (unsigned char*)&pmsgHeader; |
| 131 | newVec[0].iov_len = sizeof(pmsgHeader); |
| 132 | newVec[1].iov_base = (unsigned char*)&header; |
| 133 | newVec[1].iov_len = sizeof(header); |
Mark Salyzyn | dd9094a | 2016-03-01 13:45:42 -0800 | [diff] [blame] | 134 | |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 135 | for (payloadSize = 0, i = headerLength; i < nr + headerLength; i++) { |
| 136 | newVec[i].iov_base = vec[i - headerLength].iov_base; |
| 137 | payloadSize += newVec[i].iov_len = vec[i - headerLength].iov_len; |
| 138 | |
| 139 | if (payloadSize > LOGGER_ENTRY_MAX_PAYLOAD) { |
| 140 | newVec[i].iov_len -= payloadSize - LOGGER_ENTRY_MAX_PAYLOAD; |
| 141 | if (newVec[i].iov_len) { |
| 142 | ++i; |
| 143 | } |
| 144 | payloadSize = LOGGER_ENTRY_MAX_PAYLOAD; |
| 145 | break; |
Mark Salyzyn | dd9094a | 2016-03-01 13:45:42 -0800 | [diff] [blame] | 146 | } |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 147 | } |
| 148 | pmsgHeader.len += payloadSize; |
Mark Salyzyn | dd9094a | 2016-03-01 13:45:42 -0800 | [diff] [blame] | 149 | |
Tom Cherry | e9a284a | 2019-12-11 12:56:01 -0800 | [diff] [blame] | 150 | ret = TEMP_FAILURE_RETRY(writev(pmsg_fd, newVec, i)); |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 151 | if (ret < 0) { |
| 152 | ret = errno ? -errno : -ENOTCONN; |
| 153 | } |
Mark Salyzyn | dd9094a | 2016-03-01 13:45:42 -0800 | [diff] [blame] | 154 | |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 155 | if (ret > (ssize_t)(sizeof(header) + sizeof(pmsgHeader))) { |
| 156 | ret -= sizeof(header) - sizeof(pmsgHeader); |
| 157 | } |
Mark Salyzyn | dd9094a | 2016-03-01 13:45:42 -0800 | [diff] [blame] | 158 | |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 159 | return ret; |
Mark Salyzyn | dd9094a | 2016-03-01 13:45:42 -0800 | [diff] [blame] | 160 | } |
Mark Salyzyn | ad65502 | 2016-03-10 09:50:08 -0800 | [diff] [blame] | 161 | |
| 162 | /* |
| 163 | * Virtual pmsg filesystem |
| 164 | * |
| 165 | * Payload will comprise the string "<basedir>:<basefile>\0<content>" to a |
| 166 | * maximum of LOGGER_ENTRY_MAX_PAYLOAD, but scaled to the last newline in the |
| 167 | * file. |
| 168 | * |
| 169 | * Will hijack the header.realtime.tv_nsec field for a sequence number in usec. |
| 170 | */ |
| 171 | |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 172 | static inline const char* strnrchr(const char* buf, size_t len, char c) { |
| 173 | const char* cp = buf + len; |
| 174 | while ((--cp > buf) && (*cp != c)) |
| 175 | ; |
| 176 | if (cp <= buf) { |
| 177 | return buf + len; |
| 178 | } |
| 179 | return cp; |
Mark Salyzyn | ad65502 | 2016-03-10 09:50:08 -0800 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | /* Write a buffer as filename references (tag = <basedir>:<basename>) */ |
Tom Cherry | 3d6a878 | 2019-02-08 11:46:19 -0800 | [diff] [blame] | 183 | ssize_t __android_log_pmsg_file_write(log_id_t logId, char prio, const char* filename, |
| 184 | const char* buf, size_t len) { |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 185 | size_t length, packet_len; |
| 186 | const char* tag; |
| 187 | char *cp, *slash; |
| 188 | struct timespec ts; |
| 189 | struct iovec vec[3]; |
Mark Salyzyn | ad65502 | 2016-03-10 09:50:08 -0800 | [diff] [blame] | 190 | |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 191 | /* Make sure the logId value is not a bad idea */ |
| 192 | if ((logId == LOG_ID_KERNEL) || /* Verbotten */ |
| 193 | (logId == LOG_ID_EVENTS) || /* Do not support binary content */ |
| 194 | (logId == LOG_ID_SECURITY) || /* Bad idea to allow */ |
| 195 | ((unsigned)logId >= 32)) { /* fit within logMask on arch32 */ |
| 196 | return -EINVAL; |
| 197 | } |
Mark Salyzyn | ad65502 | 2016-03-10 09:50:08 -0800 | [diff] [blame] | 198 | |
Tom Cherry | a5ff7ae | 2020-04-08 14:36:05 -0700 | [diff] [blame] | 199 | clock_gettime(CLOCK_REALTIME, &ts); |
Mark Salyzyn | ad65502 | 2016-03-10 09:50:08 -0800 | [diff] [blame] | 200 | |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 201 | cp = strdup(filename); |
| 202 | if (!cp) { |
| 203 | return -ENOMEM; |
| 204 | } |
Mark Salyzyn | ad65502 | 2016-03-10 09:50:08 -0800 | [diff] [blame] | 205 | |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 206 | tag = cp; |
| 207 | slash = strrchr(cp, '/'); |
| 208 | if (slash) { |
| 209 | *slash = ':'; |
Mark Salyzyn | ad65502 | 2016-03-10 09:50:08 -0800 | [diff] [blame] | 210 | slash = strrchr(cp, '/'); |
| 211 | if (slash) { |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 212 | tag = slash + 1; |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | length = strlen(tag) + 1; |
| 217 | packet_len = LOGGER_ENTRY_MAX_PAYLOAD - sizeof(char) - length; |
| 218 | |
| 219 | vec[0].iov_base = &prio; |
| 220 | vec[0].iov_len = sizeof(char); |
| 221 | vec[1].iov_base = (unsigned char*)tag; |
| 222 | vec[1].iov_len = length; |
| 223 | |
Tom Cherry | f623f02 | 2019-01-10 10:37:36 -0800 | [diff] [blame] | 224 | for (ts.tv_nsec = 0, length = len; length; ts.tv_nsec += ANDROID_LOG_PMSG_FILE_SEQUENCE) { |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 225 | ssize_t ret; |
| 226 | size_t transfer; |
| 227 | |
Tom Cherry | f623f02 | 2019-01-10 10:37:36 -0800 | [diff] [blame] | 228 | if ((ts.tv_nsec / ANDROID_LOG_PMSG_FILE_SEQUENCE) >= ANDROID_LOG_PMSG_FILE_MAX_SEQUENCE) { |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 229 | len -= length; |
| 230 | break; |
Mark Salyzyn | ad65502 | 2016-03-10 09:50:08 -0800 | [diff] [blame] | 231 | } |
| 232 | |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 233 | transfer = length; |
| 234 | if (transfer > packet_len) { |
| 235 | transfer = strnrchr(buf, packet_len - 1, '\n') - buf; |
| 236 | if ((transfer < length) && (buf[transfer] == '\n')) { |
| 237 | ++transfer; |
| 238 | } |
Mark Salyzyn | ad65502 | 2016-03-10 09:50:08 -0800 | [diff] [blame] | 239 | } |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 240 | |
| 241 | vec[2].iov_base = (unsigned char*)buf; |
| 242 | vec[2].iov_len = transfer; |
| 243 | |
Tom Cherry | e9a284a | 2019-12-11 12:56:01 -0800 | [diff] [blame] | 244 | ret = PmsgWrite(logId, &ts, vec, sizeof(vec) / sizeof(vec[0])); |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 245 | |
| 246 | if (ret <= 0) { |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 247 | free(cp); |
| 248 | return ret ? ret : (len - length); |
Mark Salyzyn | ceee2f5 | 2016-10-10 07:27:42 -0700 | [diff] [blame] | 249 | } |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 250 | length -= transfer; |
| 251 | buf += transfer; |
| 252 | } |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 253 | free(cp); |
| 254 | return len; |
Mark Salyzyn | ad65502 | 2016-03-10 09:50:08 -0800 | [diff] [blame] | 255 | } |