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 #include <regex>
27 
28 #include <cutils/properties.h>
29 #include <gflags/gflags.h>
30 
31 #include "common/libs/fs/shared_fd.h"
32 #include "common/libs/utils/inotify.h"
33 #include "common/libs/utils/subprocess.h"
34 
35 static const char TOMBSTONE_DIR[] = "/data/tombstones/";
36 
37 // returns a fd which when read from, provides inotify events when tombstone
38 // creations are completed.
new_tombstone_creation_complete_notifier(void)39 static int new_tombstone_creation_complete_notifier(void) {
40   int file_close_notification_handle = inotify_init1(IN_CLOEXEC);
41   if (file_close_notification_handle == -1) {
42     ALOGE("%s: inotify_init failure error: '%s' (%d)", __FUNCTION__,
43       strerror(errno), errno);
44     return -1;
45   }
46 
47   int watch_descriptor = inotify_add_watch(file_close_notification_handle,
48                                            TOMBSTONE_DIR, IN_CREATE);
49   if (watch_descriptor == -1) {
50     ALOGE("%s: Could not add watch for '%s', error: '%s' (%d)", __FUNCTION__,
51       TOMBSTONE_DIR, strerror(errno), errno);
52     close(file_close_notification_handle);
53     return -1;
54   }
55 
56   return file_close_notification_handle;
57 }
58 
59 DEFINE_uint32(port,
60               static_cast<uint32_t>(
61                   property_get_int64("ro.boot.vsock_tombstone_port", 0)),
62               "VSOCK port to send tombstones to");
63 DEFINE_uint32(cid, 2, "VSOCK CID to send logcat output to");
64 DEFINE_bool(remove_tombstones_after_transmitting, false,
65             "Whether to remove the tombstone from VM after transmitting it");
66 #define TOMBSTONE_BUFFER_SIZE (1024)
67 
tombstone_send_to_host(const std::string & ts_path)68 static void tombstone_send_to_host(const std::string& ts_path) {
69   auto log_fd = cuttlefish::SharedFD::VsockClient(
70       FLAGS_cid, FLAGS_port, SOCK_STREAM, false /* it's guest */);
71   std::ifstream ifs(ts_path);
72   char buffer[TOMBSTONE_BUFFER_SIZE];
73   size_t num_transfers = 0;
74   size_t num_bytes_read = 0;
75   while (log_fd->IsOpen() && ifs.is_open() && !ifs.eof()) {
76     ifs.read(buffer, sizeof(buffer));
77     num_bytes_read += ifs.gcount();
78     log_fd->Write(buffer, ifs.gcount());
79     num_transfers++;
80   }
81 
82   if (!log_fd->IsOpen()) {
83     auto error = log_fd->StrError();
84     ALOGE("Unable to connect to vsock:%u:%u: %s", FLAGS_cid, FLAGS_port,
85           error.c_str());
86   } else if (!ifs.is_open()) {
87     ALOGE("%s closed in the middle of readout.", ts_path.c_str());
88   } else {
89     LOG(INFO) << num_bytes_read << " bytes transferred from "
90               << ts_path.c_str() << " over " << num_transfers << " "
91               << TOMBSTONE_BUFFER_SIZE << " byte sized transfers";
92   }
93 
94   if (FLAGS_remove_tombstones_after_transmitting) {
95     remove(ts_path.c_str());
96   }
97 }
98 
main(int argc,char ** argv)99 int main(int argc, char** argv) {
100   gflags::ParseCommandLineFlags(&argc, &argv, true);
101 
102   if(FLAGS_port == 0) {
103     LOG(FATAL_WITHOUT_ABORT) << "Port flag is required";
104     while(1) {sleep(1);};
105   }
106 
107   int tombstone_create_notification_handle =
108       new_tombstone_creation_complete_notifier();
109   if (tombstone_create_notification_handle == -1) {
110     return -1;
111   }
112 
113   LOG(INFO) << "tombstone watcher successfully initialized";
114 
115 #ifdef MICRODROID
116   property_set("tombstone_transmit.init_done", "true");
117 #endif
118 
119   std::regex re(R"(tombstone_\d+(\.pb)?)");
120   while (true) {
121     std::vector<std::string> ts_names = cuttlefish::GetFileListFromInotifyFd(
122         tombstone_create_notification_handle, IN_CREATE);
123     for (auto& ts_name : ts_names) {
124       if (regex_match(ts_name, re)) {
125         tombstone_send_to_host(std::string(TOMBSTONE_DIR) + ts_name);
126       }
127     }
128   }
129 
130   return 0;
131 }
132