[ic] Server.pm
Rich Bodo
interchange-users@icdevgroup.org
Sat Sep 21 20:38:01 2002
Trying to get acquainted with the code. Got a few developer
questions.
First, is there a developers mailing list?
Second, does anyone meet on IRC anymore? I hang out on irc.cursor.biz
every day until the camper bot kicks me off, waiting for someone to
show.
Third, an actual question about the code: Why are two children forked
in Server.pm, run_server()? I'm looking at the following code from
the 4.8 version of Server.pm:
fcntl($pidh, F_SETFD, 0)
or die ::errmsg(
"Can't fcntl close-on-exec flag for '%s': %s\n",
$Global::PIDfile, $!,
);
my ($pid1, $pid2);
if ($pid1 = fork) {
# parent
wait;
sleep 2;
exit 0;
}
elsif (not defined $pid1) {
# fork error
print "Can't fork: $!\n";
exit 1;
}
else {
# child 1
if ($pid2 = fork) {
# still child 1
exit 0;
}
elsif (not defined $pid2) {
print "child 1 can't fork: $!\n";
exit 1;
}
else {
# child 2
sleep 1 until getppid == 1;
my $running = grab_pid($pidh);
if ($running) {
print errmsg(
"The Interchange server is already running (process id %s)\n",
$running,
);
exit 1;
}
print server_start_message(
"Interchange server started in %s mode(s) (process id %s)\n",
1,
) unless $Vend::Quiet;
setsid();
fcntl($pidh, F_SETFD, 1)
or die "Can't fcntl close-on-exec flag for '$Global::PIDfile': $!\n";
$next = server_both();
unlockfile($pidh);
opendir(RUNDIR, $Global::RunDir)
or die "Couldn't open directory $Global::RunDir: $!\n";
unlink $Global::PIDfile;
exit 0;
}
}
}
It appears to me that the above code basically does the following:
1) set close on exec of the current server (parent) to one
2) fork off child process 1 (child1)
3) parent waits for child1 to terminate then exits
4) child1 forks child2 and child1 exits immediately
5) child2 waits for it's parent to die then calls
server_both()
So the parent, will hang around until a grandkid to exist, then both
the parent and child will exit. Is my analysis incorrect, or is there
a reason for the family tree here?
BTW, I have taken it upon myself to understand the workings of
Interchange. If I can get access to just one interchange developer
for infrequent coding questions like this I will pay them back
with documentation and code in spades. Thanks.
-Rich
Rich Bodo | rsb@ostel.com | 650-964-4678