unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
* bug#4944: 23.1.50; starting a server without forking a daemon
@ 2009-11-17 14:25 Kenny Stuart
  2009-11-18 18:25 ` Dan Nicolaescu
  2011-05-18 13:58 ` Kamil Cholewiński
  0 siblings, 2 replies; 5+ messages in thread
From: Kenny Stuart @ 2009-11-17 14:25 UTC (permalink / raw)
  To: emacs-pretest-bug


[-- Attachment #1.1: Type: text/plain, Size: 7274 bytes --]


Please write in English if possible, because the Emacs maintainersusually do not have translators to read other languages for them.
Your bug report will be posted to the emacs-pretest-bug@gnu.org mailing list.
Please describe exactly what actions triggered the bugand the precise symptoms of the bug.  If you can, givea recipe starting from `emacs -Q':
This is a feature request to add the facility to start an emacs serverusing a command-line option that runs as a foreground process insteadof forking a daemon and exiting.
Motivation:
Forking a daemon then exiting the main process is incompatible withsystems that manage processes as services, such as launchd on OS X,therefore it is not possible to create an effective launchd serviceusing emacs in daemon mode.
Possible Solution:
For review I have included a patch that adds a --server command lineoption that does what the --daemon option does, except it runs theserver in the foreground process rather than forking a daemon andexiting.
Please note that I am new to emacs and nix based operating systems andin the short time I have had to spend on this was unable to add a SIGINThandler to gracefully shutdown the emacs server, not that it mattersto launchd since it should send SIGTERM, but a solution would be nice.
There appear to be issues on OS X with creating a new frame using the -c optionto emacsclient as emacs crashes (so far during garbage collection) whendoing this, however I am still learning the dev tools available to me on thisplatform and it will be a while before I could effectively report such bugs, sothis is just an FYI. 
As I'm using a debug build so I can investigate further, can you confirmthat the build configure options I've used are ok and not likely to becontributing to the instability, are there any other options I should beusing?
Please also note that this is the first time I have engaged with an opensource project so if I'm not doing it right let me know.
If Emacs crashed, and you have the Emacs process in the gdb debugger,please include the output from the following gdb commands:    `bt full' and `xbacktrace'.For information about debugging Emacs, please read the file/Applications/Emacs.app/Contents/Resources/etc/DEBUG.

In GNU Emacs 23.1.50.2 (x86_64-apple-darwin10.2.0, NS apple-appkit-1038.25) of 2009-11-17 on maverick.homeWindowing system distributor `Apple', version 10.3.1038configured using `configure  '--with-ns' '--enable-asserts' 'CFLAGS=-O0 -gdwarf-2 -g3 -Wno-pointer-sign''
Important settings:  value of $LC_ALL: nil  value of $LC_COLLATE: nil  value of $LC_CTYPE: nil  value of $LC_MESSAGES: nil  value of $LC_MONETARY: nil  value of $LC_NUMERIC: nil  value of $LC_TIME: nil  value of $LANG: en_GB.UTF-8  value of $XMODIFIERS: nil  locale-coding-system: utf-8-unix  default enable-multibyte-characters: t
Major mode: Text
Minor modes in effect:  tooltip-mode: t  mouse-wheel-mode: t  tool-bar-mode: t  menu-bar-mode: t  file-name-shadow-mode: t  global-font-lock-mode: t  font-lock-mode: t  global-auto-composition-mode: t  auto-composition-mode: t  auto-encryption-mode: t  auto-compression-mode: t  line-number-mode: t  transient-mark-mode: t
Recent input:<backspace> <backspace> L i b r a r y / L a u n c h A g e n t s / <help-echo> o r g . g n u . e n <backspace> m a c s - s e r v e r . p l i s t <return> <help-echo> t e x t / x m l <return> ~ / L i b r a r y / L a u n c h A g e n t s SPC s c r i p t SPC o r <backspace> <backspace> f o r SPC e m a c s SPC s e r v e r <left> <left> <left> <left> <left> <left> <left> <left> <left> <left> <left> <left> <left> <left> <left> <left> <left> <backspace> <backspace> <backspace> <backspace> <backspace> <backspace> p l s i t <backspace> <backspace> <backspace> i s t <return> a t t a c h m e n t <return> <backspace> <help-echo> <menu-bar> <file> <write-file> <help-echo> <backspace> <backspace> <backspace> <backspace> <backspace> <backspace> <backspace> <backspace> <backspace> <backspace> <backspace> <backspace> <backspace> <backspace> <backspace> <backspace> <backspace> D o <tab> c <tab> e m a c s - m a i l . t x t <return> <down-mouse-1> <mouse-1> <wheel-up> <double-wheel-up> <triple-wheel-up> <triple-wheel-up> <triple-wheel-up> <triple-wheel-up> <wheel-down> <double-wheel-down> <wheel-down> <double-wheel-down> <wheel-down> <double-wheel-down> <triple-wheel-down> <triple-wheel-down> <triple-wheel-down> <wheel-down> <double-wheel-down> <triple-wheel-down> <triple-wheel-down> <wheel-up> <double-wheel-up> <triple-wheel-up> <triple-wheel-up> <triple-wheel-up> <wheel-up> <double-wheel-up> <triple-wheel-up> <triple-wheel-up> <triple-wheel-up> <triple-wheel-up> <wheel-up> <double-wheel-up> <wheel-down> <double-wheel-down> <triple-wheel-down> <triple-wheel-down> <wheel-up> <double-wheel-up> <triple-wheel-up> <triple-wheel-up> <triple-wheel-up> <wheel-up> <wheel-up> <wheel-up> <down-mouse-1> <mouse-movement> <mouse-movement> <help-echo> <mouse-movement> <help-echo> <mouse-movement> <mouse-movement> <mouse-movement> <help-echo> <mouse-movement> <mouse-movement> <drag-mouse-1> <menu-bar> <edit> <copy> <down-mouse-1> <down-mouse-1> <down-mouse-1> <down-mouse-1> <down-mouse-1> <down-mouse-1> <down-mouse-1> <down-mouse-1> <down-mouse-1> <down-mouse-1> <down-mouse-1> <down-mouse-1> <down-mouse-1> <down-mouse-1> <down-mouse-1> <down-mouse-1> <down-mouse-1> <down-mouse-1> M-x r e p o r t - e m a c s - b u g <return>
Recent messages:Auto-saving...doneUndo!Auto-saving...doneMaking completion list...Saving file /Users/kstuart/Documents/emacs-mail.txt...Wrote /Users/kstuart/Documents/emacs-mail.txtbyte-code: Beginning of buffer [2 times]byte-code: End of buffer [2 times]byte-code: Beginning of buffer [8 times]Saved text from "Please write in English if possible, bec"
Load-path shadows:/Applications/Emacs.app/Contents/Resources/lisp/emacs-lisp/lucid hides /Applications/Emacs.app/Contents/Resources/lisp/obsolete/lucid/Applications/Emacs.app/Contents/Resources/lisp/emacs-lisp/levents hides /Applications/Emacs.app/Contents/Resources/lisp/obsolete/levents
Features:(ispell newcomment help-mode view shadow mail-extr message ecompleterfc822 mml mml-sec password-cache mm-decode mm-bodies mm-encode mailcapmail-parse rfc2231 rfc2047 rfc2045 qp ietf-drums mailabbrev nnheadergnus-util netrc time-date mm-util mail-prsvr gmm-utils wid-editmailheader canlock sha1 hex-util hashcash mail-utils emacsbug sendmailregexp-opt server tooltip ediff-hook vc-hooks lisp-float-type mwheelns-win easymenu tool-bar dnd fontset image fringe lisp-mode registerpage menu-bar rfn-eshadow timer select scroll-bar mldrag mouse jit-lockfont-lock syntax facemenu font-core frame cham georgian utf-8-langmisc-lang vietnamese tibetan thai tai-viet lao korean japanese hebrewgreek romanian slovak czech european ethiopic indian cyrillic chinesecase-table epa-hook jka-cmpr-hook help simple abbrev loaddefs buttonminibuffer faces cus-face text-properties overlay md5 base64 format envcode-pages mule custom widget hashtable-print-readable backquotemake-network-process ns multi-tty emacs) 		 	   		  
_________________________________________________________________
Use Hotmail to send and receive mail from your different email accounts
http://clk.atdmt.com/UKM/go/186394592/direct/01/

[-- Attachment #1.2: Type: text/html, Size: 10614 bytes --]

[-- Attachment #2: emacs-foreground-server.patch --]
[-- Type: application/octet-stream, Size: 2771 bytes --]

diff --git a/src/emacs.c b/src/emacs.c
index d96b3a1..956cbee 100644
--- a/src/emacs.c
+++ b/src/emacs.c
@@ -279,6 +279,7 @@ Initialization options:\n\
 --no-window-system, -nw     do not communicate with X, ignoring $DISPLAY\n\
 --quick, -Q                 equivalent to -q --no-site-file --no-splash\n\
 --script FILE               run FILE as an Emacs Lisp script\n\
+--server                    start a server in the foreground\n\
 --terminal, -t DEVICE       use DEVICE for terminal I/O\n\
 --user, -u USER             load ~USER/.emacs instead of your own\n\
 \n%s"
@@ -1081,7 +1082,20 @@ main (int argc, char **argv)
       exit (0);
     }
 
-  if (argmatch (argv, argc, "-daemon", "--daemon", 5, NULL, &skip_args)
+  /* server mode and daemon mode are mutualy exclusive, if both are
+   * specified on the command line, server mode will take precedence */
+  if (argmatch (argv, argc, "-server", "--server", 5, NULL, &skip_args)
+      || argmatch (argv, argc, "server", "--server", 5, &dname_arg, &skip_args))
+    {
+      if (dname_arg)
+        daemon_name = xstrdup(dname_arg);
+
+      /* request to run as foreground server */
+      daemon_pipe[1] = -1;
+
+      /* FIXME: need to shut the foreground server down gracefully on SIGINT */
+    }
+  else if (argmatch (argv, argc, "-daemon", "--daemon", 5, NULL, &skip_args)
       || argmatch (argv, argc, "-daemon", "--daemon", 5, &dname_arg, &skip_args))
     {
 #ifndef DOS_NT
@@ -1855,6 +1869,7 @@ const struct standard_args standard_args[] =
   { "-batch", "--batch", 100, 0 },
   { "-script", "--script", 100, 1 },
   { "-daemon", "--daemon", 99, 0 },
+  { "-server", "--server", 99, 0 },
   { "-help", "--help", 90, 0 },
   { "-no-unibyte", "--no-unibyte", 83, 0 },
   { "-multibyte", "--multibyte", 82, 0 },
@@ -2512,7 +2527,7 @@ from the parent process and its tty file descriptors.  */)
   if (!IS_DAEMON)
     error ("This function can only be called if emacs is run as a daemon");
 
-  if (daemon_pipe[1] < 0)
+  if (daemon_pipe[1] < -1)
     error ("The daemon has already been initialized");
 
   if (NILP (Vafter_init_time))
@@ -2532,10 +2547,16 @@ from the parent process and its tty file descriptors.  */)
      Instead, we should probably close the pipe in start-process and
      call-process to make sure the pipe is never inherited by
      subprocesses.  */
-  write (daemon_pipe[1], "\n", 1);
-  close (daemon_pipe[1]);
-  /* Set it to an invalid value so we know we've already run this function.  */
-  daemon_pipe[1] = -1;
+  if (daemon_pipe[1] > 0)
+    {
+      write (daemon_pipe[1], "\n", 1);
+      close (daemon_pipe[1]);
+    }
+
+  /* indicate that this function has completed, calling it again will
+   * result in an error */
+  daemon_pipe[1] = -2;
+
   return Qt;
 }
 

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

* bug#4944: 23.1.50; starting a server without forking a daemon
  2009-11-17 14:25 bug#4944: 23.1.50; starting a server without forking a daemon Kenny Stuart
@ 2009-11-18 18:25 ` Dan Nicolaescu
       [not found]   ` <BLU0-SMTP851950D69B194ABCA7830FECA20@phx.gbl>
  2011-05-18 13:58 ` Kamil Cholewiński
  1 sibling, 1 reply; 5+ messages in thread
From: Dan Nicolaescu @ 2009-11-18 18:25 UTC (permalink / raw)
  To: Kenny Stuart; +Cc: 4944

Kenny Stuart <kstuart@hotmail.co.uk> writes:

  > Please write in English if possible, because the Emacs maintainers
  > usually do not have translators to read other languages for them.
  > 
  > Your bug report will be posted to the emacs-pretest-bug@gnu.org mailing list.
  > 
  > Please describe exactly what actions triggered the bug
  > and the precise symptoms of the bug.  If you can, give
  > a recipe starting from `emacs -Q':
  > 
  > This is a feature request to add the facility to start an emacs server
  > using a command-line option that runs as a foreground process instead
  > of forking a daemon and exiting.
  > 
  > Motivation:
  > 
  > Forking a daemon then exiting the main process is incompatible with
  > systems that manage processes as services, such as launchd on OS X,
  > therefore it is not possible to create an effective launchd service
  > using emacs in daemon mode.
  > 
  > Possible Solution:
  > 
  > For review I have included a patch that adds a --server command line
  > option that does what the --daemon option does, except it runs the
  > server in the foreground process rather than forking a daemon and
  > exiting.

How is this different from:

emacs -f server-start 

?

Also note that you do not have to start the daemon explicitly, if you use

emacsclient -a "" 

it will start an emacs daemon if one is not already running.






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

* bug#4944: 23.1.50; starting a server without forking a daemon
       [not found]   ` <BLU0-SMTP851950D69B194ABCA7830FECA20@phx.gbl>
@ 2009-11-19 16:22     ` Dan Nicolaescu
  2009-11-19 21:39       ` Kenny Stuart
  0 siblings, 1 reply; 5+ messages in thread
From: Dan Nicolaescu @ 2009-11-19 16:22 UTC (permalink / raw)
  To: Kenny Stuart; +Cc: 4944

Kenny Stuart <kstuart@hotmail.co.uk> writes:

  > On 18 Nov 2009, at 18:25, Dan Nicolaescu wrote:
  > 
  > 
  >     Kenny Stuart <kstuart@hotmail.co.uk> writes:
  > 
  > 
  >         Please write in English if possible, because the Emacs maintainers
  > 
  >         usually do not have translators to read other languages for them.
  > 
  > 
  > 
  >         Your bug report will be posted to the emacs-pretest-bug@gnu.org mailing
  >         list.
  > 
  > 
  > 
  >         Please describe exactly what actions triggered the bug
  > 
  >         and the precise symptoms of the bug.  If you can, give
  > 
  >         a recipe starting from `emacs -Q':
  > 
  > 
  > 
  >         This is a feature request to add the facility to start an emacs server
  > 
  >         using a command-line option that runs as a foreground process instead
  > 
  >         of forking a daemon and exiting.
  > 
  > 
  > 
  >         Motivation:
  > 
  > 
  > 
  >         Forking a daemon then exiting the main process is incompatible with
  > 
  >         systems that manage processes as services, such as launchd on OS X,
  > 
  >         therefore it is not possible to create an effective launchd service
  > 
  >         using emacs in daemon mode.
  > 
  > 
  > 
  >         Possible Solution:
  > 
  > 
  > 
  >         For review I have included a patch that adds a --server command line
  > 
  >         option that does what the --daemon option does, except it runs the
  > 
  >         server in the foreground process rather than forking a daemon and
  > 
  >         exiting.
  > 
  > 
  >     How is this different from:
  > 
  >     emacs -f server-start
  > 
  >     ?
  > 
  >     Also note that you do not have to start the daemon explicitly, if you use
  > 
  >     emacsclient -a ""
  > 
  >     it will start an emacs daemon if one is not already running.
  > 
  > 

Please keep 4944@emacsbugs.donarmstrong.com in Cc: so that these
messages can be archived and seen by more people.

  > Neither of these appear compatible with launchd (and probably smf under
  > Solaris).

Can you please explain what do you mean by that?

It would also help if you would explain what are you trying to do, how
are you trying to do it, and what exactly are the problems that you
faced when trying to do that.


  > emacs -f server-start will initialize the display system, which is undesirable
  > when
  > starting emacs as a service, equally closing the last emacs window will
  > terminate the
  > server.
  > 
  > emacsclient -ca "" produces the following output on my mac running OS X 10.6.2
  > 
  > emacsclient: can't find socket; have you started the server?
  > To start the server in Emacs, type "M-x server-start".
  > emacsclient: connect: Connection refused
  > emacs daemon: exec failed: 2
  > Error: server did not start correctly
  > Error: Could not start the Emacs daemon
  > 
  > So there may be an issue with the trunk builds in this regard, however even if
  > it did
  > work surely it is simply starting an instance of emacs in daemon mode, which
  > will
  > fork and exit and so would be no different than having manually started the
  > daemon
  > beforehand.






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

* bug#4944: 23.1.50; starting a server without forking a daemon
  2009-11-19 16:22     ` Dan Nicolaescu
@ 2009-11-19 21:39       ` Kenny Stuart
  0 siblings, 0 replies; 5+ messages in thread
From: Kenny Stuart @ 2009-11-19 21:39 UTC (permalink / raw)
  To: Dan Nicolaescu; +Cc: 4944


On 19 Nov 2009, at 16:22, Dan Nicolaescu wrote:

> Kenny Stuart <kstuart@hotmail.co.uk> writes:
> 
>> On 18 Nov 2009, at 18:25, Dan Nicolaescu wrote:
>> 
>> 
>>    Kenny Stuart <kstuart@hotmail.co.uk> writes:
>> 
>> 
>>        Please write in English if possible, because the Emacs maintainers
>> 
>>        usually do not have translators to read other languages for them.
>> 
>> 
>> 
>>        Your bug report will be posted to the emacs-pretest-bug@gnu.org mailing
>>        list.
>> 
>> 
>> 
>>        Please describe exactly what actions triggered the bug
>> 
>>        and the precise symptoms of the bug.  If you can, give
>> 
>>        a recipe starting from `emacs -Q':
>> 
>> 
>> 
>>        This is a feature request to add the facility to start an emacs server
>> 
>>        using a command-line option that runs as a foreground process instead
>> 
>>        of forking a daemon and exiting.
>> 
>> 
>> 
>>        Motivation:
>> 
>> 
>> 
>>        Forking a daemon then exiting the main process is incompatible with
>> 
>>        systems that manage processes as services, such as launchd on OS X,
>> 
>>        therefore it is not possible to create an effective launchd service
>> 
>>        using emacs in daemon mode.
>> 
>> 
>> 
>>        Possible Solution:
>> 
>> 
>> 
>>        For review I have included a patch that adds a --server command line
>> 
>>        option that does what the --daemon option does, except it runs the
>> 
>>        server in the foreground process rather than forking a daemon and
>> 
>>        exiting.
>> 
>> 
>>    How is this different from:
>> 
>>    emacs -f server-start
>> 
>>    ?
>> 
>>    Also note that you do not have to start the daemon explicitly, if you use
>> 
>>    emacsclient -a ""
>> 
>>    it will start an emacs daemon if one is not already running.
>> 
>> 
> 
> Please keep 4944@emacsbugs.donarmstrong.com in Cc: so that these
> messages can be archived and seen by more people.
> 
>> Neither of these appear compatible with launchd (and probably smf under
>> Solaris).
> 
> Can you please explain what do you mean by that?
> 
> It would also help if you would explain what are you trying to do, how
> are you trying to do it, and what exactly are the problems that you
> faced when trying to do that.
> 
> 
>> emacs -f server-start will initialize the display system, which is undesirable
>> when
>> starting emacs as a service, equally closing the last emacs window will
>> terminate the
>> server.
>> 
>> emacsclient -ca "" produces the following output on my mac running OS X 10.6.2
>> 
>> emacsclient: can't find socket; have you started the server?
>> To start the server in Emacs, type "M-x server-start".
>> emacsclient: connect: Connection refused
>> emacs daemon: exec failed: 2
>> Error: server did not start correctly
>> Error: Could not start the Emacs daemon
>> 
>> So there may be an issue with the trunk builds in this regard, however even if
>> it did
>> work surely it is simply starting an instance of emacs in daemon mode, which
>> will
>> fork and exit and so would be no different than having manually started the
>> daemon
>> beforehand.

I'm trying to do the equivalent of using init or Upstart on Linux to
have the system start an emacs server, on OS X launchd provides that
facility, however emacs --daemon is not compatible with launchd.

** From the launchd man page:

In Darwin, the canonical way to launch a daemon is through launchd
as opposed to more traditional mechanisms or mechanisms provided
in earlier versions of Mac OS X. These alternate methods should be
considered deprecated and not suitable for new projects.

In the launchd lexicon, a "daemon" is, by definition, a system-wide
service of which there is one instance for all clients. An "agent"
is a service that runs on a per-user basis. Daemons should not attempt
to display UI or interact directly with a user's login session. Any
and all work that involves interacting with a user should be done
through agents.

** From the launchd.plist man page:

Daemons or agents managed by launchd are expected to behave certain
ways.

A daemon or agent launched by launchd MUST NOT do the following in the
process directly launched by launchd:

  •   Call daemon(3).
  •   Do the moral equivalent of daemon(3) by calling fork(2) and have
      the parent process exit(3) or _exit(2).

A daemon or agent launched by launchd SHOULD NOT do the following as a
part of their startup initialization:

  •   Setup the user ID or group ID.
  •   Setup the working directory.
  •   chroot(2)
  •   setsid(2)
  •   Close "stray" file descriptors.
  •   Change stdio(3) to /dev/null.
  •   Setup resource limits with setrusage(2).
  •   Setup priority with setpriority(2).
  •   Ignore the SIGTERM signal.

**

I'm using a LaunchAgent to start an emacs server by placing a launchd
plist file in ~/Library/LaunchAgents, the agent is configured to start
an emacs server and respawn it should it fail, since emacs --daemon is
not launchd compatible a respawn loop starts until launchd gives up.






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

* bug#4944: 23.1.50; starting a server without forking a daemon
  2009-11-17 14:25 bug#4944: 23.1.50; starting a server without forking a daemon Kenny Stuart
  2009-11-18 18:25 ` Dan Nicolaescu
@ 2011-05-18 13:58 ` Kamil Cholewiński
  1 sibling, 0 replies; 5+ messages in thread
From: Kamil Cholewiński @ 2011-05-18 13:58 UTC (permalink / raw)
  To: 4944

Hi all,

This bug is very relevant to my interests. I'm trying to run Emacs in
daemon mode under supervision of daemontools[1], and the current
behaviour (forking into the background) makes this a PITA. daemontools
includes a small utility called "fghack" which tries to keep a forked
process in the foreground, however terminating the service still
doesn't work properly. The proper solution recommended[2] by the
designers of daemontools in the FAQ is to "fix" the misbehaving
program.

By the way, this bug seems to be a duplicate of #2677 [3].

[1] http://cr.yp.to/daemontools.html
[2] http://cr.yp.to/daemontools/faq/create.html#fghack
[3] http://debbugs.gnu.org/cgi/bugreport.cgi?bug=2677

Thanks,
Kamil





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

end of thread, other threads:[~2011-05-18 13:58 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2009-11-17 14:25 bug#4944: 23.1.50; starting a server without forking a daemon Kenny Stuart
2009-11-18 18:25 ` Dan Nicolaescu
     [not found]   ` <BLU0-SMTP851950D69B194ABCA7830FECA20@phx.gbl>
2009-11-19 16:22     ` Dan Nicolaescu
2009-11-19 21:39       ` Kenny Stuart
2011-05-18 13:58 ` Kamil Cholewiński

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