diff options
author | John Southworth <john.southworth@vyatta.com> | 2011-09-09 18:49:10 -0500 |
---|---|---|
committer | John Southworth <john.southworth@vyatta.com> | 2011-10-27 17:11:29 -0500 |
commit | e0f982d92f2aec9c6bc4fb7d1b712d69eff81b9e (patch) | |
tree | ee130a4b6ed22285693fa789a9faa3e447a75b04 | |
parent | 81fdd931fc26474c536173a5f89ecd35c351233e (diff) | |
download | vyatta-op-e0f982d92f2aec9c6bc4fb7d1b712d69eff81b9e.tar.gz vyatta-op-e0f982d92f2aec9c6bc4fb7d1b712d69eff81b9e.zip |
Cleanup background monitor sessions on logout
(cherry picked from commit 269c88bff20c49876e5780dd8bf379e3d1fe1601)
-rw-r--r-- | Makefile.am | 2 | ||||
-rw-r--r-- | etc/bash.bash_logout | 1 | ||||
-rw-r--r-- | scripts/vyatta-monitor-cleanup | 36 |
3 files changed, 39 insertions, 0 deletions
diff --git a/Makefile.am b/Makefile.am index c0b7428..9cfdfa8 100644 --- a/Makefile.am +++ b/Makefile.am @@ -68,3 +68,5 @@ install-exec-hook: cd templates; $(cpiop) $(DESTDIR)$(opdir) mkdir -p $(DESTDIR)$(etc_shell_leveldir) cd etc/shell/level; $(cpiop) $(DESTDIR)$(etc_shell_leveldir) + mkdir -p $(DESTDIR)/etc/ + cp etc/bash.bash_logout $(DESTDIR)/etc/ diff --git a/etc/bash.bash_logout b/etc/bash.bash_logout new file mode 100644 index 0000000..2f31f3c --- /dev/null +++ b/etc/bash.bash_logout @@ -0,0 +1 @@ +${vyatta_bindir}/vyatta-monitor-cleanup LOGOUT diff --git a/scripts/vyatta-monitor-cleanup b/scripts/vyatta-monitor-cleanup new file mode 100644 index 0000000..89eea93 --- /dev/null +++ b/scripts/vyatta-monitor-cleanup @@ -0,0 +1,36 @@ +#!/bin/bash +declare CURRENT_PTS=$(tty | sed -e s-/--g -e s/dev//g) +declare CURRENT_UID=$(id -u) +declare CURRENT_UNAME=$(id -un) +declare -a MPIDS +MODE=$1 +MPIDS=( $(ls /var/run/vyatta/monitor/*.pid) ) + +for pidfile in "${MPIDS[@]}"; do + pid=$(<$pidfile) + procname=$(cat /proc/$pid/cmdline 2>/dev/null) + if [[ "$procname" =~ "tail" ]] ; then + # Clean up tail processes that didn't die on logout + pts=${pidfile##*-pts} + pts=${pts%%.pid} + # If the process isn't attaced to the current PTY + if [[ "pts$pts" != "$CURRENT_PTS" ]]; then + # If I owned this process + if cat /proc/$pid/status | grep -q "Uid:.*$CURRENT_UID" ; then + # If I'm no longer on the pty that process started on + if ! who | grep -q "$CURRENT_UNAME.*pts/$pts" ; then + kill $pid + rm -rf $pidfile + fi + fi + elsif [[ "$MODE" == "LOGOUT" ]]; then + # on logout kill all monitors + kill $pid + rm -rf $pidfile + fi + else + # Clean up killed tail processes + rm -rf $pidfile + fi +done + |