67{
68 return std::async(
69 std::launch::async,
70 [packageName, launchFileName, cancelCondition, client]()
71 {
72 RCLCPP_WARN_STREAM(rclcpp::get_logger("smacc2"), "[ClRosLaunch2] Starting ros launch thread");
73
74 std::stringstream cmd;
75 cmd << "ros2 launch " << packageName << " " << launchFileName;
76 std::array<char, 128> buffer{};
77 std::string result;
78
80
81 if (!child.pipe)
82 {
83
84 RCLCPP_ERROR_STREAM(
85 rclcpp::get_logger("smacc2"), "[ClRosLaunch2] could not start: " << cmd.str());
86 return result;
87 }
88 if (client != nullptr)
89 {
90 client->launchPid_ = child.pid;
91 }
92
93 int fd = fileno(child.pipe.get());
94 bool cancelled = false;
95
96
97
98 while (!cancelled)
99 {
100 cancelled = cancelCondition();
101 pollfd pfd{fd, POLLIN, 0};
102 int ready = poll(&pfd, 1, 100);
103 if (ready < 0)
104 {
105 if (errno == EINTR) continue;
106 RCLCPP_ERROR_STREAM(
107 rclcpp::get_logger("smacc2"),
108 "[ClRosLaunch2] poll() failed: " << std::generic_category().message(errno));
109 break;
110 }
111 if (ready == 0) continue;
112
113 ssize_t bytesRead = read(fd, buffer.data(), buffer.size());
114 if (bytesRead > 0)
115 {
116 result.append(buffer.data(), static_cast<size_t>(bytesRead));
117 }
118 else if (bytesRead == 0)
119 {
120 RCLCPP_INFO(
121 rclcpp::get_logger("smacc2"), "[ClRosLaunch2] launch process closed its output");
122 break;
123 }
124 else if (errno != EINTR && errno != EAGAIN)
125 {
126 RCLCPP_ERROR_STREAM(
127 rclcpp::get_logger("smacc2"),
128 "[ClRosLaunch2] read() failed: " << std::generic_category().message(errno));
129 break;
130 }
131 }
132
133 rclcpp::sleep_for(2s);
134 if (child.pid > 0)
135 {
137 rclcpp::sleep_for(2s);
138 }
139
140 int status = 0;
141 pid_t child_pid = child.pid;
142 if (waitpid(child_pid, &status, 0) != -1)
143 {
144 if (WIFEXITED(status))
145 {
146 int exit_status = WEXITSTATUS(status);
147 RCLCPP_INFO(
148 rclcpp::get_logger("smacc2"), "Child process exited with status: %d", exit_status);
149 }
150 else if (WIFSIGNALED(status))
151 {
152 int term_signal = WTERMSIG(status);
153 RCLCPP_WARN(
154 rclcpp::get_logger("smacc2"), "Child process terminated by signal: %d", term_signal);
155 }
156 }
157 else
158 {
159 RCLCPP_ERROR(rclcpp::get_logger("smacc2"), "Error waiting for child process.");
160 }
161
162
163
164 child.pipe.reset();
165
166 RCLCPP_WARN_STREAM(rclcpp::get_logger("smacc2"), "[ClRosLaunch2] RESULT:\n" << result);
167
168 return result;
169 });
170}
void killGrandchildren(pid_t originalPid)
ProcessInfo runProcess(const char *command)