1 /*
2 * Copyright (C) 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 #pragma once
18
19 #include <stdatomic.h>
20 #include <stdbool.h>
21
22 #include <cutils/list.h>
23 #include <log/log.h>
24
25 #include "log_portability.h"
26 #include "uio.h"
27
28 __BEGIN_DECLS
29
30 /* Union, sock or fd of zero is not allowed unless static initialized */
31 union android_log_context_union {
32 void* priv;
33 atomic_int sock;
34 atomic_int fd;
35 struct listnode* node;
36 atomic_uintptr_t atomic_pointer;
37 };
38
39 struct android_log_transport_write {
40 struct listnode node;
41 const char* name; /* human name to describe the transport */
42 unsigned logMask; /* mask cache of available() success */
43 union android_log_context_union context; /* Initialized by static allocation */
44
45 int (*available)(log_id_t logId); /* Does not cause resources to be taken */
46 int (*open)(); /* can be called multiple times, reusing current resources */
47 void (*close)(); /* free up resources */
48 /* write log to transport, returns number of bytes propagated, or -errno */
49 int (*write)(log_id_t logId, struct timespec* ts, struct iovec* vec,
50 size_t nr);
51 };
52
53 struct android_log_logger_list;
54 struct android_log_transport_context;
55 struct android_log_logger;
56
57 struct android_log_transport_read {
58 struct listnode node;
59 const char* name; /* human name to describe the transport */
60
61 /* Does not cause resources to be taken */
62 int (*available)(log_id_t logId);
63 int (*version)(struct android_log_logger* logger,
64 struct android_log_transport_context* transp);
65 /* Release resources taken by the following interfaces */
66 void (*close)(struct android_log_logger_list* logger_list,
67 struct android_log_transport_context* transp);
68 /*
69 * Expect all to instantiate open automagically on any call,
70 * so we do not have an explicit open call.
71 */
72 int (*read)(struct android_log_logger_list* logger_list,
73 struct android_log_transport_context* transp,
74 struct log_msg* log_msg);
75 /* Must only be called if not ANDROID_LOG_NONBLOCK (blocking) */
76 int (*poll)(struct android_log_logger_list* logger_list,
77 struct android_log_transport_context* transp);
78
79 int (*clear)(struct android_log_logger* logger,
80 struct android_log_transport_context* transp);
81 ssize_t (*setSize)(struct android_log_logger* logger,
82 struct android_log_transport_context* transp, size_t size);
83 ssize_t (*getSize)(struct android_log_logger* logger,
84 struct android_log_transport_context* transp);
85 ssize_t (*getReadableSize)(struct android_log_logger* logger,
86 struct android_log_transport_context* transp);
87
88 ssize_t (*getPrune)(struct android_log_logger_list* logger_list,
89 struct android_log_transport_context* transp, char* buf,
90 size_t len);
91 ssize_t (*setPrune)(struct android_log_logger_list* logger_list,
92 struct android_log_transport_context* transp, char* buf,
93 size_t len);
94 ssize_t (*getStats)(struct android_log_logger_list* logger_list,
95 struct android_log_transport_context* transp, char* buf,
96 size_t len);
97 };
98
99 struct android_log_logger_list {
100 struct listnode logger;
101 struct listnode transport;
102 int mode;
103 unsigned int tail;
104 log_time start;
105 pid_t pid;
106 };
107
108 struct android_log_logger {
109 struct listnode node;
110 struct android_log_logger_list* parent;
111
112 log_id_t logId;
113 };
114
115 struct android_log_transport_context {
116 struct listnode node;
117 union android_log_context_union context; /* zero init per-transport context */
118 struct android_log_logger_list* parent;
119
120 struct android_log_transport_read* transport;
121 unsigned logMask; /* mask of requested log buffers */
122 int ret; /* return value associated with following data */
123 struct log_msg logMsg; /* peek at upcoming data, valid if logMsg.len != 0 */
124 };
125
126 /* assumes caller has structures read-locked, single threaded, or fenced */
127 #define transport_context_for_each(transp, logger_list) \
128 for ((transp) = node_to_item((logger_list)->transport.next, \
129 struct android_log_transport_context, node); \
130 ((transp) != node_to_item(&(logger_list)->transport, \
131 struct android_log_transport_context, node)) && \
132 ((transp)->parent == (logger_list)); \
133 (transp) = node_to_item((transp)->node.next, \
134 struct android_log_transport_context, node))
135
136 #define logger_for_each(logp, logger_list) \
137 for ((logp) = node_to_item((logger_list)->logger.next, \
138 struct android_log_logger, node); \
139 ((logp) != node_to_item(&(logger_list)->logger, \
140 struct android_log_logger, node)) && \
141 ((logp)->parent == (logger_list)); \
142 (logp) = \
143 node_to_item((logp)->node.next, struct android_log_logger, node))
144
145 /* OS specific dribs and drabs */
146
147 #if defined(_WIN32)
148 #include <private/android_filesystem_config.h>
149 typedef uint32_t uid_t;
__android_log_uid()150 static inline uid_t __android_log_uid() {
151 return AID_SYSTEM;
152 }
153 #else
__android_log_uid()154 static inline uid_t __android_log_uid() {
155 return getuid();
156 }
157 #endif
158
159 void __android_log_lock();
160 int __android_log_trylock();
161 void __android_log_unlock();
162
163 extern int __android_log_transport;
164
165 __END_DECLS
166