Hack book of
A zombie is a processus which is neither active, nor finished: between
life and death.
allows to turn a PHP execution into a zombie!
Table of contents
Putting a processus between life and death should be, in general, avoided,
because it is often synonym of a dysfunction. However, it
might happen to be shrewd, notably in the case of a demon
It is important to notice that a zombie is also mute: it can no longer write
on its standard output because it is theorically no longer attached to a
Hoa\Zombie is able to create zombies only if it runs
behind PHP FPM. Remind us how PHP
works: a FastCGI server (such as PHP FastCGI
or PHP FPM) will start a PHP processus when a request is
received and will stop it when a response is sent. Somehow,
the PHP processus is synchronized on the FastCGI protocol.
Fortunately, when we use PHP FPM, we are able to
desynchronize this protocol, and thus, to create zombies,
while delegating the
processus management to PHP FPM!
Creating a zombie is very simple: we only need to call the
It is common knowledge that killing a zombie is a very
attractive activity, even more if we have the choice of
weapons! That's why
the following methods:
Hoa\Zombie\Zombie::decapitate, to decapitate a zombie,
Hoa\Zombie\Zombie::bludgeon, to cosh a zombie,
Hoa\Zombie\Zombie::burn, to burn a zombie,
Hoa\Zombie\Zombie::explode, to explode a zombie,
Hoa\Zombie\Zombie::cutOff, to slice a zombie.
All these methods have been proven. Thus:
// I'm alive! Hoa\Zombie\Zombie::fork(); // I'm a zombie! Hoa\Zombie\Zombie::decapitate(); // I'm dead…
Hoa\Zombie\Zombie is also able to get the
identifier of the processus, which can be useful sometimes,
Hoa\Zombie\Zombie::getPid method. Finally, the
Hoa\Zombie\Zombie::test method allows to know whether we can
create a zombie or not.
To create a zombie, we said we need to use PHP FPM. Next, there is different ways to execute our PHP program, which will be our future zombie: either through HTTP (which will automatically create a FastCGI request to PHP FPM) or directly by sending a FastCGI request to PHP FPM. We will choose this latter.
We start with the
echo 'I guess I am sick…', "\n"; Hoa\Zombie\Zombie::fork(); // Do whatever you want here, e.g.: sleep(10); file_put_contents( __DIR__ . DS . 'AMessage', 'Hello from after-life… or somewhere about!' );
Next, we continue with the
Run.php file which will execute
Zombie.php thanks to
$fastcgi = new Hoa\Fastcgi\Responder( new Hoa\Socket\Client('tcp://127.0.0.1:9000') ); // Start Zombie.php and print the response before it becomes undead. echo $fastcgi->send([ 'GATEWAY_INTERFACE' => 'FastCGI/1.0', 'REQUEST_METHOD' => 'GET', 'SCRIPT_FILENAME' => __DIR__ . DS . 'Zombie.php' ]);
And finally, we test:
$ php-fpm -d listen=127.0.0.1:9000 $ php Run.php I guess I am sick…
After 10 seconds, we will see the
AMessage file appears,
containing Hello from after-life… or somewhere about!.
We can create a zombie at any moment and kill it as we want. If the end of the program is reached, the zombie will kill itself. We will anticipate that a demon is just a zombie that starts a server.
Hoa\Zombie allows to turn a processus into a
zombie only when it is used with PHP FPM. We invite you to
take a look at
a complete example.
An error or a suggestion about the documentation? Contributions are welcome!