3 # script to shutdown a system running buildd in a controlled way
5 # Copyright (c) 2009 Peter Palfrader <peter@palfrader.org>
7 # Permission is hereby granted, free of charge, to any person obtaining
8 # a copy of this software and associated documentation files (the
9 # "Software"), to deal in the Software without restriction, including
10 # without limitation the rights to use, copy, modify, merge, publish,
11 # distribute, sublicense, and/or sell copies of the Software, and to
12 # permit persons to whom the Software is furnished to do so, subject to
13 # the following conditions:
15 # The above copyright notice and this permission notice shall be
16 # included in all copies or substantial portions of the Software.
18 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
22 # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23 # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24 # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 echo "Usage: $0 [-h] <reason>" >&2
32 echo " -h is for halt, not for help"
37 if [ "${1:-""}" == '-h' ] ; then
42 if [ "$#" != 1 ]; then
51 wall_counter=$((wall_counter-1))
52 if [ "$wall_counter" -le 0 ]; then
53 if [ "$halt" = 1 ]; then
54 echo "System will halt for $reason when buildd has stopped" | wall
56 echo "System will reboot for $reason when buildd has stopped" | wall
62 buildd_wait_and_reboot() {
63 echo -n "Waiting for buildd to shut down"
65 while test -e ~buildd/build/buildd.pid ; do
72 if pgrep -u buildd -x buildd ; then
73 echo "pidfile is gone, but buildd process still runs"
74 while pgrep -u buildd -x buildd ; do
82 if grep 'waiting-for-clean-shutdown' ~buildd/NO-DAEMON-PLEASE > /dev/null; then
83 sudo -u buildd sh -c 'echo delete-on-boot > ~buildd/NO-DAEMON-PLEASE'
85 if [ "$halt" = 1 ]; then
86 /sbin/shutdown -h 1 "$reason"
88 /sbin/shutdown -r 1 "$reason"
92 if [ "`id -u`" != 0 ]; then
93 echo "This probably wants running as root" >&2
96 if ! id buildd > /dev/null 2>&1; then
97 echo "Is there a buildd user?" >&2
100 if ! [ -d ~buildd ]; then
101 echo "No ~buildd?" >&2
105 if ! test -e ~buildd/NO-DAEMON-PLEASE; then
106 if [ -e ~buildd/build/buildd.pid ] ; then
107 echo "Touching ~buildd/NO-DAEMON-PLEASE ~buildd/EXIT-DAEMON-PLEASE"
108 sudo -u buildd touch ~buildd/NO-DAEMON-PLEASE ~buildd/EXIT-DAEMON-PLEASE
109 sudo -u buildd sh -c 'echo waiting-for-clean-shutdown > ~buildd/NO-DAEMON-PLEASE'
110 sudo chgrp -v adm ~buildd/NO-DAEMON-PLEASE
112 echo "Sending HUP to buildd"
113 sudo -u buildd kill -HUP `sudo cat ~buildd/build/buildd.pid`
115 echo "no-daemon-please does not exist, but there is no buildd.pid file either"
116 if pgrep -u buildd -x buildd ; then
117 echo "But there is a buildd running. Bad?"
122 echo "no-daemon-please already exists"
125 buildd_wait_and_reboot