Function: server-start
server-start is an autoloaded, interactive and byte-compiled function
defined in server.el.gz.
Signature
(server-start &optional LEAVE-DEAD INHIBIT-PROMPT)
Documentation
Allow this Emacs process to be a server for client processes.
This starts a server communications subprocess through which client
"editors" can send your editing commands to this Emacs job.
To use the server, set up the program emacsclient in the Emacs
distribution as your standard "editor".
Optional argument LEAVE-DEAD (interactively, a prefix arg) means just kill any existing server communications subprocess.
If a server is already running, restart it. If clients are running, ask the user for confirmation first, unless optional argument INHIBIT-PROMPT is non-nil.
To force-start a server, do M-x server-force-delete (server-force-delete) and then
M-x server-start (server-start).
To check from a Lisp program whether a server is running, use
the server-process variable.
Probably introduced at or before Emacs version 18.
Key Bindings
Source Code
;; Defined in /usr/src/emacs/lisp/server.el.gz
;;;###autoload
(defun server-start (&optional leave-dead inhibit-prompt)
"Allow this Emacs process to be a server for client processes.
This starts a server communications subprocess through which client
\"editors\" can send your editing commands to this Emacs job.
To use the server, set up the program `emacsclient' in the Emacs
distribution as your standard \"editor\".
Optional argument LEAVE-DEAD (interactively, a prefix arg) means just
kill any existing server communications subprocess.
If a server is already running, restart it. If clients are
running, ask the user for confirmation first, unless optional
argument INHIBIT-PROMPT is non-nil.
To force-start a server, do \\[server-force-delete] and then
\\[server-start].
To check from a Lisp program whether a server is running, use
the `server-process' variable."
(interactive "P")
(when (or (not server-clients)
;; Ask the user before deleting existing clients---except
;; when we can't get user input, which may happen when
;; doing emacsclient --eval "(kill-emacs)" in daemon mode.
(cond
((and (daemonp)
(null (cdr (frame-list)))
(eq (selected-frame) terminal-frame))
leave-dead)
(inhibit-prompt t)
(t (yes-or-no-p
"The current server still has clients; delete them? "))))
(let* ((server-dir (if server-use-tcp server-auth-dir server-socket-dir))
(server-file (expand-file-name server-name server-dir)))
(when server-process
;; kill it dead!
(ignore-errors (delete-process server-process)))
;; Check to see if an uninitialized external socket has been
;; passed in, if that is the case, skip checking
;; `server-running-p' as this will return the wrong result.
(if (and internal--daemon-sockname
(not server--external-socket-initialized))
(setq server--external-socket-initialized t)
;; Delete the socket files made by previous server invocations.
(if (not (eq t (server-running-p server-name)))
;; Remove any leftover socket or authentication file.
(ignore-errors
(let (delete-by-moving-to-trash)
(delete-file server-file)
;; Also delete the directory that the server file was
;; created in -- but only in /tmp (see bug#44644).
;; There may be other servers running, too, so this may
;; fail.
(when (equal (file-name-directory
(directory-file-name
(file-name-directory server-file)))
"/tmp/")
(ignore-errors
(delete-directory (file-name-directory server-file))))))
(setq server-mode nil) ;; already set by the minor mode code
(display-warning
'server
(concat "Unable to start the Emacs server.\n"
(format "There is an existing Emacs server, named %S.\n"
server-name)
(substitute-command-keys
"To start the server in this Emacs process, stop the existing
server or call `\\[server-force-delete]' to forcibly disconnect it."))
:warning)
(setq leave-dead t)))
;; If this Emacs already had a server, clear out associated status.
(while server-clients
(server-delete-client (car server-clients)))
;; Now any previous server is properly stopped.
(if leave-dead
(progn
(unless (eq t leave-dead) (server-log (message "Server stopped")))
(setq server-process nil))
;; Make sure there is a safe directory in which to place the socket.
(server-ensure-safe-dir server-dir)
(when server-process
(server-log (message "Restarting server")))
(cl-letf (((default-file-modes) ?\700))
(add-hook 'suspend-tty-functions #'server-handle-suspend-tty)
(add-hook 'delete-frame-functions #'server-handle-delete-frame)
(add-hook 'kill-emacs-query-functions
#'server-kill-emacs-query-function)
;; We put server's kill-emacs-hook after the others, so that
;; frames are not deleted too early, because doing that
;; would severely degrade our abilities to communicate with
;; the user, while some hooks may wish to ask the user
;; questions (e.g., desktop-kill).
(add-hook 'kill-emacs-hook #'server-force-stop t) ;Cleanup upon exit.
(setq server-process
(apply #'make-network-process
:name server-name
:server t
:noquery t
:sentinel #'server-sentinel
:filter #'server-process-filter
:use-external-socket t
;; We must receive file names without being decoded.
;; Those are decoded by server-process-filter according
;; to file-name-coding-system. Also don't get
;; confused by CRs since we don't quote them.
:coding 'raw-text-unix
;; The other args depend on the kind of socket used.
(if server-use-tcp
(list :family 'ipv4 ;; We're not ready for IPv6 yet
:service (or server-port t)
:host (or server-host 'local)
:plist '(:authenticated nil))
(list :family 'local
:service server-file
:plist '(:authenticated t)))))
(unless server-process (error "Could not start server process"))
(process-put server-process :server-file server-file)
(when server-use-tcp
(let ((auth-key (server-get-auth-key)))
(process-put server-process :auth-key auth-key)
(with-temp-file server-file
(set-buffer-multibyte nil)
(setq buffer-file-coding-system 'no-conversion)
(insert (format-network-address
(process-contact server-process :local))
" " (number-to-string (emacs-pid)) ; Kept for compatibility
"\n" auth-key)))))))))