~jamesodhunt/upstart/bug-1315060

« back to all changes in this revision

Viewing changes to scripts/init-checkconf.sh

  • Committer: Dimitri John Ledkov
  • Date: 2014-03-05 13:30:11 UTC
  • mfrom: (1575.1.1 upstart)
  • Revision ID: dimitri.ledkov@canonical.com-20140305133011-65ckjcam21zrouqt
mergeĀ lp:~jamesodhunt/upstart/use-session-init-for-init-checkconf

Show diffs side-by-side

added added

removed removed

Lines of Context:
8
8
#
9
9
#---------------------------------------------------------------------
10
10
#
11
 
# Copyright (C) 2011 Canonical Ltd.
 
11
# Copyright (C) 2011-2013 Canonical Ltd.
12
12
#
13
13
# Author: James Hunt <james.hunt@canonical.com>
14
14
#
28
28
 
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
33
34
debug_enabled=n
34
35
file_valid=n
35
36
running=n
 
37
set_session=n
36
38
check_scripts=y
37
 
dbus_cmd=dbus-launch
38
 
started_dbus=n
39
39
 
40
40
cleanup()
41
41
{
42
 
  if [ ! -z "$upstart_pid" ]
43
 
  then
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
47
 
  fi
48
 
 
49
 
  if [ "$started_dbus" = y ] && [ -n "$DBUS_SESSION_BUS_PID" ]
50
 
  then
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
54
 
  fi
55
 
 
56
 
  [ -d "$confdir" ] && rm -rf "$confdir"
57
 
  [ $file_valid = y ] && exit 0
58
 
  exit 1
 
42
    # Restore
 
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"
 
46
 
 
47
    [ -n "$saved_upstart_session" ] && \
 
48
        debug "Restoring UPSTART_SESSION to '$saved_upstart_session'"
 
49
    export UPSTART_SESSION="$saved_upstart_session"
 
50
  
 
51
    if [ ! -z "$upstart_pid" ]
 
52
    then
 
53
      debug "Stopping secondary Upstart (running with PID $upstart_pid)"
 
54
      kill -9 "$upstart_pid" >/dev/null 2>&1
 
55
    fi
 
56
  
 
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
 
61
  
 
62
    exit 1
59
63
}
60
64
 
61
65
usage()
84
88
 
85
89
debug()
86
90
{
87
 
  msg="$*"
88
 
  [ $debug_enabled = y ] && echo "DEBUG: $msg"
 
91
    msg="$*"
 
92
    [ $debug_enabled = y ] && echo "DEBUG: $msg"
89
93
}
90
94
 
91
95
error()
92
96
{
93
 
  msg="$*"
94
 
  printf "ERROR: %s\n" "$msg" >&2
 
97
    msg="$*"
 
98
    printf "ERROR: %s\n" "$msg" >&2
95
99
}
96
100
 
97
101
die()
98
102
{
99
 
  error "$*"
100
 
  exit 1
 
103
    error "$*"
 
104
    exit 1
101
105
}
102
106
 
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()
105
109
{
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
110
111
}
111
112
 
112
113
trap cleanup EXIT INT TERM
113
114
 
114
115
args=$(getopt \
115
 
  -n "$script_name" \
116
 
  -a \
117
 
  --options="df:hi:sx:" \
118
 
  --longoptions="debug file: help initctl-path: noscript upstart-path:" \
119
 
  -- "$@")
 
116
    -n "$script_name" \
 
117
    -a \
 
118
    --options="df:hi:sx:" \
 
119
    --longoptions="debug file: help initctl-path: noscript upstart-path:" \
 
120
    -- "$@")
120
121
 
121
122
eval set -- "$args"
122
123
[ $? -ne 0 ] && { usage; exit 1; }
162
163
done
163
164
 
164
165
[ -z "$file" ] && file="$1"
165
 
 
166
 
# safety first
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"
 
166
[ -z "$file" ] && die "Must specify configuration file"
 
167
[ ! -f "$file" ] && die "File $file does not exist"
171
168
 
172
169
debug "upstart_path=$upstart_path"
173
170
debug "initctl_path=$initctl_path"
174
171
 
175
172
for cmd in "$upstart_path" "$initctl_path"
176
173
do
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"
180
177
done
181
178
 
 
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"
 
182
 
 
183
export saved_upstart_session="$UPSTART_SESSION"
 
184
[ -n "$UPSTART_SESSION" ] \
 
185
    && debug "Unsetting UPSTART_SESSION ($UPSTART_SESSION)" \
 
