summaryrefslogtreecommitdiff
path: root/shutdownasap.in
blob: d131867eca39a4e77de34a9f34948b7c610dbb5f (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
#!/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
    ;;
    *)
      >&2 echo 'usage: shutdownasap [-q] [-a|-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"

beforeWatchHook

i=1
while [ ${i} -gt 0 ] || ! 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

  for synActFile in /sys/block/md*/md/sync_action
  do
    if [ -r "${synActFile}" ] && ! grep -q "^idle\$" "${synActFile}"
    then
      nochWarten=true
      if ! ${quiet}; then
        echo -n "raid ${synActFile}: "
        cat "${synActFile}"
      fi
    fi
  done

  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 ! 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

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}"