Mark Salyzyn | 376e9e6 | 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 | |
| 17 | #include <errno.h> |
| 18 | #include <fcntl.h> |
| 19 | #include <unistd.h> |
| 20 | |
Mark Salyzyn | 5e6d941 | 2016-10-17 14:28:00 -0700 | [diff] [blame] | 21 | #include <log/log.h> |
Mark Salyzyn | 376e9e6 | 2016-03-01 13:45:42 -0800 | [diff] [blame] | 22 | |
| 23 | #include "config_write.h" |
| 24 | #include "fake_log_device.h" |
| 25 | #include "log_portability.h" |
| 26 | #include "logger.h" |
| 27 | |
| 28 | static int fakeOpen(); |
| 29 | static void fakeClose(); |
Tom Cherry | f623f02 | 2019-01-10 10:37:36 -0800 | [diff] [blame] | 30 | static int fakeWrite(log_id_t log_id, struct timespec* ts, struct iovec* vec, size_t nr); |
Mark Salyzyn | 376e9e6 | 2016-03-01 13:45:42 -0800 | [diff] [blame] | 31 | |
Tom Cherry | f623f02 | 2019-01-10 10:37:36 -0800 | [diff] [blame] | 32 | static int logFds[(int)LOG_ID_MAX] = {-1, -1, -1, -1, -1, -1}; |
Mark Salyzyn | 376e9e6 | 2016-03-01 13:45:42 -0800 | [diff] [blame] | 33 | |
Tom Cherry | 3d6a878 | 2019-02-08 11:46:19 -0800 | [diff] [blame] | 34 | struct android_log_transport_write fakeLoggerWrite = { |
Tom Cherry | f623f02 | 2019-01-10 10:37:36 -0800 | [diff] [blame] | 35 | .node = {&fakeLoggerWrite.node, &fakeLoggerWrite.node}, |
| 36 | .context.priv = &logFds, |
| 37 | .name = "fake", |
| 38 | .available = NULL, |
| 39 | .open = fakeOpen, |
| 40 | .close = fakeClose, |
| 41 | .write = fakeWrite, |
Mark Salyzyn | 376e9e6 | 2016-03-01 13:45:42 -0800 | [diff] [blame] | 42 | }; |
| 43 | |
| 44 | static int fakeOpen() { |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 45 | int i; |
Mark Salyzyn | 376e9e6 | 2016-03-01 13:45:42 -0800 | [diff] [blame] | 46 | |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 47 | for (i = 0; i < LOG_ID_MAX; i++) { |
| 48 | /* |
| 49 | * Known maximum size string, plus an 8 character margin to deal with |
| 50 | * possible independent changes to android_log_id_to_name(). |
| 51 | */ |
| 52 | char buf[sizeof("/dev/log_security") + 8]; |
| 53 | if (logFds[i] >= 0) { |
| 54 | continue; |
Mark Salyzyn | 376e9e6 | 2016-03-01 13:45:42 -0800 | [diff] [blame] | 55 | } |
Tom Cherry | f623f02 | 2019-01-10 10:37:36 -0800 | [diff] [blame] | 56 | snprintf(buf, sizeof(buf), "/dev/log_%s", android_log_id_to_name(static_cast<log_id_t>(i))); |
Dan Willemsen | 4e970cc | 2017-05-07 12:17:15 -0700 | [diff] [blame] | 57 | logFds[i] = fakeLogOpen(buf); |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 58 | if (logFds[i] < 0) { |
Dan Willemsen | 4e970cc | 2017-05-07 12:17:15 -0700 | [diff] [blame] | 59 | fprintf(stderr, "fakeLogOpen(%s) failed\n", buf); |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 60 | } |
| 61 | } |
| 62 | return 0; |
Mark Salyzyn | 376e9e6 | 2016-03-01 13:45:42 -0800 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | static void fakeClose() { |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 66 | int i; |
Mark Salyzyn | 376e9e6 | 2016-03-01 13:45:42 -0800 | [diff] [blame] | 67 | |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 68 | for (i = 0; i < LOG_ID_MAX; i++) { |
| 69 | fakeLogClose(logFds[i]); |
| 70 | logFds[i] = -1; |
| 71 | } |
Mark Salyzyn | 376e9e6 | 2016-03-01 13:45:42 -0800 | [diff] [blame] | 72 | } |
| 73 | |
Tom Cherry | f623f02 | 2019-01-10 10:37:36 -0800 | [diff] [blame] | 74 | static int fakeWrite(log_id_t log_id, struct timespec*, struct iovec* vec, size_t nr) { |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 75 | ssize_t ret; |
| 76 | size_t i; |
| 77 | int logFd, len; |
Mark Salyzyn | 376e9e6 | 2016-03-01 13:45:42 -0800 | [diff] [blame] | 78 | |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 79 | if (/*(int)log_id >= 0 &&*/ (int)log_id >= (int)LOG_ID_MAX) { |
| 80 | return -EINVAL; |
| 81 | } |
Mark Salyzyn | 7ad9192 | 2016-03-08 16:18:26 -0800 | [diff] [blame] | 82 | |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 83 | len = 0; |
| 84 | for (i = 0; i < nr; ++i) { |
| 85 | len += vec[i].iov_len; |
| 86 | } |
Mark Salyzyn | 7ad9192 | 2016-03-08 16:18:26 -0800 | [diff] [blame] | 87 | |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 88 | if (len > LOGGER_ENTRY_MAX_PAYLOAD) { |
| 89 | len = LOGGER_ENTRY_MAX_PAYLOAD; |
| 90 | } |
Mark Salyzyn | 376e9e6 | 2016-03-01 13:45:42 -0800 | [diff] [blame] | 91 | |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 92 | logFd = logFds[(int)log_id]; |
| 93 | ret = TEMP_FAILURE_RETRY(fakeLogWritev(logFd, vec, nr)); |
| 94 | if (ret < 0) { |
| 95 | ret = -errno; |
| 96 | } else if (ret > len) { |
| 97 | ret = len; |
| 98 | } |
Mark Salyzyn | 376e9e6 | 2016-03-01 13:45:42 -0800 | [diff] [blame] | 99 | |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 100 | return ret; |
Mark Salyzyn | 376e9e6 | 2016-03-01 13:45:42 -0800 | [diff] [blame] | 101 | } |