diff options
| author | Juanma Barranquero | 2008-12-12 00:33:30 +0000 |
|---|---|---|
| committer | Juanma Barranquero | 2008-12-12 00:33:30 +0000 |
| commit | c63a334eb0558bbc2f04cd9aa3483fe040029499 (patch) | |
| tree | e9109557f9163be3dde8333ec286cf31ea9c39ab /lisp/server.el | |
| parent | 9f215d25e03320d6fea344f87894fcae64c9375f (diff) | |
| download | emacs-c63a334eb0558bbc2f04cd9aa3483fe040029499.tar.gz emacs-c63a334eb0558bbc2f04cd9aa3483fe040029499.zip | |
* server.el (server-sentinel): Uncomment code to delete connection file.
(server-start): Save the connection file in the server property list.
Delete it only when we are reasonably convinced that it is not owned by
a running server.
(server-force-delete): New command to force-delete the connection file,
and stop the server if it is running.
(server-running-p): Return t also for local TCP servers when we find a
process with a matching PID, and :other for undecided cases.
Diffstat (limited to 'lisp/server.el')
| -rw-r--r-- | lisp/server.el | 95 |
1 files changed, 67 insertions, 28 deletions
diff --git a/lisp/server.el b/lisp/server.el index d488fb1f4ca..627805da66c 100644 --- a/lisp/server.el +++ b/lisp/server.el | |||
| @@ -325,11 +325,12 @@ If CLIENT is non-nil, add a description of it to the logged message." | |||
| 325 | (process-query-on-exit-flag proc)) | 325 | (process-query-on-exit-flag proc)) |
| 326 | (set-process-query-on-exit-flag proc nil)) | 326 | (set-process-query-on-exit-flag proc nil)) |
| 327 | ;; Delete the associated connection file, if applicable. | 327 | ;; Delete the associated connection file, if applicable. |
| 328 | ;; This is actually problematic: the file may have been overwritten by | 328 | ;; Although there's no 100% guarantee that the file is owned by the |
| 329 | ;; another Emacs server in the mean time, so it's not ours any more. | 329 | ;; running Emacs instance, server-start uses server-running-p to check |
| 330 | ;; (and (process-contact proc :server) | 330 | ;; for possible servers before doing anything, so it *should* be ours. |
| 331 | ;; (eq (process-status proc) 'closed) | 331 | (and (process-contact proc :server) |
| 332 | ;; (ignore-errors (delete-file (process-get proc :server-file)))) | 332 | (eq (process-status proc) 'closed) |
| 333 | (ignore-errors (delete-file (process-get proc :server-file)))) | ||
| 333 | (server-log (format "Status changed to %s: %s" (process-status proc) msg) proc) | 334 | (server-log (format "Status changed to %s: %s" (process-status proc) msg) proc) |
| 334 | (server-delete-client proc)) | 335 | (server-delete-client proc)) |
| 335 | 336 | ||
| @@ -458,34 +459,37 @@ job. To use the server, set up the program `emacsclient' in the | |||
| 458 | Emacs distribution as your standard \"editor\". | 459 | Emacs distribution as your standard \"editor\". |
| 459 | 460 | ||
| 460 | Optional argument LEAVE-DEAD (interactively, a prefix arg) means just | 461 | Optional argument LEAVE-DEAD (interactively, a prefix arg) means just |
| 461 | kill any existing server communications subprocess." | 462 | kill any existing server communications subprocess. |
| 463 | |||
| 464 | If a server is already running, the server is not started. | ||
| 465 | To force-start a server, do \\[server-force-delete] and then | ||
| 466 | \\[server-start]." | ||
| 462 | (interactive "P") | 467 | (interactive "P") |
| 463 | (when (or | 468 | (when (or |
| 464 | (not server-clients) | 469 | (not server-clients) |
| 465 | (yes-or-no-p | 470 | (yes-or-no-p |
| 466 | "The current server still has clients; delete them? ")) | 471 | "The current server still has clients; delete them? ")) |
| 467 | (when server-process | 472 | (let* ((server-dir (if server-use-tcp server-auth-dir server-socket-dir)) |
| 468 | ;; kill it dead! | 473 | (server-file (expand-file-name server-name server-dir))) |
| 469 | (ignore-errors (delete-process server-process))) | 474 | (when server-process |
| 470 | ;; Delete the socket files made by previous server invocations. | 475 | ;; kill it dead! |
| 471 | (when server-socket-dir | 476 | (ignore-errors (delete-process server-process))) |
| 472 | (condition-case () | 477 | ;; Delete the socket files made by previous server invocations. |
| 473 | (delete-file (expand-file-name server-name server-socket-dir)) | 478 | (if (not (eq t (server-running-p server-name))) |
| 474 | (error nil))) | 479 | ;; Remove any leftover socket or authentication file |
| 475 | ;; If this Emacs already had a server, clear out associated status. | 480 | (ignore-errors (delete-file server-file)) |
| 476 | (while server-clients | 481 | (setq server-mode nil) ;; already set by the minor mode code |
| 477 | (server-delete-client (car server-clients))) | 482 | (error "Server %S is already running" server-name)) |
| 478 | ;; Now any previous server is properly stopped. | 483 | ;; If this Emacs already had a server, clear out associated status. |
| 479 | (if leave-dead | 484 | (while server-clients |
| 480 | (progn | 485 | (server-delete-client (car server-clients))) |
| 481 | (server-log (message "Server stopped")) | 486 | ;; Now any previous server is properly stopped. |
| 482 | (setq server-process nil)) | 487 | (if leave-dead |
| 483 | (let* ((server-dir (if server-use-tcp server-auth-dir server-socket-dir)) | 488 | (progn |
| 484 | (server-file (expand-file-name server-name server-dir))) | 489 | (server-log (message "Server stopped")) |
| 490 | (setq server-process nil)) | ||
| 485 | ;; Make sure there is a safe directory in which to place the socket. | 491 | ;; Make sure there is a safe directory in which to place the socket. |
| 486 | (server-ensure-safe-dir server-dir) | 492 | (server-ensure-safe-dir server-dir) |
| 487 | ;; Remove any leftover socket or authentication file. | ||
| 488 | (ignore-errors (delete-file server-file)) | ||
| 489 | (when server-process | 493 | (when server-process |
| 490 | (server-log (message "Restarting server"))) | 494 | (server-log (message "Restarting server"))) |
| 491 | (letf (((default-file-modes) ?\700)) | 495 | (letf (((default-file-modes) ?\700)) |
| @@ -516,6 +520,7 @@ kill any existing server communications subprocess." | |||
| 516 | :service server-file | 520 | :service server-file |
| 517 | :plist '(:authenticated t))))) | 521 | :plist '(:authenticated t))))) |
| 518 | (unless server-process (error "Could not start server process")) | 522 | (unless server-process (error "Could not start server process")) |
| 523 | (process-put server-process :server-file server-file) | ||
| 519 | (when server-use-tcp | 524 | (when server-use-tcp |
| 520 | (let ((auth-key | 525 | (let ((auth-key |
| 521 | (loop | 526 | (loop |
| @@ -533,14 +538,48 @@ kill any existing server communications subprocess." | |||
| 533 | " " (int-to-string (emacs-pid)) | 538 | " " (int-to-string (emacs-pid)) |
| 534 | "\n" auth-key))))))))) | 539 | "\n" auth-key))))))))) |
| 535 | 540 | ||
| 541 | ;;;###autoload | ||
| 542 | (defun server-force-delete (&optional name) | ||
| 543 | "Unconditionally delete connection file for server NAME. | ||
| 544 | If server is running, it is first stopped. | ||
| 545 | NAME defaults to `server-name'. With argument, ask for NAME." | ||
| 546 | (interactive | ||
| 547 | (list (if current-prefix-arg | ||
| 548 | (read-string "Server name: " nil nil server-name)))) | ||
| 549 | (when server-mode (with-temp-message nil (server-mode -1))) | ||
| 550 | (let ((file (expand-file-name (or name server-name) | ||
| 551 | (if server-use-tcp | ||
| 552 | server-auth-dir | ||
| 553 | server-socket-dir)))) | ||
| 554 | (condition-case nil | ||
| 555 | (progn | ||
| 556 | (delete-file file) | ||
| 557 | (message "Connection file %S deleted" file)) | ||
| 558 | (file-error | ||
| 559 | (message "No connection file %S" file))))) | ||
| 560 | |||
| 536 | (defun server-running-p (&optional name) | 561 | (defun server-running-p (&optional name) |
| 537 | "Test whether server NAME is running." | 562 | "Test whether server NAME is running. |
| 563 | |||
| 564 | Return values: | ||
| 565 | nil the server is definitely not running. | ||
| 566 | t the server seems to be running. | ||
| 567 | something else we cannot determine whether it's running without using | ||
| 568 | commands which may have to wait for a long time." | ||
| 538 | (interactive | 569 | (interactive |
| 539 | (list (if current-prefix-arg | 570 | (list (if current-prefix-arg |
| 540 | (read-string "Server name: " nil nil server-name)))) | 571 | (read-string "Server name: " nil nil server-name)))) |
| 541 | (unless name (setq name server-name)) | 572 | (unless name (setq name server-name)) |
| 542 | (condition-case nil | 573 | (condition-case nil |
| 543 | (progn | 574 | (if server-use-tcp |
| 575 | (with-temp-buffer | ||
| 576 | (insert-file-contents-literally (expand-file-name name server-auth-dir)) | ||
| 577 | (or (and (looking-at "127\.0\.0\.1:[0-9]+ \\([0-9]+\\)") | ||
| 578 | (assq 'comm | ||
| 579 | (system-process-attributes | ||
| 580 | (string-to-number (match-string 1)))) | ||
| 581 | t) | ||
| 582 | :other)) | ||
| 544 | (delete-process | 583 | (delete-process |
| 545 | (make-network-process | 584 | (make-network-process |
| 546 | :name "server-client-test" :family 'local :server nil :noquery t | 585 | :name "server-client-test" :family 'local :server nil :noquery t |