hang on exit bug under Linux

Damien Miller djm at mindrot.org
Wed Dec 12 10:42:18 EST 2001


On Tue, 11 Dec 2001, Michael wrote:

> unless ($pid = fork) {
>    unless (fork) {
>        open(SDOUT,'>/dev/null');
>        open(STDERR,'>/dev/null');
>        open (X, 'some_process 2>&1 |'); # that generates stdio to X 
>        while (X) {   # real program uses select
>              do something
>        }
>      # dies
>        exit 0;
>    }
>    waitpid($pid,0);
>    exit 0;
> }
> 
> 
> This process will hang ssh, it should not. ....or please tell me why 
> it should.

It doesn't close or redirect stdin. Here is a better one, which doesn't
cause ssh to linger:

------------ daemon.pl

#!/usr/bin/perl -wT

use strict;
use POSIX 'setsid';

my $prog = shift @ARGV;

defined($prog)			or die "Usage: daemon.pl program [args]\n";
(-x $prog)			or die "Can't execute $prog";

chdir '/'			or die "Can't chdir to /: $!";
open STDIN, '/dev/null'		or die "Can't read /dev/null: $!";
open STDOUT, '>/dev/null'	or die "Can't write to /dev/null: $!";
defined(my $pid = fork)		or die "Can't fork: $!";
exit if $pid;
setsid				or die "Can't start a new session: $!";
open STDERR, '>&STDOUT'		or die "Can't dup stdout: $!";

exec $prog, @ARGV		or die "Can't execute: $!";

------------

-d

-- 
| By convention there is color,       \\ Damien Miller <djm at mindrot.org>
| By convention sweetness, By convention bitterness, \\ www.mindrot.org
| But in reality there are atoms and space - Democritus (c. 400 BCE)




More information about the openssh-unix-dev mailing list