egrep is obsolete
[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 # covering for former /etc/init.d/functions
11 function success() {
12     echo success "$@"
13     true
14 }
15
16 function failure() {
17     echo failure "$@"
18     false
19 }
20
21 function action() {
22     local message="$1"; shift
23     echo $message
24     "$@"
25 }
26
27 # Total number of errors
28 ERRORS=0
29
30 # Count the exit status of the last command
31 function check () {
32     ERRORS=$(($ERRORS+$?))
33 }
34
35 function dialog () {
36     echo -n "PLC: $*: " >&3
37 }
38
39 # Print result
40 function result () {
41     if [ $ERRORS -eq 0 ] ; then
42         success "$@" >&3
43     else
44         failure "$@" >&3
45     fi
46     echo >&3
47 }
48
49 # Start up a program with a plc_ prefix
50 function plc_daemon () {
51     base=${1##*/}
52
53     # See if it's already running. Look *only* at the pid file.
54     if [ -f /var/run/plc_${base}.pid ]; then
55         local line p
56         read line < /var/run/plc_${base}.pid
57         for p in $line ; do
58                 [ -z "${p//[0-9]/}" -a -d "/proc/$p" ] && pid="$pid $p"
59         done
60     fi
61
62     [ -n "${pid:-}" -a -z "${force:-}" ] && return
63
64     # And start it up.
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
69     # (gdb) where
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} $*)
75     ret=$?
76
77     if [ -f /var/run/${base}.pid ] ; then
78         mv /var/run/${base}.pid /var/run/plc_${base}.pid
79     fi
80
81     return $ret
82 }
83
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}'))"
88 }
89
90 # Forcefully make a symlink
91 function symlink () {
92     mkdir -p $(dirname $2)
93     rm -f $2
94     ln -s $1 $2
95 }
96
97 # Argument(s) or stdin to lowercase stdout
98 function lower () {
99     if [ ${#*} -ge 1 ] ; then
100         tr A-Z a-z <<<$*
101     else
102         tr A-Z a-z
103     fi
104 }
105
106 # Argument(s) or stdin to uppercase stdout
107 function upper () {
108     if [ ${#*} -ge 1 ] ; then
109         tr a-z A-Z <<<$*
110     else
111         tr a-z A-Z
112     fi
113 }
114
115 # Regenerate configuration files
116 function plc_reload () {
117     force=$1
118
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
122     files=(
123         /etc/planetlab/default_config.xml
124         /etc/planetlab/configs/site.xml
125     )
126
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
134         else
135         echo "PLC: Warning: Invalid configuration file(s) detected"
136         rm -f $tmp
137         fi
138         break
139     fi
140     done
141
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
145     fi
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
148     fi
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
152     fi
153 }
154
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.
159 # apachectl -V
160 # Server version: Apache/2.2.22 (Unix)
161 # <Directory "/usr/share/plc_api/apache">
162 # Options       +ExecCGI
163 # Order allow,deny
164 # Allow from all
165 # </Directory>
166 #### starting with 2.4 it becomes
167 # apachectl -V
168 # Server version: Apache/2.4.3 (Fedora)
169 # <Directory "/usr/share/plc_api/apache">
170 # Options       +ExecCGI
171 # Require       all granted
172 # </Directory>
173
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
181 }
182 function apache_allow () {
183     if apache_newauth; then
184         echo -e "Require all granted"
185     else
186         echo -e "Order allow,deny\n   Allow from all"
187     fi
188 }
189 function apache_forbid () {
190     if apache_newauth; then
191         echo -e "Require all denied"
192     else
193         echo -e "Order deny,allow\n   Deny from all"
194     fi
195 }
196
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