logger_read.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  1. /*
  2. ** Copyright 2013-2014, 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. #include "log/log_read.h"
  17. #include <errno.h>
  18. #include <fcntl.h>
  19. #include <pthread.h>
  20. #include <sched.h>
  21. #include <stddef.h>
  22. #include <stdlib.h>
  23. #include <string.h>
  24. #include <unistd.h>
  25. #include <android/log.h>
  26. #include <cutils/list.h>
  27. #include <private/android_filesystem_config.h>
  28. #include "config_read.h"
  29. #include "log_portability.h"
  30. #include "logger.h"
  31. /* android_logger_alloc unimplemented, no use case */
  32. /* android_logger_free not exported */
  33. static void android_logger_free(struct logger* logger) {
  34. struct android_log_logger* logger_internal = (struct android_log_logger*)logger;
  35. if (!logger_internal) {
  36. return;
  37. }
  38. list_remove(&logger_internal->node);
  39. free(logger_internal);
  40. }
  41. /* android_logger_alloc unimplemented, no use case */
  42. /* method for getting the associated sublog id */
  43. log_id_t android_logger_get_id(struct logger* logger) {
  44. return ((struct android_log_logger*)logger)->logId;
  45. }
  46. static int init_transport_context(struct android_log_logger_list* logger_list) {
  47. struct android_log_transport_read* transport;
  48. struct listnode* node;
  49. if (!logger_list) {
  50. return -EINVAL;
  51. }
  52. if (list_empty(&logger_list->logger)) {
  53. return -EINVAL;
  54. }
  55. if (!list_empty(&logger_list->transport)) {
  56. return 0;
  57. }
  58. __android_log_lock();
  59. /* mini __write_to_log_initialize() to populate transports */
  60. if (list_empty(&__android_log_transport_read) && list_empty(&__android_log_persist_read)) {
  61. __android_log_config_read();
  62. }
  63. __android_log_unlock();
  64. node = (logger_list->mode & ANDROID_LOG_PSTORE) ? &__android_log_persist_read
  65. : &__android_log_transport_read;
  66. read_transport_for_each(transport, node) {
  67. struct android_log_transport_context* transp;
  68. struct android_log_logger* logger;
  69. unsigned logMask = 0;
  70. logger_for_each(logger, logger_list) {
  71. log_id_t logId = logger->logId;
  72. if ((logId == LOG_ID_SECURITY) && (__android_log_uid() != AID_SYSTEM)) {
  73. continue;
  74. }
  75. if (transport->read && (!transport->available || (transport->available(logId) >= 0))) {
  76. logMask |= 1 << logId;
  77. }
  78. }
  79. if (!logMask) {
  80. continue;
  81. }
  82. transp = static_cast<android_log_transport_context*>(calloc(1, sizeof(*transp)));
  83. if (!transp) {
  84. return -ENOMEM;
  85. }
  86. transp->parent = logger_list;
  87. transp->transport = transport;
  88. transp->logMask = logMask;
  89. transp->ret = 1;
  90. list_add_tail(&logger_list->transport, &transp->node);
  91. }
  92. if (list_empty(&logger_list->transport)) {
  93. return -ENODEV;
  94. }
  95. return 0;
  96. }
  97. #define LOGGER_FUNCTION(logger, def, func, args...) \
  98. ssize_t ret = -EINVAL; \
  99. struct android_log_transport_context* transp; \
  100. struct android_log_logger* logger_internal = (struct android_log_logger*)(logger); \
  101. \
  102. if (!logger_internal) { \
  103. return ret; \
  104. } \
  105. ret = init_transport_context(logger_internal->parent); \
  106. if (ret < 0) { \
  107. return ret; \
  108. } \
  109. \
  110. ret = (def); \
  111. transport_context_for_each(transp, logger_internal->parent) { \
  112. if ((transp->logMask & (1 << logger_internal->logId)) && transp->transport && \
  113. transp->transport->func) { \
  114. ssize_t retval = (transp->transport->func)(logger_internal, transp, ##args); \
  115. if ((ret >= 0) || (ret == (def))) { \
  116. ret = retval; \
  117. } \
  118. } \
  119. } \
  120. return ret
  121. int android_logger_clear(struct logger* logger) {
  122. LOGGER_FUNCTION(logger, -ENODEV, clear);
  123. }
  124. /* returns the total size of the log's ring buffer */
  125. long android_logger_get_log_size(struct logger* logger) {
  126. LOGGER_FUNCTION(logger, -ENODEV, getSize);
  127. }
  128. int android_logger_set_log_size(struct logger* logger, unsigned long size) {
  129. LOGGER_FUNCTION(logger, -ENODEV, setSize, size);
  130. }
  131. /*
  132. * returns the readable size of the log's ring buffer (that is, amount of the
  133. * log consumed)
  134. */
  135. long android_logger_get_log_readable_size(struct logger* logger) {
  136. LOGGER_FUNCTION(logger, -ENODEV, getReadableSize);
  137. }
  138. /*
  139. * returns the logger version
  140. */
  141. int android_logger_get_log_version(struct logger* logger) {
  142. LOGGER_FUNCTION(logger, 4, version);
  143. }
  144. #define LOGGER_LIST_FUNCTION(logger_list, def, func, args...) \
  145. struct android_log_transport_context* transp; \
  146. struct android_log_logger_list* logger_list_internal = \
  147. (struct android_log_logger_list*)(logger_list); \
  148. \
  149. ssize_t ret = init_transport_context(logger_list_internal); \
  150. if (ret < 0) { \
  151. return ret; \
  152. } \
  153. \
  154. ret = (def); \
  155. transport_context_for_each(transp, logger_list_internal) { \
  156. if (transp->transport && (transp->transport->func)) { \
  157. ssize_t retval = (transp->transport->func)(logger_list_internal, transp, ##args); \
  158. if ((ret >= 0) || (ret == (def))) { \
  159. ret = retval; \
  160. } \
  161. } \
  162. } \
  163. return ret
  164. /*
  165. * returns statistics
  166. */
  167. ssize_t android_logger_get_statistics(struct logger_list* logger_list, char* buf, size_t len) {
  168. LOGGER_LIST_FUNCTION(logger_list, -ENODEV, getStats, buf, len);
  169. }
  170. ssize_t android_logger_get_prune_list(struct logger_list* logger_list, char* buf, size_t len) {
  171. LOGGER_LIST_FUNCTION(logger_list, -ENODEV, getPrune, buf, len);
  172. }
  173. int android_logger_set_prune_list(struct logger_list* logger_list, char* buf, size_t len) {
  174. LOGGER_LIST_FUNCTION(logger_list, -ENODEV, setPrune, buf, len);
  175. }
  176. struct logger_list* android_logger_list_alloc(int mode, unsigned int tail, pid_t pid) {
  177. struct android_log_logger_list* logger_list;
  178. logger_list = static_cast<android_log_logger_list*>(calloc(1, sizeof(*logger_list)));
  179. if (!logger_list) {
  180. return NULL;
  181. }
  182. list_init(&logger_list->logger);
  183. list_init(&logger_list->transport);
  184. logger_list->mode = mode;
  185. logger_list->tail = tail;
  186. logger_list->pid = pid;
  187. return (struct logger_list*)logger_list;
  188. }
  189. struct logger_list* android_logger_list_alloc_time(int mode, log_time start, pid_t pid) {
  190. struct android_log_logger_list* logger_list;
  191. logger_list = static_cast<android_log_logger_list*>(calloc(1, sizeof(*logger_list)));
  192. if (!logger_list) {
  193. return NULL;
  194. }
  195. list_init(&logger_list->logger);
  196. list_init(&logger_list->transport);
  197. logger_list->mode = mode;
  198. logger_list->start = start;
  199. logger_list->pid = pid;
  200. return (struct logger_list*)logger_list;
  201. }
  202. /* android_logger_list_register unimplemented, no use case */
  203. /* android_logger_list_unregister unimplemented, no use case */
  204. /* Open the named log and add it to the logger list */
  205. struct logger* android_logger_open(struct logger_list* logger_list, log_id_t logId) {
  206. struct android_log_logger_list* logger_list_internal =
  207. (struct android_log_logger_list*)logger_list;
  208. struct android_log_logger* logger;
  209. if (!logger_list_internal || (logId >= LOG_ID_MAX)) {
  210. goto err;
  211. }
  212. logger_for_each(logger, logger_list_internal) {
  213. if (logger->logId == logId) {
  214. goto ok;
  215. }
  216. }
  217. logger = static_cast<android_log_logger*>(calloc(1, sizeof(*logger)));
  218. if (!logger) {
  219. goto err;
  220. }
  221. logger->logId = logId;
  222. list_add_tail(&logger_list_internal->logger, &logger->node);
  223. logger->parent = logger_list_internal;
  224. /* Reset known transports to re-evaluate, we just added one */
  225. while (!list_empty(&logger_list_internal->transport)) {
  226. struct listnode* node = list_head(&logger_list_internal->transport);
  227. struct android_log_transport_context* transp =
  228. node_to_item(node, struct android_log_transport_context, node);
  229. list_remove(&transp->node);
  230. free(transp);
  231. }
  232. goto ok;
  233. err:
  234. logger = NULL;
  235. ok:
  236. return (struct logger*)logger;
  237. }
  238. /* Open the single named log and make it part of a new logger list */
  239. struct logger_list* android_logger_list_open(log_id_t logId, int mode, unsigned int tail,
  240. pid_t pid) {
  241. struct logger_list* logger_list = android_logger_list_alloc(mode, tail, pid);
  242. if (!logger_list) {
  243. return NULL;
  244. }
  245. if (!android_logger_open(logger_list, logId)) {
  246. android_logger_list_free(logger_list);
  247. return NULL;
  248. }
  249. return logger_list;
  250. }
  251. /* Validate log_msg packet, read function has already been null checked */
  252. static int android_transport_read(struct android_log_logger_list* logger_list,
  253. struct android_log_transport_context* transp,
  254. struct log_msg* log_msg) {
  255. int ret = (*transp->transport->read)(logger_list, transp, log_msg);
  256. if (ret > (int)sizeof(*log_msg)) {
  257. ret = sizeof(*log_msg);
  258. }
  259. transp->ret = ret;
  260. /* propagate errors, or make sure len & hdr_size members visible */
  261. if (ret < (int)(sizeof(log_msg->entry.len) + sizeof(log_msg->entry.hdr_size))) {
  262. if (ret >= (int)sizeof(log_msg->entry.len)) {
  263. log_msg->entry.len = 0;
  264. }
  265. return ret;
  266. }
  267. /* hdr_size correction (logger_entry -> logger_entry_v2+ conversion) */
  268. if (log_msg->entry_v2.hdr_size == 0) {
  269. log_msg->entry_v2.hdr_size = sizeof(struct logger_entry);
  270. }
  271. if ((log_msg->entry_v2.hdr_size < sizeof(log_msg->entry_v1)) ||
  272. (log_msg->entry_v2.hdr_size > sizeof(log_msg->entry))) {
  273. return -EINVAL;
  274. }
  275. /* len validation */
  276. if (ret <= log_msg->entry_v2.hdr_size) {
  277. log_msg->entry.len = 0;
  278. } else {
  279. log_msg->entry.len = ret - log_msg->entry_v2.hdr_size;
  280. }
  281. return ret;
  282. }
  283. /* Read from the selected logs */
  284. int android_logger_list_read(struct logger_list* logger_list, struct log_msg* log_msg) {
  285. struct android_log_transport_context* transp;
  286. struct android_log_logger_list* logger_list_internal =
  287. (struct android_log_logger_list*)logger_list;
  288. int ret = init_transport_context(logger_list_internal);
  289. if (ret < 0) {
  290. return ret;
  291. }
  292. /* at least one transport */
  293. transp = node_to_item(logger_list_internal->transport.next, struct android_log_transport_context,
  294. node);
  295. /* more than one transport? */
  296. if (transp->node.next != &logger_list_internal->transport) {
  297. /* Poll and merge sort the entries if from multiple transports */
  298. struct android_log_transport_context* oldest = NULL;
  299. int ret;
  300. int polled = 0;
  301. do {
  302. if (polled) {
  303. sched_yield();
  304. }
  305. ret = -1000;
  306. polled = 0;
  307. do {
  308. int retval = transp->ret;
  309. if ((retval > 0) && !transp->logMsg.entry.len) {
  310. if (!transp->transport->read) {
  311. retval = transp->ret = 0;
  312. } else if ((logger_list_internal->mode & ANDROID_LOG_NONBLOCK) ||
  313. !transp->transport->poll) {
  314. retval = android_transport_read(logger_list_internal, transp, &transp->logMsg);
  315. } else {
  316. int pollval = (*transp->transport->poll)(logger_list_internal, transp);
  317. if (pollval <= 0) {
  318. sched_yield();
  319. pollval = (*transp->transport->poll)(logger_list_internal, transp);
  320. }
  321. polled = 1;
  322. if (pollval < 0) {
  323. if ((pollval == -EINTR) || (pollval == -EAGAIN)) {
  324. return -EAGAIN;
  325. }
  326. retval = transp->ret = pollval;
  327. } else if (pollval > 0) {
  328. retval = android_transport_read(logger_list_internal, transp, &transp->logMsg);
  329. }
  330. }
  331. }
  332. if (ret < retval) {
  333. ret = retval;
  334. }
  335. if ((transp->ret > 0) && transp->logMsg.entry.len &&
  336. (!oldest || (oldest->logMsg.entry.sec > transp->logMsg.entry.sec) ||
  337. ((oldest->logMsg.entry.sec == transp->logMsg.entry.sec) &&
  338. (oldest->logMsg.entry.nsec > transp->logMsg.entry.nsec)))) {
  339. oldest = transp;
  340. }
  341. transp = node_to_item(transp->node.next, struct android_log_transport_context, node);
  342. } while (transp != node_to_item(&logger_list_internal->transport,
  343. struct android_log_transport_context, node));
  344. if (!oldest && (logger_list_internal->mode & ANDROID_LOG_NONBLOCK)) {
  345. return (ret < 0) ? ret : -EAGAIN;
  346. }
  347. transp = node_to_item(logger_list_internal->transport.next,
  348. struct android_log_transport_context, node);
  349. } while (!oldest && (ret > 0));
  350. if (!oldest) {
  351. return ret;
  352. }
  353. // ret is a positive value less than sizeof(struct log_msg)
  354. ret = oldest->ret;
  355. if (ret < oldest->logMsg.entry.hdr_size) {
  356. // zero truncated header fields.
  357. memset(
  358. log_msg, 0,
  359. (oldest->logMsg.entry.hdr_size > sizeof(oldest->logMsg) ? sizeof(oldest->logMsg)
  360. : oldest->logMsg.entry.hdr_size));
  361. }
  362. memcpy(log_msg, &oldest->logMsg, ret);
  363. oldest->logMsg.entry.len = 0; /* Mark it as copied */
  364. return ret;
  365. }
  366. /* if only one, no need to copy into transport_context and merge-sort */
  367. return android_transport_read(logger_list_internal, transp, log_msg);
  368. }
  369. /* Close all the logs */
  370. void android_logger_list_free(struct logger_list* logger_list) {
  371. struct android_log_logger_list* logger_list_internal =
  372. (struct android_log_logger_list*)logger_list;
  373. if (logger_list_internal == NULL) {
  374. return;
  375. }
  376. while (!list_empty(&logger_list_internal->transport)) {
  377. struct listnode* node = list_head(&logger_list_internal->transport);
  378. struct android_log_transport_context* transp =
  379. node_to_item(node, struct android_log_transport_context, node);
  380. if (transp->transport && transp->transport->close) {
  381. (*transp->transport->close)(logger_list_internal, transp);
  382. }
  383. list_remove(&transp->node);
  384. free(transp);
  385. }
  386. while (!list_empty(&logger_list_internal->logger)) {
  387. struct listnode* node = list_head(&logger_list_internal->logger);
  388. struct android_log_logger* logger = node_to_item(node, struct android_log_logger, node);
  389. android_logger_free((struct logger*)logger);
  390. }
  391. free(logger_list_internal);
  392. }