From: Andreas Monzner Date: Wed, 18 Oct 2006 00:32:00 +0000 (+0000) Subject: add a small wrapper lib for call modified versions of open/open64/fopen/fopen64/socke... X-Git-Url: http://code.vuplus.com/gitweb/?p=vuplus_dvbapp;a=commitdiff_plain;h=3e12c9cf2b9c069a0ca431f907a272daa0cf3776;ds=sidebyside add a small wrapper lib for call modified versions of open/open64/fopen/fopen64/socket/pipe --- diff --git a/configure.ac b/configure.ac index 2dddd4c..38a3aba 100644 --- a/configure.ac +++ b/configure.ac @@ -7,6 +7,7 @@ TUXBOX_APPS_DIRECTORY AC_PROG_CC AC_PROG_CXX AC_PROG_RANLIB +AM_PROG_LIBTOOL AC_ARG_WITH(libsdl, AC_HELP_STRING([--with-libsdl], [use sdl, yes or no]), @@ -87,5 +88,6 @@ lib/service/Makefile lib/components/Makefile po/Makefile main/Makefile +tools/Makefile ]) #lib/python/Plugins/Extensions/SimpleRSS/Makefile diff --git a/tools/Makefile.am b/tools/Makefile.am new file mode 100644 index 0000000..ff74357 --- /dev/null +++ b/tools/Makefile.am @@ -0,0 +1,6 @@ +lib_LTLIBRARIES = libopen.la + +libopen_la_SOURCES = libopen.c + +install-exec-local: +$(LIBTOOL) --mode=install install libopen.la $(DESTDIR)/usr/lib \ No newline at end of file diff --git a/tools/libopen.c b/tools/libopen.c new file mode 100644 index 0000000..8725f55 --- /dev/null +++ b/tools/libopen.c @@ -0,0 +1,235 @@ +#define _GNU_SOURCE +#include +#include +#include +#include +#include +#include +#include + +#undef DEBUG + +int open64(const char *pathname, int flags, ...) +{ + static int (*libc_open64) (const char* pathname, int flags, ...); + int fd=-1; + if (!libc_open64) + { + void *handle; + char *error; + handle = dlopen("/lib/libc.so.6", RTLD_LAZY); + if (!handle) + { + fputs(dlerror(), stderr); + exit(1); + } + libc_open64 = dlsym(handle, "open64"); + if ((error = dlerror()) != NULL) { + fprintf(stderr, "%s\n", error); + exit(1); + } + } + fd = libc_open64(pathname, flags); + if (fd >= 0) + { + int fd_flags = fcntl(fd, F_GETFD, 0); + if (fd_flags >= 0) + { + fd_flags |= FD_CLOEXEC; + fcntl(fd, F_SETFD, fd_flags); + } +#ifdef DEBUG + fprintf(stdout, "open64 %s, flags %d returned fd %d\n", pathname, flags, fd); +#endif + } + return fd; +} + +int open(const char *pathname, int flags, ...) +{ + static int (*libc_open) (const char* pathname, int flags, ...); + int fd=-1; + if (!libc_open) + { + void *handle; + char *error; + handle = dlopen("/lib/libc.so.6", RTLD_LAZY); + if (!handle) + { + fputs(dlerror(), stderr); + exit(1); + } + libc_open = dlsym(handle, "open"); + if ((error = dlerror()) != NULL) { + fprintf(stderr, "%s\n", error); + exit(1); + } + } + fd = libc_open(pathname, flags); + if (fd >= 0) + { + int fd_flags = fcntl(fd, F_GETFD, 0); + if (fd_flags >= 0) + { + fd_flags |= FD_CLOEXEC; + fcntl(fd, F_SETFD, fd_flags); + } +#ifdef DEBUG + fprintf(stdout, "open %s, flags %d returned fd %d\n", pathname, flags, fd); +#endif + } + return fd; +} + +FILE *fopen64(const char *pathname, const char *mode) +{ + static FILE *(*libc_fopen64) (const char* pathname, const char *mode); + FILE *f=0; + if (!libc_fopen64) + { + void *handle; + char *error; + handle = dlopen("/lib/libc.so.6", RTLD_LAZY); + if (!handle) + { + fputs(dlerror(), stderr); + exit(1); + } + libc_fopen64 = dlsym(handle, "fopen64"); + if ((error = dlerror()) != NULL) { + fprintf(stderr, "%s\n", error); + exit(1); + } + } + f = libc_fopen64(pathname, mode); + if (f) + { + int fd = fileno(f); + int fd_flags = fcntl(fd, F_GETFD, 0); + if (fd_flags >= 0) + { + fd_flags |= FD_CLOEXEC; + fcntl(fd, F_SETFD, fd_flags); + } +#ifdef DEBUG + fprintf(stdout, "fopen64 %s, mode %s returned FILE* %p fd %d\n", pathname, mode, f, fd); +#endif + } + return f; +} + +FILE *fopen(const char *pathname, const char *mode) +{ + static FILE *(*libc_fopen) (const char* pathname, const char *mode); + FILE *f=0; + if (!libc_fopen) + { + void *handle; + char *error; + handle = dlopen("/lib/libc.so.6", RTLD_LAZY); + if (!handle) + { + fputs(dlerror(), stderr); + exit(1); + } + libc_fopen = dlsym(handle, "fopen"); + if ((error = dlerror()) != NULL) { + fprintf(stderr, "%s\n", error); + exit(1); + } + } + f = libc_fopen(pathname, mode); + if (f) + { + int fd = fileno(f); + int fd_flags = fcntl(fd, F_GETFD, 0); + if (fd_flags >= 0) + { + fd_flags |= FD_CLOEXEC; + fcntl(fd, F_SETFD, fd_flags); + } +#ifdef DEBUG + fprintf(stdout, "fopen %s, mode %s returned FILE* %p fd %d\n", pathname, mode, f, fd); +#endif + } + return f; +} + +int socket(int domain, int type, int protocol) +{ + static int (*libc_socket) (int domain, int type, int protocol); + int fd=-1; + if (!libc_socket) + { + void *handle; + char *error; + handle = dlopen("/lib/libc.so.6", RTLD_LAZY); + if (!handle) + { + fputs(dlerror(), stderr); + exit(1); + } + libc_socket = dlsym(handle, "socket"); + if ((error = dlerror()) != NULL) { + fprintf(stderr, "%s\n", error); + exit(1); + } + } + fd = libc_socket(domain, type, protocol); + if (fd >= 0) + { + int fd_flags = fcntl(fd, F_GETFD, 0); + if (fd_flags >= 0) + { + fd_flags |= FD_CLOEXEC; + fcntl(fd, F_SETFD, fd_flags); + } +#ifdef DEBUG + fprintf(stdout, "socket fd %d\n", fd); +#endif + } + return fd; +} + +int pipe(int modus[2]) +{ + static int (*libc_pipe) (int modus[2]); + int ret=-1; + if (!libc_pipe) + { + void *handle; + char *error; + handle = dlopen("/lib/libc.so.6", RTLD_LAZY); + if (!handle) + { + fputs(dlerror(), stderr); + exit(1); + } + libc_pipe = dlsym(handle, "pipe"); + if ((error = dlerror()) != NULL) { + fprintf(stderr, "%s\n", error); + exit(1); + } + } + ret = libc_pipe(modus); + if (!ret) + { + int fd_flags = fcntl(modus[0], F_GETFD, 0); + if (fd_flags >= 0) + { + fd_flags |= FD_CLOEXEC; + fcntl(modus[0], F_SETFD, fd_flags); + } + fd_flags = fcntl(modus[1], F_GETFD, 0); + if (fd_flags >= 0) + { + fd_flags |= FD_CLOEXEC; + fcntl(modus[1], F_SETFD, fd_flags); + } +#ifdef DEBUG + fprintf(stdout, "pipe fds[%d, %d]\n", modus[0], modus[1]); +#endif + } + return ret; +} +