2
#---------------------------------------------------------------------
3
# Script to determine if specified config file is valid or not.
4
# By default, two checks are performed:
6
# - Ensure Upstart can parse overall file successfully
7
# - Ensure all script sections are parseable by shell
9
#---------------------------------------------------------------------
11
# Copyright (C) 2011 Canonical Ltd.
13
# Author: James Hunt <james.hunt@canonical.com>
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.
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.
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/>.
27
#---------------------------------------------------------------------
30
confdir=$(mktemp -d /tmp/${script_name}.XXXXXXXXXX)
31
upstart_path=/sbin/init
32
initctl_path=/sbin/initctl
40
if [ ! -z "$upstart_pid" ]
42
debug "stopping secondary Upstart (running with PID $upstart_pid)"
43
kill -0 "$upstart_pid" >/dev/null 2>&1 && \
44
kill -9 "$upstart_pid" >/dev/null 2>&1
47
[ -d "$confdir" ] && rm -rf "$confdir"
48
[ $file_valid = y ] && exit 0
55
Description: Determine if specified Upstart (init(8)) job configuration
58
Usage: $script_name [options] -f <conf_file>
59
$script_name [options] <conf_file>
63
-d, --debug : Show some debug output.
64
-f <file>, : Job configuration file to check.
65
--file=<file> (no default).
66
-i <path>, : Specify path to initctl binary
67
--initctl-path=<path> (default=$initctl_path).
68
-s, --noscript : Do not check script sections.
69
-x <path> : Specify path to init daemon binary
70
--upstart-path=<path> (default=$upstart_path).
71
-h, --help : Show this help.
79
[ $debug_enabled = y ] && echo "DEBUG: $msg"
85
printf "ERROR: %s\n" "$msg" >&2
94
# Return 0 if Upstart is running on the D-Bus session bus, else 1.
97
dbus-send --session --print-reply \
98
--dest='com.ubuntu.Upstart' /com/ubuntu/Upstart \
99
org.freedesktop.DBus.Properties.GetAll \
100
string:'com.ubuntu.Upstart0_6' >/dev/null 2>&1
103
trap cleanup EXIT INT TERM
108
--options="df:hi:sx:" \
109
--longoptions="debug file: help initctl-path: noscript upstart-path:" \
113
[ $? -ne 0 ] && { usage; exit 1; }
114
[ $# -eq 0 ] && { usage; exit 0; }
155
[ -z "$file" ] && file="$1"
158
[ "$(id -u)" -eq 0 ] && die "cannot run as root"
160
[ -z "$file" ] && die "must specify configuration file"
161
[ ! -f "$file" ] && die "file $file does not exist"
163
debug "upstart_path=$upstart_path"
164
debug "initctl_path=$initctl_path"
166
for cmd in "$upstart_path" "$initctl_path"
168
[ -f "$cmd" ] || die "Path $cmd does not exist"
169
[ -x "$cmd" ] || die "File $cmd not executable"
170
"$cmd" --help | grep -q -- --session || die "version of $cmd too old"
173
# this is the only safe way to run another instance of Upstart
174
"$upstart_path" --help|grep -q -- --no-startup-event || die "$upstart_path too old"
176
debug "confdir=$confdir"
179
filename=$(basename $file)
181
echo "$filename" | egrep -q '\.conf$' || die "file must end in .conf"
183
job="${filename%.conf}"
185
cp "$file" "$confdir"
189
[ $? -eq 0 ] && die "Another instance of this program is already running"
190
debug "ok - no other running instances detected"
192
upstart_out="$(mktemp --tmpdir "${script_name}-upstart-output.XXXXXXXXXX")"
193
debug "upstart_out=$upstart_out"
195
upstart_cmd=$(printf \
196
"%s --session --no-sessions --no-startup-event --verbose --confdir %s" \
199
debug "upstart_cmd=$upstart_cmd"
201
nohup $upstart_cmd >"$upstart_out" 2>&1 &
204
# Stop the shell outputting a message when Upstart is killed.
205
# We handle this ourselves in cleanup().
208
# wait for Upstart to initialize
211
debug "Waiting for Upstart to reply over D-Bus (attempt $i)"
221
[ $running = n ] && die "failed to ask Upstart to check conf file"
223
debug "Secondary Upstart ($upstart_cmd) running with PID $upstart_pid"
225
if [ "$check_scripts" = y ]
227
for section in pre-start post-start script pre-stop post-stop
229
if egrep -q "\<${section}\>" "$file"
231
cmd='sed -n "/^ *${section}/,/^ *end script/p" $file | /bin/sh -n 2>&1'
232
errors=$(eval "$cmd")
234
die "$(printf "File $file: shell syntax invalid in $section section:\n${errors}")"
239
"$initctl_path" --session list|grep -q "^${job}"
243
echo "File $file: syntax ok"
247
errors=$(grep "$job" "$upstart_out"|sed "s,${confdir}/,,g")
248
die "$(printf "File $file: syntax invalid:\n${errors}")"