commit 409acec79383a69542136b2f1393133ac44d701b Author: Paul Tagliamonte Date: Sun Sep 2 09:49:53 2012 -0400 Initial import diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3c8ed31 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +*swp +*.o +*.so* diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..1d2057f --- /dev/null +++ b/Makefile @@ -0,0 +1,22 @@ +include version.mk + +SUBDIRS = src lib +.PHONY: subdirs $(SUBDIRS) + +all: build + +lib: src + +subdirs: clean $(SUBDIRS) +$(SUBDIRS): + $(MAKE) -C $@ build + +clean: + rm -fv lib/*so* + make -C src clean + +build: subdirs + +install: subdirs + mkdir -p $(DESTDIR)/usr/include $(DESTDIR)/usr/lib + cp -r lib/*so* $(DESTDIR)/usr/lib/ diff --git a/include/open.h b/include/open.h new file mode 100644 index 0000000..f5ed913 --- /dev/null +++ b/include/open.h @@ -0,0 +1,34 @@ +/* + * Copyright (c) 2011 Paul Tagliamonte + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + * DEALINGS IN THE SOFTWARE. + */ + +#include + +#ifndef _OPEN_H_ +#define _OPEN_H_ I_CAN_HAS + +int open (const char * pathname, int flags, ...); + +int creat(const char *pathname, mode_t mode); +FILE *fopen(const char *path, const char *mode); +FILE *freopen(const char *path, const char *mode, FILE *stream); + +#endif diff --git a/lib/Makefile b/lib/Makefile new file mode 100644 index 0000000..e530fa9 --- /dev/null +++ b/lib/Makefile @@ -0,0 +1,7 @@ +include ../version.mk + +build: + ln -s libtmperamental.so.$(LIB_MAJOR).$(LIB_MINOR) \ + libtmperamental.so.$(LIB_MAJOR) + ln -s libtmperamental.so.$(LIB_MAJOR).$(LIB_MINOR) \ + libtmperamental.so diff --git a/src/Makefile b/src/Makefile new file mode 100644 index 0000000..073fc52 --- /dev/null +++ b/src/Makefile @@ -0,0 +1,27 @@ +include ../version.mk + +LIB=../lib/ +INC=../include/ + +TARGETS=$(shell ls *.c | sed s/.c$$/.o/g) +CFLAGS=-Wall -c -fPIC -I$(INC) # FIXME +CC=gcc # FIXME + +LDFLAGS=-shared \ + -Wl,--no-as-needed \ + -ldl \ + -Wl,-soname,$(LIB_NAME).so.$(LIB_MAJOR).$(LIB_MINOR) + +OUTPUT=$(LIB)$(LIB_NAME).so.$(LIB_MAJOR).$(LIB_MINOR) + +build: $(TARGETS) link + +clean: + rm -f *.o + rm -f $(OUTPUT) + +.c.o: + $(CC) $(CFLAGS) -c -o $@ $< + +link: + $(CC) $(LDFLAGS) $(TARGETS) -o $(OUTPUT) diff --git a/src/open.c b/src/open.c new file mode 100644 index 0000000..897cf38 --- /dev/null +++ b/src/open.c @@ -0,0 +1,82 @@ +/* + * Copyright (c) 2011 Paul Tagliamonte + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + * DEALINGS IN THE SOFTWARE. + */ + +#define _GNU_SOURCE 1 +/* This horseshit hack brought to you by the fine folks over at wherever + * the fuck this came from. This breaks in Cygwin. Not that that really + * matters in this small case. */ + +#include +#include +#include +#include +#include + +#include "open.h" + +void enforcer ( const char * pathname ) { + const char * to_check = "/tmp/"; + int len = strlen(to_check); + char * leading = malloc(sizeof(char) * len); + strncpy(leading, pathname, len); + if ( strcmp(to_check, leading) == 0 ) { + printf("tmperamental: caught a write to /tmp.\n"); + } +} + +int open ( const char * pathname, int flags, ... ) { + enforcer( pathname ); + + va_list v; + va_start(v, flags); + mode_t mode = va_arg(v, mode_t); + va_end(v); + if ( mode ) { + int (*orig_addr)(const char *, int, mode_t) = dlsym(RTLD_NEXT, "open"); + return orig_addr(pathname, flags, mode); + } else { + int (*orig_addr)(const char *, int) = dlsym(RTLD_NEXT, "open"); + return orig_addr(pathname, flags); + } +} + +int creat ( const char *pathname, mode_t mode ) { + enforcer( pathname ); + + int (*orig_addr)(const char *, mode_t) = dlsym(RTLD_NEXT, "creat"); + return orig_addr(pathname, mode); +} + +FILE * fopen ( const char * path, const char *mode ) { + enforcer( path ); + + FILE * (*orig_addr)(const char *, const char *) = dlsym(RTLD_NEXT, "fopen"); + return orig_addr(path, mode); +} + +FILE * freopen ( const char *path, const char *mode, FILE * stream ) { + enforcer( path ); + + FILE * (*orig_addr)(const char *, const char *, FILE *) + = dlsym(RTLD_NEXT, "freopen"); + return orig_addr(path, mode, stream); +} diff --git a/version.mk b/version.mk new file mode 100644 index 0000000..027650c --- /dev/null +++ b/version.mk @@ -0,0 +1,4 @@ +LIB_NAME = libtmperamental +LIB_MAJOR = 1 +LIB_MINOR = 0 +LIB_PATCH = 0