29
29
script_name=${0##*/}
30
30
confdir=$(mktemp -d /tmp/${script_name}.XXXXXXXXXX)
31
xdg_runtime_dir=$(mktemp -d /tmp/${script_name}.XXXXXXXXXX)
31
32
upstart_path=/sbin/init
32
33
initctl_path=/sbin/initctl
42
if [ ! -z "$upstart_pid" ]
44
debug "stopping secondary Upstart (running with PID $upstart_pid)"
45
kill -0 "$upstart_pid" >/dev/null 2>&1 && \
46
kill -9 "$upstart_pid" >/dev/null 2>&1
49
if [ "$started_dbus" = y ] && [ -n "$DBUS_SESSION_BUS_PID" ]
51
debug "stopping dbus-daemon (running with PID $DBUS_SESSION_BUS_PID)"
52
kill -0 "$DBUS_SESSION_BUS_PID" >/dev/null 2>&1 && \
53
kill -9 "$DBUS_SESSION_BUS_PID" >/dev/null 2>&1
56
[ -d "$confdir" ] && rm -rf "$confdir"
57
[ $file_valid = y ] && exit 0
43
[ -n "$saved_xdg_runtime_dir" ] && \
44
debug "Restoring XDG_RUNTIME_DIR to '$saved_xdg_runtime_dir'"
45
export XDG_RUNTIME_DIR="$saved_xdg_runtime_dir"
47
[ -n "$saved_upstart_session" ] && \
48
debug "Restoring UPSTART_SESSION to '$saved_upstart_session'"
49
export UPSTART_SESSION="$saved_upstart_session"
51
if [ ! -z "$upstart_pid" ]
53
debug "Stopping secondary Upstart (running with PID $upstart_pid)"
54
kill -9 "$upstart_pid" >/dev/null 2>&1
57
[ -d "$confdir" ] && rm -rf "$confdir"
58
dir="$xdg_runtime_dir/upstart/sessions"
59
[ -d "$dir" ] && rm -rf "$xdg_runtime_dir"
60
[ "$file_valid" = y ] && exit 0
88
[ $debug_enabled = y ] && echo "DEBUG: $msg"
92
[ $debug_enabled = y ] && echo "DEBUG: $msg"
94
printf "ERROR: %s\n" "$msg" >&2
98
printf "ERROR: %s\n" "$msg" >&2
103
# Return 0 if Upstart is running on the D-Bus session bus, else 1.
107
# Return 0 if Upstart is running, else 1
104
108
upstart_running()
106
dbus-send --session --print-reply \
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
initctl --user version >/dev/null 2>&1
112
113
trap cleanup EXIT INT TERM
117
--options="df:hi:sx:" \
118
--longoptions="debug file: help initctl-path: noscript upstart-path:" \
118
--options="df:hi:sx:" \
119
--longoptions="debug file: help initctl-path: noscript upstart-path:" \
121
122
eval set -- "$args"
122
123
[ $? -ne 0 ] && { usage; exit 1; }
164
165
[ -z "$file" ] && file="$1"
167
[ "$(id -u)" -eq 0 ] && die "cannot run as root"
169
[ -z "$file" ] && die "must specify configuration file"
170
[ ! -f "$file" ] && die "file $file does not exist"
166
[ -z "$file" ] && die "Must specify configuration file"
167
[ ! -f "$file" ] && die "File $file does not exist"
172
169
debug "upstart_path=$upstart_path"
173
170
debug "initctl_path=$initctl_path"
175
172
for cmd in "$upstart_path" "$initctl_path"
177
[ -f "$cmd" ] || die "Path $cmd does not exist"
178
[ -x "$cmd" ] || die "File $cmd not executable"
179
"$cmd" --help | grep -q -- --session || die "version of $cmd too old"
174
[ -f "$cmd" ] || die "Path $cmd does not exist"
175
[ -x "$cmd" ] || die "File $cmd not executable"
176
"$cmd" --help | grep -q -- --user || die "version of $cmd too old"
179
export saved_xdg_runtime_dir="$XDG_RUNTIME_DIR"
180
debug "Setting XDG_RUNTIME_DIR='$xdg_runtime_dir'"
181
export XDG_RUNTIME_DIR="$xdg_runtime_dir"
183
export saved_upstart_session="$UPSTART_SESSION"
184
[ -n "$UPSTART_SESSION" ] \
185
&& debug "Unsetting UPSTART_SESSION ($UPSTART_SESSION)" \
186
&& unset UPSTART_SESSION
182
188
# this is the only safe way to run another instance of Upstart
183
189
"$upstart_path" --help|grep -q -- --no-startup-event || die "$upstart_path too old"
188
194
filename=$(basename $file)
190
echo "$filename" | egrep -q '\.conf$' || die "file must end in .conf"
196
echo "$filename" | egrep -q '\.conf$' || die "File must end in .conf"
192
198
job="${filename%.conf}"
194
cp "$file" "$confdir" || die "failed to copy file $file to $confdir"
200
cp "$file" "$confdir" || die "Failed to copy file $file to $confdir"
198
[ $? -eq 0 ] && die "Another instance of this program is already running"
199
debug "ok - no other running instances detected"
201
203
upstart_out="$(mktemp --tmpdir "${script_name}-upstart-output.XXXXXXXXXX")"
202
204
debug "upstart_out=$upstart_out"
204
# auto-start dbus if it isn't already running (required in non-desktop
206
if [ -z "$DBUS_SESSION_BUS_ADDRESS" ]; then
207
[ -z "$(which $dbus_cmd)" ] && die "cannot find $dbus_cmd"
208
eval $($dbus_cmd --auto-syntax)
210
debug "started $dbus_cmd"
213
206
upstart_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" \
217
210
debug "upstart_cmd=$upstart_cmd"
219
212
nohup $upstart_cmd >"$upstart_out" 2>&1 &
214
debug "Upstart pid=$upstart_pid"
222
216
# Stop the shell outputting a message when Upstart is killed.
223
217
# We handle this ourselves in cleanup().
226
# wait for Upstart to initialize
220
# wait for Upstart to initialise
227
221
for i in $(seq 1 5)
229
debug "Waiting for Upstart to reply over D-Bus (attempt $i)"
223
sessions=$("$initctl_path" list-sessions)
225
if [ "$set_session" = n ] && [ -n "$sessions" ]
227
count=$(echo "$sessions"|wc -l)
228
[ "$count" -gt 1 ] && die "Got unexpected session count: $count"
229
session=$(echo "$sessions"|awk '{print $2}')
230
debug "Joining Upstart session '$session'"
231
export UPSTART_SESSION="$session"
235
debug "Waiting for Upstart to initialise (attempt $i)"
239
[ $running = n ] && die "failed to ask Upstart to check conf file"
247
[ $running = n ] && die "Failed to ask Upstart to check conf file"
241
249
debug "Secondary Upstart ($upstart_cmd) running with PID $upstart_pid"
243
251
if [ "$check_scripts" = y ]
245
for section in pre-start post-start script pre-stop post-stop
247
if egrep -q "\<${section}\>" "$file"
249
cmd='sed -n "/^ *${section}/,/^ *end script/p" $file | /bin/sh -n 2>&1'
250
errors=$(eval "$cmd")
252
die "$(printf "File $file: shell syntax invalid in $section section:\n${errors}")"
253
for section in pre-start post-start script pre-stop post-stop
255
if egrep -q "\<${section}\>" "$file"
257
cmd='sed -n "/^ *${section}/,/^ *end script/p" $file | /bin/sh -n 2>&1'
258
errors=$(eval "$cmd")
260
die "$(printf "File $file: shell syntax invalid in $section section:\n${errors}")"
257
"$initctl_path" --session list|grep -q "^${job}"
265
"$initctl_path" --user list|grep -q "^${job}"
261
echo "File $file: syntax ok"
269
echo "File $file: syntax ok"
265
273
errors=$(grep "$job" "$upstart_out"|sed "s,${confdir}/,,g")