summaryrefslogtreecommitdiff
path: root/shutdownasap.in
blob: 8cf3210968d04c1a70905df6b79e5a9041bcf328 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#!/bin/bash

# shutdownasap version #VERSION#

set -e

. #ETCDIR#/shutdownasap.conf

mkdir -p "${tmpDir}"
cd "${tmpDir}"

quiet=false
intentionSet=false
while [ $# -gt 0 ]; do
  case "$1" in
    '-a')
      echo 'none' >"${tmpDir}/intention"
      exit
    ;;
    '-q')
      quiet=true
    ;;
    '-r')
      echo 'reboot' >"${tmpDir}/intention"
      intentionSet=true
    ;;
    '-R')
      if [ -r "${tmpDir}/intention" ] \
      && grep -qxF 'shutdown' "${tmpDir}/intention" \
      && [ -r "${tmpDir}/pid" ] \
      && kill -0 "$(cat "${tmpDir}/pid")"; then
        exit
      fi
      echo 'reboot' >"${tmpDir}/intention"
      intentionSet=true
    ;;
    *)
      >&2 echo 'usage: shutdownasap [-q] [-a|-r|-R]'
      exit 1
    ;;
  esac
  shift
done

if ! ${intentionSet}; then
  echo 'shutdown' >"${tmpDir}/intention"
fi

[ -r "${tmpDir}/pid" ] && kill -0 "$(cat "${tmpDir}/pid")" && exit

echo $$ > "${tmpDir}/pid"

capture_output() {
  local exit_code
  local output
  output=$(
    "$@" 2>&1
  ) \
  || exit_code=$?
  if ! ${quiet}; then
    printf '%s\n' "${output}"
  fi
  return ${exit_code}
}

beforeWatchHook

i=1
while [ ${i} -gt 0 ] || ! capture_output additionalWatchHookSlow
do
  if [ ${i} -eq 0 ]
  then
    i=1
  fi
  sleep 1
  prozesse=$(
    ps aux \
    | grep -v grep \
    | grep -vF 'rsync --daemon --no-detach'
  )

  nochwarten=false

  if [ -d "${waitForDir}" ] && ls -1A "${waitForDir}" | grep -q "."
  then
    nochwarten=true
    if ! ${quiet}; then
      echo ".warteauf: "
      ls -1A "${waitForDir}"
    fi
  fi

  if users | grep -q "."
  then
    nochwarten=true
    if ! ${quiet}; then
      echo -n "users: "
      users
    fi
  fi

  errors=$(
    [ -d /sys/devices/virtual/block/ ] \
    && find /sys/devices/virtual/block/ \
      -name 'array_state' \
      -exec grep -HvxF 'clean' {} + \
      , \
      -name 'sync_action' \
      -exec grep -HxF 'recover' {} + \
      , \
      -name 'degraded' \
      -exec grep -HvxF '0' {} +
  ) || true
  if [ -n "${errors}" ]; then
    nochwarten=true
    if ! ${quiet}; then
      printf 'unclean raid array(s):\n%s\n' "${errors}"
    fi
  fi

  for s in "${!shutDownNoGoProcesses[@]}"
  do
    if echo "${prozesse}" | grep -q "\(\s\|/\)${shutDownNoGoProcesses[${s}]}\(\$\|\s\)"
    then
      nochwarten=true
      if ! ${quiet}; then
        echo "prozess: ${s}"
      fi
    fi
  done

  for s in "${!shutDownNoGoFiles[@]}"
  do
    if [ -e "${shutDownNoGoFiles[${s}]}" ] && echo "${prozesse}" | grep -q "^\S\+\s\+$(cat "${shutDownNoGoFiles[${s}]}")\s"
    then
      nochwarten=true
      if ! ${quiet}; then
        echo "datei: ${s}"
      fi
    fi
  done

  if ! capture_output additionalWatchHookFast
  then
    nochwarten=true
    if ! ${quiet}; then
      echo "additionalWatchHookFast"
    fi
  fi

  if ${nochwarten}
  then
    if ! ${quiet}; then
      echo "warten ..."
    fi
    i=10
    continue
  else
    if ! ${quiet}; then
      echo "noch ${i} Sekunden"
    fi
  fi
  i=$[${i}-1]
done

capture_output beforeShutDownHook

if command -v sudo >/dev/null \
&& [ "$(whoami)" != 'root' ]; then
  pre='sudo'
else
  pre=''
fi

case "$(head -n1 "${tmpDir}/intention")" in
  'reboot')
    ${pre} /sbin/reboot
  ;;
  'shutdown')
    ${pre} /sbin/poweroff
  ;;
  'none')
    echo 'shutdown was aborted'
  ;;
  *)
    printf 'intention "%s" not implemented\n' "$(head -n1 "${tmpDir}/intention")"
    exit 1
  ;;
esac

cd /
rm -rf --one-file-system "${tmpDir}"