sample: wrap flock in flock_fd for support on OS/X

OS/X has a flock (v0.2.3) implementation using

brew tap discoteq/discoteq
brew install flock

The current version doesn't support -x as a parameter for
an exclusive lock but uses an exclusive lock when no parameter
is passed at all.

Signed-off-by: Stefan Berger <stefanb@linux.vnet.ibm.com>
This commit is contained in:
Stefan Berger 2018-08-30 15:26:35 -04:00
parent bd8f4cae3a
commit f13d2a045c

View File

@ -49,6 +49,8 @@ LOCALCA_CONFIG=${XDG_CONFIG_HOME:-/etc}/swtpm-localca.conf
# Default logging goes to stderr
LOGFILE=""
UNAME_S=$(uname -s)
logit()
{
if [ -z "$LOGFILE" ]; then
@ -67,6 +69,18 @@ logerr()
fi
}
flock_fd()
{
local fd=$1
case "${UNAME_S}" in
Darwin)
flock $fd;;
*)
flock -x $fd;;
esac
}
# Get an configuration value from an configurations file
# @param1: The file with the options
# @param2: The name of the option
@ -123,7 +137,7 @@ get_next_cert_serial() {
touch ${LOCK}
(
# Avoid concurrent creation of next serial
flock -x 100
flock_fd 100
if [ $? -ne 0 ]; then
logerr "Could not get lock ${LOCK}"
return 1
@ -264,7 +278,7 @@ create_localca_cert() {
touch ${LOCK}
(
# Avoid concurrent creation of keys and certs
flock -x 100
flock_fd 100
if [ $? -ne 0 ]; then
logerr "Could not get lock ${LOCK}"
return 1