pmsg_reader.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589
  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. #include <ctype.h>
  17. #include <errno.h>
  18. #include <fcntl.h>
  19. #include <stdbool.h>
  20. #include <stdlib.h>
  21. #include <string.h>
  22. #include <sys/types.h>
  23. #include <private/android_filesystem_config.h>
  24. #include <private/android_logger.h>
  25. #include "config_read.h"
  26. #include "logger.h"
  27. static int pmsgAvailable(log_id_t logId);
  28. static int pmsgVersion(struct android_log_logger* logger,
  29. struct android_log_transport_context* transp);
  30. static int pmsgRead(struct android_log_logger_list* logger_list,
  31. struct android_log_transport_context* transp, struct log_msg* log_msg);
  32. static void pmsgClose(struct android_log_logger_list* logger_list,
  33. struct android_log_transport_context* transp);
  34. static int pmsgClear(struct android_log_logger* logger,
  35. struct android_log_transport_context* transp);
  36. struct android_log_transport_read pmsgLoggerRead = {
  37. .node = {&pmsgLoggerRead.node, &pmsgLoggerRead.node},
  38. .name = "pmsg",
  39. .available = pmsgAvailable,
  40. .version = pmsgVersion,
  41. .read = pmsgRead,
  42. .poll = NULL,
  43. .close = pmsgClose,
  44. .clear = pmsgClear,
  45. .setSize = NULL,
  46. .getSize = NULL,
  47. .getReadableSize = NULL,
  48. .getPrune = NULL,
  49. .setPrune = NULL,
  50. .getStats = NULL,
  51. };
  52. static int pmsgAvailable(log_id_t logId) {
  53. if (logId > LOG_ID_SECURITY) {
  54. return -EINVAL;
  55. }
  56. if (access("/dev/pmsg0", W_OK) == 0) {
  57. return 0;
  58. }
  59. return -EBADF;
  60. }
  61. /* Determine the credentials of the caller */
  62. static bool uid_has_log_permission(uid_t uid) {
  63. return (uid == AID_SYSTEM) || (uid == AID_LOG) || (uid == AID_ROOT) || (uid == AID_LOGD);
  64. }
  65. static uid_t get_best_effective_uid() {
  66. uid_t euid;
  67. uid_t uid;
  68. gid_t gid;
  69. ssize_t i;
  70. static uid_t last_uid = (uid_t)-1;
  71. if (last_uid != (uid_t)-1) {
  72. return last_uid;
  73. }
  74. uid = __android_log_uid();
  75. if (uid_has_log_permission(uid)) {
  76. return last_uid = uid;
  77. }
  78. euid = geteuid();
  79. if (uid_has_log_permission(euid)) {
  80. return last_uid = euid;
  81. }
  82. gid = getgid();
  83. if (uid_has_log_permission(gid)) {
  84. return last_uid = gid;
  85. }
  86. gid = getegid();
  87. if (uid_has_log_permission(gid)) {
  88. return last_uid = gid;
  89. }
  90. i = getgroups((size_t)0, NULL);
  91. if (i > 0) {
  92. gid_t list[i];
  93. getgroups(i, list);
  94. while (--i >= 0) {
  95. if (uid_has_log_permission(list[i])) {
  96. return last_uid = list[i];
  97. }
  98. }
  99. }
  100. return last_uid = uid;
  101. }
  102. static int pmsgClear(struct android_log_logger* logger __unused,
  103. struct android_log_transport_context* transp __unused) {
  104. if (uid_has_log_permission(get_best_effective_uid())) {
  105. return unlink("/sys/fs/pstore/pmsg-ramoops-0");
  106. }
  107. errno = EPERM;
  108. return -1;
  109. }
  110. /*
  111. * returns the logger version
  112. */
  113. static int pmsgVersion(struct android_log_logger* logger __unused,
  114. struct android_log_transport_context* transp __unused) {
  115. return 4;
  116. }
  117. static int pmsgRead(struct android_log_logger_list* logger_list,
  118. struct android_log_transport_context* transp, struct log_msg* log_msg) {
  119. ssize_t ret;
  120. off_t current, next;
  121. uid_t uid;
  122. struct android_log_logger* logger;
  123. struct __attribute__((__packed__)) {
  124. android_pmsg_log_header_t p;
  125. android_log_header_t l;
  126. uint8_t prio;
  127. } buf;
  128. static uint8_t preread_count;
  129. bool is_system;
  130. memset(log_msg, 0, sizeof(*log_msg));
  131. if (atomic_load(&transp->context.fd) <= 0) {
  132. int i, fd = open("/sys/fs/pstore/pmsg-ramoops-0", O_RDONLY | O_CLOEXEC);
  133. if (fd < 0) {
  134. return -errno;
  135. }
  136. if (fd == 0) { /* Argggg */
  137. fd = open("/sys/fs/pstore/pmsg-ramoops-0", O_RDONLY | O_CLOEXEC);
  138. close(0);
  139. if (fd < 0) {
  140. return -errno;
  141. }
  142. }
  143. i = atomic_exchange(&transp->context.fd, fd);
  144. if ((i > 0) && (i != fd)) {
  145. close(i);
  146. }
  147. preread_count = 0;
  148. }
  149. while (1) {
  150. int fd;
  151. if (preread_count < sizeof(buf)) {
  152. fd = atomic_load(&transp->context.fd);
  153. if (fd <= 0) {
  154. return -EBADF;
  155. }
  156. ret = TEMP_FAILURE_RETRY(read(fd, &buf.p.magic + preread_count, sizeof(buf) - preread_count));
  157. if (ret < 0) {
  158. return -errno;
  159. }
  160. preread_count += ret;
  161. }
  162. if (preread_count != sizeof(buf)) {
  163. return preread_count ? -EIO : -EAGAIN;
  164. }
  165. if ((buf.p.magic != LOGGER_MAGIC) || (buf.p.len <= sizeof(buf)) ||
  166. (buf.p.len > (sizeof(buf) + LOGGER_ENTRY_MAX_PAYLOAD)) || (buf.l.id >= LOG_ID_MAX) ||
  167. (buf.l.realtime.tv_nsec >= NS_PER_SEC) ||
  168. ((buf.l.id != LOG_ID_EVENTS) && (buf.l.id != LOG_ID_SECURITY) &&
  169. ((buf.prio == ANDROID_LOG_UNKNOWN) || (buf.prio == ANDROID_LOG_DEFAULT) ||
  170. (buf.prio >= ANDROID_LOG_SILENT)))) {
  171. do {
  172. memmove(&buf.p.magic, &buf.p.magic + 1, --preread_count);
  173. } while (preread_count && (buf.p.magic != LOGGER_MAGIC));
  174. continue;
  175. }
  176. preread_count = 0;
  177. if ((transp->logMask & (1 << buf.l.id)) &&
  178. ((!logger_list->start.tv_sec && !logger_list->start.tv_nsec) ||
  179. ((logger_list->start.tv_sec <= buf.l.realtime.tv_sec) &&
  180. ((logger_list->start.tv_sec != buf.l.realtime.tv_sec) ||
  181. (logger_list->start.tv_nsec <= buf.l.realtime.tv_nsec)))) &&
  182. (!logger_list->pid || (logger_list->pid == buf.p.pid))) {
  183. uid = get_best_effective_uid();
  184. is_system = uid_has_log_permission(uid);
  185. if (is_system || (uid == buf.p.uid)) {
  186. char* msg = is_system ? log_msg->entry_v4.msg : log_msg->entry_v3.msg;
  187. *msg = buf.prio;
  188. fd = atomic_load(&transp->context.fd);
  189. if (fd <= 0) {
  190. return -EBADF;
  191. }
  192. ret = TEMP_FAILURE_RETRY(read(fd, msg + sizeof(buf.prio), buf.p.len - sizeof(buf)));
  193. if (ret < 0) {
  194. return -errno;
  195. }
  196. if (ret != (ssize_t)(buf.p.len - sizeof(buf))) {
  197. return -EIO;
  198. }
  199. log_msg->entry_v4.len = buf.p.len - sizeof(buf) + sizeof(buf.prio);
  200. log_msg->entry_v4.hdr_size =
  201. is_system ? sizeof(log_msg->entry_v4) : sizeof(log_msg->entry_v3);
  202. log_msg->entry_v4.pid = buf.p.pid;
  203. log_msg->entry_v4.tid = buf.l.tid;
  204. log_msg->entry_v4.sec = buf.l.realtime.tv_sec;
  205. log_msg->entry_v4.nsec = buf.l.realtime.tv_nsec;
  206. log_msg->entry_v4.lid = buf.l.id;
  207. if (is_system) {
  208. log_msg->entry_v4.uid = buf.p.uid;
  209. }
  210. return ret + sizeof(buf.prio) + log_msg->entry_v4.hdr_size;
  211. }
  212. }
  213. fd = atomic_load(&transp->context.fd);
  214. if (fd <= 0) {
  215. return -EBADF;
  216. }
  217. current = TEMP_FAILURE_RETRY(lseek(fd, (off_t)0, SEEK_CUR));
  218. if (current < 0) {
  219. return -errno;
  220. }
  221. fd = atomic_load(&transp->context.fd);
  222. if (fd <= 0) {
  223. return -EBADF;
  224. }
  225. next = TEMP_FAILURE_RETRY(lseek(fd, (off_t)(buf.p.len - sizeof(buf)), SEEK_CUR));
  226. if (next < 0) {
  227. return -errno;
  228. }
  229. if ((next - current) != (ssize_t)(buf.p.len - sizeof(buf))) {
  230. return -EIO;
  231. }
  232. }
  233. }
  234. static void pmsgClose(struct android_log_logger_list* logger_list __unused,
  235. struct android_log_transport_context* transp) {
  236. int fd = atomic_exchange(&transp->context.fd, 0);
  237. if (fd > 0) {
  238. close(fd);
  239. }
  240. }
  241. static void* realloc_or_free(void* ptr, size_t new_size) {
  242. void* result = realloc(ptr, new_size);
  243. if (!result) {
  244. free(ptr);
  245. }
  246. return result;
  247. }
  248. ssize_t __android_log_pmsg_file_read(log_id_t logId, char prio, const char* prefix,
  249. __android_log_pmsg_file_read_fn fn, void* arg) {
  250. ssize_t ret;
  251. struct android_log_logger_list logger_list;
  252. struct android_log_transport_context transp;
  253. struct content {
  254. struct listnode node;
  255. union {
  256. struct logger_entry_v4 entry;
  257. struct logger_entry_v4 entry_v4;
  258. struct logger_entry_v3 entry_v3;
  259. struct logger_entry_v2 entry_v2;
  260. struct logger_entry entry_v1;
  261. };
  262. } * content;
  263. struct names {
  264. struct listnode node;
  265. struct listnode content;
  266. log_id_t id;
  267. char prio;
  268. char name[];
  269. } * names;
  270. struct listnode name_list;
  271. struct listnode *node, *n;
  272. size_t len, prefix_len;
  273. if (!fn) {
  274. return -EINVAL;
  275. }
  276. /* Add just enough clues in logger_list and transp to make API function */
  277. memset(&logger_list, 0, sizeof(logger_list));
  278. memset(&transp, 0, sizeof(transp));
  279. logger_list.mode = ANDROID_LOG_PSTORE | ANDROID_LOG_NONBLOCK | ANDROID_LOG_RDONLY;
  280. transp.logMask = (unsigned)-1;
  281. if (logId != LOG_ID_ANY) {
  282. transp.logMask = (1 << logId);
  283. }
  284. transp.logMask &= ~((1 << LOG_ID_KERNEL) | (1 << LOG_ID_EVENTS) | (1 << LOG_ID_SECURITY));
  285. if (!transp.logMask) {
  286. return -EINVAL;
  287. }
  288. /* Initialize name list */
  289. list_init(&name_list);
  290. ret = SSIZE_MAX;
  291. /* Validate incoming prefix, shift until it contains only 0 or 1 : or / */
  292. prefix_len = 0;
  293. if (prefix) {
  294. const char *prev = NULL, *last = NULL, *cp = prefix;
  295. while ((cp = strpbrk(cp, "/:"))) {
  296. prev = last;
  297. last = cp;
  298. cp = cp + 1;
  299. }
  300. if (prev) {
  301. prefix = prev + 1;
  302. }
  303. prefix_len = strlen(prefix);
  304. }
  305. /* Read the file content */
  306. while (pmsgRead(&logger_list, &transp, &transp.logMsg) > 0) {
  307. const char* cp;
  308. size_t hdr_size = transp.logMsg.entry.hdr_size ? transp.logMsg.entry.hdr_size
  309. : sizeof(transp.logMsg.entry_v1);
  310. char* msg = (char*)&transp.logMsg + hdr_size;
  311. const char* split = NULL;
  312. if ((hdr_size < sizeof(transp.logMsg.entry_v1)) || (hdr_size > sizeof(transp.logMsg.entry))) {
  313. continue;
  314. }
  315. /* Check for invalid sequence number */
  316. if ((transp.logMsg.entry.nsec % ANDROID_LOG_PMSG_FILE_SEQUENCE) ||
  317. ((transp.logMsg.entry.nsec / ANDROID_LOG_PMSG_FILE_SEQUENCE) >=
  318. ANDROID_LOG_PMSG_FILE_MAX_SEQUENCE)) {
  319. continue;
  320. }
  321. /* Determine if it has <dirbase>:<filebase> format for tag */
  322. len = transp.logMsg.entry.len - sizeof(prio);
  323. for (cp = msg + sizeof(prio); *cp && isprint(*cp) && !isspace(*cp) && --len; ++cp) {
  324. if (*cp == ':') {
  325. if (split) {
  326. break;
  327. }
  328. split = cp;
  329. }
  330. }
  331. if (*cp || !split) {
  332. continue;
  333. }
  334. /* Filters */
  335. if (prefix_len && strncmp(msg + sizeof(prio), prefix, prefix_len)) {
  336. size_t offset;
  337. /*
  338. * Allow : to be a synonym for /
  339. * Things we do dealing with const char * and do not alloc
  340. */
  341. split = strchr(prefix, ':');
  342. if (split) {
  343. continue;
  344. }
  345. split = strchr(prefix, '/');
  346. if (!split) {
  347. continue;
  348. }
  349. offset = split - prefix;
  350. if ((msg[offset + sizeof(prio)] != ':') || strncmp(msg + sizeof(prio), prefix, offset)) {
  351. continue;
  352. }
  353. ++offset;
  354. if ((prefix_len > offset) &&
  355. strncmp(&msg[offset + sizeof(prio)], split + 1, prefix_len - offset)) {
  356. continue;
  357. }
  358. }
  359. if ((prio != ANDROID_LOG_ANY) && (*msg < prio)) {
  360. continue;
  361. }
  362. /* check if there is an existing entry */
  363. list_for_each(node, &name_list) {
  364. names = node_to_item(node, struct names, node);
  365. if (!strcmp(names->name, msg + sizeof(prio)) && (names->id == transp.logMsg.entry.lid) &&
  366. (names->prio == *msg)) {
  367. break;
  368. }
  369. }
  370. /* We do not have an existing entry, create and add one */
  371. if (node == &name_list) {
  372. static const char numbers[] = "0123456789";
  373. unsigned long long nl;
  374. len = strlen(msg + sizeof(prio)) + 1;
  375. names = static_cast<struct names*>(calloc(1, sizeof(*names) + len));
  376. if (!names) {
  377. ret = -ENOMEM;
  378. break;
  379. }
  380. strcpy(names->name, msg + sizeof(prio));
  381. names->id = static_cast<log_id_t>(transp.logMsg.entry.lid);
  382. names->prio = *msg;
  383. list_init(&names->content);
  384. /*
  385. * Insert in reverse numeric _then_ alpha sorted order as
  386. * representative of log rotation:
  387. *
  388. * log.10
  389. * klog.10
  390. * . . .
  391. * log.2
  392. * klog.2
  393. * log.1
  394. * klog.1
  395. * log
  396. * klog
  397. *
  398. * thus when we present the content, we are provided the oldest
  399. * first, which when 'refreshed' could spill off the end of the
  400. * pmsg FIFO but retaining the newest data for last with best
  401. * chances to survive.
  402. */
  403. nl = 0;
  404. cp = strpbrk(names->name, numbers);
  405. if (cp) {
  406. nl = strtoull(cp, NULL, 10);
  407. }
  408. list_for_each_reverse(node, &name_list) {
  409. struct names* a_name = node_to_item(node, struct names, node);
  410. const char* r = a_name->name;
  411. int compare = 0;
  412. unsigned long long nr = 0;
  413. cp = strpbrk(r, numbers);
  414. if (cp) {
  415. nr = strtoull(cp, NULL, 10);
  416. }
  417. if (nr != nl) {
  418. compare = (nl > nr) ? 1 : -1;
  419. }
  420. if (compare == 0) {
  421. compare = strcmp(names->name, r);
  422. }
  423. if (compare <= 0) {
  424. break;
  425. }
  426. }
  427. list_add_head(node, &names->node);
  428. }
  429. /* Remove any file fragments that match our sequence number */
  430. list_for_each_safe(node, n, &names->content) {
  431. content = node_to_item(node, struct content, node);
  432. if (transp.logMsg.entry.nsec == content->entry.nsec) {
  433. list_remove(&content->node);
  434. free(content);
  435. }
  436. }
  437. /* Add content */
  438. content = static_cast<struct content*>(
  439. calloc(1, sizeof(content->node) + hdr_size + transp.logMsg.entry.len));
  440. if (!content) {
  441. ret = -ENOMEM;
  442. break;
  443. }
  444. memcpy(&content->entry, &transp.logMsg.entry, hdr_size + transp.logMsg.entry.len);
  445. /* Insert in sequence number sorted order, to ease reconstruction */
  446. list_for_each_reverse(node, &names->content) {
  447. if ((node_to_item(node, struct content, node))->entry.nsec < transp.logMsg.entry.nsec) {
  448. break;
  449. }
  450. }
  451. list_add_head(node, &content->node);
  452. }
  453. pmsgClose(&logger_list, &transp);
  454. /* Progress through all the collected files */
  455. list_for_each_safe(node, n, &name_list) {
  456. struct listnode *content_node, *m;
  457. char* buf;
  458. size_t sequence, tag_len;
  459. names = node_to_item(node, struct names, node);
  460. /* Construct content into a linear buffer */
  461. buf = NULL;
  462. len = 0;
  463. sequence = 0;
  464. tag_len = strlen(names->name) + sizeof(char); /* tag + nul */
  465. list_for_each_safe(content_node, m, &names->content) {
  466. ssize_t add_len;
  467. content = node_to_item(content_node, struct content, node);
  468. add_len = content->entry.len - tag_len - sizeof(prio);
  469. if (add_len <= 0) {
  470. list_remove(content_node);
  471. free(content);
  472. continue;
  473. }
  474. if (!buf) {
  475. buf = static_cast<char*>(malloc(sizeof(char)));
  476. if (!buf) {
  477. ret = -ENOMEM;
  478. list_remove(content_node);
  479. free(content);
  480. continue;
  481. }
  482. *buf = '\0';
  483. }
  484. /* Missing sequence numbers */
  485. while (sequence < content->entry.nsec) {
  486. /* plus space for enforced nul */
  487. buf = static_cast<char*>(realloc_or_free(buf, len + sizeof(char) + sizeof(char)));
  488. if (!buf) {
  489. break;
  490. }
  491. buf[len] = '\f'; /* Mark missing content with a form feed */
  492. buf[++len] = '\0';
  493. sequence += ANDROID_LOG_PMSG_FILE_SEQUENCE;
  494. }
  495. if (!buf) {
  496. ret = -ENOMEM;
  497. list_remove(content_node);
  498. free(content);
  499. continue;
  500. }
  501. /* plus space for enforced nul */
  502. buf = static_cast<char*>(realloc_or_free(buf, len + add_len + sizeof(char)));
  503. if (!buf) {
  504. ret = -ENOMEM;
  505. list_remove(content_node);
  506. free(content);
  507. continue;
  508. }
  509. memcpy(buf + len, (char*)&content->entry + content->entry.hdr_size + tag_len + sizeof(prio),
  510. add_len);
  511. len += add_len;
  512. buf[len] = '\0'; /* enforce trailing hidden nul */
  513. sequence = content->entry.nsec + ANDROID_LOG_PMSG_FILE_SEQUENCE;
  514. list_remove(content_node);
  515. free(content);
  516. }
  517. if (buf) {
  518. if (len) {
  519. /* Buffer contains enforced trailing nul just beyond length */
  520. ssize_t r;
  521. *strchr(names->name, ':') = '/'; /* Convert back to filename */
  522. r = (*fn)(names->id, names->prio, names->name, buf, len, arg);
  523. if ((ret >= 0) && (r > 0)) {
  524. if (ret == SSIZE_MAX) {
  525. ret = r;
  526. } else {
  527. ret += r;
  528. }
  529. } else if (r < ret) {
  530. ret = r;
  531. }
  532. }
  533. free(buf);
  534. }
  535. list_remove(node);
  536. free(names);
  537. }
  538. return (ret == SSIZE_MAX) ? -ENOENT : ret;
  539. }