- --- /dev/null 2022-04-19 22:28:32.062891741 +0200
- +++ pthread_interceptor.c 2022-07-06 13:47:43.337141261 +0200
- @@ -0,0 +1,86 @@
- +/*
- + * pthread_interceptor.c
- + *
- + * intercept pthread calls liek |pthread_mutex_init()| via LD_PRELOAD
- + *
- + * Written by Roland Mainz <roland.mainz@nrubsig.org>
- + */
- +
- +#define _GNU_SOURCE 1 /* needed for |RTLD_NEXT| */
- +
- +#include <pthread.h>
- +#include <unistd.h>
- +#include <errno.h>
- +#include <stdio.h>
- +#include <string.h>
- +#include <stdbool.h>
- +#include <dlfcn.h>
- +
- +/* config */
- +#define PICFG_PRINT_ONCE_MSG 1
- +#define PICFG_ENFORCE_PRIO_INHERIT 1
- +
- +/* misc */
- +#define STDERR_MSG(msg) \
- + (void)write(STDERR_FILENO, (msg), strlen(msg))
- +
- +
- +/*
- + * interceptor for |pthread_mutex_init()|
- + */
- +int pthread_mutex_init(pthread_mutex_t *restrict mutex,
- + const pthread_mutexattr_t *restrict attr) {
- + pthread_mutexattr_t *xattr = (pthread_mutexattr_t *)attr;
- + int pmi_res;
- + int pmi_errno;
- +
- +#if PICFG_PRINT_ONCE_MSG
- + static int once = 0;
- + const char *mymessage = "pthread_interceptor.so: "
- + "Once: In our own pthread_mutex_init()\n";
- + /* just print message ONCE */
- + if (once++ == 0)
- + STDERR_MSG(mymessage);
- +#endif /* PICFG_PRINT_ONCE_MSG */
- +
- + int (*original_pthread_mutex_init)(pthread_mutex_t *restrict mutex,
- + const pthread_mutexattr_t *restrict attr);
- + original_pthread_mutex_init = dlsym(RTLD_NEXT, "pthread_mutex_init");
- +
- + pthread_mutexattr_t myattr;
- + bool myattr_used = false;
- +
- + if (!xattr) {
- + (void)pthread_mutexattr_init(&myattr);
- + myattr_used = true;
- + xattr = &myattr;
- + }
- +
- +#if PICFG_ENFORCE_PRIO_INHERIT
- + int mutex_protocol = -666;
- + (void)pthread_mutexattr_getprotocol(xattr, &mutex_protocol);
- + if (mutex_protocol != PTHREAD_PRIO_INHERIT) {
- + char msgbuff[256]; /* fixed size, but no |malloc()| allowed here */
- +
- + (void)pthread_mutexattr_setprotocol(xattr, PTHREAD_PRIO_INHERIT);
- + (void)snprintf(msgbuff,
- + sizeof(msgbuff),
- + "pthread_interceptor.so: "
- + "PTHREAD_PRIO_INHERIT enforced "
- + "for mutex=0x%lx, was protocol=%d\n",
- + (unsigned long)(void *)mutex,
- + mutex_protocol);
- + STDERR_MSG(msgbuff);
- + }
- +#endif /* PICFG_ENFORCE_PRIO_INHERIT */
- +
- + pmi_res = (*original_pthread_mutex_init)(mutex, xattr);
- + pmi_errno = errno;
- +
- + if (myattr_used) {
- + (void)pthread_mutexattr_destroy(&myattr);
- + }
- +
- + errno = pmi_errno;
- + return pmi_res;
- +}
- --- /dev/null 2022-04-19 22:28:32.062891741 +0200
- +++ test1.c 2022-07-06 13:52:16.151772792 +0200
- @@ -0,0 +1,27 @@
- +/*
- + * test1 for pthread_interceptor.c
- + *
- + */
- +
- +#include <stdlib.h>
- +#include <stdio.h>
- +#include <pthread.h>
- +#include <errno.h>
- +
- +int main(int ac, char *av[])
- +{
- + (void)puts("# start.");
- +
- + pthread_mutex_t mutex1;
- + pthread_mutex_t mutex2;
- +
- + (void)pthread_mutex_init(&mutex1, NULL);
- + perror("pthread_mutex_init(1) result");
- +
- + (void)pthread_mutex_init(&mutex2, NULL);
- + perror("pthread_mutex_init(2) result");
- +
- + (void)puts("# end.");
- +
- + return EXIT_SUCCESS;
- +}
- --- /dev/null 2022-04-19 22:28:32.062891741 +0200
- +++ Makefile 2022-07-06 13:50:52.426969179 +0200
- @@ -0,0 +1,24 @@
- +#
- +# Makefile for LD_PRELOAD pthreadinterceptor
- +#
- +
- +CSTDFLAGS=-std=c11
- +CC=gcc # gcc or clang
- +
- +pthread_interceptor.o: pthread_interceptor.c
- + $(CC) $(CSTDFLAGS) -Wall -g pthread_interceptor.c -fPIC -c -o pthread_interceptor.o
- +pthread_interceptor.so: pthread_interceptor.o
- + $(CC) $(CSTDFLAGS) -Wall -g pthread_interceptor.o -fPIC -shared -ldl -lpthread -o pthread_interceptor.so
- +
- +test1: test1.c
- + $(CC) $(CSTDFLAGS) -Wall -g test1.c -o test1
- +
- +run_test: test1 pthread_interceptor.so
- + ksh93 -c 'LD_PRELOAD=$$PWD/pthread_interceptor.so ./test1'
- +
- +all: run_test
- +
- +clean:
- + rm -f *.o *.so test1
- +
- +# EOF.
Intercepting pthread_mutext_init() via LD_PRELOAD
Posted by Anonymous on Wed 6th Jul 2022 12:53
raw | new post
view followups (newest first): Intercepting pthread_mutext_init() via LD_PRELOAD by Anonymous
modification of post by Anonymous (view diff)
Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.