Skip to content

Commit

Permalink
Merge pull request #9 from hedayat/master
Browse files Browse the repository at this point in the history
Fix session startup with new systemd, using [email protected] mechanism
  • Loading branch information
locusf committed Oct 26, 2014
2 parents a1e896e + 9369480 commit 6aba852
Show file tree
Hide file tree
Showing 10 changed files with 119 additions and 231 deletions.
43 changes: 29 additions & 14 deletions bin/killx → bin/set-boot-state
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
#!/bin/sh
#
# Copyright (c) 2014, Hedayat Vatankhah <[email protected]>.
#
# Sets the current boot state in /run/systemd/boot-status (this part
# comes from old start-user-session with the following copyright notice),
# and sets default user's target according to the boot state.
#

#
# Contact: Pekka Lundstrom <[email protected]>
#
# Copyright (c) 2013, Jolla Ltd.
Expand Down Expand Up @@ -26,18 +35,24 @@
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

# This will kill Xorg if it is running
[ -z "$1" ] && echo "Boot mode missing" && exit 1
MODE=$1

# First find out the user id we use
DEF_UID=$(grep "^UID_MIN" /etc/login.defs | tr -s " " | cut -d " " -f2)

echo "Configuring boot state to $MODE"

# Save the mode we are going to run
echo "BOOTSTATE=$MODE" > /run/systemd/boot-status/bootstate
rm -f /run/systemd/boot-status/USER
rm -f /run/systemd/boot-status/ACT_DEAD
touch /run/systemd/boot-status/$MODE

XPID_FILE=/tmp/.X0-lock
[ ! -f $XPID_FILE ] && exit 0
ps ax | grep -v grep | grep Xorg || exit 0
xpid=`cat $XPID_FILE`
echo "Found X running ($xpid). Killing it"
kill -s TERM $xpid
sleep 1
[ ! -f $XPID_FILE ] && echo "X killing job well done" && exit 0
echo "X is still running. Using brute kill"
kill -s KILL $xpid
sleep 1
[ ! -f $XPID_FILE ] && echo "Brute kill got job done" && exit 0
echo "X still running, I give up"
if [[ $MODE == USER ]]; then
ln -sf /usr/lib/systemd/user/post-user-session.target \
/etc/systemd/user/${DEF_UID}.target
else
ln -sf /usr/lib/systemd/user/actdead-session.target \
/etc/systemd/user/${DEF_UID}.target
fi
83 changes: 21 additions & 62 deletions bin/start-user-session
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,14 @@
# Contact: Pekka Lundstrom <[email protected]>
#
# Copyright (c) 2013, Jolla Ltd.
# Copyright (c) 2014, Hedayat Vatankhah <[email protected]>
# All rights reserved.
#
# Updates:
# Hedayat Vatankhah <[email protected]>:
# * Modified to be compatible with new systemd. It is now run as default
# user rather than root, and is used as the users's session manager.
# Session restarts are now managed by systemd itself.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
Expand Down Expand Up @@ -31,57 +38,18 @@
# This starts user session either in ACT_DEAD or USER mode
# This also keeps eye on need for user session restart

wait_old_session_to_stop() {
# Make sure old session is stopped first. max wait in $1
local i=0
local max_wait=$1
while [ $i -lt $max_wait ]; do
OLDS=$(systemctl is-active $USER_SERVICE)
if [ "$OLDS" == "inactive" -o "$OLDS" == "unknown" ]; then
return 0
fi
echo "($i) waiting old session $OLDS"
sleep 1
let i=$i+1
done
echo "Old session did not stop within $max_wait seconds"
return 1
}

kill_old_session() {
echo "Killing old session"
systemctl --no-block stop $USER_SERVICE
wait_old_session_to_stop 5
# Kill all user session stuff
killall -u $DEF_UNAME
sleep 2
# Make sure X was stopped also
/usr/lib/startup/killx
sleep 2
OLDS=$(systemctl is-active $USER_SERVICE)
if [ "$OLDS" == "inactive" -o "$OLDS" == "unknown" ]; then
echo "Old session killed"
return 0
else
echo "Failed to kill old session"
sleep 2
return 1
fi
}

wait_user_service_to_start() {
# This waits untill user-session has started
# We also check need for restart

sleep 2 # Wait a little to make sure that [email protected] is started
local ST=""
while [ 1 ]; do
if [ -f $RESTART_FILE ]; then
# User session needs to be restarted
echo "Going to restart user session $DEF_UID"
rm -f $RESTART_FILE
kill_old_session
systemctl start --no-block $USER_SERVICE
sleep 1
return 1
fi
ST=$(systemctl is-active $USER_SERVICE)
if [ "$ST" == "active" ]; then
Expand All @@ -97,35 +65,26 @@ wait_user_service_to_start() {
}


# ------------------ main() -----------------------

[ -z "$1" ] && echo "Boot mode missing" && exit 1
MODE=$1
# First find out the user id we use
DEF_UID=$(grep "^UID_MIN" /etc/login.defs | tr -s " " | cut -d " " -f2)
DEF_UNAME=$(getent passwd $DEF_UID | cut -d ":" -f1)
USER_SERVICE=user-session\@${DEF_UID}.service

wait_old_session_to_stop 10
if [ $? -ne 0 ]; then
echo "Problem, must kill old session"
kill_old_session
fi

# Save the mode we are going to run
echo "BOOTSTATE=$MODE" > /run/systemd/boot-status/bootstate
rm -f /run/systemd/boot-status/USER
rm -f /run/systemd/boot-status/ACT_DEAD
touch /run/systemd/boot-status/$MODE
USER_SERVICE=user\@${DEF_UID}.service

echo "Starting new session in $MODE mode"
systemctl start --no-block $USER_SERVICE
echo "Starting new session"
wait_user_service_to_start
st=$?
if [ $st -eq 0 ]; then
echo "New session $MODE started"
echo "New session started"
# We are the session manager, if we exit, the session is terminated!
while sleep 100000d; do
true
done
# loop will be terminated if 'sleep' exits abnormaly, e.g. service stopped
echo "User session terminated"
else
echo "Starting new session in $MODE failed"
echo "Starting new session in failed"
fi

# Will be restarted by systemd. To shutdown, user service must be stopped using systemctl
exit $st

65 changes: 0 additions & 65 deletions oneshot/correct-users

This file was deleted.

58 changes: 19 additions & 39 deletions rpm/nemo-mobile-session.spec
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Group: System/Libraries
License: Public Domain
URL: https://github.com/nemomobile/nemo-mobile-session
Source0: %{name}-%{version}.tar.gz
BuildArch: noarch

%description
Target for nemo systemd user session
Expand All @@ -18,26 +19,11 @@ Requires: systemd-user-session-targets
Obsoletes: uxlaunch
# mer release 0.20130605.1 changed login.defs
Requires: setup >= 2.8.56-1.1.33
BuildRequires: oneshot
Requires: oneshot
%{_oneshot_requires_post}
Requires(post): /bin/chgrp, /usr/sbin/groupmod

%description common
%{summary}

%package xorg
Summary: Xorg configs for nemo-mobile-session
Group: Configs
Requires: xorg-launch-helper
Requires: nemo-mobile-session-common
Provides: nemo-mobile-session > 21
Obsoletes: nemo-mobile-session <= 21
Conflicts: nemo-mobile-session-wayland

%description xorg
%{summary}

%package wayland
Summary: Wayland configs for nemo-mobile-session
Group: Configs
Expand All @@ -59,8 +45,10 @@ mkdir -p %{buildroot}%{_sysconfdir}/systemd/system/
mkdir -p %{buildroot}%{_libdir}/systemd/user/pre-user-session.target.wants/

# Root services
install -m 0644 services/[email protected] %{buildroot}/lib/systemd/system/
install -m 0644 services/[email protected] %{buildroot}/lib/systemd/system/
install -D -m 0644 services/[email protected]/nemo.conf \
%{buildroot}/lib/systemd/system/[email protected]/nemo.conf
install -m 0644 services/[email protected] %{buildroot}/lib/systemd/system/
install -m 0644 services/start-user-session.service %{buildroot}/lib/systemd/system/
install -m 0644 services/init-done.service %{buildroot}/lib/systemd/system/

# conf
Expand All @@ -69,23 +57,22 @@ install -D -m 0644 conf/nemo-session-tmp.conf %{buildroot}%{_libdir}/tmpfiles.d/
install -m 0644 conf/50-nemo-mobile-wayland.conf %{buildroot}/var/lib/environment/nemo/

# bin
install -D -m 0744 bin/start-user-session %{buildroot}%{_libdir}/startup/start-user-session
install -D -m 0744 bin/set-boot-state %{buildroot}%{_libdir}/startup/set-boot-state
install -D -m 0755 bin/start-user-session %{buildroot}%{_libdir}/startup/start-user-session
install -D -m 0744 bin/init-done %{buildroot}/%{_libdir}/startup/init-done
install -D -m 0744 bin/killx %{buildroot}/%{_libdir}/startup/killx

ln -sf ../[email protected] %{buildroot}/lib/systemd/system/graphical.target.wants/[email protected]
ln -sf ../[email protected] %{buildroot}/lib/systemd/system/graphical.target.wants/[email protected]
ln -sf ../start-user-session.service %{buildroot}/lib/systemd/system/graphical.target.wants/start-user-session.service
ln -sf ../init-done.service %{buildroot}/lib/systemd/system/graphical.target.wants/
# In nemo actdead is not (yet) supported. We define actdead (runlevel4) to poweroff
ln -sf /lib/systemd/system/poweroff.target %{buildroot}%{_sysconfdir}/systemd/system/runlevel4.target

# nemo-mobile-session dependencies
ln -sf post-user-session.target %{buildroot}%{_libdir}/systemd/user/default.target
ln -sf ../xorg.target %{buildroot}%{_libdir}/systemd/user/pre-user-session.target.wants/

# login.defs
mkdir -p %{buildroot}/%{_oneshotdir}
install -D -m 755 oneshot/correct-users %{buildroot}/%{_oneshotdir}

# systemd --user is called with '--unit=%I.target' in nemo.conf,
# so default.target is never used. User target is setup at runtime
# by set-boot-state according to the current boot state
#ln -sf post-user-session.target %{buildroot}%{_libdir}/systemd/user/default.target

%post
if [ $1 -gt 1 ] ; then
Expand All @@ -104,30 +91,23 @@ if [ $1 -gt 1 ] ; then
[ ! -f %{_sharedstatedir}/misc/passwd.old ] && cp %{_sysconfdir}/passwd %{_sharedstatedir}/misc/passwd.old
[ ! -f %{_sharedstatedir}/misc/group.old ] && cp %{_sysconfdir}/group %{_sharedstatedir}/misc/group.old

# device specific changes
%{_bindir}/add-oneshot correct-users

fi

%files common
%defattr(-,root,root,-)
%config /var/lib/environment/nemo/50-nemo-mobile-ui.conf
%{_libdir}/tmpfiles.d/nemo-session-tmp.conf
%{_libdir}/systemd/user/default.target
/lib/systemd/system/graphical.target.wants/start-user-session@USER.service
/lib/systemd/system/graphical.target.wants/[email protected]
/lib/systemd/system/graphical.target.wants/start-user-session.service
/lib/systemd/system/graphical.target.wants/init-done.service
/lib/systemd/system/[email protected]
/lib/systemd/system/[email protected]
/lib/systemd/system/[email protected]/*
/lib/systemd/system/[email protected]
/lib/systemd/system/start-user-session.service
/lib/systemd/system/init-done.service
%{_libdir}/startup/start-user-session
%{_libdir}/startup/set-boot-state
%{_libdir}/startup/init-done
%{_libdir}/startup/killx
%{_sysconfdir}/systemd/system/runlevel4.target
%{_oneshotdir}/correct-users

%files xorg
%defattr(-,root,root,-)
%{_libdir}/systemd/user/pre-user-session.target.wants/xorg.target

%files wayland
%defattr(-,root,root,-)
Expand Down
2 changes: 1 addition & 1 deletion services/init-done.service
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

[Unit]
Description=Indicate boot is done
After=start-user-session@USER.service
After=start-user-session.service

[Service]
Type=oneshot
Expand Down
13 changes: 13 additions & 0 deletions services/[email protected]
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#
# Copyright (c) 2014, Hedayat Vatankhah <[email protected]>.
#
# Sets the boot state to USER or ACT_DEAD
# Mode is selected with %I

[Unit]
Description=Set Boot state to %I
Before=start-user-session.service

[Service]
Type=oneshot
ExecStart=/usr/lib/startup/set-boot-state %I
Loading

0 comments on commit 6aba852

Please sign in to comment.