debugging
authorRalph Ronnquist <ralph.ronnquist@gmail.com>
Wed, 26 Oct 2022 03:33:06 +0000 (14:33 +1100)
committerRalph Ronnquist <ralph.ronnquist@gmail.com>
Wed, 26 Oct 2022 03:33:06 +0000 (14:33 +1100)
libtarmap-0.c

index 89c0f4a337977e270278ae789dc965eda2efe82d..8625616493774fecfe50ce1556c2f81d41daa3b9 100644 (file)
@@ -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++;