/*
From: jln2@cec2.wustl.edu (Sammy D.)
http://groups.google.com/groups?selm=1993Sep14.212150.1479%40wuecl.wustl.edu
*/

void
daemonize()
{
	int pid, tty;

	/* clone ourself */
	pid = fork();
	if ( pid < 0 )
	{
		perror( "fork" );
		exit( 1 );
	}
	else if ( pid > 0 )
	{
		/* Parent just exits. */
		exit( 0 );
	}

	/* Go stealth (ditch our controlling tty) */
	for (tty=0; tty<3; tty++) close(tty);
	setpgrp(0, getpid());
	if ((tty = open("/dev/tty", 0) >= 0))
	{
		ioctl(tty, TIOCNOTTY, 0);
		(void) close( tty );
	}
}

