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