Function: daemon-initialized

daemon-initialized is a function defined in emacs.c.

Signature

(daemon-initialized)

Documentation

Mark the Emacs daemon as being initialized.

This finishes the daemonization process by doing the other half of detaching from the parent process and its tty file descriptors.

Source Code

// Defined in /usr/src/emacs/src/emacs.c
{
  bool err = 0;

  if (!IS_DAEMON)
    error ("This function can only be called if emacs is run as a daemon");

  if (!DAEMON_RUNNING)
    error ("The daemon has already been initialized");

  if (NILP (Vafter_init_time))
    error ("This function can only be called after loading the init files");
#ifndef WINDOWSNT

  if (daemon_type == 1)
    {
#ifdef HAVE_LIBSYSTEMD
      sd_notify(0, "READY=1");
#endif /* HAVE_LIBSYSTEMD */
    }

  if (daemon_type == 2)
    {
      int nfd;

      /* Get rid of stdin, stdout and stderr.  */
      nfd = emacs_open_noquit ("/dev/null", O_RDWR, 0);
      err |= nfd < 0;
      err |= dup2 (nfd, STDIN_FILENO) < 0;
      err |= dup2 (nfd, STDOUT_FILENO) < 0;
      err |= dup2 (nfd, STDERR_FILENO) < 0;
      err |= emacs_close (nfd) != 0;

      /* Closing the pipe will notify the parent that it can exit.
         FIXME: In case some other process inherited the pipe, closing it here
         won't notify the parent because it's still open elsewhere, so we
         additionally send a byte, just to make sure the parent really exits.
         Instead, we should probably close the pipe in start-process and
         call-process to make sure the pipe is never inherited by
         subprocesses.  */
      err |= write (daemon_pipe[1], "\n", 1) < 0;
      err |= emacs_close (daemon_pipe[1]) != 0;
    }

  /* Set it to an invalid value so we know we've already run this function.  */
  daemon_type = -daemon_type;

#else  /* WINDOWSNT */
  /* Signal the waiting emacsclient process.  */
  err |= SetEvent (w32_daemon_event) == 0;
  err |= CloseHandle (w32_daemon_event) == 0;
  /* Set it to an invalid value so we know we've already run this function.  */
  w32_daemon_event = INVALID_HANDLE_VALUE;
#endif

  if (err)
    error ("I/O error during daemon initialization");
  return Qt;
}