*** empty log message ***
[bpt/emacs.git] / src / emacs.c
index bcc7fb0..3e94f0f 100644 (file)
@@ -1,6 +1,6 @@
 /* Fully extensible Emacs, running on Unix, intended for GNU.
    Copyright (C) 1985, 1986, 1987, 1993, 1994, 1995, 1997, 1998, 1999,
-                 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008
+                 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
                  Free Software Foundation, Inc.
 
 This file is part of GNU Emacs.
@@ -106,7 +106,7 @@ int gdb_use_union = 1;
 #endif
 EMACS_INT gdb_valbits = VALBITS;
 EMACS_INT gdb_gctypebits = GCTYPEBITS;
-#ifdef DATA_SEG_BITS
+#if defined (DATA_SEG_BITS) && ! defined (USE_LSB_TAG)
 EMACS_INT gdb_data_seg_bits = DATA_SEG_BITS;
 #else
 EMACS_INT gdb_data_seg_bits = 0;
@@ -202,6 +202,10 @@ extern int inherited_pgroup;
 int display_arg;
 #endif
 
+#ifdef HAVE_NS
+extern char ns_no_defaults;
+#endif
+
 /* An address near the bottom of the stack.
    Tells GC how to save a copy of the stack.  */
 char *stack_bottom;
@@ -235,12 +239,12 @@ int noninteractive;
 
 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;
 
 /* Pipe used to send exit notification to the daemon parent at
    startup.  */
-static int daemon_pipe[2];
+int daemon_pipe[2];
 
 /* Save argv and argc.  */
 char **initial_argv;
@@ -285,8 +289,8 @@ Initialization options:\n\
 Action options:\n\
 \n\
 FILE                    visit FILE using find-file\n\
-+LINE                  go to line LINE in next FILE\n\
-+LINE:COLUMN           go to line LINE, column COLUMN, in next FILE\n\
++LINE                   go to line LINE in next FILE\n\
++LINE:COLUMN            go to line LINE, column COLUMN, in next FILE\n\
 --directory, -L DIR     add DIR to variable load-path\n\
 --eval EXPR             evaluate Emacs Lisp expression EXPR\n\
 --execute EXPR          evaluate Emacs Lisp expression EXPR\n\
@@ -796,6 +800,10 @@ main (int argc, char **argv)
 #endif
   int no_loadup = 0;
   char *junk = 0;
+  char *dname_arg = 0;
+#ifdef NS_IMPL_COCOA
+  char dname_arg2[80];
+#endif
 
 #if GC_MARK_STACK
   extern Lisp_Object *stack_base;
@@ -1074,7 +1082,8 @@ main (int argc, char **argv)
       exit (0);
     }
 
-  if (argmatch (argv, argc, "-daemon", "--daemon", 5, NULL, &skip_args))
+  if (argmatch (argv, argc, "-daemon", "--daemon", 5, NULL, &skip_args)
+      || argmatch (argv, argc, "-daemon", "--daemon", 5, &dname_arg, &skip_args))
     {
 #ifndef DOS_NT
       pid_t f;
@@ -1082,6 +1091,20 @@ main (int argc, char **argv)
       /* 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')
@@ -1092,7 +1115,20 @@ main (int argc, char **argv)
          exit (1);
        }
 
+#ifndef NS_IMPL_COCOA
       f = fork ();
+#else /* NS_IMPL_COCOA */
+      /* Under Cocoa we must do fork+exec as CoreFoundation lib fails in
+         forked process: http://developer.apple.com/ReleaseNotes/
+                                  CoreFoundation/CoreFoundation.html)
+         We mark being in the exec'd process by a daemon name argument of
+         form "--daemon=\nFD0,FD1\nNAME" where FD are the pipe file descriptors,
+         NAME is the original daemon name, if any. */
+      if (!dname_arg || !strchr (dname_arg, '\n'))
+         f = fork ();  /* in orig */
+      else
+         f = 0;  /* in exec'd */
+#endif /* NS_IMPL_COCOA */
       if (f > 0)
        {
          int retval;
@@ -1113,6 +1149,11 @@ main (int argc, char **argv)
              fprintf (stderr, "Error reading status from child\n");
              exit (1);
            }
+         else if (retval == 0)
+           {
+             fprintf (stderr, "Error: server did not start correctly\n");
+             exit (1);
+           }
 
          close (daemon_pipe[0]);
          exit (0);
@@ -1123,9 +1164,50 @@ main (int argc, char **argv)
          exit (1);
        }
 
