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.
6
6
This file is part of GNU Emacs.
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. */
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"))
871
if (! getenv ("EMACS_HEAP_EXEC"))
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");
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
882
execvp (argv[0], argv);
890
execvp (argv[0], argv);
884
/* If the exec fails, try to dump anyway. */
892
/* If the exec fails, try to dump anyway. */
888
895
#endif /* HAVE_PERSONALITY_LINUX32 */
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))
1091
/* Start as a daemon: fork a new child process which will run the
1092
rest of the initialization code, then exit.
1094
Detaching a daemon requires the following steps:
1098
- close the tty file-descriptors
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.
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')
1112
if (pipe (daemon_pipe) == -1)
1114
fprintf (stderr, "Cannot pipe!\n");
1118
#ifndef NS_IMPL_COCOA
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 */
1130
f = 0; /* in exec'd */
1131
#endif /* NS_IMPL_COCOA */
1137
/* Close unused writing end of the pipe. */
1138
close (daemon_pipe[1]);
1140
/* Just wait for the child to close its end of the pipe. */
1143
retval = read (daemon_pipe[0], &buf, 1);
1145
while (retval == -1 && errno == EINTR);
1149
fprintf (stderr, "Error reading status from child\n");
1152
else if (retval == 0)
1154
fprintf (stderr, "Error: server did not start correctly\n");
1158
close (daemon_pipe[0]);
1087
1163
fprintf (stderr, "Cannot fork!\n");
1091
nfd = open ("/dev/null", O_RDWR);
1167
#ifdef NS_IMPL_COCOA
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. */
1174
if (dname_arg && strlen (dname_arg) > 70)
1176
fprintf (stderr, "daemon: child name too long\n");
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;
1184
execv (argv[0], argv);
1185
fprintf (stderr, "emacs daemon: exec failed: %d\t%d\n", errno);
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)
1193
fprintf (stderr, "emacs daemon: daemon name absent or too long\n");
1196
dname_arg2[0] = '\0';
1197
sscanf (dname_arg, "\n%d,%d\n%s", &(daemon_pipe[0]), &(daemon_pipe[1]),
1199
dname_arg = strlen (dname_arg2) ? dname_arg2 : NULL;
1201
#endif /* NS_IMPL_COCOA */
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);
1097
1211
#ifdef HAVE_SETSID
2389
2513
DEFUN ("daemonp", Fdaemonp, Sdaemonp, 0, 0, 0,
2390
doc: /* Return t if the current emacs process is a daemon. */)
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. */)
2520
return build_string (daemon_name);
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. */)
2536
error ("This function can only be called if emacs is run as a daemon");
2538
if (daemon_pipe[1] < 0)
2539
error ("The daemon has already been initialized");
2541
if (NILP (Vafter_init_time))
2542
error ("This function can only be called after loading the init files");
2544
/* Get rid of stdin, stdout and stderr. */
2545
nfd = open ("/dev/null", O_RDWR);
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
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;