1 /*
2 * Copyright (C) 2019 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 <android-base/logging.h>
18 #include <errno.h>
19 #include <log/log.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <sys/inotify.h>
24 #include <unistd.h>
25 #include <fstream>
26
27 #include <cutils/properties.h>
28 #include <gflags/gflags.h>
29
30 #include "common/libs/fs/shared_fd.h"
31 #include "common/libs/utils/subprocess.h"
32
33 static const char TOMBSTONE_DIR[] = "/data/tombstones/";
34
35 // returns a fd which when read from, provides inotify events when tombstones
36 // are created
new_tombstone_create_notifier(void)37 static int new_tombstone_create_notifier(void) {
38 int file_create_notification_handle = inotify_init();
39 if (file_create_notification_handle == -1) {
40 ALOGE("%s: inotify_init failure error: '%s' (%d)", __FUNCTION__,
41 strerror(errno), errno);
42 return -1;
43 }
44
45 int watch_descriptor = inotify_add_watch(file_create_notification_handle,
46 TOMBSTONE_DIR, IN_CREATE);
47 if (watch_descriptor == -1) {
48 ALOGE("%s: Could not add watch for '%s', error: '%s' (%d)", __FUNCTION__,
49 TOMBSTONE_DIR, strerror(errno), errno);
50 close(file_create_notification_handle);
51 return -1;
52 }
53
54 return file_create_notification_handle;
55 }
56
57 #define INOTIFY_MAX_EVENT_SIZE (sizeof(struct inotify_event) + NAME_MAX + 1)
get_next_tombstone_path_blocking(int fd)58 static std::string get_next_tombstone_path_blocking(int fd) {
59 char event_readout[INOTIFY_MAX_EVENT_SIZE];
60 struct inotify_event *i = (inotify_event*)(event_readout);
61
62 int event_read_out_length = read(fd, event_readout, INOTIFY_MAX_EVENT_SIZE);
63
64 if(event_read_out_length == -1) {
65 ALOGE("%s: Couldn't read out inotify event due to error: '%s' (%d)",
66 __FUNCTION__, strerror(errno), errno);
67 return std::string();
68 }
69
70 // Create event didn't show up for some reason or no file name was present
71 if(event_read_out_length == sizeof(struct inotify_event)) {
72 ALOGE("%s: inotify event didn't contain filename",__FUNCTION__);
73 return std::string();
74 }
75
76 if(!(i->mask & IN_CREATE)) {
77 ALOGE("%s: inotify event didn't pertain to file creation",__FUNCTION__);
78 return std::string();
79 }
80
81 std::string ret_value(TOMBSTONE_DIR);
82 return ret_value + i->name;
83 }
84
85 DEFINE_uint32(port,
86 static_cast<uint32_t>(
87 property_get_int64("ro.boot.vsock_tombstone_port", 0)),
88 "VSOCK port to send tombstones to");
89 DEFINE_uint32(cid, 2, "VSOCK CID to send logcat output to");
90 #define TOMBSTONE_BUFFER_SIZE (1024)
91
main(int argc,char ** argv)92 int main(int argc, char** argv) {
93 gflags::ParseCommandLineFlags(&argc, &argv, true);
94
95 if(FLAGS_port == 0) {
96 LOG(FATAL_WITHOUT_ABORT) << "Port flag is required";
97 while(1) {sleep(1);};
98 }
99
100 int file_create_notification_handle = new_tombstone_create_notifier();
101 if (file_create_notification_handle == -1) {return -1;}
102
103 LOG(INFO) << "tombstone watcher successfully initialized";
104
105 while (true) {
106 std::string ts_path = get_next_tombstone_path_blocking(
107 file_create_notification_handle);
108
109 if(ts_path.empty()) {continue;}
110
111 auto log_fd = cuttlefish::SharedFD::VsockClient(FLAGS_cid, FLAGS_port,
112 SOCK_STREAM);
113
114 std::ifstream ifs(ts_path);
115 char buffer[TOMBSTONE_BUFFER_SIZE];
116 uint num_transfers = 0;
117 int num_bytes_read = 0;
118 while (log_fd->IsOpen() && ifs.is_open() && !ifs.eof()) {
119 ifs.read(buffer, sizeof(buffer));
120 num_bytes_read += ifs.gcount();
121 log_fd->Write(buffer, ifs.gcount());
122 num_transfers++;
123 }
124
125 if (!log_fd->IsOpen()) {
126 ALOGE("Unable to connect to vsock:%u:%u: %s", FLAGS_cid, FLAGS_port,
127 log_fd->StrError());
128 } else if (!ifs.is_open()) {
129 ALOGE("%s closed in the middle of readout.", ts_path.c_str());
130 } else {
131 LOG(INFO) << num_bytes_read << " chars transferred from " <<
132 ts_path.c_str() << " over " << num_transfers << " "
133 << TOMBSTONE_BUFFER_SIZE << " byte sized transfers";
134 }
135 }
136
137 return 0;
138 }
139