git.iko.soy/typeable/nixpkgs
on master help sign in
Merge pull request #124157 from nh2/libredirect-children

libredirect: Fix redirects not working for subprocesses

commit1871c113f1d44f8e8922586b333e11d174bd19ca authorPascal Bach <pascal.bach@nextrem.ch> dateSat 4 Sep 2021, 13:19 (5 years ago) parent90982affc5196c
3 files changed+35−2
pkgs/build-support/libredirect/default.nix+13 pkgs/build-support/libredirect/libredirect.c+8−1 pkgs/build-support/libredirect/test.c+14−1
pkgs/build-support/libredirect/default.nix+13
@@ -14,6 +14,8 @@ stdenv.mkDerivation rec {
1414 outputs = ["out" "hook"];
1515
1616 buildPhase = ''
17+ runHook preBuild
18+
1719 $CC -Wall -std=c99 -O3 -fPIC -ldl -shared \
1820 ${lib.optionalString stdenv.isDarwin "-Wl,-install_name,$out/lib/$libName"} \
1921 -o "$libName" \
@@ -22,9 +24,18 @@ stdenv.mkDerivation rec {
2224 if [ -n "$doInstallCheck" ]; then
2325 $CC -Wall -std=c99 -O3 test.c -o test
2426 fi
27+
28+ runHook postBuild
2529 '';
2630
31+ # We want to retain debugging info to be able to use GDB on libredirect.so
32+ # to more easily investigate which function overrides are missing or why
33+ # existing ones do not have the intended effect.
34+ dontStrip = true;
35+
2736 installPhase = ''
37+ runHook preInstall
38+
2839 install -vD "$libName" "$out/lib/$libName"
2940
3041 mkdir -p "$hook/nix-support"
@@ -36,6 +47,8 @@ stdenv.mkDerivation rec {
3647 export LD_PRELOAD="$out/lib/$libName"
3748 ''}
3849 SETUP_HOOK
50+
51+ runHook postInstall
3952 '';
4053
4154 doInstallCheck = true;
pkgs/build-support/libredirect/libredirect.c+8−1
@@ -17,15 +17,22 @@ static int nrRedirects = 0;
1717static char * from[MAX_REDIRECTS];
1818static char * to[MAX_REDIRECTS];
1919
20+static int isInitialized = 0;
21+
2022// FIXME: might run too late.
2123static void init() __attribute__((constructor));
2224
2325static void init()
2426{
27+ if (isInitialized) return;
28+
2529 char * spec = getenv("NIX_REDIRECTS");
2630 if (!spec) return;
2731
28− unsetenv("NIX_REDIRECTS");
32+ // Ensure we only run this code once.
33+ // We do not do `unsetenv("NIX_REDIRECTS")` to ensure that redirects
34+ // also get initialized for subprocesses.
35+ isInitialized = 1;
2936
3037 char * spec2 = malloc(strlen(spec) + 1);
3138 strcpy(spec2, spec);
pkgs/build-support/libredirect/test.c+14−1
@@ -10,6 +10,7 @@
1010#include <sys/wait.h>
1111
1212#define TESTPATH "/foo/bar/test"
13+#define SUBTEST "./test sub"
1314
1415extern char **environ;
1516
@@ -36,7 +37,11 @@ void test_system(void) {
3637 assert(system(TESTPATH) == 0);
3738}
3839
39−int main(void)
40+void test_subprocess(void) {
41+ assert(system(SUBTEST) == 0);
42+}
43+
44+int main(int argc, char *argv[])
4045{
4146 FILE *testfp;
4247 int testfd;
@@ -56,6 +61,14 @@ int main(void)
5661
5762 test_spawn();
5863 test_system();
64+
65+ // Only run subprocess if no arguments are given
66+ // as the subprocess will be called without argument
67+ // otherwise we will have infinite recursion
68+ if (argc == 1) {
69+ test_subprocess();
70+ }
71+
5972 test_execv();
6073
6174 /* If all goes well, this is never reached because test_execv() replaces