php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #30539 shell doesnt close the connex after forking a process
Submitted: 2004-10-23 15:01 UTC Modified: 2005-03-07 22:32 UTC
Votes:1
Avg. Score:5.0 ± 0.0
Reproduced:0 of 0 (0.0%)
From: toppi at kacke dot de Assigned:
Status: Not a bug Package: *General Issues
PHP Version: 5.0.2 OS: Fedora Core 2 Linux
Private report: No CVE-ID: None
 [2004-10-23 15:01 UTC] toppi at kacke dot de
Description:
------------
After forking a process and setting the child as sessionleader (becomes deamon) you cant disconnect from the current terminal-session (exit) solong the child is in process.

After typing exit, the terminal prints the output from the child again. Youre always connected but no more control

Current Config:

[simon@vegeta simon]$ php -v
PHP 5.0.2 (cgi) (built: Oct 23 2004 14:07:24)
Copyright (c) 1997-2004 The PHP Group
Zend Engine v2.0.2, Copyright (c) 1998-2004 Zend Technologies


Reproduce code:
---------------
#!/usr/local/bin/php -q
<?PHP

declare (ticks = 1);
set_time_limit (0);
ob_implicit_flush ();
error_reporting(E_ALL);

if( ($pid = pcntl_fork()) == -1 ) {
	echo "Error: Cant fork myself ! Dying.... \n";
}
elseif($pid) {
   	//PARENT PROCESS
   		echo "Parent-Pid: ".getmypid()." quits here\n";
		exit(0);
} else {
   	//CHILD PROCESS
   	// detatch from the controlling terminal
	if (!posix_setsid()) {
   		die("could not detach from terminal");
	}
	//Install Signalhandler
	pcntl_signal(SIGTERM, "sig_handler");
	pcntl_signal(SIGCHLD, SIG_IGN);
 	$i=0;
   	$childpid = getmypid();

	//Do something for testing
   	while ($i<20){
   		sleep(1);
   		echo "Child-Pid $childpid ist working -> type exit to quit your shell-session ->it doesnt close the connex \n";
   		$i++;
   	}
   	
   	echo "Just now we get diconnectet from the shell\n";
   	exit(0);
}

function sig_handler($signo) {
     switch($signo) {
         case SIGTERM:
             exit;
         case SIGCHLD:
             while( pcntl_waitpid(-1,$status,WNOHANG)>0 ) { }
             break;
         default:
             break;
     }
}

Expected result:
----------------
closing shell (telnet/ssh) and the child doing his background work

Actual result:
--------------
when starting ./testd.php (above) it looks

7360 simon     15   0  7308 2060 6856 S  0.0  0.2   0:00.00 sshd
7361 simon     25   0  4396 1340 3948 S  0.0  0.1   0:00.00 bash
7976 simon     16   0 10012 3452 8872 S  0.0  0.3   0:00.00 testd.php

when "EXIT" the shell
7360 simon     15   0  7308 2060 6856 S  0.0  0.2   0:00.00 sshd <--- left

solong the child repeats 20 times
7360 simon     15   0  7308 2060 6856 S  0.0  0.2   0:00.00 sshd

when the child is done
everthing got closed and the terminal is disconnected

Patches

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2004-10-23 17:40 UTC] tony2001@php.net
fclose(STDIN); after successful fork() call should help you.
 [2004-10-23 20:28 UTC] toppi at kacke dot de
Hi Tony,

it doenst work.

STDIN is not defined/open. When i understood the manual correctly, its only defined when using cli sapi.

I have 
php -v
PHP 5.0.2 (cgi) (built: Oct 23 2004 14:07:24)

I tired a lot of things, to kill the stream and getting disconnected...but nothing helped yet.

When i remember me right, we didnt have that prob @ 4.3.x. but not sure tho.

I have no clue anymore :)
 [2004-10-24 06:53 UTC] tony2001@php.net
Use CLI then? Personally I do not think that CGI version is the best choice for daemons, as it wasn't designed for such purposes.
 [2004-10-24 13:50 UTC] toppi at kacke dot de
I tired the code by using cli now. Same problem even when i close STDIN.

Also i tired it, by creating a shutdown_function()

register_shutdown_function(
       create_function('','fclose(STDIN); fclose(STDOUT); fclose(STDERR); return true;')
);

A test on another system, to make sure its not a os-config thingo, gives the same result.

Run the code as root (to make sure, its no perm. thing) doesnt help tho.

The child always post his outputstream ahead tru sshd (but no bash alive). 

When creating a socket around the child and you just close the terminal-window to get disconnected from the master, the code chrashes on connect.
 [2004-10-26 17:16 UTC] lobbin at localhost dot nu
I've seen this problem pretty often, it might be related to (open)ssh and your shell.

More info here:
http://www.openssh.com/faq.html#3.10
 [2004-10-27 21:15 UTC] toppi at kacke dot de
Thanks for the hint lobbin.

i tired the shopt -s huponexit

but Shell says its turned on but....didnt help out tho.
 [2004-10-27 22:50 UTC] toppi at kacke dot de
I forgott... 

a nohup ./testd.php also doesnt do it

seems theres no chance to fork a process when using sshd atm.

I would be very thankful about any "workaround" or any tip, behavior

Thank you guys

Toppi
 [2005-03-07 22:32 UTC] sniper@php.net
This is not PHP bug as shown by the FAQ entry lobbin posted.

 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Aug 15 13:01:28 2024 UTC