41 std::lock_guard<std::mutex> lock(
mutex_);
45 RCLCPP_INFO(
getLogger(),
"CpMicroRosAgent: already launched, skipping");
52 if (pipe(pipefd) == -1)
54 RCLCPP_ERROR(
getLogger(),
"CpMicroRosAgent: pipe() failed: %s", strerror(errno));
64 dup2(pipefd[1], STDOUT_FILENO);
65 dup2(pipefd[1], STDERR_FILENO);
68 execl(
"/bin/sh",
"/bin/sh",
"-c",
command_.c_str(),
nullptr);
81 int flags = fcntl(
pipeFd_, F_GETFL, 0);
82 fcntl(
pipeFd_, F_SETFL, flags | O_NONBLOCK);
86 std::thread readerThread(
92 ssize_t n = read(
pipeFd_, buf,
sizeof(buf) - 1);
96 RCLCPP_DEBUG(
getLogger(),
"[micro_ros_agent] %s", buf);
100 std::this_thread::sleep_for(std::chrono::milliseconds(100));
104 readerThread.detach();
111 RCLCPP_ERROR(
getLogger(),
"CpMicroRosAgent: fork() failed: %s", strerror(errno));
170 std::string command =
"pgrep -P " + std::to_string(pid);
171 FILE * pipe = popen(command.c_str(),
"r");
181 while (fgets(buffer,
sizeof(buffer), pipe) != NULL)
188 std::istringstream iss(result);
190 std::vector<pid_t> childPids;
192 while (iss >> childPid)
194 childPids.push_back(childPid);
198 for (
const pid_t & child : childPids)
204 int res = kill(pid, SIGTERM);
207 RCLCPP_FATAL(rclcpp::get_logger(
"CpMicroRosAgent"),
"Killed process %d", pid);