3 # Check existence of program
4 if [ ! -e "server" ]; then
5 (echo "Rebuild server.") 1>&2;
10 if [[ "$(uname -m)" != *arm* ]]; then
11 echo Not running on the BBB
12 # Use this to quickly test run the server in valgrind
13 #spawn-fcgi -p9005 -n ./valgrind.sh
14 # Use this to run the server normally
15 spawn-fcgi -p9005 -n ./server
19 # Check running as root
20 if [ "$(whoami)" != "root" ]; then
21 (echo "Run $0 as root.") 1>&2
26 echo Rotating the system logs
27 logrotate -f /etc/logrotate.d/mctxserv.conf
29 # Identify cape-manager slots
30 slot=$(echo /sys/devices/bone_capemgr.*/slots | awk '{print $1}')
34 if [ $(cat $slot | grep am33xx_pwm -c) -gt 0 ]; then
35 echo am33xx_pwm already loaded, not loading again;
37 echo Enabling PWM driver am33xx_pwm
38 echo am33xx_pwm > $slot
41 # Safe pins that won't interfere with one another
42 #ports=(P9_22 P9_42 P9_16 P8_13 P9_28);
43 #portnumbers=(0 2 4 6 7);
44 #Full correspondence from pwm0-pwm7
45 ports=(P9_22 P9_21 P9_42 P9_14 P9_16 P8_19 P8_13 P9_28);
46 portnumbers=(0 1 2 3 4 5 6 7);
48 # They must be exported at this stage, before the device tree
49 # overlay for that pin is enabled.
50 for ((c=0; c < ${#ports[*]}; c++)); do
51 if [ ! -d $pwm/pwm${portnumbers[$c]} ]; then
52 echo Exporting PWM ${portnumbers[$c]} \(${ports[$c]}\)
53 echo ${portnumbers[$c]} > $pwm/export
55 echo PWM ${portnumbers[$c]} already enabled
58 if [ $(cat $slot | grep ${ports[$c]} -c) -gt 0 ]; then
59 echo PWM pin ${ports[$c]} already enabled, not enabling again
61 (echo bone_pwm_${ports[$c]} > $slot) 1>&2 >> /dev/null
66 if [ $(cat $slot | grep BB-ADC -c) -gt 0 ]; then
67 echo BB-ADC already loaded, not loading again;
70 (echo BB-ADC > $slot) 1>&2 >> /dev/null
73 # Find adc_device_path
74 # NOTE: This has to be passed as a parameter, because it is not always the same. For some unfathomable reason. Hooray.
75 #adc_device_path=$(dirname $(find /sys -name *AIN0))
81 echo "Parameters are: $parameters"
83 # Run the program with parameters
84 # TODO: Can tell spawn-fcgi to run the program as an unprivelaged user?
85 # But first will have to work out how to set PWM/GPIO as unprivelaged user
86 # NOTE: Having the program automatically restart itself after a *FATAL ERROR* doesn't seem like such a good idea now
87 # (Some things that call Fatal happen under circumstances where they will just keep calling Fatal every time the program starts)
88 # Change the number of fails to 1 for now. We can potentially use different error codes for different types of errors, but that seems overkill.
90 while [ $fails -lt 1 ]; do
91 spawn-fcgi -p9005 -n -- ./server $parameters
93 if [ "$error" == "0" ]; then
96 fails=$(( $fails + 1 ))
97 (echo "Restarting server after Fatal Error #$fails") 1>&2
100 (echo "Server had too many Fatal Errors ($fails)") 1>&2