diff options
Diffstat (limited to 'libpinniped')
| -rw-r--r-- | libpinniped/Makefile | 28 | ||||
| -rw-r--r-- | libpinniped/pinniped.c | 64 | ||||
| -rw-r--r-- | libpinniped/test.c | 38 |
3 files changed, 130 insertions, 0 deletions
diff --git a/libpinniped/Makefile b/libpinniped/Makefile new file mode 100644 index 0000000..d867879 --- /dev/null +++ b/libpinniped/Makefile @@ -0,0 +1,28 @@ +# Copyright (c) 2014 Justus Winter <4winter@informatik.uni-hamburg.de> +# +# Permission to use, copy, modify, and distribute this software for any +# purpose with or without fee is hereby granted, provided that the above +# copyright notice and this permission notice appear in all copies. +# +# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES +# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF +# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR +# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES +# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN +# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF +# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + +USE_PKG = + +CFLAGS = -Wall -ggdb \ + $(foreach lib,$(USE_PKG),$(shell pkg-config --cflags $(lib))) +LDFLAGS = -rdynamic -ldl \ + $(foreach lib,$(USE_PKG),$(shell pkg-config --libs $(lib))) + +all: libpinniped.so + +check: libpinniped.so test + LD_PRELOAD=./"$<" ./test + +libpinniped.so: pinniped.o + gcc -shared -o "$@" "$<" -ldl diff --git a/libpinniped/pinniped.c b/libpinniped/pinniped.c new file mode 100644 index 0000000..348638f --- /dev/null +++ b/libpinniped/pinniped.c @@ -0,0 +1,64 @@ +/* + * Copyright (c) 2014 Justus Winter <4winter@informatik.uni-hamburg.de> + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#define _GNU_SOURCE +#include <dlfcn.h> +#include <errno.h> +#include <execinfo.h> +#include <mach.h> +#include <stdarg.h> +#include <stdio.h> +#include <string.h> + +static void +print_backtrace (const char *f, ...) +{ + va_list args; + + va_start (args, f); + vfprintf (stderr, f, args); + va_end (args); + + void *array[20]; + size_t size; + size = backtrace (array, 20); + backtrace_symbols_fd (&array[2], size - 2, 2); +} + +#define wrap(NAME) \ + kern_return_t \ + NAME (ipc_space_t task, mach_port_t name) \ + { \ + static kern_return_t (*f)(ipc_space_t, mach_port_t) = NULL; \ + if (! f) \ + f = dlsym (RTLD_NEXT, #NAME); \ + \ + kern_return_t r = f (task, name); \ + if (r != KERN_SUCCESS) \ + { \ + char buf[80]; \ + print_backtrace ("%s: " #NAME " (%d, %d): %s\n", \ + program_invocation_name, \ + task, name, \ + strerror_r (r, buf, sizeof buf)); \ + } \ + return r; \ + } + +wrap (mach_port_deallocate) +wrap (mach_port_destroy) + +#undef wrap diff --git a/libpinniped/test.c b/libpinniped/test.c new file mode 100644 index 0000000..b1f8939 --- /dev/null +++ b/libpinniped/test.c @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2014 Justus Winter <4winter@informatik.uni-hamburg.de> + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#define _GNU_SOURCE +#include <mach.h> + +void +dosth (void) +{ + mach_port_deallocate (mach_task_self (), 12345); +} + +void +dosthelse (void) +{ + mach_port_destroy (mach_task_self (), 54321); +} + +int +main () +{ + dosth (); + dosthelse (); + return 0; +} |
