unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
* Unable to get dbus.el to work with the Gentoo OpenRC 'emacs' service
@ 2023-06-02 12:39 Alexis
  2023-06-02 13:18 ` Alexis
  2023-06-10 16:16 ` Michael Albinus
  0 siblings, 2 replies; 7+ messages in thread
From: Alexis @ 2023-06-02 12:39 UTC (permalink / raw)
  To: emacs-devel


[Please cc me on replies as i'm not currently subscribed to the 
list.]

Hi all,

For a while i've been manually starting an Emacs server on login 
(via my .zlogin), after a D-Bus session has been started (also via 
my .zlogin). However, i recently tried moving to the Gentoo OpenRC 
service, and have run into a problem regarding D-Bus.

The 'emacs' OpenRC service gets started at boot, prior to 
login. There is thus no D-Bus session bus available at that time; 
the DBUS_SESSION_BUS_ADDRESS env var isn't set. As noted above, 
this changes once i log in; the DBUS_SESSION_BUS_ADDRESS variable 
gets set. However, the Emacs server still thinks that var is 
unset.

i had assumed that running:

   emacsclient --eval "(setenv \"DBUS_SESSION_BUS_ADDRESS\" 
   \"${DBUS_SESSION_BUS_ADDRESS}\")"

in my WM startup script, to set that var in the environment of the 
server process, would address this.

However, while it addresses some issues - such as Emacs being able 
to open a new tab in an existing Firefox instance - it doesn't 
seem to affect the D-Bus environment in Emacs more generally:

    (dbus-get-unique-name :session)
    -> (dbus-error "No connection to bus" :session)

    (dbus-monitor :session)
    -> (dbus-error "No connection to bus" :session-private)

There seems to be no way of 'restarting' the "dbus" feature from 
within Emacs in order to get it to pick up the session bus 
address.

* (dbus-init-bus :session)
  -> (dbus-error "No connection to bus" :session)

* (dbus-init-bus (getenv "DBUS_SESSION_BUS_ADDRESS"))
  -> 2

  which means a new connection has been created, but:

  (dbus-monitor :session)
  -> "No connection to bus"

