aboutsummaryrefslogtreecommitdiff
path: root/bin/desktop-autoloader
blob: 5a3856cc1453eb7aa77b07abaf67f558576ff87d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#!/bin/bash

# Copyright (C) 2011-2017 Klaus Ade Johnstad <klaus@linuxavdelingen.no>
# Copyright (C) 2017 Mike Gabriel <mike.gabriel@das-netzwerkteam.de>

# This program is free software; you can redistribute it
# and/or modify it under the terms of the GNU General Public
# License as published by the Free Software Foundation; either
# version 2 of the License, or (at your option) any later
# version.
#
# This program is distributed in the hope that it will be
# useful, but WITHOUT ANY WARRANTY; without even the implied
# warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
# PURPOSE.  See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public
# License along with this package; if not, write to the Free
# Software Foundation, Inc., 51 Franklin St, Fifth Floor,
# Boston, MA  02110-1301 USA

DESKTOP_AUTOLOADER_ENABLE=false
REQUIRED_MEMORY_THRESHOLD=2048
AUTOLOAD_DISPLAY=":20"
DESKTOP_SESSION_MANAGER="x-session-manager"

DEBUG_X11VNC_ENABLE=false
DEBUG_X11VNC_RFBPORT=5910

test -f /etc/default/desktop-autoloader && . /etc/default/desktop-autoloader

function is_true() {

	echo $1 | egrep "(y|Y|yes|YES|Yes|true|TRUE|True|On|ON|on)" 1>/dev/null
	return $?

}

if ! is_true "$DESKTOP_AUTOLOADER_ENABLE"; then
	echo "Desktop Autoloader is not enabled, enable it in /etc/default/desktop-autoloader"
	exit 0
fi

AVAILABLE_SYSTEM_MEMORY=$(free -m  | grep Mem | awk '{print $2}')

if [ ! "${AVAILABLE_SYSTEM_MEMORY}" -gt "${REQUIRED_MEMORY_THRESHOLD}" ]; then
	echo "Desktop Autoloader is configured to only run on systems with more than ${REQUIRED_MEMORY_THRESHOLD} MiB of RAM."
	echo "This system only has ${AVAILABLE_SYSTEM_MEMORY} MiB."
	exit 0
fi

# clean up .xsession-errors for a new launch (in case this tool is used
# on a non-diskless machine...
if [ "$(id -un)" == "desktop-autoloader" ]; then
	rm -f ~/.xsession-errors
fi

###
### input sanitizing...
###
if ! echo "${AUTOLOAD_DISPLAY}" | grep -qE "^.*:[0-9]+(|\.[0-9]+)"; then
	echo "Inappropriate setting for \$AUTOLOAD_DISPLAY variable: ${AUTOLOAD_DISPLAY}"
	exit 1
fi
if ! type -p "${DESKTOP_SESSION_MANAGER}" 1>/dev/null; then
	echo "Cannot find session manager \$DESKTOP_SESSION_MANAGER: ${DESKTOP_SESSION_MANAGER}"
	exit 1
fi

# launch an invisible X-Server
Xvfb ${AUTOLOAD_DISPLAY} -screen 0 1024x768x16 -cc 4 -nolisten tcp &
XVFB_PID=$?

# launch a session inside this Xserver
export DISPLAY="${AUTOLOAD_DISPLAY}"

# attach an x11vnc instance to the DISPLAY, if debugging is enabled
if type -p x11vnc 1>/dev/null && is_true "${DEBUG_X11VNC_ENABLE}"; then

	sleep 1
	x11vnc -rfbport "${DEBUG_X11VNC_RFBPORT}" -forever -localhost &
	X11VNC_PID=$?

fi

STARTUP="${DESKTOP_SESSION_MANAGER}" dbus-run-session /etc/X11/Xsession

kill $X11VNC_PID
kill $XVFB_PID