Emacs - Error when calling (server-start)
Asked Answered
M

11

83

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)
Mien answered 20/5, 2009 at 1:35 Comment(1)
I had a similar error starting Emacs on a Unix box; this turned out to be because I keep my ~/.emacs.d in Subversion, and upon checkout the svn client set ~/.emacs.d/server to 0755 per my umask. Instead of wrapping svn in a shell script, which defeats the purpose of having everything I need to customize Emacs live in Subversion, I just have Emacs force the correct permissions when it starts up, by adding the following to ~/.emacs.d/init.el: (set-file-modes (expand-file-name "~/.emacs.d/server") #o700)Weisburgh
B
151

I found this solution on the EmacsWiki:

The problem is the ownership of the directory ~/.emacs.d/server when you also have “Administrators” rights on your account. Create the directory ~/.emacs.d/server and set the owner of this directory to your login name and the problem is gone. As I have a “Dutch” version of Windows 7 I don’t know the English terms exactly but here’s the procedure:

Click R-mouse on ~/.emacs.d/server and select “Properties” (last item in menu). From Properties select the Tab “Security” and then select the button “Advanced”. Then select the Tab “Owner” and change the owner from Administrators (<your-pc-name>\Administrators) into <your-login-name> (<your-pc-name>\<your-login-name>. Now the server code will accept this directory as secure because you are the owner.

Hope this helps for all you guys, it solved the problem for me anyway.

W.K.R. Reutefleut

It definitely works on Vista, with Emacs 23.2.1.

Barb answered 31/5, 2010 at 14:37 Comment(3)
Fixed the problem on Windows 7 too. From bash,$ chown -R "${USER}" ~/.emacs.dStooge
It works on spacemacs [email protected] (therefore it should as well work on plain emacs 25.1.1).Chimerical
chmod 700 is OK too.Woodenhead
B
31

I enjoy to anwer of larsreed, but complite code ready to use:

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

I discass this issue in my blog article http://brain-break.blogspot.com/2009/08/when-moving-from-gnu-emacs-22.html

Also note that in 2009-09-19 fixed bug #4197 about server-ensure-safe-dir so in incoming Emacs 23.2 this workaround is not needed.

Under recently released Emacs 23.2 I have such warning:

Warning (server): Using ~/.emacs.d/server to store Emacs-server authentication files. Directories on FAT32 filesystems are NOT secure against tampering. See variable server-auth-dir for details.

To fix this as say warning you can point server-auth-dir to NTFS partition (%APPDATA% usually located Windows %SYSTEMDRIVE% and user usually format system drive as NTFS partition):

(require 'server)
(when (and (eq window-system 'w32) (file-exists-p (getenv "APPDATA")))
  (setq server-auth-dir (concat (getenv "APPDATA") "/.emacs.d/server"))
  (make-directory server-auth-dir)  )
(server-start)
Burden answered 14/10, 2009 at 14:25 Comment(9)
using emacs23 and windows vista 64b, this fixed it for me. Without the require server didn't work.Ramses
2Mario this because problem lies in FAT32 fs, not Windows itself.Burden
Thanks, this works for me on Win7-64. Note with EmacsW32 you can leave out the server-start, as it will do that later.Aromatic
This still occurs in Emacs 23.3.1 so I changed the condition to <code> (= emacs-major-version 23) (>= emacs-minor-version 1) (<= emacs-minor-version 3) </code> but that will need updating if this is not fixed in 23.4 .... So, see the comment below by @user160983 which ignores the emacs-minor-versionLubric
as I commented on earlier answer the problem exists in emacs 24 so the conditional should be (>= emacs-major-version 24)Gley
@Gley I fix to (>= emacs-major-version 23). Thanks +1Burden
This is the only solution which works for me (Emacs 24.5 on Win7-64, installed via Chocolatey).Pandit
As per my answer, run this at the command prompt: takeown /f %USERPROFILE%\.emacs.d\server /r /d yHendon
I like this pure elisp solution! thanks.Varick
L
14

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)) 
Lucre answered 20/5, 2009 at 1:56 Comment(1)
Your solution fixes my problem. I will investigate this further later and add my findings here. Thanks!Unbending
S
11

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))

Smorgasbord answered 21/8, 2009 at 18:32 Comment(4)
I had the above issue with Emacs in Cygwin and the above command worked.Brumal
the problem also occurs in emacs 24 so maybe a (>= emacs-major-version 23) would be appropriateGley
for me on Windows 7 with Emacs 24 this did not work until I added the line (require 'server) above the codeTedmann
This worked for me for years until I used this on MacOS 10.14 (Mojave) at which point it breaks magit (I share my init files across all platforms). Use the answer by gavenkoa above that also includes the constraint to restrict the change to Windows.Tessi
N
7

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.

Neper answered 10/8, 2011 at 11:20 Comment(2)
Hi there! This is not working on Linux. command-line-args never include my --batch argument. This works: (if (not noninteractive) (progn (require 'server) (server-start))) Primaveria
Ok, thanks. So then we should refine --batch-mode as (defconst --batch-mode (or noninteractive (member "--batch-mode" command-line-args))). I fix it above.Neper
L
6

Did not work for me in Windows 7.

I instead read the comments in server-ensure-safe-dir and proceeded with taking the ownership for %APPDATA% forlder and subfolders. They were owned by local Administrators, not by me.

That helped!

Louth answered 29/3, 2010 at 6:39 Comment(1)
To elaborate: Make sure that the directory %HOME%\.emacs.d\server is owned by you, not by the local Administrators group.Strobila
T
3

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))
Tiptoe answered 12/6, 2013 at 15:21 Comment(0)
H
3

If it's the server folder ownership issue that RealityMonster identified, then you can run this at the windows command prompt to fix it:

takeown /f %USERPROFILE%\.emacs.d\server /r /d y
Hendon answered 1/9, 2017 at 14:24 Comment(1)
This is the only answer that really worked for me (Windows 10 and Emacs 27.0.50). Thanks.Leaven
B
2

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.

Bernardinabernardine answered 2/1, 2015 at 16:51 Comment(0)
H
1

In case this occasionally hits people, my workstation just went through a "domain migration", which added another permission to every file on the box, then I started getting this error. After I added the expression to dummy out "server-ensure-safe-dir" this stopped failing.

(If you're wondering, the migration will be in 2-3 steps. The first one adds the permission for me in the target domain, then I get moved to the target domain, then they might (I'm not sure about this) remove the permission for the old domain. It's a big company, and many users, so they're doing it in separate steps.)

Huttan answered 9/8, 2010 at 23:15 Comment(0)
M
1

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

Meatiness answered 3/7, 2011 at 22:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.