+#ifdef NS_IMPL_COCOA
+      {
+        /* In orig process, forked as child, OR in exec'd. */
+        if (!dname_arg || !strchr (dname_arg, '\n'))
+          {  /* In orig, child: now exec w/special daemon name. */
+            char fdStr[80];
+
+            if (dname_arg && strlen (dname_arg) > 70)
+              {
+                fprintf (stderr, "daemon: child name too long\n");
+                exit (1);
+              }
+
+            sprintf (fdStr, "--daemon=\n%d,%d\n%s", daemon_pipe[0],
+                     daemon_pipe[1], dname_arg ? dname_arg : "");
+            argv[skip_args] = fdStr;
+
+            execv (argv[0], argv);
+            fprintf (stderr, "emacs daemon: exec failed: %d\t%d\n", errno);
+            exit (1);
+          }
+
+        /* In exec'd: parse special dname into pipe and name info. */
+        if (!dname_arg || !strchr (dname_arg, '\n')
+            || strlen (dname_arg) < 1 || strlen (dname_arg) > 70)
+          {
+            fprintf (stderr, "emacs daemon: daemon name absent or too long\n");
+            exit(1);
+          }
+        dname_arg2[0] = '\0';
+        sscanf (dname_arg, "\n%d,%d\n%s", &(daemon_pipe[0]), &(daemon_pipe[1]),
+                dname_arg2);
+        dname_arg = strlen (dname_arg2) ? dname_arg2 : NULL;
+      }
+#endif /* NS_IMPL_COCOA */
+
+      if (dname_arg)
+               daemon_name = xstrdup (dname_arg);
       /* Close unused reading end of the pipe.  */
       close (daemon_pipe[0]);
-      is_daemon = 1;
+      /* 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);
+
 #ifdef HAVE_SETSID
       setsid();
 #endif
@@ -1396,6 +1478,16 @@ main (int argc, char **argv)
     {
       char *tmp;
       display_arg = 4;
+      if (argmatch (argv, argc, "-q", "--no-init-file", 6, NULL, &skip_args))
+        {
+          ns_no_defaults = 1;
+          skip_args--;
+        }
+      if (argmatch (argv, argc, "-Q", "--quick", 5, NULL, &skip_args))
+        {
+          ns_no_defaults = 1;
+          skip_args--;
+        }
 #ifdef NS_IMPL_COCOA
       if (skip_args < argc)
         {
@@ -2419,19 +2511,28 @@ decode_env_path (evarname, defalt)
 }
 
 DEFUN ("daemonp", Fdaemonp, Sdaemonp, 0, 0, 0,
-       doc: /* Return t if the current emacs process is a daemon.  */)
+       doc: /* Return non-nil if the current emacs process is a daemon.
+If the daemon was given a name argument, return that name. */)
   ()
 {
-  return is_daemon ? Qt : Qnil;
+  if (IS_DAEMON)
+    if (daemon_name)
+      return build_string (daemon_name);
+    else
+      return Qt;
+  else
+    return Qnil;
 }
 
 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)
@@ -2441,13 +2542,20 @@ DEFUN ("daemon-initialized", Fdaemon_initialized, Sdaemon_initialized, 0, 0, 0,
     error ("This function can only be called after loading the init files");
 
   /* Get rid of stdin, stdout and stderr.  */
-  open ("/dev/null", O_RDWR);
+  nfd = open ("/dev/null", O_RDWR);
   dup2 (nfd, 0);
   dup2 (nfd, 1);
   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;
@@ -2571,6 +2679,9 @@ was found.  */);
               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