>From ffa4fb0843a4db7a09519099e304e72cbffaf4b7 Mon Sep 17 00:00:00 2001 From: Jim Porter Date: Sun, 24 Oct 2021 10:42:28 -0700 Subject: [PATCH 1/3] Add --lazy-daemon option, used to start a daemon on-demand via emacsclient src/emacs.c (main, Fdaemon_initialized): Handle '--lazy-daemon'. (Fdaemon_type): New function. (syms_of_emacs): Add Sdaemon_type. lib-src/emacsclient.c (start_daemon_and_retry_set_socket): Use '--lazy-daemon' when starting daemon. --- lib-src/emacsclient.c | 8 ++++---- src/emacs.c | 46 ++++++++++++++++++++++++++++++++++++------- 2 files changed, 43 insertions(+), 11 deletions(-) diff --git a/lib-src/emacsclient.c b/lib-src/emacsclient.c index cff3cec2a7..40ebfa0513 100644 --- a/lib-src/emacsclient.c +++ b/lib-src/emacsclient.c @@ -1756,7 +1756,7 @@ start_daemon_and_retry_set_socket (void) else { char emacs[] = "emacs"; - char daemon_option[] = "--daemon"; + char daemon_option[] = "--lazy-daemon"; char *d_argv[3]; d_argv[0] = emacs; d_argv[1] = daemon_option; @@ -1764,8 +1764,8 @@ start_daemon_and_retry_set_socket (void) # ifdef SOCKETS_IN_FILE_SYSTEM if (socket_name != NULL) { - /* Pass --daemon=socket_name as argument. */ - const char *deq = "--daemon="; + /* Pass --lazy-daemon=socket_name as argument. */ + const char *deq = "--lazy-daemon="; char *daemon_arg = xmalloc (strlen (deq) + strlen (socket_name) + 1); strcpy (stpcpy (daemon_arg, deq), socket_name); @@ -1790,7 +1790,7 @@ start_daemon_and_retry_set_socket (void) it is ready to accept client connections, by asserting an event whose name is known to the daemon (defined by nt/inc/ms-w32.h). */ - if (!CreateProcess (NULL, (LPSTR)"emacs --daemon", NULL, NULL, FALSE, + if (!CreateProcess (NULL, (LPSTR)"emacs --lazy-daemon", NULL, NULL, FALSE, CREATE_NO_WINDOW, NULL, NULL, &si, &pi)) { char* msg = NULL; diff --git a/src/emacs.c b/src/emacs.c index a24543a586..e41b4abc49 100644 --- a/src/emacs.c +++ b/src/emacs.c @@ -245,6 +245,9 @@ #define MAIN_PROGRAM --chdir DIR change to directory DIR\n\ --daemon, --bg-daemon[=NAME] start a (named) server in the background\n\ --fg-daemon[=NAME] start a (named) server in the foreground\n\ +--lazy-daemon[=NAME] start a (named) server in the background\ + and record that it was started on-demand\n\ + by emacsclient\n\ --debug-init enable Emacs Lisp debugger for init file\n\ --display, -d DISPLAY use X server DISPLAY\n\ ", @@ -1635,12 +1638,17 @@ main (int argc, char **argv) { daemon_type = 2; /* background */ } + else if (argmatch (argv, argc, "-lazy-daemon", "--lazy-daemon", 12, NULL, &skip_args) + || argmatch (argv, argc, "-lazy-daemon", "--lazy-daemon", 12, &dname_arg, &skip_args)) + { + daemon_type = 3; /* background, lazy */ + } if (daemon_type > 0) { #ifndef DOS_NT - if (daemon_type == 2) + if (daemon_type == 2 || daemon_type == 3) { /* Start as a background daemon: fork a new child process which will run the rest of the initialization code, then exit. @@ -1668,7 +1676,7 @@ main (int argc, char **argv) fputs ("Cannot pipe!\n", stderr); exit (1); } - } /* daemon_type == 2 */ + } /* daemon_type == 2 || daemon_type == 3 */ #ifdef HAVE_LIBSYSTEMD /* Read the number of sockets passed through by systemd. */ @@ -1692,7 +1700,7 @@ main (int argc, char **argv) stderr); #endif /* USE_GTK */ - if (daemon_type == 2) + if (daemon_type == 2 || daemon_type == 3) { pid_t f; #ifndef DAEMON_MUST_EXEC @@ -1747,8 +1755,9 @@ main (int argc, char **argv) char fdStr[80]; int fdStrlen = snprintf (fdStr, sizeof fdStr, - "--bg-daemon=\n%d,%d\n%s", daemon_pipe[0], - daemon_pipe[1], dname_arg ? dname_arg : ""); + "--%s-daemon=\n%d,%d\n%s", daemon_pipe[0], + daemon_type == 2 ? "bg" : "lazy", daemon_pipe[1], + dname_arg ? dname_arg : ""); if (! (0 <= fdStrlen && fdStrlen < sizeof fdStr)) { @@ -1785,7 +1794,7 @@ main (int argc, char **argv) emacs_close (daemon_pipe[0]); setsid (); - } /* daemon_type == 2 */ + } /* daemon_type == 2 || daemon_type == 3 */ #elif defined(WINDOWSNT) /* Indicate that we want daemon mode. */ w32_daemon_event = CreateEvent (NULL, TRUE, FALSE, W32_DAEMON_EVENT); @@ -2372,6 +2381,7 @@ main (int argc, char **argv) { "-daemon", "--daemon", 99, 0 }, { "-bg-daemon", "--bg-daemon", 99, 0 }, { "-fg-daemon", "--fg-daemon", 99, 0 }, + { "-lazy-daemon", "--lazy-daemon", 99, 0 }, { "-help", "--help", 90, 0 }, { "-nl", "--no-loadup", 70, 0 }, { "-nsl", "--no-site-lisp", 65, 0 }, @@ -3128,6 +3138,27 @@ DEFUN ("daemonp", Fdaemonp, Sdaemonp, 0, 0, 0, return Qnil; } +DEFUN ("daemon-type", Fdaemon_type, Sdaemon_type, 0, 0, 0, + doc: /* If emacs was started as a daemon, return the type of daemon. +The result is one of `foreground', `background', or `lazy'. */) + (void) +{ + switch (abs (daemon_type)) + { + case 0: + return Qnil; + case 1: + return intern_c_string ("foreground"); + case 2: + return intern_c_string ("background"); + case 3: + return intern_c_string ("lazy"); + default: + error ("Unrecognized daemon type"); + } +} + + DEFUN ("daemon-initialized", Fdaemon_initialized, Sdaemon_initialized, 0, 0, 0, doc: /* Mark the Emacs daemon as being initialized. This finishes the daemonization process by doing the other half of detaching @@ -3153,7 +3184,7 @@ DEFUN ("daemon-initialized", Fdaemon_initialized, Sdaemon_initialized, 0, 0, 0, #endif /* HAVE_LIBSYSTEMD */ } - if (daemon_type == 2) + if (daemon_type == 2 || daemon_type == 3) { int nfd; @@ -3211,6 +3242,7 @@ syms_of_emacs (void) defsubr (&Sinvocation_name); defsubr (&Sinvocation_directory); defsubr (&Sdaemonp); + defsubr (&Sdaemon_type); defsubr (&Sdaemon_initialized); DEFVAR_LISP ("command-line-args", Vcommand_line_args, -- 2.25.1