unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
* start emacs without creating frames and connect with emacsclient later
@ 2008-08-13 16:10 Dan Nicolaescu
  2008-08-13 16:40 ` Johannes Weiner
                   ` (3 more replies)
  0 siblings, 4 replies; 25+ messages in thread
From: Dan Nicolaescu @ 2008-08-13 16:10 UTC (permalink / raw)
  To: emacs-devel


It seemed that this TODO item shouldn't be that hard to do given that we
have all the infrastructure:

** Make "emacs --daemon" start emacs without showing any frame. 
Use emacsclient later to open frames.


10 minutes later it turned out to be true.

The patch below implements it.

emacs -daemon &

will start emacs without creating any frames, and it starts the server.

Later you can do:

emacsclient -t FILENAME

will create a tty frame.

and
emacsclient -c FILENAME

will create an X11 frame.

Not sure what to do about:
emacsclient FILENAME

it won't do anything visibe if no other frame is available.  Should it
create one?

Should something like this go in?  We do have a feature freeze, but this
is quite simple, and it completes the multi-tty functionality, which is
one of the major features of 23.1.

--- startup.el.~1.502.~	2008-08-06 21:53:07.000000000 -0700
+++ startup.el	2008-08-13 08:54:06.000000000 -0700
@@ -698,6 +698,8 @@ opening the first frame (e.g. open a con
 		  (attribute class &optional component subclass))
 (declare-function tool-bar-mode "tool-bar" (&optional arg))
 
