scenario_simulator_v2 C++ API
launch.hpp
Go to the documentation of this file.
1 // Copyright 2015 TIER IV, Inc. All rights reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef CONCEALER__LAUNCH_HPP_
16 #define CONCEALER__LAUNCH_HPP_
17 
18 #ifdef CONCEALER_ISOLATE_STANDARD_OUTPUT
19 #include <fcntl.h>
20 #include <sys/stat.h>
21 #include <sys/types.h>
22 #include <unistd.h>
23 #endif
24 
25 #include <boost/algorithm/string.hpp>
26 #include <concealer/execute.hpp>
27 #include <cstdlib>
28 #include <iostream>
29 #include <string>
30 #include <system_error>
31 #include <type_traits>
32 #include <vector>
33 
34 namespace concealer
35 {
36 template <typename Parameters>
38  const std::string & package, const std::string & file, const Parameters & parameters)
39 {
40 #ifdef CONCEALER_ISOLATE_STANDARD_OUTPUT
41  const std::string log_filename = "/tmp/scenario_test_runner/autoware-output.txt";
42  const auto fd = ::open(log_filename.c_str(), O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
43  ::dup2(fd, STDOUT_FILENO);
44  ::dup2(fd, STDERR_FILENO);
45  ::close(fd);
46 #endif
47 
48  const auto argv = [&]() {
49  auto argv = std::vector<std::string>();
50 
51  argv.push_back("python3");
52  argv.push_back(boost::algorithm::replace_all_copy(dollar("which ros2"), "\n", ""));
53  argv.push_back("launch");
54  argv.push_back(package);
55  argv.push_back(file);
56 
57  for (const auto & parameter : parameters) {
58  argv.push_back(parameter);
59  }
60 
61  return argv;
62  }();
63 
64  if (const auto process_id = fork(); process_id < 0) {
65  throw std::system_error(errno, std::system_category());
66  } else if (process_id == 0 and execute(argv) < 0) {
67  std::cout << std::system_error(errno, std::system_category()).what() << std::endl;
68  std::exit(EXIT_FAILURE);
69  } else {
70  return process_id;
71  }
72 }
73 } // namespace concealer
74 
75 #endif // CONCEALER__LAUNCH_HPP_
Definition: autoware.hpp:30
auto ros2_launch(const std::string &package, const std::string &file, const Parameters &parameters)
Definition: launch.hpp:37
auto dollar(const std::string &command) -> std::string
Definition: execute.cpp:57
auto execute(const std::vector< std::string > &) -> int
Definition: execute.cpp:36
pid_t process_id
Definition: record.cpp:25
std::string string
Definition: junit5.hpp:26