Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

libredirect: Add support for Darwin #50246

Merged
merged 5 commits into from
Nov 12, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 30 additions & 8 deletions pkgs/build-support/libredirect/default.nix
Original file line number Diff line number Diff line change
@@ -1,19 +1,41 @@
{ stdenv }:
{ stdenv, coreutils }:

stdenv.mkDerivation {
name = "libredirect-0";

unpackPhase = "cp ${./libredirect.c} libredirect.c";
unpackPhase = ''
cp ${./libredirect.c} libredirect.c
cp ${./test.c} test.c
'';

buildPhase =
''
gcc -Wall -std=c99 -O3 -shared libredirect.c -o libredirect.so -fPIC -ldl
'';
shlibext = stdenv.targetPlatform.extensions.sharedLibrary;

buildPhase = ''
$CC -Wall -std=c99 -O3 -shared libredirect.c \
-o "libredirect$shlibext" -fPIC -ldl

if [ -n "$doInstallCheck" ]; then
$CC -Wall -std=c99 -O3 test.c -o test
fi
'';

installPhase = ''
install -vD "libredirect$shlibext" "$out/lib/libredirect$shlibext"
'';

doInstallCheck = true;

installPhase = "mkdir -p $out/lib; cp libredirect.so $out/lib";
installCheckPhase = if stdenv.isDarwin then ''
NIX_REDIRECTS="/foo/bar/test=${coreutils}/bin/true" \
DYLD_INSERT_LIBRARIES="$out/lib/libredirect$shlibext" \
DYLD_FORCE_FLAT_NAMESPACE=1 ./test
'' else ''
NIX_REDIRECTS="/foo/bar/test=${coreutils}/bin/true" \
LD_PRELOAD="$out/lib/libredirect$shlibext" ./test
'';

meta = {
platforms = stdenv.lib.platforms.linux;
platforms = stdenv.lib.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
Expand Down
59 changes: 59 additions & 0 deletions pkgs/build-support/libredirect/test.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#include <assert.h>
#include <fcntl.h>
#include <spawn.h>
#include <stdio.h>
#include <unistd.h>

#include <sys/stat.h>
#include <sys/types.h>
#include <sys/wait.h>

#define TESTPATH "/foo/bar/test"

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);
}

int main(void)
{
FILE *testfp;
int testfd;
struct stat testsb;

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);

test_spawn();
test_execv();

/* If all goes well, this is never reached because test_execv() replaces
* the current process.
*/
return 0;
}