18

I'm trying to run D-Bus on an embedded system (Yocto Linux) and connect to it from my application code.

I get the following error when I call dbus_bus_get(DBUS_BUS_SESSION, &err);

Using X11 for dbus-daemon autolaunch was disabled 
at compile time, set your DBUS_SESSION_BUS_ADDRESS 
instead

I realize that I need to start the dbus-daemon first so I have run dbus-launch from the command line.

This prints out a value of DBUS_SESSION_BUS_ADDRESS but how could I export it programmatically?

Russia Must Remove Putin
  • 374,368
  • 89
  • 403
  • 331
User55412
  • 882
  • 2
  • 10
  • 27

6 Answers6

38

I've finally found the answer, running the following command exports the output of dbus-launch:

export $(dbus-launch)
User55412
  • 882
  • 2
  • 10
  • 27
9
pid_gnome=$(pgrep gnome-session)
DBUS_SESSION_BUS_ADDRESS=$(grep -z DBUS_SESSION_BUS_ADDRESS /proc/${pid_gnome}/environ|cut -d= -f2-)
export DBUS_SESSION_BUS_ADDRESS=${DBUS_SESSION_BUS_ADDRESS}

Please make sure that the user has the DISPLAY variable set.

Another alternative is:

export $(dbus-launch)
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
user3801989
  • 91
  • 1
  • 1
  • 1
    `pgrep gnome-session` may output multiple PIDs (e.g. three processes on Ubuntu 20.04), so it should be `pgrep gnome-session | head -1`. `DBUS_SESSION_BUS_ADDRESS=$(grep ... | cut -d= -f2-)` ends with `000a` (`\0\n`), which causes warning `bash: warning: command substitution: ignored null byte in input` when `export`. So it should be `DBUS_SESSION_BUS_ADDRESS=$(grep ... | cut -d= -f2- | tr -d '\0\n')`. – wataash Apr 12 '21 at 08:20
  • WIth `grep -P`, `| cut` is not needed. `export DBUS_SESSION_BUS_ADDRESS=$(grep -P -o -a "(?<=DBUS_SESSION_BUS_ADDRESS=).+?(?=\x00)" /proc/"$(pgrep gnome-session | head -1)"/environ)` – wataash Mar 21 '23 at 15:26
7

Type the following command into a terminal:

eval `dbus-launch --auto-syntax`
User55412
  • 882
  • 2
  • 10
  • 27
  • 8
    Thank you for this code snippet, which might provide some limited, immediate help. A [proper explanation](https://meta.stackexchange.com/q/114762/349538) would greatly improve its long-term value by showing why this is a good solution to the problem and would make it more useful to future readers with other, similar questions. Please [edit] your answer to add some explanation, including the assumptions you’ve made. – Dwhitz Mar 26 '19 at 08:43
  • Please fix your backslash. I cannot because it would not be enough characters to allow a change. – Jonathan Komar Jun 07 '20 at 08:52
  • This is not good ans. After doing this, I cant use RDP. – haofeng Oct 18 '20 at 07:41
1

When you launch your user session, do it like this:

dbus-daemon --session --fork --print-address 1 > /tmp/dbus-session-addr.txt

This will cause the session address to be written to /tmp/dbus-session-addr.txt. (The filename's not that important, it's just somewhere you've decided to store it.)

Then, when you need the variable set:

export DBUS_SESSION_BUS_ADDRESS=$(cat /tmp/dbus-session-addr.txt)

If your shell's sniffy about export-during-definition - some can be - do it in two stages:

DBUS_SESSION_BUS_ADDRESS=$(cat /tmp/dbus-session-addr.txt)
export DBUS_SESSION_BUS_ADDRESS
JonGreen
  • 369
  • 1
  • 5
0

It sounds like you are trying to get the value of DBUS_SESSION_BUS_ADDRESS in order for your application to run correctly. Try running it with dbus-run-session instead of dbus-launch. According to https://dbus.freedesktop.org/doc/dbus-launch.1.html dbus-launch should not be run from the command line, but in a shell script (see also https://dbus.freedesktop.org/doc/dbus-run-session.1.html).

ernobe
  • 11
  • 1
-5

Type in terminal:

export $DBUS_SESSION_BUS_ADDRESS
Jamal
  • 763
  • 7
  • 22
  • 32
Syfi Malik
  • 11
  • 5
  • 1
    If the variable is not set, exporting it will not solve anything, thus $(dbus-launch) solves this issue. – calexandru Feb 01 '21 at 14:34
  • 1
    This is worse than wrong as it will try to export a variable named after the contents of DBUS_SESSION_BUS_ADDRESS which makes no sense. – Arnaud Meuret Mar 17 '22 at 08:05