186
    && unset UPSTART_SESSION
 
187
 
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"
184
190
 
187
193
 
188
194
filename=$(basename $file)
189
195
 
190
 
echo "$filename" | egrep -q '\.conf$' || die "file must end in .conf"
 
196
echo "$filename" | egrep -q '\.conf$' || die "File must end in .conf"
191
197
 
192
198
job="${filename%.conf}"
193
199
 
194
 
cp "$file" "$confdir" || die "failed to copy file $file to $confdir"
 
200
cp "$file" "$confdir" || die "Failed to copy file $file to $confdir"
195
201
debug "job=$job"
196
202
 
197
 
upstart_running
198
 
[ $? -eq 0 ] && die "Another instance of this program is already running"
199
 
debug "ok - no other running instances detected"
200
 
 
201
203
upstart_out="$(mktemp --tmpdir "${script_name}-upstart-output.XXXXXXXXXX")"
202
204
debug "upstart_out=$upstart_out"
203
205
 
204
 
# auto-start dbus if it isn't already running (required in non-desktop
205
 
# environments).
206
 
if [ -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"
211
 
fi
212
 
 
213
206
upstart_cmd=$(printf \
214
 
   "%s --session --no-sessions --no-startup-event --verbose --confdir %s" \
215
 
  "$upstart_path" \
216
 
  "$confdir")
 
207
    "%s --user --no-dbus --no-sessions --no-startup-event --verbose --confdir %s" \
 
208
        "$upstart_path" \
 
209
        "$confdir")
217
210
debug "upstart_cmd=$upstart_cmd"
218
211
 
219
212
nohup $upstart_cmd >"$upstart_out" 2>&1 &
220
213
upstart_pid=$!
 
214
debug "Upstart pid=$upstart_pid"
221
215
 
222
216
# Stop the shell outputting a message when Upstart is killed.
223
217
# We handle this ourselves in cleanup().
224
218
disown 
225
219
 
226
 
# wait for Upstart to initialize
 
220
# wait for Upstart to initialise
227
221
for i in $(seq 1 5)
228
222
do
229
 
  debug "Waiting for Upstart to reply over D-Bus (attempt $i)"
230
 
  upstart_running
231
 
  if [ $? -eq 0 ]
232
 
  then
233
 
    running=y
234
 
    break
235
 
  fi
236
 
  sleep 1
 
223
    sessions=$("$initctl_path" list-sessions)
 
224
 
 
225
    if [ "$set_session" = n ] && [ -n "$sessions" ]
 
226
    then
 
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"
 
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
237
245
done
238
246
 
239
 
[ $running = n ] && die "failed to ask Upstart to check conf file"
 
247
[ $running = n ] && die "Failed to ask Upstart to check conf file"
240
248
 
241
249
debug "Secondary Upstart ($upstart_cmd) running with PID $upstart_pid"
242
250
 
243
251
if [ "$check_scripts" = y ]
244
252
then
245
 
  for section in pre-start post-start script pre-stop post-stop
246
 
  do
247
 
    if egrep -q "\<${section}\>" "$file"
248
 
    then
249
 
      cmd='sed -n "/^ *${section}/,/^ *end script/p" $file | /bin/sh -n 2>&1'
250
 
      errors=$(eval "$cmd")
251
 
      [ $? -ne 0 ] && \
252
 
        die "$(printf "File $file: shell syntax invalid in $section section:\n${errors}")"
253
 
    fi
254
 
  done
 
253
    for section in pre-start post-start script pre-stop post-stop
 
254
    do
 
255
        if egrep -q "\<${section}\>" "$file"
 
256
        then
 
257
            cmd='sed -n "/^ *${section}/,/^ *end script/p" $file | /bin/sh -n 2>&1'
 
258
            errors=$(eval "$cmd")
 
259
            [ $? -ne 0 ] && \
 
260
                die "$(printf "File $file: shell syntax invalid in $section section:\n${errors}")"
 
261
        fi
 
262
    done
255
263
fi
256
264
 
257
 
"$initctl_path" --session list|grep -q "^${job}"
 
265
"$initctl_path" --user list|grep -q "^${job}"
258
266
if [ $? -eq 0 ]
259
267
then
260
 
  file_valid=y
261
 
  echo "File $file: syntax ok"
262
 
  exit 0
 
268
    file_valid=y
 
269
    echo "File $file: syntax ok"
 
270
    exit 0
263
271
fi
264
272
 
265
273
errors=$(grep "$job" "$upstart_out"|sed "s,${confdir}/,,g")