uboot: (firmwareOdroidC2/C4) don't invoke patch tool, use patches = [] instead
https://github.com/NixOS/nixpkgs/blob/master/pkgs/stdenv/generic/setup.sh#L948 this can do it nicely. Signed-off-by: Anton Arapov <anton@deadbeef.mx>
This commit is contained in:
commit
56de2bcd43
30691 changed files with 3076956 additions and 0 deletions
115
pkgs/build-support/libredirect/default.nix
Normal file
115
pkgs/build-support/libredirect/default.nix
Normal file
|
|
@ -0,0 +1,115 @@
|
|||
{ lib, stdenv, bintools-unwrapped, llvmPackages_13, coreutils }:
|
||||
|
||||
if stdenv.hostPlatform.isStatic
|
||||
then throw ''
|
||||
libredirect is not available on static builds.
|
||||
|
||||
Please fix your derivation to not depend on libredirect on static
|
||||
builds, using something like following:
|
||||
|
||||
nativeBuildInputs =
|
||||
lib.optional (!stdenv.buildPlatform.isStatic) libredirect;
|
||||
|
||||
and disable tests as necessary, although fixing tests to work without
|
||||
libredirect is even better.
|
||||
|
||||
libredirect uses LD_PRELOAD feature of dynamic loader and does not
|
||||
work on static builds where dynamic loader is not used.
|
||||
''
|
||||
else stdenv.mkDerivation rec {
|
||||
pname = "libredirect";
|
||||
version = "0";
|
||||
|
||||
unpackPhase = ''
|
||||
cp ${./libredirect.c} libredirect.c
|
||||
cp ${./test.c} test.c
|
||||
'';
|
||||
|
||||
outputs = ["out" "hook"];
|
||||
|
||||
libName = "libredirect" + stdenv.targetPlatform.extensions.sharedLibrary;
|
||||
|
||||
buildPhase = ''
|
||||
runHook preBuild
|
||||
|
||||
${if stdenv.isDarwin && stdenv.isAarch64 then ''
|
||||
# We need the unwrapped binutils and clang:
|
||||
# We also want to build a fat library with x86_64, arm64, arm64e in there.
|
||||
# Because we use the unwrapped tools, we need to provide -isystem for headers
|
||||
# and the library search directory for libdl.
|
||||
# We can't build this on x86_64, because the libSystem we point to doesn't
|
||||
# like arm64(e).
|
||||
PATH=${bintools-unwrapped}/bin:${llvmPackages_13.clang-unwrapped}/bin:$PATH \
|
||||
clang -arch x86_64 -arch arm64 -arch arm64e \
|
||||
-isystem ${llvmPackages_13.clang.libc}/include \
|
||||
-isystem ${llvmPackages_13.libclang.lib}/lib/clang/*/include \
|
||||
-L${llvmPackages_13.clang.libc}/lib \
|
||||
-Wl,-install_name,$libName \
|
||||
-Wall -std=c99 -O3 -fPIC libredirect.c \
|
||||
-ldl -shared -o "$libName"
|
||||
'' else if stdenv.isDarwin then ''
|
||||
$CC -Wall -std=c99 -O3 -fPIC libredirect.c \
|
||||
-Wl,-install_name,$out/lib/$libName \
|
||||
-ldl -shared -o "$libName"
|
||||
'' else ''
|
||||
$CC -Wall -std=c99 -O3 -fPIC libredirect.c \
|
||||
-ldl -shared -o "$libName"
|
||||
''}
|
||||
|
||||
if [ -n "$doInstallCheck" ]; then
|
||||
$CC -Wall -std=c99 \
|
||||
${lib.optionalString (!stdenv.isDarwin) "-D_GNU_SOURCE"} \
|
||||
-O3 test.c -o test
|
||||
fi
|
||||
|
||||
runHook postBuild
|
||||
'';
|
||||
|
||||
# We want to retain debugging info to be able to use GDB on libredirect.so
|
||||
# to more easily investigate which function overrides are missing or why
|
||||
# existing ones do not have the intended effect.
|
||||
dontStrip = true;
|
||||
|
||||
installPhase = ''
|
||||
runHook preInstall
|
||||
|
||||
install -vD "$libName" "$out/lib/$libName"
|
||||
|
||||
'' + lib.optionalString (stdenv.isDarwin && stdenv.isAarch64) ''
|
||||
# dylib will be rejected unless dylib rpath gets explictly set
|
||||
install_name_tool \
|
||||
-change $libName $out/lib/$libName \
|
||||
$out/lib/$libName
|
||||
'' + ''
|
||||
# Provide a setup hook that injects our library into every process.
|
||||
mkdir -p "$hook/nix-support"
|
||||
cat <<SETUP_HOOK > "$hook/nix-support/setup-hook"
|
||||
${if stdenv.isDarwin then ''
|
||||
export DYLD_INSERT_LIBRARIES="$out/lib/$libName"
|
||||
'' else ''
|
||||
export LD_PRELOAD="$out/lib/$libName"
|
||||
''}
|
||||
SETUP_HOOK
|
||||
|
||||
runHook postInstall
|
||||
'';
|
||||
|
||||
doInstallCheck = true;
|
||||
|
||||
installCheckPhase = ''
|
||||
(
|
||||
source "$hook/nix-support/setup-hook"
|
||||
NIX_REDIRECTS="/foo/bar/test=${coreutils}/bin/true:/bar/baz=$(mktemp -d)" ./test
|
||||
)
|
||||
'';
|
||||
|
||||
meta = with lib; {
|
||||
platforms = platforms.unix;
|
||||
description = "An LD_PRELOAD library to intercept and rewrite the paths in glibc calls";
|
||||
longDescription = ''
|
||||
libredirect is an LD_PRELOAD library to intercept and rewrite the paths in
|
||||
glibc calls based on the value of $NIX_REDIRECTS, a colon-separated list
|
||||
of path prefixes to be rewritten, e.g. "/src=/dst:/usr/=/nix/store/".
|
||||
'';
|
||||
};
|
||||
}
|
||||
477
pkgs/build-support/libredirect/libredirect.c
Normal file
477
pkgs/build-support/libredirect/libredirect.c
Normal file
|
|
@ -0,0 +1,477 @@
|
|||
#define _GNU_SOURCE
|
||||
#include <stdio.h>
|
||||
#include <stdarg.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <dlfcn.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <fcntl.h>
|
||||
#include <limits.h>
|
||||
#include <string.h>
|
||||
#include <spawn.h>
|
||||
#include <dirent.h>
|
||||
|
||||
#define MAX_REDIRECTS 128
|
||||
|
||||
#ifdef __APPLE__
|
||||
struct dyld_interpose {
|
||||
const void * replacement;
|
||||
const void * replacee;
|
||||
};
|
||||
#define WRAPPER(ret, name) static ret _libredirect_wrapper_##name
|
||||
#define LOOKUP_REAL(name) &name
|
||||
#define WRAPPER_DEF(name) \
|
||||
__attribute__((used)) static struct dyld_interpose _libredirect_interpose_##name \
|
||||
__attribute__((section("__DATA,__interpose"))) = { &_libredirect_wrapper_##name, &name };
|
||||
#else
|
||||
#define WRAPPER(ret, name) ret name
|
||||
#define LOOKUP_REAL(name) dlsym(RTLD_NEXT, #name)
|
||||
#define WRAPPER_DEF(name)
|
||||
#endif
|
||||
|
||||
static int nrRedirects = 0;
|
||||
static char * from[MAX_REDIRECTS];
|
||||
static char * to[MAX_REDIRECTS];
|
||||
|
||||
static int isInitialized = 0;
|
||||
|
||||
// FIXME: might run too late.
|
||||
static void init() __attribute__((constructor));
|
||||
|
||||
static void init()
|
||||
{
|
||||
if (isInitialized) return;
|
||||
|
||||
char * spec = getenv("NIX_REDIRECTS");
|
||||
if (!spec) return;
|
||||
|
||||
// Ensure we only run this code once.
|
||||
// We do not do `unsetenv("NIX_REDIRECTS")` to ensure that redirects
|
||||
// also get initialized for subprocesses.
|
||||
isInitialized = 1;
|
||||
|
||||
char * spec2 = malloc(strlen(spec) + 1);
|
||||
strcpy(spec2, spec);
|
||||
|
||||
char * pos = spec2, * eq;
|
||||
while ((eq = strchr(pos, '='))) {
|
||||
*eq = 0;
|
||||
from[nrRedirects] = pos;
|
||||
pos = eq + 1;
|
||||
to[nrRedirects] = pos;
|
||||
nrRedirects++;
|
||||
if (nrRedirects == MAX_REDIRECTS) break;
|
||||
char * end = strchr(pos, ':');
|
||||
if (!end) break;
|
||||
*end = 0;
|
||||
pos = end + 1;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
static const char * rewrite(const char * path, char * buf)
|
||||
{
|
||||
if (path == NULL) return path;
|
||||
for (int n = 0; n < nrRedirects; ++n) {
|
||||
int len = strlen(from[n]);
|
||||
if (strncmp(path, from[n], len) != 0) continue;
|
||||
if (snprintf(buf, PATH_MAX, "%s%s", to[n], path + len) >= PATH_MAX)
|
||||
abort();
|
||||
return buf;
|
||||
}
|
||||
|
||||
return path;
|
||||
}
|
||||
|
||||
static char * rewrite_non_const(char * path, char * buf)
|
||||
{
|
||||
// as long as the argument `path` is non-const, we can consider discarding
|
||||
// the const qualifier of the return value to be safe.
|
||||
return (char *)rewrite(path, buf);
|
||||
}
|
||||
|
||||
static int open_needs_mode(int flags)
|
||||
{
|
||||
#ifdef O_TMPFILE
|
||||
return (flags & O_CREAT) || (flags & O_TMPFILE) == O_TMPFILE;
|
||||
#else
|
||||
return flags & O_CREAT;
|
||||
#endif
|
||||
}
|
||||
|
||||
/* The following set of Glibc library functions is very incomplete -
|
||||
it contains only what we needed for programs in Nixpkgs. Just add
|
||||
more functions as needed. */
|
||||
|
||||
WRAPPER(int, open)(const char * path, int flags, ...)
|
||||
{
|
||||
int (*open_real) (const char *, int, mode_t) = LOOKUP_REAL(open);
|
||||
mode_t mode = 0;
|
||||
if (open_needs_mode(flags)) {
|
||||
va_list ap;
|
||||
va_start(ap, flags);
|
||||
mode = va_arg(ap, mode_t);
|
||||
va_end(ap);
|
||||
}
|
||||
char buf[PATH_MAX];
|
||||
return open_real(rewrite(path, buf), flags, mode);
|
||||
}
|
||||
WRAPPER_DEF(open)
|
||||
|
||||
// In musl libc, open64 is simply a macro for open
|
||||
#if !defined(__APPLE__) && !defined(open64)
|
||||
WRAPPER(int, open64)(const char * path, int flags, ...)
|
||||
{
|
||||
int (*open64_real) (const char *, int, mode_t) = LOOKUP_REAL(open64);
|
||||
mode_t mode = 0;
|
||||
if (open_needs_mode(flags)) {
|
||||
va_list ap;
|
||||
va_start(ap, flags);
|
||||
mode = va_arg(ap, mode_t);
|
||||
va_end(ap);
|
||||
}
|
||||
char buf[PATH_MAX];
|
||||
return open64_real(rewrite(path, buf), flags, mode);
|
||||
}
|
||||
WRAPPER_DEF(open64)
|
||||
#endif
|
||||
|
||||
WRAPPER(int, openat)(int dirfd, const char * path, int flags, ...)
|
||||
{
|
||||
int (*openat_real) (int, const char *, int, mode_t) = LOOKUP_REAL(openat);
|
||||
mode_t mode = 0;
|
||||
if (open_needs_mode(flags)) {
|
||||
va_list ap;
|
||||
va_start(ap, flags);
|
||||
mode = va_arg(ap, mode_t);
|
||||
va_end(ap);
|
||||
}
|
||||
char buf[PATH_MAX];
|
||||
return openat_real(dirfd, rewrite(path, buf), flags, mode);
|
||||
}
|
||||
WRAPPER_DEF(openat)
|
||||
|
||||
WRAPPER(FILE *, fopen)(const char * path, const char * mode)
|
||||
{
|
||||
FILE * (*fopen_real) (const char *, const char *) = LOOKUP_REAL(fopen);
|
||||
char buf[PATH_MAX];
|
||||
return fopen_real(rewrite(path, buf), mode);
|
||||
}
|
||||
WRAPPER_DEF(fopen)
|
||||
|
||||
#ifdef __GLIBC__
|
||||
WRAPPER(FILE *, __nss_files_fopen)(const char * path)
|
||||
{
|
||||
FILE * (*__nss_files_fopen_real) (const char *) = LOOKUP_REAL(__nss_files_fopen);
|
||||
char buf[PATH_MAX];
|
||||
return __nss_files_fopen_real(rewrite(path, buf));
|
||||
}
|
||||
WRAPPER_DEF(__nss_files_fopen)
|
||||
#endif
|
||||
|
||||
// In musl libc, fopen64 is simply a macro for fopen
|
||||
#if !defined(__APPLE__) && !defined(fopen64)
|
||||
WRAPPER(FILE *, fopen64)(const char * path, const char * mode)
|
||||
{
|
||||
FILE * (*fopen64_real) (const char *, const char *) = LOOKUP_REAL(fopen64);
|
||||
char buf[PATH_MAX];
|
||||
return fopen64_real(rewrite(path, buf), mode);
|
||||
}
|
||||
WRAPPER_DEF(fopen64)
|
||||
#endif
|
||||
|
||||
#ifdef __linux__
|
||||
WRAPPER(int, __xstat)(int ver, const char * path, struct stat * st)
|
||||
{
|
||||
int (*__xstat_real) (int ver, const char *, struct stat *) = LOOKUP_REAL(__xstat);
|
||||
char buf[PATH_MAX];
|
||||
return __xstat_real(ver, rewrite(path, buf), st);
|
||||
}
|
||||
WRAPPER_DEF(__xstat)
|
||||
#endif
|
||||
|
||||
#ifdef __linux__
|
||||
WRAPPER(int, __xstat64)(int ver, const char * path, struct stat64 * st)
|
||||
{
|
||||
int (*__xstat64_real) (int ver, const char *, struct stat64 *) = LOOKUP_REAL(__xstat64);
|
||||
char buf[PATH_MAX];
|
||||
return __xstat64_real(ver, rewrite(path, buf), st);
|
||||
}
|
||||
WRAPPER_DEF(__xstat64)
|
||||
#endif
|
||||
|
||||
WRAPPER(int, stat)(const char * path, struct stat * st)
|
||||
{
|
||||
int (*__stat_real) (const char *, struct stat *) = LOOKUP_REAL(stat);
|
||||
char buf[PATH_MAX];
|
||||
return __stat_real(rewrite(path, buf), st);
|
||||
}
|
||||
WRAPPER_DEF(stat)
|
||||
|
||||
WRAPPER(int, access)(const char * path, int mode)
|
||||
{
|
||||
int (*access_real) (const char *, int mode) = LOOKUP_REAL(access);
|
||||
char buf[PATH_MAX];
|
||||
return access_real(rewrite(path, buf), mode);
|
||||
}
|
||||
WRAPPER_DEF(access)
|
||||
|
||||
WRAPPER(int, posix_spawn)(pid_t * pid, const char * path,
|
||||
const posix_spawn_file_actions_t * file_actions,
|
||||
const posix_spawnattr_t * attrp,
|
||||
char * const argv[], char * const envp[])
|
||||
{
|
||||
int (*posix_spawn_real) (pid_t *, const char *,
|
||||
const posix_spawn_file_actions_t *,
|
||||
const posix_spawnattr_t *,
|
||||
char * const argv[], char * const envp[]) = LOOKUP_REAL(posix_spawn);
|
||||
char buf[PATH_MAX];
|
||||
return posix_spawn_real(pid, rewrite(path, buf), file_actions, attrp, argv, envp);
|
||||
}
|
||||
WRAPPER_DEF(posix_spawn)
|
||||
|
||||
WRAPPER(int, posix_spawnp)(pid_t * pid, const char * file,
|
||||
const posix_spawn_file_actions_t * file_actions,
|
||||
const posix_spawnattr_t * attrp,
|
||||
char * const argv[], char * const envp[])
|
||||
{
|
||||
int (*posix_spawnp_real) (pid_t *, const char *,
|
||||
const posix_spawn_file_actions_t *,
|
||||
const posix_spawnattr_t *,
|
||||
char * const argv[], char * const envp[]) = LOOKUP_REAL(posix_spawnp);
|
||||
char buf[PATH_MAX];
|
||||
return posix_spawnp_real(pid, rewrite(file, buf), file_actions, attrp, argv, envp);
|
||||
}
|
||||
WRAPPER_DEF(posix_spawnp)
|
||||
|
||||
WRAPPER(int, execv)(const char * path, char * const argv[])
|
||||
{
|
||||
int (*execv_real) (const char * path, char * const argv[]) = LOOKUP_REAL(execv);
|
||||
char buf[PATH_MAX];
|
||||
return execv_real(rewrite(path, buf), argv);
|
||||
}
|
||||
WRAPPER_DEF(execv)
|
||||
|
||||
WRAPPER(int, execvp)(const char * path, char * const argv[])
|
||||
{
|
||||
int (*_execvp) (const char *, char * const argv[]) = LOOKUP_REAL(execvp);
|
||||
char buf[PATH_MAX];
|
||||
return _execvp(rewrite(path, buf), argv);
|
||||
}
|
||||
WRAPPER_DEF(execvp)
|
||||
|
||||
WRAPPER(int, execve)(const char * path, char * const argv[], char * const envp[])
|
||||
{
|
||||
int (*_execve) (const char *, char * const argv[], char * const envp[]) = LOOKUP_REAL(execve);
|
||||
char buf[PATH_MAX];
|
||||
return _execve(rewrite(path, buf), argv, envp);
|
||||
}
|
||||
WRAPPER_DEF(execve)
|
||||
|
||||
WRAPPER(DIR *, opendir)(const char * path)
|
||||
{
|
||||
char buf[PATH_MAX];
|
||||
DIR * (*_opendir) (const char*) = LOOKUP_REAL(opendir);
|
||||
|
||||
return _opendir(rewrite(path, buf));
|
||||
}
|
||||
WRAPPER_DEF(opendir)
|
||||
|
||||
#define SYSTEM_CMD_MAX 512
|
||||
|
||||
static char * replace_substring(char * source, char * buf, char * replace_string, char * start_ptr, char * suffix_ptr) {
|
||||
char head[SYSTEM_CMD_MAX] = {0};
|
||||
strncpy(head, source, start_ptr - source);
|
||||
|
||||
char tail[SYSTEM_CMD_MAX] = {0};
|
||||
if(suffix_ptr < source + strlen(source)) {
|
||||
strcpy(tail, suffix_ptr);
|
||||
}
|
||||
|
||||
sprintf(buf, "%s%s%s", head, replace_string, tail);
|
||||
return buf;
|
||||
}
|
||||
|
||||
static char * replace_string(char * buf, char * from, char * to) {
|
||||
int num_matches = 0;
|
||||
char * matches[SYSTEM_CMD_MAX];
|
||||
int from_len = strlen(from);
|
||||
for(int i=0; i<strlen(buf); i++){
|
||||
char *cmp_start = buf + i;
|
||||
if(strncmp(from, cmp_start, from_len) == 0){
|
||||
matches[num_matches] = cmp_start;
|
||||
num_matches++;
|
||||
}
|
||||
}
|
||||
int len_diff = strlen(to) - strlen(from);
|
||||
for(int n = 0; n < num_matches; n++) {
|
||||
char replaced[SYSTEM_CMD_MAX];
|
||||
replace_substring(buf, replaced, to, matches[n], matches[n]+from_len);
|
||||
strcpy(buf, replaced);
|
||||
for(int nn = n+1; nn < num_matches; nn++) {
|
||||
matches[nn] += len_diff;
|
||||
}
|
||||
}
|
||||
return buf;
|
||||
}
|
||||
|
||||
static void rewriteSystemCall(const char * command, char * buf) {
|
||||
char * p = buf;
|
||||
|
||||
#ifdef __APPLE__
|
||||
// The dyld environment variable is not inherited by the subprocess spawned
|
||||
// by system(), so this hack redefines it.
|
||||
Dl_info info;
|
||||
dladdr(&rewriteSystemCall, &info);
|
||||
p = stpcpy(p, "export DYLD_INSERT_LIBRARIES=");
|
||||
p = stpcpy(p, info.dli_fname);
|
||||
p = stpcpy(p, ";");
|
||||
#endif
|
||||
|
||||
stpcpy(p, command);
|
||||
|
||||
for (int n = 0; n < nrRedirects; ++n) {
|
||||
replace_string(buf, from[n], to[n]);
|
||||
}
|
||||
}
|
||||
|
||||
WRAPPER(int, system)(const char *command)
|
||||
{
|
||||
int (*_system) (const char*) = LOOKUP_REAL(system);
|
||||
|
||||
char newCommand[SYSTEM_CMD_MAX];
|
||||
rewriteSystemCall(command, newCommand);
|
||||
return _system(newCommand);
|
||||
}
|
||||
WRAPPER_DEF(system)
|
||||
|
||||
WRAPPER(int, mkdir)(const char *path, mode_t mode)
|
||||
{
|
||||
int (*mkdir_real) (const char *path, mode_t mode) = LOOKUP_REAL(mkdir);
|
||||
char buf[PATH_MAX];
|
||||
return mkdir_real(rewrite(path, buf), mode);
|
||||
}
|
||||
WRAPPER_DEF(mkdir)
|
||||
|
||||
WRAPPER(int, mkdirat)(int dirfd, const char *path, mode_t mode)
|
||||
{
|
||||
int (*mkdirat_real) (int dirfd, const char *path, mode_t mode) = LOOKUP_REAL(mkdirat);
|
||||
char buf[PATH_MAX];
|
||||
return mkdirat_real(dirfd, rewrite(path, buf), mode);
|
||||
}
|
||||
WRAPPER_DEF(mkdirat)
|
||||
|
||||
WRAPPER(int, unlink)(const char *path)
|
||||
{
|
||||
int (*unlink_real) (const char *path) = LOOKUP_REAL(unlink);
|
||||
char buf[PATH_MAX];
|
||||
return unlink_real(rewrite(path, buf));
|
||||
}
|
||||
WRAPPER_DEF(unlink)
|
||||
|
||||
WRAPPER(int, unlinkat)(int dirfd, const char *path, int flags)
|
||||
{
|
||||
int (*unlinkat_real) (int dirfd, const char *path, int flags) = LOOKUP_REAL(unlinkat);
|
||||
char buf[PATH_MAX];
|
||||
return unlinkat_real(dirfd, rewrite(path, buf), flags);
|
||||
}
|
||||
WRAPPER_DEF(unlinkat)
|
||||
|
||||
WRAPPER(int, rmdir)(const char *path)
|
||||
{
|
||||
int (*rmdir_real) (const char *path) = LOOKUP_REAL(rmdir);
|
||||
char buf[PATH_MAX];
|
||||
return rmdir_real(rewrite(path, buf));
|
||||
}
|
||||
WRAPPER_DEF(rmdir)
|
||||
|
||||
static void copy_temp_wildcard(char * dest, char * src, int suffixlen) {
|
||||
int dest_len = strnlen(dest, PATH_MAX);
|
||||
int src_len = strnlen(src, PATH_MAX);
|
||||
memcpy(dest + dest_len - (6 + suffixlen), src + src_len - (6 + suffixlen), 6);
|
||||
}
|
||||
|
||||
WRAPPER(int, mkstemp)(char *template)
|
||||
{
|
||||
int (*mkstemp_real) (char *template) = LOOKUP_REAL(mkstemp);
|
||||
char buf[PATH_MAX];
|
||||
char * rewritten = rewrite_non_const(template, buf);
|
||||
int retval = mkstemp_real(rewritten);
|
||||
if (retval >= 0 && rewritten != template) {
|
||||
copy_temp_wildcard(template, rewritten, 0);
|
||||
}
|
||||
return retval;
|
||||
}
|
||||
WRAPPER_DEF(mkstemp)
|
||||
|
||||
WRAPPER(int, mkostemp)(char *template, int flags)
|
||||
{
|
||||
int (*mkostemp_real) (char *template, int flags) = LOOKUP_REAL(mkostemp);
|
||||
char buf[PATH_MAX];
|
||||
char * rewritten = rewrite_non_const(template, buf);
|
||||
int retval = mkostemp_real(rewritten, flags);
|
||||
if (retval >= 0 && rewritten != template) {
|
||||
copy_temp_wildcard(template, rewritten, 0);
|
||||
}
|
||||
return retval;
|
||||
}
|
||||
WRAPPER_DEF(mkostemp)
|
||||
|
||||
WRAPPER(int, mkstemps)(char *template, int suffixlen)
|
||||
{
|
||||
int (*mkstemps_real) (char *template, int suffixlen) = LOOKUP_REAL(mkstemps);
|
||||
char buf[PATH_MAX];
|
||||
char * rewritten = rewrite_non_const(template, buf);
|
||||
int retval = mkstemps_real(rewritten, suffixlen);
|
||||
if (retval >= 0 && rewritten != template) {
|
||||
copy_temp_wildcard(template, rewritten, suffixlen);
|
||||
}
|
||||
return retval;
|
||||
}
|
||||
WRAPPER_DEF(mkstemps)
|
||||
|
||||
WRAPPER(int, mkostemps)(char *template, int suffixlen, int flags)
|
||||
{
|
||||
int (*mkostemps_real) (char *template, int suffixlen, int flags) = LOOKUP_REAL(mkostemps);
|
||||
char buf[PATH_MAX];
|
||||
char * rewritten = rewrite_non_const(template, buf);
|
||||
int retval = mkostemps_real(rewritten, suffixlen, flags);
|
||||
if (retval >= 0 && rewritten != template) {
|
||||
copy_temp_wildcard(template, rewritten, suffixlen);
|
||||
}
|
||||
return retval;
|
||||
}
|
||||
WRAPPER_DEF(mkostemps)
|
||||
|
||||
WRAPPER(char *, mkdtemp)(char *template)
|
||||
{
|
||||
char * (*mkdtemp_real) (char *template) = LOOKUP_REAL(mkdtemp);
|
||||
char buf[PATH_MAX];
|
||||
char * rewritten = rewrite_non_const(template, buf);
|
||||
char * retval = mkdtemp_real(rewritten);
|
||||
if (retval == NULL) {
|
||||
return retval;
|
||||
};
|
||||
if (rewritten != template) {
|
||||
copy_temp_wildcard(template, rewritten, 0);
|
||||
}
|
||||
return template;
|
||||
}
|
||||
WRAPPER_DEF(mkdtemp)
|
||||
|
||||
WRAPPER(char *, mktemp)(char *template)
|
||||
{
|
||||
char * (*mktemp_real) (char *template) = LOOKUP_REAL(mktemp);
|
||||
char buf[PATH_MAX];
|
||||
char * rewritten = rewrite_non_const(template, buf);
|
||||
char * retval = mktemp_real(rewritten);
|
||||
if (retval == NULL) {
|
||||
return retval;
|
||||
};
|
||||
if (rewritten != template) {
|
||||
copy_temp_wildcard(template, rewritten, 0);
|
||||
}
|
||||
return template;
|
||||
}
|
||||
WRAPPER_DEF(mktemp)
|
||||
144
pkgs/build-support/libredirect/test.c
Normal file
144
pkgs/build-support/libredirect/test.c
Normal file
|
|
@ -0,0 +1,144 @@
|
|||
#include <assert.h>
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <limits.h>
|
||||
#include <spawn.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include <sys/stat.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/wait.h>
|
||||
|
||||
#define TESTDIR "/bar/baz"
|
||||
#define TESTPATH "/foo/bar/test"
|
||||
#define SUBTEST "./test sub"
|
||||
|
||||
extern char **environ;
|
||||
|
||||
void test_spawn(void) {
|
||||
pid_t pid;
|
||||
int ret;
|
||||
posix_spawn_file_actions_t file_actions;
|
||||
char *argv[] = {"true", NULL};
|
||||
|
||||
assert(posix_spawn_file_actions_init(&file_actions) == 0);
|
||||
|
||||
ret = posix_spawn(&pid, TESTPATH, &file_actions, NULL, argv, environ);
|
||||
|
||||
assert(ret == 0);
|
||||
assert(waitpid(pid, NULL, 0) != -1);
|
||||
}
|
||||
|
||||
void test_execv(void) {
|
||||
char *argv[] = {"true", NULL};
|
||||
assert(execv(TESTPATH, argv) == 0);
|
||||
}
|
||||
|
||||
void test_system(void) {
|
||||
assert(system(TESTPATH) == 0);
|
||||
}
|
||||
|
||||
void test_subprocess(void) {
|
||||
assert(system(SUBTEST) == 0);
|
||||
}
|
||||
|
||||
void assert_mktemp_path(
|
||||
const char * orig_prefix,
|
||||
const char * orig_suffix,
|
||||
const char * updated
|
||||
) {
|
||||
// prefix unchanged
|
||||
assert(strncmp(updated, orig_prefix, strlen(orig_prefix)) == 0);
|
||||
// wildcards replaced
|
||||
assert(strcmp(updated + strlen(orig_prefix), "XXXXXX") != 0);
|
||||
// suffix unchanged
|
||||
assert(strcmp(updated + strlen(orig_prefix) + 6, orig_suffix) == 0);
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
FILE *testfp;
|
||||
int testfd;
|
||||
struct stat testsb;
|
||||
char buf[PATH_MAX];
|
||||
|
||||
testfp = fopen(TESTPATH, "r");
|
||||
assert(testfp != NULL);
|
||||
fclose(testfp);
|
||||
|
||||
testfd = open(TESTPATH, O_RDONLY);
|
||||
assert(testfd != -1);
|
||||
close(testfd);
|
||||
|
||||
assert(access(TESTPATH, X_OK) == 0);
|
||||
|
||||
assert(stat(TESTPATH, &testsb) != -1);
|
||||
|
||||
assert(mkdir(TESTDIR "/dir-mkdir", 0777) == 0);
|
||||
assert(unlink(TESTDIR "/dir-mkdir") == -1); // it's a directory!
|
||||
#ifndef __APPLE__
|
||||
assert(errno == EISDIR);
|
||||
#endif
|
||||
assert(rmdir(TESTDIR "/dir-mkdir") == 0);
|
||||
assert(unlink(TESTDIR "/dir-mkdir") == -1);
|
||||
assert(errno == ENOENT);
|
||||
|
||||
assert(mkdirat(123, TESTDIR "/dir-mkdirat", 0777) == 0);
|
||||
assert(unlinkat(123, TESTDIR "/dir-mkdirat", 0) == -1); // it's a directory!
|
||||
#ifndef __APPLE__
|
||||
assert(errno == EISDIR);
|
||||
#endif
|
||||
assert(unlinkat(123, TESTDIR "/dir-mkdirat", AT_REMOVEDIR) == 0);
|
||||
|
||||
strncpy(buf, TESTDIR "/tempXXXXXX", PATH_MAX);
|
||||
testfd = mkstemp(buf);
|
||||
assert(testfd > 0);
|
||||
assert_mktemp_path(TESTDIR "/temp", "", buf);
|
||||
close(testfd);
|
||||
|
||||
strncpy(buf, TESTDIR "/tempXXXXXX", PATH_MAX);
|
||||
testfd = mkostemp(buf, 0);
|
||||
assert(testfd > 0);
|
||||
assert_mktemp_path(TESTDIR "/temp", "", buf);
|
||||
close(testfd);
|
||||
|
||||
strncpy(buf, TESTDIR "/tempXXXXXX.test", PATH_MAX);
|
||||
testfd = mkstemps(buf, strlen(".test"));
|
||||
assert(testfd > 0);
|
||||
assert_mktemp_path(TESTDIR "/temp", ".test", buf);
|
||||
close(testfd);
|
||||
|
||||
strncpy(buf, TESTDIR "/tempXXXXXX.test", PATH_MAX);
|
||||
testfd = mkostemps(buf, strlen(".test"), 0);
|
||||
assert(testfd > 0);
|
||||
assert_mktemp_path(TESTDIR "/temp", ".test", buf);
|
||||
close(testfd);
|
||||
|
||||
strncpy(buf, TESTDIR "/tempXXXXXX", PATH_MAX);
|
||||
assert(mkdtemp(buf) == buf);
|
||||
assert_mktemp_path(TESTDIR "/temp", "", buf);
|
||||
|
||||
strncpy(buf, TESTDIR "/tempXXXXXX", PATH_MAX);
|
||||
assert(mktemp(buf) == buf);
|
||||
assert_mktemp_path(TESTDIR "/temp", "", buf);
|
||||
|
||||
test_spawn();
|
||||
test_system();
|
||||
|
||||
// Only run subprocess if no arguments are given
|
||||
// as the subprocess will be called without argument
|
||||
// otherwise we will have infinite recursion
|
||||
if (argc == 1) {
|
||||
test_subprocess();
|
||||
}
|
||||
|
||||
test_execv();
|
||||
|
||||
/* If all goes well, this is never reached because test_execv() replaces
|
||||
* the current process.
|
||||
*/
|
||||
return 0;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue