Ram Muthiah | 792e2ad | 2019-04-19 11:19:46 -0700 | [diff] [blame] | 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 <gflags/gflags.h> |
| 18 | #include <glog/logging.h> |
| 19 | |
| 20 | #include <chrono> |
| 21 | #include <fstream> |
| 22 | #include <iomanip> |
| 23 | #include <sstream> |
| 24 | |
| 25 | #include "common/libs/fs/shared_fd.h" |
Ram Muthiah | 792e2ad | 2019-04-19 11:19:46 -0700 | [diff] [blame] | 26 | |
| 27 | DEFINE_int32( |
| 28 | server_fd, -1, |
| 29 | "File descriptor to an already created vsock server. If negative a new " |
| 30 | "server will be created at the port specified on the config file"); |
| 31 | DEFINE_string(tombstone_dir, "", "directory to write out tombstones in"); |
| 32 | |
| 33 | static uint num_tombstones_in_last_second = 0; |
| 34 | static std::string last_tombstone_name = ""; |
| 35 | |
| 36 | static std::string next_tombstone_path() { |
| 37 | auto in_time_t = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now()); |
| 38 | std::stringstream ss; |
| 39 | ss << FLAGS_tombstone_dir << "/tombstone_" << |
| 40 | std::put_time(std::gmtime(&in_time_t), "%Y-%m-%d-%H%M%S"); |
| 41 | auto retval = ss.str(); |
| 42 | |
| 43 | // Gives tombstones unique names |
| 44 | if(retval == last_tombstone_name) { |
| 45 | num_tombstones_in_last_second++; |
| 46 | retval += "_" + std::to_string(num_tombstones_in_last_second); |
| 47 | } else { |
| 48 | last_tombstone_name = retval; |
| 49 | num_tombstones_in_last_second = 0; |
| 50 | } |
| 51 | |
| 52 | LOG(INFO) << "Creating " << retval; |
| 53 | return retval; |
| 54 | } |
| 55 | |
| 56 | #define CHUNK_RECV_MAX_LEN (1024) |
| 57 | int main(int argc, char** argv) { |
| 58 | ::android::base::InitLogging(argv, android::base::StderrLogger); |
| 59 | google::ParseCommandLineFlags(&argc, &argv, true); |
| 60 | |
Cody Schuffelen | d3f27b9 | 2019-11-26 19:04:43 -0800 | [diff] [blame] | 61 | cvd::SharedFD server_fd = cvd::SharedFD::Dup(FLAGS_server_fd); |
| 62 | close(FLAGS_server_fd); |
Ram Muthiah | 792e2ad | 2019-04-19 11:19:46 -0700 | [diff] [blame] | 63 | |
Cody Schuffelen | d3f27b9 | 2019-11-26 19:04:43 -0800 | [diff] [blame] | 64 | CHECK(server_fd->IsOpen()) << "Error inheriting tombstone server: " |
Ram Muthiah | 792e2ad | 2019-04-19 11:19:46 -0700 | [diff] [blame] | 65 | << server_fd->StrError(); |
| 66 | LOG(INFO) << "Host is starting server on port " |
Cody Schuffelen | d3f27b9 | 2019-11-26 19:04:43 -0800 | [diff] [blame] | 67 | << server_fd->VsockServerPort(); |
Ram Muthiah | 792e2ad | 2019-04-19 11:19:46 -0700 | [diff] [blame] | 68 | |
| 69 | // Server loop |
| 70 | while (true) { |
| 71 | auto conn = cvd::SharedFD::Accept(*server_fd); |
| 72 | std::ofstream file(next_tombstone_path(), |
| 73 | std::ofstream::out | std::ofstream::binary); |
| 74 | |
| 75 | while (file.is_open()) { |
| 76 | char buff[CHUNK_RECV_MAX_LEN]; |
| 77 | auto bytes_read = conn->Read(buff, sizeof(buff)); |
| 78 | if (bytes_read <= 0) { |
| 79 | // reset the other side if it's still connected |
| 80 | break; |
| 81 | } else { |
| 82 | file.write(buff, bytes_read); |
| 83 | } |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | return 0; |
| 88 | } |