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,
31                     struct iovec *vec, 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.private = &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        char buf[sizeof("/dev/log_security")];
50        snprintf(buf, sizeof(buf), "/dev/log_%s", android_log_id_to_name(i));
51        logFds[i] = fakeLogOpen(buf, O_WRONLY);
52    }
53    return 0;
54}
55
56static void fakeClose() {
57    int i;
58
59    for (i = 0; i < LOG_ID_MAX; i++) {
60        fakeLogClose(logFds[i]);
61        logFds[i] = -1;
62    }
63}
64
65static int fakeWrite(log_id_t log_id, struct timespec *ts __unused,
66                      struct iovec *vec, size_t nr)
67{
68    ssize_t ret;
69    int logFd;
70
71    if (/*(int)log_id >= 0 &&*/ (int)log_id >= (int)LOG_ID_MAX) {
72        return -EBADF;
73    }
74
75    logFd = logFds[(int)log_id];
76    ret = TEMP_FAILURE_RETRY(fakeLogWritev(logFd, vec, nr));
77    if (ret < 0) {
78        ret = -errno;
79    }
80
81    return ret;
82}
83