65 const std::string & packageName,
const std::string & launchFileName,
66 std::function<
bool()> cancelCondition,
ClRosLaunch2 * client)
70 [packageName, launchFileName, cancelCondition, client]()
72 RCLCPP_WARN_STREAM(rclcpp::get_logger(
"smacc2"),
"[ClRosLaunch2] Starting ros launch thread");
74 std::stringstream cmd;
75 cmd <<
"ros2 launch " << packageName <<
" " << launchFileName;
76 std::array<char, 128> buffer{};
85 rclcpp::get_logger(
"smacc2"),
"[ClRosLaunch2] could not start: " << cmd.str());
88 if (client !=
nullptr)
93 int fd = fileno(child.pipe.get());
94 bool cancelled =
false;
100 cancelled = cancelCondition();
101 pollfd pfd{fd, POLLIN, 0};
102 int ready = poll(&pfd, 1, 100);
105 if (errno == EINTR)
continue;
107 rclcpp::get_logger(
"smacc2"),
108 "[ClRosLaunch2] poll() failed: " << std::generic_category().message(errno));
111 if (ready == 0)
continue;
113 ssize_t bytesRead = read(fd, buffer.data(), buffer.size());
116 result.append(buffer.data(),
static_cast<size_t>(bytesRead));
118 else if (bytesRead == 0)
121 rclcpp::get_logger(
"smacc2"),
"[ClRosLaunch2] launch process closed its output");
124 else if (errno != EINTR && errno != EAGAIN)
127 rclcpp::get_logger(
"smacc2"),
128 "[ClRosLaunch2] read() failed: " << std::generic_category().message(errno));
133 rclcpp::sleep_for(2s);
137 rclcpp::sleep_for(2s);
141 pid_t child_pid = child.pid;
142 if (waitpid(child_pid, &status, 0) != -1)
144 if (WIFEXITED(status))
146 int exit_status = WEXITSTATUS(status);
148 rclcpp::get_logger(
"smacc2"),
"Child process exited with status: %d", exit_status);
150 else if (WIFSIGNALED(status))
152 int term_signal = WTERMSIG(status);
154 rclcpp::get_logger(
"smacc2"),
"Child process terminated by signal: %d", term_signal);
159 RCLCPP_ERROR(rclcpp::get_logger(
"smacc2"),
"Error waiting for child process.");
166 RCLCPP_WARN_STREAM(rclcpp::get_logger(
"smacc2"),
"[ClRosLaunch2] RESULT:\n" << result);
231 std::string command =
"pgrep -P " + std::to_string(pid);
232 FILE * pipe = popen(command.c_str(),
"r");
236 std::cerr <<
"Error executing pgrep command." << std::endl;
241 std::string result =
"";
243 while (fgets(buffer,
sizeof(buffer), pipe) != NULL)
250 std::istringstream iss(result);
252 std::vector<pid_t> childPids;
254 while (iss >> childPid)
256 childPids.push_back(childPid);
260 for (
const pid_t & child : childPids)
266 int res = kill(pid, SIGTERM);
269 RCLCPP_FATAL(rclcpp::get_logger(
"smacc2"),
"Killed process %d", pid);