]> git.proxmox.com Git - swtpm.git/commitdiff
sample: wrap flock in flock_fd for support on OS/X
authorStefan Berger <stefanb@linux.vnet.ibm.com>
Thu, 30 Aug 2018 19:26:35 +0000 (15:26 -0400)
committerStefan Berger <stefanb@linux.vnet.ibm.com>
Thu, 30 Aug 2018 19:27:25 +0000 (15:27 -0400)
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>
samples/swtpm-localca

index 25754df14bb46ddafdd840733fd5ea3fdc3af5b0..1965b1a0d36028cc43823c97cd6112146f52565c 100755 (executable)
@@ -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