2008-10-31 Ulrich Mueller * emacs.c (pid_file): New variable. (main): Save process id of child in file pid_file. (shut_down_emacs): Unlink pid_file. (standard_args, USAGE1): Add --pidfile. --- emacs-orig/src/emacs.c 29 Oct 2008 18:03:03 -0000 1.454 +++ emacs/src/emacs.c 31 Oct 2008 17:08:19 -0000 @@ -242,6 +242,9 @@ startup. */ int daemon_pipe[2]; +/* Name of the file where the daemon's process id is saved. */ +static char *pid_file; + /* Save argv and argc. */ char **initial_argv; int initial_argc; @@ -274,6 +277,7 @@ --no-site-file do not load site-start.el\n\ --no-splash do not display a splash screen on startup\n\ --no-window-system, -nw do not communicate with X, ignoring $DISPLAY\n\ +--pidfile FILE save process id in FILE; only with --daemon\n\ --quick, -Q equivalent to -q --no-site-file --no-splash\n\ --script FILE run FILE as an Emacs Lisp script\n\ --terminal, -t DEVICE use DEVICE for terminal I/O\n\ @@ -1080,6 +1084,7 @@ { #ifndef DOS_NT pid_t f; + char *pfile_arg = NULL; /* Start as a daemon: fork a new child process which will run the rest of the initialization code, then exit. @@ -1146,6 +1151,20 @@ #ifdef HAVE_SETSID setsid(); #endif + if (argmatch (argv, argc, "-pidfile", "--pidfile", 5, + &pfile_arg, &skip_args)) + { + FILE *fp; + pid_file = xstrdup (pfile_arg); + if (!(fp = fopen (pid_file, "w"))) + { + fprintf (stderr, "Cannot create pid file %s: %s\n", + pid_file, strerror (errno)); + exit (1); + } + fprintf (fp, "%ld\n", (long) getpid ()); + fclose (fp); + } #else /* DOS_NT */ fprintf (stderr, "This platform does not support the -daemon flag.\n"); exit (1); @@ -1804,6 +1823,7 @@ { "-batch", "--batch", 100, 0 }, { "-script", "--script", 100, 1 }, { "-daemon", "--daemon", 99, 0 }, + { "-pidfile", "--pidfile", 98, 1 }, { "-help", "--help", 90, 0 }, { "-no-unibyte", "--no-unibyte", 83, 0 }, { "-multibyte", "--multibyte", 82, 0 }, @@ -2155,6 +2175,11 @@ #endif /* HAVE_X_WINDOWS */ #endif +#ifndef DOS_NT + if (pid_file) + unlink (pid_file); +#endif + #ifdef SIGIO /* There is a tendency for a SIGIO signal to arrive within exit, and cause a SIGHUP because the input descriptor is already closed. */