#!/bin/sh
# Reassign ownership of the console to root, this should disallow
# assignment of console output to any random users's xterm
# $XConsortium: TakeConsole,v 1.2 93/09/28 14:30:29 gildea Exp $
# $Id: TakeConsole,v 1.3 1998/09/30 21:20:26 bieker Exp $


# This list _MUST_ be the same as in GiveConsole
CONSOLE_DEVICES="console audio video radio cdrom dsp mixer vbi sequencer sequencer2 midi"

for i in $CONSOLE_DEVICES; do
	chown --dereference root.root /dev/$i
	chmod 600 /dev/$i
done
chmod 622 /dev/console

# kill all processes from the user that still have opened
# one of these devices

# NOTE: If the user is root, don't kill any processes
if [ $USER = "root" ]; then
  exit
fi


OFS=$IFS
for i in $CONSOLE_DEVICES; do
	fuser -u /dev/$i
done | while read line; do
	IFS="$OFS:()"
	set -- $line
	IFS=$OFS
	dev=$1
	shift
	while [ $# -gt 1 ]; do
		pid=$1
		user=$2
		shift 2
		if [ $user = $USER ]; then
			kill -9 $pid
		fi
	done
done
