Convert "reboot/halt" events

svn path=/nixos/branches/fix-style/; revision=14407
This commit is contained in:
Marc Weber 2009-03-06 12:27:47 +00:00
parent 76f7978cc6
commit 8cad533a76
3 changed files with 113 additions and 104 deletions

View File

@ -427,6 +427,7 @@ in
(import ../upstart-jobs/nscd.nix) # Name service cache daemon. (import ../upstart-jobs/nscd.nix) # Name service cache daemon.
(import ../upstart-jobs/maintenance-shell.nix) # Handles the maintenance/stalled event (single-user shell). (import ../upstart-jobs/maintenance-shell.nix) # Handles the maintenance/stalled event (single-user shell).
(import ../upstart-jobs/ctrl-alt-delete.nix) # Ctrl-alt-delete action. (import ../upstart-jobs/ctrl-alt-delete.nix) # Ctrl-alt-delete action.
(import ../upstart-jobs/halt.nix) # FIXME (assertion) # Handles the reboot/halt events.
# security # security

View File

@ -77,15 +77,6 @@ let
inherit config; inherit config;
}) })
# Handles the reboot/halt events.
++ (map
(event: makeJob (import ../upstart-jobs/halt.nix {
inherit (pkgs) bash utillinux;
inherit event;
}))
["reboot" "halt" "system-halt" "power-off"]
)
# User-defined events. # User-defined events.
++ (map makeJob (config.services.extraJobs)); ++ (map makeJob (config.services.extraJobs));

View File

@ -1,29 +1,38 @@
{bash, event, utillinux}: {pkgs, config, ...}:
###### implementation
/* FIXME
assert event == "reboot" assert event == "reboot"
|| event == "halt" || event == "halt"
|| event == "system-halt" || event == "system-halt"
|| event == "power-off"; || event == "power-off";
*/
{ let
inherit (pkgs) bash utillinux;
jobFun = event : {
name = "sys-" + event; name = "sys-" + event;
job = " job = ''
start on ${event} start on ${event}
script script
set +e # continue in case of errors set +e # continue in case of errors
exec < /dev/tty1 > /dev/tty1 2>&1 exec < /dev/tty1 > /dev/tty1 2>&1
echo \"\" echo ""
echo \"<<< SYSTEM SHUTDOWN >>>\" echo "<<< SYSTEM SHUTDOWN >>>"
echo \"\" echo ""
export PATH=${utillinux}/bin:${utillinux}/sbin:$PATH export PATH=${utillinux}/bin:${utillinux}/sbin:$PATH
# Set the hardware clock to the system time. # Set the hardware clock to the system time.
echo \"Setting the hardware clock...\" echo "Setting the hardware clock..."
hwclock --systohc --utc || true hwclock --systohc --utc || true
@ -32,53 +41,53 @@ script
# Kill all remaining processes except init and this one. # Kill all remaining processes except init and this one.
echo \"Sending the TERM signal to all processes...\" echo "Sending the TERM signal to all processes..."
kill -TERM -1 || true kill -TERM -1 || true
sleep 1 # wait briefly sleep 1 # wait briefly
echo \"Sending the KILL signal to all processes...\" echo "Sending the KILL signal to all processes..."
kill -KILL -1 || true kill -KILL -1 || true
# Unmount helper functions. # Unmount helper functions.
getMountPoints() { getMountPoints() {
cat /proc/mounts \\ cat /proc/mounts \
| grep -v '^rootfs' \\ | grep -v '^rootfs' \
| sed 's|^[^ ]\\+ \\+\\([^ ]\\+\\).*|\\1|' \\ | sed 's|^[^ ]\+ \+\([^ ]\+\).*|\1|' \
| grep -v '/proc\\|/sys\\|/dev' | grep -v '/proc\|/sys\|/dev'
} }
getDevice() { getDevice() {
local mountPoint=$1 local mountPoint=$1
cat /proc/mounts \\ cat /proc/mounts \
| grep -v '^rootfs' \\ | grep -v '^rootfs' \
| grep \"^[^ ]\\+ \\+$mountPoint \\+\" \\ | grep "^[^ ]\+ \+$mountPoint \+" \
| sed 's|^\\([^ ]\\+\\).*|\\1|' | sed 's|^\([^ ]\+\).*|\1|'
} }
# Unmount file systems. We repeat this until no more file systems # Unmount file systems. We repeat this until no more file systems
# can be unmounted. This is to handle loopback devices, file # can be unmounted. This is to handle loopback devices, file
# systems mounted on other file systems and so on. # systems mounted on other file systems and so on.
tryAgain=1 tryAgain=1
while test -n \"$tryAgain\"; do while test -n "$tryAgain"; do
tryAgain= tryAgain=
for mp in $(getMountPoints); do for mp in $(getMountPoints); do
device=$(getDevice $mp) device=$(getDevice $mp)
echo \"unmounting $mp...\" echo "unmounting $mp..."
if umount -f -n \"$mp\"; then if umount -f -n "$mp"; then
if test \"$mp\" != /; then tryAgain=1; fi if test "$mp" != /; then tryAgain=1; fi
else else
mount -n -o remount,ro \"$mp\" || true mount -n -o remount,ro "$mp" || true
fi fi
# Hack: work around a bug in mount (mount -o remount on a # Hack: work around a bug in mount (mount -o remount on a
# loop device forgets the loop=/dev/loopN entry in # loop device forgets the loop=/dev/loopN entry in
# /etc/mtab). # /etc/mtab).
if echo \"$device\" | grep -q '/dev/loop'; then if echo "$device" | grep -q '/dev/loop'; then
echo \"removing loop device $device...\" echo "removing loop device $device..."
losetup -d \"$device\" || true losetup -d "$device" || true
fi fi
done done
done done
@ -97,7 +106,15 @@ script
exec halt -f -p exec halt -f -p
fi fi
end script end script
"; '';
};
in
{
services = {
extraJobs = map jobFun ["reboot" "halt" "system-halt" "power-off"];
};
} }