diff options
| author | Dan Nicolaescu <dann@ics.uci.edu> | 2008-09-21 23:31:40 +0000 |
|---|---|---|
| committer | Dan Nicolaescu <dann@ics.uci.edu> | 2008-09-21 23:31:40 +0000 |
| commit | eab2ee89441d51e885e3df6b3fbcb3c12a177232 (patch) | |
| tree | 16fff121c73fb31938ace464c17dd6650c2bcf5d /src/emacs.c | |
| parent | d06f8010663c3eacd0fd873f816803a6ffceb692 (diff) | |
| download | emacs-eab2ee89441d51e885e3df6b3fbcb3c12a177232.tar.gz | |
* emacs.c (standard_args): Add --daemon.
(main): Disconnect from the terminal when --daemon is passed.
(is_daemon): New variable.
(Fdaemonp): New function.
(syms_of_emacs): Defsubr it.
* startup.el (command-line): Start the server when in daemon mode.
* cmdargs.texi (Initial Options): Document --daemon.
Diffstat (limited to 'src/emacs.c')
| -rw-r--r-- | src/emacs.c | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/src/emacs.c b/src/emacs.c index 68127df0c06..131662c27a1 100644 --- a/src/emacs.c +++ b/src/emacs.c @@ -232,6 +232,9 @@ int noninteractive; int noninteractive1; +/* Nonzero means Emacs was started as a daemon. */ +int is_daemon = 0; + /* Save argv and argc. */ char **initial_argv; int initial_argc; @@ -1068,6 +1071,34 @@ main (int argc, char **argv) exit (0); } +#ifndef DOS_NT + if (argmatch (argv, argc, "-daemon", "--daemon", 5, NULL, &skip_args)) + { + pid_t f = fork(); + int nfd; + if (f > 0) + exit(0); + if (f < 0) + { + fprintf (stderr, "Cannot fork!\n"); + exit(1); + } + + nfd = open("/dev/null", O_RDWR); + dup2(nfd, 0); + dup2(nfd, 1); + dup2(nfd, 2); + close (nfd); + is_daemon = 1; +#ifdef HAVE_SETSID + setsid(); +#endif + } +#else /* DOS_NT */ + fprintf (stderr, "This platform does not support the -daemon flag.\n"); + exit (1); +#endif /* DOS_NT */ + if (! noninteractive) { #ifdef BSD_PGRPS @@ -1719,6 +1750,7 @@ struct standard_args standard_args[] = { "-nw", "--no-windows", 110, 0 }, { "-batch", "--batch", 100, 0 }, { "-script", "--script", 100, 1 }, + { "-daemon", "--daemon", 99, 0 }, { "-help", "--help", 90, 0 }, { "-no-unibyte", "--no-unibyte", 83, 0 }, { "-multibyte", "--multibyte", 82, 0 }, @@ -2350,6 +2382,13 @@ decode_env_path (evarname, defalt) return Fnreverse (lpath); } +DEFUN ("daemonp", Fdaemonp, Sdaemonp, 0, 0, 0, + doc: /* Make the current emacs process a daemon.*/) + (void) +{ + return is_daemon ? Qt : Qnil; +} + void syms_of_emacs () { @@ -2368,6 +2407,7 @@ syms_of_emacs () defsubr (&Sinvocation_name); defsubr (&Sinvocation_directory); + defsubr (&Sdaemonp); DEFVAR_LISP ("command-line-args", &Vcommand_line_args, doc: /* Args passed by shell to Emacs, as a list of strings. |
