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 # Source function library
11 . /etc/init.d/functions
13 # Total number of errors
16 # Count the exit status of the last command
18 ERRORS=$(($ERRORS+$?))
23 echo -n "PLC: $*: " >&3
28 if [ $ERRORS -eq 0 ] ; then
36 # Start up a program with a plc_ prefix
37 function plc_daemon () {
40 # See if it's already running. Look *only* at the pid file.
41 if [ -f /var/run/plc_${base}.pid ]; then
43 read line < /var/run/plc_${base}.pid
45 [ -z "${p//[0-9]/}" -a -d "/proc/$p" ] && pid="$pid $p"
49 [ -n "${pid:-}" -a -z "${force:-}" ] && return
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
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} $*)
64 if [ -f /var/run/${base}.pid ] ; then
65 mv /var/run/${base}.pid /var/run/plc_${base}.pid
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
76 # Forcefully make a symlink
78 mkdir -p $(dirname $2)
83 # Argument(s) or stdin to lowercase stdout
85 if [ ${#*} -ge 1 ] ; then
92 # Argument(s) or stdin to uppercase stdout
94 if [ ${#*} -ge 1 ] ; then
101 # Regenerate configuration files
102 function plc_reload () {
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
109 /etc/planetlab/default_config.xml
110 /etc/planetlab/configs/site.xml
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
121 echo "PLC: Warning: Invalid configuration file(s) detected"
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
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
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
141 #################### feb 2013 for f18
142 # Authorization directives change in apache 2.4
143 # http://httpd.apache.org/docs/2.4/upgrading.html#access
144 #### up to 2.2, this would be e.g.
146 # Server version: Apache/2.2.22 (Unix)
147 # <Directory "/usr/share/plc_api/apache">
152 #### starting with 2.4 it becomes
154 # Server version: Apache/2.4.3 (Fedora)
155 # <Directory "/usr/share/plc_api/apache">
157 # Require all granted
160 function apache_newauth () {
161 apache_version=$(apachectl -V 2> /dev/null | grep 'Server version' | cut -d ' ' -f3 | sed -e 's,^.*/,,')
162 apache_major=$(echo $apache_version | cut -d. -f1)
163 apache_minor=$(echo $apache_version | cut -d. -f2)
164 test "$apache_minor" -ge 4
166 function apache_allow () {
167 if apache_newauth; then
168 echo -e "Require all granted"
170 echo -e "Order allow,deny\n Allow from all"
173 function apache_forbid () {
174 if apache_newauth; then
175 echo -e "Require all denied"
177 echo -e "Order deny,allow\n Deny from all"
181 # Make copies of stdout and stderr. The plc initscript redirects
182 # stdout and stderr to a logfile if -v is not specified.
183 [ ! -e /proc/self/fd/3 ] && exec 3>&1
184 [ ! -e /proc/self/fd/4 ] && exec 4>&2