summaryrefslogtreecommitdiffstats
path: root/sysutils/pfSense-upgrade/files/pfSense-upgrade.wrapper
blob: 1c02471f27a3847de6339c342a77b5d1a7477add (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#!/bin/sh
#
# pfSense-upgrade.wrapper
#
# part of pfSense (https://www.pfsense.org)
# Copyright (c) 2015-2016 Rubicon Communications, LLC (Netgate)
# All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

pfsense_upgrade=$(realpath $(dirname $0)/../libexec/$(basename $0))
lockfile="/tmp/$(basename $0).lock"

if [ ! -f ${pfsense_upgrade} ]; then
	echo "ERROR: Unable to find ${pfsense_upgrade}"
	exit 1
fi

# lockf return codes, from sysexits(3)
EX_TEMPFAIL=75	# The specified lock file was already locked by another process.
EX_CANTCREAT=73	# The lockf utility was unable	to create the lock file, e.g.,
		# because of insufficient access privileges.
EX_OSERR=71	# A system call (e.g.,	fork(2)) failed unexpectedly.
EX_SOFTWARE=70	# The command did not exit normally, but may have been	signaled
		# or stopped.

# Special return code to detect when pfSense-upgrade was upgraded and must
# be called again to make sure latest version is running
EX_UPGRADE=99

while true; do
	unset run_again
	/usr/bin/lockf -s -t 5 ${lockfile} ${pfsense_upgrade} "$@"
	rc=$?

	unset error
	case "$rc" in
		${EX_TEMPFAIL})
			echo "Another instance is already running... Aborting!"
			exit 1
			;;
		${EX_CANTCREAT})
			echo "ERROR: Unable to create lockfile ${lockfile}"
			exit 1
			;;
		${EX_OSERR})
			echo "ERROR: An unexpected OS error happened"
			error=1
			;;
		${EX_SOFTWARE})
			echo "ERROR: An unexpected error happened"
			error=1
			;;
		${EX_UPGRADE})
			run_again=1
			;;
	esac

	[ -f "${lockfile}" ] \
		&& rm -f ${lockfile}

	[ -n "${error}" ] \
		&& exit 1

	[ -n "${run_again}" ] \
		&& continue

	exit $rc
done
OpenPOWER on IntegriCloud