#!/bin/ksh -p
#
# ident "@(#)dtlogin-notify.sh	1.4 07/11/14"
#
# Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#
# Script to notify the dtlogin X Display Manager that it needs to rescan
# its configuration settings.  In the Sun Ray case it's typically the
# Xservers and/or Xconfig files that have been modified.
#

verbose=0
umask 0022
unset LD_LIBRARY_PATH

DMNAME=/usr/dt/bin/dtlogin
DMSIGNAL="-HUP"

# Get the stored master dtlogin PID, verify that it matches a dtlogin
# process whose PPID is 1 and UID is 0, and if so send that process a
# HUP.

PIDFILE="/var/dt/Xpid"
EXITVAL=1
# Work around a dtlogin bug by reading just the first line from its
# PID file.
read RAWPID < "$PIDFILE"
if [[ $? -ne 0 ]] ; then
    print -u2 "$0: failed to obtain master PID from $PIDFILE"
else
    typeset -i10 DMPID="$RAWPID"
    if [[ $? -ne 0 ]] ; then
	print -u2 "$0: master PID \"$RAWPID\" is non-numeric"
    elif [[ 0 -ge $DMPID ]] ; then
	print -u2 "$0: master PID \"$RAWPID\" ($DMPID) is out of range"
    else
	set -- $(/bin/ps -oppid= -ouid= -ocomm= -p "$DMPID")
	if [[ $? -ne 0 ]] ; then
	    print -u2 "$0: /bin/ps failed for PID $DMPID"
	elif [[ $1 -ne 1 ]] || [[ $2 -ne 0 ]] || [[ $3 != "$DMNAME" ]] ; then
	    print -u2 "$0: PID=$DMPID PPID=$1 UID=$2 COMM=$3, not master"
	else
	    kill "$DMSIGNAL" "$DMPID"
	    EXITVAL=$?
	fi
    fi
fi
exit "$EXITVAL"
