From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Romain Francoise Newsgroups: gmane.emacs.bugs Subject: bug#1058: 23.0.60; emacs --daemon should not return until socket is ready Date: Thu, 02 Oct 2008 19:54:51 +0200 Organization: orebokech dot com Message-ID: <87myhmdgr8.fsf@elegiac.orebokech.com> References: <1222782234_2281@mail.internode.on.net> <200810011651.m91GpAZQ010333@mothra.ics.uci.edu> <87k5csds03.fsf@elegiac.orebokech.com> <200810012332.m91NWJ4s014658@mothra.ics.uci.edu> <874p3vediy.fsf@elegiac.orebokech.com> <200810020814.m928Ed1C016380@mothra.ics.uci.edu> Reply-To: Romain Francoise , 1058@emacsbugs.donarmstrong.com NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: ger.gmane.org 1222971162 24503 80.91.229.12 (2 Oct 2008 18:12:42 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Thu, 2 Oct 2008 18:12:42 +0000 (UTC) Cc: trentbuck@gmail.com, 1058@emacsbugs.donarmstrong.com To: Dan Nicolaescu Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Thu Oct 02 20:13:38 2008 connect(): Connection refused Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([199.232.76.165]) by lo.gmane.org with esmtp (Exim 4.50) id 1KlSg8-0004Pn-3G for geb-bug-gnu-emacs@m.gmane.org; Thu, 02 Oct 2008 20:13:34 +0200 Original-Received: from localhost ([127.0.0.1]:34576 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1KlSf4-0006qV-2F for geb-bug-gnu-emacs@m.gmane.org; Thu, 02 Oct 2008 14:12:26 -0400 Original-Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1KlSci-00067f-M8 for bug-gnu-emacs@gnu.org; Thu, 02 Oct 2008 14:10:00 -0400 Original-Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1KlSci-00067G-1J for bug-gnu-emacs@gnu.org; Thu, 02 Oct 2008 14:10:00 -0400 Original-Received: from [199.232.76.173] (port=54701 helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1KlSch-00067B-K4 for bug-gnu-emacs@gnu.org; Thu, 02 Oct 2008 14:09:59 -0400 Original-Received: from rzlab.ucr.edu ([138.23.92.77]:54374) by monty-python.gnu.org with esmtps (TLS-1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.60) (envelope-from ) id 1KlScg-0000tf-8w for bug-gnu-emacs@gnu.org; Thu, 02 Oct 2008 14:09:58 -0400 Original-Received: from rzlab.ucr.edu (rzlab.ucr.edu [127.0.0.1]) by rzlab.ucr.edu (8.13.8/8.13.8/Debian-3) with ESMTP id m92I9uct026109; Thu, 2 Oct 2008 11:09:56 -0700 Original-Received: (from debbugs@localhost) by rzlab.ucr.edu (8.13.8/8.13.8/Submit) id m92I55gS025074; Thu, 2 Oct 2008 11:05:05 -0700 X-Loop: don@donarmstrong.com Resent-From: Romain Francoise Resent-To: bug-submit-list@donarmstrong.com Resent-CC: Emacs Bugs Resent-Date: Thu, 02 Oct 2008 18:05:05 +0000 Resent-Message-ID: Resent-Sender: don@donarmstrong.com X-Emacs-PR-Message: report 1058 X-Emacs-PR-Package: emacs X-Emacs-PR-Keywords: Original-Received: via spool by 1058-submit@emacsbugs.donarmstrong.com id=B1058.122297013923039 (code B ref 1058); Thu, 02 Oct 2008 18:05:05 +0000 Original-Received: (at 1058) by emacsbugs.donarmstrong.com; 2 Oct 2008 17:55:39 +0000 Original-Received: from smtp6-g19.free.fr (smtp6-g19.free.fr [212.27.42.36]) by rzlab.ucr.edu (8.13.8/8.13.8/Debian-3) with ESMTP id m92Htaop023031 for <1058@emacsbugs.donarmstrong.com>; Thu, 2 Oct 2008 10:55:37 -0700 Original-Received: from smtp6-g19.free.fr (localhost.localdomain [127.0.0.1]) by smtp6-g19.free.fr (Postfix) with ESMTP id 7229A17299; Thu, 2 Oct 2008 19:55:35 +0200 (CEST) Original-Received: from elegiac.orebokech.com (home.orebokech.com [82.67.41.165]) by smtp6-g19.free.fr (Postfix) with ESMTP id 8BAED17280; Thu, 2 Oct 2008 19:55:11 +0200 (CEST) Original-Received: by elegiac.orebokech.com (Postfix, from userid 1000) id 8F21F3B1DF; Thu, 2 Oct 2008 19:54:51 +0200 (CEST) X-Face: }9mYu,e_@+e!`Z-P5kVXa3\_b:hdJ"B)ww[&=b<2=awG:GOIM (Dan Nicolaescu's message of "Thu, 2 Oct 2008 01:14:39 -0700 (PDT)") X-detected-operating-system: by monty-python.gnu.org: GNU/Linux 2.6 (newer, 3) Resent-Date: Thu, 02 Oct 2008 14:10:00 -0400 X-BeenThere: bug-gnu-emacs@gnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Original-Sender: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.bugs:20992 Archived-At: Here's version one of a patch for this, please let me know what you think. diff --git a/lisp/startup.el b/lisp/startup.el index 5bd73f9..22c92c5 100644 --- a/lisp/startup.el +++ b/lisp/startup.el @@ -1224,7 +1224,8 @@ opening the first frame (e.g. open a connection to an X server).") ;; processing all command line arguments to allow e.g. `server-name' ;; to be changed before the server starts. (when (daemonp) - (server-start)) + (server-start) + (daemon-initialized)) ;; Run emacs-session-restore (session management) if started by ;; the session manager and we have a session manager connection. diff --git a/src/emacs.c b/src/emacs.c index f94d2d3..d4d16b7 100644 --- a/src/emacs.c +++ b/src/emacs.c @@ -235,6 +235,13 @@ int noninteractive1; /* Nonzero means Emacs was started as a daemon. */ int is_daemon = 0; +/* Set nonzero after the daemon has finished initializing. */ +int daemon_initialized = 0; + +/* Pipe used to send exit notification to the daemon parent at + startup. */ +int daemon_pipe[2]; + /* Save argv and argc. */ char **initial_argv; int initial_argc; @@ -1074,16 +1081,56 @@ main (int argc, char **argv) if (argmatch (argv, argc, "-daemon", "--daemon", 5, NULL, &skip_args)) { #ifndef DOS_NT - pid_t f = fork (); + pid_t f; int nfd; + + /* Start as a daemon: fork a new child process which will run the + rest of the initialization code, then exit. + + We want to avoid exiting before the server socket is ready, so + use a pipe for synchronization. The parent waits for the child + to close its end of the pipe (using `daemon-initialized') + before exiting. */ + if (pipe (daemon_pipe) == -1) + { + fprintf (stderr, "Cannot pipe!\n"); + exit (1); + } + + f = fork (); if (f > 0) - exit (0); + { + int retval; + char buf[1]; + + /* Close unused writing end of the pipe. */ + close (daemon_pipe[1]); + + /* Just wait for the child to close its end of the pipe. */ + do + { + retval = read (daemon_pipe[0], &buf, 1); + } + while (retval == -1 && errno == EINTR); + + if (retval < 0) + { + fprintf (stderr, "Error reading status from child\n"); + exit (1); + } + + close (daemon_pipe[0]); + exit (0); + } if (f < 0) { fprintf (stderr, "Cannot fork!\n"); exit (1); } + /* Close unused reading end of the pipe. */ + close (daemon_pipe[0]); + nfd = open ("/dev/null", O_RDWR); dup2 (nfd, 0); dup2 (nfd, 1); @@ -2389,6 +2436,24 @@ DEFUN ("daemonp", Fdaemonp, Sdaemonp, 0, 0, 0, return is_daemon ? Qt : Qnil; } +DEFUN ("daemon-initialized", Fdaemon_initialized, Sdaemon_initialized, 0, 0, 0, + doc: /* Mark the Emacs daemon as being initialized. */) + () +{ + if (is_daemon) + { + if (! daemon_initialized) + { + /* Closing the pipe will notify the parent that it can exit. */ + close (daemon_pipe[1]); + daemon_initialized = 1; + } + } + else + error ("Emacs was not started as a daemon"); + return daemon_initialized ? Qt : Qnil; +} + void syms_of_emacs () { @@ -2408,6 +2473,7 @@ syms_of_emacs () defsubr (&Sinvocation_name); defsubr (&Sinvocation_directory); defsubr (&Sdaemonp); + defsubr (&Sdaemon_initialized); DEFVAR_LISP ("command-line-args", &Vcommand_line_args, doc: /* Args passed by shell to Emacs, as a list of strings.