12971cf31b2e94d003908b6c974234fdf90c14cbcChristopher Wiley/*
22971cf31b2e94d003908b6c974234fdf90c14cbcChristopher Wiley * Copyright (C) 2015 The Android Open Source Project
32971cf31b2e94d003908b6c974234fdf90c14cbcChristopher Wiley *
42971cf31b2e94d003908b6c974234fdf90c14cbcChristopher Wiley * Licensed under the Apache License, Version 2.0 (the "License");
52971cf31b2e94d003908b6c974234fdf90c14cbcChristopher Wiley * you may not use this file except in compliance with the License.
62971cf31b2e94d003908b6c974234fdf90c14cbcChristopher Wiley * You may obtain a copy of the License at
72971cf31b2e94d003908b6c974234fdf90c14cbcChristopher Wiley *
82971cf31b2e94d003908b6c974234fdf90c14cbcChristopher Wiley *      http://www.apache.org/licenses/LICENSE-2.0
92971cf31b2e94d003908b6c974234fdf90c14cbcChristopher Wiley *
102971cf31b2e94d003908b6c974234fdf90c14cbcChristopher Wiley * Unless required by applicable law or agreed to in writing, software
112971cf31b2e94d003908b6c974234fdf90c14cbcChristopher Wiley * distributed under the License is distributed on an "AS IS" BASIS,
122971cf31b2e94d003908b6c974234fdf90c14cbcChristopher Wiley * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
132971cf31b2e94d003908b6c974234fdf90c14cbcChristopher Wiley * See the License for the specific language governing permissions and
142971cf31b2e94d003908b6c974234fdf90c14cbcChristopher Wiley * limitations under the License.
152971cf31b2e94d003908b6c974234fdf90c14cbcChristopher Wiley */
162971cf31b2e94d003908b6c974234fdf90c14cbcChristopher Wiley
172971cf31b2e94d003908b6c974234fdf90c14cbcChristopher Wiley#include <fstream>
182971cf31b2e94d003908b6c974234fdf90c14cbcChristopher Wiley#include <iostream>
192971cf31b2e94d003908b6c974234fdf90c14cbcChristopher Wiley#include <string>
202971cf31b2e94d003908b6c974234fdf90c14cbcChristopher Wiley#include <vector>
212971cf31b2e94d003908b6c974234fdf90c14cbcChristopher Wiley
222971cf31b2e94d003908b6c974234fdf90c14cbcChristopher Wiley#include <errno.h>
232971cf31b2e94d003908b6c974234fdf90c14cbcChristopher Wiley#include <time.h>
242971cf31b2e94d003908b6c974234fdf90c14cbcChristopher Wiley#include <unistd.h>
252971cf31b2e94d003908b6c974234fdf90c14cbcChristopher Wiley
262971cf31b2e94d003908b6c974234fdf90c14cbcChristopher Wileyusing std::cerr;
272971cf31b2e94d003908b6c974234fdf90c14cbcChristopher Wileyusing std::cout;
282971cf31b2e94d003908b6c974234fdf90c14cbcChristopher Wileyusing std::endl;
292971cf31b2e94d003908b6c974234fdf90c14cbcChristopher Wileyusing std::getline;
302971cf31b2e94d003908b6c974234fdf90c14cbcChristopher Wileyusing std::ifstream;
312971cf31b2e94d003908b6c974234fdf90c14cbcChristopher Wileyusing std::string;
322971cf31b2e94d003908b6c974234fdf90c14cbcChristopher Wileyusing std::vector;
332971cf31b2e94d003908b6c974234fdf90c14cbcChristopher Wiley
342971cf31b2e94d003908b6c974234fdf90c14cbcChristopher Wileynamespace {
352971cf31b2e94d003908b6c974234fdf90c14cbcChristopher Wiley
362971cf31b2e94d003908b6c974234fdf90c14cbcChristopher Wileybool ReadLines(const string& input_file_path, vector<string>* lines) {
372971cf31b2e94d003908b6c974234fdf90c14cbcChristopher Wiley  ifstream watched_file(input_file_path);
382971cf31b2e94d003908b6c974234fdf90c14cbcChristopher Wiley  if (!watched_file.is_open()) {
392971cf31b2e94d003908b6c974234fdf90c14cbcChristopher Wiley    cerr << "Unable to open input file: " << input_file_path << endl;
402971cf31b2e94d003908b6c974234fdf90c14cbcChristopher Wiley    return false;
412971cf31b2e94d003908b6c974234fdf90c14cbcChristopher Wiley  }
422971cf31b2e94d003908b6c974234fdf90c14cbcChristopher Wiley
432971cf31b2e94d003908b6c974234fdf90c14cbcChristopher Wiley  string line;
442971cf31b2e94d003908b6c974234fdf90c14cbcChristopher Wiley  while (getline(watched_file, line)) {
452971cf31b2e94d003908b6c974234fdf90c14cbcChristopher Wiley    lines->push_back(line);
462971cf31b2e94d003908b6c974234fdf90c14cbcChristopher Wiley  }
472971cf31b2e94d003908b6c974234fdf90c14cbcChristopher Wiley  watched_file.close();
482971cf31b2e94d003908b6c974234fdf90c14cbcChristopher Wiley  return true;
492971cf31b2e94d003908b6c974234fdf90c14cbcChristopher Wiley}
502971cf31b2e94d003908b6c974234fdf90c14cbcChristopher Wiley
512971cf31b2e94d003908b6c974234fdf90c14cbcChristopher Wileybool HasSentinel(const vector<string>& lines, const string& sentinel) {
522971cf31b2e94d003908b6c974234fdf90c14cbcChristopher Wiley  for (const auto& line : lines) {
532971cf31b2e94d003908b6c974234fdf90c14cbcChristopher Wiley    if (line.find(sentinel) != string::npos) {
542971cf31b2e94d003908b6c974234fdf90c14cbcChristopher Wiley      return true;
552971cf31b2e94d003908b6c974234fdf90c14cbcChristopher Wiley    }
562971cf31b2e94d003908b6c974234fdf90c14cbcChristopher Wiley  }
572971cf31b2e94d003908b6c974234fdf90c14cbcChristopher Wiley  return false;
582971cf31b2e94d003908b6c974234fdf90c14cbcChristopher Wiley}
592971cf31b2e94d003908b6c974234fdf90c14cbcChristopher Wiley
602971cf31b2e94d003908b6c974234fdf90c14cbcChristopher Wiley}  // namespace
612971cf31b2e94d003908b6c974234fdf90c14cbcChristopher Wiley
622971cf31b2e94d003908b6c974234fdf90c14cbcChristopher Wileyint main(int argc, const char* argv[]) {
632971cf31b2e94d003908b6c974234fdf90c14cbcChristopher Wiley  if (argc != 5) {
642971cf31b2e94d003908b6c974234fdf90c14cbcChristopher Wiley    cerr << "Invalid usage." << endl;
652971cf31b2e94d003908b6c974234fdf90c14cbcChristopher Wiley    cerr << argv[0]
662971cf31b2e94d003908b6c974234fdf90c14cbcChristopher Wiley         << " <timeout in seconds>"
672971cf31b2e94d003908b6c974234fdf90c14cbcChristopher Wiley         << " <input file path>"
682971cf31b2e94d003908b6c974234fdf90c14cbcChristopher Wiley         << " <success sentinel>"
692971cf31b2e94d003908b6c974234fdf90c14cbcChristopher Wiley         << " <failure sentinel>" << endl;
702971cf31b2e94d003908b6c974234fdf90c14cbcChristopher Wiley    return -EINVAL;
712971cf31b2e94d003908b6c974234fdf90c14cbcChristopher Wiley  }
722971cf31b2e94d003908b6c974234fdf90c14cbcChristopher Wiley  const string timeout_as_str = argv[1];
732971cf31b2e94d003908b6c974234fdf90c14cbcChristopher Wiley  const string input_file_path = argv[2];
742971cf31b2e94d003908b6c974234fdf90c14cbcChristopher Wiley  const string success_sentinel = argv[3];
752971cf31b2e94d003908b6c974234fdf90c14cbcChristopher Wiley  const string failure_sentinel = argv[4];
762971cf31b2e94d003908b6c974234fdf90c14cbcChristopher Wiley
772971cf31b2e94d003908b6c974234fdf90c14cbcChristopher Wiley  const int timeout_seconds = atoi(timeout_as_str.c_str());
782971cf31b2e94d003908b6c974234fdf90c14cbcChristopher Wiley  if (timeout_seconds <= 0) {
792971cf31b2e94d003908b6c974234fdf90c14cbcChristopher Wiley    cerr << "Invalid timeout value (in seconds): " << timeout_as_str << endl;
802971cf31b2e94d003908b6c974234fdf90c14cbcChristopher Wiley    return -EINVAL;
812971cf31b2e94d003908b6c974234fdf90c14cbcChristopher Wiley  }
822971cf31b2e94d003908b6c974234fdf90c14cbcChristopher Wiley
832971cf31b2e94d003908b6c974234fdf90c14cbcChristopher Wiley  int exit_code = 1;
842971cf31b2e94d003908b6c974234fdf90c14cbcChristopher Wiley  const time_t start_time = time(nullptr);
852971cf31b2e94d003908b6c974234fdf90c14cbcChristopher Wiley  vector<string> lines;
862971cf31b2e94d003908b6c974234fdf90c14cbcChristopher Wiley  while (true) {
872971cf31b2e94d003908b6c974234fdf90c14cbcChristopher Wiley    sleep(1);
882971cf31b2e94d003908b6c974234fdf90c14cbcChristopher Wiley    if (time(nullptr) - start_time > timeout_seconds) {
892971cf31b2e94d003908b6c974234fdf90c14cbcChristopher Wiley      cerr << "Timed out waiting for success/failure sentinel." << endl;
902971cf31b2e94d003908b6c974234fdf90c14cbcChristopher Wiley      break;
912971cf31b2e94d003908b6c974234fdf90c14cbcChristopher Wiley    }
922971cf31b2e94d003908b6c974234fdf90c14cbcChristopher Wiley    // Ignore errors when reading lines.  The file may not immediately exist
932971cf31b2e94d003908b6c974234fdf90c14cbcChristopher Wiley    // because it takes the Java process some time to create it.
942971cf31b2e94d003908b6c974234fdf90c14cbcChristopher Wiley    lines.clear();
952971cf31b2e94d003908b6c974234fdf90c14cbcChristopher Wiley    ReadLines(input_file_path, &lines);
962971cf31b2e94d003908b6c974234fdf90c14cbcChristopher Wiley
972971cf31b2e94d003908b6c974234fdf90c14cbcChristopher Wiley    if (HasSentinel(lines, success_sentinel)) {
982971cf31b2e94d003908b6c974234fdf90c14cbcChristopher Wiley      exit_code = 0;
992971cf31b2e94d003908b6c974234fdf90c14cbcChristopher Wiley      break;
1002971cf31b2e94d003908b6c974234fdf90c14cbcChristopher Wiley    }
1012971cf31b2e94d003908b6c974234fdf90c14cbcChristopher Wiley    if (HasSentinel(lines, failure_sentinel)) {
1022971cf31b2e94d003908b6c974234fdf90c14cbcChristopher Wiley      break;
1032971cf31b2e94d003908b6c974234fdf90c14cbcChristopher Wiley    }
1042971cf31b2e94d003908b6c974234fdf90c14cbcChristopher Wiley  }
1052971cf31b2e94d003908b6c974234fdf90c14cbcChristopher Wiley
1062971cf31b2e94d003908b6c974234fdf90c14cbcChristopher Wiley  cout << "Found output:" << endl;
1072971cf31b2e94d003908b6c974234fdf90c14cbcChristopher Wiley  for (const auto& line : lines) {
1082971cf31b2e94d003908b6c974234fdf90c14cbcChristopher Wiley    cout << "  " << line << endl;
1092971cf31b2e94d003908b6c974234fdf90c14cbcChristopher Wiley  }
1102971cf31b2e94d003908b6c974234fdf90c14cbcChristopher Wiley  return exit_code;
1112971cf31b2e94d003908b6c974234fdf90c14cbcChristopher Wiley}
112