unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
* emacs --daemon should provide a way to get its process id
@ 2008-10-31 19:19 Ulrich Mueller
  2008-11-01 14:05 ` bwoster
  2008-11-02  1:55 ` Stefan Monnier
  0 siblings, 2 replies; 5+ messages in thread
From: Ulrich Mueller @ 2008-10-31 19:19 UTC (permalink / raw)
  To: emacs-devel

[-- Attachment #1: message body text --]
[-- Type: text/plain, Size: 549 bytes --]

Hi,

I've played around with "emacs --daemon" and different ways of
starting/stopping it, as a user and also as a system service (managed
by OpenRC's "start-stop-daemon"). What seems to be missing is an easy
way to get the process id of the daemon.

Most other programs running as a daemon save their process id in a
file, typically located under /var/run.

While I believe that "emacs --daemon" shouldn't do this by default,
an option to enable it would be very useful.

Any opinions about attached patch which adds a "--pidfile" option?

Ulrich



[-- Attachment #2: emacs-cvs-pidfile.patch --]
[-- Type: text/plain, Size: 2418 bytes --]

2008-10-31  Ulrich Mueller  <ulm@kph.uni-mainz.de>

	* 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.  */

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2008-11-02  2:18 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2008-10-31 19:19 emacs --daemon should provide a way to get its process id Ulrich Mueller
2008-11-01 14:05 ` bwoster
2008-11-01 17:33   ` Ulrich Mueller
2008-11-02  1:55 ` Stefan Monnier
2008-11-02  2:18   ` Juanma Barranquero

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).