blob: 25da1a3bf455a0918e9949a0f7e7685e95129da1 [file] [log] [blame]
Ram Muthiah792e2ad2019-04-19 11:19:46 -07001/*
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 Muthiah792e2ad2019-04-19 11:19:46 -070026
27DEFINE_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");
31DEFINE_string(tombstone_dir, "", "directory to write out tombstones in");
32
33static uint num_tombstones_in_last_second = 0;
34static std::string last_tombstone_name = "";
35
36static 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)
57int main(int argc, char** argv) {
58 ::android::base::InitLogging(argv, android::base::StderrLogger);
59 google::ParseCommandLineFlags(&argc, &argv, true);
60
Cody Schuffelend3f27b92019-11-26 19:04:43 -080061 cvd::SharedFD server_fd = cvd::SharedFD::Dup(FLAGS_server_fd);
62 close(FLAGS_server_fd);
Ram Muthiah792e2ad2019-04-19 11:19:46 -070063
Cody Schuffelend3f27b92019-11-26 19:04:43 -080064 CHECK(server_fd->IsOpen()) << "Error inheriting tombstone server: "
Ram Muthiah792e2ad2019-04-19 11:19:46 -070065 << server_fd->StrError();
66 LOG(INFO) << "Host is starting server on port "
Cody Schuffelend3f27b92019-11-26 19:04:43 -080067 << server_fd->VsockServerPort();
Ram Muthiah792e2ad2019-04-19 11:19:46 -070068
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}