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
49 local home=$(getent passwd "$user" | awk -F: '{print $6}')
51 if ! test -e "$home"/NO-DAEMON-PLEASE; then
52 if [ -e "$home"/build/buildd.pid ] ; then
53 echo "Touching ${home}/NO-DAEMON-PLEASE ${home}/EXIT-DAEMON-PLEASE"
54 sudo -u "$user" touch "$home"/NO-DAEMON-PLEASE "$home"/EXIT-DAEMON-PLEASE
55 sudo -u "$user" sh -c "echo waiting-for-clean-shutdown > '${home}/'NO-DAEMON-PLEASE"
56 sudo chgrp -v adm "$home"/NO-DAEMON-PLEASE
58 echo "Sending HUP to $user:buildd"
59 sudo -u "$user" kill -HUP "$(sudo cat "${home}"/build/buildd.pid)"
61 echo "$user: no-daemon-please does not exist, but there is no buildd.pid file either"
62 if pgrep -u "$user" -x buildd ; then
63 echo "But there is a buildd running. Bad?"
68 echo "no-daemon-please already exists"
75 wall_counter=$((wall_counter-1))
76 if [ "$wall_counter" -le 0 ]; then
77 if [ "$halt" = 1 ]; then
78 echo "System will halt for $reason when buildd has stopped" | wall
80 echo "System will reboot for $reason when buildd has stopped" | wall
86 buildd_wait_and_reboot() {
87 echo -n "Waiting for buildd to shut down"
89 while test -e ~buildd/build/buildd.pid || test -e ~buildd2/build/buildd.pid; do
96 if pgrep -u buildd -x buildd || pgrep -u buildd2 - x buildd; then
97 echo "pidfile is gone, but buildd process still runs"
98 while pgrep -u buildd -x buildd ; do
106 if grep 'waiting-for-clean-shutdown' ~buildd/NO-DAEMON-PLEASE > /dev/null; then
107 sudo -u buildd sh -c 'echo delete-on-boot > ~buildd/NO-DAEMON-PLEASE'
109 if grep 'waiting-for-clean-shutdown' ~buildd2/NO-DAEMON-PLEASE > /dev/null; then
110 sudo -u buildd2 sh -c 'echo delete-on-boot > ~buildd2/NO-DAEMON-PLEASE'
112 if [ "$halt" = 1 ]; then
113 /sbin/shutdown -h 1 "$reason"
115 /sbin/shutdown -r 1 "$reason"
119 if [ "`id -u`" != 0 ]; then
120 echo "This probably wants running as root" >&2
123 if ! id buildd > /dev/null 2>&1; then
124 echo "Is there a buildd user?" >&2
127 if ! [ -d ~buildd ]; then
128 echo "No ~buildd?" >&2
133 [ -d ~buildd2 ] && touch_stuff buildd2
135 buildd_wait_and_reboot