unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
From: Ulrich Mueller <ulm@gentoo.org>
To: emacs-devel@gnu.org
Subject: emacs --daemon should provide a way to get its process id
Date: Fri, 31 Oct 2008 20:19:37 +0100	[thread overview]
Message-ID: <18699.23113.985700.25955@a1ihome1.kph.uni-mainz.de> (raw)

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

             reply	other threads:[~2008-10-31 19:19 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-10-31 19:19 Ulrich Mueller [this message]
2008-11-01 14:05 ` emacs --daemon should provide a way to get its process id bwoster
2008-11-01 17:33   ` Ulrich Mueller
2008-11-02  1:55 ` Stefan Monnier
2008-11-02  2:18   ` Juanma Barranquero

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=18699.23113.985700.25955@a1ihome1.kph.uni-mainz.de \
    --to=ulm@gentoo.org \
    --cc=emacs-devel@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).