-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Expand file tree
/
Copy pathcontainer-sdk-rootdir-wrapper
More file actions
executable file
·186 lines (169 loc) · 6.62 KB
/
container-sdk-rootdir-wrapper
File metadata and controls
executable file
·186 lines (169 loc) · 6.62 KB
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#!/usr/bin/env sh
#
# Copyright (C) 2025 Igalia S.L.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
# ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
set -eu
SCRIPTPATH="$(realpath -- "${0}")"
SCRIPTDIR="$(dirname -- "${SCRIPTPATH}")"
WEBKITDIR="$(realpath -- "${SCRIPTDIR}/../..")"
WEBKIT_SOURCE_DIR="${WEBKIT_SOURCE_DIR:-${WEBKITDIR}}"
WEBKIT_SOURCE_DIR="$(realpath -m -- "${WEBKIT_SOURCE_DIR}")"
help() {
cat <<EOF
Usage: ${0} program-to-execute [arguments...]
${0} --create-symlink
This script is used by the WebKit tooling when running inside the
webkit-container-sdk for GTK/WPE port development. It enables building
WebKit and running tests inside a common path at /sdk/webkit.
Options:
-h, --help Show this help message
--create-symlink Create /sdk/webkit as a symlink to ${WEBKIT_SOURCE_DIR}
and exit. Useful for tools not executed through this
wrapper (like gdb) that need to resolve /sdk/webkit paths.
Default behavior:
Creates a private mount namespace where /sdk/webkit is a bind-mount
to ${WEBKIT_SOURCE_DIR}, then executes the specified program.
Examples:
${0} Tools/Scripts/build-webkit --wpe --release
${0} --create-symlink
EOF
exit 0
}
error() {
printf '%s\n' "${@}" 1>&2
exit 1
}
# If path starts with ${WEBKIT_SOURCE_DIR}, replace that prefix with /sdk/webkit.
maybe_replace_path_prefix_to_rootsdk() {
case "${1}" in
"${WEBKIT_SOURCE_DIR}"|"${WEBKIT_SOURCE_DIR}"/*)
printf '%s\n' "/sdk/webkit${1#"${WEBKIT_SOURCE_DIR}"}"
;;
*)
printf '%s\n' "${1}"
;;
esac
}
resolve_relpath_to_abs_if_outside() {
abs=$(realpath -m -- "${ORGPWD}/${1}")
case "${abs}" in
"${WEBKIT_SOURCE_DIR}"|"${WEBKIT_SOURCE_DIR}"/*)
# Inside the tree: keep original relative value
printf '%s\n' "${1}"
;;
*) # relative path outside the tree: make it absolute
printf '%s\n' "${abs}"
;;
esac
}
maybe_update_relpath_to_abs_if_outside() {
case "${1}" in
""|/*|*://*)
# do not touch if is empty or looks like an abs path or a URL
printf '%s\n' "${1}"
;;
.|..|./*|../*|*/*)
resolve_relpath_to_abs_if_outside "${1}"
;;
*)
if [ -L "${1}" ]; then
resolve_relpath_to_abs_if_outside "${1}"
else
printf '%s\n' "${1}"
fi
;;
esac
}
# Convert relative paths to absolute if they point outside ${WEBKIT_SOURCE_DIR}
args_maybe_update_outside_relpaths() {
for arg in "${@}"; do
case "${arg}" in
--*=*) # handle arguments like "--option=value"
key="${arg%%=*}"
val="${arg#*=}"
val="$(maybe_update_relpath_to_abs_if_outside "${val}")"
arg="${key}=${val}"
;;
*)
arg="$(maybe_update_relpath_to_abs_if_outside "${arg}")"
;;
esac
printf "'"
printf '%s' "${arg}" | sed "s/'/'\\\\''/g"
printf "' "
done
}
[ "$#" -ge 1 ] || help
[ "${1}" = "--help" ] && help
[ "${1}" = "-h" ] && help
[ -d "${WEBKIT_SOURCE_DIR}" ] || error "Can't find directory: ${WEBKIT_SOURCE_DIR}"
[ -d /sdk ] || error "Can't find /sdk directory"
[ -w /sdk ] || error "Directory /sdk is not writable for user $(whoami)"
case "${WEBKIT_SOURCE_DIR}" in
/sdk|/sdk/*) error "${0} should not be invoked with WEBKIT_SOURCE_DIR under /sdk directory" ;;
esac
if [ "${1}" = "--create-symlink" ]; then
[ "$#" -ge 2 ] && error "No extra arguments can be passed with --create-symlink"
if [ -L "/sdk/webkit" ]; then
CURSYMLINKDEST="$(realpath -m -- /sdk/webkit)"
if [ "${CURSYMLINKDEST}" != "${WEBKIT_SOURCE_DIR}" ]; then
rm -f "/sdk/webkit"
ln -s "${WEBKIT_SOURCE_DIR}" "/sdk/webkit"
echo "Symlink at /sdk/webkit changed from ${CURSYMLINKDEST} to ${WEBKIT_SOURCE_DIR}"
fi
elif ! [ -e "/sdk/webkit" ]; then
ln -s "${WEBKIT_SOURCE_DIR}" "/sdk/webkit"
else
error "ERROR: /sdk/webkit exists and is not a symlink. This is unexpected"
fi
exit 0
fi
CURUID="$(id -u)"
CURGID="$(id -g)"
ORGPWD="$(pwd)"
SDKPWD="$(maybe_replace_path_prefix_to_rootsdk "${ORGPWD}")"
# Try also with the realpath, just in case pwd is inside a symlinked dir.
if [ "${ORGPWD}" = "${SDKPWD}" ]; then
ORGPWD="$(realpath -- "${ORGPWD}")"
SDKPWD="$(maybe_replace_path_prefix_to_rootsdk "${ORGPWD}")"
fi
# When $(pwd) is inside ${WEBKIT_SOURCE_DIR}, switch (cd) to the equivalent bind-mount path
# under /sdk/webkit. Any relative path arguments that point outside ${WEBKIT_SOURCE_DIR} are
# converted to absolute paths first, since they wouldn't resolve correctly after the switch.
[ "${ORGPWD}" != "${SDKPWD}" ] && eval "set -- $(args_maybe_update_outside_relpaths "${@}")"
# Since util-linux 2.27 is not needed to execute mount --make-rprivate /
# because unshare does that automatically by default for new mount namespaces
exec unshare -rm sh -c '
set -eu
WEBKIT_SOURCE_DIR="${1}"
SDKPWD="${2}"
CURUID="${3}"
CURGID="${4}"
shift 4
mount -t tmpfs -o size=16M tmpfs /sdk
mkdir /sdk/webkit
mount --bind "${WEBKIT_SOURCE_DIR}" /sdk/webkit
cd "${SDKPWD}"
export WEBKIT_CONTAINER_SDK_INSIDE_MOUNT_NAMESPACE=1
exec unshare --map-user="${CURUID}" --map-group="${CURGID}" -- "${@}"
' -- "${WEBKIT_SOURCE_DIR}" "${SDKPWD}" "${CURUID}" "${CURGID}" "${@}"