#! /bin/sh
#
# tty_console_getty
#   Health check program for the Linux Health Checker
#
# Copyright IBM Corp. 2012
# Author(s): Hendrik Brueckner <brueckner@linux.vnet.ibm.com>
#
# All rights reserved. This program and the accompanying materials
# are made available under the terms of the Eclipse Public License v1.0
# which accompanies this distribution, and is available at
# http://www.eclipse.org/legal/epl-v10.html
#
# Contributors: See file CONTRIBUTORS which is part of this package
#

# Exception IDs
LNXHC_EXCEPTION_GETTY_ON_CONSOLE="getty_on_console"


# Non-zero if health check program should output additional information
VERBOSE="$LNXHC_VERBOSE"

# Non-zero if health check program should output debugging information
DEBUG="$LNXHC_DEBUG"

# Health check ID
CHECK_ID="$LNXHC_CHECK_ID"

# Health check installation directory
CHECK_DIR="$LNXHC_CHECK_DIR"

# Path to the file used to report exceptions
EX_FILE="$LNXHC_EXCEPTION"

# Path to the file containing data for sysinfo item 'ps_aux'
SYSINFO_PS_TTY_CONSOLE="$LNXHC_SYSINFO_ps_tty_console"


#
# lnxhc_exception - report exception
# @ex_id: ID of the exception to report
#
lnxhc_exception()
{
	local EX_ID="$1"
	echo "$EX_ID" >> "$EX_FILE" || exit 1
}

#
# lnxhc_exception_var - report value of an exception template variable
# @var_id: ID of the exception template variable
# @value: value of the exception template variable
#
lnxhc_exception_var()
{
	local VAR_ID="$1"
	local VALUE="$2"
	echo "$VAR_ID=\"$VALUE\"" >> "$EX_FILE" || exit 1
}

# test if there is check data available
if ! test -r "$SYSINFO_PS_TTY_CONSOLE"; then
	echo "$CHECK_ID: Required check data is missing." 2>&1
	exit 1
fi

# check if there something running on /dev/console
if grep -q -E 'getty|login' $SYSINFO_PS_TTY_CONSOLE; then
	# raise exception
	lnxhc_exception "$LNXHC_EXCEPTION_GETTY_ON_CONSOLE"
fi

exit 0
