3 # Common functions for PLC startup/shutdown scripts
5 # Mark Huang <mlhuang@cs.princeton.edu>
6 # Copyright (C) 2006 The Trustees of Princeton University
8 # $Id: functions,v 1.4 2006/04/25 21:18:19 mlhuang Exp $
11 export PATH=/sbin:/bin:/usr/bin:/usr/sbin
13 # Source function library
14 . /etc/init.d/functions
16 # Total number of errors
19 # Count the exit status of the last command
22 ERRORS=$(($ERRORS+$?))
28 echo -n "PLC: $*: " >&3
34 if [ $ERRORS -eq 0 ] ; then
42 # Start up a program with a plc_ prefix
47 # See if it's already running. Look *only* at the pid file.
48 if [ -f /var/run/plc_${base}.pid ]; then
50 read line < /var/run/plc_${base}.pid
52 [ -z "${p//[0-9]/}" -a -d "/proc/$p" ] && pid="$pid $p"
56 [ -n "${pid:-}" -a -z "${force:-}" ] && return
59 (exec -a plc_${base} $*)
62 if [ -f /var/run/${base}.pid ] ; then
63 mv /var/run/${base}.pid /var/run/plc_${base}.pid
69 # Print IP address of hostname if resolvable
72 perl -MSocket -e '($a,$b,$c,$d,@addrs) = gethostbyname($ARGV[0]); print inet_ntoa($addrs[0]) . "\n";' $1 2>/dev/null
75 # Print the CNAME of an SSL certificate
78 openssl x509 -noout -in $1 -subject | \
79 sed -n -e 's@.*/CN=\([^/]*\).*@\1@p'
82 # Print the emailAddress of an SSL certificate
85 openssl x509 -noout -in $1 -subject | \
86 sed -n -e 's@.*/emailAddress=\([^/]*\).*@\1@p'
89 # Forcefully make a symlink
92 mkdir -p $(dirname $2)
97 # Make copies of stdout and stderr. The plc initscript redirects
98 # stdout and stderr to a logfile if -v is not specified.
99 [ ! -e /dev/fd/3 ] && exec 3>&1
100 [ ! -e /dev/fd/4 ] && exec 4>&2