SMACC2
Loading...
Searching...
No Matches
Classes | Functions
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 258 of file smacc_ros_launch_client_2.cpp.

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 214 of file smacc_ros_launch_client_2.cpp.

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

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)

Aditional functions

Definition at line 170 of file smacc_ros_launch_client_2.cpp.

171{
173 info.pid = -1; // Inicializar el PID a -1 (indicando error)
174 info.pipe = nullptr;
175
176 int pipefd[2]; // Descriptor de archivo para el pipe
177
178 if (pipe(pipefd) == -1)
179 {
180 perror("pipe");
181 return info;
182 }
183
184 pid_t pid = fork();
185
186 if (pid == 0)
187 {
188 // Esto se ejecuta en el proceso hijo
189 close(pipefd[0]); // Cerramos el extremo de lectura del pipe en el proceso hijo
190 dup2(pipefd[1], STDOUT_FILENO); // Redireccionamos la salida estándar al pipe
191 close(pipefd[1]); // Cerramos el extremo de escritura del pipe en el proceso hijo
192
193 execl("/bin/sh", "/bin/sh", "-c", command, nullptr); // Ejecuta el comando dado
194
195 // Si execl retorna, significa que hubo un error
196 std::cerr << "Error al ejecutar el comando: " << command << std::endl;
197 _exit(1); // Usar _exit para evitar la ejecución de códigos de salida de manejo de errores
198 }
199 else if (pid > 0)
200 {
201 // Esto se ejecuta en el proceso padre
202 close(pipefd[1]); // Cerramos el extremo de escritura del pipe en el proceso padre
203 info.pid = pid;
204 info.pipe = fdopen(pipefd[0], "r"); // Abrir el descriptor de archivo de lectura en modo texto
205 }
206 else
207 {
208 std::cerr << "Error al crear el proceso hijo." << std::endl;
209 }
210
211 return info;
212}

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

Here is the caller graph for this function: