2 # Common functions for PLC startup/shutdown scripts
4 # Mark Huang <mlhuang@cs.princeton.edu>
5 # Copyright (C) 2006 The Trustees of Princeton University
8 export PATH=/sbin:/bin:/usr/bin:/usr/sbin
10 # covering for former /etc/init.d/functions
22 local message="$1"; shift
27 # Total number of errors
30 # Count the exit status of the last command
32 ERRORS=$(($ERRORS+$?))
36 echo -n "PLC: $*: " >&3
41 if [ $ERRORS -eq 0 ] ; then
49 # Start up a program with a plc_ prefix
50 function plc_daemon () {
53 # See if it's already running. Look *only* at the pid file.
54 if [ -f /var/run/plc_${base}.pid ]; then
56 read line < /var/run/plc_${base}.pid
58 [ -z "${p//[0-9]/}" -a -d "/proc/$p" ] && pid="$pid $p"
62 [ -n "${pid:-}" -a -z "${force:-}" ] && return
65 # Thierry -- June 18 2007
66 # when invoking, e.g. service plc start httpd from an ssh connection
67 # ssh stupidly hangs when everything is done
68 # it turns out the forked ssh daemon exhibits the following stack at that point
70 # #0 0x001d6402 in __kernel_vsyscall ()
71 # #1 0x003c2e7d in ___newselect_nocancel () from /lib/libc.so.6
72 # #2 0x004387b4 in main () from /usr/sbin/sshd
73 # So I figured the various file descriptors used were not properly closed
74 (exec 3>&- 4>&- ; exec -a plc_${base} $*)
77 if [ -f /var/run/${base}.pid ] ; then
78 mv /var/run/${base}.pid /var/run/plc_${base}.pid
84 # Print IP address of hostname if resolvable
85 function gethostbyname () {
86 python -c 'import socket; import sys; print socket.gethostbyname(sys.argv[1])' $1 2>/dev/null
89 # Forcefully make a symlink
91 mkdir -p $(dirname $2)
96 # Argument(s) or stdin to lowercase stdout
98 if [ ${#*} -ge 1 ] ; then
105 # Argument(s) or stdin to uppercase stdout
107 if [ ${#*} -ge 1 ] ; then
114 # Regenerate configuration files
115 function plc_reload () {
118 # Regenerate the main configuration file from default values
119 # overlaid with site-specific and current values.
120 # Thierry -- 2007-07-05 : values in plc_config.xml are *not* taken into account here
122 /etc/planetlab/default_config.xml
123 /etc/planetlab/configs/site.xml
126 for file in "${files[@]}" ; do
127 if [ -n "$force" -o $file -nt /etc/planetlab/plc_config.xml ] ; then
128 tmp=$(mktemp /tmp/plc_config.xml.XXXXXX)
129 plc-config --xml "${files[@]}" >$tmp
130 if [ $? -eq 0 ] ; then
131 mv $tmp /etc/planetlab/plc_config.xml
132 chmod 444 /etc/planetlab/plc_config.xml
134 echo "PLC: Warning: Invalid configuration file(s) detected"
141 # Convert configuration to various formats
142 if [ -n "$force" -o /etc/planetlab/plc_config.xml -nt /etc/planetlab/plc_config ] ; then
143 plc-config --shell >/etc/planetlab/plc_config
145 if [ -n "$force" -o /etc/planetlab/plc_config.xml -nt /etc/planetlab/plc_config.py ] ; then
146 plc-config --python >/etc/planetlab/plc_config.py
148 if [ -n "$force" -o /etc/planetlab/plc_config.xml -nt /etc/planetlab/php/plc_config.php ] ; then
149 mkdir -p /etc/planetlab/php
150 plc-config --php >/etc/planetlab/php/plc_config.php
154 #################### feb 2013 for f18
155 # Authorization directives change in apache 2.4
156 # http://httpd.apache.org/docs/2.4/upgrading.html#access
157 #### up to 2.2, this would be e.g.
159 # Server version: Apache/2.2.22 (Unix)
160 # <Directory "/usr/share/plc_api/apache">
165 #### starting with 2.4 it becomes
167 # Server version: Apache/2.4.3 (Fedora)
168 # <Directory "/usr/share/plc_api/apache">
170 # Require all granted
173 function apache_newauth () {
174 apache_version=$(apachectl -V 2> /dev/null | grep 'Server version' | cut -d ' ' -f3 | sed -e 's,^.*/,,')
175 apache_major=$(echo $apache_version | cut -d. -f1)
176 apache_minor=$(echo $apache_version | cut -d. -f2)
177 test "$apache_minor" -ge 4
179 function apache_allow () {
180 if apache_newauth; then
181 echo -e "Require all granted"
183 echo -e "Order allow,deny\n Allow from all"
186 function apache_forbid () {
187 if apache_newauth; then
188 echo -e "Require all denied"
190 echo -e "Order deny,allow\n Deny from all"
194 # Make copies of stdout and stderr. The plc initscript redirects
195 # stdout and stderr to a logfile if -v is not specified.
196 [ ! -e /proc/self/fd/3 ] && exec 3>&1
197 [ ! -e /proc/self/fd/4 ] && exec 4>&2