Subject: #1107 - 23.0.60; Emacs --daemon crashes when emacsclient tries to establish a connection on OS X - Emacs bug report logs

From: Adrian Robert <[email-masked]>
Date: Tue, 9 Dec 2008 23:18:01 -0500

I have not had the chance to refine my fix so I'm attaching my patch  
here in hopes that someone else can work on it.  It uses exec()  
instead of fork() to launch the child, using a daemon name argument to  
differentiate the child.  This prevents normal use of the name  
argument.  Moreover, the pipe connection does not work (not sure why),  
so it is disabled.

Index: src/emacs.c
===================================================================
RCS file: /sources/emacs/emacs/src/emacs.c,v
retrieving revision 1.456
diff -u -p -r1.456 emacs.c
--- src/emacs.c	8 Dec 2008 16:22:40 -0000	1.456
+++ src/emacs.c	10 Dec 2008 04:16:16 -0000
@@ -1102,21 +1102,26 @@ main (int argc, char **argv)
  	 use a pipe for synchronization.  The parent waits for the child
  	 to close its end of the pipe (using `daemon-initialized')
  	 before exiting.  */
+#ifndef HAVE_NS
        if (pipe (daemon_pipe) == -1)
  	{
  	  fprintf (stderr, "Cannot pipe!\n");
  	  exit (1);
  	}
-
        f = fork ();
+#else
+      if (!dname_arg || strcmp (dname_arg, "child"))
+	  f = fork ();
+      else
+	  f = 0;
+#endif
        if (f > 0)
  	{
  	  int retval;
  	  char buf[1];
-
+#ifndef HAVE_NS
  	  /* Close unused writing end of the pipe.  */
  	  close (daemon_pipe[1]);
-
  	  /* Just wait for the child to close its end of the pipe.  */
  	  do
  	    {
@@ -1131,6 +1136,9 @@ main (int argc, char **argv)
  	    }

  	  close (daemon_pipe[0]);
+#else
+	  sleep(5);
+#endif
  	  exit (0);
  	}
        if (f < 0)
@@ -1139,13 +1147,30 @@ main (int argc, char **argv)
  	  exit (1);
  	}

+#ifdef HAVE_NS
+      {
+        char *empty[1] = { NULL };
+        char *newargs[4] = {argv[0], "--daemon=child", "-Q", NULL};
+        if (!dname_arg || strcmp (dname_arg, "child")) {
+          int c = execve(argv[0], newargs, empty);
+          fprintf(stderr, "SHOULDN'T BE HERE: %d\t%d\n",c,errno);
+          exit(1);
+        }
+        daemon_pipe[1] = 1; // hack to get IS_DAEMON to work
+        if (dname_arg && !strcmp(dname_arg, "child"))
+          dname_arg = NULL;
+      }
+#endif
+
        if (dname_arg)
         	daemon_name = xstrdup (dname_arg);
+#ifndef HAVE_NS
        /* Close unused reading end of the pipe.  */
        close (daemon_pipe[0]);
        /* Make sure that the used end of the pipe is closed on exec, so
  	 that it is not accessible to programs started from .emacs.  */
        fcntl (daemon_pipe[1], F_SETFD, FD_CLOEXEC);
+#endif

  #ifdef HAVE_SETSID
        setsid();
@@ -2484,10 +2509,13 @@ from the parent process and its tty file
       Instead, we should probably close the pipe in start-process and
       call-process to make sure the pipe is never inherited by
       subprocesses.  */
+#ifndef HAVE_NS
    write (daemon_pipe[1], "\n", 1);
    close (daemon_pipe[1]);
+#endif
    /* Set it to an invalid value so we know we've already run this  
function.  */
    daemon_pipe[1] = -1;
+
    return Qt;
  }