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
21#include <log/log.h>
22
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();
30static int fakeWrite(log_id_t log_id, struct timespec* ts, struct iovec* vec,
31                     size_t nr);
32
33static int logFds[(int)LOG_ID_MAX] = { -1, -1, -1, -1, -1, -1 };
34
35LIBLOG_HIDDEN struct android_log_transport_write fakeLoggerWrite = {
36  .node = { &fakeLoggerWrite.node, &fakeLoggerWrite.node },
37  .context.priv = &logFds,
38  .name = "fake",
39  .available = NULL,
40  .open = fakeOpen,
41  .close = fakeClose,
42  .write = fakeWrite,
43};
44
45static int fakeOpen() {
46  int i;
47
48  for (i = 0; i < LOG_ID_MAX; i++) {
49    /*
50     * Known maximum size string, plus an 8 character margin to deal with
51     * possible independent changes to android_log_id_to_name().
52     */
53    char buf[sizeof("/dev/log_security") + 8];
54    if (logFds[i] >= 0) {
55      continue;
56    }
57    snprintf(buf, sizeof(buf), "/dev/log_%s", android_log_id_to_name(i));
58    logFds[i] = fakeLogOpen(buf, O_WRONLY);
59    if (logFds[i] < 0) {
60      fprintf(stderr, "fakeLogOpen(%s, O_WRONLY) failed\n", buf);
61    }
62  }
63  return 0;
64}
65
66static void fakeClose() {
67  int i;
68
69  for (i = 0; i < LOG_ID_MAX; i++) {
70    fakeLogClose(logFds[i]);
71    logFds[i] = -1;
72  }
73}
74
75static int fakeWrite(log_id_t log_id, struct timespec* ts __unused,
76                     struct iovec* vec, size_t nr) {
77  ssize_t ret;
78  size_t i;
79  int logFd, len;
80
81  if (/*(int)log_id >= 0 &&*/ (int)log_id >= (int)LOG_ID_MAX) {
82    return -EINVAL;
83  }
84
85  len = 0;
86  for (i = 0; i < nr; ++i) {
87    len += vec[i].iov_len;
88  }
89
90  if (len > LOGGER_ENTRY_MAX_PAYLOAD) {
91    len = LOGGER_ENTRY_MAX_PAYLOAD;
92  }
93
94  logFd = logFds[(int)log_id];
95  ret = TEMP_FAILURE_RETRY(fakeLogWritev(logFd, vec, nr));
96  if (ret < 0) {
97    ret = -errno;
98  } else if (ret > len) {
99    ret = len;
100  }
101
102  return ret;
103}
104