From 66db33b5431a1f01df6c500c22d5ca0bd362acc1 Mon Sep 17 00:00:00 2001 From: Michael Biebl Date: Sat, 8 Mar 2008 03:16:33 +0100 Subject: Implement the root check using id, $EUID seems to be a bash only feature. --- src/pm-action.in | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src') diff --git a/src/pm-action.in b/src/pm-action.in index 82aa295..58f283e 100644 --- a/src/pm-action.in +++ b/src/pm-action.in @@ -40,7 +40,7 @@ help() exit 0 } -if [ -n "$EUID" -a "$EUID" != "0" ]; then +if [ "$(id -u)" != "0" ]; then echo This utility may only be run by the root user. 1>&2 exit 1 fi -- cgit v1.2.3 From ac2e951e553f69dc5ab04b37f4e973bebdb1f5c4 Mon Sep 17 00:00:00 2001 From: Victor Lowther Date: Fri, 14 Mar 2008 07:21:34 -0500 Subject: Split functions.in into functions.in and pm-functions.in pm-functions.in contains the functionality that should only be used by the pm-utils frontends (pm-action, pm-is-supported, and pm-powersave). functions.in contains code that is shared between the hooks and the pm-utils frontends. The reason behind splitting this out is to reduce namespace pollution in the hooks and to eventaully make it easier to split the hooks into their own package. --- pm/Makefile.am | 4 +- pm/functions.in | 127 +---------------------------------------------- pm/pm-functions.in | 131 +++++++++++++++++++++++++++++++++++++++++++++++++ src/pm-action.in | 3 +- src/pm-is-supported.in | 2 +- src/pm-powersave.in | 3 +- 6 files changed, 137 insertions(+), 133 deletions(-) create mode 100644 pm/pm-functions.in (limited to 'src') diff --git a/pm/Makefile.am b/pm/Makefile.am index d17ba35..abfa3ff 100644 --- a/pm/Makefile.am +++ b/pm/Makefile.am @@ -6,11 +6,11 @@ SUBDIRS = \ pm_libdir = $(libdir)/pm-utils pm_sysconfdir = $(sysconfdir)/pm -pm_lib_SCRIPTS = functions +pm_lib_SCRIPTS = functions pm-functions dist_pm_lib_DATA = defaults -script_in_files = functions.in +script_in_files = functions.in pm-functions.in CLEANFILES = $(script_in_files:.in=) diff --git a/pm/functions.in b/pm/functions.in index dbce365..5503be0 100644 --- a/pm/functions.in +++ b/pm/functions.in @@ -1,60 +1,10 @@ #!/bin/sh # vim:noexpandtab +# Common functionality for the hooks. -# Default values go here. It is important to _not_ initialize some -# variables here. They are: -# -# PM_CMDLINE -# RESUME_MODULES -# # for great debugging! [ "${PM_DEBUG}" = "true" ] && set -x -set -a -PM_UTILS_LIBDIR="@PM-UTILS-LIBDIR@" -PM_UTILS_ETCDIR="@PM-UTILS-SYSCONFDIR@" -PM_UTILS_RUNDIR="/var/run/pm-utils" - -PATH=/sbin:/usr/sbin:/bin:/usr/bin:"${PM_UTILS_LIBDIR}"/bin -HIBERNATE_MODE="platform" -HIBERNATE_RESUME_POST_VIDEO=no -INHIBIT="${PM_UTILS_RUNDIR}/inhibit" -PM_LOGFILE="${PM_LOGFILE:=/var/log/pm-suspend.log}" -SUSPEND_MODULES="" -TEMPORARY_CPUFREQ_GOVERNOR="performance" -LOCKDIR="${PM_UTILS_RUNDIR}/locks" -STORAGEDIR="${PM_UTILS_RUNDIR}/storage" -SLEEP_MODULE="kernel" -NA=254 - -# Use c sort order -export LC_COLLATE=C - -[ -f "${PM_UTILS_LIBDIR}"/defaults ] && . "${PM_UTILS_LIBDIR}"/defaults - -set +a - -log() { [ $LOGGING ] && echo $*; } - -source_configs() -{ - for cfg in "${PM_UTILS_ETCDIR}"/config.d/*[!~] ; do - [ -f "$cfg" ] || continue - set -a - . "${cfg}" - set +a - done -} - -source_configs - -SLEEP_FUNCTIONS="${PM_UTILS_LIBDIR}/module.d/${SLEEP_MODULE}" -[ -f "${SLEEP_FUNCTIONS}" ] || { - log "Requested sleep module $SLEEP_MODULE not available." - exit 1 -} - -. "${SLEEP_FUNCTIONS}" # try to take the lock. Fail if we cannot get it. try_lock() @@ -95,23 +45,6 @@ release_lock() } -take_suspend_lock() -{ - VT=$(fgconsole) - chvt 63 - try_lock "pm-utils.lock" || return 1 - mkdir -p "${STORAGEDIR}" - return 0 -} - -remove_suspend_lock() -{ - rm -rf "${STORAGEDIR}" - chvt 1 - chvt $VT - release_lock "pm-utils.lock" -} - command_exists() { @@ -121,48 +54,6 @@ command_exists() return $? } -hook_exit_status(){ - case $1 in - 0) log "success." ;; - $NA) log "not applicable." ;; - *) log "Returned exit code $1." ;; - esac -} - -run_hooks() { - # $1 = type of hook to find. - # $2 = paramaters to pass to hooks. - # $3 = if present and equal to "reverse", run hooks backwards. - # Currently only power and sleep are meaningful. - local syshooks="${PM_UTILS_ETCDIR}/$1.d" - local phooks="${PM_UTILS_LIBDIR}/$1.d" - local sort="sort" - local base - local hook - local oifs="${IFS}" - # the next two lines are not a typo or a formatting error! - local nifs=" -" - IFS="${nifs}" # tolerate spaces in filenames. - [ "$3" = "reverse" ] && sort="sort -r" - for base in $(IFS="${oifs}"; for f in "$syshooks/"*[!~] "$phooks/"*[!~]; - do [ -O "$f" ] && echo ${f##*/} ; done | $sort | uniq) ; - do - if [ -f "$syshooks/$base" ]; then - hook="$syshooks/$base" - elif [ -f "$phooks/$base" ]; then - hook="$phooks/$base" - fi - [ -x "${hook}" ] && ( - IFS="${oifs}" - log "$(date): running ${hook} $2" - "${hook}" $2 - hook_exit_status $? - ) - done - IFS="${oifs}" -} - get_power_status() { RETVAL=0 @@ -182,22 +73,6 @@ get_power_status() return $RETVAL } -init_logfile() -{ - if [ -h "$1" ]; then - echo "$1 is a symbolic link, refusing to overwrite." - return 1 - elif [ ! -O "$1" ]; then - echo "We do not own $1, refusing to overwrite." - return 1 - elif [ -z "$1" ]; then - echo "Please pass a filename to init_logfile." - return 1 - fi - export LOGGING="true" - exec > "$1" 2>&1 -} - _rmmod() { if modprobe -r "$1"; then diff --git a/pm/pm-functions.in b/pm/pm-functions.in new file mode 100644 index 0000000..88eb841 --- /dev/null +++ b/pm/pm-functions.in @@ -0,0 +1,131 @@ +#!/bin/sh +# vim:noexpandtab + + +# Default values go here. It is important to _not_ initialize some +# variables here. They are: +# +# PM_CMDLINE +# RESUME_MODULES +# +# for great debugging! +[ "${PM_DEBUG}" = "true" ] && { + export PM_DEBUG + set -x +} +set -a +PM_UTILS_LIBDIR="@PM-UTILS-LIBDIR@" +PM_UTILS_ETCDIR="@PM-UTILS-SYSCONFDIR@" +PM_UTILS_RUNDIR="/var/run/pm-utils" + +PATH=/sbin:/usr/sbin:/bin:/usr/bin:"${PM_UTILS_LIBDIR}"/bin +HIBERNATE_MODE="platform" +HIBERNATE_RESUME_POST_VIDEO=no +INHIBIT="${PM_UTILS_RUNDIR}/inhibit" +PM_LOGFILE="${PM_LOGFILE:=/var/log/pm-suspend.log}" +SUSPEND_MODULES="" +TEMPORARY_CPUFREQ_GOVERNOR="performance" +LOCKDIR="${PM_UTILS_RUNDIR}/locks" +STORAGEDIR="${PM_UTILS_RUNDIR}/storage" +SLEEP_MODULE="kernel" +NA=254 +PM_FUNCTIONS="$PM_UTILS_LIBDIR/functions" +# Use c sort order +export LC_COLLATE=C + +[ -f "${PM_UTILS_LIBDIR}"/defaults ] && . "${PM_UTILS_LIBDIR}"/defaults + +set +a + +for cfg in "${PM_UTILS_ETCDIR}"/config.d/*[!~] ; do + [ -f "$cfg" ] || continue + set -a + . "${cfg}" + set +a +done + +. "${PM_FUNCTIONS}" + +take_suspend_lock() +{ + VT=$(fgconsole) + chvt 63 + try_lock "pm-utils.lock" || return 1 + mkdir -p "${STORAGEDIR}" + return 0 +} + +remove_suspend_lock() +{ + rm -rf "${STORAGEDIR}" + chvt 1 + chvt $VT + release_lock "pm-utils.lock" +} + +hook_exit_status(){ + case $1 in + 0) echo "success." ;; + $NA) echo "not applicable." ;; + *) echo "Returned exit code $1." ;; + esac +} + +run_hooks() { + # $1 = type of hook to find. + # $2 = paramaters to pass to hooks. + # $3 = if present and equal to "reverse", run hooks backwards. + # Currently only power and sleep are meaningful. + local syshooks="${PM_UTILS_ETCDIR}/$1.d" + local phooks="${PM_UTILS_LIBDIR}/$1.d" + local sort="sort" + local base + local hook + local oifs="${IFS}" + # the next two lines are not a typo or a formatting error! + local nifs=" +" + IFS="${nifs}" # tolerate spaces in filenames. + [ "$3" = "reverse" ] && sort="sort -r" + for base in $(IFS="${oifs}"; for f in "$syshooks/"*[!~] "$phooks/"*[!~]; + do [ -O "$f" ] && echo ${f##*/} ; done | $sort | uniq) ; + do + if [ -f "$syshooks/$base" ]; then + hook="$syshooks/$base" + elif [ -f "$phooks/$base" ]; then + hook="$phooks/$base" + fi + [ -x "${hook}" ] && ( + IFS="${oifs}" + echo "$(date): running ${hook} $2" + "${hook}" $2 + hook_exit_status $? + ) + done + IFS="${oifs}" +} + +init_logfile() +{ + if [ -h "$1" ]; then + echo "$1 is a symbolic link, refusing to overwrite." + return 1 + elif [ ! -O "$1" ]; then + echo "We do not own $1, refusing to overwrite." + return 1 + elif [ -z "$1" ]; then + echo "Please pass a filename to init_logfile." + return 1 + fi + exec > "$1" 2>&1 +} + + +SLEEP_FUNCTIONS="${PM_UTILS_LIBDIR}/module.d/${SLEEP_MODULE}" +[ -f "${SLEEP_FUNCTIONS}" ] || { + echo "Requested sleep module $SLEEP_MODULE not available." + exit 1 +} + +. "${SLEEP_FUNCTIONS}" + diff --git a/src/pm-action.in b/src/pm-action.in index 58f283e..dfb055a 100644 --- a/src/pm-action.in +++ b/src/pm-action.in @@ -26,8 +26,7 @@ # The rule here? Simplicity. -export PM_FUNCTIONS="@PM-UTILS-LIBDIR@/functions" -. "${PM_FUNCTIONS}" +. "@PM-UTILS-LIBDIR@/pm-functions" # save the command line parameters for the hooks. export PM_CMDLINE="$*" diff --git a/src/pm-is-supported.in b/src/pm-is-supported.in index bfce1ae..9446846 100644 --- a/src/pm-is-supported.in +++ b/src/pm-is-supported.in @@ -16,7 +16,7 @@ # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA # -. "@PM-UTILS-LIBDIR@/functions" +. "@PM-UTILS-LIBDIR@/pm-functions" help_options() { diff --git a/src/pm-powersave.in b/src/pm-powersave.in index a1cdb4a..d0c2d5f 100644 --- a/src/pm-powersave.in +++ b/src/pm-powersave.in @@ -25,8 +25,7 @@ # POWERSAVE_LOGFILE="/dev/null" -export PM_FUNCTIONS="@PM-UTILS-LIBDIR@/functions" -. "${PM_FUNCTIONS}" +. "@PM-UTILS-LIBDIR@/pm-functions" if [ "$1" = "true" -o "$1" = "false" ] ; then init_logfile "${POWERSAVE_LOGFILE}" -- cgit v1.2.3