SMACC2
Loading...
Searching...
No Matches
smacc2::client_bases Namespace Reference

Classes

class  ClRosLaunch
 
class  ClRosLaunch2
 
class  ISmaccActionClient
 
struct  ProcessInfo
 
class  SmaccActionClientBase
 
class  SmaccPublisherClient
 
class  SmaccServiceClient
 
class  SmaccServiceServerClient
 
class  SmaccSubscriberClient
 

Functions

ProcessInfo runProcess (const char *command)
 
void killGrandchildren (pid_t originalPid)
 
void killProcessesRecursive (pid_t pid)
 

Function Documentation

◆ killGrandchildren()

void smacc2::client_bases::killGrandchildren ( pid_t originalPid)

Definition at line 249 of file smacc_ros_launch_client_2.cpp.

249{ killProcessesRecursive(originalPid); }

References killProcessesRecursive().

Referenced by smacc2::client_bases::ClRosLaunch2::executeRosLaunch().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ killProcessesRecursive()

void smacc2::client_bases::killProcessesRecursive ( pid_t pid)

Definition at line 205 of file smacc_ros_launch_client_2.cpp.

206{
207 std::string command = "pgrep -P " + std::to_string(pid);
208 FILE * pipe = popen(command.c_str(), "r");
209
210 if (!pipe)
211 {
212 std::cerr << "Error executing pgrep command." << std::endl;
213 return;
214 }
215
216 char buffer[128];
217 std::string result = "";
218
219 while (fgets(buffer, sizeof(buffer), pipe) != NULL)
220 {
221 result += buffer;
222 }
223
224 pclose(pipe);
225
226 std::istringstream iss(result);
227 pid_t childPid;
228 std::vector<pid_t> childPids;
229
230 while (iss >> childPid)
231 {
232 childPids.push_back(childPid);
233 }
234
235 // Kill child processes before killing the parent process
236 for (const pid_t & child : childPids)
237 {
239 }
240
241 // After killing all children, kill the parent process
242 int res = kill(pid, SIGTERM);
243 if (res == 0)
244 {
245 RCLCPP_FATAL(rclcpp::get_logger("smacc2"), "Killed process %d", pid);
246 }
247}

References killProcessesRecursive().

Referenced by killGrandchildren(), and killProcessesRecursive().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ runProcess()

ProcessInfo smacc2::client_bases::runProcess ( const char * command)

Additional functions

Definition at line 161 of file smacc_ros_launch_client_2.cpp.

162{
163 ProcessInfo info;
164 info.pid = -1; // Initialize PID to -1 (error sentinel)
165 info.pipe = nullptr;
166
167 int pipefd[2]; // File descriptors for the pipe
168
169 if (pipe(pipefd) == -1)
170 {
171 perror("pipe");
172 return info;
173 }
174
175 pid_t pid = fork();
176
177 if (pid == 0)
178 {
179 // Child process
180 close(pipefd[0]); // Close read end in child
181 dup2(pipefd[1], STDOUT_FILENO); // Redirect stdout to pipe
182 close(pipefd[1]); // Close write end in child
183
184 execl("/bin/sh", "/bin/sh", "-c", command, nullptr); // Execute the command
185
186 // execl only returns on error
187 std::cerr << "Error executing command: " << command << std::endl;
188 _exit(1); // Use _exit to skip at exit handlers
189 }
190 else if (pid > 0)
191 {
192 // Parent process
193 close(pipefd[1]); // Close write end in parent
194 info.pid = pid;
195 info.pipe = fdopen(pipefd[0], "r"); // Open read end as text-mode FILE
196 }
197 else
198 {
199 std::cerr << "Error creating child process." << std::endl;
200 }
201
202 return info;
203}

References smacc2::client_bases::ProcessInfo::pid, and smacc2::client_bases::ProcessInfo::pipe.

Referenced by smacc2::client_bases::ClRosLaunch2::executeRosLaunch().

Here is the caller graph for this function: