Skip to content

Instantly share code, notes, and snippets.

@Low-power
Last active July 7, 2020 06:12
Show Gist options
  • Save Low-power/d778e890b47c2e6c359d12c25101596f to your computer and use it in GitHub Desktop.
Save Low-power/d778e890b47c2e6c359d12c25101596f to your computer and use it in GitHub Desktop.
Ctrl-Alt-Del warning for systemd.
#!/bin/sh
# Ctrl-Alt-Del warning for systemd.
# Override default CAD action of rebooting system.
# Copyright 2015-2020 Rivoreo
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
if [ ! -d /etc/systemd/system ]; then
echo "systemd system unit directory not found" 1>&2
exit 1
fi
set -e
if [ "`cat /proc/sys/kernel/ctrl-alt-del`" != 0 ]; then
echo "Setting kernel Ctrl-Alt-Del handling" 1>&2
echo 0 > /proc/sys/kernel/ctrl-alt-del
echo "kernel.ctrl-alt-del=0" >> /etc/sysctl.conf
fi
rm -f /etc/systemd/system/ctrl-alt-del.target
cat > /etc/systemd/system/ctrl-alt-del.target << EOF
# This file is part of systemd.
#
# systemd is free software; you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 2.1 of the License, or
# (at your option) any later version.
[Unit]
Description=Ctrl-Alt-Del
Documentation=man:systemd.special(7)
DefaultDependencies=no
Requires=ctrl-alt-del-warning.service
After=ctrl-alt-del-warning.service
AllowIsolate=yes
EOF
chmod 644 /etc/systemd/system/ctrl-alt-del.target
cp -p /etc/systemd/system/ctrl-alt-del.target /etc/systemd/system/ctrl-alt-del.target.backup
ln -f /etc/systemd/system/ctrl-alt-del.target /etc/systemd/system/ctrl-alt-del.target.link
cat > /etc/systemd/system/ctrl-alt-del-warning.service << EOF
[Unit]
Description=Show a BSOD like warning when Ctrl-Alt-Del is pressed
ConditionFileIsExecutable=/usr/sbin/ctrl-alt-del-warning
[Service]
Type=oneshot
ExecStart=/usr/sbin/ctrl-alt-del-warning start
ExecStart=/bin/false
TimeoutSec=0
RemainAfterExit=yes
GuessMainPID=no
Restart=no
KillMode=none
EOF
cat > /usr/sbin/ctrl-alt-del-warning << EOF
#!/bin/bash
set +H > /dev/null 2>&1
exec 2>> /tmp/cad
if [ "\$1" = start ]; then
exec "\$0" & exit 0
fi
nested=0
if [ -f /var/run/ctrl-alt-del-warning.pid ]; then
another_pid="\`cat /var/run/ctrl-alt-del-warning.pid\`"
if [ -n "\$another_pid" ] && [ -d "/proc/\$another_pid" ]; then
nested=1
fi
fi
[ \$nested = 0 ] && echo \$\$ > /var/run/ctrl-alt-del-warning.pid
#exec < /dev/tty0 > /dev/tty0
orig_tty=\`cat /sys/devices/virtual/tty/tty0/active\`
chvt 8
exec < /dev/tty8 > /dev/tty8
#read
#tty >> /tmp/cad
printf '\\033[?47h\\033[1;1H\\033[7\\033[?25l\\033[2K\\033[1m\\033[44m\\033[H\\033[J
*** WARNING: Ctrl-Alt-Del is no longer supported on this system.\\n
Press any key to return.\\n'
if [ \$nested = 1 ]; then
echo "Stop pressing Ctrl-Alt-Del!"
exit
fi
#read -n 1 < /dev/tty0
read -n 1 < /dev/tty8
printf '\\033[?47l\\033[?25h'
[ "\$TERM" = screen -o "\$TERM" = linux ] && printf '\\033[0m\\033[H\\033[J'
chvt "\${orig_tty#tty}"
[ \$nested = 0 ] && unlink /var/run/ctrl-alt-del-warning.pid
EOF
chmod 755 /usr/sbin/ctrl-alt-del-warning
if [ -d /lib/systemd/system ]; then
rm -f /lib/systemd/system/ctrl-alt-del.target
cp -p /etc/systemd/system/ctrl-alt-del.target /lib/systemd/system/ || true
fi
if [ "$1" = --full ]; then
if grep -Eq "^[[:space:]]*CtrlAltDelBurstAction[[:space:]]*=" /etc/systemd/system.conf; then
sed -i -r 's/(^[[:space:]]*CtrlAltDelBurstAction)[[:space:]]*=.+/\1=none' /etc/systemd/system.conf
elif grep -Eq '^#CtrlAltDelBurstAction=reboot-force$' /etc/systemd/system.conf; then
sed -i -r '/^#CtrlAltDelBurstAction=reboot-force$/aCtrlAltDelBurstAction=none' /etc/systemd/system.conf
else
echo "CtrlAltDelBurstAction=none" >> /etc/systemd/system.conf
fi
fi
systemctl daemon-reload
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment