Mark Salyzyn | facf94c | 2016-03-01 13:45:42 -0800 | [diff] [blame] | 1 | /* |
| 2 | ** Copyright 2013-2014, 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 <errno.h> |
| 18 | #include <fcntl.h> |
| 19 | #include <pthread.h> |
| 20 | #include <sched.h> |
| 21 | #include <stddef.h> |
| 22 | #include <stdlib.h> |
| 23 | #include <string.h> |
| 24 | #include <unistd.h> |
| 25 | |
| 26 | #include <cutils/list.h> |
| 27 | #include <log/log.h> |
| 28 | #include <log/logger.h> |
Mark Salyzyn | 5e635f7 | 2016-03-28 16:20:29 -0700 | [diff] [blame^] | 29 | #include <private/android_filesystem_config.h> |
Mark Salyzyn | facf94c | 2016-03-01 13:45:42 -0800 | [diff] [blame] | 30 | |
| 31 | #include "config_read.h" |
| 32 | #include "log_portability.h" |
| 33 | #include "logger.h" |
| 34 | |
| 35 | /* android_logger_alloc unimplemented, no use case */ |
| 36 | /* android_logger_free not exported */ |
| 37 | static void android_logger_free(struct logger *logger) |
| 38 | { |
| 39 | struct android_log_logger *logger_internal = |
| 40 | (struct android_log_logger *)logger; |
| 41 | |
| 42 | if (!logger_internal) { |
| 43 | return; |
| 44 | } |
| 45 | |
| 46 | list_remove(&logger_internal->node); |
| 47 | |
| 48 | free(logger_internal); |
| 49 | } |
| 50 | |
| 51 | /* android_logger_alloc unimplemented, no use case */ |
| 52 | |
| 53 | /* method for getting the associated sublog id */ |
| 54 | LIBLOG_ABI_PUBLIC log_id_t android_logger_get_id(struct logger *logger) |
| 55 | { |
| 56 | return ((struct android_log_logger *)logger)->logId; |
| 57 | } |
| 58 | |
| 59 | static int init_transport_context(struct android_log_logger_list *logger_list) |
| 60 | { |
| 61 | struct android_log_transport_read *transport; |
| 62 | struct listnode *node; |
| 63 | |
| 64 | if (!logger_list) { |
| 65 | return -EINVAL; |
| 66 | } |
| 67 | |
| 68 | if (list_empty(&logger_list->logger)) { |
| 69 | return -EINVAL; |
| 70 | } |
| 71 | |
| 72 | if (!list_empty(&logger_list->transport)) { |
| 73 | return 0; |
| 74 | } |
| 75 | |
| 76 | __android_log_lock(); |
| 77 | /* mini __write_to_log_initialize() to populate transports */ |
| 78 | if (list_empty(&__android_log_transport_read) && |
| 79 | list_empty(&__android_log_persist_read)) { |
| 80 | __android_log_config_read(); |
| 81 | } |
| 82 | __android_log_unlock(); |
| 83 | |
| 84 | node = (logger_list->mode & ANDROID_LOG_PSTORE) ? |
| 85 | &__android_log_persist_read : &__android_log_transport_read; |
| 86 | |
| 87 | read_transport_for_each(transport, node) { |
| 88 | struct android_log_transport_context *transp; |
| 89 | struct android_log_logger *logger; |
| 90 | unsigned logMask = 0; |
| 91 | |
| 92 | logger_for_each(logger, logger_list) { |
| 93 | log_id_t logId = logger->logId; |
| 94 | |
Mark Salyzyn | 5e635f7 | 2016-03-28 16:20:29 -0700 | [diff] [blame^] | 95 | if ((logId == LOG_ID_SECURITY) && |
| 96 | (__android_log_uid() != AID_SYSTEM)) { |
| 97 | continue; |
| 98 | } |
Mark Salyzyn | facf94c | 2016-03-01 13:45:42 -0800 | [diff] [blame] | 99 | if (transport->read && |
| 100 | (!transport->available || |
| 101 | (transport->available(logId) >= 0))) { |
| 102 | logMask |= 1 << logId; |
| 103 | } |
| 104 | } |
| 105 | if (!logMask) { |
| 106 | continue; |
| 107 | } |
| 108 | transp = calloc(1, sizeof(*transp)); |
| 109 | if (!transp) { |
| 110 | return -ENOMEM; |
| 111 | } |
| 112 | transp->parent = logger_list; |
| 113 | transp->transport = transport; |
| 114 | transp->logMask = logMask; |
| 115 | transp->ret = 1; |
| 116 | list_add_tail(&logger_list->transport, &transp->node); |
| 117 | } |
| 118 | if (list_empty(&logger_list->transport)) { |
| 119 | return -ENODEV; |
| 120 | } |
| 121 | return 0; |
| 122 | } |
| 123 | |
| 124 | #define LOGGER_FUNCTION(logger, def, func, args...) \ |
| 125 | ssize_t ret = -EINVAL; \ |
| 126 | struct android_log_transport_context *transp; \ |
| 127 | struct android_log_logger *logger_internal = \ |
| 128 | (struct android_log_logger *)logger; \ |
| 129 | \ |
| 130 | if (!logger_internal) { \ |
| 131 | return ret; \ |
| 132 | } \ |
| 133 | ret = init_transport_context(logger_internal->parent); \ |
| 134 | if (ret < 0) { \ |
| 135 | return ret; \ |
| 136 | } \ |
| 137 | \ |
| 138 | ret = (def); \ |
| 139 | transport_context_for_each(transp, logger_internal->parent) { \ |
| 140 | if ((transp->logMask & (1 << logger_internal->logId)) && \ |
| 141 | transp->transport && transp->transport->func) { \ |
| 142 | ssize_t retval = (transp->transport->func)(logger_internal, \ |
| 143 | transp, ## args); \ |
| 144 | if ((ret >= 0) || (ret == (def))) { \ |
| 145 | ret = retval; \ |
| 146 | } \ |
| 147 | } \ |
| 148 | } \ |
| 149 | return ret |
| 150 | |
| 151 | LIBLOG_ABI_PUBLIC int android_logger_clear(struct logger *logger) |
| 152 | { |
| 153 | LOGGER_FUNCTION(logger, -ENODEV, clear); |
| 154 | } |
| 155 | |
| 156 | /* returns the total size of the log's ring buffer */ |
| 157 | LIBLOG_ABI_PUBLIC long android_logger_get_log_size(struct logger *logger) |
| 158 | { |
| 159 | LOGGER_FUNCTION(logger, -ENODEV, getSize); |
| 160 | } |
| 161 | |
| 162 | LIBLOG_ABI_PUBLIC int android_logger_set_log_size(struct logger *logger, |
| 163 | unsigned long size) |
| 164 | { |
| 165 | LOGGER_FUNCTION(logger, -ENODEV, setSize, size); |
| 166 | } |
| 167 | |
| 168 | /* |
| 169 | * returns the readable size of the log's ring buffer (that is, amount of the |
| 170 | * log consumed) |
| 171 | */ |
| 172 | LIBLOG_ABI_PUBLIC long android_logger_get_log_readable_size( |
| 173 | struct logger *logger) |
| 174 | { |
| 175 | LOGGER_FUNCTION(logger, -ENODEV, getReadableSize); |
| 176 | } |
| 177 | |
| 178 | /* |
| 179 | * returns the logger version |
| 180 | */ |
| 181 | LIBLOG_ABI_PUBLIC int android_logger_get_log_version(struct logger *logger) |
| 182 | { |
| 183 | LOGGER_FUNCTION(logger, 4, version); |
| 184 | } |
| 185 | |
| 186 | #define LOGGER_LIST_FUNCTION(logger_list, def, func, args...) \ |
| 187 | struct android_log_transport_context *transp; \ |
| 188 | struct android_log_logger_list *logger_list_internal = \ |
| 189 | (struct android_log_logger_list *)logger_list; \ |
| 190 | \ |
| 191 | ssize_t ret = init_transport_context(logger_list_internal); \ |
| 192 | if (ret < 0) { \ |
| 193 | return ret; \ |
| 194 | } \ |
| 195 | \ |
| 196 | ret = (def); \ |
| 197 | transport_context_for_each(transp, logger_list_internal) { \ |
| 198 | if (transp->transport && (transp->transport->func)) { \ |
| 199 | ssize_t retval = (transp->transport->func)(logger_list_internal, \ |
| 200 | transp, ## args); \ |
| 201 | if ((ret >= 0) || (ret == (def))) { \ |
| 202 | ret = retval; \ |
| 203 | } \ |
| 204 | } \ |
| 205 | } \ |
| 206 | return ret |
| 207 | |
| 208 | /* |
| 209 | * returns statistics |
| 210 | */ |
| 211 | LIBLOG_ABI_PUBLIC ssize_t android_logger_get_statistics( |
| 212 | struct logger_list *logger_list, |
| 213 | char *buf, size_t len) |
| 214 | { |
| 215 | LOGGER_LIST_FUNCTION(logger_list, -ENODEV, getStats, buf, len); |
| 216 | } |
| 217 | |
| 218 | LIBLOG_ABI_PUBLIC ssize_t android_logger_get_prune_list( |
| 219 | struct logger_list *logger_list, |
| 220 | char *buf, size_t len) |
| 221 | { |
| 222 | LOGGER_LIST_FUNCTION(logger_list, -ENODEV, getPrune, buf, len); |
| 223 | } |
| 224 | |
| 225 | LIBLOG_ABI_PUBLIC int android_logger_set_prune_list( |
| 226 | struct logger_list *logger_list, |
| 227 | char *buf, size_t len) |
| 228 | { |
| 229 | LOGGER_LIST_FUNCTION(logger_list, -ENODEV, setPrune, buf, len); |
| 230 | } |
| 231 | |
| 232 | LIBLOG_ABI_PUBLIC struct logger_list *android_logger_list_alloc( |
| 233 | int mode, |
| 234 | unsigned int tail, |
| 235 | pid_t pid) |
| 236 | { |
| 237 | struct android_log_logger_list *logger_list; |
| 238 | |
| 239 | logger_list = calloc(1, sizeof(*logger_list)); |
| 240 | if (!logger_list) { |
| 241 | return NULL; |
| 242 | } |
| 243 | |
| 244 | list_init(&logger_list->logger); |
| 245 | list_init(&logger_list->transport); |
| 246 | logger_list->mode = mode; |
| 247 | logger_list->tail = tail; |
| 248 | logger_list->pid = pid; |
| 249 | |
| 250 | return (struct logger_list *)logger_list; |
| 251 | } |
| 252 | |
| 253 | LIBLOG_ABI_PUBLIC struct logger_list *android_logger_list_alloc_time( |
| 254 | int mode, |
| 255 | log_time start, |
| 256 | pid_t pid) |
| 257 | { |
| 258 | struct android_log_logger_list *logger_list; |
| 259 | |
| 260 | logger_list = calloc(1, sizeof(*logger_list)); |
| 261 | if (!logger_list) { |
| 262 | return NULL; |
| 263 | } |
| 264 | |
| 265 | list_init(&logger_list->logger); |
| 266 | list_init(&logger_list->transport); |
| 267 | logger_list->mode = mode; |
| 268 | logger_list->start = start; |
| 269 | logger_list->pid = pid; |
| 270 | |
| 271 | return (struct logger_list *)logger_list; |
| 272 | } |
| 273 | |
| 274 | /* android_logger_list_register unimplemented, no use case */ |
| 275 | /* android_logger_list_unregister unimplemented, no use case */ |
| 276 | |
| 277 | /* Open the named log and add it to the logger list */ |
| 278 | LIBLOG_ABI_PUBLIC struct logger *android_logger_open( |
| 279 | struct logger_list *logger_list, |
| 280 | log_id_t logId) |
| 281 | { |
| 282 | struct android_log_logger_list *logger_list_internal = |
| 283 | (struct android_log_logger_list *)logger_list; |
| 284 | struct android_log_logger *logger; |
| 285 | |
| 286 | if (!logger_list_internal || (logId >= LOG_ID_MAX)) { |
| 287 | goto err; |
| 288 | } |
| 289 | |
| 290 | logger_for_each(logger, logger_list_internal) { |
| 291 | if (logger->logId == logId) { |
| 292 | goto ok; |
| 293 | } |
| 294 | } |
| 295 | |
| 296 | logger = calloc(1, sizeof(*logger)); |
| 297 | if (!logger) { |
| 298 | goto err; |
| 299 | } |
| 300 | |
| 301 | logger->logId = logId; |
| 302 | list_add_tail(&logger_list_internal->logger, &logger->node); |
| 303 | logger->parent = logger_list_internal; |
| 304 | |
| 305 | /* Reset known transports to re-evaluate, we just added one */ |
| 306 | while (!list_empty(&logger_list_internal->transport)) { |
| 307 | struct listnode *node = list_head(&logger_list_internal->transport); |
| 308 | struct android_log_transport_context *transp = |
| 309 | node_to_item(node, struct android_log_transport_context, node); |
| 310 | |
| 311 | list_remove(&transp->node); |
| 312 | free(transp); |
| 313 | } |
| 314 | goto ok; |
| 315 | |
| 316 | err: |
| 317 | logger = NULL; |
| 318 | ok: |
| 319 | return (struct logger *)logger; |
| 320 | } |
| 321 | |
| 322 | /* Open the single named log and make it part of a new logger list */ |
| 323 | LIBLOG_ABI_PUBLIC struct logger_list *android_logger_list_open( |
| 324 | log_id_t logId, |
| 325 | int mode, |
| 326 | unsigned int tail, |
| 327 | pid_t pid) |
| 328 | { |
| 329 | struct logger_list *logger_list = |
| 330 | android_logger_list_alloc(mode, tail, pid); |
| 331 | |
| 332 | if (!logger_list) { |
| 333 | return NULL; |
| 334 | } |
| 335 | |
| 336 | if (!android_logger_open(logger_list, logId)) { |
| 337 | android_logger_list_free(logger_list); |
| 338 | return NULL; |
| 339 | } |
| 340 | |
| 341 | return logger_list; |
| 342 | } |
| 343 | |
| 344 | /* Read from the selected logs */ |
| 345 | LIBLOG_ABI_PUBLIC int android_logger_list_read(struct logger_list *logger_list, |
| 346 | struct log_msg *log_msg) |
| 347 | { |
| 348 | struct android_log_transport_context *transp; |
| 349 | struct android_log_logger_list *logger_list_internal = |
| 350 | (struct android_log_logger_list *)logger_list; |
| 351 | |
| 352 | int ret = init_transport_context(logger_list_internal); |
| 353 | if (ret < 0) { |
| 354 | return ret; |
| 355 | } |
| 356 | |
| 357 | /* at least one transport */ |
| 358 | transp = node_to_item(logger_list_internal->transport.next, |
| 359 | struct android_log_transport_context, node); |
| 360 | |
| 361 | /* more than one transport? */ |
| 362 | if (transp->node.next != &logger_list_internal->transport) { |
| 363 | /* Poll and merge sort the entries if from multiple transports */ |
| 364 | struct android_log_transport_context *oldest = NULL; |
| 365 | int ret; |
| 366 | int polled = 0; |
| 367 | do { |
| 368 | if (polled) { |
| 369 | sched_yield(); |
| 370 | } |
| 371 | ret = -1000; |
| 372 | polled = 0; |
| 373 | do { |
| 374 | int retval = transp->ret; |
| 375 | if ((retval > 0) && !transp->logMsg.entry.len) { |
| 376 | if (!transp->transport->read) { |
| 377 | retval = transp->ret = 0; |
| 378 | } else if ((logger_list_internal->mode & |
| 379 | ANDROID_LOG_NONBLOCK) || |
| 380 | !transp->transport->poll) { |
| 381 | retval = transp->ret = (*transp->transport->read)( |
| 382 | logger_list_internal, |
| 383 | transp, |
| 384 | &transp->logMsg); |
| 385 | } else { |
| 386 | int pollval = (*transp->transport->poll)( |
| 387 | logger_list_internal, transp); |
| 388 | if (pollval <= 0) { |
| 389 | sched_yield(); |
| 390 | pollval = (*transp->transport->poll)( |
| 391 | logger_list_internal, transp); |
| 392 | } |
| 393 | polled = 1; |
| 394 | if (pollval < 0) { |
| 395 | if ((pollval == -EINTR) || (pollval == -EAGAIN)) { |
| 396 | return -EAGAIN; |
| 397 | } |
| 398 | retval = transp->ret = pollval; |
| 399 | } else if (pollval > 0) { |
| 400 | retval = transp->ret = (*transp->transport->read)( |
| 401 | logger_list_internal, |
| 402 | transp, |
| 403 | &transp->logMsg); |
| 404 | } |
| 405 | } |
| 406 | } |
| 407 | if (ret < retval) { |
| 408 | ret = retval; |
| 409 | } |
| 410 | if ((transp->ret > 0) && transp->logMsg.entry.len && |
| 411 | (!oldest || |
| 412 | (oldest->logMsg.entry.sec > |
| 413 | transp->logMsg.entry.sec) || |
| 414 | ((oldest->logMsg.entry.sec == |
| 415 | transp->logMsg.entry.sec) && |
| 416 | (oldest->logMsg.entry.nsec > |
| 417 | transp->logMsg.entry.nsec)))) { |
| 418 | oldest = transp; |
| 419 | } |
| 420 | transp = node_to_item(transp->node.next, |
| 421 | struct android_log_transport_context, |
| 422 | node); |
| 423 | } while (transp != node_to_item( |
| 424 | &logger_list_internal->transport, |
| 425 | struct android_log_transport_context, |
| 426 | node)); |
| 427 | if (!oldest && |
| 428 | (logger_list_internal->mode & ANDROID_LOG_NONBLOCK)) { |
| 429 | return (ret < 0) ? ret : -EAGAIN; |
| 430 | } |
| 431 | transp = node_to_item(logger_list_internal->transport.next, |
| 432 | struct android_log_transport_context, node); |
| 433 | } while (!oldest && (ret > 0)); |
| 434 | if (!oldest) { |
| 435 | return ret; |
| 436 | } |
| 437 | memcpy(log_msg, &oldest->logMsg, oldest->logMsg.entry.len + |
| 438 | (oldest->logMsg.entry.hdr_size ? |
| 439 | oldest->logMsg.entry.hdr_size : |
| 440 | sizeof(struct logger_entry))); |
| 441 | oldest->logMsg.entry.len = 0; /* Mark it as copied */ |
| 442 | return oldest->ret; |
| 443 | } |
| 444 | |
| 445 | /* if only one, no need to copy into transport_context and merge-sort */ |
| 446 | return (transp->transport->read)(logger_list_internal, transp, log_msg); |
| 447 | } |
| 448 | |
| 449 | /* Close all the logs */ |
| 450 | LIBLOG_ABI_PUBLIC void android_logger_list_free(struct logger_list *logger_list) |
| 451 | { |
| 452 | struct android_log_logger_list *logger_list_internal = |
| 453 | (struct android_log_logger_list *)logger_list; |
| 454 | |
| 455 | if (logger_list_internal == NULL) { |
| 456 | return; |
| 457 | } |
| 458 | |
| 459 | while (!list_empty(&logger_list_internal->transport)) { |
| 460 | struct listnode *node = list_head(&logger_list_internal->transport); |
| 461 | struct android_log_transport_context *transp = |
| 462 | node_to_item(node, struct android_log_transport_context, node); |
| 463 | |
| 464 | if (transp->transport && transp->transport->close) { |
| 465 | (*transp->transport->close)(logger_list_internal, transp); |
| 466 | } |
| 467 | list_remove(&transp->node); |
| 468 | free(transp); |
| 469 | } |
| 470 | |
| 471 | while (!list_empty(&logger_list_internal->logger)) { |
| 472 | struct listnode *node = list_head(&logger_list_internal->logger); |
| 473 | struct android_log_logger *logger = |
| 474 | node_to_item(node, struct android_log_logger, node); |
| 475 | android_logger_free((struct logger *)logger); |
| 476 | } |
| 477 | |
| 478 | free(logger_list_internal); |
| 479 | } |