src/vlock.sh, src/vlock-current.c: even "Please press [ENTER]" message from environment
[vlock.git] / src / vlock.sh
blobdf454a218c7b180fa8a3ec39d82eb356b4fb5cfe
1 #!%BOURNE_SHELL%
3 # exit on error
4 set -e
6 # ignore some signals
7 trap : HUP INT QUIT TSTP
9 VLOCK_ALL=%PREFIX%/sbin/vlock-all
10 VLOCK_NEW=%PREFIX%/sbin/vlock-new
11 VLOCK_NOSYSRQ=%PREFIX%/sbin/vlock-nosysrq
12 VLOCK_CURRENT=%PREFIX%/sbin/vlock-current
13 VLOCK_VERSION=%VLOCK_VERSION%
15 print_help() {
16 echo >&2 "vlock: locks virtual consoles, saving your current session."
17 echo >&2 "Usage: vlock [options]"
18 echo >&2 " Where [options] are any of:"
19 echo >&2 "-c or --current: lock only this virtual console, allowing user to"
20 echo >&2 " switch to other virtual consoles."
21 echo >&2 "-a or --all: lock all virtual consoles by preventing other users"
22 echo >&2 " from switching virtual consoles."
23 echo >&2 "-n or --new: allocate a new virtual console before locking,"
24 echo >&2 " implies --all."
25 echo >&2 "-s or --disable-sysrq: disable sysrq while consoles are locked to"
26 echo >&2 " prevent killing vlock with SAK, requires --all."
27 echo >&2 "-v or --version: Print the version number of vlock and exit."
28 echo >&2 "-h or --help: Print this help message and exit."
29 exit $1
32 checked_exec() {
33 if [ -f "$1" ] && [ ! -x "$1" ] ; then
34 echo >&2 "vlock: cannot execute \`$1': Permission denied"
35 echo >&2 "Please check the documentation for more information."
36 exit 1
37 else
38 exec "$@"
42 main() {
43 local opts lock_all lock_new nosysrq
45 ( getopt -T >/dev/null )
47 if [ $? -eq 4 ] ; then
48 # gnu getopt
49 opts=`getopt -o acnsvh --long current,all,new,disable-sysrq,version,help \
50 -n vlock -- "$@"`
51 else
52 opts=`getopt acnsvh "$@"`
55 if [ $? -ne 0 ] ; then
56 print_help 1
59 eval set -- "$opts"
61 lock_all=0
62 lock_new=0
63 nosysrq=0
65 while : ; do
66 case "$1" in
67 -a|--all)
68 lock_all=1
69 shift
71 -c|--current)
72 lock_all=0
73 shift
75 -s|--disable-sysrq)
76 nosysrq=1
77 shift
79 -n|--new)
80 lock_new=1
81 lock_all=1
82 shift
84 -h|--help)
85 print_help 0
87 -v|--version)
88 echo "vlock version $VLOCK_VERSION" >&2
89 exit
91 --) shift ; break ;;
92 *)
93 echo "getopt error: $1" >&2
94 exit 1
96 esac
97 done
99 if [ $lock_new -ne 0 ] && [ -n "$DISPLAY" ] ; then
100 # work around an annoying X11 bug
101 sleep 1
104 if [ $lock_all -ne 0 ] ; then
105 : ${VLOCK_MESSAGE:="\
106 The entire console display is now completely locked.
107 You will not be able to switch to another virtual console.
109 Please press [ENTER] to unlock."}
110 export VLOCK_MESSAGE
112 if [ $nosysrq -ne 0 ] ; then
113 if [ $lock_new -ne 0 ] ; then
114 export VLOCK_NEW=1
115 else
116 unset VLOCK_NEW
119 checked_exec "$VLOCK_NOSYSRQ"
120 elif [ $lock_new -ne 0 ] ; then
121 checked_exec "$VLOCK_NEW"
122 else
123 checked_exec "$VLOCK_ALL"
125 else
126 : ${VLOCK_MESSAGE:="\
127 This TTY is now locked.
129 Please press [ENTER] to unlock."}
130 export VLOCK_MESSAGE
132 checked_exec "$VLOCK_CURRENT"
136 main "$@"