From 61b2bbe3eb265861265c1cf928e156e3c7052721 Mon Sep 17 00:00:00 2001 From: Ralph Ronnquist Date: Wed, 26 Oct 2022 14:33:06 +1100 Subject: [PATCH] debugging --- libtarmap-0.c | 43 ++++++++++++++++++++++++++++++------------- 1 file changed, 30 insertions(+), 13 deletions(-) diff --git a/libtarmap-0.c b/libtarmap-0.c index 89c0f4a..8625616 100644 --- a/libtarmap-0.c +++ b/libtarmap-0.c @@ -69,9 +69,11 @@ static FILE *tryopentar(const char *pathname) { // Override "openat" int openat(int dirfd, const char *pathname, int flags) { - static int (*real_openat)(int, const char*, int); - if ( real_openat == 0 ) { - real_openat = dlsym( libc6, "openat" ); + static int (*real_openat)(int, const char*, int) = 0; + if ( libc6 == 0 ) { + void *lib = dlopen( "libc.so.6", RTLD_LAZY ); + real_openat = dlsym( lib, "openat" ); + return real_openat( dirfd, pathname, flags ); } //fprintf( stderr, "openat( %s )\n", pathname ); if ( data.tarmap ) { @@ -85,9 +87,11 @@ int openat(int dirfd, const char *pathname, int flags) { // Override "open" int open(const char *pathname, int flags) { - static int (*real_open)(const char *pathname, int flags); - if ( real_open == 0 ) { - real_open = dlsym( libc6, "open" ); + static int (*real_open)(const char *pathname, int flags) = 0; + if ( libc6 == 0 ) { + void *lib = dlopen( "libc.so.6", RTLD_LAZY ); + real_open = dlsym( lib, "open" ); + return real_open( pathname, flags ); } //fprintf( stderr, "open( %s )\n", pathname ); if ( data.tarmap ) { @@ -101,9 +105,11 @@ int open(const char *pathname, int flags) { // Override "fopen" FILE *fopen(const char *pathname, const char *mode) { - static FILE *(*real_fopen)(const char *pathname, const char *mode); + static FILE *(*real_fopen)(const char *pathname, const char *mode) = 0; if ( real_fopen == 0 ) { - real_fopen = dlsym( libc6, "fopen" ); + void *lib = dlopen( "libc.so.6", RTLD_LAZY ); + real_fopen = dlsym( lib, "fopen" ); + return real_fopen( pathname, mode ); } if ( data.tarmap ) { FILE *file = tryopentar( pathname ); @@ -121,14 +127,17 @@ void so_init() { if ( libc6 ) { return; } - libc6 = dlopen( "libc.so.6", RTLD_LAZY ); char *tarfile = getenv( TARMAP ); if ( tarfile == 0 || *tarfile == 0 ) { //fprintf( stderr, "(libtarmap: no tar)\n" ); data.tarmap = 0; return; // Stop here for unset or cleared environment } - //fprintf( stderr, "libtarmap: tarfile = %s\n", tarfile ); + //fprintf( stderr, "libtarmap: %s\n", tarfile? tarfile : "(null)" ); + libc6 = dlopen( "libc.so.6", RTLD_LAZY ); + real_openat = dlsym( libc6, "openat" ); + real_open = dlsym( libc6, "open" ); + real_fopen = dlsym( libc6, "fopen" ); if ( ( data.tarmap = realpath( tarfile, 0 ) ) == 0 ) { // Cannot find the tar file .. that's total badness! perror( tarfile ); @@ -142,8 +151,17 @@ void so_init() { perror( data.tarmap ); exit( 1 ); } + int fd = fileno( file ); data.buffer = malloc( DATASZ ); - data.size = read( fileno( file ), data.buffer, DATASZ ); + char *p = data.buffer; + int max = DATASZ; + int n; + while ( ( n = read( fd, p, max ) ) > 0 ) { + data.size += n; + p += n; + max -= n; + } + //fprintf( stderr, "data.size = %d\n", data.size ); if ( data.size == 0 ) { perror( cmd ); // Not a tar or empty tar .. that's total badness! @@ -160,8 +178,7 @@ void so_init() { free( cmd ); cmd = 0; char *end = data.buffer + data.size; - char *p; - int n = 0; + n = 0; for ( p = data.buffer; p < end; p++ ) { if ( *p == '\n' ) { n++; -- 2.47.2