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

* Re: emacs --daemon should provide a way to get its process id
  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
  1 sibling, 1 reply; 5+ messages in thread
From: bwoster @ 2008-11-01 14:05 UTC (permalink / raw)
  To: Emacs-devel



Ulrich Mueller-2 wrote:
> 
> 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.
> ...
> Any opinions about attached patch which adds a "--pidfile" option?
> ...
> Ulrich
> 

I'm all for it! I've just spent an hour trying to test out an init.d script,
but it is not very robust without a pidfile, so your patch would make things
so much easier...

Emacs does have the (emacs-pid) lisp function, but that too is not easy to
try, and there may be race conditions when two --daemons are started.

Actually... with your patch, if two emacs --daemon --pidfile=same_file are
started, what will happen?

-- 
View this message in context: http://www.nabble.com/emacs---daemon-should-provide-a-way-to-get-its-process-id-tp20272581p20280352.html
Sent from the Emacs - Dev mailing list archive at Nabble.com.





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

* Re: emacs --daemon should provide a way to get its process id
  2008-11-01 14:05 ` bwoster
@ 2008-11-01 17:33   ` Ulrich Mueller
  0 siblings, 0 replies; 5+ messages in thread
From: Ulrich Mueller @ 2008-11-01 17:33 UTC (permalink / raw)
  To: bwoster; +Cc: emacs-devel

>>>>> On Sat, 1 Nov 2008, bwoster wrote:

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

> I'm all for it! I've just spent an hour trying to test out an init.d
> script, but it is not very robust without a pidfile, so your patch
> would make things so much easier...

> Emacs does have the (emacs-pid) lisp function, but that too is not
> easy to try, and there may be race conditions when two --daemons are
> started.

> Actually... with your patch, if two emacs --daemon
> --pidfile=same_file are started, what will happen?

The second one will win and overwrite the file. That's why you
normally start daemons with a tool like "start-stop-daemon" which
takes care of locking.

Ulrich




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

* Re: emacs --daemon should provide a way to get its process id
  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-02  1:55 ` Stefan Monnier
  2008-11-02  2:18   ` Juanma Barranquero
  1 sibling, 1 reply; 5+ messages in thread
From: Stefan Monnier @ 2008-11-02  1:55 UTC (permalink / raw)
  To: Ulrich Mueller; +Cc: emacs-devel

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

You can use the --eval argument to make the daemon write its (emacs-pid)
to some file.  I think that'll be enough for now.


        Stefan





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

* Re: emacs --daemon should provide a way to get its process id
  2008-11-02  1:55 ` Stefan Monnier
@ 2008-11-02  2:18   ` Juanma Barranquero
  0 siblings, 0 replies; 5+ messages in thread
From: Juanma Barranquero @ 2008-11-02  2:18 UTC (permalink / raw)
  To: Stefan Monnier; +Cc: Ulrich Mueller, emacs-devel

On Sun, Nov 2, 2008 at 02:55, Stefan Monnier <monnier@iro.umontreal.ca> wrote:

> You can use the --eval argument to make the daemon write its (emacs-pid)
> to some file.  I think that'll be enough for now.

Also, if server.el is configured to use TCP, the emacs pid is written
to the server authentication file.

  Juanma




^ 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).