* (dbus-list-hash-table)
  -> (((:signal "unix:path=/tmp/dbus-[etc]

  where [etc] is the correct current value of 
  DBUS_SESSION_BUS_ADDRESS for the session, and yet:

  (dbus-monitor :session)
  -> "No connection to bus"

* Redefining `dbus-handle-bus-disconnect`:

(defun my-dbus-handle-bus-disconnect ()
  "React to a bus disconnection.
BUS is the bus that disconnected.  This routine unregisters all
handlers on the given bus and causes all synchronous calls
pending at the time of disconnect to fail."
  (let ((bus (getenv "DBUS_SESSION_BUS_ADDRESS"))
        keys-to-remove)
    (maphash
     (lambda (key value)
       (when (and (eq (nth 0 key) :serial)
                  (eq (nth 1 key) bus))
         (run-hook-with-args
          'dbus-event-error-functions
          (list 'dbus-event
                bus
                dbus-message-type-error
                (nth 2 key) ; serial
                nil         ; service
                nil         ; destination
                nil         ; path
                nil         ; interface
                nil         ; member
                value)      ; handler
          (list 'dbus-error dbus-error-disconnected  "Bus 
          disconnected" bus))
         (push key keys-to-remove)))
     dbus-registered-objects-table)
    (dolist (key keys-to-remove)
      (remhash key dbus-registered-objects-table))))

  followed by:

  (my-dbus-handle-bus-disconnect)
  (dbus-ignore-errors
    (dbus-init-bus :session))

  returns `nil` (i.e. without errors), but

  (dbus-monitor :session)
  -> "No connection to bus"

* (unload-feature 'dbus t)

where `t` is required due to various in-tree packages having a 
dependency on dbus.el, returns `nil` (i.e. without errors), but:

  (require 'dbus)
  -> dbus
  (dbus-monitor :session)
  -> "No connection to bus"

What am i missing? Or is it simply not possible to 'restart' 
dbus.el from ELisp in order to get it to create a :session bus 
based on the updated value of DBUS_SESSION_BUS_ADDRESS in the 
server process environment?


Alexis.



^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Unable to get dbus.el to work with the Gentoo OpenRC 'emacs' service
  2023-06-02 12:39 Unable to get dbus.el to work with the Gentoo OpenRC 'emacs' service Alexis
@ 2023-06-02 13:18 ` Alexis
  2023-06-10 16:16 ` Michael Albinus
  1 sibling, 0 replies; 7+ messages in thread
From: Alexis @ 2023-06-02 13:18 UTC (permalink / raw)
  To: emacs-devel


Alexis <flexibeast@gmail.com> writes:

> [Please cc me on replies as i'm not currently subscribed to the 
> list.]
>
> Hi all,
>
> For a while i've been manually starting an Emacs server on login 
> (via
> my .zlogin), after a D-Bus session has been started (also via my
> .zlogin). However, i recently tried moving to the Gentoo OpenRC
> service, and have run into a problem regarding D-Bus.
>
> The 'emacs' OpenRC service gets started at boot, prior to login.

Oh, sorry, i meant to note that OpenRC does not currently have 
user services, though that functionality is being worked on:

    https://github.com/OpenRC/openrc/pull/573


Alexis.



^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Unable to get dbus.el to work with the Gentoo OpenRC 'emacs' service
  2023-06-02 12:39 Unable to get dbus.el to work with the Gentoo OpenRC 'emacs' service Alexis
  2023-06-02 13:18 ` Alexis
@ 2023-06-10 16:16 ` Michael Albinus
  2023-06-12  3:02   ` Alexis
  2023-06-12  8:25   ` Robert Pluim
  1 sibling, 2 replies; 7+ messages in thread
From: Michael Albinus @ 2023-06-10 16:16 UTC (permalink / raw)
  To: Alexis; +Cc: emacs-devel

[-- Attachment #1: Type: text/plain, Size: 2735 bytes --]

Alexis <flexibeast@gmail.com> writes:

> Hi all,

Hi Alexis,

> For a while i've been manually starting an Emacs server on login (via
> my .zlogin), after a D-Bus session has been started (also via my
> .zlogin). However, i recently tried moving to the Gentoo OpenRC
> service, and have run into a problem regarding D-Bus.
>
> The 'emacs' OpenRC service gets started at boot, prior to login. There
> is thus no D-Bus session bus available at that time; the
> DBUS_SESSION_BUS_ADDRESS env var isn't set. As noted above, this
> changes once i log in; the DBUS_SESSION_BUS_ADDRESS variable gets
> set. However, the Emacs server still thinks that var is unset.
>
> i had assumed that running:
>
>   emacsclient --eval "(setenv \"DBUS_SESSION_BUS_ADDRESS\"
>   \"${DBUS_SESSION_BUS_ADDRESS}\")"
>
> in my WM startup script, to set that var in the environment of the
> server process, would address this.
>
> However, while it addresses some issues - such as Emacs being able to
> open a new tab in an existing Firefox instance - it doesn't seem to
> affect the D-Bus environment in Emacs more generally:
>
>    (dbus-get-unique-name :session)
>    -> (dbus-error "No connection to bus" :session)

I can reproduce it. On my Fedora desktop, I have reproduced it with
running from a shell

--8<---------------cut here---------------start------------->8---
# unsetenv DBUS_SESSION_BUS_ADDRESS
# emacs -Q
--8<---------------cut here---------------end--------------->8---

If I now eval the following in the *scratch* buffer

--8<---------------cut here---------------start------------->8---
(setenv "DBUS_SESSION_BUS_ADDRESS" "unix:path=/run/user/1000/bus")
(dbus--init-bus :session)
--8<---------------cut here---------------end--------------->8---

I get the same error like you

--8<---------------cut here---------------start------------->8---
-> (dbus-error "No connection to bus" :session)
--8<---------------cut here---------------end--------------->8---

Debugging Fdbus__init_bus in dbusbind.c shows, that the error happens in
XD_DBUS_VALIDATE_BUS_ADDRESS. This macro calls on C level

--8<---------------cut here---------------start------------->8---
getenv ("DBUS_SESSION_BUS_ADDRESS")
--8<---------------cut here---------------end--------------->8---

However, the (setenv ...) form above is on Lisp level, and it writes the
change into the internal Lisp variable process-environment. This doesn't
match.

> What am i missing? Or is it simply not possible to 'restart' dbus.el
> from ELisp in order to get it to create a :session bus based on the
> updated value of DBUS_SESSION_BUS_ADDRESS in the server process
> environment?

The appended patch has fixed this for me. Could you, please, check?

> Alexis.

Best regards, Michael.


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: Type: text/x-patch, Size: 1518 bytes --]

diff --git a/src/dbusbind.c b/src/dbusbind.c
index d96b735c79a..96f9329a97e 100644
--- a/src/dbusbind.c
+++ b/src/dbusbind.c
@@ -276,7 +276,9 @@ XD_OBJECT_TO_STRING (Lisp_Object object)

 #define XD_DBUS_VALIDATE_BUS_ADDRESS(bus)				\
   do {									\
-    char const *session_bus_address = getenv ("DBUS_SESSION_BUS_ADDRESS"); \
+    Lisp_Object session_bus_address					\
+      = Fgetenv_internal (build_string ("DBUS_SESSION_BUS_ADDRESS"),	\
+			  Qnil);					\
     if (STRINGP (bus))							\
       {									\
 	DBusAddressEntry **entries;					\
@@ -289,9 +291,8 @@ #define XD_DBUS_VALIDATE_BUS_ADDRESS(bus)				\
 	dbus_error_free (&derror);					\
 	dbus_address_entries_free (entries);				\
 	/* Canonicalize session bus address.  */			\
-	if ((session_bus_address != NULL)				\
-	    && (!NILP (Fstring_equal					\
-		       (bus, build_string (session_bus_address)))))	\
+	if ((STRINGP (session_bus_address))				\
+	    && (!NILP (Fstring_equal (bus, session_bus_address))))	\
 	  bus = QCsession;						\
       }									\
 									\
@@ -304,7 +305,7 @@ #define XD_DBUS_VALIDATE_BUS_ADDRESS(bus)				\
 	  XD_SIGNAL2 (build_string ("Wrong bus name"), bus);		\
 	/* We do not want to have an autolaunch for the session bus.  */ \
 	if ((EQ (bus, QCsession) || EQ (bus, QCsession_private))	\
-	    && session_bus_address == NULL)				\
+	    && NILP (session_bus_address))				\
 	  XD_SIGNAL2 (build_string ("No connection to bus"), bus);	\
       }									\
   } while (0)

^ permalink raw reply related	[flat|nested] 7+ messages in thread

* Re: Unable to get dbus.el to work with the Gentoo OpenRC 'emacs' service
  2023-06-10 16:16 ` Michael Albinus
@ 2023-06-12  3:02   ` Alexis
  2023-06-12 10:51     ` Michael Albinus
  2023-06-12  8:25   ` Robert Pluim
  1 sibling, 1 reply; 7+ messages in thread
From: Alexis @ 2023-06-12  3:02 UTC (permalink / raw)
  To: Michael Albinus; +Cc: emacs-devel


Michael Albinus <michael.albinus@gmx.de> writes:

> The appended patch has fixed this for me. Could you, please, 
> check?

Yep, that appears to have fixed the issue. :-) Thanks Michael!


Alexis.



^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Unable to get dbus.el to work with the Gentoo OpenRC 'emacs' service
  2023-06-10 16:16 ` Michael Albinus
  2023-06-12  3:02   ` Alexis
@ 2023-06-12  8:25   ` Robert Pluim
  2023-06-12 10:48     ` Michael Albinus
  1 sibling, 1 reply; 7+ messages in thread
From: Robert Pluim @ 2023-06-12  8:25 UTC (permalink / raw)
  To: Michael Albinus; +Cc: Alexis, emacs-devel

>>>>> On Sat, 10 Jun 2023 18:16:36 +0200, Michael Albinus <michael.albinus@gmx.de> said:
    Michael> diff --git a/src/dbusbind.c b/src/dbusbind.c
    Michael> index d96b735c79a..96f9329a97e 100644
    Michael> --- a/src/dbusbind.c
    Michael> +++ b/src/dbusbind.c
    Michael> @@ -276,7 +276,9 @@ XD_OBJECT_TO_STRING (Lisp_Object object)

    Michael>  #define XD_DBUS_VALIDATE_BUS_ADDRESS(bus)				\
    Michael>    do {									\
    Michael> -    char const *session_bus_address = getenv ("DBUS_SESSION_BUS_ADDRESS"); \
    Michael> +    Lisp_Object session_bus_address					\
    Michael> +      = Fgetenv_internal (build_string ("DBUS_SESSION_BUS_ADDRESS"),	\
    Michael> +			  Qnil);
    Michael>    \

Since youʼre already at the C level, perhaps you could use `egetenv'
instead and avoid the C<->lisp string changes.

Robert
-- 



^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Unable to get dbus.el to work with the Gentoo OpenRC 'emacs' service
  2023-06-12  8:25   ` Robert Pluim
@ 2023-06-12 10:48     ` Michael Albinus
  0 siblings, 0 replies; 7+ messages in thread
From: Michael Albinus @ 2023-06-12 10:48 UTC (permalink / raw)
  To: Robert Pluim; +Cc: Alexis, emacs-devel

Robert Pluim <rpluim@gmail.com> writes:

Hi Robert,

> Since youʼre already at the C level, perhaps you could use `egetenv'
> instead and avoid the C<->lisp string changes.

Indeed, I didn't think about. Thanks!

> Robert

Best regards, Michael.



^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Unable to get dbus.el to work with the Gentoo OpenRC 'emacs' service
  2023-06-12  3:02   ` Alexis
@ 2023-06-12 10:51     ` Michael Albinus
  0 siblings, 0 replies; 7+ messages in thread
From: Michael Albinus @ 2023-06-12 10:51 UTC (permalink / raw)
  To: Alexis; +Cc: emacs-devel

[-- Attachment #1: Type: text/plain, Size: 363 bytes --]

Alexis <flexibeast@gmail.com> writes:

Hi Alexis,

>> The appended patch has fixed this for me. Could you, please, check?
>
> Yep, that appears to have fixed the issue. :-) Thanks Michael!

Thanks for the feedback, I've pushed the modified patch to the emacs-29
branch. The patch shrinks to just one line change, see appended.

> Alexis.

Best regards, Michael.


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: Type: text/x-patch, Size: 510 bytes --]

diff --git a/src/dbusbind.c b/src/dbusbind.c
index d96b735c79a..97094e407d7 100644
--- a/src/dbusbind.c
+++ b/src/dbusbind.c
@@ -276,7 +276,7 @@ XD_OBJECT_TO_STRING (Lisp_Object object)

 #define XD_DBUS_VALIDATE_BUS_ADDRESS(bus)				\
   do {									\
-    char const *session_bus_address = getenv ("DBUS_SESSION_BUS_ADDRESS"); \
+    char const *session_bus_address = egetenv ("DBUS_SESSION_BUS_ADDRESS"); \
     if (STRINGP (bus))							\
       {									\
 	DBusAddressEntry **entries;					\

^ permalink raw reply related	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2023-06-12 10:51 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-06-02 12:39 Unable to get dbus.el to work with the Gentoo OpenRC 'emacs' service Alexis
2023-06-02 13:18 ` Alexis
2023-06-10 16:16 ` Michael Albinus
2023-06-12  3:02   ` Alexis
2023-06-12 10:51     ` Michael Albinus
2023-06-12  8:25   ` Robert Pluim
2023-06-12 10:48     ` Michael Albinus

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).