~ubuntu-branches/ubuntu/karmic/emacs-snapshot/karmic

« back to all changes in this revision

Viewing changes to src/emacs.c

  • Committer: Bazaar Package Importer
  • Author(s): Reinhard Tartler
  • Date: 2009-04-05 09:14:30 UTC
  • mto: This revision was merged to the branch mainline in revision 34.
  • Revision ID: james.westby@ubuntu.com-20090405091430-nw07lynn2arotjbe
Tags: upstream-20090320
ImportĀ upstreamĀ versionĀ 20090320

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/* Fully extensible Emacs, running on Unix, intended for GNU.
2
2
   Copyright (C) 1985, 1986, 1987, 1993, 1994, 1995, 1997, 1998, 1999,
3
 
                 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008
 
3
                 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
4
4
                 Free Software Foundation, Inc.
5
5
 
6
6
This file is part of GNU Emacs.
106
106
#endif
107
107
EMACS_INT gdb_valbits = VALBITS;
108
108
EMACS_INT gdb_gctypebits = GCTYPEBITS;
109
 
#ifdef DATA_SEG_BITS
 
109
#if defined (DATA_SEG_BITS) && ! defined (USE_LSB_TAG)
110
110
EMACS_INT gdb_data_seg_bits = DATA_SEG_BITS;
111
111
#else
112
112
EMACS_INT gdb_data_seg_bits = 0;
202
202
int display_arg;
203
203
#endif
204
204
 
 
205
#ifdef HAVE_NS
 
206
extern char ns_no_defaults;
 
207
#endif
 
208
 
205
209
/* An address near the bottom of the stack.
206
210
   Tells GC how to save a copy of the stack.  */
207
211
char *stack_bottom;
212
216
/* The gap between BSS end and heap start as far as we can tell.  */
213
217
static unsigned long heap_bss_diff;
214
218
 
215
 
/* If the gap between BSS end and heap start is larger than this we try to
216
 
   work around it, and if that fails, output a warning in dump-emacs.  */
 
219
/* If the gap between BSS end and heap start is larger than this
 
220
   output a warning in dump-emacs.  */
217
221
#define MAX_HEAP_BSS_DIFF (1024*1024)
218
222
 
219
223
 
235
239
 
236
240
int noninteractive1;
237
241
 
238
 
/* Nonzero means Emacs was started as a daemon.  */
239
 
int is_daemon = 0;
 
242
/* Name for the server started by the daemon.*/
 
243
static char *daemon_name;
 
244
 
 
245
/* Pipe used to send exit notification to the daemon parent at
 
246
   startup.  */
 
247
int daemon_pipe[2];
240
248
 
241
249
/* Save argv and argc.  */
242
250
char **initial_argv;
281
289
Action options:\n\
282
290
\n\
283
291
FILE                    visit FILE using find-file\n\
284
 
+LINE                   go to line LINE in next FILE\n\
285
 
+LINE:COLUMN            go to line LINE, column COLUMN, in next FILE\n\
 
292
+LINE                   go to line LINE in next FILE\n\
 
293
+LINE:COLUMN            go to line LINE, column COLUMN, in next FILE\n\
286
294
--directory, -L DIR     add DIR to variable load-path\n\
287
295
--eval EXPR             evaluate Emacs Lisp expression EXPR\n\
288
296
--execute EXPR          evaluate Emacs Lisp expression EXPR\n\
792
800
#endif
793
801
  int no_loadup = 0;
794
802
  char *junk = 0;
 
803
  char *dname_arg = 0;
 
804
#ifdef NS_IMPL_COCOA
 
805
  char dname_arg2[80];
 
806
#endif
795
807
 
796
808
#if GC_MARK_STACK
797
809
  extern Lisp_Object *stack_base;
861
873
    }
862
874
 
863
875
#ifdef HAVE_PERSONALITY_LINUX32
864
 
  /* See if there is a gap between the end of BSS and the heap.
865
 
     In that case, set personality and exec ourself again.  */
866
876
  if (!initialized
867
877
      && (strcmp (argv[argc-1], "dump") == 0
868
878
          || strcmp (argv[argc-1], "bootstrap") == 0)
869
 
      && heap_bss_diff > MAX_HEAP_BSS_DIFF)
 
