Setting tag myplc-5.3-5
[myplc.git] / plc.d / functions
index b407217..161da9e 100644 (file)
 # -*-Shell-script-*-
-#
 # Common functions for PLC startup/shutdown scripts
 #
 # Mark Huang <mlhuang@cs.princeton.edu>
 # Copyright (C) 2006 The Trustees of Princeton University
 #
-# $Id: functions,v 1.6 2006/07/10 21:05:37 mlhuang Exp $
-#
 
 export PATH=/sbin:/bin:/usr/bin:/usr/sbin
 
-# Source function library
-. /etc/init.d/functions
+# covering for former /etc/init.d/functions
+function success() {
+    echo success "$@"
+    true
+}
+
+function failure() {
+    echo failure "$@"
+    false
+}
+
+function action() {
+    local message="$1"; shift
+    echo $message
+    "$@"
+}
 
 # Total number of errors
 ERRORS=0
 
 # Count the exit status of the last command
-check ()
-{
+function check () {
     ERRORS=$(($ERRORS+$?))
 }
 
-# Print status header
-dialog ()
-{
+function dialog () {
     echo -n "PLC: $*: " >&3
 }
 
 # Print result
-result ()
-{
+function result () {
     if [ $ERRORS -eq 0 ] ; then
-       success "$*" >&3
+        success "$@" >&3
     else
-       failure "$*" >&3
+        failure "$@" >&3
     fi
     echo >&3
 }
 
 # Start up a program with a plc_ prefix
-plc_daemon ()
-{
+function plc_daemon () {
     base=${1##*/}
 
     # See if it's already running. Look *only* at the pid file.
     if [ -f /var/run/plc_${base}.pid ]; then
-       local line p
-       read line < /var/run/plc_${base}.pid
-       for p in $line ; do
-               [ -z "${p//[0-9]/}" -a -d "/proc/$p" ] && pid="$pid $p"
-       done
+        local line p
+        read line < /var/run/plc_${base}.pid
+        for p in $line ; do
+                [ -z "${p//[0-9]/}" -a -d "/proc/$p" ] && pid="$pid $p"
+        done
     fi
 
     [ -n "${pid:-}" -a -z "${force:-}" ] && return
 
     # And start it up.
-    (exec -a plc_${base} $*)
+    # Thierry -- June 18 2007
+    # when invoking, e.g. service plc start httpd from an ssh connection
+    # ssh stupidly hangs when everything is done
+    # it turns out the forked ssh daemon exhibits the following stack at that point
+    # (gdb) where
+    # #0  0x001d6402 in __kernel_vsyscall ()
+    # #1  0x003c2e7d in ___newselect_nocancel () from /lib/libc.so.6
+    # #2  0x004387b4 in main () from /usr/sbin/sshd
+    # So I figured the various file descriptors used were not properly closed
+    (exec 3>&- 4>&- ; exec -a plc_${base} $*)
     ret=$?
 
     if [ -f /var/run/${base}.pid ] ; then
-       mv /var/run/${base}.pid /var/run/plc_${base}.pid
+        mv /var/run/${base}.pid /var/run/plc_${base}.pid
     fi
 
     return $ret
 }
 
 # Print IP address of hostname if resolvable
-gethostbyname ()
-{
-    perl -MSocket -e '($a,$b,$c,$d,@addrs) = gethostbyname($ARGV[0]); print inet_ntoa($addrs[0]) . "\n";' $1 2>/dev/null
+function gethostbyname () {
+    python -c 'import socket; import sys; print socket.gethostbyname(sys.argv[1])' $1 2>/dev/null
 }
 
 # Forcefully make a symlink
-symlink ()
-{
+function symlink () {
     mkdir -p $(dirname $2)
     rm -f $2
     ln -s $1 $2
 }
 
 # Argument(s) or stdin to lowercase stdout
-lower ()
-{
+function lower () {
     if [ ${#*} -ge 1 ] ; then
-       tr A-Z a-z <<EOF
-$*
-EOF
+        tr A-Z a-z <<<$*
     else
-       tr A-Z a-z
+        tr A-Z a-z
     fi
 }
 
 # Argument(s) or stdin to uppercase stdout
-upper ()
-{
+function upper () {
     if [ ${#*} -ge 1 ] ; then
-       tr a-z A-Z <<EOF
-$*
-EOF
+        tr a-z A-Z <<<$*
+    else
+        tr a-z A-Z
+    fi
+}
+
+# Regenerate configuration files
+function plc_reload () {
+    force=$1
+
+    # Regenerate the main configuration file from default values
+    # overlaid with site-specific and current values.
+    # Thierry -- 2007-07-05 : values in plc_config.xml are *not* taken into account here
+    files=(
+        /etc/planetlab/default_config.xml
+        /etc/planetlab/configs/site.xml
+    )
+
+    for file in "${files[@]}" ; do
+    if [ -n "$force" -o $file -nt /etc/planetlab/plc_config.xml ] ; then
+        tmp=$(mktemp /tmp/plc_config.xml.XXXXXX)
+        plc-config --xml "${files[@]}" >$tmp
+        if [ $? -eq 0 ] ; then
+        mv $tmp /etc/planetlab/plc_config.xml
+        chmod 444 /etc/planetlab/plc_config.xml
+        else
+        echo "PLC: Warning: Invalid configuration file(s) detected"
+        rm -f $tmp
+        fi
+        break
+    fi
+    done
+
+    # Convert configuration to various formats
+    if [ -n "$force" -o /etc/planetlab/plc_config.xml -nt /etc/planetlab/plc_config ] ; then
+    plc-config --shell >/etc/planetlab/plc_config
+    fi
+    if [ -n "$force" -o /etc/planetlab/plc_config.xml -nt /etc/planetlab/plc_config.py ] ; then
+    plc-config --python >/etc/planetlab/plc_config.py
+    fi
+    if [ -n "$force" -o /etc/planetlab/plc_config.xml -nt /etc/planetlab/php/plc_config.php ] ; then
+    mkdir -p /etc/planetlab/php
+    plc-config --php >/etc/planetlab/php/plc_config.php
+    fi
+}
+
+#################### feb 2013 for f18
+# Authorization directives change in apache 2.4
+# http://httpd.apache.org/docs/2.4/upgrading.html#access
+#### up to 2.2, this would be e.g.
+# apachectl -V
+# Server version: Apache/2.2.22 (Unix)
+# <Directory "/usr/share/plc_api/apache">
+# Options       +ExecCGI
+# Order allow,deny
+# Allow from all
+# </Directory>
+#### starting with 2.4 it becomes
+# apachectl -V
+# Server version: Apache/2.4.3 (Fedora)
+# <Directory "/usr/share/plc_api/apache">
+# Options       +ExecCGI
+# Require       all granted
+# </Directory>
+
+function apache_newauth () {
+    apache_version=$(apachectl -V 2> /dev/null | grep 'Server version' | cut -d ' ' -f3 | sed -e 's,^.*/,,')
+    apache_major=$(echo $apache_version | cut -d. -f1)
+    apache_minor=$(echo $apache_version | cut -d. -f2)
+    test "$apache_minor" -ge 4
+}
+function apache_allow () {
+    if apache_newauth; then
+        echo -e "Require all granted"
+    else
+        echo -e "Order allow,deny\n   Allow from all"
+    fi
+}
+function apache_forbid () {
+    if apache_newauth; then
+        echo -e "Require all denied"
     else
-       tr a-z A-Z
+        echo -e "Order deny,allow\n   Deny from all"
     fi
 }
 
 # Make copies of stdout and stderr. The plc initscript redirects
 # stdout and stderr to a logfile if -v is not specified.
-[ ! -e /dev/fd/3 ] && exec 3>&1
-[ ! -e /dev/fd/4 ] && exec 4>&2
+[ ! -e /proc/self/fd/3 ] && exec 3>&1
+[ ! -e /proc/self/fd/4 ] && exec 4>&2