Subject: bug#19688: [patch] add support for emacs daemon on Windows

From: Eli Zaretskii <[email-masked]>
Date: Mon, 26 Jan 2015 08:00:37 +0200

[Please keep the bug address on the CC list, so that this whole
discussion gets archived.]

> Date: Mon, 26 Jan 2015 08:16:38 +0900
> From: Mark Laws <[email-masked]>
> 
> >> +#define W32_EMACS_SERVER_GUID "{0B8E5DCB-D7CF-4423-A9F1-2F6927F0D318}"
> >
> > Where did this GUID come from?
> >
> I generated it myself.

Is that safe?  Do we care whether this GUID is globally unique?  Why
exactly do we need it to begin with?

> Moved to ms-w32.h and removed changes relating to the header.

Thanks.

> >> +#else
> >> +bool w32_is_daemon;
> >> +bool w32_daemon_is_initialized;
> >> +static HANDLE w32_daemon_event;
> >> +#endif
> >
> > Why do we need anything beyond the event handle?  Can't it serve
> > double duty as a flag as well?  We could use INVALID_HANDLE_VALUE
> > and/or NULL as distinct values with specific meanings.
> 
> I've reworked it to use only w32_daemon_event. One minor issue: to
> avoid including any Windows-related headers in lisp.h, the extern
> declaration of w32_daemon_event there is void* instead of HANDLE. I
> doubt the type of HANDLE will ever change, so it shouldn't matter
> much.

That should be fine, but there's no need to have the handle in lisp.h,
either.  We could put it on w32.h, for example, which is already
included by emacs.c and other files.  If w32.h doesn't fit the bill,
please try to find some other w32*.h which does.

> >> +#ifndef WINDOWSNT
> >>    /* Make sure IS_DAEMON starts up as false.  */
> >>    daemon_pipe[1] = 0;
> >> +#endif
> >
> > You should do a similar initialization on WINDOWSNT, to avoid using
> > the value that was initialized when Emacs was dumped.
> 
> I'm not sure I understand. Do you mean for w32_daemon_event? If so, it
> should already always be zero when Emacs starts up.

It might not be zero.  Recall that Emacs is run during the build
process, when it executes some code, and then "dumps itself", which
involves writing portions of its memory to a disk file that thereafter
becomes the emacs.exe you run normally.  If, for some reason,
w32_daemon_event is initialized in this first instance, it will not be
zero in the dumped image.

So we always re-initialize such variables explicitly, instead of
relying on the linker initializations.  You can see that in the
sources, and that is also the reason why the Posix code initializes
daemon_pipe[1].

> > Also, the call to daemon_check_preconditions should be outside of the
> > #ifdef, as it is used on all platforms.
> 
> daemon_check_preconditions already gets called for both WINDOWSNT and
> !WINDOWSNT (see the #else). If we move it outside the #ifdef to
> eliminate having to mention it for both cases, then "nfd" and "err"
> will also have to be outside the ifdef in order to be compatible with
> strict pre-C99 compilers that only accept variable declarations at the
> beginning of blocks.

C99 compliant compiler is a prerequisite in the development version of
Emacs, so this problem doesn't exist.

Thanks.