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_tombstones_path_blocking(int fd)58 static std::vector<std::string> get_next_tombstones_path_blocking(int fd) {
59 char event_readout[INOTIFY_MAX_EVENT_SIZE];
60 int bytes_parsed = 0;
61 std::vector<std::string> tombstone_paths;
62 // Each successful read can contain one or more of inotify_event events
63 // Note: read() on inotify returns 'whole' events, will never partially
64 // populate the buffer.
65 int event_read_out_length = read(fd, event_readout, INOTIFY_MAX_EVENT_SIZE);
66
67 if(event_read_out_length == -1) {
68 ALOGE("%s: Couldn't read out inotify event due to error: '%s' (%d)",
69 __FUNCTION__, strerror(errno), errno);
70 return std::vector<std::string>();
71 }
72
73 while (bytes_parsed < event_read_out_length) {
74 struct inotify_event* event =
75 reinterpret_cast<inotify_event*>(event_readout + bytes_parsed);
76 bytes_parsed += sizeof(struct inotify_event) + event->len;
77
78 // No file name was present
79 if (event->len == 0) {
80 ALOGE("%s: inotify event didn't contain filename", __FUNCTION__);
81 continue;
82 }
83 if (!(event->mask & IN_CREATE)) {
84 ALOGE("%s: inotify event didn't pertain to file creation", __FUNCTION__);
85 continue;
86 }
87 tombstone_paths.push_back(std::string(TOMBSTONE_DIR) +
88 std::string(event->name));
89 }
90
91 return tombstone_paths;
92 }
93
94 DEFINE_uint32(port,
95 static_cast<uint32_t>(
96 property_get_int64("ro.boot.vsock_tombstone_port", 0)),
97 "VSOCK port to send tombstones to");
98 DEFINE_uint32(cid, 2, "VSOCK CID to send logcat output to");
99 #define TOMBSTONE_BUFFER_SIZE (1024)
100
main(int argc,char ** argv)101 int main(int argc, char** argv) {
102 gflags::ParseCommandLineFlags(&argc, &argv, true);
103
104 if(FLAGS_port == 0) {
105 LOG(FATAL_WITHOUT_ABORT) << "Port flag is required";
106 while(1) {sleep(1);};
107 }
108
109 int file_create_notification_handle = new_tombstone_create_notifier();
110 if (file_create_notification_handle == -1) {return -1;}
111
112 LOG(INFO) << "tombstone watcher successfully initialized";
113
114 while (true) {
115 std::vector<std::string> ts_paths =
116 get_next_tombstones_path_blocking(file_create_notification_handle);
117 for (auto& ts_path : ts_paths) {
118 auto log_fd =
119 cuttlefish::SharedFD::VsockClient(FLAGS_cid, FLAGS_port, SOCK_STREAM);
120 std::ifstream ifs(ts_path);
121 char buffer[TOMBSTONE_BUFFER_SIZE];
122 uint num_transfers = 0;
123 int num_bytes_read = 0;
124 while (log_fd->IsOpen() && ifs.is_open() && !ifs.eof()) {
125 ifs.read(buffer, sizeof(buffer));
126 num_bytes_read += ifs.gcount();
127 log_fd->Write(buffer, ifs.gcount());
128 num_transfers++;
129 }
130
131 if (!log_fd->IsOpen()) {
132 auto error = log_fd->StrError();
133 ALOGE("Unable to connect to vsock:%u:%u: %s", FLAGS_cid, FLAGS_port,
134 error.c_str());
135 } else if (!ifs.is_open()) {
136 ALOGE("%s closed in the middle of readout.", ts_path.c_str());
137 } else {
138 LOG(INFO) << num_bytes_read << " chars transferred from "
139 << ts_path.c_str() << " over " << num_transfers << " "
140 << TOMBSTONE_BUFFER_SIZE << " byte sized transfers";
141 }
142 }
143 }
144
145 return 0;
146 }
147