Re: [PHP-INSTALL] Questions regarding limits of processes launched by system, exec, passthru ...

[email protected] (Keith Roberts)
Newsgroups php.install
Message-ID <[email protected]>
On Thu, 25 Sep 2008, Valentin Schmid - ICSurselva AG wrote:

> To: [email protected]
> From: Valentin Schmid - ICSurselva AG <[email protected]>
> Subject: [PHP-INSTALL] Questions regarding limits of processes launched by
>     system, exec, passthru ...
> 
> Hello all,
>
> Is there a way to limit the memory consumption and / or the CPU
> consumption of processes launched by the php functions system,
> exec, passthru, proc_open and shell_exec?
>
> We use mod_php with an apache (mpm-prefork) on Linux.
>
> The following settings don't have any effect at all:
> PHP:
>  max_execution_time 30
>  memory_limit 8M
> Apache:
>  RLimitCPU 30 30
>  RLimitMEM 8388608 8388608
>
> The limits above do have effect on php-scripts (without system calls)
> and on CGIs (as well on processes launched by CGIs).
>
> Any Ideas?
>
> Kind Regards
> valli
>
>
> PS: I tested it with the following two scripts:
> system_memorytest.php
> =====================
> <html>
> <head>
>  <title>php-systemcall-memory test</title>
> </head>
> <body>
>  php-systemcall-memory test<br>
>  ... and here's the system call:<br>
>  <pre>
> <?php
>   $cmd = '/usr/bin/perl -e \'
>      $| = 1;
>      print "start of the systemcall<br>\n";
>      $s = "teststr_";
>      while (1) {
>         print "len=".length($s)."<br>\n";
>         sleep(1);
>         $s .= $s;
>      }
>   \'';
>   print htmlspecialchars($cmd);
> ?>
>  </pre>
> <?php
>  ob_flush();
>  flush();
>  system($cmd);
> ?>
> </body>
> </html>
>
>
> system_timeouttest.php
> ======================
> <html>
> <head>
>  <title>php-systemcall-timeout test</title>
> </head>
> <body>
>  php-systemcall-timeout test<br>
>  ... and here's the system call:<br>
>  <pre>
> <?php
>   $cmd = '/usr/bin/perl -e \'
>      $| = 1;
>      print "start of the systemcall<br>\n";
>      $i = 0;
>      while (1) {
>         if (($i % 10000000) == 0) {
>            print "i=".$i."<br>\n";
>         }
>         $i += 1;
>      }
>   \'';
>   print htmlspecialchars($cmd);
> ?>
>  </pre>
> <?php
>  ob_flush();
>  flush();
>  system($cmd);
> ?>
> </body>
> </html>
>

What OS are you running? If it's Unix/Linux then you might 
be able to control these settings with ulimit.

Please see the manual page for ulimit for details.

'pinfo' is a lynx-style info page browser that I find 
very usefull for browsing info manual pages on Linux.

from the Linux info pages for ulimit:

Here is a list of resources for which you can specify a 
limit. Memory and file sizes are measured in bytes.

`RLIMIT_CPU'
The maximum amount of CPU time the process can use.  If it 
runs for longer than this, it gets a signal: `SIGXCPU'. 
The value is measured in seconds.  *Note Operation Error 
Signals::.

`RLIMIT_FSIZE'
The maximum size of file the process can create. Trying to 
write a larger file causes a signal: `SIGXFSZ'.  *Note 
Operation Error Signals::.

`RLIMIT_DATA'
The maximum size of data memory for the process.  If the 
process tries to allocate data memory beyond this amount, 
the allocation function fails.

`RLIMIT_STACK'
The maximum stack size for the process.  If the process 
tries to extend its stack past this size, it gets a 
`SIGSEGV' signal. *Note Program Error Signals::.

`RLIMIT_CORE'
The maximum size core file that this process can create. 
If the process terminates and would dump a core file larger 
than this, then no core file is created.  So setting this 
limit to zero prevents core files from ever being created.

`RLIMIT_RSS'
The maximum amount of physical memory that this process 
should get. This parameter is a guide for the system's 
scheduler and memory allocator; the system may give the 
process more memory when there is a surplus.

`RLIMIT_MEMLOCK'
The maximum amount of memory that can be locked into 
physical memory (so it will never be paged out).

`RLIMIT_NPROC'
The maximum number of processes that can be created with the 
same user ID.  If you have reached the limit for your user 
ID, `fork' will fail with `EAGAIN'.  *Note Creating a 
Process::.

`RLIMIT_NOFILE'
`RLIMIT_OFILE'
The maximum number of files that the process can open.  If 
it tries to open more files than this, its open attempt 
fails with `errno' `EMFILE'.  *Note Error Codes::.  Not all 
systems support this limit; GNU does, and 4.4 BSD does.

`RLIMIT_AS'
The maximum size of total memory that this process should 
get. If the process tries to allocate more memory beyond 
this amount with, for example, `brk', `malloc', `mmap' or 
`sbrk', the allocation function fails.

`RLIM_NLIMITS'
The number of different resource limits.  Any valid RESOURCE 
operand must be less than `RLIM_NLIMITS'.

-- Constant: int RLIM_INFINITY
This constant stands for a value of "infinity" when 
supplied as the limit value in `setrlimit'.

`ulimit' and the command symbols are declared in `ulimit.h'.

HTH

Kind Regards,

Keith Roberts

-----------------------------------------------------------------
Websites:
http://www.php-debuggers.net
http://www.karsites.net
http://www.raised-from-the-dead.org.uk

The mind of the prudent is ever getting knowledge, and the
eear of the wise is ever seeking, inquiring for and craving
knowledge. Pr. 18:15 Amp

All email addresses are challenge-response protected with
TMDA [http://tmda.net]
-----------------------------------------------------------------
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.