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 local host="$1"; shift
87 python3 -c "import socket; import sys; print(socket.gethostbyname('${host}'))"
90 # Forcefully make a symlink
92 mkdir -p $(dirname $2)
97 # Argument(s) or stdin to lowercase stdout
99 if [ ${#*} -ge 1 ] ; then
106 # Argument(s) or stdin to uppercase stdout
108 if [ ${#*} -ge 1 ] ; then
115 # Regenerate configuration files
116 function plc_reload () {
119 # Regenerate the main configuration file from default values
120 # overlaid with site-specific and current values.
121 # Thierry -- 2007-07-05 : values in plc_config.xml are *not* taken into account here
123 /etc/planetlab/default_config.xml
124 /etc/planetlab/configs/site.xml
127 for file in "${files[@]}" ; do
128 if [ -n "$force" -o $file -nt /etc/planetlab/plc_config.xml ] ; then
129 tmp=$(mktemp /tmp/plc_config.xml.XXXXXX)
130 plc-config --xml "${files[@]}" >$tmp
131 if [ $? -eq 0 ] ; then
132 mv $tmp /etc/planetlab/plc_config.xml
133 chmod 444 /etc/planetlab/plc_config.xml
135 echo "PLC: Warning: Invalid configuration file(s) detected"
142 # Convert configuration to various formats
143 if [ -n "$force" -o /etc/planetlab/plc_config.xml -nt /etc/planetlab/plc_config ] ; then
144 plc-config --shell >/etc/planetlab/plc_config
146 if [ -n "$force" -o /etc/planetlab/plc_config.xml -nt /etc/planetlab/plc_config.py ] ; then
147 plc-config --python >/etc/planetlab/plc_config.py
149 if [ -n "$force" -o /etc/planetlab/plc_config.xml -nt /etc/planetlab/php/plc_config.php ] ; then
150 mkdir -p /etc/planetlab/php
151 plc-config --php >/etc/planetlab/php/plc_config.php
155 #################### feb 2013 for f18
156 # Authorization directives change in apache 2.4
157 # http://httpd.apache.org/docs/2.4/upgrading.html#access
158 #### up to 2.2, this would be e.g.
160 # Server version: Apache/2.2.22 (Unix)
161 # <Directory "/usr/share/plc_api/apache">
166 #### starting with 2.4 it becomes
168 # Server version: Apache/2.4.3 (Fedora)
169 # <Directory "/usr/share/plc_api/apache">
171 # Require all granted
174 function apache_newauth () {
175 # somehow in 2019 apachectl stopped answering the -V option
176 # apache_version=$(apachectl -V 2> /dev/null | grep 'Server version' | cut -d ' ' -f3 | sed -e 's,^.*/,,')
177 apache_version=$(rpm -q --queryformat "%{VERSION}" httpd)
178 apache_major=$(echo $apache_version | cut -d. -f1)
179 apache_minor=$(echo $apache_version | cut -d. -f2)
180 test "$apache_minor" -ge 4
182 function apache_allow () {
183 if apache_newauth; then
184 echo -e "Require all granted"
186 echo -e "Order allow,deny\n Allow from all"
189 function apache_forbid () {
190 if apache_newauth; then
191 echo -e "Require all denied"
193 echo -e "Order deny,allow\n Deny from all"
197 # Make copies of stdout and stderr. The plc initscript redirects
198 # stdout and stderr to a logfile if -v is not specified.
199 [ ! -e /proc/self/fd/3 ] && exec 3>&1
200 [ ! -e /proc/self/fd/4 ] && exec 4>&2