Emacs - Error when calling (server-start)

2019-01-08 04:17发布

I am currently using GNU Emacs 23.0.93.1 in Windows Vista SP1. In my .emacs file I make a call to (server-start) and that is causing an error with the message The directory ~/.emacs.d/server is unsafe. Has anyone seen this and know a fix or workaround? ... other than leaving server turned off ;)

Here is the stack trace:

Debugger entered--Lisp error: (error "The directory ~/.emacs.d/server is unsafe")
  signal(error ("The directory ~/.emacs.d/server is unsafe"))
  error("The directory %s is unsafe" "~/.emacs.d/server")
  server-ensure-safe-dir("~\\.emacs.d\\server\\")
  server-start(nil)
  call-interactively(server-start t nil)
  execute-extended-command(nil)
  call-interactively(execute-extended-command nil nil)

11条回答
2楼-- · 2019-01-08 04:41

Additionally you do not want the server to be started in batch-mode. In my .emacs I therefore use

(defconst --batch-mode 
  (or noninteractive (member "--batch-mode" command-line-args))
  "True when running in batch-mode (--batch-mode command-line switch set).")

and then

(unless --batch-mode
  (require 'server)
  (when (and (= emacs-major-version 23)
         (= emacs-minor-version 1)
         (equal window-system 'w32))
    ;; Suppress error "directory ~/.emacs.d/server is unsafe" on Windows.
    (defun server-ensure-safe-dir (dir) "Noop" t))
  (server-start))

Still the server feature is capricious: server-start throws when the %HOME%/.emacs.d/server directory does not exist. In succession Emacs won't start up again! The obvious solution is to create the missing directory and try again; I found the solution somewhere on the net but really can't remember where. The following code runs successfully for years now on several of my Windows machines:

(unless --batch-mode
  (require 'server)
  (when (and (= emacs-major-version 23)
         (= emacs-minor-version 1)
         (equal window-system 'w32))
    ;; Suppress error "directory ~/.emacs.d/server is unsafe" on Windows.
    (defun server-ensure-safe-dir (dir) "Noop" t))
  (condition-case nil
      (server-start)
    (error
     (let* ((server-dir (if server-use-tcp server-auth-dir server-socket-dir)))
       (when (and server-use-tcp
          (not (file-accessible-directory-p server-dir)))
     (display-warning
      'server (format "Creating %S" server-dir) :warning)
     (make-directory server-dir t)
     (server-start))))
    )
  )

This code also works when running Emacs from a stick.

Hope this helps.

查看更多
祖国的老花朵
3楼-- · 2019-01-08 04:47

To avoid hacking in the lisp directory, you can just add the following to your .emacs:

(require 'server) (and (>= emacs-major-version 23) (defun server-ensure-safe-dir (dir) "Noop" t))

查看更多
一纸荒年 Trace。
4楼-- · 2019-01-08 04:54

Below step works for me: 1. Execute code below as .reg file. Emacs win version will treat any values in registry as Env Var.

[HKEY_LOCAL_MACHINE\SOFTWARE\GNU\Emacs]
"HOME"="C:/<your_emacs_home>"
"EMACS_SERVER_FILE"="C:/<your_emacs_home>/server/main_server"
"ALTERNATE_EDITOR"="C:/<your_emacs_loc>/bin/runemacs.exe"
  1. Add code below to your .emacs/init.el. The key here should be "server-auth-dir".
(require 'server)
(setq server-auth-dir "~/server")  ;;Server file location
(setq server-name "main_server")   ;;Server mutex file name
(server-start)

By steps above server mode works for me correctly and perfect.

查看更多
beautiful°
5楼-- · 2019-01-08 04:57

Very helpful answer from gavenkoa. I'm having this problem on Emacs 24.1, Windows 2003.

Unfortunately, overriding server-ensure-safe-dir to become a noop, as suggested in your first snippet, didn't work for me in all situations. Specifically, it did not work when applied before (server-start) had executed at least once, because the initial execution would also create the directory, if it doesn't exist. With the noop version, the directory would not be created at all.

The workaround that worked for me in the sense that it eliminated the error message, while still creating the directory properly, was the following code, put before (server-start) in my Emacs initialization file. It puts an advice around server-ensure-safe-dir to ignore any errors raised from there. Doesn't solve the root cause of the problem, but good enough for me.

(defadvice server-ensure-safe-dir (around
                                   my-around-server-ensure-safe-dir
                                   activate)
  "Ignores any errors raised from server-ensure-safe-dir"
  (ignore-errors ad-do-it))
查看更多
我想做一个坏孩纸
6楼-- · 2019-01-08 04:59

last time I tried, the "Take ownership" shell extension did the job

查看更多
聊天终结者
7楼-- · 2019-01-08 05:03

This is a known Emacs bug on Windows. A workaround is to comment out this line in server-ensure-safe-dir in server.el the you'll want to byte recompile after the change:

;; FIXME: Busted on Windows. 
;; (eql (nth 2 attrs) (user-uid)) 
查看更多
登录 后发表回答