- split up guest.init (/etc/init.d/plc inside the chroot) into
[myplc.git] / plc.d / functions
1 # -*-Shell-script-*-
2 #
3 # Common functions for PLC startup/shutdown scripts
4 #
5 # Mark Huang <mlhuang@cs.princeton.edu>
6 # Copyright (C) 2006 The Trustees of Princeton University
7 #
8 # $Id: guest.init,v 1.12 2006/04/04 22:09:47 mlhuang Exp $
9 #
10
11 export PATH=/sbin:/bin:/usr/bin:/usr/sbin
12
13 # Source function library
14 . /etc/init.d/functions
15
16 # Source configuration
17 . /etc/planetlab/plc_config
18
19 # Total number of errors
20 ERRORS=0
21
22 # Count the exit status of the last command
23 check ()
24 {
25     ERRORS=$(($ERRORS+$?))
26 }
27
28 # Print status header
29 dialog ()
30 {
31     echo -n "PLC: $*: " >&3
32 }
33
34 # Print result
35 result ()
36 {
37     if [ $ERRORS -eq 0 ] ; then
38         success "$*" >&3
39     else
40         failure "$*" >&3
41     fi
42     echo >&3
43 }
44
45 # Start up a program with a plc_ prefix
46 plc_daemon ()
47 {
48     base=${1##*/}
49
50     # See if it's already running. Look *only* at the pid file.
51     if [ -f /var/run/plc_${base}.pid ]; then
52         local line p
53         read line < /var/run/plc_${base}.pid
54         for p in $line ; do
55                 [ -z "${p//[0-9]/}" -a -d "/proc/$p" ] && pid="$pid $p"
56         done
57     fi
58
59     [ -n "${pid:-}" -a -z "${force:-}" ] && return
60
61     # And start it up.
62     (exec -a plc_${base} $*)
63
64     if [ -f /var/run/${base}.pid ] ; then
65         mv /var/run/${base}.pid /var/run/plc_${base}.pid
66     fi
67 }
68
69 # Print IP address of hostname if resolvable
70 gethostbyname ()
71 {
72     perl -MSocket -e '($a,$b,$c,$d,@addrs) = gethostbyname($ARGV[0]); print inet_ntoa($addrs[0]) . "\n";' $1 2>/dev/null
73 }
74
75 # Print the CNAME of an SSL certificate
76 ssl_cname ()
77 {
78     openssl x509 -noout -in $1 -subject | \
79         sed -n -e 's@.*/CN=\([^/]*\).*@\1@p'
80 }
81
82 # Forcefully make a symlink
83 symlink ()
84 {
85     mkdir -p $(dirname $2)
86     rm -f $2
87     ln -s $1 $2
88 }
89
90 # Make copies of stdout and stderr. The plc initscript redirects
91 # stdout and stderr to a logfile if -v is not specified.
92 [ ! -e /dev/fd/3 ] && exec 3>&1
93 [ ! -e /dev/fd/4 ] && exec 4>&2