This is the mail archive of the libc-alpha@sources.redhat.com mailing list for the glibc project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Question about wrappers and cleanup hooks


Hi,

I am trying to write a wrapper library that catches file access calls.
Basically it seems to work fairly well, except for an issue with pthread
cleanup handlers.

Basically if I wrap a function, the wrapped function's cleanup handler
is not called (easy to demonstrate with the NPTL tst-cancelx4 test):

-----
 # LD_PRELOAD=libopen-wrapper.so ./nptl/tst-cancelx4 | grep 'not called'
cleanup handler not called for 'open'
-----

Attached is above test case that can be compiled with:

-----
gcc -nostdlib -shared -fPIC -o libopen-wrapper.so open-wrapper.c -ldl
-----

Any comments/pointers will be appreciated.


Thanks,

-- 
Martin Schlemmer

#define _GNU_SOURCE
#define _REENTRANT

#include <dlfcn.h>
#include <errno.h>
#include <stdarg.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

static int (*true_open) (const char *, int, ...) = NULL;

int open(const char *name, int flags, ...) {
        va_list ap;
        mode_t mode = 0;
	void *libc_handle = RTLD_NEXT;
	char *dlerror_msg = NULL;
	int old_errno = errno;

	if (NULL == true_open) {
		dlerror();
		true_open = dlvsym(libc_handle, "open", "GLIBC_2.0");
		if (NULL == true_open) {
			dlerror_msg = dlerror();
			if (NULL != dlerror_msg)
				printf("%s\n", dlerror_msg);
			else
				printf("Error loading requested symbol!\n");
			exit(1);
		}
	}

        if (flags & O_CREAT) {
                va_start(ap, flags);
                mode = va_arg(ap, mode_t);
                va_end(ap);
        }

	errno = old_errno;

	return true_open(name, flags, mode);
}

Attachment: signature.asc
Description: This is a digitally signed message part


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]