hal/tools/linux Makefile.am, 1.17, 1.18 hal-luks-remove-linux, NONE, 1.1 hal-luks-remove.in-linux, NONE, 1.1 hal-luks-setup-linux, NONE, 1.1 hal-luks-teardown-linux, NONE, 1.1 hal-system-lcd-get-brightness-linux, NONE, 1.1 hal-system-lcd-set-brightness-linux, NONE, 1.1 hal-system-power-hibernate-linux, NONE, 1.1 hal-system-power-reboot-linux, NONE, 1.1 hal-system-power-set-power-save-linux, NONE, 1.1 hal-system-power-shutdown-linux, NONE, 1.1 hal-system-power-suspend-linux, NONE, 1.1 hal-system-storage-cleanup-mountpoints-linux, NONE, 1.1 hal-system-storage-eject-linux, NONE, 1.1 hal-system-storage-unmount-linux, NONE, 1.1

Joe Marcus Clarke marcus at kemper.freedesktop.org
Sun May 14 11:49:55 PDT 2006


Update of /cvs/hal/hal/tools/linux
In directory kemper:/tmp/cvs-serv17906/tools/linux

Modified Files:
	Makefile.am 
Added Files:
	hal-luks-remove-linux hal-luks-remove.in-linux 
	hal-luks-setup-linux hal-luks-teardown-linux 
	hal-system-lcd-get-brightness-linux 
	hal-system-lcd-set-brightness-linux 
	hal-system-power-hibernate-linux hal-system-power-reboot-linux 
	hal-system-power-set-power-save-linux 
	hal-system-power-shutdown-linux hal-system-power-suspend-linux 
	hal-system-storage-cleanup-mountpoints-linux 
	hal-system-storage-eject-linux 
	hal-system-storage-unmount-linux 
Log Message:
Split out the tools scripts into an OS-independent wrapper, and an OS-dependent
backend.  The backend selection with be based on the utsname.sysname value.
Also, add FreeBSD support to hal-storage-mount.c (code from
Jean-Yves Lefort <jylefort at FreeBSD.org>).

Reviewed by:	Artem Kachitchkine, David Zeuthen
Approved by:	David Zeuthen


Index: Makefile.am
===================================================================
RCS file: /cvs/hal/hal/tools/linux/Makefile.am,v
retrieving revision 1.17
retrieving revision 1.18
diff -u -d -r1.17 -r1.18
--- Makefile.am	29 Jan 2006 16:54:22 -0000	1.17
+++ Makefile.am	14 May 2006 18:49:53 -0000	1.18
@@ -3,5 +3,23 @@
 udevrulesdir = $(sysconfdir)/udev/rules.d
 udevrules_DATA = 90-hal.rules
 
+scriptdir = $(libdir)/hal/scripts
+
+script_SCRIPTS =					\
+	hal-luks-setup-linux                          	\
+	hal-luks-teardown-linux                       	\
+	hal-luks-remove-linux		                \
+	hal-system-power-suspend-linux			\
+	hal-system-power-hibernate-linux		\
+	hal-system-power-shutdown-linux			\
+	hal-system-power-reboot-linux			\
+	hal-system-lcd-get-brightness-linux		\
+	hal-system-lcd-set-brightness-linux		\
+	hal-system-power-set-power-save-linux		\
+	hal-system-storage-unmount-linux		\
+	hal-system-storage-eject-linux			\
+	hal-system-storage-cleanup-mountpoint-linux   	\
+	hal-system-storage-cleanup-mountpoints-linux
+
 EXTRA_DIST = 90-hal.rules
 

--- NEW FILE: hal-luks-remove-linux ---
#!/bin/bash

# Copyright (C) 2005 W. Michael Petullo <mike at flyn.org>
# Copyright (C) 2006 David Zeuthen <davidz at redhat.com>
# 
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2.

CRYPTSETUP=/sbin/cryptsetup

