meta-digi-dey: suspend: add critical section

Add critical section in suspend script to avoid that more than
one instance can be executed concurrently. This happens, for
instance, when the power button key is pressed while the system
is resuming, causing issues on the wifi interface.

Signed-off-by: Mike Engel <Mike.Engel@digi.com>

https://jira.digi.com/browse/DEL-3694
This commit is contained in:
Mike Engel 2017-02-14 16:12:06 +01:00
parent a54c070454
commit 0236c3dfb4
1 changed files with 23 additions and 1 deletions

View File

@ -3,7 +3,7 @@
# #
# suspend # suspend
# #
# Copyright (C) 2009-2014 by Digi International Inc. # Copyright (C) 2009-2017 by Digi International Inc.
# All rights reserved. # All rights reserved.
# #
# This program is free software; you can redistribute it and/or modify it # This program is free software; you can redistribute it and/or modify it
@ -17,6 +17,8 @@
scriptname="$(basename ${0})" scriptname="$(basename ${0})"
syspower="/sys/power/state" syspower="/sys/power/state"
lockfile="/var/lock/${scriptname}.lock"
lockfd="9"
usage() { usage() {
printf "\nSuspend system to RAM memory\n" printf "\nSuspend system to RAM memory\n"
@ -37,6 +39,21 @@ resume_interfaces() {
fi fi
} }
enter_critical_section() {
# Create lock file
eval "exec ${lockfd}>${lockfile}"
# Acquire the lock in non blocking mode. Otherwise, additional calls
# to the script will be queued and the system will endlessly go in
# and out of suspend to ram
flock -n "${lockfd}" || exit 0
}
exit_critical_section() {
# Release the lock
flock -u "${lockfd}"
}
while getopts "h" c; do while getopts "h" c; do
case "${c}" in case "${c}" in
h) usage; exit;; h) usage; exit;;
@ -44,6 +61,9 @@ while getopts "h" c; do
done done
if [ -f "${syspower}" ]; then if [ -f "${syspower}" ]; then
# Avoid running multiple instances of this script in parallel
enter_critical_section
# Pre-suspend actions # Pre-suspend actions
suspend_interfaces suspend_interfaces
@ -53,6 +73,8 @@ if [ -f "${syspower}" ]; then
# Post-resume actions # Post-resume actions
resume_interfaces resume_interfaces
exit_critical_section
else else
printf "\n[ERROR] File ${syspower} not found\n\n" printf "\n[ERROR] File ${syspower} not found\n\n"
fi fi