Subject: bug#30421: 25.3; desktop.el: Steal lock when no living "emacs" process owns it

From: Pierre Neidhardt <[email-masked]>
Date: Sun, 11 Feb 2018 10:54:13 +0100

By default, desktop-save-mode will not save (and will set
desktop-dirname to nil) if a lock file exists with a PID in it.

It's problematic however when Emacs gets killed and does not release the
lock.  Upon next start, desktop-save-mode will refuse to save because
the lock exists, even though no process is using it.  In terms of user
experience, it's pretty bad considering the error feedback is just one
line in the *Messages* buffer (I almost never notice it when it happens)
and the problem is persistent across reboots (the lock file will remain
as long as the user does not remove it manually).

Furthermore, isn't it strange to just check if there lock file contains
a number and not actually check if it's an existing PID?

I think it would make much more sense to actually check if the owner is
an _existing_ process named "emacs".

Here is my suggested patch:

	(defun desktop-owner (&optional dirname)
	  "Return the PID of the Emacs process that owns the desktop file in DIRNAME.
	Return nil if no desktop file found or no Emacs process is using it.
	DIRNAME omitted or nil means use `desktop-dirname'."
	  (let (owner
	        (file (desktop-full-lock-name dirname)))
	    (and (file-exists-p file)
	         (ignore-errors
	           (with-temp-buffer
	             (insert-file-contents-literally file)
	             (goto-char (point-min))
	             (setq owner (read (current-buffer)))
	             (integerp owner)
	+            (process-attributes owner)
	+            (string= "emacs" (alist-get 'comm (process-attributes owner)))))
	         owner)))

In GNU Emacs 25.3.1 (x86_64-unknown-linux-gnu, GTK+ Version 3.22.26)
 of 2017-12-16 built on build
Windowing system distributor 'The X.Org Foundation', version 11.0.11906000
System Description:	Void Linux

Configured using:
 'configure --with-x-toolkit=gtk3 --with-xwidgets --prefix=/usr
 --sysconfdir=/etc --sbindir=/usr/bin --bindir=/usr/bin
 --mandir=/usr/share/man --infodir=/usr/share/info --localstatedir=/var
 --with-file-notification=inotify --with-modules --with-jpeg --with-tiff
 --with-gif --with-png --with-xpm --with-rsvg --without-imagemagick
 --with-xml2 --with-gnutls --with-sound --with-m17n-flt
 --host=x86_64-unknown-linux-gnu --build=x86_64-unknown-linux-gnu
 'CFLAGS=-fno-PIE -mtune=generic -O2 -pipe -g' 'CPPFLAGS= '
 'LDFLAGS=-no-pie -Wl,--as-needed ''

Configured features:
XPM JPEG TIFF GIF PNG RSVG SOUND DBUS GSETTINGS NOTIFY ACL GNUTLS
LIBXML2 FREETYPE M17N_FLT LIBOTF XFT ZLIB TOOLKIT_SCROLL_BARS GTK3 X11
MODULES XWIDGETS

Important settings:
  value of $LC_COLLATE: C
  value of $LANG: en_US.UTF-8
  locale-coding-system: utf-8-unix