• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
28 static int fakeOpen();
29 static void fakeClose();
30 static int fakeWrite(log_id_t log_id, struct timespec* ts, struct iovec* vec, size_t nr);
31 
32 static int logFds[(int)LOG_ID_MAX] = {-1, -1, -1, -1, -1, -1};
33 
34 struct android_log_transport_write fakeLoggerWrite = {
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,
42 };
43 
fakeOpen()44 static int fakeOpen() {
45   int i;
46 
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;
55     }
56     snprintf(buf, sizeof(buf), "/dev/log_%s", android_log_id_to_name(static_cast<log_id_t>(i)));
57     logFds[i] = fakeLogOpen(buf);
58     if (logFds[i] < 0) {
59       fprintf(stderr, "fakeLogOpen(%s) failed\n", buf);
60     }
61   }
62   return 0;
63 }
64 
fakeClose()65 static void fakeClose() {
66   int i;
67 
68   for (i = 0; i < LOG_ID_MAX; i++) {
69     fakeLogClose(logFds[i]);
70     logFds[i] = -1;
71   }
72 }
73 
fakeWrite(log_id_t log_id,struct timespec *,struct iovec * vec,size_t nr)74 static int fakeWrite(log_id_t log_id, struct timespec*, struct iovec* vec, size_t nr) {
75   ssize_t ret;
76   size_t i;
77   int logFd, len;
78 
79   if (/*(int)log_id >= 0 &&*/ (int)log_id >= (int)LOG_ID_MAX) {
80     return -EINVAL;
81   }
82 
83   len = 0;
84   for (i = 0; i < nr; ++i) {
85     len += vec[i].iov_len;
86   }
87 
88   if (len > LOGGER_ENTRY_MAX_PAYLOAD) {
89     len = LOGGER_ENTRY_MAX_PAYLOAD;
90   }
91 
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   }
99 
100   return ret;
101 }
102