1 /*
2 * Copyright (C) 2013-2017 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 <sys/types.h>
18 #include <time.h>
19 #include <unistd.h>
20
21 #include <string>
22
23 #include <android-base/chrono_utils.h>
24 #include <android-base/stringprintf.h>
25 #include <android/log.h> // minimal logging API
26 #include <gtest/gtest.h>
27 #include <log/log_properties.h>
28 #include <log/log_read.h>
29 #include <log/log_time.h>
30
31 #ifdef __ANDROID__
read_with_wrap()32 static void read_with_wrap() {
33 // Read the last line in the log to get a starting timestamp. We're assuming
34 // the log is not empty.
35 const int mode = ANDROID_LOG_NONBLOCK;
36 struct logger_list* logger_list = android_logger_list_open(LOG_ID_SYSTEM, mode, 1000, 0);
37
38 ASSERT_NE(logger_list, nullptr);
39
40 log_msg log_msg;
41 int ret = android_logger_list_read(logger_list, &log_msg);
42 android_logger_list_close(logger_list);
43 ASSERT_GT(ret, 0);
44
45 log_time start(log_msg.entry.sec, log_msg.entry.nsec);
46 ASSERT_NE(start, log_time());
47
48 logger_list =
49 android_logger_list_alloc_time(mode | ANDROID_LOG_WRAP, start, 0);
50 ASSERT_NE(logger_list, nullptr);
51 struct logger* logger = android_logger_open(logger_list, LOG_ID_SYSTEM);
52 EXPECT_NE(logger, nullptr);
53 if (logger) {
54 android_logger_list_read(logger_list, &log_msg);
55 }
56
57 android_logger_list_close(logger_list);
58 }
59 #endif
60
61 // b/64143705 confirm fixed
TEST(liblog,wrap_mode_blocks)62 TEST(liblog, wrap_mode_blocks) {
63 #ifdef __ANDROID__
64 // The read call is expected to take up to 2 hours in the happy case. There was a previous bug
65 // where it would take only 30 seconds due to an alarm() in logd_reader.cpp. That alarm has been
66 // removed, so we check here that the read call blocks for a reasonable amount of time (5s).
67
68 struct sigaction ignore = {.sa_handler = [](int) { _exit(0); }};
69 struct sigaction old_sigaction;
70 sigaction(SIGALRM, &ignore, &old_sigaction);
71 alarm(5);
72
73 android::base::Timer timer;
74 read_with_wrap();
75
76 FAIL() << "read_with_wrap() should not return before the alarm is triggered.";
77
78 alarm(0);
79 sigaction(SIGALRM, &old_sigaction, nullptr);
80 #else
81 GTEST_LOG_(INFO) << "This test does nothing.\n";
82 #endif
83 }
84