#!/bin/bash

#
# Cleanup stale uuid lock files from the unix_user module
#
#
# There is an extremely narrow but unavoidable race condition where
# this script could break the synchronization between two instances of
# unix_user.rb waiting for the same lock.
#

# Find lock files from unix_user.rb that are over a week old.  The
# number of files could be well beyond the command line length limit.
find /var/lock -mindepth 1 -maxdepth 1 -type f -mtime +7 \
    \( -name 'oo-create.*' -o -name 'oo-modify-ssh-keys.*' \) 2>/dev/null | {
        while read lockfile
        do
            (
                flock 200
                rm -f "$lockfile" 200>&-
                flock -u 200
            ) 200>"$lockfile"
        done
    }

