1
My version:
//test.c (04/07/2019)
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/types.h>
void stop_attack(pid_t child){
int wstatus;
system("killall xerxes");
if(kill(child, SIGTERM)!=0){
kill(child, SIGKILL);
}
wait(&wstatus);
}
int main(void){
const time_t timer=3600;
pid_t child=fork();
if(child<0){
exit(EXIT_FAILURE);
}else if(child==0){
for(time_t i=0; i<=timer; i++){
usleep(1000000);
}
__kill(child);
}else{
system("./xerxes 127.0.0.1 80");
}
return EXIT_SUCCESS;
}
In the above case I used a system call (system("killall xerxes")
), but I’m not sure if this is an efficient and safe approach, as there may be situations where this may fail.
There is a more efficient and secure way to kill an external process via a program written in C, without the need to use the function system()
?