[comp.lang.perl] How do I detach from control terminal?

kai@sp1.csrd.uiuc.edu (Kuck And Associates) (08/07/90)

I've written in Perl the equivalent of the C code I have used
in a daemon to detach from the control terminal, but it doesn't
seem to work (on a BSD Unix host).

Can anyone tell me what I'm doing wrong in this example?

	Patrick Wolfe   (pat@kai.com, kailand!pat)
	System Programmer/Operations Manager, Kuck & Associates
	"Any opinions expressed are my own, not my employers's.
	 Please don't call my boss and complain ... again."


--  cut here  --  cut here  --  cut here  --  cut here  --
#!/usr/local/bin/perl3
$logfile = "/dev/null";
do detach ();
sleep (600);
exit (0);

# behave like a proper daemon - set our own process group and detach from our control terminal
sub detach
{
chdir ("/");
open (STDIN, "< /dev/null") || die "cannot redirect stdin from /dev/null: $!";
open (STDOUT, "> $logfile") || die "cannot redirect stdout to $logfile: $!";
open (STDERR, ">&STDOUT") || die "cannot dup stderr to stdout: $!";
select (STDERR); $| = 1;
select (STDOUT); $| = 1;

if (open (TTY, "> /dev/tty")) {
	do "sys/ioctl.h";	# probably /usr/local/lib/perl/sys/ioctl.h
	if ($@ ne "") {
		print STDERR "check_uptime: cannot \"do sys/ioctl.h\": $@\n";
		print STDERR "you probably need to run \"makelib sys/ioctl.h\"\n";
		exit (1);
		}
	ioctl (TTY, $TIOCNOTTY, 0);
	close (TTY);
	}

setpgrp (0, $$);
}

lwall@jpl-devvax.JPL.NASA.GOV (Larry Wall) (08/07/90)

In article <1990Aug6.190050.2706@csrd.uiuc.edu> kai@sp1.csrd.uiuc.edu (Kuck And Associates) writes:
: I've written in Perl the equivalent of the C code I have used
: in a daemon to detach from the control terminal, but it doesn't
: seem to work (on a BSD Unix host).
: 
: Can anyone tell me what I'm doing wrong in this example?

: 	do "sys/ioctl.h";	# probably /usr/local/lib/perl/sys/ioctl.h
: 	if ($@ ne "") {
: 		print STDERR "check_uptime: cannot \"do sys/ioctl.h\": $@\n";
: 		print STDERR "you probably need to run \"makelib sys/ioctl.h\"\n";
: 		exit (1);
: 		}
: 	ioctl (TTY, $TIOCNOTTY, 0);

That should probably be &TIOCNOTTY.

After the next patch, you'll want to turn those lines into

	require "sys/ioctl.ph";
	ioctl(TTY, &TIOCNOTTY, 0);

Note the .ph extension change also.

Larry