blob: c0b0e69a50fba43cf55e7e923324ca9ab2b3797b [file] [log] [blame]
Mark Salyzyn376e9e62016-03-01 13:45:42 -08001/*
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 Salyzyn5e6d9412016-10-17 14:28:00 -070021#include <log/log.h>
Mark Salyzyn376e9e62016-03-01 13:45:42 -080022
23#include "config_write.h"
24#include "fake_log_device.h"
25#include "log_portability.h"
26#include "logger.h"
27
28static int fakeOpen();
29static void fakeClose();
Tom Cherryf623f022019-01-10 10:37:36 -080030static int fakeWrite(log_id_t log_id, struct timespec* ts, struct iovec* vec, size_t nr);
Mark Salyzyn376e9e62016-03-01 13:45:42 -080031
Tom Cherryf623f022019-01-10 10:37:36 -080032static int logFds[(int)LOG_ID_MAX] = {-1, -1, -1, -1, -1, -1};
Mark Salyzyn376e9e62016-03-01 13:45:42 -080033
Tom Cherry3d6a8782019-02-08 11:46:19 -080034struct android_log_transport_write fakeLoggerWrite = {
Tom Cherryf623f022019-01-10 10:37:36 -080035 .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 Salyzyn376e9e62016-03-01 13:45:42 -080042};
43
44static int fakeOpen() {
Mark Salyzyn6e315682017-03-09 08:09:43 -080045 int i;
Mark Salyzyn376e9e62016-03-01 13:45:42 -080046
Mark Salyzyn6e315682017-03-09 08:09:43 -080047 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 Salyzyn376e9e62016-03-01 13:45:42 -080055 }
Tom Cherryf623f022019-01-10 10:37:36 -080056 snprintf(buf, sizeof(buf), "/dev/log_%s", android_log_id_to_name(static_cast<log_id_t>(i)));
Dan Willemsen4e970cc2017-05-07 12:17:15 -070057 logFds[i] = fakeLogOpen(buf);
Mark Salyzyn6e315682017-03-09 08:09:43 -080058 if (logFds[i] < 0) {
Dan Willemsen4e970cc2017-05-07 12:17:15 -070059 fprintf(stderr, "fakeLogOpen(%s) failed\n", buf);
Mark Salyzyn6e315682017-03-09 08:09:43 -080060 }
61 }
62 return 0;
Mark Salyzyn376e9e62016-03-01 13:45:42 -080063}
64
65static void fakeClose() {
Mark Salyzyn6e315682017-03-09 08:09:43 -080066 int i;
Mark Salyzyn376e9e62016-03-01 13:45:42 -080067
Mark Salyzyn6e315682017-03-09 08:09:43 -080068 for (i = 0; i < LOG_ID_MAX; i++) {
69 fakeLogClose(logFds[i]);
70 logFds[i] = -1;
71 }
Mark Salyzyn376e9e62016-03-01 13:45:42 -080072}
73
Tom Cherryf623f022019-01-10 10:37:36 -080074static int fakeWrite(log_id_t log_id, struct timespec*, struct iovec* vec, size_t nr) {
Mark Salyzyn6e315682017-03-09 08:09:43 -080075 ssize_t ret;
76 size_t i;
77 int logFd, len;
Mark Salyzyn376e9e62016-03-01 13:45:42 -080078
Mark Salyzyn6e315682017-03-09 08:09:43 -080079 if (/*(int)log_id >= 0 &&*/ (int)log_id >= (int)LOG_ID_MAX) {
80 return -EINVAL;
81 }
Mark Salyzyn7ad91922016-03-08 16:18:26 -080082
Mark Salyzyn6e315682017-03-09 08:09:43 -080083 len = 0;
84 for (i = 0; i < nr; ++i) {
85 len += vec[i].iov_len;
86 }
Mark Salyzyn7ad91922016-03-08 16:18:26 -080087
Mark Salyzyn6e315682017-03-09 08:09:43 -080088 if (len > LOGGER_ENTRY_MAX_PAYLOAD) {
89 len = LOGGER_ENTRY_MAX_PAYLOAD;
90 }
Mark Salyzyn376e9e62016-03-01 13:45:42 -080091
Mark Salyzyn6e315682017-03-09 08:09:43 -080092 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 Salyzyn376e9e62016-03-01 13:45:42 -080099
Mark Salyzyn6e315682017-03-09 08:09:43 -0800100 return ret;
Mark Salyzyn376e9e62016-03-01 13:45:42 -0800101}