Integration of dbus and KDE: starting and stopping the session part of dbus with KDM.
Integration of dbus and KDE: starting and stopping the session part of dbus with KDM.Contents
0. Introduction 0. IntroductionSince some time now a lot of applications make use of D-BUS. This is the case with KDE 3.5, the current stable release of KDE. With the upcomming KDE 4, D-BUS is getting more important, replacing DCOP. In this howto I want to describe a way to start and stop the user and session dependent part of dbus. The major goals I have in mind with this approach are: - make the environmentvariables important for d-bus aware applications available; - make sure the dbus sessionpart is not started more than once for a single user; - make sure the dbus sessionpart is stopped when a session of a user ends; I assume the user logs in with KDM, the loginmanager for KDE 3.5. The construction could very well used with other loginmanagers (XDM, GDM) as well. KDM has the ability to run scripts at the beginning of a session (at startup), and at the end (reset). One of them can start (and stop) the dbus session part. 1. KDM: the files
-- snip -- for script in /etc/session.d/kdm/startup/*.sh; do if [ -x $script ]; then eval $script $USER kdm fi; done; and the code to the Xreset file: -- snip -- for script in /etc/session.d/kdm/reset/*.sh; do if [ -x $script ]; then eval $script $USER kdm fi; done;
Create the directories where the scripts go:
install -m755 -d /etc/session.d/kdm/startup
1. Starting the sessiondaemon of dbus and make the environmentvariables available
The dbus package is split up in two parts: one systemwide part and one
for (each) session/user. The systemwide part (a daemon) is started at boottime,
with special privileges of a dedicated user. The sessionwide part (also a daemon)
has to started when a session for a user begins, and stopped when the session ends. 1.1 About the startupfiles of Bash
The sessiondaemon of dbus creates a file which contains the environmentvariables.
As stated above this file should be read (sourced) by bash when it starts for this user.
dbus-launch --auto-syntax
cat >> /etc/profile.d/dbus-session.sh << "EOF"
if [ -f $HOME/.dbus-session ]; then
. $HOME/.dbus-session
fi;
EOF
This way, the environment variables are made available when Bash starts. 1.2 Starting the sessionbus part of dbus
I assume that dbus is installed, and that it is started at boottime.
cd /etc/session.d/scripts/start cat >> dbus-session-start.sh << "EOF"
#!/bin/bash
retcode=0;
userid=$1
userproperties=$(getent passwd | grep -m 1 -E "^$userid")
homedir=$(echo $userproperties | cut -d ":" -f 6);
gidnr=$(echo $userproperties | cut -d ":" -f 4);
uidnr=$(echo $userproperties | cut -d ":" -f 3);
if [ -d $homedir ]; then
#
# do a check whether dbus-daemon is already running
# dbus-daemon needs to be started by the user (uidnr) logging in
#
if [ -f $homedir/.dbus-session ]; then
# do a check the dbus-daemon for this user is running with the pid
# in the .dbus-session file
# pid according to the ps command
ps_dbus_session_pid=$(ps aux | grep -m 1 -E "^$userid.*dbus-daemon.*session.*" \
| grep -v "grep" | sed 's@[[:space:]][[:space:]]*@ @g' | cut -d " " -f 2)
# read the pid from the .dbus-session file
. $homedir/.dbus-session
# check they are the same
if [ -z "$ps_dbus_session_pid" ]; then
# dbus for this user not running
rm $homedir/.dbus-session
elif [ $DBUS_SESSION_BUS_PID -ne $ps_dbus_session_pid ]; then
# there is something wrong: stop dbus-daemon for this user
# and remove .dbus-session file
if [ $(id -u) -eq 0 ]; then
sudo -H -u $userid sh -c "kill $ps_dbus_session_pid"
elif [ $(id -u) -eq $uidnr ]; then
kill -SIGTERM $ps_dbus_session_pid;
fi
rm $homedir/.dbus-session
fi
fi
if [ ! -f $homedir/.dbus-session ]; then
# only start a dbus session if .dbus-session file it not found
# in users homedirectory
if [ $(id -u) -eq 0 ]; then
sudo -u $userid -H /bin/sh -c "dbus-launch --auto-syntax > $homedir/.dbus-session"
retcode=$?
chown $uidnr:$gidnr $homedir/.dbus-session
elif [ $(id -u) -eq $uidnr ]; then
dbus-launch --auto-syntax > $homedir/.dbus-session
retcode=$?
fi
fi
fi;
if [ $retcode -ne 0 ]; then
echo "An error with dbus ($retcode)."
fi;
exit $retcode
EOF
2.3 Stopping the sessionbus part of dbusCreating the dbus-session-stop.sh script in the reset directory: cd /etc/session.d/scripts/stop cat >> dbus-session-stop.sh << "EOF"
#!/bin/bash
retcode=0;
userid=$1
userproperties=$(getent passwd | grep -m 1 -E "^$userid")
homedir=$(echo $userproperties | cut -d ":" -f 6);
gidnr=$(echo $userproperties | cut -d ":" -f 4);
uidnr=$(echo $userproperties | cut -d ":" -f 3);
if [ -f $homedir/.dbus-session ]; then
. $homedir/.dbus-session
if [ -n "$DBUS_SESSION_BUS_PID" ]; then
if [ $(id -u) -eq 0 ]; then
sudo -u $userid -H /bin/sh -c "kill $DBUS_SESSION_BUS_PID"
retcode=$?
rm $homedir/.dbus-session
elif [ $(id -u) -eq $uidnr ]; then
kill $DBUS_SESSION_BUS_PID
retcode=$?
rm $homedir/.dbus-session
fi
fi
fi;
if [ $retcode -ne 0 ]; then
echo "An error with dbus ($retcode)."
fi;
exit $retcode
EOF
2.4 One user is more than one time logged in
For most of the situations this construction is good enough. Most users
have one session at a time. Now what happens when a user has more than
one sessions at the same time? Is it nessacary to start the sessionpart
of dbus for every session, or is one instance sufficient? CHANGELOG:
[2006-01-18]
|



Recent comments
9 hours 19 min ago
14 hours 18 min ago
15 hours 45 min ago
16 hours 38 min ago
18 hours 21 min ago
22 hours 44 min ago
23 hours 36 min ago
1 day 1 hour ago
1 day 14 hours ago
1 day 16 hours ago