[Bug 1205109] New: emacs cannot reach XDG_RUNTIME_DIR
![](https://seccdn.libravatar.org/avatar/a895f78a81a109471893519443e4d933.jpg?s=120&d=mm&r=g)
http://bugzilla.opensuse.org/show_bug.cgi?id=1205109
Bug ID: 1205109
Summary: emacs cannot reach XDG_RUNTIME_DIR
Classification: openSUSE
Product: openSUSE Distribution
Version: Leap 15.4
Hardware: All
OS: openSUSE Leap 15.4
Status: NEW
Severity: Normal
Priority: P5 - None
Component: Maintenance
Assignee: maintenance@opensuse.org
Reporter: zimtsui@gmail.com
QA Contact: qa-bugs@suse.de
Found By: ---
Blocker: ---
# Description
Emacs checks whether environment variable `XDG_RUNTIME_DIR` exists to determine
where a certain socket file should be placed. If `XDG_RUNTIME_DIR` doesn't
exist, emacs will fallback to some other location. See
https://git.savannah.gnu.org/cgit/emacs.git/tree/lib-src/emacsclient.c?h=ema...
`sudo` defaults to unset all environment variables and set some certain ones.
So any program run by `sudo` should miss `XDG_RUNTIME_DIR` in its environment.
If I run `sudo emacs-nox`, everything goes expectedly. But if I run `sudo
emacs`, emacs prompts `Unable to set up transient service directory:
XDG_RUNTIME_DIR "/run/user/0" not available: No such file or directory`.
# Possible reason
The binary `/usr/bin/emacs` is not built from emacs source, but made by the
SUSE package maintainer. The binary checks whether it's run in terminal or X to
determine whether it should exec `emacs-nox` or `emacs-x11`.
The `/usr/bin/emacs` binary defines an environment variable `XDG_RUNTIME_DIR`
as `/run/user/
![](https://seccdn.libravatar.org/avatar/a895f78a81a109471893519443e4d933.jpg?s=120&d=mm&r=g)
http://bugzilla.opensuse.org/show_bug.cgi?id=1205109
Andreas Stieger
![](https://seccdn.libravatar.org/avatar/a895f78a81a109471893519443e4d933.jpg?s=120&d=mm&r=g)
http://bugzilla.opensuse.org/show_bug.cgi?id=1205109
http://bugzilla.opensuse.org/show_bug.cgi?id=1205109#c1
Dr. Werner Fink
# Possible reason
The binary `/usr/bin/emacs` is not built from emacs source, but made by the SUSE package maintainer. The binary checks whether it's run in terminal or X to determine whether it should exec `emacs-nox` or `emacs-x11`.
/usr/bin/emacs is a shell script and not a binary which enables to switch between emacs-x11, emacs-gtk, or emacs-nox. Abd indeed this script also tries to provide a connection to the dbus API if not given.
The `/usr/bin/emacs` binary defines an environment variable `XDG_RUNTIME_DIR` as `/run/user/
` arbitrarily. But `/run/user/0` doesn't exist because it should be created by `/etc/profile` in a login shell. And `sudo` doesn't spawn a new login shell.
The /run/user/
![](https://seccdn.libravatar.org/avatar/a895f78a81a109471893519443e4d933.jpg?s=120&d=mm&r=g)
http://bugzilla.opensuse.org/show_bug.cgi?id=1205109
http://bugzilla.opensuse.org/show_bug.cgi?id=1205109#c2
Zim Tsui
Give the attached shell script as a replacement of /usr/bin/emacs a try and report if this does work for you.
I tried that but it doesn't work. Your substitute script simply replaces `UID` with `EUID`. This is redundant because `sudo` already handles that well. Try the below as regular user: ```shell $ sudo sh -c "echo \$UID" 0 $ sudo sh -c "echo \$EUID" 0 ``` The key point is that the directory `/run/user/0` doesn't exist if you have never logged in as root since boot. `su -` or `sudo -i` simply emulates a login shell, but actually doesn't create a new session. See https://github.com/systemd/systemd/issues/7451#issuecomment-346787237 A possible solution is doing nothing about the environment. Since emacs has its own logic about handling the absence of environment variables, why interfere it? -- You are receiving this mail because: You are on the CC list for the bug.
![](https://seccdn.libravatar.org/avatar/a895f78a81a109471893519443e4d933.jpg?s=120&d=mm&r=g)
http://bugzilla.opensuse.org/show_bug.cgi?id=1205109
http://bugzilla.opensuse.org/show_bug.cgi?id=1205109#c3
--- Comment #3 from Dr. Werner Fink
(In reply to Dr. Werner Fink from comment #1)
Give the attached shell script as a replacement of /usr/bin/emacs a try and report if this does work for you.
I tried that but it doesn't work.
Your substitute script simply replaces `UID` with `EUID`. This is redundant because `sudo` already handles that well. Try the below as regular user:
```shell $ sudo sh -c "echo \$UID" 0 $ sudo sh -c "echo \$EUID" 0 ```
The aim is to replace XDG_RUNTIME_DIR to the correct one
The key point is that the directory `/run/user/0` doesn't exist if you have never logged in as root since boot.
`su -` or `sudo -i` simply emulates a login shell, but actually doesn't create a new session. See https://github.com/systemd/systemd/issues/7451#issuecomment-346787237
A possible solution is doing nothing about the environment. Since emacs has its own logic about handling the absence of environment variables, why interfere it?
just try sudo /usr/bin/emacs-nox and then try with installed emacs-x11 sudo /usr/bin/emacs-gtk and see what happens ... I see e.g. XDG_RUNTIME_DIR (/run/user/223) is not owned by us (uid 0), but by uid 223! (This could e.g. happen if you try to connect to a non-root PulseAudio as a root user, over the native protocol. Don't do that.) that is the exported XDG variables will be taken by user root. -- You are receiving this mail because: You are on the CC list for the bug.
![](https://seccdn.libravatar.org/avatar/a895f78a81a109471893519443e4d933.jpg?s=120&d=mm&r=g)
http://bugzilla.opensuse.org/show_bug.cgi?id=1205109
http://bugzilla.opensuse.org/show_bug.cgi?id=1205109#c4
--- Comment #4 from Zim Tsui
just try
sudo /usr/bin/emacs-nox
and then try with installed emacs-x11
sudo /usr/bin/emacs-gtk
and see what happens ... I see e.g.
If I run `sudo emacs-nox` directly, no errors occurred. Only if I run `sudo emacs`, it prints errors. This is because the script `/usr/bin/emacs` set the variable `XDG_RUNTIME_DIR` to `/run/user/0` when I run `sudo emacs`, while the directory `/run/user/0` doesn't exist. The reason why `/run/user/0` doesn't exist is that `sudo` doesn't create a new real session to login as root(UID:0), it merely SIMULATES a login shell. The systemd is not aware of someone loging in as root, and will not create the directory `/run/user/0`.
The aim is to replace XDG_RUNTIME_DIR to the correct one
The variable `XDG_RUNTIME_DIR` should be managed only by systemd rather than by user scripts, because only systemd knows what to do else with the variable, such as creating the corresponding directories. It's dangerous to set the XDG_ variables manually. The simplist solution to this issue is not to set the XDG variables in `/usr/bin/emacs`, as if the variables are not set when `emacs-nox` is run directly. -- You are receiving this mail because: You are on the CC list for the bug.
![](https://seccdn.libravatar.org/avatar/a895f78a81a109471893519443e4d933.jpg?s=120&d=mm&r=g)
http://bugzilla.opensuse.org/show_bug.cgi?id=1205109
http://bugzilla.opensuse.org/show_bug.cgi?id=1205109#c5
--- Comment #5 from Dr. Werner Fink
(In reply to Dr. Werner Fink from comment #3)
just try
sudo /usr/bin/emacs-nox
and then try with installed emacs-x11
sudo /usr/bin/emacs-gtk
and see what happens ... I see e.g.
If I run `sudo emacs-nox` directly, no errors occurred.
I've asked for emacs-gtk not for emacs-nox, please install the package emacs-x11 and then retry with EMACS_TOOLKIT=gtk sudo emacs
Only if I run `sudo emacs`, it prints errors.
This is because the script `/usr/bin/emacs` set the variable `XDG_RUNTIME_DIR` to `/run/user/0` when I run `sudo emacs`, while the directory `/run/user/0` doesn't exist.
The reason why `/run/user/0` doesn't exist is that `sudo` doesn't create a new real session to login as root(UID:0), it merely SIMULATES a login shell. The systemd is not aware of someone loging in as root, and will not create the directory `/run/user/0`.
I'm aware of, nevertheless the question is how to solve the problem for the most emacs users out there. And the x11/gtk version of GNU Emacs is with dbus support. And this script exists e.g. for ssh connections.
The aim is to replace XDG_RUNTIME_DIR to the correct one
The variable `XDG_RUNTIME_DIR` should be managed only by systemd rather than by user scripts, because only systemd knows what to do else with the variable, such as creating the corresponding directories. It's dangerous to set the XDG_ variables manually.
This managment does currently not happen.
The simplist solution to this issue is not to set the XDG variables in `/usr/bin/emacs`, as if the variables are not set when `emacs-nox` is run directly.
That is a corner case ... I'm interested to solve it also for emacs with dbus support. -- You are receiving this mail because: You are on the CC list for the bug.
![](https://seccdn.libravatar.org/avatar/a895f78a81a109471893519443e4d933.jpg?s=120&d=mm&r=g)
http://bugzilla.opensuse.org/show_bug.cgi?id=1205109
Dr. Werner Fink
![](https://seccdn.libravatar.org/avatar/a895f78a81a109471893519443e4d933.jpg?s=120&d=mm&r=g)
http://bugzilla.opensuse.org/show_bug.cgi?id=1205109
http://bugzilla.opensuse.org/show_bug.cgi?id=1205109#c6
--- Comment #6 from Zim Tsui
I've asked for emacs-gtk not for emacs-nox, please install the package emacs-x11 and then retry with
EMACS_TOOLKIT=gtk sudo emacs
After trying what you said, more bugs appear. (=���=') If I run `sudo emacs`, - An error happened. - A terminal emacs is open. - The open emacs's process name is `emacs-gtk`. - There is an additional process `dbus-run-session`. If I run `EMACS_TOOLKIT=gtk sudo emacs`, - An error happened. - A terminal emacs is open. - The open emacs's process name is `emacs-gtk`. - There is an additional process `dbus-run-session`. If I run `sudo emacs-x11`, - No errors happened. - A terminal emacs is open. - The open emacs's process name is `emacs-x11`. If I run `sudo emacs-gtk`, - No errors happened. - A terminal emacs is open. - The open emacs's process name is `emacs-gtk`. If I switch to root via `su -`, and then run `emacs` - An error happened. - An X emacs is open. - The open emacs's process name is `emacs-gtk`. - There is an additional process `dbus-run-session`. It's difficult to describe all the behavior, why not reproduce that by your self? -- You are receiving this mail because: You are on the CC list for the bug.
![](https://seccdn.libravatar.org/avatar/a895f78a81a109471893519443e4d933.jpg?s=120&d=mm&r=g)
http://bugzilla.opensuse.org/show_bug.cgi?id=1205109
http://bugzilla.opensuse.org/show_bug.cgi?id=1205109#c7
--- Comment #7 from Dr. Werner Fink
(In reply to Dr. Werner Fink from comment #5)
It's difficult to describe all the behavior, why not reproduce that by your self?
Because I've less problem as I work often as root and therefore /run/user/0 simply exists -- You are receiving this mail because: You are on the CC list for the bug.
![](https://seccdn.libravatar.org/avatar/a895f78a81a109471893519443e4d933.jpg?s=120&d=mm&r=g)
http://bugzilla.opensuse.org/show_bug.cgi?id=1205109
http://bugzilla.opensuse.org/show_bug.cgi?id=1205109#c8
--- Comment #8 from Dr. Werner Fink
![](https://seccdn.libravatar.org/avatar/a895f78a81a109471893519443e4d933.jpg?s=120&d=mm&r=g)
http://bugzilla.opensuse.org/show_bug.cgi?id=1205109
http://bugzilla.opensuse.org/show_bug.cgi?id=1205109#c9
Zim Tsui
Btw:
I see for
sudo emacs-gtk XDG_RUNTIME_DIR (/run/user/223) is not owned by us (uid 0), but by uid 223! (This could e.g. happen if you try to connect to a non-root PulseAudio as a root user, over the native protocol. Don't do that.)
this is an error I guess
`XDG_RUNTIME_DIR` is always resolved to `/run/user/0`, because the script `/usr/bin/emacs` set `XDG_RUNTIME_DIR` to `/run/user/$UID`, where UID and EUID both equal 0 within `sudo`. It has nothing to do with the regular user. Besides, I'm not asking for help, I'm simply reporting a bug, because I seldom use Emacs. -- You are receiving this mail because: You are on the CC list for the bug.
![](https://seccdn.libravatar.org/avatar/a895f78a81a109471893519443e4d933.jpg?s=120&d=mm&r=g)
http://bugzilla.opensuse.org/show_bug.cgi?id=1205109
http://bugzilla.opensuse.org/show_bug.cgi?id=1205109#c10
--- Comment #10 from OBSbugzilla Bot
![](https://seccdn.libravatar.org/avatar/a895f78a81a109471893519443e4d933.jpg?s=120&d=mm&r=g)
http://bugzilla.opensuse.org/show_bug.cgi?id=1205109
http://bugzilla.opensuse.org/show_bug.cgi?id=1205109#c11
Dr. Werner Fink
participants (1)
-
bugzilla_noreply@suse.com