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