+(defvar startup-unmapped nil)
+
 (defun command-line ()
   (setq before-init-time (current-time)
         command-line-default-directory default-directory)
@@ -863,6 +865,8 @@ opening the first frame (e.g. open a con
 	  (push '(icon-type . t) default-frame-alist))
 	 ((member argi '("-nbc" "-no-blinking-cursor"))
 	  (setq no-blinking-cursor t))
+	 ((equal argi "-unmapped")
+	  (setq startup-unmapped t))
 	 ;; Push the popped arg back on the list of arguments.
 	 (t
           (push argi args)
@@ -877,9 +881,11 @@ opening the first frame (e.g. open a con
 
   (run-hooks 'before-init-hook)
 
-  ;; Under X Window, this creates the X frame and deletes the terminal frame.
-  (when (fboundp 'frame-initialize)
-    (frame-initialize))
+  (if startup-unmapped
+      (server-start)
+    ;; Under X Window, this creates the X frame and deletes the terminal frame.
+    (when (and (fboundp 'frame-initialize))
+      (frame-initialize)))
 
   ;; Turn off blinking cursor if so specified in X resources.  This is here
   ;; only because all other settings of no-blinking-cursor are here.




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

* Re: start emacs without creating frames and connect with emacsclient later
  2008-08-13 16:10 start emacs without creating frames and connect with emacsclient later Dan Nicolaescu
@ 2008-08-13 16:40 ` Johannes Weiner
  2008-08-13 20:49 ` Stefan Monnier
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 25+ messages in thread
From: Johannes Weiner @ 2008-08-13 16:40 UTC (permalink / raw)
  To: Dan Nicolaescu; +Cc: emacs-devel

Hi,

Dan Nicolaescu <dann@ics.uci.edu> writes:

> It seemed that this TODO item shouldn't be that hard to do given that we
> have all the infrastructure:
>
> ** Make "emacs --daemon" start emacs without showing any frame. 
> Use emacsclient later to open frames.
>
>
> 10 minutes later it turned out to be true.
>
> The patch below implements it.
>
> emacs -daemon &

Uhm, hardly a daemon if it quits when I quit the shell.

> will start emacs without creating any frames, and it starts the server.
>
> Later you can do:
>
> emacsclient -t FILENAME
>
> will create a tty frame.
>
> and
> emacsclient -c FILENAME
>
> will create an X11 frame.
>
> Not sure what to do about:
> emacsclient FILENAME
>
> it won't do anything visibe if no other frame is available.  Should it
> create one?

I think a good solution would be to do it like emacs itself does it:
Start a graphical frame (unless -nw) when that is possible, otherwise a
terminal frame.

Right now I use the below program as a workaround, perhaps someone might
find it interesting.

	Hannes

#define _XOPEN_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <fcntl.h>
#include <unistd.h>

static const char emacs[]	= "emacs";
static char *args[]		= { "emacs", "-f", "server-start", NULL };

/* Run the emacs server */
void child_emacs(char *slavename)
{
	int slave;

	setsid();
	slave = open(slavename, O_RDWR);
	dup2(slave, 0);
	dup2(slave, 1);
	dup2(slave, 2);
	execvp(emacs, args);
	abort();
}

/* Run the reader that unblocks emacs' terminal */
void child_reader(int master)
{
	char buf[512];

	setsid();

	/* could be used for debugging in the future */
	while (read(master, buf, sizeof(buf)))
		/* do nothing */;
}

int main(void)
{
	int master;
	char *slavename;

	/* Open master terminal */
	master = open("/dev/ptmx", O_RDWR);
	grantpt(master);
	unlockpt(master);
	slavename = ptsname(master);

	/* Fork the emacs process */
	switch (fork()) {
	case -1:
		abort();
		break;
	case 0:
		child_emacs(slavename);
		break;
	default:
		/* Fork the reader process */
		switch (fork()) {
		case -1:
			abort();
			break;
		case 0:
			child_reader(master);
			break;
		}
	}

	return 0;
}




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

* Re: start emacs without creating frames and connect with emacsclient later
  2008-08-13 16:10 start emacs without creating frames and connect with emacsclient later Dan Nicolaescu
  2008-08-13 16:40 ` Johannes Weiner
@ 2008-08-13 20:49 ` Stefan Monnier
  2008-08-14  5:19 ` Richard M. Stallman
  2008-08-15 15:36 ` Phil Jackson
  3 siblings, 0 replies; 25+ messages in thread
From: Stefan Monnier @ 2008-08-13 20:49 UTC (permalink / raw)
  To: Dan Nicolaescu; +Cc: emacs-devel

> It seemed that this TODO item shouldn't be that hard to do given that we
> have all the infrastructure:

> ** Make "emacs --daemon" start emacs without showing any frame. 
> Use emacsclient later to open frames.

> 10 minutes later it turned out to be true.

> The patch below implements it.

Thanks.  The patch looks OK, pretty much along the lines of what
I'd expected.  [ Modulo some confusion between "-unmapped" and
"-daemon" and I'm not sure what it means to completely eliminate the
call to frame-initialize.  ]

> Should something like this go in?  We do have a feature freeze, but this
> is quite simple, and it completes the multi-tty functionality, which is
> one of the major features of 23.1.

I'd rather leave it for 23.2.


        Stefan




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

* Re: start emacs without creating frames and connect with emacsclient later
  2008-08-13 16:10 start emacs without creating frames and connect with emacsclient later Dan Nicolaescu
  2008-08-13 16:40 ` Johannes Weiner
  2008-08-13 20:49 ` Stefan Monnier
@ 2008-08-14  5:19 ` Richard M. Stallman
  2008-08-14 17:21   ` Stefan Monnier
                     ` (2 more replies)
  2008-08-15 15:36 ` Phil Jackson
  3 siblings, 3 replies; 25+ messages in thread
From: Richard M. Stallman @ 2008-08-14  5:19 UTC (permalink / raw)
  To: Dan Nicolaescu; +Cc: emacs-devel

    Should something like this go in?  We do have a feature freeze, but this
    is quite simple, and it completes the multi-tty functionality, which is
    one of the major features of 23.1.

I think it should go in
provided you write the text for etc/NEWS AND all the relevant manuals.




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

* Re: start emacs without creating frames and connect with emacsclient later
  2008-08-14  5:19 ` Richard M. Stallman
@ 2008-08-14 17:21   ` Stefan Monnier
  2008-08-15 12:44     ` Richard M. Stallman
  2008-08-21 17:20   ` Dan Nicolaescu
  2008-09-01 14:02   ` Dan Nicolaescu
  2 siblings, 1 reply; 25+ messages in thread
From: Stefan Monnier @ 2008-08-14 17:21 UTC (permalink / raw)
  To: rms; +Cc: Dan Nicolaescu, emacs-devel

>     Should something like this go in?  We do have a feature freeze, but this
>     is quite simple, and it completes the multi-tty functionality, which is
>     one of the major features of 23.1.

> I think it should go in
> provided you write the text for etc/NEWS AND all the relevant manuals.

I strongly suspect that the proposed patch is a bit naive and will need
more work to make it work right (the fact that it ends up not calling
frame-initialize at all is very suspect).  I.e. it will lead to several
new bug reports.

So I'd rather leave it for 23.2.  Seeing how long it took for someone to
even try to write the patch, it's not very high priority,


        Stefan




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

* Re: start emacs without creating frames and connect with emacsclient later
  2008-08-14 17:21   ` Stefan Monnier
@ 2008-08-15 12:44     ` Richard M. Stallman
  2008-08-15 15:25       ` Dan Nicolaescu
  0 siblings, 1 reply; 25+ messages in thread
From: Richard M. Stallman @ 2008-08-15 12:44 UTC (permalink / raw)
  To: Stefan Monnier; +Cc: dann, emacs-devel

    I strongly suspect that the proposed patch is a bit naive and will need
    more work to make it work right (the fact that it ends up not calling
    frame-initialize at all is very suspect).  I.e. it will lead to several
    new bug reports.

I won't argue for installing code if it isn't correct.

My point is simply that this is more in the nature of
completing a recently added feature
than adding a new feature.  I don't see that a feature
freeze should be so rigid as to exclude this sort of thing.




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

* Re: start emacs without creating frames and connect with emacsclient later
  2008-08-15 12:44     ` Richard M. Stallman
@ 2008-08-15 15:25       ` Dan Nicolaescu
  0 siblings, 0 replies; 25+ messages in thread
From: Dan Nicolaescu @ 2008-08-15 15:25 UTC (permalink / raw)
  To: rms; +Cc: Stefan Monnier, emacs-devel

"Richard M. Stallman" <rms@gnu.org> writes:

  >     I strongly suspect that the proposed patch is a bit naive and will need
  >     more work to make it work right (the fact that it ends up not calling
  >     frame-initialize at all is very suspect).  I.e. it will lead to several
  >     new bug reports.
  > 
  > I won't argue for installing code if it isn't correct.

It it probably incomplete.  Given that it's less than 5 lines, I'd guess
that making it right won't increase the size too much.

Help with getting it completely right would be greatly appreciated.




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

* Re: start emacs without creating frames and connect with emacsclient later
  2008-08-13 16:10 start emacs without creating frames and connect with emacsclient later Dan Nicolaescu
                   ` (2 preceding siblings ...)
  2008-08-14  5:19 ` Richard M. Stallman
@ 2008-08-15 15:36 ` Phil Jackson
  3 siblings, 0 replies; 25+ messages in thread
From: Phil Jackson @ 2008-08-15 15:36 UTC (permalink / raw)
  To: Dan Nicolaescu; +Cc: emacs-devel

Dan Nicolaescu <dann@ics.uci.edu> writes:

> It seemed that this TODO item shouldn't be that hard to do given that we
> have all the infrastructure:
>
> ** Make "emacs --daemon" start emacs without showing any frame. 
> Use emacsclient later to open frames.

[...]

Very cool. I'd really like to see this. At the moment I use this script:

http://www.shellarchive.co.uk/content/shell/emacs-server

Cheers,
Phil
-- 
 Philip Jackson
 http://www.shellarchive.co.uk




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

* Re: start emacs without creating frames and connect with emacsclient later
  2008-08-14  5:19 ` Richard M. Stallman
  2008-08-14 17:21   ` Stefan Monnier
@ 2008-08-21 17:20   ` Dan Nicolaescu
  2008-08-21 22:55     ` David De La Harpe Golden
  2008-08-22 11:58     ` David Hansen
  2008-09-01 14:02   ` Dan Nicolaescu
  2 siblings, 2 replies; 25+ messages in thread
From: Dan Nicolaescu @ 2008-08-21 17:20 UTC (permalink / raw)
  To: rms; +Cc: emacs-devel

"Richard M. Stallman" <rms@gnu.org> writes:

  >     Should something like this go in?  We do have a feature freeze, but this
  >     is quite simple, and it completes the multi-tty functionality, which is
  >     one of the major features of 23.1.
  > 
  > I think it should go in
  > provided you write the text for etc/NEWS AND all the relevant manuals.

I'll update the all the docs whenever it gets checked in. 

Here's a new version of the patch, now it has code to detach from the
terminal, so it is a real daemon (how scary, emacs is haunted by daemons
now).

Index: lisp/startup.el
===================================================================
RCS file: /cvsroot/emacs/emacs/lisp/startup.el,v
retrieving revision 1.502
diff -u -3 -p -r1.502 startup.el
--- lisp/startup.el	7 Aug 2008 03:10:08 -0000	1.502
+++ lisp/startup.el	21 Aug 2008 16:55:54 -0000
@@ -877,9 +877,11 @@ opening the first frame (e.g. open a con
 
   (run-hooks 'before-init-hook)
 
-  ;; Under X Window, this creates the X frame and deletes the terminal frame.
-  (when (fboundp 'frame-initialize)
-    (frame-initialize))
+  (if (daemonp)
+      (server-start)
+    ;; Under X Window, this creates the X frame and deletes the terminal frame.
+    (when (and (fboundp 'frame-initialize))
+      (frame-initialize)))
 
   ;; Turn off blinking cursor if so specified in X resources.  This is here
   ;; only because all other settings of no-blinking-cursor are here.
Index: src/emacs.c
===================================================================
RCS file: /cvsroot/emacs/emacs/src/emacs.c,v
retrieving revision 1.442
diff -u -3 -p -r1.442 emacs.c
--- src/emacs.c	5 Aug 2008 17:33:05 -0000	1.442
+++ src/emacs.c	21 Aug 2008 16:55:54 -0000
@@ -232,6 +232,9 @@ int noninteractive;
 
 int noninteractive1;
 
+/* Nonzero means Emacs was started as a daemon.  */
+int is_daemon = 0;
+
 /* Save argv and argc.  */
 char **initial_argv;
 int initial_argc;
@@ -1068,6 +1070,25 @@ main (int argc, char **argv)
       exit (0);
     }
 
+  if (argmatch (argv, argc, "-daemon", "--daemon", 5, NULL, &skip_args))
+    {
+      pid_t f = fork();
+      if (f > 0)
+	exit(0);
+      if (f < 0)
+	{
+	  fprintf (stderr, "Cannot fork!\n");
+	  exit(1);
+	}
+      close(0);
+      close(1);
+      close(2);
+      is_daemon = 1;
+#ifdef HAVE_SETSID
+      setsid();
+#endif
+    }
+
   if (! noninteractive)
     {
 #ifdef BSD_PGRPS
@@ -1715,6 +1736,7 @@ struct standard_args standard_args[] =
   { "-nw", "--no-windows", 110, 0 },
   { "-batch", "--batch", 100, 0 },
   { "-script", "--script", 100, 1 },
+  { "-daemon", "--daemon", 99, 0 },
   { "-help", "--help", 90, 0 },
   { "-no-unibyte", "--no-unibyte", 83, 0 },
   { "-multibyte", "--multibyte", 82, 0 },
@@ -2346,6 +2368,13 @@ decode_env_path (evarname, defalt)
   return Fnreverse (lpath);
 }
 
+DEFUN ("daemonp", Fdaemonp, Sdaemonp, 0, 0, 0,
+       doc: /* Make the current emacs process a daemon.*/)
+  (void)
+{
+  return is_daemon ? Qt : Qnil;
+}
+
 void
 syms_of_emacs ()
 {
@@ -2364,6 +2393,7 @@ syms_of_emacs ()
 
   defsubr (&Sinvocation_name);
   defsubr (&Sinvocation_directory);
+  defsubr (&Sdaemonp);
 
   DEFVAR_LISP ("command-line-args", &Vcommand_line_args,
 	       doc: /* Args passed by shell to Emacs, as a list of strings.




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

* Re: start emacs without creating frames and connect with emacsclient later
  2008-08-21 17:20   ` Dan Nicolaescu
@ 2008-08-21 22:55     ` David De La Harpe Golden
  2008-08-21 23:55       ` Dan Nicolaescu
  2008-08-22  7:24       ` Paul R
  2008-08-22 11:58     ` David Hansen
  1 sibling, 2 replies; 25+ messages in thread
From: David De La Harpe Golden @ 2008-08-21 22:55 UTC (permalink / raw)
  To: Dan Nicolaescu; +Cc: rms, emacs-devel

Dan Nicolaescu wrote:
> "Richard M. Stallman" <rms@gnu.org> writes:
> 
>   >     Should something like this go in?  We do have a feature freeze, but this
>   >     is quite simple, and it completes the multi-tty functionality, which is
>   >     one of the major features of 23.1.
>   > 
>   > I think it should go in
>   > provided you write the text for etc/NEWS AND all the relevant manuals.
> 
> I'll update the all the docs whenever it gets checked in. 
> 
> Here's a new version of the patch, now it has code to detach from the
> terminal, so it is a real daemon (how scary, emacs is haunted by daemons
> now).
> 

FWIW (Which may not be much...),  I'm not sure that
[not opening an initial frame yet retaining ability to open frames] and
[daemonizing (detaching from the process group)] and
[starting an emacsclient server]
should be conflated to quite such a degree.


IMO, a most useful use case for no initial full frame would be in
conjunction with systray support i.e. something like emacs --tray (or
better a customize variable) that means emacs docks into the systray
while obeying X session management.

That way one could start emacs like a mail client such as kmail,
at gui login, and it would be in the systray, popping up new frames
on left click, and perhaps having a right click menu
{new frame, start/stop server, quit} or the like.

Not sure any of that need to _daemonize_ though (not saying daemonizing
isn't useful!) - in fact it might be counterproductive, as emacs
might kind of "escape" the session manager.










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

* Re: start emacs without creating frames and connect with emacsclient later
  2008-08-21 22:55     ` David De La Harpe Golden
@ 2008-08-21 23:55       ` Dan Nicolaescu
  2008-08-22  5:59         ` tomas
  2008-08-22  7:24       ` Paul R
  1 sibling, 1 reply; 25+ messages in thread
From: Dan Nicolaescu @ 2008-08-21 23:55 UTC (permalink / raw)
  To: David De La Harpe Golden; +Cc: rms, emacs-devel

David De La Harpe Golden <david@harpegolden.net> writes:

  > Dan Nicolaescu wrote:
  > > "Richard M. Stallman" <rms@gnu.org> writes:
  > > 
  > >   >     Should something like this go in?  We do have a feature freeze, but this
  > >   >     is quite simple, and it completes the multi-tty functionality, which is
  > >   >     one of the major features of 23.1.
  > >   > 
  > >   > I think it should go in
  > >   > provided you write the text for etc/NEWS AND all the relevant manuals.
  > > 
  > > I'll update the all the docs whenever it gets checked in. 
  > > 
  > > Here's a new version of the patch, now it has code to detach from the
  > > terminal, so it is a real daemon (how scary, emacs is haunted by daemons
  > > now).
  > > 
  > 
  > FWIW (Which may not be much...),  I'm not sure that
  > [not opening an initial frame yet retaining ability to open frames] and
  > [daemonizing (detaching from the process group)] and
  > [starting an emacsclient server]
  > should be conflated to quite such a degree.

Well, it's a design goal, and it fits perfectly my use case, which is
not uncommon, a number of people have written scripts to simulate
something like this with a suspended screen session.

  > IMO, a most useful use case for no initial full frame would be in
  > conjunction with systray support i.e. something like emacs --tray (or
  > better a customize variable) that means emacs docks into the systray
  > while obeying X session management.

If someone wants to provide something like that, they can go ahead and
develop a patch.  (I personally have no interest for such a use).

  > That way one could start emacs like a mail client such as kmail,
  > at gui login, and it would be in the systray, popping up new frames
  > on left click, and perhaps having a right click menu
  > {new frame, start/stop server, quit} or the like.
  > 
  > Not sure any of that need to _daemonize_ though (not saying daemonizing
  > isn't useful!) - in fact it might be counterproductive, as emacs
  > might kind of "escape" the session manager.

The fact that emacs "escape" the session manager here is absolutely
intentional.  Being able to access an emacs session over the net,
keeping emacs alive if X crashes, etc is extremely helpful.




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

* Re: start emacs without creating frames and connect with emacsclient later
  2008-08-21 23:55       ` Dan Nicolaescu
@ 2008-08-22  5:59         ` tomas
  0 siblings, 0 replies; 25+ messages in thread
From: tomas @ 2008-08-22  5:59 UTC (permalink / raw)
  To: Dan Nicolaescu; +Cc: emacs-devel, rms, David De La Harpe Golden

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On Thu, Aug 21, 2008 at 04:55:59PM -0700, Dan Nicolaescu wrote:
> David De La Harpe Golden <david@harpegolden.net> writes:
[...]
>   > FWIW (Which may not be much...),  I'm not sure that
[...]
>   > should be conflated to quite such a degree.
[...]
> The fact that emacs "escape" the session manager here is absolutely
> intentional.  Being able to access an emacs session over the net,
> keeping emacs alive if X crashes, etc is extremely helpful.

As I understood David, he wasn't arguing against that, but for a
separation of concerns (daemon vs windowless), which actually are two
dimensions. Which IMHO makes a lot of sense.

Regards
- -- tomás
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (GNU/Linux)

iD8DBQFIrlWpBcgs9XrR2kYRAuiFAJ0RvRInk5A6MTjJlGn2BJlZnmoGzQCcCTVe
MLWLEvODbWtGGGpdNEI+QPk=
=Uw6J
-----END PGP SIGNATURE-----




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

* Re: start emacs without creating frames and connect with emacsclient later
  2008-08-21 22:55     ` David De La Harpe Golden
  2008-08-21 23:55       ` Dan Nicolaescu
@ 2008-08-22  7:24       ` Paul R
  1 sibling, 0 replies; 25+ messages in thread
From: Paul R @ 2008-08-22  7:24 UTC (permalink / raw)
  To: David De La Harpe Golden; +Cc: Dan Nicolaescu, rms, emacs-devel

On Thu, 21 Aug 2008 23:55:22 +0100, David De La Harpe Golden <david@harpegolden.net> said:
David> Not sure any of that need to _daemonize_ though (not saying
David> daemonizing isn't useful!) - in fact it might be
David> counterproductive, as emacs might kind of "escape" the session
David> manager.

As I understood it, the initial goal was to have a silent running
instance of emacs on a remote server or on the local machine that
would be accessed through multi-tty.
From this point, it would not be hard to develop a systray applet that
would launch some emacs clients or so. No need to have the emacs
*server* linked by any mean to the X session.

-- 
  Paul




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

* Re: start emacs without creating frames and connect with emacsclient later
  2008-08-21 17:20   ` Dan Nicolaescu
  2008-08-21 22:55     ` David De La Harpe Golden
@ 2008-08-22 11:58     ` David Hansen
  2008-09-01 14:03       ` Dan Nicolaescu
  1 sibling, 1 reply; 25+ messages in thread
From: David Hansen @ 2008-08-22 11:58 UTC (permalink / raw)
  To: emacs-devel

On Thu, 21 Aug 2008 10:20:28 -0700 Dan Nicolaescu wrote:

> +      close(0);
> +      close(1);
> +      close(2);

Just curious, never wrote a daemon: Shouldn't you dup2 /dev/null here or
something?  Not having these fds looks scary to me.

David





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

* Re: start emacs without creating frames and connect with emacsclient later
  2008-08-14  5:19 ` Richard M. Stallman
  2008-08-14 17:21   ` Stefan Monnier
  2008-08-21 17:20   ` Dan Nicolaescu
@ 2008-09-01 14:02   ` Dan Nicolaescu
  2008-09-01 18:29     ` Stefan Monnier
  2 siblings, 1 reply; 25+ messages in thread
From: Dan Nicolaescu @ 2008-09-01 14:02 UTC (permalink / raw)
  To: rms; +Cc: emacs-devel

"Richard M. Stallman" <rms@gnu.org> writes:

  >     Should something like this go in?  We do have a feature freeze, but this
  >     is quite simple, and it completes the multi-tty functionality, which is
  >     one of the major features of 23.1.
  > 
  > I think it should go in
  > provided you write the text for etc/NEWS AND all the relevant manuals.

I got a lot of private emails from people very happy to use this
feature.  
And some people actually want to distribute the patch.  So here's an
updated version including documentation.

Index: doc/emacs/cmdargs.texi
===================================================================
RCS file: /cvsroot/emacs/emacs/doc/emacs/cmdargs.texi,v
retrieving revision 1.3
diff -u -3 -p -r1.3 cmdargs.texi
--- doc/emacs/cmdargs.texi	22 Jan 2008 23:53:34 -0000	1.3
+++ doc/emacs/cmdargs.texi	1 Sep 2008 13:56:17 -0000
@@ -277,6 +277,15 @@ option and @samp{-Q} are the only option
 Start emacs with minimum customizations.  This is like using @samp{-q}
 and @samp{--no-site-file}, but also disables the startup screen.
 
+@item -daemon
+@opindex -daemon
+@itemx --daemon
+@opindex --daemon
+Start emacs in background as a daemon (i.e. it will disconnect from the
+terminal), do not open any frames and start the server.  Clients can
+connect and create graphical or terminal frames using
+@code{emacsclient}.
+
 @item --no-splash
 @opindex --no-splash
 @vindex inhibit-splash-screen
Index: etc/NEWS
===================================================================
RCS file: /cvsroot/emacs/emacs/etc/NEWS,v
retrieving revision 1.1843
diff -u -3 -p -r1.1843 NEWS
--- etc/NEWS	31 Aug 2008 02:13:38 -0000	1.1843
+++ etc/NEWS	1 Sep 2008 13:56:17 -0000
@@ -130,6 +130,11 @@ Now, the default behavior is to open a n
 Use the -c option to get the old behavior of opening files in the
 currently selected Emacs frame.
 
+*** Emacs can now start in background, as a daemon when using the
+--daemon command line argument.  It disconnects from the terminal and
+starts the server.  Clients can connect and create graphical or
+terminal frames using emacsclient.
+
 *** The new command close-display-connection closes a connection to a
 remote display.
 
Index: lisp/startup.el
===================================================================
RCS file: /cvsroot/emacs/emacs/lisp/startup.el,v
retrieving revision 1.503
diff -u -3 -p -r1.503 startup.el
--- lisp/startup.el	23 Aug 2008 16:59:19 -0000	1.503
+++ lisp/startup.el	1 Sep 2008 13:56:17 -0000
@@ -881,9 +881,11 @@ opening the first frame (e.g. open a con
 
   (run-hooks 'before-init-hook)
 
-  ;; Under X Window, this creates the X frame and deletes the terminal frame.
-  (when (fboundp 'frame-initialize)
-    (frame-initialize))
+  (if (daemonp)
+      (server-start)
+    ;; Under X Window, this creates the X frame and deletes the terminal frame.
+    (when (and (fboundp 'frame-initialize))
+      (frame-initialize)))
 
   ;; Turn off blinking cursor if so specified in X resources.  This is here
   ;; only because all other settings of no-blinking-cursor are here.
Index: src/emacs.c
===================================================================
RCS file: /cvsroot/emacs/emacs/src/emacs.c,v
retrieving revision 1.443
diff -u -3 -p -r1.443 emacs.c
--- src/emacs.c	23 Aug 2008 16:45:13 -0000	1.443
+++ src/emacs.c	1 Sep 2008 13:56:18 -0000
@@ -232,6 +232,9 @@ int noninteractive;
 
 int noninteractive1;
 
+/* Nonzero means Emacs was started as a daemon.  */
+int is_daemon = 0;
+
 /* Save argv and argc.  */
 char **initial_argv;
 int initial_argc;
@@ -1068,6 +1071,34 @@ main (int argc, char **argv)
       exit (0);
     }
 
+#ifndef DOS_NT
+  if (argmatch (argv, argc, "-daemon", "--daemon", 5, NULL, &skip_args))
+    {
+      pid_t f = fork();
+      int nfd;
+      if (f > 0)
+	exit(0);
+      if (f < 0)
+	{
+	  fprintf (stderr, "Cannot fork!\n");
+	  exit(1);
+	}
+
+      nfd = open("/dev/null", O_RDWR);
+      dup2(nfd, 0);
+      dup2(nfd, 1);
+      dup2(nfd, 2);
+      close (nfd);
+      is_daemon = 1;
+#ifdef HAVE_SETSID
+      setsid();
+#endif
+    }
+#else /* DOS_NT */
+  fprintf (stderr, "This platform does not support the -daemon flag.\n");
+  exit (1);
+#endif /* DOS_NT */
+
   if (! noninteractive)
     {
 #ifdef BSD_PGRPS
@@ -1719,6 +1750,7 @@ struct standard_args standard_args[] =
   { "-nw", "--no-windows", 110, 0 },
   { "-batch", "--batch", 100, 0 },
   { "-script", "--script", 100, 1 },
+  { "-daemon", "--daemon", 99, 0 },
   { "-help", "--help", 90, 0 },
   { "-no-unibyte", "--no-unibyte", 83, 0 },
   { "-multibyte", "--multibyte", 82, 0 },
@@ -2350,6 +2382,13 @@ decode_env_path (evarname, defalt)
   return Fnreverse (lpath);
 }
 
+DEFUN ("daemonp", Fdaemonp, Sdaemonp, 0, 0, 0,
+       doc: /* Make the current emacs process a daemon.*/)
+  (void)
+{
+  return is_daemon ? Qt : Qnil;
+}
+
 void
 syms_of_emacs ()
 {
@@ -2368,6 +2407,7 @@ syms_of_emacs ()
 
   defsubr (&Sinvocation_name);
   defsubr (&Sinvocation_directory);
+  defsubr (&Sdaemonp);
 
   DEFVAR_LISP ("command-line-args", &Vcommand_line_args,
 	       doc: /* Args passed by shell to Emacs, as a list of strings.




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

* Re: start emacs without creating frames and connect with emacsclient later
  2008-08-22 11:58     ` David Hansen
@ 2008-09-01 14:03       ` Dan Nicolaescu
  0 siblings, 0 replies; 25+ messages in thread
From: Dan Nicolaescu @ 2008-09-01 14:03 UTC (permalink / raw)
  To: emacs-devel

David Hansen <david.hansen@gmx.net> writes:

  > On Thu, 21 Aug 2008 10:20:28 -0700 Dan Nicolaescu wrote:
  > 
  > > +      close(0);
  > > +      close(1);
  > > +      close(2);
  > 
  > Just curious, never wrote a daemon: Shouldn't you dup2 /dev/null here or
  > something?  Not having these fds looks scary to me.

Thanks, sounds like a good idea!  The updated version that I just posted
does that.




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

* Re: start emacs without creating frames and connect with emacsclient later
  2008-09-01 14:02   ` Dan Nicolaescu
@ 2008-09-01 18:29     ` Stefan Monnier
  2008-09-01 23:11       ` Dan Nicolaescu
  0 siblings, 1 reply; 25+ messages in thread
From: Stefan Monnier @ 2008-09-01 18:29 UTC (permalink / raw)
  To: Dan Nicolaescu; +Cc: rms, emacs-devel

> I got a lot of private emails from people very happy to use this
> feature.   And some people actually want to distribute the patch.
> So here's an updated version including documentation.

What about the problem I mentioned that your code will end up not
calling `frame-initialize' at all?  Have you looked into it?


        Stefan




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

* Re: start emacs without creating frames and connect with emacsclient later
  2008-09-01 18:29     ` Stefan Monnier
@ 2008-09-01 23:11       ` Dan Nicolaescu
  2008-09-02 19:53         ` Stefan Monnier
  0 siblings, 1 reply; 25+ messages in thread
From: Dan Nicolaescu @ 2008-09-01 23:11 UTC (permalink / raw)
  To: Stefan Monnier; +Cc: rms, emacs-devel

Stefan Monnier <monnier@iro.umontreal.ca> writes:

  > > I got a lot of private emails from people very happy to use this
  > > feature.   And some people actually want to distribute the patch.
  > > So here's an updated version including documentation.
  > 
  > What about the problem I mentioned that your code will end up not
  > calling `frame-initialize' at all?  Have you looked into it?

AFAICT what is done in `frame-initialize' should not be needed in this case.




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

* Re: start emacs without creating frames and connect with emacsclient later
  2008-09-01 23:11       ` Dan Nicolaescu
@ 2008-09-02 19:53         ` Stefan Monnier
  2008-09-03  6:43           ` Dan Nicolaescu
  0 siblings, 1 reply; 25+ messages in thread
From: Stefan Monnier @ 2008-09-02 19:53 UTC (permalink / raw)
  To: Dan Nicolaescu; +Cc: rms, emacs-devel

>> > I got a lot of private emails from people very happy to use this
>> > feature.   And some people actually want to distribute the patch.
>> > So here's an updated version including documentation.
>> 
>> What about the problem I mentioned that your code will end up not
>> calling `frame-initialize' at all?  Have you looked into it?

> AFAICT what is done in `frame-initialize' should not be needed in this case.

Then could you add a comment that explains why?
If that's really the case, I could consider installing the code since it
seems otherwise to be OK.


        Stefan




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

* Re: start emacs without creating frames and connect with emacsclient later
  2008-09-02 19:53         ` Stefan Monnier
@ 2008-09-03  6:43           ` Dan Nicolaescu
  2008-09-03  6:50             ` martin rudalics
       [not found]             ` <jwv7i9tyzk5.fsf-monnier+emacs@gnu.org>
  0 siblings, 2 replies; 25+ messages in thread
From: Dan Nicolaescu @ 2008-09-03  6:43 UTC (permalink / raw)
  To: Stefan Monnier; +Cc: rms, emacs-devel

Stefan Monnier <monnier@IRO.UMontreal.CA> writes:

  > >> > I got a lot of private emails from people very happy to use this
  > >> > feature.   And some people actually want to distribute the patch.
  > >> > So here's an updated version including documentation.
  > >> 
  > >> What about the problem I mentioned that your code will end up not
  > >> calling `frame-initialize' at all?  Have you looked into it?
  > 
  > > AFAICT what is done in `frame-initialize' should not be needed in this case.
  > 
  > Then could you add a comment that explains why?

Does this sound OK?

Index: startup.el
===================================================================
RCS file: /cvsroot/emacs/emacs/lisp/startup.el,v
retrieving revision 1.503
diff -u -3 -p -u -p -r1.503 startup.el
--- startup.el	23 Aug 2008 16:59:19 -0000	1.503
+++ startup.el	3 Sep 2008 06:41:37 -0000
@@ -881,9 +881,15 @@ opening the first frame (e.g. open a con
 
   (run-hooks 'before-init-hook)
 
-  ;; Under X Window, this creates the X frame and deletes the terminal frame.
-  (when (fboundp 'frame-initialize)
-    (frame-initialize))
+  (if (daemonp)
+      ;; Just start the server here, no need to run
+      ;; `frame-initialize', it deals with creating a frame and
+      ;; setting the parameters for the initial frame, we don't need
+      ;; any of those.
+      (server-start)
+    ;; Under X Window, this creates the X frame and deletes the terminal frame.
+    (when (and (fboundp 'frame-initialize))
+      (frame-initialize)))
 
   ;; Turn off blinking cursor if so specified in X resources.  This is here
   ;; only because all other settings of no-blinking-cursor are here.




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

* Re: start emacs without creating frames and connect with emacsclient later
  2008-09-03  6:43           ` Dan Nicolaescu
@ 2008-09-03  6:50             ` martin rudalics
  2008-09-03 11:53               ` Dan Nicolaescu
       [not found]             ` <jwv7i9tyzk5.fsf-monnier+emacs@gnu.org>
  1 sibling, 1 reply; 25+ messages in thread
From: martin rudalics @ 2008-09-03  6:50 UTC (permalink / raw)
  To: Dan Nicolaescu; +Cc: emacs-devel

> +    (when (and (fboundp 'frame-initialize))
> +      (frame-initialize)))

Why this one-armed `and'?

martin






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

* Re: start emacs without creating frames and connect with emacsclient later
  2008-09-03  6:50             ` martin rudalics
@ 2008-09-03 11:53               ` Dan Nicolaescu
  2008-09-03 12:38                 ` martin rudalics
  0 siblings, 1 reply; 25+ messages in thread
From: Dan Nicolaescu @ 2008-09-03 11:53 UTC (permalink / raw)
  To: martin rudalics; +Cc: emacs-devel

martin rudalics <rudalics@gmx.at> writes:

  > > +    (when (and (fboundp 'frame-initialize))
  > > +      (frame-initialize)))
  > 
  > Why this one-armed `and'?

Please look at the diff again, that's preexisting code...




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

* Re: start emacs without creating frames and connect with emacsclient later
  2008-09-03 11:53               ` Dan Nicolaescu
@ 2008-09-03 12:38                 ` martin rudalics
  2008-09-03 20:14                   ` Dan Nicolaescu
  0 siblings, 1 reply; 25+ messages in thread
From: martin rudalics @ 2008-09-03 12:38 UTC (permalink / raw)
  To: Dan Nicolaescu; +Cc: emacs-devel

> Please look at the diff again, that's preexisting code...

The old code has ...

   ;; Under X Window, this creates the X frame and deletes the terminal frame.
   (when (fboundp 'frame-initialize)
     (frame-initialize))

... no `and'.

martin




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

* Re: start emacs without creating frames and connect with emacsclient later
  2008-09-03 12:38                 ` martin rudalics
@ 2008-09-03 20:14                   ` Dan Nicolaescu
  0 siblings, 0 replies; 25+ messages in thread
From: Dan Nicolaescu @ 2008-09-03 20:14 UTC (permalink / raw)
  To: martin rudalics; +Cc: emacs-devel

martin rudalics <rudalics@gmx.at> writes:

  > > Please look at the diff again, that's preexisting code...
  > 
  > The old code has ...
  > 
  >   ;; Under X Window, this creates the X frame and deletes the terminal frame.
  >   (when (fboundp 'frame-initialize)
  >     (frame-initialize))
  > 
  > ... no `and'.

Ah, OK, I'll get rid of it.   I planned to get rid of the `when' test
there anyway (it's always true), but that's for a different patch.




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

* Re: start emacs without creating frames and connect with emacsclient later
       [not found]                       ` <jwvskrt2tbz.fsf-monnier+emacs@gnu.org>
@ 2008-09-21 23:36                         ` Dan Nicolaescu
  0 siblings, 0 replies; 25+ messages in thread
From: Dan Nicolaescu @ 2008-09-21 23:36 UTC (permalink / raw)
  To: Stefan Monnier; +Cc: Chong Yidong, emacs-devel

Stefan Monnier <monnier@iro.umontreal.ca> writes:

  > > Setting `special-display-function' to 'special-display-popup-frame in
  > > `frame-initialize' does not do anything by default, the default value
  > > for `special-display-function' is:
  > 
  > > window.el:640:(defcustom special-display-function 'special-display-popup-frame
  > 
  > OK, so let's remove this `setq' and then install your patch.

Done.  Thanks!




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

end of thread, other threads:[~2008-09-21 23:36 UTC | newest]

Thread overview: 25+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2008-08-13 16:10 start emacs without creating frames and connect with emacsclient later Dan Nicolaescu
2008-08-13 16:40 ` Johannes Weiner
2008-08-13 20:49 ` Stefan Monnier
2008-08-14  5:19 ` Richard M. Stallman
2008-08-14 17:21   ` Stefan Monnier
2008-08-15 12:44     ` Richard M. Stallman
2008-08-15 15:25       ` Dan Nicolaescu
2008-08-21 17:20   ` Dan Nicolaescu
2008-08-21 22:55     ` David De La Harpe Golden
2008-08-21 23:55       ` Dan Nicolaescu
2008-08-22  5:59         ` tomas
2008-08-22  7:24       ` Paul R
2008-08-22 11:58     ` David Hansen
2008-09-01 14:03       ` Dan Nicolaescu
2008-09-01 14:02   ` Dan Nicolaescu
2008-09-01 18:29     ` Stefan Monnier
2008-09-01 23:11       ` Dan Nicolaescu
2008-09-02 19:53         ` Stefan Monnier
2008-09-03  6:43           ` Dan Nicolaescu
2008-09-03  6:50             ` martin rudalics
2008-09-03 11:53               ` Dan Nicolaescu
2008-09-03 12:38                 ` martin rudalics
2008-09-03 20:14                   ` Dan Nicolaescu
     [not found]             ` <jwv7i9tyzk5.fsf-monnier+emacs@gnu.org>
     [not found]               ` <200809211007.m8LA7TLQ014367@mothra.ics.uci.edu>
     [not found]                 ` <87k5d5u359.fsf@cyd.mit.edu>
     [not found]                   ` <jwvabe14o4w.fsf-monnier+emacs@gnu.org>
     [not found]                     ` <200809211826.m8LIQBbq016320@mothra.ics.uci.edu>
     [not found]                       ` <jwvskrt2tbz.fsf-monnier+emacs@gnu.org>
2008-09-21 23:36                         ` Dan Nicolaescu
2008-08-15 15:36 ` Phil Jackson

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