-
Notifications
You must be signed in to change notification settings - Fork 0
/
gen_worker.sh
executable file
·156 lines (128 loc) · 3.91 KB
/
gen_worker.sh
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#!/bin/bash
export GK_WORKER_MASTER_PID=${BASHPID}
trap 'exit 1' SIGTERM
# Prevent aliases from causing portage to act inappropriately.
# Make sure it's before everything so we don't mess aliases that follow.
unalias -a
# Make sure this isn't exported to scripts we execute.
unset BASH_COMPAT
source "${GK_SHARE}"/gen_funcs.sh || exit 1
# Unset some variables that break things.
unset GZIP BZIP BZIP2 CDPATH GREP_OPTIONS GREP_COLOR GLOBIGNORE
die() {
set +x
if [ "$#" -gt '0' ]
then
print_error 1 "ERROR: ${1}"
fi
[[ -n "${GK_WORKER_MASTER_PID}" && ${BASHPID} == ${GK_WORKER_MASTER_PID} ]] || kill -s SIGTERM ${GK_WORKER_MASTER_PID}
exit 1
}
# Make sure genkernel's gen_die() won't be used -- make it an alias of
# this script's die function.
gen_die() {
die "$@"
}
# @FUNCTION: gkexec
# @USAGE: <command> [<pipestatus-to-check>]
# @DESCRIPTION:
# Executes command with support for genkernel's logging.
# Will die when command will exit with nonzero exit status.
#
# Genkernel has its own logfile and loglevel handling
# with things like color/nocolor support.
# To support this, we cannot just execute commands. Depending
# on loglevel for example, we maybe have to use pipes.
# To avoid writing complex statements each time, gkexec
# wrapper was created.
#
# <command> Command to execute.
#
# <pipestatus-to-check> By default, the first command's
# exit status will be checked. When executing multiple
# commands with pipes, this argument controls which
# command's exit status will be checked to decide if
# command has been successfully executed.
gkexec() {
if [ ${#} -gt 2 ]
then
# guard against ${array[@]}, first argument must be seen as a single word (${array[*]})
die "$(get_useful_function_stack "${FUNCNAME}")Invalid usage of ${FUNCNAME}(): Function takes at most three arguments (${#} given)!"
fi
local -a command=( "${1}" )
local pipes=${2:-0}
print_info 3 "COMMAND: ${command[@]}" 1 0 1
command+=( "$(catch_output_and_failures "Command '${command[@]}' failed!" ${pipes})" )
eval "${command[@]}"
}
# Prevent recursion.
unset -f cleanup gkbuild unpack
if [[ -s "${SANDBOX_LOG}" ]]
then
print_warning 3 "Stale sandbox log '${SANDBOX_LOG}' detected, removing ..."
# We use SANDBOX_LOG to check for sandbox violations,
# so we ensure that there can't be a stale log to
# interfere with our logic.
x=
if [[ -n ${SANDBOX_ON} ]]
then
x=${SANDBOX_ON}
export SANDBOX_ON=0
fi
rm -f "${SANDBOX_LOG}" \
|| die "Failed to remove stale sandbox log: '${SANDBOX_LOG}'!"
if [[ -n ${x} ]]
then
export SANDBOX_ON=${x}
fi
unset x
fi
__sb_append_var() {
local _v=$1 ; shift
local var="SANDBOX_${_v}"
[[ -z $1 || -n $2 ]] && die "Usage: add$(LC_ALL=C tr "[:upper:]" "[:lower:]" <<< "${_v}") <colon-delimited list of paths>"
export ${var}="${!var:+${!var}:}$1"
}
# addread() { __sb_append_var ${0#add} "$@" ; }
addread() { __sb_append_var READ "$@" ; }
addwrite() { __sb_append_var WRITE "$@" ; }
adddeny() { __sb_append_var DENY "$@" ; }
addpredict() { __sb_append_var PREDICT "$@" ; }
catch_output_and_failures() {
local error_msg=${1:-"Command failed!"}
local pipes=${2:-0}
local output_processor=
if [[ ${LOGLEVEL} -ge 4 ]]
then
output_processor="2>&1 | tee -a \"${LOGFILE}\"; [[ \${PIPESTATUS[${pipes}]} -ne 0 ]] && die \"${error_msg}\" || true"
else
output_processor=">> \"${LOGFILE}\" 2>&1 || die \"${error_msg}\""
fi
echo ${output_processor}
}
# the sandbox is ENABLED by default
export SANDBOX_ON=1
#if no perms are specified, dirs/files will have decent defaults
#(not secretive, but not stupid)
umask 022
if [[ "${#}" -lt 1 ]]
then
die 'No module specified!'
fi
case "${1}" in
build)
MODULE="${GK_SHARE}/worker_modules/gkbuild.sh"
;;
dropbear)
MODULE="${GK_SHARE}/worker_modules/dropbear.sh"
;;
unpack)
MODULE="${GK_SHARE}/worker_modules/unpack.sh"
;;
*)
die "Unknown module '${1}' specified!"
;;
esac
source "${MODULE}" || die "Failed to source '${MODULE}'!"
__module_main
exit $?