While working on the LoRaWAN gateway project I came to requirement where I needed to start dbus user session so that various system services could talk to each other over the dbus. I could use system wide dbus session but I decided to use user session to make debugging easier.
The dbus user session will be started as a SystemD service before other services that use dbus. To start a user session dbus-launch command is used and it is writing process id and session address to the output as key/value pairs. Format of the output is:
DBUS_SESSION_BUS_ADDRESS=unix:abstract=/tmp/dbus-teW5DiLRE0,guid=0a67ec3093886a111bc509a65cc45278
DBUS_SESSION_BUS_PID=295
More details here: https://dbus.freedesktop.org/doc/dbus-launch.1.html
Example Shell script to start dbus user session
#!/bin/sh
echo "Starting Dbus user session"
GATEWAY_RUN_DIR="/var/run/gateway"
GATEWAY_DBUS_SESSION_FILE="$GATEWAY_RUN_DIR/gateway-dbus-session"
GATEWAY_DBUS_PID_FILE="$GATEWAY_RUN_DIR/gateway-dbus-session.pid"
# If doesesnt exist create a directory for the gateway files
mkdir -p "$GATEWAY_RUN_DIR"
dbus-launch --sh-syntax > "$GATEWAY_DBUS_SESSION_FILE"
# dbus-launch outputs key value pairs, so it can be sourced to read back variables to obtain process pid
# Example output from the dbus-launch:
# DBUS_SESSION_BUS_ADDRESS=unix:abstract=/tmp/dbus-teW5DiLRE0,guid=0a67ec3093886a111bc509a65cc45278
# DBUS_SESSION_BUS_PID=295
source "$GATEWAY_DBUS_SESSION_FILE"
echo "Dbus session started:"
echo -e "\tSession file: $GATEWAY_DBUS_SESSION_FILE"
echo -e "\tPID file: $GATEWAY_DBUS_PID_FILE"
echo -e "\tDbus session socket: $DBUS_SESSION_BUS_ADDRESS"
echo -e "\tProcess PID: $DBUS_SESSION_BUS_PID"
# Write PID to pid file
echo "$DBUS_SESSION_BUS_PID" > "$GATEWAY_DBUS_PID_FILE"
Starting Dbus user session as SystemD service
[Unit]
Description=Dbus User Session Service
After=dbus.service
[Service]
Type=forking
PIDFile=/var/run/gateway/gateway-dbus-session.pid
ExecStart=/usr/bin/gw-dbus-user-session.sh
[Install]
WantedBy=multi-user.target
PID file permision problems on SystemD 237
When trying to pass a PID file to the SystemD it will complain about user permissions on the PID file.
Apr 27 14:09:22 imx6ul-var-dart systemd[1]: gw-dbus-user-session.service: Permission denied while opening PID file or unsafe symlink chain: /var/run/gateway/gateway-dbus-session.pid
This seems to be because of the stricter rules of the ownership in the /run folder when SystemD version 237 is used.
Discussion and patch for this problem can be found here:
Issue: http:// https://github.com/systemd/systemd/issues/8085
Patch: https://github.com/Werkov/systemd/commit/811cc88c10cfe23b6f98d7f0b8332003da38412b
Yocto recipe for the SystemD user session daemon
SUMMARY = "Gateway DBus user session daemon"
DESCRIPTION = "Recipe for the dbus user session daemon"
LICENSE = "MIT"
LIC_FILES_CHKSUM = "file://${COMMON_LICENSE_DIR}/MIT;md5=0835ade698e0bcf8506ecda2f7b4f302"
inherit systemd
SRC_URI = "file://."
S = "${WORKDIR}"
RDEPENDS_${PN} += "bash"
FILES_${PN} += " ${bindir}/gw-dbus-user-session.sh \
${systemd_unitdir}/system/gw-dbus-user-session.service \
"
SYSTEMD_SERVICE_${PN} = "gw-dbus-user-session.service"
do_install() {
install -d ${D}${bindir}
install -m 755 ${S}/gw-dbus-user-session.sh ${D}${bindir}
install -d ${D}${systemd_unitdir}/system
install -m 755 ${S}/gw-dbus-user-session.service ${D}${systemd_unitdir}/system
}