changeset 99178:8af0a96bf7da

* xdisp.c (pos_visible_p, redisplay_internal, message3_nolog) (message2_nolog): Check FRAME_INITIAL_P instead of noninteractively. * emacs.c (is_daemon): Remove. (main): Don't set is_daemon. (IS_DAEMON): New macro. (Fdaemonp, Fdaemon_initialized): Use it. (Fdaemon_initialized): Wrtie a char into the pipe to make sure the parent exits. (syms_of_emacs): Explicitly initialize daemon_pipe[1].
author Stefan Monnier <monnier@iro.umontreal.ca>
date Tue, 28 Oct 2008 01:02:44 +0000
parents a70ba38c4731
children ff3f463a949c
files src/ChangeLog src/emacs.c src/xdisp.c
diffstat 3 files changed, 50 insertions(+), 12 deletions(-) [+]
line wrap: on
line diff
--- a/src/ChangeLog	Tue Oct 28 00:42:34 2008 +0000
+++ b/src/ChangeLog	Tue Oct 28 01:02:44 2008 +0000
@@ -1,3 +1,16 @@
+2008-10-28  Stefan Monnier  <monnier@iro.umontreal.ca>
+
+	* xdisp.c (pos_visible_p, redisplay_internal, message3_nolog)
+	(message2_nolog): Check FRAME_INITIAL_P instead of noninteractively.
+
+	* emacs.c (is_daemon): Remove.
+	(main): Don't set is_daemon.
+	(IS_DAEMON): New macro.
+	(Fdaemonp, Fdaemon_initialized): Use it.
+	(Fdaemon_initialized): Wrtie a char into the pipe to make sure the
+	parent exits.
+	(syms_of_emacs): Explicitly initialize daemon_pipe[1].
+
 2008-10-27  Chong Yidong  <cyd@stupidchicken.com>
 
 	* nsterm.m (ns_draw_window_cursor): When hbar cursor is on
--- a/src/emacs.c	Tue Oct 28 00:42:34 2008 +0000
+++ b/src/emacs.c	Tue Oct 28 01:02:44 2008 +0000
@@ -235,8 +235,6 @@
 
 int noninteractive1;
 
-/* Nonzero means Emacs was started as a daemon.  */
-int is_daemon = 0;
 /* Name for the server started by the daemon.*/
 static char *daemon_name;
 
@@ -244,6 +242,8 @@
    startup.  */
 static int daemon_pipe[2];
 
+#define IS_DAEMON (daemon_pipe[1] != 0)
+
 /* Save argv and argc.  */
 char **initial_argv;
 int initial_argc;
@@ -1086,6 +1086,20 @@
       /* Start as a daemon: fork a new child process which will run the
 	 rest of the initialization code, then exit.
 
+	 Detaching a daemon requires the following steps:
+	 - fork
+	 - setsid
+	 - exit the parent
+	 - close the tty file-descriptors
+
+	 We only want to do the last 2 steps once the daemon is ready to
+	 serve requests, i.e. after loading .emacs (initialization).
+	 OTOH initialization may start subprocesses (e.g. ispell) and these
+	 should be run from the proper process (the one that will end up
+	 running as daemon) and with the proper "session id" in order for
+	 them to keep working after detaching, so fork and setsid need to be
+	 performed before initialization.
+
 	 We want to avoid exiting before the server socket is ready, so
 	 use a pipe for synchronization.  The parent waits for the child
 	 to close its end of the pipe (using `daemon-initialized')
@@ -1131,7 +1145,6 @@
        	daemon_name = xstrdup (dname_arg);
       /* Close unused reading end of the pipe.  */
       close (daemon_pipe[0]);
-      is_daemon = 1;
 #ifdef HAVE_SETSID
       setsid();
 #endif
@@ -2429,7 +2442,7 @@
 If the daemon was given a name argument, return that name. */)
   ()
 {
-  if (is_daemon)
+  if (IS_DAEMON)
     if (daemon_name)
       return build_string (daemon_name);
     else
@@ -2439,12 +2452,14 @@
 }
 
 DEFUN ("daemon-initialized", Fdaemon_initialized, Sdaemon_initialized, 0, 0, 0,
-       doc: /* Mark the Emacs daemon as being initialized.  */)
+       doc: /* Mark the Emacs daemon as being initialized.
+This finishes the daemonization process by doing the other half of detaching
+from the parent process and its tty file descriptors.  */)
   ()
 {
   int nfd;
 
-  if (!is_daemon)
+  if (!IS_DAEMON)
     error ("This function can only be called if emacs is run as a daemon");
 
   if (daemon_pipe[1] < 0)
@@ -2460,7 +2475,14 @@
   dup2 (nfd, 2);
   close (nfd);
 
-  /* Closing the pipe will notify the parent that it can exit.  */
+  /* Closing the pipe will notify the parent that it can exit.
+     FIXME: In case some other process inherited the pipe, closing it here
+     won't notify the parent because it's still open elsewhere, so we
+     additionally send a byte, just to make sure the parent really exits.
+     Instead, we should probably close the pipe in start-process and
+     call-process to make sure the pipe is never inherited by
+     subprocesses.  */
+  write (daemon_pipe[1], "\n", 1);
   close (daemon_pipe[1]);
   /* Set it to an invalid value so we know we've already run this function.  */
   daemon_pipe[1] = -1;
@@ -2584,6 +2606,9 @@
 	       doc: /* Value of `current-time' after loading the init files.
 This is nil during initialization.  */);
   Vafter_init_time = Qnil;
+
+  /* Make sure IS_DAEMON starts up as false.  */
+  daemon_pipe[1] = 0;
 }
 
 /* arch-tag: 7bfd356a-c720-4612-8ab6-aa4222931c2e
--- a/src/xdisp.c	Tue Oct 28 00:42:34 2008 +0000
+++ b/src/xdisp.c	Tue Oct 28 01:02:44 2008 +0000
@@ -1323,7 +1323,7 @@
   int visible_p = 0;
   struct buffer *old_buffer = NULL;
 
-  if (noninteractive)
+  if (FRAME_INITIAL_P (XFRAME (WINDOW_FRAME (w))))
     return visible_p;
 
   if (XBUFFER (w->buffer) != current_buffer)
@@ -7891,7 +7891,7 @@
   struct frame *sf = SELECTED_FRAME ();
   message_enable_multibyte = multibyte;
 
-  if (noninteractive)
+  if (FRAME_INITIAL_P (sf))
     {
       if (noninteractive_need_newline)
 	putc ('\n', stderr);
@@ -7990,7 +7990,7 @@
   struct frame *sf = SELECTED_FRAME ();
   message_enable_multibyte = multibyte;
 
-  if (noninteractive)
+  if (FRAME_INITIAL_P (sf))
     {
       if (noninteractive_need_newline)
 	putc ('\n', stderr);
@@ -8088,7 +8088,7 @@
 	    putc ('\n', stderr);
 	  noninteractive_need_newline = 0;
 	  fprintf (stderr, m, SDATA (string));
-	  if (cursor_in_echo_area == 0)
+	  if (!cursor_in_echo_area)
 	    fprintf (stderr, "\n");
 	  fflush (stderr);
 	}
@@ -11300,7 +11300,7 @@
   /* No redisplay if running in batch mode or frame is not yet fully
      initialized, or redisplay is explicitly turned off by setting
      Vinhibit_redisplay.  */
-  if (noninteractive
+  if (FRAME_INITIAL_P (SELECTED_FRAME ())
       || !NILP (Vinhibit_redisplay))
     return;