home *** CD-ROM | disk | FTP | other *** search
/ Chip 2011 November / CHIP_2011_11.iso / Programy / Linux / Ubuntu_64-bit / ubuntu-11.04-desktop-amd64.iso / casper / filesystem.squashfs / bin / init-checkconf < prev    next >
Text File  |  2011-04-20  |  6KB  |  233 lines

  1. #!/bin/bash
  2. #---------------------------------------------------------------------
  3. # Script to determine if specified config file is valid or not.
  4. # By default, two checks are performed:
  5. #
  6. #   - Ensure Upstart can parse overall file successfully
  7. #   - Ensure all script sections are parseable by shell
  8. #
  9. #---------------------------------------------------------------------
  10. #
  11. # Copyright (C) 2011 Canonical Ltd.
  12. #
  13. # Author: James Hunt <james.hunt@canonical.com>
  14. #
  15. # This program is free software: you can redistribute it and/or modify
  16. # it under the terms of the GNU General Public License as published by
  17. # the Free Software Foundation, version 3 of the License.
  18. #
  19. # This program is distributed in the hope that it will be useful,
  20. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  22. # GNU General Public License for more details.
  23. #
  24. # You should have received a copy of the GNU General Public License
  25. # along with this program.  If not, see <http://www.gnu.org/licenses/>.
  26. #
  27. #---------------------------------------------------------------------
  28.  
  29. script_name=${0##*/}
  30. confdir=$(mktemp -d /tmp/${script_name}.XXXXXXXXXX)
  31. upstart_path=/sbin/init
  32. initctl_path=/sbin/initctl
  33. debug_enabled=n
  34. file_valid=n
  35. running=n
  36. check_scripts=y
  37.  
  38. cleanup()
  39. {
  40.   kill -0 "$upstart_pid" >/dev/null 2>&1 && \
  41.   kill -9 "$upstart_pid" >/dev/null 2>&1
  42.   [ -d "$confdir" ] && rm -rf "$confdir"
  43.   [ $file_valid = y ] && exit 0
  44.   exit 1
  45. }
  46.  
  47. usage()
  48. {
  49. cat <<EOT
  50. Description: Determine if specified Upstart (init(8)) job configuration
  51.              file is valid.
  52.  
  53. Usage: $script_name [options] -f <conf_file>
  54.        $script_name [options]    <conf_file>
  55.  
  56. Options:
  57.  
  58.  -d, --debug           : Show some debug output.
  59.  -f <file>,            : Job configuration file to check.
  60.  --file=<file>           (no default).
  61.  -i <path>,            : Specify path to initctl binary
  62.  --initctl-path=<path>   (default=$initctl_path).
  63.  -s, --noscript        : Do not check script sections.
  64.  -x <path>             : Specify path to init daemon binary
  65.  --upstart-path=<path>   (default=$upstart_path).
  66.  -h, --help            : Show this help.
  67.  
  68. EOT
  69. }
  70.  
  71. debug()
  72. {
  73.   msg="$*"
  74.   [ $debug_enabled = y ] && echo "DEBUG: $msg"
  75. }
  76.  
  77. error()
  78. {
  79.   msg="$*"
  80.   printf "ERROR: %s\n" "$msg" >&2
  81. }
  82.  
  83. die()
  84. {
  85.   error "$*"
  86.   exit 1
  87. }
  88.  
  89. trap cleanup EXIT INT TERM
  90.  
  91. args=$(getopt \
  92.   -n "$script_name" \
  93.   -a \
  94.   --options="df:hi:sx:" \
  95.   --longoptions="debug file: help initctl-path: noscript upstart-path:" \
  96.   -- "$@")
  97.  
  98. eval set -- "$args"
  99. [ $? -ne 0 ] && { usage; exit 1; }
  100. [ $# -eq 0 ] && { usage; exit 0; }
  101.  
  102. while [ $# -gt 0 ]
  103. do
  104.     case "$1" in
  105.       -d|--debug)
  106.         debug_enabled=y
  107.         ;;
  108.  
  109.       -f|--file)
  110.         file="$2"
  111.         shift
  112.         ;;
  113.  
  114.       -h|--help)
  115.         usage
  116.         exit 0
  117.         ;;
  118.  
  119.       -i|--initctl-path)
  120.         initctl_path="$2"
  121.         shift
  122.         ;;
  123.  
  124.       -s|--noscript)
  125.         check_scripts=n
  126.         ;;
  127.  
  128.       -x|--upstart-path)
  129.         upstart_path="$2"
  130.         shift
  131.         ;;
  132.  
  133.       --)
  134.         shift
  135.         break
  136.         ;;
  137.     esac
  138.     shift
  139. done
  140.  
  141. [ -z "$file" ] && file="$1"
  142.  
  143. # safety first
  144. [ "$(id -u)" -eq 0 ] && die "cannot run as root"
  145.  
  146. [   -z "$file" ] && die "must specify configuration file"
  147. [ ! -f "$file" ] && die "file $file does not exist"
  148.  
  149. debug "upstart_path=$upstart_path"
  150. debug "initctl_path=$initctl_path"
  151.  
  152. for cmd in "$upstart_path" "$initctl_path"
  153. do
  154.   [ -f "$cmd" ] || die "Path $cmd does not exist"
  155.   [ -x "$cmd" ] || die "File $cmd not executable"
  156.   "$cmd" --help | grep -q -- --session || die "version of $cmd too old"
  157. done
  158.  
  159. # this is the only safe way to run another instance of Upstart
  160. "$upstart_path" --help|grep -q -- --no-startup-event || die "$upstart_path too old"
  161.  
  162. debug "confdir=$confdir"
  163. debug "file=$file"
  164.  
  165. filename=$(basename $file)
  166.  
  167. echo "$filename" | egrep -q '\.conf$' || die "file must end in .conf"
  168.  
  169. job="${filename%.conf}"
  170.  
  171. cp "$file" "$confdir"
  172. debug "job=$job"
  173.  
  174. upstart_out="$(mktemp --tmpdir "${script_name}-upstart-output.XXXXXXXXXX")"
  175. debug "upstart_out=$upstart_out"
  176.  
  177. upstart_cmd=$(printf \
  178.    "%s --session --no-sessions --no-startup-event --verbose --confdir %s" \
  179.   "$upstart_path" \
  180.   "$confdir")
  181. debug "upstart_cmd=$upstart_cmd"
  182.  
  183. nohup $upstart_cmd >"$upstart_out" 2>&1 &
  184. upstart_pid=$!
  185.  
  186. # Stop the shell outputting a message when Upstart is killed.
  187. # We handle this ourselves in cleanup().
  188. disown 
  189.  
  190. # wait for Upstart to initialize
  191. for i in $(seq 1 5)
  192. do
  193.   debug "Waiting for Upstart to reply over D-Bus (attempt $i)"
  194.   dbus-send --session --print-reply \
  195.     --dest='com.ubuntu.Upstart' /com/ubuntu/Upstart \
  196.     org.freedesktop.DBus.Properties.GetAll \
  197.     string:'com.ubuntu.Upstart0_6' >/dev/null 2>&1
  198.   if [ $? -eq 0 ]
  199.   then
  200.     running=y
  201.     break
  202.   fi
  203.   sleep 1
  204. done
  205.  
  206. [ $running = n ] && die "failed to ask Upstart to check conf file"
  207.  
  208. debug "Secondary Upstart ($upstart_cmd) running with PID $upstart_pid"
  209.  
  210. if [ "$check_scripts" = y ]
  211. then
  212.   for section in pre-start post-start script pre-stop post-stop
  213.   do
  214.     if egrep -q "\<${section}\>" "$file"
  215.     then
  216.       cmd='sed -n "/^ *${section}/,/^ *end script/p" $file | /bin/sh -n 2>&1'
  217.       errors=$(eval "$cmd")
  218.       [ $? -ne 0 ] && \
  219.         die "$(printf "File $file: shell syntax invalid in $section section:\n${errors}")"
  220.     fi
  221.   done
  222. fi
  223.  
  224. if "$initctl_path" --session status "$job" >/dev/null 2>&1
  225. then
  226.   file_valid=y
  227.   echo "File $file: syntax ok"
  228.   exit 0
  229. fi
  230.  
  231. errors=$(grep "$job" "$upstart_out"|sed "s,${confdir}/,,g")
  232. die "$(printf "File $file: syntax invalid:\n${errors}")"
  233.