From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Ulrich Mueller Newsgroups: gmane.emacs.devel Subject: emacs --daemon should provide a way to get its process id Date: Fri, 31 Oct 2008 20:19:37 +0100 Message-ID: <18699.23113.985700.25955@a1ihome1.kph.uni-mainz.de> NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="----cutting-here-may-damage-your-screen-surface----14411401----" Content-Transfer-Encoding: 7bit X-Trace: ger.gmane.org 1225480819 4818 80.91.229.12 (31 Oct 2008 19:20:19 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Fri, 31 Oct 2008 19:20:19 +0000 (UTC) To: emacs-devel@gnu.org Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Fri Oct 31 20:21:21 2008 connect(): Connection refused Return-path: Envelope-to: ged-emacs-devel@m.gmane.org Original-Received: from lists.gnu.org ([199.232.76.165]) by lo.gmane.org with esmtp (Exim 4.50) id 1KvzYa-0001lV-0h for ged-emacs-devel@m.gmane.org; Fri, 31 Oct 2008 20:21:17 +0100 Original-Received: from localhost ([127.0.0.1]:42133 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1KvzXK-0002Jc-RS for ged-emacs-devel@m.gmane.org; Fri, 31 Oct 2008 15:19:58 -0400 Original-Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1KvzXF-0002Iu-U0 for emacs-devel@gnu.org; Fri, 31 Oct 2008 15:19:53 -0400 Original-Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1KvzXB-0002Hw-D4 for emacs-devel@gnu.org; Fri, 31 Oct 2008 15:19:52 -0400 Original-Received: from [199.232.76.173] (port=43506 helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1KvzXB-0002Ht-Am for emacs-devel@gnu.org; Fri, 31 Oct 2008 15:19:49 -0400 Original-Received: from a1iwww1.kph.uni-mainz.de ([134.93.134.1]:43290) by monty-python.gnu.org with esmtps (TLS-1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.60) (envelope-from ) id 1KvzXA-0004Zn-7f for emacs-devel@gnu.org; Fri, 31 Oct 2008 15:19:49 -0400 Original-Received: from a1ihome1.kph.uni-mainz.de (a1ihome1.kph.uni-mainz.de [134.93.134.75]) by a1iwww1.kph.uni-mainz.de (8.14.0/8.13.4) with ESMTP id m9VJJcKr024632 for ; Fri, 31 Oct 2008 20:19:38 +0100 Original-Received: from a1ihome1.kph.uni-mainz.de (localhost [127.0.0.1]) by a1ihome1.kph.uni-mainz.de (8.14.0/8.14.1) with ESMTP id m9VJJc6A020424; Fri, 31 Oct 2008 20:19:38 +0100 Original-Received: (from ulm@localhost) by a1ihome1.kph.uni-mainz.de (8.14.0/8.14.0/Submit) id m9VJJccm020419; Fri, 31 Oct 2008 20:19:38 +0100 X-Mailer: VM 8.0.2-487 under Emacs 22.2.1 (i686-pc-linux-gnu) X-detected-operating-system: by monty-python.gnu.org: GNU/Linux 2.6 (newer, 1) X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Original-Sender: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.devel:105223 Archived-At: ------cutting-here-may-damage-your-screen-surface----14411401---- Content-Type: text/plain; charset=us-ascii Content-Description: message body text Content-Transfer-Encoding: 7bit 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 ------cutting-here-may-damage-your-screen-surface----14411401---- Content-Type: text/plain; name="emacs-cvs-pidfile.patch" Content-Disposition: inline; filename="emacs-cvs-pidfile.patch" Content-Transfer-Encoding: 7bit 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. */ ------cutting-here-may-damage-your-screen-surface----14411401------