# make sure that the cleartext volume is unmounted
CLEARTEXT_VOLUME_UDI=`hal-find-by-property --key volume.crypto_luks.clear.backing_volume --string $HAL_PROP_INFO_UDI`
RET=$?
if [ x$RET = x0 ]; then
    MOUNT_POINT=`hal-get-property --udi $CLEARTEXT_VOLUME_UDI --key volume.mount_point`
    if [ ! x${#MOUNT_POINT} = x0 ]; then
	/bin/umount -l "$MOUNT_POINT"
        # remove directory only if HAL has created it
	if [ -e "$MOUNT_POINT/.created-by-hal" ]; then
	    rm -f "$MOUNT_POINT/.created-by-hal"
	    rmdir --ignore-fail-on-non-empty "$MOUNT_POINT"
	fi
	
    fi
fi

if [ ! -f $CRYPTSETUP ]; then
	echo Error removing $HAL_PROP_BLOCK_DEVICE - $CRYPTSETUP not found >&2
	exit 1
fi

if [ -z $HAL_PROP_VOLUME_UUID ]; then
	echo Error determining UUID of $HAL_PROP_BLOCK_DEVICE >&2
	exit 1
fi

if ! $CRYPTSETUP luksClose luks_crypto_$HAL_PROP_VOLUME_UUID 2> /dev/null; then
	echo Error removing luks_crypto_$HAL_PROP_VOLUME_UUID >&2
	exit 1
fi

exit 0

--- NEW FILE: hal-luks-remove.in-linux ---
#!/bin/bash

#   FILE: hal-luks-remove -- HAL method wrapper for cryptsetup remove
# AUTHOR: W. Michael Petullo <mike at flyn.org>
#   DATE: 25 July 2005
# 
# Copyright (C) 2005 W. Michael Petullo <mike at flyn.org>
# All rights reserved.
# 
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

CRYPTSETUP=/sbin/cryptsetup

HAL_FIND_BY_PROPERTY=@BINDIR@/hal-find-by-property
HAL_GET_PROPERTY=@BINDIR@/hal-get-property

# make sure that the cleartext volume is unmounted
CLEARTEXT_VOLUME_UDI=`$HAL_FIND_BY_PROPERTY --key volume.crypto_luks.clear.backing_volume --string $HAL_PROP_INFO_UDI`
RET=$?
if [ x$RET = x0 ]; then
  MOUNT_POINT=`$HAL_GET_PROPERTY --udi $CLEARTEXT_VOLUME_UDI --key volume.mount_point`
  if [ ! x${#MOUNT_POINT} = x0 ]; then
    /bin/umount -l $MOUNT_POINT
  fi
fi

if [ ! -f $CRYPTSETUP ]; then
	echo Error removing $HAL_PROP_BLOCK_DEVICE - $CRYPTSETUP not found >&2
	exit 1
fi

if [ -z $HAL_PROP_VOLUME_UUID ]; then
	echo Error determining UUID of $HAL_PROP_BLOCK_DEVICE >&2
	exit 1
fi

if ! $CRYPTSETUP remove luks_crypto_$HAL_PROP_VOLUME_UUID 2> /dev/null; then
	echo Error removing luks_crypto_$HAL_PROP_VOLUME_UUID >&2
	exit 1
fi

exit 0

--- NEW FILE: hal-luks-setup-linux ---
#!/bin/bash

# Copyright (C) 2005 W. Michael Petullo <mike at flyn.org>
# Copyright (C) 2006 David Zeuthen <davidz at redhat.com>
# 
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2.

read password
PASSWORD=${PASSWORD//[^[:alnum:]_=[:space:]:.+-]/@}

CRYPTSETUP=/sbin/cryptsetup

if [ ! -f $CRYPTSETUP ]; then
    echo org.freedesktop.Hal.Device.Volume.Crypto.SetupError >&2
    echo Error setting up $HAL_PROP_BLOCK_DEVICE - $CRYPTSETUP not found >&2
    exit 1
fi

if [ -e /dev/mapper/luks_crypto_$HAL_PROP_VOLUME_UUID ]; then
    echo org.freedesktop.Hal.Device.Volume.Crypto.SetupError >&2
    echo $HAL_PROP_BLOCK_DEVICE is already setup? >&2
    exit 1
fi

if ! echo $password | $CRYPTSETUP luksOpen $HAL_PROP_BLOCK_DEVICE luks_crypto_$HAL_PROP_VOLUME_UUID  2> /dev/null; then
    /bin/sleep 3
    echo org.freedesktop.Hal.Device.Volume.Crypto.SetupPasswordError >&2
    echo Error setting up $HAL_PROP_BLOCK_DEVICE - bad password? >&2
    exit 1
fi

hal-set-property --udi=$UDI --key="info.callouts.remove" --strlist-pre="hal-luks-remove" > /dev/null 2>&1

exit 0

--- NEW FILE: hal-luks-teardown-linux ---
#!/bin/bash

# Copyright (C) 2005 W. Michael Petullo <mike at flyn.org>
# Copyright (C) 2006 David Zeuthen <davidz at redhat.com>
# 
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2.

CRYPTSETUP=/sbin/cryptsetup

if [ ! -f $CRYPTSETUP ]; then
	echo org.freedesktop.Hal.Device.Volume.Crypto.TeardownError >&2
	echo Error tearing down $HAL_PROP_BLOCK_DEVICE - $CRYPTSETUP not found >&2
	exit 1
fi

if ! $CRYPTSETUP luksClose luks_crypto_$HAL_PROP_VOLUME_UUID  2> /dev/null; then
	echo org.freedesktop.Hal.Device.Volume.Crypto.TeardownError >&2
	echo Error tearing down $HAL_PROP_BLOCK_DEVICE - fs still mounted? >&2
	exit 1
fi

hal-set-property --udi=$UDI --key="info.callouts.remove" --strlist-rem="hal-luks-remove" > /dev/null 2>&1

--- NEW FILE: hal-system-lcd-get-brightness-linux ---
#!/bin/sh
#
# Copyright (C) 2005 Richard Hughes <richard at hughsie.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.

if [ "$HAL_PROP_LAPTOP_PANEL_ACCESS_METHOD" = "pmu" ]; then
	value="`hal-system-power-pmu getlcd`"
	if [ $? -ne 0 ]; then
		echo "org.freedesktop.Hal.Device.LaptopPanel.NotSupported" >&2
		exit 1
	fi
	exit ${value}
fi

# Check for file existance and that it's readable
if [ ! -r $HAL_PROP_LINUX_ACPI_PATH ]; then
	echo "org.freedesktop.Hal.Device.LaptopPanel.NotSupported" >&2
	echo "$1 not readable!" >&2
	exit 1
fi

if [ "$HAL_PROP_LAPTOP_PANEL_ACCESS_METHOD" = "toshiba" ]; then
	# cat /proc/acpi/toshiba/lcd
	#  brightness:              5
	#  brightness_levels:       8
	value="`cat $HAL_PROP_LINUX_ACPI_PATH | grep brightness: | awk '{print $2;}'`"
elif [ "$HAL_PROP_LAPTOP_PANEL_ACCESS_METHOD" = "asus" ]; then
	# cat /proc/acpi/asus/brn
	#  5
	value="`cat $HAL_PROP_LINUX_ACPI_PATH`"
elif [ "$HAL_PROP_LAPTOP_PANEL_ACCESS_METHOD" = "panasonic" ]; then
	# cat /proc/acpi/pcc/brightness
	#  5
	value="`cat $HAL_PROP_LINUX_ACPI_PATH`"
elif [ "$HAL_PROP_LAPTOP_PANEL_ACCESS_METHOD" = "ibm" ]; then
	# cat /proc/acpi/ibm/brightness
	#  level:          5
	#  commands:       up, down
	#  commands:       level <level> (<level> is 0-7)
	value="`cat $HAL_PROP_LINUX_ACPI_PATH | grep level: | awk '{print $2;}'`"
elif [ "$HAL_PROP_LAPTOP_PANEL_ACCESS_METHOD" = "sony" ]; then
	# cat /proc/acpi/sony/brightness
	#  5
	value="`cat $HAL_PROP_LINUX_ACPI_PATH`"
	let "value = ${value} - 1"
elif [ "$HAL_PROP_LAPTOP_PANEL_ACCESS_METHOD" = "omnibook" ]; then
	# cat /proc/omnibook/lcd
	#  LCD brightness:  7
	value="`cat $HAL_PROP_LINUX_ACPI_PATH | awk '{print $3;}'`"
elif [ "$HAL_PROP_LAPTOP_PANEL_ACCESS_METHOD" = "sonypi" ]; then
	# spicctrl -B
	# 70
	# 0..255
	value="`/usr/sbin/spicctrl -B`"
	RETVAL=$?
	if [ $RETVAL != 0 ]; then
		echo "org.freedesktop.Hal.Device.LaptopPanel.NotSupported" >&2
		exit 1;
	fi
	exit ${value}
else
	echo "org.freedesktop.Hal.Device.LaptopPanel.NotSupported" >&2
	echo "No ACPI method found" >&2
	exit 1
fi

exit ${value}

--- NEW FILE: hal-system-lcd-set-brightness-linux ---
#!/bin/sh
#
# Copyright (C) 2005 Richard Hughes <richard at hughsie.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.

if [ "$HAL_PROP_LAPTOP_PANEL_ACCESS_METHOD" = "pmu" ]; then
	hal-system-power-pmu setlcd $value
	if [ $? -ne 0 ]; then
		echo "org.freedesktop.Hal.Device.LaptopPanel.NotSupported" >&2
		exit 1
	fi
	exit 0
fi

# Check for file existance and that it's writable
if [ ! -w $HAL_PROP_LINUX_ACPI_PATH ]; then
	echo "org.freedesktop.Hal.Device.LaptopPanel.NotSupported" >&2
	echo "$1 not writable!" >&2
	exit 1
fi

if [ "$HAL_PROP_LAPTOP_PANEL_ACCESS_METHOD" = "toshiba" ]; then
	# echo "brightness: {0..x}" >/proc/acpi/toshiba/lcd
	echo "brightness: $value" > $HAL_PROP_LINUX_ACPI_PATH
elif [ "$HAL_PROP_LAPTOP_PANEL_ACCESS_METHOD" = "asus" ]; then
	# echo {0..15} > /proc/acpi/asus/brn
	# http://www.taupro.com/wiki/ChemBook/LCDdisplayPowerConfiguration
	echo "$value" > $HAL_PROP_LINUX_ACPI_PATH
elif [ "$HAL_PROP_LAPTOP_PANEL_ACCESS_METHOD" = "panasonic" ]; then
	# echo {0..15} > /proc/acpi/pcc/brightness
	# http://readlist.com/lists/vger.kernel.org/linux-kernel/7/36405.html
	echo "$value" > $HAL_PROP_LINUX_ACPI_PATH
elif [ "$HAL_PROP_LAPTOP_PANEL_ACCESS_METHOD" = "ibm" ]; then
	# echo "level {0..7}" > /proc/acpi/ibm/brightness
	# http://ibm-acpi.sourceforge.net/README
	echo "level $value" > $HAL_PROP_LINUX_ACPI_PATH
elif [ "$HAL_PROP_LAPTOP_PANEL_ACCESS_METHOD" = "sony" ]; then
	# echo "{1..8}" > /proc/acpi/sony/brightness
	# http://popies.net/sonypi/2.6-sony_acpi4.patch
	echo "$((value + 1))" > $HAL_PROP_LINUX_ACPI_PATH
elif [ "$HAL_PROP_LAPTOP_PANEL_ACCESS_METHOD" = "omnibook" ]; then
	# echo "{0..7}" > /proc/omnibook/lcd
	# http://bugzilla.gnome.org/show_bug.cgi?id=331458
	echo "$value" > $HAL_PROP_LINUX_ACPI_PATH
elif [ "$HAL_PROP_LAPTOP_PANEL_ACCESS_METHOD" = "sonypi" ]; then
	# spicctrl -B
	# 70
	# 0..255
	value="`/usr/sbin/spicctrl -B`"
	RETVAL=$?
	if [ $RETVAL != 0 ]; then
		echo "org.freedesktop.Hal.Device.LaptopPanel.NotSupported" >&2
		exit 1;
	fi
	exit ${value}
else
	echo "org.freedesktop.Hal.Device.LaptopPanel.NotSupported" >&2
	echo "No ACPI method found" >&2
	exit 1
fi

exit 0

--- NEW FILE: hal-system-power-hibernate-linux ---
#!/bin/sh

POWERSAVED_SUSPEND2DISK="dbus-send --system --dest=com.novell.powersave \
                         --print-reply /com/novell/powersave \
                         com.novell.powersave.action.SuspendToDisk"

unsupported() {
	echo org.freedesktop.Hal.Device.SystemPowerManagement.NotSupported >&2
	echo No hibernate script found >&2
	exit 1
}

#SuSE and ALTLinux only support powersave
if [ -f /etc/altlinux-release ] || [ -f "/etc/SuSE-release" ] ; then
	if [ -x /usr/bin/powersave ] ; then
	        $POWERSAVED_SUSPEND2DISK
		RET=$?
	else
		unsupported
	fi

#RedHat/Fedora only support pm-utils
elif [ -f /etc/redhat-release ] || [ -f /etc/fedora-release ] ; then
	if [ -x /usr/sbin/pm-hibernate ] ; then
		/usr/sbin/pm-hibernate
		RET=$?
	else
		unsupported
	fi

#Other distros just need to have *any* tools installed
else
	if [ -x "/usr/bin/powersave" ] ; then
	        $POWERSAVED_SUSPEND2DISK
		RET=$?
	elif [ -x "/usr/sbin/pmi" ] ; then
		/usr/sbin/pmi action hibernate force
		RET=$?
	elif [ -x "/usr/sbin/pm-hibernate" ] ; then
		/usr/sbin/pm-hibernate
		RET=$?
	elif [ -x "/usr/sbin/hibernate" ] ; then
		# Suspend2 tools installed
		/usr/sbin/hibernate --force
		RET=$?
	elif [ -w "/sys/power/state" ] ; then
		# Use the raw kernel sysfs interface
		echo "disk" > /sys/power/state
		RET=$?
	else
		unsupported
		fi
	fi

#Refresh devices as a resume can do funny things
for type in button battery ac_adapter
do
	devices=`hal-find-by-capability --capability $type`
	for device in $devices
	do
		dbus-send --system --print-reply --dest=org.freedesktop.Hal \
			  $device org.freedesktop.Hal.Device.Rescan
	done
done

exit $RET

--- NEW FILE: hal-system-power-reboot-linux ---
#!/bin/sh

unsupported() {
	echo "org.freedesktop.Hal.Device.SystemPowerManagement.NotSupported" >&2
	echo "No reboot command found" >&2
	exit 1
}

#Try for common tools
if [ -x "/sbin/shutdown" ] ; then
	/sbin/shutdown -r now
	exit $?
elif [ -x "/usr/sbin/shutdown" ] ; then
	/usr/sbin/shutdown -r now
	exit $?
else
	unsupported
fi

--- NEW FILE: hal-system-power-set-power-save-linux ---
#!/bin/sh
read value

unsupported() {
	echo org.freedesktop.Hal.Device.SystemPowerManagement.NotSupported >&2
	echo No powersave method found >&2
	exit 1
}

#SuSE and ALTLinux only support powersave
if [ -f /etc/altlinux-release ] || [ -f /etc/SuSE-release ] ; then
	if [ -x "/usr/bin/powersave" ] ; then
		if [ $value = "true" ]; then
			/usr/bin/powersave -e Powersave
			RET=$?
		elif [ $value = "false" ]; then
			/usr/bin/powersave -e Performance
			RET=$?
		fi
	else
		unsupported
	fi

#RedHat/Fedora only support pm-utils
elif [ -f /etc/redhat-release ] || [ -f /etc/fedora-release ] ; then
	if [ -x "/usr/sbin/pm-powersave" ] ; then
		if [ $value = "true" ]; then
			/usr/sbin/pm-powersave true
			RET=$?
		elif [ $value = "false" ]; then
			/usr/sbin/pm-powersave false
			RET=$?
		fi
	else
		unsupported
	fi

else
	# cannot set proc stuff here, so error out
	unsupported
	fi 

exit $RET


--- NEW FILE: hal-system-power-shutdown-linux ---
#!/bin/sh

unsupported() {
	echo "org.freedesktop.Hal.Device.SystemPowerManagement.NotSupported" >&2
	echo "No shutdown command found" >&2
	exit 1
}

#Try for common tools
if [ -x "/sbin/shutdown" ] ; then
	/sbin/shutdown -h now
	exit $?
elif [ -x "/usr/sbin/shutdown" ] ; then
	/usr/sbin/shutdown -h now
	exit $?
else
	unsupported
fi

--- NEW FILE: hal-system-power-suspend-linux ---
#!/bin/sh

POWERSAVED_SUSPEND2RAM="dbus-send --system --dest=com.novell.powersave \
                        --print-reply /com/novell/powersave \
                        com.novell.powersave.action.SuspendToRam"

alarm_not_supported() {
	echo org.freedesktop.Hal.Device.SystemPowerManagement.AlarmNotSupported >&2
	echo Waking the system up is not supported >&2
	exit 1
}

unsupported() {
	echo org.freedesktop.Hal.Device.SystemPowerManagement.NotSupported >&2
	echo No suspend method found >&2
	exit 1
}

read seconds_to_sleep

#PMU systems cannot use /sys/power/state yet, so use a helper to issue an ioctl
if [ "$HAL_PROP_LAPTOP_PANEL_ACCESS_METHOD" == "pmu" ]; then
	hal-system-power-pmu sleep
	if [ $? -ne 0 ]; then
		echo "org.freedesktop.Hal.Device.SystemPowerManagement.NotSupported" >&2
		exit 1
	fi
	exit 0
fi

#SuSE and ALTLinux only support powersave
if [ -f "/etc/altlinux-release" ] || [ -f "/etc/SuSE-release" ] ; then
	if [ -x /usr/bin/powersave ] ; then
	        $POWERSAVED_SUSPEND2RAM
		RET=$?
	else
		# TODO: add support
		unsupported
	fi

#RedHat/Fedora only support pm-utils
elif [ -f "/etc/redhat-release" ] || [ -f "/etc/fedora-release" ] ; then
	# TODO: fix pm-suspend to take a --wakeup-alarm argument
	if [ $seconds_to_sleep != "0" ] ; then
		alarm_not_supported
	fi
	# TODO: fixup pm-suspend to define erroc code (see alarm above) and throw
	#	   the appropriate exception
	if [ -x "/usr/sbin/pm-suspend" ] ; then
		/usr/sbin/pm-suspend
		RET=$?
	else
		# TODO: add support
		unsupported
	fi

#FreeBSD uses zzz to suspend for both ACPI and APM
elif [ "x`uname -s`" = "xFreeBSD" ] ; then
	if [ -x /usr/sbin/zzz ] ; then
		/usr/sbin/zzz
		RET=$?
	else
		unsupported
	fi

#Other distros just need to have *any* tools installed
else
	if [ -x "/usr/bin/powersave" ] ; then
	    $POWERSAVED_SUSPEND2RAM
	    RET=$?
	elif [ -x "/usr/sbin/pmi" ] ; then
	    /usr/sbin/pmi action suspend force
	    RET=$?
	elif [ -w "/sys/power/state" ] ; then
	    # Use the raw kernel sysfs interface
	    echo "mem" > /sys/power/state
	    RET=$?
	else
	    # TODO: add other scripts support
	    unsupported
	    fi
	fi

#Refresh devices as a resume can do funny things
for type in button battery ac_adapter
do
	devices=`hal-find-by-capability --capability $type`
	for device in $devices
	do
		dbus-send --system --dest=org.freedesktop.Hal \
			  $device org.freedesktop.Hal.Device.Rescan
	done
done

exit $RET

--- NEW FILE: hal-system-storage-cleanup-mountpoints-linux ---
#!/bin/sh

# Copyright (C) 2005, Kay Sievers <kay.sievers at vrfy.org>
# Copyright (C) 2006, David Zeuthen <davidz at redhat.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2.

MOUNT_ROOT="/media"

for DIR in $MOUNT_ROOT/*; do

    # check if we created it
    if [ -e "$DIR/.created-by-hal" ]; then
	BUSY=0
        # /proc/mounts substitute spaces with 040
        DIRMOD=`echo "$DIR" | sed -e 's/[[:space:]]/040/g'`

        while read dev dir type options; do
	    if [ "$dir" = "$DIRMOD" ]; then
	        BUSY=1
	        break
	    fi
	done < /proc/mounts

	if [ "$BUSY" != "1" ]; then
	    rm -f "$DIR/.created-by-hal"
	    rmdir "$DIR" 2>/dev/null || true
	fi
    fi
done

exit 0

--- NEW FILE: hal-system-storage-eject-linux ---
#!/bin/sh

# Copyright (C) 2005, Kay Sievers <kay.sievers at vrfy.org>
# Copyright (C) 2006, David Zeuthen <david at fubar.dk>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2.
#

# read parameters
# "<option1>\t<option2>\n"
# Only allow ^a-zA-Z0-9_= in the string because otherwise someone may
# pass e.g. umask=0600,suid,dev or umask=`/bin/evil`

rc=0
read GIVEN_EJECTOPTIONS
GIVEN_EJECTOPTIONS="`echo \"$GIVEN_EJECTOPTIONS\" | tr -cd \"a-zA-Z0-9_=[:space:]\"`"
RESULT=$(eject "$HAL_PROP_BLOCK_DEVICE" 2>&1)

if [ $? -ne 0 ]; then
    case "$RESULT" in
	*busy*)
	    echo "org.freedesktop.Hal.Device.Volume.Busy" >&2
	    echo "Device is busy." >&2
	    ;;
	*)
	    echo "org.freedesktop.Hal.Device.Volume.UnknownFailure" >&2
	    echo "Unknown failure." >&2
    esac
    exit 1
fi

if [ -n "$HAL_PROP_INFO_HAL_MOUNT_CREATED_MOUNT_POINT" ]; then
    # remove directory only if HAL has created it
    if [ -e $HAL_PROP_INFO_HAL_MOUNT_CREATED_MOUNT_POINT/.created-by-hal ]; then
	rm -f $HAL_PROP_INFO_HAL_MOUNT_CREATED_MOUNT_POINT/.created-by-hal
	rmdir "$HAL_PROP_INFO_HAL_MOUNT_CREATED_MOUNT_POINT" 2>/dev/null || true
    fi
fi

exit 0

--- NEW FILE: hal-system-storage-unmount-linux ---
#!/bin/sh

# Copyright (C) 2005, Kay Sievers <kay.sievers at vrfy.org>
# Copyright (C) 2006, David Zeuthen <david at fubar.dk>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2.

# read parameters
# "lazy\tforce\n"
# Only allow ^a-zA-Z0-9_= in the string because otherwise someone may
# pass e.g. umask=0600,suid,dev or umask=`/bin/evil`

read GIVEN_UNMOUNTOPTIONS
GIVEN_UNMOUNTOPTIONS="`echo \"$GIVEN_UNMOUNTOPTIONS\" | tr -cd \"a-zA-Z0-9_=[:space:]\"`"

if [ -n "$GIVEN_UNMOUNTOPTIONS" ]; then
    for OPTION in $GIVEN_UNMOUNTOPTIONS; do
	OPTION_WAS_OK="0"
	for VALID_OPTION in $HAL_PROP_VOLUME_UNMOUNT_VALID_OPTIONS; do
	    if [ "x$OPTION" = "x$VALID_OPTION" ]; then
		OPTION_WAS_OK="1"
		break
	    fi
	done

	if [ "$OPTION_WAS_OK" = "1" ]; then
		case "$OPTION" in
		    "lazy")
			UNMOUNTOPTIONS="$UNMOUNTOPTIONS -l"
			OPTION_WAS_OK="1"
			;;
		    "force")
			UNMOUNTOPTIONS="$UNMOUNTOPTIONS -f"
			OPTION_WAS_OK="1"
			;;
		    *)
			echo "org.freedesktop.Hal.Device.Volume.UnsupportedUnmountOption" >&2
			echo "The option '$OPTION' is not supported" >&2
			exit 1
		esac
	else
		echo "org.freedesktop.Hal.Device.Volume.InvalidUnmountOption" >&2
		echo "The option '$OPTION' is invalid" >&2
		exit 1
	fi
    done
fi

RESULT=$(umount $UNMOUNTOPTIONS "$MOUNT_POINT"  2>&1)
if [ $? -ne 0 ]; then
    case "$RESULT" in
	*busy*)
	    echo "org.freedesktop.Hal.Device.Volume.Busy" >&2
	    echo "Device is busy." >&2
	    ;;
	*"not mounted"*)
	    echo "org.freedesktop.Hal.Device.Volume.NotMounted" >&2
	    echo "Device is not mounted." >&2
	    ;;
	*)
	    echo "org.freedesktop.Hal.Device.Volume.UnknownFailure" >&2
	    echo "Unknown failure." >&2
    esac
    exit 1
fi

# remove directory only if HAL has created it
if [ -e "$MOUNT_POINT/.created-by-hal" ]; then
  rm -f "$MOUNT_POINT/.created-by-hal"
  rmdir "$MOUNT_POINT" 2>/dev/null || true
fi

hal-set-property --udi $UDI --key info.hal_mount.created_mount_point --remove > /dev/null 2>&1
hal-set-property --udi $UDI --key info.hal_mount.mounted_by_uid --remove > /dev/null 2>&1

exit 0




More information about the hal-commit mailing list