[erlang-bugs] Incomplete detaching from a controlling terminal

Sergei Golovan sgolovan@REDACTED
Sun Jul 20 06:28:46 CEST 2008


Hi!

Appears that if started with -detached option erlexec doesn't fully
detach from a controlling terminal which may end with killing a
detached service. For example, in Debian GNU/Linux (current unstable
or testing) installing an Erlang-based service (e.g. YAWS) leads to
killing the yaws daemon just after apt-get finishes.

I've asked in debian-devel mailing list showing the following code from erlexec:

 if (start_detached) {
   int status = fork();
   if (status != 0)
     return 0;
   status = fork();
   if (status != 0)
     return 0;

   close(0);
   open("/dev/null", O_RDONLY);
   close(1);
   open("/dev/null", O_WRONLY);
   close(2);
   open("/dev/null", O_WRONLY);
 }
 {
   execv(emu, Eargsp); /* executing the main Erlang emulator */
 }

and the answer was that a call to setsid() is missing just before the
seconf fork(), so the session leader remains unchanged and if it
finishes the current daemonized process gets a signal.

The minimal patch which fixes the issue for me is the following:

--- erts/etc/common/erlexec.c.orig
+++ erts/etc/common/erlexec.c
@@ -913,6 +913,7 @@
 	int status = fork();
 	if (status != 0)	/* Parent */
 	    return 0;
+	setsid();
 	status = fork();
 	if (status != 0)	/* Parent */
 	    return 0;

Though there could be the case when setsid() isn't available and more
sophisticated technique is needed. Russ Allbery in
http://lists.debian.org/debian-devel/2008/07/msg00623.html proposed a
function daemon() which is to be accompanied by checks for setsid()
and TIOCNOTTY ioctl.

Cheers!
-- 
Sergei Golovan



More information about the erlang-bugs mailing list