]> git.ipfire.org Git - people/stevee/network.git/blob - functions.lock
headers: Add missing function and rename existing ones.
[people/stevee/network.git] / functions.lock
1 #!/bin/bash
2 ###############################################################################
3 # #
4 # IPFire.org - A linux based firewall #
5 # Copyright (C) 2012 Michael Tremer & Christian Schmidt #
6 # #
7 # This program is free software: you can redistribute it and/or modify #
8 # it under the terms of the GNU General Public License as published by #
9 # the Free Software Foundation, either version 3 of the License, or #
10 # (at your option) any later version. #
11 # #
12 # This program is distributed in the hope that it will be useful, #
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of #
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
15 # GNU General Public License for more details. #
16 # #
17 # You should have received a copy of the GNU General Public License #
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. #
19 # #
20 ###############################################################################
21
22 function lock_acquire() {
23 local lockfile="${1}"
24 assert isset lockfile
25
26 # timeout value in seconds
27 local timeout=60
28 timeout=$(( ${timeout} * 10 ))
29
30 log DEBUG "Acquiring lock '${lockfile}'."
31
32 local free="false"
33 while [ ${timeout} -gt 0 ]; do
34 if [ ! -e "${lockfile}" ]; then
35 free="true"
36 break
37 fi
38
39 timeout=$(( ${timeout} - 1 ))
40 sleep 0.1
41 done
42
43 assert ${free} "Could not acquire lock '${lockfile}'."
44
45 # Write out pid to the lockfile and make sure that
46 # nobody else can access it.
47 echo "$$" > ${lockfile}
48 chmod 600 ${lockfile}
49 }
50
51 function lock_release() {
52 local lockfile="${1}"
53
54 log DEBUG "Releasing lock '${lockfile}'."
55
56 # Remove the lockfile (okay if it does not exist).
57 rm -f ${lockfile}
58 }