Sophie

Sophie

distrib > Mandriva > 9.1 > ppc > media > main > by-pkgid > bb23757c2103e08e206a6d1231227700 > files > 3

suspend-scripts-1.4-1mdk.noarch.rpm

#!/bin/sh
# -*- Mode: shell-script -*-
# Copyright (C) 2003 by Chmouel Boudjnah <chmouel@mandrakesoft.com>
# Redistribution of this file is permitted under the terms of the GNU
# Public License (GPL)
. /etc/sysconfig/suspend

SCRIPTDIR="/etc/sysconfig/suspend-scripts/suspend.d"
debug=

while getopts d opt
  do
  case "$opt" in
      d) debug="yes";;
  esac
done
shift $((OPTIND - 1))

ARG=$1

function parse {
    grep=$1
    local suspend_second=
    local suspend=
    local name=

    for file in $SCRIPTDIR/*;do
	suspend_second=
	suspend=
	[[ $file == *~ || $file == *.rpmsave || $file == *.rpmold || $file == *\#* ]] && continue
	suspend=$(grep "^# ${grep}: " $file|sed 's/.* \([0-9]*\)/\1/')
	suspend_second=$(grep "^# ${grep}-second: " $file|sed 's/.* \([0-9]*\)/\1/')
	name=${file##*/}

	if [[ -n $suspend_second ]];then
	    echo $suspend_second ${name}-second
	fi

	[[ -z $suspend ]] && continue

	echo $suspend $name

    done | sort -n
}

function doit {
    action=$1
    for name in $(parse $1|cut -d" " -f2 );do
	[[ -n $debug ]] && echo "Launching $name"
	if [[ $name == *-second ]];then
	    if [[ -n $debug ]];then
		/bin/bash ${SCRIPTDIR}/${name%%-*} ${1}2
	    else
	    /bin/bash ${SCRIPTDIR}/${name%%-*} ${1}2 2>/dev/null >/dev/null
	    fi
	else
	    if [[ -n $debug ]];then
		/bin/bash ${SCRIPTDIR}/$name ${1}
	    else
		/bin/bash ${SCRIPTDIR}/$name ${1} 2>/dev/null >/dev/null
	    fi
	fi
	sync
    done
}

function detect_swap_part {
    while read -a entry;do
	[[ ${entry[1]} == swap ]] && echo ${entry[0]}
    done < /etc/fstab
}

function check_ram {
    local total=
    local swap=
    while read -a entry;do
	[[ ${entry[0]/:/} == MemTotal ]] && total=${entry[1]}
	[[ ${entry[0]/:/} == SwapTotal ]] && swap=${entry[1]}
    done < /proc/meminfo
    [[ $swap -lt $total ]] && {
	echo "You have ${total}Kb available memory and only ${swap}Kb for swapping"
	exit 1
    }
}

function suspend {
    doit suspend
}

function resume {
    doit resume
}

function suspend_to_memory {
    ret=0
    TMP=/tmp/.suspend.$$
    ps ax > $TMP
    SWAP_PART=$(detect_swap_part)
    [[ -z $SWAP_PART ]] && { echo "Cannot detect a SWAP_PARTITIONS"; exit 1 ;}

    check_ram

    FILE=/proc/acpi/sleep
    PARAM=4
    [[ -n $SWSUSP_FORCE_SUSPEND_MODE ]] && FILE=/proc/sys/kernel/swsusp
    [[ -n $SWSUSP_FORCE_SUSPEND_MODE ]] && PARAM="1 $SWSUSP_FORCE_SUSPEND_MODE"

    if ! grep -q 'resume=' /proc/cmdline;then
    echo "You need to specify resume=$SWAP_PART at kernel booting"
    exit 1;
    fi
    suspend
    if ! (/bin/echo $PARAM > $FILE &) ;then
	ret=1
    fi
    if [ $ret = 0 ] ;then    
	sleep 5
    pids=`awk '($5 == "/bin/echo"){print $1}' $TMP`
    for pid in $pids; do
	usleep 500000
    done
    fi
    rm -f $TMP
    resume
}

case $1 in
    acpi)
	suspend_to_memory
	;;
    apm)
	case $2 in
	    suspend)
		suspend;;
	    resume)
		resume;;
	    *)
	esac
	;;*)

esac