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: Wed, 01 Oct 2008 21:39:40 +0200 Organization: orebokech dot com Message-ID: <87k5csds03.fsf@elegiac.orebokech.com> References: <1222782234_2281@mail.internode.on.net> <200810011651.m91GpAZQ010333@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 1222890620 30663 80.91.229.12 (1 Oct 2008 19:50:20 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Wed, 1 Oct 2008 19:50:20 +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 Wed Oct 01 21:51:18 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 1Kl7j2-0005G8-PP for geb-bug-gnu-emacs@m.gmane.org; Wed, 01 Oct 2008 21:51:09 +0200 Original-Received: from localhost ([127.0.0.1]:45432 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1Kl7hz-0000Dp-Of for geb-bug-gnu-emacs@m.gmane.org; Wed, 01 Oct 2008 15:50:03 -0400 Original-Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1Kl7hv-0000CI-U0 for bug-gnu-emacs@gnu.org; Wed, 01 Oct 2008 15:49:59 -0400 Original-Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1Kl7hv-0000BJ-2n for bug-gnu-emacs@gnu.org; Wed, 01 Oct 2008 15:49:59 -0400 Original-Received: from [199.232.76.173] (port=42736 helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1Kl7hu-0000B4-Ne for bug-gnu-emacs@gnu.org; Wed, 01 Oct 2008 15:49:58 -0400 Original-Received: from rzlab.ucr.edu ([138.23.92.77]:55345) by monty-python.gnu.org with esmtps (TLS-1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.60) (envelope-from ) id 1Kl7hu-0001wX-66 for bug-gnu-emacs@gnu.org; Wed, 01 Oct 2008 15:49: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 m91JntcV014724; Wed, 1 Oct 2008 12:49:56 -0700 Original-Received: (from debbugs@localhost) by rzlab.ucr.edu (8.13.8/8.13.8/Submit) id m91Jj2WP013578; Wed, 1 Oct 2008 12:45:02 -0700 X-Loop: don@donarmstrong.com Resent-From: Romain Francoise Resent-To: bug-submit-list@donarmstrong.com Resent-CC: Emacs Bugs Resent-Date: Wed, 01 Oct 2008 19:45:02 +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.122288998612358 (code B ref 1058); Wed, 01 Oct 2008 19:45:02 +0000 Original-Received: (at 1058) by emacsbugs.donarmstrong.com; 1 Oct 2008 19:39:46 +0000 Original-Received: from smtp1-g19.free.fr (smtp1-g19.free.fr [212.27.42.27]) by rzlab.ucr.edu (8.13.8/8.13.8/Debian-3) with ESMTP id m91JdggD012350 for <1058@emacsbugs.donarmstrong.com>; Wed, 1 Oct 2008 12:39:43 -0700 Original-Received: from smtp1-g19.free.fr (localhost.localdomain [127.0.0.1]) by smtp1-g19.free.fr (Postfix) with ESMTP id 77ACC1AB31F; Wed, 1 Oct 2008 21:39:41 +0200 (CEST) Original-Received: from elegiac.orebokech.com (home.orebokech.com [82.67.41.165]) by smtp1-g19.free.fr (Postfix) with ESMTP id 1EDD01AB39E; Wed, 1 Oct 2008 21:39:40 +0200 (CEST) Original-Received: by elegiac.orebokech.com (Postfix, from userid 1000) id 2F8CB3B1DF; Wed, 1 Oct 2008 21:39:40 +0200 (CEST) X-Face: }9mYu,e_@+e!`Z-P5kVXa3\_b:hdJ"B)ww[&=b<2=awG:GOIM (Dan Nicolaescu's message of "Wed, 1 Oct 2008 09:51:10 -0700 (PDT)") X-detected-operating-system: by monty-python.gnu.org: GNU/Linux 2.6 (newer, 3) Resent-Date: Wed, 01 Oct 2008 15:49:59 -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:20969 Archived-At: Hi Dan, Trent, Dan Nicolaescu writes: > Just curious, what do you get if you do: > emacs -Q --daemon; while ! ls /tmp/emacs1187/; do sleep 1; done Yes, the delay is caused by my change to load the init file before starting the server. > Unfortunately ATM I don't see an easy way to do that. > Ideas/patches are certainly welcome. It's easy to do, but not very clean. WDYT? diff --git a/lisp/startup.el b/lisp/startup.el index 5bd73f9..d9e37be 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-detach-parent)) ;; 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..a2842d4 100644 --- a/src/emacs.c +++ b/src/emacs.c @@ -235,6 +235,10 @@ int noninteractive1; /* Nonzero means Emacs was started as a daemon. */ int is_daemon = 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 +1078,31 @@ main (int argc, char **argv) if (argmatch (argv, argc, "-daemon", "--daemon", 5, NULL, &skip_args)) { #ifndef DOS_NT + if (pipe (daemon_pipe) == -1) + { + fprintf (stderr, "Cannot pipe!\n"); + exit (1); + } pid_t f = fork (); int nfd; if (f > 0) - exit (0); + { + char buf[2]; + close (daemon_pipe[1]); + /* Wait for the child to close its end of the pipe before + exiting. */ + while (read (daemon_pipe[0], &buf, 1) < 0) + sleep (0.1); + close (daemon_pipe[0]); + exit (0); + } if (f < 0) { fprintf (stderr, "Cannot fork!\n"); exit (1); } + close (daemon_pipe[0]); nfd = open ("/dev/null", O_RDWR); dup2 (nfd, 0); dup2 (nfd, 1); @@ -2389,6 +2408,16 @@ DEFUN ("daemonp", Fdaemonp, Sdaemonp, 0, 0, 0, return is_daemon ? Qt : Qnil; } +DEFUN ("daemon-detach-parent", Fdaemon_detach_parent, Sdaemon_detach_parent, + 0, 0, 0, + doc: /* Detach Emacs from its invocation parent if it was started as a daemon. */) + () +{ + if (is_daemon) + close (daemon_pipe[1]); + return Qt; +} + void syms_of_emacs () { @@ -2408,6 +2437,7 @@ syms_of_emacs () defsubr (&Sinvocation_name); defsubr (&Sinvocation_directory); defsubr (&Sdaemonp); + defsubr (&Sdaemon_detach_parent); DEFVAR_LISP ("command-line-args", &Vcommand_line_args, doc: /* Args passed by shell to Emacs, as a list of strings.