2021-10-01 23:19:29 +01:00
|
|
|
|
# Assert that FILE exists and is executable
|
|
|
|
|
#
|
|
|
|
|
# assertExecutable FILE
|
|
|
|
|
assertExecutable() {
|
|
|
|
|
local file="$1"
|
|
|
|
|
[[ -f "$file" && -x "$file" ]] || \
|
|
|
|
|
die "Cannot wrap '$file' because it is not an executable file"
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-01 00:02:08 +01:00
|
|
|
|
# Generate a binary executable wrapper for wrapping an executable.
|
|
|
|
|
# The binary is compiled from generated C-code using gcc.
|
|
|
|
|
# makeBinaryWrapper EXECUTABLE OUT_PATH ARGS
|
|
|
|
|
|
|
|
|
|
# ARGS:
|
|
|
|
|
# --argv0 NAME : set name of executed process to NAME
|
2021-08-23 22:44:57 +01:00
|
|
|
|
# (otherwise it’s called …-wrapped)
|
2021-06-01 00:02:08 +01:00
|
|
|
|
# --set VAR VAL : add VAR with value VAL to the executable’s
|
|
|
|
|
# environment
|
|
|
|
|
# --set-default VAR VAL : like --set, but only adds VAR if not already set in
|
|
|
|
|
# the environment
|
|
|
|
|
# --unset VAR : remove VAR from the environment
|
2021-08-23 22:44:57 +01:00
|
|
|
|
# --add-flags FLAGS : add FLAGS to invocation of executable
|
|
|
|
|
|
|
|
|
|
# --prefix ENV SEP VAL : suffix/prefix ENV with VAL, separated by SEP
|
|
|
|
|
# --suffix
|
|
|
|
|
|
2021-06-01 00:02:08 +01:00
|
|
|
|
# To troubleshoot a binary wrapper after you compiled it,
|
|
|
|
|
# use the `strings` command or open the binary file in a text editor.
|
|
|
|
|
makeBinaryWrapper() {
|
2021-10-19 13:37:05 +01:00
|
|
|
|
assertExecutable "$1"
|
2021-12-02 01:07:17 +00:00
|
|
|
|
makeDocumentedCWrapper "$1" "${@:3}" | cc -Os -x c -o "$2" -
|
2021-06-01 00:02:08 +01:00
|
|
|
|
}
|
|
|
|
|
|
2021-10-01 23:20:25 +01:00
|
|
|
|
# Syntax: wrapProgramBinary <PROGRAM> <MAKE-WRAPPER FLAGS...>
|
|
|
|
|
wrapProgramBinary() {
|
|
|
|
|
local prog="$1"
|
|
|
|
|
local hidden
|
|
|
|
|
|
|
|
|
|
assertExecutable "$prog"
|
|
|
|
|
|
|
|
|
|
hidden="$(dirname "$prog")/.$(basename "$prog")"-wrapped
|
|
|
|
|
while [ -e "$hidden" ]; do
|
|
|
|
|
hidden="${hidden}_"
|
|
|
|
|
done
|
|
|
|
|
mv "$prog" "$hidden"
|
|
|
|
|
# Silence warning about unexpanded $0:
|
|
|
|
|
# shellcheck disable=SC2016
|
|
|
|
|
makeBinaryWrapper "$hidden" "$prog" --argv0 '$0' "${@:2}"
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-01 00:02:08 +01:00
|
|
|
|
# Generate source code for the wrapper in such a way that the wrapper source code
|
|
|
|
|
# will still be readable even after compilation
|
|
|
|
|
# makeDocumentedCWrapper EXECUTABLE ARGS
|
|
|
|
|
# ARGS: same as makeBinaryWrapper
|
|
|
|
|
makeDocumentedCWrapper() {
|
2021-10-01 16:43:23 +01:00
|
|
|
|
local src docs
|
|
|
|
|
src=$(makeCWrapper "$@")
|
|
|
|
|
docs=$(documentationString "$src")
|
|
|
|
|
printf '%s\n\n' "$src"
|
|
|
|
|
printf '%s\n' "$docs"
|
2021-06-01 00:02:08 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# makeCWrapper EXECUTABLE ARGS
|
|
|
|
|
# ARGS: same as makeBinaryWrapper
|
|
|
|
|
makeCWrapper() {
|
2021-10-01 17:51:09 +01:00
|
|
|
|
local argv0 n params cmd main flagsBefore flags executable params length
|
2021-12-01 22:27:26 +00:00
|
|
|
|
local uses_prefix uses_suffix uses_assert uses_assert_success uses_stdio uses_asprintf
|
2021-10-01 16:43:23 +01:00
|
|
|
|
executable=$(escapeStringLiteral "$1")
|
|
|
|
|
params=("$@")
|
2021-10-01 17:51:09 +01:00
|
|
|
|
length=${#params[*]}
|
|
|
|
|
for ((n = 1; n < length; n += 1)); do
|
2021-10-01 16:43:23 +01:00
|
|
|
|
p="${params[n]}"
|
|
|
|
|
case $p in
|
|
|
|
|
--set)
|
|
|
|
|
cmd=$(setEnv "${params[n + 1]}" "${params[n + 2]}")
|
|
|
|
|
main="$main $cmd"$'\n'
|
|
|
|
|
n=$((n + 2))
|
2021-10-04 13:29:22 +01:00
|
|
|
|
[ $n -ge "$length" ] && main="$main #error makeCWrapper: $p takes 2 arguments"$'\n'
|
2021-10-01 16:43:23 +01:00
|
|
|
|
;;
|
|
|
|
|
--set-default)
|
|
|
|
|
cmd=$(setDefaultEnv "${params[n + 1]}" "${params[n + 2]}")
|
|
|
|
|
main="$main $cmd"$'\n'
|
2021-12-01 22:27:26 +00:00
|
|
|
|
uses_stdio=1
|
2021-12-01 21:49:20 +00:00
|
|
|
|
uses_assert_success=1
|
2021-10-01 16:43:23 +01:00
|
|
|
|
n=$((n + 2))
|
2021-10-04 13:29:22 +01:00
|
|
|
|
[ $n -ge "$length" ] && main="$main #error makeCWrapper: $p takes 2 arguments"$'\n'
|
2021-10-01 16:43:23 +01:00
|
|
|
|
;;
|
|
|
|
|
--unset)
|
|
|
|
|
cmd=$(unsetEnv "${params[n + 1]}")
|
|
|
|
|
main="$main $cmd"$'\n'
|
2021-12-01 22:27:26 +00:00
|
|
|
|
uses_stdio=1
|
2021-12-01 21:49:20 +00:00
|
|
|
|
uses_assert_success=1
|
2021-10-01 16:43:23 +01:00
|
|
|
|
n=$((n + 1))
|
2021-10-04 13:29:22 +01:00
|
|
|
|
[ $n -ge "$length" ] && main="$main #error makeCWrapper: $p takes 1 argument"$'\n'
|
2021-10-01 16:43:23 +01:00
|
|
|
|
;;
|
|
|
|
|
--prefix)
|
|
|
|
|
cmd=$(setEnvPrefix "${params[n + 1]}" "${params[n + 2]}" "${params[n + 3]}")
|
|
|
|
|
main="$main $cmd"$'\n'
|
|
|
|
|
uses_prefix=1
|
2021-12-01 22:27:26 +00:00
|
|
|
|
uses_asprintf=1
|
|
|
|
|
uses_stdio=1
|
2021-12-01 21:49:20 +00:00
|
|
|
|
uses_assert_success=1
|
2021-11-09 01:55:26 +00:00
|
|
|
|
uses_assert=1
|
2021-10-01 16:43:23 +01:00
|
|
|
|
n=$((n + 3))
|
2021-10-04 13:29:22 +01:00
|
|
|
|
[ $n -ge "$length" ] && main="$main #error makeCWrapper: $p takes 3 arguments"$'\n'
|
2021-10-01 16:43:23 +01:00
|
|
|
|
;;
|
|
|
|
|
--suffix)
|
|
|
|
|
cmd=$(setEnvSuffix "${params[n + 1]}" "${params[n + 2]}" "${params[n + 3]}")
|
|
|
|
|
main="$main $cmd"$'\n'
|
|
|
|
|
uses_suffix=1
|
2021-12-01 22:27:26 +00:00
|
|
|
|
uses_asprintf=1
|
|
|
|
|
uses_stdio=1
|
2021-12-01 21:49:20 +00:00
|
|
|
|
uses_assert_success=1
|
2021-11-09 01:55:26 +00:00
|
|
|
|
uses_assert=1
|
2021-10-01 16:43:23 +01:00
|
|
|
|
n=$((n + 3))
|
2021-10-04 13:29:22 +01:00
|
|
|
|
[ $n -ge "$length" ] && main="$main #error makeCWrapper: $p takes 3 arguments"$'\n'
|
2021-10-01 16:43:23 +01:00
|
|
|
|
;;
|
|
|
|
|
--add-flags)
|
|
|
|
|
flags="${params[n + 1]}"
|
|
|
|
|
flagsBefore="$flagsBefore $flags"
|
2021-11-09 01:55:26 +00:00
|
|
|
|
uses_assert=1
|
2021-10-01 16:43:23 +01:00
|
|
|
|
n=$((n + 1))
|
2021-10-04 13:29:22 +01:00
|
|
|
|
[ $n -ge "$length" ] && main="$main #error makeCWrapper: $p takes 1 argument"$'\n'
|
2021-10-01 16:43:23 +01:00
|
|
|
|
;;
|
|
|
|
|
--argv0)
|
|
|
|
|
argv0=$(escapeStringLiteral "${params[n + 1]}")
|
|
|
|
|
n=$((n + 1))
|
2021-10-04 13:29:22 +01:00
|
|
|
|
[ $n -ge "$length" ] && main="$main #error makeCWrapper: $p takes 1 argument"$'\n'
|
2021-10-01 16:43:23 +01:00
|
|
|
|
;;
|
|
|
|
|
*) # Using an error macro, we will make sure the compiler gives an understandable error message
|
2021-10-04 13:29:22 +01:00
|
|
|
|
main="$main #error makeCWrapper: Uknown argument ${p}"$'\n'
|
2021-10-01 16:43:23 +01:00
|
|
|
|
;;
|
|
|
|
|
esac
|
2021-06-01 00:02:08 +01:00
|
|
|
|
done
|
2021-10-01 16:43:23 +01:00
|
|
|
|
# shellcheck disable=SC2086
|
2021-08-26 13:36:30 +01:00
|
|
|
|
[ -z "$flagsBefore" ] || main="$main"${main:+$'\n'}$(addFlags $flagsBefore)$'\n'$'\n'
|
2021-08-23 22:44:57 +01:00
|
|
|
|
main="$main argv[0] = \"${argv0:-${executable}}\";"$'\n'
|
|
|
|
|
main="$main return execv(\"${executable}\", argv);"$'\n'
|
|
|
|
|
|
2021-12-01 22:27:26 +00:00
|
|
|
|
[ -z "$uses_asprintf" ] || printf '%s\n' "#define _GNU_SOURCE /* See feature_test_macros(7) */"
|
2021-10-01 16:43:23 +01:00
|
|
|
|
printf '%s\n' "#include <unistd.h>"
|
|
|
|
|
printf '%s\n' "#include <stdlib.h>"
|
2021-12-01 22:27:26 +00:00
|
|
|
|
[ -z "$uses_assert" ] || printf '%s\n' "#include <assert.h>"
|
|
|
|
|
[ -z "$uses_stdio" ] || printf '%s\n' "#include <stdio.h>"
|
2021-12-01 21:56:18 +00:00
|
|
|
|
[ -z "$uses_assert_success" ] || printf '\n%s\n' "#define assert_success(e) do { if ((e) < 0) { perror(#e); abort(); } } while (0)"
|
2021-12-01 22:27:26 +00:00
|
|
|
|
[ -z "$uses_prefix" ] || printf '\n%s\n' "$(setEnvPrefixFn)"
|
|
|
|
|
[ -z "$uses_suffix" ] || printf '\n%s\n' "$(setEnvSuffixFn)"
|
2021-10-01 16:43:23 +01:00
|
|
|
|
printf '\n%s' "int main(int argc, char **argv) {"
|
|
|
|
|
printf '\n%s' "$main"
|
|
|
|
|
printf '%s\n' "}"
|
2021-08-23 22:44:57 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
addFlags() {
|
2021-10-01 16:43:23 +01:00
|
|
|
|
local result n flag flags var
|
|
|
|
|
var="argv_tmp"
|
2021-08-23 22:44:57 +01:00
|
|
|
|
flags=("$@")
|
|
|
|
|
for ((n = 0; n < ${#flags[*]}; n += 1)); do
|
2021-08-26 13:36:30 +01:00
|
|
|
|
flag=$(escapeStringLiteral "${flags[$n]}")
|
2021-10-01 16:43:23 +01:00
|
|
|
|
result="$result ${var}[$((n+1))] = \"$flag\";"$'\n'
|
2021-08-23 22:44:57 +01:00
|
|
|
|
done
|
2021-12-01 22:07:30 +00:00
|
|
|
|
printf ' %s\n' "char **$var = calloc($((n+1)) + argc, sizeof(*$var));"
|
2021-11-09 01:55:26 +00:00
|
|
|
|
printf ' %s\n' "assert($var != NULL);"
|
2021-10-01 16:43:23 +01:00
|
|
|
|
printf ' %s\n' "${var}[0] = argv[0];"
|
|
|
|
|
printf '%s' "$result"
|
|
|
|
|
printf ' %s\n' "for (int i = 1; i < argc; ++i) {"
|
|
|
|
|
printf ' %s\n' " ${var}[$n + i] = argv[i];"
|
|
|
|
|
printf ' %s\n' "}"
|
|
|
|
|
printf ' %s\n' "${var}[$n + argc] = NULL;"
|
|
|
|
|
printf ' %s\n' "argv = $var;"
|
2021-08-23 22:44:57 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# prefix ENV SEP VAL
|
|
|
|
|
setEnvPrefix() {
|
2021-10-01 16:43:23 +01:00
|
|
|
|
local env sep val
|
|
|
|
|
env=$(escapeStringLiteral "$1")
|
|
|
|
|
sep=$(escapeStringLiteral "$2")
|
|
|
|
|
val=$(escapeStringLiteral "$3")
|
|
|
|
|
printf '%s' "set_env_prefix(\"$env\", \"$sep\", \"$val\");"
|
2021-12-01 21:49:20 +00:00
|
|
|
|
assertValidEnvName "$1"
|
2021-08-23 22:44:57 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# suffix ENV SEP VAL
|
|
|
|
|
setEnvSuffix() {
|
2021-10-01 16:43:23 +01:00
|
|
|
|
local env sep val
|
|
|
|
|
env=$(escapeStringLiteral "$1")
|
|
|
|
|
sep=$(escapeStringLiteral "$2")
|
|
|
|
|
val=$(escapeStringLiteral "$3")
|
|
|
|
|
printf '%s' "set_env_suffix(\"$env\", \"$sep\", \"$val\");"
|
2021-12-01 21:49:20 +00:00
|
|
|
|
assertValidEnvName "$1"
|
2021-06-01 00:02:08 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# setEnv KEY VALUE
|
|
|
|
|
setEnv() {
|
2021-10-01 16:43:23 +01:00
|
|
|
|
local key value
|
|
|
|
|
key=$(escapeStringLiteral "$1")
|
|
|
|
|
value=$(escapeStringLiteral "$2")
|
|
|
|
|
printf '%s' "putenv(\"$key=$value\");"
|
2021-12-01 21:49:20 +00:00
|
|
|
|
assertValidEnvName "$1"
|
2021-06-01 00:02:08 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# setDefaultEnv KEY VALUE
|
|
|
|
|
setDefaultEnv() {
|
2021-10-01 16:43:23 +01:00
|
|
|
|
local key value
|
|
|
|
|
key=$(escapeStringLiteral "$1")
|
|
|
|
|
value=$(escapeStringLiteral "$2")
|
2021-12-01 21:49:20 +00:00
|
|
|
|
printf '%s' "assert_success(setenv(\"$key\", \"$value\", 0));"
|
|
|
|
|
assertValidEnvName "$1"
|
2021-06-01 00:02:08 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# unsetEnv KEY
|
|
|
|
|
unsetEnv() {
|
2021-10-01 16:43:23 +01:00
|
|
|
|
local key
|
|
|
|
|
key=$(escapeStringLiteral "$1")
|
2021-12-01 21:49:20 +00:00
|
|
|
|
printf '%s' "assert_success(unsetenv(\"$key\"));"
|
|
|
|
|
assertValidEnvName "$1"
|
2021-06-01 00:02:08 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# Put the entire source code into const char* SOURCE_CODE to make it readable after compilation.
|
|
|
|
|
# documentationString SOURCE_CODE
|
|
|
|
|
documentationString() {
|
2021-10-01 16:43:23 +01:00
|
|
|
|
local docs
|
|
|
|
|
docs=$(escapeStringLiteral $'\n----------\n// This binary wrapper was compiled from the following generated C-code:\n'"$1"$'\n----------\n')
|
|
|
|
|
printf '%s' "const char * SOURCE_CODE = \"$docs\";"
|
2021-06-01 00:02:08 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# Makes it safe to insert STRING within quotes in a C String Literal.
|
|
|
|
|
# escapeStringLiteral STRING
|
|
|
|
|
escapeStringLiteral() {
|
|
|
|
|
local result
|
|
|
|
|
result=${1//$'\\'/$'\\\\'}
|
|
|
|
|
result=${result//\"/'\"'}
|
|
|
|
|
result=${result//$'\n'/"\n"}
|
|
|
|
|
result=${result//$'\r'/"\r"}
|
2021-10-01 16:43:23 +01:00
|
|
|
|
printf '%s' "$result"
|
2021-06-01 00:02:08 +01:00
|
|
|
|
}
|
2021-08-23 22:44:57 +01:00
|
|
|
|
|
2021-12-01 21:49:20 +00:00
|
|
|
|
assertValidEnvName() {
|
|
|
|
|
case "$1" in
|
|
|
|
|
*=*) printf '\n%s\n' " #error Illegal environment variable name \`$1\` (cannot contain \`=\`)";;
|
|
|
|
|
"") printf '\n%s\n' " #error Environment variable name can't be empty.";;
|
|
|
|
|
esac
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-23 22:44:57 +01:00
|
|
|
|
setEnvPrefixFn() {
|
2021-10-01 16:43:23 +01:00
|
|
|
|
printf '%s' "\
|
2021-12-01 22:27:26 +00:00
|
|
|
|
void set_env_prefix(char *env, char *sep, char *prefix) {
|
2021-10-01 16:43:23 +01:00
|
|
|
|
char *existing = getenv(env);
|
2021-12-01 22:27:26 +00:00
|
|
|
|
if (existing) {
|
|
|
|
|
char *val;
|
|
|
|
|
assert_success(asprintf(&val, \"%s%s%s\", prefix, sep, existing));
|
|
|
|
|
assert_success(setenv(env, val, 1));
|
|
|
|
|
free(val);
|
|
|
|
|
} else {
|
|
|
|
|
assert_success(setenv(env, prefix, 1));
|
|
|
|
|
}
|
2021-10-01 16:43:23 +01:00
|
|
|
|
}
|
|
|
|
|
"
|
2021-08-23 22:44:57 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
setEnvSuffixFn() {
|
2021-10-01 16:43:23 +01:00
|
|
|
|
printf '%s' "\
|
2021-12-01 22:27:26 +00:00
|
|
|
|
void set_env_suffix(char *env, char *sep, char *suffix) {
|
2021-10-01 16:43:23 +01:00
|
|
|
|
char *existing = getenv(env);
|
2021-12-01 22:27:26 +00:00
|
|
|
|
if (existing) {
|
|
|
|
|
char *val;
|
|
|
|
|
assert_success(asprintf(&val, \"%s%s%s\", existing, sep, suffix));
|
|
|
|
|
assert_success(setenv(env, val, 1));
|
|
|
|
|
free(val);
|
|
|
|
|
} else {
|
|
|
|
|
assert_success(setenv(env, suffix, 1));
|
|
|
|
|
}
|
2021-10-01 16:43:23 +01:00
|
|
|
|
}
|
|
|
|
|
"
|
2021-08-23 22:44:57 +01:00
|
|
|
|
}
|