3ca315b8757589363450e18c7308b1dba6537477
[myplc.git] / plc.d / functions
1 # -*-Shell-script-*-
2 # Common functions for PLC startup/shutdown scripts
3 #
4 # Mark Huang <mlhuang@cs.princeton.edu>
5 # Copyright (C) 2006 The Trustees of Princeton University
6 #
7
8 export PATH=/sbin:/bin:/usr/bin:/usr/sbin
9
10 # Source function library
11 . /etc/init.d/functions
12
13 # Total number of errors
14 ERRORS=0
15
16 # Count the exit status of the last command
17 function check () {
18     ERRORS=$(($ERRORS+$?))
19 }
20
21 # Print status header
22 function dialog () {
23     echo -n "PLC: $*: " >&3
24 }
25
26 # Print result
27 function result () {
28     if [ $ERRORS -eq 0 ] ; then
29         success "$*" >&3
30     else
31         failure "$*" >&3
32     fi
33     echo >&3
34 }
35
36 # Start up a program with a plc_ prefix
37 function plc_daemon () {
38     base=${1##*/}
39
40     # See if it's already running. Look *only* at the pid file.
41     if [ -f /var/run/plc_${base}.pid ]; then
42         local line p
43         read line < /var/run/plc_${base}.pid
44         for p in $line ; do
45                 [ -z "${p//[0-9]/}" -a -d "/proc/$p" ] && pid="$pid $p"
46         done
47     fi
48
49     [ -n "${pid:-}" -a -z "${force:-}" ] && return
50
51     # And start it up.
52     # Thierry -- June 18 2007
53     # when invoking, e.g. service plc start httpd from an ssh connection
54     # ssh stupidly hangs when everything is done
55     # it turns out the forked ssh daemon exhibits the following stack at that point
56     # (gdb) where
57     # #0  0x001d6402 in __kernel_vsyscall ()
58     # #1  0x003c2e7d in ___newselect_nocancel () from /lib/libc.so.6
59     # #2  0x004387b4 in main () from /usr/sbin/sshd
60     # So I figured the various file descriptors used were not properly closed
61     (exec 3>&- 4>&- ; exec -a plc_${base} $*)
62     ret=$?
63
64     if [ -f /var/run/${base}.pid ] ; then
65         mv /var/run/${base}.pid /var/run/plc_${base}.pid
66     fi
67
68     return $ret
69 }
70
71 # Print IP address of hostname if resolvable
72 function gethostbyname () {
73     python -c 'import socket; import sys; print socket.gethostbyname(sys.argv[1])' $1 2>/dev/null
74 }
75
76 # Forcefully make a symlink
77 function symlink () {
78     mkdir -p $(dirname $2)
79     rm -f $2
80     ln -s $1 $2
81 }
82
83 # Argument(s) or stdin to lowercase stdout
84 function lower () {
85     if [ ${#*} -ge 1 ] ; then
86         tr A-Z a-z <<<$*
87     else
88         tr A-Z a-z
89     fi
90 }
91
92 # Argument(s) or stdin to uppercase stdout
93 function upper () {
94     if [ ${#*} -ge 1 ] ; then
95         tr a-z A-Z <<<$*
96     else
97         tr a-z A-Z
98     fi
99 }
100
101 # Regenerate configuration files
102 function plc_reload () {
103     force=$1
104
105     # Regenerate the main configuration file from default values
106     # overlaid with site-specific and current values.
107     # Thierry -- 2007-07-05 : values in plc_config.xml are *not* taken into account here
108     files=(
109         /etc/planetlab/default_config.xml
110         /etc/planetlab/configs/site.xml
111     )
112
113     for file in "${files[@]}" ; do
114     if [ -n "$force" -o $file -nt /etc/planetlab/plc_config.xml ] ; then
115         tmp=$(mktemp /tmp/plc_config.xml.XXXXXX)
116         plc-config --xml "${files[@]}" >$tmp
117         if [ $? -eq 0 ] ; then
118         mv $tmp /etc/planetlab/plc_config.xml
119         chmod 444 /etc/planetlab/plc_config.xml
120         else
121         echo "PLC: Warning: Invalid configuration file(s) detected"
122         rm -f $tmp
123         fi
124         break
125     fi
126     done
127
128     # Convert configuration to various formats
129     if [ -n "$force" -o /etc/planetlab/plc_config.xml -nt /etc/planetlab/plc_config ] ; then
130     plc-config --shell >/etc/planetlab/plc_config
131     fi
132     if [ -n "$force" -o /etc/planetlab/plc_config.xml -nt /etc/planetlab/plc_config.py ] ; then
133     plc-config --python >/etc/planetlab/plc_config.py
134     fi
135     if [ -n "$force" -o /etc/planetlab/plc_config.xml -nt /etc/planetlab/php/plc_config.php ] ; then
136     mkdir -p /etc/planetlab/php
137     plc-config --php >/etc/planetlab/php/plc_config.php
138     fi
139 }
140
141 # Make copies of stdout and stderr. The plc initscript redirects
142 # stdout and stderr to a logfile if -v is not specified.
143 [ ! -e /proc/self/fd/3 ] && exec 3>&1
144 [ ! -e /proc/self/fd/4 ] && exec 4>&2