]> ruderich.org/simon Gitweb - config/dotfiles.git/blob - shell/rc
shell/rc,tmux.conf: Auto-logout on TTYs after timeout.
[config/dotfiles.git] / shell / rc
1 # Shell configuration file.
2
3 # Copyright (C) 2013  Simon Ruderich
4 #
5 # This file is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # This file is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this file.  If not, see <http://www.gnu.org/licenses/>.
17
18
19 # Normal umask for root (readable by all users).
20 if test "`id -u`" -eq 0; then
21     umask 022
22 # Restricted umask for normal users (readable only by me).
23 else
24     umask 077
25 fi
26
27 # Disable flow control (^s ^q). I use GNU Screen/Tmux which also supports this
28 # in a similar way (entering copy mode) and enabling flow control by accident
29 # is annoying.
30 if test -t 0; then
31     stty -ixon -ixoff
32 fi
33
34 # Auto-logout after timeout on TTYs. It's easy to forget to logout after
35 # switching back to X11 from a TTY.
36 timeout_setup_screen_lock() {
37     echo "Locking GNU screen after $timeout seconds (TTY detected)."
38     screen -X idle $timeout lockscreen
39     echo
40 }
41 timeout_setup_tmux_lock() {
42     echo "Locking Tmux after $timeout seconds (TTY detected)."
43     tmux set-option lock-after-time $timeout
44     echo
45 }
46 timeout_setup_shell_lock() {
47     echo "Auto-logout after $timeout seconds (TTY detected)."
48     TMOUT=$timeout
49     echo
50 }
51 # Timeout in seconds.
52 local timeout >/dev/null 2>&1
53 timeout=600
54 case "$TERM" in
55     # `tty` doesn't work inside a pseudo-terminal as provided by GNU
56     # Screen/Tmux.
57     #
58     # The following settings for GNU Screen/Tmux will only auto-lock new
59     # sessions, not when attaching to existing sessions!
60     screen*)
61         if test -n "$STY"; then
62             case "$STY" in
63                 *.tty[0-9]*.*)
64                     timeout_setup_screen_lock
65                     ;;
66             esac
67         elif test -n "$TMUX"; then
68             if ! type vlock >/dev/null; then
69                 echo "vlock not found, locking won't work in Tmux!" >&2
70             fi
71
72             case "`tmux display -p '#{client_tty}'`" in
73                 /dev/tty[0-9]*)
74                     timeout_setup_tmux_lock
75                     ;;
76                 /dev/*)
77                     # Not on TTY, nothing to do.
78                     ;;
79                 *)
80                     echo 'Tmux < 1.8 found, TTY check might be incorrect!' >&2
81
82                     local client >/dev/null 2>&1
83                     client=`printf '%s' "$TMUX" | sed 's/^.*,\([0-9]*\)$/\1/'`
84
85                     case "`tmux list-clients \
86                             | grep -E "^/dev/.*?: $client "`" in
87                         /dev/tty[0-9]*)
88                             timeout_setup_tmux_lock
89                             ;;
90                     esac
91                     ;;
92             esac
93         else
94             echo 'TERM=screen but neither $STY nor $TMUX found!' >&2
95             timeout_setup_shell_lock
96         fi
97         ;;
98
99     *)
100         case `tty` in
101             /dev/tty[0-9]*)
102                 timeout_setup_shell_lock
103                 ;;
104         esac
105         ;;
106 esac
107 unset timeout
108 unset timeout_setup_screen_lock
109 unset timeout_setup_tmux_lock
110 unset timeout_setup_screen_lock
111
112 # vim: ft=sh