879
      && ! getenv ("EMACS_HEAP_EXEC"))
870
880
    {
871
 
      if (! getenv ("EMACS_HEAP_EXEC"))
872
 
        {
873
 
          /* Set this so we only do this once.  */
874
 
          putenv("EMACS_HEAP_EXEC=true");
 
881
      /* Set this so we only do this once.  */
 
882
      putenv("EMACS_HEAP_EXEC=true");
875
883
 
876
 
          /* A flag to turn off address randomization which is introduced
877
 
           in linux kernel shipped with fedora core 4 */
 
884
      /* A flag to turn off address randomization which is introduced
 
885
         in linux kernel shipped with fedora core 4 */
878
886
#define ADD_NO_RANDOMIZE 0x0040000
879
 
          personality (PER_LINUX32 | ADD_NO_RANDOMIZE);
 
887
      personality (PER_LINUX32 | ADD_NO_RANDOMIZE);
880
888
#undef  ADD_NO_RANDOMIZE
881
889
 
882
 
          execvp (argv[0], argv);
 
890
      execvp (argv[0], argv);
883
891
 
884
 
          /* If the exec fails, try to dump anyway.  */
885
 
          perror ("execvp");
886
 
        }
 
892
      /* If the exec fails, try to dump anyway.  */
 
893
      perror ("execvp");
887
894
    }
888
895
#endif /* HAVE_PERSONALITY_LINUX32 */
889
896
 
1075
1082
      exit (0);
1076
1083
    }
1077
1084
 
1078
 
  if (argmatch (argv, argc, "-daemon", "--daemon", 5, NULL, &skip_args))
 
1085
  if (argmatch (argv, argc, "-daemon", "--daemon", 5, NULL, &skip_args)
 
1086
      || argmatch (argv, argc, "-daemon", "--daemon", 5, &dname_arg, &skip_args))
1079
1087
    {
1080
1088
#ifndef DOS_NT
1081
 
      pid_t f = fork ();
1082
 
      int nfd;
 
1089
      pid_t f;
 
1090
 
 
1091
      /* Start as a daemon: fork a new child process which will run the
 
1092
         rest of the initialization code, then exit.
 
1093
 
 
1094
         Detaching a daemon requires the following steps:
 
1095
         - fork
 
1096
         - setsid
 
1097
         - exit the parent
 
1098
         - close the tty file-descriptors
 
1099
 
 
1100
         We only want to do the last 2 steps once the daemon is ready to
 
1101
         serve requests, i.e. after loading .emacs (initialization).
 
1102
         OTOH initialization may start subprocesses (e.g. ispell) and these
 
1103
         should be run from the proper process (the one that will end up
 
1104
         running as daemon) and with the proper "session id" in order for
 
1105
         them to keep working after detaching, so fork and setsid need to be
 
1106
         performed before initialization.
 
1107
 
 
1108
         We want to avoid exiting before the server socket is ready, so
 
1109
         use a pipe for synchronization.  The parent waits for the child
 
1110
         to close its end of the pipe (using `daemon-initialized')
 
1111
         before exiting.  */
 
1112
      if (pipe (daemon_pipe) == -1)
 
1113
        {
 
1114
          fprintf (stderr, "Cannot pipe!\n");
 
1115
          exit (1);
 
1116
        }
 
1117
 
 
1118
#ifndef NS_IMPL_COCOA
 
1119
      f = fork ();
 
1120
#else /* NS_IMPL_COCOA */
 
1121
      /* Under Cocoa we must do fork+exec as CoreFoundation lib fails in
 
1122
         forked process: http://developer.apple.com/ReleaseNotes/
 
1123
                                  CoreFoundation/CoreFoundation.html)
 
1124
         We mark being in the exec'd process by a daemon name argument of
 
1125
         form "--daemon=\nFD0,FD1\nNAME" where FD are the pipe file descriptors,
 
1126
         NAME is the original daemon name, if any. */
 
1127
      if (!dname_arg || !strchr (dname_arg, '\n'))
 
1128
          f = fork ();  /* in orig */
 
1129
      else
 
1130
          f = 0;  /* in exec'd */
 
1131
#endif /* NS_IMPL_COCOA */
1083
1132
      if (f > 0)
1084
 
        exit (0);
 
1133
        {
 
1134
          int retval;
 
1135
          char buf[1];
 
1136
 
 
1137
          /* Close unused writing end of the pipe.  */
 
1138
          close (daemon_pipe[1]);
 
1139
 
 
1140
          /* Just wait for the child to close its end of the pipe.  */
 
1141
          do
 
1142
            {
 
1143
              retval = read (daemon_pipe[0], &buf, 1);
 
1144
            }
 
1145
          while (retval == -1 && errno == EINTR);
 
1146
 
 
1147
          if (retval < 0)
 
1148
            {
 
1149
              fprintf (stderr, "Error reading status from child\n");
 
1150
              exit (1);
 
1151
            }
 
1152
          else if (retval == 0)
 
1153
            {
 
1154
              fprintf (stderr, "Error: server did not start correctly\n");
 
1155
              exit (1);
 
1156
            }
 
1157
 
 
1158
          close (daemon_pipe[0]);
 
1159
          exit (0);
 
1160
        }
1085
1161
      if (f < 0)
1086
1162
        {
1087
1163
          fprintf (stderr, "Cannot fork!\n");
1088
1164
          exit (1);
1089
1165
        }
1090
1166
 
1091
 
      nfd = open ("/dev/null", O_RDWR);
1092
 
      dup2 (nfd, 0);
1093
 
      dup2 (nfd, 1);
1094
 
      dup2 (nfd, 2);
1095
 
      close (nfd);
1096
 
      is_daemon = 1;
 
1167
#ifdef NS_IMPL_COCOA
 
1168
      {
 
1169
        /* In orig process, forked as child, OR in exec'd. */
 
1170
        if (!dname_arg || !strchr (dname_arg, '\n'))
 
1171
          {  /* In orig, child: now exec w/special daemon name. */
 
1172
            char fdStr[80];
 
1173
 
 
1174
            if (dname_arg && strlen (dname_arg) > 70)
 
1175
              {
 
1176
                fprintf (stderr, "daemon: child name too long\n");
 
1177
                exit (1);
 
1178
              }
 
1179
 
 
1180
            sprintf (fdStr, "--daemon=\n%d,%d\n%s", daemon_pipe[0],
 
1181
                     daemon_pipe[1], dname_arg ? dname_arg : "");
 
1182
            argv[skip_args] = fdStr;
 
1183
 
 
1184
            execv (argv[0], argv);
 
1185
            fprintf (stderr, "emacs daemon: exec failed: %d\t%d\n", errno);
 
1186
            exit (1);
 
1187
          }
 
1188
 
 
1189
        /* In exec'd: parse special dname into pipe and name info. */
 
1190
        if (!dname_arg || !strchr (dname_arg, '\n')
 
1191
            || strlen (dname_arg) < 1 || strlen (dname_arg) > 70)
 
1192
          {
 
1193
            fprintf (stderr, "emacs daemon: daemon name absent or too long\n");
 
1194
            exit(1);
 
1195
          }
 
1196
        dname_arg2[0] = '\0';
 
1197
        sscanf (dname_arg, "\n%d,%d\n%s", &(daemon_pipe[0]), &(daemon_pipe[1]),
 
1198
                dname_arg2);
 
1199
        dname_arg = strlen (dname_arg2) ? dname_arg2 : NULL;
 
1200
      }
 
1201
#endif /* NS_IMPL_COCOA */
 
1202
 
 
1203
      if (dname_arg)
 
1204
        daemon_name = xstrdup (dname_arg);
 
1205
      /* Close unused reading end of the pipe.  */
 
1206
      close (daemon_pipe[0]);
 
1207
      /* Make sure that the used end of the pipe is closed on exec, so
 
1208
         that it is not accessible to programs started from .emacs.  */
 
1209
      fcntl (daemon_pipe[1], F_SETFD, FD_CLOEXEC);
 
1210
 
1097
1211
#ifdef HAVE_SETSID
1098
1212
      setsid();
1099
1213
#endif
1364
1478
    {
1365
1479
      char *tmp;
1366
1480
      display_arg = 4;
 
1481
      if (argmatch (argv, argc, "-q", "--no-init-file", 6, NULL, &skip_args))
 
1482
        {
 
1483
          ns_no_defaults = 1;
 
1484
          skip_args--;
 
1485
        }
 
1486
      if (argmatch (argv, argc, "-Q", "--quick", 5, NULL, &skip_args))
 
1487
        {
 
1488
          ns_no_defaults = 1;
 
1489
          skip_args--;
 
1490
        }
1367
1491
#ifdef NS_IMPL_COCOA
1368
1492
      if (skip_args < argc)
1369
1493
        {
2387
2511
}
2388
2512
 
2389
2513
DEFUN ("daemonp", Fdaemonp, Sdaemonp, 0, 0, 0,
2390
 
       doc: /* Return t if the current emacs process is a daemon.  */)
2391
 
  ()
2392
 
{
2393
 
  return is_daemon ? Qt : Qnil;
 
2514
       doc: /* Return non-nil if the current emacs process is a daemon.
 
2515
If the daemon was given a name argument, return that name. */)
 
2516
  ()
 
2517
{
 
2518
  if (IS_DAEMON)
 
2519
    if (daemon_name)
 
2520
      return build_string (daemon_name);
 
2521
    else
 
2522
      return Qt;
 
2523
  else
 
2524
    return Qnil;
 
2525
}
 
2526
 
 
2527
DEFUN ("daemon-initialized", Fdaemon_initialized, Sdaemon_initialized, 0, 0, 0,
 
2528
       doc: /* Mark the Emacs daemon as being initialized.
 
2529
This finishes the daemonization process by doing the other half of detaching
 
2530
from the parent process and its tty file descriptors.  */)
 
2531
  ()
 
2532
{
 
2533
  int nfd;
 
2534
 
 
2535
  if (!IS_DAEMON)
 
2536
    error ("This function can only be called if emacs is run as a daemon");
 
2537
 
 
2538
  if (daemon_pipe[1] < 0)
 
2539
    error ("The daemon has already been initialized");
 
2540
 
 
2541
  if (NILP (Vafter_init_time))
 
2542
    error ("This function can only be called after loading the init files");
 
2543
 
 
2544
  /* Get rid of stdin, stdout and stderr.  */
 
2545
  nfd = open ("/dev/null", O_RDWR);
 
2546
  dup2 (nfd, 0);
 
2547
  dup2 (nfd, 1);
 
2548
  dup2 (nfd, 2);
 
2549
  close (nfd);
 
2550
 
 
2551
  /* Closing the pipe will notify the parent that it can exit.
 
2552
     FIXME: In case some other process inherited the pipe, closing it here
 
2553
     won't notify the parent because it's still open elsewhere, so we
 
2554
     additionally send a byte, just to make sure the parent really exits.
 
2555
     Instead, we should probably close the pipe in start-process and
 
2556
     call-process to make sure the pipe is never inherited by
 
2557
     subprocesses.  */
 
2558
  write (daemon_pipe[1], "\n", 1);
 
2559
  close (daemon_pipe[1]);
 
2560
  /* Set it to an invalid value so we know we've already run this function.  */
 
2561
  daemon_pipe[1] = -1;
 
2562
  return Qt;
2394
2563
}
2395
2564
 
2396
2565
void
2412
2581
  defsubr (&Sinvocation_name);
2413
2582
  defsubr (&Sinvocation_directory);
2414
2583
  defsubr (&Sdaemonp);
 
2584
  defsubr (&Sdaemon_initialized);
2415
2585
 
2416
2586
  DEFVAR_LISP ("command-line-args", &Vcommand_line_args,
2417
2587
               doc: /* Args passed by shell to Emacs, as a list of strings.
2509
2679
               doc: /* Value of `current-time' after loading the init files.
2510
2680
This is nil during initialization.  */);
2511
2681
  Vafter_init_time = Qnil;
 
2682
 
 
2683
  /* Make sure IS_DAEMON starts up as false.  */
 
2684
  daemon_pipe[1] = 0;
2512
2685
}
2513
2686
 
2514
2687
/* arch-tag: 7bfd356a-c720-4612-8ab6-aa4222931c2e