How to make the superman of the process, the one that once he’s running there is no way to kill it. Here’s a approach I implemented:
Definitions:
- main: the invincible program
- watchers: the two other programs that guarantee the main to be unkillable.
First of all the main program could be any program, it would be better if it doesn’t need to be in a specific directory to be able to run. The main program doesn’t need to be modified, this technique could be applied to any process, Firefox for example
Here’s how it goes, both watcher take the main’s pid and path as arguments and each other pid. So now we have 3 process running (the main and the two watchers). A watcher constantly ping the other watcher and the main process. When one of them is killed, the watcher will relaunch that program (either the main process or another watcher process).
At any point there should never be less than 2 process running. If any of the 3 get killed, it will re spawn right away.
The watcher uses a controller to dictate his behavior. The controller is made from a loop that:
1.check if the other watcher is still alive
2.check if the main process is still alive
3.wait a few micro second (avoid to overload the cpu)
4.loop back
To “ping” a process we use the kill function that send a given signal to a given process, instead of sending a regular signal we send the signal number 0. The function will return -1 if the signal could not be send, meaning that the receiver process is most likely not alive anymore.
while(loop == 1)
{
if( kill(watcher_pid, 0) == -1)
recover_watcher();
else if( kill(main_pid, 0) == -1)
recover_main();
else
usleep(50);
}
there is 2 tricky spots, spawning the watcher and spawning the main. These 2 operations are completely different and present different risks.
To spawn a new watcher, let’s call it bob, the remaining watcher, let’s call it raoul, need to make sure bob is not in the same process group. So that is a user ask to kill either bob’s process group or raoul’s it won’t kill both watcher. In order to achieve that raoul will create a child process with the fork function and that child will create bob’s process. Then the child will terminate. So bob won’t have a parent process anymore, it is now an orphelan and it is now in a different process group then raoul. Take a look at the following snippet:
//creating child
int status = 0;
status = fork();
if(status == -1) fail("fork() - firsh child");
else if(status == 0)
{
//now inside child and creating raould
status = fork();
if(status == -1) fail("fork() - raoul");
else if(status == 0)
{
//raoul will execute the main controller
mainController();
}
else
{
//termination child
usleep(5);
exit(EXIT_SUCCESS);
}
}
To spawn the main process, we just need to make sure that we don’t spawn it twice. That the two watcher are not both spawning the main process at the same time.
March 17, 2011 at 11:25 pm
The best to prove if you’re right or wrong will be to create the “Lex Luthor” program that will try to kill the invincible process.By doing some simultaneous attacks!
Let’s fight!