Merge lp:~jamesodhunt/upstart/use-session-init-for-init-checkconf into lp:upstart

Proposed by James Hunt
Status: Merged
Merged at revision: 1600
Proposed branch: lp:~jamesodhunt/upstart/use-session-init-for-init-checkconf
Merge into: lp:upstart
Diff against target: 331 lines (+106/-93)
3 files modified
ChangeLog (+7/-0)
scripts/init-checkconf.sh (+95/-87)
scripts/man/init-checkconf.8 (+4/-6)
To merge this branch: bzr merge lp:~jamesodhunt/upstart/use-session-init-for-init-checkconf
Reviewer Review Type Date Requested Status
Upstart Reviewers Pending
Review via email: mp+195667@code.launchpad.net

Description of the change

Making the Session Init connect to the D-Bus session bus on request ('initctl notify-dbus-address') breaks init-checkconf.

This branch simplifies init-checkconf to not rely on D-bus, but instead spawn a Session Init.

I'll raise a follow-on MP that will add tests for the Upstart scripts to ensure they are not inadvertently broken in the future.

To post a comment you must log in.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'ChangeLog'
--- ChangeLog 2013-11-18 10:31:21 +0000
+++ ChangeLog 2013-11-18 20:18:10 +0000
@@ -1,3 +1,10 @@
12013-11-18 James Hunt <james.hunt@ubuntu.com>
2
3 * scripts/init-checkconf.sh:
4 - Don't rely on D-Bus any more: spawn a Session Init instead since
5 this is simpler and allows the two major limitations to be dropped.
6 * scripts/man/init-checkconf.8: Updated date and limitations section.
7
12013-11-16 Dmitrijs Ledkovs <xnox@ubuntu.com>82013-11-16 Dmitrijs Ledkovs <xnox@ubuntu.com>
29
3 * init/xdg.c, util/Makefile.am, test/Makefile.am, init/conf.c:10 * init/xdg.c, util/Makefile.am, test/Makefile.am, init/conf.c:
411
=== modified file 'scripts/init-checkconf.sh'
--- scripts/init-checkconf.sh 2013-01-23 14:28:07 +0000
+++ scripts/init-checkconf.sh 2013-11-18 20:18:10 +0000
@@ -8,7 +8,7 @@
8#8#
9#---------------------------------------------------------------------9#---------------------------------------------------------------------
10#10#
11# Copyright (C) 2011 Canonical Ltd.11# Copyright (C) 2011-2013 Canonical Ltd.
12#12#
13# Author: James Hunt <james.hunt@canonical.com>13# Author: James Hunt <james.hunt@canonical.com>
14#14#
@@ -28,34 +28,38 @@
2828
29script_name=${0##*/}29script_name=${0##*/}
30confdir=$(mktemp -d /tmp/${script_name}.XXXXXXXXXX)30confdir=$(mktemp -d /tmp/${script_name}.XXXXXXXXXX)
31xdg_runtime_dir=$(mktemp -d /tmp/${script_name}.XXXXXXXXXX)
31upstart_path=/sbin/init32upstart_path=/sbin/init
32initctl_path=/sbin/initctl33initctl_path=/sbin/initctl
33debug_enabled=n34debug_enabled=n
34file_valid=n35file_valid=n
35running=n36running=n
37set_session=n
36check_scripts=y38check_scripts=y
37dbus_cmd=dbus-launch
38started_dbus=n
3939
40cleanup()40cleanup()
41{41{
42 if [ ! -z "$upstart_pid" ]42 # Restore
43 then43 [ -n "$saved_xdg_runtime_dir" ] && \
44 debug "stopping secondary Upstart (running with PID $upstart_pid)"44 debug "Restoring XDG_RUNTIME_DIR to '$saved_xdg_runtime_dir'"
45 kill -0 "$upstart_pid" >/dev/null 2>&1 && \45 export XDG_RUNTIME_DIR="$saved_xdg_runtime_dir"
46 kill -9 "$upstart_pid" >/dev/null 2>&146
47 fi47 [ -n "$saved_upstart_session" ] && \
4848 debug "Restoring UPSTART_SESSION to '$saved_upstart_session'"
49 if [ "$started_dbus" = y ] && [ -n "$DBUS_SESSION_BUS_PID" ]49 export UPSTART_SESSION="$saved_upstart_session"
50 then50
51 debug "stopping dbus-daemon (running with PID $DBUS_SESSION_BUS_PID)"51 if [ ! -z "$upstart_pid" ]
52 kill -0 "$DBUS_SESSION_BUS_PID" >/dev/null 2>&1 && \52 then
53 kill -9 "$DBUS_SESSION_BUS_PID" >/dev/null 2>&153 debug "Stopping secondary Upstart (running with PID $upstart_pid)"
54 fi54 kill -9 "$upstart_pid" >/dev/null 2>&1
5555 fi
56 [ -d "$confdir" ] && rm -rf "$confdir"56
57 [ $file_valid = y ] && exit 057 [ -d "$confdir" ] && rm -rf "$confdir"
58 exit 158 dir="$xdg_runtime_dir/upstart/sessions"
59 [ -d "$dir" ] && rm -rf "$xdg_runtime_dir"
60 [ "$file_valid" = y ] && exit 0
61
62 exit 1
59}63}
6064
61usage()65usage()
@@ -84,39 +88,36 @@
8488
85debug()89debug()
86{90{
87 msg="$*"91 msg="$*"
88 [ $debug_enabled = y ] && echo "DEBUG: $msg"92 [ $debug_enabled = y ] && echo "DEBUG: $msg"
89}93}
9094
91error()95error()
92{96{
93 msg="$*"97 msg="$*"
94 printf "ERROR: %s\n" "$msg" >&298 printf "ERROR: %s\n" "$msg" >&2
95}99}
96100
97die()101die()
98{102{
99 error "$*"103 error "$*"
100 exit 1104 exit 1
101}105}
102106
103# Return 0 if Upstart is running on the D-Bus session bus, else 1.107# Return 0 if Upstart is running, else 1
104upstart_running()108upstart_running()
105{109{
106 dbus-send --session --print-reply \110 initctl --user version >/dev/null 2>&1
107 --dest='com.ubuntu.Upstart' /com/ubuntu/Upstart \
108 org.freedesktop.DBus.Properties.GetAll \
109 string:'com.ubuntu.Upstart0_6' >/dev/null 2>&1
110}111}
111112
112trap cleanup EXIT INT TERM113trap cleanup EXIT INT TERM
113114
114args=$(getopt \115args=$(getopt \
115 -n "$script_name" \116 -n "$script_name" \
116 -a \117 -a \
117 --options="df:hi:sx:" \118 --options="df:hi:sx:" \
118 --longoptions="debug file: help initctl-path: noscript upstart-path:" \119 --longoptions="debug file: help initctl-path: noscript upstart-path:" \
119 -- "$@")120 -- "$@")
120121
121eval set -- "$args"122eval set -- "$args"
122[ $? -ne 0 ] && { usage; exit 1; }123[ $? -ne 0 ] && { usage; exit 1; }
@@ -162,23 +163,28 @@
162done163done
163164
164[ -z "$file" ] && file="$1"165[ -z "$file" ] && file="$1"
165166[ -z "$file" ] && die "Must specify configuration file"
166# safety first167[ ! -f "$file" ] && die "File $file does not exist"
167[ "$(id -u)" -eq 0 ] && die "cannot run as root"
168
169[ -z "$file" ] && die "must specify configuration file"
170[ ! -f "$file" ] && die "file $file does not exist"
171168
172debug "upstart_path=$upstart_path"169debug "upstart_path=$upstart_path"
173debug "initctl_path=$initctl_path"170debug "initctl_path=$initctl_path"
174171
175for cmd in "$upstart_path" "$initctl_path"172for cmd in "$upstart_path" "$initctl_path"
176do173do
177 [ -f "$cmd" ] || die "Path $cmd does not exist"174 [ -f "$cmd" ] || die "Path $cmd does not exist"
178 [ -x "$cmd" ] || die "File $cmd not executable"175 [ -x "$cmd" ] || die "File $cmd not executable"
179 "$cmd" --help | grep -q -- --session || die "version of $cmd too old"176 "$cmd" --help | grep -q -- --user || die "version of $cmd too old"
180done177done
181178
179export saved_xdg_runtime_dir="$XDG_RUNTIME_DIR"
180debug "Setting XDG_RUNTIME_DIR='$xdg_runtime_dir'"
181export XDG_RUNTIME_DIR="$xdg_runtime_dir"
182
183export saved_upstart_session="$UPSTART_SESSION"
184[ -n "$UPSTART_SESSION" ] \
185 && debug "Unsetting UPSTART_SESSION ($UPSTART_SESSION)" \
186 && unset UPSTART_SESSION
187
182# this is the only safe way to run another instance of Upstart188# this is the only safe way to run another instance of Upstart
183"$upstart_path" --help|grep -q -- --no-startup-event || die "$upstart_path too old"189"$upstart_path" --help|grep -q -- --no-startup-event || die "$upstart_path too old"
184190
@@ -187,79 +193,81 @@
187193
188filename=$(basename $file)194filename=$(basename $file)
189195
190echo "$filename" | egrep -q '\.conf$' || die "file must end in .conf"196echo "$filename" | egrep -q '\.conf$' || die "File must end in .conf"
191197
192job="${filename%.conf}"198job="${filename%.conf}"
193199
194cp "$file" "$confdir" || die "failed to copy file $file to $confdir"200cp "$file" "$confdir" || die "Failed to copy file $file to $confdir"
195debug "job=$job"201debug "job=$job"
196202
197upstart_running
198[ $? -eq 0 ] && die "Another instance of this program is already running"
199debug "ok - no other running instances detected"
200
201upstart_out="$(mktemp --tmpdir "${script_name}-upstart-output.XXXXXXXXXX")"203upstart_out="$(mktemp --tmpdir "${script_name}-upstart-output.XXXXXXXXXX")"
202debug "upstart_out=$upstart_out"204debug "upstart_out=$upstart_out"
203205
204# auto-start dbus if it isn't already running (required in non-desktop
205# environments).
206if [ -z "$DBUS_SESSION_BUS_ADDRESS" ]; then
207 [ -z "$(which $dbus_cmd)" ] && die "cannot find $dbus_cmd"
208 eval $($dbus_cmd --auto-syntax)
209 started_dbus=y
210 debug "started $dbus_cmd"
211fi
212
213upstart_cmd=$(printf \206upstart_cmd=$(printf \
214 "%s --session --no-sessions --no-startup-event --verbose --confdir %s" \207 "%s --user --no-dbus --no-sessions --no-startup-event --verbose --confdir %s" \
215 "$upstart_path" \208 "$upstart_path" \
216 "$confdir")209 "$confdir")
217debug "upstart_cmd=$upstart_cmd"210debug "upstart_cmd=$upstart_cmd"
218211
219nohup $upstart_cmd >"$upstart_out" 2>&1 &212nohup $upstart_cmd >"$upstart_out" 2>&1 &
220upstart_pid=$!213upstart_pid=$!
214debug "Upstart pid=$upstart_pid"
221215
222# Stop the shell outputting a message when Upstart is killed.216# Stop the shell outputting a message when Upstart is killed.
223# We handle this ourselves in cleanup().217# We handle this ourselves in cleanup().
224disown 218disown
225219
226# wait for Upstart to initialize220# wait for Upstart to initialise
227for i in $(seq 1 5)221for i in $(seq 1 5)
228do222do
229 debug "Waiting for Upstart to reply over D-Bus (attempt $i)"223 sessions=$("$initctl_path" list-sessions)
230 upstart_running224
231 if [ $? -eq 0 ]225 if [ "$set_session" = n ] && [ -n "$sessions" ]
232 then226 then
233 running=y227 count=$(echo "$sessions"|wc -l)
234 break228 [ "$count" -gt 1 ] && die "Got unexpected session count: $count"
235 fi229 session=$(echo "$sessions"|awk '{print $2}')
236 sleep 1230 debug "Joining Upstart session '$session'"
231 export UPSTART_SESSION="$session"
232 set_session=y
233 fi
234
235 debug "Waiting for Upstart to initialise (attempt $i)"
236
237 upstart_running
238 if [ $? -eq 0 ]
239 then
240 running=y
241 break
242 fi
243
244 sleep 1
237done245done
238246
239[ $running = n ] && die "failed to ask Upstart to check conf file"247[ $running = n ] && die "Failed to ask Upstart to check conf file"
240248
241debug "Secondary Upstart ($upstart_cmd) running with PID $upstart_pid"249debug "Secondary Upstart ($upstart_cmd) running with PID $upstart_pid"
242250
243if [ "$check_scripts" = y ]251if [ "$check_scripts" = y ]
244then252then
245 for section in pre-start post-start script pre-stop post-stop253 for section in pre-start post-start script pre-stop post-stop
246 do254 do
247 if egrep -q "\<${section}\>" "$file"255 if egrep -q "\<${section}\>" "$file"
248 then256 then
249 cmd='sed -n "/^ *${section}/,/^ *end script/p" $file | /bin/sh -n 2>&1'257 cmd='sed -n "/^ *${section}/,/^ *end script/p" $file | /bin/sh -n 2>&1'
250 errors=$(eval "$cmd")258 errors=$(eval "$cmd")
251 [ $? -ne 0 ] && \259 [ $? -ne 0 ] && \
252 die "$(printf "File $file: shell syntax invalid in $section section:\n${errors}")"260 die "$(printf "File $file: shell syntax invalid in $section section:\n${errors}")"
253 fi261 fi
254 done262 done
255fi263fi
256264
257"$initctl_path" --session list|grep -q "^${job}"265"$initctl_path" --user list|grep -q "^${job}"
258if [ $? -eq 0 ]266if [ $? -eq 0 ]
259then267then
260 file_valid=y268 file_valid=y
261 echo "File $file: syntax ok"269 echo "File $file: syntax ok"
262 exit 0270 exit 0
263fi271fi
264272
265errors=$(grep "$job" "$upstart_out"|sed "s,${confdir}/,,g")273errors=$(grep "$job" "$upstart_out"|sed "s,${confdir}/,,g")
266274
=== modified file 'scripts/man/init-checkconf.8'
--- scripts/man/init-checkconf.8 2011-06-01 14:57:41 +0000
+++ scripts/man/init-checkconf.8 2013-11-18 20:18:10 +0000
@@ -1,4 +1,4 @@
1.TH init\-checkconf 8 2011-04-06 "Upstart"1.TH init\-checkconf 8 2013-11-19 "Upstart"
2.\"2.\"
3.SH NAME3.SH NAME
4init\-checkconf \- manual page for init-checkconf4init\-checkconf \- manual page for init-checkconf
@@ -52,17 +52,15 @@
52.\"52.\"
53.SH LIMITATIONS53.SH LIMITATIONS
54.IP \(bu 454.IP \(bu 4
55This program will not run as the root user.55.B exec
56.IP \(bu 456stanzas containing shell meta-characters will not be checked as scripts.
57It is not possible for a user to run multiple simultaneous
58instances of this program.
59.\"57.\"
60.SH REPORTING BUGS58.SH REPORTING BUGS
61Report bugs at59Report bugs at
62.RB < https://launchpad.net/upstart/+bugs >60.RB < https://launchpad.net/upstart/+bugs >
63.\"61.\"
64.SH COPYRIGHT62.SH COPYRIGHT
65Copyright \(co 2011 Canonical Ltd.63Copyright \(co 2011-2013 Canonical Ltd.
66.br64.br
67This is free software; see the source for copying conditions. There is NO65This is free software; see the source for copying conditions. There is NO
68warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.66warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

Subscribers

People subscribed via source and target branches