Function: kill-emacs

kill-emacs is an interactive function defined in emacs.c.

Signature

(kill-emacs &optional ARG RESTART)

Documentation

Exit the Emacs job and kill it.

If ARG is an integer, return ARG as the exit program code. If ARG is a string, stuff it as keyboard input. Any other value of ARG, or ARG omitted, means return an exit code that indicates successful program termination.

If RESTART is non-nil, instead of just exiting at the end, start a new Emacs process, using the same command line arguments as the currently running Emacs process.

This function is called upon receipt of the signals SIGTERM or SIGHUP, and upon SIGINT in batch mode. (Other fatal signals shut down Emacs without calling this function.)

The value of kill-emacs-hook, if not void, is a list of functions
(of no args), all of which are called before Emacs is actually
killed.

View in manual

Probably introduced at or before Emacs version 17.

Key Bindings

Source Code

// Defined in /usr/src/emacs/src/emacs.c
{
  int exit_code;

#ifndef WINDOWSNT
  /* Do some checking before shutting down Emacs, because errors
     can't be meaningfully reported afterwards.  */
  if (!NILP (restart)
      /* Don't perform the following checks when Emacs is running as
	 an Android GUI application, because there the system is
	 relied on to restart Emacs.  */
#if defined HAVE_ANDROID && !defined ANDROID_STUBIFY
      && !android_init_gui
#endif
      )
    {
      /* This is very unlikely, but it's possible to execute a binary
	 (on some systems) with no argv.  */
      if (initial_argc < 1)
	error ("No command line arguments known; unable to re-execute Emacs");

      /* Check that the binary hasn't gone away.  */
      if (!initial_emacs_executable)
	error ("Unknown Emacs executable");

      if (!file_access_p (initial_emacs_executable, F_OK))
	error ("Emacs executable \"%s\" can't be found", initial_argv[0]);
    }
#endif

#ifdef HAVE_LIBSYSTEMD
  /* Notify systemd we are shutting down, but only if we have notified
     it about startup.  */
  if (daemon_type == -1)
    sd_notify(0, "STOPPING=1");
#endif /* HAVE_LIBSYSTEMD */

  /* Fsignal calls emacs_abort () if it sees that waiting_for_input is
     set.  */
  waiting_for_input = 0;
  if (!NILP (find_symbol_value (Qkill_emacs_hook)))
    {
      if (noninteractive)
	safe_run_hooks (Qkill_emacs_hook);
      else
	call1 (Qrun_hook_query_error_with_timeout, Qkill_emacs_hook);
    }

#ifdef HAVE_X_WINDOWS
  /* Transfer any clipboards we own to the clipboard manager.  */
  x_clipboard_manager_save_all ();
#endif

  shut_down_emacs (0, (STRINGP (arg) && !feof (stdin)) ? arg : Qnil);

  /* If we have an auto-save list file,
     kill it because we are exiting Emacs deliberately (not crashing).
     Do it after shut_down_emacs, which does an auto-save.  */
  if (STRINGP (Vauto_save_list_file_name))
    {
      Lisp_Object listfile;
      listfile = Fexpand_file_name (Vauto_save_list_file_name, Qnil);
      emacs_unlink (SSDATA (listfile));
    }

#ifdef HAVE_NATIVE_COMP
  eln_load_path_final_clean_up ();
#endif
#if defined HAVE_ANDROID && !defined ANDROID_STUBIFY
  if (android_init_gui)
    {
      struct sigaction sa;

      /* Calls to exit may be followed by invalid accesses from
	 toolkit-managed threads as the thread group is destroyed, which
	 are inconsequential when the process is being terminated, but
	 which must be suppressed to inhibit reporting of superfluous
	 crashes by the system.

         Execution won't return to Emacs whatever the value of RESTART,
         as `android_restart_emacs' will only ever abort or succeed.  */
      sigemptyset (&sa.sa_mask);
      sa.sa_handler = _exit;
      sigaction (SIGSEGV, &sa, NULL);
      sigaction (SIGBUS, &sa, NULL);
    }
#endif /* HAVE_ANDROID && !ANDROID_STUBIFY */

  if (!NILP (restart))
    {
      turn_on_atimers (false);
#if defined HAVE_ANDROID && !defined ANDROID_STUBIFY
      /* Re-executing the Emacs process created by the system doesn't
	 work.  Instead, schedule a restart for a few hundered
	 milliseconds and exit Emacs.  */
      if (android_init_gui)
	android_restart_emacs ();
#endif
#ifdef WINDOWSNT
      if (w32_reexec_emacs (initial_cmdline, initial_wd) < 0)
#else
      initial_argv[0] = initial_emacs_executable;
      if (execvp (*initial_argv, initial_argv) < 1)
#endif
	emacs_perror ("Unable to re-execute Emacs");
    }

  if (FIXNUMP (arg))
    exit_code = (XFIXNUM (arg) < 0
		 ? XFIXNUM (arg) | INT_MIN
		 : XFIXNUM (arg) & INT_MAX);
  else
    exit_code = EXIT_SUCCESS;
  exit (exit_code);
}