What this PHP code does? - php

I read What's the most dangerous/worst PHP script you've ever seen? on quora.
It had following code as answer with a upvote.
<?php
`:(){ :|: & };:`;
Even I have worked with PHP more than 3 years I have no idea what this code does and I am little bit scare to run on it localhost too because I don't know what it do.
What is that code?

This question was previously asked and answered on Ask Ubuntu.
In his answer there, SuperMatt writes:
This is called a fork bomb.
:() means you are defining a function called :
{:|: &} means run the function : and send its output to the :
function again and run that in the background.
The ; is a command separator, like &&.
: runs the function the first time.
Essentially you are creating a function that calls itself twice every
call and doesn't have any way to terminate itself. It will keep
doubling up until you run out of system resources.
Running in Virtualbox was quite sensible really otherwise you would
have had to restart your pc.

Related

Why does my php script terminate using nohup via PHP CLI

This is more a question about nohup than my PHP script although I will include code for you guys to see. I am using a script what is designed to never end, meaning script termination should never take place. In an ideal world the script would run forever. This is achieved with <?php while (true) {} ?> which I am led to believe is the correct way of doing this?
However I am finding my script is terminating for reasons unknown every few days. The longest the script has run for is 4 days. I am left baffled and unable to reproduce test case scenarios without the aid of having the output from the process at the time of termination. Does nohup allow you to see what happens when the process terminates?
I can see the process running when I do ps aux and once the script has finished execution it disappears from the ps aux list, suggesting that the problem is with the she'll environment the script is run in rather than any portion of my code?
Can anybody help. Any debugging tools for this would be appreciated.
EDIT: I am looking for tools to debug this scenario any help appreciated.
The problem here was with MySQL. MySQL needed to be configured not to die after a long session. (or re-establish periodically)
Use show variables like 'wait_timeout'; to see what your setup is configured for.

executing a command line in codeigniter - PHP

I have a Codeigniter framework on place, which I'm building a test framework on top of it.
Once of the features of the test framework is to remotely deploy test suits. Therefore, I'm trying to deploy a Python test suit I wrote from the webpage:
public function deploy_test()
{
if( !$this->input->is_cli_request() ) {
echo json_encode(system("python test_suit.py"));
}
}
from my controller, so, when a test deployed, I want to run it. For proof of concept I did that way. However, the job never get deployed, nor is the command line ever executed.
When I ran that piece of code from the terminal, it works as supposed. What am I missing here?
Thank you
** Its curious to mention when I execute ls -l it works ...
Basically, this is a bad idea - if the host has declined access to executing things on the server (after all, how does it know that your code is not malicious), you really shouldn't try to work around this, at least without speaking to your provider and asking them if they're ok for you to do this!
After all, executing an LS isn't really going to be malicious, but you could try and execute a Python script that 'rm -rf's the server.
Now, you might be able to find a work around, but to my mind, the question is should you? They might interpret this as a hacking attempting.

How to know where the script stopped?

I was wondering if there was a way to know where the script stopped (ie: file + line), which would be useful for debugging or for removing stupid 'exit' calls lost somewhere in the code.
Thanks in advance
Wrong (and I'm sorry for not testing it first): You could use register_shutdown_function in conjunction with debug_backtrace.
See here for a duplicate of your question: Fastest way to determine where PHP script exits .
If you want to remove exit from the script, try using PHP Code Sniffer PEAR package http://pear.php.net/package/PHP_CodeSniffer
Just create a sniffer to find out where all the exits are in the code (you get a report of file and line).
If you want to find out what line a script stopped at, use a debugger and you can get a stack trace to find out the last line a script executed too (Xdebug is easy to use and set up). Any debugger is going to severely hinder performance as it needs to manage more memory.
I'm not sure what IDE your using (if any), but this would be trivial using xdebug. I personally use it with netbeans and it works great although it is a bit tricky to setup. It will let you walk through your code step by step and show exactly where it is exiting.

Launch a process and get its return code or interrupt it if it lasts too long

I have a PHP script launched from a command-line (aka CLI mode, no webserver involved).
In this script I launch a command that will run from some time, usually exiting after a couple minutes. But at times, this command will run for hours because of various issues, and the best I can do is kill it and wait for a while before launching it again.
Two things I want to emphasize :
I have no control of the code inside that command, and can't improve failure detection.
It's not an important task, and it's perfectly ok to have it working that way.
That being said, I would like to improve things in my code, so that I can kill the child process if the command has been running for more than N seconds. But I still want to get the return code from the command, when it runs fine.
Pseudo-code should be something like this :
Launch command
While command is running
{
If the command is done running
{
echo return code
}
else
{
If the command has been running for more than N seconds
{
Kill the child process
}
}
}
How would you implement this in PHP ?
Thank you !
Solution : I ended up using the SIG_ALERT signal. More info on the signals handling and the pcntl lib in the pages provided by Gordon in his post.
Read through this Chapter of Practical PHP Programming.
It covers interoperating with Processes extensively. And also have a look at the PHP Manual's pages on Process Control.
You might also be interested in Gearman
Sorry for not providing any code. I never had the need for Process Control stuff, so I just kept in memory where to look in case I'd ever need it.

Don't wait for the process to exit

I have a PHP script that is called from a cron job every minute. This script takes some info from the database and then calls another PHP script using the System function (passing it some parameters).
That means that I can start up to 10 scripts from this "main" one. And what I would like to do is that I would call the script and continue the execution of the main script, that is, not wait for the System call to complete and then call the next one.
How can this be done?
You may be able to use proc_open(), stream_select() and stream_set_blocking() in concert to achieve this kind of thing.
If that sounds vague, I was going to paste a big chunk of code in here that I used in a recent project that did something similar, but then felt it may hinder rather than help! In summary though, the code worked like this:
cronjob calls cronjob_wrapper.php
cronjob_wrapper.php creates a new Manager class and then calls start on it.
Manager class start method check to see how many instances are running (looking for pid files in a particular location). If it's less than a given max number of instances it writes out it's own process id to a pid file and then carries on
Manage class creates an instance of an appropriate Encoder class and calls exec on it.
The exec method uses proc_open, stream_select and stream_set_blocking to run a system command in a non-blocking fashion (running ffmpeg in this case - and could take quite a while!)
When it has finally run it cleans up its PID file and bails out.
Now the reason I'm being vague and handwavy is that our multiple instances here are being handled by the cronjob not by PHP. I was trying to do very much the kind of thing you are talking about, and got something working pretty well with pcntl_fork() and friends, but in the end I encountered a couple of problems (if I recall at least one was a bug in PHP) and decided that this approach was a much more rock-solid way to achieve the same thing. YMMV.
Well worth a look at those functions though, you can achieve a lot with them. Though somehow I don't think PHP will ever become the sockets programming language of choice... :)
If your OS supports it, you can use the pcntl_fork() function to spin off child processes that the parent doesn't wait for. Be careful though, it is easy to accidentally create too many child processes, especially if they take longer than expected to run!
I think the answer would be very similar to those already provided for Asynchronous PHP calls.
http://php.net/pcntl_fork
It's *NIX only but you can fork your script using the PCNTL extension.
I'm not sure that PHP supports threading. Check here.
You could run them in the background:
system('php yourscript.php &');
You just have to make sure that you check on the total number of processes running. All in all, not a super elegant solution. Instead cron you could let one script run for forever, I am thinking something like this:
<?php
while(true) {
// do whatever needs to be done.
}
?>
Careful though. PHP is not exactly known to be used as a daemon.
use php's version of fork or threads.

Categories